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