blob: c900b532c21726adc5e4c9cca785f8898db5bbea [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalfunc.c: Builtin functions
12 */
13#define USING_FLOAT_STUFF
14
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef VMS
24# include <float.h>
25#endif
26
27#ifdef MACOS
28# include <time.h> /* for time_t */
29#endif
30
31static char *e_listarg = N_("E686: Argument of %s must be a List");
32#ifdef FEAT_QUICKFIX
33static char *e_stringreq = N_("E928: String required");
34#endif
35
36#ifdef FEAT_FLOAT
37static void f_abs(typval_T *argvars, typval_T *rettv);
38static void f_acos(typval_T *argvars, typval_T *rettv);
39#endif
40static void f_add(typval_T *argvars, typval_T *rettv);
41static void f_and(typval_T *argvars, typval_T *rettv);
42static void f_append(typval_T *argvars, typval_T *rettv);
43static void f_argc(typval_T *argvars, typval_T *rettv);
44static void f_argidx(typval_T *argvars, typval_T *rettv);
45static void f_arglistid(typval_T *argvars, typval_T *rettv);
46static void f_argv(typval_T *argvars, typval_T *rettv);
47static void f_assert_equal(typval_T *argvars, typval_T *rettv);
48static void f_assert_exception(typval_T *argvars, typval_T *rettv);
49static void f_assert_fails(typval_T *argvars, typval_T *rettv);
50static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaar61c04492016-07-23 15:35:35 +020051static void f_assert_inrange(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020052static void f_assert_match(typval_T *argvars, typval_T *rettv);
53static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
54static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar42205552017-03-18 19:42:22 +010055static void f_assert_report(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020056static void f_assert_true(typval_T *argvars, typval_T *rettv);
57#ifdef FEAT_FLOAT
58static void f_asin(typval_T *argvars, typval_T *rettv);
59static void f_atan(typval_T *argvars, typval_T *rettv);
60static void f_atan2(typval_T *argvars, typval_T *rettv);
61#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010062#ifdef FEAT_BEVAL
63static void f_balloon_show(typval_T *argvars, typval_T *rettv);
64#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020065static void f_browse(typval_T *argvars, typval_T *rettv);
66static void f_browsedir(typval_T *argvars, typval_T *rettv);
67static void f_bufexists(typval_T *argvars, typval_T *rettv);
68static void f_buflisted(typval_T *argvars, typval_T *rettv);
69static void f_bufloaded(typval_T *argvars, typval_T *rettv);
70static void f_bufname(typval_T *argvars, typval_T *rettv);
71static void f_bufnr(typval_T *argvars, typval_T *rettv);
72static void f_bufwinid(typval_T *argvars, typval_T *rettv);
73static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
74static void f_byte2line(typval_T *argvars, typval_T *rettv);
75static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
76static void f_byteidx(typval_T *argvars, typval_T *rettv);
77static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
78static void f_call(typval_T *argvars, typval_T *rettv);
79#ifdef FEAT_FLOAT
80static void f_ceil(typval_T *argvars, typval_T *rettv);
81#endif
82#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +010083static void f_ch_canread(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020084static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0874a832016-09-01 15:11:51 +020085static void f_ch_close_in(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020086static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
87static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
88static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
89static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
90static void f_ch_info(typval_T *argvars, typval_T *rettv);
91static void f_ch_log(typval_T *argvars, typval_T *rettv);
92static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
93static void f_ch_open(typval_T *argvars, typval_T *rettv);
94static void f_ch_read(typval_T *argvars, typval_T *rettv);
95static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
96static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
97static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
98static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
99static void f_ch_status(typval_T *argvars, typval_T *rettv);
100#endif
101static void f_changenr(typval_T *argvars, typval_T *rettv);
102static void f_char2nr(typval_T *argvars, typval_T *rettv);
103static void f_cindent(typval_T *argvars, typval_T *rettv);
104static void f_clearmatches(typval_T *argvars, typval_T *rettv);
105static void f_col(typval_T *argvars, typval_T *rettv);
106#if defined(FEAT_INS_EXPAND)
107static void f_complete(typval_T *argvars, typval_T *rettv);
108static void f_complete_add(typval_T *argvars, typval_T *rettv);
109static void f_complete_check(typval_T *argvars, typval_T *rettv);
110#endif
111static void f_confirm(typval_T *argvars, typval_T *rettv);
112static void f_copy(typval_T *argvars, typval_T *rettv);
113#ifdef FEAT_FLOAT
114static void f_cos(typval_T *argvars, typval_T *rettv);
115static void f_cosh(typval_T *argvars, typval_T *rettv);
116#endif
117static void f_count(typval_T *argvars, typval_T *rettv);
118static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
119static void f_cursor(typval_T *argsvars, typval_T *rettv);
120static void f_deepcopy(typval_T *argvars, typval_T *rettv);
121static void f_delete(typval_T *argvars, typval_T *rettv);
122static void f_did_filetype(typval_T *argvars, typval_T *rettv);
123static void f_diff_filler(typval_T *argvars, typval_T *rettv);
124static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
125static void f_empty(typval_T *argvars, typval_T *rettv);
126static void f_escape(typval_T *argvars, typval_T *rettv);
127static void f_eval(typval_T *argvars, typval_T *rettv);
128static void f_eventhandler(typval_T *argvars, typval_T *rettv);
129static void f_executable(typval_T *argvars, typval_T *rettv);
130static void f_execute(typval_T *argvars, typval_T *rettv);
131static void f_exepath(typval_T *argvars, typval_T *rettv);
132static void f_exists(typval_T *argvars, typval_T *rettv);
133#ifdef FEAT_FLOAT
134static void f_exp(typval_T *argvars, typval_T *rettv);
135#endif
136static void f_expand(typval_T *argvars, typval_T *rettv);
137static void f_extend(typval_T *argvars, typval_T *rettv);
138static void f_feedkeys(typval_T *argvars, typval_T *rettv);
139static void f_filereadable(typval_T *argvars, typval_T *rettv);
140static void f_filewritable(typval_T *argvars, typval_T *rettv);
141static void f_filter(typval_T *argvars, typval_T *rettv);
142static void f_finddir(typval_T *argvars, typval_T *rettv);
143static void f_findfile(typval_T *argvars, typval_T *rettv);
144#ifdef FEAT_FLOAT
145static void f_float2nr(typval_T *argvars, typval_T *rettv);
146static void f_floor(typval_T *argvars, typval_T *rettv);
147static void f_fmod(typval_T *argvars, typval_T *rettv);
148#endif
149static void f_fnameescape(typval_T *argvars, typval_T *rettv);
150static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
151static void f_foldclosed(typval_T *argvars, typval_T *rettv);
152static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
153static void f_foldlevel(typval_T *argvars, typval_T *rettv);
154static void f_foldtext(typval_T *argvars, typval_T *rettv);
155static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
156static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200157static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200158static void f_function(typval_T *argvars, typval_T *rettv);
159static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
160static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200161static void f_getbufinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200162static void f_getbufline(typval_T *argvars, typval_T *rettv);
163static void f_getbufvar(typval_T *argvars, typval_T *rettv);
164static void f_getchar(typval_T *argvars, typval_T *rettv);
165static void f_getcharmod(typval_T *argvars, typval_T *rettv);
166static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
167static void f_getcmdline(typval_T *argvars, typval_T *rettv);
168#if defined(FEAT_CMDL_COMPL)
169static void f_getcompletion(typval_T *argvars, typval_T *rettv);
170#endif
171static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
172static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
173static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
174static void f_getcwd(typval_T *argvars, typval_T *rettv);
175static void f_getfontname(typval_T *argvars, typval_T *rettv);
176static void f_getfperm(typval_T *argvars, typval_T *rettv);
177static void f_getfsize(typval_T *argvars, typval_T *rettv);
178static void f_getftime(typval_T *argvars, typval_T *rettv);
179static void f_getftype(typval_T *argvars, typval_T *rettv);
180static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200181static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200182static void f_getmatches(typval_T *argvars, typval_T *rettv);
183static void f_getpid(typval_T *argvars, typval_T *rettv);
184static void f_getcurpos(typval_T *argvars, typval_T *rettv);
185static void f_getpos(typval_T *argvars, typval_T *rettv);
186static void f_getqflist(typval_T *argvars, typval_T *rettv);
187static void f_getreg(typval_T *argvars, typval_T *rettv);
188static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200189static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200190static void f_gettabvar(typval_T *argvars, typval_T *rettv);
191static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200192static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200193static void f_getwinposx(typval_T *argvars, typval_T *rettv);
194static void f_getwinposy(typval_T *argvars, typval_T *rettv);
195static void f_getwinvar(typval_T *argvars, typval_T *rettv);
196static void f_glob(typval_T *argvars, typval_T *rettv);
197static void f_globpath(typval_T *argvars, typval_T *rettv);
198static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
199static void f_has(typval_T *argvars, typval_T *rettv);
200static void f_has_key(typval_T *argvars, typval_T *rettv);
201static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
202static void f_hasmapto(typval_T *argvars, typval_T *rettv);
203static void f_histadd(typval_T *argvars, typval_T *rettv);
204static void f_histdel(typval_T *argvars, typval_T *rettv);
205static void f_histget(typval_T *argvars, typval_T *rettv);
206static void f_histnr(typval_T *argvars, typval_T *rettv);
207static void f_hlID(typval_T *argvars, typval_T *rettv);
208static void f_hlexists(typval_T *argvars, typval_T *rettv);
209static void f_hostname(typval_T *argvars, typval_T *rettv);
210static void f_iconv(typval_T *argvars, typval_T *rettv);
211static void f_indent(typval_T *argvars, typval_T *rettv);
212static void f_index(typval_T *argvars, typval_T *rettv);
213static void f_input(typval_T *argvars, typval_T *rettv);
214static void f_inputdialog(typval_T *argvars, typval_T *rettv);
215static void f_inputlist(typval_T *argvars, typval_T *rettv);
216static void f_inputrestore(typval_T *argvars, typval_T *rettv);
217static void f_inputsave(typval_T *argvars, typval_T *rettv);
218static void f_inputsecret(typval_T *argvars, typval_T *rettv);
219static void f_insert(typval_T *argvars, typval_T *rettv);
220static void f_invert(typval_T *argvars, typval_T *rettv);
221static void f_isdirectory(typval_T *argvars, typval_T *rettv);
222static void f_islocked(typval_T *argvars, typval_T *rettv);
223#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
224static void f_isnan(typval_T *argvars, typval_T *rettv);
225#endif
226static void f_items(typval_T *argvars, typval_T *rettv);
227#ifdef FEAT_JOB_CHANNEL
228static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
229static void f_job_info(typval_T *argvars, typval_T *rettv);
230static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
231static void f_job_start(typval_T *argvars, typval_T *rettv);
232static void f_job_stop(typval_T *argvars, typval_T *rettv);
233static void f_job_status(typval_T *argvars, typval_T *rettv);
234#endif
235static void f_join(typval_T *argvars, typval_T *rettv);
236static void f_js_decode(typval_T *argvars, typval_T *rettv);
237static void f_js_encode(typval_T *argvars, typval_T *rettv);
238static void f_json_decode(typval_T *argvars, typval_T *rettv);
239static void f_json_encode(typval_T *argvars, typval_T *rettv);
240static void f_keys(typval_T *argvars, typval_T *rettv);
241static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
242static void f_len(typval_T *argvars, typval_T *rettv);
243static void f_libcall(typval_T *argvars, typval_T *rettv);
244static void f_libcallnr(typval_T *argvars, typval_T *rettv);
245static void f_line(typval_T *argvars, typval_T *rettv);
246static void f_line2byte(typval_T *argvars, typval_T *rettv);
247static void f_lispindent(typval_T *argvars, typval_T *rettv);
248static void f_localtime(typval_T *argvars, typval_T *rettv);
249#ifdef FEAT_FLOAT
250static void f_log(typval_T *argvars, typval_T *rettv);
251static void f_log10(typval_T *argvars, typval_T *rettv);
252#endif
253#ifdef FEAT_LUA
254static void f_luaeval(typval_T *argvars, typval_T *rettv);
255#endif
256static void f_map(typval_T *argvars, typval_T *rettv);
257static void f_maparg(typval_T *argvars, typval_T *rettv);
258static void f_mapcheck(typval_T *argvars, typval_T *rettv);
259static void f_match(typval_T *argvars, typval_T *rettv);
260static void f_matchadd(typval_T *argvars, typval_T *rettv);
261static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
262static void f_matcharg(typval_T *argvars, typval_T *rettv);
263static void f_matchdelete(typval_T *argvars, typval_T *rettv);
264static void f_matchend(typval_T *argvars, typval_T *rettv);
265static void f_matchlist(typval_T *argvars, typval_T *rettv);
266static void f_matchstr(typval_T *argvars, typval_T *rettv);
267static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
268static void f_max(typval_T *argvars, typval_T *rettv);
269static void f_min(typval_T *argvars, typval_T *rettv);
270#ifdef vim_mkdir
271static void f_mkdir(typval_T *argvars, typval_T *rettv);
272#endif
273static void f_mode(typval_T *argvars, typval_T *rettv);
274#ifdef FEAT_MZSCHEME
275static void f_mzeval(typval_T *argvars, typval_T *rettv);
276#endif
277static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
278static void f_nr2char(typval_T *argvars, typval_T *rettv);
279static void f_or(typval_T *argvars, typval_T *rettv);
280static void f_pathshorten(typval_T *argvars, typval_T *rettv);
281#ifdef FEAT_PERL
282static void f_perleval(typval_T *argvars, typval_T *rettv);
283#endif
284#ifdef FEAT_FLOAT
285static void f_pow(typval_T *argvars, typval_T *rettv);
286#endif
287static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
288static void f_printf(typval_T *argvars, typval_T *rettv);
289static void f_pumvisible(typval_T *argvars, typval_T *rettv);
290#ifdef FEAT_PYTHON3
291static void f_py3eval(typval_T *argvars, typval_T *rettv);
292#endif
293#ifdef FEAT_PYTHON
294static void f_pyeval(typval_T *argvars, typval_T *rettv);
295#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100296#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
297static void f_pyxeval(typval_T *argvars, typval_T *rettv);
298#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200299static void f_range(typval_T *argvars, typval_T *rettv);
300static void f_readfile(typval_T *argvars, typval_T *rettv);
301static void f_reltime(typval_T *argvars, typval_T *rettv);
302#ifdef FEAT_FLOAT
303static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
304#endif
305static void f_reltimestr(typval_T *argvars, typval_T *rettv);
306static void f_remote_expr(typval_T *argvars, typval_T *rettv);
307static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
308static void f_remote_peek(typval_T *argvars, typval_T *rettv);
309static void f_remote_read(typval_T *argvars, typval_T *rettv);
310static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100311static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200312static void f_remove(typval_T *argvars, typval_T *rettv);
313static void f_rename(typval_T *argvars, typval_T *rettv);
314static void f_repeat(typval_T *argvars, typval_T *rettv);
315static void f_resolve(typval_T *argvars, typval_T *rettv);
316static void f_reverse(typval_T *argvars, typval_T *rettv);
317#ifdef FEAT_FLOAT
318static void f_round(typval_T *argvars, typval_T *rettv);
319#endif
320static void f_screenattr(typval_T *argvars, typval_T *rettv);
321static void f_screenchar(typval_T *argvars, typval_T *rettv);
322static void f_screencol(typval_T *argvars, typval_T *rettv);
323static void f_screenrow(typval_T *argvars, typval_T *rettv);
324static void f_search(typval_T *argvars, typval_T *rettv);
325static void f_searchdecl(typval_T *argvars, typval_T *rettv);
326static void f_searchpair(typval_T *argvars, typval_T *rettv);
327static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
328static void f_searchpos(typval_T *argvars, typval_T *rettv);
329static void f_server2client(typval_T *argvars, typval_T *rettv);
330static void f_serverlist(typval_T *argvars, typval_T *rettv);
331static void f_setbufvar(typval_T *argvars, typval_T *rettv);
332static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
333static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
334static void f_setfperm(typval_T *argvars, typval_T *rettv);
335static void f_setline(typval_T *argvars, typval_T *rettv);
336static void f_setloclist(typval_T *argvars, typval_T *rettv);
337static void f_setmatches(typval_T *argvars, typval_T *rettv);
338static void f_setpos(typval_T *argvars, typval_T *rettv);
339static void f_setqflist(typval_T *argvars, typval_T *rettv);
340static void f_setreg(typval_T *argvars, typval_T *rettv);
341static void f_settabvar(typval_T *argvars, typval_T *rettv);
342static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
343static void f_setwinvar(typval_T *argvars, typval_T *rettv);
344#ifdef FEAT_CRYPT
345static void f_sha256(typval_T *argvars, typval_T *rettv);
346#endif /* FEAT_CRYPT */
347static void f_shellescape(typval_T *argvars, typval_T *rettv);
348static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
349static void f_simplify(typval_T *argvars, typval_T *rettv);
350#ifdef FEAT_FLOAT
351static void f_sin(typval_T *argvars, typval_T *rettv);
352static void f_sinh(typval_T *argvars, typval_T *rettv);
353#endif
354static void f_sort(typval_T *argvars, typval_T *rettv);
355static void f_soundfold(typval_T *argvars, typval_T *rettv);
356static void f_spellbadword(typval_T *argvars, typval_T *rettv);
357static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
358static void f_split(typval_T *argvars, typval_T *rettv);
359#ifdef FEAT_FLOAT
360static void f_sqrt(typval_T *argvars, typval_T *rettv);
361static void f_str2float(typval_T *argvars, typval_T *rettv);
362#endif
363static void f_str2nr(typval_T *argvars, typval_T *rettv);
364static void f_strchars(typval_T *argvars, typval_T *rettv);
365#ifdef HAVE_STRFTIME
366static void f_strftime(typval_T *argvars, typval_T *rettv);
367#endif
368static void f_strgetchar(typval_T *argvars, typval_T *rettv);
369static void f_stridx(typval_T *argvars, typval_T *rettv);
370static void f_string(typval_T *argvars, typval_T *rettv);
371static void f_strlen(typval_T *argvars, typval_T *rettv);
372static void f_strcharpart(typval_T *argvars, typval_T *rettv);
373static void f_strpart(typval_T *argvars, typval_T *rettv);
374static void f_strridx(typval_T *argvars, typval_T *rettv);
375static void f_strtrans(typval_T *argvars, typval_T *rettv);
376static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
377static void f_strwidth(typval_T *argvars, typval_T *rettv);
378static void f_submatch(typval_T *argvars, typval_T *rettv);
379static void f_substitute(typval_T *argvars, typval_T *rettv);
380static void f_synID(typval_T *argvars, typval_T *rettv);
381static void f_synIDattr(typval_T *argvars, typval_T *rettv);
382static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
383static void f_synstack(typval_T *argvars, typval_T *rettv);
384static void f_synconcealed(typval_T *argvars, typval_T *rettv);
385static void f_system(typval_T *argvars, typval_T *rettv);
386static void f_systemlist(typval_T *argvars, typval_T *rettv);
387static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
388static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
389static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
390static void f_taglist(typval_T *argvars, typval_T *rettv);
391static void f_tagfiles(typval_T *argvars, typval_T *rettv);
392static void f_tempname(typval_T *argvars, typval_T *rettv);
393static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
394static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100395static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200396static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100397static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200398#ifdef FEAT_JOB_CHANNEL
399static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
400#endif
401static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
402#ifdef FEAT_JOB_CHANNEL
403static void f_test_null_job(typval_T *argvars, typval_T *rettv);
404#endif
405static void f_test_null_list(typval_T *argvars, typval_T *rettv);
406static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
407static void f_test_null_string(typval_T *argvars, typval_T *rettv);
408static void f_test_settime(typval_T *argvars, typval_T *rettv);
409#ifdef FEAT_FLOAT
410static void f_tan(typval_T *argvars, typval_T *rettv);
411static void f_tanh(typval_T *argvars, typval_T *rettv);
412#endif
413#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200414static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200415static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200416static void f_timer_start(typval_T *argvars, typval_T *rettv);
417static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200418static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200419#endif
420static void f_tolower(typval_T *argvars, typval_T *rettv);
421static void f_toupper(typval_T *argvars, typval_T *rettv);
422static void f_tr(typval_T *argvars, typval_T *rettv);
423#ifdef FEAT_FLOAT
424static void f_trunc(typval_T *argvars, typval_T *rettv);
425#endif
426static void f_type(typval_T *argvars, typval_T *rettv);
427static void f_undofile(typval_T *argvars, typval_T *rettv);
428static void f_undotree(typval_T *argvars, typval_T *rettv);
429static void f_uniq(typval_T *argvars, typval_T *rettv);
430static void f_values(typval_T *argvars, typval_T *rettv);
431static void f_virtcol(typval_T *argvars, typval_T *rettv);
432static void f_visualmode(typval_T *argvars, typval_T *rettv);
433static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
434static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
435static void f_win_getid(typval_T *argvars, typval_T *rettv);
436static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
437static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
438static void f_win_id2win(typval_T *argvars, typval_T *rettv);
439static void f_winbufnr(typval_T *argvars, typval_T *rettv);
440static void f_wincol(typval_T *argvars, typval_T *rettv);
441static void f_winheight(typval_T *argvars, typval_T *rettv);
442static void f_winline(typval_T *argvars, typval_T *rettv);
443static void f_winnr(typval_T *argvars, typval_T *rettv);
444static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
445static void f_winrestview(typval_T *argvars, typval_T *rettv);
446static void f_winsaveview(typval_T *argvars, typval_T *rettv);
447static void f_winwidth(typval_T *argvars, typval_T *rettv);
448static void f_writefile(typval_T *argvars, typval_T *rettv);
449static void f_wordcount(typval_T *argvars, typval_T *rettv);
450static void f_xor(typval_T *argvars, typval_T *rettv);
451
452/*
453 * Array with names and number of arguments of all internal functions
454 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
455 */
456static struct fst
457{
458 char *f_name; /* function name */
459 char f_min_argc; /* minimal number of arguments */
460 char f_max_argc; /* maximal number of arguments */
461 void (*f_func)(typval_T *args, typval_T *rvar);
462 /* implementation of function */
463} functions[] =
464{
465#ifdef FEAT_FLOAT
466 {"abs", 1, 1, f_abs},
467 {"acos", 1, 1, f_acos}, /* WJMc */
468#endif
469 {"add", 2, 2, f_add},
470 {"and", 2, 2, f_and},
471 {"append", 2, 2, f_append},
472 {"argc", 0, 0, f_argc},
473 {"argidx", 0, 0, f_argidx},
474 {"arglistid", 0, 2, f_arglistid},
475 {"argv", 0, 1, f_argv},
476#ifdef FEAT_FLOAT
477 {"asin", 1, 1, f_asin}, /* WJMc */
478#endif
479 {"assert_equal", 2, 3, f_assert_equal},
480 {"assert_exception", 1, 2, f_assert_exception},
481 {"assert_fails", 1, 2, f_assert_fails},
482 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100483 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200484 {"assert_match", 2, 3, f_assert_match},
485 {"assert_notequal", 2, 3, f_assert_notequal},
486 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100487 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200488 {"assert_true", 1, 2, f_assert_true},
489#ifdef FEAT_FLOAT
490 {"atan", 1, 1, f_atan},
491 {"atan2", 2, 2, f_atan2},
492#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100493#ifdef FEAT_BEVAL
494 {"balloon_show", 1, 1, f_balloon_show},
495#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200496 {"browse", 4, 4, f_browse},
497 {"browsedir", 2, 2, f_browsedir},
498 {"bufexists", 1, 1, f_bufexists},
499 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
500 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
501 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
502 {"buflisted", 1, 1, f_buflisted},
503 {"bufloaded", 1, 1, f_bufloaded},
504 {"bufname", 1, 1, f_bufname},
505 {"bufnr", 1, 2, f_bufnr},
506 {"bufwinid", 1, 1, f_bufwinid},
507 {"bufwinnr", 1, 1, f_bufwinnr},
508 {"byte2line", 1, 1, f_byte2line},
509 {"byteidx", 2, 2, f_byteidx},
510 {"byteidxcomp", 2, 2, f_byteidxcomp},
511 {"call", 2, 3, f_call},
512#ifdef FEAT_FLOAT
513 {"ceil", 1, 1, f_ceil},
514#endif
515#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100516 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200517 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200518 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200519 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
520 {"ch_evalraw", 2, 3, f_ch_evalraw},
521 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
522 {"ch_getjob", 1, 1, f_ch_getjob},
523 {"ch_info", 1, 1, f_ch_info},
524 {"ch_log", 1, 2, f_ch_log},
525 {"ch_logfile", 1, 2, f_ch_logfile},
526 {"ch_open", 1, 2, f_ch_open},
527 {"ch_read", 1, 2, f_ch_read},
528 {"ch_readraw", 1, 2, f_ch_readraw},
529 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
530 {"ch_sendraw", 2, 3, f_ch_sendraw},
531 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200532 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200533#endif
534 {"changenr", 0, 0, f_changenr},
535 {"char2nr", 1, 2, f_char2nr},
536 {"cindent", 1, 1, f_cindent},
537 {"clearmatches", 0, 0, f_clearmatches},
538 {"col", 1, 1, f_col},
539#if defined(FEAT_INS_EXPAND)
540 {"complete", 2, 2, f_complete},
541 {"complete_add", 1, 1, f_complete_add},
542 {"complete_check", 0, 0, f_complete_check},
543#endif
544 {"confirm", 1, 4, f_confirm},
545 {"copy", 1, 1, f_copy},
546#ifdef FEAT_FLOAT
547 {"cos", 1, 1, f_cos},
548 {"cosh", 1, 1, f_cosh},
549#endif
550 {"count", 2, 4, f_count},
551 {"cscope_connection",0,3, f_cscope_connection},
552 {"cursor", 1, 3, f_cursor},
553 {"deepcopy", 1, 2, f_deepcopy},
554 {"delete", 1, 2, f_delete},
555 {"did_filetype", 0, 0, f_did_filetype},
556 {"diff_filler", 1, 1, f_diff_filler},
557 {"diff_hlID", 2, 2, f_diff_hlID},
558 {"empty", 1, 1, f_empty},
559 {"escape", 2, 2, f_escape},
560 {"eval", 1, 1, f_eval},
561 {"eventhandler", 0, 0, f_eventhandler},
562 {"executable", 1, 1, f_executable},
563 {"execute", 1, 2, f_execute},
564 {"exepath", 1, 1, f_exepath},
565 {"exists", 1, 1, f_exists},
566#ifdef FEAT_FLOAT
567 {"exp", 1, 1, f_exp},
568#endif
569 {"expand", 1, 3, f_expand},
570 {"extend", 2, 3, f_extend},
571 {"feedkeys", 1, 2, f_feedkeys},
572 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
573 {"filereadable", 1, 1, f_filereadable},
574 {"filewritable", 1, 1, f_filewritable},
575 {"filter", 2, 2, f_filter},
576 {"finddir", 1, 3, f_finddir},
577 {"findfile", 1, 3, f_findfile},
578#ifdef FEAT_FLOAT
579 {"float2nr", 1, 1, f_float2nr},
580 {"floor", 1, 1, f_floor},
581 {"fmod", 2, 2, f_fmod},
582#endif
583 {"fnameescape", 1, 1, f_fnameescape},
584 {"fnamemodify", 2, 2, f_fnamemodify},
585 {"foldclosed", 1, 1, f_foldclosed},
586 {"foldclosedend", 1, 1, f_foldclosedend},
587 {"foldlevel", 1, 1, f_foldlevel},
588 {"foldtext", 0, 0, f_foldtext},
589 {"foldtextresult", 1, 1, f_foldtextresult},
590 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200591 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200592 {"function", 1, 3, f_function},
593 {"garbagecollect", 0, 1, f_garbagecollect},
594 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200595 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200596 {"getbufline", 2, 3, f_getbufline},
597 {"getbufvar", 2, 3, f_getbufvar},
598 {"getchar", 0, 1, f_getchar},
599 {"getcharmod", 0, 0, f_getcharmod},
600 {"getcharsearch", 0, 0, f_getcharsearch},
601 {"getcmdline", 0, 0, f_getcmdline},
602 {"getcmdpos", 0, 0, f_getcmdpos},
603 {"getcmdtype", 0, 0, f_getcmdtype},
604 {"getcmdwintype", 0, 0, f_getcmdwintype},
605#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200606 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200607#endif
608 {"getcurpos", 0, 0, f_getcurpos},
609 {"getcwd", 0, 2, f_getcwd},
610 {"getfontname", 0, 1, f_getfontname},
611 {"getfperm", 1, 1, f_getfperm},
612 {"getfsize", 1, 1, f_getfsize},
613 {"getftime", 1, 1, f_getftime},
614 {"getftype", 1, 1, f_getftype},
615 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200616 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200617 {"getmatches", 0, 0, f_getmatches},
618 {"getpid", 0, 0, f_getpid},
619 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200620 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200621 {"getreg", 0, 3, f_getreg},
622 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200623 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200624 {"gettabvar", 2, 3, f_gettabvar},
625 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200626 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200627 {"getwinposx", 0, 0, f_getwinposx},
628 {"getwinposy", 0, 0, f_getwinposy},
629 {"getwinvar", 2, 3, f_getwinvar},
630 {"glob", 1, 4, f_glob},
631 {"glob2regpat", 1, 1, f_glob2regpat},
632 {"globpath", 2, 5, f_globpath},
633 {"has", 1, 1, f_has},
634 {"has_key", 2, 2, f_has_key},
635 {"haslocaldir", 0, 2, f_haslocaldir},
636 {"hasmapto", 1, 3, f_hasmapto},
637 {"highlightID", 1, 1, f_hlID}, /* obsolete */
638 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
639 {"histadd", 2, 2, f_histadd},
640 {"histdel", 1, 2, f_histdel},
641 {"histget", 1, 2, f_histget},
642 {"histnr", 1, 1, f_histnr},
643 {"hlID", 1, 1, f_hlID},
644 {"hlexists", 1, 1, f_hlexists},
645 {"hostname", 0, 0, f_hostname},
646 {"iconv", 3, 3, f_iconv},
647 {"indent", 1, 1, f_indent},
648 {"index", 2, 4, f_index},
649 {"input", 1, 3, f_input},
650 {"inputdialog", 1, 3, f_inputdialog},
651 {"inputlist", 1, 1, f_inputlist},
652 {"inputrestore", 0, 0, f_inputrestore},
653 {"inputsave", 0, 0, f_inputsave},
654 {"inputsecret", 1, 2, f_inputsecret},
655 {"insert", 2, 3, f_insert},
656 {"invert", 1, 1, f_invert},
657 {"isdirectory", 1, 1, f_isdirectory},
658 {"islocked", 1, 1, f_islocked},
659#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
660 {"isnan", 1, 1, f_isnan},
661#endif
662 {"items", 1, 1, f_items},
663#ifdef FEAT_JOB_CHANNEL
664 {"job_getchannel", 1, 1, f_job_getchannel},
665 {"job_info", 1, 1, f_job_info},
666 {"job_setoptions", 2, 2, f_job_setoptions},
667 {"job_start", 1, 2, f_job_start},
668 {"job_status", 1, 1, f_job_status},
669 {"job_stop", 1, 2, f_job_stop},
670#endif
671 {"join", 1, 2, f_join},
672 {"js_decode", 1, 1, f_js_decode},
673 {"js_encode", 1, 1, f_js_encode},
674 {"json_decode", 1, 1, f_json_decode},
675 {"json_encode", 1, 1, f_json_encode},
676 {"keys", 1, 1, f_keys},
677 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
678 {"len", 1, 1, f_len},
679 {"libcall", 3, 3, f_libcall},
680 {"libcallnr", 3, 3, f_libcallnr},
681 {"line", 1, 1, f_line},
682 {"line2byte", 1, 1, f_line2byte},
683 {"lispindent", 1, 1, f_lispindent},
684 {"localtime", 0, 0, f_localtime},
685#ifdef FEAT_FLOAT
686 {"log", 1, 1, f_log},
687 {"log10", 1, 1, f_log10},
688#endif
689#ifdef FEAT_LUA
690 {"luaeval", 1, 2, f_luaeval},
691#endif
692 {"map", 2, 2, f_map},
693 {"maparg", 1, 4, f_maparg},
694 {"mapcheck", 1, 3, f_mapcheck},
695 {"match", 2, 4, f_match},
696 {"matchadd", 2, 5, f_matchadd},
697 {"matchaddpos", 2, 5, f_matchaddpos},
698 {"matcharg", 1, 1, f_matcharg},
699 {"matchdelete", 1, 1, f_matchdelete},
700 {"matchend", 2, 4, f_matchend},
701 {"matchlist", 2, 4, f_matchlist},
702 {"matchstr", 2, 4, f_matchstr},
703 {"matchstrpos", 2, 4, f_matchstrpos},
704 {"max", 1, 1, f_max},
705 {"min", 1, 1, f_min},
706#ifdef vim_mkdir
707 {"mkdir", 1, 3, f_mkdir},
708#endif
709 {"mode", 0, 1, f_mode},
710#ifdef FEAT_MZSCHEME
711 {"mzeval", 1, 1, f_mzeval},
712#endif
713 {"nextnonblank", 1, 1, f_nextnonblank},
714 {"nr2char", 1, 2, f_nr2char},
715 {"or", 2, 2, f_or},
716 {"pathshorten", 1, 1, f_pathshorten},
717#ifdef FEAT_PERL
718 {"perleval", 1, 1, f_perleval},
719#endif
720#ifdef FEAT_FLOAT
721 {"pow", 2, 2, f_pow},
722#endif
723 {"prevnonblank", 1, 1, f_prevnonblank},
724 {"printf", 2, 19, f_printf},
725 {"pumvisible", 0, 0, f_pumvisible},
726#ifdef FEAT_PYTHON3
727 {"py3eval", 1, 1, f_py3eval},
728#endif
729#ifdef FEAT_PYTHON
730 {"pyeval", 1, 1, f_pyeval},
731#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100732#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
733 {"pyxeval", 1, 1, f_pyxeval},
734#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200735 {"range", 1, 3, f_range},
736 {"readfile", 1, 3, f_readfile},
737 {"reltime", 0, 2, f_reltime},
738#ifdef FEAT_FLOAT
739 {"reltimefloat", 1, 1, f_reltimefloat},
740#endif
741 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100742 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200743 {"remote_foreground", 1, 1, f_remote_foreground},
744 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100745 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200746 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100747 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200748 {"remove", 2, 3, f_remove},
749 {"rename", 2, 2, f_rename},
750 {"repeat", 2, 2, f_repeat},
751 {"resolve", 1, 1, f_resolve},
752 {"reverse", 1, 1, f_reverse},
753#ifdef FEAT_FLOAT
754 {"round", 1, 1, f_round},
755#endif
756 {"screenattr", 2, 2, f_screenattr},
757 {"screenchar", 2, 2, f_screenchar},
758 {"screencol", 0, 0, f_screencol},
759 {"screenrow", 0, 0, f_screenrow},
760 {"search", 1, 4, f_search},
761 {"searchdecl", 1, 3, f_searchdecl},
762 {"searchpair", 3, 7, f_searchpair},
763 {"searchpairpos", 3, 7, f_searchpairpos},
764 {"searchpos", 1, 4, f_searchpos},
765 {"server2client", 2, 2, f_server2client},
766 {"serverlist", 0, 0, f_serverlist},
767 {"setbufvar", 3, 3, f_setbufvar},
768 {"setcharsearch", 1, 1, f_setcharsearch},
769 {"setcmdpos", 1, 1, f_setcmdpos},
770 {"setfperm", 2, 2, f_setfperm},
771 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200772 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200773 {"setmatches", 1, 1, f_setmatches},
774 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200775 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200776 {"setreg", 2, 3, f_setreg},
777 {"settabvar", 3, 3, f_settabvar},
778 {"settabwinvar", 4, 4, f_settabwinvar},
779 {"setwinvar", 3, 3, f_setwinvar},
780#ifdef FEAT_CRYPT
781 {"sha256", 1, 1, f_sha256},
782#endif
783 {"shellescape", 1, 2, f_shellescape},
784 {"shiftwidth", 0, 0, f_shiftwidth},
785 {"simplify", 1, 1, f_simplify},
786#ifdef FEAT_FLOAT
787 {"sin", 1, 1, f_sin},
788 {"sinh", 1, 1, f_sinh},
789#endif
790 {"sort", 1, 3, f_sort},
791 {"soundfold", 1, 1, f_soundfold},
792 {"spellbadword", 0, 1, f_spellbadword},
793 {"spellsuggest", 1, 3, f_spellsuggest},
794 {"split", 1, 3, f_split},
795#ifdef FEAT_FLOAT
796 {"sqrt", 1, 1, f_sqrt},
797 {"str2float", 1, 1, f_str2float},
798#endif
799 {"str2nr", 1, 2, f_str2nr},
800 {"strcharpart", 2, 3, f_strcharpart},
801 {"strchars", 1, 2, f_strchars},
802 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
803#ifdef HAVE_STRFTIME
804 {"strftime", 1, 2, f_strftime},
805#endif
806 {"strgetchar", 2, 2, f_strgetchar},
807 {"stridx", 2, 3, f_stridx},
808 {"string", 1, 1, f_string},
809 {"strlen", 1, 1, f_strlen},
810 {"strpart", 2, 3, f_strpart},
811 {"strridx", 2, 3, f_strridx},
812 {"strtrans", 1, 1, f_strtrans},
813 {"strwidth", 1, 1, f_strwidth},
814 {"submatch", 1, 2, f_submatch},
815 {"substitute", 4, 4, f_substitute},
816 {"synID", 3, 3, f_synID},
817 {"synIDattr", 2, 3, f_synIDattr},
818 {"synIDtrans", 1, 1, f_synIDtrans},
819 {"synconcealed", 2, 2, f_synconcealed},
820 {"synstack", 2, 2, f_synstack},
821 {"system", 1, 2, f_system},
822 {"systemlist", 1, 2, f_systemlist},
823 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
824 {"tabpagenr", 0, 1, f_tabpagenr},
825 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
826 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100827 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200828#ifdef FEAT_FLOAT
829 {"tan", 1, 1, f_tan},
830 {"tanh", 1, 1, f_tanh},
831#endif
832 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200833#ifdef FEAT_TERMINAL
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200834 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200835 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200836 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200837 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200838 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200839 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200840 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200841 {"term_getstatus", 1, 1, f_term_getstatus},
842 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar7c9aec42017-08-03 13:51:25 +0200843 {"term_gettty", 1, 1, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200844 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200845 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200846 {"term_sendkeys", 2, 2, f_term_sendkeys},
847 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200848 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200849#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200850 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
851 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200852 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100853 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200854#ifdef FEAT_JOB_CHANNEL
855 {"test_null_channel", 0, 0, f_test_null_channel},
856#endif
857 {"test_null_dict", 0, 0, f_test_null_dict},
858#ifdef FEAT_JOB_CHANNEL
859 {"test_null_job", 0, 0, f_test_null_job},
860#endif
861 {"test_null_list", 0, 0, f_test_null_list},
862 {"test_null_partial", 0, 0, f_test_null_partial},
863 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100864 {"test_override", 2, 2, f_test_override},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200865 {"test_settime", 1, 1, f_test_settime},
866#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200867 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200868 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200869 {"timer_start", 2, 3, f_timer_start},
870 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200871 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200872#endif
873 {"tolower", 1, 1, f_tolower},
874 {"toupper", 1, 1, f_toupper},
875 {"tr", 3, 3, f_tr},
876#ifdef FEAT_FLOAT
877 {"trunc", 1, 1, f_trunc},
878#endif
879 {"type", 1, 1, f_type},
880 {"undofile", 1, 1, f_undofile},
881 {"undotree", 0, 0, f_undotree},
882 {"uniq", 1, 3, f_uniq},
883 {"values", 1, 1, f_values},
884 {"virtcol", 1, 1, f_virtcol},
885 {"visualmode", 0, 1, f_visualmode},
886 {"wildmenumode", 0, 0, f_wildmenumode},
887 {"win_findbuf", 1, 1, f_win_findbuf},
888 {"win_getid", 0, 2, f_win_getid},
889 {"win_gotoid", 1, 1, f_win_gotoid},
890 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
891 {"win_id2win", 1, 1, f_win_id2win},
892 {"winbufnr", 1, 1, f_winbufnr},
893 {"wincol", 0, 0, f_wincol},
894 {"winheight", 1, 1, f_winheight},
895 {"winline", 0, 0, f_winline},
896 {"winnr", 0, 1, f_winnr},
897 {"winrestcmd", 0, 0, f_winrestcmd},
898 {"winrestview", 1, 1, f_winrestview},
899 {"winsaveview", 0, 0, f_winsaveview},
900 {"winwidth", 1, 1, f_winwidth},
901 {"wordcount", 0, 0, f_wordcount},
902 {"writefile", 2, 3, f_writefile},
903 {"xor", 2, 2, f_xor},
904};
905
906#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
907
908/*
909 * Function given to ExpandGeneric() to obtain the list of internal
910 * or user defined function names.
911 */
912 char_u *
913get_function_name(expand_T *xp, int idx)
914{
915 static int intidx = -1;
916 char_u *name;
917
918 if (idx == 0)
919 intidx = -1;
920 if (intidx < 0)
921 {
922 name = get_user_func_name(xp, idx);
923 if (name != NULL)
924 return name;
925 }
926 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
927 {
928 STRCPY(IObuff, functions[intidx].f_name);
929 STRCAT(IObuff, "(");
930 if (functions[intidx].f_max_argc == 0)
931 STRCAT(IObuff, ")");
932 return IObuff;
933 }
934
935 return NULL;
936}
937
938/*
939 * Function given to ExpandGeneric() to obtain the list of internal or
940 * user defined variable or function names.
941 */
942 char_u *
943get_expr_name(expand_T *xp, int idx)
944{
945 static int intidx = -1;
946 char_u *name;
947
948 if (idx == 0)
949 intidx = -1;
950 if (intidx < 0)
951 {
952 name = get_function_name(xp, idx);
953 if (name != NULL)
954 return name;
955 }
956 return get_user_var_name(xp, ++intidx);
957}
958
959#endif /* FEAT_CMDL_COMPL */
960
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200961/*
962 * Find internal function in table above.
963 * Return index, or -1 if not found
964 */
965 int
966find_internal_func(
967 char_u *name) /* name of the function */
968{
969 int first = 0;
970 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
971 int cmp;
972 int x;
973
974 /*
975 * Find the function name in the table. Binary search.
976 */
977 while (first <= last)
978 {
979 x = first + ((unsigned)(last - first) >> 1);
980 cmp = STRCMP(name, functions[x].f_name);
981 if (cmp < 0)
982 last = x - 1;
983 else if (cmp > 0)
984 first = x + 1;
985 else
986 return x;
987 }
988 return -1;
989}
990
991 int
992call_internal_func(
993 char_u *name,
994 int argcount,
995 typval_T *argvars,
996 typval_T *rettv)
997{
998 int i;
999
1000 i = find_internal_func(name);
1001 if (i < 0)
1002 return ERROR_UNKNOWN;
1003 if (argcount < functions[i].f_min_argc)
1004 return ERROR_TOOFEW;
1005 if (argcount > functions[i].f_max_argc)
1006 return ERROR_TOOMANY;
1007 argvars[argcount].v_type = VAR_UNKNOWN;
1008 functions[i].f_func(argvars, rettv);
1009 return ERROR_NONE;
1010}
1011
1012/*
1013 * Return TRUE for a non-zero Number and a non-empty String.
1014 */
1015 static int
1016non_zero_arg(typval_T *argvars)
1017{
1018 return ((argvars[0].v_type == VAR_NUMBER
1019 && argvars[0].vval.v_number != 0)
1020 || (argvars[0].v_type == VAR_SPECIAL
1021 && argvars[0].vval.v_number == VVAL_TRUE)
1022 || (argvars[0].v_type == VAR_STRING
1023 && argvars[0].vval.v_string != NULL
1024 && *argvars[0].vval.v_string != NUL));
1025}
1026
1027/*
1028 * Get the lnum from the first argument.
1029 * Also accepts ".", "$", etc., but that only works for the current buffer.
1030 * Returns -1 on error.
1031 */
1032 static linenr_T
1033get_tv_lnum(typval_T *argvars)
1034{
1035 typval_T rettv;
1036 linenr_T lnum;
1037
1038 lnum = (linenr_T)get_tv_number_chk(&argvars[0], NULL);
1039 if (lnum == 0) /* no valid number, try using line() */
1040 {
1041 rettv.v_type = VAR_NUMBER;
1042 f_line(argvars, &rettv);
1043 lnum = (linenr_T)rettv.vval.v_number;
1044 clear_tv(&rettv);
1045 }
1046 return lnum;
1047}
1048
1049#ifdef FEAT_FLOAT
1050static int get_float_arg(typval_T *argvars, float_T *f);
1051
1052/*
1053 * Get the float value of "argvars[0]" into "f".
1054 * Returns FAIL when the argument is not a Number or Float.
1055 */
1056 static int
1057get_float_arg(typval_T *argvars, float_T *f)
1058{
1059 if (argvars[0].v_type == VAR_FLOAT)
1060 {
1061 *f = argvars[0].vval.v_float;
1062 return OK;
1063 }
1064 if (argvars[0].v_type == VAR_NUMBER)
1065 {
1066 *f = (float_T)argvars[0].vval.v_number;
1067 return OK;
1068 }
1069 EMSG(_("E808: Number or Float required"));
1070 return FAIL;
1071}
1072
1073/*
1074 * "abs(expr)" function
1075 */
1076 static void
1077f_abs(typval_T *argvars, typval_T *rettv)
1078{
1079 if (argvars[0].v_type == VAR_FLOAT)
1080 {
1081 rettv->v_type = VAR_FLOAT;
1082 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1083 }
1084 else
1085 {
1086 varnumber_T n;
1087 int error = FALSE;
1088
1089 n = get_tv_number_chk(&argvars[0], &error);
1090 if (error)
1091 rettv->vval.v_number = -1;
1092 else if (n > 0)
1093 rettv->vval.v_number = n;
1094 else
1095 rettv->vval.v_number = -n;
1096 }
1097}
1098
1099/*
1100 * "acos()" function
1101 */
1102 static void
1103f_acos(typval_T *argvars, typval_T *rettv)
1104{
1105 float_T f = 0.0;
1106
1107 rettv->v_type = VAR_FLOAT;
1108 if (get_float_arg(argvars, &f) == OK)
1109 rettv->vval.v_float = acos(f);
1110 else
1111 rettv->vval.v_float = 0.0;
1112}
1113#endif
1114
1115/*
1116 * "add(list, item)" function
1117 */
1118 static void
1119f_add(typval_T *argvars, typval_T *rettv)
1120{
1121 list_T *l;
1122
1123 rettv->vval.v_number = 1; /* Default: Failed */
1124 if (argvars[0].v_type == VAR_LIST)
1125 {
1126 if ((l = argvars[0].vval.v_list) != NULL
1127 && !tv_check_lock(l->lv_lock,
1128 (char_u *)N_("add() argument"), TRUE)
1129 && list_append_tv(l, &argvars[1]) == OK)
1130 copy_tv(&argvars[0], rettv);
1131 }
1132 else
1133 EMSG(_(e_listreq));
1134}
1135
1136/*
1137 * "and(expr, expr)" function
1138 */
1139 static void
1140f_and(typval_T *argvars, typval_T *rettv)
1141{
1142 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
1143 & get_tv_number_chk(&argvars[1], NULL);
1144}
1145
1146/*
1147 * "append(lnum, string/list)" function
1148 */
1149 static void
1150f_append(typval_T *argvars, typval_T *rettv)
1151{
1152 long lnum;
1153 char_u *line;
1154 list_T *l = NULL;
1155 listitem_T *li = NULL;
1156 typval_T *tv;
1157 long added = 0;
1158
1159 /* When coming here from Insert mode, sync undo, so that this can be
1160 * undone separately from what was previously inserted. */
1161 if (u_sync_once == 2)
1162 {
1163 u_sync_once = 1; /* notify that u_sync() was called */
1164 u_sync(TRUE);
1165 }
1166
1167 lnum = get_tv_lnum(argvars);
1168 if (lnum >= 0
1169 && lnum <= curbuf->b_ml.ml_line_count
1170 && u_save(lnum, lnum + 1) == OK)
1171 {
1172 if (argvars[1].v_type == VAR_LIST)
1173 {
1174 l = argvars[1].vval.v_list;
1175 if (l == NULL)
1176 return;
1177 li = l->lv_first;
1178 }
1179 for (;;)
1180 {
1181 if (l == NULL)
1182 tv = &argvars[1]; /* append a string */
1183 else if (li == NULL)
1184 break; /* end of list */
1185 else
1186 tv = &li->li_tv; /* append item from list */
1187 line = get_tv_string_chk(tv);
1188 if (line == NULL) /* type error */
1189 {
1190 rettv->vval.v_number = 1; /* Failed */
1191 break;
1192 }
1193 ml_append(lnum + added, line, (colnr_T)0, FALSE);
1194 ++added;
1195 if (l == NULL)
1196 break;
1197 li = li->li_next;
1198 }
1199
1200 appended_lines_mark(lnum, added);
1201 if (curwin->w_cursor.lnum > lnum)
1202 curwin->w_cursor.lnum += added;
1203 }
1204 else
1205 rettv->vval.v_number = 1; /* Failed */
1206}
1207
1208/*
1209 * "argc()" function
1210 */
1211 static void
1212f_argc(typval_T *argvars UNUSED, typval_T *rettv)
1213{
1214 rettv->vval.v_number = ARGCOUNT;
1215}
1216
1217/*
1218 * "argidx()" function
1219 */
1220 static void
1221f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1222{
1223 rettv->vval.v_number = curwin->w_arg_idx;
1224}
1225
1226/*
1227 * "arglistid()" function
1228 */
1229 static void
1230f_arglistid(typval_T *argvars, typval_T *rettv)
1231{
1232 win_T *wp;
1233
1234 rettv->vval.v_number = -1;
1235 wp = find_tabwin(&argvars[0], &argvars[1]);
1236 if (wp != NULL)
1237 rettv->vval.v_number = wp->w_alist->id;
1238}
1239
1240/*
1241 * "argv(nr)" function
1242 */
1243 static void
1244f_argv(typval_T *argvars, typval_T *rettv)
1245{
1246 int idx;
1247
1248 if (argvars[0].v_type != VAR_UNKNOWN)
1249 {
1250 idx = (int)get_tv_number_chk(&argvars[0], NULL);
1251 if (idx >= 0 && idx < ARGCOUNT)
1252 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
1253 else
1254 rettv->vval.v_string = NULL;
1255 rettv->v_type = VAR_STRING;
1256 }
1257 else if (rettv_list_alloc(rettv) == OK)
1258 for (idx = 0; idx < ARGCOUNT; ++idx)
1259 list_append_string(rettv->vval.v_list,
1260 alist_name(&ARGLIST[idx]), -1);
1261}
1262
1263/*
1264 * "assert_equal(expected, actual[, msg])" function
1265 */
1266 static void
1267f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
1268{
1269 assert_equal_common(argvars, ASSERT_EQUAL);
1270}
1271
1272/*
1273 * "assert_notequal(expected, actual[, msg])" function
1274 */
1275 static void
1276f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED)
1277{
1278 assert_equal_common(argvars, ASSERT_NOTEQUAL);
1279}
1280
1281/*
1282 * "assert_exception(string[, msg])" function
1283 */
1284 static void
1285f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
1286{
1287 assert_exception(argvars);
1288}
1289
1290/*
1291 * "assert_fails(cmd [, error])" function
1292 */
1293 static void
1294f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
1295{
1296 assert_fails(argvars);
1297}
1298
1299/*
1300 * "assert_false(actual[, msg])" function
1301 */
1302 static void
1303f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
1304{
1305 assert_bool(argvars, FALSE);
1306}
1307
1308/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001309 * "assert_inrange(lower, upper[, msg])" function
1310 */
1311 static void
1312f_assert_inrange(typval_T *argvars, typval_T *rettv UNUSED)
1313{
1314 assert_inrange(argvars);
1315}
1316
1317/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001318 * "assert_match(pattern, actual[, msg])" function
1319 */
1320 static void
1321f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
1322{
1323 assert_match_common(argvars, ASSERT_MATCH);
1324}
1325
1326/*
1327 * "assert_notmatch(pattern, actual[, msg])" function
1328 */
1329 static void
1330f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED)
1331{
1332 assert_match_common(argvars, ASSERT_NOTMATCH);
1333}
1334
1335/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001336 * "assert_report(msg)" function
1337 */
1338 static void
1339f_assert_report(typval_T *argvars, typval_T *rettv UNUSED)
1340{
1341 assert_report(argvars);
1342}
1343
1344/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001345 * "assert_true(actual[, msg])" function
1346 */
1347 static void
1348f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
1349{
1350 assert_bool(argvars, TRUE);
1351}
1352
1353#ifdef FEAT_FLOAT
1354/*
1355 * "asin()" function
1356 */
1357 static void
1358f_asin(typval_T *argvars, typval_T *rettv)
1359{
1360 float_T f = 0.0;
1361
1362 rettv->v_type = VAR_FLOAT;
1363 if (get_float_arg(argvars, &f) == OK)
1364 rettv->vval.v_float = asin(f);
1365 else
1366 rettv->vval.v_float = 0.0;
1367}
1368
1369/*
1370 * "atan()" function
1371 */
1372 static void
1373f_atan(typval_T *argvars, typval_T *rettv)
1374{
1375 float_T f = 0.0;
1376
1377 rettv->v_type = VAR_FLOAT;
1378 if (get_float_arg(argvars, &f) == OK)
1379 rettv->vval.v_float = atan(f);
1380 else
1381 rettv->vval.v_float = 0.0;
1382}
1383
1384/*
1385 * "atan2()" function
1386 */
1387 static void
1388f_atan2(typval_T *argvars, typval_T *rettv)
1389{
1390 float_T fx = 0.0, fy = 0.0;
1391
1392 rettv->v_type = VAR_FLOAT;
1393 if (get_float_arg(argvars, &fx) == OK
1394 && get_float_arg(&argvars[1], &fy) == OK)
1395 rettv->vval.v_float = atan2(fx, fy);
1396 else
1397 rettv->vval.v_float = 0.0;
1398}
1399#endif
1400
1401/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001402 * "balloon_show()" function
1403 */
1404#ifdef FEAT_BEVAL
1405 static void
1406f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1407{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001408 if (balloonEval != NULL)
1409 gui_mch_post_balloon(balloonEval, get_tv_string_chk(&argvars[0]));
Bram Moolenaar59716a22017-03-01 20:32:44 +01001410}
1411#endif
1412
1413/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001414 * "browse(save, title, initdir, default)" function
1415 */
1416 static void
1417f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1418{
1419#ifdef FEAT_BROWSE
1420 int save;
1421 char_u *title;
1422 char_u *initdir;
1423 char_u *defname;
1424 char_u buf[NUMBUFLEN];
1425 char_u buf2[NUMBUFLEN];
1426 int error = FALSE;
1427
1428 save = (int)get_tv_number_chk(&argvars[0], &error);
1429 title = get_tv_string_chk(&argvars[1]);
1430 initdir = get_tv_string_buf_chk(&argvars[2], buf);
1431 defname = get_tv_string_buf_chk(&argvars[3], buf2);
1432
1433 if (error || title == NULL || initdir == NULL || defname == NULL)
1434 rettv->vval.v_string = NULL;
1435 else
1436 rettv->vval.v_string =
1437 do_browse(save ? BROWSE_SAVE : 0,
1438 title, defname, NULL, initdir, NULL, curbuf);
1439#else
1440 rettv->vval.v_string = NULL;
1441#endif
1442 rettv->v_type = VAR_STRING;
1443}
1444
1445/*
1446 * "browsedir(title, initdir)" function
1447 */
1448 static void
1449f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1450{
1451#ifdef FEAT_BROWSE
1452 char_u *title;
1453 char_u *initdir;
1454 char_u buf[NUMBUFLEN];
1455
1456 title = get_tv_string_chk(&argvars[0]);
1457 initdir = get_tv_string_buf_chk(&argvars[1], buf);
1458
1459 if (title == NULL || initdir == NULL)
1460 rettv->vval.v_string = NULL;
1461 else
1462 rettv->vval.v_string = do_browse(BROWSE_DIR,
1463 title, NULL, NULL, initdir, NULL, curbuf);
1464#else
1465 rettv->vval.v_string = NULL;
1466#endif
1467 rettv->v_type = VAR_STRING;
1468}
1469
1470static buf_T *find_buffer(typval_T *avar);
1471
1472/*
1473 * Find a buffer by number or exact name.
1474 */
1475 static buf_T *
1476find_buffer(typval_T *avar)
1477{
1478 buf_T *buf = NULL;
1479
1480 if (avar->v_type == VAR_NUMBER)
1481 buf = buflist_findnr((int)avar->vval.v_number);
1482 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1483 {
1484 buf = buflist_findname_exp(avar->vval.v_string);
1485 if (buf == NULL)
1486 {
1487 /* No full path name match, try a match with a URL or a "nofile"
1488 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001489 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001490 if (buf->b_fname != NULL
1491 && (path_with_url(buf->b_fname)
1492#ifdef FEAT_QUICKFIX
1493 || bt_nofile(buf)
1494#endif
1495 )
1496 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1497 break;
1498 }
1499 }
1500 return buf;
1501}
1502
1503/*
1504 * "bufexists(expr)" function
1505 */
1506 static void
1507f_bufexists(typval_T *argvars, typval_T *rettv)
1508{
1509 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1510}
1511
1512/*
1513 * "buflisted(expr)" function
1514 */
1515 static void
1516f_buflisted(typval_T *argvars, typval_T *rettv)
1517{
1518 buf_T *buf;
1519
1520 buf = find_buffer(&argvars[0]);
1521 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1522}
1523
1524/*
1525 * "bufloaded(expr)" function
1526 */
1527 static void
1528f_bufloaded(typval_T *argvars, typval_T *rettv)
1529{
1530 buf_T *buf;
1531
1532 buf = find_buffer(&argvars[0]);
1533 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1534}
1535
1536 buf_T *
1537buflist_find_by_name(char_u *name, int curtab_only)
1538{
1539 int save_magic;
1540 char_u *save_cpo;
1541 buf_T *buf;
1542
1543 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1544 save_magic = p_magic;
1545 p_magic = TRUE;
1546 save_cpo = p_cpo;
1547 p_cpo = (char_u *)"";
1548
1549 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1550 TRUE, FALSE, curtab_only));
1551
1552 p_magic = save_magic;
1553 p_cpo = save_cpo;
1554 return buf;
1555}
1556
1557/*
1558 * Get buffer by number or pattern.
1559 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001560 buf_T *
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001561get_buf_tv(typval_T *tv, int curtab_only)
1562{
1563 char_u *name = tv->vval.v_string;
1564 buf_T *buf;
1565
1566 if (tv->v_type == VAR_NUMBER)
1567 return buflist_findnr((int)tv->vval.v_number);
1568 if (tv->v_type != VAR_STRING)
1569 return NULL;
1570 if (name == NULL || *name == NUL)
1571 return curbuf;
1572 if (name[0] == '$' && name[1] == NUL)
1573 return lastbuf;
1574
1575 buf = buflist_find_by_name(name, curtab_only);
1576
1577 /* If not found, try expanding the name, like done for bufexists(). */
1578 if (buf == NULL)
1579 buf = find_buffer(tv);
1580
1581 return buf;
1582}
1583
1584/*
1585 * "bufname(expr)" function
1586 */
1587 static void
1588f_bufname(typval_T *argvars, typval_T *rettv)
1589{
1590 buf_T *buf;
1591
1592 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1593 ++emsg_off;
1594 buf = get_buf_tv(&argvars[0], FALSE);
1595 rettv->v_type = VAR_STRING;
1596 if (buf != NULL && buf->b_fname != NULL)
1597 rettv->vval.v_string = vim_strsave(buf->b_fname);
1598 else
1599 rettv->vval.v_string = NULL;
1600 --emsg_off;
1601}
1602
1603/*
1604 * "bufnr(expr)" function
1605 */
1606 static void
1607f_bufnr(typval_T *argvars, typval_T *rettv)
1608{
1609 buf_T *buf;
1610 int error = FALSE;
1611 char_u *name;
1612
1613 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1614 ++emsg_off;
1615 buf = get_buf_tv(&argvars[0], FALSE);
1616 --emsg_off;
1617
1618 /* If the buffer isn't found and the second argument is not zero create a
1619 * new buffer. */
1620 if (buf == NULL
1621 && argvars[1].v_type != VAR_UNKNOWN
1622 && get_tv_number_chk(&argvars[1], &error) != 0
1623 && !error
1624 && (name = get_tv_string_chk(&argvars[0])) != NULL
1625 && !error)
1626 buf = buflist_new(name, NULL, (linenr_T)1, 0);
1627
1628 if (buf != NULL)
1629 rettv->vval.v_number = buf->b_fnum;
1630 else
1631 rettv->vval.v_number = -1;
1632}
1633
1634 static void
1635buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
1636{
1637#ifdef FEAT_WINDOWS
1638 win_T *wp;
1639 int winnr = 0;
1640#endif
1641 buf_T *buf;
1642
1643 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1644 ++emsg_off;
1645 buf = get_buf_tv(&argvars[0], TRUE);
1646#ifdef FEAT_WINDOWS
Bram Moolenaar29323592016-07-24 22:04:11 +02001647 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001648 {
1649 ++winnr;
1650 if (wp->w_buffer == buf)
1651 break;
1652 }
1653 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
1654#else
1655 rettv->vval.v_number = (curwin->w_buffer == buf
1656 ? (get_nr ? 1 : curwin->w_id) : -1);
1657#endif
1658 --emsg_off;
1659}
1660
1661/*
1662 * "bufwinid(nr)" function
1663 */
1664 static void
1665f_bufwinid(typval_T *argvars, typval_T *rettv)
1666{
1667 buf_win_common(argvars, rettv, FALSE);
1668}
1669
1670/*
1671 * "bufwinnr(nr)" function
1672 */
1673 static void
1674f_bufwinnr(typval_T *argvars, typval_T *rettv)
1675{
1676 buf_win_common(argvars, rettv, TRUE);
1677}
1678
1679/*
1680 * "byte2line(byte)" function
1681 */
1682 static void
1683f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1684{
1685#ifndef FEAT_BYTEOFF
1686 rettv->vval.v_number = -1;
1687#else
1688 long boff = 0;
1689
1690 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
1691 if (boff < 0)
1692 rettv->vval.v_number = -1;
1693 else
1694 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
1695 (linenr_T)0, &boff);
1696#endif
1697}
1698
1699 static void
1700byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
1701{
1702#ifdef FEAT_MBYTE
1703 char_u *t;
1704#endif
1705 char_u *str;
1706 varnumber_T idx;
1707
1708 str = get_tv_string_chk(&argvars[0]);
1709 idx = get_tv_number_chk(&argvars[1], NULL);
1710 rettv->vval.v_number = -1;
1711 if (str == NULL || idx < 0)
1712 return;
1713
1714#ifdef FEAT_MBYTE
1715 t = str;
1716 for ( ; idx > 0; idx--)
1717 {
1718 if (*t == NUL) /* EOL reached */
1719 return;
1720 if (enc_utf8 && comp)
1721 t += utf_ptr2len(t);
1722 else
1723 t += (*mb_ptr2len)(t);
1724 }
1725 rettv->vval.v_number = (varnumber_T)(t - str);
1726#else
1727 if ((size_t)idx <= STRLEN(str))
1728 rettv->vval.v_number = idx;
1729#endif
1730}
1731
1732/*
1733 * "byteidx()" function
1734 */
1735 static void
1736f_byteidx(typval_T *argvars, typval_T *rettv)
1737{
1738 byteidx(argvars, rettv, FALSE);
1739}
1740
1741/*
1742 * "byteidxcomp()" function
1743 */
1744 static void
1745f_byteidxcomp(typval_T *argvars, typval_T *rettv)
1746{
1747 byteidx(argvars, rettv, TRUE);
1748}
1749
1750/*
1751 * "call(func, arglist [, dict])" function
1752 */
1753 static void
1754f_call(typval_T *argvars, typval_T *rettv)
1755{
1756 char_u *func;
1757 partial_T *partial = NULL;
1758 dict_T *selfdict = NULL;
1759
1760 if (argvars[1].v_type != VAR_LIST)
1761 {
1762 EMSG(_(e_listreq));
1763 return;
1764 }
1765 if (argvars[1].vval.v_list == NULL)
1766 return;
1767
1768 if (argvars[0].v_type == VAR_FUNC)
1769 func = argvars[0].vval.v_string;
1770 else if (argvars[0].v_type == VAR_PARTIAL)
1771 {
1772 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001773 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001774 }
1775 else
1776 func = get_tv_string(&argvars[0]);
1777 if (*func == NUL)
1778 return; /* type error or empty name */
1779
1780 if (argvars[2].v_type != VAR_UNKNOWN)
1781 {
1782 if (argvars[2].v_type != VAR_DICT)
1783 {
1784 EMSG(_(e_dictreq));
1785 return;
1786 }
1787 selfdict = argvars[2].vval.v_dict;
1788 }
1789
1790 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
1791}
1792
1793#ifdef FEAT_FLOAT
1794/*
1795 * "ceil({float})" function
1796 */
1797 static void
1798f_ceil(typval_T *argvars, typval_T *rettv)
1799{
1800 float_T f = 0.0;
1801
1802 rettv->v_type = VAR_FLOAT;
1803 if (get_float_arg(argvars, &f) == OK)
1804 rettv->vval.v_float = ceil(f);
1805 else
1806 rettv->vval.v_float = 0.0;
1807}
1808#endif
1809
1810#ifdef FEAT_JOB_CHANNEL
1811/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01001812 * "ch_canread()" function
1813 */
1814 static void
1815f_ch_canread(typval_T *argvars, typval_T *rettv)
1816{
Bram Moolenaar958dc692016-12-01 15:34:12 +01001817 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01001818
1819 rettv->vval.v_number = 0;
1820 if (channel != NULL)
1821 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
1822 || channel_has_readahead(channel, PART_OUT)
1823 || channel_has_readahead(channel, PART_ERR);
1824}
1825
1826/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001827 * "ch_close()" function
1828 */
1829 static void
1830f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
1831{
1832 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
1833
1834 if (channel != NULL)
1835 {
1836 channel_close(channel, FALSE);
1837 channel_clear(channel);
1838 }
1839}
1840
1841/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02001842 * "ch_close()" function
1843 */
1844 static void
1845f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
1846{
1847 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
1848
1849 if (channel != NULL)
1850 channel_close_in(channel);
1851}
1852
1853/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001854 * "ch_getbufnr()" function
1855 */
1856 static void
1857f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
1858{
1859 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1860
1861 rettv->vval.v_number = -1;
1862 if (channel != NULL)
1863 {
1864 char_u *what = get_tv_string(&argvars[1]);
1865 int part;
1866
1867 if (STRCMP(what, "err") == 0)
1868 part = PART_ERR;
1869 else if (STRCMP(what, "out") == 0)
1870 part = PART_OUT;
1871 else if (STRCMP(what, "in") == 0)
1872 part = PART_IN;
1873 else
1874 part = PART_SOCK;
1875 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
1876 rettv->vval.v_number =
1877 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
1878 }
1879}
1880
1881/*
1882 * "ch_getjob()" function
1883 */
1884 static void
1885f_ch_getjob(typval_T *argvars, typval_T *rettv)
1886{
1887 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1888
1889 if (channel != NULL)
1890 {
1891 rettv->v_type = VAR_JOB;
1892 rettv->vval.v_job = channel->ch_job;
1893 if (channel->ch_job != NULL)
1894 ++channel->ch_job->jv_refcount;
1895 }
1896}
1897
1898/*
1899 * "ch_info()" function
1900 */
1901 static void
1902f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
1903{
1904 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1905
1906 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
1907 channel_info(channel, rettv->vval.v_dict);
1908}
1909
1910/*
1911 * "ch_log()" function
1912 */
1913 static void
1914f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
1915{
1916 char_u *msg = get_tv_string(&argvars[0]);
1917 channel_T *channel = NULL;
1918
1919 if (argvars[1].v_type != VAR_UNKNOWN)
1920 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
1921
1922 ch_log(channel, (char *)msg);
1923}
1924
1925/*
1926 * "ch_logfile()" function
1927 */
1928 static void
1929f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
1930{
1931 char_u *fname;
1932 char_u *opt = (char_u *)"";
1933 char_u buf[NUMBUFLEN];
1934
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02001935 /* Don't open a file in restricted mode. */
1936 if (check_restricted() || check_secure())
1937 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001938 fname = get_tv_string(&argvars[0]);
1939 if (argvars[1].v_type == VAR_STRING)
1940 opt = get_tv_string_buf(&argvars[1], buf);
1941 ch_logfile(fname, opt);
1942}
1943
1944/*
1945 * "ch_open()" function
1946 */
1947 static void
1948f_ch_open(typval_T *argvars, typval_T *rettv)
1949{
1950 rettv->v_type = VAR_CHANNEL;
1951 if (check_restricted() || check_secure())
1952 return;
1953 rettv->vval.v_channel = channel_open_func(argvars);
1954}
1955
1956/*
1957 * "ch_read()" function
1958 */
1959 static void
1960f_ch_read(typval_T *argvars, typval_T *rettv)
1961{
1962 common_channel_read(argvars, rettv, FALSE);
1963}
1964
1965/*
1966 * "ch_readraw()" function
1967 */
1968 static void
1969f_ch_readraw(typval_T *argvars, typval_T *rettv)
1970{
1971 common_channel_read(argvars, rettv, TRUE);
1972}
1973
1974/*
1975 * "ch_evalexpr()" function
1976 */
1977 static void
1978f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
1979{
1980 ch_expr_common(argvars, rettv, TRUE);
1981}
1982
1983/*
1984 * "ch_sendexpr()" function
1985 */
1986 static void
1987f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
1988{
1989 ch_expr_common(argvars, rettv, FALSE);
1990}
1991
1992/*
1993 * "ch_evalraw()" function
1994 */
1995 static void
1996f_ch_evalraw(typval_T *argvars, typval_T *rettv)
1997{
1998 ch_raw_common(argvars, rettv, TRUE);
1999}
2000
2001/*
2002 * "ch_sendraw()" function
2003 */
2004 static void
2005f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2006{
2007 ch_raw_common(argvars, rettv, FALSE);
2008}
2009
2010/*
2011 * "ch_setoptions()" function
2012 */
2013 static void
2014f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2015{
2016 channel_T *channel;
2017 jobopt_T opt;
2018
2019 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2020 if (channel == NULL)
2021 return;
2022 clear_job_options(&opt);
2023 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002024 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002025 channel_set_options(channel, &opt);
2026 free_job_options(&opt);
2027}
2028
2029/*
2030 * "ch_status()" function
2031 */
2032 static void
2033f_ch_status(typval_T *argvars, typval_T *rettv)
2034{
2035 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002036 jobopt_T opt;
2037 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002038
2039 /* return an empty string by default */
2040 rettv->v_type = VAR_STRING;
2041 rettv->vval.v_string = NULL;
2042
2043 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002044
2045 if (argvars[1].v_type != VAR_UNKNOWN)
2046 {
2047 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002048 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002049 && (opt.jo_set & JO_PART))
2050 part = opt.jo_part;
2051 }
2052
2053 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002054}
2055#endif
2056
2057/*
2058 * "changenr()" function
2059 */
2060 static void
2061f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2062{
2063 rettv->vval.v_number = curbuf->b_u_seq_cur;
2064}
2065
2066/*
2067 * "char2nr(string)" function
2068 */
2069 static void
2070f_char2nr(typval_T *argvars, typval_T *rettv)
2071{
2072#ifdef FEAT_MBYTE
2073 if (has_mbyte)
2074 {
2075 int utf8 = 0;
2076
2077 if (argvars[1].v_type != VAR_UNKNOWN)
2078 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
2079
2080 if (utf8)
2081 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
2082 else
2083 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
2084 }
2085 else
2086#endif
2087 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
2088}
2089
2090/*
2091 * "cindent(lnum)" function
2092 */
2093 static void
2094f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2095{
2096#ifdef FEAT_CINDENT
2097 pos_T pos;
2098 linenr_T lnum;
2099
2100 pos = curwin->w_cursor;
2101 lnum = get_tv_lnum(argvars);
2102 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2103 {
2104 curwin->w_cursor.lnum = lnum;
2105 rettv->vval.v_number = get_c_indent();
2106 curwin->w_cursor = pos;
2107 }
2108 else
2109#endif
2110 rettv->vval.v_number = -1;
2111}
2112
2113/*
2114 * "clearmatches()" function
2115 */
2116 static void
2117f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2118{
2119#ifdef FEAT_SEARCH_EXTRA
2120 clear_matches(curwin);
2121#endif
2122}
2123
2124/*
2125 * "col(string)" function
2126 */
2127 static void
2128f_col(typval_T *argvars, typval_T *rettv)
2129{
2130 colnr_T col = 0;
2131 pos_T *fp;
2132 int fnum = curbuf->b_fnum;
2133
2134 fp = var2fpos(&argvars[0], FALSE, &fnum);
2135 if (fp != NULL && fnum == curbuf->b_fnum)
2136 {
2137 if (fp->col == MAXCOL)
2138 {
2139 /* '> can be MAXCOL, get the length of the line then */
2140 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2141 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2142 else
2143 col = MAXCOL;
2144 }
2145 else
2146 {
2147 col = fp->col + 1;
2148#ifdef FEAT_VIRTUALEDIT
2149 /* col(".") when the cursor is on the NUL at the end of the line
2150 * because of "coladd" can be seen as an extra column. */
2151 if (virtual_active() && fp == &curwin->w_cursor)
2152 {
2153 char_u *p = ml_get_cursor();
2154
2155 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2156 curwin->w_virtcol - curwin->w_cursor.coladd))
2157 {
2158# ifdef FEAT_MBYTE
2159 int l;
2160
2161 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2162 col += l;
2163# else
2164 if (*p != NUL && p[1] == NUL)
2165 ++col;
2166# endif
2167 }
2168 }
2169#endif
2170 }
2171 }
2172 rettv->vval.v_number = col;
2173}
2174
2175#if defined(FEAT_INS_EXPAND)
2176/*
2177 * "complete()" function
2178 */
2179 static void
2180f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2181{
2182 int startcol;
2183
2184 if ((State & INSERT) == 0)
2185 {
2186 EMSG(_("E785: complete() can only be used in Insert mode"));
2187 return;
2188 }
2189
2190 /* Check for undo allowed here, because if something was already inserted
2191 * the line was already saved for undo and this check isn't done. */
2192 if (!undo_allowed())
2193 return;
2194
2195 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2196 {
2197 EMSG(_(e_invarg));
2198 return;
2199 }
2200
2201 startcol = (int)get_tv_number_chk(&argvars[0], NULL);
2202 if (startcol <= 0)
2203 return;
2204
2205 set_completion(startcol - 1, argvars[1].vval.v_list);
2206}
2207
2208/*
2209 * "complete_add()" function
2210 */
2211 static void
2212f_complete_add(typval_T *argvars, typval_T *rettv)
2213{
2214 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2215}
2216
2217/*
2218 * "complete_check()" function
2219 */
2220 static void
2221f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2222{
2223 int saved = RedrawingDisabled;
2224
2225 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002226 ins_compl_check_keys(0, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002227 rettv->vval.v_number = compl_interrupted;
2228 RedrawingDisabled = saved;
2229}
2230#endif
2231
2232/*
2233 * "confirm(message, buttons[, default [, type]])" function
2234 */
2235 static void
2236f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2237{
2238#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2239 char_u *message;
2240 char_u *buttons = NULL;
2241 char_u buf[NUMBUFLEN];
2242 char_u buf2[NUMBUFLEN];
2243 int def = 1;
2244 int type = VIM_GENERIC;
2245 char_u *typestr;
2246 int error = FALSE;
2247
2248 message = get_tv_string_chk(&argvars[0]);
2249 if (message == NULL)
2250 error = TRUE;
2251 if (argvars[1].v_type != VAR_UNKNOWN)
2252 {
2253 buttons = get_tv_string_buf_chk(&argvars[1], buf);
2254 if (buttons == NULL)
2255 error = TRUE;
2256 if (argvars[2].v_type != VAR_UNKNOWN)
2257 {
2258 def = (int)get_tv_number_chk(&argvars[2], &error);
2259 if (argvars[3].v_type != VAR_UNKNOWN)
2260 {
2261 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
2262 if (typestr == NULL)
2263 error = TRUE;
2264 else
2265 {
2266 switch (TOUPPER_ASC(*typestr))
2267 {
2268 case 'E': type = VIM_ERROR; break;
2269 case 'Q': type = VIM_QUESTION; break;
2270 case 'I': type = VIM_INFO; break;
2271 case 'W': type = VIM_WARNING; break;
2272 case 'G': type = VIM_GENERIC; break;
2273 }
2274 }
2275 }
2276 }
2277 }
2278
2279 if (buttons == NULL || *buttons == NUL)
2280 buttons = (char_u *)_("&Ok");
2281
2282 if (!error)
2283 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2284 def, NULL, FALSE);
2285#endif
2286}
2287
2288/*
2289 * "copy()" function
2290 */
2291 static void
2292f_copy(typval_T *argvars, typval_T *rettv)
2293{
2294 item_copy(&argvars[0], rettv, FALSE, 0);
2295}
2296
2297#ifdef FEAT_FLOAT
2298/*
2299 * "cos()" function
2300 */
2301 static void
2302f_cos(typval_T *argvars, typval_T *rettv)
2303{
2304 float_T f = 0.0;
2305
2306 rettv->v_type = VAR_FLOAT;
2307 if (get_float_arg(argvars, &f) == OK)
2308 rettv->vval.v_float = cos(f);
2309 else
2310 rettv->vval.v_float = 0.0;
2311}
2312
2313/*
2314 * "cosh()" function
2315 */
2316 static void
2317f_cosh(typval_T *argvars, typval_T *rettv)
2318{
2319 float_T f = 0.0;
2320
2321 rettv->v_type = VAR_FLOAT;
2322 if (get_float_arg(argvars, &f) == OK)
2323 rettv->vval.v_float = cosh(f);
2324 else
2325 rettv->vval.v_float = 0.0;
2326}
2327#endif
2328
2329/*
2330 * "count()" function
2331 */
2332 static void
2333f_count(typval_T *argvars, typval_T *rettv)
2334{
2335 long n = 0;
2336 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002337 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002338
Bram Moolenaar9966b212017-07-28 16:46:57 +02002339 if (argvars[2].v_type != VAR_UNKNOWN)
2340 ic = (int)get_tv_number_chk(&argvars[2], &error);
2341
2342 if (argvars[0].v_type == VAR_STRING)
2343 {
2344 char_u *expr = get_tv_string_chk(&argvars[1]);
2345 char_u *p = argvars[0].vval.v_string;
2346 char_u *next;
2347
2348 if (!error && expr != NULL && p != NULL)
2349 {
2350 if (ic)
2351 {
2352 size_t len = STRLEN(expr);
2353
2354 while (*p != NUL)
2355 {
2356 if (MB_STRNICMP(p, expr, len) == 0)
2357 {
2358 ++n;
2359 p += len;
2360 }
2361 else
2362 MB_PTR_ADV(p);
2363 }
2364 }
2365 else
2366 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2367 != NULL)
2368 {
2369 ++n;
2370 p = next + STRLEN(expr);
2371 }
2372 }
2373
2374 }
2375 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002376 {
2377 listitem_T *li;
2378 list_T *l;
2379 long idx;
2380
2381 if ((l = argvars[0].vval.v_list) != NULL)
2382 {
2383 li = l->lv_first;
2384 if (argvars[2].v_type != VAR_UNKNOWN)
2385 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002386 if (argvars[3].v_type != VAR_UNKNOWN)
2387 {
2388 idx = (long)get_tv_number_chk(&argvars[3], &error);
2389 if (!error)
2390 {
2391 li = list_find(l, idx);
2392 if (li == NULL)
2393 EMSGN(_(e_listidx), idx);
2394 }
2395 }
2396 if (error)
2397 li = NULL;
2398 }
2399
2400 for ( ; li != NULL; li = li->li_next)
2401 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2402 ++n;
2403 }
2404 }
2405 else if (argvars[0].v_type == VAR_DICT)
2406 {
2407 int todo;
2408 dict_T *d;
2409 hashitem_T *hi;
2410
2411 if ((d = argvars[0].vval.v_dict) != NULL)
2412 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002413 if (argvars[2].v_type != VAR_UNKNOWN)
2414 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002415 if (argvars[3].v_type != VAR_UNKNOWN)
2416 EMSG(_(e_invarg));
2417 }
2418
2419 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2420 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2421 {
2422 if (!HASHITEM_EMPTY(hi))
2423 {
2424 --todo;
2425 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2426 ++n;
2427 }
2428 }
2429 }
2430 }
2431 else
2432 EMSG2(_(e_listdictarg), "count()");
2433 rettv->vval.v_number = n;
2434}
2435
2436/*
2437 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2438 *
2439 * Checks the existence of a cscope connection.
2440 */
2441 static void
2442f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2443{
2444#ifdef FEAT_CSCOPE
2445 int num = 0;
2446 char_u *dbpath = NULL;
2447 char_u *prepend = NULL;
2448 char_u buf[NUMBUFLEN];
2449
2450 if (argvars[0].v_type != VAR_UNKNOWN
2451 && argvars[1].v_type != VAR_UNKNOWN)
2452 {
2453 num = (int)get_tv_number(&argvars[0]);
2454 dbpath = get_tv_string(&argvars[1]);
2455 if (argvars[2].v_type != VAR_UNKNOWN)
2456 prepend = get_tv_string_buf(&argvars[2], buf);
2457 }
2458
2459 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2460#endif
2461}
2462
2463/*
2464 * "cursor(lnum, col)" function, or
2465 * "cursor(list)"
2466 *
2467 * Moves the cursor to the specified line and column.
2468 * Returns 0 when the position could be set, -1 otherwise.
2469 */
2470 static void
2471f_cursor(typval_T *argvars, typval_T *rettv)
2472{
2473 long line, col;
2474#ifdef FEAT_VIRTUALEDIT
2475 long coladd = 0;
2476#endif
2477 int set_curswant = TRUE;
2478
2479 rettv->vval.v_number = -1;
2480 if (argvars[1].v_type == VAR_UNKNOWN)
2481 {
2482 pos_T pos;
2483 colnr_T curswant = -1;
2484
2485 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2486 {
2487 EMSG(_(e_invarg));
2488 return;
2489 }
2490 line = pos.lnum;
2491 col = pos.col;
2492#ifdef FEAT_VIRTUALEDIT
2493 coladd = pos.coladd;
2494#endif
2495 if (curswant >= 0)
2496 {
2497 curwin->w_curswant = curswant - 1;
2498 set_curswant = FALSE;
2499 }
2500 }
2501 else
2502 {
2503 line = get_tv_lnum(argvars);
2504 col = (long)get_tv_number_chk(&argvars[1], NULL);
2505#ifdef FEAT_VIRTUALEDIT
2506 if (argvars[2].v_type != VAR_UNKNOWN)
2507 coladd = (long)get_tv_number_chk(&argvars[2], NULL);
2508#endif
2509 }
2510 if (line < 0 || col < 0
2511#ifdef FEAT_VIRTUALEDIT
2512 || coladd < 0
2513#endif
2514 )
2515 return; /* type error; errmsg already given */
2516 if (line > 0)
2517 curwin->w_cursor.lnum = line;
2518 if (col > 0)
2519 curwin->w_cursor.col = col - 1;
2520#ifdef FEAT_VIRTUALEDIT
2521 curwin->w_cursor.coladd = coladd;
2522#endif
2523
2524 /* Make sure the cursor is in a valid position. */
2525 check_cursor();
2526#ifdef FEAT_MBYTE
2527 /* Correct cursor for multi-byte character. */
2528 if (has_mbyte)
2529 mb_adjust_cursor();
2530#endif
2531
2532 curwin->w_set_curswant = set_curswant;
2533 rettv->vval.v_number = 0;
2534}
2535
2536/*
2537 * "deepcopy()" function
2538 */
2539 static void
2540f_deepcopy(typval_T *argvars, typval_T *rettv)
2541{
2542 int noref = 0;
2543 int copyID;
2544
2545 if (argvars[1].v_type != VAR_UNKNOWN)
2546 noref = (int)get_tv_number_chk(&argvars[1], NULL);
2547 if (noref < 0 || noref > 1)
2548 EMSG(_(e_invarg));
2549 else
2550 {
2551 copyID = get_copyID();
2552 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2553 }
2554}
2555
2556/*
2557 * "delete()" function
2558 */
2559 static void
2560f_delete(typval_T *argvars, typval_T *rettv)
2561{
2562 char_u nbuf[NUMBUFLEN];
2563 char_u *name;
2564 char_u *flags;
2565
2566 rettv->vval.v_number = -1;
2567 if (check_restricted() || check_secure())
2568 return;
2569
2570 name = get_tv_string(&argvars[0]);
2571 if (name == NULL || *name == NUL)
2572 {
2573 EMSG(_(e_invarg));
2574 return;
2575 }
2576
2577 if (argvars[1].v_type != VAR_UNKNOWN)
2578 flags = get_tv_string_buf(&argvars[1], nbuf);
2579 else
2580 flags = (char_u *)"";
2581
2582 if (*flags == NUL)
2583 /* delete a file */
2584 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2585 else if (STRCMP(flags, "d") == 0)
2586 /* delete an empty directory */
2587 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2588 else if (STRCMP(flags, "rf") == 0)
2589 /* delete a directory recursively */
2590 rettv->vval.v_number = delete_recursive(name);
2591 else
2592 EMSG2(_(e_invexpr2), flags);
2593}
2594
2595/*
2596 * "did_filetype()" function
2597 */
2598 static void
2599f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2600{
2601#ifdef FEAT_AUTOCMD
2602 rettv->vval.v_number = did_filetype;
2603#endif
2604}
2605
2606/*
2607 * "diff_filler()" function
2608 */
2609 static void
2610f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2611{
2612#ifdef FEAT_DIFF
2613 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
2614#endif
2615}
2616
2617/*
2618 * "diff_hlID()" function
2619 */
2620 static void
2621f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2622{
2623#ifdef FEAT_DIFF
2624 linenr_T lnum = get_tv_lnum(argvars);
2625 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01002626 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002627 static int fnum = 0;
2628 static int change_start = 0;
2629 static int change_end = 0;
2630 static hlf_T hlID = (hlf_T)0;
2631 int filler_lines;
2632 int col;
2633
2634 if (lnum < 0) /* ignore type error in {lnum} arg */
2635 lnum = 0;
2636 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002637 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002638 || fnum != curbuf->b_fnum)
2639 {
2640 /* New line, buffer, change: need to get the values. */
2641 filler_lines = diff_check(curwin, lnum);
2642 if (filler_lines < 0)
2643 {
2644 if (filler_lines == -1)
2645 {
2646 change_start = MAXCOL;
2647 change_end = -1;
2648 if (diff_find_change(curwin, lnum, &change_start, &change_end))
2649 hlID = HLF_ADD; /* added line */
2650 else
2651 hlID = HLF_CHD; /* changed line */
2652 }
2653 else
2654 hlID = HLF_ADD; /* added line */
2655 }
2656 else
2657 hlID = (hlf_T)0;
2658 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002659 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002660 fnum = curbuf->b_fnum;
2661 }
2662
2663 if (hlID == HLF_CHD || hlID == HLF_TXD)
2664 {
2665 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
2666 if (col >= change_start && col <= change_end)
2667 hlID = HLF_TXD; /* changed text */
2668 else
2669 hlID = HLF_CHD; /* changed line */
2670 }
2671 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
2672#endif
2673}
2674
2675/*
2676 * "empty({expr})" function
2677 */
2678 static void
2679f_empty(typval_T *argvars, typval_T *rettv)
2680{
2681 int n = FALSE;
2682
2683 switch (argvars[0].v_type)
2684 {
2685 case VAR_STRING:
2686 case VAR_FUNC:
2687 n = argvars[0].vval.v_string == NULL
2688 || *argvars[0].vval.v_string == NUL;
2689 break;
2690 case VAR_PARTIAL:
2691 n = FALSE;
2692 break;
2693 case VAR_NUMBER:
2694 n = argvars[0].vval.v_number == 0;
2695 break;
2696 case VAR_FLOAT:
2697#ifdef FEAT_FLOAT
2698 n = argvars[0].vval.v_float == 0.0;
2699 break;
2700#endif
2701 case VAR_LIST:
2702 n = argvars[0].vval.v_list == NULL
2703 || argvars[0].vval.v_list->lv_first == NULL;
2704 break;
2705 case VAR_DICT:
2706 n = argvars[0].vval.v_dict == NULL
2707 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2708 break;
2709 case VAR_SPECIAL:
2710 n = argvars[0].vval.v_number != VVAL_TRUE;
2711 break;
2712
2713 case VAR_JOB:
2714#ifdef FEAT_JOB_CHANNEL
2715 n = argvars[0].vval.v_job == NULL
2716 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2717 break;
2718#endif
2719 case VAR_CHANNEL:
2720#ifdef FEAT_JOB_CHANNEL
2721 n = argvars[0].vval.v_channel == NULL
2722 || !channel_is_open(argvars[0].vval.v_channel);
2723 break;
2724#endif
2725 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01002726 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002727 n = TRUE;
2728 break;
2729 }
2730
2731 rettv->vval.v_number = n;
2732}
2733
2734/*
2735 * "escape({string}, {chars})" function
2736 */
2737 static void
2738f_escape(typval_T *argvars, typval_T *rettv)
2739{
2740 char_u buf[NUMBUFLEN];
2741
2742 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
2743 get_tv_string_buf(&argvars[1], buf));
2744 rettv->v_type = VAR_STRING;
2745}
2746
2747/*
2748 * "eval()" function
2749 */
2750 static void
2751f_eval(typval_T *argvars, typval_T *rettv)
2752{
2753 char_u *s, *p;
2754
2755 s = get_tv_string_chk(&argvars[0]);
2756 if (s != NULL)
2757 s = skipwhite(s);
2758
2759 p = s;
2760 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
2761 {
2762 if (p != NULL && !aborting())
2763 EMSG2(_(e_invexpr2), p);
2764 need_clr_eos = FALSE;
2765 rettv->v_type = VAR_NUMBER;
2766 rettv->vval.v_number = 0;
2767 }
2768 else if (*s != NUL)
2769 EMSG(_(e_trailing));
2770}
2771
2772/*
2773 * "eventhandler()" function
2774 */
2775 static void
2776f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
2777{
2778 rettv->vval.v_number = vgetc_busy;
2779}
2780
2781/*
2782 * "executable()" function
2783 */
2784 static void
2785f_executable(typval_T *argvars, typval_T *rettv)
2786{
2787 char_u *name = get_tv_string(&argvars[0]);
2788
2789 /* Check in $PATH and also check directly if there is a directory name. */
2790 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
2791 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
2792}
2793
2794static garray_T redir_execute_ga;
2795
2796/*
2797 * Append "value[value_len]" to the execute() output.
2798 */
2799 void
2800execute_redir_str(char_u *value, int value_len)
2801{
2802 int len;
2803
2804 if (value_len == -1)
2805 len = (int)STRLEN(value); /* Append the entire string */
2806 else
2807 len = value_len; /* Append only "value_len" characters */
2808 if (ga_grow(&redir_execute_ga, len) == OK)
2809 {
2810 mch_memmove((char *)redir_execute_ga.ga_data
2811 + redir_execute_ga.ga_len, value, len);
2812 redir_execute_ga.ga_len += len;
2813 }
2814}
2815
2816/*
2817 * Get next line from a list.
2818 * Called by do_cmdline() to get the next line.
2819 * Returns allocated string, or NULL for end of function.
2820 */
2821
2822 static char_u *
2823get_list_line(
2824 int c UNUSED,
2825 void *cookie,
2826 int indent UNUSED)
2827{
2828 listitem_T **p = (listitem_T **)cookie;
2829 listitem_T *item = *p;
2830 char_u buf[NUMBUFLEN];
2831 char_u *s;
2832
2833 if (item == NULL)
2834 return NULL;
2835 s = get_tv_string_buf_chk(&item->li_tv, buf);
2836 *p = item->li_next;
2837 return s == NULL ? NULL : vim_strsave(s);
2838}
2839
2840/*
2841 * "execute()" function
2842 */
2843 static void
2844f_execute(typval_T *argvars, typval_T *rettv)
2845{
2846 char_u *cmd = NULL;
2847 list_T *list = NULL;
2848 int save_msg_silent = msg_silent;
2849 int save_emsg_silent = emsg_silent;
2850 int save_emsg_noredir = emsg_noredir;
2851 int save_redir_execute = redir_execute;
2852 garray_T save_ga;
2853
2854 rettv->vval.v_string = NULL;
2855 rettv->v_type = VAR_STRING;
2856
2857 if (argvars[0].v_type == VAR_LIST)
2858 {
2859 list = argvars[0].vval.v_list;
2860 if (list == NULL || list->lv_first == NULL)
2861 /* empty list, no commands, empty output */
2862 return;
2863 ++list->lv_refcount;
2864 }
2865 else
2866 {
2867 cmd = get_tv_string_chk(&argvars[0]);
2868 if (cmd == NULL)
2869 return;
2870 }
2871
2872 if (argvars[1].v_type != VAR_UNKNOWN)
2873 {
2874 char_u buf[NUMBUFLEN];
2875 char_u *s = get_tv_string_buf_chk(&argvars[1], buf);
2876
2877 if (s == NULL)
2878 return;
2879 if (STRNCMP(s, "silent", 6) == 0)
2880 ++msg_silent;
2881 if (STRCMP(s, "silent!") == 0)
2882 {
2883 emsg_silent = TRUE;
2884 emsg_noredir = TRUE;
2885 }
2886 }
2887 else
2888 ++msg_silent;
2889
2890 if (redir_execute)
2891 save_ga = redir_execute_ga;
2892 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
2893 redir_execute = TRUE;
2894
2895 if (cmd != NULL)
2896 do_cmdline_cmd(cmd);
2897 else
2898 {
2899 listitem_T *item = list->lv_first;
2900
2901 do_cmdline(NULL, get_list_line, (void *)&item,
2902 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
2903 --list->lv_refcount;
2904 }
2905
Bram Moolenaard297f352017-01-29 20:31:21 +01002906 /* Need to append a NUL to the result. */
2907 if (ga_grow(&redir_execute_ga, 1) == OK)
2908 {
2909 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
2910 rettv->vval.v_string = redir_execute_ga.ga_data;
2911 }
2912 else
2913 {
2914 ga_clear(&redir_execute_ga);
2915 rettv->vval.v_string = NULL;
2916 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002917 msg_silent = save_msg_silent;
2918 emsg_silent = save_emsg_silent;
2919 emsg_noredir = save_emsg_noredir;
2920
2921 redir_execute = save_redir_execute;
2922 if (redir_execute)
2923 redir_execute_ga = save_ga;
2924
2925 /* "silent reg" or "silent echo x" leaves msg_col somewhere in the
2926 * line. Put it back in the first column. */
2927 msg_col = 0;
2928}
2929
2930/*
2931 * "exepath()" function
2932 */
2933 static void
2934f_exepath(typval_T *argvars, typval_T *rettv)
2935{
2936 char_u *p = NULL;
2937
2938 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
2939 rettv->v_type = VAR_STRING;
2940 rettv->vval.v_string = p;
2941}
2942
2943/*
2944 * "exists()" function
2945 */
2946 static void
2947f_exists(typval_T *argvars, typval_T *rettv)
2948{
2949 char_u *p;
2950 char_u *name;
2951 int n = FALSE;
2952 int len = 0;
2953
2954 p = get_tv_string(&argvars[0]);
2955 if (*p == '$') /* environment variable */
2956 {
2957 /* first try "normal" environment variables (fast) */
2958 if (mch_getenv(p + 1) != NULL)
2959 n = TRUE;
2960 else
2961 {
2962 /* try expanding things like $VIM and ${HOME} */
2963 p = expand_env_save(p);
2964 if (p != NULL && *p != '$')
2965 n = TRUE;
2966 vim_free(p);
2967 }
2968 }
2969 else if (*p == '&' || *p == '+') /* option */
2970 {
2971 n = (get_option_tv(&p, NULL, TRUE) == OK);
2972 if (*skipwhite(p) != NUL)
2973 n = FALSE; /* trailing garbage */
2974 }
2975 else if (*p == '*') /* internal or user defined function */
2976 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002977 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002978 }
2979 else if (*p == ':')
2980 {
2981 n = cmd_exists(p + 1);
2982 }
2983 else if (*p == '#')
2984 {
2985#ifdef FEAT_AUTOCMD
2986 if (p[1] == '#')
2987 n = autocmd_supported(p + 2);
2988 else
2989 n = au_exists(p + 1);
2990#endif
2991 }
2992 else /* internal variable */
2993 {
2994 char_u *tofree;
2995 typval_T tv;
2996
2997 /* get_name_len() takes care of expanding curly braces */
2998 name = p;
2999 len = get_name_len(&p, &tofree, TRUE, FALSE);
3000 if (len > 0)
3001 {
3002 if (tofree != NULL)
3003 name = tofree;
3004 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
3005 if (n)
3006 {
3007 /* handle d.key, l[idx], f(expr) */
3008 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
3009 if (n)
3010 clear_tv(&tv);
3011 }
3012 }
3013 if (*p != NUL)
3014 n = FALSE;
3015
3016 vim_free(tofree);
3017 }
3018
3019 rettv->vval.v_number = n;
3020}
3021
3022#ifdef FEAT_FLOAT
3023/*
3024 * "exp()" function
3025 */
3026 static void
3027f_exp(typval_T *argvars, typval_T *rettv)
3028{
3029 float_T f = 0.0;
3030
3031 rettv->v_type = VAR_FLOAT;
3032 if (get_float_arg(argvars, &f) == OK)
3033 rettv->vval.v_float = exp(f);
3034 else
3035 rettv->vval.v_float = 0.0;
3036}
3037#endif
3038
3039/*
3040 * "expand()" function
3041 */
3042 static void
3043f_expand(typval_T *argvars, typval_T *rettv)
3044{
3045 char_u *s;
3046 int len;
3047 char_u *errormsg;
3048 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3049 expand_T xpc;
3050 int error = FALSE;
3051 char_u *result;
3052
3053 rettv->v_type = VAR_STRING;
3054 if (argvars[1].v_type != VAR_UNKNOWN
3055 && argvars[2].v_type != VAR_UNKNOWN
3056 && get_tv_number_chk(&argvars[2], &error)
3057 && !error)
3058 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003059 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003060 }
3061
3062 s = get_tv_string(&argvars[0]);
3063 if (*s == '%' || *s == '#' || *s == '<')
3064 {
3065 ++emsg_off;
3066 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3067 --emsg_off;
3068 if (rettv->v_type == VAR_LIST)
3069 {
3070 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3071 list_append_string(rettv->vval.v_list, result, -1);
3072 else
3073 vim_free(result);
3074 }
3075 else
3076 rettv->vval.v_string = result;
3077 }
3078 else
3079 {
3080 /* When the optional second argument is non-zero, don't remove matches
3081 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3082 if (argvars[1].v_type != VAR_UNKNOWN
3083 && get_tv_number_chk(&argvars[1], &error))
3084 options |= WILD_KEEP_ALL;
3085 if (!error)
3086 {
3087 ExpandInit(&xpc);
3088 xpc.xp_context = EXPAND_FILES;
3089 if (p_wic)
3090 options += WILD_ICASE;
3091 if (rettv->v_type == VAR_STRING)
3092 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3093 options, WILD_ALL);
3094 else if (rettv_list_alloc(rettv) != FAIL)
3095 {
3096 int i;
3097
3098 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3099 for (i = 0; i < xpc.xp_numfiles; i++)
3100 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3101 ExpandCleanup(&xpc);
3102 }
3103 }
3104 else
3105 rettv->vval.v_string = NULL;
3106 }
3107}
3108
3109/*
3110 * "extend(list, list [, idx])" function
3111 * "extend(dict, dict [, action])" function
3112 */
3113 static void
3114f_extend(typval_T *argvars, typval_T *rettv)
3115{
3116 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3117
3118 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3119 {
3120 list_T *l1, *l2;
3121 listitem_T *item;
3122 long before;
3123 int error = FALSE;
3124
3125 l1 = argvars[0].vval.v_list;
3126 l2 = argvars[1].vval.v_list;
3127 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
3128 && l2 != NULL)
3129 {
3130 if (argvars[2].v_type != VAR_UNKNOWN)
3131 {
3132 before = (long)get_tv_number_chk(&argvars[2], &error);
3133 if (error)
3134 return; /* type error; errmsg already given */
3135
3136 if (before == l1->lv_len)
3137 item = NULL;
3138 else
3139 {
3140 item = list_find(l1, before);
3141 if (item == NULL)
3142 {
3143 EMSGN(_(e_listidx), before);
3144 return;
3145 }
3146 }
3147 }
3148 else
3149 item = NULL;
3150 list_extend(l1, l2, item);
3151
3152 copy_tv(&argvars[0], rettv);
3153 }
3154 }
3155 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3156 {
3157 dict_T *d1, *d2;
3158 char_u *action;
3159 int i;
3160
3161 d1 = argvars[0].vval.v_dict;
3162 d2 = argvars[1].vval.v_dict;
3163 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
3164 && d2 != NULL)
3165 {
3166 /* Check the third argument. */
3167 if (argvars[2].v_type != VAR_UNKNOWN)
3168 {
3169 static char *(av[]) = {"keep", "force", "error"};
3170
3171 action = get_tv_string_chk(&argvars[2]);
3172 if (action == NULL)
3173 return; /* type error; errmsg already given */
3174 for (i = 0; i < 3; ++i)
3175 if (STRCMP(action, av[i]) == 0)
3176 break;
3177 if (i == 3)
3178 {
3179 EMSG2(_(e_invarg2), action);
3180 return;
3181 }
3182 }
3183 else
3184 action = (char_u *)"force";
3185
3186 dict_extend(d1, d2, action);
3187
3188 copy_tv(&argvars[0], rettv);
3189 }
3190 }
3191 else
3192 EMSG2(_(e_listdictarg), "extend()");
3193}
3194
3195/*
3196 * "feedkeys()" function
3197 */
3198 static void
3199f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3200{
3201 int remap = TRUE;
3202 int insert = FALSE;
3203 char_u *keys, *flags;
3204 char_u nbuf[NUMBUFLEN];
3205 int typed = FALSE;
3206 int execute = FALSE;
3207 int dangerous = FALSE;
3208 char_u *keys_esc;
3209
3210 /* This is not allowed in the sandbox. If the commands would still be
3211 * executed in the sandbox it would be OK, but it probably happens later,
3212 * when "sandbox" is no longer set. */
3213 if (check_secure())
3214 return;
3215
3216 keys = get_tv_string(&argvars[0]);
3217
3218 if (argvars[1].v_type != VAR_UNKNOWN)
3219 {
3220 flags = get_tv_string_buf(&argvars[1], nbuf);
3221 for ( ; *flags != NUL; ++flags)
3222 {
3223 switch (*flags)
3224 {
3225 case 'n': remap = FALSE; break;
3226 case 'm': remap = TRUE; break;
3227 case 't': typed = TRUE; break;
3228 case 'i': insert = TRUE; break;
3229 case 'x': execute = TRUE; break;
3230 case '!': dangerous = TRUE; break;
3231 }
3232 }
3233 }
3234
3235 if (*keys != NUL || execute)
3236 {
3237 /* Need to escape K_SPECIAL and CSI before putting the string in the
3238 * typeahead buffer. */
3239 keys_esc = vim_strsave_escape_csi(keys);
3240 if (keys_esc != NULL)
3241 {
3242 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
3243 insert ? 0 : typebuf.tb_len, !typed, FALSE);
3244 vim_free(keys_esc);
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003245 if (vgetc_busy
3246#ifdef FEAT_TIMERS
3247 || timer_busy
3248#endif
3249 )
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003250 typebuf_was_filled = TRUE;
3251 if (execute)
3252 {
3253 int save_msg_scroll = msg_scroll;
3254
3255 /* Avoid a 1 second delay when the keys start Insert mode. */
3256 msg_scroll = FALSE;
3257
3258 if (!dangerous)
3259 ++ex_normal_busy;
3260 exec_normal(TRUE);
3261 if (!dangerous)
3262 --ex_normal_busy;
3263 msg_scroll |= save_msg_scroll;
3264 }
3265 }
3266 }
3267}
3268
3269/*
3270 * "filereadable()" function
3271 */
3272 static void
3273f_filereadable(typval_T *argvars, typval_T *rettv)
3274{
3275 int fd;
3276 char_u *p;
3277 int n;
3278
3279#ifndef O_NONBLOCK
3280# define O_NONBLOCK 0
3281#endif
3282 p = get_tv_string(&argvars[0]);
3283 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3284 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3285 {
3286 n = TRUE;
3287 close(fd);
3288 }
3289 else
3290 n = FALSE;
3291
3292 rettv->vval.v_number = n;
3293}
3294
3295/*
3296 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3297 * rights to write into.
3298 */
3299 static void
3300f_filewritable(typval_T *argvars, typval_T *rettv)
3301{
3302 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
3303}
3304
3305 static void
3306findfilendir(
3307 typval_T *argvars UNUSED,
3308 typval_T *rettv,
3309 int find_what UNUSED)
3310{
3311#ifdef FEAT_SEARCHPATH
3312 char_u *fname;
3313 char_u *fresult = NULL;
3314 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3315 char_u *p;
3316 char_u pathbuf[NUMBUFLEN];
3317 int count = 1;
3318 int first = TRUE;
3319 int error = FALSE;
3320#endif
3321
3322 rettv->vval.v_string = NULL;
3323 rettv->v_type = VAR_STRING;
3324
3325#ifdef FEAT_SEARCHPATH
3326 fname = get_tv_string(&argvars[0]);
3327
3328 if (argvars[1].v_type != VAR_UNKNOWN)
3329 {
3330 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
3331 if (p == NULL)
3332 error = TRUE;
3333 else
3334 {
3335 if (*p != NUL)
3336 path = p;
3337
3338 if (argvars[2].v_type != VAR_UNKNOWN)
3339 count = (int)get_tv_number_chk(&argvars[2], &error);
3340 }
3341 }
3342
3343 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3344 error = TRUE;
3345
3346 if (*fname != NUL && !error)
3347 {
3348 do
3349 {
3350 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3351 vim_free(fresult);
3352 fresult = find_file_in_path_option(first ? fname : NULL,
3353 first ? (int)STRLEN(fname) : 0,
3354 0, first, path,
3355 find_what,
3356 curbuf->b_ffname,
3357 find_what == FINDFILE_DIR
3358 ? (char_u *)"" : curbuf->b_p_sua);
3359 first = FALSE;
3360
3361 if (fresult != NULL && rettv->v_type == VAR_LIST)
3362 list_append_string(rettv->vval.v_list, fresult, -1);
3363
3364 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3365 }
3366
3367 if (rettv->v_type == VAR_STRING)
3368 rettv->vval.v_string = fresult;
3369#endif
3370}
3371
3372/*
3373 * "filter()" function
3374 */
3375 static void
3376f_filter(typval_T *argvars, typval_T *rettv)
3377{
3378 filter_map(argvars, rettv, FALSE);
3379}
3380
3381/*
3382 * "finddir({fname}[, {path}[, {count}]])" function
3383 */
3384 static void
3385f_finddir(typval_T *argvars, typval_T *rettv)
3386{
3387 findfilendir(argvars, rettv, FINDFILE_DIR);
3388}
3389
3390/*
3391 * "findfile({fname}[, {path}[, {count}]])" function
3392 */
3393 static void
3394f_findfile(typval_T *argvars, typval_T *rettv)
3395{
3396 findfilendir(argvars, rettv, FINDFILE_FILE);
3397}
3398
3399#ifdef FEAT_FLOAT
3400/*
3401 * "float2nr({float})" function
3402 */
3403 static void
3404f_float2nr(typval_T *argvars, typval_T *rettv)
3405{
3406 float_T f = 0.0;
3407
3408 if (get_float_arg(argvars, &f) == OK)
3409 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003410 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003411 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003412 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003413 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003414 else
3415 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003416 }
3417}
3418
3419/*
3420 * "floor({float})" function
3421 */
3422 static void
3423f_floor(typval_T *argvars, typval_T *rettv)
3424{
3425 float_T f = 0.0;
3426
3427 rettv->v_type = VAR_FLOAT;
3428 if (get_float_arg(argvars, &f) == OK)
3429 rettv->vval.v_float = floor(f);
3430 else
3431 rettv->vval.v_float = 0.0;
3432}
3433
3434/*
3435 * "fmod()" function
3436 */
3437 static void
3438f_fmod(typval_T *argvars, typval_T *rettv)
3439{
3440 float_T fx = 0.0, fy = 0.0;
3441
3442 rettv->v_type = VAR_FLOAT;
3443 if (get_float_arg(argvars, &fx) == OK
3444 && get_float_arg(&argvars[1], &fy) == OK)
3445 rettv->vval.v_float = fmod(fx, fy);
3446 else
3447 rettv->vval.v_float = 0.0;
3448}
3449#endif
3450
3451/*
3452 * "fnameescape({string})" function
3453 */
3454 static void
3455f_fnameescape(typval_T *argvars, typval_T *rettv)
3456{
3457 rettv->vval.v_string = vim_strsave_fnameescape(
3458 get_tv_string(&argvars[0]), FALSE);
3459 rettv->v_type = VAR_STRING;
3460}
3461
3462/*
3463 * "fnamemodify({fname}, {mods})" function
3464 */
3465 static void
3466f_fnamemodify(typval_T *argvars, typval_T *rettv)
3467{
3468 char_u *fname;
3469 char_u *mods;
3470 int usedlen = 0;
3471 int len;
3472 char_u *fbuf = NULL;
3473 char_u buf[NUMBUFLEN];
3474
3475 fname = get_tv_string_chk(&argvars[0]);
3476 mods = get_tv_string_buf_chk(&argvars[1], buf);
3477 if (fname == NULL || mods == NULL)
3478 fname = NULL;
3479 else
3480 {
3481 len = (int)STRLEN(fname);
3482 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
3483 }
3484
3485 rettv->v_type = VAR_STRING;
3486 if (fname == NULL)
3487 rettv->vval.v_string = NULL;
3488 else
3489 rettv->vval.v_string = vim_strnsave(fname, len);
3490 vim_free(fbuf);
3491}
3492
3493static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
3494
3495/*
3496 * "foldclosed()" function
3497 */
3498 static void
3499foldclosed_both(
3500 typval_T *argvars UNUSED,
3501 typval_T *rettv,
3502 int end UNUSED)
3503{
3504#ifdef FEAT_FOLDING
3505 linenr_T lnum;
3506 linenr_T first, last;
3507
3508 lnum = get_tv_lnum(argvars);
3509 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3510 {
3511 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3512 {
3513 if (end)
3514 rettv->vval.v_number = (varnumber_T)last;
3515 else
3516 rettv->vval.v_number = (varnumber_T)first;
3517 return;
3518 }
3519 }
3520#endif
3521 rettv->vval.v_number = -1;
3522}
3523
3524/*
3525 * "foldclosed()" function
3526 */
3527 static void
3528f_foldclosed(typval_T *argvars, typval_T *rettv)
3529{
3530 foldclosed_both(argvars, rettv, FALSE);
3531}
3532
3533/*
3534 * "foldclosedend()" function
3535 */
3536 static void
3537f_foldclosedend(typval_T *argvars, typval_T *rettv)
3538{
3539 foldclosed_both(argvars, rettv, TRUE);
3540}
3541
3542/*
3543 * "foldlevel()" function
3544 */
3545 static void
3546f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3547{
3548#ifdef FEAT_FOLDING
3549 linenr_T lnum;
3550
3551 lnum = get_tv_lnum(argvars);
3552 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3553 rettv->vval.v_number = foldLevel(lnum);
3554#endif
3555}
3556
3557/*
3558 * "foldtext()" function
3559 */
3560 static void
3561f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
3562{
3563#ifdef FEAT_FOLDING
3564 linenr_T foldstart;
3565 linenr_T foldend;
3566 char_u *dashes;
3567 linenr_T lnum;
3568 char_u *s;
3569 char_u *r;
3570 int len;
3571 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003572 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003573#endif
3574
3575 rettv->v_type = VAR_STRING;
3576 rettv->vval.v_string = NULL;
3577#ifdef FEAT_FOLDING
3578 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
3579 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
3580 dashes = get_vim_var_str(VV_FOLDDASHES);
3581 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
3582 && dashes != NULL)
3583 {
3584 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02003585 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003586 if (!linewhite(lnum))
3587 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003588
3589 /* Find interesting text in this line. */
3590 s = skipwhite(ml_get(lnum));
3591 /* skip C comment-start */
3592 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
3593 {
3594 s = skipwhite(s + 2);
3595 if (*skipwhite(s) == NUL
3596 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
3597 {
3598 s = skipwhite(ml_get(lnum + 1));
3599 if (*s == '*')
3600 s = skipwhite(s + 1);
3601 }
3602 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02003603 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003604 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003605 r = alloc((unsigned)(STRLEN(txt)
3606 + STRLEN(dashes) /* for %s */
3607 + 20 /* for %3ld */
3608 + STRLEN(s))); /* concatenated */
3609 if (r != NULL)
3610 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02003611 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003612 len = (int)STRLEN(r);
3613 STRCAT(r, s);
3614 /* remove 'foldmarker' and 'commentstring' */
3615 foldtext_cleanup(r + len);
3616 rettv->vval.v_string = r;
3617 }
3618 }
3619#endif
3620}
3621
3622/*
3623 * "foldtextresult(lnum)" function
3624 */
3625 static void
3626f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
3627{
3628#ifdef FEAT_FOLDING
3629 linenr_T lnum;
3630 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003631 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003632 foldinfo_T foldinfo;
3633 int fold_count;
3634#endif
3635
3636 rettv->v_type = VAR_STRING;
3637 rettv->vval.v_string = NULL;
3638#ifdef FEAT_FOLDING
3639 lnum = get_tv_lnum(argvars);
3640 /* treat illegal types and illegal string values for {lnum} the same */
3641 if (lnum < 0)
3642 lnum = 0;
3643 fold_count = foldedCount(curwin, lnum, &foldinfo);
3644 if (fold_count > 0)
3645 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01003646 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
3647 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003648 if (text == buf)
3649 text = vim_strsave(text);
3650 rettv->vval.v_string = text;
3651 }
3652#endif
3653}
3654
3655/*
3656 * "foreground()" function
3657 */
3658 static void
3659f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3660{
3661#ifdef FEAT_GUI
3662 if (gui.in_use)
3663 gui_mch_set_foreground();
3664#else
3665# ifdef WIN32
3666 win32_set_foreground();
3667# endif
3668#endif
3669}
3670
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003671 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003672common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003673{
3674 char_u *s;
3675 char_u *name;
3676 int use_string = FALSE;
3677 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003678 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003679
3680 if (argvars[0].v_type == VAR_FUNC)
3681 {
3682 /* function(MyFunc, [arg], dict) */
3683 s = argvars[0].vval.v_string;
3684 }
3685 else if (argvars[0].v_type == VAR_PARTIAL
3686 && argvars[0].vval.v_partial != NULL)
3687 {
3688 /* function(dict.MyFunc, [arg]) */
3689 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003690 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003691 }
3692 else
3693 {
3694 /* function('MyFunc', [arg], dict) */
3695 s = get_tv_string(&argvars[0]);
3696 use_string = TRUE;
3697 }
3698
Bram Moolenaar843b8842016-08-21 14:36:15 +02003699 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003700 {
3701 name = s;
3702 trans_name = trans_function_name(&name, FALSE,
3703 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
3704 if (*name != NUL)
3705 s = NULL;
3706 }
3707
Bram Moolenaar843b8842016-08-21 14:36:15 +02003708 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
3709 || (is_funcref && trans_name == NULL))
Bram Moolenaar5582ef12016-09-14 22:16:13 +02003710 EMSG2(_(e_invarg2), use_string ? get_tv_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003711 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003712 else if (trans_name != NULL && (is_funcref
3713 ? find_func(trans_name) == NULL
3714 : !translated_function_exists(trans_name)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003715 EMSG2(_("E700: Unknown function: %s"), s);
3716 else
3717 {
3718 int dict_idx = 0;
3719 int arg_idx = 0;
3720 list_T *list = NULL;
3721
3722 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
3723 {
3724 char sid_buf[25];
3725 int off = *s == 's' ? 2 : 5;
3726
3727 /* Expand s: and <SID> into <SNR>nr_, so that the function can
3728 * also be called from another script. Using trans_function_name()
3729 * would also work, but some plugins depend on the name being
3730 * printable text. */
3731 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
3732 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
3733 if (name != NULL)
3734 {
3735 STRCPY(name, sid_buf);
3736 STRCAT(name, s + off);
3737 }
3738 }
3739 else
3740 name = vim_strsave(s);
3741
3742 if (argvars[1].v_type != VAR_UNKNOWN)
3743 {
3744 if (argvars[2].v_type != VAR_UNKNOWN)
3745 {
3746 /* function(name, [args], dict) */
3747 arg_idx = 1;
3748 dict_idx = 2;
3749 }
3750 else if (argvars[1].v_type == VAR_DICT)
3751 /* function(name, dict) */
3752 dict_idx = 1;
3753 else
3754 /* function(name, [args]) */
3755 arg_idx = 1;
3756 if (dict_idx > 0)
3757 {
3758 if (argvars[dict_idx].v_type != VAR_DICT)
3759 {
3760 EMSG(_("E922: expected a dict"));
3761 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003762 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003763 }
3764 if (argvars[dict_idx].vval.v_dict == NULL)
3765 dict_idx = 0;
3766 }
3767 if (arg_idx > 0)
3768 {
3769 if (argvars[arg_idx].v_type != VAR_LIST)
3770 {
3771 EMSG(_("E923: Second argument of function() must be a list or a dict"));
3772 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003773 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003774 }
3775 list = argvars[arg_idx].vval.v_list;
3776 if (list == NULL || list->lv_len == 0)
3777 arg_idx = 0;
3778 }
3779 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003780 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003781 {
3782 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
3783
3784 /* result is a VAR_PARTIAL */
3785 if (pt == NULL)
3786 vim_free(name);
3787 else
3788 {
3789 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
3790 {
3791 listitem_T *li;
3792 int i = 0;
3793 int arg_len = 0;
3794 int lv_len = 0;
3795
3796 if (arg_pt != NULL)
3797 arg_len = arg_pt->pt_argc;
3798 if (list != NULL)
3799 lv_len = list->lv_len;
3800 pt->pt_argc = arg_len + lv_len;
3801 pt->pt_argv = (typval_T *)alloc(
3802 sizeof(typval_T) * pt->pt_argc);
3803 if (pt->pt_argv == NULL)
3804 {
3805 vim_free(pt);
3806 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003807 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003808 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003809 for (i = 0; i < arg_len; i++)
3810 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
3811 if (lv_len > 0)
3812 for (li = list->lv_first; li != NULL;
3813 li = li->li_next)
3814 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003815 }
3816
3817 /* For "function(dict.func, [], dict)" and "func" is a partial
3818 * use "dict". That is backwards compatible. */
3819 if (dict_idx > 0)
3820 {
3821 /* The dict is bound explicitly, pt_auto is FALSE. */
3822 pt->pt_dict = argvars[dict_idx].vval.v_dict;
3823 ++pt->pt_dict->dv_refcount;
3824 }
3825 else if (arg_pt != NULL)
3826 {
3827 /* If the dict was bound automatically the result is also
3828 * bound automatically. */
3829 pt->pt_dict = arg_pt->pt_dict;
3830 pt->pt_auto = arg_pt->pt_auto;
3831 if (pt->pt_dict != NULL)
3832 ++pt->pt_dict->dv_refcount;
3833 }
3834
3835 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003836 if (arg_pt != NULL && arg_pt->pt_func != NULL)
3837 {
3838 pt->pt_func = arg_pt->pt_func;
3839 func_ptr_ref(pt->pt_func);
3840 vim_free(name);
3841 }
3842 else if (is_funcref)
3843 {
3844 pt->pt_func = find_func(trans_name);
3845 func_ptr_ref(pt->pt_func);
3846 vim_free(name);
3847 }
3848 else
3849 {
3850 pt->pt_name = name;
3851 func_ref(name);
3852 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003853 }
3854 rettv->v_type = VAR_PARTIAL;
3855 rettv->vval.v_partial = pt;
3856 }
3857 else
3858 {
3859 /* result is a VAR_FUNC */
3860 rettv->v_type = VAR_FUNC;
3861 rettv->vval.v_string = name;
3862 func_ref(name);
3863 }
3864 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003865theend:
3866 vim_free(trans_name);
3867}
3868
3869/*
3870 * "funcref()" function
3871 */
3872 static void
3873f_funcref(typval_T *argvars, typval_T *rettv)
3874{
3875 common_function(argvars, rettv, TRUE);
3876}
3877
3878/*
3879 * "function()" function
3880 */
3881 static void
3882f_function(typval_T *argvars, typval_T *rettv)
3883{
3884 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003885}
3886
3887/*
3888 * "garbagecollect()" function
3889 */
3890 static void
3891f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
3892{
3893 /* This is postponed until we are back at the toplevel, because we may be
3894 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
3895 want_garbage_collect = TRUE;
3896
3897 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
3898 garbage_collect_at_exit = TRUE;
3899}
3900
3901/*
3902 * "get()" function
3903 */
3904 static void
3905f_get(typval_T *argvars, typval_T *rettv)
3906{
3907 listitem_T *li;
3908 list_T *l;
3909 dictitem_T *di;
3910 dict_T *d;
3911 typval_T *tv = NULL;
3912
3913 if (argvars[0].v_type == VAR_LIST)
3914 {
3915 if ((l = argvars[0].vval.v_list) != NULL)
3916 {
3917 int error = FALSE;
3918
3919 li = list_find(l, (long)get_tv_number_chk(&argvars[1], &error));
3920 if (!error && li != NULL)
3921 tv = &li->li_tv;
3922 }
3923 }
3924 else if (argvars[0].v_type == VAR_DICT)
3925 {
3926 if ((d = argvars[0].vval.v_dict) != NULL)
3927 {
3928 di = dict_find(d, get_tv_string(&argvars[1]), -1);
3929 if (di != NULL)
3930 tv = &di->di_tv;
3931 }
3932 }
3933 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
3934 {
3935 partial_T *pt;
3936 partial_T fref_pt;
3937
3938 if (argvars[0].v_type == VAR_PARTIAL)
3939 pt = argvars[0].vval.v_partial;
3940 else
3941 {
3942 vim_memset(&fref_pt, 0, sizeof(fref_pt));
3943 fref_pt.pt_name = argvars[0].vval.v_string;
3944 pt = &fref_pt;
3945 }
3946
3947 if (pt != NULL)
3948 {
3949 char_u *what = get_tv_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003950 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003951
3952 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
3953 {
3954 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003955 n = partial_name(pt);
3956 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003957 rettv->vval.v_string = NULL;
3958 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003959 {
3960 rettv->vval.v_string = vim_strsave(n);
3961 if (rettv->v_type == VAR_FUNC)
3962 func_ref(rettv->vval.v_string);
3963 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003964 }
3965 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003966 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003967 else if (STRCMP(what, "args") == 0)
3968 {
3969 rettv->v_type = VAR_LIST;
3970 if (rettv_list_alloc(rettv) == OK)
3971 {
3972 int i;
3973
3974 for (i = 0; i < pt->pt_argc; ++i)
3975 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
3976 }
3977 }
3978 else
3979 EMSG2(_(e_invarg2), what);
3980 return;
3981 }
3982 }
3983 else
3984 EMSG2(_(e_listdictarg), "get()");
3985
3986 if (tv == NULL)
3987 {
3988 if (argvars[2].v_type != VAR_UNKNOWN)
3989 copy_tv(&argvars[2], rettv);
3990 }
3991 else
3992 copy_tv(tv, rettv);
3993}
3994
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003995#ifdef FEAT_SIGNS
3996/*
3997 * Returns information about signs placed in a buffer as list of dicts.
3998 */
3999 static void
4000get_buffer_signs(buf_T *buf, list_T *l)
4001{
4002 signlist_T *sign;
4003
4004 for (sign = buf->b_signlist; sign; sign = sign->next)
4005 {
4006 dict_T *d = dict_alloc();
4007
4008 if (d != NULL)
4009 {
4010 dict_add_nr_str(d, "id", sign->id, NULL);
4011 dict_add_nr_str(d, "lnum", sign->lnum, NULL);
Bram Moolenaar6a402ed2016-08-28 14:11:24 +02004012 dict_add_nr_str(d, "name", 0L, sign_typenr2name(sign->typenr));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004013
4014 list_append_dict(l, d);
4015 }
4016 }
4017}
4018#endif
4019
4020/*
4021 * Returns buffer options, variables and other attributes in a dictionary.
4022 */
4023 static dict_T *
4024get_buffer_info(buf_T *buf)
4025{
4026 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004027 tabpage_T *tp;
4028 win_T *wp;
4029 list_T *windows;
4030
4031 dict = dict_alloc();
4032 if (dict == NULL)
4033 return NULL;
4034
Bram Moolenaar33928832016-08-18 21:22:04 +02004035 dict_add_nr_str(dict, "bufnr", buf->b_fnum, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004036 dict_add_nr_str(dict, "name", 0L,
4037 buf->b_ffname != NULL ? buf->b_ffname : (char_u *)"");
Bram Moolenaarf845b872017-01-06 14:04:54 +01004038 dict_add_nr_str(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4039 : buflist_findlnum(buf), NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004040 dict_add_nr_str(dict, "loaded", buf->b_ml.ml_mfp != NULL, NULL);
4041 dict_add_nr_str(dict, "listed", buf->b_p_bl, NULL);
4042 dict_add_nr_str(dict, "changed", bufIsChanged(buf), NULL);
Bram Moolenaar95c526e2017-02-25 14:59:34 +01004043 dict_add_nr_str(dict, "changedtick", CHANGEDTICK(buf), NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004044 dict_add_nr_str(dict, "hidden",
4045 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0,
4046 NULL);
4047
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004048 /* Get a reference to buffer variables */
4049 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004050
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004051 /* List of windows displaying this buffer */
4052 windows = list_alloc();
4053 if (windows != NULL)
4054 {
4055 FOR_ALL_TAB_WINDOWS(tp, wp)
4056 if (wp->w_buffer == buf)
4057 list_append_number(windows, (varnumber_T)wp->w_id);
4058 dict_add_list(dict, "windows", windows);
4059 }
4060
4061#ifdef FEAT_SIGNS
4062 if (buf->b_signlist != NULL)
4063 {
4064 /* List of signs placed in this buffer */
4065 list_T *signs = list_alloc();
4066 if (signs != NULL)
4067 {
4068 get_buffer_signs(buf, signs);
4069 dict_add_list(dict, "signs", signs);
4070 }
4071 }
4072#endif
4073
4074 return dict;
4075}
4076
4077/*
4078 * "getbufinfo()" function
4079 */
4080 static void
4081f_getbufinfo(typval_T *argvars, typval_T *rettv)
4082{
4083 buf_T *buf = NULL;
4084 buf_T *argbuf = NULL;
4085 dict_T *d;
4086 int filtered = FALSE;
4087 int sel_buflisted = FALSE;
4088 int sel_bufloaded = FALSE;
4089
4090 if (rettv_list_alloc(rettv) != OK)
4091 return;
4092
4093 /* List of all the buffers or selected buffers */
4094 if (argvars[0].v_type == VAR_DICT)
4095 {
4096 dict_T *sel_d = argvars[0].vval.v_dict;
4097
4098 if (sel_d != NULL)
4099 {
4100 dictitem_T *di;
4101
4102 filtered = TRUE;
4103
4104 di = dict_find(sel_d, (char_u *)"buflisted", -1);
4105 if (di != NULL && get_tv_number(&di->di_tv))
4106 sel_buflisted = TRUE;
4107
4108 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
4109 if (di != NULL && get_tv_number(&di->di_tv))
4110 sel_bufloaded = TRUE;
4111 }
4112 }
4113 else if (argvars[0].v_type != VAR_UNKNOWN)
4114 {
4115 /* Information about one buffer. Argument specifies the buffer */
4116 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4117 ++emsg_off;
4118 argbuf = get_buf_tv(&argvars[0], FALSE);
4119 --emsg_off;
4120 if (argbuf == NULL)
4121 return;
4122 }
4123
4124 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004125 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004126 {
4127 if (argbuf != NULL && argbuf != buf)
4128 continue;
4129 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
4130 || (sel_buflisted && !buf->b_p_bl)))
4131 continue;
4132
4133 d = get_buffer_info(buf);
4134 if (d != NULL)
4135 list_append_dict(rettv->vval.v_list, d);
4136 if (argbuf != NULL)
4137 return;
4138 }
4139}
4140
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004141static void get_buffer_lines(buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv);
4142
4143/*
4144 * Get line or list of lines from buffer "buf" into "rettv".
4145 * Return a range (from start to end) of lines in rettv from the specified
4146 * buffer.
4147 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4148 */
4149 static void
4150get_buffer_lines(
4151 buf_T *buf,
4152 linenr_T start,
4153 linenr_T end,
4154 int retlist,
4155 typval_T *rettv)
4156{
4157 char_u *p;
4158
4159 rettv->v_type = VAR_STRING;
4160 rettv->vval.v_string = NULL;
4161 if (retlist && rettv_list_alloc(rettv) == FAIL)
4162 return;
4163
4164 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4165 return;
4166
4167 if (!retlist)
4168 {
4169 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4170 p = ml_get_buf(buf, start, FALSE);
4171 else
4172 p = (char_u *)"";
4173 rettv->vval.v_string = vim_strsave(p);
4174 }
4175 else
4176 {
4177 if (end < start)
4178 return;
4179
4180 if (start < 1)
4181 start = 1;
4182 if (end > buf->b_ml.ml_line_count)
4183 end = buf->b_ml.ml_line_count;
4184 while (start <= end)
4185 if (list_append_string(rettv->vval.v_list,
4186 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4187 break;
4188 }
4189}
4190
4191/*
4192 * Get the lnum from the first argument.
4193 * Also accepts "$", then "buf" is used.
4194 * Returns 0 on error.
4195 */
4196 static linenr_T
4197get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
4198{
4199 if (argvars[0].v_type == VAR_STRING
4200 && argvars[0].vval.v_string != NULL
4201 && argvars[0].vval.v_string[0] == '$'
4202 && buf != NULL)
4203 return buf->b_ml.ml_line_count;
4204 return (linenr_T)get_tv_number_chk(&argvars[0], NULL);
4205}
4206
4207/*
4208 * "getbufline()" function
4209 */
4210 static void
4211f_getbufline(typval_T *argvars, typval_T *rettv)
4212{
4213 linenr_T lnum;
4214 linenr_T end;
4215 buf_T *buf;
4216
4217 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4218 ++emsg_off;
4219 buf = get_buf_tv(&argvars[0], FALSE);
4220 --emsg_off;
4221
4222 lnum = get_tv_lnum_buf(&argvars[1], buf);
4223 if (argvars[2].v_type == VAR_UNKNOWN)
4224 end = lnum;
4225 else
4226 end = get_tv_lnum_buf(&argvars[2], buf);
4227
4228 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4229}
4230
4231/*
4232 * "getbufvar()" function
4233 */
4234 static void
4235f_getbufvar(typval_T *argvars, typval_T *rettv)
4236{
4237 buf_T *buf;
4238 buf_T *save_curbuf;
4239 char_u *varname;
4240 dictitem_T *v;
4241 int done = FALSE;
4242
4243 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4244 varname = get_tv_string_chk(&argvars[1]);
4245 ++emsg_off;
4246 buf = get_buf_tv(&argvars[0], FALSE);
4247
4248 rettv->v_type = VAR_STRING;
4249 rettv->vval.v_string = NULL;
4250
4251 if (buf != NULL && varname != NULL)
4252 {
4253 /* set curbuf to be our buf, temporarily */
4254 save_curbuf = curbuf;
4255 curbuf = buf;
4256
Bram Moolenaar30567352016-08-27 21:25:44 +02004257 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004258 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004259 if (varname[1] == NUL)
4260 {
4261 /* get all buffer-local options in a dict */
4262 dict_T *opts = get_winbuf_options(TRUE);
4263
4264 if (opts != NULL)
4265 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004266 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004267 done = TRUE;
4268 }
4269 }
4270 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4271 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004272 done = TRUE;
4273 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004274 else
4275 {
4276 /* Look up the variable. */
4277 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4278 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4279 'b', varname, FALSE);
4280 if (v != NULL)
4281 {
4282 copy_tv(&v->di_tv, rettv);
4283 done = TRUE;
4284 }
4285 }
4286
4287 /* restore previous notion of curbuf */
4288 curbuf = save_curbuf;
4289 }
4290
4291 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4292 /* use the default value */
4293 copy_tv(&argvars[2], rettv);
4294
4295 --emsg_off;
4296}
4297
4298/*
4299 * "getchar()" function
4300 */
4301 static void
4302f_getchar(typval_T *argvars, typval_T *rettv)
4303{
4304 varnumber_T n;
4305 int error = FALSE;
4306
4307 /* Position the cursor. Needed after a message that ends in a space. */
4308 windgoto(msg_row, msg_col);
4309
4310 ++no_mapping;
4311 ++allow_keys;
4312 for (;;)
4313 {
4314 if (argvars[0].v_type == VAR_UNKNOWN)
4315 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004316 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004317 else if (get_tv_number_chk(&argvars[0], &error) == 1)
4318 /* getchar(1): only check if char avail */
4319 n = vpeekc_any();
4320 else if (error || vpeekc_any() == NUL)
4321 /* illegal argument or getchar(0) and no char avail: return zero */
4322 n = 0;
4323 else
4324 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004325 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004326
4327 if (n == K_IGNORE)
4328 continue;
4329 break;
4330 }
4331 --no_mapping;
4332 --allow_keys;
4333
4334 set_vim_var_nr(VV_MOUSE_WIN, 0);
4335 set_vim_var_nr(VV_MOUSE_WINID, 0);
4336 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4337 set_vim_var_nr(VV_MOUSE_COL, 0);
4338
4339 rettv->vval.v_number = n;
4340 if (IS_SPECIAL(n) || mod_mask != 0)
4341 {
4342 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4343 int i = 0;
4344
4345 /* Turn a special key into three bytes, plus modifier. */
4346 if (mod_mask != 0)
4347 {
4348 temp[i++] = K_SPECIAL;
4349 temp[i++] = KS_MODIFIER;
4350 temp[i++] = mod_mask;
4351 }
4352 if (IS_SPECIAL(n))
4353 {
4354 temp[i++] = K_SPECIAL;
4355 temp[i++] = K_SECOND(n);
4356 temp[i++] = K_THIRD(n);
4357 }
4358#ifdef FEAT_MBYTE
4359 else if (has_mbyte)
4360 i += (*mb_char2bytes)(n, temp + i);
4361#endif
4362 else
4363 temp[i++] = n;
4364 temp[i++] = NUL;
4365 rettv->v_type = VAR_STRING;
4366 rettv->vval.v_string = vim_strsave(temp);
4367
4368#ifdef FEAT_MOUSE
4369 if (is_mouse_key(n))
4370 {
4371 int row = mouse_row;
4372 int col = mouse_col;
4373 win_T *win;
4374 linenr_T lnum;
4375# ifdef FEAT_WINDOWS
4376 win_T *wp;
4377# endif
4378 int winnr = 1;
4379
4380 if (row >= 0 && col >= 0)
4381 {
4382 /* Find the window at the mouse coordinates and compute the
4383 * text position. */
4384 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004385 if (win == NULL)
4386 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004387 (void)mouse_comp_pos(win, &row, &col, &lnum);
4388# ifdef FEAT_WINDOWS
4389 for (wp = firstwin; wp != win; wp = wp->w_next)
4390 ++winnr;
4391# endif
4392 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4393 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4394 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4395 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4396 }
4397 }
4398#endif
4399 }
4400}
4401
4402/*
4403 * "getcharmod()" function
4404 */
4405 static void
4406f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4407{
4408 rettv->vval.v_number = mod_mask;
4409}
4410
4411/*
4412 * "getcharsearch()" function
4413 */
4414 static void
4415f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4416{
4417 if (rettv_dict_alloc(rettv) != FAIL)
4418 {
4419 dict_T *dict = rettv->vval.v_dict;
4420
4421 dict_add_nr_str(dict, "char", 0L, last_csearch());
4422 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
4423 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
4424 }
4425}
4426
4427/*
4428 * "getcmdline()" function
4429 */
4430 static void
4431f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4432{
4433 rettv->v_type = VAR_STRING;
4434 rettv->vval.v_string = get_cmdline_str();
4435}
4436
4437/*
4438 * "getcmdpos()" function
4439 */
4440 static void
4441f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4442{
4443 rettv->vval.v_number = get_cmdline_pos() + 1;
4444}
4445
4446/*
4447 * "getcmdtype()" function
4448 */
4449 static void
4450f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4451{
4452 rettv->v_type = VAR_STRING;
4453 rettv->vval.v_string = alloc(2);
4454 if (rettv->vval.v_string != NULL)
4455 {
4456 rettv->vval.v_string[0] = get_cmdline_type();
4457 rettv->vval.v_string[1] = NUL;
4458 }
4459}
4460
4461/*
4462 * "getcmdwintype()" function
4463 */
4464 static void
4465f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4466{
4467 rettv->v_type = VAR_STRING;
4468 rettv->vval.v_string = NULL;
4469#ifdef FEAT_CMDWIN
4470 rettv->vval.v_string = alloc(2);
4471 if (rettv->vval.v_string != NULL)
4472 {
4473 rettv->vval.v_string[0] = cmdwin_type;
4474 rettv->vval.v_string[1] = NUL;
4475 }
4476#endif
4477}
4478
4479#if defined(FEAT_CMDL_COMPL)
4480/*
4481 * "getcompletion()" function
4482 */
4483 static void
4484f_getcompletion(typval_T *argvars, typval_T *rettv)
4485{
4486 char_u *pat;
4487 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004488 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004489 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
4490 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004491
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004492 if (argvars[2].v_type != VAR_UNKNOWN)
4493 filtered = get_tv_number_chk(&argvars[2], NULL);
4494
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004495 if (p_wic)
4496 options |= WILD_ICASE;
4497
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004498 /* For filtered results, 'wildignore' is used */
4499 if (!filtered)
4500 options |= WILD_KEEP_ALL;
4501
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004502 ExpandInit(&xpc);
4503 xpc.xp_pattern = get_tv_string(&argvars[0]);
4504 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4505 xpc.xp_context = cmdcomplete_str_to_type(get_tv_string(&argvars[1]));
4506 if (xpc.xp_context == EXPAND_NOTHING)
4507 {
4508 if (argvars[1].v_type == VAR_STRING)
4509 EMSG2(_(e_invarg2), argvars[1].vval.v_string);
4510 else
4511 EMSG(_(e_invarg));
4512 return;
4513 }
4514
4515# if defined(FEAT_MENU)
4516 if (xpc.xp_context == EXPAND_MENUS)
4517 {
4518 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4519 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4520 }
4521# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02004522#ifdef FEAT_CSCOPE
4523 if (xpc.xp_context == EXPAND_CSCOPE)
4524 {
4525 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4526 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4527 }
4528#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02004529#ifdef FEAT_SIGNS
4530 if (xpc.xp_context == EXPAND_SIGN)
4531 {
4532 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4533 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4534 }
4535#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004536
4537 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4538 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
4539 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004540 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004541
4542 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4543
4544 for (i = 0; i < xpc.xp_numfiles; i++)
4545 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4546 }
4547 vim_free(pat);
4548 ExpandCleanup(&xpc);
4549}
4550#endif
4551
4552/*
4553 * "getcwd()" function
4554 */
4555 static void
4556f_getcwd(typval_T *argvars, typval_T *rettv)
4557{
4558 win_T *wp = NULL;
4559 char_u *cwd;
4560
4561 rettv->v_type = VAR_STRING;
4562 rettv->vval.v_string = NULL;
4563
4564 wp = find_tabwin(&argvars[0], &argvars[1]);
4565 if (wp != NULL)
4566 {
4567 if (wp->w_localdir != NULL)
4568 rettv->vval.v_string = vim_strsave(wp->w_localdir);
4569 else if (globaldir != NULL)
4570 rettv->vval.v_string = vim_strsave(globaldir);
4571 else
4572 {
4573 cwd = alloc(MAXPATHL);
4574 if (cwd != NULL)
4575 {
4576 if (mch_dirname(cwd, MAXPATHL) != FAIL)
4577 rettv->vval.v_string = vim_strsave(cwd);
4578 vim_free(cwd);
4579 }
4580 }
4581#ifdef BACKSLASH_IN_FILENAME
4582 if (rettv->vval.v_string != NULL)
4583 slash_adjust(rettv->vval.v_string);
4584#endif
4585 }
4586}
4587
4588/*
4589 * "getfontname()" function
4590 */
4591 static void
4592f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4593{
4594 rettv->v_type = VAR_STRING;
4595 rettv->vval.v_string = NULL;
4596#ifdef FEAT_GUI
4597 if (gui.in_use)
4598 {
4599 GuiFont font;
4600 char_u *name = NULL;
4601
4602 if (argvars[0].v_type == VAR_UNKNOWN)
4603 {
4604 /* Get the "Normal" font. Either the name saved by
4605 * hl_set_font_name() or from the font ID. */
4606 font = gui.norm_font;
4607 name = hl_get_font_name();
4608 }
4609 else
4610 {
4611 name = get_tv_string(&argvars[0]);
4612 if (STRCMP(name, "*") == 0) /* don't use font dialog */
4613 return;
4614 font = gui_mch_get_font(name, FALSE);
4615 if (font == NOFONT)
4616 return; /* Invalid font name, return empty string. */
4617 }
4618 rettv->vval.v_string = gui_mch_get_fontname(font, name);
4619 if (argvars[0].v_type != VAR_UNKNOWN)
4620 gui_mch_free_font(font);
4621 }
4622#endif
4623}
4624
4625/*
4626 * "getfperm({fname})" function
4627 */
4628 static void
4629f_getfperm(typval_T *argvars, typval_T *rettv)
4630{
4631 char_u *fname;
4632 stat_T st;
4633 char_u *perm = NULL;
4634 char_u flags[] = "rwx";
4635 int i;
4636
4637 fname = get_tv_string(&argvars[0]);
4638
4639 rettv->v_type = VAR_STRING;
4640 if (mch_stat((char *)fname, &st) >= 0)
4641 {
4642 perm = vim_strsave((char_u *)"---------");
4643 if (perm != NULL)
4644 {
4645 for (i = 0; i < 9; i++)
4646 {
4647 if (st.st_mode & (1 << (8 - i)))
4648 perm[i] = flags[i % 3];
4649 }
4650 }
4651 }
4652 rettv->vval.v_string = perm;
4653}
4654
4655/*
4656 * "getfsize({fname})" function
4657 */
4658 static void
4659f_getfsize(typval_T *argvars, typval_T *rettv)
4660{
4661 char_u *fname;
4662 stat_T st;
4663
4664 fname = get_tv_string(&argvars[0]);
4665
4666 rettv->v_type = VAR_NUMBER;
4667
4668 if (mch_stat((char *)fname, &st) >= 0)
4669 {
4670 if (mch_isdir(fname))
4671 rettv->vval.v_number = 0;
4672 else
4673 {
4674 rettv->vval.v_number = (varnumber_T)st.st_size;
4675
4676 /* non-perfect check for overflow */
4677 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
4678 rettv->vval.v_number = -2;
4679 }
4680 }
4681 else
4682 rettv->vval.v_number = -1;
4683}
4684
4685/*
4686 * "getftime({fname})" function
4687 */
4688 static void
4689f_getftime(typval_T *argvars, typval_T *rettv)
4690{
4691 char_u *fname;
4692 stat_T st;
4693
4694 fname = get_tv_string(&argvars[0]);
4695
4696 if (mch_stat((char *)fname, &st) >= 0)
4697 rettv->vval.v_number = (varnumber_T)st.st_mtime;
4698 else
4699 rettv->vval.v_number = -1;
4700}
4701
4702/*
4703 * "getftype({fname})" function
4704 */
4705 static void
4706f_getftype(typval_T *argvars, typval_T *rettv)
4707{
4708 char_u *fname;
4709 stat_T st;
4710 char_u *type = NULL;
4711 char *t;
4712
4713 fname = get_tv_string(&argvars[0]);
4714
4715 rettv->v_type = VAR_STRING;
4716 if (mch_lstat((char *)fname, &st) >= 0)
4717 {
4718#ifdef S_ISREG
4719 if (S_ISREG(st.st_mode))
4720 t = "file";
4721 else if (S_ISDIR(st.st_mode))
4722 t = "dir";
4723# ifdef S_ISLNK
4724 else if (S_ISLNK(st.st_mode))
4725 t = "link";
4726# endif
4727# ifdef S_ISBLK
4728 else if (S_ISBLK(st.st_mode))
4729 t = "bdev";
4730# endif
4731# ifdef S_ISCHR
4732 else if (S_ISCHR(st.st_mode))
4733 t = "cdev";
4734# endif
4735# ifdef S_ISFIFO
4736 else if (S_ISFIFO(st.st_mode))
4737 t = "fifo";
4738# endif
4739# ifdef S_ISSOCK
4740 else if (S_ISSOCK(st.st_mode))
4741 t = "fifo";
4742# endif
4743 else
4744 t = "other";
4745#else
4746# ifdef S_IFMT
4747 switch (st.st_mode & S_IFMT)
4748 {
4749 case S_IFREG: t = "file"; break;
4750 case S_IFDIR: t = "dir"; break;
4751# ifdef S_IFLNK
4752 case S_IFLNK: t = "link"; break;
4753# endif
4754# ifdef S_IFBLK
4755 case S_IFBLK: t = "bdev"; break;
4756# endif
4757# ifdef S_IFCHR
4758 case S_IFCHR: t = "cdev"; break;
4759# endif
4760# ifdef S_IFIFO
4761 case S_IFIFO: t = "fifo"; break;
4762# endif
4763# ifdef S_IFSOCK
4764 case S_IFSOCK: t = "socket"; break;
4765# endif
4766 default: t = "other";
4767 }
4768# else
4769 if (mch_isdir(fname))
4770 t = "dir";
4771 else
4772 t = "file";
4773# endif
4774#endif
4775 type = vim_strsave((char_u *)t);
4776 }
4777 rettv->vval.v_string = type;
4778}
4779
4780/*
4781 * "getline(lnum, [end])" function
4782 */
4783 static void
4784f_getline(typval_T *argvars, typval_T *rettv)
4785{
4786 linenr_T lnum;
4787 linenr_T end;
4788 int retlist;
4789
4790 lnum = get_tv_lnum(argvars);
4791 if (argvars[1].v_type == VAR_UNKNOWN)
4792 {
4793 end = 0;
4794 retlist = FALSE;
4795 }
4796 else
4797 {
4798 end = get_tv_lnum(&argvars[1]);
4799 retlist = TRUE;
4800 }
4801
4802 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
4803}
4804
Bram Moolenaar4ae20952016-08-13 15:29:14 +02004805#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02004806 static void
4807get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
4808{
Bram Moolenaard823fa92016-08-12 16:29:27 +02004809 if (what_arg->v_type == VAR_UNKNOWN)
4810 {
4811 if (rettv_list_alloc(rettv) == OK)
4812 if (is_qf || wp != NULL)
4813 (void)get_errorlist(wp, -1, rettv->vval.v_list);
4814 }
4815 else
4816 {
4817 if (rettv_dict_alloc(rettv) == OK)
4818 if (is_qf || (wp != NULL))
4819 {
4820 if (what_arg->v_type == VAR_DICT)
4821 {
4822 dict_T *d = what_arg->vval.v_dict;
4823
4824 if (d != NULL)
4825 get_errorlist_properties(wp, d, rettv->vval.v_dict);
4826 }
4827 else
4828 EMSG(_(e_dictreq));
4829 }
4830 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004831}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02004832#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02004833
4834/*
4835 * "getloclist()" function
4836 */
4837 static void
4838f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4839{
4840#ifdef FEAT_QUICKFIX
4841 win_T *wp;
4842
4843 wp = find_win_by_nr(&argvars[0], NULL);
4844 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
4845#endif
4846}
4847
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004848/*
4849 * "getmatches()" function
4850 */
4851 static void
4852f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4853{
4854#ifdef FEAT_SEARCH_EXTRA
4855 dict_T *dict;
4856 matchitem_T *cur = curwin->w_match_head;
4857 int i;
4858
4859 if (rettv_list_alloc(rettv) == OK)
4860 {
4861 while (cur != NULL)
4862 {
4863 dict = dict_alloc();
4864 if (dict == NULL)
4865 return;
4866 if (cur->match.regprog == NULL)
4867 {
4868 /* match added with matchaddpos() */
4869 for (i = 0; i < MAXPOSMATCH; ++i)
4870 {
4871 llpos_T *llpos;
4872 char buf[6];
4873 list_T *l;
4874
4875 llpos = &cur->pos.pos[i];
4876 if (llpos->lnum == 0)
4877 break;
4878 l = list_alloc();
4879 if (l == NULL)
4880 break;
4881 list_append_number(l, (varnumber_T)llpos->lnum);
4882 if (llpos->col > 0)
4883 {
4884 list_append_number(l, (varnumber_T)llpos->col);
4885 list_append_number(l, (varnumber_T)llpos->len);
4886 }
4887 sprintf(buf, "pos%d", i + 1);
4888 dict_add_list(dict, buf, l);
4889 }
4890 }
4891 else
4892 {
4893 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
4894 }
4895 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
4896 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
4897 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
4898# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
4899 if (cur->conceal_char)
4900 {
4901 char_u buf[MB_MAXBYTES + 1];
4902
4903 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
4904 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
4905 }
4906# endif
4907 list_append_dict(rettv->vval.v_list, dict);
4908 cur = cur->next;
4909 }
4910 }
4911#endif
4912}
4913
4914/*
4915 * "getpid()" function
4916 */
4917 static void
4918f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
4919{
4920 rettv->vval.v_number = mch_get_pid();
4921}
4922
4923 static void
4924getpos_both(
4925 typval_T *argvars,
4926 typval_T *rettv,
4927 int getcurpos)
4928{
4929 pos_T *fp;
4930 list_T *l;
4931 int fnum = -1;
4932
4933 if (rettv_list_alloc(rettv) == OK)
4934 {
4935 l = rettv->vval.v_list;
4936 if (getcurpos)
4937 fp = &curwin->w_cursor;
4938 else
4939 fp = var2fpos(&argvars[0], TRUE, &fnum);
4940 if (fnum != -1)
4941 list_append_number(l, (varnumber_T)fnum);
4942 else
4943 list_append_number(l, (varnumber_T)0);
4944 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
4945 : (varnumber_T)0);
4946 list_append_number(l, (fp != NULL)
4947 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
4948 : (varnumber_T)0);
4949 list_append_number(l,
4950#ifdef FEAT_VIRTUALEDIT
4951 (fp != NULL) ? (varnumber_T)fp->coladd :
4952#endif
4953 (varnumber_T)0);
4954 if (getcurpos)
4955 {
4956 update_curswant();
4957 list_append_number(l, curwin->w_curswant == MAXCOL ?
4958 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
4959 }
4960 }
4961 else
4962 rettv->vval.v_number = FALSE;
4963}
4964
4965
4966/*
4967 * "getcurpos()" function
4968 */
4969 static void
4970f_getcurpos(typval_T *argvars, typval_T *rettv)
4971{
4972 getpos_both(argvars, rettv, TRUE);
4973}
4974
4975/*
4976 * "getpos(string)" function
4977 */
4978 static void
4979f_getpos(typval_T *argvars, typval_T *rettv)
4980{
4981 getpos_both(argvars, rettv, FALSE);
4982}
4983
4984/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004985 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004986 */
4987 static void
4988f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4989{
4990#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02004991 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004992#endif
4993}
4994
4995/*
4996 * "getreg()" function
4997 */
4998 static void
4999f_getreg(typval_T *argvars, typval_T *rettv)
5000{
5001 char_u *strregname;
5002 int regname;
5003 int arg2 = FALSE;
5004 int return_list = FALSE;
5005 int error = FALSE;
5006
5007 if (argvars[0].v_type != VAR_UNKNOWN)
5008 {
5009 strregname = get_tv_string_chk(&argvars[0]);
5010 error = strregname == NULL;
5011 if (argvars[1].v_type != VAR_UNKNOWN)
5012 {
5013 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
5014 if (!error && argvars[2].v_type != VAR_UNKNOWN)
5015 return_list = (int)get_tv_number_chk(&argvars[2], &error);
5016 }
5017 }
5018 else
5019 strregname = get_vim_var_str(VV_REG);
5020
5021 if (error)
5022 return;
5023
5024 regname = (strregname == NULL ? '"' : *strregname);
5025 if (regname == 0)
5026 regname = '"';
5027
5028 if (return_list)
5029 {
5030 rettv->v_type = VAR_LIST;
5031 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5032 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5033 if (rettv->vval.v_list == NULL)
5034 (void)rettv_list_alloc(rettv);
5035 else
5036 ++rettv->vval.v_list->lv_refcount;
5037 }
5038 else
5039 {
5040 rettv->v_type = VAR_STRING;
5041 rettv->vval.v_string = get_reg_contents(regname,
5042 arg2 ? GREG_EXPR_SRC : 0);
5043 }
5044}
5045
5046/*
5047 * "getregtype()" function
5048 */
5049 static void
5050f_getregtype(typval_T *argvars, typval_T *rettv)
5051{
5052 char_u *strregname;
5053 int regname;
5054 char_u buf[NUMBUFLEN + 2];
5055 long reglen = 0;
5056
5057 if (argvars[0].v_type != VAR_UNKNOWN)
5058 {
5059 strregname = get_tv_string_chk(&argvars[0]);
5060 if (strregname == NULL) /* type error; errmsg already given */
5061 {
5062 rettv->v_type = VAR_STRING;
5063 rettv->vval.v_string = NULL;
5064 return;
5065 }
5066 }
5067 else
5068 /* Default to v:register */
5069 strregname = get_vim_var_str(VV_REG);
5070
5071 regname = (strregname == NULL ? '"' : *strregname);
5072 if (regname == 0)
5073 regname = '"';
5074
5075 buf[0] = NUL;
5076 buf[1] = NUL;
5077 switch (get_reg_type(regname, &reglen))
5078 {
5079 case MLINE: buf[0] = 'V'; break;
5080 case MCHAR: buf[0] = 'v'; break;
5081 case MBLOCK:
5082 buf[0] = Ctrl_V;
5083 sprintf((char *)buf + 1, "%ld", reglen + 1);
5084 break;
5085 }
5086 rettv->v_type = VAR_STRING;
5087 rettv->vval.v_string = vim_strsave(buf);
5088}
5089
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005090#ifdef FEAT_WINDOWS
5091/*
5092 * Returns information (variables, options, etc.) about a tab page
5093 * as a dictionary.
5094 */
5095 static dict_T *
5096get_tabpage_info(tabpage_T *tp, int tp_idx)
5097{
5098 win_T *wp;
5099 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005100 list_T *l;
5101
5102 dict = dict_alloc();
5103 if (dict == NULL)
5104 return NULL;
5105
Bram Moolenaar33928832016-08-18 21:22:04 +02005106 dict_add_nr_str(dict, "tabnr", tp_idx, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005107
5108 l = list_alloc();
5109 if (l != NULL)
5110 {
5111 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5112 wp; wp = wp->w_next)
5113 list_append_number(l, (varnumber_T)wp->w_id);
5114 dict_add_list(dict, "windows", l);
5115 }
5116
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005117 /* Make a reference to tabpage variables */
5118 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005119
5120 return dict;
5121}
5122#endif
5123
5124/*
5125 * "gettabinfo()" function
5126 */
5127 static void
5128f_gettabinfo(typval_T *argvars, typval_T *rettv)
5129{
5130#ifdef FEAT_WINDOWS
5131 tabpage_T *tp, *tparg = NULL;
5132 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005133 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005134
5135 if (rettv_list_alloc(rettv) != OK)
5136 return;
5137
5138 if (argvars[0].v_type != VAR_UNKNOWN)
5139 {
5140 /* Information about one tab page */
5141 tparg = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5142 if (tparg == NULL)
5143 return;
5144 }
5145
5146 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005147 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005148 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005149 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005150 if (tparg != NULL && tp != tparg)
5151 continue;
5152 d = get_tabpage_info(tp, tpnr);
5153 if (d != NULL)
5154 list_append_dict(rettv->vval.v_list, d);
5155 if (tparg != NULL)
5156 return;
5157 }
5158#endif
5159}
5160
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005161/*
5162 * "gettabvar()" function
5163 */
5164 static void
5165f_gettabvar(typval_T *argvars, typval_T *rettv)
5166{
5167 win_T *oldcurwin;
5168 tabpage_T *tp, *oldtabpage;
5169 dictitem_T *v;
5170 char_u *varname;
5171 int done = FALSE;
5172
5173 rettv->v_type = VAR_STRING;
5174 rettv->vval.v_string = NULL;
5175
5176 varname = get_tv_string_chk(&argvars[1]);
5177 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5178 if (tp != NULL && varname != NULL)
5179 {
5180 /* Set tp to be our tabpage, temporarily. Also set the window to the
5181 * first window in the tabpage, otherwise the window is not valid. */
5182 if (switch_win(&oldcurwin, &oldtabpage,
5183 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
5184 == OK)
5185 {
5186 /* look up the variable */
5187 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5188 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5189 if (v != NULL)
5190 {
5191 copy_tv(&v->di_tv, rettv);
5192 done = TRUE;
5193 }
5194 }
5195
5196 /* restore previous notion of curwin */
5197 restore_win(oldcurwin, oldtabpage, TRUE);
5198 }
5199
5200 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5201 copy_tv(&argvars[2], rettv);
5202}
5203
5204/*
5205 * "gettabwinvar()" function
5206 */
5207 static void
5208f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5209{
5210 getwinvar(argvars, rettv, 1);
5211}
5212
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005213#ifdef FEAT_WINDOWS
5214/*
5215 * Returns information about a window as a dictionary.
5216 */
5217 static dict_T *
5218get_win_info(win_T *wp, short tpnr, short winnr)
5219{
5220 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005221
5222 dict = dict_alloc();
5223 if (dict == NULL)
5224 return NULL;
5225
Bram Moolenaar33928832016-08-18 21:22:04 +02005226 dict_add_nr_str(dict, "tabnr", tpnr, NULL);
5227 dict_add_nr_str(dict, "winnr", winnr, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005228 dict_add_nr_str(dict, "winid", wp->w_id, NULL);
5229 dict_add_nr_str(dict, "height", wp->w_height, NULL);
5230 dict_add_nr_str(dict, "width", wp->w_width, NULL);
Bram Moolenaar33928832016-08-18 21:22:04 +02005231 dict_add_nr_str(dict, "bufnr", wp->w_buffer->b_fnum, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005232
Bram Moolenaar69905d12017-08-13 18:14:47 +02005233#ifdef FEAT_TERMINAL
5234 dict_add_nr_str(dict, "terminal", bt_terminal(wp->w_buffer), NULL);
5235#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005236#ifdef FEAT_QUICKFIX
5237 dict_add_nr_str(dict, "quickfix", bt_quickfix(wp->w_buffer), NULL);
5238 dict_add_nr_str(dict, "loclist",
5239 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL), NULL);
5240#endif
5241
Bram Moolenaar30567352016-08-27 21:25:44 +02005242 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005243 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005244
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005245 return dict;
5246}
5247#endif
5248
5249/*
5250 * "getwininfo()" function
5251 */
5252 static void
5253f_getwininfo(typval_T *argvars, typval_T *rettv)
5254{
5255#ifdef FEAT_WINDOWS
5256 tabpage_T *tp;
5257 win_T *wp = NULL, *wparg = NULL;
5258 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005259 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005260#endif
5261
5262 if (rettv_list_alloc(rettv) != OK)
5263 return;
5264
5265#ifdef FEAT_WINDOWS
5266 if (argvars[0].v_type != VAR_UNKNOWN)
5267 {
5268 wparg = win_id2wp(argvars);
5269 if (wparg == NULL)
5270 return;
5271 }
5272
5273 /* Collect information about either all the windows across all the tab
5274 * pages or one particular window.
5275 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005276 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005277 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005278 tabnr++;
5279 winnr = 0;
5280 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005281 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005282 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005283 if (wparg != NULL && wp != wparg)
5284 continue;
5285 d = get_win_info(wp, tabnr, winnr);
5286 if (d != NULL)
5287 list_append_dict(rettv->vval.v_list, d);
5288 if (wparg != NULL)
5289 /* found information about a specific window */
5290 return;
5291 }
5292 }
5293#endif
5294}
5295
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005296/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005297 * "win_findbuf()" function
5298 */
5299 static void
5300f_win_findbuf(typval_T *argvars, typval_T *rettv)
5301{
5302 if (rettv_list_alloc(rettv) != FAIL)
5303 win_findbuf(argvars, rettv->vval.v_list);
5304}
5305
5306/*
5307 * "win_getid()" function
5308 */
5309 static void
5310f_win_getid(typval_T *argvars, typval_T *rettv)
5311{
5312 rettv->vval.v_number = win_getid(argvars);
5313}
5314
5315/*
5316 * "win_gotoid()" function
5317 */
5318 static void
5319f_win_gotoid(typval_T *argvars, typval_T *rettv)
5320{
5321 rettv->vval.v_number = win_gotoid(argvars);
5322}
5323
5324/*
5325 * "win_id2tabwin()" function
5326 */
5327 static void
5328f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5329{
5330 if (rettv_list_alloc(rettv) != FAIL)
5331 win_id2tabwin(argvars, rettv->vval.v_list);
5332}
5333
5334/*
5335 * "win_id2win()" function
5336 */
5337 static void
5338f_win_id2win(typval_T *argvars, typval_T *rettv)
5339{
5340 rettv->vval.v_number = win_id2win(argvars);
5341}
5342
5343/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005344 * "getwinposx()" function
5345 */
5346 static void
5347f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5348{
5349 rettv->vval.v_number = -1;
5350#ifdef FEAT_GUI
5351 if (gui.in_use)
5352 {
5353 int x, y;
5354
5355 if (gui_mch_get_winpos(&x, &y) == OK)
5356 rettv->vval.v_number = x;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005357 return;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005358 }
5359#endif
5360#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5361 {
5362 int x, y;
5363
5364 if (term_get_winpos(&x, &y) == OK)
5365 rettv->vval.v_number = x;
5366 }
5367#endif
5368}
5369
5370/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005371 * "getwinposy()" function
5372 */
5373 static void
5374f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5375{
5376 rettv->vval.v_number = -1;
5377#ifdef FEAT_GUI
5378 if (gui.in_use)
5379 {
5380 int x, y;
5381
5382 if (gui_mch_get_winpos(&x, &y) == OK)
5383 rettv->vval.v_number = y;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005384 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005385 }
5386#endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005387#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5388 {
5389 int x, y;
5390
5391 if (term_get_winpos(&x, &y) == OK)
5392 rettv->vval.v_number = y;
5393 }
5394#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005395}
5396
5397/*
5398 * "getwinvar()" function
5399 */
5400 static void
5401f_getwinvar(typval_T *argvars, typval_T *rettv)
5402{
5403 getwinvar(argvars, rettv, 0);
5404}
5405
5406/*
5407 * "glob()" function
5408 */
5409 static void
5410f_glob(typval_T *argvars, typval_T *rettv)
5411{
5412 int options = WILD_SILENT|WILD_USE_NL;
5413 expand_T xpc;
5414 int error = FALSE;
5415
5416 /* When the optional second argument is non-zero, don't remove matches
5417 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5418 rettv->v_type = VAR_STRING;
5419 if (argvars[1].v_type != VAR_UNKNOWN)
5420 {
5421 if (get_tv_number_chk(&argvars[1], &error))
5422 options |= WILD_KEEP_ALL;
5423 if (argvars[2].v_type != VAR_UNKNOWN)
5424 {
5425 if (get_tv_number_chk(&argvars[2], &error))
5426 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005427 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005428 }
5429 if (argvars[3].v_type != VAR_UNKNOWN
5430 && get_tv_number_chk(&argvars[3], &error))
5431 options |= WILD_ALLLINKS;
5432 }
5433 }
5434 if (!error)
5435 {
5436 ExpandInit(&xpc);
5437 xpc.xp_context = EXPAND_FILES;
5438 if (p_wic)
5439 options += WILD_ICASE;
5440 if (rettv->v_type == VAR_STRING)
5441 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
5442 NULL, options, WILD_ALL);
5443 else if (rettv_list_alloc(rettv) != FAIL)
5444 {
5445 int i;
5446
5447 ExpandOne(&xpc, get_tv_string(&argvars[0]),
5448 NULL, options, WILD_ALL_KEEP);
5449 for (i = 0; i < xpc.xp_numfiles; i++)
5450 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5451
5452 ExpandCleanup(&xpc);
5453 }
5454 }
5455 else
5456 rettv->vval.v_string = NULL;
5457}
5458
5459/*
5460 * "globpath()" function
5461 */
5462 static void
5463f_globpath(typval_T *argvars, typval_T *rettv)
5464{
5465 int flags = 0;
5466 char_u buf1[NUMBUFLEN];
5467 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
5468 int error = FALSE;
5469 garray_T ga;
5470 int i;
5471
5472 /* When the optional second argument is non-zero, don't remove matches
5473 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5474 rettv->v_type = VAR_STRING;
5475 if (argvars[2].v_type != VAR_UNKNOWN)
5476 {
5477 if (get_tv_number_chk(&argvars[2], &error))
5478 flags |= WILD_KEEP_ALL;
5479 if (argvars[3].v_type != VAR_UNKNOWN)
5480 {
5481 if (get_tv_number_chk(&argvars[3], &error))
5482 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005483 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005484 }
5485 if (argvars[4].v_type != VAR_UNKNOWN
5486 && get_tv_number_chk(&argvars[4], &error))
5487 flags |= WILD_ALLLINKS;
5488 }
5489 }
5490 if (file != NULL && !error)
5491 {
5492 ga_init2(&ga, (int)sizeof(char_u *), 10);
5493 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
5494 if (rettv->v_type == VAR_STRING)
5495 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
5496 else if (rettv_list_alloc(rettv) != FAIL)
5497 for (i = 0; i < ga.ga_len; ++i)
5498 list_append_string(rettv->vval.v_list,
5499 ((char_u **)(ga.ga_data))[i], -1);
5500 ga_clear_strings(&ga);
5501 }
5502 else
5503 rettv->vval.v_string = NULL;
5504}
5505
5506/*
5507 * "glob2regpat()" function
5508 */
5509 static void
5510f_glob2regpat(typval_T *argvars, typval_T *rettv)
5511{
5512 char_u *pat = get_tv_string_chk(&argvars[0]);
5513
5514 rettv->v_type = VAR_STRING;
5515 rettv->vval.v_string = (pat == NULL)
5516 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
5517}
5518
5519/* for VIM_VERSION_ defines */
5520#include "version.h"
5521
5522/*
5523 * "has()" function
5524 */
5525 static void
5526f_has(typval_T *argvars, typval_T *rettv)
5527{
5528 int i;
5529 char_u *name;
5530 int n = FALSE;
5531 static char *(has_list[]) =
5532 {
5533#ifdef AMIGA
5534 "amiga",
5535# ifdef FEAT_ARP
5536 "arp",
5537# endif
5538#endif
5539#ifdef __BEOS__
5540 "beos",
5541#endif
5542#ifdef MACOS
5543 "mac",
5544#endif
5545#if defined(MACOS_X_UNIX)
5546 "macunix", /* built with 'darwin' enabled */
5547#endif
5548#if defined(__APPLE__) && __APPLE__ == 1
5549 "osx", /* built with or without 'darwin' enabled */
5550#endif
5551#ifdef __QNX__
5552 "qnx",
5553#endif
5554#ifdef UNIX
5555 "unix",
5556#endif
5557#ifdef VMS
5558 "vms",
5559#endif
5560#ifdef WIN32
5561 "win32",
5562#endif
5563#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
5564 "win32unix",
5565#endif
5566#if defined(WIN64) || defined(_WIN64)
5567 "win64",
5568#endif
5569#ifdef EBCDIC
5570 "ebcdic",
5571#endif
5572#ifndef CASE_INSENSITIVE_FILENAME
5573 "fname_case",
5574#endif
5575#ifdef HAVE_ACL
5576 "acl",
5577#endif
5578#ifdef FEAT_ARABIC
5579 "arabic",
5580#endif
5581#ifdef FEAT_AUTOCMD
5582 "autocmd",
5583#endif
5584#ifdef FEAT_BEVAL
5585 "balloon_eval",
5586# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
5587 "balloon_multiline",
5588# endif
5589#endif
5590#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
5591 "builtin_terms",
5592# ifdef ALL_BUILTIN_TCAPS
5593 "all_builtin_terms",
5594# endif
5595#endif
5596#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
5597 || defined(FEAT_GUI_W32) \
5598 || defined(FEAT_GUI_MOTIF))
5599 "browsefilter",
5600#endif
5601#ifdef FEAT_BYTEOFF
5602 "byte_offset",
5603#endif
5604#ifdef FEAT_JOB_CHANNEL
5605 "channel",
5606#endif
5607#ifdef FEAT_CINDENT
5608 "cindent",
5609#endif
5610#ifdef FEAT_CLIENTSERVER
5611 "clientserver",
5612#endif
5613#ifdef FEAT_CLIPBOARD
5614 "clipboard",
5615#endif
5616#ifdef FEAT_CMDL_COMPL
5617 "cmdline_compl",
5618#endif
5619#ifdef FEAT_CMDHIST
5620 "cmdline_hist",
5621#endif
5622#ifdef FEAT_COMMENTS
5623 "comments",
5624#endif
5625#ifdef FEAT_CONCEAL
5626 "conceal",
5627#endif
5628#ifdef FEAT_CRYPT
5629 "cryptv",
5630 "crypt-blowfish",
5631 "crypt-blowfish2",
5632#endif
5633#ifdef FEAT_CSCOPE
5634 "cscope",
5635#endif
5636#ifdef FEAT_CURSORBIND
5637 "cursorbind",
5638#endif
5639#ifdef CURSOR_SHAPE
5640 "cursorshape",
5641#endif
5642#ifdef DEBUG
5643 "debug",
5644#endif
5645#ifdef FEAT_CON_DIALOG
5646 "dialog_con",
5647#endif
5648#ifdef FEAT_GUI_DIALOG
5649 "dialog_gui",
5650#endif
5651#ifdef FEAT_DIFF
5652 "diff",
5653#endif
5654#ifdef FEAT_DIGRAPHS
5655 "digraphs",
5656#endif
5657#ifdef FEAT_DIRECTX
5658 "directx",
5659#endif
5660#ifdef FEAT_DND
5661 "dnd",
5662#endif
5663#ifdef FEAT_EMACS_TAGS
5664 "emacs_tags",
5665#endif
5666 "eval", /* always present, of course! */
5667 "ex_extra", /* graduated feature */
5668#ifdef FEAT_SEARCH_EXTRA
5669 "extra_search",
5670#endif
5671#ifdef FEAT_FKMAP
5672 "farsi",
5673#endif
5674#ifdef FEAT_SEARCHPATH
5675 "file_in_path",
5676#endif
5677#ifdef FEAT_FILTERPIPE
5678 "filterpipe",
5679#endif
5680#ifdef FEAT_FIND_ID
5681 "find_in_path",
5682#endif
5683#ifdef FEAT_FLOAT
5684 "float",
5685#endif
5686#ifdef FEAT_FOLDING
5687 "folding",
5688#endif
5689#ifdef FEAT_FOOTER
5690 "footer",
5691#endif
5692#if !defined(USE_SYSTEM) && defined(UNIX)
5693 "fork",
5694#endif
5695#ifdef FEAT_GETTEXT
5696 "gettext",
5697#endif
5698#ifdef FEAT_GUI
5699 "gui",
5700#endif
5701#ifdef FEAT_GUI_ATHENA
5702# ifdef FEAT_GUI_NEXTAW
5703 "gui_neXtaw",
5704# else
5705 "gui_athena",
5706# endif
5707#endif
5708#ifdef FEAT_GUI_GTK
5709 "gui_gtk",
5710# ifdef USE_GTK3
5711 "gui_gtk3",
5712# else
5713 "gui_gtk2",
5714# endif
5715#endif
5716#ifdef FEAT_GUI_GNOME
5717 "gui_gnome",
5718#endif
5719#ifdef FEAT_GUI_MAC
5720 "gui_mac",
5721#endif
5722#ifdef FEAT_GUI_MOTIF
5723 "gui_motif",
5724#endif
5725#ifdef FEAT_GUI_PHOTON
5726 "gui_photon",
5727#endif
5728#ifdef FEAT_GUI_W32
5729 "gui_win32",
5730#endif
5731#ifdef FEAT_HANGULIN
5732 "hangul_input",
5733#endif
5734#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
5735 "iconv",
5736#endif
5737#ifdef FEAT_INS_EXPAND
5738 "insert_expand",
5739#endif
5740#ifdef FEAT_JOB_CHANNEL
5741 "job",
5742#endif
5743#ifdef FEAT_JUMPLIST
5744 "jumplist",
5745#endif
5746#ifdef FEAT_KEYMAP
5747 "keymap",
5748#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02005749 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005750#ifdef FEAT_LANGMAP
5751 "langmap",
5752#endif
5753#ifdef FEAT_LIBCALL
5754 "libcall",
5755#endif
5756#ifdef FEAT_LINEBREAK
5757 "linebreak",
5758#endif
5759#ifdef FEAT_LISP
5760 "lispindent",
5761#endif
5762#ifdef FEAT_LISTCMDS
5763 "listcmds",
5764#endif
5765#ifdef FEAT_LOCALMAP
5766 "localmap",
5767#endif
5768#ifdef FEAT_LUA
5769# ifndef DYNAMIC_LUA
5770 "lua",
5771# endif
5772#endif
5773#ifdef FEAT_MENU
5774 "menu",
5775#endif
5776#ifdef FEAT_SESSION
5777 "mksession",
5778#endif
5779#ifdef FEAT_MODIFY_FNAME
5780 "modify_fname",
5781#endif
5782#ifdef FEAT_MOUSE
5783 "mouse",
5784#endif
5785#ifdef FEAT_MOUSESHAPE
5786 "mouseshape",
5787#endif
5788#if defined(UNIX) || defined(VMS)
5789# ifdef FEAT_MOUSE_DEC
5790 "mouse_dec",
5791# endif
5792# ifdef FEAT_MOUSE_GPM
5793 "mouse_gpm",
5794# endif
5795# ifdef FEAT_MOUSE_JSB
5796 "mouse_jsbterm",
5797# endif
5798# ifdef FEAT_MOUSE_NET
5799 "mouse_netterm",
5800# endif
5801# ifdef FEAT_MOUSE_PTERM
5802 "mouse_pterm",
5803# endif
5804# ifdef FEAT_MOUSE_SGR
5805 "mouse_sgr",
5806# endif
5807# ifdef FEAT_SYSMOUSE
5808 "mouse_sysmouse",
5809# endif
5810# ifdef FEAT_MOUSE_URXVT
5811 "mouse_urxvt",
5812# endif
5813# ifdef FEAT_MOUSE_XTERM
5814 "mouse_xterm",
5815# endif
5816#endif
5817#ifdef FEAT_MBYTE
5818 "multi_byte",
5819#endif
5820#ifdef FEAT_MBYTE_IME
5821 "multi_byte_ime",
5822#endif
5823#ifdef FEAT_MULTI_LANG
5824 "multi_lang",
5825#endif
5826#ifdef FEAT_MZSCHEME
5827#ifndef DYNAMIC_MZSCHEME
5828 "mzscheme",
5829#endif
5830#endif
5831#ifdef FEAT_NUM64
5832 "num64",
5833#endif
5834#ifdef FEAT_OLE
5835 "ole",
5836#endif
5837 "packages",
5838#ifdef FEAT_PATH_EXTRA
5839 "path_extra",
5840#endif
5841#ifdef FEAT_PERL
5842#ifndef DYNAMIC_PERL
5843 "perl",
5844#endif
5845#endif
5846#ifdef FEAT_PERSISTENT_UNDO
5847 "persistent_undo",
5848#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005849#if defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005850 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005851 "pythonx",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005852#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005853#if defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005854 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005855 "pythonx",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005856#endif
5857#ifdef FEAT_POSTSCRIPT
5858 "postscript",
5859#endif
5860#ifdef FEAT_PRINTER
5861 "printer",
5862#endif
5863#ifdef FEAT_PROFILE
5864 "profile",
5865#endif
5866#ifdef FEAT_RELTIME
5867 "reltime",
5868#endif
5869#ifdef FEAT_QUICKFIX
5870 "quickfix",
5871#endif
5872#ifdef FEAT_RIGHTLEFT
5873 "rightleft",
5874#endif
5875#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
5876 "ruby",
5877#endif
5878#ifdef FEAT_SCROLLBIND
5879 "scrollbind",
5880#endif
5881#ifdef FEAT_CMDL_INFO
5882 "showcmd",
5883 "cmdline_info",
5884#endif
5885#ifdef FEAT_SIGNS
5886 "signs",
5887#endif
5888#ifdef FEAT_SMARTINDENT
5889 "smartindent",
5890#endif
5891#ifdef STARTUPTIME
5892 "startuptime",
5893#endif
5894#ifdef FEAT_STL_OPT
5895 "statusline",
5896#endif
5897#ifdef FEAT_SUN_WORKSHOP
5898 "sun_workshop",
5899#endif
5900#ifdef FEAT_NETBEANS_INTG
5901 "netbeans_intg",
5902#endif
5903#ifdef FEAT_SPELL
5904 "spell",
5905#endif
5906#ifdef FEAT_SYN_HL
5907 "syntax",
5908#endif
5909#if defined(USE_SYSTEM) || !defined(UNIX)
5910 "system",
5911#endif
5912#ifdef FEAT_TAG_BINS
5913 "tag_binary",
5914#endif
5915#ifdef FEAT_TAG_OLDSTATIC
5916 "tag_old_static",
5917#endif
5918#ifdef FEAT_TAG_ANYWHITE
5919 "tag_any_white",
5920#endif
5921#ifdef FEAT_TCL
5922# ifndef DYNAMIC_TCL
5923 "tcl",
5924# endif
5925#endif
5926#ifdef FEAT_TERMGUICOLORS
5927 "termguicolors",
5928#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02005929#if defined(FEAT_TERMINAL) && !defined(WIN3264)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005930 "terminal",
5931#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005932#ifdef TERMINFO
5933 "terminfo",
5934#endif
5935#ifdef FEAT_TERMRESPONSE
5936 "termresponse",
5937#endif
5938#ifdef FEAT_TEXTOBJ
5939 "textobjects",
5940#endif
5941#ifdef HAVE_TGETENT
5942 "tgetent",
5943#endif
5944#ifdef FEAT_TIMERS
5945 "timers",
5946#endif
5947#ifdef FEAT_TITLE
5948 "title",
5949#endif
5950#ifdef FEAT_TOOLBAR
5951 "toolbar",
5952#endif
5953#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
5954 "unnamedplus",
5955#endif
5956#ifdef FEAT_USR_CMDS
5957 "user-commands", /* was accidentally included in 5.4 */
5958 "user_commands",
5959#endif
5960#ifdef FEAT_VIMINFO
5961 "viminfo",
5962#endif
5963#ifdef FEAT_WINDOWS
5964 "vertsplit",
5965#endif
5966#ifdef FEAT_VIRTUALEDIT
5967 "virtualedit",
5968#endif
5969 "visual",
5970#ifdef FEAT_VISUALEXTRA
5971 "visualextra",
5972#endif
5973#ifdef FEAT_VREPLACE
5974 "vreplace",
5975#endif
5976#ifdef FEAT_WILDIGN
5977 "wildignore",
5978#endif
5979#ifdef FEAT_WILDMENU
5980 "wildmenu",
5981#endif
5982#ifdef FEAT_WINDOWS
5983 "windows",
5984#endif
5985#ifdef FEAT_WAK
5986 "winaltkeys",
5987#endif
5988#ifdef FEAT_WRITEBACKUP
5989 "writebackup",
5990#endif
5991#ifdef FEAT_XIM
5992 "xim",
5993#endif
5994#ifdef FEAT_XFONTSET
5995 "xfontset",
5996#endif
5997#ifdef FEAT_XPM_W32
5998 "xpm",
5999 "xpm_w32", /* for backward compatibility */
6000#else
6001# if defined(HAVE_XPM)
6002 "xpm",
6003# endif
6004#endif
6005#ifdef USE_XSMP
6006 "xsmp",
6007#endif
6008#ifdef USE_XSMP_INTERACT
6009 "xsmp_interact",
6010#endif
6011#ifdef FEAT_XCLIPBOARD
6012 "xterm_clipboard",
6013#endif
6014#ifdef FEAT_XTERM_SAVE
6015 "xterm_save",
6016#endif
6017#if defined(UNIX) && defined(FEAT_X11)
6018 "X11",
6019#endif
6020 NULL
6021 };
6022
6023 name = get_tv_string(&argvars[0]);
6024 for (i = 0; has_list[i] != NULL; ++i)
6025 if (STRICMP(name, has_list[i]) == 0)
6026 {
6027 n = TRUE;
6028 break;
6029 }
6030
6031 if (n == FALSE)
6032 {
6033 if (STRNICMP(name, "patch", 5) == 0)
6034 {
6035 if (name[5] == '-'
6036 && STRLEN(name) >= 11
6037 && vim_isdigit(name[6])
6038 && vim_isdigit(name[8])
6039 && vim_isdigit(name[10]))
6040 {
6041 int major = atoi((char *)name + 6);
6042 int minor = atoi((char *)name + 8);
6043
6044 /* Expect "patch-9.9.01234". */
6045 n = (major < VIM_VERSION_MAJOR
6046 || (major == VIM_VERSION_MAJOR
6047 && (minor < VIM_VERSION_MINOR
6048 || (minor == VIM_VERSION_MINOR
6049 && has_patch(atoi((char *)name + 10))))));
6050 }
6051 else
6052 n = has_patch(atoi((char *)name + 5));
6053 }
6054 else if (STRICMP(name, "vim_starting") == 0)
6055 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006056 else if (STRICMP(name, "ttyin") == 0)
6057 n = mch_input_isatty();
6058 else if (STRICMP(name, "ttyout") == 0)
6059 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006060#ifdef FEAT_MBYTE
6061 else if (STRICMP(name, "multi_byte_encoding") == 0)
6062 n = has_mbyte;
6063#endif
6064#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
6065 else if (STRICMP(name, "balloon_multiline") == 0)
6066 n = multiline_balloon_available();
6067#endif
6068#ifdef DYNAMIC_TCL
6069 else if (STRICMP(name, "tcl") == 0)
6070 n = tcl_enabled(FALSE);
6071#endif
6072#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6073 else if (STRICMP(name, "iconv") == 0)
6074 n = iconv_enabled(FALSE);
6075#endif
6076#ifdef DYNAMIC_LUA
6077 else if (STRICMP(name, "lua") == 0)
6078 n = lua_enabled(FALSE);
6079#endif
6080#ifdef DYNAMIC_MZSCHEME
6081 else if (STRICMP(name, "mzscheme") == 0)
6082 n = mzscheme_enabled(FALSE);
6083#endif
6084#ifdef DYNAMIC_RUBY
6085 else if (STRICMP(name, "ruby") == 0)
6086 n = ruby_enabled(FALSE);
6087#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006088#ifdef DYNAMIC_PYTHON
6089 else if (STRICMP(name, "python") == 0)
6090 n = python_enabled(FALSE);
6091#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006092#ifdef DYNAMIC_PYTHON3
6093 else if (STRICMP(name, "python3") == 0)
6094 n = python3_enabled(FALSE);
6095#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006096#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6097 else if (STRICMP(name, "pythonx") == 0)
6098 {
6099# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6100 if (p_pyx == 0)
6101 n = python3_enabled(FALSE) || python_enabled(FALSE);
6102 else if (p_pyx == 3)
6103 n = python3_enabled(FALSE);
6104 else if (p_pyx == 2)
6105 n = python_enabled(FALSE);
6106# elif defined(DYNAMIC_PYTHON)
6107 n = python_enabled(FALSE);
6108# elif defined(DYNAMIC_PYTHON3)
6109 n = python3_enabled(FALSE);
6110# endif
6111 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006112#endif
6113#ifdef DYNAMIC_PERL
6114 else if (STRICMP(name, "perl") == 0)
6115 n = perl_enabled(FALSE);
6116#endif
6117#ifdef FEAT_GUI
6118 else if (STRICMP(name, "gui_running") == 0)
6119 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006120# ifdef FEAT_BROWSE
6121 else if (STRICMP(name, "browse") == 0)
6122 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6123# endif
6124#endif
6125#ifdef FEAT_SYN_HL
6126 else if (STRICMP(name, "syntax_items") == 0)
6127 n = syntax_present(curwin);
6128#endif
6129#if defined(WIN3264)
6130 else if (STRICMP(name, "win95") == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006131 n = FALSE; /* Win9x is no more supported. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006132#endif
6133#ifdef FEAT_NETBEANS_INTG
6134 else if (STRICMP(name, "netbeans_enabled") == 0)
6135 n = netbeans_active();
6136#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006137#if defined(FEAT_TERMINAL) && defined(WIN3264)
6138 else if (STRICMP(name, "terminal") == 0)
6139 n = terminal_enabled();
6140#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006141 }
6142
6143 rettv->vval.v_number = n;
6144}
6145
6146/*
6147 * "has_key()" function
6148 */
6149 static void
6150f_has_key(typval_T *argvars, typval_T *rettv)
6151{
6152 if (argvars[0].v_type != VAR_DICT)
6153 {
6154 EMSG(_(e_dictreq));
6155 return;
6156 }
6157 if (argvars[0].vval.v_dict == NULL)
6158 return;
6159
6160 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
6161 get_tv_string(&argvars[1]), -1) != NULL;
6162}
6163
6164/*
6165 * "haslocaldir()" function
6166 */
6167 static void
6168f_haslocaldir(typval_T *argvars, typval_T *rettv)
6169{
6170 win_T *wp = NULL;
6171
6172 wp = find_tabwin(&argvars[0], &argvars[1]);
6173 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6174}
6175
6176/*
6177 * "hasmapto()" function
6178 */
6179 static void
6180f_hasmapto(typval_T *argvars, typval_T *rettv)
6181{
6182 char_u *name;
6183 char_u *mode;
6184 char_u buf[NUMBUFLEN];
6185 int abbr = FALSE;
6186
6187 name = get_tv_string(&argvars[0]);
6188 if (argvars[1].v_type == VAR_UNKNOWN)
6189 mode = (char_u *)"nvo";
6190 else
6191 {
6192 mode = get_tv_string_buf(&argvars[1], buf);
6193 if (argvars[2].v_type != VAR_UNKNOWN)
6194 abbr = (int)get_tv_number(&argvars[2]);
6195 }
6196
6197 if (map_to_exists(name, mode, abbr))
6198 rettv->vval.v_number = TRUE;
6199 else
6200 rettv->vval.v_number = FALSE;
6201}
6202
6203/*
6204 * "histadd()" function
6205 */
6206 static void
6207f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6208{
6209#ifdef FEAT_CMDHIST
6210 int histype;
6211 char_u *str;
6212 char_u buf[NUMBUFLEN];
6213#endif
6214
6215 rettv->vval.v_number = FALSE;
6216 if (check_restricted() || check_secure())
6217 return;
6218#ifdef FEAT_CMDHIST
6219 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6220 histype = str != NULL ? get_histtype(str) : -1;
6221 if (histype >= 0)
6222 {
6223 str = get_tv_string_buf(&argvars[1], buf);
6224 if (*str != NUL)
6225 {
6226 init_history();
6227 add_to_history(histype, str, FALSE, NUL);
6228 rettv->vval.v_number = TRUE;
6229 return;
6230 }
6231 }
6232#endif
6233}
6234
6235/*
6236 * "histdel()" function
6237 */
6238 static void
6239f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6240{
6241#ifdef FEAT_CMDHIST
6242 int n;
6243 char_u buf[NUMBUFLEN];
6244 char_u *str;
6245
6246 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6247 if (str == NULL)
6248 n = 0;
6249 else if (argvars[1].v_type == VAR_UNKNOWN)
6250 /* only one argument: clear entire history */
6251 n = clr_history(get_histtype(str));
6252 else if (argvars[1].v_type == VAR_NUMBER)
6253 /* index given: remove that entry */
6254 n = del_history_idx(get_histtype(str),
6255 (int)get_tv_number(&argvars[1]));
6256 else
6257 /* string given: remove all matching entries */
6258 n = del_history_entry(get_histtype(str),
6259 get_tv_string_buf(&argvars[1], buf));
6260 rettv->vval.v_number = n;
6261#endif
6262}
6263
6264/*
6265 * "histget()" function
6266 */
6267 static void
6268f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6269{
6270#ifdef FEAT_CMDHIST
6271 int type;
6272 int idx;
6273 char_u *str;
6274
6275 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6276 if (str == NULL)
6277 rettv->vval.v_string = NULL;
6278 else
6279 {
6280 type = get_histtype(str);
6281 if (argvars[1].v_type == VAR_UNKNOWN)
6282 idx = get_history_idx(type);
6283 else
6284 idx = (int)get_tv_number_chk(&argvars[1], NULL);
6285 /* -1 on type error */
6286 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6287 }
6288#else
6289 rettv->vval.v_string = NULL;
6290#endif
6291 rettv->v_type = VAR_STRING;
6292}
6293
6294/*
6295 * "histnr()" function
6296 */
6297 static void
6298f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6299{
6300 int i;
6301
6302#ifdef FEAT_CMDHIST
6303 char_u *history = get_tv_string_chk(&argvars[0]);
6304
6305 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6306 if (i >= HIST_CMD && i < HIST_COUNT)
6307 i = get_history_idx(i);
6308 else
6309#endif
6310 i = -1;
6311 rettv->vval.v_number = i;
6312}
6313
6314/*
6315 * "highlightID(name)" function
6316 */
6317 static void
6318f_hlID(typval_T *argvars, typval_T *rettv)
6319{
6320 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
6321}
6322
6323/*
6324 * "highlight_exists()" function
6325 */
6326 static void
6327f_hlexists(typval_T *argvars, typval_T *rettv)
6328{
6329 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
6330}
6331
6332/*
6333 * "hostname()" function
6334 */
6335 static void
6336f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6337{
6338 char_u hostname[256];
6339
6340 mch_get_host_name(hostname, 256);
6341 rettv->v_type = VAR_STRING;
6342 rettv->vval.v_string = vim_strsave(hostname);
6343}
6344
6345/*
6346 * iconv() function
6347 */
6348 static void
6349f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6350{
6351#ifdef FEAT_MBYTE
6352 char_u buf1[NUMBUFLEN];
6353 char_u buf2[NUMBUFLEN];
6354 char_u *from, *to, *str;
6355 vimconv_T vimconv;
6356#endif
6357
6358 rettv->v_type = VAR_STRING;
6359 rettv->vval.v_string = NULL;
6360
6361#ifdef FEAT_MBYTE
6362 str = get_tv_string(&argvars[0]);
6363 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
6364 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
6365 vimconv.vc_type = CONV_NONE;
6366 convert_setup(&vimconv, from, to);
6367
6368 /* If the encodings are equal, no conversion needed. */
6369 if (vimconv.vc_type == CONV_NONE)
6370 rettv->vval.v_string = vim_strsave(str);
6371 else
6372 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6373
6374 convert_setup(&vimconv, NULL, NULL);
6375 vim_free(from);
6376 vim_free(to);
6377#endif
6378}
6379
6380/*
6381 * "indent()" function
6382 */
6383 static void
6384f_indent(typval_T *argvars, typval_T *rettv)
6385{
6386 linenr_T lnum;
6387
6388 lnum = get_tv_lnum(argvars);
6389 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6390 rettv->vval.v_number = get_indent_lnum(lnum);
6391 else
6392 rettv->vval.v_number = -1;
6393}
6394
6395/*
6396 * "index()" function
6397 */
6398 static void
6399f_index(typval_T *argvars, typval_T *rettv)
6400{
6401 list_T *l;
6402 listitem_T *item;
6403 long idx = 0;
6404 int ic = FALSE;
6405
6406 rettv->vval.v_number = -1;
6407 if (argvars[0].v_type != VAR_LIST)
6408 {
6409 EMSG(_(e_listreq));
6410 return;
6411 }
6412 l = argvars[0].vval.v_list;
6413 if (l != NULL)
6414 {
6415 item = l->lv_first;
6416 if (argvars[2].v_type != VAR_UNKNOWN)
6417 {
6418 int error = FALSE;
6419
6420 /* Start at specified item. Use the cached index that list_find()
6421 * sets, so that a negative number also works. */
6422 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
6423 idx = l->lv_idx;
6424 if (argvars[3].v_type != VAR_UNKNOWN)
6425 ic = (int)get_tv_number_chk(&argvars[3], &error);
6426 if (error)
6427 item = NULL;
6428 }
6429
6430 for ( ; item != NULL; item = item->li_next, ++idx)
6431 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6432 {
6433 rettv->vval.v_number = idx;
6434 break;
6435 }
6436 }
6437}
6438
6439static int inputsecret_flag = 0;
6440
6441/*
6442 * "input()" function
6443 * Also handles inputsecret() when inputsecret is set.
6444 */
6445 static void
6446f_input(typval_T *argvars, typval_T *rettv)
6447{
6448 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6449}
6450
6451/*
6452 * "inputdialog()" function
6453 */
6454 static void
6455f_inputdialog(typval_T *argvars, typval_T *rettv)
6456{
6457#if defined(FEAT_GUI_TEXTDIALOG)
6458 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
6459 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6460 {
6461 char_u *message;
6462 char_u buf[NUMBUFLEN];
6463 char_u *defstr = (char_u *)"";
6464
6465 message = get_tv_string_chk(&argvars[0]);
6466 if (argvars[1].v_type != VAR_UNKNOWN
6467 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
6468 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6469 else
6470 IObuff[0] = NUL;
6471 if (message != NULL && defstr != NULL
6472 && do_dialog(VIM_QUESTION, NULL, message,
6473 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6474 rettv->vval.v_string = vim_strsave(IObuff);
6475 else
6476 {
6477 if (message != NULL && defstr != NULL
6478 && argvars[1].v_type != VAR_UNKNOWN
6479 && argvars[2].v_type != VAR_UNKNOWN)
6480 rettv->vval.v_string = vim_strsave(
6481 get_tv_string_buf(&argvars[2], buf));
6482 else
6483 rettv->vval.v_string = NULL;
6484 }
6485 rettv->v_type = VAR_STRING;
6486 }
6487 else
6488#endif
6489 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6490}
6491
6492/*
6493 * "inputlist()" function
6494 */
6495 static void
6496f_inputlist(typval_T *argvars, typval_T *rettv)
6497{
6498 listitem_T *li;
6499 int selected;
6500 int mouse_used;
6501
6502#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02006503 /* While starting up, there is no place to enter text. When running tests
6504 * with --not-a-term we assume feedkeys() will be used. */
6505 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006506 return;
6507#endif
6508 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6509 {
6510 EMSG2(_(e_listarg), "inputlist()");
6511 return;
6512 }
6513
6514 msg_start();
6515 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
6516 lines_left = Rows; /* avoid more prompt */
6517 msg_scroll = TRUE;
6518 msg_clr_eos();
6519
6520 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
6521 {
6522 msg_puts(get_tv_string(&li->li_tv));
6523 msg_putchar('\n');
6524 }
6525
6526 /* Ask for choice. */
6527 selected = prompt_for_number(&mouse_used);
6528 if (mouse_used)
6529 selected -= lines_left;
6530
6531 rettv->vval.v_number = selected;
6532}
6533
6534
6535static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6536
6537/*
6538 * "inputrestore()" function
6539 */
6540 static void
6541f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6542{
6543 if (ga_userinput.ga_len > 0)
6544 {
6545 --ga_userinput.ga_len;
6546 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
6547 + ga_userinput.ga_len);
6548 /* default return is zero == OK */
6549 }
6550 else if (p_verbose > 1)
6551 {
6552 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
6553 rettv->vval.v_number = 1; /* Failed */
6554 }
6555}
6556
6557/*
6558 * "inputsave()" function
6559 */
6560 static void
6561f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
6562{
6563 /* Add an entry to the stack of typeahead storage. */
6564 if (ga_grow(&ga_userinput, 1) == OK)
6565 {
6566 save_typeahead((tasave_T *)(ga_userinput.ga_data)
6567 + ga_userinput.ga_len);
6568 ++ga_userinput.ga_len;
6569 /* default return is zero == OK */
6570 }
6571 else
6572 rettv->vval.v_number = 1; /* Failed */
6573}
6574
6575/*
6576 * "inputsecret()" function
6577 */
6578 static void
6579f_inputsecret(typval_T *argvars, typval_T *rettv)
6580{
6581 ++cmdline_star;
6582 ++inputsecret_flag;
6583 f_input(argvars, rettv);
6584 --cmdline_star;
6585 --inputsecret_flag;
6586}
6587
6588/*
6589 * "insert()" function
6590 */
6591 static void
6592f_insert(typval_T *argvars, typval_T *rettv)
6593{
6594 long before = 0;
6595 listitem_T *item;
6596 list_T *l;
6597 int error = FALSE;
6598
6599 if (argvars[0].v_type != VAR_LIST)
6600 EMSG2(_(e_listarg), "insert()");
6601 else if ((l = argvars[0].vval.v_list) != NULL
6602 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
6603 {
6604 if (argvars[2].v_type != VAR_UNKNOWN)
6605 before = (long)get_tv_number_chk(&argvars[2], &error);
6606 if (error)
6607 return; /* type error; errmsg already given */
6608
6609 if (before == l->lv_len)
6610 item = NULL;
6611 else
6612 {
6613 item = list_find(l, before);
6614 if (item == NULL)
6615 {
6616 EMSGN(_(e_listidx), before);
6617 l = NULL;
6618 }
6619 }
6620 if (l != NULL)
6621 {
6622 list_insert_tv(l, &argvars[1], item);
6623 copy_tv(&argvars[0], rettv);
6624 }
6625 }
6626}
6627
6628/*
6629 * "invert(expr)" function
6630 */
6631 static void
6632f_invert(typval_T *argvars, typval_T *rettv)
6633{
6634 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
6635}
6636
6637/*
6638 * "isdirectory()" function
6639 */
6640 static void
6641f_isdirectory(typval_T *argvars, typval_T *rettv)
6642{
6643 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
6644}
6645
6646/*
6647 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
6648 * or it refers to a List or Dictionary that is locked.
6649 */
6650 static int
6651tv_islocked(typval_T *tv)
6652{
6653 return (tv->v_lock & VAR_LOCKED)
6654 || (tv->v_type == VAR_LIST
6655 && tv->vval.v_list != NULL
6656 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
6657 || (tv->v_type == VAR_DICT
6658 && tv->vval.v_dict != NULL
6659 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
6660}
6661
6662/*
6663 * "islocked()" function
6664 */
6665 static void
6666f_islocked(typval_T *argvars, typval_T *rettv)
6667{
6668 lval_T lv;
6669 char_u *end;
6670 dictitem_T *di;
6671
6672 rettv->vval.v_number = -1;
6673 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01006674 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006675 if (end != NULL && lv.ll_name != NULL)
6676 {
6677 if (*end != NUL)
6678 EMSG(_(e_trailing));
6679 else
6680 {
6681 if (lv.ll_tv == NULL)
6682 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006683 di = find_var(lv.ll_name, NULL, TRUE);
6684 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006685 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006686 /* Consider a variable locked when:
6687 * 1. the variable itself is locked
6688 * 2. the value of the variable is locked.
6689 * 3. the List or Dict value is locked.
6690 */
6691 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
6692 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006693 }
6694 }
6695 else if (lv.ll_range)
6696 EMSG(_("E786: Range not allowed"));
6697 else if (lv.ll_newkey != NULL)
6698 EMSG2(_(e_dictkey), lv.ll_newkey);
6699 else if (lv.ll_list != NULL)
6700 /* List item. */
6701 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
6702 else
6703 /* Dictionary item. */
6704 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
6705 }
6706 }
6707
6708 clear_lval(&lv);
6709}
6710
6711#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
6712/*
6713 * "isnan()" function
6714 */
6715 static void
6716f_isnan(typval_T *argvars, typval_T *rettv)
6717{
6718 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
6719 && isnan(argvars[0].vval.v_float);
6720}
6721#endif
6722
6723/*
6724 * "items(dict)" function
6725 */
6726 static void
6727f_items(typval_T *argvars, typval_T *rettv)
6728{
6729 dict_list(argvars, rettv, 2);
6730}
6731
6732#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
6733/*
6734 * Get the job from the argument.
6735 * Returns NULL if the job is invalid.
6736 */
6737 static job_T *
6738get_job_arg(typval_T *tv)
6739{
6740 job_T *job;
6741
6742 if (tv->v_type != VAR_JOB)
6743 {
6744 EMSG2(_(e_invarg2), get_tv_string(tv));
6745 return NULL;
6746 }
6747 job = tv->vval.v_job;
6748
6749 if (job == NULL)
6750 EMSG(_("E916: not a valid job"));
6751 return job;
6752}
6753
6754/*
6755 * "job_getchannel()" function
6756 */
6757 static void
6758f_job_getchannel(typval_T *argvars, typval_T *rettv)
6759{
6760 job_T *job = get_job_arg(&argvars[0]);
6761
6762 if (job != NULL)
6763 {
6764 rettv->v_type = VAR_CHANNEL;
6765 rettv->vval.v_channel = job->jv_channel;
6766 if (job->jv_channel != NULL)
6767 ++job->jv_channel->ch_refcount;
6768 }
6769}
6770
6771/*
6772 * "job_info()" function
6773 */
6774 static void
6775f_job_info(typval_T *argvars, typval_T *rettv)
6776{
6777 job_T *job = get_job_arg(&argvars[0]);
6778
6779 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
6780 job_info(job, rettv->vval.v_dict);
6781}
6782
6783/*
6784 * "job_setoptions()" function
6785 */
6786 static void
6787f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
6788{
6789 job_T *job = get_job_arg(&argvars[0]);
6790 jobopt_T opt;
6791
6792 if (job == NULL)
6793 return;
6794 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02006795 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006796 job_set_options(job, &opt);
6797 free_job_options(&opt);
6798}
6799
6800/*
6801 * "job_start()" function
6802 */
6803 static void
6804f_job_start(typval_T *argvars, typval_T *rettv)
6805{
6806 rettv->v_type = VAR_JOB;
6807 if (check_restricted() || check_secure())
6808 return;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006809 rettv->vval.v_job = job_start(argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006810}
6811
6812/*
6813 * "job_status()" function
6814 */
6815 static void
6816f_job_status(typval_T *argvars, typval_T *rettv)
6817{
6818 job_T *job = get_job_arg(&argvars[0]);
6819
6820 if (job != NULL)
6821 {
6822 rettv->v_type = VAR_STRING;
6823 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
6824 }
6825}
6826
6827/*
6828 * "job_stop()" function
6829 */
6830 static void
6831f_job_stop(typval_T *argvars, typval_T *rettv)
6832{
6833 job_T *job = get_job_arg(&argvars[0]);
6834
6835 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02006836 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006837}
6838#endif
6839
6840/*
6841 * "join()" function
6842 */
6843 static void
6844f_join(typval_T *argvars, typval_T *rettv)
6845{
6846 garray_T ga;
6847 char_u *sep;
6848
6849 if (argvars[0].v_type != VAR_LIST)
6850 {
6851 EMSG(_(e_listreq));
6852 return;
6853 }
6854 if (argvars[0].vval.v_list == NULL)
6855 return;
6856 if (argvars[1].v_type == VAR_UNKNOWN)
6857 sep = (char_u *)" ";
6858 else
6859 sep = get_tv_string_chk(&argvars[1]);
6860
6861 rettv->v_type = VAR_STRING;
6862
6863 if (sep != NULL)
6864 {
6865 ga_init2(&ga, (int)sizeof(char), 80);
6866 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
6867 ga_append(&ga, NUL);
6868 rettv->vval.v_string = (char_u *)ga.ga_data;
6869 }
6870 else
6871 rettv->vval.v_string = NULL;
6872}
6873
6874/*
6875 * "js_decode()" function
6876 */
6877 static void
6878f_js_decode(typval_T *argvars, typval_T *rettv)
6879{
6880 js_read_T reader;
6881
6882 reader.js_buf = get_tv_string(&argvars[0]);
6883 reader.js_fill = NULL;
6884 reader.js_used = 0;
6885 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
6886 EMSG(_(e_invarg));
6887}
6888
6889/*
6890 * "js_encode()" function
6891 */
6892 static void
6893f_js_encode(typval_T *argvars, typval_T *rettv)
6894{
6895 rettv->v_type = VAR_STRING;
6896 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
6897}
6898
6899/*
6900 * "json_decode()" function
6901 */
6902 static void
6903f_json_decode(typval_T *argvars, typval_T *rettv)
6904{
6905 js_read_T reader;
6906
6907 reader.js_buf = get_tv_string(&argvars[0]);
6908 reader.js_fill = NULL;
6909 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01006910 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006911}
6912
6913/*
6914 * "json_encode()" function
6915 */
6916 static void
6917f_json_encode(typval_T *argvars, typval_T *rettv)
6918{
6919 rettv->v_type = VAR_STRING;
6920 rettv->vval.v_string = json_encode(&argvars[0], 0);
6921}
6922
6923/*
6924 * "keys()" function
6925 */
6926 static void
6927f_keys(typval_T *argvars, typval_T *rettv)
6928{
6929 dict_list(argvars, rettv, 0);
6930}
6931
6932/*
6933 * "last_buffer_nr()" function.
6934 */
6935 static void
6936f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
6937{
6938 int n = 0;
6939 buf_T *buf;
6940
Bram Moolenaar29323592016-07-24 22:04:11 +02006941 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006942 if (n < buf->b_fnum)
6943 n = buf->b_fnum;
6944
6945 rettv->vval.v_number = n;
6946}
6947
6948/*
6949 * "len()" function
6950 */
6951 static void
6952f_len(typval_T *argvars, typval_T *rettv)
6953{
6954 switch (argvars[0].v_type)
6955 {
6956 case VAR_STRING:
6957 case VAR_NUMBER:
6958 rettv->vval.v_number = (varnumber_T)STRLEN(
6959 get_tv_string(&argvars[0]));
6960 break;
6961 case VAR_LIST:
6962 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
6963 break;
6964 case VAR_DICT:
6965 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
6966 break;
6967 case VAR_UNKNOWN:
6968 case VAR_SPECIAL:
6969 case VAR_FLOAT:
6970 case VAR_FUNC:
6971 case VAR_PARTIAL:
6972 case VAR_JOB:
6973 case VAR_CHANNEL:
6974 EMSG(_("E701: Invalid type for len()"));
6975 break;
6976 }
6977}
6978
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006979 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01006980libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006981{
6982#ifdef FEAT_LIBCALL
6983 char_u *string_in;
6984 char_u **string_result;
6985 int nr_result;
6986#endif
6987
6988 rettv->v_type = type;
6989 if (type != VAR_NUMBER)
6990 rettv->vval.v_string = NULL;
6991
6992 if (check_restricted() || check_secure())
6993 return;
6994
6995#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02006996 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006997 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
6998 {
6999 string_in = NULL;
7000 if (argvars[2].v_type == VAR_STRING)
7001 string_in = argvars[2].vval.v_string;
7002 if (type == VAR_NUMBER)
7003 string_result = NULL;
7004 else
7005 string_result = &rettv->vval.v_string;
7006 if (mch_libcall(argvars[0].vval.v_string,
7007 argvars[1].vval.v_string,
7008 string_in,
7009 argvars[2].vval.v_number,
7010 string_result,
7011 &nr_result) == OK
7012 && type == VAR_NUMBER)
7013 rettv->vval.v_number = nr_result;
7014 }
7015#endif
7016}
7017
7018/*
7019 * "libcall()" function
7020 */
7021 static void
7022f_libcall(typval_T *argvars, typval_T *rettv)
7023{
7024 libcall_common(argvars, rettv, VAR_STRING);
7025}
7026
7027/*
7028 * "libcallnr()" function
7029 */
7030 static void
7031f_libcallnr(typval_T *argvars, typval_T *rettv)
7032{
7033 libcall_common(argvars, rettv, VAR_NUMBER);
7034}
7035
7036/*
7037 * "line(string)" function
7038 */
7039 static void
7040f_line(typval_T *argvars, typval_T *rettv)
7041{
7042 linenr_T lnum = 0;
7043 pos_T *fp;
7044 int fnum;
7045
7046 fp = var2fpos(&argvars[0], TRUE, &fnum);
7047 if (fp != NULL)
7048 lnum = fp->lnum;
7049 rettv->vval.v_number = lnum;
7050}
7051
7052/*
7053 * "line2byte(lnum)" function
7054 */
7055 static void
7056f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7057{
7058#ifndef FEAT_BYTEOFF
7059 rettv->vval.v_number = -1;
7060#else
7061 linenr_T lnum;
7062
7063 lnum = get_tv_lnum(argvars);
7064 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7065 rettv->vval.v_number = -1;
7066 else
7067 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7068 if (rettv->vval.v_number >= 0)
7069 ++rettv->vval.v_number;
7070#endif
7071}
7072
7073/*
7074 * "lispindent(lnum)" function
7075 */
7076 static void
7077f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7078{
7079#ifdef FEAT_LISP
7080 pos_T pos;
7081 linenr_T lnum;
7082
7083 pos = curwin->w_cursor;
7084 lnum = get_tv_lnum(argvars);
7085 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7086 {
7087 curwin->w_cursor.lnum = lnum;
7088 rettv->vval.v_number = get_lisp_indent();
7089 curwin->w_cursor = pos;
7090 }
7091 else
7092#endif
7093 rettv->vval.v_number = -1;
7094}
7095
7096/*
7097 * "localtime()" function
7098 */
7099 static void
7100f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7101{
7102 rettv->vval.v_number = (varnumber_T)time(NULL);
7103}
7104
7105static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
7106
7107 static void
7108get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7109{
7110 char_u *keys;
7111 char_u *which;
7112 char_u buf[NUMBUFLEN];
7113 char_u *keys_buf = NULL;
7114 char_u *rhs;
7115 int mode;
7116 int abbr = FALSE;
7117 int get_dict = FALSE;
7118 mapblock_T *mp;
7119 int buffer_local;
7120
7121 /* return empty string for failure */
7122 rettv->v_type = VAR_STRING;
7123 rettv->vval.v_string = NULL;
7124
7125 keys = get_tv_string(&argvars[0]);
7126 if (*keys == NUL)
7127 return;
7128
7129 if (argvars[1].v_type != VAR_UNKNOWN)
7130 {
7131 which = get_tv_string_buf_chk(&argvars[1], buf);
7132 if (argvars[2].v_type != VAR_UNKNOWN)
7133 {
7134 abbr = (int)get_tv_number(&argvars[2]);
7135 if (argvars[3].v_type != VAR_UNKNOWN)
7136 get_dict = (int)get_tv_number(&argvars[3]);
7137 }
7138 }
7139 else
7140 which = (char_u *)"";
7141 if (which == NULL)
7142 return;
7143
7144 mode = get_map_mode(&which, 0);
7145
7146 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7147 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7148 vim_free(keys_buf);
7149
7150 if (!get_dict)
7151 {
7152 /* Return a string. */
7153 if (rhs != NULL)
7154 rettv->vval.v_string = str2special_save(rhs, FALSE);
7155
7156 }
7157 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7158 {
7159 /* Return a dictionary. */
7160 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7161 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7162 dict_T *dict = rettv->vval.v_dict;
7163
7164 dict_add_nr_str(dict, "lhs", 0L, lhs);
7165 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
7166 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
7167 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
7168 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
7169 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
7170 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
7171 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
7172 dict_add_nr_str(dict, "mode", 0L, mapmode);
7173
7174 vim_free(lhs);
7175 vim_free(mapmode);
7176 }
7177}
7178
7179#ifdef FEAT_FLOAT
7180/*
7181 * "log()" function
7182 */
7183 static void
7184f_log(typval_T *argvars, typval_T *rettv)
7185{
7186 float_T f = 0.0;
7187
7188 rettv->v_type = VAR_FLOAT;
7189 if (get_float_arg(argvars, &f) == OK)
7190 rettv->vval.v_float = log(f);
7191 else
7192 rettv->vval.v_float = 0.0;
7193}
7194
7195/*
7196 * "log10()" function
7197 */
7198 static void
7199f_log10(typval_T *argvars, typval_T *rettv)
7200{
7201 float_T f = 0.0;
7202
7203 rettv->v_type = VAR_FLOAT;
7204 if (get_float_arg(argvars, &f) == OK)
7205 rettv->vval.v_float = log10(f);
7206 else
7207 rettv->vval.v_float = 0.0;
7208}
7209#endif
7210
7211#ifdef FEAT_LUA
7212/*
7213 * "luaeval()" function
7214 */
7215 static void
7216f_luaeval(typval_T *argvars, typval_T *rettv)
7217{
7218 char_u *str;
7219 char_u buf[NUMBUFLEN];
7220
7221 str = get_tv_string_buf(&argvars[0], buf);
7222 do_luaeval(str, argvars + 1, rettv);
7223}
7224#endif
7225
7226/*
7227 * "map()" function
7228 */
7229 static void
7230f_map(typval_T *argvars, typval_T *rettv)
7231{
7232 filter_map(argvars, rettv, TRUE);
7233}
7234
7235/*
7236 * "maparg()" function
7237 */
7238 static void
7239f_maparg(typval_T *argvars, typval_T *rettv)
7240{
7241 get_maparg(argvars, rettv, TRUE);
7242}
7243
7244/*
7245 * "mapcheck()" function
7246 */
7247 static void
7248f_mapcheck(typval_T *argvars, typval_T *rettv)
7249{
7250 get_maparg(argvars, rettv, FALSE);
7251}
7252
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007253typedef enum
7254{
7255 MATCH_END, /* matchend() */
7256 MATCH_MATCH, /* match() */
7257 MATCH_STR, /* matchstr() */
7258 MATCH_LIST, /* matchlist() */
7259 MATCH_POS /* matchstrpos() */
7260} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007261
7262 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007263find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007264{
7265 char_u *str = NULL;
7266 long len = 0;
7267 char_u *expr = NULL;
7268 char_u *pat;
7269 regmatch_T regmatch;
7270 char_u patbuf[NUMBUFLEN];
7271 char_u strbuf[NUMBUFLEN];
7272 char_u *save_cpo;
7273 long start = 0;
7274 long nth = 1;
7275 colnr_T startcol = 0;
7276 int match = 0;
7277 list_T *l = NULL;
7278 listitem_T *li = NULL;
7279 long idx = 0;
7280 char_u *tofree = NULL;
7281
7282 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7283 save_cpo = p_cpo;
7284 p_cpo = (char_u *)"";
7285
7286 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007287 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007288 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007289 /* type MATCH_LIST: return empty list when there are no matches.
7290 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007291 if (rettv_list_alloc(rettv) == FAIL)
7292 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007293 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007294 && (list_append_string(rettv->vval.v_list,
7295 (char_u *)"", 0) == FAIL
7296 || list_append_number(rettv->vval.v_list,
7297 (varnumber_T)-1) == FAIL
7298 || list_append_number(rettv->vval.v_list,
7299 (varnumber_T)-1) == FAIL
7300 || list_append_number(rettv->vval.v_list,
7301 (varnumber_T)-1) == FAIL))
7302 {
7303 list_free(rettv->vval.v_list);
7304 rettv->vval.v_list = NULL;
7305 goto theend;
7306 }
7307 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007308 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007309 {
7310 rettv->v_type = VAR_STRING;
7311 rettv->vval.v_string = NULL;
7312 }
7313
7314 if (argvars[0].v_type == VAR_LIST)
7315 {
7316 if ((l = argvars[0].vval.v_list) == NULL)
7317 goto theend;
7318 li = l->lv_first;
7319 }
7320 else
7321 {
7322 expr = str = get_tv_string(&argvars[0]);
7323 len = (long)STRLEN(str);
7324 }
7325
7326 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
7327 if (pat == NULL)
7328 goto theend;
7329
7330 if (argvars[2].v_type != VAR_UNKNOWN)
7331 {
7332 int error = FALSE;
7333
7334 start = (long)get_tv_number_chk(&argvars[2], &error);
7335 if (error)
7336 goto theend;
7337 if (l != NULL)
7338 {
7339 li = list_find(l, start);
7340 if (li == NULL)
7341 goto theend;
7342 idx = l->lv_idx; /* use the cached index */
7343 }
7344 else
7345 {
7346 if (start < 0)
7347 start = 0;
7348 if (start > len)
7349 goto theend;
7350 /* When "count" argument is there ignore matches before "start",
7351 * otherwise skip part of the string. Differs when pattern is "^"
7352 * or "\<". */
7353 if (argvars[3].v_type != VAR_UNKNOWN)
7354 startcol = start;
7355 else
7356 {
7357 str += start;
7358 len -= start;
7359 }
7360 }
7361
7362 if (argvars[3].v_type != VAR_UNKNOWN)
7363 nth = (long)get_tv_number_chk(&argvars[3], &error);
7364 if (error)
7365 goto theend;
7366 }
7367
7368 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7369 if (regmatch.regprog != NULL)
7370 {
7371 regmatch.rm_ic = p_ic;
7372
7373 for (;;)
7374 {
7375 if (l != NULL)
7376 {
7377 if (li == NULL)
7378 {
7379 match = FALSE;
7380 break;
7381 }
7382 vim_free(tofree);
7383 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7384 if (str == NULL)
7385 break;
7386 }
7387
7388 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7389
7390 if (match && --nth <= 0)
7391 break;
7392 if (l == NULL && !match)
7393 break;
7394
7395 /* Advance to just after the match. */
7396 if (l != NULL)
7397 {
7398 li = li->li_next;
7399 ++idx;
7400 }
7401 else
7402 {
7403#ifdef FEAT_MBYTE
7404 startcol = (colnr_T)(regmatch.startp[0]
7405 + (*mb_ptr2len)(regmatch.startp[0]) - str);
7406#else
7407 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
7408#endif
7409 if (startcol > (colnr_T)len
7410 || str + startcol <= regmatch.startp[0])
7411 {
7412 match = FALSE;
7413 break;
7414 }
7415 }
7416 }
7417
7418 if (match)
7419 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007420 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007421 {
7422 listitem_T *li1 = rettv->vval.v_list->lv_first;
7423 listitem_T *li2 = li1->li_next;
7424 listitem_T *li3 = li2->li_next;
7425 listitem_T *li4 = li3->li_next;
7426
7427 vim_free(li1->li_tv.vval.v_string);
7428 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7429 (int)(regmatch.endp[0] - regmatch.startp[0]));
7430 li3->li_tv.vval.v_number =
7431 (varnumber_T)(regmatch.startp[0] - expr);
7432 li4->li_tv.vval.v_number =
7433 (varnumber_T)(regmatch.endp[0] - expr);
7434 if (l != NULL)
7435 li2->li_tv.vval.v_number = (varnumber_T)idx;
7436 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007437 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007438 {
7439 int i;
7440
7441 /* return list with matched string and submatches */
7442 for (i = 0; i < NSUBEXP; ++i)
7443 {
7444 if (regmatch.endp[i] == NULL)
7445 {
7446 if (list_append_string(rettv->vval.v_list,
7447 (char_u *)"", 0) == FAIL)
7448 break;
7449 }
7450 else if (list_append_string(rettv->vval.v_list,
7451 regmatch.startp[i],
7452 (int)(regmatch.endp[i] - regmatch.startp[i]))
7453 == FAIL)
7454 break;
7455 }
7456 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007457 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007458 {
7459 /* return matched string */
7460 if (l != NULL)
7461 copy_tv(&li->li_tv, rettv);
7462 else
7463 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
7464 (int)(regmatch.endp[0] - regmatch.startp[0]));
7465 }
7466 else if (l != NULL)
7467 rettv->vval.v_number = idx;
7468 else
7469 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007470 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007471 rettv->vval.v_number =
7472 (varnumber_T)(regmatch.startp[0] - str);
7473 else
7474 rettv->vval.v_number =
7475 (varnumber_T)(regmatch.endp[0] - str);
7476 rettv->vval.v_number += (varnumber_T)(str - expr);
7477 }
7478 }
7479 vim_regfree(regmatch.regprog);
7480 }
7481
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007482theend:
7483 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007484 /* matchstrpos() without a list: drop the second item. */
7485 listitem_remove(rettv->vval.v_list,
7486 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007487 vim_free(tofree);
7488 p_cpo = save_cpo;
7489}
7490
7491/*
7492 * "match()" function
7493 */
7494 static void
7495f_match(typval_T *argvars, typval_T *rettv)
7496{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007497 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007498}
7499
7500/*
7501 * "matchadd()" function
7502 */
7503 static void
7504f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7505{
7506#ifdef FEAT_SEARCH_EXTRA
7507 char_u buf[NUMBUFLEN];
7508 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
7509 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
7510 int prio = 10; /* default priority */
7511 int id = -1;
7512 int error = FALSE;
7513 char_u *conceal_char = NULL;
7514
7515 rettv->vval.v_number = -1;
7516
7517 if (grp == NULL || pat == NULL)
7518 return;
7519 if (argvars[2].v_type != VAR_UNKNOWN)
7520 {
7521 prio = (int)get_tv_number_chk(&argvars[2], &error);
7522 if (argvars[3].v_type != VAR_UNKNOWN)
7523 {
7524 id = (int)get_tv_number_chk(&argvars[3], &error);
7525 if (argvars[4].v_type != VAR_UNKNOWN)
7526 {
7527 if (argvars[4].v_type != VAR_DICT)
7528 {
7529 EMSG(_(e_dictreq));
7530 return;
7531 }
7532 if (dict_find(argvars[4].vval.v_dict,
7533 (char_u *)"conceal", -1) != NULL)
7534 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7535 (char_u *)"conceal", FALSE);
7536 }
7537 }
7538 }
7539 if (error == TRUE)
7540 return;
7541 if (id >= 1 && id <= 3)
7542 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007543 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007544 return;
7545 }
7546
7547 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
7548 conceal_char);
7549#endif
7550}
7551
7552/*
7553 * "matchaddpos()" function
7554 */
7555 static void
7556f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7557{
7558#ifdef FEAT_SEARCH_EXTRA
7559 char_u buf[NUMBUFLEN];
7560 char_u *group;
7561 int prio = 10;
7562 int id = -1;
7563 int error = FALSE;
7564 list_T *l;
7565 char_u *conceal_char = NULL;
7566
7567 rettv->vval.v_number = -1;
7568
7569 group = get_tv_string_buf_chk(&argvars[0], buf);
7570 if (group == NULL)
7571 return;
7572
7573 if (argvars[1].v_type != VAR_LIST)
7574 {
7575 EMSG2(_(e_listarg), "matchaddpos()");
7576 return;
7577 }
7578 l = argvars[1].vval.v_list;
7579 if (l == NULL)
7580 return;
7581
7582 if (argvars[2].v_type != VAR_UNKNOWN)
7583 {
7584 prio = (int)get_tv_number_chk(&argvars[2], &error);
7585 if (argvars[3].v_type != VAR_UNKNOWN)
7586 {
7587 id = (int)get_tv_number_chk(&argvars[3], &error);
7588 if (argvars[4].v_type != VAR_UNKNOWN)
7589 {
7590 if (argvars[4].v_type != VAR_DICT)
7591 {
7592 EMSG(_(e_dictreq));
7593 return;
7594 }
7595 if (dict_find(argvars[4].vval.v_dict,
7596 (char_u *)"conceal", -1) != NULL)
7597 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7598 (char_u *)"conceal", FALSE);
7599 }
7600 }
7601 }
7602 if (error == TRUE)
7603 return;
7604
7605 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
7606 if (id == 1 || id == 2)
7607 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007608 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007609 return;
7610 }
7611
7612 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
7613 conceal_char);
7614#endif
7615}
7616
7617/*
7618 * "matcharg()" function
7619 */
7620 static void
7621f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
7622{
7623 if (rettv_list_alloc(rettv) == OK)
7624 {
7625#ifdef FEAT_SEARCH_EXTRA
7626 int id = (int)get_tv_number(&argvars[0]);
7627 matchitem_T *m;
7628
7629 if (id >= 1 && id <= 3)
7630 {
7631 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
7632 {
7633 list_append_string(rettv->vval.v_list,
7634 syn_id2name(m->hlg_id), -1);
7635 list_append_string(rettv->vval.v_list, m->pattern, -1);
7636 }
7637 else
7638 {
7639 list_append_string(rettv->vval.v_list, NULL, -1);
7640 list_append_string(rettv->vval.v_list, NULL, -1);
7641 }
7642 }
7643#endif
7644 }
7645}
7646
7647/*
7648 * "matchdelete()" function
7649 */
7650 static void
7651f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7652{
7653#ifdef FEAT_SEARCH_EXTRA
7654 rettv->vval.v_number = match_delete(curwin,
7655 (int)get_tv_number(&argvars[0]), TRUE);
7656#endif
7657}
7658
7659/*
7660 * "matchend()" function
7661 */
7662 static void
7663f_matchend(typval_T *argvars, typval_T *rettv)
7664{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007665 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007666}
7667
7668/*
7669 * "matchlist()" function
7670 */
7671 static void
7672f_matchlist(typval_T *argvars, typval_T *rettv)
7673{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007674 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007675}
7676
7677/*
7678 * "matchstr()" function
7679 */
7680 static void
7681f_matchstr(typval_T *argvars, typval_T *rettv)
7682{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007683 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007684}
7685
7686/*
7687 * "matchstrpos()" function
7688 */
7689 static void
7690f_matchstrpos(typval_T *argvars, typval_T *rettv)
7691{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007692 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007693}
7694
7695static void max_min(typval_T *argvars, typval_T *rettv, int domax);
7696
7697 static void
7698max_min(typval_T *argvars, typval_T *rettv, int domax)
7699{
7700 varnumber_T n = 0;
7701 varnumber_T i;
7702 int error = FALSE;
7703
7704 if (argvars[0].v_type == VAR_LIST)
7705 {
7706 list_T *l;
7707 listitem_T *li;
7708
7709 l = argvars[0].vval.v_list;
7710 if (l != NULL)
7711 {
7712 li = l->lv_first;
7713 if (li != NULL)
7714 {
7715 n = get_tv_number_chk(&li->li_tv, &error);
7716 for (;;)
7717 {
7718 li = li->li_next;
7719 if (li == NULL)
7720 break;
7721 i = get_tv_number_chk(&li->li_tv, &error);
7722 if (domax ? i > n : i < n)
7723 n = i;
7724 }
7725 }
7726 }
7727 }
7728 else if (argvars[0].v_type == VAR_DICT)
7729 {
7730 dict_T *d;
7731 int first = TRUE;
7732 hashitem_T *hi;
7733 int todo;
7734
7735 d = argvars[0].vval.v_dict;
7736 if (d != NULL)
7737 {
7738 todo = (int)d->dv_hashtab.ht_used;
7739 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
7740 {
7741 if (!HASHITEM_EMPTY(hi))
7742 {
7743 --todo;
7744 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
7745 if (first)
7746 {
7747 n = i;
7748 first = FALSE;
7749 }
7750 else if (domax ? i > n : i < n)
7751 n = i;
7752 }
7753 }
7754 }
7755 }
7756 else
Bram Moolenaar26b84332016-09-04 21:42:36 +02007757 EMSG2(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007758 rettv->vval.v_number = error ? 0 : n;
7759}
7760
7761/*
7762 * "max()" function
7763 */
7764 static void
7765f_max(typval_T *argvars, typval_T *rettv)
7766{
7767 max_min(argvars, rettv, TRUE);
7768}
7769
7770/*
7771 * "min()" function
7772 */
7773 static void
7774f_min(typval_T *argvars, typval_T *rettv)
7775{
7776 max_min(argvars, rettv, FALSE);
7777}
7778
7779static int mkdir_recurse(char_u *dir, int prot);
7780
7781/*
7782 * Create the directory in which "dir" is located, and higher levels when
7783 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02007784 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007785 */
7786 static int
7787mkdir_recurse(char_u *dir, int prot)
7788{
7789 char_u *p;
7790 char_u *updir;
7791 int r = FAIL;
7792
7793 /* Get end of directory name in "dir".
7794 * We're done when it's "/" or "c:/". */
7795 p = gettail_sep(dir);
7796 if (p <= get_past_head(dir))
7797 return OK;
7798
7799 /* If the directory exists we're done. Otherwise: create it.*/
7800 updir = vim_strnsave(dir, (int)(p - dir));
7801 if (updir == NULL)
7802 return FAIL;
7803 if (mch_isdir(updir))
7804 r = OK;
7805 else if (mkdir_recurse(updir, prot) == OK)
7806 r = vim_mkdir_emsg(updir, prot);
7807 vim_free(updir);
7808 return r;
7809}
7810
7811#ifdef vim_mkdir
7812/*
7813 * "mkdir()" function
7814 */
7815 static void
7816f_mkdir(typval_T *argvars, typval_T *rettv)
7817{
7818 char_u *dir;
7819 char_u buf[NUMBUFLEN];
7820 int prot = 0755;
7821
7822 rettv->vval.v_number = FAIL;
7823 if (check_restricted() || check_secure())
7824 return;
7825
7826 dir = get_tv_string_buf(&argvars[0], buf);
7827 if (*dir == NUL)
7828 rettv->vval.v_number = FAIL;
7829 else
7830 {
7831 if (*gettail(dir) == NUL)
7832 /* remove trailing slashes */
7833 *gettail_sep(dir) = NUL;
7834
7835 if (argvars[1].v_type != VAR_UNKNOWN)
7836 {
7837 if (argvars[2].v_type != VAR_UNKNOWN)
7838 prot = (int)get_tv_number_chk(&argvars[2], NULL);
7839 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
7840 mkdir_recurse(dir, prot);
7841 }
7842 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
7843 }
7844}
7845#endif
7846
7847/*
7848 * "mode()" function
7849 */
7850 static void
7851f_mode(typval_T *argvars, typval_T *rettv)
7852{
7853 char_u buf[3];
7854
7855 buf[1] = NUL;
7856 buf[2] = NUL;
7857
7858 if (time_for_testing == 93784)
7859 {
7860 /* Testing the two-character code. */
7861 buf[0] = 'x';
7862 buf[1] = '!';
7863 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02007864#ifdef FEAT_TERMINAL
7865 else if (term_use_loop())
7866 buf[0] = 't';
7867#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007868 else if (VIsual_active)
7869 {
7870 if (VIsual_select)
7871 buf[0] = VIsual_mode + 's' - 'v';
7872 else
7873 buf[0] = VIsual_mode;
7874 }
7875 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
7876 || State == CONFIRM)
7877 {
7878 buf[0] = 'r';
7879 if (State == ASKMORE)
7880 buf[1] = 'm';
7881 else if (State == CONFIRM)
7882 buf[1] = '?';
7883 }
7884 else if (State == EXTERNCMD)
7885 buf[0] = '!';
7886 else if (State & INSERT)
7887 {
7888#ifdef FEAT_VREPLACE
7889 if (State & VREPLACE_FLAG)
7890 {
7891 buf[0] = 'R';
7892 buf[1] = 'v';
7893 }
7894 else
7895#endif
Bram Moolenaare90858d2017-02-01 17:24:34 +01007896 {
7897 if (State & REPLACE_FLAG)
7898 buf[0] = 'R';
7899 else
7900 buf[0] = 'i';
7901#ifdef FEAT_INS_EXPAND
7902 if (ins_compl_active())
7903 buf[1] = 'c';
7904 else if (ctrl_x_mode == 1)
7905 buf[1] = 'x';
7906#endif
7907 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007908 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01007909 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007910 {
7911 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01007912 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007913 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01007914 else if (exmode_active == EXMODE_NORMAL)
7915 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007916 }
7917 else
7918 {
7919 buf[0] = 'n';
7920 if (finish_op)
7921 buf[1] = 'o';
7922 }
7923
7924 /* Clear out the minor mode when the argument is not a non-zero number or
7925 * non-empty string. */
7926 if (!non_zero_arg(&argvars[0]))
7927 buf[1] = NUL;
7928
7929 rettv->vval.v_string = vim_strsave(buf);
7930 rettv->v_type = VAR_STRING;
7931}
7932
7933#if defined(FEAT_MZSCHEME) || defined(PROTO)
7934/*
7935 * "mzeval()" function
7936 */
7937 static void
7938f_mzeval(typval_T *argvars, typval_T *rettv)
7939{
7940 char_u *str;
7941 char_u buf[NUMBUFLEN];
7942
7943 str = get_tv_string_buf(&argvars[0], buf);
7944 do_mzeval(str, rettv);
7945}
7946
7947 void
7948mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
7949{
7950 typval_T argvars[3];
7951
7952 argvars[0].v_type = VAR_STRING;
7953 argvars[0].vval.v_string = name;
7954 copy_tv(args, &argvars[1]);
7955 argvars[2].v_type = VAR_UNKNOWN;
7956 f_call(argvars, rettv);
7957 clear_tv(&argvars[1]);
7958}
7959#endif
7960
7961/*
7962 * "nextnonblank()" function
7963 */
7964 static void
7965f_nextnonblank(typval_T *argvars, typval_T *rettv)
7966{
7967 linenr_T lnum;
7968
7969 for (lnum = get_tv_lnum(argvars); ; ++lnum)
7970 {
7971 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
7972 {
7973 lnum = 0;
7974 break;
7975 }
7976 if (*skipwhite(ml_get(lnum)) != NUL)
7977 break;
7978 }
7979 rettv->vval.v_number = lnum;
7980}
7981
7982/*
7983 * "nr2char()" function
7984 */
7985 static void
7986f_nr2char(typval_T *argvars, typval_T *rettv)
7987{
7988 char_u buf[NUMBUFLEN];
7989
7990#ifdef FEAT_MBYTE
7991 if (has_mbyte)
7992 {
7993 int utf8 = 0;
7994
7995 if (argvars[1].v_type != VAR_UNKNOWN)
7996 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
7997 if (utf8)
7998 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
7999 else
8000 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8001 }
8002 else
8003#endif
8004 {
8005 buf[0] = (char_u)get_tv_number(&argvars[0]);
8006 buf[1] = NUL;
8007 }
8008 rettv->v_type = VAR_STRING;
8009 rettv->vval.v_string = vim_strsave(buf);
8010}
8011
8012/*
8013 * "or(expr, expr)" function
8014 */
8015 static void
8016f_or(typval_T *argvars, typval_T *rettv)
8017{
8018 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8019 | get_tv_number_chk(&argvars[1], NULL);
8020}
8021
8022/*
8023 * "pathshorten()" function
8024 */
8025 static void
8026f_pathshorten(typval_T *argvars, typval_T *rettv)
8027{
8028 char_u *p;
8029
8030 rettv->v_type = VAR_STRING;
8031 p = get_tv_string_chk(&argvars[0]);
8032 if (p == NULL)
8033 rettv->vval.v_string = NULL;
8034 else
8035 {
8036 p = vim_strsave(p);
8037 rettv->vval.v_string = p;
8038 if (p != NULL)
8039 shorten_dir(p);
8040 }
8041}
8042
8043#ifdef FEAT_PERL
8044/*
8045 * "perleval()" function
8046 */
8047 static void
8048f_perleval(typval_T *argvars, typval_T *rettv)
8049{
8050 char_u *str;
8051 char_u buf[NUMBUFLEN];
8052
8053 str = get_tv_string_buf(&argvars[0], buf);
8054 do_perleval(str, rettv);
8055}
8056#endif
8057
8058#ifdef FEAT_FLOAT
8059/*
8060 * "pow()" function
8061 */
8062 static void
8063f_pow(typval_T *argvars, typval_T *rettv)
8064{
8065 float_T fx = 0.0, fy = 0.0;
8066
8067 rettv->v_type = VAR_FLOAT;
8068 if (get_float_arg(argvars, &fx) == OK
8069 && get_float_arg(&argvars[1], &fy) == OK)
8070 rettv->vval.v_float = pow(fx, fy);
8071 else
8072 rettv->vval.v_float = 0.0;
8073}
8074#endif
8075
8076/*
8077 * "prevnonblank()" function
8078 */
8079 static void
8080f_prevnonblank(typval_T *argvars, typval_T *rettv)
8081{
8082 linenr_T lnum;
8083
8084 lnum = get_tv_lnum(argvars);
8085 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8086 lnum = 0;
8087 else
8088 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8089 --lnum;
8090 rettv->vval.v_number = lnum;
8091}
8092
8093/* This dummy va_list is here because:
8094 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8095 * - locally in the function results in a "used before set" warning
8096 * - using va_start() to initialize it gives "function with fixed args" error */
8097static va_list ap;
8098
8099/*
8100 * "printf()" function
8101 */
8102 static void
8103f_printf(typval_T *argvars, typval_T *rettv)
8104{
8105 char_u buf[NUMBUFLEN];
8106 int len;
8107 char_u *s;
8108 int saved_did_emsg = did_emsg;
8109 char *fmt;
8110
8111 rettv->v_type = VAR_STRING;
8112 rettv->vval.v_string = NULL;
8113
8114 /* Get the required length, allocate the buffer and do it for real. */
8115 did_emsg = FALSE;
8116 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008117 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008118 if (!did_emsg)
8119 {
8120 s = alloc(len + 1);
8121 if (s != NULL)
8122 {
8123 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008124 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8125 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008126 }
8127 }
8128 did_emsg |= saved_did_emsg;
8129}
8130
8131/*
8132 * "pumvisible()" function
8133 */
8134 static void
8135f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8136{
8137#ifdef FEAT_INS_EXPAND
8138 if (pum_visible())
8139 rettv->vval.v_number = 1;
8140#endif
8141}
8142
8143#ifdef FEAT_PYTHON3
8144/*
8145 * "py3eval()" function
8146 */
8147 static void
8148f_py3eval(typval_T *argvars, typval_T *rettv)
8149{
8150 char_u *str;
8151 char_u buf[NUMBUFLEN];
8152
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008153 if (p_pyx == 0)
8154 p_pyx = 3;
8155
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008156 str = get_tv_string_buf(&argvars[0], buf);
8157 do_py3eval(str, rettv);
8158}
8159#endif
8160
8161#ifdef FEAT_PYTHON
8162/*
8163 * "pyeval()" function
8164 */
8165 static void
8166f_pyeval(typval_T *argvars, typval_T *rettv)
8167{
8168 char_u *str;
8169 char_u buf[NUMBUFLEN];
8170
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008171 if (p_pyx == 0)
8172 p_pyx = 2;
8173
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008174 str = get_tv_string_buf(&argvars[0], buf);
8175 do_pyeval(str, rettv);
8176}
8177#endif
8178
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008179#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8180/*
8181 * "pyxeval()" function
8182 */
8183 static void
8184f_pyxeval(typval_T *argvars, typval_T *rettv)
8185{
8186# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8187 init_pyxversion();
8188 if (p_pyx == 2)
8189 f_pyeval(argvars, rettv);
8190 else
8191 f_py3eval(argvars, rettv);
8192# elif defined(FEAT_PYTHON)
8193 f_pyeval(argvars, rettv);
8194# elif defined(FEAT_PYTHON3)
8195 f_py3eval(argvars, rettv);
8196# endif
8197}
8198#endif
8199
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008200/*
8201 * "range()" function
8202 */
8203 static void
8204f_range(typval_T *argvars, typval_T *rettv)
8205{
8206 varnumber_T start;
8207 varnumber_T end;
8208 varnumber_T stride = 1;
8209 varnumber_T i;
8210 int error = FALSE;
8211
8212 start = get_tv_number_chk(&argvars[0], &error);
8213 if (argvars[1].v_type == VAR_UNKNOWN)
8214 {
8215 end = start - 1;
8216 start = 0;
8217 }
8218 else
8219 {
8220 end = get_tv_number_chk(&argvars[1], &error);
8221 if (argvars[2].v_type != VAR_UNKNOWN)
8222 stride = get_tv_number_chk(&argvars[2], &error);
8223 }
8224
8225 if (error)
8226 return; /* type error; errmsg already given */
8227 if (stride == 0)
8228 EMSG(_("E726: Stride is zero"));
8229 else if (stride > 0 ? end + 1 < start : end - 1 > start)
8230 EMSG(_("E727: Start past end"));
8231 else
8232 {
8233 if (rettv_list_alloc(rettv) == OK)
8234 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8235 if (list_append_number(rettv->vval.v_list,
8236 (varnumber_T)i) == FAIL)
8237 break;
8238 }
8239}
8240
8241/*
8242 * "readfile()" function
8243 */
8244 static void
8245f_readfile(typval_T *argvars, typval_T *rettv)
8246{
8247 int binary = FALSE;
8248 int failed = FALSE;
8249 char_u *fname;
8250 FILE *fd;
8251 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8252 int io_size = sizeof(buf);
8253 int readlen; /* size of last fread() */
8254 char_u *prev = NULL; /* previously read bytes, if any */
8255 long prevlen = 0; /* length of data in prev */
8256 long prevsize = 0; /* size of prev buffer */
8257 long maxline = MAXLNUM;
8258 long cnt = 0;
8259 char_u *p; /* position in buf */
8260 char_u *start; /* start of current line */
8261
8262 if (argvars[1].v_type != VAR_UNKNOWN)
8263 {
8264 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
8265 binary = TRUE;
8266 if (argvars[2].v_type != VAR_UNKNOWN)
8267 maxline = (long)get_tv_number(&argvars[2]);
8268 }
8269
8270 if (rettv_list_alloc(rettv) == FAIL)
8271 return;
8272
8273 /* Always open the file in binary mode, library functions have a mind of
8274 * their own about CR-LF conversion. */
8275 fname = get_tv_string(&argvars[0]);
8276 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8277 {
8278 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
8279 return;
8280 }
8281
8282 while (cnt < maxline || maxline < 0)
8283 {
8284 readlen = (int)fread(buf, 1, io_size, fd);
8285
8286 /* This for loop processes what was read, but is also entered at end
8287 * of file so that either:
8288 * - an incomplete line gets written
8289 * - a "binary" file gets an empty line at the end if it ends in a
8290 * newline. */
8291 for (p = buf, start = buf;
8292 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8293 ++p)
8294 {
8295 if (*p == '\n' || readlen <= 0)
8296 {
8297 listitem_T *li;
8298 char_u *s = NULL;
8299 long_u len = p - start;
8300
8301 /* Finished a line. Remove CRs before NL. */
8302 if (readlen > 0 && !binary)
8303 {
8304 while (len > 0 && start[len - 1] == '\r')
8305 --len;
8306 /* removal may cross back to the "prev" string */
8307 if (len == 0)
8308 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8309 --prevlen;
8310 }
8311 if (prevlen == 0)
8312 s = vim_strnsave(start, (int)len);
8313 else
8314 {
8315 /* Change "prev" buffer to be the right size. This way
8316 * the bytes are only copied once, and very long lines are
8317 * allocated only once. */
8318 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8319 {
8320 mch_memmove(s + prevlen, start, len);
8321 s[prevlen + len] = NUL;
8322 prev = NULL; /* the list will own the string */
8323 prevlen = prevsize = 0;
8324 }
8325 }
8326 if (s == NULL)
8327 {
8328 do_outofmem_msg((long_u) prevlen + len + 1);
8329 failed = TRUE;
8330 break;
8331 }
8332
8333 if ((li = listitem_alloc()) == NULL)
8334 {
8335 vim_free(s);
8336 failed = TRUE;
8337 break;
8338 }
8339 li->li_tv.v_type = VAR_STRING;
8340 li->li_tv.v_lock = 0;
8341 li->li_tv.vval.v_string = s;
8342 list_append(rettv->vval.v_list, li);
8343
8344 start = p + 1; /* step over newline */
8345 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
8346 break;
8347 }
8348 else if (*p == NUL)
8349 *p = '\n';
8350#ifdef FEAT_MBYTE
8351 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
8352 * when finding the BF and check the previous two bytes. */
8353 else if (*p == 0xbf && enc_utf8 && !binary)
8354 {
8355 /* Find the two bytes before the 0xbf. If p is at buf, or buf
8356 * + 1, these may be in the "prev" string. */
8357 char_u back1 = p >= buf + 1 ? p[-1]
8358 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
8359 char_u back2 = p >= buf + 2 ? p[-2]
8360 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
8361 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
8362
8363 if (back2 == 0xef && back1 == 0xbb)
8364 {
8365 char_u *dest = p - 2;
8366
8367 /* Usually a BOM is at the beginning of a file, and so at
8368 * the beginning of a line; then we can just step over it.
8369 */
8370 if (start == dest)
8371 start = p + 1;
8372 else
8373 {
8374 /* have to shuffle buf to close gap */
8375 int adjust_prevlen = 0;
8376
8377 if (dest < buf)
8378 {
8379 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
8380 dest = buf;
8381 }
8382 if (readlen > p - buf + 1)
8383 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
8384 readlen -= 3 - adjust_prevlen;
8385 prevlen -= adjust_prevlen;
8386 p = dest - 1;
8387 }
8388 }
8389 }
8390#endif
8391 } /* for */
8392
8393 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
8394 break;
8395 if (start < p)
8396 {
8397 /* There's part of a line in buf, store it in "prev". */
8398 if (p - start + prevlen >= prevsize)
8399 {
8400 /* need bigger "prev" buffer */
8401 char_u *newprev;
8402
8403 /* A common use case is ordinary text files and "prev" gets a
8404 * fragment of a line, so the first allocation is made
8405 * small, to avoid repeatedly 'allocing' large and
8406 * 'reallocing' small. */
8407 if (prevsize == 0)
8408 prevsize = (long)(p - start);
8409 else
8410 {
8411 long grow50pc = (prevsize * 3) / 2;
8412 long growmin = (long)((p - start) * 2 + prevlen);
8413 prevsize = grow50pc > growmin ? grow50pc : growmin;
8414 }
8415 newprev = prev == NULL ? alloc(prevsize)
8416 : vim_realloc(prev, prevsize);
8417 if (newprev == NULL)
8418 {
8419 do_outofmem_msg((long_u)prevsize);
8420 failed = TRUE;
8421 break;
8422 }
8423 prev = newprev;
8424 }
8425 /* Add the line part to end of "prev". */
8426 mch_memmove(prev + prevlen, start, p - start);
8427 prevlen += (long)(p - start);
8428 }
8429 } /* while */
8430
8431 /*
8432 * For a negative line count use only the lines at the end of the file,
8433 * free the rest.
8434 */
8435 if (!failed && maxline < 0)
8436 while (cnt > -maxline)
8437 {
8438 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
8439 --cnt;
8440 }
8441
8442 if (failed)
8443 {
8444 list_free(rettv->vval.v_list);
8445 /* readfile doc says an empty list is returned on error */
8446 rettv->vval.v_list = list_alloc();
8447 }
8448
8449 vim_free(prev);
8450 fclose(fd);
8451}
8452
8453#if defined(FEAT_RELTIME)
8454static int list2proftime(typval_T *arg, proftime_T *tm);
8455
8456/*
8457 * Convert a List to proftime_T.
8458 * Return FAIL when there is something wrong.
8459 */
8460 static int
8461list2proftime(typval_T *arg, proftime_T *tm)
8462{
8463 long n1, n2;
8464 int error = FALSE;
8465
8466 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
8467 || arg->vval.v_list->lv_len != 2)
8468 return FAIL;
8469 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
8470 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
8471# ifdef WIN3264
8472 tm->HighPart = n1;
8473 tm->LowPart = n2;
8474# else
8475 tm->tv_sec = n1;
8476 tm->tv_usec = n2;
8477# endif
8478 return error ? FAIL : OK;
8479}
8480#endif /* FEAT_RELTIME */
8481
8482/*
8483 * "reltime()" function
8484 */
8485 static void
8486f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8487{
8488#ifdef FEAT_RELTIME
8489 proftime_T res;
8490 proftime_T start;
8491
8492 if (argvars[0].v_type == VAR_UNKNOWN)
8493 {
8494 /* No arguments: get current time. */
8495 profile_start(&res);
8496 }
8497 else if (argvars[1].v_type == VAR_UNKNOWN)
8498 {
8499 if (list2proftime(&argvars[0], &res) == FAIL)
8500 return;
8501 profile_end(&res);
8502 }
8503 else
8504 {
8505 /* Two arguments: compute the difference. */
8506 if (list2proftime(&argvars[0], &start) == FAIL
8507 || list2proftime(&argvars[1], &res) == FAIL)
8508 return;
8509 profile_sub(&res, &start);
8510 }
8511
8512 if (rettv_list_alloc(rettv) == OK)
8513 {
8514 long n1, n2;
8515
8516# ifdef WIN3264
8517 n1 = res.HighPart;
8518 n2 = res.LowPart;
8519# else
8520 n1 = res.tv_sec;
8521 n2 = res.tv_usec;
8522# endif
8523 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
8524 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
8525 }
8526#endif
8527}
8528
8529#ifdef FEAT_FLOAT
8530/*
8531 * "reltimefloat()" function
8532 */
8533 static void
8534f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
8535{
8536# ifdef FEAT_RELTIME
8537 proftime_T tm;
8538# endif
8539
8540 rettv->v_type = VAR_FLOAT;
8541 rettv->vval.v_float = 0;
8542# ifdef FEAT_RELTIME
8543 if (list2proftime(&argvars[0], &tm) == OK)
8544 rettv->vval.v_float = profile_float(&tm);
8545# endif
8546}
8547#endif
8548
8549/*
8550 * "reltimestr()" function
8551 */
8552 static void
8553f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
8554{
8555#ifdef FEAT_RELTIME
8556 proftime_T tm;
8557#endif
8558
8559 rettv->v_type = VAR_STRING;
8560 rettv->vval.v_string = NULL;
8561#ifdef FEAT_RELTIME
8562 if (list2proftime(&argvars[0], &tm) == OK)
8563 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
8564#endif
8565}
8566
8567#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
8568static void make_connection(void);
8569static int check_connection(void);
8570
8571 static void
8572make_connection(void)
8573{
8574 if (X_DISPLAY == NULL
8575# ifdef FEAT_GUI
8576 && !gui.in_use
8577# endif
8578 )
8579 {
8580 x_force_connect = TRUE;
8581 setup_term_clip();
8582 x_force_connect = FALSE;
8583 }
8584}
8585
8586 static int
8587check_connection(void)
8588{
8589 make_connection();
8590 if (X_DISPLAY == NULL)
8591 {
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01008592 EMSG(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008593 return FAIL;
8594 }
8595 return OK;
8596}
8597#endif
8598
8599#ifdef FEAT_CLIENTSERVER
8600 static void
8601remote_common(typval_T *argvars, typval_T *rettv, int expr)
8602{
8603 char_u *server_name;
8604 char_u *keys;
8605 char_u *r = NULL;
8606 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008607 int timeout = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008608# ifdef WIN32
8609 HWND w;
8610# else
8611 Window w;
8612# endif
8613
8614 if (check_restricted() || check_secure())
8615 return;
8616
8617# ifdef FEAT_X11
8618 if (check_connection() == FAIL)
8619 return;
8620# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008621 if (argvars[2].v_type != VAR_UNKNOWN
8622 && argvars[3].v_type != VAR_UNKNOWN)
8623 timeout = get_tv_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008624
8625 server_name = get_tv_string_chk(&argvars[0]);
8626 if (server_name == NULL)
8627 return; /* type error; errmsg already given */
8628 keys = get_tv_string_buf(&argvars[1], buf);
8629# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008630 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008631# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008632 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
8633 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008634# endif
8635 {
8636 if (r != NULL)
8637 EMSG(r); /* sending worked but evaluation failed */
8638 else
8639 EMSG2(_("E241: Unable to send to %s"), server_name);
8640 return;
8641 }
8642
8643 rettv->vval.v_string = r;
8644
8645 if (argvars[2].v_type != VAR_UNKNOWN)
8646 {
8647 dictitem_T v;
8648 char_u str[30];
8649 char_u *idvar;
8650
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008651 idvar = get_tv_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008652 if (idvar != NULL && *idvar != NUL)
8653 {
8654 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
8655 v.di_tv.v_type = VAR_STRING;
8656 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008657 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008658 vim_free(v.di_tv.vval.v_string);
8659 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008660 }
8661}
8662#endif
8663
8664/*
8665 * "remote_expr()" function
8666 */
8667 static void
8668f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
8669{
8670 rettv->v_type = VAR_STRING;
8671 rettv->vval.v_string = NULL;
8672#ifdef FEAT_CLIENTSERVER
8673 remote_common(argvars, rettv, TRUE);
8674#endif
8675}
8676
8677/*
8678 * "remote_foreground()" function
8679 */
8680 static void
8681f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8682{
8683#ifdef FEAT_CLIENTSERVER
8684# ifdef WIN32
8685 /* On Win32 it's done in this application. */
8686 {
8687 char_u *server_name = get_tv_string_chk(&argvars[0]);
8688
8689 if (server_name != NULL)
8690 serverForeground(server_name);
8691 }
8692# else
8693 /* Send a foreground() expression to the server. */
8694 argvars[1].v_type = VAR_STRING;
8695 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
8696 argvars[2].v_type = VAR_UNKNOWN;
8697 remote_common(argvars, rettv, TRUE);
8698 vim_free(argvars[1].vval.v_string);
8699# endif
8700#endif
8701}
8702
8703 static void
8704f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
8705{
8706#ifdef FEAT_CLIENTSERVER
8707 dictitem_T v;
8708 char_u *s = NULL;
8709# ifdef WIN32
8710 long_u n = 0;
8711# endif
8712 char_u *serverid;
8713
8714 if (check_restricted() || check_secure())
8715 {
8716 rettv->vval.v_number = -1;
8717 return;
8718 }
8719 serverid = get_tv_string_chk(&argvars[0]);
8720 if (serverid == NULL)
8721 {
8722 rettv->vval.v_number = -1;
8723 return; /* type error; errmsg already given */
8724 }
8725# ifdef WIN32
8726 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
8727 if (n == 0)
8728 rettv->vval.v_number = -1;
8729 else
8730 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008731 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008732 rettv->vval.v_number = (s != NULL);
8733 }
8734# else
8735 if (check_connection() == FAIL)
8736 return;
8737
8738 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
8739 serverStrToWin(serverid), &s);
8740# endif
8741
8742 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
8743 {
8744 char_u *retvar;
8745
8746 v.di_tv.v_type = VAR_STRING;
8747 v.di_tv.vval.v_string = vim_strsave(s);
8748 retvar = get_tv_string_chk(&argvars[1]);
8749 if (retvar != NULL)
8750 set_var(retvar, &v.di_tv, FALSE);
8751 vim_free(v.di_tv.vval.v_string);
8752 }
8753#else
8754 rettv->vval.v_number = -1;
8755#endif
8756}
8757
8758 static void
8759f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
8760{
8761 char_u *r = NULL;
8762
8763#ifdef FEAT_CLIENTSERVER
8764 char_u *serverid = get_tv_string_chk(&argvars[0]);
8765
8766 if (serverid != NULL && !check_restricted() && !check_secure())
8767 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008768 int timeout = 0;
Bram Moolenaar1662ce12017-03-19 21:47:50 +01008769# ifdef WIN32
8770 /* The server's HWND is encoded in the 'id' parameter */
8771 long_u n = 0;
8772# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008773
8774 if (argvars[1].v_type != VAR_UNKNOWN)
8775 timeout = get_tv_number(&argvars[1]);
8776
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008777# ifdef WIN32
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008778 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
8779 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008780 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008781 if (r == NULL)
8782# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008783 if (check_connection() == FAIL
8784 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
8785 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008786# endif
8787 EMSG(_("E277: Unable to read a server reply"));
8788 }
8789#endif
8790 rettv->v_type = VAR_STRING;
8791 rettv->vval.v_string = r;
8792}
8793
8794/*
8795 * "remote_send()" function
8796 */
8797 static void
8798f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
8799{
8800 rettv->v_type = VAR_STRING;
8801 rettv->vval.v_string = NULL;
8802#ifdef FEAT_CLIENTSERVER
8803 remote_common(argvars, rettv, FALSE);
8804#endif
8805}
8806
8807/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01008808 * "remote_startserver()" function
8809 */
8810 static void
8811f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8812{
8813#ifdef FEAT_CLIENTSERVER
8814 char_u *server = get_tv_string_chk(&argvars[0]);
8815
8816 if (server == NULL)
8817 return; /* type error; errmsg already given */
8818 if (serverName != NULL)
8819 EMSG(_("E941: already started a server"));
8820 else
8821 {
8822# ifdef FEAT_X11
8823 if (check_connection() == OK)
8824 serverRegisterName(X_DISPLAY, server);
8825# else
8826 serverSetName(server);
8827# endif
8828 }
8829#else
8830 EMSG(_("E942: +clientserver feature not available"));
8831#endif
8832}
8833
8834/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008835 * "remove()" function
8836 */
8837 static void
8838f_remove(typval_T *argvars, typval_T *rettv)
8839{
8840 list_T *l;
8841 listitem_T *item, *item2;
8842 listitem_T *li;
8843 long idx;
8844 long end;
8845 char_u *key;
8846 dict_T *d;
8847 dictitem_T *di;
8848 char_u *arg_errmsg = (char_u *)N_("remove() argument");
8849
8850 if (argvars[0].v_type == VAR_DICT)
8851 {
8852 if (argvars[2].v_type != VAR_UNKNOWN)
8853 EMSG2(_(e_toomanyarg), "remove()");
8854 else if ((d = argvars[0].vval.v_dict) != NULL
8855 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
8856 {
8857 key = get_tv_string_chk(&argvars[1]);
8858 if (key != NULL)
8859 {
8860 di = dict_find(d, key, -1);
8861 if (di == NULL)
8862 EMSG2(_(e_dictkey), key);
8863 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
8864 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
8865 {
8866 *rettv = di->di_tv;
8867 init_tv(&di->di_tv);
8868 dictitem_remove(d, di);
8869 }
8870 }
8871 }
8872 }
8873 else if (argvars[0].v_type != VAR_LIST)
8874 EMSG2(_(e_listdictarg), "remove()");
8875 else if ((l = argvars[0].vval.v_list) != NULL
8876 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
8877 {
8878 int error = FALSE;
8879
8880 idx = (long)get_tv_number_chk(&argvars[1], &error);
8881 if (error)
8882 ; /* type error: do nothing, errmsg already given */
8883 else if ((item = list_find(l, idx)) == NULL)
8884 EMSGN(_(e_listidx), idx);
8885 else
8886 {
8887 if (argvars[2].v_type == VAR_UNKNOWN)
8888 {
8889 /* Remove one item, return its value. */
8890 vimlist_remove(l, item, item);
8891 *rettv = item->li_tv;
8892 vim_free(item);
8893 }
8894 else
8895 {
8896 /* Remove range of items, return list with values. */
8897 end = (long)get_tv_number_chk(&argvars[2], &error);
8898 if (error)
8899 ; /* type error: do nothing */
8900 else if ((item2 = list_find(l, end)) == NULL)
8901 EMSGN(_(e_listidx), end);
8902 else
8903 {
8904 int cnt = 0;
8905
8906 for (li = item; li != NULL; li = li->li_next)
8907 {
8908 ++cnt;
8909 if (li == item2)
8910 break;
8911 }
8912 if (li == NULL) /* didn't find "item2" after "item" */
8913 EMSG(_(e_invrange));
8914 else
8915 {
8916 vimlist_remove(l, item, item2);
8917 if (rettv_list_alloc(rettv) == OK)
8918 {
8919 l = rettv->vval.v_list;
8920 l->lv_first = item;
8921 l->lv_last = item2;
8922 item->li_prev = NULL;
8923 item2->li_next = NULL;
8924 l->lv_len = cnt;
8925 }
8926 }
8927 }
8928 }
8929 }
8930 }
8931}
8932
8933/*
8934 * "rename({from}, {to})" function
8935 */
8936 static void
8937f_rename(typval_T *argvars, typval_T *rettv)
8938{
8939 char_u buf[NUMBUFLEN];
8940
8941 if (check_restricted() || check_secure())
8942 rettv->vval.v_number = -1;
8943 else
8944 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
8945 get_tv_string_buf(&argvars[1], buf));
8946}
8947
8948/*
8949 * "repeat()" function
8950 */
8951 static void
8952f_repeat(typval_T *argvars, typval_T *rettv)
8953{
8954 char_u *p;
8955 int n;
8956 int slen;
8957 int len;
8958 char_u *r;
8959 int i;
8960
8961 n = (int)get_tv_number(&argvars[1]);
8962 if (argvars[0].v_type == VAR_LIST)
8963 {
8964 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
8965 while (n-- > 0)
8966 if (list_extend(rettv->vval.v_list,
8967 argvars[0].vval.v_list, NULL) == FAIL)
8968 break;
8969 }
8970 else
8971 {
8972 p = get_tv_string(&argvars[0]);
8973 rettv->v_type = VAR_STRING;
8974 rettv->vval.v_string = NULL;
8975
8976 slen = (int)STRLEN(p);
8977 len = slen * n;
8978 if (len <= 0)
8979 return;
8980
8981 r = alloc(len + 1);
8982 if (r != NULL)
8983 {
8984 for (i = 0; i < n; i++)
8985 mch_memmove(r + i * slen, p, (size_t)slen);
8986 r[len] = NUL;
8987 }
8988
8989 rettv->vval.v_string = r;
8990 }
8991}
8992
8993/*
8994 * "resolve()" function
8995 */
8996 static void
8997f_resolve(typval_T *argvars, typval_T *rettv)
8998{
8999 char_u *p;
9000#ifdef HAVE_READLINK
9001 char_u *buf = NULL;
9002#endif
9003
9004 p = get_tv_string(&argvars[0]);
9005#ifdef FEAT_SHORTCUT
9006 {
9007 char_u *v = NULL;
9008
9009 v = mch_resolve_shortcut(p);
9010 if (v != NULL)
9011 rettv->vval.v_string = v;
9012 else
9013 rettv->vval.v_string = vim_strsave(p);
9014 }
9015#else
9016# ifdef HAVE_READLINK
9017 {
9018 char_u *cpy;
9019 int len;
9020 char_u *remain = NULL;
9021 char_u *q;
9022 int is_relative_to_current = FALSE;
9023 int has_trailing_pathsep = FALSE;
9024 int limit = 100;
9025
9026 p = vim_strsave(p);
9027
9028 if (p[0] == '.' && (vim_ispathsep(p[1])
9029 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9030 is_relative_to_current = TRUE;
9031
9032 len = STRLEN(p);
9033 if (len > 0 && after_pathsep(p, p + len))
9034 {
9035 has_trailing_pathsep = TRUE;
9036 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9037 }
9038
9039 q = getnextcomp(p);
9040 if (*q != NUL)
9041 {
9042 /* Separate the first path component in "p", and keep the
9043 * remainder (beginning with the path separator). */
9044 remain = vim_strsave(q - 1);
9045 q[-1] = NUL;
9046 }
9047
9048 buf = alloc(MAXPATHL + 1);
9049 if (buf == NULL)
9050 goto fail;
9051
9052 for (;;)
9053 {
9054 for (;;)
9055 {
9056 len = readlink((char *)p, (char *)buf, MAXPATHL);
9057 if (len <= 0)
9058 break;
9059 buf[len] = NUL;
9060
9061 if (limit-- == 0)
9062 {
9063 vim_free(p);
9064 vim_free(remain);
9065 EMSG(_("E655: Too many symbolic links (cycle?)"));
9066 rettv->vval.v_string = NULL;
9067 goto fail;
9068 }
9069
9070 /* Ensure that the result will have a trailing path separator
9071 * if the argument has one. */
9072 if (remain == NULL && has_trailing_pathsep)
9073 add_pathsep(buf);
9074
9075 /* Separate the first path component in the link value and
9076 * concatenate the remainders. */
9077 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9078 if (*q != NUL)
9079 {
9080 if (remain == NULL)
9081 remain = vim_strsave(q - 1);
9082 else
9083 {
9084 cpy = concat_str(q - 1, remain);
9085 if (cpy != NULL)
9086 {
9087 vim_free(remain);
9088 remain = cpy;
9089 }
9090 }
9091 q[-1] = NUL;
9092 }
9093
9094 q = gettail(p);
9095 if (q > p && *q == NUL)
9096 {
9097 /* Ignore trailing path separator. */
9098 q[-1] = NUL;
9099 q = gettail(p);
9100 }
9101 if (q > p && !mch_isFullName(buf))
9102 {
9103 /* symlink is relative to directory of argument */
9104 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
9105 if (cpy != NULL)
9106 {
9107 STRCPY(cpy, p);
9108 STRCPY(gettail(cpy), buf);
9109 vim_free(p);
9110 p = cpy;
9111 }
9112 }
9113 else
9114 {
9115 vim_free(p);
9116 p = vim_strsave(buf);
9117 }
9118 }
9119
9120 if (remain == NULL)
9121 break;
9122
9123 /* Append the first path component of "remain" to "p". */
9124 q = getnextcomp(remain + 1);
9125 len = q - remain - (*q != NUL);
9126 cpy = vim_strnsave(p, STRLEN(p) + len);
9127 if (cpy != NULL)
9128 {
9129 STRNCAT(cpy, remain, len);
9130 vim_free(p);
9131 p = cpy;
9132 }
9133 /* Shorten "remain". */
9134 if (*q != NUL)
9135 STRMOVE(remain, q - 1);
9136 else
9137 {
9138 vim_free(remain);
9139 remain = NULL;
9140 }
9141 }
9142
9143 /* If the result is a relative path name, make it explicitly relative to
9144 * the current directory if and only if the argument had this form. */
9145 if (!vim_ispathsep(*p))
9146 {
9147 if (is_relative_to_current
9148 && *p != NUL
9149 && !(p[0] == '.'
9150 && (p[1] == NUL
9151 || vim_ispathsep(p[1])
9152 || (p[1] == '.'
9153 && (p[2] == NUL
9154 || vim_ispathsep(p[2]))))))
9155 {
9156 /* Prepend "./". */
9157 cpy = concat_str((char_u *)"./", p);
9158 if (cpy != NULL)
9159 {
9160 vim_free(p);
9161 p = cpy;
9162 }
9163 }
9164 else if (!is_relative_to_current)
9165 {
9166 /* Strip leading "./". */
9167 q = p;
9168 while (q[0] == '.' && vim_ispathsep(q[1]))
9169 q += 2;
9170 if (q > p)
9171 STRMOVE(p, p + 2);
9172 }
9173 }
9174
9175 /* Ensure that the result will have no trailing path separator
9176 * if the argument had none. But keep "/" or "//". */
9177 if (!has_trailing_pathsep)
9178 {
9179 q = p + STRLEN(p);
9180 if (after_pathsep(p, q))
9181 *gettail_sep(p) = NUL;
9182 }
9183
9184 rettv->vval.v_string = p;
9185 }
9186# else
9187 rettv->vval.v_string = vim_strsave(p);
9188# endif
9189#endif
9190
9191 simplify_filename(rettv->vval.v_string);
9192
9193#ifdef HAVE_READLINK
9194fail:
9195 vim_free(buf);
9196#endif
9197 rettv->v_type = VAR_STRING;
9198}
9199
9200/*
9201 * "reverse({list})" function
9202 */
9203 static void
9204f_reverse(typval_T *argvars, typval_T *rettv)
9205{
9206 list_T *l;
9207 listitem_T *li, *ni;
9208
9209 if (argvars[0].v_type != VAR_LIST)
9210 EMSG2(_(e_listarg), "reverse()");
9211 else if ((l = argvars[0].vval.v_list) != NULL
9212 && !tv_check_lock(l->lv_lock,
9213 (char_u *)N_("reverse() argument"), TRUE))
9214 {
9215 li = l->lv_last;
9216 l->lv_first = l->lv_last = NULL;
9217 l->lv_len = 0;
9218 while (li != NULL)
9219 {
9220 ni = li->li_prev;
9221 list_append(l, li);
9222 li = ni;
9223 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009224 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009225 l->lv_idx = l->lv_len - l->lv_idx - 1;
9226 }
9227}
9228
9229#define SP_NOMOVE 0x01 /* don't move cursor */
9230#define SP_REPEAT 0x02 /* repeat to find outer pair */
9231#define SP_RETCOUNT 0x04 /* return matchcount */
9232#define SP_SETPCMARK 0x08 /* set previous context mark */
9233#define SP_START 0x10 /* accept match at start position */
9234#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9235#define SP_END 0x40 /* leave cursor at end of match */
9236#define SP_COLUMN 0x80 /* start at cursor column */
9237
9238static int get_search_arg(typval_T *varp, int *flagsp);
9239
9240/*
9241 * Get flags for a search function.
9242 * Possibly sets "p_ws".
9243 * Returns BACKWARD, FORWARD or zero (for an error).
9244 */
9245 static int
9246get_search_arg(typval_T *varp, int *flagsp)
9247{
9248 int dir = FORWARD;
9249 char_u *flags;
9250 char_u nbuf[NUMBUFLEN];
9251 int mask;
9252
9253 if (varp->v_type != VAR_UNKNOWN)
9254 {
9255 flags = get_tv_string_buf_chk(varp, nbuf);
9256 if (flags == NULL)
9257 return 0; /* type error; errmsg already given */
9258 while (*flags != NUL)
9259 {
9260 switch (*flags)
9261 {
9262 case 'b': dir = BACKWARD; break;
9263 case 'w': p_ws = TRUE; break;
9264 case 'W': p_ws = FALSE; break;
9265 default: mask = 0;
9266 if (flagsp != NULL)
9267 switch (*flags)
9268 {
9269 case 'c': mask = SP_START; break;
9270 case 'e': mask = SP_END; break;
9271 case 'm': mask = SP_RETCOUNT; break;
9272 case 'n': mask = SP_NOMOVE; break;
9273 case 'p': mask = SP_SUBPAT; break;
9274 case 'r': mask = SP_REPEAT; break;
9275 case 's': mask = SP_SETPCMARK; break;
9276 case 'z': mask = SP_COLUMN; break;
9277 }
9278 if (mask == 0)
9279 {
9280 EMSG2(_(e_invarg2), flags);
9281 dir = 0;
9282 }
9283 else
9284 *flagsp |= mask;
9285 }
9286 if (dir == 0)
9287 break;
9288 ++flags;
9289 }
9290 }
9291 return dir;
9292}
9293
9294/*
9295 * Shared by search() and searchpos() functions.
9296 */
9297 static int
9298search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9299{
9300 int flags;
9301 char_u *pat;
9302 pos_T pos;
9303 pos_T save_cursor;
9304 int save_p_ws = p_ws;
9305 int dir;
9306 int retval = 0; /* default: FAIL */
9307 long lnum_stop = 0;
9308 proftime_T tm;
9309#ifdef FEAT_RELTIME
9310 long time_limit = 0;
9311#endif
9312 int options = SEARCH_KEEP;
9313 int subpatnum;
9314
9315 pat = get_tv_string(&argvars[0]);
9316 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
9317 if (dir == 0)
9318 goto theend;
9319 flags = *flagsp;
9320 if (flags & SP_START)
9321 options |= SEARCH_START;
9322 if (flags & SP_END)
9323 options |= SEARCH_END;
9324 if (flags & SP_COLUMN)
9325 options |= SEARCH_COL;
9326
9327 /* Optional arguments: line number to stop searching and timeout. */
9328 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9329 {
9330 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
9331 if (lnum_stop < 0)
9332 goto theend;
9333#ifdef FEAT_RELTIME
9334 if (argvars[3].v_type != VAR_UNKNOWN)
9335 {
9336 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
9337 if (time_limit < 0)
9338 goto theend;
9339 }
9340#endif
9341 }
9342
9343#ifdef FEAT_RELTIME
9344 /* Set the time limit, if there is one. */
9345 profile_setlimit(time_limit, &tm);
9346#endif
9347
9348 /*
9349 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
9350 * Check to make sure only those flags are set.
9351 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
9352 * flags cannot be set. Check for that condition also.
9353 */
9354 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
9355 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9356 {
9357 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
9358 goto theend;
9359 }
9360
9361 pos = save_cursor = curwin->w_cursor;
9362 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009363 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009364 if (subpatnum != FAIL)
9365 {
9366 if (flags & SP_SUBPAT)
9367 retval = subpatnum;
9368 else
9369 retval = pos.lnum;
9370 if (flags & SP_SETPCMARK)
9371 setpcmark();
9372 curwin->w_cursor = pos;
9373 if (match_pos != NULL)
9374 {
9375 /* Store the match cursor position */
9376 match_pos->lnum = pos.lnum;
9377 match_pos->col = pos.col + 1;
9378 }
9379 /* "/$" will put the cursor after the end of the line, may need to
9380 * correct that here */
9381 check_cursor();
9382 }
9383
9384 /* If 'n' flag is used: restore cursor position. */
9385 if (flags & SP_NOMOVE)
9386 curwin->w_cursor = save_cursor;
9387 else
9388 curwin->w_set_curswant = TRUE;
9389theend:
9390 p_ws = save_p_ws;
9391
9392 return retval;
9393}
9394
9395#ifdef FEAT_FLOAT
9396
9397/*
9398 * round() is not in C90, use ceil() or floor() instead.
9399 */
9400 float_T
9401vim_round(float_T f)
9402{
9403 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
9404}
9405
9406/*
9407 * "round({float})" function
9408 */
9409 static void
9410f_round(typval_T *argvars, typval_T *rettv)
9411{
9412 float_T f = 0.0;
9413
9414 rettv->v_type = VAR_FLOAT;
9415 if (get_float_arg(argvars, &f) == OK)
9416 rettv->vval.v_float = vim_round(f);
9417 else
9418 rettv->vval.v_float = 0.0;
9419}
9420#endif
9421
9422/*
9423 * "screenattr()" function
9424 */
9425 static void
9426f_screenattr(typval_T *argvars, typval_T *rettv)
9427{
9428 int row;
9429 int col;
9430 int c;
9431
9432 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9433 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9434 if (row < 0 || row >= screen_Rows
9435 || col < 0 || col >= screen_Columns)
9436 c = -1;
9437 else
9438 c = ScreenAttrs[LineOffset[row] + col];
9439 rettv->vval.v_number = c;
9440}
9441
9442/*
9443 * "screenchar()" function
9444 */
9445 static void
9446f_screenchar(typval_T *argvars, typval_T *rettv)
9447{
9448 int row;
9449 int col;
9450 int off;
9451 int c;
9452
9453 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9454 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9455 if (row < 0 || row >= screen_Rows
9456 || col < 0 || col >= screen_Columns)
9457 c = -1;
9458 else
9459 {
9460 off = LineOffset[row] + col;
9461#ifdef FEAT_MBYTE
9462 if (enc_utf8 && ScreenLinesUC[off] != 0)
9463 c = ScreenLinesUC[off];
9464 else
9465#endif
9466 c = ScreenLines[off];
9467 }
9468 rettv->vval.v_number = c;
9469}
9470
9471/*
9472 * "screencol()" function
9473 *
9474 * First column is 1 to be consistent with virtcol().
9475 */
9476 static void
9477f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
9478{
9479 rettv->vval.v_number = screen_screencol() + 1;
9480}
9481
9482/*
9483 * "screenrow()" function
9484 */
9485 static void
9486f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
9487{
9488 rettv->vval.v_number = screen_screenrow() + 1;
9489}
9490
9491/*
9492 * "search()" function
9493 */
9494 static void
9495f_search(typval_T *argvars, typval_T *rettv)
9496{
9497 int flags = 0;
9498
9499 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
9500}
9501
9502/*
9503 * "searchdecl()" function
9504 */
9505 static void
9506f_searchdecl(typval_T *argvars, typval_T *rettv)
9507{
9508 int locally = 1;
9509 int thisblock = 0;
9510 int error = FALSE;
9511 char_u *name;
9512
9513 rettv->vval.v_number = 1; /* default: FAIL */
9514
9515 name = get_tv_string_chk(&argvars[0]);
9516 if (argvars[1].v_type != VAR_UNKNOWN)
9517 {
9518 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
9519 if (!error && argvars[2].v_type != VAR_UNKNOWN)
9520 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
9521 }
9522 if (!error && name != NULL)
9523 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
9524 locally, thisblock, SEARCH_KEEP) == FAIL;
9525}
9526
9527/*
9528 * Used by searchpair() and searchpairpos()
9529 */
9530 static int
9531searchpair_cmn(typval_T *argvars, pos_T *match_pos)
9532{
9533 char_u *spat, *mpat, *epat;
9534 char_u *skip;
9535 int save_p_ws = p_ws;
9536 int dir;
9537 int flags = 0;
9538 char_u nbuf1[NUMBUFLEN];
9539 char_u nbuf2[NUMBUFLEN];
9540 char_u nbuf3[NUMBUFLEN];
9541 int retval = 0; /* default: FAIL */
9542 long lnum_stop = 0;
9543 long time_limit = 0;
9544
9545 /* Get the three pattern arguments: start, middle, end. */
9546 spat = get_tv_string_chk(&argvars[0]);
9547 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
9548 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
9549 if (spat == NULL || mpat == NULL || epat == NULL)
9550 goto theend; /* type error */
9551
9552 /* Handle the optional fourth argument: flags */
9553 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
9554 if (dir == 0)
9555 goto theend;
9556
9557 /* Don't accept SP_END or SP_SUBPAT.
9558 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
9559 */
9560 if ((flags & (SP_END | SP_SUBPAT)) != 0
9561 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9562 {
9563 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
9564 goto theend;
9565 }
9566
9567 /* Using 'r' implies 'W', otherwise it doesn't work. */
9568 if (flags & SP_REPEAT)
9569 p_ws = FALSE;
9570
9571 /* Optional fifth argument: skip expression */
9572 if (argvars[3].v_type == VAR_UNKNOWN
9573 || argvars[4].v_type == VAR_UNKNOWN)
9574 skip = (char_u *)"";
9575 else
9576 {
9577 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
9578 if (argvars[5].v_type != VAR_UNKNOWN)
9579 {
9580 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
9581 if (lnum_stop < 0)
9582 goto theend;
9583#ifdef FEAT_RELTIME
9584 if (argvars[6].v_type != VAR_UNKNOWN)
9585 {
9586 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
9587 if (time_limit < 0)
9588 goto theend;
9589 }
9590#endif
9591 }
9592 }
9593 if (skip == NULL)
9594 goto theend; /* type error */
9595
9596 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
9597 match_pos, lnum_stop, time_limit);
9598
9599theend:
9600 p_ws = save_p_ws;
9601
9602 return retval;
9603}
9604
9605/*
9606 * "searchpair()" function
9607 */
9608 static void
9609f_searchpair(typval_T *argvars, typval_T *rettv)
9610{
9611 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
9612}
9613
9614/*
9615 * "searchpairpos()" function
9616 */
9617 static void
9618f_searchpairpos(typval_T *argvars, typval_T *rettv)
9619{
9620 pos_T match_pos;
9621 int lnum = 0;
9622 int col = 0;
9623
9624 if (rettv_list_alloc(rettv) == FAIL)
9625 return;
9626
9627 if (searchpair_cmn(argvars, &match_pos) > 0)
9628 {
9629 lnum = match_pos.lnum;
9630 col = match_pos.col;
9631 }
9632
9633 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9634 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9635}
9636
9637/*
9638 * Search for a start/middle/end thing.
9639 * Used by searchpair(), see its documentation for the details.
9640 * Returns 0 or -1 for no match,
9641 */
9642 long
9643do_searchpair(
9644 char_u *spat, /* start pattern */
9645 char_u *mpat, /* middle pattern */
9646 char_u *epat, /* end pattern */
9647 int dir, /* BACKWARD or FORWARD */
9648 char_u *skip, /* skip expression */
9649 int flags, /* SP_SETPCMARK and other SP_ values */
9650 pos_T *match_pos,
9651 linenr_T lnum_stop, /* stop at this line if not zero */
9652 long time_limit UNUSED) /* stop after this many msec */
9653{
9654 char_u *save_cpo;
9655 char_u *pat, *pat2 = NULL, *pat3 = NULL;
9656 long retval = 0;
9657 pos_T pos;
9658 pos_T firstpos;
9659 pos_T foundpos;
9660 pos_T save_cursor;
9661 pos_T save_pos;
9662 int n;
9663 int r;
9664 int nest = 1;
9665 int err;
9666 int options = SEARCH_KEEP;
9667 proftime_T tm;
9668
9669 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
9670 save_cpo = p_cpo;
9671 p_cpo = empty_option;
9672
9673#ifdef FEAT_RELTIME
9674 /* Set the time limit, if there is one. */
9675 profile_setlimit(time_limit, &tm);
9676#endif
9677
9678 /* Make two search patterns: start/end (pat2, for in nested pairs) and
9679 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009680 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
9681 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009682 if (pat2 == NULL || pat3 == NULL)
9683 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009684 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009685 if (*mpat == NUL)
9686 STRCPY(pat3, pat2);
9687 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009688 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009689 spat, epat, mpat);
9690 if (flags & SP_START)
9691 options |= SEARCH_START;
9692
9693 save_cursor = curwin->w_cursor;
9694 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009695 CLEAR_POS(&firstpos);
9696 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009697 pat = pat3;
9698 for (;;)
9699 {
9700 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009701 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009702 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009703 /* didn't find it or found the first match again: FAIL */
9704 break;
9705
9706 if (firstpos.lnum == 0)
9707 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009708 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009709 {
9710 /* Found the same position again. Can happen with a pattern that
9711 * has "\zs" at the end and searching backwards. Advance one
9712 * character and try again. */
9713 if (dir == BACKWARD)
9714 decl(&pos);
9715 else
9716 incl(&pos);
9717 }
9718 foundpos = pos;
9719
9720 /* clear the start flag to avoid getting stuck here */
9721 options &= ~SEARCH_START;
9722
9723 /* If the skip pattern matches, ignore this match. */
9724 if (*skip != NUL)
9725 {
9726 save_pos = curwin->w_cursor;
9727 curwin->w_cursor = pos;
9728 r = eval_to_bool(skip, &err, NULL, FALSE);
9729 curwin->w_cursor = save_pos;
9730 if (err)
9731 {
9732 /* Evaluating {skip} caused an error, break here. */
9733 curwin->w_cursor = save_cursor;
9734 retval = -1;
9735 break;
9736 }
9737 if (r)
9738 continue;
9739 }
9740
9741 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
9742 {
9743 /* Found end when searching backwards or start when searching
9744 * forward: nested pair. */
9745 ++nest;
9746 pat = pat2; /* nested, don't search for middle */
9747 }
9748 else
9749 {
9750 /* Found end when searching forward or start when searching
9751 * backward: end of (nested) pair; or found middle in outer pair. */
9752 if (--nest == 1)
9753 pat = pat3; /* outer level, search for middle */
9754 }
9755
9756 if (nest == 0)
9757 {
9758 /* Found the match: return matchcount or line number. */
9759 if (flags & SP_RETCOUNT)
9760 ++retval;
9761 else
9762 retval = pos.lnum;
9763 if (flags & SP_SETPCMARK)
9764 setpcmark();
9765 curwin->w_cursor = pos;
9766 if (!(flags & SP_REPEAT))
9767 break;
9768 nest = 1; /* search for next unmatched */
9769 }
9770 }
9771
9772 if (match_pos != NULL)
9773 {
9774 /* Store the match cursor position */
9775 match_pos->lnum = curwin->w_cursor.lnum;
9776 match_pos->col = curwin->w_cursor.col + 1;
9777 }
9778
9779 /* If 'n' flag is used or search failed: restore cursor position. */
9780 if ((flags & SP_NOMOVE) || retval == 0)
9781 curwin->w_cursor = save_cursor;
9782
9783theend:
9784 vim_free(pat2);
9785 vim_free(pat3);
9786 if (p_cpo == empty_option)
9787 p_cpo = save_cpo;
9788 else
9789 /* Darn, evaluating the {skip} expression changed the value. */
9790 free_string_option(save_cpo);
9791
9792 return retval;
9793}
9794
9795/*
9796 * "searchpos()" function
9797 */
9798 static void
9799f_searchpos(typval_T *argvars, typval_T *rettv)
9800{
9801 pos_T match_pos;
9802 int lnum = 0;
9803 int col = 0;
9804 int n;
9805 int flags = 0;
9806
9807 if (rettv_list_alloc(rettv) == FAIL)
9808 return;
9809
9810 n = search_cmn(argvars, &match_pos, &flags);
9811 if (n > 0)
9812 {
9813 lnum = match_pos.lnum;
9814 col = match_pos.col;
9815 }
9816
9817 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9818 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9819 if (flags & SP_SUBPAT)
9820 list_append_number(rettv->vval.v_list, (varnumber_T)n);
9821}
9822
9823 static void
9824f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
9825{
9826#ifdef FEAT_CLIENTSERVER
9827 char_u buf[NUMBUFLEN];
9828 char_u *server = get_tv_string_chk(&argvars[0]);
9829 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
9830
9831 rettv->vval.v_number = -1;
9832 if (server == NULL || reply == NULL)
9833 return;
9834 if (check_restricted() || check_secure())
9835 return;
9836# ifdef FEAT_X11
9837 if (check_connection() == FAIL)
9838 return;
9839# endif
9840
9841 if (serverSendReply(server, reply) < 0)
9842 {
9843 EMSG(_("E258: Unable to send to client"));
9844 return;
9845 }
9846 rettv->vval.v_number = 0;
9847#else
9848 rettv->vval.v_number = -1;
9849#endif
9850}
9851
9852 static void
9853f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
9854{
9855 char_u *r = NULL;
9856
9857#ifdef FEAT_CLIENTSERVER
9858# ifdef WIN32
9859 r = serverGetVimNames();
9860# else
9861 make_connection();
9862 if (X_DISPLAY != NULL)
9863 r = serverGetVimNames(X_DISPLAY);
9864# endif
9865#endif
9866 rettv->v_type = VAR_STRING;
9867 rettv->vval.v_string = r;
9868}
9869
9870/*
9871 * "setbufvar()" function
9872 */
9873 static void
9874f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
9875{
9876 buf_T *buf;
9877 char_u *varname, *bufvarname;
9878 typval_T *varp;
9879 char_u nbuf[NUMBUFLEN];
9880
9881 if (check_restricted() || check_secure())
9882 return;
9883 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9884 varname = get_tv_string_chk(&argvars[1]);
9885 buf = get_buf_tv(&argvars[0], FALSE);
9886 varp = &argvars[2];
9887
9888 if (buf != NULL && varname != NULL && varp != NULL)
9889 {
9890 if (*varname == '&')
9891 {
9892 long numval;
9893 char_u *strval;
9894 int error = FALSE;
9895 aco_save_T aco;
9896
9897 /* set curbuf to be our buf, temporarily */
9898 aucmd_prepbuf(&aco, buf);
9899
9900 ++varname;
9901 numval = (long)get_tv_number_chk(varp, &error);
9902 strval = get_tv_string_buf_chk(varp, nbuf);
9903 if (!error && strval != NULL)
9904 set_option_value(varname, numval, strval, OPT_LOCAL);
9905
9906 /* reset notion of buffer */
9907 aucmd_restbuf(&aco);
9908 }
9909 else
9910 {
9911 buf_T *save_curbuf = curbuf;
9912
9913 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
9914 if (bufvarname != NULL)
9915 {
9916 curbuf = buf;
9917 STRCPY(bufvarname, "b:");
9918 STRCPY(bufvarname + 2, varname);
9919 set_var(bufvarname, varp, TRUE);
9920 vim_free(bufvarname);
9921 curbuf = save_curbuf;
9922 }
9923 }
9924 }
9925}
9926
9927 static void
9928f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
9929{
9930 dict_T *d;
9931 dictitem_T *di;
9932 char_u *csearch;
9933
9934 if (argvars[0].v_type != VAR_DICT)
9935 {
9936 EMSG(_(e_dictreq));
9937 return;
9938 }
9939
9940 if ((d = argvars[0].vval.v_dict) != NULL)
9941 {
9942 csearch = get_dict_string(d, (char_u *)"char", FALSE);
9943 if (csearch != NULL)
9944 {
9945#ifdef FEAT_MBYTE
9946 if (enc_utf8)
9947 {
9948 int pcc[MAX_MCO];
9949 int c = utfc_ptr2char(csearch, pcc);
9950
9951 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
9952 }
9953 else
9954#endif
9955 set_last_csearch(PTR2CHAR(csearch),
9956 csearch, MB_PTR2LEN(csearch));
9957 }
9958
9959 di = dict_find(d, (char_u *)"forward", -1);
9960 if (di != NULL)
9961 set_csearch_direction((int)get_tv_number(&di->di_tv)
9962 ? FORWARD : BACKWARD);
9963
9964 di = dict_find(d, (char_u *)"until", -1);
9965 if (di != NULL)
9966 set_csearch_until(!!get_tv_number(&di->di_tv));
9967 }
9968}
9969
9970/*
9971 * "setcmdpos()" function
9972 */
9973 static void
9974f_setcmdpos(typval_T *argvars, typval_T *rettv)
9975{
9976 int pos = (int)get_tv_number(&argvars[0]) - 1;
9977
9978 if (pos >= 0)
9979 rettv->vval.v_number = set_cmdline_pos(pos);
9980}
9981
9982/*
9983 * "setfperm({fname}, {mode})" function
9984 */
9985 static void
9986f_setfperm(typval_T *argvars, typval_T *rettv)
9987{
9988 char_u *fname;
9989 char_u modebuf[NUMBUFLEN];
9990 char_u *mode_str;
9991 int i;
9992 int mask;
9993 int mode = 0;
9994
9995 rettv->vval.v_number = 0;
9996 fname = get_tv_string_chk(&argvars[0]);
9997 if (fname == NULL)
9998 return;
9999 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
10000 if (mode_str == NULL)
10001 return;
10002 if (STRLEN(mode_str) != 9)
10003 {
10004 EMSG2(_(e_invarg2), mode_str);
10005 return;
10006 }
10007
10008 mask = 1;
10009 for (i = 8; i >= 0; --i)
10010 {
10011 if (mode_str[i] != '-')
10012 mode |= mask;
10013 mask = mask << 1;
10014 }
10015 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10016}
10017
10018/*
10019 * "setline()" function
10020 */
10021 static void
10022f_setline(typval_T *argvars, typval_T *rettv)
10023{
10024 linenr_T lnum;
10025 char_u *line = NULL;
10026 list_T *l = NULL;
10027 listitem_T *li = NULL;
10028 long added = 0;
10029 linenr_T lcount = curbuf->b_ml.ml_line_count;
10030
10031 lnum = get_tv_lnum(&argvars[0]);
10032 if (argvars[1].v_type == VAR_LIST)
10033 {
10034 l = argvars[1].vval.v_list;
10035 li = l->lv_first;
10036 }
10037 else
10038 line = get_tv_string_chk(&argvars[1]);
10039
10040 /* default result is zero == OK */
10041 for (;;)
10042 {
10043 if (l != NULL)
10044 {
10045 /* list argument, get next string */
10046 if (li == NULL)
10047 break;
10048 line = get_tv_string_chk(&li->li_tv);
10049 li = li->li_next;
10050 }
10051
10052 rettv->vval.v_number = 1; /* FAIL */
10053 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
10054 break;
10055
10056 /* When coming here from Insert mode, sync undo, so that this can be
10057 * undone separately from what was previously inserted. */
10058 if (u_sync_once == 2)
10059 {
10060 u_sync_once = 1; /* notify that u_sync() was called */
10061 u_sync(TRUE);
10062 }
10063
10064 if (lnum <= curbuf->b_ml.ml_line_count)
10065 {
10066 /* existing line, replace it */
10067 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
10068 {
10069 changed_bytes(lnum, 0);
10070 if (lnum == curwin->w_cursor.lnum)
10071 check_cursor_col();
10072 rettv->vval.v_number = 0; /* OK */
10073 }
10074 }
10075 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
10076 {
10077 /* lnum is one past the last line, append the line */
10078 ++added;
10079 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
10080 rettv->vval.v_number = 0; /* OK */
10081 }
10082
10083 if (l == NULL) /* only one string argument */
10084 break;
10085 ++lnum;
10086 }
10087
10088 if (added > 0)
10089 appended_lines_mark(lcount, added);
10090}
10091
Bram Moolenaard823fa92016-08-12 16:29:27 +020010092static 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 +020010093
10094/*
10095 * Used by "setqflist()" and "setloclist()" functions
10096 */
10097 static void
10098set_qf_ll_list(
10099 win_T *wp UNUSED,
10100 typval_T *list_arg UNUSED,
10101 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010102 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010103 typval_T *rettv)
10104{
10105#ifdef FEAT_QUICKFIX
10106 static char *e_invact = N_("E927: Invalid action: '%s'");
10107 char_u *act;
10108 int action = 0;
10109#endif
10110
10111 rettv->vval.v_number = -1;
10112
10113#ifdef FEAT_QUICKFIX
10114 if (list_arg->v_type != VAR_LIST)
10115 EMSG(_(e_listreq));
10116 else
10117 {
10118 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010119 dict_T *d = NULL;
10120 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010121
10122 if (action_arg->v_type == VAR_STRING)
10123 {
10124 act = get_tv_string_chk(action_arg);
10125 if (act == NULL)
10126 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020010127 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
10128 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010129 action = *act;
10130 else
10131 EMSG2(_(e_invact), act);
10132 }
10133 else if (action_arg->v_type == VAR_UNKNOWN)
10134 action = ' ';
10135 else
10136 EMSG(_(e_stringreq));
10137
Bram Moolenaard823fa92016-08-12 16:29:27 +020010138 if (action_arg->v_type != VAR_UNKNOWN
10139 && what_arg->v_type != VAR_UNKNOWN)
10140 {
10141 if (what_arg->v_type == VAR_DICT)
10142 d = what_arg->vval.v_dict;
10143 else
10144 {
10145 EMSG(_(e_dictreq));
10146 valid_dict = FALSE;
10147 }
10148 }
10149
10150 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
10151 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()"), d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010152 rettv->vval.v_number = 0;
10153 }
10154#endif
10155}
10156
10157/*
10158 * "setloclist()" function
10159 */
10160 static void
10161f_setloclist(typval_T *argvars, typval_T *rettv)
10162{
10163 win_T *win;
10164
10165 rettv->vval.v_number = -1;
10166
10167 win = find_win_by_nr(&argvars[0], NULL);
10168 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020010169 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010170}
10171
10172/*
10173 * "setmatches()" function
10174 */
10175 static void
10176f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10177{
10178#ifdef FEAT_SEARCH_EXTRA
10179 list_T *l;
10180 listitem_T *li;
10181 dict_T *d;
10182 list_T *s = NULL;
10183
10184 rettv->vval.v_number = -1;
10185 if (argvars[0].v_type != VAR_LIST)
10186 {
10187 EMSG(_(e_listreq));
10188 return;
10189 }
10190 if ((l = argvars[0].vval.v_list) != NULL)
10191 {
10192
10193 /* To some extent make sure that we are dealing with a list from
10194 * "getmatches()". */
10195 li = l->lv_first;
10196 while (li != NULL)
10197 {
10198 if (li->li_tv.v_type != VAR_DICT
10199 || (d = li->li_tv.vval.v_dict) == NULL)
10200 {
10201 EMSG(_(e_invarg));
10202 return;
10203 }
10204 if (!(dict_find(d, (char_u *)"group", -1) != NULL
10205 && (dict_find(d, (char_u *)"pattern", -1) != NULL
10206 || dict_find(d, (char_u *)"pos1", -1) != NULL)
10207 && dict_find(d, (char_u *)"priority", -1) != NULL
10208 && dict_find(d, (char_u *)"id", -1) != NULL))
10209 {
10210 EMSG(_(e_invarg));
10211 return;
10212 }
10213 li = li->li_next;
10214 }
10215
10216 clear_matches(curwin);
10217 li = l->lv_first;
10218 while (li != NULL)
10219 {
10220 int i = 0;
10221 char_u buf[5];
10222 dictitem_T *di;
10223 char_u *group;
10224 int priority;
10225 int id;
10226 char_u *conceal;
10227
10228 d = li->li_tv.vval.v_dict;
10229 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
10230 {
10231 if (s == NULL)
10232 {
10233 s = list_alloc();
10234 if (s == NULL)
10235 return;
10236 }
10237
10238 /* match from matchaddpos() */
10239 for (i = 1; i < 9; i++)
10240 {
10241 sprintf((char *)buf, (char *)"pos%d", i);
10242 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
10243 {
10244 if (di->di_tv.v_type != VAR_LIST)
10245 return;
10246
10247 list_append_tv(s, &di->di_tv);
10248 s->lv_refcount++;
10249 }
10250 else
10251 break;
10252 }
10253 }
10254
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010255 group = get_dict_string(d, (char_u *)"group", TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010256 priority = (int)get_dict_number(d, (char_u *)"priority");
10257 id = (int)get_dict_number(d, (char_u *)"id");
10258 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010259 ? get_dict_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010260 : NULL;
10261 if (i == 0)
10262 {
10263 match_add(curwin, group,
10264 get_dict_string(d, (char_u *)"pattern", FALSE),
10265 priority, id, NULL, conceal);
10266 }
10267 else
10268 {
10269 match_add(curwin, group, NULL, priority, id, s, conceal);
10270 list_unref(s);
10271 s = NULL;
10272 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010273 vim_free(group);
10274 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010275
10276 li = li->li_next;
10277 }
10278 rettv->vval.v_number = 0;
10279 }
10280#endif
10281}
10282
10283/*
10284 * "setpos()" function
10285 */
10286 static void
10287f_setpos(typval_T *argvars, typval_T *rettv)
10288{
10289 pos_T pos;
10290 int fnum;
10291 char_u *name;
10292 colnr_T curswant = -1;
10293
10294 rettv->vval.v_number = -1;
10295 name = get_tv_string_chk(argvars);
10296 if (name != NULL)
10297 {
10298 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10299 {
10300 if (--pos.col < 0)
10301 pos.col = 0;
10302 if (name[0] == '.' && name[1] == NUL)
10303 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010304 /* set cursor; "fnum" is ignored */
10305 curwin->w_cursor = pos;
10306 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010307 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010308 curwin->w_curswant = curswant - 1;
10309 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010310 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010311 check_cursor();
10312 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010313 }
10314 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10315 {
10316 /* set mark */
10317 if (setmark_pos(name[1], &pos, fnum) == OK)
10318 rettv->vval.v_number = 0;
10319 }
10320 else
10321 EMSG(_(e_invarg));
10322 }
10323 }
10324}
10325
10326/*
10327 * "setqflist()" function
10328 */
10329 static void
10330f_setqflist(typval_T *argvars, typval_T *rettv)
10331{
Bram Moolenaard823fa92016-08-12 16:29:27 +020010332 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010333}
10334
10335/*
10336 * "setreg()" function
10337 */
10338 static void
10339f_setreg(typval_T *argvars, typval_T *rettv)
10340{
10341 int regname;
10342 char_u *strregname;
10343 char_u *stropt;
10344 char_u *strval;
10345 int append;
10346 char_u yank_type;
10347 long block_len;
10348
10349 block_len = -1;
10350 yank_type = MAUTO;
10351 append = FALSE;
10352
10353 strregname = get_tv_string_chk(argvars);
10354 rettv->vval.v_number = 1; /* FAIL is default */
10355
10356 if (strregname == NULL)
10357 return; /* type error; errmsg already given */
10358 regname = *strregname;
10359 if (regname == 0 || regname == '@')
10360 regname = '"';
10361
10362 if (argvars[2].v_type != VAR_UNKNOWN)
10363 {
10364 stropt = get_tv_string_chk(&argvars[2]);
10365 if (stropt == NULL)
10366 return; /* type error */
10367 for (; *stropt != NUL; ++stropt)
10368 switch (*stropt)
10369 {
10370 case 'a': case 'A': /* append */
10371 append = TRUE;
10372 break;
10373 case 'v': case 'c': /* character-wise selection */
10374 yank_type = MCHAR;
10375 break;
10376 case 'V': case 'l': /* line-wise selection */
10377 yank_type = MLINE;
10378 break;
10379 case 'b': case Ctrl_V: /* block-wise selection */
10380 yank_type = MBLOCK;
10381 if (VIM_ISDIGIT(stropt[1]))
10382 {
10383 ++stropt;
10384 block_len = getdigits(&stropt) - 1;
10385 --stropt;
10386 }
10387 break;
10388 }
10389 }
10390
10391 if (argvars[1].v_type == VAR_LIST)
10392 {
10393 char_u **lstval;
10394 char_u **allocval;
10395 char_u buf[NUMBUFLEN];
10396 char_u **curval;
10397 char_u **curallocval;
10398 list_T *ll = argvars[1].vval.v_list;
10399 listitem_T *li;
10400 int len;
10401
10402 /* If the list is NULL handle like an empty list. */
10403 len = ll == NULL ? 0 : ll->lv_len;
10404
10405 /* First half: use for pointers to result lines; second half: use for
10406 * pointers to allocated copies. */
10407 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
10408 if (lstval == NULL)
10409 return;
10410 curval = lstval;
10411 allocval = lstval + len + 2;
10412 curallocval = allocval;
10413
10414 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
10415 li = li->li_next)
10416 {
10417 strval = get_tv_string_buf_chk(&li->li_tv, buf);
10418 if (strval == NULL)
10419 goto free_lstval;
10420 if (strval == buf)
10421 {
10422 /* Need to make a copy, next get_tv_string_buf_chk() will
10423 * overwrite the string. */
10424 strval = vim_strsave(buf);
10425 if (strval == NULL)
10426 goto free_lstval;
10427 *curallocval++ = strval;
10428 }
10429 *curval++ = strval;
10430 }
10431 *curval++ = NULL;
10432
10433 write_reg_contents_lst(regname, lstval, -1,
10434 append, yank_type, block_len);
10435free_lstval:
10436 while (curallocval > allocval)
10437 vim_free(*--curallocval);
10438 vim_free(lstval);
10439 }
10440 else
10441 {
10442 strval = get_tv_string_chk(&argvars[1]);
10443 if (strval == NULL)
10444 return;
10445 write_reg_contents_ex(regname, strval, -1,
10446 append, yank_type, block_len);
10447 }
10448 rettv->vval.v_number = 0;
10449}
10450
10451/*
10452 * "settabvar()" function
10453 */
10454 static void
10455f_settabvar(typval_T *argvars, typval_T *rettv)
10456{
10457#ifdef FEAT_WINDOWS
10458 tabpage_T *save_curtab;
10459 tabpage_T *tp;
10460#endif
10461 char_u *varname, *tabvarname;
10462 typval_T *varp;
10463
10464 rettv->vval.v_number = 0;
10465
10466 if (check_restricted() || check_secure())
10467 return;
10468
10469#ifdef FEAT_WINDOWS
10470 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
10471#endif
10472 varname = get_tv_string_chk(&argvars[1]);
10473 varp = &argvars[2];
10474
10475 if (varname != NULL && varp != NULL
10476#ifdef FEAT_WINDOWS
10477 && tp != NULL
10478#endif
10479 )
10480 {
10481#ifdef FEAT_WINDOWS
10482 save_curtab = curtab;
10483 goto_tabpage_tp(tp, FALSE, FALSE);
10484#endif
10485
10486 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
10487 if (tabvarname != NULL)
10488 {
10489 STRCPY(tabvarname, "t:");
10490 STRCPY(tabvarname + 2, varname);
10491 set_var(tabvarname, varp, TRUE);
10492 vim_free(tabvarname);
10493 }
10494
10495#ifdef FEAT_WINDOWS
10496 /* Restore current tabpage */
10497 if (valid_tabpage(save_curtab))
10498 goto_tabpage_tp(save_curtab, FALSE, FALSE);
10499#endif
10500 }
10501}
10502
10503/*
10504 * "settabwinvar()" function
10505 */
10506 static void
10507f_settabwinvar(typval_T *argvars, typval_T *rettv)
10508{
10509 setwinvar(argvars, rettv, 1);
10510}
10511
10512/*
10513 * "setwinvar()" function
10514 */
10515 static void
10516f_setwinvar(typval_T *argvars, typval_T *rettv)
10517{
10518 setwinvar(argvars, rettv, 0);
10519}
10520
10521#ifdef FEAT_CRYPT
10522/*
10523 * "sha256({string})" function
10524 */
10525 static void
10526f_sha256(typval_T *argvars, typval_T *rettv)
10527{
10528 char_u *p;
10529
10530 p = get_tv_string(&argvars[0]);
10531 rettv->vval.v_string = vim_strsave(
10532 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
10533 rettv->v_type = VAR_STRING;
10534}
10535#endif /* FEAT_CRYPT */
10536
10537/*
10538 * "shellescape({string})" function
10539 */
10540 static void
10541f_shellescape(typval_T *argvars, typval_T *rettv)
10542{
Bram Moolenaar20615522017-06-05 18:46:26 +020010543 int do_special = non_zero_arg(&argvars[1]);
10544
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010545 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar20615522017-06-05 18:46:26 +020010546 get_tv_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010547 rettv->v_type = VAR_STRING;
10548}
10549
10550/*
10551 * shiftwidth() function
10552 */
10553 static void
10554f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
10555{
10556 rettv->vval.v_number = get_sw_value(curbuf);
10557}
10558
10559/*
10560 * "simplify()" function
10561 */
10562 static void
10563f_simplify(typval_T *argvars, typval_T *rettv)
10564{
10565 char_u *p;
10566
10567 p = get_tv_string(&argvars[0]);
10568 rettv->vval.v_string = vim_strsave(p);
10569 simplify_filename(rettv->vval.v_string); /* simplify in place */
10570 rettv->v_type = VAR_STRING;
10571}
10572
10573#ifdef FEAT_FLOAT
10574/*
10575 * "sin()" function
10576 */
10577 static void
10578f_sin(typval_T *argvars, typval_T *rettv)
10579{
10580 float_T f = 0.0;
10581
10582 rettv->v_type = VAR_FLOAT;
10583 if (get_float_arg(argvars, &f) == OK)
10584 rettv->vval.v_float = sin(f);
10585 else
10586 rettv->vval.v_float = 0.0;
10587}
10588
10589/*
10590 * "sinh()" function
10591 */
10592 static void
10593f_sinh(typval_T *argvars, typval_T *rettv)
10594{
10595 float_T f = 0.0;
10596
10597 rettv->v_type = VAR_FLOAT;
10598 if (get_float_arg(argvars, &f) == OK)
10599 rettv->vval.v_float = sinh(f);
10600 else
10601 rettv->vval.v_float = 0.0;
10602}
10603#endif
10604
10605static int
10606#ifdef __BORLANDC__
10607 _RTLENTRYF
10608#endif
10609 item_compare(const void *s1, const void *s2);
10610static int
10611#ifdef __BORLANDC__
10612 _RTLENTRYF
10613#endif
10614 item_compare2(const void *s1, const void *s2);
10615
10616/* struct used in the array that's given to qsort() */
10617typedef struct
10618{
10619 listitem_T *item;
10620 int idx;
10621} sortItem_T;
10622
10623/* struct storing information about current sort */
10624typedef struct
10625{
10626 int item_compare_ic;
10627 int item_compare_numeric;
10628 int item_compare_numbers;
10629#ifdef FEAT_FLOAT
10630 int item_compare_float;
10631#endif
10632 char_u *item_compare_func;
10633 partial_T *item_compare_partial;
10634 dict_T *item_compare_selfdict;
10635 int item_compare_func_err;
10636 int item_compare_keep_zero;
10637} sortinfo_T;
10638static sortinfo_T *sortinfo = NULL;
10639static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
10640#define ITEM_COMPARE_FAIL 999
10641
10642/*
10643 * Compare functions for f_sort() and f_uniq() below.
10644 */
10645 static int
10646#ifdef __BORLANDC__
10647_RTLENTRYF
10648#endif
10649item_compare(const void *s1, const void *s2)
10650{
10651 sortItem_T *si1, *si2;
10652 typval_T *tv1, *tv2;
10653 char_u *p1, *p2;
10654 char_u *tofree1 = NULL, *tofree2 = NULL;
10655 int res;
10656 char_u numbuf1[NUMBUFLEN];
10657 char_u numbuf2[NUMBUFLEN];
10658
10659 si1 = (sortItem_T *)s1;
10660 si2 = (sortItem_T *)s2;
10661 tv1 = &si1->item->li_tv;
10662 tv2 = &si2->item->li_tv;
10663
10664 if (sortinfo->item_compare_numbers)
10665 {
10666 varnumber_T v1 = get_tv_number(tv1);
10667 varnumber_T v2 = get_tv_number(tv2);
10668
10669 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10670 }
10671
10672#ifdef FEAT_FLOAT
10673 if (sortinfo->item_compare_float)
10674 {
10675 float_T v1 = get_tv_float(tv1);
10676 float_T v2 = get_tv_float(tv2);
10677
10678 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10679 }
10680#endif
10681
10682 /* tv2string() puts quotes around a string and allocates memory. Don't do
10683 * that for string variables. Use a single quote when comparing with a
10684 * non-string to do what the docs promise. */
10685 if (tv1->v_type == VAR_STRING)
10686 {
10687 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
10688 p1 = (char_u *)"'";
10689 else
10690 p1 = tv1->vval.v_string;
10691 }
10692 else
10693 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
10694 if (tv2->v_type == VAR_STRING)
10695 {
10696 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
10697 p2 = (char_u *)"'";
10698 else
10699 p2 = tv2->vval.v_string;
10700 }
10701 else
10702 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
10703 if (p1 == NULL)
10704 p1 = (char_u *)"";
10705 if (p2 == NULL)
10706 p2 = (char_u *)"";
10707 if (!sortinfo->item_compare_numeric)
10708 {
10709 if (sortinfo->item_compare_ic)
10710 res = STRICMP(p1, p2);
10711 else
10712 res = STRCMP(p1, p2);
10713 }
10714 else
10715 {
10716 double n1, n2;
10717 n1 = strtod((char *)p1, (char **)&p1);
10718 n2 = strtod((char *)p2, (char **)&p2);
10719 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
10720 }
10721
10722 /* When the result would be zero, compare the item indexes. Makes the
10723 * sort stable. */
10724 if (res == 0 && !sortinfo->item_compare_keep_zero)
10725 res = si1->idx > si2->idx ? 1 : -1;
10726
10727 vim_free(tofree1);
10728 vim_free(tofree2);
10729 return res;
10730}
10731
10732 static int
10733#ifdef __BORLANDC__
10734_RTLENTRYF
10735#endif
10736item_compare2(const void *s1, const void *s2)
10737{
10738 sortItem_T *si1, *si2;
10739 int res;
10740 typval_T rettv;
10741 typval_T argv[3];
10742 int dummy;
10743 char_u *func_name;
10744 partial_T *partial = sortinfo->item_compare_partial;
10745
10746 /* shortcut after failure in previous call; compare all items equal */
10747 if (sortinfo->item_compare_func_err)
10748 return 0;
10749
10750 si1 = (sortItem_T *)s1;
10751 si2 = (sortItem_T *)s2;
10752
10753 if (partial == NULL)
10754 func_name = sortinfo->item_compare_func;
10755 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020010756 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010757
10758 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
10759 * in the copy without changing the original list items. */
10760 copy_tv(&si1->item->li_tv, &argv[0]);
10761 copy_tv(&si2->item->li_tv, &argv[1]);
10762
10763 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
10764 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020010765 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010766 partial, sortinfo->item_compare_selfdict);
10767 clear_tv(&argv[0]);
10768 clear_tv(&argv[1]);
10769
10770 if (res == FAIL)
10771 res = ITEM_COMPARE_FAIL;
10772 else
10773 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
10774 if (sortinfo->item_compare_func_err)
10775 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
10776 clear_tv(&rettv);
10777
10778 /* When the result would be zero, compare the pointers themselves. Makes
10779 * the sort stable. */
10780 if (res == 0 && !sortinfo->item_compare_keep_zero)
10781 res = si1->idx > si2->idx ? 1 : -1;
10782
10783 return res;
10784}
10785
10786/*
10787 * "sort({list})" function
10788 */
10789 static void
10790do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
10791{
10792 list_T *l;
10793 listitem_T *li;
10794 sortItem_T *ptrs;
10795 sortinfo_T *old_sortinfo;
10796 sortinfo_T info;
10797 long len;
10798 long i;
10799
10800 /* Pointer to current info struct used in compare function. Save and
10801 * restore the current one for nested calls. */
10802 old_sortinfo = sortinfo;
10803 sortinfo = &info;
10804
10805 if (argvars[0].v_type != VAR_LIST)
10806 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
10807 else
10808 {
10809 l = argvars[0].vval.v_list;
10810 if (l == NULL || tv_check_lock(l->lv_lock,
10811 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
10812 TRUE))
10813 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020010814 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010815
10816 len = list_len(l);
10817 if (len <= 1)
10818 goto theend; /* short list sorts pretty quickly */
10819
10820 info.item_compare_ic = FALSE;
10821 info.item_compare_numeric = FALSE;
10822 info.item_compare_numbers = FALSE;
10823#ifdef FEAT_FLOAT
10824 info.item_compare_float = FALSE;
10825#endif
10826 info.item_compare_func = NULL;
10827 info.item_compare_partial = NULL;
10828 info.item_compare_selfdict = NULL;
10829 if (argvars[1].v_type != VAR_UNKNOWN)
10830 {
10831 /* optional second argument: {func} */
10832 if (argvars[1].v_type == VAR_FUNC)
10833 info.item_compare_func = argvars[1].vval.v_string;
10834 else if (argvars[1].v_type == VAR_PARTIAL)
10835 info.item_compare_partial = argvars[1].vval.v_partial;
10836 else
10837 {
10838 int error = FALSE;
10839
10840 i = (long)get_tv_number_chk(&argvars[1], &error);
10841 if (error)
10842 goto theend; /* type error; errmsg already given */
10843 if (i == 1)
10844 info.item_compare_ic = TRUE;
10845 else if (argvars[1].v_type != VAR_NUMBER)
10846 info.item_compare_func = get_tv_string(&argvars[1]);
10847 else if (i != 0)
10848 {
10849 EMSG(_(e_invarg));
10850 goto theend;
10851 }
10852 if (info.item_compare_func != NULL)
10853 {
10854 if (*info.item_compare_func == NUL)
10855 {
10856 /* empty string means default sort */
10857 info.item_compare_func = NULL;
10858 }
10859 else if (STRCMP(info.item_compare_func, "n") == 0)
10860 {
10861 info.item_compare_func = NULL;
10862 info.item_compare_numeric = TRUE;
10863 }
10864 else if (STRCMP(info.item_compare_func, "N") == 0)
10865 {
10866 info.item_compare_func = NULL;
10867 info.item_compare_numbers = TRUE;
10868 }
10869#ifdef FEAT_FLOAT
10870 else if (STRCMP(info.item_compare_func, "f") == 0)
10871 {
10872 info.item_compare_func = NULL;
10873 info.item_compare_float = TRUE;
10874 }
10875#endif
10876 else if (STRCMP(info.item_compare_func, "i") == 0)
10877 {
10878 info.item_compare_func = NULL;
10879 info.item_compare_ic = TRUE;
10880 }
10881 }
10882 }
10883
10884 if (argvars[2].v_type != VAR_UNKNOWN)
10885 {
10886 /* optional third argument: {dict} */
10887 if (argvars[2].v_type != VAR_DICT)
10888 {
10889 EMSG(_(e_dictreq));
10890 goto theend;
10891 }
10892 info.item_compare_selfdict = argvars[2].vval.v_dict;
10893 }
10894 }
10895
10896 /* Make an array with each entry pointing to an item in the List. */
10897 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
10898 if (ptrs == NULL)
10899 goto theend;
10900
10901 i = 0;
10902 if (sort)
10903 {
10904 /* sort(): ptrs will be the list to sort */
10905 for (li = l->lv_first; li != NULL; li = li->li_next)
10906 {
10907 ptrs[i].item = li;
10908 ptrs[i].idx = i;
10909 ++i;
10910 }
10911
10912 info.item_compare_func_err = FALSE;
10913 info.item_compare_keep_zero = FALSE;
10914 /* test the compare function */
10915 if ((info.item_compare_func != NULL
10916 || info.item_compare_partial != NULL)
10917 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
10918 == ITEM_COMPARE_FAIL)
10919 EMSG(_("E702: Sort compare function failed"));
10920 else
10921 {
10922 /* Sort the array with item pointers. */
10923 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
10924 info.item_compare_func == NULL
10925 && info.item_compare_partial == NULL
10926 ? item_compare : item_compare2);
10927
10928 if (!info.item_compare_func_err)
10929 {
10930 /* Clear the List and append the items in sorted order. */
10931 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
10932 l->lv_len = 0;
10933 for (i = 0; i < len; ++i)
10934 list_append(l, ptrs[i].item);
10935 }
10936 }
10937 }
10938 else
10939 {
10940 int (*item_compare_func_ptr)(const void *, const void *);
10941
10942 /* f_uniq(): ptrs will be a stack of items to remove */
10943 info.item_compare_func_err = FALSE;
10944 info.item_compare_keep_zero = TRUE;
10945 item_compare_func_ptr = info.item_compare_func != NULL
10946 || info.item_compare_partial != NULL
10947 ? item_compare2 : item_compare;
10948
10949 for (li = l->lv_first; li != NULL && li->li_next != NULL;
10950 li = li->li_next)
10951 {
10952 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
10953 == 0)
10954 ptrs[i++].item = li;
10955 if (info.item_compare_func_err)
10956 {
10957 EMSG(_("E882: Uniq compare function failed"));
10958 break;
10959 }
10960 }
10961
10962 if (!info.item_compare_func_err)
10963 {
10964 while (--i >= 0)
10965 {
10966 li = ptrs[i].item->li_next;
10967 ptrs[i].item->li_next = li->li_next;
10968 if (li->li_next != NULL)
10969 li->li_next->li_prev = ptrs[i].item;
10970 else
10971 l->lv_last = ptrs[i].item;
10972 list_fix_watch(l, li);
10973 listitem_free(li);
10974 l->lv_len--;
10975 }
10976 }
10977 }
10978
10979 vim_free(ptrs);
10980 }
10981theend:
10982 sortinfo = old_sortinfo;
10983}
10984
10985/*
10986 * "sort({list})" function
10987 */
10988 static void
10989f_sort(typval_T *argvars, typval_T *rettv)
10990{
10991 do_sort_uniq(argvars, rettv, TRUE);
10992}
10993
10994/*
10995 * "uniq({list})" function
10996 */
10997 static void
10998f_uniq(typval_T *argvars, typval_T *rettv)
10999{
11000 do_sort_uniq(argvars, rettv, FALSE);
11001}
11002
11003/*
11004 * "soundfold({word})" function
11005 */
11006 static void
11007f_soundfold(typval_T *argvars, typval_T *rettv)
11008{
11009 char_u *s;
11010
11011 rettv->v_type = VAR_STRING;
11012 s = get_tv_string(&argvars[0]);
11013#ifdef FEAT_SPELL
11014 rettv->vval.v_string = eval_soundfold(s);
11015#else
11016 rettv->vval.v_string = vim_strsave(s);
11017#endif
11018}
11019
11020/*
11021 * "spellbadword()" function
11022 */
11023 static void
11024f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
11025{
11026 char_u *word = (char_u *)"";
11027 hlf_T attr = HLF_COUNT;
11028 int len = 0;
11029
11030 if (rettv_list_alloc(rettv) == FAIL)
11031 return;
11032
11033#ifdef FEAT_SPELL
11034 if (argvars[0].v_type == VAR_UNKNOWN)
11035 {
11036 /* Find the start and length of the badly spelled word. */
11037 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
11038 if (len != 0)
11039 word = ml_get_cursor();
11040 }
11041 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
11042 {
11043 char_u *str = get_tv_string_chk(&argvars[0]);
11044 int capcol = -1;
11045
11046 if (str != NULL)
11047 {
11048 /* Check the argument for spelling. */
11049 while (*str != NUL)
11050 {
11051 len = spell_check(curwin, str, &attr, &capcol, FALSE);
11052 if (attr != HLF_COUNT)
11053 {
11054 word = str;
11055 break;
11056 }
11057 str += len;
11058 }
11059 }
11060 }
11061#endif
11062
11063 list_append_string(rettv->vval.v_list, word, len);
11064 list_append_string(rettv->vval.v_list, (char_u *)(
11065 attr == HLF_SPB ? "bad" :
11066 attr == HLF_SPR ? "rare" :
11067 attr == HLF_SPL ? "local" :
11068 attr == HLF_SPC ? "caps" :
11069 ""), -1);
11070}
11071
11072/*
11073 * "spellsuggest()" function
11074 */
11075 static void
11076f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
11077{
11078#ifdef FEAT_SPELL
11079 char_u *str;
11080 int typeerr = FALSE;
11081 int maxcount;
11082 garray_T ga;
11083 int i;
11084 listitem_T *li;
11085 int need_capital = FALSE;
11086#endif
11087
11088 if (rettv_list_alloc(rettv) == FAIL)
11089 return;
11090
11091#ifdef FEAT_SPELL
11092 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
11093 {
11094 str = get_tv_string(&argvars[0]);
11095 if (argvars[1].v_type != VAR_UNKNOWN)
11096 {
11097 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
11098 if (maxcount <= 0)
11099 return;
11100 if (argvars[2].v_type != VAR_UNKNOWN)
11101 {
11102 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
11103 if (typeerr)
11104 return;
11105 }
11106 }
11107 else
11108 maxcount = 25;
11109
11110 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
11111
11112 for (i = 0; i < ga.ga_len; ++i)
11113 {
11114 str = ((char_u **)ga.ga_data)[i];
11115
11116 li = listitem_alloc();
11117 if (li == NULL)
11118 vim_free(str);
11119 else
11120 {
11121 li->li_tv.v_type = VAR_STRING;
11122 li->li_tv.v_lock = 0;
11123 li->li_tv.vval.v_string = str;
11124 list_append(rettv->vval.v_list, li);
11125 }
11126 }
11127 ga_clear(&ga);
11128 }
11129#endif
11130}
11131
11132 static void
11133f_split(typval_T *argvars, typval_T *rettv)
11134{
11135 char_u *str;
11136 char_u *end;
11137 char_u *pat = NULL;
11138 regmatch_T regmatch;
11139 char_u patbuf[NUMBUFLEN];
11140 char_u *save_cpo;
11141 int match;
11142 colnr_T col = 0;
11143 int keepempty = FALSE;
11144 int typeerr = FALSE;
11145
11146 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11147 save_cpo = p_cpo;
11148 p_cpo = (char_u *)"";
11149
11150 str = get_tv_string(&argvars[0]);
11151 if (argvars[1].v_type != VAR_UNKNOWN)
11152 {
11153 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11154 if (pat == NULL)
11155 typeerr = TRUE;
11156 if (argvars[2].v_type != VAR_UNKNOWN)
11157 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
11158 }
11159 if (pat == NULL || *pat == NUL)
11160 pat = (char_u *)"[\\x01- ]\\+";
11161
11162 if (rettv_list_alloc(rettv) == FAIL)
11163 return;
11164 if (typeerr)
11165 return;
11166
11167 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11168 if (regmatch.regprog != NULL)
11169 {
11170 regmatch.rm_ic = FALSE;
11171 while (*str != NUL || keepempty)
11172 {
11173 if (*str == NUL)
11174 match = FALSE; /* empty item at the end */
11175 else
11176 match = vim_regexec_nl(&regmatch, str, col);
11177 if (match)
11178 end = regmatch.startp[0];
11179 else
11180 end = str + STRLEN(str);
11181 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
11182 && *str != NUL && match && end < regmatch.endp[0]))
11183 {
11184 if (list_append_string(rettv->vval.v_list, str,
11185 (int)(end - str)) == FAIL)
11186 break;
11187 }
11188 if (!match)
11189 break;
11190 /* Advance to just after the match. */
11191 if (regmatch.endp[0] > str)
11192 col = 0;
11193 else
11194 {
11195 /* Don't get stuck at the same match. */
11196#ifdef FEAT_MBYTE
11197 col = (*mb_ptr2len)(regmatch.endp[0]);
11198#else
11199 col = 1;
11200#endif
11201 }
11202 str = regmatch.endp[0];
11203 }
11204
11205 vim_regfree(regmatch.regprog);
11206 }
11207
11208 p_cpo = save_cpo;
11209}
11210
11211#ifdef FEAT_FLOAT
11212/*
11213 * "sqrt()" function
11214 */
11215 static void
11216f_sqrt(typval_T *argvars, typval_T *rettv)
11217{
11218 float_T f = 0.0;
11219
11220 rettv->v_type = VAR_FLOAT;
11221 if (get_float_arg(argvars, &f) == OK)
11222 rettv->vval.v_float = sqrt(f);
11223 else
11224 rettv->vval.v_float = 0.0;
11225}
11226
11227/*
11228 * "str2float()" function
11229 */
11230 static void
11231f_str2float(typval_T *argvars, typval_T *rettv)
11232{
11233 char_u *p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011234 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011235
Bram Moolenaar08243d22017-01-10 16:12:29 +010011236 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011237 p = skipwhite(p + 1);
11238 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011239 if (isneg)
11240 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011241 rettv->v_type = VAR_FLOAT;
11242}
11243#endif
11244
11245/*
11246 * "str2nr()" function
11247 */
11248 static void
11249f_str2nr(typval_T *argvars, typval_T *rettv)
11250{
11251 int base = 10;
11252 char_u *p;
11253 varnumber_T n;
11254 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010011255 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011256
11257 if (argvars[1].v_type != VAR_UNKNOWN)
11258 {
11259 base = (int)get_tv_number(&argvars[1]);
11260 if (base != 2 && base != 8 && base != 10 && base != 16)
11261 {
11262 EMSG(_(e_invarg));
11263 return;
11264 }
11265 }
11266
11267 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011268 isneg = (*p == '-');
11269 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011270 p = skipwhite(p + 1);
11271 switch (base)
11272 {
11273 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
11274 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
11275 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
11276 default: what = 0;
11277 }
11278 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011279 if (isneg)
11280 rettv->vval.v_number = -n;
11281 else
11282 rettv->vval.v_number = n;
11283
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011284}
11285
11286#ifdef HAVE_STRFTIME
11287/*
11288 * "strftime({format}[, {time}])" function
11289 */
11290 static void
11291f_strftime(typval_T *argvars, typval_T *rettv)
11292{
11293 char_u result_buf[256];
11294 struct tm *curtime;
11295 time_t seconds;
11296 char_u *p;
11297
11298 rettv->v_type = VAR_STRING;
11299
11300 p = get_tv_string(&argvars[0]);
11301 if (argvars[1].v_type == VAR_UNKNOWN)
11302 seconds = time(NULL);
11303 else
11304 seconds = (time_t)get_tv_number(&argvars[1]);
11305 curtime = localtime(&seconds);
11306 /* MSVC returns NULL for an invalid value of seconds. */
11307 if (curtime == NULL)
11308 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
11309 else
11310 {
11311# ifdef FEAT_MBYTE
11312 vimconv_T conv;
11313 char_u *enc;
11314
11315 conv.vc_type = CONV_NONE;
11316 enc = enc_locale();
11317 convert_setup(&conv, p_enc, enc);
11318 if (conv.vc_type != CONV_NONE)
11319 p = string_convert(&conv, p, NULL);
11320# endif
11321 if (p != NULL)
11322 (void)strftime((char *)result_buf, sizeof(result_buf),
11323 (char *)p, curtime);
11324 else
11325 result_buf[0] = NUL;
11326
11327# ifdef FEAT_MBYTE
11328 if (conv.vc_type != CONV_NONE)
11329 vim_free(p);
11330 convert_setup(&conv, enc, p_enc);
11331 if (conv.vc_type != CONV_NONE)
11332 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
11333 else
11334# endif
11335 rettv->vval.v_string = vim_strsave(result_buf);
11336
11337# ifdef FEAT_MBYTE
11338 /* Release conversion descriptors */
11339 convert_setup(&conv, NULL, NULL);
11340 vim_free(enc);
11341# endif
11342 }
11343}
11344#endif
11345
11346/*
11347 * "strgetchar()" function
11348 */
11349 static void
11350f_strgetchar(typval_T *argvars, typval_T *rettv)
11351{
11352 char_u *str;
11353 int len;
11354 int error = FALSE;
11355 int charidx;
11356
11357 rettv->vval.v_number = -1;
11358 str = get_tv_string_chk(&argvars[0]);
11359 if (str == NULL)
11360 return;
11361 len = (int)STRLEN(str);
11362 charidx = (int)get_tv_number_chk(&argvars[1], &error);
11363 if (error)
11364 return;
11365#ifdef FEAT_MBYTE
11366 {
11367 int byteidx = 0;
11368
11369 while (charidx >= 0 && byteidx < len)
11370 {
11371 if (charidx == 0)
11372 {
11373 rettv->vval.v_number = mb_ptr2char(str + byteidx);
11374 break;
11375 }
11376 --charidx;
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011377 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011378 }
11379 }
11380#else
11381 if (charidx < len)
11382 rettv->vval.v_number = str[charidx];
11383#endif
11384}
11385
11386/*
11387 * "stridx()" function
11388 */
11389 static void
11390f_stridx(typval_T *argvars, typval_T *rettv)
11391{
11392 char_u buf[NUMBUFLEN];
11393 char_u *needle;
11394 char_u *haystack;
11395 char_u *save_haystack;
11396 char_u *pos;
11397 int start_idx;
11398
11399 needle = get_tv_string_chk(&argvars[1]);
11400 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
11401 rettv->vval.v_number = -1;
11402 if (needle == NULL || haystack == NULL)
11403 return; /* type error; errmsg already given */
11404
11405 if (argvars[2].v_type != VAR_UNKNOWN)
11406 {
11407 int error = FALSE;
11408
11409 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
11410 if (error || start_idx >= (int)STRLEN(haystack))
11411 return;
11412 if (start_idx >= 0)
11413 haystack += start_idx;
11414 }
11415
11416 pos = (char_u *)strstr((char *)haystack, (char *)needle);
11417 if (pos != NULL)
11418 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
11419}
11420
11421/*
11422 * "string()" function
11423 */
11424 static void
11425f_string(typval_T *argvars, typval_T *rettv)
11426{
11427 char_u *tofree;
11428 char_u numbuf[NUMBUFLEN];
11429
11430 rettv->v_type = VAR_STRING;
11431 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
11432 get_copyID());
11433 /* Make a copy if we have a value but it's not in allocated memory. */
11434 if (rettv->vval.v_string != NULL && tofree == NULL)
11435 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
11436}
11437
11438/*
11439 * "strlen()" function
11440 */
11441 static void
11442f_strlen(typval_T *argvars, typval_T *rettv)
11443{
11444 rettv->vval.v_number = (varnumber_T)(STRLEN(
11445 get_tv_string(&argvars[0])));
11446}
11447
11448/*
11449 * "strchars()" function
11450 */
11451 static void
11452f_strchars(typval_T *argvars, typval_T *rettv)
11453{
11454 char_u *s = get_tv_string(&argvars[0]);
11455 int skipcc = 0;
11456#ifdef FEAT_MBYTE
11457 varnumber_T len = 0;
11458 int (*func_mb_ptr2char_adv)(char_u **pp);
11459#endif
11460
11461 if (argvars[1].v_type != VAR_UNKNOWN)
11462 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
11463 if (skipcc < 0 || skipcc > 1)
11464 EMSG(_(e_invarg));
11465 else
11466 {
11467#ifdef FEAT_MBYTE
11468 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
11469 while (*s != NUL)
11470 {
11471 func_mb_ptr2char_adv(&s);
11472 ++len;
11473 }
11474 rettv->vval.v_number = len;
11475#else
11476 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
11477#endif
11478 }
11479}
11480
11481/*
11482 * "strdisplaywidth()" function
11483 */
11484 static void
11485f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
11486{
11487 char_u *s = get_tv_string(&argvars[0]);
11488 int col = 0;
11489
11490 if (argvars[1].v_type != VAR_UNKNOWN)
11491 col = (int)get_tv_number(&argvars[1]);
11492
11493 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
11494}
11495
11496/*
11497 * "strwidth()" function
11498 */
11499 static void
11500f_strwidth(typval_T *argvars, typval_T *rettv)
11501{
11502 char_u *s = get_tv_string(&argvars[0]);
11503
11504 rettv->vval.v_number = (varnumber_T)(
11505#ifdef FEAT_MBYTE
11506 mb_string2cells(s, -1)
11507#else
11508 STRLEN(s)
11509#endif
11510 );
11511}
11512
11513/*
11514 * "strcharpart()" function
11515 */
11516 static void
11517f_strcharpart(typval_T *argvars, typval_T *rettv)
11518{
11519#ifdef FEAT_MBYTE
11520 char_u *p;
11521 int nchar;
11522 int nbyte = 0;
11523 int charlen;
11524 int len = 0;
11525 int slen;
11526 int error = FALSE;
11527
11528 p = get_tv_string(&argvars[0]);
11529 slen = (int)STRLEN(p);
11530
11531 nchar = (int)get_tv_number_chk(&argvars[1], &error);
11532 if (!error)
11533 {
11534 if (nchar > 0)
11535 while (nchar > 0 && nbyte < slen)
11536 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011537 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011538 --nchar;
11539 }
11540 else
11541 nbyte = nchar;
11542 if (argvars[2].v_type != VAR_UNKNOWN)
11543 {
11544 charlen = (int)get_tv_number(&argvars[2]);
11545 while (charlen > 0 && nbyte + len < slen)
11546 {
11547 int off = nbyte + len;
11548
11549 if (off < 0)
11550 len += 1;
11551 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011552 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011553 --charlen;
11554 }
11555 }
11556 else
11557 len = slen - nbyte; /* default: all bytes that are available. */
11558 }
11559
11560 /*
11561 * Only return the overlap between the specified part and the actual
11562 * string.
11563 */
11564 if (nbyte < 0)
11565 {
11566 len += nbyte;
11567 nbyte = 0;
11568 }
11569 else if (nbyte > slen)
11570 nbyte = slen;
11571 if (len < 0)
11572 len = 0;
11573 else if (nbyte + len > slen)
11574 len = slen - nbyte;
11575
11576 rettv->v_type = VAR_STRING;
11577 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
11578#else
11579 f_strpart(argvars, rettv);
11580#endif
11581}
11582
11583/*
11584 * "strpart()" function
11585 */
11586 static void
11587f_strpart(typval_T *argvars, typval_T *rettv)
11588{
11589 char_u *p;
11590 int n;
11591 int len;
11592 int slen;
11593 int error = FALSE;
11594
11595 p = get_tv_string(&argvars[0]);
11596 slen = (int)STRLEN(p);
11597
11598 n = (int)get_tv_number_chk(&argvars[1], &error);
11599 if (error)
11600 len = 0;
11601 else if (argvars[2].v_type != VAR_UNKNOWN)
11602 len = (int)get_tv_number(&argvars[2]);
11603 else
11604 len = slen - n; /* default len: all bytes that are available. */
11605
11606 /*
11607 * Only return the overlap between the specified part and the actual
11608 * string.
11609 */
11610 if (n < 0)
11611 {
11612 len += n;
11613 n = 0;
11614 }
11615 else if (n > slen)
11616 n = slen;
11617 if (len < 0)
11618 len = 0;
11619 else if (n + len > slen)
11620 len = slen - n;
11621
11622 rettv->v_type = VAR_STRING;
11623 rettv->vval.v_string = vim_strnsave(p + n, len);
11624}
11625
11626/*
11627 * "strridx()" function
11628 */
11629 static void
11630f_strridx(typval_T *argvars, typval_T *rettv)
11631{
11632 char_u buf[NUMBUFLEN];
11633 char_u *needle;
11634 char_u *haystack;
11635 char_u *rest;
11636 char_u *lastmatch = NULL;
11637 int haystack_len, end_idx;
11638
11639 needle = get_tv_string_chk(&argvars[1]);
11640 haystack = get_tv_string_buf_chk(&argvars[0], buf);
11641
11642 rettv->vval.v_number = -1;
11643 if (needle == NULL || haystack == NULL)
11644 return; /* type error; errmsg already given */
11645
11646 haystack_len = (int)STRLEN(haystack);
11647 if (argvars[2].v_type != VAR_UNKNOWN)
11648 {
11649 /* Third argument: upper limit for index */
11650 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
11651 if (end_idx < 0)
11652 return; /* can never find a match */
11653 }
11654 else
11655 end_idx = haystack_len;
11656
11657 if (*needle == NUL)
11658 {
11659 /* Empty string matches past the end. */
11660 lastmatch = haystack + end_idx;
11661 }
11662 else
11663 {
11664 for (rest = haystack; *rest != '\0'; ++rest)
11665 {
11666 rest = (char_u *)strstr((char *)rest, (char *)needle);
11667 if (rest == NULL || rest > haystack + end_idx)
11668 break;
11669 lastmatch = rest;
11670 }
11671 }
11672
11673 if (lastmatch == NULL)
11674 rettv->vval.v_number = -1;
11675 else
11676 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
11677}
11678
11679/*
11680 * "strtrans()" function
11681 */
11682 static void
11683f_strtrans(typval_T *argvars, typval_T *rettv)
11684{
11685 rettv->v_type = VAR_STRING;
11686 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
11687}
11688
11689/*
11690 * "submatch()" function
11691 */
11692 static void
11693f_submatch(typval_T *argvars, typval_T *rettv)
11694{
11695 int error = FALSE;
11696 int no;
11697 int retList = 0;
11698
11699 no = (int)get_tv_number_chk(&argvars[0], &error);
11700 if (error)
11701 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020011702 if (no < 0 || no >= NSUBEXP)
11703 {
Bram Moolenaar79518e22017-02-17 16:31:35 +010011704 EMSGN(_("E935: invalid submatch number: %d"), no);
11705 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020011706 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011707 if (argvars[1].v_type != VAR_UNKNOWN)
11708 retList = (int)get_tv_number_chk(&argvars[1], &error);
11709 if (error)
11710 return;
11711
11712 if (retList == 0)
11713 {
11714 rettv->v_type = VAR_STRING;
11715 rettv->vval.v_string = reg_submatch(no);
11716 }
11717 else
11718 {
11719 rettv->v_type = VAR_LIST;
11720 rettv->vval.v_list = reg_submatch_list(no);
11721 }
11722}
11723
11724/*
11725 * "substitute()" function
11726 */
11727 static void
11728f_substitute(typval_T *argvars, typval_T *rettv)
11729{
11730 char_u patbuf[NUMBUFLEN];
11731 char_u subbuf[NUMBUFLEN];
11732 char_u flagsbuf[NUMBUFLEN];
11733
11734 char_u *str = get_tv_string_chk(&argvars[0]);
11735 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011736 char_u *sub = NULL;
11737 typval_T *expr = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011738 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
11739
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011740 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
11741 expr = &argvars[2];
11742 else
11743 sub = get_tv_string_buf_chk(&argvars[2], subbuf);
11744
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011745 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011746 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
11747 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011748 rettv->vval.v_string = NULL;
11749 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011750 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011751}
11752
11753/*
11754 * "synID(lnum, col, trans)" function
11755 */
11756 static void
11757f_synID(typval_T *argvars UNUSED, typval_T *rettv)
11758{
11759 int id = 0;
11760#ifdef FEAT_SYN_HL
11761 linenr_T lnum;
11762 colnr_T col;
11763 int trans;
11764 int transerr = FALSE;
11765
11766 lnum = get_tv_lnum(argvars); /* -1 on type error */
11767 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
11768 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
11769
11770 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11771 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
11772 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
11773#endif
11774
11775 rettv->vval.v_number = id;
11776}
11777
11778/*
11779 * "synIDattr(id, what [, mode])" function
11780 */
11781 static void
11782f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
11783{
11784 char_u *p = NULL;
11785#ifdef FEAT_SYN_HL
11786 int id;
11787 char_u *what;
11788 char_u *mode;
11789 char_u modebuf[NUMBUFLEN];
11790 int modec;
11791
11792 id = (int)get_tv_number(&argvars[0]);
11793 what = get_tv_string(&argvars[1]);
11794 if (argvars[2].v_type != VAR_UNKNOWN)
11795 {
11796 mode = get_tv_string_buf(&argvars[2], modebuf);
11797 modec = TOLOWER_ASC(mode[0]);
11798 if (modec != 't' && modec != 'c' && modec != 'g')
11799 modec = 0; /* replace invalid with current */
11800 }
11801 else
11802 {
11803#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
11804 if (USE_24BIT)
11805 modec = 'g';
11806 else
11807#endif
11808 if (t_colors > 1)
11809 modec = 'c';
11810 else
11811 modec = 't';
11812 }
11813
11814
11815 switch (TOLOWER_ASC(what[0]))
11816 {
11817 case 'b':
11818 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
11819 p = highlight_color(id, what, modec);
11820 else /* bold */
11821 p = highlight_has_attr(id, HL_BOLD, modec);
11822 break;
11823
11824 case 'f': /* fg[#] or font */
11825 p = highlight_color(id, what, modec);
11826 break;
11827
11828 case 'i':
11829 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
11830 p = highlight_has_attr(id, HL_INVERSE, modec);
11831 else /* italic */
11832 p = highlight_has_attr(id, HL_ITALIC, modec);
11833 break;
11834
11835 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020011836 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011837 break;
11838
11839 case 'r': /* reverse */
11840 p = highlight_has_attr(id, HL_INVERSE, modec);
11841 break;
11842
11843 case 's':
11844 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
11845 p = highlight_color(id, what, modec);
11846 else /* standout */
11847 p = highlight_has_attr(id, HL_STANDOUT, modec);
11848 break;
11849
11850 case 'u':
11851 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
11852 /* underline */
11853 p = highlight_has_attr(id, HL_UNDERLINE, modec);
11854 else
11855 /* undercurl */
11856 p = highlight_has_attr(id, HL_UNDERCURL, modec);
11857 break;
11858 }
11859
11860 if (p != NULL)
11861 p = vim_strsave(p);
11862#endif
11863 rettv->v_type = VAR_STRING;
11864 rettv->vval.v_string = p;
11865}
11866
11867/*
11868 * "synIDtrans(id)" function
11869 */
11870 static void
11871f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
11872{
11873 int id;
11874
11875#ifdef FEAT_SYN_HL
11876 id = (int)get_tv_number(&argvars[0]);
11877
11878 if (id > 0)
11879 id = syn_get_final_id(id);
11880 else
11881#endif
11882 id = 0;
11883
11884 rettv->vval.v_number = id;
11885}
11886
11887/*
11888 * "synconcealed(lnum, col)" function
11889 */
11890 static void
11891f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
11892{
11893#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
11894 linenr_T lnum;
11895 colnr_T col;
11896 int syntax_flags = 0;
11897 int cchar;
11898 int matchid = 0;
11899 char_u str[NUMBUFLEN];
11900#endif
11901
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011902 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011903
11904#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
11905 lnum = get_tv_lnum(argvars); /* -1 on type error */
11906 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
11907
11908 vim_memset(str, NUL, sizeof(str));
11909
11910 if (rettv_list_alloc(rettv) != FAIL)
11911 {
11912 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11913 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
11914 && curwin->w_p_cole > 0)
11915 {
11916 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
11917 syntax_flags = get_syntax_info(&matchid);
11918
11919 /* get the conceal character */
11920 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
11921 {
11922 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020011923 if (cchar == NUL && curwin->w_p_cole == 1)
11924 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011925 if (cchar != NUL)
11926 {
11927# ifdef FEAT_MBYTE
11928 if (has_mbyte)
11929 (*mb_char2bytes)(cchar, str);
11930 else
11931# endif
11932 str[0] = cchar;
11933 }
11934 }
11935 }
11936
11937 list_append_number(rettv->vval.v_list,
11938 (syntax_flags & HL_CONCEAL) != 0);
11939 /* -1 to auto-determine strlen */
11940 list_append_string(rettv->vval.v_list, str, -1);
11941 list_append_number(rettv->vval.v_list, matchid);
11942 }
11943#endif
11944}
11945
11946/*
11947 * "synstack(lnum, col)" function
11948 */
11949 static void
11950f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
11951{
11952#ifdef FEAT_SYN_HL
11953 linenr_T lnum;
11954 colnr_T col;
11955 int i;
11956 int id;
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#ifdef FEAT_SYN_HL
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 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11966 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
11967 && rettv_list_alloc(rettv) != FAIL)
11968 {
11969 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
11970 for (i = 0; ; ++i)
11971 {
11972 id = syn_get_stack_item(i);
11973 if (id < 0)
11974 break;
11975 if (list_append_number(rettv->vval.v_list, id) == FAIL)
11976 break;
11977 }
11978 }
11979#endif
11980}
11981
11982 static void
11983get_cmd_output_as_rettv(
11984 typval_T *argvars,
11985 typval_T *rettv,
11986 int retlist)
11987{
11988 char_u *res = NULL;
11989 char_u *p;
11990 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011991 int err = FALSE;
11992 FILE *fd;
11993 list_T *list = NULL;
11994 int flags = SHELL_SILENT;
11995
11996 rettv->v_type = VAR_STRING;
11997 rettv->vval.v_string = NULL;
11998 if (check_restricted() || check_secure())
11999 goto errret;
12000
12001 if (argvars[1].v_type != VAR_UNKNOWN)
12002 {
12003 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010012004 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012005 * command.
12006 */
12007 if ((infile = vim_tempname('i', TRUE)) == NULL)
12008 {
12009 EMSG(_(e_notmp));
12010 goto errret;
12011 }
12012
12013 fd = mch_fopen((char *)infile, WRITEBIN);
12014 if (fd == NULL)
12015 {
12016 EMSG2(_(e_notopen), infile);
12017 goto errret;
12018 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010012019 if (argvars[1].v_type == VAR_NUMBER)
12020 {
12021 linenr_T lnum;
12022 buf_T *buf;
12023
12024 buf = buflist_findnr(argvars[1].vval.v_number);
12025 if (buf == NULL)
12026 {
12027 EMSGN(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010012028 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010012029 goto errret;
12030 }
12031
12032 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
12033 {
12034 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
12035 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
12036 {
12037 err = TRUE;
12038 break;
12039 }
12040 if (putc(NL, fd) == EOF)
12041 {
12042 err = TRUE;
12043 break;
12044 }
12045 }
12046 }
12047 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012048 {
12049 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
12050 err = TRUE;
12051 }
12052 else
12053 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010012054 size_t len;
12055 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012056
12057 p = get_tv_string_buf_chk(&argvars[1], buf);
12058 if (p == NULL)
12059 {
12060 fclose(fd);
12061 goto errret; /* type error; errmsg already given */
12062 }
12063 len = STRLEN(p);
12064 if (len > 0 && fwrite(p, len, 1, fd) != 1)
12065 err = TRUE;
12066 }
12067 if (fclose(fd) != 0)
12068 err = TRUE;
12069 if (err)
12070 {
12071 EMSG(_("E677: Error writing temp file"));
12072 goto errret;
12073 }
12074 }
12075
12076 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
12077 * echoes typeahead, that messes up the display. */
12078 if (!msg_silent)
12079 flags += SHELL_COOKED;
12080
12081 if (retlist)
12082 {
12083 int len;
12084 listitem_T *li;
12085 char_u *s = NULL;
12086 char_u *start;
12087 char_u *end;
12088 int i;
12089
12090 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
12091 if (res == NULL)
12092 goto errret;
12093
12094 list = list_alloc();
12095 if (list == NULL)
12096 goto errret;
12097
12098 for (i = 0; i < len; ++i)
12099 {
12100 start = res + i;
12101 while (i < len && res[i] != NL)
12102 ++i;
12103 end = res + i;
12104
12105 s = alloc((unsigned)(end - start + 1));
12106 if (s == NULL)
12107 goto errret;
12108
12109 for (p = s; start < end; ++p, ++start)
12110 *p = *start == NUL ? NL : *start;
12111 *p = NUL;
12112
12113 li = listitem_alloc();
12114 if (li == NULL)
12115 {
12116 vim_free(s);
12117 goto errret;
12118 }
12119 li->li_tv.v_type = VAR_STRING;
12120 li->li_tv.v_lock = 0;
12121 li->li_tv.vval.v_string = s;
12122 list_append(list, li);
12123 }
12124
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012125 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012126 list = NULL;
12127 }
12128 else
12129 {
12130 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
12131#ifdef USE_CR
12132 /* translate <CR> into <NL> */
12133 if (res != NULL)
12134 {
12135 char_u *s;
12136
12137 for (s = res; *s; ++s)
12138 {
12139 if (*s == CAR)
12140 *s = NL;
12141 }
12142 }
12143#else
12144# ifdef USE_CRNL
12145 /* translate <CR><NL> into <NL> */
12146 if (res != NULL)
12147 {
12148 char_u *s, *d;
12149
12150 d = res;
12151 for (s = res; *s; ++s)
12152 {
12153 if (s[0] == CAR && s[1] == NL)
12154 ++s;
12155 *d++ = *s;
12156 }
12157 *d = NUL;
12158 }
12159# endif
12160#endif
12161 rettv->vval.v_string = res;
12162 res = NULL;
12163 }
12164
12165errret:
12166 if (infile != NULL)
12167 {
12168 mch_remove(infile);
12169 vim_free(infile);
12170 }
12171 if (res != NULL)
12172 vim_free(res);
12173 if (list != NULL)
12174 list_free(list);
12175}
12176
12177/*
12178 * "system()" function
12179 */
12180 static void
12181f_system(typval_T *argvars, typval_T *rettv)
12182{
12183 get_cmd_output_as_rettv(argvars, rettv, FALSE);
12184}
12185
12186/*
12187 * "systemlist()" function
12188 */
12189 static void
12190f_systemlist(typval_T *argvars, typval_T *rettv)
12191{
12192 get_cmd_output_as_rettv(argvars, rettv, TRUE);
12193}
12194
12195/*
12196 * "tabpagebuflist()" function
12197 */
12198 static void
12199f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12200{
12201#ifdef FEAT_WINDOWS
12202 tabpage_T *tp;
12203 win_T *wp = NULL;
12204
12205 if (argvars[0].v_type == VAR_UNKNOWN)
12206 wp = firstwin;
12207 else
12208 {
12209 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12210 if (tp != NULL)
12211 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12212 }
12213 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
12214 {
12215 for (; wp != NULL; wp = wp->w_next)
12216 if (list_append_number(rettv->vval.v_list,
12217 wp->w_buffer->b_fnum) == FAIL)
12218 break;
12219 }
12220#endif
12221}
12222
12223
12224/*
12225 * "tabpagenr()" function
12226 */
12227 static void
12228f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
12229{
12230 int nr = 1;
12231#ifdef FEAT_WINDOWS
12232 char_u *arg;
12233
12234 if (argvars[0].v_type != VAR_UNKNOWN)
12235 {
12236 arg = get_tv_string_chk(&argvars[0]);
12237 nr = 0;
12238 if (arg != NULL)
12239 {
12240 if (STRCMP(arg, "$") == 0)
12241 nr = tabpage_index(NULL) - 1;
12242 else
12243 EMSG2(_(e_invexpr2), arg);
12244 }
12245 }
12246 else
12247 nr = tabpage_index(curtab);
12248#endif
12249 rettv->vval.v_number = nr;
12250}
12251
12252
12253#ifdef FEAT_WINDOWS
12254static int get_winnr(tabpage_T *tp, typval_T *argvar);
12255
12256/*
12257 * Common code for tabpagewinnr() and winnr().
12258 */
12259 static int
12260get_winnr(tabpage_T *tp, typval_T *argvar)
12261{
12262 win_T *twin;
12263 int nr = 1;
12264 win_T *wp;
12265 char_u *arg;
12266
12267 twin = (tp == curtab) ? curwin : tp->tp_curwin;
12268 if (argvar->v_type != VAR_UNKNOWN)
12269 {
12270 arg = get_tv_string_chk(argvar);
12271 if (arg == NULL)
12272 nr = 0; /* type error; errmsg already given */
12273 else if (STRCMP(arg, "$") == 0)
12274 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
12275 else if (STRCMP(arg, "#") == 0)
12276 {
12277 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
12278 if (twin == NULL)
12279 nr = 0;
12280 }
12281 else
12282 {
12283 EMSG2(_(e_invexpr2), arg);
12284 nr = 0;
12285 }
12286 }
12287
12288 if (nr > 0)
12289 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12290 wp != twin; wp = wp->w_next)
12291 {
12292 if (wp == NULL)
12293 {
12294 /* didn't find it in this tabpage */
12295 nr = 0;
12296 break;
12297 }
12298 ++nr;
12299 }
12300 return nr;
12301}
12302#endif
12303
12304/*
12305 * "tabpagewinnr()" function
12306 */
12307 static void
12308f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
12309{
12310 int nr = 1;
12311#ifdef FEAT_WINDOWS
12312 tabpage_T *tp;
12313
12314 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12315 if (tp == NULL)
12316 nr = 0;
12317 else
12318 nr = get_winnr(tp, &argvars[1]);
12319#endif
12320 rettv->vval.v_number = nr;
12321}
12322
12323
12324/*
12325 * "tagfiles()" function
12326 */
12327 static void
12328f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
12329{
12330 char_u *fname;
12331 tagname_T tn;
12332 int first;
12333
12334 if (rettv_list_alloc(rettv) == FAIL)
12335 return;
12336 fname = alloc(MAXPATHL);
12337 if (fname == NULL)
12338 return;
12339
12340 for (first = TRUE; ; first = FALSE)
12341 if (get_tagfname(&tn, first, fname) == FAIL
12342 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
12343 break;
12344 tagname_free(&tn);
12345 vim_free(fname);
12346}
12347
12348/*
12349 * "taglist()" function
12350 */
12351 static void
12352f_taglist(typval_T *argvars, typval_T *rettv)
12353{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012354 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012355 char_u *tag_pattern;
12356
12357 tag_pattern = get_tv_string(&argvars[0]);
12358
12359 rettv->vval.v_number = FALSE;
12360 if (*tag_pattern == NUL)
12361 return;
12362
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012363 if (argvars[1].v_type != VAR_UNKNOWN)
12364 fname = get_tv_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012365 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012366 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012367}
12368
12369/*
12370 * "tempname()" function
12371 */
12372 static void
12373f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
12374{
12375 static int x = 'A';
12376
12377 rettv->v_type = VAR_STRING;
12378 rettv->vval.v_string = vim_tempname(x, FALSE);
12379
12380 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
12381 * names. Skip 'I' and 'O', they are used for shell redirection. */
12382 do
12383 {
12384 if (x == 'Z')
12385 x = '0';
12386 else if (x == '9')
12387 x = 'A';
12388 else
12389 {
12390#ifdef EBCDIC
12391 if (x == 'I')
12392 x = 'J';
12393 else if (x == 'R')
12394 x = 'S';
12395 else
12396#endif
12397 ++x;
12398 }
12399 } while (x == 'I' || x == 'O');
12400}
12401
12402#ifdef FEAT_FLOAT
12403/*
12404 * "tan()" function
12405 */
12406 static void
12407f_tan(typval_T *argvars, typval_T *rettv)
12408{
12409 float_T f = 0.0;
12410
12411 rettv->v_type = VAR_FLOAT;
12412 if (get_float_arg(argvars, &f) == OK)
12413 rettv->vval.v_float = tan(f);
12414 else
12415 rettv->vval.v_float = 0.0;
12416}
12417
12418/*
12419 * "tanh()" function
12420 */
12421 static void
12422f_tanh(typval_T *argvars, typval_T *rettv)
12423{
12424 float_T f = 0.0;
12425
12426 rettv->v_type = VAR_FLOAT;
12427 if (get_float_arg(argvars, &f) == OK)
12428 rettv->vval.v_float = tanh(f);
12429 else
12430 rettv->vval.v_float = 0.0;
12431}
12432#endif
12433
12434/*
12435 * "test_alloc_fail(id, countdown, repeat)" function
12436 */
12437 static void
12438f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
12439{
12440 if (argvars[0].v_type != VAR_NUMBER
12441 || argvars[0].vval.v_number <= 0
12442 || argvars[1].v_type != VAR_NUMBER
12443 || argvars[1].vval.v_number < 0
12444 || argvars[2].v_type != VAR_NUMBER)
12445 EMSG(_(e_invarg));
12446 else
12447 {
12448 alloc_fail_id = argvars[0].vval.v_number;
12449 if (alloc_fail_id >= aid_last)
12450 EMSG(_(e_invarg));
12451 alloc_fail_countdown = argvars[1].vval.v_number;
12452 alloc_fail_repeat = argvars[2].vval.v_number;
12453 did_outofmem_msg = FALSE;
12454 }
12455}
12456
12457/*
12458 * "test_autochdir()"
12459 */
12460 static void
12461f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12462{
12463#if defined(FEAT_AUTOCHDIR)
12464 test_autochdir = TRUE;
12465#endif
12466}
12467
12468/*
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012469 * "test_disable({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012470 */
12471 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012472f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012473{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012474 char_u *name = (char_u *)"";
12475 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012476 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012477
12478 if (argvars[0].v_type != VAR_STRING
12479 || (argvars[1].v_type) != VAR_NUMBER)
12480 EMSG(_(e_invarg));
12481 else
12482 {
12483 name = get_tv_string_chk(&argvars[0]);
12484 val = (int)get_tv_number(&argvars[1]);
12485
12486 if (STRCMP(name, (char_u *)"redraw") == 0)
12487 disable_redraw_for_testing = val;
12488 else if (STRCMP(name, (char_u *)"char_avail") == 0)
12489 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012490 else if (STRCMP(name, (char_u *)"starting") == 0)
12491 {
12492 if (val)
12493 {
12494 if (save_starting < 0)
12495 save_starting = starting;
12496 starting = 0;
12497 }
12498 else
12499 {
12500 starting = save_starting;
12501 save_starting = -1;
12502 }
12503 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012504 else if (STRCMP(name, (char_u *)"ALL") == 0)
12505 {
12506 disable_char_avail_for_testing = FALSE;
12507 disable_redraw_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012508 if (save_starting >= 0)
12509 {
12510 starting = save_starting;
12511 save_starting = -1;
12512 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012513 }
12514 else
12515 EMSG2(_(e_invarg2), name);
12516 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012517}
12518
12519/*
12520 * "test_garbagecollect_now()" function
12521 */
12522 static void
12523f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12524{
12525 /* This is dangerous, any Lists and Dicts used internally may be freed
12526 * while still in use. */
12527 garbage_collect(TRUE);
12528}
12529
Bram Moolenaare0c31f62017-03-01 15:07:05 +010012530/*
12531 * "test_ignore_error()" function
12532 */
12533 static void
12534f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
12535{
12536 ignore_error_for_testing(get_tv_string(&argvars[0]));
12537}
12538
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012539#ifdef FEAT_JOB_CHANNEL
12540 static void
12541f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
12542{
12543 rettv->v_type = VAR_CHANNEL;
12544 rettv->vval.v_channel = NULL;
12545}
12546#endif
12547
12548 static void
12549f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
12550{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012551 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012552}
12553
12554#ifdef FEAT_JOB_CHANNEL
12555 static void
12556f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
12557{
12558 rettv->v_type = VAR_JOB;
12559 rettv->vval.v_job = NULL;
12560}
12561#endif
12562
12563 static void
12564f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
12565{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012566 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012567}
12568
12569 static void
12570f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
12571{
12572 rettv->v_type = VAR_PARTIAL;
12573 rettv->vval.v_partial = NULL;
12574}
12575
12576 static void
12577f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
12578{
12579 rettv->v_type = VAR_STRING;
12580 rettv->vval.v_string = NULL;
12581}
12582
12583 static void
12584f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
12585{
12586 time_for_testing = (time_t)get_tv_number(&argvars[0]);
12587}
12588
12589#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
12590/*
12591 * Get a callback from "arg". It can be a Funcref or a function name.
12592 * When "arg" is zero return an empty string.
12593 * Return NULL for an invalid argument.
12594 */
12595 char_u *
12596get_callback(typval_T *arg, partial_T **pp)
12597{
12598 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
12599 {
12600 *pp = arg->vval.v_partial;
12601 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012602 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012603 }
12604 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012605 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012606 {
12607 func_ref(arg->vval.v_string);
12608 return arg->vval.v_string;
12609 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012610 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
12611 return (char_u *)"";
12612 EMSG(_("E921: Invalid callback argument"));
12613 return NULL;
12614}
12615
12616/*
12617 * Unref/free "callback" and "partial" retured by get_callback().
12618 */
12619 void
12620free_callback(char_u *callback, partial_T *partial)
12621{
12622 if (partial != NULL)
12623 partial_unref(partial);
12624 else if (callback != NULL)
12625 {
12626 func_unref(callback);
12627 vim_free(callback);
12628 }
12629}
12630#endif
12631
12632#ifdef FEAT_TIMERS
12633/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020012634 * "timer_info([timer])" function
12635 */
12636 static void
12637f_timer_info(typval_T *argvars, typval_T *rettv)
12638{
12639 timer_T *timer = NULL;
12640
12641 if (rettv_list_alloc(rettv) != OK)
12642 return;
12643 if (argvars[0].v_type != VAR_UNKNOWN)
12644 {
12645 if (argvars[0].v_type != VAR_NUMBER)
12646 EMSG(_(e_number_exp));
12647 else
12648 {
12649 timer = find_timer((int)get_tv_number(&argvars[0]));
12650 if (timer != NULL)
12651 add_timer_info(rettv, timer);
12652 }
12653 }
12654 else
12655 add_timer_info_all(rettv);
12656}
12657
12658/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012659 * "timer_pause(timer, paused)" function
12660 */
12661 static void
12662f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
12663{
12664 timer_T *timer = NULL;
12665 int paused = (int)get_tv_number(&argvars[1]);
12666
12667 if (argvars[0].v_type != VAR_NUMBER)
12668 EMSG(_(e_number_exp));
12669 else
12670 {
12671 timer = find_timer((int)get_tv_number(&argvars[0]));
12672 if (timer != NULL)
12673 timer->tr_paused = paused;
12674 }
12675}
12676
12677/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012678 * "timer_start(time, callback [, options])" function
12679 */
12680 static void
12681f_timer_start(typval_T *argvars, typval_T *rettv)
12682{
Bram Moolenaar75537a92016-09-05 22:45:28 +020012683 long msec = (long)get_tv_number(&argvars[0]);
12684 timer_T *timer;
12685 int repeat = 0;
12686 char_u *callback;
12687 dict_T *dict;
12688 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012689
Bram Moolenaar75537a92016-09-05 22:45:28 +020012690 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012691 if (check_secure())
12692 return;
12693 if (argvars[2].v_type != VAR_UNKNOWN)
12694 {
12695 if (argvars[2].v_type != VAR_DICT
12696 || (dict = argvars[2].vval.v_dict) == NULL)
12697 {
12698 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
12699 return;
12700 }
12701 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
12702 repeat = get_dict_number(dict, (char_u *)"repeat");
12703 }
12704
Bram Moolenaar75537a92016-09-05 22:45:28 +020012705 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012706 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020012707 return;
12708
12709 timer = create_timer(msec, repeat);
12710 if (timer == NULL)
12711 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012712 else
12713 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020012714 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020012715 timer->tr_callback = vim_strsave(callback);
12716 else
12717 /* pointer into the partial */
12718 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020012719 timer->tr_partial = partial;
12720 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012721 }
12722}
12723
12724/*
12725 * "timer_stop(timer)" function
12726 */
12727 static void
12728f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
12729{
12730 timer_T *timer;
12731
12732 if (argvars[0].v_type != VAR_NUMBER)
12733 {
12734 EMSG(_(e_number_exp));
12735 return;
12736 }
12737 timer = find_timer((int)get_tv_number(&argvars[0]));
12738 if (timer != NULL)
12739 stop_timer(timer);
12740}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012741
12742/*
12743 * "timer_stopall()" function
12744 */
12745 static void
12746f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12747{
12748 stop_all_timers();
12749}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012750#endif
12751
12752/*
12753 * "tolower(string)" function
12754 */
12755 static void
12756f_tolower(typval_T *argvars, typval_T *rettv)
12757{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012758 rettv->v_type = VAR_STRING;
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +010012759 rettv->vval.v_string = strlow_save(get_tv_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012760}
12761
12762/*
12763 * "toupper(string)" function
12764 */
12765 static void
12766f_toupper(typval_T *argvars, typval_T *rettv)
12767{
12768 rettv->v_type = VAR_STRING;
12769 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
12770}
12771
12772/*
12773 * "tr(string, fromstr, tostr)" function
12774 */
12775 static void
12776f_tr(typval_T *argvars, typval_T *rettv)
12777{
12778 char_u *in_str;
12779 char_u *fromstr;
12780 char_u *tostr;
12781 char_u *p;
12782#ifdef FEAT_MBYTE
12783 int inlen;
12784 int fromlen;
12785 int tolen;
12786 int idx;
12787 char_u *cpstr;
12788 int cplen;
12789 int first = TRUE;
12790#endif
12791 char_u buf[NUMBUFLEN];
12792 char_u buf2[NUMBUFLEN];
12793 garray_T ga;
12794
12795 in_str = get_tv_string(&argvars[0]);
12796 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
12797 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
12798
12799 /* Default return value: empty string. */
12800 rettv->v_type = VAR_STRING;
12801 rettv->vval.v_string = NULL;
12802 if (fromstr == NULL || tostr == NULL)
12803 return; /* type error; errmsg already given */
12804 ga_init2(&ga, (int)sizeof(char), 80);
12805
12806#ifdef FEAT_MBYTE
12807 if (!has_mbyte)
12808#endif
12809 /* not multi-byte: fromstr and tostr must be the same length */
12810 if (STRLEN(fromstr) != STRLEN(tostr))
12811 {
12812#ifdef FEAT_MBYTE
12813error:
12814#endif
12815 EMSG2(_(e_invarg2), fromstr);
12816 ga_clear(&ga);
12817 return;
12818 }
12819
12820 /* fromstr and tostr have to contain the same number of chars */
12821 while (*in_str != NUL)
12822 {
12823#ifdef FEAT_MBYTE
12824 if (has_mbyte)
12825 {
12826 inlen = (*mb_ptr2len)(in_str);
12827 cpstr = in_str;
12828 cplen = inlen;
12829 idx = 0;
12830 for (p = fromstr; *p != NUL; p += fromlen)
12831 {
12832 fromlen = (*mb_ptr2len)(p);
12833 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
12834 {
12835 for (p = tostr; *p != NUL; p += tolen)
12836 {
12837 tolen = (*mb_ptr2len)(p);
12838 if (idx-- == 0)
12839 {
12840 cplen = tolen;
12841 cpstr = p;
12842 break;
12843 }
12844 }
12845 if (*p == NUL) /* tostr is shorter than fromstr */
12846 goto error;
12847 break;
12848 }
12849 ++idx;
12850 }
12851
12852 if (first && cpstr == in_str)
12853 {
12854 /* Check that fromstr and tostr have the same number of
12855 * (multi-byte) characters. Done only once when a character
12856 * of in_str doesn't appear in fromstr. */
12857 first = FALSE;
12858 for (p = tostr; *p != NUL; p += tolen)
12859 {
12860 tolen = (*mb_ptr2len)(p);
12861 --idx;
12862 }
12863 if (idx != 0)
12864 goto error;
12865 }
12866
12867 (void)ga_grow(&ga, cplen);
12868 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
12869 ga.ga_len += cplen;
12870
12871 in_str += inlen;
12872 }
12873 else
12874#endif
12875 {
12876 /* When not using multi-byte chars we can do it faster. */
12877 p = vim_strchr(fromstr, *in_str);
12878 if (p != NULL)
12879 ga_append(&ga, tostr[p - fromstr]);
12880 else
12881 ga_append(&ga, *in_str);
12882 ++in_str;
12883 }
12884 }
12885
12886 /* add a terminating NUL */
12887 (void)ga_grow(&ga, 1);
12888 ga_append(&ga, NUL);
12889
12890 rettv->vval.v_string = ga.ga_data;
12891}
12892
12893#ifdef FEAT_FLOAT
12894/*
12895 * "trunc({float})" function
12896 */
12897 static void
12898f_trunc(typval_T *argvars, typval_T *rettv)
12899{
12900 float_T f = 0.0;
12901
12902 rettv->v_type = VAR_FLOAT;
12903 if (get_float_arg(argvars, &f) == OK)
12904 /* trunc() is not in C90, use floor() or ceil() instead. */
12905 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
12906 else
12907 rettv->vval.v_float = 0.0;
12908}
12909#endif
12910
12911/*
12912 * "type(expr)" function
12913 */
12914 static void
12915f_type(typval_T *argvars, typval_T *rettv)
12916{
12917 int n = -1;
12918
12919 switch (argvars[0].v_type)
12920 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020012921 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
12922 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012923 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020012924 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
12925 case VAR_LIST: n = VAR_TYPE_LIST; break;
12926 case VAR_DICT: n = VAR_TYPE_DICT; break;
12927 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012928 case VAR_SPECIAL:
12929 if (argvars[0].vval.v_number == VVAL_FALSE
12930 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020012931 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012932 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020012933 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012934 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020012935 case VAR_JOB: n = VAR_TYPE_JOB; break;
12936 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012937 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010012938 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012939 n = -1;
12940 break;
12941 }
12942 rettv->vval.v_number = n;
12943}
12944
12945/*
12946 * "undofile(name)" function
12947 */
12948 static void
12949f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
12950{
12951 rettv->v_type = VAR_STRING;
12952#ifdef FEAT_PERSISTENT_UNDO
12953 {
12954 char_u *fname = get_tv_string(&argvars[0]);
12955
12956 if (*fname == NUL)
12957 {
12958 /* If there is no file name there will be no undo file. */
12959 rettv->vval.v_string = NULL;
12960 }
12961 else
12962 {
12963 char_u *ffname = FullName_save(fname, FALSE);
12964
12965 if (ffname != NULL)
12966 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
12967 vim_free(ffname);
12968 }
12969 }
12970#else
12971 rettv->vval.v_string = NULL;
12972#endif
12973}
12974
12975/*
12976 * "undotree()" function
12977 */
12978 static void
12979f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
12980{
12981 if (rettv_dict_alloc(rettv) == OK)
12982 {
12983 dict_T *dict = rettv->vval.v_dict;
12984 list_T *list;
12985
12986 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
12987 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
12988 dict_add_nr_str(dict, "save_last",
12989 (long)curbuf->b_u_save_nr_last, NULL);
12990 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
12991 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
12992 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
12993
12994 list = list_alloc();
12995 if (list != NULL)
12996 {
12997 u_eval_tree(curbuf->b_u_oldhead, list);
12998 dict_add_list(dict, "entries", list);
12999 }
13000 }
13001}
13002
13003/*
13004 * "values(dict)" function
13005 */
13006 static void
13007f_values(typval_T *argvars, typval_T *rettv)
13008{
13009 dict_list(argvars, rettv, 1);
13010}
13011
13012/*
13013 * "virtcol(string)" function
13014 */
13015 static void
13016f_virtcol(typval_T *argvars, typval_T *rettv)
13017{
13018 colnr_T vcol = 0;
13019 pos_T *fp;
13020 int fnum = curbuf->b_fnum;
13021
13022 fp = var2fpos(&argvars[0], FALSE, &fnum);
13023 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
13024 && fnum == curbuf->b_fnum)
13025 {
13026 getvvcol(curwin, fp, NULL, NULL, &vcol);
13027 ++vcol;
13028 }
13029
13030 rettv->vval.v_number = vcol;
13031}
13032
13033/*
13034 * "visualmode()" function
13035 */
13036 static void
13037f_visualmode(typval_T *argvars, typval_T *rettv)
13038{
13039 char_u str[2];
13040
13041 rettv->v_type = VAR_STRING;
13042 str[0] = curbuf->b_visual_mode_eval;
13043 str[1] = NUL;
13044 rettv->vval.v_string = vim_strsave(str);
13045
13046 /* A non-zero number or non-empty string argument: reset mode. */
13047 if (non_zero_arg(&argvars[0]))
13048 curbuf->b_visual_mode_eval = NUL;
13049}
13050
13051/*
13052 * "wildmenumode()" function
13053 */
13054 static void
13055f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13056{
13057#ifdef FEAT_WILDMENU
13058 if (wild_menu_showing)
13059 rettv->vval.v_number = 1;
13060#endif
13061}
13062
13063/*
13064 * "winbufnr(nr)" function
13065 */
13066 static void
13067f_winbufnr(typval_T *argvars, typval_T *rettv)
13068{
13069 win_T *wp;
13070
13071 wp = find_win_by_nr(&argvars[0], NULL);
13072 if (wp == NULL)
13073 rettv->vval.v_number = -1;
13074 else
13075 rettv->vval.v_number = wp->w_buffer->b_fnum;
13076}
13077
13078/*
13079 * "wincol()" function
13080 */
13081 static void
13082f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
13083{
13084 validate_cursor();
13085 rettv->vval.v_number = curwin->w_wcol + 1;
13086}
13087
13088/*
13089 * "winheight(nr)" function
13090 */
13091 static void
13092f_winheight(typval_T *argvars, typval_T *rettv)
13093{
13094 win_T *wp;
13095
13096 wp = find_win_by_nr(&argvars[0], NULL);
13097 if (wp == NULL)
13098 rettv->vval.v_number = -1;
13099 else
13100 rettv->vval.v_number = wp->w_height;
13101}
13102
13103/*
13104 * "winline()" function
13105 */
13106 static void
13107f_winline(typval_T *argvars UNUSED, typval_T *rettv)
13108{
13109 validate_cursor();
13110 rettv->vval.v_number = curwin->w_wrow + 1;
13111}
13112
13113/*
13114 * "winnr()" function
13115 */
13116 static void
13117f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
13118{
13119 int nr = 1;
13120
13121#ifdef FEAT_WINDOWS
13122 nr = get_winnr(curtab, &argvars[0]);
13123#endif
13124 rettv->vval.v_number = nr;
13125}
13126
13127/*
13128 * "winrestcmd()" function
13129 */
13130 static void
13131f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
13132{
13133#ifdef FEAT_WINDOWS
13134 win_T *wp;
13135 int winnr = 1;
13136 garray_T ga;
13137 char_u buf[50];
13138
13139 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020013140 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013141 {
13142 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
13143 ga_concat(&ga, buf);
13144 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
13145 ga_concat(&ga, buf);
13146 ++winnr;
13147 }
13148 ga_append(&ga, NUL);
13149
13150 rettv->vval.v_string = ga.ga_data;
13151#else
13152 rettv->vval.v_string = NULL;
13153#endif
13154 rettv->v_type = VAR_STRING;
13155}
13156
13157/*
13158 * "winrestview()" function
13159 */
13160 static void
13161f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
13162{
13163 dict_T *dict;
13164
13165 if (argvars[0].v_type != VAR_DICT
13166 || (dict = argvars[0].vval.v_dict) == NULL)
13167 EMSG(_(e_invarg));
13168 else
13169 {
13170 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
13171 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
13172 if (dict_find(dict, (char_u *)"col", -1) != NULL)
13173 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
13174#ifdef FEAT_VIRTUALEDIT
13175 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
13176 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
13177#endif
13178 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
13179 {
13180 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
13181 curwin->w_set_curswant = FALSE;
13182 }
13183
13184 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
13185 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
13186#ifdef FEAT_DIFF
13187 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
13188 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
13189#endif
13190 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
13191 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
13192 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
13193 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
13194
13195 check_cursor();
13196 win_new_height(curwin, curwin->w_height);
13197# ifdef FEAT_WINDOWS
13198 win_new_width(curwin, W_WIDTH(curwin));
13199# endif
13200 changed_window_setting();
13201
13202 if (curwin->w_topline <= 0)
13203 curwin->w_topline = 1;
13204 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
13205 curwin->w_topline = curbuf->b_ml.ml_line_count;
13206#ifdef FEAT_DIFF
13207 check_topfill(curwin, TRUE);
13208#endif
13209 }
13210}
13211
13212/*
13213 * "winsaveview()" function
13214 */
13215 static void
13216f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
13217{
13218 dict_T *dict;
13219
13220 if (rettv_dict_alloc(rettv) == FAIL)
13221 return;
13222 dict = rettv->vval.v_dict;
13223
13224 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
13225 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
13226#ifdef FEAT_VIRTUALEDIT
13227 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
13228#endif
13229 update_curswant();
13230 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
13231
13232 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
13233#ifdef FEAT_DIFF
13234 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
13235#endif
13236 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
13237 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
13238}
13239
13240/*
13241 * "winwidth(nr)" function
13242 */
13243 static void
13244f_winwidth(typval_T *argvars, typval_T *rettv)
13245{
13246 win_T *wp;
13247
13248 wp = find_win_by_nr(&argvars[0], NULL);
13249 if (wp == NULL)
13250 rettv->vval.v_number = -1;
13251 else
13252#ifdef FEAT_WINDOWS
13253 rettv->vval.v_number = wp->w_width;
13254#else
13255 rettv->vval.v_number = Columns;
13256#endif
13257}
13258
13259/*
13260 * "wordcount()" function
13261 */
13262 static void
13263f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
13264{
13265 if (rettv_dict_alloc(rettv) == FAIL)
13266 return;
13267 cursor_pos_info(rettv->vval.v_dict);
13268}
13269
13270/*
13271 * "writefile()" function
13272 */
13273 static void
13274f_writefile(typval_T *argvars, typval_T *rettv)
13275{
13276 int binary = FALSE;
13277 int append = FALSE;
13278 char_u *fname;
13279 FILE *fd;
13280 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013281 listitem_T *li;
13282 list_T *list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013283
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013284 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013285 if (check_restricted() || check_secure())
13286 return;
13287
13288 if (argvars[0].v_type != VAR_LIST)
13289 {
13290 EMSG2(_(e_listarg), "writefile()");
13291 return;
13292 }
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013293 list = argvars[0].vval.v_list;
13294 if (list == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013295 return;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013296 for (li = list->lv_first; li != NULL; li = li->li_next)
13297 if (get_tv_string_chk(&li->li_tv) == NULL)
13298 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013299
13300 if (argvars[2].v_type != VAR_UNKNOWN)
13301 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013302 char_u *arg2 = get_tv_string_chk(&argvars[2]);
13303
13304 if (arg2 == NULL)
13305 return;
13306 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013307 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013308 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013309 append = TRUE;
13310 }
13311
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013312 fname = get_tv_string_chk(&argvars[1]);
13313 if (fname == NULL)
13314 return;
13315
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013316 /* Always open the file in binary mode, library functions have a mind of
13317 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013318 if (*fname == NUL || (fd = mch_fopen((char *)fname,
13319 append ? APPENDBIN : WRITEBIN)) == NULL)
13320 {
13321 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
13322 ret = -1;
13323 }
13324 else
13325 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013326 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013327 ret = -1;
13328 fclose(fd);
13329 }
13330
13331 rettv->vval.v_number = ret;
13332}
13333
13334/*
13335 * "xor(expr, expr)" function
13336 */
13337 static void
13338f_xor(typval_T *argvars, typval_T *rettv)
13339{
13340 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
13341 ^ get_tv_number_chk(&argvars[1], NULL);
13342}
13343
13344
13345#endif /* FEAT_EVAL */