blob: 1620efe50179f6547a7910eee05fa9d33efd3965 [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
Bram Moolenaard0573012017-10-28 21:11:06 +020027#ifdef MACOS_X
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020028# 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);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +010047static void f_assert_beeps(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020048static void f_assert_equal(typval_T *argvars, typval_T *rettv);
Bram Moolenaard96ff162018-02-18 22:13:29 +010049static void f_assert_equalfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020050static void f_assert_exception(typval_T *argvars, typval_T *rettv);
51static void f_assert_fails(typval_T *argvars, typval_T *rettv);
52static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaar61c04492016-07-23 15:35:35 +020053static void f_assert_inrange(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020054static void f_assert_match(typval_T *argvars, typval_T *rettv);
55static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
56static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar42205552017-03-18 19:42:22 +010057static void f_assert_report(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020058static void f_assert_true(typval_T *argvars, typval_T *rettv);
59#ifdef FEAT_FLOAT
60static void f_asin(typval_T *argvars, typval_T *rettv);
61static void f_atan(typval_T *argvars, typval_T *rettv);
62static void f_atan2(typval_T *argvars, typval_T *rettv);
63#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010064#ifdef FEAT_BEVAL
65static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010066# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010067static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010068# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010069#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020070static void f_browse(typval_T *argvars, typval_T *rettv);
71static void f_browsedir(typval_T *argvars, typval_T *rettv);
72static void f_bufexists(typval_T *argvars, typval_T *rettv);
73static void f_buflisted(typval_T *argvars, typval_T *rettv);
74static void f_bufloaded(typval_T *argvars, typval_T *rettv);
75static void f_bufname(typval_T *argvars, typval_T *rettv);
76static void f_bufnr(typval_T *argvars, typval_T *rettv);
77static void f_bufwinid(typval_T *argvars, typval_T *rettv);
78static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
79static void f_byte2line(typval_T *argvars, typval_T *rettv);
80static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
81static void f_byteidx(typval_T *argvars, typval_T *rettv);
82static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
83static void f_call(typval_T *argvars, typval_T *rettv);
84#ifdef FEAT_FLOAT
85static void f_ceil(typval_T *argvars, typval_T *rettv);
86#endif
87#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +010088static void f_ch_canread(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020089static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0874a832016-09-01 15:11:51 +020090static void f_ch_close_in(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020091static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
92static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
93static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
94static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
95static void f_ch_info(typval_T *argvars, typval_T *rettv);
96static void f_ch_log(typval_T *argvars, typval_T *rettv);
97static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
98static void f_ch_open(typval_T *argvars, typval_T *rettv);
99static void f_ch_read(typval_T *argvars, typval_T *rettv);
100static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
101static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
102static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
103static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
104static void f_ch_status(typval_T *argvars, typval_T *rettv);
105#endif
106static void f_changenr(typval_T *argvars, typval_T *rettv);
107static void f_char2nr(typval_T *argvars, typval_T *rettv);
108static void f_cindent(typval_T *argvars, typval_T *rettv);
109static void f_clearmatches(typval_T *argvars, typval_T *rettv);
110static void f_col(typval_T *argvars, typval_T *rettv);
111#if defined(FEAT_INS_EXPAND)
112static void f_complete(typval_T *argvars, typval_T *rettv);
113static void f_complete_add(typval_T *argvars, typval_T *rettv);
114static void f_complete_check(typval_T *argvars, typval_T *rettv);
115#endif
116static void f_confirm(typval_T *argvars, typval_T *rettv);
117static void f_copy(typval_T *argvars, typval_T *rettv);
118#ifdef FEAT_FLOAT
119static void f_cos(typval_T *argvars, typval_T *rettv);
120static void f_cosh(typval_T *argvars, typval_T *rettv);
121#endif
122static void f_count(typval_T *argvars, typval_T *rettv);
123static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
124static void f_cursor(typval_T *argsvars, typval_T *rettv);
125static void f_deepcopy(typval_T *argvars, typval_T *rettv);
126static void f_delete(typval_T *argvars, typval_T *rettv);
127static void f_did_filetype(typval_T *argvars, typval_T *rettv);
128static void f_diff_filler(typval_T *argvars, typval_T *rettv);
129static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
130static void f_empty(typval_T *argvars, typval_T *rettv);
131static void f_escape(typval_T *argvars, typval_T *rettv);
132static void f_eval(typval_T *argvars, typval_T *rettv);
133static void f_eventhandler(typval_T *argvars, typval_T *rettv);
134static void f_executable(typval_T *argvars, typval_T *rettv);
135static void f_execute(typval_T *argvars, typval_T *rettv);
136static void f_exepath(typval_T *argvars, typval_T *rettv);
137static void f_exists(typval_T *argvars, typval_T *rettv);
138#ifdef FEAT_FLOAT
139static void f_exp(typval_T *argvars, typval_T *rettv);
140#endif
141static void f_expand(typval_T *argvars, typval_T *rettv);
142static void f_extend(typval_T *argvars, typval_T *rettv);
143static void f_feedkeys(typval_T *argvars, typval_T *rettv);
144static void f_filereadable(typval_T *argvars, typval_T *rettv);
145static void f_filewritable(typval_T *argvars, typval_T *rettv);
146static void f_filter(typval_T *argvars, typval_T *rettv);
147static void f_finddir(typval_T *argvars, typval_T *rettv);
148static void f_findfile(typval_T *argvars, typval_T *rettv);
149#ifdef FEAT_FLOAT
150static void f_float2nr(typval_T *argvars, typval_T *rettv);
151static void f_floor(typval_T *argvars, typval_T *rettv);
152static void f_fmod(typval_T *argvars, typval_T *rettv);
153#endif
154static void f_fnameescape(typval_T *argvars, typval_T *rettv);
155static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
156static void f_foldclosed(typval_T *argvars, typval_T *rettv);
157static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
158static void f_foldlevel(typval_T *argvars, typval_T *rettv);
159static void f_foldtext(typval_T *argvars, typval_T *rettv);
160static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
161static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200162static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200163static void f_function(typval_T *argvars, typval_T *rettv);
164static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
165static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200166static void f_getbufinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200167static void f_getbufline(typval_T *argvars, typval_T *rettv);
168static void f_getbufvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100169static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200170static void f_getchar(typval_T *argvars, typval_T *rettv);
171static void f_getcharmod(typval_T *argvars, typval_T *rettv);
172static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
173static void f_getcmdline(typval_T *argvars, typval_T *rettv);
174#if defined(FEAT_CMDL_COMPL)
175static void f_getcompletion(typval_T *argvars, typval_T *rettv);
176#endif
177static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
178static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
179static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
180static void f_getcwd(typval_T *argvars, typval_T *rettv);
181static void f_getfontname(typval_T *argvars, typval_T *rettv);
182static void f_getfperm(typval_T *argvars, typval_T *rettv);
183static void f_getfsize(typval_T *argvars, typval_T *rettv);
184static void f_getftime(typval_T *argvars, typval_T *rettv);
185static void f_getftype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +0100186static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200187static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200188static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200189static void f_getmatches(typval_T *argvars, typval_T *rettv);
190static void f_getpid(typval_T *argvars, typval_T *rettv);
191static void f_getcurpos(typval_T *argvars, typval_T *rettv);
192static void f_getpos(typval_T *argvars, typval_T *rettv);
193static void f_getqflist(typval_T *argvars, typval_T *rettv);
194static void f_getreg(typval_T *argvars, typval_T *rettv);
195static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200196static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200197static void f_gettabvar(typval_T *argvars, typval_T *rettv);
198static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200199static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100200static void f_getwinpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200201static void f_getwinposx(typval_T *argvars, typval_T *rettv);
202static void f_getwinposy(typval_T *argvars, typval_T *rettv);
203static void f_getwinvar(typval_T *argvars, typval_T *rettv);
204static void f_glob(typval_T *argvars, typval_T *rettv);
205static void f_globpath(typval_T *argvars, typval_T *rettv);
206static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
207static void f_has(typval_T *argvars, typval_T *rettv);
208static void f_has_key(typval_T *argvars, typval_T *rettv);
209static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
210static void f_hasmapto(typval_T *argvars, typval_T *rettv);
211static void f_histadd(typval_T *argvars, typval_T *rettv);
212static void f_histdel(typval_T *argvars, typval_T *rettv);
213static void f_histget(typval_T *argvars, typval_T *rettv);
214static void f_histnr(typval_T *argvars, typval_T *rettv);
215static void f_hlID(typval_T *argvars, typval_T *rettv);
216static void f_hlexists(typval_T *argvars, typval_T *rettv);
217static void f_hostname(typval_T *argvars, typval_T *rettv);
218static void f_iconv(typval_T *argvars, typval_T *rettv);
219static void f_indent(typval_T *argvars, typval_T *rettv);
220static void f_index(typval_T *argvars, typval_T *rettv);
221static void f_input(typval_T *argvars, typval_T *rettv);
222static void f_inputdialog(typval_T *argvars, typval_T *rettv);
223static void f_inputlist(typval_T *argvars, typval_T *rettv);
224static void f_inputrestore(typval_T *argvars, typval_T *rettv);
225static void f_inputsave(typval_T *argvars, typval_T *rettv);
226static void f_inputsecret(typval_T *argvars, typval_T *rettv);
227static void f_insert(typval_T *argvars, typval_T *rettv);
228static void f_invert(typval_T *argvars, typval_T *rettv);
229static void f_isdirectory(typval_T *argvars, typval_T *rettv);
230static void f_islocked(typval_T *argvars, typval_T *rettv);
231#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
232static void f_isnan(typval_T *argvars, typval_T *rettv);
233#endif
234static void f_items(typval_T *argvars, typval_T *rettv);
235#ifdef FEAT_JOB_CHANNEL
236static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
237static void f_job_info(typval_T *argvars, typval_T *rettv);
238static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
239static void f_job_start(typval_T *argvars, typval_T *rettv);
240static void f_job_stop(typval_T *argvars, typval_T *rettv);
241static void f_job_status(typval_T *argvars, typval_T *rettv);
242#endif
243static void f_join(typval_T *argvars, typval_T *rettv);
244static void f_js_decode(typval_T *argvars, typval_T *rettv);
245static void f_js_encode(typval_T *argvars, typval_T *rettv);
246static void f_json_decode(typval_T *argvars, typval_T *rettv);
247static void f_json_encode(typval_T *argvars, typval_T *rettv);
248static void f_keys(typval_T *argvars, typval_T *rettv);
249static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
250static void f_len(typval_T *argvars, typval_T *rettv);
251static void f_libcall(typval_T *argvars, typval_T *rettv);
252static void f_libcallnr(typval_T *argvars, typval_T *rettv);
253static void f_line(typval_T *argvars, typval_T *rettv);
254static void f_line2byte(typval_T *argvars, typval_T *rettv);
255static void f_lispindent(typval_T *argvars, typval_T *rettv);
256static void f_localtime(typval_T *argvars, typval_T *rettv);
257#ifdef FEAT_FLOAT
258static void f_log(typval_T *argvars, typval_T *rettv);
259static void f_log10(typval_T *argvars, typval_T *rettv);
260#endif
261#ifdef FEAT_LUA
262static void f_luaeval(typval_T *argvars, typval_T *rettv);
263#endif
264static void f_map(typval_T *argvars, typval_T *rettv);
265static void f_maparg(typval_T *argvars, typval_T *rettv);
266static void f_mapcheck(typval_T *argvars, typval_T *rettv);
267static void f_match(typval_T *argvars, typval_T *rettv);
268static void f_matchadd(typval_T *argvars, typval_T *rettv);
269static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
270static void f_matcharg(typval_T *argvars, typval_T *rettv);
271static void f_matchdelete(typval_T *argvars, typval_T *rettv);
272static void f_matchend(typval_T *argvars, typval_T *rettv);
273static void f_matchlist(typval_T *argvars, typval_T *rettv);
274static void f_matchstr(typval_T *argvars, typval_T *rettv);
275static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
276static void f_max(typval_T *argvars, typval_T *rettv);
277static void f_min(typval_T *argvars, typval_T *rettv);
278#ifdef vim_mkdir
279static void f_mkdir(typval_T *argvars, typval_T *rettv);
280#endif
281static void f_mode(typval_T *argvars, typval_T *rettv);
282#ifdef FEAT_MZSCHEME
283static void f_mzeval(typval_T *argvars, typval_T *rettv);
284#endif
285static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
286static void f_nr2char(typval_T *argvars, typval_T *rettv);
287static void f_or(typval_T *argvars, typval_T *rettv);
288static void f_pathshorten(typval_T *argvars, typval_T *rettv);
289#ifdef FEAT_PERL
290static void f_perleval(typval_T *argvars, typval_T *rettv);
291#endif
292#ifdef FEAT_FLOAT
293static void f_pow(typval_T *argvars, typval_T *rettv);
294#endif
295static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
296static void f_printf(typval_T *argvars, typval_T *rettv);
297static void f_pumvisible(typval_T *argvars, typval_T *rettv);
298#ifdef FEAT_PYTHON3
299static void f_py3eval(typval_T *argvars, typval_T *rettv);
300#endif
301#ifdef FEAT_PYTHON
302static void f_pyeval(typval_T *argvars, typval_T *rettv);
303#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100304#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
305static void f_pyxeval(typval_T *argvars, typval_T *rettv);
306#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200307static void f_range(typval_T *argvars, typval_T *rettv);
308static void f_readfile(typval_T *argvars, typval_T *rettv);
309static void f_reltime(typval_T *argvars, typval_T *rettv);
310#ifdef FEAT_FLOAT
311static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
312#endif
313static void f_reltimestr(typval_T *argvars, typval_T *rettv);
314static void f_remote_expr(typval_T *argvars, typval_T *rettv);
315static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
316static void f_remote_peek(typval_T *argvars, typval_T *rettv);
317static void f_remote_read(typval_T *argvars, typval_T *rettv);
318static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100319static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200320static void f_remove(typval_T *argvars, typval_T *rettv);
321static void f_rename(typval_T *argvars, typval_T *rettv);
322static void f_repeat(typval_T *argvars, typval_T *rettv);
323static void f_resolve(typval_T *argvars, typval_T *rettv);
324static void f_reverse(typval_T *argvars, typval_T *rettv);
325#ifdef FEAT_FLOAT
326static void f_round(typval_T *argvars, typval_T *rettv);
327#endif
328static void f_screenattr(typval_T *argvars, typval_T *rettv);
329static void f_screenchar(typval_T *argvars, typval_T *rettv);
330static void f_screencol(typval_T *argvars, typval_T *rettv);
331static void f_screenrow(typval_T *argvars, typval_T *rettv);
332static void f_search(typval_T *argvars, typval_T *rettv);
333static void f_searchdecl(typval_T *argvars, typval_T *rettv);
334static void f_searchpair(typval_T *argvars, typval_T *rettv);
335static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
336static void f_searchpos(typval_T *argvars, typval_T *rettv);
337static void f_server2client(typval_T *argvars, typval_T *rettv);
338static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200339static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200340static void f_setbufvar(typval_T *argvars, typval_T *rettv);
341static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
342static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
343static void f_setfperm(typval_T *argvars, typval_T *rettv);
344static void f_setline(typval_T *argvars, typval_T *rettv);
345static void f_setloclist(typval_T *argvars, typval_T *rettv);
346static void f_setmatches(typval_T *argvars, typval_T *rettv);
347static void f_setpos(typval_T *argvars, typval_T *rettv);
348static void f_setqflist(typval_T *argvars, typval_T *rettv);
349static void f_setreg(typval_T *argvars, typval_T *rettv);
350static void f_settabvar(typval_T *argvars, typval_T *rettv);
351static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
352static void f_setwinvar(typval_T *argvars, typval_T *rettv);
353#ifdef FEAT_CRYPT
354static void f_sha256(typval_T *argvars, typval_T *rettv);
355#endif /* FEAT_CRYPT */
356static void f_shellescape(typval_T *argvars, typval_T *rettv);
357static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
358static void f_simplify(typval_T *argvars, typval_T *rettv);
359#ifdef FEAT_FLOAT
360static void f_sin(typval_T *argvars, typval_T *rettv);
361static void f_sinh(typval_T *argvars, typval_T *rettv);
362#endif
363static void f_sort(typval_T *argvars, typval_T *rettv);
364static void f_soundfold(typval_T *argvars, typval_T *rettv);
365static void f_spellbadword(typval_T *argvars, typval_T *rettv);
366static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
367static void f_split(typval_T *argvars, typval_T *rettv);
368#ifdef FEAT_FLOAT
369static void f_sqrt(typval_T *argvars, typval_T *rettv);
370static void f_str2float(typval_T *argvars, typval_T *rettv);
371#endif
372static void f_str2nr(typval_T *argvars, typval_T *rettv);
373static void f_strchars(typval_T *argvars, typval_T *rettv);
374#ifdef HAVE_STRFTIME
375static void f_strftime(typval_T *argvars, typval_T *rettv);
376#endif
377static void f_strgetchar(typval_T *argvars, typval_T *rettv);
378static void f_stridx(typval_T *argvars, typval_T *rettv);
379static void f_string(typval_T *argvars, typval_T *rettv);
380static void f_strlen(typval_T *argvars, typval_T *rettv);
381static void f_strcharpart(typval_T *argvars, typval_T *rettv);
382static void f_strpart(typval_T *argvars, typval_T *rettv);
383static void f_strridx(typval_T *argvars, typval_T *rettv);
384static void f_strtrans(typval_T *argvars, typval_T *rettv);
385static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
386static void f_strwidth(typval_T *argvars, typval_T *rettv);
387static void f_submatch(typval_T *argvars, typval_T *rettv);
388static void f_substitute(typval_T *argvars, typval_T *rettv);
389static void f_synID(typval_T *argvars, typval_T *rettv);
390static void f_synIDattr(typval_T *argvars, typval_T *rettv);
391static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
392static void f_synstack(typval_T *argvars, typval_T *rettv);
393static void f_synconcealed(typval_T *argvars, typval_T *rettv);
394static void f_system(typval_T *argvars, typval_T *rettv);
395static void f_systemlist(typval_T *argvars, typval_T *rettv);
396static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
397static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
398static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
399static void f_taglist(typval_T *argvars, typval_T *rettv);
400static void f_tagfiles(typval_T *argvars, typval_T *rettv);
401static void f_tempname(typval_T *argvars, typval_T *rettv);
402static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
403static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200404static void f_test_feedinput(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100405static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200406static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100407static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200408#ifdef FEAT_JOB_CHANNEL
409static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
410#endif
411static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
412#ifdef FEAT_JOB_CHANNEL
413static void f_test_null_job(typval_T *argvars, typval_T *rettv);
414#endif
415static void f_test_null_list(typval_T *argvars, typval_T *rettv);
416static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
417static void f_test_null_string(typval_T *argvars, typval_T *rettv);
418static void f_test_settime(typval_T *argvars, typval_T *rettv);
419#ifdef FEAT_FLOAT
420static void f_tan(typval_T *argvars, typval_T *rettv);
421static void f_tanh(typval_T *argvars, typval_T *rettv);
422#endif
423#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200424static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200425static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200426static void f_timer_start(typval_T *argvars, typval_T *rettv);
427static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200428static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200429#endif
430static void f_tolower(typval_T *argvars, typval_T *rettv);
431static void f_toupper(typval_T *argvars, typval_T *rettv);
432static void f_tr(typval_T *argvars, typval_T *rettv);
433#ifdef FEAT_FLOAT
434static void f_trunc(typval_T *argvars, typval_T *rettv);
435#endif
436static void f_type(typval_T *argvars, typval_T *rettv);
437static void f_undofile(typval_T *argvars, typval_T *rettv);
438static void f_undotree(typval_T *argvars, typval_T *rettv);
439static void f_uniq(typval_T *argvars, typval_T *rettv);
440static void f_values(typval_T *argvars, typval_T *rettv);
441static void f_virtcol(typval_T *argvars, typval_T *rettv);
442static void f_visualmode(typval_T *argvars, typval_T *rettv);
443static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
444static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
445static void f_win_getid(typval_T *argvars, typval_T *rettv);
446static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
447static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
448static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100449static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200450static void f_winbufnr(typval_T *argvars, typval_T *rettv);
451static void f_wincol(typval_T *argvars, typval_T *rettv);
452static void f_winheight(typval_T *argvars, typval_T *rettv);
453static void f_winline(typval_T *argvars, typval_T *rettv);
454static void f_winnr(typval_T *argvars, typval_T *rettv);
455static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
456static void f_winrestview(typval_T *argvars, typval_T *rettv);
457static void f_winsaveview(typval_T *argvars, typval_T *rettv);
458static void f_winwidth(typval_T *argvars, typval_T *rettv);
459static void f_writefile(typval_T *argvars, typval_T *rettv);
460static void f_wordcount(typval_T *argvars, typval_T *rettv);
461static void f_xor(typval_T *argvars, typval_T *rettv);
462
463/*
464 * Array with names and number of arguments of all internal functions
465 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
466 */
467static struct fst
468{
469 char *f_name; /* function name */
470 char f_min_argc; /* minimal number of arguments */
471 char f_max_argc; /* maximal number of arguments */
472 void (*f_func)(typval_T *args, typval_T *rvar);
473 /* implementation of function */
474} functions[] =
475{
476#ifdef FEAT_FLOAT
477 {"abs", 1, 1, f_abs},
478 {"acos", 1, 1, f_acos}, /* WJMc */
479#endif
480 {"add", 2, 2, f_add},
481 {"and", 2, 2, f_and},
482 {"append", 2, 2, f_append},
483 {"argc", 0, 0, f_argc},
484 {"argidx", 0, 0, f_argidx},
485 {"arglistid", 0, 2, f_arglistid},
486 {"argv", 0, 1, f_argv},
487#ifdef FEAT_FLOAT
488 {"asin", 1, 1, f_asin}, /* WJMc */
489#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100490 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200491 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaard96ff162018-02-18 22:13:29 +0100492 {"assert_equalfile", 2, 2, f_assert_equalfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200493 {"assert_exception", 1, 2, f_assert_exception},
494 {"assert_fails", 1, 2, f_assert_fails},
495 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100496 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200497 {"assert_match", 2, 3, f_assert_match},
498 {"assert_notequal", 2, 3, f_assert_notequal},
499 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100500 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200501 {"assert_true", 1, 2, f_assert_true},
502#ifdef FEAT_FLOAT
503 {"atan", 1, 1, f_atan},
504 {"atan2", 2, 2, f_atan2},
505#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100506#ifdef FEAT_BEVAL
507 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100508# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100509 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100510# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100511#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200512 {"browse", 4, 4, f_browse},
513 {"browsedir", 2, 2, f_browsedir},
514 {"bufexists", 1, 1, f_bufexists},
515 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
516 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
517 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
518 {"buflisted", 1, 1, f_buflisted},
519 {"bufloaded", 1, 1, f_bufloaded},
520 {"bufname", 1, 1, f_bufname},
521 {"bufnr", 1, 2, f_bufnr},
522 {"bufwinid", 1, 1, f_bufwinid},
523 {"bufwinnr", 1, 1, f_bufwinnr},
524 {"byte2line", 1, 1, f_byte2line},
525 {"byteidx", 2, 2, f_byteidx},
526 {"byteidxcomp", 2, 2, f_byteidxcomp},
527 {"call", 2, 3, f_call},
528#ifdef FEAT_FLOAT
529 {"ceil", 1, 1, f_ceil},
530#endif
531#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100532 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200533 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200534 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200535 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
536 {"ch_evalraw", 2, 3, f_ch_evalraw},
537 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
538 {"ch_getjob", 1, 1, f_ch_getjob},
539 {"ch_info", 1, 1, f_ch_info},
540 {"ch_log", 1, 2, f_ch_log},
541 {"ch_logfile", 1, 2, f_ch_logfile},
542 {"ch_open", 1, 2, f_ch_open},
543 {"ch_read", 1, 2, f_ch_read},
544 {"ch_readraw", 1, 2, f_ch_readraw},
545 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
546 {"ch_sendraw", 2, 3, f_ch_sendraw},
547 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200548 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200549#endif
550 {"changenr", 0, 0, f_changenr},
551 {"char2nr", 1, 2, f_char2nr},
552 {"cindent", 1, 1, f_cindent},
553 {"clearmatches", 0, 0, f_clearmatches},
554 {"col", 1, 1, f_col},
555#if defined(FEAT_INS_EXPAND)
556 {"complete", 2, 2, f_complete},
557 {"complete_add", 1, 1, f_complete_add},
558 {"complete_check", 0, 0, f_complete_check},
559#endif
560 {"confirm", 1, 4, f_confirm},
561 {"copy", 1, 1, f_copy},
562#ifdef FEAT_FLOAT
563 {"cos", 1, 1, f_cos},
564 {"cosh", 1, 1, f_cosh},
565#endif
566 {"count", 2, 4, f_count},
567 {"cscope_connection",0,3, f_cscope_connection},
568 {"cursor", 1, 3, f_cursor},
569 {"deepcopy", 1, 2, f_deepcopy},
570 {"delete", 1, 2, f_delete},
571 {"did_filetype", 0, 0, f_did_filetype},
572 {"diff_filler", 1, 1, f_diff_filler},
573 {"diff_hlID", 2, 2, f_diff_hlID},
574 {"empty", 1, 1, f_empty},
575 {"escape", 2, 2, f_escape},
576 {"eval", 1, 1, f_eval},
577 {"eventhandler", 0, 0, f_eventhandler},
578 {"executable", 1, 1, f_executable},
579 {"execute", 1, 2, f_execute},
580 {"exepath", 1, 1, f_exepath},
581 {"exists", 1, 1, f_exists},
582#ifdef FEAT_FLOAT
583 {"exp", 1, 1, f_exp},
584#endif
585 {"expand", 1, 3, f_expand},
586 {"extend", 2, 3, f_extend},
587 {"feedkeys", 1, 2, f_feedkeys},
588 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
589 {"filereadable", 1, 1, f_filereadable},
590 {"filewritable", 1, 1, f_filewritable},
591 {"filter", 2, 2, f_filter},
592 {"finddir", 1, 3, f_finddir},
593 {"findfile", 1, 3, f_findfile},
594#ifdef FEAT_FLOAT
595 {"float2nr", 1, 1, f_float2nr},
596 {"floor", 1, 1, f_floor},
597 {"fmod", 2, 2, f_fmod},
598#endif
599 {"fnameescape", 1, 1, f_fnameescape},
600 {"fnamemodify", 2, 2, f_fnamemodify},
601 {"foldclosed", 1, 1, f_foldclosed},
602 {"foldclosedend", 1, 1, f_foldclosedend},
603 {"foldlevel", 1, 1, f_foldlevel},
604 {"foldtext", 0, 0, f_foldtext},
605 {"foldtextresult", 1, 1, f_foldtextresult},
606 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200607 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200608 {"function", 1, 3, f_function},
609 {"garbagecollect", 0, 1, f_garbagecollect},
610 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200611 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200612 {"getbufline", 2, 3, f_getbufline},
613 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100614 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200615 {"getchar", 0, 1, f_getchar},
616 {"getcharmod", 0, 0, f_getcharmod},
617 {"getcharsearch", 0, 0, f_getcharsearch},
618 {"getcmdline", 0, 0, f_getcmdline},
619 {"getcmdpos", 0, 0, f_getcmdpos},
620 {"getcmdtype", 0, 0, f_getcmdtype},
621 {"getcmdwintype", 0, 0, f_getcmdwintype},
622#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200623 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200624#endif
625 {"getcurpos", 0, 0, f_getcurpos},
626 {"getcwd", 0, 2, f_getcwd},
627 {"getfontname", 0, 1, f_getfontname},
628 {"getfperm", 1, 1, f_getfperm},
629 {"getfsize", 1, 1, f_getfsize},
630 {"getftime", 1, 1, f_getftime},
631 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100632 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200633 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200634 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200635 {"getmatches", 0, 0, f_getmatches},
636 {"getpid", 0, 0, f_getpid},
637 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200638 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200639 {"getreg", 0, 3, f_getreg},
640 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200641 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200642 {"gettabvar", 2, 3, f_gettabvar},
643 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200644 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100645 {"getwinpos", 0, 1, f_getwinpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200646 {"getwinposx", 0, 0, f_getwinposx},
647 {"getwinposy", 0, 0, f_getwinposy},
648 {"getwinvar", 2, 3, f_getwinvar},
649 {"glob", 1, 4, f_glob},
650 {"glob2regpat", 1, 1, f_glob2regpat},
651 {"globpath", 2, 5, f_globpath},
652 {"has", 1, 1, f_has},
653 {"has_key", 2, 2, f_has_key},
654 {"haslocaldir", 0, 2, f_haslocaldir},
655 {"hasmapto", 1, 3, f_hasmapto},
656 {"highlightID", 1, 1, f_hlID}, /* obsolete */
657 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
658 {"histadd", 2, 2, f_histadd},
659 {"histdel", 1, 2, f_histdel},
660 {"histget", 1, 2, f_histget},
661 {"histnr", 1, 1, f_histnr},
662 {"hlID", 1, 1, f_hlID},
663 {"hlexists", 1, 1, f_hlexists},
664 {"hostname", 0, 0, f_hostname},
665 {"iconv", 3, 3, f_iconv},
666 {"indent", 1, 1, f_indent},
667 {"index", 2, 4, f_index},
668 {"input", 1, 3, f_input},
669 {"inputdialog", 1, 3, f_inputdialog},
670 {"inputlist", 1, 1, f_inputlist},
671 {"inputrestore", 0, 0, f_inputrestore},
672 {"inputsave", 0, 0, f_inputsave},
673 {"inputsecret", 1, 2, f_inputsecret},
674 {"insert", 2, 3, f_insert},
675 {"invert", 1, 1, f_invert},
676 {"isdirectory", 1, 1, f_isdirectory},
677 {"islocked", 1, 1, f_islocked},
678#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
679 {"isnan", 1, 1, f_isnan},
680#endif
681 {"items", 1, 1, f_items},
682#ifdef FEAT_JOB_CHANNEL
683 {"job_getchannel", 1, 1, f_job_getchannel},
684 {"job_info", 1, 1, f_job_info},
685 {"job_setoptions", 2, 2, f_job_setoptions},
686 {"job_start", 1, 2, f_job_start},
687 {"job_status", 1, 1, f_job_status},
688 {"job_stop", 1, 2, f_job_stop},
689#endif
690 {"join", 1, 2, f_join},
691 {"js_decode", 1, 1, f_js_decode},
692 {"js_encode", 1, 1, f_js_encode},
693 {"json_decode", 1, 1, f_json_decode},
694 {"json_encode", 1, 1, f_json_encode},
695 {"keys", 1, 1, f_keys},
696 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
697 {"len", 1, 1, f_len},
698 {"libcall", 3, 3, f_libcall},
699 {"libcallnr", 3, 3, f_libcallnr},
700 {"line", 1, 1, f_line},
701 {"line2byte", 1, 1, f_line2byte},
702 {"lispindent", 1, 1, f_lispindent},
703 {"localtime", 0, 0, f_localtime},
704#ifdef FEAT_FLOAT
705 {"log", 1, 1, f_log},
706 {"log10", 1, 1, f_log10},
707#endif
708#ifdef FEAT_LUA
709 {"luaeval", 1, 2, f_luaeval},
710#endif
711 {"map", 2, 2, f_map},
712 {"maparg", 1, 4, f_maparg},
713 {"mapcheck", 1, 3, f_mapcheck},
714 {"match", 2, 4, f_match},
715 {"matchadd", 2, 5, f_matchadd},
716 {"matchaddpos", 2, 5, f_matchaddpos},
717 {"matcharg", 1, 1, f_matcharg},
718 {"matchdelete", 1, 1, f_matchdelete},
719 {"matchend", 2, 4, f_matchend},
720 {"matchlist", 2, 4, f_matchlist},
721 {"matchstr", 2, 4, f_matchstr},
722 {"matchstrpos", 2, 4, f_matchstrpos},
723 {"max", 1, 1, f_max},
724 {"min", 1, 1, f_min},
725#ifdef vim_mkdir
726 {"mkdir", 1, 3, f_mkdir},
727#endif
728 {"mode", 0, 1, f_mode},
729#ifdef FEAT_MZSCHEME
730 {"mzeval", 1, 1, f_mzeval},
731#endif
732 {"nextnonblank", 1, 1, f_nextnonblank},
733 {"nr2char", 1, 2, f_nr2char},
734 {"or", 2, 2, f_or},
735 {"pathshorten", 1, 1, f_pathshorten},
736#ifdef FEAT_PERL
737 {"perleval", 1, 1, f_perleval},
738#endif
739#ifdef FEAT_FLOAT
740 {"pow", 2, 2, f_pow},
741#endif
742 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100743 {"printf", 1, 19, f_printf},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200744 {"pumvisible", 0, 0, f_pumvisible},
745#ifdef FEAT_PYTHON3
746 {"py3eval", 1, 1, f_py3eval},
747#endif
748#ifdef FEAT_PYTHON
749 {"pyeval", 1, 1, f_pyeval},
750#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100751#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
752 {"pyxeval", 1, 1, f_pyxeval},
753#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200754 {"range", 1, 3, f_range},
755 {"readfile", 1, 3, f_readfile},
756 {"reltime", 0, 2, f_reltime},
757#ifdef FEAT_FLOAT
758 {"reltimefloat", 1, 1, f_reltimefloat},
759#endif
760 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100761 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200762 {"remote_foreground", 1, 1, f_remote_foreground},
763 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100764 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200765 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100766 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200767 {"remove", 2, 3, f_remove},
768 {"rename", 2, 2, f_rename},
769 {"repeat", 2, 2, f_repeat},
770 {"resolve", 1, 1, f_resolve},
771 {"reverse", 1, 1, f_reverse},
772#ifdef FEAT_FLOAT
773 {"round", 1, 1, f_round},
774#endif
775 {"screenattr", 2, 2, f_screenattr},
776 {"screenchar", 2, 2, f_screenchar},
777 {"screencol", 0, 0, f_screencol},
778 {"screenrow", 0, 0, f_screenrow},
779 {"search", 1, 4, f_search},
780 {"searchdecl", 1, 3, f_searchdecl},
781 {"searchpair", 3, 7, f_searchpair},
782 {"searchpairpos", 3, 7, f_searchpairpos},
783 {"searchpos", 1, 4, f_searchpos},
784 {"server2client", 2, 2, f_server2client},
785 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200786 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200787 {"setbufvar", 3, 3, f_setbufvar},
788 {"setcharsearch", 1, 1, f_setcharsearch},
789 {"setcmdpos", 1, 1, f_setcmdpos},
790 {"setfperm", 2, 2, f_setfperm},
791 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200792 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200793 {"setmatches", 1, 1, f_setmatches},
794 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200795 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200796 {"setreg", 2, 3, f_setreg},
797 {"settabvar", 3, 3, f_settabvar},
798 {"settabwinvar", 4, 4, f_settabwinvar},
799 {"setwinvar", 3, 3, f_setwinvar},
800#ifdef FEAT_CRYPT
801 {"sha256", 1, 1, f_sha256},
802#endif
803 {"shellescape", 1, 2, f_shellescape},
804 {"shiftwidth", 0, 0, f_shiftwidth},
805 {"simplify", 1, 1, f_simplify},
806#ifdef FEAT_FLOAT
807 {"sin", 1, 1, f_sin},
808 {"sinh", 1, 1, f_sinh},
809#endif
810 {"sort", 1, 3, f_sort},
811 {"soundfold", 1, 1, f_soundfold},
812 {"spellbadword", 0, 1, f_spellbadword},
813 {"spellsuggest", 1, 3, f_spellsuggest},
814 {"split", 1, 3, f_split},
815#ifdef FEAT_FLOAT
816 {"sqrt", 1, 1, f_sqrt},
817 {"str2float", 1, 1, f_str2float},
818#endif
819 {"str2nr", 1, 2, f_str2nr},
820 {"strcharpart", 2, 3, f_strcharpart},
821 {"strchars", 1, 2, f_strchars},
822 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
823#ifdef HAVE_STRFTIME
824 {"strftime", 1, 2, f_strftime},
825#endif
826 {"strgetchar", 2, 2, f_strgetchar},
827 {"stridx", 2, 3, f_stridx},
828 {"string", 1, 1, f_string},
829 {"strlen", 1, 1, f_strlen},
830 {"strpart", 2, 3, f_strpart},
831 {"strridx", 2, 3, f_strridx},
832 {"strtrans", 1, 1, f_strtrans},
833 {"strwidth", 1, 1, f_strwidth},
834 {"submatch", 1, 2, f_submatch},
835 {"substitute", 4, 4, f_substitute},
836 {"synID", 3, 3, f_synID},
837 {"synIDattr", 2, 3, f_synIDattr},
838 {"synIDtrans", 1, 1, f_synIDtrans},
839 {"synconcealed", 2, 2, f_synconcealed},
840 {"synstack", 2, 2, f_synstack},
841 {"system", 1, 2, f_system},
842 {"systemlist", 1, 2, f_systemlist},
843 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
844 {"tabpagenr", 0, 1, f_tabpagenr},
845 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
846 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100847 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200848#ifdef FEAT_FLOAT
849 {"tan", 1, 1, f_tan},
850 {"tanh", 1, 1, f_tanh},
851#endif
852 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200853#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100854 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
855 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100856 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200857 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200858 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200859 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200860 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200861 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200862 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200863 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200864 {"term_getstatus", 1, 1, f_term_getstatus},
865 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200866 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200867 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200868 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200869 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100870 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100871 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200872 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200873 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200874#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200875 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
876 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200877 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200878 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100879 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200880#ifdef FEAT_JOB_CHANNEL
881 {"test_null_channel", 0, 0, f_test_null_channel},
882#endif
883 {"test_null_dict", 0, 0, f_test_null_dict},
884#ifdef FEAT_JOB_CHANNEL
885 {"test_null_job", 0, 0, f_test_null_job},
886#endif
887 {"test_null_list", 0, 0, f_test_null_list},
888 {"test_null_partial", 0, 0, f_test_null_partial},
889 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100890 {"test_override", 2, 2, f_test_override},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200891 {"test_settime", 1, 1, f_test_settime},
892#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200893 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200894 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200895 {"timer_start", 2, 3, f_timer_start},
896 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200897 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200898#endif
899 {"tolower", 1, 1, f_tolower},
900 {"toupper", 1, 1, f_toupper},
901 {"tr", 3, 3, f_tr},
902#ifdef FEAT_FLOAT
903 {"trunc", 1, 1, f_trunc},
904#endif
905 {"type", 1, 1, f_type},
906 {"undofile", 1, 1, f_undofile},
907 {"undotree", 0, 0, f_undotree},
908 {"uniq", 1, 3, f_uniq},
909 {"values", 1, 1, f_values},
910 {"virtcol", 1, 1, f_virtcol},
911 {"visualmode", 0, 1, f_visualmode},
912 {"wildmenumode", 0, 0, f_wildmenumode},
913 {"win_findbuf", 1, 1, f_win_findbuf},
914 {"win_getid", 0, 2, f_win_getid},
915 {"win_gotoid", 1, 1, f_win_gotoid},
916 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
917 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100918 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200919 {"winbufnr", 1, 1, f_winbufnr},
920 {"wincol", 0, 0, f_wincol},
921 {"winheight", 1, 1, f_winheight},
922 {"winline", 0, 0, f_winline},
923 {"winnr", 0, 1, f_winnr},
924 {"winrestcmd", 0, 0, f_winrestcmd},
925 {"winrestview", 1, 1, f_winrestview},
926 {"winsaveview", 0, 0, f_winsaveview},
927 {"winwidth", 1, 1, f_winwidth},
928 {"wordcount", 0, 0, f_wordcount},
929 {"writefile", 2, 3, f_writefile},
930 {"xor", 2, 2, f_xor},
931};
932
933#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
934
935/*
936 * Function given to ExpandGeneric() to obtain the list of internal
937 * or user defined function names.
938 */
939 char_u *
940get_function_name(expand_T *xp, int idx)
941{
942 static int intidx = -1;
943 char_u *name;
944
945 if (idx == 0)
946 intidx = -1;
947 if (intidx < 0)
948 {
949 name = get_user_func_name(xp, idx);
950 if (name != NULL)
951 return name;
952 }
953 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
954 {
955 STRCPY(IObuff, functions[intidx].f_name);
956 STRCAT(IObuff, "(");
957 if (functions[intidx].f_max_argc == 0)
958 STRCAT(IObuff, ")");
959 return IObuff;
960 }
961
962 return NULL;
963}
964
965/*
966 * Function given to ExpandGeneric() to obtain the list of internal or
967 * user defined variable or function names.
968 */
969 char_u *
970get_expr_name(expand_T *xp, int idx)
971{
972 static int intidx = -1;
973 char_u *name;
974
975 if (idx == 0)
976 intidx = -1;
977 if (intidx < 0)
978 {
979 name = get_function_name(xp, idx);
980 if (name != NULL)
981 return name;
982 }
983 return get_user_var_name(xp, ++intidx);
984}
985
986#endif /* FEAT_CMDL_COMPL */
987
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200988/*
989 * Find internal function in table above.
990 * Return index, or -1 if not found
991 */
992 int
993find_internal_func(
994 char_u *name) /* name of the function */
995{
996 int first = 0;
997 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
998 int cmp;
999 int x;
1000
1001 /*
1002 * Find the function name in the table. Binary search.
1003 */
1004 while (first <= last)
1005 {
1006 x = first + ((unsigned)(last - first) >> 1);
1007 cmp = STRCMP(name, functions[x].f_name);
1008 if (cmp < 0)
1009 last = x - 1;
1010 else if (cmp > 0)
1011 first = x + 1;
1012 else
1013 return x;
1014 }
1015 return -1;
1016}
1017
1018 int
1019call_internal_func(
1020 char_u *name,
1021 int argcount,
1022 typval_T *argvars,
1023 typval_T *rettv)
1024{
1025 int i;
1026
1027 i = find_internal_func(name);
1028 if (i < 0)
1029 return ERROR_UNKNOWN;
1030 if (argcount < functions[i].f_min_argc)
1031 return ERROR_TOOFEW;
1032 if (argcount > functions[i].f_max_argc)
1033 return ERROR_TOOMANY;
1034 argvars[argcount].v_type = VAR_UNKNOWN;
1035 functions[i].f_func(argvars, rettv);
1036 return ERROR_NONE;
1037}
1038
1039/*
1040 * Return TRUE for a non-zero Number and a non-empty String.
1041 */
1042 static int
1043non_zero_arg(typval_T *argvars)
1044{
1045 return ((argvars[0].v_type == VAR_NUMBER
1046 && argvars[0].vval.v_number != 0)
1047 || (argvars[0].v_type == VAR_SPECIAL
1048 && argvars[0].vval.v_number == VVAL_TRUE)
1049 || (argvars[0].v_type == VAR_STRING
1050 && argvars[0].vval.v_string != NULL
1051 && *argvars[0].vval.v_string != NUL));
1052}
1053
1054/*
1055 * Get the lnum from the first argument.
1056 * Also accepts ".", "$", etc., but that only works for the current buffer.
1057 * Returns -1 on error.
1058 */
1059 static linenr_T
1060get_tv_lnum(typval_T *argvars)
1061{
1062 typval_T rettv;
1063 linenr_T lnum;
1064
1065 lnum = (linenr_T)get_tv_number_chk(&argvars[0], NULL);
1066 if (lnum == 0) /* no valid number, try using line() */
1067 {
1068 rettv.v_type = VAR_NUMBER;
1069 f_line(argvars, &rettv);
1070 lnum = (linenr_T)rettv.vval.v_number;
1071 clear_tv(&rettv);
1072 }
1073 return lnum;
1074}
1075
1076#ifdef FEAT_FLOAT
1077static int get_float_arg(typval_T *argvars, float_T *f);
1078
1079/*
1080 * Get the float value of "argvars[0]" into "f".
1081 * Returns FAIL when the argument is not a Number or Float.
1082 */
1083 static int
1084get_float_arg(typval_T *argvars, float_T *f)
1085{
1086 if (argvars[0].v_type == VAR_FLOAT)
1087 {
1088 *f = argvars[0].vval.v_float;
1089 return OK;
1090 }
1091 if (argvars[0].v_type == VAR_NUMBER)
1092 {
1093 *f = (float_T)argvars[0].vval.v_number;
1094 return OK;
1095 }
1096 EMSG(_("E808: Number or Float required"));
1097 return FAIL;
1098}
1099
1100/*
1101 * "abs(expr)" function
1102 */
1103 static void
1104f_abs(typval_T *argvars, typval_T *rettv)
1105{
1106 if (argvars[0].v_type == VAR_FLOAT)
1107 {
1108 rettv->v_type = VAR_FLOAT;
1109 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1110 }
1111 else
1112 {
1113 varnumber_T n;
1114 int error = FALSE;
1115
1116 n = get_tv_number_chk(&argvars[0], &error);
1117 if (error)
1118 rettv->vval.v_number = -1;
1119 else if (n > 0)
1120 rettv->vval.v_number = n;
1121 else
1122 rettv->vval.v_number = -n;
1123 }
1124}
1125
1126/*
1127 * "acos()" function
1128 */
1129 static void
1130f_acos(typval_T *argvars, typval_T *rettv)
1131{
1132 float_T f = 0.0;
1133
1134 rettv->v_type = VAR_FLOAT;
1135 if (get_float_arg(argvars, &f) == OK)
1136 rettv->vval.v_float = acos(f);
1137 else
1138 rettv->vval.v_float = 0.0;
1139}
1140#endif
1141
1142/*
1143 * "add(list, item)" function
1144 */
1145 static void
1146f_add(typval_T *argvars, typval_T *rettv)
1147{
1148 list_T *l;
1149
1150 rettv->vval.v_number = 1; /* Default: Failed */
1151 if (argvars[0].v_type == VAR_LIST)
1152 {
1153 if ((l = argvars[0].vval.v_list) != NULL
1154 && !tv_check_lock(l->lv_lock,
1155 (char_u *)N_("add() argument"), TRUE)
1156 && list_append_tv(l, &argvars[1]) == OK)
1157 copy_tv(&argvars[0], rettv);
1158 }
1159 else
1160 EMSG(_(e_listreq));
1161}
1162
1163/*
1164 * "and(expr, expr)" function
1165 */
1166 static void
1167f_and(typval_T *argvars, typval_T *rettv)
1168{
1169 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
1170 & get_tv_number_chk(&argvars[1], NULL);
1171}
1172
1173/*
1174 * "append(lnum, string/list)" function
1175 */
1176 static void
1177f_append(typval_T *argvars, typval_T *rettv)
1178{
1179 long lnum;
1180 char_u *line;
1181 list_T *l = NULL;
1182 listitem_T *li = NULL;
1183 typval_T *tv;
1184 long added = 0;
1185
1186 /* When coming here from Insert mode, sync undo, so that this can be
1187 * undone separately from what was previously inserted. */
1188 if (u_sync_once == 2)
1189 {
1190 u_sync_once = 1; /* notify that u_sync() was called */
1191 u_sync(TRUE);
1192 }
1193
1194 lnum = get_tv_lnum(argvars);
1195 if (lnum >= 0
1196 && lnum <= curbuf->b_ml.ml_line_count
1197 && u_save(lnum, lnum + 1) == OK)
1198 {
1199 if (argvars[1].v_type == VAR_LIST)
1200 {
1201 l = argvars[1].vval.v_list;
1202 if (l == NULL)
1203 return;
1204 li = l->lv_first;
1205 }
1206 for (;;)
1207 {
1208 if (l == NULL)
1209 tv = &argvars[1]; /* append a string */
1210 else if (li == NULL)
1211 break; /* end of list */
1212 else
1213 tv = &li->li_tv; /* append item from list */
1214 line = get_tv_string_chk(tv);
1215 if (line == NULL) /* type error */
1216 {
1217 rettv->vval.v_number = 1; /* Failed */
1218 break;
1219 }
1220 ml_append(lnum + added, line, (colnr_T)0, FALSE);
1221 ++added;
1222 if (l == NULL)
1223 break;
1224 li = li->li_next;
1225 }
1226
1227 appended_lines_mark(lnum, added);
1228 if (curwin->w_cursor.lnum > lnum)
1229 curwin->w_cursor.lnum += added;
1230 }
1231 else
1232 rettv->vval.v_number = 1; /* Failed */
1233}
1234
1235/*
1236 * "argc()" function
1237 */
1238 static void
1239f_argc(typval_T *argvars UNUSED, typval_T *rettv)
1240{
1241 rettv->vval.v_number = ARGCOUNT;
1242}
1243
1244/*
1245 * "argidx()" function
1246 */
1247 static void
1248f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1249{
1250 rettv->vval.v_number = curwin->w_arg_idx;
1251}
1252
1253/*
1254 * "arglistid()" function
1255 */
1256 static void
1257f_arglistid(typval_T *argvars, typval_T *rettv)
1258{
1259 win_T *wp;
1260
1261 rettv->vval.v_number = -1;
1262 wp = find_tabwin(&argvars[0], &argvars[1]);
1263 if (wp != NULL)
1264 rettv->vval.v_number = wp->w_alist->id;
1265}
1266
1267/*
1268 * "argv(nr)" function
1269 */
1270 static void
1271f_argv(typval_T *argvars, typval_T *rettv)
1272{
1273 int idx;
1274
1275 if (argvars[0].v_type != VAR_UNKNOWN)
1276 {
1277 idx = (int)get_tv_number_chk(&argvars[0], NULL);
1278 if (idx >= 0 && idx < ARGCOUNT)
1279 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
1280 else
1281 rettv->vval.v_string = NULL;
1282 rettv->v_type = VAR_STRING;
1283 }
1284 else if (rettv_list_alloc(rettv) == OK)
1285 for (idx = 0; idx < ARGCOUNT; ++idx)
1286 list_append_string(rettv->vval.v_list,
1287 alist_name(&ARGLIST[idx]), -1);
1288}
1289
1290/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001291 * "assert_beeps(cmd [, error])" function
1292 */
1293 static void
1294f_assert_beeps(typval_T *argvars, typval_T *rettv UNUSED)
1295{
1296 assert_beeps(argvars);
1297}
1298
1299/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001300 * "assert_equal(expected, actual[, msg])" function
1301 */
1302 static void
1303f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
1304{
1305 assert_equal_common(argvars, ASSERT_EQUAL);
1306}
1307
1308/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001309 * "assert_equalfile(fname-one, fname-two)" function
1310 */
1311 static void
1312f_assert_equalfile(typval_T *argvars, typval_T *rettv UNUSED)
1313{
1314 assert_equalfile(argvars);
1315}
1316
1317/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001318 * "assert_notequal(expected, actual[, msg])" function
1319 */
1320 static void
1321f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED)
1322{
1323 assert_equal_common(argvars, ASSERT_NOTEQUAL);
1324}
1325
1326/*
1327 * "assert_exception(string[, msg])" function
1328 */
1329 static void
1330f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
1331{
1332 assert_exception(argvars);
1333}
1334
1335/*
1336 * "assert_fails(cmd [, error])" function
1337 */
1338 static void
1339f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
1340{
1341 assert_fails(argvars);
1342}
1343
1344/*
1345 * "assert_false(actual[, msg])" function
1346 */
1347 static void
1348f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
1349{
1350 assert_bool(argvars, FALSE);
1351}
1352
1353/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001354 * "assert_inrange(lower, upper[, msg])" function
1355 */
1356 static void
1357f_assert_inrange(typval_T *argvars, typval_T *rettv UNUSED)
1358{
1359 assert_inrange(argvars);
1360}
1361
1362/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001363 * "assert_match(pattern, actual[, msg])" function
1364 */
1365 static void
1366f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
1367{
1368 assert_match_common(argvars, ASSERT_MATCH);
1369}
1370
1371/*
1372 * "assert_notmatch(pattern, actual[, msg])" function
1373 */
1374 static void
1375f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED)
1376{
1377 assert_match_common(argvars, ASSERT_NOTMATCH);
1378}
1379
1380/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001381 * "assert_report(msg)" function
1382 */
1383 static void
1384f_assert_report(typval_T *argvars, typval_T *rettv UNUSED)
1385{
1386 assert_report(argvars);
1387}
1388
1389/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001390 * "assert_true(actual[, msg])" function
1391 */
1392 static void
1393f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
1394{
1395 assert_bool(argvars, TRUE);
1396}
1397
1398#ifdef FEAT_FLOAT
1399/*
1400 * "asin()" function
1401 */
1402 static void
1403f_asin(typval_T *argvars, typval_T *rettv)
1404{
1405 float_T f = 0.0;
1406
1407 rettv->v_type = VAR_FLOAT;
1408 if (get_float_arg(argvars, &f) == OK)
1409 rettv->vval.v_float = asin(f);
1410 else
1411 rettv->vval.v_float = 0.0;
1412}
1413
1414/*
1415 * "atan()" function
1416 */
1417 static void
1418f_atan(typval_T *argvars, typval_T *rettv)
1419{
1420 float_T f = 0.0;
1421
1422 rettv->v_type = VAR_FLOAT;
1423 if (get_float_arg(argvars, &f) == OK)
1424 rettv->vval.v_float = atan(f);
1425 else
1426 rettv->vval.v_float = 0.0;
1427}
1428
1429/*
1430 * "atan2()" function
1431 */
1432 static void
1433f_atan2(typval_T *argvars, typval_T *rettv)
1434{
1435 float_T fx = 0.0, fy = 0.0;
1436
1437 rettv->v_type = VAR_FLOAT;
1438 if (get_float_arg(argvars, &fx) == OK
1439 && get_float_arg(&argvars[1], &fy) == OK)
1440 rettv->vval.v_float = atan2(fx, fy);
1441 else
1442 rettv->vval.v_float = 0.0;
1443}
1444#endif
1445
1446/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001447 * "balloon_show()" function
1448 */
1449#ifdef FEAT_BEVAL
1450 static void
1451f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1452{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001453 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001454 {
1455 if (argvars[0].v_type == VAR_LIST
1456# ifdef FEAT_GUI
1457 && !gui.in_use
1458# endif
1459 )
1460 post_balloon(balloonEval, NULL, argvars[0].vval.v_list);
1461 else
1462 post_balloon(balloonEval, get_tv_string_chk(&argvars[0]), NULL);
1463 }
1464}
1465
Bram Moolenaar669a8282017-11-19 20:13:05 +01001466# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001467 static void
1468f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1469{
1470 if (rettv_list_alloc(rettv) == OK)
1471 {
1472 char_u *msg = get_tv_string_chk(&argvars[0]);
1473
1474 if (msg != NULL)
1475 {
1476 pumitem_T *array;
1477 int size = split_message(msg, &array);
1478 int i;
1479
1480 /* Skip the first and last item, they are always empty. */
1481 for (i = 1; i < size - 1; ++i)
1482 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001483 while (size > 0)
1484 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001485 vim_free(array);
1486 }
1487 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001488}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001489# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001490#endif
1491
1492/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001493 * "browse(save, title, initdir, default)" function
1494 */
1495 static void
1496f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1497{
1498#ifdef FEAT_BROWSE
1499 int save;
1500 char_u *title;
1501 char_u *initdir;
1502 char_u *defname;
1503 char_u buf[NUMBUFLEN];
1504 char_u buf2[NUMBUFLEN];
1505 int error = FALSE;
1506
1507 save = (int)get_tv_number_chk(&argvars[0], &error);
1508 title = get_tv_string_chk(&argvars[1]);
1509 initdir = get_tv_string_buf_chk(&argvars[2], buf);
1510 defname = get_tv_string_buf_chk(&argvars[3], buf2);
1511
1512 if (error || title == NULL || initdir == NULL || defname == NULL)
1513 rettv->vval.v_string = NULL;
1514 else
1515 rettv->vval.v_string =
1516 do_browse(save ? BROWSE_SAVE : 0,
1517 title, defname, NULL, initdir, NULL, curbuf);
1518#else
1519 rettv->vval.v_string = NULL;
1520#endif
1521 rettv->v_type = VAR_STRING;
1522}
1523
1524/*
1525 * "browsedir(title, initdir)" function
1526 */
1527 static void
1528f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1529{
1530#ifdef FEAT_BROWSE
1531 char_u *title;
1532 char_u *initdir;
1533 char_u buf[NUMBUFLEN];
1534
1535 title = get_tv_string_chk(&argvars[0]);
1536 initdir = get_tv_string_buf_chk(&argvars[1], buf);
1537
1538 if (title == NULL || initdir == NULL)
1539 rettv->vval.v_string = NULL;
1540 else
1541 rettv->vval.v_string = do_browse(BROWSE_DIR,
1542 title, NULL, NULL, initdir, NULL, curbuf);
1543#else
1544 rettv->vval.v_string = NULL;
1545#endif
1546 rettv->v_type = VAR_STRING;
1547}
1548
1549static buf_T *find_buffer(typval_T *avar);
1550
1551/*
1552 * Find a buffer by number or exact name.
1553 */
1554 static buf_T *
1555find_buffer(typval_T *avar)
1556{
1557 buf_T *buf = NULL;
1558
1559 if (avar->v_type == VAR_NUMBER)
1560 buf = buflist_findnr((int)avar->vval.v_number);
1561 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1562 {
1563 buf = buflist_findname_exp(avar->vval.v_string);
1564 if (buf == NULL)
1565 {
1566 /* No full path name match, try a match with a URL or a "nofile"
1567 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001568 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001569 if (buf->b_fname != NULL
1570 && (path_with_url(buf->b_fname)
1571#ifdef FEAT_QUICKFIX
1572 || bt_nofile(buf)
1573#endif
1574 )
1575 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1576 break;
1577 }
1578 }
1579 return buf;
1580}
1581
1582/*
1583 * "bufexists(expr)" function
1584 */
1585 static void
1586f_bufexists(typval_T *argvars, typval_T *rettv)
1587{
1588 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1589}
1590
1591/*
1592 * "buflisted(expr)" function
1593 */
1594 static void
1595f_buflisted(typval_T *argvars, typval_T *rettv)
1596{
1597 buf_T *buf;
1598
1599 buf = find_buffer(&argvars[0]);
1600 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1601}
1602
1603/*
1604 * "bufloaded(expr)" function
1605 */
1606 static void
1607f_bufloaded(typval_T *argvars, typval_T *rettv)
1608{
1609 buf_T *buf;
1610
1611 buf = find_buffer(&argvars[0]);
1612 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1613}
1614
1615 buf_T *
1616buflist_find_by_name(char_u *name, int curtab_only)
1617{
1618 int save_magic;
1619 char_u *save_cpo;
1620 buf_T *buf;
1621
1622 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1623 save_magic = p_magic;
1624 p_magic = TRUE;
1625 save_cpo = p_cpo;
1626 p_cpo = (char_u *)"";
1627
1628 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1629 TRUE, FALSE, curtab_only));
1630
1631 p_magic = save_magic;
1632 p_cpo = save_cpo;
1633 return buf;
1634}
1635
1636/*
1637 * Get buffer by number or pattern.
1638 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001639 buf_T *
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001640get_buf_tv(typval_T *tv, int curtab_only)
1641{
1642 char_u *name = tv->vval.v_string;
1643 buf_T *buf;
1644
1645 if (tv->v_type == VAR_NUMBER)
1646 return buflist_findnr((int)tv->vval.v_number);
1647 if (tv->v_type != VAR_STRING)
1648 return NULL;
1649 if (name == NULL || *name == NUL)
1650 return curbuf;
1651 if (name[0] == '$' && name[1] == NUL)
1652 return lastbuf;
1653
1654 buf = buflist_find_by_name(name, curtab_only);
1655
1656 /* If not found, try expanding the name, like done for bufexists(). */
1657 if (buf == NULL)
1658 buf = find_buffer(tv);
1659
1660 return buf;
1661}
1662
1663/*
1664 * "bufname(expr)" function
1665 */
1666 static void
1667f_bufname(typval_T *argvars, typval_T *rettv)
1668{
1669 buf_T *buf;
1670
1671 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1672 ++emsg_off;
1673 buf = get_buf_tv(&argvars[0], FALSE);
1674 rettv->v_type = VAR_STRING;
1675 if (buf != NULL && buf->b_fname != NULL)
1676 rettv->vval.v_string = vim_strsave(buf->b_fname);
1677 else
1678 rettv->vval.v_string = NULL;
1679 --emsg_off;
1680}
1681
1682/*
1683 * "bufnr(expr)" function
1684 */
1685 static void
1686f_bufnr(typval_T *argvars, typval_T *rettv)
1687{
1688 buf_T *buf;
1689 int error = FALSE;
1690 char_u *name;
1691
1692 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1693 ++emsg_off;
1694 buf = get_buf_tv(&argvars[0], FALSE);
1695 --emsg_off;
1696
1697 /* If the buffer isn't found and the second argument is not zero create a
1698 * new buffer. */
1699 if (buf == NULL
1700 && argvars[1].v_type != VAR_UNKNOWN
1701 && get_tv_number_chk(&argvars[1], &error) != 0
1702 && !error
1703 && (name = get_tv_string_chk(&argvars[0])) != NULL
1704 && !error)
1705 buf = buflist_new(name, NULL, (linenr_T)1, 0);
1706
1707 if (buf != NULL)
1708 rettv->vval.v_number = buf->b_fnum;
1709 else
1710 rettv->vval.v_number = -1;
1711}
1712
1713 static void
1714buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
1715{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001716 win_T *wp;
1717 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001718 buf_T *buf;
1719
1720 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1721 ++emsg_off;
1722 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02001723 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001724 {
1725 ++winnr;
1726 if (wp->w_buffer == buf)
1727 break;
1728 }
1729 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001730 --emsg_off;
1731}
1732
1733/*
1734 * "bufwinid(nr)" function
1735 */
1736 static void
1737f_bufwinid(typval_T *argvars, typval_T *rettv)
1738{
1739 buf_win_common(argvars, rettv, FALSE);
1740}
1741
1742/*
1743 * "bufwinnr(nr)" function
1744 */
1745 static void
1746f_bufwinnr(typval_T *argvars, typval_T *rettv)
1747{
1748 buf_win_common(argvars, rettv, TRUE);
1749}
1750
1751/*
1752 * "byte2line(byte)" function
1753 */
1754 static void
1755f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1756{
1757#ifndef FEAT_BYTEOFF
1758 rettv->vval.v_number = -1;
1759#else
1760 long boff = 0;
1761
1762 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
1763 if (boff < 0)
1764 rettv->vval.v_number = -1;
1765 else
1766 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
1767 (linenr_T)0, &boff);
1768#endif
1769}
1770
1771 static void
1772byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
1773{
1774#ifdef FEAT_MBYTE
1775 char_u *t;
1776#endif
1777 char_u *str;
1778 varnumber_T idx;
1779
1780 str = get_tv_string_chk(&argvars[0]);
1781 idx = get_tv_number_chk(&argvars[1], NULL);
1782 rettv->vval.v_number = -1;
1783 if (str == NULL || idx < 0)
1784 return;
1785
1786#ifdef FEAT_MBYTE
1787 t = str;
1788 for ( ; idx > 0; idx--)
1789 {
1790 if (*t == NUL) /* EOL reached */
1791 return;
1792 if (enc_utf8 && comp)
1793 t += utf_ptr2len(t);
1794 else
1795 t += (*mb_ptr2len)(t);
1796 }
1797 rettv->vval.v_number = (varnumber_T)(t - str);
1798#else
1799 if ((size_t)idx <= STRLEN(str))
1800 rettv->vval.v_number = idx;
1801#endif
1802}
1803
1804/*
1805 * "byteidx()" function
1806 */
1807 static void
1808f_byteidx(typval_T *argvars, typval_T *rettv)
1809{
1810 byteidx(argvars, rettv, FALSE);
1811}
1812
1813/*
1814 * "byteidxcomp()" function
1815 */
1816 static void
1817f_byteidxcomp(typval_T *argvars, typval_T *rettv)
1818{
1819 byteidx(argvars, rettv, TRUE);
1820}
1821
1822/*
1823 * "call(func, arglist [, dict])" function
1824 */
1825 static void
1826f_call(typval_T *argvars, typval_T *rettv)
1827{
1828 char_u *func;
1829 partial_T *partial = NULL;
1830 dict_T *selfdict = NULL;
1831
1832 if (argvars[1].v_type != VAR_LIST)
1833 {
1834 EMSG(_(e_listreq));
1835 return;
1836 }
1837 if (argvars[1].vval.v_list == NULL)
1838 return;
1839
1840 if (argvars[0].v_type == VAR_FUNC)
1841 func = argvars[0].vval.v_string;
1842 else if (argvars[0].v_type == VAR_PARTIAL)
1843 {
1844 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001845 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001846 }
1847 else
1848 func = get_tv_string(&argvars[0]);
1849 if (*func == NUL)
1850 return; /* type error or empty name */
1851
1852 if (argvars[2].v_type != VAR_UNKNOWN)
1853 {
1854 if (argvars[2].v_type != VAR_DICT)
1855 {
1856 EMSG(_(e_dictreq));
1857 return;
1858 }
1859 selfdict = argvars[2].vval.v_dict;
1860 }
1861
1862 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
1863}
1864
1865#ifdef FEAT_FLOAT
1866/*
1867 * "ceil({float})" function
1868 */
1869 static void
1870f_ceil(typval_T *argvars, typval_T *rettv)
1871{
1872 float_T f = 0.0;
1873
1874 rettv->v_type = VAR_FLOAT;
1875 if (get_float_arg(argvars, &f) == OK)
1876 rettv->vval.v_float = ceil(f);
1877 else
1878 rettv->vval.v_float = 0.0;
1879}
1880#endif
1881
1882#ifdef FEAT_JOB_CHANNEL
1883/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01001884 * "ch_canread()" function
1885 */
1886 static void
1887f_ch_canread(typval_T *argvars, typval_T *rettv)
1888{
Bram Moolenaar958dc692016-12-01 15:34:12 +01001889 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01001890
1891 rettv->vval.v_number = 0;
1892 if (channel != NULL)
1893 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
1894 || channel_has_readahead(channel, PART_OUT)
1895 || channel_has_readahead(channel, PART_ERR);
1896}
1897
1898/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001899 * "ch_close()" function
1900 */
1901 static void
1902f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
1903{
1904 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
1905
1906 if (channel != NULL)
1907 {
1908 channel_close(channel, FALSE);
1909 channel_clear(channel);
1910 }
1911}
1912
1913/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02001914 * "ch_close()" function
1915 */
1916 static void
1917f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
1918{
1919 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
1920
1921 if (channel != NULL)
1922 channel_close_in(channel);
1923}
1924
1925/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001926 * "ch_getbufnr()" function
1927 */
1928 static void
1929f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
1930{
1931 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1932
1933 rettv->vval.v_number = -1;
1934 if (channel != NULL)
1935 {
1936 char_u *what = get_tv_string(&argvars[1]);
1937 int part;
1938
1939 if (STRCMP(what, "err") == 0)
1940 part = PART_ERR;
1941 else if (STRCMP(what, "out") == 0)
1942 part = PART_OUT;
1943 else if (STRCMP(what, "in") == 0)
1944 part = PART_IN;
1945 else
1946 part = PART_SOCK;
1947 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
1948 rettv->vval.v_number =
1949 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
1950 }
1951}
1952
1953/*
1954 * "ch_getjob()" function
1955 */
1956 static void
1957f_ch_getjob(typval_T *argvars, typval_T *rettv)
1958{
1959 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1960
1961 if (channel != NULL)
1962 {
1963 rettv->v_type = VAR_JOB;
1964 rettv->vval.v_job = channel->ch_job;
1965 if (channel->ch_job != NULL)
1966 ++channel->ch_job->jv_refcount;
1967 }
1968}
1969
1970/*
1971 * "ch_info()" function
1972 */
1973 static void
1974f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
1975{
1976 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1977
1978 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
1979 channel_info(channel, rettv->vval.v_dict);
1980}
1981
1982/*
1983 * "ch_log()" function
1984 */
1985 static void
1986f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
1987{
1988 char_u *msg = get_tv_string(&argvars[0]);
1989 channel_T *channel = NULL;
1990
1991 if (argvars[1].v_type != VAR_UNKNOWN)
1992 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
1993
1994 ch_log(channel, (char *)msg);
1995}
1996
1997/*
1998 * "ch_logfile()" function
1999 */
2000 static void
2001f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
2002{
2003 char_u *fname;
2004 char_u *opt = (char_u *)"";
2005 char_u buf[NUMBUFLEN];
2006
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02002007 /* Don't open a file in restricted mode. */
2008 if (check_restricted() || check_secure())
2009 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002010 fname = get_tv_string(&argvars[0]);
2011 if (argvars[1].v_type == VAR_STRING)
2012 opt = get_tv_string_buf(&argvars[1], buf);
2013 ch_logfile(fname, opt);
2014}
2015
2016/*
2017 * "ch_open()" function
2018 */
2019 static void
2020f_ch_open(typval_T *argvars, typval_T *rettv)
2021{
2022 rettv->v_type = VAR_CHANNEL;
2023 if (check_restricted() || check_secure())
2024 return;
2025 rettv->vval.v_channel = channel_open_func(argvars);
2026}
2027
2028/*
2029 * "ch_read()" function
2030 */
2031 static void
2032f_ch_read(typval_T *argvars, typval_T *rettv)
2033{
2034 common_channel_read(argvars, rettv, FALSE);
2035}
2036
2037/*
2038 * "ch_readraw()" function
2039 */
2040 static void
2041f_ch_readraw(typval_T *argvars, typval_T *rettv)
2042{
2043 common_channel_read(argvars, rettv, TRUE);
2044}
2045
2046/*
2047 * "ch_evalexpr()" function
2048 */
2049 static void
2050f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2051{
2052 ch_expr_common(argvars, rettv, TRUE);
2053}
2054
2055/*
2056 * "ch_sendexpr()" function
2057 */
2058 static void
2059f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2060{
2061 ch_expr_common(argvars, rettv, FALSE);
2062}
2063
2064/*
2065 * "ch_evalraw()" function
2066 */
2067 static void
2068f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2069{
2070 ch_raw_common(argvars, rettv, TRUE);
2071}
2072
2073/*
2074 * "ch_sendraw()" function
2075 */
2076 static void
2077f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2078{
2079 ch_raw_common(argvars, rettv, FALSE);
2080}
2081
2082/*
2083 * "ch_setoptions()" function
2084 */
2085 static void
2086f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2087{
2088 channel_T *channel;
2089 jobopt_T opt;
2090
2091 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2092 if (channel == NULL)
2093 return;
2094 clear_job_options(&opt);
2095 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002096 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002097 channel_set_options(channel, &opt);
2098 free_job_options(&opt);
2099}
2100
2101/*
2102 * "ch_status()" function
2103 */
2104 static void
2105f_ch_status(typval_T *argvars, typval_T *rettv)
2106{
2107 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002108 jobopt_T opt;
2109 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002110
2111 /* return an empty string by default */
2112 rettv->v_type = VAR_STRING;
2113 rettv->vval.v_string = NULL;
2114
2115 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002116
2117 if (argvars[1].v_type != VAR_UNKNOWN)
2118 {
2119 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002120 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002121 && (opt.jo_set & JO_PART))
2122 part = opt.jo_part;
2123 }
2124
2125 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002126}
2127#endif
2128
2129/*
2130 * "changenr()" function
2131 */
2132 static void
2133f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2134{
2135 rettv->vval.v_number = curbuf->b_u_seq_cur;
2136}
2137
2138/*
2139 * "char2nr(string)" function
2140 */
2141 static void
2142f_char2nr(typval_T *argvars, typval_T *rettv)
2143{
2144#ifdef FEAT_MBYTE
2145 if (has_mbyte)
2146 {
2147 int utf8 = 0;
2148
2149 if (argvars[1].v_type != VAR_UNKNOWN)
2150 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
2151
2152 if (utf8)
2153 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
2154 else
2155 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
2156 }
2157 else
2158#endif
2159 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
2160}
2161
2162/*
2163 * "cindent(lnum)" function
2164 */
2165 static void
2166f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2167{
2168#ifdef FEAT_CINDENT
2169 pos_T pos;
2170 linenr_T lnum;
2171
2172 pos = curwin->w_cursor;
2173 lnum = get_tv_lnum(argvars);
2174 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2175 {
2176 curwin->w_cursor.lnum = lnum;
2177 rettv->vval.v_number = get_c_indent();
2178 curwin->w_cursor = pos;
2179 }
2180 else
2181#endif
2182 rettv->vval.v_number = -1;
2183}
2184
2185/*
2186 * "clearmatches()" function
2187 */
2188 static void
2189f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2190{
2191#ifdef FEAT_SEARCH_EXTRA
2192 clear_matches(curwin);
2193#endif
2194}
2195
2196/*
2197 * "col(string)" function
2198 */
2199 static void
2200f_col(typval_T *argvars, typval_T *rettv)
2201{
2202 colnr_T col = 0;
2203 pos_T *fp;
2204 int fnum = curbuf->b_fnum;
2205
2206 fp = var2fpos(&argvars[0], FALSE, &fnum);
2207 if (fp != NULL && fnum == curbuf->b_fnum)
2208 {
2209 if (fp->col == MAXCOL)
2210 {
2211 /* '> can be MAXCOL, get the length of the line then */
2212 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2213 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2214 else
2215 col = MAXCOL;
2216 }
2217 else
2218 {
2219 col = fp->col + 1;
2220#ifdef FEAT_VIRTUALEDIT
2221 /* col(".") when the cursor is on the NUL at the end of the line
2222 * because of "coladd" can be seen as an extra column. */
2223 if (virtual_active() && fp == &curwin->w_cursor)
2224 {
2225 char_u *p = ml_get_cursor();
2226
2227 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2228 curwin->w_virtcol - curwin->w_cursor.coladd))
2229 {
2230# ifdef FEAT_MBYTE
2231 int l;
2232
2233 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2234 col += l;
2235# else
2236 if (*p != NUL && p[1] == NUL)
2237 ++col;
2238# endif
2239 }
2240 }
2241#endif
2242 }
2243 }
2244 rettv->vval.v_number = col;
2245}
2246
2247#if defined(FEAT_INS_EXPAND)
2248/*
2249 * "complete()" function
2250 */
2251 static void
2252f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2253{
2254 int startcol;
2255
2256 if ((State & INSERT) == 0)
2257 {
2258 EMSG(_("E785: complete() can only be used in Insert mode"));
2259 return;
2260 }
2261
2262 /* Check for undo allowed here, because if something was already inserted
2263 * the line was already saved for undo and this check isn't done. */
2264 if (!undo_allowed())
2265 return;
2266
2267 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2268 {
2269 EMSG(_(e_invarg));
2270 return;
2271 }
2272
2273 startcol = (int)get_tv_number_chk(&argvars[0], NULL);
2274 if (startcol <= 0)
2275 return;
2276
2277 set_completion(startcol - 1, argvars[1].vval.v_list);
2278}
2279
2280/*
2281 * "complete_add()" function
2282 */
2283 static void
2284f_complete_add(typval_T *argvars, typval_T *rettv)
2285{
2286 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2287}
2288
2289/*
2290 * "complete_check()" function
2291 */
2292 static void
2293f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2294{
2295 int saved = RedrawingDisabled;
2296
2297 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002298 ins_compl_check_keys(0, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002299 rettv->vval.v_number = compl_interrupted;
2300 RedrawingDisabled = saved;
2301}
2302#endif
2303
2304/*
2305 * "confirm(message, buttons[, default [, type]])" function
2306 */
2307 static void
2308f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2309{
2310#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2311 char_u *message;
2312 char_u *buttons = NULL;
2313 char_u buf[NUMBUFLEN];
2314 char_u buf2[NUMBUFLEN];
2315 int def = 1;
2316 int type = VIM_GENERIC;
2317 char_u *typestr;
2318 int error = FALSE;
2319
2320 message = get_tv_string_chk(&argvars[0]);
2321 if (message == NULL)
2322 error = TRUE;
2323 if (argvars[1].v_type != VAR_UNKNOWN)
2324 {
2325 buttons = get_tv_string_buf_chk(&argvars[1], buf);
2326 if (buttons == NULL)
2327 error = TRUE;
2328 if (argvars[2].v_type != VAR_UNKNOWN)
2329 {
2330 def = (int)get_tv_number_chk(&argvars[2], &error);
2331 if (argvars[3].v_type != VAR_UNKNOWN)
2332 {
2333 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
2334 if (typestr == NULL)
2335 error = TRUE;
2336 else
2337 {
2338 switch (TOUPPER_ASC(*typestr))
2339 {
2340 case 'E': type = VIM_ERROR; break;
2341 case 'Q': type = VIM_QUESTION; break;
2342 case 'I': type = VIM_INFO; break;
2343 case 'W': type = VIM_WARNING; break;
2344 case 'G': type = VIM_GENERIC; break;
2345 }
2346 }
2347 }
2348 }
2349 }
2350
2351 if (buttons == NULL || *buttons == NUL)
2352 buttons = (char_u *)_("&Ok");
2353
2354 if (!error)
2355 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2356 def, NULL, FALSE);
2357#endif
2358}
2359
2360/*
2361 * "copy()" function
2362 */
2363 static void
2364f_copy(typval_T *argvars, typval_T *rettv)
2365{
2366 item_copy(&argvars[0], rettv, FALSE, 0);
2367}
2368
2369#ifdef FEAT_FLOAT
2370/*
2371 * "cos()" function
2372 */
2373 static void
2374f_cos(typval_T *argvars, typval_T *rettv)
2375{
2376 float_T f = 0.0;
2377
2378 rettv->v_type = VAR_FLOAT;
2379 if (get_float_arg(argvars, &f) == OK)
2380 rettv->vval.v_float = cos(f);
2381 else
2382 rettv->vval.v_float = 0.0;
2383}
2384
2385/*
2386 * "cosh()" function
2387 */
2388 static void
2389f_cosh(typval_T *argvars, typval_T *rettv)
2390{
2391 float_T f = 0.0;
2392
2393 rettv->v_type = VAR_FLOAT;
2394 if (get_float_arg(argvars, &f) == OK)
2395 rettv->vval.v_float = cosh(f);
2396 else
2397 rettv->vval.v_float = 0.0;
2398}
2399#endif
2400
2401/*
2402 * "count()" function
2403 */
2404 static void
2405f_count(typval_T *argvars, typval_T *rettv)
2406{
2407 long n = 0;
2408 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002409 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002410
Bram Moolenaar9966b212017-07-28 16:46:57 +02002411 if (argvars[2].v_type != VAR_UNKNOWN)
2412 ic = (int)get_tv_number_chk(&argvars[2], &error);
2413
2414 if (argvars[0].v_type == VAR_STRING)
2415 {
2416 char_u *expr = get_tv_string_chk(&argvars[1]);
2417 char_u *p = argvars[0].vval.v_string;
2418 char_u *next;
2419
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002420 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002421 {
2422 if (ic)
2423 {
2424 size_t len = STRLEN(expr);
2425
2426 while (*p != NUL)
2427 {
2428 if (MB_STRNICMP(p, expr, len) == 0)
2429 {
2430 ++n;
2431 p += len;
2432 }
2433 else
2434 MB_PTR_ADV(p);
2435 }
2436 }
2437 else
2438 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2439 != NULL)
2440 {
2441 ++n;
2442 p = next + STRLEN(expr);
2443 }
2444 }
2445
2446 }
2447 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002448 {
2449 listitem_T *li;
2450 list_T *l;
2451 long idx;
2452
2453 if ((l = argvars[0].vval.v_list) != NULL)
2454 {
2455 li = l->lv_first;
2456 if (argvars[2].v_type != VAR_UNKNOWN)
2457 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002458 if (argvars[3].v_type != VAR_UNKNOWN)
2459 {
2460 idx = (long)get_tv_number_chk(&argvars[3], &error);
2461 if (!error)
2462 {
2463 li = list_find(l, idx);
2464 if (li == NULL)
2465 EMSGN(_(e_listidx), idx);
2466 }
2467 }
2468 if (error)
2469 li = NULL;
2470 }
2471
2472 for ( ; li != NULL; li = li->li_next)
2473 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2474 ++n;
2475 }
2476 }
2477 else if (argvars[0].v_type == VAR_DICT)
2478 {
2479 int todo;
2480 dict_T *d;
2481 hashitem_T *hi;
2482
2483 if ((d = argvars[0].vval.v_dict) != NULL)
2484 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002485 if (argvars[2].v_type != VAR_UNKNOWN)
2486 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002487 if (argvars[3].v_type != VAR_UNKNOWN)
2488 EMSG(_(e_invarg));
2489 }
2490
2491 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2492 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2493 {
2494 if (!HASHITEM_EMPTY(hi))
2495 {
2496 --todo;
2497 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2498 ++n;
2499 }
2500 }
2501 }
2502 }
2503 else
2504 EMSG2(_(e_listdictarg), "count()");
2505 rettv->vval.v_number = n;
2506}
2507
2508/*
2509 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2510 *
2511 * Checks the existence of a cscope connection.
2512 */
2513 static void
2514f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2515{
2516#ifdef FEAT_CSCOPE
2517 int num = 0;
2518 char_u *dbpath = NULL;
2519 char_u *prepend = NULL;
2520 char_u buf[NUMBUFLEN];
2521
2522 if (argvars[0].v_type != VAR_UNKNOWN
2523 && argvars[1].v_type != VAR_UNKNOWN)
2524 {
2525 num = (int)get_tv_number(&argvars[0]);
2526 dbpath = get_tv_string(&argvars[1]);
2527 if (argvars[2].v_type != VAR_UNKNOWN)
2528 prepend = get_tv_string_buf(&argvars[2], buf);
2529 }
2530
2531 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2532#endif
2533}
2534
2535/*
2536 * "cursor(lnum, col)" function, or
2537 * "cursor(list)"
2538 *
2539 * Moves the cursor to the specified line and column.
2540 * Returns 0 when the position could be set, -1 otherwise.
2541 */
2542 static void
2543f_cursor(typval_T *argvars, typval_T *rettv)
2544{
2545 long line, col;
2546#ifdef FEAT_VIRTUALEDIT
2547 long coladd = 0;
2548#endif
2549 int set_curswant = TRUE;
2550
2551 rettv->vval.v_number = -1;
2552 if (argvars[1].v_type == VAR_UNKNOWN)
2553 {
2554 pos_T pos;
2555 colnr_T curswant = -1;
2556
2557 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2558 {
2559 EMSG(_(e_invarg));
2560 return;
2561 }
2562 line = pos.lnum;
2563 col = pos.col;
2564#ifdef FEAT_VIRTUALEDIT
2565 coladd = pos.coladd;
2566#endif
2567 if (curswant >= 0)
2568 {
2569 curwin->w_curswant = curswant - 1;
2570 set_curswant = FALSE;
2571 }
2572 }
2573 else
2574 {
2575 line = get_tv_lnum(argvars);
2576 col = (long)get_tv_number_chk(&argvars[1], NULL);
2577#ifdef FEAT_VIRTUALEDIT
2578 if (argvars[2].v_type != VAR_UNKNOWN)
2579 coladd = (long)get_tv_number_chk(&argvars[2], NULL);
2580#endif
2581 }
2582 if (line < 0 || col < 0
2583#ifdef FEAT_VIRTUALEDIT
2584 || coladd < 0
2585#endif
2586 )
2587 return; /* type error; errmsg already given */
2588 if (line > 0)
2589 curwin->w_cursor.lnum = line;
2590 if (col > 0)
2591 curwin->w_cursor.col = col - 1;
2592#ifdef FEAT_VIRTUALEDIT
2593 curwin->w_cursor.coladd = coladd;
2594#endif
2595
2596 /* Make sure the cursor is in a valid position. */
2597 check_cursor();
2598#ifdef FEAT_MBYTE
2599 /* Correct cursor for multi-byte character. */
2600 if (has_mbyte)
2601 mb_adjust_cursor();
2602#endif
2603
2604 curwin->w_set_curswant = set_curswant;
2605 rettv->vval.v_number = 0;
2606}
2607
2608/*
2609 * "deepcopy()" function
2610 */
2611 static void
2612f_deepcopy(typval_T *argvars, typval_T *rettv)
2613{
2614 int noref = 0;
2615 int copyID;
2616
2617 if (argvars[1].v_type != VAR_UNKNOWN)
2618 noref = (int)get_tv_number_chk(&argvars[1], NULL);
2619 if (noref < 0 || noref > 1)
2620 EMSG(_(e_invarg));
2621 else
2622 {
2623 copyID = get_copyID();
2624 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2625 }
2626}
2627
2628/*
2629 * "delete()" function
2630 */
2631 static void
2632f_delete(typval_T *argvars, typval_T *rettv)
2633{
2634 char_u nbuf[NUMBUFLEN];
2635 char_u *name;
2636 char_u *flags;
2637
2638 rettv->vval.v_number = -1;
2639 if (check_restricted() || check_secure())
2640 return;
2641
2642 name = get_tv_string(&argvars[0]);
2643 if (name == NULL || *name == NUL)
2644 {
2645 EMSG(_(e_invarg));
2646 return;
2647 }
2648
2649 if (argvars[1].v_type != VAR_UNKNOWN)
2650 flags = get_tv_string_buf(&argvars[1], nbuf);
2651 else
2652 flags = (char_u *)"";
2653
2654 if (*flags == NUL)
2655 /* delete a file */
2656 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2657 else if (STRCMP(flags, "d") == 0)
2658 /* delete an empty directory */
2659 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2660 else if (STRCMP(flags, "rf") == 0)
2661 /* delete a directory recursively */
2662 rettv->vval.v_number = delete_recursive(name);
2663 else
2664 EMSG2(_(e_invexpr2), flags);
2665}
2666
2667/*
2668 * "did_filetype()" function
2669 */
2670 static void
2671f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2672{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002673 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002674}
2675
2676/*
2677 * "diff_filler()" function
2678 */
2679 static void
2680f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2681{
2682#ifdef FEAT_DIFF
2683 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
2684#endif
2685}
2686
2687/*
2688 * "diff_hlID()" function
2689 */
2690 static void
2691f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2692{
2693#ifdef FEAT_DIFF
2694 linenr_T lnum = get_tv_lnum(argvars);
2695 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01002696 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002697 static int fnum = 0;
2698 static int change_start = 0;
2699 static int change_end = 0;
2700 static hlf_T hlID = (hlf_T)0;
2701 int filler_lines;
2702 int col;
2703
2704 if (lnum < 0) /* ignore type error in {lnum} arg */
2705 lnum = 0;
2706 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002707 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002708 || fnum != curbuf->b_fnum)
2709 {
2710 /* New line, buffer, change: need to get the values. */
2711 filler_lines = diff_check(curwin, lnum);
2712 if (filler_lines < 0)
2713 {
2714 if (filler_lines == -1)
2715 {
2716 change_start = MAXCOL;
2717 change_end = -1;
2718 if (diff_find_change(curwin, lnum, &change_start, &change_end))
2719 hlID = HLF_ADD; /* added line */
2720 else
2721 hlID = HLF_CHD; /* changed line */
2722 }
2723 else
2724 hlID = HLF_ADD; /* added line */
2725 }
2726 else
2727 hlID = (hlf_T)0;
2728 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002729 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002730 fnum = curbuf->b_fnum;
2731 }
2732
2733 if (hlID == HLF_CHD || hlID == HLF_TXD)
2734 {
2735 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
2736 if (col >= change_start && col <= change_end)
2737 hlID = HLF_TXD; /* changed text */
2738 else
2739 hlID = HLF_CHD; /* changed line */
2740 }
2741 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
2742#endif
2743}
2744
2745/*
2746 * "empty({expr})" function
2747 */
2748 static void
2749f_empty(typval_T *argvars, typval_T *rettv)
2750{
2751 int n = FALSE;
2752
2753 switch (argvars[0].v_type)
2754 {
2755 case VAR_STRING:
2756 case VAR_FUNC:
2757 n = argvars[0].vval.v_string == NULL
2758 || *argvars[0].vval.v_string == NUL;
2759 break;
2760 case VAR_PARTIAL:
2761 n = FALSE;
2762 break;
2763 case VAR_NUMBER:
2764 n = argvars[0].vval.v_number == 0;
2765 break;
2766 case VAR_FLOAT:
2767#ifdef FEAT_FLOAT
2768 n = argvars[0].vval.v_float == 0.0;
2769 break;
2770#endif
2771 case VAR_LIST:
2772 n = argvars[0].vval.v_list == NULL
2773 || argvars[0].vval.v_list->lv_first == NULL;
2774 break;
2775 case VAR_DICT:
2776 n = argvars[0].vval.v_dict == NULL
2777 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2778 break;
2779 case VAR_SPECIAL:
2780 n = argvars[0].vval.v_number != VVAL_TRUE;
2781 break;
2782
2783 case VAR_JOB:
2784#ifdef FEAT_JOB_CHANNEL
2785 n = argvars[0].vval.v_job == NULL
2786 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2787 break;
2788#endif
2789 case VAR_CHANNEL:
2790#ifdef FEAT_JOB_CHANNEL
2791 n = argvars[0].vval.v_channel == NULL
2792 || !channel_is_open(argvars[0].vval.v_channel);
2793 break;
2794#endif
2795 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01002796 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002797 n = TRUE;
2798 break;
2799 }
2800
2801 rettv->vval.v_number = n;
2802}
2803
2804/*
2805 * "escape({string}, {chars})" function
2806 */
2807 static void
2808f_escape(typval_T *argvars, typval_T *rettv)
2809{
2810 char_u buf[NUMBUFLEN];
2811
2812 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
2813 get_tv_string_buf(&argvars[1], buf));
2814 rettv->v_type = VAR_STRING;
2815}
2816
2817/*
2818 * "eval()" function
2819 */
2820 static void
2821f_eval(typval_T *argvars, typval_T *rettv)
2822{
2823 char_u *s, *p;
2824
2825 s = get_tv_string_chk(&argvars[0]);
2826 if (s != NULL)
2827 s = skipwhite(s);
2828
2829 p = s;
2830 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
2831 {
2832 if (p != NULL && !aborting())
2833 EMSG2(_(e_invexpr2), p);
2834 need_clr_eos = FALSE;
2835 rettv->v_type = VAR_NUMBER;
2836 rettv->vval.v_number = 0;
2837 }
2838 else if (*s != NUL)
2839 EMSG(_(e_trailing));
2840}
2841
2842/*
2843 * "eventhandler()" function
2844 */
2845 static void
2846f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
2847{
2848 rettv->vval.v_number = vgetc_busy;
2849}
2850
2851/*
2852 * "executable()" function
2853 */
2854 static void
2855f_executable(typval_T *argvars, typval_T *rettv)
2856{
2857 char_u *name = get_tv_string(&argvars[0]);
2858
2859 /* Check in $PATH and also check directly if there is a directory name. */
2860 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
2861 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
2862}
2863
2864static garray_T redir_execute_ga;
2865
2866/*
2867 * Append "value[value_len]" to the execute() output.
2868 */
2869 void
2870execute_redir_str(char_u *value, int value_len)
2871{
2872 int len;
2873
2874 if (value_len == -1)
2875 len = (int)STRLEN(value); /* Append the entire string */
2876 else
2877 len = value_len; /* Append only "value_len" characters */
2878 if (ga_grow(&redir_execute_ga, len) == OK)
2879 {
2880 mch_memmove((char *)redir_execute_ga.ga_data
2881 + redir_execute_ga.ga_len, value, len);
2882 redir_execute_ga.ga_len += len;
2883 }
2884}
2885
2886/*
2887 * Get next line from a list.
2888 * Called by do_cmdline() to get the next line.
2889 * Returns allocated string, or NULL for end of function.
2890 */
2891
2892 static char_u *
2893get_list_line(
2894 int c UNUSED,
2895 void *cookie,
2896 int indent UNUSED)
2897{
2898 listitem_T **p = (listitem_T **)cookie;
2899 listitem_T *item = *p;
2900 char_u buf[NUMBUFLEN];
2901 char_u *s;
2902
2903 if (item == NULL)
2904 return NULL;
2905 s = get_tv_string_buf_chk(&item->li_tv, buf);
2906 *p = item->li_next;
2907 return s == NULL ? NULL : vim_strsave(s);
2908}
2909
2910/*
2911 * "execute()" function
2912 */
2913 static void
2914f_execute(typval_T *argvars, typval_T *rettv)
2915{
2916 char_u *cmd = NULL;
2917 list_T *list = NULL;
2918 int save_msg_silent = msg_silent;
2919 int save_emsg_silent = emsg_silent;
2920 int save_emsg_noredir = emsg_noredir;
2921 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01002922 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002923 garray_T save_ga;
2924
2925 rettv->vval.v_string = NULL;
2926 rettv->v_type = VAR_STRING;
2927
2928 if (argvars[0].v_type == VAR_LIST)
2929 {
2930 list = argvars[0].vval.v_list;
2931 if (list == NULL || list->lv_first == NULL)
2932 /* empty list, no commands, empty output */
2933 return;
2934 ++list->lv_refcount;
2935 }
2936 else
2937 {
2938 cmd = get_tv_string_chk(&argvars[0]);
2939 if (cmd == NULL)
2940 return;
2941 }
2942
2943 if (argvars[1].v_type != VAR_UNKNOWN)
2944 {
2945 char_u buf[NUMBUFLEN];
2946 char_u *s = get_tv_string_buf_chk(&argvars[1], buf);
2947
2948 if (s == NULL)
2949 return;
2950 if (STRNCMP(s, "silent", 6) == 0)
2951 ++msg_silent;
2952 if (STRCMP(s, "silent!") == 0)
2953 {
2954 emsg_silent = TRUE;
2955 emsg_noredir = TRUE;
2956 }
2957 }
2958 else
2959 ++msg_silent;
2960
2961 if (redir_execute)
2962 save_ga = redir_execute_ga;
2963 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
2964 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01002965 redir_off = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002966
2967 if (cmd != NULL)
2968 do_cmdline_cmd(cmd);
2969 else
2970 {
2971 listitem_T *item = list->lv_first;
2972
2973 do_cmdline(NULL, get_list_line, (void *)&item,
2974 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
2975 --list->lv_refcount;
2976 }
2977
Bram Moolenaard297f352017-01-29 20:31:21 +01002978 /* Need to append a NUL to the result. */
2979 if (ga_grow(&redir_execute_ga, 1) == OK)
2980 {
2981 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
2982 rettv->vval.v_string = redir_execute_ga.ga_data;
2983 }
2984 else
2985 {
2986 ga_clear(&redir_execute_ga);
2987 rettv->vval.v_string = NULL;
2988 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002989 msg_silent = save_msg_silent;
2990 emsg_silent = save_emsg_silent;
2991 emsg_noredir = save_emsg_noredir;
2992
2993 redir_execute = save_redir_execute;
2994 if (redir_execute)
2995 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01002996 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002997
2998 /* "silent reg" or "silent echo x" leaves msg_col somewhere in the
2999 * line. Put it back in the first column. */
3000 msg_col = 0;
3001}
3002
3003/*
3004 * "exepath()" function
3005 */
3006 static void
3007f_exepath(typval_T *argvars, typval_T *rettv)
3008{
3009 char_u *p = NULL;
3010
3011 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
3012 rettv->v_type = VAR_STRING;
3013 rettv->vval.v_string = p;
3014}
3015
3016/*
3017 * "exists()" function
3018 */
3019 static void
3020f_exists(typval_T *argvars, typval_T *rettv)
3021{
3022 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003023 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003024
3025 p = get_tv_string(&argvars[0]);
3026 if (*p == '$') /* environment variable */
3027 {
3028 /* first try "normal" environment variables (fast) */
3029 if (mch_getenv(p + 1) != NULL)
3030 n = TRUE;
3031 else
3032 {
3033 /* try expanding things like $VIM and ${HOME} */
3034 p = expand_env_save(p);
3035 if (p != NULL && *p != '$')
3036 n = TRUE;
3037 vim_free(p);
3038 }
3039 }
3040 else if (*p == '&' || *p == '+') /* option */
3041 {
3042 n = (get_option_tv(&p, NULL, TRUE) == OK);
3043 if (*skipwhite(p) != NUL)
3044 n = FALSE; /* trailing garbage */
3045 }
3046 else if (*p == '*') /* internal or user defined function */
3047 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003048 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003049 }
3050 else if (*p == ':')
3051 {
3052 n = cmd_exists(p + 1);
3053 }
3054 else if (*p == '#')
3055 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003056 if (p[1] == '#')
3057 n = autocmd_supported(p + 2);
3058 else
3059 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003060 }
3061 else /* internal variable */
3062 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003063 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003064 }
3065
3066 rettv->vval.v_number = n;
3067}
3068
3069#ifdef FEAT_FLOAT
3070/*
3071 * "exp()" function
3072 */
3073 static void
3074f_exp(typval_T *argvars, typval_T *rettv)
3075{
3076 float_T f = 0.0;
3077
3078 rettv->v_type = VAR_FLOAT;
3079 if (get_float_arg(argvars, &f) == OK)
3080 rettv->vval.v_float = exp(f);
3081 else
3082 rettv->vval.v_float = 0.0;
3083}
3084#endif
3085
3086/*
3087 * "expand()" function
3088 */
3089 static void
3090f_expand(typval_T *argvars, typval_T *rettv)
3091{
3092 char_u *s;
3093 int len;
3094 char_u *errormsg;
3095 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3096 expand_T xpc;
3097 int error = FALSE;
3098 char_u *result;
3099
3100 rettv->v_type = VAR_STRING;
3101 if (argvars[1].v_type != VAR_UNKNOWN
3102 && argvars[2].v_type != VAR_UNKNOWN
3103 && get_tv_number_chk(&argvars[2], &error)
3104 && !error)
3105 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003106 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003107 }
3108
3109 s = get_tv_string(&argvars[0]);
3110 if (*s == '%' || *s == '#' || *s == '<')
3111 {
3112 ++emsg_off;
3113 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3114 --emsg_off;
3115 if (rettv->v_type == VAR_LIST)
3116 {
3117 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3118 list_append_string(rettv->vval.v_list, result, -1);
3119 else
3120 vim_free(result);
3121 }
3122 else
3123 rettv->vval.v_string = result;
3124 }
3125 else
3126 {
3127 /* When the optional second argument is non-zero, don't remove matches
3128 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3129 if (argvars[1].v_type != VAR_UNKNOWN
3130 && get_tv_number_chk(&argvars[1], &error))
3131 options |= WILD_KEEP_ALL;
3132 if (!error)
3133 {
3134 ExpandInit(&xpc);
3135 xpc.xp_context = EXPAND_FILES;
3136 if (p_wic)
3137 options += WILD_ICASE;
3138 if (rettv->v_type == VAR_STRING)
3139 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3140 options, WILD_ALL);
3141 else if (rettv_list_alloc(rettv) != FAIL)
3142 {
3143 int i;
3144
3145 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3146 for (i = 0; i < xpc.xp_numfiles; i++)
3147 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3148 ExpandCleanup(&xpc);
3149 }
3150 }
3151 else
3152 rettv->vval.v_string = NULL;
3153 }
3154}
3155
3156/*
3157 * "extend(list, list [, idx])" function
3158 * "extend(dict, dict [, action])" function
3159 */
3160 static void
3161f_extend(typval_T *argvars, typval_T *rettv)
3162{
3163 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3164
3165 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3166 {
3167 list_T *l1, *l2;
3168 listitem_T *item;
3169 long before;
3170 int error = FALSE;
3171
3172 l1 = argvars[0].vval.v_list;
3173 l2 = argvars[1].vval.v_list;
3174 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
3175 && l2 != NULL)
3176 {
3177 if (argvars[2].v_type != VAR_UNKNOWN)
3178 {
3179 before = (long)get_tv_number_chk(&argvars[2], &error);
3180 if (error)
3181 return; /* type error; errmsg already given */
3182
3183 if (before == l1->lv_len)
3184 item = NULL;
3185 else
3186 {
3187 item = list_find(l1, before);
3188 if (item == NULL)
3189 {
3190 EMSGN(_(e_listidx), before);
3191 return;
3192 }
3193 }
3194 }
3195 else
3196 item = NULL;
3197 list_extend(l1, l2, item);
3198
3199 copy_tv(&argvars[0], rettv);
3200 }
3201 }
3202 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3203 {
3204 dict_T *d1, *d2;
3205 char_u *action;
3206 int i;
3207
3208 d1 = argvars[0].vval.v_dict;
3209 d2 = argvars[1].vval.v_dict;
3210 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
3211 && d2 != NULL)
3212 {
3213 /* Check the third argument. */
3214 if (argvars[2].v_type != VAR_UNKNOWN)
3215 {
3216 static char *(av[]) = {"keep", "force", "error"};
3217
3218 action = get_tv_string_chk(&argvars[2]);
3219 if (action == NULL)
3220 return; /* type error; errmsg already given */
3221 for (i = 0; i < 3; ++i)
3222 if (STRCMP(action, av[i]) == 0)
3223 break;
3224 if (i == 3)
3225 {
3226 EMSG2(_(e_invarg2), action);
3227 return;
3228 }
3229 }
3230 else
3231 action = (char_u *)"force";
3232
3233 dict_extend(d1, d2, action);
3234
3235 copy_tv(&argvars[0], rettv);
3236 }
3237 }
3238 else
3239 EMSG2(_(e_listdictarg), "extend()");
3240}
3241
3242/*
3243 * "feedkeys()" function
3244 */
3245 static void
3246f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3247{
3248 int remap = TRUE;
3249 int insert = FALSE;
3250 char_u *keys, *flags;
3251 char_u nbuf[NUMBUFLEN];
3252 int typed = FALSE;
3253 int execute = FALSE;
3254 int dangerous = FALSE;
3255 char_u *keys_esc;
3256
3257 /* This is not allowed in the sandbox. If the commands would still be
3258 * executed in the sandbox it would be OK, but it probably happens later,
3259 * when "sandbox" is no longer set. */
3260 if (check_secure())
3261 return;
3262
3263 keys = get_tv_string(&argvars[0]);
3264
3265 if (argvars[1].v_type != VAR_UNKNOWN)
3266 {
3267 flags = get_tv_string_buf(&argvars[1], nbuf);
3268 for ( ; *flags != NUL; ++flags)
3269 {
3270 switch (*flags)
3271 {
3272 case 'n': remap = FALSE; break;
3273 case 'm': remap = TRUE; break;
3274 case 't': typed = TRUE; break;
3275 case 'i': insert = TRUE; break;
3276 case 'x': execute = TRUE; break;
3277 case '!': dangerous = TRUE; break;
3278 }
3279 }
3280 }
3281
3282 if (*keys != NUL || execute)
3283 {
3284 /* Need to escape K_SPECIAL and CSI before putting the string in the
3285 * typeahead buffer. */
3286 keys_esc = vim_strsave_escape_csi(keys);
3287 if (keys_esc != NULL)
3288 {
3289 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
3290 insert ? 0 : typebuf.tb_len, !typed, FALSE);
3291 vim_free(keys_esc);
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003292 if (vgetc_busy
3293#ifdef FEAT_TIMERS
3294 || timer_busy
3295#endif
3296 )
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003297 typebuf_was_filled = TRUE;
3298 if (execute)
3299 {
3300 int save_msg_scroll = msg_scroll;
3301
3302 /* Avoid a 1 second delay when the keys start Insert mode. */
3303 msg_scroll = FALSE;
3304
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02003305#ifdef FEAT_TERMINAL
3306 if (term_use_loop())
3307 terminal_loop(FALSE);
3308 else
3309#endif
3310 {
3311 if (!dangerous)
3312 ++ex_normal_busy;
3313 exec_normal(TRUE);
3314 if (!dangerous)
3315 --ex_normal_busy;
3316 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003317 msg_scroll |= save_msg_scroll;
3318 }
3319 }
3320 }
3321}
3322
3323/*
3324 * "filereadable()" function
3325 */
3326 static void
3327f_filereadable(typval_T *argvars, typval_T *rettv)
3328{
3329 int fd;
3330 char_u *p;
3331 int n;
3332
3333#ifndef O_NONBLOCK
3334# define O_NONBLOCK 0
3335#endif
3336 p = get_tv_string(&argvars[0]);
3337 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3338 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3339 {
3340 n = TRUE;
3341 close(fd);
3342 }
3343 else
3344 n = FALSE;
3345
3346 rettv->vval.v_number = n;
3347}
3348
3349/*
3350 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3351 * rights to write into.
3352 */
3353 static void
3354f_filewritable(typval_T *argvars, typval_T *rettv)
3355{
3356 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
3357}
3358
3359 static void
3360findfilendir(
3361 typval_T *argvars UNUSED,
3362 typval_T *rettv,
3363 int find_what UNUSED)
3364{
3365#ifdef FEAT_SEARCHPATH
3366 char_u *fname;
3367 char_u *fresult = NULL;
3368 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3369 char_u *p;
3370 char_u pathbuf[NUMBUFLEN];
3371 int count = 1;
3372 int first = TRUE;
3373 int error = FALSE;
3374#endif
3375
3376 rettv->vval.v_string = NULL;
3377 rettv->v_type = VAR_STRING;
3378
3379#ifdef FEAT_SEARCHPATH
3380 fname = get_tv_string(&argvars[0]);
3381
3382 if (argvars[1].v_type != VAR_UNKNOWN)
3383 {
3384 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
3385 if (p == NULL)
3386 error = TRUE;
3387 else
3388 {
3389 if (*p != NUL)
3390 path = p;
3391
3392 if (argvars[2].v_type != VAR_UNKNOWN)
3393 count = (int)get_tv_number_chk(&argvars[2], &error);
3394 }
3395 }
3396
3397 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3398 error = TRUE;
3399
3400 if (*fname != NUL && !error)
3401 {
3402 do
3403 {
3404 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3405 vim_free(fresult);
3406 fresult = find_file_in_path_option(first ? fname : NULL,
3407 first ? (int)STRLEN(fname) : 0,
3408 0, first, path,
3409 find_what,
3410 curbuf->b_ffname,
3411 find_what == FINDFILE_DIR
3412 ? (char_u *)"" : curbuf->b_p_sua);
3413 first = FALSE;
3414
3415 if (fresult != NULL && rettv->v_type == VAR_LIST)
3416 list_append_string(rettv->vval.v_list, fresult, -1);
3417
3418 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3419 }
3420
3421 if (rettv->v_type == VAR_STRING)
3422 rettv->vval.v_string = fresult;
3423#endif
3424}
3425
3426/*
3427 * "filter()" function
3428 */
3429 static void
3430f_filter(typval_T *argvars, typval_T *rettv)
3431{
3432 filter_map(argvars, rettv, FALSE);
3433}
3434
3435/*
3436 * "finddir({fname}[, {path}[, {count}]])" function
3437 */
3438 static void
3439f_finddir(typval_T *argvars, typval_T *rettv)
3440{
3441 findfilendir(argvars, rettv, FINDFILE_DIR);
3442}
3443
3444/*
3445 * "findfile({fname}[, {path}[, {count}]])" function
3446 */
3447 static void
3448f_findfile(typval_T *argvars, typval_T *rettv)
3449{
3450 findfilendir(argvars, rettv, FINDFILE_FILE);
3451}
3452
3453#ifdef FEAT_FLOAT
3454/*
3455 * "float2nr({float})" function
3456 */
3457 static void
3458f_float2nr(typval_T *argvars, typval_T *rettv)
3459{
3460 float_T f = 0.0;
3461
3462 if (get_float_arg(argvars, &f) == OK)
3463 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003464 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003465 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003466 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003467 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003468 else
3469 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003470 }
3471}
3472
3473/*
3474 * "floor({float})" function
3475 */
3476 static void
3477f_floor(typval_T *argvars, typval_T *rettv)
3478{
3479 float_T f = 0.0;
3480
3481 rettv->v_type = VAR_FLOAT;
3482 if (get_float_arg(argvars, &f) == OK)
3483 rettv->vval.v_float = floor(f);
3484 else
3485 rettv->vval.v_float = 0.0;
3486}
3487
3488/*
3489 * "fmod()" function
3490 */
3491 static void
3492f_fmod(typval_T *argvars, typval_T *rettv)
3493{
3494 float_T fx = 0.0, fy = 0.0;
3495
3496 rettv->v_type = VAR_FLOAT;
3497 if (get_float_arg(argvars, &fx) == OK
3498 && get_float_arg(&argvars[1], &fy) == OK)
3499 rettv->vval.v_float = fmod(fx, fy);
3500 else
3501 rettv->vval.v_float = 0.0;
3502}
3503#endif
3504
3505/*
3506 * "fnameescape({string})" function
3507 */
3508 static void
3509f_fnameescape(typval_T *argvars, typval_T *rettv)
3510{
3511 rettv->vval.v_string = vim_strsave_fnameescape(
3512 get_tv_string(&argvars[0]), FALSE);
3513 rettv->v_type = VAR_STRING;
3514}
3515
3516/*
3517 * "fnamemodify({fname}, {mods})" function
3518 */
3519 static void
3520f_fnamemodify(typval_T *argvars, typval_T *rettv)
3521{
3522 char_u *fname;
3523 char_u *mods;
3524 int usedlen = 0;
3525 int len;
3526 char_u *fbuf = NULL;
3527 char_u buf[NUMBUFLEN];
3528
3529 fname = get_tv_string_chk(&argvars[0]);
3530 mods = get_tv_string_buf_chk(&argvars[1], buf);
3531 if (fname == NULL || mods == NULL)
3532 fname = NULL;
3533 else
3534 {
3535 len = (int)STRLEN(fname);
3536 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
3537 }
3538
3539 rettv->v_type = VAR_STRING;
3540 if (fname == NULL)
3541 rettv->vval.v_string = NULL;
3542 else
3543 rettv->vval.v_string = vim_strnsave(fname, len);
3544 vim_free(fbuf);
3545}
3546
3547static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
3548
3549/*
3550 * "foldclosed()" function
3551 */
3552 static void
3553foldclosed_both(
3554 typval_T *argvars UNUSED,
3555 typval_T *rettv,
3556 int end UNUSED)
3557{
3558#ifdef FEAT_FOLDING
3559 linenr_T lnum;
3560 linenr_T first, last;
3561
3562 lnum = get_tv_lnum(argvars);
3563 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3564 {
3565 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3566 {
3567 if (end)
3568 rettv->vval.v_number = (varnumber_T)last;
3569 else
3570 rettv->vval.v_number = (varnumber_T)first;
3571 return;
3572 }
3573 }
3574#endif
3575 rettv->vval.v_number = -1;
3576}
3577
3578/*
3579 * "foldclosed()" function
3580 */
3581 static void
3582f_foldclosed(typval_T *argvars, typval_T *rettv)
3583{
3584 foldclosed_both(argvars, rettv, FALSE);
3585}
3586
3587/*
3588 * "foldclosedend()" function
3589 */
3590 static void
3591f_foldclosedend(typval_T *argvars, typval_T *rettv)
3592{
3593 foldclosed_both(argvars, rettv, TRUE);
3594}
3595
3596/*
3597 * "foldlevel()" function
3598 */
3599 static void
3600f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3601{
3602#ifdef FEAT_FOLDING
3603 linenr_T lnum;
3604
3605 lnum = get_tv_lnum(argvars);
3606 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3607 rettv->vval.v_number = foldLevel(lnum);
3608#endif
3609}
3610
3611/*
3612 * "foldtext()" function
3613 */
3614 static void
3615f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
3616{
3617#ifdef FEAT_FOLDING
3618 linenr_T foldstart;
3619 linenr_T foldend;
3620 char_u *dashes;
3621 linenr_T lnum;
3622 char_u *s;
3623 char_u *r;
3624 int len;
3625 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003626 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003627#endif
3628
3629 rettv->v_type = VAR_STRING;
3630 rettv->vval.v_string = NULL;
3631#ifdef FEAT_FOLDING
3632 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
3633 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
3634 dashes = get_vim_var_str(VV_FOLDDASHES);
3635 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
3636 && dashes != NULL)
3637 {
3638 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02003639 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003640 if (!linewhite(lnum))
3641 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003642
3643 /* Find interesting text in this line. */
3644 s = skipwhite(ml_get(lnum));
3645 /* skip C comment-start */
3646 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
3647 {
3648 s = skipwhite(s + 2);
3649 if (*skipwhite(s) == NUL
3650 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
3651 {
3652 s = skipwhite(ml_get(lnum + 1));
3653 if (*s == '*')
3654 s = skipwhite(s + 1);
3655 }
3656 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02003657 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003658 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003659 r = alloc((unsigned)(STRLEN(txt)
3660 + STRLEN(dashes) /* for %s */
3661 + 20 /* for %3ld */
3662 + STRLEN(s))); /* concatenated */
3663 if (r != NULL)
3664 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02003665 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003666 len = (int)STRLEN(r);
3667 STRCAT(r, s);
3668 /* remove 'foldmarker' and 'commentstring' */
3669 foldtext_cleanup(r + len);
3670 rettv->vval.v_string = r;
3671 }
3672 }
3673#endif
3674}
3675
3676/*
3677 * "foldtextresult(lnum)" function
3678 */
3679 static void
3680f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
3681{
3682#ifdef FEAT_FOLDING
3683 linenr_T lnum;
3684 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003685 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003686 foldinfo_T foldinfo;
3687 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003688 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003689#endif
3690
3691 rettv->v_type = VAR_STRING;
3692 rettv->vval.v_string = NULL;
3693#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003694 if (entered)
3695 return; /* reject recursive use */
3696 entered = TRUE;
3697
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003698 lnum = get_tv_lnum(argvars);
3699 /* treat illegal types and illegal string values for {lnum} the same */
3700 if (lnum < 0)
3701 lnum = 0;
3702 fold_count = foldedCount(curwin, lnum, &foldinfo);
3703 if (fold_count > 0)
3704 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01003705 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
3706 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003707 if (text == buf)
3708 text = vim_strsave(text);
3709 rettv->vval.v_string = text;
3710 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003711
3712 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003713#endif
3714}
3715
3716/*
3717 * "foreground()" function
3718 */
3719 static void
3720f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3721{
3722#ifdef FEAT_GUI
3723 if (gui.in_use)
3724 gui_mch_set_foreground();
3725#else
3726# ifdef WIN32
3727 win32_set_foreground();
3728# endif
3729#endif
3730}
3731
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003732 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003733common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003734{
3735 char_u *s;
3736 char_u *name;
3737 int use_string = FALSE;
3738 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003739 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003740
3741 if (argvars[0].v_type == VAR_FUNC)
3742 {
3743 /* function(MyFunc, [arg], dict) */
3744 s = argvars[0].vval.v_string;
3745 }
3746 else if (argvars[0].v_type == VAR_PARTIAL
3747 && argvars[0].vval.v_partial != NULL)
3748 {
3749 /* function(dict.MyFunc, [arg]) */
3750 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003751 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003752 }
3753 else
3754 {
3755 /* function('MyFunc', [arg], dict) */
3756 s = get_tv_string(&argvars[0]);
3757 use_string = TRUE;
3758 }
3759
Bram Moolenaar843b8842016-08-21 14:36:15 +02003760 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003761 {
3762 name = s;
3763 trans_name = trans_function_name(&name, FALSE,
3764 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
3765 if (*name != NUL)
3766 s = NULL;
3767 }
3768
Bram Moolenaar843b8842016-08-21 14:36:15 +02003769 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
3770 || (is_funcref && trans_name == NULL))
Bram Moolenaar5582ef12016-09-14 22:16:13 +02003771 EMSG2(_(e_invarg2), use_string ? get_tv_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003772 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003773 else if (trans_name != NULL && (is_funcref
3774 ? find_func(trans_name) == NULL
3775 : !translated_function_exists(trans_name)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003776 EMSG2(_("E700: Unknown function: %s"), s);
3777 else
3778 {
3779 int dict_idx = 0;
3780 int arg_idx = 0;
3781 list_T *list = NULL;
3782
3783 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
3784 {
3785 char sid_buf[25];
3786 int off = *s == 's' ? 2 : 5;
3787
3788 /* Expand s: and <SID> into <SNR>nr_, so that the function can
3789 * also be called from another script. Using trans_function_name()
3790 * would also work, but some plugins depend on the name being
3791 * printable text. */
3792 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
3793 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
3794 if (name != NULL)
3795 {
3796 STRCPY(name, sid_buf);
3797 STRCAT(name, s + off);
3798 }
3799 }
3800 else
3801 name = vim_strsave(s);
3802
3803 if (argvars[1].v_type != VAR_UNKNOWN)
3804 {
3805 if (argvars[2].v_type != VAR_UNKNOWN)
3806 {
3807 /* function(name, [args], dict) */
3808 arg_idx = 1;
3809 dict_idx = 2;
3810 }
3811 else if (argvars[1].v_type == VAR_DICT)
3812 /* function(name, dict) */
3813 dict_idx = 1;
3814 else
3815 /* function(name, [args]) */
3816 arg_idx = 1;
3817 if (dict_idx > 0)
3818 {
3819 if (argvars[dict_idx].v_type != VAR_DICT)
3820 {
3821 EMSG(_("E922: expected a dict"));
3822 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003823 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003824 }
3825 if (argvars[dict_idx].vval.v_dict == NULL)
3826 dict_idx = 0;
3827 }
3828 if (arg_idx > 0)
3829 {
3830 if (argvars[arg_idx].v_type != VAR_LIST)
3831 {
3832 EMSG(_("E923: Second argument of function() must be a list or a dict"));
3833 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003834 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003835 }
3836 list = argvars[arg_idx].vval.v_list;
3837 if (list == NULL || list->lv_len == 0)
3838 arg_idx = 0;
3839 }
3840 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003841 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003842 {
3843 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
3844
3845 /* result is a VAR_PARTIAL */
3846 if (pt == NULL)
3847 vim_free(name);
3848 else
3849 {
3850 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
3851 {
3852 listitem_T *li;
3853 int i = 0;
3854 int arg_len = 0;
3855 int lv_len = 0;
3856
3857 if (arg_pt != NULL)
3858 arg_len = arg_pt->pt_argc;
3859 if (list != NULL)
3860 lv_len = list->lv_len;
3861 pt->pt_argc = arg_len + lv_len;
3862 pt->pt_argv = (typval_T *)alloc(
3863 sizeof(typval_T) * pt->pt_argc);
3864 if (pt->pt_argv == NULL)
3865 {
3866 vim_free(pt);
3867 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003868 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003869 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003870 for (i = 0; i < arg_len; i++)
3871 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
3872 if (lv_len > 0)
3873 for (li = list->lv_first; li != NULL;
3874 li = li->li_next)
3875 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003876 }
3877
3878 /* For "function(dict.func, [], dict)" and "func" is a partial
3879 * use "dict". That is backwards compatible. */
3880 if (dict_idx > 0)
3881 {
3882 /* The dict is bound explicitly, pt_auto is FALSE. */
3883 pt->pt_dict = argvars[dict_idx].vval.v_dict;
3884 ++pt->pt_dict->dv_refcount;
3885 }
3886 else if (arg_pt != NULL)
3887 {
3888 /* If the dict was bound automatically the result is also
3889 * bound automatically. */
3890 pt->pt_dict = arg_pt->pt_dict;
3891 pt->pt_auto = arg_pt->pt_auto;
3892 if (pt->pt_dict != NULL)
3893 ++pt->pt_dict->dv_refcount;
3894 }
3895
3896 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003897 if (arg_pt != NULL && arg_pt->pt_func != NULL)
3898 {
3899 pt->pt_func = arg_pt->pt_func;
3900 func_ptr_ref(pt->pt_func);
3901 vim_free(name);
3902 }
3903 else if (is_funcref)
3904 {
3905 pt->pt_func = find_func(trans_name);
3906 func_ptr_ref(pt->pt_func);
3907 vim_free(name);
3908 }
3909 else
3910 {
3911 pt->pt_name = name;
3912 func_ref(name);
3913 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003914 }
3915 rettv->v_type = VAR_PARTIAL;
3916 rettv->vval.v_partial = pt;
3917 }
3918 else
3919 {
3920 /* result is a VAR_FUNC */
3921 rettv->v_type = VAR_FUNC;
3922 rettv->vval.v_string = name;
3923 func_ref(name);
3924 }
3925 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003926theend:
3927 vim_free(trans_name);
3928}
3929
3930/*
3931 * "funcref()" function
3932 */
3933 static void
3934f_funcref(typval_T *argvars, typval_T *rettv)
3935{
3936 common_function(argvars, rettv, TRUE);
3937}
3938
3939/*
3940 * "function()" function
3941 */
3942 static void
3943f_function(typval_T *argvars, typval_T *rettv)
3944{
3945 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003946}
3947
3948/*
3949 * "garbagecollect()" function
3950 */
3951 static void
3952f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
3953{
3954 /* This is postponed until we are back at the toplevel, because we may be
3955 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
3956 want_garbage_collect = TRUE;
3957
3958 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
3959 garbage_collect_at_exit = TRUE;
3960}
3961
3962/*
3963 * "get()" function
3964 */
3965 static void
3966f_get(typval_T *argvars, typval_T *rettv)
3967{
3968 listitem_T *li;
3969 list_T *l;
3970 dictitem_T *di;
3971 dict_T *d;
3972 typval_T *tv = NULL;
3973
3974 if (argvars[0].v_type == VAR_LIST)
3975 {
3976 if ((l = argvars[0].vval.v_list) != NULL)
3977 {
3978 int error = FALSE;
3979
3980 li = list_find(l, (long)get_tv_number_chk(&argvars[1], &error));
3981 if (!error && li != NULL)
3982 tv = &li->li_tv;
3983 }
3984 }
3985 else if (argvars[0].v_type == VAR_DICT)
3986 {
3987 if ((d = argvars[0].vval.v_dict) != NULL)
3988 {
3989 di = dict_find(d, get_tv_string(&argvars[1]), -1);
3990 if (di != NULL)
3991 tv = &di->di_tv;
3992 }
3993 }
3994 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
3995 {
3996 partial_T *pt;
3997 partial_T fref_pt;
3998
3999 if (argvars[0].v_type == VAR_PARTIAL)
4000 pt = argvars[0].vval.v_partial;
4001 else
4002 {
4003 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4004 fref_pt.pt_name = argvars[0].vval.v_string;
4005 pt = &fref_pt;
4006 }
4007
4008 if (pt != NULL)
4009 {
4010 char_u *what = get_tv_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004011 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004012
4013 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4014 {
4015 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004016 n = partial_name(pt);
4017 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004018 rettv->vval.v_string = NULL;
4019 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004020 {
4021 rettv->vval.v_string = vim_strsave(n);
4022 if (rettv->v_type == VAR_FUNC)
4023 func_ref(rettv->vval.v_string);
4024 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004025 }
4026 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004027 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004028 else if (STRCMP(what, "args") == 0)
4029 {
4030 rettv->v_type = VAR_LIST;
4031 if (rettv_list_alloc(rettv) == OK)
4032 {
4033 int i;
4034
4035 for (i = 0; i < pt->pt_argc; ++i)
4036 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4037 }
4038 }
4039 else
4040 EMSG2(_(e_invarg2), what);
4041 return;
4042 }
4043 }
4044 else
4045 EMSG2(_(e_listdictarg), "get()");
4046
4047 if (tv == NULL)
4048 {
4049 if (argvars[2].v_type != VAR_UNKNOWN)
4050 copy_tv(&argvars[2], rettv);
4051 }
4052 else
4053 copy_tv(tv, rettv);
4054}
4055
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004056#ifdef FEAT_SIGNS
4057/*
4058 * Returns information about signs placed in a buffer as list of dicts.
4059 */
4060 static void
4061get_buffer_signs(buf_T *buf, list_T *l)
4062{
4063 signlist_T *sign;
4064
4065 for (sign = buf->b_signlist; sign; sign = sign->next)
4066 {
4067 dict_T *d = dict_alloc();
4068
4069 if (d != NULL)
4070 {
4071 dict_add_nr_str(d, "id", sign->id, NULL);
4072 dict_add_nr_str(d, "lnum", sign->lnum, NULL);
Bram Moolenaar6a402ed2016-08-28 14:11:24 +02004073 dict_add_nr_str(d, "name", 0L, sign_typenr2name(sign->typenr));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004074
4075 list_append_dict(l, d);
4076 }
4077 }
4078}
4079#endif
4080
4081/*
4082 * Returns buffer options, variables and other attributes in a dictionary.
4083 */
4084 static dict_T *
4085get_buffer_info(buf_T *buf)
4086{
4087 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004088 tabpage_T *tp;
4089 win_T *wp;
4090 list_T *windows;
4091
4092 dict = dict_alloc();
4093 if (dict == NULL)
4094 return NULL;
4095
Bram Moolenaar33928832016-08-18 21:22:04 +02004096 dict_add_nr_str(dict, "bufnr", buf->b_fnum, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004097 dict_add_nr_str(dict, "name", 0L,
4098 buf->b_ffname != NULL ? buf->b_ffname : (char_u *)"");
Bram Moolenaarf845b872017-01-06 14:04:54 +01004099 dict_add_nr_str(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4100 : buflist_findlnum(buf), NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004101 dict_add_nr_str(dict, "loaded", buf->b_ml.ml_mfp != NULL, NULL);
4102 dict_add_nr_str(dict, "listed", buf->b_p_bl, NULL);
4103 dict_add_nr_str(dict, "changed", bufIsChanged(buf), NULL);
Bram Moolenaar95c526e2017-02-25 14:59:34 +01004104 dict_add_nr_str(dict, "changedtick", CHANGEDTICK(buf), NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004105 dict_add_nr_str(dict, "hidden",
4106 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0,
4107 NULL);
4108
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004109 /* Get a reference to buffer variables */
4110 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004111
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004112 /* List of windows displaying this buffer */
4113 windows = list_alloc();
4114 if (windows != NULL)
4115 {
4116 FOR_ALL_TAB_WINDOWS(tp, wp)
4117 if (wp->w_buffer == buf)
4118 list_append_number(windows, (varnumber_T)wp->w_id);
4119 dict_add_list(dict, "windows", windows);
4120 }
4121
4122#ifdef FEAT_SIGNS
4123 if (buf->b_signlist != NULL)
4124 {
4125 /* List of signs placed in this buffer */
4126 list_T *signs = list_alloc();
4127 if (signs != NULL)
4128 {
4129 get_buffer_signs(buf, signs);
4130 dict_add_list(dict, "signs", signs);
4131 }
4132 }
4133#endif
4134
4135 return dict;
4136}
4137
4138/*
4139 * "getbufinfo()" function
4140 */
4141 static void
4142f_getbufinfo(typval_T *argvars, typval_T *rettv)
4143{
4144 buf_T *buf = NULL;
4145 buf_T *argbuf = NULL;
4146 dict_T *d;
4147 int filtered = FALSE;
4148 int sel_buflisted = FALSE;
4149 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004150 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004151
4152 if (rettv_list_alloc(rettv) != OK)
4153 return;
4154
4155 /* List of all the buffers or selected buffers */
4156 if (argvars[0].v_type == VAR_DICT)
4157 {
4158 dict_T *sel_d = argvars[0].vval.v_dict;
4159
4160 if (sel_d != NULL)
4161 {
4162 dictitem_T *di;
4163
4164 filtered = TRUE;
4165
4166 di = dict_find(sel_d, (char_u *)"buflisted", -1);
4167 if (di != NULL && get_tv_number(&di->di_tv))
4168 sel_buflisted = TRUE;
4169
4170 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
4171 if (di != NULL && get_tv_number(&di->di_tv))
4172 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004173
4174 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
4175 if (di != NULL && get_tv_number(&di->di_tv))
4176 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004177 }
4178 }
4179 else if (argvars[0].v_type != VAR_UNKNOWN)
4180 {
4181 /* Information about one buffer. Argument specifies the buffer */
4182 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4183 ++emsg_off;
4184 argbuf = get_buf_tv(&argvars[0], FALSE);
4185 --emsg_off;
4186 if (argbuf == NULL)
4187 return;
4188 }
4189
4190 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004191 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004192 {
4193 if (argbuf != NULL && argbuf != buf)
4194 continue;
4195 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004196 || (sel_buflisted && !buf->b_p_bl)
4197 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004198 continue;
4199
4200 d = get_buffer_info(buf);
4201 if (d != NULL)
4202 list_append_dict(rettv->vval.v_list, d);
4203 if (argbuf != NULL)
4204 return;
4205 }
4206}
4207
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004208static void get_buffer_lines(buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv);
4209
4210/*
4211 * Get line or list of lines from buffer "buf" into "rettv".
4212 * Return a range (from start to end) of lines in rettv from the specified
4213 * buffer.
4214 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4215 */
4216 static void
4217get_buffer_lines(
4218 buf_T *buf,
4219 linenr_T start,
4220 linenr_T end,
4221 int retlist,
4222 typval_T *rettv)
4223{
4224 char_u *p;
4225
4226 rettv->v_type = VAR_STRING;
4227 rettv->vval.v_string = NULL;
4228 if (retlist && rettv_list_alloc(rettv) == FAIL)
4229 return;
4230
4231 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4232 return;
4233
4234 if (!retlist)
4235 {
4236 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4237 p = ml_get_buf(buf, start, FALSE);
4238 else
4239 p = (char_u *)"";
4240 rettv->vval.v_string = vim_strsave(p);
4241 }
4242 else
4243 {
4244 if (end < start)
4245 return;
4246
4247 if (start < 1)
4248 start = 1;
4249 if (end > buf->b_ml.ml_line_count)
4250 end = buf->b_ml.ml_line_count;
4251 while (start <= end)
4252 if (list_append_string(rettv->vval.v_list,
4253 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4254 break;
4255 }
4256}
4257
4258/*
4259 * Get the lnum from the first argument.
4260 * Also accepts "$", then "buf" is used.
4261 * Returns 0 on error.
4262 */
4263 static linenr_T
4264get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
4265{
4266 if (argvars[0].v_type == VAR_STRING
4267 && argvars[0].vval.v_string != NULL
4268 && argvars[0].vval.v_string[0] == '$'
4269 && buf != NULL)
4270 return buf->b_ml.ml_line_count;
4271 return (linenr_T)get_tv_number_chk(&argvars[0], NULL);
4272}
4273
4274/*
4275 * "getbufline()" function
4276 */
4277 static void
4278f_getbufline(typval_T *argvars, typval_T *rettv)
4279{
4280 linenr_T lnum;
4281 linenr_T end;
4282 buf_T *buf;
4283
4284 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4285 ++emsg_off;
4286 buf = get_buf_tv(&argvars[0], FALSE);
4287 --emsg_off;
4288
4289 lnum = get_tv_lnum_buf(&argvars[1], buf);
4290 if (argvars[2].v_type == VAR_UNKNOWN)
4291 end = lnum;
4292 else
4293 end = get_tv_lnum_buf(&argvars[2], buf);
4294
4295 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4296}
4297
4298/*
4299 * "getbufvar()" function
4300 */
4301 static void
4302f_getbufvar(typval_T *argvars, typval_T *rettv)
4303{
4304 buf_T *buf;
4305 buf_T *save_curbuf;
4306 char_u *varname;
4307 dictitem_T *v;
4308 int done = FALSE;
4309
4310 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4311 varname = get_tv_string_chk(&argvars[1]);
4312 ++emsg_off;
4313 buf = get_buf_tv(&argvars[0], FALSE);
4314
4315 rettv->v_type = VAR_STRING;
4316 rettv->vval.v_string = NULL;
4317
4318 if (buf != NULL && varname != NULL)
4319 {
4320 /* set curbuf to be our buf, temporarily */
4321 save_curbuf = curbuf;
4322 curbuf = buf;
4323
Bram Moolenaar30567352016-08-27 21:25:44 +02004324 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004325 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004326 if (varname[1] == NUL)
4327 {
4328 /* get all buffer-local options in a dict */
4329 dict_T *opts = get_winbuf_options(TRUE);
4330
4331 if (opts != NULL)
4332 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004333 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004334 done = TRUE;
4335 }
4336 }
4337 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4338 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004339 done = TRUE;
4340 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004341 else
4342 {
4343 /* Look up the variable. */
4344 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4345 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4346 'b', varname, FALSE);
4347 if (v != NULL)
4348 {
4349 copy_tv(&v->di_tv, rettv);
4350 done = TRUE;
4351 }
4352 }
4353
4354 /* restore previous notion of curbuf */
4355 curbuf = save_curbuf;
4356 }
4357
4358 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4359 /* use the default value */
4360 copy_tv(&argvars[2], rettv);
4361
4362 --emsg_off;
4363}
4364
4365/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004366 * "getchangelist()" function
4367 */
4368 static void
4369f_getchangelist(typval_T *argvars, typval_T *rettv)
4370{
4371#ifdef FEAT_JUMPLIST
4372 buf_T *buf;
4373 int i;
4374 list_T *l;
4375 dict_T *d;
4376#endif
4377
4378 if (rettv_list_alloc(rettv) != OK)
4379 return;
4380
4381#ifdef FEAT_JUMPLIST
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004382 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4383 ++emsg_off;
4384 buf = get_buf_tv(&argvars[0], FALSE);
4385 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004386 if (buf == NULL)
4387 return;
4388
4389 l = list_alloc();
4390 if (l == NULL)
4391 return;
4392
4393 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4394 return;
4395 /*
4396 * The current window change list index tracks only the position in the
4397 * current buffer change list. For other buffers, use the change list
4398 * length as the current index.
4399 */
4400 list_append_number(rettv->vval.v_list,
4401 (varnumber_T)((buf == curwin->w_buffer)
4402 ? curwin->w_changelistidx : buf->b_changelistlen));
4403
4404 for (i = 0; i < buf->b_changelistlen; ++i)
4405 {
4406 if (buf->b_changelist[i].lnum == 0)
4407 continue;
4408 if ((d = dict_alloc()) == NULL)
4409 return;
4410 if (list_append_dict(l, d) == FAIL)
4411 return;
4412 dict_add_nr_str(d, "lnum", (long)buf->b_changelist[i].lnum, NULL);
4413 dict_add_nr_str(d, "col", (long)buf->b_changelist[i].col, NULL);
4414# ifdef FEAT_VIRTUALEDIT
4415 dict_add_nr_str(d, "coladd", (long)buf->b_changelist[i].coladd, NULL);
4416# endif
4417 }
4418#endif
4419}
4420/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004421 * "getchar()" function
4422 */
4423 static void
4424f_getchar(typval_T *argvars, typval_T *rettv)
4425{
4426 varnumber_T n;
4427 int error = FALSE;
4428
4429 /* Position the cursor. Needed after a message that ends in a space. */
4430 windgoto(msg_row, msg_col);
4431
4432 ++no_mapping;
4433 ++allow_keys;
4434 for (;;)
4435 {
4436 if (argvars[0].v_type == VAR_UNKNOWN)
4437 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004438 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004439 else if (get_tv_number_chk(&argvars[0], &error) == 1)
4440 /* getchar(1): only check if char avail */
4441 n = vpeekc_any();
4442 else if (error || vpeekc_any() == NUL)
4443 /* illegal argument or getchar(0) and no char avail: return zero */
4444 n = 0;
4445 else
4446 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004447 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004448
4449 if (n == K_IGNORE)
4450 continue;
4451 break;
4452 }
4453 --no_mapping;
4454 --allow_keys;
4455
4456 set_vim_var_nr(VV_MOUSE_WIN, 0);
4457 set_vim_var_nr(VV_MOUSE_WINID, 0);
4458 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4459 set_vim_var_nr(VV_MOUSE_COL, 0);
4460
4461 rettv->vval.v_number = n;
4462 if (IS_SPECIAL(n) || mod_mask != 0)
4463 {
4464 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4465 int i = 0;
4466
4467 /* Turn a special key into three bytes, plus modifier. */
4468 if (mod_mask != 0)
4469 {
4470 temp[i++] = K_SPECIAL;
4471 temp[i++] = KS_MODIFIER;
4472 temp[i++] = mod_mask;
4473 }
4474 if (IS_SPECIAL(n))
4475 {
4476 temp[i++] = K_SPECIAL;
4477 temp[i++] = K_SECOND(n);
4478 temp[i++] = K_THIRD(n);
4479 }
4480#ifdef FEAT_MBYTE
4481 else if (has_mbyte)
4482 i += (*mb_char2bytes)(n, temp + i);
4483#endif
4484 else
4485 temp[i++] = n;
4486 temp[i++] = NUL;
4487 rettv->v_type = VAR_STRING;
4488 rettv->vval.v_string = vim_strsave(temp);
4489
4490#ifdef FEAT_MOUSE
4491 if (is_mouse_key(n))
4492 {
4493 int row = mouse_row;
4494 int col = mouse_col;
4495 win_T *win;
4496 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004497 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004498 int winnr = 1;
4499
4500 if (row >= 0 && col >= 0)
4501 {
4502 /* Find the window at the mouse coordinates and compute the
4503 * text position. */
4504 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004505 if (win == NULL)
4506 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004507 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004508 for (wp = firstwin; wp != win; wp = wp->w_next)
4509 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004510 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4511 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4512 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4513 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4514 }
4515 }
4516#endif
4517 }
4518}
4519
4520/*
4521 * "getcharmod()" function
4522 */
4523 static void
4524f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4525{
4526 rettv->vval.v_number = mod_mask;
4527}
4528
4529/*
4530 * "getcharsearch()" function
4531 */
4532 static void
4533f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4534{
4535 if (rettv_dict_alloc(rettv) != FAIL)
4536 {
4537 dict_T *dict = rettv->vval.v_dict;
4538
4539 dict_add_nr_str(dict, "char", 0L, last_csearch());
4540 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
4541 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
4542 }
4543}
4544
4545/*
4546 * "getcmdline()" function
4547 */
4548 static void
4549f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4550{
4551 rettv->v_type = VAR_STRING;
4552 rettv->vval.v_string = get_cmdline_str();
4553}
4554
4555/*
4556 * "getcmdpos()" function
4557 */
4558 static void
4559f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4560{
4561 rettv->vval.v_number = get_cmdline_pos() + 1;
4562}
4563
4564/*
4565 * "getcmdtype()" function
4566 */
4567 static void
4568f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4569{
4570 rettv->v_type = VAR_STRING;
4571 rettv->vval.v_string = alloc(2);
4572 if (rettv->vval.v_string != NULL)
4573 {
4574 rettv->vval.v_string[0] = get_cmdline_type();
4575 rettv->vval.v_string[1] = NUL;
4576 }
4577}
4578
4579/*
4580 * "getcmdwintype()" function
4581 */
4582 static void
4583f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4584{
4585 rettv->v_type = VAR_STRING;
4586 rettv->vval.v_string = NULL;
4587#ifdef FEAT_CMDWIN
4588 rettv->vval.v_string = alloc(2);
4589 if (rettv->vval.v_string != NULL)
4590 {
4591 rettv->vval.v_string[0] = cmdwin_type;
4592 rettv->vval.v_string[1] = NUL;
4593 }
4594#endif
4595}
4596
4597#if defined(FEAT_CMDL_COMPL)
4598/*
4599 * "getcompletion()" function
4600 */
4601 static void
4602f_getcompletion(typval_T *argvars, typval_T *rettv)
4603{
4604 char_u *pat;
4605 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004606 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004607 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
4608 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004609
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004610 if (argvars[2].v_type != VAR_UNKNOWN)
4611 filtered = get_tv_number_chk(&argvars[2], NULL);
4612
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004613 if (p_wic)
4614 options |= WILD_ICASE;
4615
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004616 /* For filtered results, 'wildignore' is used */
4617 if (!filtered)
4618 options |= WILD_KEEP_ALL;
4619
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004620 ExpandInit(&xpc);
4621 xpc.xp_pattern = get_tv_string(&argvars[0]);
4622 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4623 xpc.xp_context = cmdcomplete_str_to_type(get_tv_string(&argvars[1]));
4624 if (xpc.xp_context == EXPAND_NOTHING)
4625 {
4626 if (argvars[1].v_type == VAR_STRING)
4627 EMSG2(_(e_invarg2), argvars[1].vval.v_string);
4628 else
4629 EMSG(_(e_invarg));
4630 return;
4631 }
4632
4633# if defined(FEAT_MENU)
4634 if (xpc.xp_context == EXPAND_MENUS)
4635 {
4636 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4637 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4638 }
4639# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02004640#ifdef FEAT_CSCOPE
4641 if (xpc.xp_context == EXPAND_CSCOPE)
4642 {
4643 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4644 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4645 }
4646#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02004647#ifdef FEAT_SIGNS
4648 if (xpc.xp_context == EXPAND_SIGN)
4649 {
4650 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4651 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4652 }
4653#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004654
4655 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4656 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
4657 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004658 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004659
4660 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4661
4662 for (i = 0; i < xpc.xp_numfiles; i++)
4663 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4664 }
4665 vim_free(pat);
4666 ExpandCleanup(&xpc);
4667}
4668#endif
4669
4670/*
4671 * "getcwd()" function
4672 */
4673 static void
4674f_getcwd(typval_T *argvars, typval_T *rettv)
4675{
4676 win_T *wp = NULL;
4677 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01004678 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004679
4680 rettv->v_type = VAR_STRING;
4681 rettv->vval.v_string = NULL;
4682
Bram Moolenaar54591292018-02-09 20:53:59 +01004683 if (argvars[0].v_type == VAR_NUMBER && argvars[0].vval.v_number == -1)
4684 global = TRUE;
4685 else
4686 wp = find_tabwin(&argvars[0], &argvars[1]);
4687
4688 if (wp != NULL && wp->w_localdir != NULL)
4689 rettv->vval.v_string = vim_strsave(wp->w_localdir);
4690 else if (wp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004691 {
Bram Moolenaar54591292018-02-09 20:53:59 +01004692 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004693 rettv->vval.v_string = vim_strsave(globaldir);
4694 else
4695 {
4696 cwd = alloc(MAXPATHL);
4697 if (cwd != NULL)
4698 {
4699 if (mch_dirname(cwd, MAXPATHL) != FAIL)
4700 rettv->vval.v_string = vim_strsave(cwd);
4701 vim_free(cwd);
4702 }
4703 }
4704#ifdef BACKSLASH_IN_FILENAME
4705 if (rettv->vval.v_string != NULL)
4706 slash_adjust(rettv->vval.v_string);
4707#endif
4708 }
4709}
4710
4711/*
4712 * "getfontname()" function
4713 */
4714 static void
4715f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4716{
4717 rettv->v_type = VAR_STRING;
4718 rettv->vval.v_string = NULL;
4719#ifdef FEAT_GUI
4720 if (gui.in_use)
4721 {
4722 GuiFont font;
4723 char_u *name = NULL;
4724
4725 if (argvars[0].v_type == VAR_UNKNOWN)
4726 {
4727 /* Get the "Normal" font. Either the name saved by
4728 * hl_set_font_name() or from the font ID. */
4729 font = gui.norm_font;
4730 name = hl_get_font_name();
4731 }
4732 else
4733 {
4734 name = get_tv_string(&argvars[0]);
4735 if (STRCMP(name, "*") == 0) /* don't use font dialog */
4736 return;
4737 font = gui_mch_get_font(name, FALSE);
4738 if (font == NOFONT)
4739 return; /* Invalid font name, return empty string. */
4740 }
4741 rettv->vval.v_string = gui_mch_get_fontname(font, name);
4742 if (argvars[0].v_type != VAR_UNKNOWN)
4743 gui_mch_free_font(font);
4744 }
4745#endif
4746}
4747
4748/*
4749 * "getfperm({fname})" function
4750 */
4751 static void
4752f_getfperm(typval_T *argvars, typval_T *rettv)
4753{
4754 char_u *fname;
4755 stat_T st;
4756 char_u *perm = NULL;
4757 char_u flags[] = "rwx";
4758 int i;
4759
4760 fname = get_tv_string(&argvars[0]);
4761
4762 rettv->v_type = VAR_STRING;
4763 if (mch_stat((char *)fname, &st) >= 0)
4764 {
4765 perm = vim_strsave((char_u *)"---------");
4766 if (perm != NULL)
4767 {
4768 for (i = 0; i < 9; i++)
4769 {
4770 if (st.st_mode & (1 << (8 - i)))
4771 perm[i] = flags[i % 3];
4772 }
4773 }
4774 }
4775 rettv->vval.v_string = perm;
4776}
4777
4778/*
4779 * "getfsize({fname})" function
4780 */
4781 static void
4782f_getfsize(typval_T *argvars, typval_T *rettv)
4783{
4784 char_u *fname;
4785 stat_T st;
4786
4787 fname = get_tv_string(&argvars[0]);
4788
4789 rettv->v_type = VAR_NUMBER;
4790
4791 if (mch_stat((char *)fname, &st) >= 0)
4792 {
4793 if (mch_isdir(fname))
4794 rettv->vval.v_number = 0;
4795 else
4796 {
4797 rettv->vval.v_number = (varnumber_T)st.st_size;
4798
4799 /* non-perfect check for overflow */
4800 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
4801 rettv->vval.v_number = -2;
4802 }
4803 }
4804 else
4805 rettv->vval.v_number = -1;
4806}
4807
4808/*
4809 * "getftime({fname})" function
4810 */
4811 static void
4812f_getftime(typval_T *argvars, typval_T *rettv)
4813{
4814 char_u *fname;
4815 stat_T st;
4816
4817 fname = get_tv_string(&argvars[0]);
4818
4819 if (mch_stat((char *)fname, &st) >= 0)
4820 rettv->vval.v_number = (varnumber_T)st.st_mtime;
4821 else
4822 rettv->vval.v_number = -1;
4823}
4824
4825/*
4826 * "getftype({fname})" function
4827 */
4828 static void
4829f_getftype(typval_T *argvars, typval_T *rettv)
4830{
4831 char_u *fname;
4832 stat_T st;
4833 char_u *type = NULL;
4834 char *t;
4835
4836 fname = get_tv_string(&argvars[0]);
4837
4838 rettv->v_type = VAR_STRING;
4839 if (mch_lstat((char *)fname, &st) >= 0)
4840 {
4841#ifdef S_ISREG
4842 if (S_ISREG(st.st_mode))
4843 t = "file";
4844 else if (S_ISDIR(st.st_mode))
4845 t = "dir";
4846# ifdef S_ISLNK
4847 else if (S_ISLNK(st.st_mode))
4848 t = "link";
4849# endif
4850# ifdef S_ISBLK
4851 else if (S_ISBLK(st.st_mode))
4852 t = "bdev";
4853# endif
4854# ifdef S_ISCHR
4855 else if (S_ISCHR(st.st_mode))
4856 t = "cdev";
4857# endif
4858# ifdef S_ISFIFO
4859 else if (S_ISFIFO(st.st_mode))
4860 t = "fifo";
4861# endif
4862# ifdef S_ISSOCK
4863 else if (S_ISSOCK(st.st_mode))
4864 t = "fifo";
4865# endif
4866 else
4867 t = "other";
4868#else
4869# ifdef S_IFMT
4870 switch (st.st_mode & S_IFMT)
4871 {
4872 case S_IFREG: t = "file"; break;
4873 case S_IFDIR: t = "dir"; break;
4874# ifdef S_IFLNK
4875 case S_IFLNK: t = "link"; break;
4876# endif
4877# ifdef S_IFBLK
4878 case S_IFBLK: t = "bdev"; break;
4879# endif
4880# ifdef S_IFCHR
4881 case S_IFCHR: t = "cdev"; break;
4882# endif
4883# ifdef S_IFIFO
4884 case S_IFIFO: t = "fifo"; break;
4885# endif
4886# ifdef S_IFSOCK
4887 case S_IFSOCK: t = "socket"; break;
4888# endif
4889 default: t = "other";
4890 }
4891# else
4892 if (mch_isdir(fname))
4893 t = "dir";
4894 else
4895 t = "file";
4896# endif
4897#endif
4898 type = vim_strsave((char_u *)t);
4899 }
4900 rettv->vval.v_string = type;
4901}
4902
4903/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01004904 * "getjumplist()" function
4905 */
4906 static void
4907f_getjumplist(typval_T *argvars, typval_T *rettv)
4908{
4909#ifdef FEAT_JUMPLIST
4910 win_T *wp;
4911 int i;
4912 list_T *l;
4913 dict_T *d;
4914#endif
4915
4916 if (rettv_list_alloc(rettv) != OK)
4917 return;
4918
4919#ifdef FEAT_JUMPLIST
4920 wp = find_tabwin(&argvars[0], &argvars[1]);
4921 if (wp == NULL)
4922 return;
4923
4924 l = list_alloc();
4925 if (l == NULL)
4926 return;
4927
4928 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4929 return;
4930 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
4931
Bram Moolenaar48679742018-02-13 13:33:29 +01004932 cleanup_jumplist(wp, TRUE);
4933
Bram Moolenaar4f505882018-02-10 21:06:32 +01004934 for (i = 0; i < wp->w_jumplistlen; ++i)
4935 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004936 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
4937 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01004938 if ((d = dict_alloc()) == NULL)
4939 return;
4940 if (list_append_dict(l, d) == FAIL)
4941 return;
4942 dict_add_nr_str(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum,
4943 NULL);
4944 dict_add_nr_str(d, "col", (long)wp->w_jumplist[i].fmark.mark.col,
4945 NULL);
4946# ifdef FEAT_VIRTUALEDIT
4947 dict_add_nr_str(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd,
4948 NULL);
4949# endif
4950 dict_add_nr_str(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum, NULL);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004951 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaar4f505882018-02-10 21:06:32 +01004952 dict_add_nr_str(d, "filename", 0L, wp->w_jumplist[i].fname);
4953 }
4954#endif
4955}
4956
4957/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004958 * "getline(lnum, [end])" function
4959 */
4960 static void
4961f_getline(typval_T *argvars, typval_T *rettv)
4962{
4963 linenr_T lnum;
4964 linenr_T end;
4965 int retlist;
4966
4967 lnum = get_tv_lnum(argvars);
4968 if (argvars[1].v_type == VAR_UNKNOWN)
4969 {
4970 end = 0;
4971 retlist = FALSE;
4972 }
4973 else
4974 {
4975 end = get_tv_lnum(&argvars[1]);
4976 retlist = TRUE;
4977 }
4978
4979 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
4980}
4981
Bram Moolenaar4ae20952016-08-13 15:29:14 +02004982#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02004983 static void
4984get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
4985{
Bram Moolenaard823fa92016-08-12 16:29:27 +02004986 if (what_arg->v_type == VAR_UNKNOWN)
4987 {
4988 if (rettv_list_alloc(rettv) == OK)
4989 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004990 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004991 }
4992 else
4993 {
4994 if (rettv_dict_alloc(rettv) == OK)
4995 if (is_qf || (wp != NULL))
4996 {
4997 if (what_arg->v_type == VAR_DICT)
4998 {
4999 dict_T *d = what_arg->vval.v_dict;
5000
5001 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005002 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005003 }
5004 else
5005 EMSG(_(e_dictreq));
5006 }
5007 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005008}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005009#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005010
5011/*
5012 * "getloclist()" function
5013 */
5014 static void
5015f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5016{
5017#ifdef FEAT_QUICKFIX
5018 win_T *wp;
5019
5020 wp = find_win_by_nr(&argvars[0], NULL);
5021 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5022#endif
5023}
5024
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005025/*
5026 * "getmatches()" function
5027 */
5028 static void
5029f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5030{
5031#ifdef FEAT_SEARCH_EXTRA
5032 dict_T *dict;
5033 matchitem_T *cur = curwin->w_match_head;
5034 int i;
5035
5036 if (rettv_list_alloc(rettv) == OK)
5037 {
5038 while (cur != NULL)
5039 {
5040 dict = dict_alloc();
5041 if (dict == NULL)
5042 return;
5043 if (cur->match.regprog == NULL)
5044 {
5045 /* match added with matchaddpos() */
5046 for (i = 0; i < MAXPOSMATCH; ++i)
5047 {
5048 llpos_T *llpos;
5049 char buf[6];
5050 list_T *l;
5051
5052 llpos = &cur->pos.pos[i];
5053 if (llpos->lnum == 0)
5054 break;
5055 l = list_alloc();
5056 if (l == NULL)
5057 break;
5058 list_append_number(l, (varnumber_T)llpos->lnum);
5059 if (llpos->col > 0)
5060 {
5061 list_append_number(l, (varnumber_T)llpos->col);
5062 list_append_number(l, (varnumber_T)llpos->len);
5063 }
5064 sprintf(buf, "pos%d", i + 1);
5065 dict_add_list(dict, buf, l);
5066 }
5067 }
5068 else
5069 {
5070 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
5071 }
5072 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
5073 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
5074 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
5075# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
5076 if (cur->conceal_char)
5077 {
5078 char_u buf[MB_MAXBYTES + 1];
5079
5080 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
5081 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
5082 }
5083# endif
5084 list_append_dict(rettv->vval.v_list, dict);
5085 cur = cur->next;
5086 }
5087 }
5088#endif
5089}
5090
5091/*
5092 * "getpid()" function
5093 */
5094 static void
5095f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5096{
5097 rettv->vval.v_number = mch_get_pid();
5098}
5099
5100 static void
5101getpos_both(
5102 typval_T *argvars,
5103 typval_T *rettv,
5104 int getcurpos)
5105{
5106 pos_T *fp;
5107 list_T *l;
5108 int fnum = -1;
5109
5110 if (rettv_list_alloc(rettv) == OK)
5111 {
5112 l = rettv->vval.v_list;
5113 if (getcurpos)
5114 fp = &curwin->w_cursor;
5115 else
5116 fp = var2fpos(&argvars[0], TRUE, &fnum);
5117 if (fnum != -1)
5118 list_append_number(l, (varnumber_T)fnum);
5119 else
5120 list_append_number(l, (varnumber_T)0);
5121 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5122 : (varnumber_T)0);
5123 list_append_number(l, (fp != NULL)
5124 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5125 : (varnumber_T)0);
5126 list_append_number(l,
5127#ifdef FEAT_VIRTUALEDIT
5128 (fp != NULL) ? (varnumber_T)fp->coladd :
5129#endif
5130 (varnumber_T)0);
5131 if (getcurpos)
5132 {
5133 update_curswant();
5134 list_append_number(l, curwin->w_curswant == MAXCOL ?
5135 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
5136 }
5137 }
5138 else
5139 rettv->vval.v_number = FALSE;
5140}
5141
5142
5143/*
5144 * "getcurpos()" function
5145 */
5146 static void
5147f_getcurpos(typval_T *argvars, typval_T *rettv)
5148{
5149 getpos_both(argvars, rettv, TRUE);
5150}
5151
5152/*
5153 * "getpos(string)" function
5154 */
5155 static void
5156f_getpos(typval_T *argvars, typval_T *rettv)
5157{
5158 getpos_both(argvars, rettv, FALSE);
5159}
5160
5161/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005162 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005163 */
5164 static void
5165f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5166{
5167#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005168 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005169#endif
5170}
5171
5172/*
5173 * "getreg()" function
5174 */
5175 static void
5176f_getreg(typval_T *argvars, typval_T *rettv)
5177{
5178 char_u *strregname;
5179 int regname;
5180 int arg2 = FALSE;
5181 int return_list = FALSE;
5182 int error = FALSE;
5183
5184 if (argvars[0].v_type != VAR_UNKNOWN)
5185 {
5186 strregname = get_tv_string_chk(&argvars[0]);
5187 error = strregname == NULL;
5188 if (argvars[1].v_type != VAR_UNKNOWN)
5189 {
5190 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
5191 if (!error && argvars[2].v_type != VAR_UNKNOWN)
5192 return_list = (int)get_tv_number_chk(&argvars[2], &error);
5193 }
5194 }
5195 else
5196 strregname = get_vim_var_str(VV_REG);
5197
5198 if (error)
5199 return;
5200
5201 regname = (strregname == NULL ? '"' : *strregname);
5202 if (regname == 0)
5203 regname = '"';
5204
5205 if (return_list)
5206 {
5207 rettv->v_type = VAR_LIST;
5208 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5209 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5210 if (rettv->vval.v_list == NULL)
5211 (void)rettv_list_alloc(rettv);
5212 else
5213 ++rettv->vval.v_list->lv_refcount;
5214 }
5215 else
5216 {
5217 rettv->v_type = VAR_STRING;
5218 rettv->vval.v_string = get_reg_contents(regname,
5219 arg2 ? GREG_EXPR_SRC : 0);
5220 }
5221}
5222
5223/*
5224 * "getregtype()" function
5225 */
5226 static void
5227f_getregtype(typval_T *argvars, typval_T *rettv)
5228{
5229 char_u *strregname;
5230 int regname;
5231 char_u buf[NUMBUFLEN + 2];
5232 long reglen = 0;
5233
5234 if (argvars[0].v_type != VAR_UNKNOWN)
5235 {
5236 strregname = get_tv_string_chk(&argvars[0]);
5237 if (strregname == NULL) /* type error; errmsg already given */
5238 {
5239 rettv->v_type = VAR_STRING;
5240 rettv->vval.v_string = NULL;
5241 return;
5242 }
5243 }
5244 else
5245 /* Default to v:register */
5246 strregname = get_vim_var_str(VV_REG);
5247
5248 regname = (strregname == NULL ? '"' : *strregname);
5249 if (regname == 0)
5250 regname = '"';
5251
5252 buf[0] = NUL;
5253 buf[1] = NUL;
5254 switch (get_reg_type(regname, &reglen))
5255 {
5256 case MLINE: buf[0] = 'V'; break;
5257 case MCHAR: buf[0] = 'v'; break;
5258 case MBLOCK:
5259 buf[0] = Ctrl_V;
5260 sprintf((char *)buf + 1, "%ld", reglen + 1);
5261 break;
5262 }
5263 rettv->v_type = VAR_STRING;
5264 rettv->vval.v_string = vim_strsave(buf);
5265}
5266
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005267/*
5268 * Returns information (variables, options, etc.) about a tab page
5269 * as a dictionary.
5270 */
5271 static dict_T *
5272get_tabpage_info(tabpage_T *tp, int tp_idx)
5273{
5274 win_T *wp;
5275 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005276 list_T *l;
5277
5278 dict = dict_alloc();
5279 if (dict == NULL)
5280 return NULL;
5281
Bram Moolenaar33928832016-08-18 21:22:04 +02005282 dict_add_nr_str(dict, "tabnr", tp_idx, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005283
5284 l = list_alloc();
5285 if (l != NULL)
5286 {
5287 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5288 wp; wp = wp->w_next)
5289 list_append_number(l, (varnumber_T)wp->w_id);
5290 dict_add_list(dict, "windows", l);
5291 }
5292
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005293 /* Make a reference to tabpage variables */
5294 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005295
5296 return dict;
5297}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005298
5299/*
5300 * "gettabinfo()" function
5301 */
5302 static void
5303f_gettabinfo(typval_T *argvars, typval_T *rettv)
5304{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005305 tabpage_T *tp, *tparg = NULL;
5306 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005307 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005308
5309 if (rettv_list_alloc(rettv) != OK)
5310 return;
5311
5312 if (argvars[0].v_type != VAR_UNKNOWN)
5313 {
5314 /* Information about one tab page */
5315 tparg = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5316 if (tparg == NULL)
5317 return;
5318 }
5319
5320 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005321 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005322 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005323 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005324 if (tparg != NULL && tp != tparg)
5325 continue;
5326 d = get_tabpage_info(tp, tpnr);
5327 if (d != NULL)
5328 list_append_dict(rettv->vval.v_list, d);
5329 if (tparg != NULL)
5330 return;
5331 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005332}
5333
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005334/*
5335 * "gettabvar()" function
5336 */
5337 static void
5338f_gettabvar(typval_T *argvars, typval_T *rettv)
5339{
5340 win_T *oldcurwin;
5341 tabpage_T *tp, *oldtabpage;
5342 dictitem_T *v;
5343 char_u *varname;
5344 int done = FALSE;
5345
5346 rettv->v_type = VAR_STRING;
5347 rettv->vval.v_string = NULL;
5348
5349 varname = get_tv_string_chk(&argvars[1]);
5350 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5351 if (tp != NULL && varname != NULL)
5352 {
5353 /* Set tp to be our tabpage, temporarily. Also set the window to the
5354 * first window in the tabpage, otherwise the window is not valid. */
5355 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005356 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5357 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005358 {
5359 /* look up the variable */
5360 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5361 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5362 if (v != NULL)
5363 {
5364 copy_tv(&v->di_tv, rettv);
5365 done = TRUE;
5366 }
5367 }
5368
5369 /* restore previous notion of curwin */
5370 restore_win(oldcurwin, oldtabpage, TRUE);
5371 }
5372
5373 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5374 copy_tv(&argvars[2], rettv);
5375}
5376
5377/*
5378 * "gettabwinvar()" function
5379 */
5380 static void
5381f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5382{
5383 getwinvar(argvars, rettv, 1);
5384}
5385
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005386/*
5387 * Returns information about a window as a dictionary.
5388 */
5389 static dict_T *
5390get_win_info(win_T *wp, short tpnr, short winnr)
5391{
5392 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005393
5394 dict = dict_alloc();
5395 if (dict == NULL)
5396 return NULL;
5397
Bram Moolenaar33928832016-08-18 21:22:04 +02005398 dict_add_nr_str(dict, "tabnr", tpnr, NULL);
5399 dict_add_nr_str(dict, "winnr", winnr, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005400 dict_add_nr_str(dict, "winid", wp->w_id, NULL);
5401 dict_add_nr_str(dict, "height", wp->w_height, NULL);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005402#ifdef FEAT_MENU
5403 dict_add_nr_str(dict, "winbar", wp->w_winbar_height, NULL);
5404#endif
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005405 dict_add_nr_str(dict, "width", wp->w_width, NULL);
Bram Moolenaar33928832016-08-18 21:22:04 +02005406 dict_add_nr_str(dict, "bufnr", wp->w_buffer->b_fnum, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005407
Bram Moolenaar69905d12017-08-13 18:14:47 +02005408#ifdef FEAT_TERMINAL
5409 dict_add_nr_str(dict, "terminal", bt_terminal(wp->w_buffer), NULL);
5410#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005411#ifdef FEAT_QUICKFIX
5412 dict_add_nr_str(dict, "quickfix", bt_quickfix(wp->w_buffer), NULL);
5413 dict_add_nr_str(dict, "loclist",
5414 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL), NULL);
5415#endif
5416
Bram Moolenaar30567352016-08-27 21:25:44 +02005417 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005418 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005419
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005420 return dict;
5421}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005422
5423/*
5424 * "getwininfo()" function
5425 */
5426 static void
5427f_getwininfo(typval_T *argvars, typval_T *rettv)
5428{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005429 tabpage_T *tp;
5430 win_T *wp = NULL, *wparg = NULL;
5431 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005432 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005433
5434 if (rettv_list_alloc(rettv) != OK)
5435 return;
5436
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005437 if (argvars[0].v_type != VAR_UNKNOWN)
5438 {
5439 wparg = win_id2wp(argvars);
5440 if (wparg == NULL)
5441 return;
5442 }
5443
5444 /* Collect information about either all the windows across all the tab
5445 * pages or one particular window.
5446 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005447 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005448 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005449 tabnr++;
5450 winnr = 0;
5451 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005452 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005453 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005454 if (wparg != NULL && wp != wparg)
5455 continue;
5456 d = get_win_info(wp, tabnr, winnr);
5457 if (d != NULL)
5458 list_append_dict(rettv->vval.v_list, d);
5459 if (wparg != NULL)
5460 /* found information about a specific window */
5461 return;
5462 }
5463 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005464}
5465
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005466/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005467 * "win_findbuf()" function
5468 */
5469 static void
5470f_win_findbuf(typval_T *argvars, typval_T *rettv)
5471{
5472 if (rettv_list_alloc(rettv) != FAIL)
5473 win_findbuf(argvars, rettv->vval.v_list);
5474}
5475
5476/*
5477 * "win_getid()" function
5478 */
5479 static void
5480f_win_getid(typval_T *argvars, typval_T *rettv)
5481{
5482 rettv->vval.v_number = win_getid(argvars);
5483}
5484
5485/*
5486 * "win_gotoid()" function
5487 */
5488 static void
5489f_win_gotoid(typval_T *argvars, typval_T *rettv)
5490{
5491 rettv->vval.v_number = win_gotoid(argvars);
5492}
5493
5494/*
5495 * "win_id2tabwin()" function
5496 */
5497 static void
5498f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5499{
5500 if (rettv_list_alloc(rettv) != FAIL)
5501 win_id2tabwin(argvars, rettv->vval.v_list);
5502}
5503
5504/*
5505 * "win_id2win()" function
5506 */
5507 static void
5508f_win_id2win(typval_T *argvars, typval_T *rettv)
5509{
5510 rettv->vval.v_number = win_id2win(argvars);
5511}
5512
5513/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005514 * "win_screenpos()" function
5515 */
5516 static void
5517f_win_screenpos(typval_T *argvars, typval_T *rettv)
5518{
5519 win_T *wp;
5520
5521 if (rettv_list_alloc(rettv) == FAIL)
5522 return;
5523
5524 wp = find_win_by_nr(&argvars[0], NULL);
5525 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5526 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5527}
5528
5529/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005530 * "getwinpos({timeout})" function
5531 */
5532 static void
5533f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
5534{
5535 int x = -1;
5536 int y = -1;
5537
5538 if (rettv_list_alloc(rettv) == FAIL)
5539 return;
5540#ifdef FEAT_GUI
5541 if (gui.in_use)
5542 gui_mch_get_winpos(&x, &y);
5543# if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5544 else
5545# endif
5546#endif
5547#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5548 {
5549 varnumber_T timeout = 100;
5550
5551 if (argvars[0].v_type != VAR_UNKNOWN)
5552 timeout = get_tv_number(&argvars[0]);
5553 term_get_winpos(&x, &y, timeout);
5554 }
5555#endif
5556 list_append_number(rettv->vval.v_list, (varnumber_T)x);
5557 list_append_number(rettv->vval.v_list, (varnumber_T)y);
5558}
5559
5560
5561/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005562 * "getwinposx()" function
5563 */
5564 static void
5565f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5566{
5567 rettv->vval.v_number = -1;
5568#ifdef FEAT_GUI
5569 if (gui.in_use)
5570 {
5571 int x, y;
5572
5573 if (gui_mch_get_winpos(&x, &y) == OK)
5574 rettv->vval.v_number = x;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005575 return;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005576 }
5577#endif
5578#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5579 {
5580 int x, y;
5581
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005582 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005583 rettv->vval.v_number = x;
5584 }
5585#endif
5586}
5587
5588/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005589 * "getwinposy()" function
5590 */
5591 static void
5592f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5593{
5594 rettv->vval.v_number = -1;
5595#ifdef FEAT_GUI
5596 if (gui.in_use)
5597 {
5598 int x, y;
5599
5600 if (gui_mch_get_winpos(&x, &y) == OK)
5601 rettv->vval.v_number = y;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005602 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005603 }
5604#endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005605#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5606 {
5607 int x, y;
5608
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005609 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005610 rettv->vval.v_number = y;
5611 }
5612#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005613}
5614
5615/*
5616 * "getwinvar()" function
5617 */
5618 static void
5619f_getwinvar(typval_T *argvars, typval_T *rettv)
5620{
5621 getwinvar(argvars, rettv, 0);
5622}
5623
5624/*
5625 * "glob()" function
5626 */
5627 static void
5628f_glob(typval_T *argvars, typval_T *rettv)
5629{
5630 int options = WILD_SILENT|WILD_USE_NL;
5631 expand_T xpc;
5632 int error = FALSE;
5633
5634 /* When the optional second argument is non-zero, don't remove matches
5635 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5636 rettv->v_type = VAR_STRING;
5637 if (argvars[1].v_type != VAR_UNKNOWN)
5638 {
5639 if (get_tv_number_chk(&argvars[1], &error))
5640 options |= WILD_KEEP_ALL;
5641 if (argvars[2].v_type != VAR_UNKNOWN)
5642 {
5643 if (get_tv_number_chk(&argvars[2], &error))
5644 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005645 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005646 }
5647 if (argvars[3].v_type != VAR_UNKNOWN
5648 && get_tv_number_chk(&argvars[3], &error))
5649 options |= WILD_ALLLINKS;
5650 }
5651 }
5652 if (!error)
5653 {
5654 ExpandInit(&xpc);
5655 xpc.xp_context = EXPAND_FILES;
5656 if (p_wic)
5657 options += WILD_ICASE;
5658 if (rettv->v_type == VAR_STRING)
5659 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
5660 NULL, options, WILD_ALL);
5661 else if (rettv_list_alloc(rettv) != FAIL)
5662 {
5663 int i;
5664
5665 ExpandOne(&xpc, get_tv_string(&argvars[0]),
5666 NULL, options, WILD_ALL_KEEP);
5667 for (i = 0; i < xpc.xp_numfiles; i++)
5668 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5669
5670 ExpandCleanup(&xpc);
5671 }
5672 }
5673 else
5674 rettv->vval.v_string = NULL;
5675}
5676
5677/*
5678 * "globpath()" function
5679 */
5680 static void
5681f_globpath(typval_T *argvars, typval_T *rettv)
5682{
5683 int flags = 0;
5684 char_u buf1[NUMBUFLEN];
5685 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
5686 int error = FALSE;
5687 garray_T ga;
5688 int i;
5689
5690 /* When the optional second argument is non-zero, don't remove matches
5691 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5692 rettv->v_type = VAR_STRING;
5693 if (argvars[2].v_type != VAR_UNKNOWN)
5694 {
5695 if (get_tv_number_chk(&argvars[2], &error))
5696 flags |= WILD_KEEP_ALL;
5697 if (argvars[3].v_type != VAR_UNKNOWN)
5698 {
5699 if (get_tv_number_chk(&argvars[3], &error))
5700 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005701 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005702 }
5703 if (argvars[4].v_type != VAR_UNKNOWN
5704 && get_tv_number_chk(&argvars[4], &error))
5705 flags |= WILD_ALLLINKS;
5706 }
5707 }
5708 if (file != NULL && !error)
5709 {
5710 ga_init2(&ga, (int)sizeof(char_u *), 10);
5711 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
5712 if (rettv->v_type == VAR_STRING)
5713 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
5714 else if (rettv_list_alloc(rettv) != FAIL)
5715 for (i = 0; i < ga.ga_len; ++i)
5716 list_append_string(rettv->vval.v_list,
5717 ((char_u **)(ga.ga_data))[i], -1);
5718 ga_clear_strings(&ga);
5719 }
5720 else
5721 rettv->vval.v_string = NULL;
5722}
5723
5724/*
5725 * "glob2regpat()" function
5726 */
5727 static void
5728f_glob2regpat(typval_T *argvars, typval_T *rettv)
5729{
5730 char_u *pat = get_tv_string_chk(&argvars[0]);
5731
5732 rettv->v_type = VAR_STRING;
5733 rettv->vval.v_string = (pat == NULL)
5734 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
5735}
5736
5737/* for VIM_VERSION_ defines */
5738#include "version.h"
5739
5740/*
5741 * "has()" function
5742 */
5743 static void
5744f_has(typval_T *argvars, typval_T *rettv)
5745{
5746 int i;
5747 char_u *name;
5748 int n = FALSE;
5749 static char *(has_list[]) =
5750 {
5751#ifdef AMIGA
5752 "amiga",
5753# ifdef FEAT_ARP
5754 "arp",
5755# endif
5756#endif
5757#ifdef __BEOS__
5758 "beos",
5759#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02005760#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01005761 "mac", /* Mac OS X (and, once, Mac OS Classic) */
5762 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02005763# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01005764 "macunix", /* Mac OS X, with the darwin feature */
5765 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02005766# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005767#endif
5768#ifdef __QNX__
5769 "qnx",
5770#endif
5771#ifdef UNIX
5772 "unix",
5773#endif
5774#ifdef VMS
5775 "vms",
5776#endif
5777#ifdef WIN32
5778 "win32",
5779#endif
5780#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
5781 "win32unix",
5782#endif
5783#if defined(WIN64) || defined(_WIN64)
5784 "win64",
5785#endif
5786#ifdef EBCDIC
5787 "ebcdic",
5788#endif
5789#ifndef CASE_INSENSITIVE_FILENAME
5790 "fname_case",
5791#endif
5792#ifdef HAVE_ACL
5793 "acl",
5794#endif
5795#ifdef FEAT_ARABIC
5796 "arabic",
5797#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005798 "autocmd",
Bram Moolenaare42a6d22017-11-12 19:21:51 +01005799#ifdef FEAT_AUTOSERVERNAME
5800 "autoservername",
5801#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01005802#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005803 "balloon_eval",
5804# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
5805 "balloon_multiline",
5806# endif
5807#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01005808#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01005809 "balloon_eval_term",
5810#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005811#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
5812 "builtin_terms",
5813# ifdef ALL_BUILTIN_TCAPS
5814 "all_builtin_terms",
5815# endif
5816#endif
5817#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
5818 || defined(FEAT_GUI_W32) \
5819 || defined(FEAT_GUI_MOTIF))
5820 "browsefilter",
5821#endif
5822#ifdef FEAT_BYTEOFF
5823 "byte_offset",
5824#endif
5825#ifdef FEAT_JOB_CHANNEL
5826 "channel",
5827#endif
5828#ifdef FEAT_CINDENT
5829 "cindent",
5830#endif
5831#ifdef FEAT_CLIENTSERVER
5832 "clientserver",
5833#endif
5834#ifdef FEAT_CLIPBOARD
5835 "clipboard",
5836#endif
5837#ifdef FEAT_CMDL_COMPL
5838 "cmdline_compl",
5839#endif
5840#ifdef FEAT_CMDHIST
5841 "cmdline_hist",
5842#endif
5843#ifdef FEAT_COMMENTS
5844 "comments",
5845#endif
5846#ifdef FEAT_CONCEAL
5847 "conceal",
5848#endif
5849#ifdef FEAT_CRYPT
5850 "cryptv",
5851 "crypt-blowfish",
5852 "crypt-blowfish2",
5853#endif
5854#ifdef FEAT_CSCOPE
5855 "cscope",
5856#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005857 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005858#ifdef CURSOR_SHAPE
5859 "cursorshape",
5860#endif
5861#ifdef DEBUG
5862 "debug",
5863#endif
5864#ifdef FEAT_CON_DIALOG
5865 "dialog_con",
5866#endif
5867#ifdef FEAT_GUI_DIALOG
5868 "dialog_gui",
5869#endif
5870#ifdef FEAT_DIFF
5871 "diff",
5872#endif
5873#ifdef FEAT_DIGRAPHS
5874 "digraphs",
5875#endif
5876#ifdef FEAT_DIRECTX
5877 "directx",
5878#endif
5879#ifdef FEAT_DND
5880 "dnd",
5881#endif
5882#ifdef FEAT_EMACS_TAGS
5883 "emacs_tags",
5884#endif
5885 "eval", /* always present, of course! */
5886 "ex_extra", /* graduated feature */
5887#ifdef FEAT_SEARCH_EXTRA
5888 "extra_search",
5889#endif
5890#ifdef FEAT_FKMAP
5891 "farsi",
5892#endif
5893#ifdef FEAT_SEARCHPATH
5894 "file_in_path",
5895#endif
5896#ifdef FEAT_FILTERPIPE
5897 "filterpipe",
5898#endif
5899#ifdef FEAT_FIND_ID
5900 "find_in_path",
5901#endif
5902#ifdef FEAT_FLOAT
5903 "float",
5904#endif
5905#ifdef FEAT_FOLDING
5906 "folding",
5907#endif
5908#ifdef FEAT_FOOTER
5909 "footer",
5910#endif
5911#if !defined(USE_SYSTEM) && defined(UNIX)
5912 "fork",
5913#endif
5914#ifdef FEAT_GETTEXT
5915 "gettext",
5916#endif
5917#ifdef FEAT_GUI
5918 "gui",
5919#endif
5920#ifdef FEAT_GUI_ATHENA
5921# ifdef FEAT_GUI_NEXTAW
5922 "gui_neXtaw",
5923# else
5924 "gui_athena",
5925# endif
5926#endif
5927#ifdef FEAT_GUI_GTK
5928 "gui_gtk",
5929# ifdef USE_GTK3
5930 "gui_gtk3",
5931# else
5932 "gui_gtk2",
5933# endif
5934#endif
5935#ifdef FEAT_GUI_GNOME
5936 "gui_gnome",
5937#endif
5938#ifdef FEAT_GUI_MAC
5939 "gui_mac",
5940#endif
5941#ifdef FEAT_GUI_MOTIF
5942 "gui_motif",
5943#endif
5944#ifdef FEAT_GUI_PHOTON
5945 "gui_photon",
5946#endif
5947#ifdef FEAT_GUI_W32
5948 "gui_win32",
5949#endif
5950#ifdef FEAT_HANGULIN
5951 "hangul_input",
5952#endif
5953#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
5954 "iconv",
5955#endif
5956#ifdef FEAT_INS_EXPAND
5957 "insert_expand",
5958#endif
5959#ifdef FEAT_JOB_CHANNEL
5960 "job",
5961#endif
5962#ifdef FEAT_JUMPLIST
5963 "jumplist",
5964#endif
5965#ifdef FEAT_KEYMAP
5966 "keymap",
5967#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02005968 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005969#ifdef FEAT_LANGMAP
5970 "langmap",
5971#endif
5972#ifdef FEAT_LIBCALL
5973 "libcall",
5974#endif
5975#ifdef FEAT_LINEBREAK
5976 "linebreak",
5977#endif
5978#ifdef FEAT_LISP
5979 "lispindent",
5980#endif
5981#ifdef FEAT_LISTCMDS
5982 "listcmds",
5983#endif
5984#ifdef FEAT_LOCALMAP
5985 "localmap",
5986#endif
5987#ifdef FEAT_LUA
5988# ifndef DYNAMIC_LUA
5989 "lua",
5990# endif
5991#endif
5992#ifdef FEAT_MENU
5993 "menu",
5994#endif
5995#ifdef FEAT_SESSION
5996 "mksession",
5997#endif
5998#ifdef FEAT_MODIFY_FNAME
5999 "modify_fname",
6000#endif
6001#ifdef FEAT_MOUSE
6002 "mouse",
6003#endif
6004#ifdef FEAT_MOUSESHAPE
6005 "mouseshape",
6006#endif
6007#if defined(UNIX) || defined(VMS)
6008# ifdef FEAT_MOUSE_DEC
6009 "mouse_dec",
6010# endif
6011# ifdef FEAT_MOUSE_GPM
6012 "mouse_gpm",
6013# endif
6014# ifdef FEAT_MOUSE_JSB
6015 "mouse_jsbterm",
6016# endif
6017# ifdef FEAT_MOUSE_NET
6018 "mouse_netterm",
6019# endif
6020# ifdef FEAT_MOUSE_PTERM
6021 "mouse_pterm",
6022# endif
6023# ifdef FEAT_MOUSE_SGR
6024 "mouse_sgr",
6025# endif
6026# ifdef FEAT_SYSMOUSE
6027 "mouse_sysmouse",
6028# endif
6029# ifdef FEAT_MOUSE_URXVT
6030 "mouse_urxvt",
6031# endif
6032# ifdef FEAT_MOUSE_XTERM
6033 "mouse_xterm",
6034# endif
6035#endif
6036#ifdef FEAT_MBYTE
6037 "multi_byte",
6038#endif
6039#ifdef FEAT_MBYTE_IME
6040 "multi_byte_ime",
6041#endif
6042#ifdef FEAT_MULTI_LANG
6043 "multi_lang",
6044#endif
6045#ifdef FEAT_MZSCHEME
6046#ifndef DYNAMIC_MZSCHEME
6047 "mzscheme",
6048#endif
6049#endif
6050#ifdef FEAT_NUM64
6051 "num64",
6052#endif
6053#ifdef FEAT_OLE
6054 "ole",
6055#endif
6056 "packages",
6057#ifdef FEAT_PATH_EXTRA
6058 "path_extra",
6059#endif
6060#ifdef FEAT_PERL
6061#ifndef DYNAMIC_PERL
6062 "perl",
6063#endif
6064#endif
6065#ifdef FEAT_PERSISTENT_UNDO
6066 "persistent_undo",
6067#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006068#if defined(FEAT_PYTHON)
6069 "python_compiled",
6070# if defined(DYNAMIC_PYTHON)
6071 "python_dynamic",
6072# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006073 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006074 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006075# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006076#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006077#if defined(FEAT_PYTHON3)
6078 "python3_compiled",
6079# if defined(DYNAMIC_PYTHON3)
6080 "python3_dynamic",
6081# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006082 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006083 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006084# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006085#endif
6086#ifdef FEAT_POSTSCRIPT
6087 "postscript",
6088#endif
6089#ifdef FEAT_PRINTER
6090 "printer",
6091#endif
6092#ifdef FEAT_PROFILE
6093 "profile",
6094#endif
6095#ifdef FEAT_RELTIME
6096 "reltime",
6097#endif
6098#ifdef FEAT_QUICKFIX
6099 "quickfix",
6100#endif
6101#ifdef FEAT_RIGHTLEFT
6102 "rightleft",
6103#endif
6104#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6105 "ruby",
6106#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006107 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006108#ifdef FEAT_CMDL_INFO
6109 "showcmd",
6110 "cmdline_info",
6111#endif
6112#ifdef FEAT_SIGNS
6113 "signs",
6114#endif
6115#ifdef FEAT_SMARTINDENT
6116 "smartindent",
6117#endif
6118#ifdef STARTUPTIME
6119 "startuptime",
6120#endif
6121#ifdef FEAT_STL_OPT
6122 "statusline",
6123#endif
6124#ifdef FEAT_SUN_WORKSHOP
6125 "sun_workshop",
6126#endif
6127#ifdef FEAT_NETBEANS_INTG
6128 "netbeans_intg",
6129#endif
6130#ifdef FEAT_SPELL
6131 "spell",
6132#endif
6133#ifdef FEAT_SYN_HL
6134 "syntax",
6135#endif
6136#if defined(USE_SYSTEM) || !defined(UNIX)
6137 "system",
6138#endif
6139#ifdef FEAT_TAG_BINS
6140 "tag_binary",
6141#endif
6142#ifdef FEAT_TAG_OLDSTATIC
6143 "tag_old_static",
6144#endif
6145#ifdef FEAT_TAG_ANYWHITE
6146 "tag_any_white",
6147#endif
6148#ifdef FEAT_TCL
6149# ifndef DYNAMIC_TCL
6150 "tcl",
6151# endif
6152#endif
6153#ifdef FEAT_TERMGUICOLORS
6154 "termguicolors",
6155#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006156#if defined(FEAT_TERMINAL) && !defined(WIN3264)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006157 "terminal",
6158#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006159#ifdef TERMINFO
6160 "terminfo",
6161#endif
6162#ifdef FEAT_TERMRESPONSE
6163 "termresponse",
6164#endif
6165#ifdef FEAT_TEXTOBJ
6166 "textobjects",
6167#endif
6168#ifdef HAVE_TGETENT
6169 "tgetent",
6170#endif
6171#ifdef FEAT_TIMERS
6172 "timers",
6173#endif
6174#ifdef FEAT_TITLE
6175 "title",
6176#endif
6177#ifdef FEAT_TOOLBAR
6178 "toolbar",
6179#endif
6180#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6181 "unnamedplus",
6182#endif
6183#ifdef FEAT_USR_CMDS
6184 "user-commands", /* was accidentally included in 5.4 */
6185 "user_commands",
6186#endif
6187#ifdef FEAT_VIMINFO
6188 "viminfo",
6189#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006190 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006191#ifdef FEAT_VIRTUALEDIT
6192 "virtualedit",
6193#endif
6194 "visual",
6195#ifdef FEAT_VISUALEXTRA
6196 "visualextra",
6197#endif
6198#ifdef FEAT_VREPLACE
6199 "vreplace",
6200#endif
6201#ifdef FEAT_WILDIGN
6202 "wildignore",
6203#endif
6204#ifdef FEAT_WILDMENU
6205 "wildmenu",
6206#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006207 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006208#ifdef FEAT_WAK
6209 "winaltkeys",
6210#endif
6211#ifdef FEAT_WRITEBACKUP
6212 "writebackup",
6213#endif
6214#ifdef FEAT_XIM
6215 "xim",
6216#endif
6217#ifdef FEAT_XFONTSET
6218 "xfontset",
6219#endif
6220#ifdef FEAT_XPM_W32
6221 "xpm",
6222 "xpm_w32", /* for backward compatibility */
6223#else
6224# if defined(HAVE_XPM)
6225 "xpm",
6226# endif
6227#endif
6228#ifdef USE_XSMP
6229 "xsmp",
6230#endif
6231#ifdef USE_XSMP_INTERACT
6232 "xsmp_interact",
6233#endif
6234#ifdef FEAT_XCLIPBOARD
6235 "xterm_clipboard",
6236#endif
6237#ifdef FEAT_XTERM_SAVE
6238 "xterm_save",
6239#endif
6240#if defined(UNIX) && defined(FEAT_X11)
6241 "X11",
6242#endif
6243 NULL
6244 };
6245
6246 name = get_tv_string(&argvars[0]);
6247 for (i = 0; has_list[i] != NULL; ++i)
6248 if (STRICMP(name, has_list[i]) == 0)
6249 {
6250 n = TRUE;
6251 break;
6252 }
6253
6254 if (n == FALSE)
6255 {
6256 if (STRNICMP(name, "patch", 5) == 0)
6257 {
6258 if (name[5] == '-'
6259 && STRLEN(name) >= 11
6260 && vim_isdigit(name[6])
6261 && vim_isdigit(name[8])
6262 && vim_isdigit(name[10]))
6263 {
6264 int major = atoi((char *)name + 6);
6265 int minor = atoi((char *)name + 8);
6266
6267 /* Expect "patch-9.9.01234". */
6268 n = (major < VIM_VERSION_MAJOR
6269 || (major == VIM_VERSION_MAJOR
6270 && (minor < VIM_VERSION_MINOR
6271 || (minor == VIM_VERSION_MINOR
6272 && has_patch(atoi((char *)name + 10))))));
6273 }
6274 else
6275 n = has_patch(atoi((char *)name + 5));
6276 }
6277 else if (STRICMP(name, "vim_starting") == 0)
6278 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006279 else if (STRICMP(name, "ttyin") == 0)
6280 n = mch_input_isatty();
6281 else if (STRICMP(name, "ttyout") == 0)
6282 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006283#ifdef FEAT_MBYTE
6284 else if (STRICMP(name, "multi_byte_encoding") == 0)
6285 n = has_mbyte;
6286#endif
6287#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
6288 else if (STRICMP(name, "balloon_multiline") == 0)
6289 n = multiline_balloon_available();
6290#endif
6291#ifdef DYNAMIC_TCL
6292 else if (STRICMP(name, "tcl") == 0)
6293 n = tcl_enabled(FALSE);
6294#endif
6295#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6296 else if (STRICMP(name, "iconv") == 0)
6297 n = iconv_enabled(FALSE);
6298#endif
6299#ifdef DYNAMIC_LUA
6300 else if (STRICMP(name, "lua") == 0)
6301 n = lua_enabled(FALSE);
6302#endif
6303#ifdef DYNAMIC_MZSCHEME
6304 else if (STRICMP(name, "mzscheme") == 0)
6305 n = mzscheme_enabled(FALSE);
6306#endif
6307#ifdef DYNAMIC_RUBY
6308 else if (STRICMP(name, "ruby") == 0)
6309 n = ruby_enabled(FALSE);
6310#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006311#ifdef DYNAMIC_PYTHON
6312 else if (STRICMP(name, "python") == 0)
6313 n = python_enabled(FALSE);
6314#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006315#ifdef DYNAMIC_PYTHON3
6316 else if (STRICMP(name, "python3") == 0)
6317 n = python3_enabled(FALSE);
6318#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006319#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6320 else if (STRICMP(name, "pythonx") == 0)
6321 {
6322# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6323 if (p_pyx == 0)
6324 n = python3_enabled(FALSE) || python_enabled(FALSE);
6325 else if (p_pyx == 3)
6326 n = python3_enabled(FALSE);
6327 else if (p_pyx == 2)
6328 n = python_enabled(FALSE);
6329# elif defined(DYNAMIC_PYTHON)
6330 n = python_enabled(FALSE);
6331# elif defined(DYNAMIC_PYTHON3)
6332 n = python3_enabled(FALSE);
6333# endif
6334 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006335#endif
6336#ifdef DYNAMIC_PERL
6337 else if (STRICMP(name, "perl") == 0)
6338 n = perl_enabled(FALSE);
6339#endif
6340#ifdef FEAT_GUI
6341 else if (STRICMP(name, "gui_running") == 0)
6342 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006343# ifdef FEAT_BROWSE
6344 else if (STRICMP(name, "browse") == 0)
6345 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6346# endif
6347#endif
6348#ifdef FEAT_SYN_HL
6349 else if (STRICMP(name, "syntax_items") == 0)
6350 n = syntax_present(curwin);
6351#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006352#ifdef FEAT_VTP
6353 else if (STRICMP(name, "vcon") == 0)
6354 n = has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006355#endif
6356#ifdef FEAT_NETBEANS_INTG
6357 else if (STRICMP(name, "netbeans_enabled") == 0)
6358 n = netbeans_active();
6359#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006360#if defined(FEAT_TERMINAL) && defined(WIN3264)
6361 else if (STRICMP(name, "terminal") == 0)
6362 n = terminal_enabled();
6363#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006364 }
6365
6366 rettv->vval.v_number = n;
6367}
6368
6369/*
6370 * "has_key()" function
6371 */
6372 static void
6373f_has_key(typval_T *argvars, typval_T *rettv)
6374{
6375 if (argvars[0].v_type != VAR_DICT)
6376 {
6377 EMSG(_(e_dictreq));
6378 return;
6379 }
6380 if (argvars[0].vval.v_dict == NULL)
6381 return;
6382
6383 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
6384 get_tv_string(&argvars[1]), -1) != NULL;
6385}
6386
6387/*
6388 * "haslocaldir()" function
6389 */
6390 static void
6391f_haslocaldir(typval_T *argvars, typval_T *rettv)
6392{
6393 win_T *wp = NULL;
6394
6395 wp = find_tabwin(&argvars[0], &argvars[1]);
6396 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6397}
6398
6399/*
6400 * "hasmapto()" function
6401 */
6402 static void
6403f_hasmapto(typval_T *argvars, typval_T *rettv)
6404{
6405 char_u *name;
6406 char_u *mode;
6407 char_u buf[NUMBUFLEN];
6408 int abbr = FALSE;
6409
6410 name = get_tv_string(&argvars[0]);
6411 if (argvars[1].v_type == VAR_UNKNOWN)
6412 mode = (char_u *)"nvo";
6413 else
6414 {
6415 mode = get_tv_string_buf(&argvars[1], buf);
6416 if (argvars[2].v_type != VAR_UNKNOWN)
6417 abbr = (int)get_tv_number(&argvars[2]);
6418 }
6419
6420 if (map_to_exists(name, mode, abbr))
6421 rettv->vval.v_number = TRUE;
6422 else
6423 rettv->vval.v_number = FALSE;
6424}
6425
6426/*
6427 * "histadd()" function
6428 */
6429 static void
6430f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6431{
6432#ifdef FEAT_CMDHIST
6433 int histype;
6434 char_u *str;
6435 char_u buf[NUMBUFLEN];
6436#endif
6437
6438 rettv->vval.v_number = FALSE;
6439 if (check_restricted() || check_secure())
6440 return;
6441#ifdef FEAT_CMDHIST
6442 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6443 histype = str != NULL ? get_histtype(str) : -1;
6444 if (histype >= 0)
6445 {
6446 str = get_tv_string_buf(&argvars[1], buf);
6447 if (*str != NUL)
6448 {
6449 init_history();
6450 add_to_history(histype, str, FALSE, NUL);
6451 rettv->vval.v_number = TRUE;
6452 return;
6453 }
6454 }
6455#endif
6456}
6457
6458/*
6459 * "histdel()" function
6460 */
6461 static void
6462f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6463{
6464#ifdef FEAT_CMDHIST
6465 int n;
6466 char_u buf[NUMBUFLEN];
6467 char_u *str;
6468
6469 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6470 if (str == NULL)
6471 n = 0;
6472 else if (argvars[1].v_type == VAR_UNKNOWN)
6473 /* only one argument: clear entire history */
6474 n = clr_history(get_histtype(str));
6475 else if (argvars[1].v_type == VAR_NUMBER)
6476 /* index given: remove that entry */
6477 n = del_history_idx(get_histtype(str),
6478 (int)get_tv_number(&argvars[1]));
6479 else
6480 /* string given: remove all matching entries */
6481 n = del_history_entry(get_histtype(str),
6482 get_tv_string_buf(&argvars[1], buf));
6483 rettv->vval.v_number = n;
6484#endif
6485}
6486
6487/*
6488 * "histget()" function
6489 */
6490 static void
6491f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6492{
6493#ifdef FEAT_CMDHIST
6494 int type;
6495 int idx;
6496 char_u *str;
6497
6498 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6499 if (str == NULL)
6500 rettv->vval.v_string = NULL;
6501 else
6502 {
6503 type = get_histtype(str);
6504 if (argvars[1].v_type == VAR_UNKNOWN)
6505 idx = get_history_idx(type);
6506 else
6507 idx = (int)get_tv_number_chk(&argvars[1], NULL);
6508 /* -1 on type error */
6509 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6510 }
6511#else
6512 rettv->vval.v_string = NULL;
6513#endif
6514 rettv->v_type = VAR_STRING;
6515}
6516
6517/*
6518 * "histnr()" function
6519 */
6520 static void
6521f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6522{
6523 int i;
6524
6525#ifdef FEAT_CMDHIST
6526 char_u *history = get_tv_string_chk(&argvars[0]);
6527
6528 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6529 if (i >= HIST_CMD && i < HIST_COUNT)
6530 i = get_history_idx(i);
6531 else
6532#endif
6533 i = -1;
6534 rettv->vval.v_number = i;
6535}
6536
6537/*
6538 * "highlightID(name)" function
6539 */
6540 static void
6541f_hlID(typval_T *argvars, typval_T *rettv)
6542{
6543 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
6544}
6545
6546/*
6547 * "highlight_exists()" function
6548 */
6549 static void
6550f_hlexists(typval_T *argvars, typval_T *rettv)
6551{
6552 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
6553}
6554
6555/*
6556 * "hostname()" function
6557 */
6558 static void
6559f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6560{
6561 char_u hostname[256];
6562
6563 mch_get_host_name(hostname, 256);
6564 rettv->v_type = VAR_STRING;
6565 rettv->vval.v_string = vim_strsave(hostname);
6566}
6567
6568/*
6569 * iconv() function
6570 */
6571 static void
6572f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6573{
6574#ifdef FEAT_MBYTE
6575 char_u buf1[NUMBUFLEN];
6576 char_u buf2[NUMBUFLEN];
6577 char_u *from, *to, *str;
6578 vimconv_T vimconv;
6579#endif
6580
6581 rettv->v_type = VAR_STRING;
6582 rettv->vval.v_string = NULL;
6583
6584#ifdef FEAT_MBYTE
6585 str = get_tv_string(&argvars[0]);
6586 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
6587 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
6588 vimconv.vc_type = CONV_NONE;
6589 convert_setup(&vimconv, from, to);
6590
6591 /* If the encodings are equal, no conversion needed. */
6592 if (vimconv.vc_type == CONV_NONE)
6593 rettv->vval.v_string = vim_strsave(str);
6594 else
6595 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6596
6597 convert_setup(&vimconv, NULL, NULL);
6598 vim_free(from);
6599 vim_free(to);
6600#endif
6601}
6602
6603/*
6604 * "indent()" function
6605 */
6606 static void
6607f_indent(typval_T *argvars, typval_T *rettv)
6608{
6609 linenr_T lnum;
6610
6611 lnum = get_tv_lnum(argvars);
6612 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6613 rettv->vval.v_number = get_indent_lnum(lnum);
6614 else
6615 rettv->vval.v_number = -1;
6616}
6617
6618/*
6619 * "index()" function
6620 */
6621 static void
6622f_index(typval_T *argvars, typval_T *rettv)
6623{
6624 list_T *l;
6625 listitem_T *item;
6626 long idx = 0;
6627 int ic = FALSE;
6628
6629 rettv->vval.v_number = -1;
6630 if (argvars[0].v_type != VAR_LIST)
6631 {
6632 EMSG(_(e_listreq));
6633 return;
6634 }
6635 l = argvars[0].vval.v_list;
6636 if (l != NULL)
6637 {
6638 item = l->lv_first;
6639 if (argvars[2].v_type != VAR_UNKNOWN)
6640 {
6641 int error = FALSE;
6642
6643 /* Start at specified item. Use the cached index that list_find()
6644 * sets, so that a negative number also works. */
6645 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
6646 idx = l->lv_idx;
6647 if (argvars[3].v_type != VAR_UNKNOWN)
6648 ic = (int)get_tv_number_chk(&argvars[3], &error);
6649 if (error)
6650 item = NULL;
6651 }
6652
6653 for ( ; item != NULL; item = item->li_next, ++idx)
6654 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6655 {
6656 rettv->vval.v_number = idx;
6657 break;
6658 }
6659 }
6660}
6661
6662static int inputsecret_flag = 0;
6663
6664/*
6665 * "input()" function
6666 * Also handles inputsecret() when inputsecret is set.
6667 */
6668 static void
6669f_input(typval_T *argvars, typval_T *rettv)
6670{
6671 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6672}
6673
6674/*
6675 * "inputdialog()" function
6676 */
6677 static void
6678f_inputdialog(typval_T *argvars, typval_T *rettv)
6679{
6680#if defined(FEAT_GUI_TEXTDIALOG)
6681 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
6682 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6683 {
6684 char_u *message;
6685 char_u buf[NUMBUFLEN];
6686 char_u *defstr = (char_u *)"";
6687
6688 message = get_tv_string_chk(&argvars[0]);
6689 if (argvars[1].v_type != VAR_UNKNOWN
6690 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
6691 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6692 else
6693 IObuff[0] = NUL;
6694 if (message != NULL && defstr != NULL
6695 && do_dialog(VIM_QUESTION, NULL, message,
6696 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6697 rettv->vval.v_string = vim_strsave(IObuff);
6698 else
6699 {
6700 if (message != NULL && defstr != NULL
6701 && argvars[1].v_type != VAR_UNKNOWN
6702 && argvars[2].v_type != VAR_UNKNOWN)
6703 rettv->vval.v_string = vim_strsave(
6704 get_tv_string_buf(&argvars[2], buf));
6705 else
6706 rettv->vval.v_string = NULL;
6707 }
6708 rettv->v_type = VAR_STRING;
6709 }
6710 else
6711#endif
6712 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6713}
6714
6715/*
6716 * "inputlist()" function
6717 */
6718 static void
6719f_inputlist(typval_T *argvars, typval_T *rettv)
6720{
6721 listitem_T *li;
6722 int selected;
6723 int mouse_used;
6724
6725#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02006726 /* While starting up, there is no place to enter text. When running tests
6727 * with --not-a-term we assume feedkeys() will be used. */
6728 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006729 return;
6730#endif
6731 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6732 {
6733 EMSG2(_(e_listarg), "inputlist()");
6734 return;
6735 }
6736
6737 msg_start();
6738 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
6739 lines_left = Rows; /* avoid more prompt */
6740 msg_scroll = TRUE;
6741 msg_clr_eos();
6742
6743 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
6744 {
6745 msg_puts(get_tv_string(&li->li_tv));
6746 msg_putchar('\n');
6747 }
6748
6749 /* Ask for choice. */
6750 selected = prompt_for_number(&mouse_used);
6751 if (mouse_used)
6752 selected -= lines_left;
6753
6754 rettv->vval.v_number = selected;
6755}
6756
6757
6758static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6759
6760/*
6761 * "inputrestore()" function
6762 */
6763 static void
6764f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6765{
6766 if (ga_userinput.ga_len > 0)
6767 {
6768 --ga_userinput.ga_len;
6769 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
6770 + ga_userinput.ga_len);
6771 /* default return is zero == OK */
6772 }
6773 else if (p_verbose > 1)
6774 {
6775 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
6776 rettv->vval.v_number = 1; /* Failed */
6777 }
6778}
6779
6780/*
6781 * "inputsave()" function
6782 */
6783 static void
6784f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
6785{
6786 /* Add an entry to the stack of typeahead storage. */
6787 if (ga_grow(&ga_userinput, 1) == OK)
6788 {
6789 save_typeahead((tasave_T *)(ga_userinput.ga_data)
6790 + ga_userinput.ga_len);
6791 ++ga_userinput.ga_len;
6792 /* default return is zero == OK */
6793 }
6794 else
6795 rettv->vval.v_number = 1; /* Failed */
6796}
6797
6798/*
6799 * "inputsecret()" function
6800 */
6801 static void
6802f_inputsecret(typval_T *argvars, typval_T *rettv)
6803{
6804 ++cmdline_star;
6805 ++inputsecret_flag;
6806 f_input(argvars, rettv);
6807 --cmdline_star;
6808 --inputsecret_flag;
6809}
6810
6811/*
6812 * "insert()" function
6813 */
6814 static void
6815f_insert(typval_T *argvars, typval_T *rettv)
6816{
6817 long before = 0;
6818 listitem_T *item;
6819 list_T *l;
6820 int error = FALSE;
6821
6822 if (argvars[0].v_type != VAR_LIST)
6823 EMSG2(_(e_listarg), "insert()");
6824 else if ((l = argvars[0].vval.v_list) != NULL
6825 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
6826 {
6827 if (argvars[2].v_type != VAR_UNKNOWN)
6828 before = (long)get_tv_number_chk(&argvars[2], &error);
6829 if (error)
6830 return; /* type error; errmsg already given */
6831
6832 if (before == l->lv_len)
6833 item = NULL;
6834 else
6835 {
6836 item = list_find(l, before);
6837 if (item == NULL)
6838 {
6839 EMSGN(_(e_listidx), before);
6840 l = NULL;
6841 }
6842 }
6843 if (l != NULL)
6844 {
6845 list_insert_tv(l, &argvars[1], item);
6846 copy_tv(&argvars[0], rettv);
6847 }
6848 }
6849}
6850
6851/*
6852 * "invert(expr)" function
6853 */
6854 static void
6855f_invert(typval_T *argvars, typval_T *rettv)
6856{
6857 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
6858}
6859
6860/*
6861 * "isdirectory()" function
6862 */
6863 static void
6864f_isdirectory(typval_T *argvars, typval_T *rettv)
6865{
6866 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
6867}
6868
6869/*
6870 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
6871 * or it refers to a List or Dictionary that is locked.
6872 */
6873 static int
6874tv_islocked(typval_T *tv)
6875{
6876 return (tv->v_lock & VAR_LOCKED)
6877 || (tv->v_type == VAR_LIST
6878 && tv->vval.v_list != NULL
6879 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
6880 || (tv->v_type == VAR_DICT
6881 && tv->vval.v_dict != NULL
6882 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
6883}
6884
6885/*
6886 * "islocked()" function
6887 */
6888 static void
6889f_islocked(typval_T *argvars, typval_T *rettv)
6890{
6891 lval_T lv;
6892 char_u *end;
6893 dictitem_T *di;
6894
6895 rettv->vval.v_number = -1;
6896 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01006897 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006898 if (end != NULL && lv.ll_name != NULL)
6899 {
6900 if (*end != NUL)
6901 EMSG(_(e_trailing));
6902 else
6903 {
6904 if (lv.ll_tv == NULL)
6905 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006906 di = find_var(lv.ll_name, NULL, TRUE);
6907 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006908 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006909 /* Consider a variable locked when:
6910 * 1. the variable itself is locked
6911 * 2. the value of the variable is locked.
6912 * 3. the List or Dict value is locked.
6913 */
6914 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
6915 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006916 }
6917 }
6918 else if (lv.ll_range)
6919 EMSG(_("E786: Range not allowed"));
6920 else if (lv.ll_newkey != NULL)
6921 EMSG2(_(e_dictkey), lv.ll_newkey);
6922 else if (lv.ll_list != NULL)
6923 /* List item. */
6924 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
6925 else
6926 /* Dictionary item. */
6927 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
6928 }
6929 }
6930
6931 clear_lval(&lv);
6932}
6933
6934#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
6935/*
6936 * "isnan()" function
6937 */
6938 static void
6939f_isnan(typval_T *argvars, typval_T *rettv)
6940{
6941 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
6942 && isnan(argvars[0].vval.v_float);
6943}
6944#endif
6945
6946/*
6947 * "items(dict)" function
6948 */
6949 static void
6950f_items(typval_T *argvars, typval_T *rettv)
6951{
6952 dict_list(argvars, rettv, 2);
6953}
6954
6955#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
6956/*
6957 * Get the job from the argument.
6958 * Returns NULL if the job is invalid.
6959 */
6960 static job_T *
6961get_job_arg(typval_T *tv)
6962{
6963 job_T *job;
6964
6965 if (tv->v_type != VAR_JOB)
6966 {
6967 EMSG2(_(e_invarg2), get_tv_string(tv));
6968 return NULL;
6969 }
6970 job = tv->vval.v_job;
6971
6972 if (job == NULL)
6973 EMSG(_("E916: not a valid job"));
6974 return job;
6975}
6976
6977/*
6978 * "job_getchannel()" function
6979 */
6980 static void
6981f_job_getchannel(typval_T *argvars, typval_T *rettv)
6982{
6983 job_T *job = get_job_arg(&argvars[0]);
6984
6985 if (job != NULL)
6986 {
6987 rettv->v_type = VAR_CHANNEL;
6988 rettv->vval.v_channel = job->jv_channel;
6989 if (job->jv_channel != NULL)
6990 ++job->jv_channel->ch_refcount;
6991 }
6992}
6993
6994/*
6995 * "job_info()" function
6996 */
6997 static void
6998f_job_info(typval_T *argvars, typval_T *rettv)
6999{
7000 job_T *job = get_job_arg(&argvars[0]);
7001
7002 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7003 job_info(job, rettv->vval.v_dict);
7004}
7005
7006/*
7007 * "job_setoptions()" function
7008 */
7009 static void
7010f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7011{
7012 job_T *job = get_job_arg(&argvars[0]);
7013 jobopt_T opt;
7014
7015 if (job == NULL)
7016 return;
7017 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007018 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007019 job_set_options(job, &opt);
7020 free_job_options(&opt);
7021}
7022
7023/*
7024 * "job_start()" function
7025 */
7026 static void
7027f_job_start(typval_T *argvars, typval_T *rettv)
7028{
7029 rettv->v_type = VAR_JOB;
7030 if (check_restricted() || check_secure())
7031 return;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02007032 rettv->vval.v_job = job_start(argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007033}
7034
7035/*
7036 * "job_status()" function
7037 */
7038 static void
7039f_job_status(typval_T *argvars, typval_T *rettv)
7040{
7041 job_T *job = get_job_arg(&argvars[0]);
7042
7043 if (job != NULL)
7044 {
7045 rettv->v_type = VAR_STRING;
7046 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7047 }
7048}
7049
7050/*
7051 * "job_stop()" function
7052 */
7053 static void
7054f_job_stop(typval_T *argvars, typval_T *rettv)
7055{
7056 job_T *job = get_job_arg(&argvars[0]);
7057
7058 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007059 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007060}
7061#endif
7062
7063/*
7064 * "join()" function
7065 */
7066 static void
7067f_join(typval_T *argvars, typval_T *rettv)
7068{
7069 garray_T ga;
7070 char_u *sep;
7071
7072 if (argvars[0].v_type != VAR_LIST)
7073 {
7074 EMSG(_(e_listreq));
7075 return;
7076 }
7077 if (argvars[0].vval.v_list == NULL)
7078 return;
7079 if (argvars[1].v_type == VAR_UNKNOWN)
7080 sep = (char_u *)" ";
7081 else
7082 sep = get_tv_string_chk(&argvars[1]);
7083
7084 rettv->v_type = VAR_STRING;
7085
7086 if (sep != NULL)
7087 {
7088 ga_init2(&ga, (int)sizeof(char), 80);
7089 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7090 ga_append(&ga, NUL);
7091 rettv->vval.v_string = (char_u *)ga.ga_data;
7092 }
7093 else
7094 rettv->vval.v_string = NULL;
7095}
7096
7097/*
7098 * "js_decode()" function
7099 */
7100 static void
7101f_js_decode(typval_T *argvars, typval_T *rettv)
7102{
7103 js_read_T reader;
7104
7105 reader.js_buf = get_tv_string(&argvars[0]);
7106 reader.js_fill = NULL;
7107 reader.js_used = 0;
7108 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
7109 EMSG(_(e_invarg));
7110}
7111
7112/*
7113 * "js_encode()" function
7114 */
7115 static void
7116f_js_encode(typval_T *argvars, typval_T *rettv)
7117{
7118 rettv->v_type = VAR_STRING;
7119 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7120}
7121
7122/*
7123 * "json_decode()" function
7124 */
7125 static void
7126f_json_decode(typval_T *argvars, typval_T *rettv)
7127{
7128 js_read_T reader;
7129
7130 reader.js_buf = get_tv_string(&argvars[0]);
7131 reader.js_fill = NULL;
7132 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007133 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007134}
7135
7136/*
7137 * "json_encode()" function
7138 */
7139 static void
7140f_json_encode(typval_T *argvars, typval_T *rettv)
7141{
7142 rettv->v_type = VAR_STRING;
7143 rettv->vval.v_string = json_encode(&argvars[0], 0);
7144}
7145
7146/*
7147 * "keys()" function
7148 */
7149 static void
7150f_keys(typval_T *argvars, typval_T *rettv)
7151{
7152 dict_list(argvars, rettv, 0);
7153}
7154
7155/*
7156 * "last_buffer_nr()" function.
7157 */
7158 static void
7159f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7160{
7161 int n = 0;
7162 buf_T *buf;
7163
Bram Moolenaar29323592016-07-24 22:04:11 +02007164 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007165 if (n < buf->b_fnum)
7166 n = buf->b_fnum;
7167
7168 rettv->vval.v_number = n;
7169}
7170
7171/*
7172 * "len()" function
7173 */
7174 static void
7175f_len(typval_T *argvars, typval_T *rettv)
7176{
7177 switch (argvars[0].v_type)
7178 {
7179 case VAR_STRING:
7180 case VAR_NUMBER:
7181 rettv->vval.v_number = (varnumber_T)STRLEN(
7182 get_tv_string(&argvars[0]));
7183 break;
7184 case VAR_LIST:
7185 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7186 break;
7187 case VAR_DICT:
7188 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7189 break;
7190 case VAR_UNKNOWN:
7191 case VAR_SPECIAL:
7192 case VAR_FLOAT:
7193 case VAR_FUNC:
7194 case VAR_PARTIAL:
7195 case VAR_JOB:
7196 case VAR_CHANNEL:
7197 EMSG(_("E701: Invalid type for len()"));
7198 break;
7199 }
7200}
7201
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007202 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007203libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007204{
7205#ifdef FEAT_LIBCALL
7206 char_u *string_in;
7207 char_u **string_result;
7208 int nr_result;
7209#endif
7210
7211 rettv->v_type = type;
7212 if (type != VAR_NUMBER)
7213 rettv->vval.v_string = NULL;
7214
7215 if (check_restricted() || check_secure())
7216 return;
7217
7218#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007219 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007220 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7221 {
7222 string_in = NULL;
7223 if (argvars[2].v_type == VAR_STRING)
7224 string_in = argvars[2].vval.v_string;
7225 if (type == VAR_NUMBER)
7226 string_result = NULL;
7227 else
7228 string_result = &rettv->vval.v_string;
7229 if (mch_libcall(argvars[0].vval.v_string,
7230 argvars[1].vval.v_string,
7231 string_in,
7232 argvars[2].vval.v_number,
7233 string_result,
7234 &nr_result) == OK
7235 && type == VAR_NUMBER)
7236 rettv->vval.v_number = nr_result;
7237 }
7238#endif
7239}
7240
7241/*
7242 * "libcall()" function
7243 */
7244 static void
7245f_libcall(typval_T *argvars, typval_T *rettv)
7246{
7247 libcall_common(argvars, rettv, VAR_STRING);
7248}
7249
7250/*
7251 * "libcallnr()" function
7252 */
7253 static void
7254f_libcallnr(typval_T *argvars, typval_T *rettv)
7255{
7256 libcall_common(argvars, rettv, VAR_NUMBER);
7257}
7258
7259/*
7260 * "line(string)" function
7261 */
7262 static void
7263f_line(typval_T *argvars, typval_T *rettv)
7264{
7265 linenr_T lnum = 0;
7266 pos_T *fp;
7267 int fnum;
7268
7269 fp = var2fpos(&argvars[0], TRUE, &fnum);
7270 if (fp != NULL)
7271 lnum = fp->lnum;
7272 rettv->vval.v_number = lnum;
7273}
7274
7275/*
7276 * "line2byte(lnum)" function
7277 */
7278 static void
7279f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7280{
7281#ifndef FEAT_BYTEOFF
7282 rettv->vval.v_number = -1;
7283#else
7284 linenr_T lnum;
7285
7286 lnum = get_tv_lnum(argvars);
7287 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7288 rettv->vval.v_number = -1;
7289 else
7290 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7291 if (rettv->vval.v_number >= 0)
7292 ++rettv->vval.v_number;
7293#endif
7294}
7295
7296/*
7297 * "lispindent(lnum)" function
7298 */
7299 static void
7300f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7301{
7302#ifdef FEAT_LISP
7303 pos_T pos;
7304 linenr_T lnum;
7305
7306 pos = curwin->w_cursor;
7307 lnum = get_tv_lnum(argvars);
7308 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7309 {
7310 curwin->w_cursor.lnum = lnum;
7311 rettv->vval.v_number = get_lisp_indent();
7312 curwin->w_cursor = pos;
7313 }
7314 else
7315#endif
7316 rettv->vval.v_number = -1;
7317}
7318
7319/*
7320 * "localtime()" function
7321 */
7322 static void
7323f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7324{
7325 rettv->vval.v_number = (varnumber_T)time(NULL);
7326}
7327
7328static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
7329
7330 static void
7331get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7332{
7333 char_u *keys;
7334 char_u *which;
7335 char_u buf[NUMBUFLEN];
7336 char_u *keys_buf = NULL;
7337 char_u *rhs;
7338 int mode;
7339 int abbr = FALSE;
7340 int get_dict = FALSE;
7341 mapblock_T *mp;
7342 int buffer_local;
7343
7344 /* return empty string for failure */
7345 rettv->v_type = VAR_STRING;
7346 rettv->vval.v_string = NULL;
7347
7348 keys = get_tv_string(&argvars[0]);
7349 if (*keys == NUL)
7350 return;
7351
7352 if (argvars[1].v_type != VAR_UNKNOWN)
7353 {
7354 which = get_tv_string_buf_chk(&argvars[1], buf);
7355 if (argvars[2].v_type != VAR_UNKNOWN)
7356 {
7357 abbr = (int)get_tv_number(&argvars[2]);
7358 if (argvars[3].v_type != VAR_UNKNOWN)
7359 get_dict = (int)get_tv_number(&argvars[3]);
7360 }
7361 }
7362 else
7363 which = (char_u *)"";
7364 if (which == NULL)
7365 return;
7366
7367 mode = get_map_mode(&which, 0);
7368
7369 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7370 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7371 vim_free(keys_buf);
7372
7373 if (!get_dict)
7374 {
7375 /* Return a string. */
7376 if (rhs != NULL)
7377 rettv->vval.v_string = str2special_save(rhs, FALSE);
7378
7379 }
7380 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7381 {
7382 /* Return a dictionary. */
7383 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7384 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7385 dict_T *dict = rettv->vval.v_dict;
7386
7387 dict_add_nr_str(dict, "lhs", 0L, lhs);
7388 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
7389 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
7390 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
7391 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
7392 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
7393 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
7394 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
7395 dict_add_nr_str(dict, "mode", 0L, mapmode);
7396
7397 vim_free(lhs);
7398 vim_free(mapmode);
7399 }
7400}
7401
7402#ifdef FEAT_FLOAT
7403/*
7404 * "log()" function
7405 */
7406 static void
7407f_log(typval_T *argvars, typval_T *rettv)
7408{
7409 float_T f = 0.0;
7410
7411 rettv->v_type = VAR_FLOAT;
7412 if (get_float_arg(argvars, &f) == OK)
7413 rettv->vval.v_float = log(f);
7414 else
7415 rettv->vval.v_float = 0.0;
7416}
7417
7418/*
7419 * "log10()" function
7420 */
7421 static void
7422f_log10(typval_T *argvars, typval_T *rettv)
7423{
7424 float_T f = 0.0;
7425
7426 rettv->v_type = VAR_FLOAT;
7427 if (get_float_arg(argvars, &f) == OK)
7428 rettv->vval.v_float = log10(f);
7429 else
7430 rettv->vval.v_float = 0.0;
7431}
7432#endif
7433
7434#ifdef FEAT_LUA
7435/*
7436 * "luaeval()" function
7437 */
7438 static void
7439f_luaeval(typval_T *argvars, typval_T *rettv)
7440{
7441 char_u *str;
7442 char_u buf[NUMBUFLEN];
7443
7444 str = get_tv_string_buf(&argvars[0], buf);
7445 do_luaeval(str, argvars + 1, rettv);
7446}
7447#endif
7448
7449/*
7450 * "map()" function
7451 */
7452 static void
7453f_map(typval_T *argvars, typval_T *rettv)
7454{
7455 filter_map(argvars, rettv, TRUE);
7456}
7457
7458/*
7459 * "maparg()" function
7460 */
7461 static void
7462f_maparg(typval_T *argvars, typval_T *rettv)
7463{
7464 get_maparg(argvars, rettv, TRUE);
7465}
7466
7467/*
7468 * "mapcheck()" function
7469 */
7470 static void
7471f_mapcheck(typval_T *argvars, typval_T *rettv)
7472{
7473 get_maparg(argvars, rettv, FALSE);
7474}
7475
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007476typedef enum
7477{
7478 MATCH_END, /* matchend() */
7479 MATCH_MATCH, /* match() */
7480 MATCH_STR, /* matchstr() */
7481 MATCH_LIST, /* matchlist() */
7482 MATCH_POS /* matchstrpos() */
7483} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007484
7485 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007486find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007487{
7488 char_u *str = NULL;
7489 long len = 0;
7490 char_u *expr = NULL;
7491 char_u *pat;
7492 regmatch_T regmatch;
7493 char_u patbuf[NUMBUFLEN];
7494 char_u strbuf[NUMBUFLEN];
7495 char_u *save_cpo;
7496 long start = 0;
7497 long nth = 1;
7498 colnr_T startcol = 0;
7499 int match = 0;
7500 list_T *l = NULL;
7501 listitem_T *li = NULL;
7502 long idx = 0;
7503 char_u *tofree = NULL;
7504
7505 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7506 save_cpo = p_cpo;
7507 p_cpo = (char_u *)"";
7508
7509 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007510 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007511 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007512 /* type MATCH_LIST: return empty list when there are no matches.
7513 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007514 if (rettv_list_alloc(rettv) == FAIL)
7515 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007516 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007517 && (list_append_string(rettv->vval.v_list,
7518 (char_u *)"", 0) == FAIL
7519 || list_append_number(rettv->vval.v_list,
7520 (varnumber_T)-1) == FAIL
7521 || list_append_number(rettv->vval.v_list,
7522 (varnumber_T)-1) == FAIL
7523 || list_append_number(rettv->vval.v_list,
7524 (varnumber_T)-1) == FAIL))
7525 {
7526 list_free(rettv->vval.v_list);
7527 rettv->vval.v_list = NULL;
7528 goto theend;
7529 }
7530 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007531 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007532 {
7533 rettv->v_type = VAR_STRING;
7534 rettv->vval.v_string = NULL;
7535 }
7536
7537 if (argvars[0].v_type == VAR_LIST)
7538 {
7539 if ((l = argvars[0].vval.v_list) == NULL)
7540 goto theend;
7541 li = l->lv_first;
7542 }
7543 else
7544 {
7545 expr = str = get_tv_string(&argvars[0]);
7546 len = (long)STRLEN(str);
7547 }
7548
7549 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
7550 if (pat == NULL)
7551 goto theend;
7552
7553 if (argvars[2].v_type != VAR_UNKNOWN)
7554 {
7555 int error = FALSE;
7556
7557 start = (long)get_tv_number_chk(&argvars[2], &error);
7558 if (error)
7559 goto theend;
7560 if (l != NULL)
7561 {
7562 li = list_find(l, start);
7563 if (li == NULL)
7564 goto theend;
7565 idx = l->lv_idx; /* use the cached index */
7566 }
7567 else
7568 {
7569 if (start < 0)
7570 start = 0;
7571 if (start > len)
7572 goto theend;
7573 /* When "count" argument is there ignore matches before "start",
7574 * otherwise skip part of the string. Differs when pattern is "^"
7575 * or "\<". */
7576 if (argvars[3].v_type != VAR_UNKNOWN)
7577 startcol = start;
7578 else
7579 {
7580 str += start;
7581 len -= start;
7582 }
7583 }
7584
7585 if (argvars[3].v_type != VAR_UNKNOWN)
7586 nth = (long)get_tv_number_chk(&argvars[3], &error);
7587 if (error)
7588 goto theend;
7589 }
7590
7591 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7592 if (regmatch.regprog != NULL)
7593 {
7594 regmatch.rm_ic = p_ic;
7595
7596 for (;;)
7597 {
7598 if (l != NULL)
7599 {
7600 if (li == NULL)
7601 {
7602 match = FALSE;
7603 break;
7604 }
7605 vim_free(tofree);
7606 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7607 if (str == NULL)
7608 break;
7609 }
7610
7611 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7612
7613 if (match && --nth <= 0)
7614 break;
7615 if (l == NULL && !match)
7616 break;
7617
7618 /* Advance to just after the match. */
7619 if (l != NULL)
7620 {
7621 li = li->li_next;
7622 ++idx;
7623 }
7624 else
7625 {
7626#ifdef FEAT_MBYTE
7627 startcol = (colnr_T)(regmatch.startp[0]
7628 + (*mb_ptr2len)(regmatch.startp[0]) - str);
7629#else
7630 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
7631#endif
7632 if (startcol > (colnr_T)len
7633 || str + startcol <= regmatch.startp[0])
7634 {
7635 match = FALSE;
7636 break;
7637 }
7638 }
7639 }
7640
7641 if (match)
7642 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007643 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007644 {
7645 listitem_T *li1 = rettv->vval.v_list->lv_first;
7646 listitem_T *li2 = li1->li_next;
7647 listitem_T *li3 = li2->li_next;
7648 listitem_T *li4 = li3->li_next;
7649
7650 vim_free(li1->li_tv.vval.v_string);
7651 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7652 (int)(regmatch.endp[0] - regmatch.startp[0]));
7653 li3->li_tv.vval.v_number =
7654 (varnumber_T)(regmatch.startp[0] - expr);
7655 li4->li_tv.vval.v_number =
7656 (varnumber_T)(regmatch.endp[0] - expr);
7657 if (l != NULL)
7658 li2->li_tv.vval.v_number = (varnumber_T)idx;
7659 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007660 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007661 {
7662 int i;
7663
7664 /* return list with matched string and submatches */
7665 for (i = 0; i < NSUBEXP; ++i)
7666 {
7667 if (regmatch.endp[i] == NULL)
7668 {
7669 if (list_append_string(rettv->vval.v_list,
7670 (char_u *)"", 0) == FAIL)
7671 break;
7672 }
7673 else if (list_append_string(rettv->vval.v_list,
7674 regmatch.startp[i],
7675 (int)(regmatch.endp[i] - regmatch.startp[i]))
7676 == FAIL)
7677 break;
7678 }
7679 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007680 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007681 {
7682 /* return matched string */
7683 if (l != NULL)
7684 copy_tv(&li->li_tv, rettv);
7685 else
7686 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
7687 (int)(regmatch.endp[0] - regmatch.startp[0]));
7688 }
7689 else if (l != NULL)
7690 rettv->vval.v_number = idx;
7691 else
7692 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007693 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007694 rettv->vval.v_number =
7695 (varnumber_T)(regmatch.startp[0] - str);
7696 else
7697 rettv->vval.v_number =
7698 (varnumber_T)(regmatch.endp[0] - str);
7699 rettv->vval.v_number += (varnumber_T)(str - expr);
7700 }
7701 }
7702 vim_regfree(regmatch.regprog);
7703 }
7704
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007705theend:
7706 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007707 /* matchstrpos() without a list: drop the second item. */
7708 listitem_remove(rettv->vval.v_list,
7709 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007710 vim_free(tofree);
7711 p_cpo = save_cpo;
7712}
7713
7714/*
7715 * "match()" function
7716 */
7717 static void
7718f_match(typval_T *argvars, typval_T *rettv)
7719{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007720 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007721}
7722
7723/*
7724 * "matchadd()" function
7725 */
7726 static void
7727f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7728{
7729#ifdef FEAT_SEARCH_EXTRA
7730 char_u buf[NUMBUFLEN];
7731 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
7732 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
7733 int prio = 10; /* default priority */
7734 int id = -1;
7735 int error = FALSE;
7736 char_u *conceal_char = NULL;
7737
7738 rettv->vval.v_number = -1;
7739
7740 if (grp == NULL || pat == NULL)
7741 return;
7742 if (argvars[2].v_type != VAR_UNKNOWN)
7743 {
7744 prio = (int)get_tv_number_chk(&argvars[2], &error);
7745 if (argvars[3].v_type != VAR_UNKNOWN)
7746 {
7747 id = (int)get_tv_number_chk(&argvars[3], &error);
7748 if (argvars[4].v_type != VAR_UNKNOWN)
7749 {
7750 if (argvars[4].v_type != VAR_DICT)
7751 {
7752 EMSG(_(e_dictreq));
7753 return;
7754 }
7755 if (dict_find(argvars[4].vval.v_dict,
7756 (char_u *)"conceal", -1) != NULL)
7757 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7758 (char_u *)"conceal", FALSE);
7759 }
7760 }
7761 }
7762 if (error == TRUE)
7763 return;
7764 if (id >= 1 && id <= 3)
7765 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007766 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007767 return;
7768 }
7769
7770 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
7771 conceal_char);
7772#endif
7773}
7774
7775/*
7776 * "matchaddpos()" function
7777 */
7778 static void
7779f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7780{
7781#ifdef FEAT_SEARCH_EXTRA
7782 char_u buf[NUMBUFLEN];
7783 char_u *group;
7784 int prio = 10;
7785 int id = -1;
7786 int error = FALSE;
7787 list_T *l;
7788 char_u *conceal_char = NULL;
7789
7790 rettv->vval.v_number = -1;
7791
7792 group = get_tv_string_buf_chk(&argvars[0], buf);
7793 if (group == NULL)
7794 return;
7795
7796 if (argvars[1].v_type != VAR_LIST)
7797 {
7798 EMSG2(_(e_listarg), "matchaddpos()");
7799 return;
7800 }
7801 l = argvars[1].vval.v_list;
7802 if (l == NULL)
7803 return;
7804
7805 if (argvars[2].v_type != VAR_UNKNOWN)
7806 {
7807 prio = (int)get_tv_number_chk(&argvars[2], &error);
7808 if (argvars[3].v_type != VAR_UNKNOWN)
7809 {
7810 id = (int)get_tv_number_chk(&argvars[3], &error);
7811 if (argvars[4].v_type != VAR_UNKNOWN)
7812 {
7813 if (argvars[4].v_type != VAR_DICT)
7814 {
7815 EMSG(_(e_dictreq));
7816 return;
7817 }
7818 if (dict_find(argvars[4].vval.v_dict,
7819 (char_u *)"conceal", -1) != NULL)
7820 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7821 (char_u *)"conceal", FALSE);
7822 }
7823 }
7824 }
7825 if (error == TRUE)
7826 return;
7827
7828 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
7829 if (id == 1 || id == 2)
7830 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007831 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007832 return;
7833 }
7834
7835 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
7836 conceal_char);
7837#endif
7838}
7839
7840/*
7841 * "matcharg()" function
7842 */
7843 static void
7844f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
7845{
7846 if (rettv_list_alloc(rettv) == OK)
7847 {
7848#ifdef FEAT_SEARCH_EXTRA
7849 int id = (int)get_tv_number(&argvars[0]);
7850 matchitem_T *m;
7851
7852 if (id >= 1 && id <= 3)
7853 {
7854 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
7855 {
7856 list_append_string(rettv->vval.v_list,
7857 syn_id2name(m->hlg_id), -1);
7858 list_append_string(rettv->vval.v_list, m->pattern, -1);
7859 }
7860 else
7861 {
7862 list_append_string(rettv->vval.v_list, NULL, -1);
7863 list_append_string(rettv->vval.v_list, NULL, -1);
7864 }
7865 }
7866#endif
7867 }
7868}
7869
7870/*
7871 * "matchdelete()" function
7872 */
7873 static void
7874f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7875{
7876#ifdef FEAT_SEARCH_EXTRA
7877 rettv->vval.v_number = match_delete(curwin,
7878 (int)get_tv_number(&argvars[0]), TRUE);
7879#endif
7880}
7881
7882/*
7883 * "matchend()" function
7884 */
7885 static void
7886f_matchend(typval_T *argvars, typval_T *rettv)
7887{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007888 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007889}
7890
7891/*
7892 * "matchlist()" function
7893 */
7894 static void
7895f_matchlist(typval_T *argvars, typval_T *rettv)
7896{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007897 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007898}
7899
7900/*
7901 * "matchstr()" function
7902 */
7903 static void
7904f_matchstr(typval_T *argvars, typval_T *rettv)
7905{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007906 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007907}
7908
7909/*
7910 * "matchstrpos()" function
7911 */
7912 static void
7913f_matchstrpos(typval_T *argvars, typval_T *rettv)
7914{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007915 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007916}
7917
7918static void max_min(typval_T *argvars, typval_T *rettv, int domax);
7919
7920 static void
7921max_min(typval_T *argvars, typval_T *rettv, int domax)
7922{
7923 varnumber_T n = 0;
7924 varnumber_T i;
7925 int error = FALSE;
7926
7927 if (argvars[0].v_type == VAR_LIST)
7928 {
7929 list_T *l;
7930 listitem_T *li;
7931
7932 l = argvars[0].vval.v_list;
7933 if (l != NULL)
7934 {
7935 li = l->lv_first;
7936 if (li != NULL)
7937 {
7938 n = get_tv_number_chk(&li->li_tv, &error);
7939 for (;;)
7940 {
7941 li = li->li_next;
7942 if (li == NULL)
7943 break;
7944 i = get_tv_number_chk(&li->li_tv, &error);
7945 if (domax ? i > n : i < n)
7946 n = i;
7947 }
7948 }
7949 }
7950 }
7951 else if (argvars[0].v_type == VAR_DICT)
7952 {
7953 dict_T *d;
7954 int first = TRUE;
7955 hashitem_T *hi;
7956 int todo;
7957
7958 d = argvars[0].vval.v_dict;
7959 if (d != NULL)
7960 {
7961 todo = (int)d->dv_hashtab.ht_used;
7962 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
7963 {
7964 if (!HASHITEM_EMPTY(hi))
7965 {
7966 --todo;
7967 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
7968 if (first)
7969 {
7970 n = i;
7971 first = FALSE;
7972 }
7973 else if (domax ? i > n : i < n)
7974 n = i;
7975 }
7976 }
7977 }
7978 }
7979 else
Bram Moolenaar26b84332016-09-04 21:42:36 +02007980 EMSG2(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007981 rettv->vval.v_number = error ? 0 : n;
7982}
7983
7984/*
7985 * "max()" function
7986 */
7987 static void
7988f_max(typval_T *argvars, typval_T *rettv)
7989{
7990 max_min(argvars, rettv, TRUE);
7991}
7992
7993/*
7994 * "min()" function
7995 */
7996 static void
7997f_min(typval_T *argvars, typval_T *rettv)
7998{
7999 max_min(argvars, rettv, FALSE);
8000}
8001
8002static int mkdir_recurse(char_u *dir, int prot);
8003
8004/*
8005 * Create the directory in which "dir" is located, and higher levels when
8006 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008007 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008008 */
8009 static int
8010mkdir_recurse(char_u *dir, int prot)
8011{
8012 char_u *p;
8013 char_u *updir;
8014 int r = FAIL;
8015
8016 /* Get end of directory name in "dir".
8017 * We're done when it's "/" or "c:/". */
8018 p = gettail_sep(dir);
8019 if (p <= get_past_head(dir))
8020 return OK;
8021
8022 /* If the directory exists we're done. Otherwise: create it.*/
8023 updir = vim_strnsave(dir, (int)(p - dir));
8024 if (updir == NULL)
8025 return FAIL;
8026 if (mch_isdir(updir))
8027 r = OK;
8028 else if (mkdir_recurse(updir, prot) == OK)
8029 r = vim_mkdir_emsg(updir, prot);
8030 vim_free(updir);
8031 return r;
8032}
8033
8034#ifdef vim_mkdir
8035/*
8036 * "mkdir()" function
8037 */
8038 static void
8039f_mkdir(typval_T *argvars, typval_T *rettv)
8040{
8041 char_u *dir;
8042 char_u buf[NUMBUFLEN];
8043 int prot = 0755;
8044
8045 rettv->vval.v_number = FAIL;
8046 if (check_restricted() || check_secure())
8047 return;
8048
8049 dir = get_tv_string_buf(&argvars[0], buf);
8050 if (*dir == NUL)
8051 rettv->vval.v_number = FAIL;
8052 else
8053 {
8054 if (*gettail(dir) == NUL)
8055 /* remove trailing slashes */
8056 *gettail_sep(dir) = NUL;
8057
8058 if (argvars[1].v_type != VAR_UNKNOWN)
8059 {
8060 if (argvars[2].v_type != VAR_UNKNOWN)
8061 prot = (int)get_tv_number_chk(&argvars[2], NULL);
8062 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
8063 mkdir_recurse(dir, prot);
8064 }
8065 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
8066 }
8067}
8068#endif
8069
8070/*
8071 * "mode()" function
8072 */
8073 static void
8074f_mode(typval_T *argvars, typval_T *rettv)
8075{
8076 char_u buf[3];
8077
8078 buf[1] = NUL;
8079 buf[2] = NUL;
8080
8081 if (time_for_testing == 93784)
8082 {
8083 /* Testing the two-character code. */
8084 buf[0] = 'x';
8085 buf[1] = '!';
8086 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008087#ifdef FEAT_TERMINAL
8088 else if (term_use_loop())
8089 buf[0] = 't';
8090#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008091 else if (VIsual_active)
8092 {
8093 if (VIsual_select)
8094 buf[0] = VIsual_mode + 's' - 'v';
8095 else
8096 buf[0] = VIsual_mode;
8097 }
8098 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8099 || State == CONFIRM)
8100 {
8101 buf[0] = 'r';
8102 if (State == ASKMORE)
8103 buf[1] = 'm';
8104 else if (State == CONFIRM)
8105 buf[1] = '?';
8106 }
8107 else if (State == EXTERNCMD)
8108 buf[0] = '!';
8109 else if (State & INSERT)
8110 {
8111#ifdef FEAT_VREPLACE
8112 if (State & VREPLACE_FLAG)
8113 {
8114 buf[0] = 'R';
8115 buf[1] = 'v';
8116 }
8117 else
8118#endif
Bram Moolenaare90858d2017-02-01 17:24:34 +01008119 {
8120 if (State & REPLACE_FLAG)
8121 buf[0] = 'R';
8122 else
8123 buf[0] = 'i';
8124#ifdef FEAT_INS_EXPAND
8125 if (ins_compl_active())
8126 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008127 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008128 buf[1] = 'x';
8129#endif
8130 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008131 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008132 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008133 {
8134 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008135 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008136 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008137 else if (exmode_active == EXMODE_NORMAL)
8138 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008139 }
8140 else
8141 {
8142 buf[0] = 'n';
8143 if (finish_op)
8144 buf[1] = 'o';
8145 }
8146
8147 /* Clear out the minor mode when the argument is not a non-zero number or
8148 * non-empty string. */
8149 if (!non_zero_arg(&argvars[0]))
8150 buf[1] = NUL;
8151
8152 rettv->vval.v_string = vim_strsave(buf);
8153 rettv->v_type = VAR_STRING;
8154}
8155
8156#if defined(FEAT_MZSCHEME) || defined(PROTO)
8157/*
8158 * "mzeval()" function
8159 */
8160 static void
8161f_mzeval(typval_T *argvars, typval_T *rettv)
8162{
8163 char_u *str;
8164 char_u buf[NUMBUFLEN];
8165
8166 str = get_tv_string_buf(&argvars[0], buf);
8167 do_mzeval(str, rettv);
8168}
8169
8170 void
8171mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8172{
8173 typval_T argvars[3];
8174
8175 argvars[0].v_type = VAR_STRING;
8176 argvars[0].vval.v_string = name;
8177 copy_tv(args, &argvars[1]);
8178 argvars[2].v_type = VAR_UNKNOWN;
8179 f_call(argvars, rettv);
8180 clear_tv(&argvars[1]);
8181}
8182#endif
8183
8184/*
8185 * "nextnonblank()" function
8186 */
8187 static void
8188f_nextnonblank(typval_T *argvars, typval_T *rettv)
8189{
8190 linenr_T lnum;
8191
8192 for (lnum = get_tv_lnum(argvars); ; ++lnum)
8193 {
8194 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8195 {
8196 lnum = 0;
8197 break;
8198 }
8199 if (*skipwhite(ml_get(lnum)) != NUL)
8200 break;
8201 }
8202 rettv->vval.v_number = lnum;
8203}
8204
8205/*
8206 * "nr2char()" function
8207 */
8208 static void
8209f_nr2char(typval_T *argvars, typval_T *rettv)
8210{
8211 char_u buf[NUMBUFLEN];
8212
8213#ifdef FEAT_MBYTE
8214 if (has_mbyte)
8215 {
8216 int utf8 = 0;
8217
8218 if (argvars[1].v_type != VAR_UNKNOWN)
8219 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
8220 if (utf8)
8221 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8222 else
8223 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8224 }
8225 else
8226#endif
8227 {
8228 buf[0] = (char_u)get_tv_number(&argvars[0]);
8229 buf[1] = NUL;
8230 }
8231 rettv->v_type = VAR_STRING;
8232 rettv->vval.v_string = vim_strsave(buf);
8233}
8234
8235/*
8236 * "or(expr, expr)" function
8237 */
8238 static void
8239f_or(typval_T *argvars, typval_T *rettv)
8240{
8241 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8242 | get_tv_number_chk(&argvars[1], NULL);
8243}
8244
8245/*
8246 * "pathshorten()" function
8247 */
8248 static void
8249f_pathshorten(typval_T *argvars, typval_T *rettv)
8250{
8251 char_u *p;
8252
8253 rettv->v_type = VAR_STRING;
8254 p = get_tv_string_chk(&argvars[0]);
8255 if (p == NULL)
8256 rettv->vval.v_string = NULL;
8257 else
8258 {
8259 p = vim_strsave(p);
8260 rettv->vval.v_string = p;
8261 if (p != NULL)
8262 shorten_dir(p);
8263 }
8264}
8265
8266#ifdef FEAT_PERL
8267/*
8268 * "perleval()" function
8269 */
8270 static void
8271f_perleval(typval_T *argvars, typval_T *rettv)
8272{
8273 char_u *str;
8274 char_u buf[NUMBUFLEN];
8275
8276 str = get_tv_string_buf(&argvars[0], buf);
8277 do_perleval(str, rettv);
8278}
8279#endif
8280
8281#ifdef FEAT_FLOAT
8282/*
8283 * "pow()" function
8284 */
8285 static void
8286f_pow(typval_T *argvars, typval_T *rettv)
8287{
8288 float_T fx = 0.0, fy = 0.0;
8289
8290 rettv->v_type = VAR_FLOAT;
8291 if (get_float_arg(argvars, &fx) == OK
8292 && get_float_arg(&argvars[1], &fy) == OK)
8293 rettv->vval.v_float = pow(fx, fy);
8294 else
8295 rettv->vval.v_float = 0.0;
8296}
8297#endif
8298
8299/*
8300 * "prevnonblank()" function
8301 */
8302 static void
8303f_prevnonblank(typval_T *argvars, typval_T *rettv)
8304{
8305 linenr_T lnum;
8306
8307 lnum = get_tv_lnum(argvars);
8308 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8309 lnum = 0;
8310 else
8311 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8312 --lnum;
8313 rettv->vval.v_number = lnum;
8314}
8315
8316/* This dummy va_list is here because:
8317 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8318 * - locally in the function results in a "used before set" warning
8319 * - using va_start() to initialize it gives "function with fixed args" error */
8320static va_list ap;
8321
8322/*
8323 * "printf()" function
8324 */
8325 static void
8326f_printf(typval_T *argvars, typval_T *rettv)
8327{
8328 char_u buf[NUMBUFLEN];
8329 int len;
8330 char_u *s;
8331 int saved_did_emsg = did_emsg;
8332 char *fmt;
8333
8334 rettv->v_type = VAR_STRING;
8335 rettv->vval.v_string = NULL;
8336
8337 /* Get the required length, allocate the buffer and do it for real. */
8338 did_emsg = FALSE;
8339 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008340 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008341 if (!did_emsg)
8342 {
8343 s = alloc(len + 1);
8344 if (s != NULL)
8345 {
8346 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008347 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8348 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008349 }
8350 }
8351 did_emsg |= saved_did_emsg;
8352}
8353
8354/*
8355 * "pumvisible()" function
8356 */
8357 static void
8358f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8359{
8360#ifdef FEAT_INS_EXPAND
8361 if (pum_visible())
8362 rettv->vval.v_number = 1;
8363#endif
8364}
8365
8366#ifdef FEAT_PYTHON3
8367/*
8368 * "py3eval()" function
8369 */
8370 static void
8371f_py3eval(typval_T *argvars, typval_T *rettv)
8372{
8373 char_u *str;
8374 char_u buf[NUMBUFLEN];
8375
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008376 if (p_pyx == 0)
8377 p_pyx = 3;
8378
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008379 str = get_tv_string_buf(&argvars[0], buf);
8380 do_py3eval(str, rettv);
8381}
8382#endif
8383
8384#ifdef FEAT_PYTHON
8385/*
8386 * "pyeval()" function
8387 */
8388 static void
8389f_pyeval(typval_T *argvars, typval_T *rettv)
8390{
8391 char_u *str;
8392 char_u buf[NUMBUFLEN];
8393
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008394 if (p_pyx == 0)
8395 p_pyx = 2;
8396
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008397 str = get_tv_string_buf(&argvars[0], buf);
8398 do_pyeval(str, rettv);
8399}
8400#endif
8401
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008402#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8403/*
8404 * "pyxeval()" function
8405 */
8406 static void
8407f_pyxeval(typval_T *argvars, typval_T *rettv)
8408{
8409# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8410 init_pyxversion();
8411 if (p_pyx == 2)
8412 f_pyeval(argvars, rettv);
8413 else
8414 f_py3eval(argvars, rettv);
8415# elif defined(FEAT_PYTHON)
8416 f_pyeval(argvars, rettv);
8417# elif defined(FEAT_PYTHON3)
8418 f_py3eval(argvars, rettv);
8419# endif
8420}
8421#endif
8422
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008423/*
8424 * "range()" function
8425 */
8426 static void
8427f_range(typval_T *argvars, typval_T *rettv)
8428{
8429 varnumber_T start;
8430 varnumber_T end;
8431 varnumber_T stride = 1;
8432 varnumber_T i;
8433 int error = FALSE;
8434
8435 start = get_tv_number_chk(&argvars[0], &error);
8436 if (argvars[1].v_type == VAR_UNKNOWN)
8437 {
8438 end = start - 1;
8439 start = 0;
8440 }
8441 else
8442 {
8443 end = get_tv_number_chk(&argvars[1], &error);
8444 if (argvars[2].v_type != VAR_UNKNOWN)
8445 stride = get_tv_number_chk(&argvars[2], &error);
8446 }
8447
8448 if (error)
8449 return; /* type error; errmsg already given */
8450 if (stride == 0)
8451 EMSG(_("E726: Stride is zero"));
8452 else if (stride > 0 ? end + 1 < start : end - 1 > start)
8453 EMSG(_("E727: Start past end"));
8454 else
8455 {
8456 if (rettv_list_alloc(rettv) == OK)
8457 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8458 if (list_append_number(rettv->vval.v_list,
8459 (varnumber_T)i) == FAIL)
8460 break;
8461 }
8462}
8463
8464/*
8465 * "readfile()" function
8466 */
8467 static void
8468f_readfile(typval_T *argvars, typval_T *rettv)
8469{
8470 int binary = FALSE;
8471 int failed = FALSE;
8472 char_u *fname;
8473 FILE *fd;
8474 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8475 int io_size = sizeof(buf);
8476 int readlen; /* size of last fread() */
8477 char_u *prev = NULL; /* previously read bytes, if any */
8478 long prevlen = 0; /* length of data in prev */
8479 long prevsize = 0; /* size of prev buffer */
8480 long maxline = MAXLNUM;
8481 long cnt = 0;
8482 char_u *p; /* position in buf */
8483 char_u *start; /* start of current line */
8484
8485 if (argvars[1].v_type != VAR_UNKNOWN)
8486 {
8487 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
8488 binary = TRUE;
8489 if (argvars[2].v_type != VAR_UNKNOWN)
8490 maxline = (long)get_tv_number(&argvars[2]);
8491 }
8492
8493 if (rettv_list_alloc(rettv) == FAIL)
8494 return;
8495
8496 /* Always open the file in binary mode, library functions have a mind of
8497 * their own about CR-LF conversion. */
8498 fname = get_tv_string(&argvars[0]);
8499 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8500 {
8501 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
8502 return;
8503 }
8504
8505 while (cnt < maxline || maxline < 0)
8506 {
8507 readlen = (int)fread(buf, 1, io_size, fd);
8508
8509 /* This for loop processes what was read, but is also entered at end
8510 * of file so that either:
8511 * - an incomplete line gets written
8512 * - a "binary" file gets an empty line at the end if it ends in a
8513 * newline. */
8514 for (p = buf, start = buf;
8515 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8516 ++p)
8517 {
8518 if (*p == '\n' || readlen <= 0)
8519 {
8520 listitem_T *li;
8521 char_u *s = NULL;
8522 long_u len = p - start;
8523
8524 /* Finished a line. Remove CRs before NL. */
8525 if (readlen > 0 && !binary)
8526 {
8527 while (len > 0 && start[len - 1] == '\r')
8528 --len;
8529 /* removal may cross back to the "prev" string */
8530 if (len == 0)
8531 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8532 --prevlen;
8533 }
8534 if (prevlen == 0)
8535 s = vim_strnsave(start, (int)len);
8536 else
8537 {
8538 /* Change "prev" buffer to be the right size. This way
8539 * the bytes are only copied once, and very long lines are
8540 * allocated only once. */
8541 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8542 {
8543 mch_memmove(s + prevlen, start, len);
8544 s[prevlen + len] = NUL;
8545 prev = NULL; /* the list will own the string */
8546 prevlen = prevsize = 0;
8547 }
8548 }
8549 if (s == NULL)
8550 {
8551 do_outofmem_msg((long_u) prevlen + len + 1);
8552 failed = TRUE;
8553 break;
8554 }
8555
8556 if ((li = listitem_alloc()) == NULL)
8557 {
8558 vim_free(s);
8559 failed = TRUE;
8560 break;
8561 }
8562 li->li_tv.v_type = VAR_STRING;
8563 li->li_tv.v_lock = 0;
8564 li->li_tv.vval.v_string = s;
8565 list_append(rettv->vval.v_list, li);
8566
8567 start = p + 1; /* step over newline */
8568 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
8569 break;
8570 }
8571 else if (*p == NUL)
8572 *p = '\n';
8573#ifdef FEAT_MBYTE
8574 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
8575 * when finding the BF and check the previous two bytes. */
8576 else if (*p == 0xbf && enc_utf8 && !binary)
8577 {
8578 /* Find the two bytes before the 0xbf. If p is at buf, or buf
8579 * + 1, these may be in the "prev" string. */
8580 char_u back1 = p >= buf + 1 ? p[-1]
8581 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
8582 char_u back2 = p >= buf + 2 ? p[-2]
8583 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
8584 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
8585
8586 if (back2 == 0xef && back1 == 0xbb)
8587 {
8588 char_u *dest = p - 2;
8589
8590 /* Usually a BOM is at the beginning of a file, and so at
8591 * the beginning of a line; then we can just step over it.
8592 */
8593 if (start == dest)
8594 start = p + 1;
8595 else
8596 {
8597 /* have to shuffle buf to close gap */
8598 int adjust_prevlen = 0;
8599
8600 if (dest < buf)
8601 {
8602 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
8603 dest = buf;
8604 }
8605 if (readlen > p - buf + 1)
8606 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
8607 readlen -= 3 - adjust_prevlen;
8608 prevlen -= adjust_prevlen;
8609 p = dest - 1;
8610 }
8611 }
8612 }
8613#endif
8614 } /* for */
8615
8616 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
8617 break;
8618 if (start < p)
8619 {
8620 /* There's part of a line in buf, store it in "prev". */
8621 if (p - start + prevlen >= prevsize)
8622 {
8623 /* need bigger "prev" buffer */
8624 char_u *newprev;
8625
8626 /* A common use case is ordinary text files and "prev" gets a
8627 * fragment of a line, so the first allocation is made
8628 * small, to avoid repeatedly 'allocing' large and
8629 * 'reallocing' small. */
8630 if (prevsize == 0)
8631 prevsize = (long)(p - start);
8632 else
8633 {
8634 long grow50pc = (prevsize * 3) / 2;
8635 long growmin = (long)((p - start) * 2 + prevlen);
8636 prevsize = grow50pc > growmin ? grow50pc : growmin;
8637 }
8638 newprev = prev == NULL ? alloc(prevsize)
8639 : vim_realloc(prev, prevsize);
8640 if (newprev == NULL)
8641 {
8642 do_outofmem_msg((long_u)prevsize);
8643 failed = TRUE;
8644 break;
8645 }
8646 prev = newprev;
8647 }
8648 /* Add the line part to end of "prev". */
8649 mch_memmove(prev + prevlen, start, p - start);
8650 prevlen += (long)(p - start);
8651 }
8652 } /* while */
8653
8654 /*
8655 * For a negative line count use only the lines at the end of the file,
8656 * free the rest.
8657 */
8658 if (!failed && maxline < 0)
8659 while (cnt > -maxline)
8660 {
8661 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
8662 --cnt;
8663 }
8664
8665 if (failed)
8666 {
8667 list_free(rettv->vval.v_list);
8668 /* readfile doc says an empty list is returned on error */
8669 rettv->vval.v_list = list_alloc();
8670 }
8671
8672 vim_free(prev);
8673 fclose(fd);
8674}
8675
8676#if defined(FEAT_RELTIME)
8677static int list2proftime(typval_T *arg, proftime_T *tm);
8678
8679/*
8680 * Convert a List to proftime_T.
8681 * Return FAIL when there is something wrong.
8682 */
8683 static int
8684list2proftime(typval_T *arg, proftime_T *tm)
8685{
8686 long n1, n2;
8687 int error = FALSE;
8688
8689 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
8690 || arg->vval.v_list->lv_len != 2)
8691 return FAIL;
8692 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
8693 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
8694# ifdef WIN3264
8695 tm->HighPart = n1;
8696 tm->LowPart = n2;
8697# else
8698 tm->tv_sec = n1;
8699 tm->tv_usec = n2;
8700# endif
8701 return error ? FAIL : OK;
8702}
8703#endif /* FEAT_RELTIME */
8704
8705/*
8706 * "reltime()" function
8707 */
8708 static void
8709f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8710{
8711#ifdef FEAT_RELTIME
8712 proftime_T res;
8713 proftime_T start;
8714
8715 if (argvars[0].v_type == VAR_UNKNOWN)
8716 {
8717 /* No arguments: get current time. */
8718 profile_start(&res);
8719 }
8720 else if (argvars[1].v_type == VAR_UNKNOWN)
8721 {
8722 if (list2proftime(&argvars[0], &res) == FAIL)
8723 return;
8724 profile_end(&res);
8725 }
8726 else
8727 {
8728 /* Two arguments: compute the difference. */
8729 if (list2proftime(&argvars[0], &start) == FAIL
8730 || list2proftime(&argvars[1], &res) == FAIL)
8731 return;
8732 profile_sub(&res, &start);
8733 }
8734
8735 if (rettv_list_alloc(rettv) == OK)
8736 {
8737 long n1, n2;
8738
8739# ifdef WIN3264
8740 n1 = res.HighPart;
8741 n2 = res.LowPart;
8742# else
8743 n1 = res.tv_sec;
8744 n2 = res.tv_usec;
8745# endif
8746 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
8747 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
8748 }
8749#endif
8750}
8751
8752#ifdef FEAT_FLOAT
8753/*
8754 * "reltimefloat()" function
8755 */
8756 static void
8757f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
8758{
8759# ifdef FEAT_RELTIME
8760 proftime_T tm;
8761# endif
8762
8763 rettv->v_type = VAR_FLOAT;
8764 rettv->vval.v_float = 0;
8765# ifdef FEAT_RELTIME
8766 if (list2proftime(&argvars[0], &tm) == OK)
8767 rettv->vval.v_float = profile_float(&tm);
8768# endif
8769}
8770#endif
8771
8772/*
8773 * "reltimestr()" function
8774 */
8775 static void
8776f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
8777{
8778#ifdef FEAT_RELTIME
8779 proftime_T tm;
8780#endif
8781
8782 rettv->v_type = VAR_STRING;
8783 rettv->vval.v_string = NULL;
8784#ifdef FEAT_RELTIME
8785 if (list2proftime(&argvars[0], &tm) == OK)
8786 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
8787#endif
8788}
8789
8790#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
8791static void make_connection(void);
8792static int check_connection(void);
8793
8794 static void
8795make_connection(void)
8796{
8797 if (X_DISPLAY == NULL
8798# ifdef FEAT_GUI
8799 && !gui.in_use
8800# endif
8801 )
8802 {
8803 x_force_connect = TRUE;
8804 setup_term_clip();
8805 x_force_connect = FALSE;
8806 }
8807}
8808
8809 static int
8810check_connection(void)
8811{
8812 make_connection();
8813 if (X_DISPLAY == NULL)
8814 {
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01008815 EMSG(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008816 return FAIL;
8817 }
8818 return OK;
8819}
8820#endif
8821
8822#ifdef FEAT_CLIENTSERVER
8823 static void
8824remote_common(typval_T *argvars, typval_T *rettv, int expr)
8825{
8826 char_u *server_name;
8827 char_u *keys;
8828 char_u *r = NULL;
8829 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008830 int timeout = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008831# ifdef WIN32
8832 HWND w;
8833# else
8834 Window w;
8835# endif
8836
8837 if (check_restricted() || check_secure())
8838 return;
8839
8840# ifdef FEAT_X11
8841 if (check_connection() == FAIL)
8842 return;
8843# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008844 if (argvars[2].v_type != VAR_UNKNOWN
8845 && argvars[3].v_type != VAR_UNKNOWN)
8846 timeout = get_tv_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008847
8848 server_name = get_tv_string_chk(&argvars[0]);
8849 if (server_name == NULL)
8850 return; /* type error; errmsg already given */
8851 keys = get_tv_string_buf(&argvars[1], buf);
8852# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008853 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008854# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008855 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
8856 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008857# endif
8858 {
8859 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008860 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008861 EMSG(r); /* sending worked but evaluation failed */
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008862 vim_free(r);
8863 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008864 else
8865 EMSG2(_("E241: Unable to send to %s"), server_name);
8866 return;
8867 }
8868
8869 rettv->vval.v_string = r;
8870
8871 if (argvars[2].v_type != VAR_UNKNOWN)
8872 {
8873 dictitem_T v;
8874 char_u str[30];
8875 char_u *idvar;
8876
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008877 idvar = get_tv_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008878 if (idvar != NULL && *idvar != NUL)
8879 {
8880 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
8881 v.di_tv.v_type = VAR_STRING;
8882 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008883 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008884 vim_free(v.di_tv.vval.v_string);
8885 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008886 }
8887}
8888#endif
8889
8890/*
8891 * "remote_expr()" function
8892 */
8893 static void
8894f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
8895{
8896 rettv->v_type = VAR_STRING;
8897 rettv->vval.v_string = NULL;
8898#ifdef FEAT_CLIENTSERVER
8899 remote_common(argvars, rettv, TRUE);
8900#endif
8901}
8902
8903/*
8904 * "remote_foreground()" function
8905 */
8906 static void
8907f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8908{
8909#ifdef FEAT_CLIENTSERVER
8910# ifdef WIN32
8911 /* On Win32 it's done in this application. */
8912 {
8913 char_u *server_name = get_tv_string_chk(&argvars[0]);
8914
8915 if (server_name != NULL)
8916 serverForeground(server_name);
8917 }
8918# else
8919 /* Send a foreground() expression to the server. */
8920 argvars[1].v_type = VAR_STRING;
8921 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
8922 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008923 rettv->v_type = VAR_STRING;
8924 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008925 remote_common(argvars, rettv, TRUE);
8926 vim_free(argvars[1].vval.v_string);
8927# endif
8928#endif
8929}
8930
8931 static void
8932f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
8933{
8934#ifdef FEAT_CLIENTSERVER
8935 dictitem_T v;
8936 char_u *s = NULL;
8937# ifdef WIN32
8938 long_u n = 0;
8939# endif
8940 char_u *serverid;
8941
8942 if (check_restricted() || check_secure())
8943 {
8944 rettv->vval.v_number = -1;
8945 return;
8946 }
8947 serverid = get_tv_string_chk(&argvars[0]);
8948 if (serverid == NULL)
8949 {
8950 rettv->vval.v_number = -1;
8951 return; /* type error; errmsg already given */
8952 }
8953# ifdef WIN32
8954 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
8955 if (n == 0)
8956 rettv->vval.v_number = -1;
8957 else
8958 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008959 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008960 rettv->vval.v_number = (s != NULL);
8961 }
8962# else
8963 if (check_connection() == FAIL)
8964 return;
8965
8966 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
8967 serverStrToWin(serverid), &s);
8968# endif
8969
8970 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
8971 {
8972 char_u *retvar;
8973
8974 v.di_tv.v_type = VAR_STRING;
8975 v.di_tv.vval.v_string = vim_strsave(s);
8976 retvar = get_tv_string_chk(&argvars[1]);
8977 if (retvar != NULL)
8978 set_var(retvar, &v.di_tv, FALSE);
8979 vim_free(v.di_tv.vval.v_string);
8980 }
8981#else
8982 rettv->vval.v_number = -1;
8983#endif
8984}
8985
8986 static void
8987f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
8988{
8989 char_u *r = NULL;
8990
8991#ifdef FEAT_CLIENTSERVER
8992 char_u *serverid = get_tv_string_chk(&argvars[0]);
8993
8994 if (serverid != NULL && !check_restricted() && !check_secure())
8995 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008996 int timeout = 0;
Bram Moolenaar1662ce12017-03-19 21:47:50 +01008997# ifdef WIN32
8998 /* The server's HWND is encoded in the 'id' parameter */
8999 long_u n = 0;
9000# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009001
9002 if (argvars[1].v_type != VAR_UNKNOWN)
9003 timeout = get_tv_number(&argvars[1]);
9004
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009005# ifdef WIN32
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009006 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9007 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009008 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009009 if (r == NULL)
9010# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009011 if (check_connection() == FAIL
9012 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9013 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009014# endif
9015 EMSG(_("E277: Unable to read a server reply"));
9016 }
9017#endif
9018 rettv->v_type = VAR_STRING;
9019 rettv->vval.v_string = r;
9020}
9021
9022/*
9023 * "remote_send()" function
9024 */
9025 static void
9026f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9027{
9028 rettv->v_type = VAR_STRING;
9029 rettv->vval.v_string = NULL;
9030#ifdef FEAT_CLIENTSERVER
9031 remote_common(argvars, rettv, FALSE);
9032#endif
9033}
9034
9035/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009036 * "remote_startserver()" function
9037 */
9038 static void
9039f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9040{
9041#ifdef FEAT_CLIENTSERVER
9042 char_u *server = get_tv_string_chk(&argvars[0]);
9043
9044 if (server == NULL)
9045 return; /* type error; errmsg already given */
9046 if (serverName != NULL)
9047 EMSG(_("E941: already started a server"));
9048 else
9049 {
9050# ifdef FEAT_X11
9051 if (check_connection() == OK)
9052 serverRegisterName(X_DISPLAY, server);
9053# else
9054 serverSetName(server);
9055# endif
9056 }
9057#else
9058 EMSG(_("E942: +clientserver feature not available"));
9059#endif
9060}
9061
9062/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009063 * "remove()" function
9064 */
9065 static void
9066f_remove(typval_T *argvars, typval_T *rettv)
9067{
9068 list_T *l;
9069 listitem_T *item, *item2;
9070 listitem_T *li;
9071 long idx;
9072 long end;
9073 char_u *key;
9074 dict_T *d;
9075 dictitem_T *di;
9076 char_u *arg_errmsg = (char_u *)N_("remove() argument");
9077
9078 if (argvars[0].v_type == VAR_DICT)
9079 {
9080 if (argvars[2].v_type != VAR_UNKNOWN)
9081 EMSG2(_(e_toomanyarg), "remove()");
9082 else if ((d = argvars[0].vval.v_dict) != NULL
9083 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
9084 {
9085 key = get_tv_string_chk(&argvars[1]);
9086 if (key != NULL)
9087 {
9088 di = dict_find(d, key, -1);
9089 if (di == NULL)
9090 EMSG2(_(e_dictkey), key);
9091 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
9092 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
9093 {
9094 *rettv = di->di_tv;
9095 init_tv(&di->di_tv);
9096 dictitem_remove(d, di);
9097 }
9098 }
9099 }
9100 }
9101 else if (argvars[0].v_type != VAR_LIST)
9102 EMSG2(_(e_listdictarg), "remove()");
9103 else if ((l = argvars[0].vval.v_list) != NULL
9104 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
9105 {
9106 int error = FALSE;
9107
9108 idx = (long)get_tv_number_chk(&argvars[1], &error);
9109 if (error)
9110 ; /* type error: do nothing, errmsg already given */
9111 else if ((item = list_find(l, idx)) == NULL)
9112 EMSGN(_(e_listidx), idx);
9113 else
9114 {
9115 if (argvars[2].v_type == VAR_UNKNOWN)
9116 {
9117 /* Remove one item, return its value. */
9118 vimlist_remove(l, item, item);
9119 *rettv = item->li_tv;
9120 vim_free(item);
9121 }
9122 else
9123 {
9124 /* Remove range of items, return list with values. */
9125 end = (long)get_tv_number_chk(&argvars[2], &error);
9126 if (error)
9127 ; /* type error: do nothing */
9128 else if ((item2 = list_find(l, end)) == NULL)
9129 EMSGN(_(e_listidx), end);
9130 else
9131 {
9132 int cnt = 0;
9133
9134 for (li = item; li != NULL; li = li->li_next)
9135 {
9136 ++cnt;
9137 if (li == item2)
9138 break;
9139 }
9140 if (li == NULL) /* didn't find "item2" after "item" */
9141 EMSG(_(e_invrange));
9142 else
9143 {
9144 vimlist_remove(l, item, item2);
9145 if (rettv_list_alloc(rettv) == OK)
9146 {
9147 l = rettv->vval.v_list;
9148 l->lv_first = item;
9149 l->lv_last = item2;
9150 item->li_prev = NULL;
9151 item2->li_next = NULL;
9152 l->lv_len = cnt;
9153 }
9154 }
9155 }
9156 }
9157 }
9158 }
9159}
9160
9161/*
9162 * "rename({from}, {to})" function
9163 */
9164 static void
9165f_rename(typval_T *argvars, typval_T *rettv)
9166{
9167 char_u buf[NUMBUFLEN];
9168
9169 if (check_restricted() || check_secure())
9170 rettv->vval.v_number = -1;
9171 else
9172 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
9173 get_tv_string_buf(&argvars[1], buf));
9174}
9175
9176/*
9177 * "repeat()" function
9178 */
9179 static void
9180f_repeat(typval_T *argvars, typval_T *rettv)
9181{
9182 char_u *p;
9183 int n;
9184 int slen;
9185 int len;
9186 char_u *r;
9187 int i;
9188
9189 n = (int)get_tv_number(&argvars[1]);
9190 if (argvars[0].v_type == VAR_LIST)
9191 {
9192 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9193 while (n-- > 0)
9194 if (list_extend(rettv->vval.v_list,
9195 argvars[0].vval.v_list, NULL) == FAIL)
9196 break;
9197 }
9198 else
9199 {
9200 p = get_tv_string(&argvars[0]);
9201 rettv->v_type = VAR_STRING;
9202 rettv->vval.v_string = NULL;
9203
9204 slen = (int)STRLEN(p);
9205 len = slen * n;
9206 if (len <= 0)
9207 return;
9208
9209 r = alloc(len + 1);
9210 if (r != NULL)
9211 {
9212 for (i = 0; i < n; i++)
9213 mch_memmove(r + i * slen, p, (size_t)slen);
9214 r[len] = NUL;
9215 }
9216
9217 rettv->vval.v_string = r;
9218 }
9219}
9220
9221/*
9222 * "resolve()" function
9223 */
9224 static void
9225f_resolve(typval_T *argvars, typval_T *rettv)
9226{
9227 char_u *p;
9228#ifdef HAVE_READLINK
9229 char_u *buf = NULL;
9230#endif
9231
9232 p = get_tv_string(&argvars[0]);
9233#ifdef FEAT_SHORTCUT
9234 {
9235 char_u *v = NULL;
9236
9237 v = mch_resolve_shortcut(p);
9238 if (v != NULL)
9239 rettv->vval.v_string = v;
9240 else
9241 rettv->vval.v_string = vim_strsave(p);
9242 }
9243#else
9244# ifdef HAVE_READLINK
9245 {
9246 char_u *cpy;
9247 int len;
9248 char_u *remain = NULL;
9249 char_u *q;
9250 int is_relative_to_current = FALSE;
9251 int has_trailing_pathsep = FALSE;
9252 int limit = 100;
9253
9254 p = vim_strsave(p);
9255
9256 if (p[0] == '.' && (vim_ispathsep(p[1])
9257 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9258 is_relative_to_current = TRUE;
9259
9260 len = STRLEN(p);
9261 if (len > 0 && after_pathsep(p, p + len))
9262 {
9263 has_trailing_pathsep = TRUE;
9264 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9265 }
9266
9267 q = getnextcomp(p);
9268 if (*q != NUL)
9269 {
9270 /* Separate the first path component in "p", and keep the
9271 * remainder (beginning with the path separator). */
9272 remain = vim_strsave(q - 1);
9273 q[-1] = NUL;
9274 }
9275
9276 buf = alloc(MAXPATHL + 1);
9277 if (buf == NULL)
9278 goto fail;
9279
9280 for (;;)
9281 {
9282 for (;;)
9283 {
9284 len = readlink((char *)p, (char *)buf, MAXPATHL);
9285 if (len <= 0)
9286 break;
9287 buf[len] = NUL;
9288
9289 if (limit-- == 0)
9290 {
9291 vim_free(p);
9292 vim_free(remain);
9293 EMSG(_("E655: Too many symbolic links (cycle?)"));
9294 rettv->vval.v_string = NULL;
9295 goto fail;
9296 }
9297
9298 /* Ensure that the result will have a trailing path separator
9299 * if the argument has one. */
9300 if (remain == NULL && has_trailing_pathsep)
9301 add_pathsep(buf);
9302
9303 /* Separate the first path component in the link value and
9304 * concatenate the remainders. */
9305 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9306 if (*q != NUL)
9307 {
9308 if (remain == NULL)
9309 remain = vim_strsave(q - 1);
9310 else
9311 {
9312 cpy = concat_str(q - 1, remain);
9313 if (cpy != NULL)
9314 {
9315 vim_free(remain);
9316 remain = cpy;
9317 }
9318 }
9319 q[-1] = NUL;
9320 }
9321
9322 q = gettail(p);
9323 if (q > p && *q == NUL)
9324 {
9325 /* Ignore trailing path separator. */
9326 q[-1] = NUL;
9327 q = gettail(p);
9328 }
9329 if (q > p && !mch_isFullName(buf))
9330 {
9331 /* symlink is relative to directory of argument */
9332 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
9333 if (cpy != NULL)
9334 {
9335 STRCPY(cpy, p);
9336 STRCPY(gettail(cpy), buf);
9337 vim_free(p);
9338 p = cpy;
9339 }
9340 }
9341 else
9342 {
9343 vim_free(p);
9344 p = vim_strsave(buf);
9345 }
9346 }
9347
9348 if (remain == NULL)
9349 break;
9350
9351 /* Append the first path component of "remain" to "p". */
9352 q = getnextcomp(remain + 1);
9353 len = q - remain - (*q != NUL);
9354 cpy = vim_strnsave(p, STRLEN(p) + len);
9355 if (cpy != NULL)
9356 {
9357 STRNCAT(cpy, remain, len);
9358 vim_free(p);
9359 p = cpy;
9360 }
9361 /* Shorten "remain". */
9362 if (*q != NUL)
9363 STRMOVE(remain, q - 1);
9364 else
Bram Moolenaard23a8232018-02-10 18:45:26 +01009365 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009366 }
9367
9368 /* If the result is a relative path name, make it explicitly relative to
9369 * the current directory if and only if the argument had this form. */
9370 if (!vim_ispathsep(*p))
9371 {
9372 if (is_relative_to_current
9373 && *p != NUL
9374 && !(p[0] == '.'
9375 && (p[1] == NUL
9376 || vim_ispathsep(p[1])
9377 || (p[1] == '.'
9378 && (p[2] == NUL
9379 || vim_ispathsep(p[2]))))))
9380 {
9381 /* Prepend "./". */
9382 cpy = concat_str((char_u *)"./", p);
9383 if (cpy != NULL)
9384 {
9385 vim_free(p);
9386 p = cpy;
9387 }
9388 }
9389 else if (!is_relative_to_current)
9390 {
9391 /* Strip leading "./". */
9392 q = p;
9393 while (q[0] == '.' && vim_ispathsep(q[1]))
9394 q += 2;
9395 if (q > p)
9396 STRMOVE(p, p + 2);
9397 }
9398 }
9399
9400 /* Ensure that the result will have no trailing path separator
9401 * if the argument had none. But keep "/" or "//". */
9402 if (!has_trailing_pathsep)
9403 {
9404 q = p + STRLEN(p);
9405 if (after_pathsep(p, q))
9406 *gettail_sep(p) = NUL;
9407 }
9408
9409 rettv->vval.v_string = p;
9410 }
9411# else
9412 rettv->vval.v_string = vim_strsave(p);
9413# endif
9414#endif
9415
9416 simplify_filename(rettv->vval.v_string);
9417
9418#ifdef HAVE_READLINK
9419fail:
9420 vim_free(buf);
9421#endif
9422 rettv->v_type = VAR_STRING;
9423}
9424
9425/*
9426 * "reverse({list})" function
9427 */
9428 static void
9429f_reverse(typval_T *argvars, typval_T *rettv)
9430{
9431 list_T *l;
9432 listitem_T *li, *ni;
9433
9434 if (argvars[0].v_type != VAR_LIST)
9435 EMSG2(_(e_listarg), "reverse()");
9436 else if ((l = argvars[0].vval.v_list) != NULL
9437 && !tv_check_lock(l->lv_lock,
9438 (char_u *)N_("reverse() argument"), TRUE))
9439 {
9440 li = l->lv_last;
9441 l->lv_first = l->lv_last = NULL;
9442 l->lv_len = 0;
9443 while (li != NULL)
9444 {
9445 ni = li->li_prev;
9446 list_append(l, li);
9447 li = ni;
9448 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009449 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009450 l->lv_idx = l->lv_len - l->lv_idx - 1;
9451 }
9452}
9453
9454#define SP_NOMOVE 0x01 /* don't move cursor */
9455#define SP_REPEAT 0x02 /* repeat to find outer pair */
9456#define SP_RETCOUNT 0x04 /* return matchcount */
9457#define SP_SETPCMARK 0x08 /* set previous context mark */
9458#define SP_START 0x10 /* accept match at start position */
9459#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9460#define SP_END 0x40 /* leave cursor at end of match */
9461#define SP_COLUMN 0x80 /* start at cursor column */
9462
9463static int get_search_arg(typval_T *varp, int *flagsp);
9464
9465/*
9466 * Get flags for a search function.
9467 * Possibly sets "p_ws".
9468 * Returns BACKWARD, FORWARD or zero (for an error).
9469 */
9470 static int
9471get_search_arg(typval_T *varp, int *flagsp)
9472{
9473 int dir = FORWARD;
9474 char_u *flags;
9475 char_u nbuf[NUMBUFLEN];
9476 int mask;
9477
9478 if (varp->v_type != VAR_UNKNOWN)
9479 {
9480 flags = get_tv_string_buf_chk(varp, nbuf);
9481 if (flags == NULL)
9482 return 0; /* type error; errmsg already given */
9483 while (*flags != NUL)
9484 {
9485 switch (*flags)
9486 {
9487 case 'b': dir = BACKWARD; break;
9488 case 'w': p_ws = TRUE; break;
9489 case 'W': p_ws = FALSE; break;
9490 default: mask = 0;
9491 if (flagsp != NULL)
9492 switch (*flags)
9493 {
9494 case 'c': mask = SP_START; break;
9495 case 'e': mask = SP_END; break;
9496 case 'm': mask = SP_RETCOUNT; break;
9497 case 'n': mask = SP_NOMOVE; break;
9498 case 'p': mask = SP_SUBPAT; break;
9499 case 'r': mask = SP_REPEAT; break;
9500 case 's': mask = SP_SETPCMARK; break;
9501 case 'z': mask = SP_COLUMN; break;
9502 }
9503 if (mask == 0)
9504 {
9505 EMSG2(_(e_invarg2), flags);
9506 dir = 0;
9507 }
9508 else
9509 *flagsp |= mask;
9510 }
9511 if (dir == 0)
9512 break;
9513 ++flags;
9514 }
9515 }
9516 return dir;
9517}
9518
9519/*
9520 * Shared by search() and searchpos() functions.
9521 */
9522 static int
9523search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9524{
9525 int flags;
9526 char_u *pat;
9527 pos_T pos;
9528 pos_T save_cursor;
9529 int save_p_ws = p_ws;
9530 int dir;
9531 int retval = 0; /* default: FAIL */
9532 long lnum_stop = 0;
9533 proftime_T tm;
9534#ifdef FEAT_RELTIME
9535 long time_limit = 0;
9536#endif
9537 int options = SEARCH_KEEP;
9538 int subpatnum;
9539
9540 pat = get_tv_string(&argvars[0]);
9541 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
9542 if (dir == 0)
9543 goto theend;
9544 flags = *flagsp;
9545 if (flags & SP_START)
9546 options |= SEARCH_START;
9547 if (flags & SP_END)
9548 options |= SEARCH_END;
9549 if (flags & SP_COLUMN)
9550 options |= SEARCH_COL;
9551
9552 /* Optional arguments: line number to stop searching and timeout. */
9553 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9554 {
9555 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
9556 if (lnum_stop < 0)
9557 goto theend;
9558#ifdef FEAT_RELTIME
9559 if (argvars[3].v_type != VAR_UNKNOWN)
9560 {
9561 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
9562 if (time_limit < 0)
9563 goto theend;
9564 }
9565#endif
9566 }
9567
9568#ifdef FEAT_RELTIME
9569 /* Set the time limit, if there is one. */
9570 profile_setlimit(time_limit, &tm);
9571#endif
9572
9573 /*
9574 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
9575 * Check to make sure only those flags are set.
9576 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
9577 * flags cannot be set. Check for that condition also.
9578 */
9579 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
9580 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9581 {
9582 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
9583 goto theend;
9584 }
9585
9586 pos = save_cursor = curwin->w_cursor;
9587 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009588 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009589 if (subpatnum != FAIL)
9590 {
9591 if (flags & SP_SUBPAT)
9592 retval = subpatnum;
9593 else
9594 retval = pos.lnum;
9595 if (flags & SP_SETPCMARK)
9596 setpcmark();
9597 curwin->w_cursor = pos;
9598 if (match_pos != NULL)
9599 {
9600 /* Store the match cursor position */
9601 match_pos->lnum = pos.lnum;
9602 match_pos->col = pos.col + 1;
9603 }
9604 /* "/$" will put the cursor after the end of the line, may need to
9605 * correct that here */
9606 check_cursor();
9607 }
9608
9609 /* If 'n' flag is used: restore cursor position. */
9610 if (flags & SP_NOMOVE)
9611 curwin->w_cursor = save_cursor;
9612 else
9613 curwin->w_set_curswant = TRUE;
9614theend:
9615 p_ws = save_p_ws;
9616
9617 return retval;
9618}
9619
9620#ifdef FEAT_FLOAT
9621
9622/*
9623 * round() is not in C90, use ceil() or floor() instead.
9624 */
9625 float_T
9626vim_round(float_T f)
9627{
9628 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
9629}
9630
9631/*
9632 * "round({float})" function
9633 */
9634 static void
9635f_round(typval_T *argvars, typval_T *rettv)
9636{
9637 float_T f = 0.0;
9638
9639 rettv->v_type = VAR_FLOAT;
9640 if (get_float_arg(argvars, &f) == OK)
9641 rettv->vval.v_float = vim_round(f);
9642 else
9643 rettv->vval.v_float = 0.0;
9644}
9645#endif
9646
9647/*
9648 * "screenattr()" function
9649 */
9650 static void
9651f_screenattr(typval_T *argvars, typval_T *rettv)
9652{
9653 int row;
9654 int col;
9655 int c;
9656
9657 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9658 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9659 if (row < 0 || row >= screen_Rows
9660 || col < 0 || col >= screen_Columns)
9661 c = -1;
9662 else
9663 c = ScreenAttrs[LineOffset[row] + col];
9664 rettv->vval.v_number = c;
9665}
9666
9667/*
9668 * "screenchar()" function
9669 */
9670 static void
9671f_screenchar(typval_T *argvars, typval_T *rettv)
9672{
9673 int row;
9674 int col;
9675 int off;
9676 int c;
9677
9678 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9679 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9680 if (row < 0 || row >= screen_Rows
9681 || col < 0 || col >= screen_Columns)
9682 c = -1;
9683 else
9684 {
9685 off = LineOffset[row] + col;
9686#ifdef FEAT_MBYTE
9687 if (enc_utf8 && ScreenLinesUC[off] != 0)
9688 c = ScreenLinesUC[off];
9689 else
9690#endif
9691 c = ScreenLines[off];
9692 }
9693 rettv->vval.v_number = c;
9694}
9695
9696/*
9697 * "screencol()" function
9698 *
9699 * First column is 1 to be consistent with virtcol().
9700 */
9701 static void
9702f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
9703{
9704 rettv->vval.v_number = screen_screencol() + 1;
9705}
9706
9707/*
9708 * "screenrow()" function
9709 */
9710 static void
9711f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
9712{
9713 rettv->vval.v_number = screen_screenrow() + 1;
9714}
9715
9716/*
9717 * "search()" function
9718 */
9719 static void
9720f_search(typval_T *argvars, typval_T *rettv)
9721{
9722 int flags = 0;
9723
9724 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
9725}
9726
9727/*
9728 * "searchdecl()" function
9729 */
9730 static void
9731f_searchdecl(typval_T *argvars, typval_T *rettv)
9732{
9733 int locally = 1;
9734 int thisblock = 0;
9735 int error = FALSE;
9736 char_u *name;
9737
9738 rettv->vval.v_number = 1; /* default: FAIL */
9739
9740 name = get_tv_string_chk(&argvars[0]);
9741 if (argvars[1].v_type != VAR_UNKNOWN)
9742 {
9743 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
9744 if (!error && argvars[2].v_type != VAR_UNKNOWN)
9745 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
9746 }
9747 if (!error && name != NULL)
9748 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
9749 locally, thisblock, SEARCH_KEEP) == FAIL;
9750}
9751
9752/*
9753 * Used by searchpair() and searchpairpos()
9754 */
9755 static int
9756searchpair_cmn(typval_T *argvars, pos_T *match_pos)
9757{
9758 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01009759 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009760 int save_p_ws = p_ws;
9761 int dir;
9762 int flags = 0;
9763 char_u nbuf1[NUMBUFLEN];
9764 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009765 int retval = 0; /* default: FAIL */
9766 long lnum_stop = 0;
9767 long time_limit = 0;
9768
9769 /* Get the three pattern arguments: start, middle, end. */
9770 spat = get_tv_string_chk(&argvars[0]);
9771 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
9772 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
9773 if (spat == NULL || mpat == NULL || epat == NULL)
9774 goto theend; /* type error */
9775
9776 /* Handle the optional fourth argument: flags */
9777 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
9778 if (dir == 0)
9779 goto theend;
9780
9781 /* Don't accept SP_END or SP_SUBPAT.
9782 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
9783 */
9784 if ((flags & (SP_END | SP_SUBPAT)) != 0
9785 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9786 {
9787 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
9788 goto theend;
9789 }
9790
9791 /* Using 'r' implies 'W', otherwise it doesn't work. */
9792 if (flags & SP_REPEAT)
9793 p_ws = FALSE;
9794
9795 /* Optional fifth argument: skip expression */
9796 if (argvars[3].v_type == VAR_UNKNOWN
9797 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01009798 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009799 else
9800 {
Bram Moolenaar48570482017-10-30 21:48:41 +01009801 skip = &argvars[4];
9802 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
9803 && skip->v_type != VAR_STRING)
9804 {
9805 /* Type error */
9806 goto theend;
9807 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009808 if (argvars[5].v_type != VAR_UNKNOWN)
9809 {
9810 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
9811 if (lnum_stop < 0)
9812 goto theend;
9813#ifdef FEAT_RELTIME
9814 if (argvars[6].v_type != VAR_UNKNOWN)
9815 {
9816 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
9817 if (time_limit < 0)
9818 goto theend;
9819 }
9820#endif
9821 }
9822 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009823
9824 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
9825 match_pos, lnum_stop, time_limit);
9826
9827theend:
9828 p_ws = save_p_ws;
9829
9830 return retval;
9831}
9832
9833/*
9834 * "searchpair()" function
9835 */
9836 static void
9837f_searchpair(typval_T *argvars, typval_T *rettv)
9838{
9839 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
9840}
9841
9842/*
9843 * "searchpairpos()" function
9844 */
9845 static void
9846f_searchpairpos(typval_T *argvars, typval_T *rettv)
9847{
9848 pos_T match_pos;
9849 int lnum = 0;
9850 int col = 0;
9851
9852 if (rettv_list_alloc(rettv) == FAIL)
9853 return;
9854
9855 if (searchpair_cmn(argvars, &match_pos) > 0)
9856 {
9857 lnum = match_pos.lnum;
9858 col = match_pos.col;
9859 }
9860
9861 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9862 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9863}
9864
9865/*
9866 * Search for a start/middle/end thing.
9867 * Used by searchpair(), see its documentation for the details.
9868 * Returns 0 or -1 for no match,
9869 */
9870 long
9871do_searchpair(
9872 char_u *spat, /* start pattern */
9873 char_u *mpat, /* middle pattern */
9874 char_u *epat, /* end pattern */
9875 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +01009876 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009877 int flags, /* SP_SETPCMARK and other SP_ values */
9878 pos_T *match_pos,
9879 linenr_T lnum_stop, /* stop at this line if not zero */
9880 long time_limit UNUSED) /* stop after this many msec */
9881{
9882 char_u *save_cpo;
9883 char_u *pat, *pat2 = NULL, *pat3 = NULL;
9884 long retval = 0;
9885 pos_T pos;
9886 pos_T firstpos;
9887 pos_T foundpos;
9888 pos_T save_cursor;
9889 pos_T save_pos;
9890 int n;
9891 int r;
9892 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01009893 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009894 int err;
9895 int options = SEARCH_KEEP;
9896 proftime_T tm;
9897
9898 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
9899 save_cpo = p_cpo;
9900 p_cpo = empty_option;
9901
9902#ifdef FEAT_RELTIME
9903 /* Set the time limit, if there is one. */
9904 profile_setlimit(time_limit, &tm);
9905#endif
9906
9907 /* Make two search patterns: start/end (pat2, for in nested pairs) and
9908 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009909 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
9910 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009911 if (pat2 == NULL || pat3 == NULL)
9912 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009913 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009914 if (*mpat == NUL)
9915 STRCPY(pat3, pat2);
9916 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009917 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009918 spat, epat, mpat);
9919 if (flags & SP_START)
9920 options |= SEARCH_START;
9921
Bram Moolenaar48570482017-10-30 21:48:41 +01009922 if (skip != NULL)
9923 {
9924 /* Empty string means to not use the skip expression. */
9925 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
9926 use_skip = skip->vval.v_string != NULL
9927 && *skip->vval.v_string != NUL;
9928 }
9929
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009930 save_cursor = curwin->w_cursor;
9931 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009932 CLEAR_POS(&firstpos);
9933 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009934 pat = pat3;
9935 for (;;)
9936 {
9937 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009938 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009939 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009940 /* didn't find it or found the first match again: FAIL */
9941 break;
9942
9943 if (firstpos.lnum == 0)
9944 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009945 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009946 {
9947 /* Found the same position again. Can happen with a pattern that
9948 * has "\zs" at the end and searching backwards. Advance one
9949 * character and try again. */
9950 if (dir == BACKWARD)
9951 decl(&pos);
9952 else
9953 incl(&pos);
9954 }
9955 foundpos = pos;
9956
9957 /* clear the start flag to avoid getting stuck here */
9958 options &= ~SEARCH_START;
9959
9960 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +01009961 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009962 {
9963 save_pos = curwin->w_cursor;
9964 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01009965 err = FALSE;
9966 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009967 curwin->w_cursor = save_pos;
9968 if (err)
9969 {
9970 /* Evaluating {skip} caused an error, break here. */
9971 curwin->w_cursor = save_cursor;
9972 retval = -1;
9973 break;
9974 }
9975 if (r)
9976 continue;
9977 }
9978
9979 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
9980 {
9981 /* Found end when searching backwards or start when searching
9982 * forward: nested pair. */
9983 ++nest;
9984 pat = pat2; /* nested, don't search for middle */
9985 }
9986 else
9987 {
9988 /* Found end when searching forward or start when searching
9989 * backward: end of (nested) pair; or found middle in outer pair. */
9990 if (--nest == 1)
9991 pat = pat3; /* outer level, search for middle */
9992 }
9993
9994 if (nest == 0)
9995 {
9996 /* Found the match: return matchcount or line number. */
9997 if (flags & SP_RETCOUNT)
9998 ++retval;
9999 else
10000 retval = pos.lnum;
10001 if (flags & SP_SETPCMARK)
10002 setpcmark();
10003 curwin->w_cursor = pos;
10004 if (!(flags & SP_REPEAT))
10005 break;
10006 nest = 1; /* search for next unmatched */
10007 }
10008 }
10009
10010 if (match_pos != NULL)
10011 {
10012 /* Store the match cursor position */
10013 match_pos->lnum = curwin->w_cursor.lnum;
10014 match_pos->col = curwin->w_cursor.col + 1;
10015 }
10016
10017 /* If 'n' flag is used or search failed: restore cursor position. */
10018 if ((flags & SP_NOMOVE) || retval == 0)
10019 curwin->w_cursor = save_cursor;
10020
10021theend:
10022 vim_free(pat2);
10023 vim_free(pat3);
10024 if (p_cpo == empty_option)
10025 p_cpo = save_cpo;
10026 else
10027 /* Darn, evaluating the {skip} expression changed the value. */
10028 free_string_option(save_cpo);
10029
10030 return retval;
10031}
10032
10033/*
10034 * "searchpos()" function
10035 */
10036 static void
10037f_searchpos(typval_T *argvars, typval_T *rettv)
10038{
10039 pos_T match_pos;
10040 int lnum = 0;
10041 int col = 0;
10042 int n;
10043 int flags = 0;
10044
10045 if (rettv_list_alloc(rettv) == FAIL)
10046 return;
10047
10048 n = search_cmn(argvars, &match_pos, &flags);
10049 if (n > 0)
10050 {
10051 lnum = match_pos.lnum;
10052 col = match_pos.col;
10053 }
10054
10055 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10056 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10057 if (flags & SP_SUBPAT)
10058 list_append_number(rettv->vval.v_list, (varnumber_T)n);
10059}
10060
10061 static void
10062f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
10063{
10064#ifdef FEAT_CLIENTSERVER
10065 char_u buf[NUMBUFLEN];
10066 char_u *server = get_tv_string_chk(&argvars[0]);
10067 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
10068
10069 rettv->vval.v_number = -1;
10070 if (server == NULL || reply == NULL)
10071 return;
10072 if (check_restricted() || check_secure())
10073 return;
10074# ifdef FEAT_X11
10075 if (check_connection() == FAIL)
10076 return;
10077# endif
10078
10079 if (serverSendReply(server, reply) < 0)
10080 {
10081 EMSG(_("E258: Unable to send to client"));
10082 return;
10083 }
10084 rettv->vval.v_number = 0;
10085#else
10086 rettv->vval.v_number = -1;
10087#endif
10088}
10089
10090 static void
10091f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10092{
10093 char_u *r = NULL;
10094
10095#ifdef FEAT_CLIENTSERVER
10096# ifdef WIN32
10097 r = serverGetVimNames();
10098# else
10099 make_connection();
10100 if (X_DISPLAY != NULL)
10101 r = serverGetVimNames(X_DISPLAY);
10102# endif
10103#endif
10104 rettv->v_type = VAR_STRING;
10105 rettv->vval.v_string = r;
10106}
10107
10108/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010109 * Set line or list of lines in buffer "buf".
10110 */
10111 static void
10112set_buffer_lines(buf_T *buf, linenr_T lnum, typval_T *lines, typval_T *rettv)
10113{
10114 char_u *line = NULL;
10115 list_T *l = NULL;
10116 listitem_T *li = NULL;
10117 long added = 0;
10118 linenr_T lcount;
Bram Moolenaar0c4dc882017-11-06 21:32:54 +010010119 buf_T *curbuf_save = NULL;
10120 win_T *curwin_save = NULL;
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010121 int is_curbuf = buf == curbuf;
10122
Bram Moolenaar9d954202017-09-04 20:34:19 +020010123 /* When using the current buffer ml_mfp will be set if needed. Useful when
10124 * setline() is used on startup. For other buffers the buffer must be
10125 * loaded. */
10126 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010127 {
10128 rettv->vval.v_number = 1; /* FAIL */
10129 return;
10130 }
10131
Bram Moolenaar0c4dc882017-11-06 21:32:54 +010010132 if (!is_curbuf)
10133 {
10134 wininfo_T *wip;
10135
10136 curbuf_save = curbuf;
10137 curwin_save = curwin;
10138 curbuf = buf;
10139 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
10140 {
10141 if (wip->wi_win != NULL)
10142 {
10143 curwin = wip->wi_win;
10144 break;
10145 }
10146 }
10147 }
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010148
10149 lcount = curbuf->b_ml.ml_line_count;
10150
10151 if (lines->v_type == VAR_LIST)
10152 {
10153 l = lines->vval.v_list;
10154 li = l->lv_first;
10155 }
10156 else
10157 line = get_tv_string_chk(lines);
10158
10159 /* default result is zero == OK */
10160 for (;;)
10161 {
10162 if (l != NULL)
10163 {
10164 /* list argument, get next string */
10165 if (li == NULL)
10166 break;
10167 line = get_tv_string_chk(&li->li_tv);
10168 li = li->li_next;
10169 }
10170
10171 rettv->vval.v_number = 1; /* FAIL */
10172 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
10173 break;
10174
10175 /* When coming here from Insert mode, sync undo, so that this can be
10176 * undone separately from what was previously inserted. */
10177 if (u_sync_once == 2)
10178 {
10179 u_sync_once = 1; /* notify that u_sync() was called */
10180 u_sync(TRUE);
10181 }
10182
10183 if (lnum <= curbuf->b_ml.ml_line_count)
10184 {
10185 /* existing line, replace it */
10186 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
10187 {
10188 changed_bytes(lnum, 0);
10189 if (is_curbuf && lnum == curwin->w_cursor.lnum)
10190 check_cursor_col();
10191 rettv->vval.v_number = 0; /* OK */
10192 }
10193 }
10194 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
10195 {
10196 /* lnum is one past the last line, append the line */
10197 ++added;
10198 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
10199 rettv->vval.v_number = 0; /* OK */
10200 }
10201
10202 if (l == NULL) /* only one string argument */
10203 break;
10204 ++lnum;
10205 }
10206
10207 if (added > 0)
10208 appended_lines_mark(lcount, added);
10209
Bram Moolenaar0c4dc882017-11-06 21:32:54 +010010210 if (!is_curbuf)
10211 {
10212 curbuf = curbuf_save;
10213 curwin = curwin_save;
10214 }
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010215}
10216
10217/*
10218 * "setbufline()" function
10219 */
10220 static void
10221f_setbufline(argvars, rettv)
10222 typval_T *argvars;
10223 typval_T *rettv;
10224{
10225 linenr_T lnum;
10226 buf_T *buf;
10227
10228 buf = get_buf_tv(&argvars[0], FALSE);
10229 if (buf == NULL)
10230 rettv->vval.v_number = 1; /* FAIL */
10231 else
10232 {
10233 lnum = get_tv_lnum_buf(&argvars[1], buf);
10234
10235 set_buffer_lines(buf, lnum, &argvars[2], rettv);
10236 }
10237}
10238
10239/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010240 * "setbufvar()" function
10241 */
10242 static void
10243f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10244{
10245 buf_T *buf;
10246 char_u *varname, *bufvarname;
10247 typval_T *varp;
10248 char_u nbuf[NUMBUFLEN];
10249
10250 if (check_restricted() || check_secure())
10251 return;
10252 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10253 varname = get_tv_string_chk(&argvars[1]);
10254 buf = get_buf_tv(&argvars[0], FALSE);
10255 varp = &argvars[2];
10256
10257 if (buf != NULL && varname != NULL && varp != NULL)
10258 {
10259 if (*varname == '&')
10260 {
10261 long numval;
10262 char_u *strval;
10263 int error = FALSE;
10264 aco_save_T aco;
10265
10266 /* set curbuf to be our buf, temporarily */
10267 aucmd_prepbuf(&aco, buf);
10268
10269 ++varname;
10270 numval = (long)get_tv_number_chk(varp, &error);
10271 strval = get_tv_string_buf_chk(varp, nbuf);
10272 if (!error && strval != NULL)
10273 set_option_value(varname, numval, strval, OPT_LOCAL);
10274
10275 /* reset notion of buffer */
10276 aucmd_restbuf(&aco);
10277 }
10278 else
10279 {
10280 buf_T *save_curbuf = curbuf;
10281
10282 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
10283 if (bufvarname != NULL)
10284 {
10285 curbuf = buf;
10286 STRCPY(bufvarname, "b:");
10287 STRCPY(bufvarname + 2, varname);
10288 set_var(bufvarname, varp, TRUE);
10289 vim_free(bufvarname);
10290 curbuf = save_curbuf;
10291 }
10292 }
10293 }
10294}
10295
10296 static void
10297f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10298{
10299 dict_T *d;
10300 dictitem_T *di;
10301 char_u *csearch;
10302
10303 if (argvars[0].v_type != VAR_DICT)
10304 {
10305 EMSG(_(e_dictreq));
10306 return;
10307 }
10308
10309 if ((d = argvars[0].vval.v_dict) != NULL)
10310 {
10311 csearch = get_dict_string(d, (char_u *)"char", FALSE);
10312 if (csearch != NULL)
10313 {
10314#ifdef FEAT_MBYTE
10315 if (enc_utf8)
10316 {
10317 int pcc[MAX_MCO];
10318 int c = utfc_ptr2char(csearch, pcc);
10319
10320 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10321 }
10322 else
10323#endif
10324 set_last_csearch(PTR2CHAR(csearch),
10325 csearch, MB_PTR2LEN(csearch));
10326 }
10327
10328 di = dict_find(d, (char_u *)"forward", -1);
10329 if (di != NULL)
10330 set_csearch_direction((int)get_tv_number(&di->di_tv)
10331 ? FORWARD : BACKWARD);
10332
10333 di = dict_find(d, (char_u *)"until", -1);
10334 if (di != NULL)
10335 set_csearch_until(!!get_tv_number(&di->di_tv));
10336 }
10337}
10338
10339/*
10340 * "setcmdpos()" function
10341 */
10342 static void
10343f_setcmdpos(typval_T *argvars, typval_T *rettv)
10344{
10345 int pos = (int)get_tv_number(&argvars[0]) - 1;
10346
10347 if (pos >= 0)
10348 rettv->vval.v_number = set_cmdline_pos(pos);
10349}
10350
10351/*
10352 * "setfperm({fname}, {mode})" function
10353 */
10354 static void
10355f_setfperm(typval_T *argvars, typval_T *rettv)
10356{
10357 char_u *fname;
10358 char_u modebuf[NUMBUFLEN];
10359 char_u *mode_str;
10360 int i;
10361 int mask;
10362 int mode = 0;
10363
10364 rettv->vval.v_number = 0;
10365 fname = get_tv_string_chk(&argvars[0]);
10366 if (fname == NULL)
10367 return;
10368 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
10369 if (mode_str == NULL)
10370 return;
10371 if (STRLEN(mode_str) != 9)
10372 {
10373 EMSG2(_(e_invarg2), mode_str);
10374 return;
10375 }
10376
10377 mask = 1;
10378 for (i = 8; i >= 0; --i)
10379 {
10380 if (mode_str[i] != '-')
10381 mode |= mask;
10382 mask = mask << 1;
10383 }
10384 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10385}
10386
10387/*
10388 * "setline()" function
10389 */
10390 static void
10391f_setline(typval_T *argvars, typval_T *rettv)
10392{
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010393 linenr_T lnum = get_tv_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010394
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010395 set_buffer_lines(curbuf, lnum, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010396}
10397
Bram Moolenaard823fa92016-08-12 16:29:27 +020010398static 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 +020010399
10400/*
10401 * Used by "setqflist()" and "setloclist()" functions
10402 */
10403 static void
10404set_qf_ll_list(
10405 win_T *wp UNUSED,
10406 typval_T *list_arg UNUSED,
10407 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010408 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010409 typval_T *rettv)
10410{
10411#ifdef FEAT_QUICKFIX
10412 static char *e_invact = N_("E927: Invalid action: '%s'");
10413 char_u *act;
10414 int action = 0;
10415#endif
10416
10417 rettv->vval.v_number = -1;
10418
10419#ifdef FEAT_QUICKFIX
10420 if (list_arg->v_type != VAR_LIST)
10421 EMSG(_(e_listreq));
10422 else
10423 {
10424 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010425 dict_T *d = NULL;
10426 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010427
10428 if (action_arg->v_type == VAR_STRING)
10429 {
10430 act = get_tv_string_chk(action_arg);
10431 if (act == NULL)
10432 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020010433 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
10434 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010435 action = *act;
10436 else
10437 EMSG2(_(e_invact), act);
10438 }
10439 else if (action_arg->v_type == VAR_UNKNOWN)
10440 action = ' ';
10441 else
10442 EMSG(_(e_stringreq));
10443
Bram Moolenaard823fa92016-08-12 16:29:27 +020010444 if (action_arg->v_type != VAR_UNKNOWN
10445 && what_arg->v_type != VAR_UNKNOWN)
10446 {
10447 if (what_arg->v_type == VAR_DICT)
10448 d = what_arg->vval.v_dict;
10449 else
10450 {
10451 EMSG(_(e_dictreq));
10452 valid_dict = FALSE;
10453 }
10454 }
10455
10456 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
10457 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()"), d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010458 rettv->vval.v_number = 0;
10459 }
10460#endif
10461}
10462
10463/*
10464 * "setloclist()" function
10465 */
10466 static void
10467f_setloclist(typval_T *argvars, typval_T *rettv)
10468{
10469 win_T *win;
10470
10471 rettv->vval.v_number = -1;
10472
10473 win = find_win_by_nr(&argvars[0], NULL);
10474 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020010475 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010476}
10477
10478/*
10479 * "setmatches()" function
10480 */
10481 static void
10482f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10483{
10484#ifdef FEAT_SEARCH_EXTRA
10485 list_T *l;
10486 listitem_T *li;
10487 dict_T *d;
10488 list_T *s = NULL;
10489
10490 rettv->vval.v_number = -1;
10491 if (argvars[0].v_type != VAR_LIST)
10492 {
10493 EMSG(_(e_listreq));
10494 return;
10495 }
10496 if ((l = argvars[0].vval.v_list) != NULL)
10497 {
10498
10499 /* To some extent make sure that we are dealing with a list from
10500 * "getmatches()". */
10501 li = l->lv_first;
10502 while (li != NULL)
10503 {
10504 if (li->li_tv.v_type != VAR_DICT
10505 || (d = li->li_tv.vval.v_dict) == NULL)
10506 {
10507 EMSG(_(e_invarg));
10508 return;
10509 }
10510 if (!(dict_find(d, (char_u *)"group", -1) != NULL
10511 && (dict_find(d, (char_u *)"pattern", -1) != NULL
10512 || dict_find(d, (char_u *)"pos1", -1) != NULL)
10513 && dict_find(d, (char_u *)"priority", -1) != NULL
10514 && dict_find(d, (char_u *)"id", -1) != NULL))
10515 {
10516 EMSG(_(e_invarg));
10517 return;
10518 }
10519 li = li->li_next;
10520 }
10521
10522 clear_matches(curwin);
10523 li = l->lv_first;
10524 while (li != NULL)
10525 {
10526 int i = 0;
10527 char_u buf[5];
10528 dictitem_T *di;
10529 char_u *group;
10530 int priority;
10531 int id;
10532 char_u *conceal;
10533
10534 d = li->li_tv.vval.v_dict;
10535 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
10536 {
10537 if (s == NULL)
10538 {
10539 s = list_alloc();
10540 if (s == NULL)
10541 return;
10542 }
10543
10544 /* match from matchaddpos() */
10545 for (i = 1; i < 9; i++)
10546 {
10547 sprintf((char *)buf, (char *)"pos%d", i);
10548 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
10549 {
10550 if (di->di_tv.v_type != VAR_LIST)
10551 return;
10552
10553 list_append_tv(s, &di->di_tv);
10554 s->lv_refcount++;
10555 }
10556 else
10557 break;
10558 }
10559 }
10560
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010561 group = get_dict_string(d, (char_u *)"group", TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010562 priority = (int)get_dict_number(d, (char_u *)"priority");
10563 id = (int)get_dict_number(d, (char_u *)"id");
10564 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010565 ? get_dict_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010566 : NULL;
10567 if (i == 0)
10568 {
10569 match_add(curwin, group,
10570 get_dict_string(d, (char_u *)"pattern", FALSE),
10571 priority, id, NULL, conceal);
10572 }
10573 else
10574 {
10575 match_add(curwin, group, NULL, priority, id, s, conceal);
10576 list_unref(s);
10577 s = NULL;
10578 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010579 vim_free(group);
10580 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010581
10582 li = li->li_next;
10583 }
10584 rettv->vval.v_number = 0;
10585 }
10586#endif
10587}
10588
10589/*
10590 * "setpos()" function
10591 */
10592 static void
10593f_setpos(typval_T *argvars, typval_T *rettv)
10594{
10595 pos_T pos;
10596 int fnum;
10597 char_u *name;
10598 colnr_T curswant = -1;
10599
10600 rettv->vval.v_number = -1;
10601 name = get_tv_string_chk(argvars);
10602 if (name != NULL)
10603 {
10604 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10605 {
10606 if (--pos.col < 0)
10607 pos.col = 0;
10608 if (name[0] == '.' && name[1] == NUL)
10609 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010610 /* set cursor; "fnum" is ignored */
10611 curwin->w_cursor = pos;
10612 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010613 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010614 curwin->w_curswant = curswant - 1;
10615 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010616 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010617 check_cursor();
10618 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010619 }
10620 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10621 {
10622 /* set mark */
10623 if (setmark_pos(name[1], &pos, fnum) == OK)
10624 rettv->vval.v_number = 0;
10625 }
10626 else
10627 EMSG(_(e_invarg));
10628 }
10629 }
10630}
10631
10632/*
10633 * "setqflist()" function
10634 */
10635 static void
10636f_setqflist(typval_T *argvars, typval_T *rettv)
10637{
Bram Moolenaard823fa92016-08-12 16:29:27 +020010638 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010639}
10640
10641/*
10642 * "setreg()" function
10643 */
10644 static void
10645f_setreg(typval_T *argvars, typval_T *rettv)
10646{
10647 int regname;
10648 char_u *strregname;
10649 char_u *stropt;
10650 char_u *strval;
10651 int append;
10652 char_u yank_type;
10653 long block_len;
10654
10655 block_len = -1;
10656 yank_type = MAUTO;
10657 append = FALSE;
10658
10659 strregname = get_tv_string_chk(argvars);
10660 rettv->vval.v_number = 1; /* FAIL is default */
10661
10662 if (strregname == NULL)
10663 return; /* type error; errmsg already given */
10664 regname = *strregname;
10665 if (regname == 0 || regname == '@')
10666 regname = '"';
10667
10668 if (argvars[2].v_type != VAR_UNKNOWN)
10669 {
10670 stropt = get_tv_string_chk(&argvars[2]);
10671 if (stropt == NULL)
10672 return; /* type error */
10673 for (; *stropt != NUL; ++stropt)
10674 switch (*stropt)
10675 {
10676 case 'a': case 'A': /* append */
10677 append = TRUE;
10678 break;
10679 case 'v': case 'c': /* character-wise selection */
10680 yank_type = MCHAR;
10681 break;
10682 case 'V': case 'l': /* line-wise selection */
10683 yank_type = MLINE;
10684 break;
10685 case 'b': case Ctrl_V: /* block-wise selection */
10686 yank_type = MBLOCK;
10687 if (VIM_ISDIGIT(stropt[1]))
10688 {
10689 ++stropt;
10690 block_len = getdigits(&stropt) - 1;
10691 --stropt;
10692 }
10693 break;
10694 }
10695 }
10696
10697 if (argvars[1].v_type == VAR_LIST)
10698 {
10699 char_u **lstval;
10700 char_u **allocval;
10701 char_u buf[NUMBUFLEN];
10702 char_u **curval;
10703 char_u **curallocval;
10704 list_T *ll = argvars[1].vval.v_list;
10705 listitem_T *li;
10706 int len;
10707
10708 /* If the list is NULL handle like an empty list. */
10709 len = ll == NULL ? 0 : ll->lv_len;
10710
10711 /* First half: use for pointers to result lines; second half: use for
10712 * pointers to allocated copies. */
10713 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
10714 if (lstval == NULL)
10715 return;
10716 curval = lstval;
10717 allocval = lstval + len + 2;
10718 curallocval = allocval;
10719
10720 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
10721 li = li->li_next)
10722 {
10723 strval = get_tv_string_buf_chk(&li->li_tv, buf);
10724 if (strval == NULL)
10725 goto free_lstval;
10726 if (strval == buf)
10727 {
10728 /* Need to make a copy, next get_tv_string_buf_chk() will
10729 * overwrite the string. */
10730 strval = vim_strsave(buf);
10731 if (strval == NULL)
10732 goto free_lstval;
10733 *curallocval++ = strval;
10734 }
10735 *curval++ = strval;
10736 }
10737 *curval++ = NULL;
10738
10739 write_reg_contents_lst(regname, lstval, -1,
10740 append, yank_type, block_len);
10741free_lstval:
10742 while (curallocval > allocval)
10743 vim_free(*--curallocval);
10744 vim_free(lstval);
10745 }
10746 else
10747 {
10748 strval = get_tv_string_chk(&argvars[1]);
10749 if (strval == NULL)
10750 return;
10751 write_reg_contents_ex(regname, strval, -1,
10752 append, yank_type, block_len);
10753 }
10754 rettv->vval.v_number = 0;
10755}
10756
10757/*
10758 * "settabvar()" function
10759 */
10760 static void
10761f_settabvar(typval_T *argvars, typval_T *rettv)
10762{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010763 tabpage_T *save_curtab;
10764 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010765 char_u *varname, *tabvarname;
10766 typval_T *varp;
10767
10768 rettv->vval.v_number = 0;
10769
10770 if (check_restricted() || check_secure())
10771 return;
10772
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010773 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010774 varname = get_tv_string_chk(&argvars[1]);
10775 varp = &argvars[2];
10776
Bram Moolenaar4033c552017-09-16 20:54:51 +020010777 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010778 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010779 save_curtab = curtab;
10780 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010781
10782 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
10783 if (tabvarname != NULL)
10784 {
10785 STRCPY(tabvarname, "t:");
10786 STRCPY(tabvarname + 2, varname);
10787 set_var(tabvarname, varp, TRUE);
10788 vim_free(tabvarname);
10789 }
10790
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010791 /* Restore current tabpage */
10792 if (valid_tabpage(save_curtab))
10793 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010794 }
10795}
10796
10797/*
10798 * "settabwinvar()" function
10799 */
10800 static void
10801f_settabwinvar(typval_T *argvars, typval_T *rettv)
10802{
10803 setwinvar(argvars, rettv, 1);
10804}
10805
10806/*
10807 * "setwinvar()" function
10808 */
10809 static void
10810f_setwinvar(typval_T *argvars, typval_T *rettv)
10811{
10812 setwinvar(argvars, rettv, 0);
10813}
10814
10815#ifdef FEAT_CRYPT
10816/*
10817 * "sha256({string})" function
10818 */
10819 static void
10820f_sha256(typval_T *argvars, typval_T *rettv)
10821{
10822 char_u *p;
10823
10824 p = get_tv_string(&argvars[0]);
10825 rettv->vval.v_string = vim_strsave(
10826 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
10827 rettv->v_type = VAR_STRING;
10828}
10829#endif /* FEAT_CRYPT */
10830
10831/*
10832 * "shellescape({string})" function
10833 */
10834 static void
10835f_shellescape(typval_T *argvars, typval_T *rettv)
10836{
Bram Moolenaar20615522017-06-05 18:46:26 +020010837 int do_special = non_zero_arg(&argvars[1]);
10838
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010839 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar20615522017-06-05 18:46:26 +020010840 get_tv_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010841 rettv->v_type = VAR_STRING;
10842}
10843
10844/*
10845 * shiftwidth() function
10846 */
10847 static void
10848f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
10849{
10850 rettv->vval.v_number = get_sw_value(curbuf);
10851}
10852
10853/*
10854 * "simplify()" function
10855 */
10856 static void
10857f_simplify(typval_T *argvars, typval_T *rettv)
10858{
10859 char_u *p;
10860
10861 p = get_tv_string(&argvars[0]);
10862 rettv->vval.v_string = vim_strsave(p);
10863 simplify_filename(rettv->vval.v_string); /* simplify in place */
10864 rettv->v_type = VAR_STRING;
10865}
10866
10867#ifdef FEAT_FLOAT
10868/*
10869 * "sin()" function
10870 */
10871 static void
10872f_sin(typval_T *argvars, typval_T *rettv)
10873{
10874 float_T f = 0.0;
10875
10876 rettv->v_type = VAR_FLOAT;
10877 if (get_float_arg(argvars, &f) == OK)
10878 rettv->vval.v_float = sin(f);
10879 else
10880 rettv->vval.v_float = 0.0;
10881}
10882
10883/*
10884 * "sinh()" function
10885 */
10886 static void
10887f_sinh(typval_T *argvars, typval_T *rettv)
10888{
10889 float_T f = 0.0;
10890
10891 rettv->v_type = VAR_FLOAT;
10892 if (get_float_arg(argvars, &f) == OK)
10893 rettv->vval.v_float = sinh(f);
10894 else
10895 rettv->vval.v_float = 0.0;
10896}
10897#endif
10898
10899static int
10900#ifdef __BORLANDC__
10901 _RTLENTRYF
10902#endif
10903 item_compare(const void *s1, const void *s2);
10904static int
10905#ifdef __BORLANDC__
10906 _RTLENTRYF
10907#endif
10908 item_compare2(const void *s1, const void *s2);
10909
10910/* struct used in the array that's given to qsort() */
10911typedef struct
10912{
10913 listitem_T *item;
10914 int idx;
10915} sortItem_T;
10916
10917/* struct storing information about current sort */
10918typedef struct
10919{
10920 int item_compare_ic;
10921 int item_compare_numeric;
10922 int item_compare_numbers;
10923#ifdef FEAT_FLOAT
10924 int item_compare_float;
10925#endif
10926 char_u *item_compare_func;
10927 partial_T *item_compare_partial;
10928 dict_T *item_compare_selfdict;
10929 int item_compare_func_err;
10930 int item_compare_keep_zero;
10931} sortinfo_T;
10932static sortinfo_T *sortinfo = NULL;
10933static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
10934#define ITEM_COMPARE_FAIL 999
10935
10936/*
10937 * Compare functions for f_sort() and f_uniq() below.
10938 */
10939 static int
10940#ifdef __BORLANDC__
10941_RTLENTRYF
10942#endif
10943item_compare(const void *s1, const void *s2)
10944{
10945 sortItem_T *si1, *si2;
10946 typval_T *tv1, *tv2;
10947 char_u *p1, *p2;
10948 char_u *tofree1 = NULL, *tofree2 = NULL;
10949 int res;
10950 char_u numbuf1[NUMBUFLEN];
10951 char_u numbuf2[NUMBUFLEN];
10952
10953 si1 = (sortItem_T *)s1;
10954 si2 = (sortItem_T *)s2;
10955 tv1 = &si1->item->li_tv;
10956 tv2 = &si2->item->li_tv;
10957
10958 if (sortinfo->item_compare_numbers)
10959 {
10960 varnumber_T v1 = get_tv_number(tv1);
10961 varnumber_T v2 = get_tv_number(tv2);
10962
10963 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10964 }
10965
10966#ifdef FEAT_FLOAT
10967 if (sortinfo->item_compare_float)
10968 {
10969 float_T v1 = get_tv_float(tv1);
10970 float_T v2 = get_tv_float(tv2);
10971
10972 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10973 }
10974#endif
10975
10976 /* tv2string() puts quotes around a string and allocates memory. Don't do
10977 * that for string variables. Use a single quote when comparing with a
10978 * non-string to do what the docs promise. */
10979 if (tv1->v_type == VAR_STRING)
10980 {
10981 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
10982 p1 = (char_u *)"'";
10983 else
10984 p1 = tv1->vval.v_string;
10985 }
10986 else
10987 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
10988 if (tv2->v_type == VAR_STRING)
10989 {
10990 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
10991 p2 = (char_u *)"'";
10992 else
10993 p2 = tv2->vval.v_string;
10994 }
10995 else
10996 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
10997 if (p1 == NULL)
10998 p1 = (char_u *)"";
10999 if (p2 == NULL)
11000 p2 = (char_u *)"";
11001 if (!sortinfo->item_compare_numeric)
11002 {
11003 if (sortinfo->item_compare_ic)
11004 res = STRICMP(p1, p2);
11005 else
11006 res = STRCMP(p1, p2);
11007 }
11008 else
11009 {
11010 double n1, n2;
11011 n1 = strtod((char *)p1, (char **)&p1);
11012 n2 = strtod((char *)p2, (char **)&p2);
11013 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
11014 }
11015
11016 /* When the result would be zero, compare the item indexes. Makes the
11017 * sort stable. */
11018 if (res == 0 && !sortinfo->item_compare_keep_zero)
11019 res = si1->idx > si2->idx ? 1 : -1;
11020
11021 vim_free(tofree1);
11022 vim_free(tofree2);
11023 return res;
11024}
11025
11026 static int
11027#ifdef __BORLANDC__
11028_RTLENTRYF
11029#endif
11030item_compare2(const void *s1, const void *s2)
11031{
11032 sortItem_T *si1, *si2;
11033 int res;
11034 typval_T rettv;
11035 typval_T argv[3];
11036 int dummy;
11037 char_u *func_name;
11038 partial_T *partial = sortinfo->item_compare_partial;
11039
11040 /* shortcut after failure in previous call; compare all items equal */
11041 if (sortinfo->item_compare_func_err)
11042 return 0;
11043
11044 si1 = (sortItem_T *)s1;
11045 si2 = (sortItem_T *)s2;
11046
11047 if (partial == NULL)
11048 func_name = sortinfo->item_compare_func;
11049 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020011050 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011051
11052 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
11053 * in the copy without changing the original list items. */
11054 copy_tv(&si1->item->li_tv, &argv[0]);
11055 copy_tv(&si2->item->li_tv, &argv[1]);
11056
11057 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
11058 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020011059 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011060 partial, sortinfo->item_compare_selfdict);
11061 clear_tv(&argv[0]);
11062 clear_tv(&argv[1]);
11063
11064 if (res == FAIL)
11065 res = ITEM_COMPARE_FAIL;
11066 else
11067 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
11068 if (sortinfo->item_compare_func_err)
11069 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
11070 clear_tv(&rettv);
11071
11072 /* When the result would be zero, compare the pointers themselves. Makes
11073 * the sort stable. */
11074 if (res == 0 && !sortinfo->item_compare_keep_zero)
11075 res = si1->idx > si2->idx ? 1 : -1;
11076
11077 return res;
11078}
11079
11080/*
11081 * "sort({list})" function
11082 */
11083 static void
11084do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
11085{
11086 list_T *l;
11087 listitem_T *li;
11088 sortItem_T *ptrs;
11089 sortinfo_T *old_sortinfo;
11090 sortinfo_T info;
11091 long len;
11092 long i;
11093
11094 /* Pointer to current info struct used in compare function. Save and
11095 * restore the current one for nested calls. */
11096 old_sortinfo = sortinfo;
11097 sortinfo = &info;
11098
11099 if (argvars[0].v_type != VAR_LIST)
11100 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
11101 else
11102 {
11103 l = argvars[0].vval.v_list;
11104 if (l == NULL || tv_check_lock(l->lv_lock,
11105 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
11106 TRUE))
11107 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011108 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011109
11110 len = list_len(l);
11111 if (len <= 1)
11112 goto theend; /* short list sorts pretty quickly */
11113
11114 info.item_compare_ic = FALSE;
11115 info.item_compare_numeric = FALSE;
11116 info.item_compare_numbers = FALSE;
11117#ifdef FEAT_FLOAT
11118 info.item_compare_float = FALSE;
11119#endif
11120 info.item_compare_func = NULL;
11121 info.item_compare_partial = NULL;
11122 info.item_compare_selfdict = NULL;
11123 if (argvars[1].v_type != VAR_UNKNOWN)
11124 {
11125 /* optional second argument: {func} */
11126 if (argvars[1].v_type == VAR_FUNC)
11127 info.item_compare_func = argvars[1].vval.v_string;
11128 else if (argvars[1].v_type == VAR_PARTIAL)
11129 info.item_compare_partial = argvars[1].vval.v_partial;
11130 else
11131 {
11132 int error = FALSE;
11133
11134 i = (long)get_tv_number_chk(&argvars[1], &error);
11135 if (error)
11136 goto theend; /* type error; errmsg already given */
11137 if (i == 1)
11138 info.item_compare_ic = TRUE;
11139 else if (argvars[1].v_type != VAR_NUMBER)
11140 info.item_compare_func = get_tv_string(&argvars[1]);
11141 else if (i != 0)
11142 {
11143 EMSG(_(e_invarg));
11144 goto theend;
11145 }
11146 if (info.item_compare_func != NULL)
11147 {
11148 if (*info.item_compare_func == NUL)
11149 {
11150 /* empty string means default sort */
11151 info.item_compare_func = NULL;
11152 }
11153 else if (STRCMP(info.item_compare_func, "n") == 0)
11154 {
11155 info.item_compare_func = NULL;
11156 info.item_compare_numeric = TRUE;
11157 }
11158 else if (STRCMP(info.item_compare_func, "N") == 0)
11159 {
11160 info.item_compare_func = NULL;
11161 info.item_compare_numbers = TRUE;
11162 }
11163#ifdef FEAT_FLOAT
11164 else if (STRCMP(info.item_compare_func, "f") == 0)
11165 {
11166 info.item_compare_func = NULL;
11167 info.item_compare_float = TRUE;
11168 }
11169#endif
11170 else if (STRCMP(info.item_compare_func, "i") == 0)
11171 {
11172 info.item_compare_func = NULL;
11173 info.item_compare_ic = TRUE;
11174 }
11175 }
11176 }
11177
11178 if (argvars[2].v_type != VAR_UNKNOWN)
11179 {
11180 /* optional third argument: {dict} */
11181 if (argvars[2].v_type != VAR_DICT)
11182 {
11183 EMSG(_(e_dictreq));
11184 goto theend;
11185 }
11186 info.item_compare_selfdict = argvars[2].vval.v_dict;
11187 }
11188 }
11189
11190 /* Make an array with each entry pointing to an item in the List. */
11191 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
11192 if (ptrs == NULL)
11193 goto theend;
11194
11195 i = 0;
11196 if (sort)
11197 {
11198 /* sort(): ptrs will be the list to sort */
11199 for (li = l->lv_first; li != NULL; li = li->li_next)
11200 {
11201 ptrs[i].item = li;
11202 ptrs[i].idx = i;
11203 ++i;
11204 }
11205
11206 info.item_compare_func_err = FALSE;
11207 info.item_compare_keep_zero = FALSE;
11208 /* test the compare function */
11209 if ((info.item_compare_func != NULL
11210 || info.item_compare_partial != NULL)
11211 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11212 == ITEM_COMPARE_FAIL)
11213 EMSG(_("E702: Sort compare function failed"));
11214 else
11215 {
11216 /* Sort the array with item pointers. */
11217 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
11218 info.item_compare_func == NULL
11219 && info.item_compare_partial == NULL
11220 ? item_compare : item_compare2);
11221
11222 if (!info.item_compare_func_err)
11223 {
11224 /* Clear the List and append the items in sorted order. */
11225 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
11226 l->lv_len = 0;
11227 for (i = 0; i < len; ++i)
11228 list_append(l, ptrs[i].item);
11229 }
11230 }
11231 }
11232 else
11233 {
11234 int (*item_compare_func_ptr)(const void *, const void *);
11235
11236 /* f_uniq(): ptrs will be a stack of items to remove */
11237 info.item_compare_func_err = FALSE;
11238 info.item_compare_keep_zero = TRUE;
11239 item_compare_func_ptr = info.item_compare_func != NULL
11240 || info.item_compare_partial != NULL
11241 ? item_compare2 : item_compare;
11242
11243 for (li = l->lv_first; li != NULL && li->li_next != NULL;
11244 li = li->li_next)
11245 {
11246 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
11247 == 0)
11248 ptrs[i++].item = li;
11249 if (info.item_compare_func_err)
11250 {
11251 EMSG(_("E882: Uniq compare function failed"));
11252 break;
11253 }
11254 }
11255
11256 if (!info.item_compare_func_err)
11257 {
11258 while (--i >= 0)
11259 {
11260 li = ptrs[i].item->li_next;
11261 ptrs[i].item->li_next = li->li_next;
11262 if (li->li_next != NULL)
11263 li->li_next->li_prev = ptrs[i].item;
11264 else
11265 l->lv_last = ptrs[i].item;
11266 list_fix_watch(l, li);
11267 listitem_free(li);
11268 l->lv_len--;
11269 }
11270 }
11271 }
11272
11273 vim_free(ptrs);
11274 }
11275theend:
11276 sortinfo = old_sortinfo;
11277}
11278
11279/*
11280 * "sort({list})" function
11281 */
11282 static void
11283f_sort(typval_T *argvars, typval_T *rettv)
11284{
11285 do_sort_uniq(argvars, rettv, TRUE);
11286}
11287
11288/*
11289 * "uniq({list})" function
11290 */
11291 static void
11292f_uniq(typval_T *argvars, typval_T *rettv)
11293{
11294 do_sort_uniq(argvars, rettv, FALSE);
11295}
11296
11297/*
11298 * "soundfold({word})" function
11299 */
11300 static void
11301f_soundfold(typval_T *argvars, typval_T *rettv)
11302{
11303 char_u *s;
11304
11305 rettv->v_type = VAR_STRING;
11306 s = get_tv_string(&argvars[0]);
11307#ifdef FEAT_SPELL
11308 rettv->vval.v_string = eval_soundfold(s);
11309#else
11310 rettv->vval.v_string = vim_strsave(s);
11311#endif
11312}
11313
11314/*
11315 * "spellbadword()" function
11316 */
11317 static void
11318f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
11319{
11320 char_u *word = (char_u *)"";
11321 hlf_T attr = HLF_COUNT;
11322 int len = 0;
11323
11324 if (rettv_list_alloc(rettv) == FAIL)
11325 return;
11326
11327#ifdef FEAT_SPELL
11328 if (argvars[0].v_type == VAR_UNKNOWN)
11329 {
11330 /* Find the start and length of the badly spelled word. */
11331 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
11332 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011333 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011334 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011335 curwin->w_set_curswant = TRUE;
11336 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011337 }
11338 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
11339 {
11340 char_u *str = get_tv_string_chk(&argvars[0]);
11341 int capcol = -1;
11342
11343 if (str != NULL)
11344 {
11345 /* Check the argument for spelling. */
11346 while (*str != NUL)
11347 {
11348 len = spell_check(curwin, str, &attr, &capcol, FALSE);
11349 if (attr != HLF_COUNT)
11350 {
11351 word = str;
11352 break;
11353 }
11354 str += len;
11355 }
11356 }
11357 }
11358#endif
11359
11360 list_append_string(rettv->vval.v_list, word, len);
11361 list_append_string(rettv->vval.v_list, (char_u *)(
11362 attr == HLF_SPB ? "bad" :
11363 attr == HLF_SPR ? "rare" :
11364 attr == HLF_SPL ? "local" :
11365 attr == HLF_SPC ? "caps" :
11366 ""), -1);
11367}
11368
11369/*
11370 * "spellsuggest()" function
11371 */
11372 static void
11373f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
11374{
11375#ifdef FEAT_SPELL
11376 char_u *str;
11377 int typeerr = FALSE;
11378 int maxcount;
11379 garray_T ga;
11380 int i;
11381 listitem_T *li;
11382 int need_capital = FALSE;
11383#endif
11384
11385 if (rettv_list_alloc(rettv) == FAIL)
11386 return;
11387
11388#ifdef FEAT_SPELL
11389 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
11390 {
11391 str = get_tv_string(&argvars[0]);
11392 if (argvars[1].v_type != VAR_UNKNOWN)
11393 {
11394 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
11395 if (maxcount <= 0)
11396 return;
11397 if (argvars[2].v_type != VAR_UNKNOWN)
11398 {
11399 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
11400 if (typeerr)
11401 return;
11402 }
11403 }
11404 else
11405 maxcount = 25;
11406
11407 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
11408
11409 for (i = 0; i < ga.ga_len; ++i)
11410 {
11411 str = ((char_u **)ga.ga_data)[i];
11412
11413 li = listitem_alloc();
11414 if (li == NULL)
11415 vim_free(str);
11416 else
11417 {
11418 li->li_tv.v_type = VAR_STRING;
11419 li->li_tv.v_lock = 0;
11420 li->li_tv.vval.v_string = str;
11421 list_append(rettv->vval.v_list, li);
11422 }
11423 }
11424 ga_clear(&ga);
11425 }
11426#endif
11427}
11428
11429 static void
11430f_split(typval_T *argvars, typval_T *rettv)
11431{
11432 char_u *str;
11433 char_u *end;
11434 char_u *pat = NULL;
11435 regmatch_T regmatch;
11436 char_u patbuf[NUMBUFLEN];
11437 char_u *save_cpo;
11438 int match;
11439 colnr_T col = 0;
11440 int keepempty = FALSE;
11441 int typeerr = FALSE;
11442
11443 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11444 save_cpo = p_cpo;
11445 p_cpo = (char_u *)"";
11446
11447 str = get_tv_string(&argvars[0]);
11448 if (argvars[1].v_type != VAR_UNKNOWN)
11449 {
11450 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11451 if (pat == NULL)
11452 typeerr = TRUE;
11453 if (argvars[2].v_type != VAR_UNKNOWN)
11454 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
11455 }
11456 if (pat == NULL || *pat == NUL)
11457 pat = (char_u *)"[\\x01- ]\\+";
11458
11459 if (rettv_list_alloc(rettv) == FAIL)
11460 return;
11461 if (typeerr)
11462 return;
11463
11464 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11465 if (regmatch.regprog != NULL)
11466 {
11467 regmatch.rm_ic = FALSE;
11468 while (*str != NUL || keepempty)
11469 {
11470 if (*str == NUL)
11471 match = FALSE; /* empty item at the end */
11472 else
11473 match = vim_regexec_nl(&regmatch, str, col);
11474 if (match)
11475 end = regmatch.startp[0];
11476 else
11477 end = str + STRLEN(str);
11478 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
11479 && *str != NUL && match && end < regmatch.endp[0]))
11480 {
11481 if (list_append_string(rettv->vval.v_list, str,
11482 (int)(end - str)) == FAIL)
11483 break;
11484 }
11485 if (!match)
11486 break;
11487 /* Advance to just after the match. */
11488 if (regmatch.endp[0] > str)
11489 col = 0;
11490 else
11491 {
11492 /* Don't get stuck at the same match. */
11493#ifdef FEAT_MBYTE
11494 col = (*mb_ptr2len)(regmatch.endp[0]);
11495#else
11496 col = 1;
11497#endif
11498 }
11499 str = regmatch.endp[0];
11500 }
11501
11502 vim_regfree(regmatch.regprog);
11503 }
11504
11505 p_cpo = save_cpo;
11506}
11507
11508#ifdef FEAT_FLOAT
11509/*
11510 * "sqrt()" function
11511 */
11512 static void
11513f_sqrt(typval_T *argvars, typval_T *rettv)
11514{
11515 float_T f = 0.0;
11516
11517 rettv->v_type = VAR_FLOAT;
11518 if (get_float_arg(argvars, &f) == OK)
11519 rettv->vval.v_float = sqrt(f);
11520 else
11521 rettv->vval.v_float = 0.0;
11522}
11523
11524/*
11525 * "str2float()" function
11526 */
11527 static void
11528f_str2float(typval_T *argvars, typval_T *rettv)
11529{
11530 char_u *p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011531 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011532
Bram Moolenaar08243d22017-01-10 16:12:29 +010011533 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011534 p = skipwhite(p + 1);
11535 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011536 if (isneg)
11537 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011538 rettv->v_type = VAR_FLOAT;
11539}
11540#endif
11541
11542/*
11543 * "str2nr()" function
11544 */
11545 static void
11546f_str2nr(typval_T *argvars, typval_T *rettv)
11547{
11548 int base = 10;
11549 char_u *p;
11550 varnumber_T n;
11551 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010011552 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011553
11554 if (argvars[1].v_type != VAR_UNKNOWN)
11555 {
11556 base = (int)get_tv_number(&argvars[1]);
11557 if (base != 2 && base != 8 && base != 10 && base != 16)
11558 {
11559 EMSG(_(e_invarg));
11560 return;
11561 }
11562 }
11563
11564 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011565 isneg = (*p == '-');
11566 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011567 p = skipwhite(p + 1);
11568 switch (base)
11569 {
11570 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
11571 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
11572 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
11573 default: what = 0;
11574 }
11575 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011576 if (isneg)
11577 rettv->vval.v_number = -n;
11578 else
11579 rettv->vval.v_number = n;
11580
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011581}
11582
11583#ifdef HAVE_STRFTIME
11584/*
11585 * "strftime({format}[, {time}])" function
11586 */
11587 static void
11588f_strftime(typval_T *argvars, typval_T *rettv)
11589{
11590 char_u result_buf[256];
11591 struct tm *curtime;
11592 time_t seconds;
11593 char_u *p;
11594
11595 rettv->v_type = VAR_STRING;
11596
11597 p = get_tv_string(&argvars[0]);
11598 if (argvars[1].v_type == VAR_UNKNOWN)
11599 seconds = time(NULL);
11600 else
11601 seconds = (time_t)get_tv_number(&argvars[1]);
11602 curtime = localtime(&seconds);
11603 /* MSVC returns NULL for an invalid value of seconds. */
11604 if (curtime == NULL)
11605 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
11606 else
11607 {
11608# ifdef FEAT_MBYTE
11609 vimconv_T conv;
11610 char_u *enc;
11611
11612 conv.vc_type = CONV_NONE;
11613 enc = enc_locale();
11614 convert_setup(&conv, p_enc, enc);
11615 if (conv.vc_type != CONV_NONE)
11616 p = string_convert(&conv, p, NULL);
11617# endif
11618 if (p != NULL)
11619 (void)strftime((char *)result_buf, sizeof(result_buf),
11620 (char *)p, curtime);
11621 else
11622 result_buf[0] = NUL;
11623
11624# ifdef FEAT_MBYTE
11625 if (conv.vc_type != CONV_NONE)
11626 vim_free(p);
11627 convert_setup(&conv, enc, p_enc);
11628 if (conv.vc_type != CONV_NONE)
11629 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
11630 else
11631# endif
11632 rettv->vval.v_string = vim_strsave(result_buf);
11633
11634# ifdef FEAT_MBYTE
11635 /* Release conversion descriptors */
11636 convert_setup(&conv, NULL, NULL);
11637 vim_free(enc);
11638# endif
11639 }
11640}
11641#endif
11642
11643/*
11644 * "strgetchar()" function
11645 */
11646 static void
11647f_strgetchar(typval_T *argvars, typval_T *rettv)
11648{
11649 char_u *str;
11650 int len;
11651 int error = FALSE;
11652 int charidx;
11653
11654 rettv->vval.v_number = -1;
11655 str = get_tv_string_chk(&argvars[0]);
11656 if (str == NULL)
11657 return;
11658 len = (int)STRLEN(str);
11659 charidx = (int)get_tv_number_chk(&argvars[1], &error);
11660 if (error)
11661 return;
11662#ifdef FEAT_MBYTE
11663 {
11664 int byteidx = 0;
11665
11666 while (charidx >= 0 && byteidx < len)
11667 {
11668 if (charidx == 0)
11669 {
11670 rettv->vval.v_number = mb_ptr2char(str + byteidx);
11671 break;
11672 }
11673 --charidx;
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011674 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011675 }
11676 }
11677#else
11678 if (charidx < len)
11679 rettv->vval.v_number = str[charidx];
11680#endif
11681}
11682
11683/*
11684 * "stridx()" function
11685 */
11686 static void
11687f_stridx(typval_T *argvars, typval_T *rettv)
11688{
11689 char_u buf[NUMBUFLEN];
11690 char_u *needle;
11691 char_u *haystack;
11692 char_u *save_haystack;
11693 char_u *pos;
11694 int start_idx;
11695
11696 needle = get_tv_string_chk(&argvars[1]);
11697 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
11698 rettv->vval.v_number = -1;
11699 if (needle == NULL || haystack == NULL)
11700 return; /* type error; errmsg already given */
11701
11702 if (argvars[2].v_type != VAR_UNKNOWN)
11703 {
11704 int error = FALSE;
11705
11706 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
11707 if (error || start_idx >= (int)STRLEN(haystack))
11708 return;
11709 if (start_idx >= 0)
11710 haystack += start_idx;
11711 }
11712
11713 pos = (char_u *)strstr((char *)haystack, (char *)needle);
11714 if (pos != NULL)
11715 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
11716}
11717
11718/*
11719 * "string()" function
11720 */
11721 static void
11722f_string(typval_T *argvars, typval_T *rettv)
11723{
11724 char_u *tofree;
11725 char_u numbuf[NUMBUFLEN];
11726
11727 rettv->v_type = VAR_STRING;
11728 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
11729 get_copyID());
11730 /* Make a copy if we have a value but it's not in allocated memory. */
11731 if (rettv->vval.v_string != NULL && tofree == NULL)
11732 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
11733}
11734
11735/*
11736 * "strlen()" function
11737 */
11738 static void
11739f_strlen(typval_T *argvars, typval_T *rettv)
11740{
11741 rettv->vval.v_number = (varnumber_T)(STRLEN(
11742 get_tv_string(&argvars[0])));
11743}
11744
11745/*
11746 * "strchars()" function
11747 */
11748 static void
11749f_strchars(typval_T *argvars, typval_T *rettv)
11750{
11751 char_u *s = get_tv_string(&argvars[0]);
11752 int skipcc = 0;
11753#ifdef FEAT_MBYTE
11754 varnumber_T len = 0;
11755 int (*func_mb_ptr2char_adv)(char_u **pp);
11756#endif
11757
11758 if (argvars[1].v_type != VAR_UNKNOWN)
11759 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
11760 if (skipcc < 0 || skipcc > 1)
11761 EMSG(_(e_invarg));
11762 else
11763 {
11764#ifdef FEAT_MBYTE
11765 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
11766 while (*s != NUL)
11767 {
11768 func_mb_ptr2char_adv(&s);
11769 ++len;
11770 }
11771 rettv->vval.v_number = len;
11772#else
11773 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
11774#endif
11775 }
11776}
11777
11778/*
11779 * "strdisplaywidth()" function
11780 */
11781 static void
11782f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
11783{
11784 char_u *s = get_tv_string(&argvars[0]);
11785 int col = 0;
11786
11787 if (argvars[1].v_type != VAR_UNKNOWN)
11788 col = (int)get_tv_number(&argvars[1]);
11789
11790 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
11791}
11792
11793/*
11794 * "strwidth()" function
11795 */
11796 static void
11797f_strwidth(typval_T *argvars, typval_T *rettv)
11798{
11799 char_u *s = get_tv_string(&argvars[0]);
11800
11801 rettv->vval.v_number = (varnumber_T)(
11802#ifdef FEAT_MBYTE
11803 mb_string2cells(s, -1)
11804#else
11805 STRLEN(s)
11806#endif
11807 );
11808}
11809
11810/*
11811 * "strcharpart()" function
11812 */
11813 static void
11814f_strcharpart(typval_T *argvars, typval_T *rettv)
11815{
11816#ifdef FEAT_MBYTE
11817 char_u *p;
11818 int nchar;
11819 int nbyte = 0;
11820 int charlen;
11821 int len = 0;
11822 int slen;
11823 int error = FALSE;
11824
11825 p = get_tv_string(&argvars[0]);
11826 slen = (int)STRLEN(p);
11827
11828 nchar = (int)get_tv_number_chk(&argvars[1], &error);
11829 if (!error)
11830 {
11831 if (nchar > 0)
11832 while (nchar > 0 && nbyte < slen)
11833 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011834 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011835 --nchar;
11836 }
11837 else
11838 nbyte = nchar;
11839 if (argvars[2].v_type != VAR_UNKNOWN)
11840 {
11841 charlen = (int)get_tv_number(&argvars[2]);
11842 while (charlen > 0 && nbyte + len < slen)
11843 {
11844 int off = nbyte + len;
11845
11846 if (off < 0)
11847 len += 1;
11848 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011849 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011850 --charlen;
11851 }
11852 }
11853 else
11854 len = slen - nbyte; /* default: all bytes that are available. */
11855 }
11856
11857 /*
11858 * Only return the overlap between the specified part and the actual
11859 * string.
11860 */
11861 if (nbyte < 0)
11862 {
11863 len += nbyte;
11864 nbyte = 0;
11865 }
11866 else if (nbyte > slen)
11867 nbyte = slen;
11868 if (len < 0)
11869 len = 0;
11870 else if (nbyte + len > slen)
11871 len = slen - nbyte;
11872
11873 rettv->v_type = VAR_STRING;
11874 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
11875#else
11876 f_strpart(argvars, rettv);
11877#endif
11878}
11879
11880/*
11881 * "strpart()" function
11882 */
11883 static void
11884f_strpart(typval_T *argvars, typval_T *rettv)
11885{
11886 char_u *p;
11887 int n;
11888 int len;
11889 int slen;
11890 int error = FALSE;
11891
11892 p = get_tv_string(&argvars[0]);
11893 slen = (int)STRLEN(p);
11894
11895 n = (int)get_tv_number_chk(&argvars[1], &error);
11896 if (error)
11897 len = 0;
11898 else if (argvars[2].v_type != VAR_UNKNOWN)
11899 len = (int)get_tv_number(&argvars[2]);
11900 else
11901 len = slen - n; /* default len: all bytes that are available. */
11902
11903 /*
11904 * Only return the overlap between the specified part and the actual
11905 * string.
11906 */
11907 if (n < 0)
11908 {
11909 len += n;
11910 n = 0;
11911 }
11912 else if (n > slen)
11913 n = slen;
11914 if (len < 0)
11915 len = 0;
11916 else if (n + len > slen)
11917 len = slen - n;
11918
11919 rettv->v_type = VAR_STRING;
11920 rettv->vval.v_string = vim_strnsave(p + n, len);
11921}
11922
11923/*
11924 * "strridx()" function
11925 */
11926 static void
11927f_strridx(typval_T *argvars, typval_T *rettv)
11928{
11929 char_u buf[NUMBUFLEN];
11930 char_u *needle;
11931 char_u *haystack;
11932 char_u *rest;
11933 char_u *lastmatch = NULL;
11934 int haystack_len, end_idx;
11935
11936 needle = get_tv_string_chk(&argvars[1]);
11937 haystack = get_tv_string_buf_chk(&argvars[0], buf);
11938
11939 rettv->vval.v_number = -1;
11940 if (needle == NULL || haystack == NULL)
11941 return; /* type error; errmsg already given */
11942
11943 haystack_len = (int)STRLEN(haystack);
11944 if (argvars[2].v_type != VAR_UNKNOWN)
11945 {
11946 /* Third argument: upper limit for index */
11947 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
11948 if (end_idx < 0)
11949 return; /* can never find a match */
11950 }
11951 else
11952 end_idx = haystack_len;
11953
11954 if (*needle == NUL)
11955 {
11956 /* Empty string matches past the end. */
11957 lastmatch = haystack + end_idx;
11958 }
11959 else
11960 {
11961 for (rest = haystack; *rest != '\0'; ++rest)
11962 {
11963 rest = (char_u *)strstr((char *)rest, (char *)needle);
11964 if (rest == NULL || rest > haystack + end_idx)
11965 break;
11966 lastmatch = rest;
11967 }
11968 }
11969
11970 if (lastmatch == NULL)
11971 rettv->vval.v_number = -1;
11972 else
11973 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
11974}
11975
11976/*
11977 * "strtrans()" function
11978 */
11979 static void
11980f_strtrans(typval_T *argvars, typval_T *rettv)
11981{
11982 rettv->v_type = VAR_STRING;
11983 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
11984}
11985
11986/*
11987 * "submatch()" function
11988 */
11989 static void
11990f_submatch(typval_T *argvars, typval_T *rettv)
11991{
11992 int error = FALSE;
11993 int no;
11994 int retList = 0;
11995
11996 no = (int)get_tv_number_chk(&argvars[0], &error);
11997 if (error)
11998 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020011999 if (no < 0 || no >= NSUBEXP)
12000 {
Bram Moolenaar79518e22017-02-17 16:31:35 +010012001 EMSGN(_("E935: invalid submatch number: %d"), no);
12002 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012003 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012004 if (argvars[1].v_type != VAR_UNKNOWN)
12005 retList = (int)get_tv_number_chk(&argvars[1], &error);
12006 if (error)
12007 return;
12008
12009 if (retList == 0)
12010 {
12011 rettv->v_type = VAR_STRING;
12012 rettv->vval.v_string = reg_submatch(no);
12013 }
12014 else
12015 {
12016 rettv->v_type = VAR_LIST;
12017 rettv->vval.v_list = reg_submatch_list(no);
12018 }
12019}
12020
12021/*
12022 * "substitute()" function
12023 */
12024 static void
12025f_substitute(typval_T *argvars, typval_T *rettv)
12026{
12027 char_u patbuf[NUMBUFLEN];
12028 char_u subbuf[NUMBUFLEN];
12029 char_u flagsbuf[NUMBUFLEN];
12030
12031 char_u *str = get_tv_string_chk(&argvars[0]);
12032 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012033 char_u *sub = NULL;
12034 typval_T *expr = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012035 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
12036
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012037 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
12038 expr = &argvars[2];
12039 else
12040 sub = get_tv_string_buf_chk(&argvars[2], subbuf);
12041
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012042 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012043 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
12044 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012045 rettv->vval.v_string = NULL;
12046 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012047 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012048}
12049
12050/*
12051 * "synID(lnum, col, trans)" function
12052 */
12053 static void
12054f_synID(typval_T *argvars UNUSED, typval_T *rettv)
12055{
12056 int id = 0;
12057#ifdef FEAT_SYN_HL
12058 linenr_T lnum;
12059 colnr_T col;
12060 int trans;
12061 int transerr = FALSE;
12062
12063 lnum = get_tv_lnum(argvars); /* -1 on type error */
12064 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12065 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
12066
12067 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12068 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
12069 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
12070#endif
12071
12072 rettv->vval.v_number = id;
12073}
12074
12075/*
12076 * "synIDattr(id, what [, mode])" function
12077 */
12078 static void
12079f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
12080{
12081 char_u *p = NULL;
12082#ifdef FEAT_SYN_HL
12083 int id;
12084 char_u *what;
12085 char_u *mode;
12086 char_u modebuf[NUMBUFLEN];
12087 int modec;
12088
12089 id = (int)get_tv_number(&argvars[0]);
12090 what = get_tv_string(&argvars[1]);
12091 if (argvars[2].v_type != VAR_UNKNOWN)
12092 {
12093 mode = get_tv_string_buf(&argvars[2], modebuf);
12094 modec = TOLOWER_ASC(mode[0]);
12095 if (modec != 't' && modec != 'c' && modec != 'g')
12096 modec = 0; /* replace invalid with current */
12097 }
12098 else
12099 {
12100#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
12101 if (USE_24BIT)
12102 modec = 'g';
12103 else
12104#endif
12105 if (t_colors > 1)
12106 modec = 'c';
12107 else
12108 modec = 't';
12109 }
12110
12111
12112 switch (TOLOWER_ASC(what[0]))
12113 {
12114 case 'b':
12115 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12116 p = highlight_color(id, what, modec);
12117 else /* bold */
12118 p = highlight_has_attr(id, HL_BOLD, modec);
12119 break;
12120
12121 case 'f': /* fg[#] or font */
12122 p = highlight_color(id, what, modec);
12123 break;
12124
12125 case 'i':
12126 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
12127 p = highlight_has_attr(id, HL_INVERSE, modec);
12128 else /* italic */
12129 p = highlight_has_attr(id, HL_ITALIC, modec);
12130 break;
12131
12132 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020012133 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012134 break;
12135
12136 case 'r': /* reverse */
12137 p = highlight_has_attr(id, HL_INVERSE, modec);
12138 break;
12139
12140 case 's':
12141 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
12142 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020012143 /* strikeout */
12144 else if (TOLOWER_ASC(what[1]) == 't' &&
12145 TOLOWER_ASC(what[2]) == 'r')
12146 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012147 else /* standout */
12148 p = highlight_has_attr(id, HL_STANDOUT, modec);
12149 break;
12150
12151 case 'u':
12152 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
12153 /* underline */
12154 p = highlight_has_attr(id, HL_UNDERLINE, modec);
12155 else
12156 /* undercurl */
12157 p = highlight_has_attr(id, HL_UNDERCURL, modec);
12158 break;
12159 }
12160
12161 if (p != NULL)
12162 p = vim_strsave(p);
12163#endif
12164 rettv->v_type = VAR_STRING;
12165 rettv->vval.v_string = p;
12166}
12167
12168/*
12169 * "synIDtrans(id)" function
12170 */
12171 static void
12172f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
12173{
12174 int id;
12175
12176#ifdef FEAT_SYN_HL
12177 id = (int)get_tv_number(&argvars[0]);
12178
12179 if (id > 0)
12180 id = syn_get_final_id(id);
12181 else
12182#endif
12183 id = 0;
12184
12185 rettv->vval.v_number = id;
12186}
12187
12188/*
12189 * "synconcealed(lnum, col)" function
12190 */
12191 static void
12192f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
12193{
12194#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12195 linenr_T lnum;
12196 colnr_T col;
12197 int syntax_flags = 0;
12198 int cchar;
12199 int matchid = 0;
12200 char_u str[NUMBUFLEN];
12201#endif
12202
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012203 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012204
12205#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12206 lnum = get_tv_lnum(argvars); /* -1 on type error */
12207 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12208
12209 vim_memset(str, NUL, sizeof(str));
12210
12211 if (rettv_list_alloc(rettv) != FAIL)
12212 {
12213 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12214 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12215 && curwin->w_p_cole > 0)
12216 {
12217 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
12218 syntax_flags = get_syntax_info(&matchid);
12219
12220 /* get the conceal character */
12221 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
12222 {
12223 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020012224 if (cchar == NUL && curwin->w_p_cole == 1)
12225 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012226 if (cchar != NUL)
12227 {
12228# ifdef FEAT_MBYTE
12229 if (has_mbyte)
12230 (*mb_char2bytes)(cchar, str);
12231 else
12232# endif
12233 str[0] = cchar;
12234 }
12235 }
12236 }
12237
12238 list_append_number(rettv->vval.v_list,
12239 (syntax_flags & HL_CONCEAL) != 0);
12240 /* -1 to auto-determine strlen */
12241 list_append_string(rettv->vval.v_list, str, -1);
12242 list_append_number(rettv->vval.v_list, matchid);
12243 }
12244#endif
12245}
12246
12247/*
12248 * "synstack(lnum, col)" function
12249 */
12250 static void
12251f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
12252{
12253#ifdef FEAT_SYN_HL
12254 linenr_T lnum;
12255 colnr_T col;
12256 int i;
12257 int id;
12258#endif
12259
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012260 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012261
12262#ifdef FEAT_SYN_HL
12263 lnum = get_tv_lnum(argvars); /* -1 on type error */
12264 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12265
12266 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12267 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12268 && rettv_list_alloc(rettv) != FAIL)
12269 {
12270 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
12271 for (i = 0; ; ++i)
12272 {
12273 id = syn_get_stack_item(i);
12274 if (id < 0)
12275 break;
12276 if (list_append_number(rettv->vval.v_list, id) == FAIL)
12277 break;
12278 }
12279 }
12280#endif
12281}
12282
12283 static void
12284get_cmd_output_as_rettv(
12285 typval_T *argvars,
12286 typval_T *rettv,
12287 int retlist)
12288{
12289 char_u *res = NULL;
12290 char_u *p;
12291 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012292 int err = FALSE;
12293 FILE *fd;
12294 list_T *list = NULL;
12295 int flags = SHELL_SILENT;
12296
12297 rettv->v_type = VAR_STRING;
12298 rettv->vval.v_string = NULL;
12299 if (check_restricted() || check_secure())
12300 goto errret;
12301
12302 if (argvars[1].v_type != VAR_UNKNOWN)
12303 {
12304 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010012305 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012306 * command.
12307 */
12308 if ((infile = vim_tempname('i', TRUE)) == NULL)
12309 {
12310 EMSG(_(e_notmp));
12311 goto errret;
12312 }
12313
12314 fd = mch_fopen((char *)infile, WRITEBIN);
12315 if (fd == NULL)
12316 {
12317 EMSG2(_(e_notopen), infile);
12318 goto errret;
12319 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010012320 if (argvars[1].v_type == VAR_NUMBER)
12321 {
12322 linenr_T lnum;
12323 buf_T *buf;
12324
12325 buf = buflist_findnr(argvars[1].vval.v_number);
12326 if (buf == NULL)
12327 {
12328 EMSGN(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010012329 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010012330 goto errret;
12331 }
12332
12333 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
12334 {
12335 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
12336 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
12337 {
12338 err = TRUE;
12339 break;
12340 }
12341 if (putc(NL, fd) == EOF)
12342 {
12343 err = TRUE;
12344 break;
12345 }
12346 }
12347 }
12348 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012349 {
12350 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
12351 err = TRUE;
12352 }
12353 else
12354 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010012355 size_t len;
12356 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012357
12358 p = get_tv_string_buf_chk(&argvars[1], buf);
12359 if (p == NULL)
12360 {
12361 fclose(fd);
12362 goto errret; /* type error; errmsg already given */
12363 }
12364 len = STRLEN(p);
12365 if (len > 0 && fwrite(p, len, 1, fd) != 1)
12366 err = TRUE;
12367 }
12368 if (fclose(fd) != 0)
12369 err = TRUE;
12370 if (err)
12371 {
12372 EMSG(_("E677: Error writing temp file"));
12373 goto errret;
12374 }
12375 }
12376
12377 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
12378 * echoes typeahead, that messes up the display. */
12379 if (!msg_silent)
12380 flags += SHELL_COOKED;
12381
12382 if (retlist)
12383 {
12384 int len;
12385 listitem_T *li;
12386 char_u *s = NULL;
12387 char_u *start;
12388 char_u *end;
12389 int i;
12390
12391 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
12392 if (res == NULL)
12393 goto errret;
12394
12395 list = list_alloc();
12396 if (list == NULL)
12397 goto errret;
12398
12399 for (i = 0; i < len; ++i)
12400 {
12401 start = res + i;
12402 while (i < len && res[i] != NL)
12403 ++i;
12404 end = res + i;
12405
12406 s = alloc((unsigned)(end - start + 1));
12407 if (s == NULL)
12408 goto errret;
12409
12410 for (p = s; start < end; ++p, ++start)
12411 *p = *start == NUL ? NL : *start;
12412 *p = NUL;
12413
12414 li = listitem_alloc();
12415 if (li == NULL)
12416 {
12417 vim_free(s);
12418 goto errret;
12419 }
12420 li->li_tv.v_type = VAR_STRING;
12421 li->li_tv.v_lock = 0;
12422 li->li_tv.vval.v_string = s;
12423 list_append(list, li);
12424 }
12425
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012426 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012427 list = NULL;
12428 }
12429 else
12430 {
12431 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
12432#ifdef USE_CR
12433 /* translate <CR> into <NL> */
12434 if (res != NULL)
12435 {
12436 char_u *s;
12437
12438 for (s = res; *s; ++s)
12439 {
12440 if (*s == CAR)
12441 *s = NL;
12442 }
12443 }
12444#else
12445# ifdef USE_CRNL
12446 /* translate <CR><NL> into <NL> */
12447 if (res != NULL)
12448 {
12449 char_u *s, *d;
12450
12451 d = res;
12452 for (s = res; *s; ++s)
12453 {
12454 if (s[0] == CAR && s[1] == NL)
12455 ++s;
12456 *d++ = *s;
12457 }
12458 *d = NUL;
12459 }
12460# endif
12461#endif
12462 rettv->vval.v_string = res;
12463 res = NULL;
12464 }
12465
12466errret:
12467 if (infile != NULL)
12468 {
12469 mch_remove(infile);
12470 vim_free(infile);
12471 }
12472 if (res != NULL)
12473 vim_free(res);
12474 if (list != NULL)
12475 list_free(list);
12476}
12477
12478/*
12479 * "system()" function
12480 */
12481 static void
12482f_system(typval_T *argvars, typval_T *rettv)
12483{
12484 get_cmd_output_as_rettv(argvars, rettv, FALSE);
12485}
12486
12487/*
12488 * "systemlist()" function
12489 */
12490 static void
12491f_systemlist(typval_T *argvars, typval_T *rettv)
12492{
12493 get_cmd_output_as_rettv(argvars, rettv, TRUE);
12494}
12495
12496/*
12497 * "tabpagebuflist()" function
12498 */
12499 static void
12500f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12501{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012502 tabpage_T *tp;
12503 win_T *wp = NULL;
12504
12505 if (argvars[0].v_type == VAR_UNKNOWN)
12506 wp = firstwin;
12507 else
12508 {
12509 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12510 if (tp != NULL)
12511 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12512 }
12513 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
12514 {
12515 for (; wp != NULL; wp = wp->w_next)
12516 if (list_append_number(rettv->vval.v_list,
12517 wp->w_buffer->b_fnum) == FAIL)
12518 break;
12519 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012520}
12521
12522
12523/*
12524 * "tabpagenr()" function
12525 */
12526 static void
12527f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
12528{
12529 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012530 char_u *arg;
12531
12532 if (argvars[0].v_type != VAR_UNKNOWN)
12533 {
12534 arg = get_tv_string_chk(&argvars[0]);
12535 nr = 0;
12536 if (arg != NULL)
12537 {
12538 if (STRCMP(arg, "$") == 0)
12539 nr = tabpage_index(NULL) - 1;
12540 else
12541 EMSG2(_(e_invexpr2), arg);
12542 }
12543 }
12544 else
12545 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012546 rettv->vval.v_number = nr;
12547}
12548
12549
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012550static int get_winnr(tabpage_T *tp, typval_T *argvar);
12551
12552/*
12553 * Common code for tabpagewinnr() and winnr().
12554 */
12555 static int
12556get_winnr(tabpage_T *tp, typval_T *argvar)
12557{
12558 win_T *twin;
12559 int nr = 1;
12560 win_T *wp;
12561 char_u *arg;
12562
12563 twin = (tp == curtab) ? curwin : tp->tp_curwin;
12564 if (argvar->v_type != VAR_UNKNOWN)
12565 {
12566 arg = get_tv_string_chk(argvar);
12567 if (arg == NULL)
12568 nr = 0; /* type error; errmsg already given */
12569 else if (STRCMP(arg, "$") == 0)
12570 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
12571 else if (STRCMP(arg, "#") == 0)
12572 {
12573 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
12574 if (twin == NULL)
12575 nr = 0;
12576 }
12577 else
12578 {
12579 EMSG2(_(e_invexpr2), arg);
12580 nr = 0;
12581 }
12582 }
12583
12584 if (nr > 0)
12585 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12586 wp != twin; wp = wp->w_next)
12587 {
12588 if (wp == NULL)
12589 {
12590 /* didn't find it in this tabpage */
12591 nr = 0;
12592 break;
12593 }
12594 ++nr;
12595 }
12596 return nr;
12597}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012598
12599/*
12600 * "tabpagewinnr()" function
12601 */
12602 static void
12603f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
12604{
12605 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012606 tabpage_T *tp;
12607
12608 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12609 if (tp == NULL)
12610 nr = 0;
12611 else
12612 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012613 rettv->vval.v_number = nr;
12614}
12615
12616
12617/*
12618 * "tagfiles()" function
12619 */
12620 static void
12621f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
12622{
12623 char_u *fname;
12624 tagname_T tn;
12625 int first;
12626
12627 if (rettv_list_alloc(rettv) == FAIL)
12628 return;
12629 fname = alloc(MAXPATHL);
12630 if (fname == NULL)
12631 return;
12632
12633 for (first = TRUE; ; first = FALSE)
12634 if (get_tagfname(&tn, first, fname) == FAIL
12635 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
12636 break;
12637 tagname_free(&tn);
12638 vim_free(fname);
12639}
12640
12641/*
12642 * "taglist()" function
12643 */
12644 static void
12645f_taglist(typval_T *argvars, typval_T *rettv)
12646{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012647 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012648 char_u *tag_pattern;
12649
12650 tag_pattern = get_tv_string(&argvars[0]);
12651
12652 rettv->vval.v_number = FALSE;
12653 if (*tag_pattern == NUL)
12654 return;
12655
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012656 if (argvars[1].v_type != VAR_UNKNOWN)
12657 fname = get_tv_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012658 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012659 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012660}
12661
12662/*
12663 * "tempname()" function
12664 */
12665 static void
12666f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
12667{
12668 static int x = 'A';
12669
12670 rettv->v_type = VAR_STRING;
12671 rettv->vval.v_string = vim_tempname(x, FALSE);
12672
12673 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
12674 * names. Skip 'I' and 'O', they are used for shell redirection. */
12675 do
12676 {
12677 if (x == 'Z')
12678 x = '0';
12679 else if (x == '9')
12680 x = 'A';
12681 else
12682 {
12683#ifdef EBCDIC
12684 if (x == 'I')
12685 x = 'J';
12686 else if (x == 'R')
12687 x = 'S';
12688 else
12689#endif
12690 ++x;
12691 }
12692 } while (x == 'I' || x == 'O');
12693}
12694
12695#ifdef FEAT_FLOAT
12696/*
12697 * "tan()" function
12698 */
12699 static void
12700f_tan(typval_T *argvars, typval_T *rettv)
12701{
12702 float_T f = 0.0;
12703
12704 rettv->v_type = VAR_FLOAT;
12705 if (get_float_arg(argvars, &f) == OK)
12706 rettv->vval.v_float = tan(f);
12707 else
12708 rettv->vval.v_float = 0.0;
12709}
12710
12711/*
12712 * "tanh()" function
12713 */
12714 static void
12715f_tanh(typval_T *argvars, typval_T *rettv)
12716{
12717 float_T f = 0.0;
12718
12719 rettv->v_type = VAR_FLOAT;
12720 if (get_float_arg(argvars, &f) == OK)
12721 rettv->vval.v_float = tanh(f);
12722 else
12723 rettv->vval.v_float = 0.0;
12724}
12725#endif
12726
12727/*
12728 * "test_alloc_fail(id, countdown, repeat)" function
12729 */
12730 static void
12731f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
12732{
12733 if (argvars[0].v_type != VAR_NUMBER
12734 || argvars[0].vval.v_number <= 0
12735 || argvars[1].v_type != VAR_NUMBER
12736 || argvars[1].vval.v_number < 0
12737 || argvars[2].v_type != VAR_NUMBER)
12738 EMSG(_(e_invarg));
12739 else
12740 {
12741 alloc_fail_id = argvars[0].vval.v_number;
12742 if (alloc_fail_id >= aid_last)
12743 EMSG(_(e_invarg));
12744 alloc_fail_countdown = argvars[1].vval.v_number;
12745 alloc_fail_repeat = argvars[2].vval.v_number;
12746 did_outofmem_msg = FALSE;
12747 }
12748}
12749
12750/*
12751 * "test_autochdir()"
12752 */
12753 static void
12754f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12755{
12756#if defined(FEAT_AUTOCHDIR)
12757 test_autochdir = TRUE;
12758#endif
12759}
12760
12761/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020012762 * "test_feedinput()"
12763 */
12764 static void
12765f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
12766{
12767#ifdef USE_INPUT_BUF
12768 char_u *val = get_tv_string_chk(&argvars[0]);
12769
12770 if (val != NULL)
12771 {
12772 trash_input_buf();
12773 add_to_input_buf_csi(val, (int)STRLEN(val));
12774 }
12775#endif
12776}
12777
12778/*
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012779 * "test_disable({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012780 */
12781 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012782f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012783{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012784 char_u *name = (char_u *)"";
12785 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012786 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012787
12788 if (argvars[0].v_type != VAR_STRING
12789 || (argvars[1].v_type) != VAR_NUMBER)
12790 EMSG(_(e_invarg));
12791 else
12792 {
12793 name = get_tv_string_chk(&argvars[0]);
12794 val = (int)get_tv_number(&argvars[1]);
12795
12796 if (STRCMP(name, (char_u *)"redraw") == 0)
12797 disable_redraw_for_testing = val;
12798 else if (STRCMP(name, (char_u *)"char_avail") == 0)
12799 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012800 else if (STRCMP(name, (char_u *)"starting") == 0)
12801 {
12802 if (val)
12803 {
12804 if (save_starting < 0)
12805 save_starting = starting;
12806 starting = 0;
12807 }
12808 else
12809 {
12810 starting = save_starting;
12811 save_starting = -1;
12812 }
12813 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012814 else if (STRCMP(name, (char_u *)"ALL") == 0)
12815 {
12816 disable_char_avail_for_testing = FALSE;
12817 disable_redraw_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012818 if (save_starting >= 0)
12819 {
12820 starting = save_starting;
12821 save_starting = -1;
12822 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012823 }
12824 else
12825 EMSG2(_(e_invarg2), name);
12826 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012827}
12828
12829/*
12830 * "test_garbagecollect_now()" function
12831 */
12832 static void
12833f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12834{
12835 /* This is dangerous, any Lists and Dicts used internally may be freed
12836 * while still in use. */
12837 garbage_collect(TRUE);
12838}
12839
Bram Moolenaare0c31f62017-03-01 15:07:05 +010012840/*
12841 * "test_ignore_error()" function
12842 */
12843 static void
12844f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
12845{
12846 ignore_error_for_testing(get_tv_string(&argvars[0]));
12847}
12848
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012849#ifdef FEAT_JOB_CHANNEL
12850 static void
12851f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
12852{
12853 rettv->v_type = VAR_CHANNEL;
12854 rettv->vval.v_channel = NULL;
12855}
12856#endif
12857
12858 static void
12859f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
12860{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012861 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012862}
12863
12864#ifdef FEAT_JOB_CHANNEL
12865 static void
12866f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
12867{
12868 rettv->v_type = VAR_JOB;
12869 rettv->vval.v_job = NULL;
12870}
12871#endif
12872
12873 static void
12874f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
12875{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012876 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012877}
12878
12879 static void
12880f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
12881{
12882 rettv->v_type = VAR_PARTIAL;
12883 rettv->vval.v_partial = NULL;
12884}
12885
12886 static void
12887f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
12888{
12889 rettv->v_type = VAR_STRING;
12890 rettv->vval.v_string = NULL;
12891}
12892
12893 static void
12894f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
12895{
12896 time_for_testing = (time_t)get_tv_number(&argvars[0]);
12897}
12898
12899#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
12900/*
12901 * Get a callback from "arg". It can be a Funcref or a function name.
12902 * When "arg" is zero return an empty string.
12903 * Return NULL for an invalid argument.
12904 */
12905 char_u *
12906get_callback(typval_T *arg, partial_T **pp)
12907{
12908 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
12909 {
12910 *pp = arg->vval.v_partial;
12911 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012912 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012913 }
12914 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012915 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012916 {
12917 func_ref(arg->vval.v_string);
12918 return arg->vval.v_string;
12919 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012920 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
12921 return (char_u *)"";
12922 EMSG(_("E921: Invalid callback argument"));
12923 return NULL;
12924}
12925
12926/*
12927 * Unref/free "callback" and "partial" retured by get_callback().
12928 */
12929 void
12930free_callback(char_u *callback, partial_T *partial)
12931{
12932 if (partial != NULL)
12933 partial_unref(partial);
12934 else if (callback != NULL)
12935 {
12936 func_unref(callback);
12937 vim_free(callback);
12938 }
12939}
12940#endif
12941
12942#ifdef FEAT_TIMERS
12943/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020012944 * "timer_info([timer])" function
12945 */
12946 static void
12947f_timer_info(typval_T *argvars, typval_T *rettv)
12948{
12949 timer_T *timer = NULL;
12950
12951 if (rettv_list_alloc(rettv) != OK)
12952 return;
12953 if (argvars[0].v_type != VAR_UNKNOWN)
12954 {
12955 if (argvars[0].v_type != VAR_NUMBER)
12956 EMSG(_(e_number_exp));
12957 else
12958 {
12959 timer = find_timer((int)get_tv_number(&argvars[0]));
12960 if (timer != NULL)
12961 add_timer_info(rettv, timer);
12962 }
12963 }
12964 else
12965 add_timer_info_all(rettv);
12966}
12967
12968/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012969 * "timer_pause(timer, paused)" function
12970 */
12971 static void
12972f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
12973{
12974 timer_T *timer = NULL;
12975 int paused = (int)get_tv_number(&argvars[1]);
12976
12977 if (argvars[0].v_type != VAR_NUMBER)
12978 EMSG(_(e_number_exp));
12979 else
12980 {
12981 timer = find_timer((int)get_tv_number(&argvars[0]));
12982 if (timer != NULL)
12983 timer->tr_paused = paused;
12984 }
12985}
12986
12987/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012988 * "timer_start(time, callback [, options])" function
12989 */
12990 static void
12991f_timer_start(typval_T *argvars, typval_T *rettv)
12992{
Bram Moolenaar75537a92016-09-05 22:45:28 +020012993 long msec = (long)get_tv_number(&argvars[0]);
12994 timer_T *timer;
12995 int repeat = 0;
12996 char_u *callback;
12997 dict_T *dict;
12998 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012999
Bram Moolenaar75537a92016-09-05 22:45:28 +020013000 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013001 if (check_secure())
13002 return;
13003 if (argvars[2].v_type != VAR_UNKNOWN)
13004 {
13005 if (argvars[2].v_type != VAR_DICT
13006 || (dict = argvars[2].vval.v_dict) == NULL)
13007 {
13008 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
13009 return;
13010 }
13011 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
13012 repeat = get_dict_number(dict, (char_u *)"repeat");
13013 }
13014
Bram Moolenaar75537a92016-09-05 22:45:28 +020013015 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013016 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020013017 return;
13018
13019 timer = create_timer(msec, repeat);
13020 if (timer == NULL)
13021 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013022 else
13023 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020013024 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020013025 timer->tr_callback = vim_strsave(callback);
13026 else
13027 /* pointer into the partial */
13028 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020013029 timer->tr_partial = partial;
13030 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013031 }
13032}
13033
13034/*
13035 * "timer_stop(timer)" function
13036 */
13037 static void
13038f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
13039{
13040 timer_T *timer;
13041
13042 if (argvars[0].v_type != VAR_NUMBER)
13043 {
13044 EMSG(_(e_number_exp));
13045 return;
13046 }
13047 timer = find_timer((int)get_tv_number(&argvars[0]));
13048 if (timer != NULL)
13049 stop_timer(timer);
13050}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013051
13052/*
13053 * "timer_stopall()" function
13054 */
13055 static void
13056f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13057{
13058 stop_all_timers();
13059}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013060#endif
13061
13062/*
13063 * "tolower(string)" function
13064 */
13065 static void
13066f_tolower(typval_T *argvars, typval_T *rettv)
13067{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013068 rettv->v_type = VAR_STRING;
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +010013069 rettv->vval.v_string = strlow_save(get_tv_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013070}
13071
13072/*
13073 * "toupper(string)" function
13074 */
13075 static void
13076f_toupper(typval_T *argvars, typval_T *rettv)
13077{
13078 rettv->v_type = VAR_STRING;
13079 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
13080}
13081
13082/*
13083 * "tr(string, fromstr, tostr)" function
13084 */
13085 static void
13086f_tr(typval_T *argvars, typval_T *rettv)
13087{
13088 char_u *in_str;
13089 char_u *fromstr;
13090 char_u *tostr;
13091 char_u *p;
13092#ifdef FEAT_MBYTE
13093 int inlen;
13094 int fromlen;
13095 int tolen;
13096 int idx;
13097 char_u *cpstr;
13098 int cplen;
13099 int first = TRUE;
13100#endif
13101 char_u buf[NUMBUFLEN];
13102 char_u buf2[NUMBUFLEN];
13103 garray_T ga;
13104
13105 in_str = get_tv_string(&argvars[0]);
13106 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
13107 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
13108
13109 /* Default return value: empty string. */
13110 rettv->v_type = VAR_STRING;
13111 rettv->vval.v_string = NULL;
13112 if (fromstr == NULL || tostr == NULL)
13113 return; /* type error; errmsg already given */
13114 ga_init2(&ga, (int)sizeof(char), 80);
13115
13116#ifdef FEAT_MBYTE
13117 if (!has_mbyte)
13118#endif
13119 /* not multi-byte: fromstr and tostr must be the same length */
13120 if (STRLEN(fromstr) != STRLEN(tostr))
13121 {
13122#ifdef FEAT_MBYTE
13123error:
13124#endif
13125 EMSG2(_(e_invarg2), fromstr);
13126 ga_clear(&ga);
13127 return;
13128 }
13129
13130 /* fromstr and tostr have to contain the same number of chars */
13131 while (*in_str != NUL)
13132 {
13133#ifdef FEAT_MBYTE
13134 if (has_mbyte)
13135 {
13136 inlen = (*mb_ptr2len)(in_str);
13137 cpstr = in_str;
13138 cplen = inlen;
13139 idx = 0;
13140 for (p = fromstr; *p != NUL; p += fromlen)
13141 {
13142 fromlen = (*mb_ptr2len)(p);
13143 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
13144 {
13145 for (p = tostr; *p != NUL; p += tolen)
13146 {
13147 tolen = (*mb_ptr2len)(p);
13148 if (idx-- == 0)
13149 {
13150 cplen = tolen;
13151 cpstr = p;
13152 break;
13153 }
13154 }
13155 if (*p == NUL) /* tostr is shorter than fromstr */
13156 goto error;
13157 break;
13158 }
13159 ++idx;
13160 }
13161
13162 if (first && cpstr == in_str)
13163 {
13164 /* Check that fromstr and tostr have the same number of
13165 * (multi-byte) characters. Done only once when a character
13166 * of in_str doesn't appear in fromstr. */
13167 first = FALSE;
13168 for (p = tostr; *p != NUL; p += tolen)
13169 {
13170 tolen = (*mb_ptr2len)(p);
13171 --idx;
13172 }
13173 if (idx != 0)
13174 goto error;
13175 }
13176
13177 (void)ga_grow(&ga, cplen);
13178 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
13179 ga.ga_len += cplen;
13180
13181 in_str += inlen;
13182 }
13183 else
13184#endif
13185 {
13186 /* When not using multi-byte chars we can do it faster. */
13187 p = vim_strchr(fromstr, *in_str);
13188 if (p != NULL)
13189 ga_append(&ga, tostr[p - fromstr]);
13190 else
13191 ga_append(&ga, *in_str);
13192 ++in_str;
13193 }
13194 }
13195
13196 /* add a terminating NUL */
13197 (void)ga_grow(&ga, 1);
13198 ga_append(&ga, NUL);
13199
13200 rettv->vval.v_string = ga.ga_data;
13201}
13202
13203#ifdef FEAT_FLOAT
13204/*
13205 * "trunc({float})" function
13206 */
13207 static void
13208f_trunc(typval_T *argvars, typval_T *rettv)
13209{
13210 float_T f = 0.0;
13211
13212 rettv->v_type = VAR_FLOAT;
13213 if (get_float_arg(argvars, &f) == OK)
13214 /* trunc() is not in C90, use floor() or ceil() instead. */
13215 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
13216 else
13217 rettv->vval.v_float = 0.0;
13218}
13219#endif
13220
13221/*
13222 * "type(expr)" function
13223 */
13224 static void
13225f_type(typval_T *argvars, typval_T *rettv)
13226{
13227 int n = -1;
13228
13229 switch (argvars[0].v_type)
13230 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020013231 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
13232 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013233 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020013234 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
13235 case VAR_LIST: n = VAR_TYPE_LIST; break;
13236 case VAR_DICT: n = VAR_TYPE_DICT; break;
13237 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013238 case VAR_SPECIAL:
13239 if (argvars[0].vval.v_number == VVAL_FALSE
13240 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020013241 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013242 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020013243 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013244 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020013245 case VAR_JOB: n = VAR_TYPE_JOB; break;
13246 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013247 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010013248 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013249 n = -1;
13250 break;
13251 }
13252 rettv->vval.v_number = n;
13253}
13254
13255/*
13256 * "undofile(name)" function
13257 */
13258 static void
13259f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
13260{
13261 rettv->v_type = VAR_STRING;
13262#ifdef FEAT_PERSISTENT_UNDO
13263 {
13264 char_u *fname = get_tv_string(&argvars[0]);
13265
13266 if (*fname == NUL)
13267 {
13268 /* If there is no file name there will be no undo file. */
13269 rettv->vval.v_string = NULL;
13270 }
13271 else
13272 {
13273 char_u *ffname = FullName_save(fname, FALSE);
13274
13275 if (ffname != NULL)
13276 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
13277 vim_free(ffname);
13278 }
13279 }
13280#else
13281 rettv->vval.v_string = NULL;
13282#endif
13283}
13284
13285/*
13286 * "undotree()" function
13287 */
13288 static void
13289f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
13290{
13291 if (rettv_dict_alloc(rettv) == OK)
13292 {
13293 dict_T *dict = rettv->vval.v_dict;
13294 list_T *list;
13295
13296 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
13297 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
13298 dict_add_nr_str(dict, "save_last",
13299 (long)curbuf->b_u_save_nr_last, NULL);
13300 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
13301 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
13302 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
13303
13304 list = list_alloc();
13305 if (list != NULL)
13306 {
13307 u_eval_tree(curbuf->b_u_oldhead, list);
13308 dict_add_list(dict, "entries", list);
13309 }
13310 }
13311}
13312
13313/*
13314 * "values(dict)" function
13315 */
13316 static void
13317f_values(typval_T *argvars, typval_T *rettv)
13318{
13319 dict_list(argvars, rettv, 1);
13320}
13321
13322/*
13323 * "virtcol(string)" function
13324 */
13325 static void
13326f_virtcol(typval_T *argvars, typval_T *rettv)
13327{
13328 colnr_T vcol = 0;
13329 pos_T *fp;
13330 int fnum = curbuf->b_fnum;
13331
13332 fp = var2fpos(&argvars[0], FALSE, &fnum);
13333 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
13334 && fnum == curbuf->b_fnum)
13335 {
13336 getvvcol(curwin, fp, NULL, NULL, &vcol);
13337 ++vcol;
13338 }
13339
13340 rettv->vval.v_number = vcol;
13341}
13342
13343/*
13344 * "visualmode()" function
13345 */
13346 static void
13347f_visualmode(typval_T *argvars, typval_T *rettv)
13348{
13349 char_u str[2];
13350
13351 rettv->v_type = VAR_STRING;
13352 str[0] = curbuf->b_visual_mode_eval;
13353 str[1] = NUL;
13354 rettv->vval.v_string = vim_strsave(str);
13355
13356 /* A non-zero number or non-empty string argument: reset mode. */
13357 if (non_zero_arg(&argvars[0]))
13358 curbuf->b_visual_mode_eval = NUL;
13359}
13360
13361/*
13362 * "wildmenumode()" function
13363 */
13364 static void
13365f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13366{
13367#ifdef FEAT_WILDMENU
13368 if (wild_menu_showing)
13369 rettv->vval.v_number = 1;
13370#endif
13371}
13372
13373/*
13374 * "winbufnr(nr)" function
13375 */
13376 static void
13377f_winbufnr(typval_T *argvars, typval_T *rettv)
13378{
13379 win_T *wp;
13380
13381 wp = find_win_by_nr(&argvars[0], NULL);
13382 if (wp == NULL)
13383 rettv->vval.v_number = -1;
13384 else
13385 rettv->vval.v_number = wp->w_buffer->b_fnum;
13386}
13387
13388/*
13389 * "wincol()" function
13390 */
13391 static void
13392f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
13393{
13394 validate_cursor();
13395 rettv->vval.v_number = curwin->w_wcol + 1;
13396}
13397
13398/*
13399 * "winheight(nr)" function
13400 */
13401 static void
13402f_winheight(typval_T *argvars, typval_T *rettv)
13403{
13404 win_T *wp;
13405
13406 wp = find_win_by_nr(&argvars[0], NULL);
13407 if (wp == NULL)
13408 rettv->vval.v_number = -1;
13409 else
13410 rettv->vval.v_number = wp->w_height;
13411}
13412
13413/*
13414 * "winline()" function
13415 */
13416 static void
13417f_winline(typval_T *argvars UNUSED, typval_T *rettv)
13418{
13419 validate_cursor();
13420 rettv->vval.v_number = curwin->w_wrow + 1;
13421}
13422
13423/*
13424 * "winnr()" function
13425 */
13426 static void
13427f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
13428{
13429 int nr = 1;
13430
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013431 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013432 rettv->vval.v_number = nr;
13433}
13434
13435/*
13436 * "winrestcmd()" function
13437 */
13438 static void
13439f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
13440{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013441 win_T *wp;
13442 int winnr = 1;
13443 garray_T ga;
13444 char_u buf[50];
13445
13446 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020013447 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013448 {
13449 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
13450 ga_concat(&ga, buf);
13451 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
13452 ga_concat(&ga, buf);
13453 ++winnr;
13454 }
13455 ga_append(&ga, NUL);
13456
13457 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013458 rettv->v_type = VAR_STRING;
13459}
13460
13461/*
13462 * "winrestview()" function
13463 */
13464 static void
13465f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
13466{
13467 dict_T *dict;
13468
13469 if (argvars[0].v_type != VAR_DICT
13470 || (dict = argvars[0].vval.v_dict) == NULL)
13471 EMSG(_(e_invarg));
13472 else
13473 {
13474 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
13475 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
13476 if (dict_find(dict, (char_u *)"col", -1) != NULL)
13477 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
13478#ifdef FEAT_VIRTUALEDIT
13479 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
13480 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
13481#endif
13482 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
13483 {
13484 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
13485 curwin->w_set_curswant = FALSE;
13486 }
13487
13488 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
13489 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
13490#ifdef FEAT_DIFF
13491 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
13492 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
13493#endif
13494 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
13495 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
13496 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
13497 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
13498
13499 check_cursor();
13500 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020013501 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013502 changed_window_setting();
13503
13504 if (curwin->w_topline <= 0)
13505 curwin->w_topline = 1;
13506 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
13507 curwin->w_topline = curbuf->b_ml.ml_line_count;
13508#ifdef FEAT_DIFF
13509 check_topfill(curwin, TRUE);
13510#endif
13511 }
13512}
13513
13514/*
13515 * "winsaveview()" function
13516 */
13517 static void
13518f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
13519{
13520 dict_T *dict;
13521
13522 if (rettv_dict_alloc(rettv) == FAIL)
13523 return;
13524 dict = rettv->vval.v_dict;
13525
13526 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
13527 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
13528#ifdef FEAT_VIRTUALEDIT
13529 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
13530#endif
13531 update_curswant();
13532 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
13533
13534 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
13535#ifdef FEAT_DIFF
13536 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
13537#endif
13538 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
13539 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
13540}
13541
13542/*
13543 * "winwidth(nr)" function
13544 */
13545 static void
13546f_winwidth(typval_T *argvars, typval_T *rettv)
13547{
13548 win_T *wp;
13549
13550 wp = find_win_by_nr(&argvars[0], NULL);
13551 if (wp == NULL)
13552 rettv->vval.v_number = -1;
13553 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013554 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013555}
13556
13557/*
13558 * "wordcount()" function
13559 */
13560 static void
13561f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
13562{
13563 if (rettv_dict_alloc(rettv) == FAIL)
13564 return;
13565 cursor_pos_info(rettv->vval.v_dict);
13566}
13567
13568/*
13569 * "writefile()" function
13570 */
13571 static void
13572f_writefile(typval_T *argvars, typval_T *rettv)
13573{
13574 int binary = FALSE;
13575 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013576#ifdef HAVE_FSYNC
13577 int do_fsync = p_fs;
13578#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013579 char_u *fname;
13580 FILE *fd;
13581 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013582 listitem_T *li;
13583 list_T *list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013584
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013585 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013586 if (check_restricted() || check_secure())
13587 return;
13588
13589 if (argvars[0].v_type != VAR_LIST)
13590 {
13591 EMSG2(_(e_listarg), "writefile()");
13592 return;
13593 }
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013594 list = argvars[0].vval.v_list;
13595 if (list == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013596 return;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013597 for (li = list->lv_first; li != NULL; li = li->li_next)
13598 if (get_tv_string_chk(&li->li_tv) == NULL)
13599 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013600
13601 if (argvars[2].v_type != VAR_UNKNOWN)
13602 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013603 char_u *arg2 = get_tv_string_chk(&argvars[2]);
13604
13605 if (arg2 == NULL)
13606 return;
13607 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013608 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013609 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013610 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013611#ifdef HAVE_FSYNC
13612 if (vim_strchr(arg2, 's') != NULL)
13613 do_fsync = TRUE;
13614 else if (vim_strchr(arg2, 'S') != NULL)
13615 do_fsync = FALSE;
13616#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013617 }
13618
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013619 fname = get_tv_string_chk(&argvars[1]);
13620 if (fname == NULL)
13621 return;
13622
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013623 /* Always open the file in binary mode, library functions have a mind of
13624 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013625 if (*fname == NUL || (fd = mch_fopen((char *)fname,
13626 append ? APPENDBIN : WRITEBIN)) == NULL)
13627 {
13628 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
13629 ret = -1;
13630 }
13631 else
13632 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013633 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013634 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013635#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010013636 else if (do_fsync)
13637 /* Ignore the error, the user wouldn't know what to do about it.
13638 * May happen for a device. */
13639 ignored = fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013640#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013641 fclose(fd);
13642 }
13643
13644 rettv->vval.v_number = ret;
13645}
13646
13647/*
13648 * "xor(expr, expr)" function
13649 */
13650 static void
13651f_xor(typval_T *argvars, typval_T *rettv)
13652{
13653 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
13654 ^ get_tv_number_chk(&argvars[1], NULL);
13655}
13656
13657
13658#endif /* FEAT_EVAL */