blob: aa0ec60739aae5ccdf91f42830588f52b25e1778 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalfunc.c: Builtin functions
12 */
13#define USING_FLOAT_STUFF
14
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef VMS
24# include <float.h>
25#endif
26
Bram Moolenaard0573012017-10-28 21:11:06 +020027#ifdef MACOS_X
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020028# include <time.h> /* for time_t */
29#endif
30
31static char *e_listarg = N_("E686: Argument of %s must be a List");
32#ifdef FEAT_QUICKFIX
33static char *e_stringreq = N_("E928: String required");
34#endif
35
36#ifdef FEAT_FLOAT
37static void f_abs(typval_T *argvars, typval_T *rettv);
38static void f_acos(typval_T *argvars, typval_T *rettv);
39#endif
40static void f_add(typval_T *argvars, typval_T *rettv);
41static void f_and(typval_T *argvars, typval_T *rettv);
42static void f_append(typval_T *argvars, typval_T *rettv);
43static void f_argc(typval_T *argvars, typval_T *rettv);
44static void f_argidx(typval_T *argvars, typval_T *rettv);
45static void f_arglistid(typval_T *argvars, typval_T *rettv);
46static void f_argv(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +010047static void f_assert_beeps(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020048static void f_assert_equal(typval_T *argvars, typval_T *rettv);
Bram Moolenaard96ff162018-02-18 22:13:29 +010049static void f_assert_equalfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020050static void f_assert_exception(typval_T *argvars, typval_T *rettv);
51static void f_assert_fails(typval_T *argvars, typval_T *rettv);
52static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaar61c04492016-07-23 15:35:35 +020053static void f_assert_inrange(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020054static void f_assert_match(typval_T *argvars, typval_T *rettv);
55static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
56static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar42205552017-03-18 19:42:22 +010057static void f_assert_report(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020058static void f_assert_true(typval_T *argvars, typval_T *rettv);
59#ifdef FEAT_FLOAT
60static void f_asin(typval_T *argvars, typval_T *rettv);
61static void f_atan(typval_T *argvars, typval_T *rettv);
62static void f_atan2(typval_T *argvars, typval_T *rettv);
63#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010064#ifdef FEAT_BEVAL
65static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010066# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010067static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010068# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010069#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020070static void f_browse(typval_T *argvars, typval_T *rettv);
71static void f_browsedir(typval_T *argvars, typval_T *rettv);
72static void f_bufexists(typval_T *argvars, typval_T *rettv);
73static void f_buflisted(typval_T *argvars, typval_T *rettv);
74static void f_bufloaded(typval_T *argvars, typval_T *rettv);
75static void f_bufname(typval_T *argvars, typval_T *rettv);
76static void f_bufnr(typval_T *argvars, typval_T *rettv);
77static void f_bufwinid(typval_T *argvars, typval_T *rettv);
78static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
79static void f_byte2line(typval_T *argvars, typval_T *rettv);
80static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
81static void f_byteidx(typval_T *argvars, typval_T *rettv);
82static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
83static void f_call(typval_T *argvars, typval_T *rettv);
84#ifdef FEAT_FLOAT
85static void f_ceil(typval_T *argvars, typval_T *rettv);
86#endif
87#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +010088static void f_ch_canread(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020089static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0874a832016-09-01 15:11:51 +020090static void f_ch_close_in(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020091static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
92static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
93static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
94static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
95static void f_ch_info(typval_T *argvars, typval_T *rettv);
96static void f_ch_log(typval_T *argvars, typval_T *rettv);
97static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
98static void f_ch_open(typval_T *argvars, typval_T *rettv);
99static void f_ch_read(typval_T *argvars, typval_T *rettv);
100static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
101static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
102static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
103static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
104static void f_ch_status(typval_T *argvars, typval_T *rettv);
105#endif
106static void f_changenr(typval_T *argvars, typval_T *rettv);
107static void f_char2nr(typval_T *argvars, typval_T *rettv);
108static void f_cindent(typval_T *argvars, typval_T *rettv);
109static void f_clearmatches(typval_T *argvars, typval_T *rettv);
110static void f_col(typval_T *argvars, typval_T *rettv);
111#if defined(FEAT_INS_EXPAND)
112static void f_complete(typval_T *argvars, typval_T *rettv);
113static void f_complete_add(typval_T *argvars, typval_T *rettv);
114static void f_complete_check(typval_T *argvars, typval_T *rettv);
115#endif
116static void f_confirm(typval_T *argvars, typval_T *rettv);
117static void f_copy(typval_T *argvars, typval_T *rettv);
118#ifdef FEAT_FLOAT
119static void f_cos(typval_T *argvars, typval_T *rettv);
120static void f_cosh(typval_T *argvars, typval_T *rettv);
121#endif
122static void f_count(typval_T *argvars, typval_T *rettv);
123static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
124static void f_cursor(typval_T *argsvars, typval_T *rettv);
125static void f_deepcopy(typval_T *argvars, typval_T *rettv);
126static void f_delete(typval_T *argvars, typval_T *rettv);
127static void f_did_filetype(typval_T *argvars, typval_T *rettv);
128static void f_diff_filler(typval_T *argvars, typval_T *rettv);
129static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
130static void f_empty(typval_T *argvars, typval_T *rettv);
131static void f_escape(typval_T *argvars, typval_T *rettv);
132static void f_eval(typval_T *argvars, typval_T *rettv);
133static void f_eventhandler(typval_T *argvars, typval_T *rettv);
134static void f_executable(typval_T *argvars, typval_T *rettv);
135static void f_execute(typval_T *argvars, typval_T *rettv);
136static void f_exepath(typval_T *argvars, typval_T *rettv);
137static void f_exists(typval_T *argvars, typval_T *rettv);
138#ifdef FEAT_FLOAT
139static void f_exp(typval_T *argvars, typval_T *rettv);
140#endif
141static void f_expand(typval_T *argvars, typval_T *rettv);
142static void f_extend(typval_T *argvars, typval_T *rettv);
143static void f_feedkeys(typval_T *argvars, typval_T *rettv);
144static void f_filereadable(typval_T *argvars, typval_T *rettv);
145static void f_filewritable(typval_T *argvars, typval_T *rettv);
146static void f_filter(typval_T *argvars, typval_T *rettv);
147static void f_finddir(typval_T *argvars, typval_T *rettv);
148static void f_findfile(typval_T *argvars, typval_T *rettv);
149#ifdef FEAT_FLOAT
150static void f_float2nr(typval_T *argvars, typval_T *rettv);
151static void f_floor(typval_T *argvars, typval_T *rettv);
152static void f_fmod(typval_T *argvars, typval_T *rettv);
153#endif
154static void f_fnameescape(typval_T *argvars, typval_T *rettv);
155static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
156static void f_foldclosed(typval_T *argvars, typval_T *rettv);
157static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
158static void f_foldlevel(typval_T *argvars, typval_T *rettv);
159static void f_foldtext(typval_T *argvars, typval_T *rettv);
160static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
161static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200162static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200163static void f_function(typval_T *argvars, typval_T *rettv);
164static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
165static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200166static void f_getbufinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200167static void f_getbufline(typval_T *argvars, typval_T *rettv);
168static void f_getbufvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100169static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200170static void f_getchar(typval_T *argvars, typval_T *rettv);
171static void f_getcharmod(typval_T *argvars, typval_T *rettv);
172static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
173static void f_getcmdline(typval_T *argvars, typval_T *rettv);
174#if defined(FEAT_CMDL_COMPL)
175static void f_getcompletion(typval_T *argvars, typval_T *rettv);
176#endif
177static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
178static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
179static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
180static void f_getcwd(typval_T *argvars, typval_T *rettv);
181static void f_getfontname(typval_T *argvars, typval_T *rettv);
182static void f_getfperm(typval_T *argvars, typval_T *rettv);
183static void f_getfsize(typval_T *argvars, typval_T *rettv);
184static void f_getftime(typval_T *argvars, typval_T *rettv);
185static void f_getftype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +0100186static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200187static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200188static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200189static void f_getmatches(typval_T *argvars, typval_T *rettv);
190static void f_getpid(typval_T *argvars, typval_T *rettv);
191static void f_getcurpos(typval_T *argvars, typval_T *rettv);
192static void f_getpos(typval_T *argvars, typval_T *rettv);
193static void f_getqflist(typval_T *argvars, typval_T *rettv);
194static void f_getreg(typval_T *argvars, typval_T *rettv);
195static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200196static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200197static void f_gettabvar(typval_T *argvars, typval_T *rettv);
198static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200199static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100200static void f_getwinpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200201static void f_getwinposx(typval_T *argvars, typval_T *rettv);
202static void f_getwinposy(typval_T *argvars, typval_T *rettv);
203static void f_getwinvar(typval_T *argvars, typval_T *rettv);
204static void f_glob(typval_T *argvars, typval_T *rettv);
205static void f_globpath(typval_T *argvars, typval_T *rettv);
206static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
207static void f_has(typval_T *argvars, typval_T *rettv);
208static void f_has_key(typval_T *argvars, typval_T *rettv);
209static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
210static void f_hasmapto(typval_T *argvars, typval_T *rettv);
211static void f_histadd(typval_T *argvars, typval_T *rettv);
212static void f_histdel(typval_T *argvars, typval_T *rettv);
213static void f_histget(typval_T *argvars, typval_T *rettv);
214static void f_histnr(typval_T *argvars, typval_T *rettv);
215static void f_hlID(typval_T *argvars, typval_T *rettv);
216static void f_hlexists(typval_T *argvars, typval_T *rettv);
217static void f_hostname(typval_T *argvars, typval_T *rettv);
218static void f_iconv(typval_T *argvars, typval_T *rettv);
219static void f_indent(typval_T *argvars, typval_T *rettv);
220static void f_index(typval_T *argvars, typval_T *rettv);
221static void f_input(typval_T *argvars, typval_T *rettv);
222static void f_inputdialog(typval_T *argvars, typval_T *rettv);
223static void f_inputlist(typval_T *argvars, typval_T *rettv);
224static void f_inputrestore(typval_T *argvars, typval_T *rettv);
225static void f_inputsave(typval_T *argvars, typval_T *rettv);
226static void f_inputsecret(typval_T *argvars, typval_T *rettv);
227static void f_insert(typval_T *argvars, typval_T *rettv);
228static void f_invert(typval_T *argvars, typval_T *rettv);
229static void f_isdirectory(typval_T *argvars, typval_T *rettv);
230static void f_islocked(typval_T *argvars, typval_T *rettv);
231#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
232static void f_isnan(typval_T *argvars, typval_T *rettv);
233#endif
234static void f_items(typval_T *argvars, typval_T *rettv);
235#ifdef FEAT_JOB_CHANNEL
236static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
237static void f_job_info(typval_T *argvars, typval_T *rettv);
238static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
239static void f_job_start(typval_T *argvars, typval_T *rettv);
240static void f_job_stop(typval_T *argvars, typval_T *rettv);
241static void f_job_status(typval_T *argvars, typval_T *rettv);
242#endif
243static void f_join(typval_T *argvars, typval_T *rettv);
244static void f_js_decode(typval_T *argvars, typval_T *rettv);
245static void f_js_encode(typval_T *argvars, typval_T *rettv);
246static void f_json_decode(typval_T *argvars, typval_T *rettv);
247static void f_json_encode(typval_T *argvars, typval_T *rettv);
248static void f_keys(typval_T *argvars, typval_T *rettv);
249static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
250static void f_len(typval_T *argvars, typval_T *rettv);
251static void f_libcall(typval_T *argvars, typval_T *rettv);
252static void f_libcallnr(typval_T *argvars, typval_T *rettv);
253static void f_line(typval_T *argvars, typval_T *rettv);
254static void f_line2byte(typval_T *argvars, typval_T *rettv);
255static void f_lispindent(typval_T *argvars, typval_T *rettv);
256static void f_localtime(typval_T *argvars, typval_T *rettv);
257#ifdef FEAT_FLOAT
258static void f_log(typval_T *argvars, typval_T *rettv);
259static void f_log10(typval_T *argvars, typval_T *rettv);
260#endif
261#ifdef FEAT_LUA
262static void f_luaeval(typval_T *argvars, typval_T *rettv);
263#endif
264static void f_map(typval_T *argvars, typval_T *rettv);
265static void f_maparg(typval_T *argvars, typval_T *rettv);
266static void f_mapcheck(typval_T *argvars, typval_T *rettv);
267static void f_match(typval_T *argvars, typval_T *rettv);
268static void f_matchadd(typval_T *argvars, typval_T *rettv);
269static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
270static void f_matcharg(typval_T *argvars, typval_T *rettv);
271static void f_matchdelete(typval_T *argvars, typval_T *rettv);
272static void f_matchend(typval_T *argvars, typval_T *rettv);
273static void f_matchlist(typval_T *argvars, typval_T *rettv);
274static void f_matchstr(typval_T *argvars, typval_T *rettv);
275static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
276static void f_max(typval_T *argvars, typval_T *rettv);
277static void f_min(typval_T *argvars, typval_T *rettv);
278#ifdef vim_mkdir
279static void f_mkdir(typval_T *argvars, typval_T *rettv);
280#endif
281static void f_mode(typval_T *argvars, typval_T *rettv);
282#ifdef FEAT_MZSCHEME
283static void f_mzeval(typval_T *argvars, typval_T *rettv);
284#endif
285static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
286static void f_nr2char(typval_T *argvars, typval_T *rettv);
287static void f_or(typval_T *argvars, typval_T *rettv);
288static void f_pathshorten(typval_T *argvars, typval_T *rettv);
289#ifdef FEAT_PERL
290static void f_perleval(typval_T *argvars, typval_T *rettv);
291#endif
292#ifdef FEAT_FLOAT
293static void f_pow(typval_T *argvars, typval_T *rettv);
294#endif
295static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
296static void f_printf(typval_T *argvars, typval_T *rettv);
297static void f_pumvisible(typval_T *argvars, typval_T *rettv);
298#ifdef FEAT_PYTHON3
299static void f_py3eval(typval_T *argvars, typval_T *rettv);
300#endif
301#ifdef FEAT_PYTHON
302static void f_pyeval(typval_T *argvars, typval_T *rettv);
303#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100304#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
305static void f_pyxeval(typval_T *argvars, typval_T *rettv);
306#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200307static void f_range(typval_T *argvars, typval_T *rettv);
308static void f_readfile(typval_T *argvars, typval_T *rettv);
309static void f_reltime(typval_T *argvars, typval_T *rettv);
310#ifdef FEAT_FLOAT
311static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
312#endif
313static void f_reltimestr(typval_T *argvars, typval_T *rettv);
314static void f_remote_expr(typval_T *argvars, typval_T *rettv);
315static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
316static void f_remote_peek(typval_T *argvars, typval_T *rettv);
317static void f_remote_read(typval_T *argvars, typval_T *rettv);
318static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100319static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200320static void f_remove(typval_T *argvars, typval_T *rettv);
321static void f_rename(typval_T *argvars, typval_T *rettv);
322static void f_repeat(typval_T *argvars, typval_T *rettv);
323static void f_resolve(typval_T *argvars, typval_T *rettv);
324static void f_reverse(typval_T *argvars, typval_T *rettv);
325#ifdef FEAT_FLOAT
326static void f_round(typval_T *argvars, typval_T *rettv);
327#endif
328static void f_screenattr(typval_T *argvars, typval_T *rettv);
329static void f_screenchar(typval_T *argvars, typval_T *rettv);
330static void f_screencol(typval_T *argvars, typval_T *rettv);
331static void f_screenrow(typval_T *argvars, typval_T *rettv);
332static void f_search(typval_T *argvars, typval_T *rettv);
333static void f_searchdecl(typval_T *argvars, typval_T *rettv);
334static void f_searchpair(typval_T *argvars, typval_T *rettv);
335static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
336static void f_searchpos(typval_T *argvars, typval_T *rettv);
337static void f_server2client(typval_T *argvars, typval_T *rettv);
338static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200339static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200340static void f_setbufvar(typval_T *argvars, typval_T *rettv);
341static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
342static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
343static void f_setfperm(typval_T *argvars, typval_T *rettv);
344static void f_setline(typval_T *argvars, typval_T *rettv);
345static void f_setloclist(typval_T *argvars, typval_T *rettv);
346static void f_setmatches(typval_T *argvars, typval_T *rettv);
347static void f_setpos(typval_T *argvars, typval_T *rettv);
348static void f_setqflist(typval_T *argvars, typval_T *rettv);
349static void f_setreg(typval_T *argvars, typval_T *rettv);
350static void f_settabvar(typval_T *argvars, typval_T *rettv);
351static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
352static void f_setwinvar(typval_T *argvars, typval_T *rettv);
353#ifdef FEAT_CRYPT
354static void f_sha256(typval_T *argvars, typval_T *rettv);
355#endif /* FEAT_CRYPT */
356static void f_shellescape(typval_T *argvars, typval_T *rettv);
357static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
358static void f_simplify(typval_T *argvars, typval_T *rettv);
359#ifdef FEAT_FLOAT
360static void f_sin(typval_T *argvars, typval_T *rettv);
361static void f_sinh(typval_T *argvars, typval_T *rettv);
362#endif
363static void f_sort(typval_T *argvars, typval_T *rettv);
364static void f_soundfold(typval_T *argvars, typval_T *rettv);
365static void f_spellbadword(typval_T *argvars, typval_T *rettv);
366static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
367static void f_split(typval_T *argvars, typval_T *rettv);
368#ifdef FEAT_FLOAT
369static void f_sqrt(typval_T *argvars, typval_T *rettv);
370static void f_str2float(typval_T *argvars, typval_T *rettv);
371#endif
372static void f_str2nr(typval_T *argvars, typval_T *rettv);
373static void f_strchars(typval_T *argvars, typval_T *rettv);
374#ifdef HAVE_STRFTIME
375static void f_strftime(typval_T *argvars, typval_T *rettv);
376#endif
377static void f_strgetchar(typval_T *argvars, typval_T *rettv);
378static void f_stridx(typval_T *argvars, typval_T *rettv);
379static void f_string(typval_T *argvars, typval_T *rettv);
380static void f_strlen(typval_T *argvars, typval_T *rettv);
381static void f_strcharpart(typval_T *argvars, typval_T *rettv);
382static void f_strpart(typval_T *argvars, typval_T *rettv);
383static void f_strridx(typval_T *argvars, typval_T *rettv);
384static void f_strtrans(typval_T *argvars, typval_T *rettv);
385static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
386static void f_strwidth(typval_T *argvars, typval_T *rettv);
387static void f_submatch(typval_T *argvars, typval_T *rettv);
388static void f_substitute(typval_T *argvars, typval_T *rettv);
389static void f_synID(typval_T *argvars, typval_T *rettv);
390static void f_synIDattr(typval_T *argvars, typval_T *rettv);
391static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
392static void f_synstack(typval_T *argvars, typval_T *rettv);
393static void f_synconcealed(typval_T *argvars, typval_T *rettv);
394static void f_system(typval_T *argvars, typval_T *rettv);
395static void f_systemlist(typval_T *argvars, typval_T *rettv);
396static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
397static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
398static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
399static void f_taglist(typval_T *argvars, typval_T *rettv);
400static void f_tagfiles(typval_T *argvars, typval_T *rettv);
401static void f_tempname(typval_T *argvars, typval_T *rettv);
402static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
403static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200404static void f_test_feedinput(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100405static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200406static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100407static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200408#ifdef FEAT_JOB_CHANNEL
409static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
410#endif
411static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
412#ifdef FEAT_JOB_CHANNEL
413static void f_test_null_job(typval_T *argvars, typval_T *rettv);
414#endif
415static void f_test_null_list(typval_T *argvars, typval_T *rettv);
416static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
417static void f_test_null_string(typval_T *argvars, typval_T *rettv);
418static void f_test_settime(typval_T *argvars, typval_T *rettv);
419#ifdef FEAT_FLOAT
420static void f_tan(typval_T *argvars, typval_T *rettv);
421static void f_tanh(typval_T *argvars, typval_T *rettv);
422#endif
423#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200424static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200425static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200426static void f_timer_start(typval_T *argvars, typval_T *rettv);
427static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200428static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200429#endif
430static void f_tolower(typval_T *argvars, typval_T *rettv);
431static void f_toupper(typval_T *argvars, typval_T *rettv);
432static void f_tr(typval_T *argvars, typval_T *rettv);
433#ifdef FEAT_FLOAT
434static void f_trunc(typval_T *argvars, typval_T *rettv);
435#endif
436static void f_type(typval_T *argvars, typval_T *rettv);
437static void f_undofile(typval_T *argvars, typval_T *rettv);
438static void f_undotree(typval_T *argvars, typval_T *rettv);
439static void f_uniq(typval_T *argvars, typval_T *rettv);
440static void f_values(typval_T *argvars, typval_T *rettv);
441static void f_virtcol(typval_T *argvars, typval_T *rettv);
442static void f_visualmode(typval_T *argvars, typval_T *rettv);
443static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
444static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
445static void f_win_getid(typval_T *argvars, typval_T *rettv);
446static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
447static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
448static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100449static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200450static void f_winbufnr(typval_T *argvars, typval_T *rettv);
451static void f_wincol(typval_T *argvars, typval_T *rettv);
452static void f_winheight(typval_T *argvars, typval_T *rettv);
453static void f_winline(typval_T *argvars, typval_T *rettv);
454static void f_winnr(typval_T *argvars, typval_T *rettv);
455static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
456static void f_winrestview(typval_T *argvars, typval_T *rettv);
457static void f_winsaveview(typval_T *argvars, typval_T *rettv);
458static void f_winwidth(typval_T *argvars, typval_T *rettv);
459static void f_writefile(typval_T *argvars, typval_T *rettv);
460static void f_wordcount(typval_T *argvars, typval_T *rettv);
461static void f_xor(typval_T *argvars, typval_T *rettv);
462
463/*
464 * Array with names and number of arguments of all internal functions
465 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
466 */
467static struct fst
468{
469 char *f_name; /* function name */
470 char f_min_argc; /* minimal number of arguments */
471 char f_max_argc; /* maximal number of arguments */
472 void (*f_func)(typval_T *args, typval_T *rvar);
473 /* implementation of function */
474} functions[] =
475{
476#ifdef FEAT_FLOAT
477 {"abs", 1, 1, f_abs},
478 {"acos", 1, 1, f_acos}, /* WJMc */
479#endif
480 {"add", 2, 2, f_add},
481 {"and", 2, 2, f_and},
482 {"append", 2, 2, f_append},
483 {"argc", 0, 0, f_argc},
484 {"argidx", 0, 0, f_argidx},
485 {"arglistid", 0, 2, f_arglistid},
486 {"argv", 0, 1, f_argv},
487#ifdef FEAT_FLOAT
488 {"asin", 1, 1, f_asin}, /* WJMc */
489#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100490 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200491 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaard96ff162018-02-18 22:13:29 +0100492 {"assert_equalfile", 2, 2, f_assert_equalfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200493 {"assert_exception", 1, 2, f_assert_exception},
494 {"assert_fails", 1, 2, f_assert_fails},
495 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100496 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200497 {"assert_match", 2, 3, f_assert_match},
498 {"assert_notequal", 2, 3, f_assert_notequal},
499 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100500 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200501 {"assert_true", 1, 2, f_assert_true},
502#ifdef FEAT_FLOAT
503 {"atan", 1, 1, f_atan},
504 {"atan2", 2, 2, f_atan2},
505#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100506#ifdef FEAT_BEVAL
507 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100508# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100509 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100510# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100511#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200512 {"browse", 4, 4, f_browse},
513 {"browsedir", 2, 2, f_browsedir},
514 {"bufexists", 1, 1, f_bufexists},
515 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
516 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
517 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
518 {"buflisted", 1, 1, f_buflisted},
519 {"bufloaded", 1, 1, f_bufloaded},
520 {"bufname", 1, 1, f_bufname},
521 {"bufnr", 1, 2, f_bufnr},
522 {"bufwinid", 1, 1, f_bufwinid},
523 {"bufwinnr", 1, 1, f_bufwinnr},
524 {"byte2line", 1, 1, f_byte2line},
525 {"byteidx", 2, 2, f_byteidx},
526 {"byteidxcomp", 2, 2, f_byteidxcomp},
527 {"call", 2, 3, f_call},
528#ifdef FEAT_FLOAT
529 {"ceil", 1, 1, f_ceil},
530#endif
531#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100532 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200533 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200534 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200535 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
536 {"ch_evalraw", 2, 3, f_ch_evalraw},
537 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
538 {"ch_getjob", 1, 1, f_ch_getjob},
539 {"ch_info", 1, 1, f_ch_info},
540 {"ch_log", 1, 2, f_ch_log},
541 {"ch_logfile", 1, 2, f_ch_logfile},
542 {"ch_open", 1, 2, f_ch_open},
543 {"ch_read", 1, 2, f_ch_read},
544 {"ch_readraw", 1, 2, f_ch_readraw},
545 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
546 {"ch_sendraw", 2, 3, f_ch_sendraw},
547 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200548 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200549#endif
550 {"changenr", 0, 0, f_changenr},
551 {"char2nr", 1, 2, f_char2nr},
552 {"cindent", 1, 1, f_cindent},
553 {"clearmatches", 0, 0, f_clearmatches},
554 {"col", 1, 1, f_col},
555#if defined(FEAT_INS_EXPAND)
556 {"complete", 2, 2, f_complete},
557 {"complete_add", 1, 1, f_complete_add},
558 {"complete_check", 0, 0, f_complete_check},
559#endif
560 {"confirm", 1, 4, f_confirm},
561 {"copy", 1, 1, f_copy},
562#ifdef FEAT_FLOAT
563 {"cos", 1, 1, f_cos},
564 {"cosh", 1, 1, f_cosh},
565#endif
566 {"count", 2, 4, f_count},
567 {"cscope_connection",0,3, f_cscope_connection},
568 {"cursor", 1, 3, f_cursor},
569 {"deepcopy", 1, 2, f_deepcopy},
570 {"delete", 1, 2, f_delete},
571 {"did_filetype", 0, 0, f_did_filetype},
572 {"diff_filler", 1, 1, f_diff_filler},
573 {"diff_hlID", 2, 2, f_diff_hlID},
574 {"empty", 1, 1, f_empty},
575 {"escape", 2, 2, f_escape},
576 {"eval", 1, 1, f_eval},
577 {"eventhandler", 0, 0, f_eventhandler},
578 {"executable", 1, 1, f_executable},
579 {"execute", 1, 2, f_execute},
580 {"exepath", 1, 1, f_exepath},
581 {"exists", 1, 1, f_exists},
582#ifdef FEAT_FLOAT
583 {"exp", 1, 1, f_exp},
584#endif
585 {"expand", 1, 3, f_expand},
586 {"extend", 2, 3, f_extend},
587 {"feedkeys", 1, 2, f_feedkeys},
588 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
589 {"filereadable", 1, 1, f_filereadable},
590 {"filewritable", 1, 1, f_filewritable},
591 {"filter", 2, 2, f_filter},
592 {"finddir", 1, 3, f_finddir},
593 {"findfile", 1, 3, f_findfile},
594#ifdef FEAT_FLOAT
595 {"float2nr", 1, 1, f_float2nr},
596 {"floor", 1, 1, f_floor},
597 {"fmod", 2, 2, f_fmod},
598#endif
599 {"fnameescape", 1, 1, f_fnameescape},
600 {"fnamemodify", 2, 2, f_fnamemodify},
601 {"foldclosed", 1, 1, f_foldclosed},
602 {"foldclosedend", 1, 1, f_foldclosedend},
603 {"foldlevel", 1, 1, f_foldlevel},
604 {"foldtext", 0, 0, f_foldtext},
605 {"foldtextresult", 1, 1, f_foldtextresult},
606 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200607 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200608 {"function", 1, 3, f_function},
609 {"garbagecollect", 0, 1, f_garbagecollect},
610 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200611 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200612 {"getbufline", 2, 3, f_getbufline},
613 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100614 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200615 {"getchar", 0, 1, f_getchar},
616 {"getcharmod", 0, 0, f_getcharmod},
617 {"getcharsearch", 0, 0, f_getcharsearch},
618 {"getcmdline", 0, 0, f_getcmdline},
619 {"getcmdpos", 0, 0, f_getcmdpos},
620 {"getcmdtype", 0, 0, f_getcmdtype},
621 {"getcmdwintype", 0, 0, f_getcmdwintype},
622#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200623 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200624#endif
625 {"getcurpos", 0, 0, f_getcurpos},
626 {"getcwd", 0, 2, f_getcwd},
627 {"getfontname", 0, 1, f_getfontname},
628 {"getfperm", 1, 1, f_getfperm},
629 {"getfsize", 1, 1, f_getfsize},
630 {"getftime", 1, 1, f_getftime},
631 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100632 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200633 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200634 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200635 {"getmatches", 0, 0, f_getmatches},
636 {"getpid", 0, 0, f_getpid},
637 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200638 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200639 {"getreg", 0, 3, f_getreg},
640 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200641 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200642 {"gettabvar", 2, 3, f_gettabvar},
643 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200644 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100645 {"getwinpos", 0, 1, f_getwinpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200646 {"getwinposx", 0, 0, f_getwinposx},
647 {"getwinposy", 0, 0, f_getwinposy},
648 {"getwinvar", 2, 3, f_getwinvar},
649 {"glob", 1, 4, f_glob},
650 {"glob2regpat", 1, 1, f_glob2regpat},
651 {"globpath", 2, 5, f_globpath},
652 {"has", 1, 1, f_has},
653 {"has_key", 2, 2, f_has_key},
654 {"haslocaldir", 0, 2, f_haslocaldir},
655 {"hasmapto", 1, 3, f_hasmapto},
656 {"highlightID", 1, 1, f_hlID}, /* obsolete */
657 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
658 {"histadd", 2, 2, f_histadd},
659 {"histdel", 1, 2, f_histdel},
660 {"histget", 1, 2, f_histget},
661 {"histnr", 1, 1, f_histnr},
662 {"hlID", 1, 1, f_hlID},
663 {"hlexists", 1, 1, f_hlexists},
664 {"hostname", 0, 0, f_hostname},
665 {"iconv", 3, 3, f_iconv},
666 {"indent", 1, 1, f_indent},
667 {"index", 2, 4, f_index},
668 {"input", 1, 3, f_input},
669 {"inputdialog", 1, 3, f_inputdialog},
670 {"inputlist", 1, 1, f_inputlist},
671 {"inputrestore", 0, 0, f_inputrestore},
672 {"inputsave", 0, 0, f_inputsave},
673 {"inputsecret", 1, 2, f_inputsecret},
674 {"insert", 2, 3, f_insert},
675 {"invert", 1, 1, f_invert},
676 {"isdirectory", 1, 1, f_isdirectory},
677 {"islocked", 1, 1, f_islocked},
678#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
679 {"isnan", 1, 1, f_isnan},
680#endif
681 {"items", 1, 1, f_items},
682#ifdef FEAT_JOB_CHANNEL
683 {"job_getchannel", 1, 1, f_job_getchannel},
684 {"job_info", 1, 1, f_job_info},
685 {"job_setoptions", 2, 2, f_job_setoptions},
686 {"job_start", 1, 2, f_job_start},
687 {"job_status", 1, 1, f_job_status},
688 {"job_stop", 1, 2, f_job_stop},
689#endif
690 {"join", 1, 2, f_join},
691 {"js_decode", 1, 1, f_js_decode},
692 {"js_encode", 1, 1, f_js_encode},
693 {"json_decode", 1, 1, f_json_decode},
694 {"json_encode", 1, 1, f_json_encode},
695 {"keys", 1, 1, f_keys},
696 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
697 {"len", 1, 1, f_len},
698 {"libcall", 3, 3, f_libcall},
699 {"libcallnr", 3, 3, f_libcallnr},
700 {"line", 1, 1, f_line},
701 {"line2byte", 1, 1, f_line2byte},
702 {"lispindent", 1, 1, f_lispindent},
703 {"localtime", 0, 0, f_localtime},
704#ifdef FEAT_FLOAT
705 {"log", 1, 1, f_log},
706 {"log10", 1, 1, f_log10},
707#endif
708#ifdef FEAT_LUA
709 {"luaeval", 1, 2, f_luaeval},
710#endif
711 {"map", 2, 2, f_map},
712 {"maparg", 1, 4, f_maparg},
713 {"mapcheck", 1, 3, f_mapcheck},
714 {"match", 2, 4, f_match},
715 {"matchadd", 2, 5, f_matchadd},
716 {"matchaddpos", 2, 5, f_matchaddpos},
717 {"matcharg", 1, 1, f_matcharg},
718 {"matchdelete", 1, 1, f_matchdelete},
719 {"matchend", 2, 4, f_matchend},
720 {"matchlist", 2, 4, f_matchlist},
721 {"matchstr", 2, 4, f_matchstr},
722 {"matchstrpos", 2, 4, f_matchstrpos},
723 {"max", 1, 1, f_max},
724 {"min", 1, 1, f_min},
725#ifdef vim_mkdir
726 {"mkdir", 1, 3, f_mkdir},
727#endif
728 {"mode", 0, 1, f_mode},
729#ifdef FEAT_MZSCHEME
730 {"mzeval", 1, 1, f_mzeval},
731#endif
732 {"nextnonblank", 1, 1, f_nextnonblank},
733 {"nr2char", 1, 2, f_nr2char},
734 {"or", 2, 2, f_or},
735 {"pathshorten", 1, 1, f_pathshorten},
736#ifdef FEAT_PERL
737 {"perleval", 1, 1, f_perleval},
738#endif
739#ifdef FEAT_FLOAT
740 {"pow", 2, 2, f_pow},
741#endif
742 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100743 {"printf", 1, 19, f_printf},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200744 {"pumvisible", 0, 0, f_pumvisible},
745#ifdef FEAT_PYTHON3
746 {"py3eval", 1, 1, f_py3eval},
747#endif
748#ifdef FEAT_PYTHON
749 {"pyeval", 1, 1, f_pyeval},
750#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100751#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
752 {"pyxeval", 1, 1, f_pyxeval},
753#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200754 {"range", 1, 3, f_range},
755 {"readfile", 1, 3, f_readfile},
756 {"reltime", 0, 2, f_reltime},
757#ifdef FEAT_FLOAT
758 {"reltimefloat", 1, 1, f_reltimefloat},
759#endif
760 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100761 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200762 {"remote_foreground", 1, 1, f_remote_foreground},
763 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100764 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200765 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100766 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200767 {"remove", 2, 3, f_remove},
768 {"rename", 2, 2, f_rename},
769 {"repeat", 2, 2, f_repeat},
770 {"resolve", 1, 1, f_resolve},
771 {"reverse", 1, 1, f_reverse},
772#ifdef FEAT_FLOAT
773 {"round", 1, 1, f_round},
774#endif
775 {"screenattr", 2, 2, f_screenattr},
776 {"screenchar", 2, 2, f_screenchar},
777 {"screencol", 0, 0, f_screencol},
778 {"screenrow", 0, 0, f_screenrow},
779 {"search", 1, 4, f_search},
780 {"searchdecl", 1, 3, f_searchdecl},
781 {"searchpair", 3, 7, f_searchpair},
782 {"searchpairpos", 3, 7, f_searchpairpos},
783 {"searchpos", 1, 4, f_searchpos},
784 {"server2client", 2, 2, f_server2client},
785 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200786 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200787 {"setbufvar", 3, 3, f_setbufvar},
788 {"setcharsearch", 1, 1, f_setcharsearch},
789 {"setcmdpos", 1, 1, f_setcmdpos},
790 {"setfperm", 2, 2, f_setfperm},
791 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200792 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200793 {"setmatches", 1, 1, f_setmatches},
794 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200795 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200796 {"setreg", 2, 3, f_setreg},
797 {"settabvar", 3, 3, f_settabvar},
798 {"settabwinvar", 4, 4, f_settabwinvar},
799 {"setwinvar", 3, 3, f_setwinvar},
800#ifdef FEAT_CRYPT
801 {"sha256", 1, 1, f_sha256},
802#endif
803 {"shellescape", 1, 2, f_shellescape},
804 {"shiftwidth", 0, 0, f_shiftwidth},
805 {"simplify", 1, 1, f_simplify},
806#ifdef FEAT_FLOAT
807 {"sin", 1, 1, f_sin},
808 {"sinh", 1, 1, f_sinh},
809#endif
810 {"sort", 1, 3, f_sort},
811 {"soundfold", 1, 1, f_soundfold},
812 {"spellbadword", 0, 1, f_spellbadword},
813 {"spellsuggest", 1, 3, f_spellsuggest},
814 {"split", 1, 3, f_split},
815#ifdef FEAT_FLOAT
816 {"sqrt", 1, 1, f_sqrt},
817 {"str2float", 1, 1, f_str2float},
818#endif
819 {"str2nr", 1, 2, f_str2nr},
820 {"strcharpart", 2, 3, f_strcharpart},
821 {"strchars", 1, 2, f_strchars},
822 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
823#ifdef HAVE_STRFTIME
824 {"strftime", 1, 2, f_strftime},
825#endif
826 {"strgetchar", 2, 2, f_strgetchar},
827 {"stridx", 2, 3, f_stridx},
828 {"string", 1, 1, f_string},
829 {"strlen", 1, 1, f_strlen},
830 {"strpart", 2, 3, f_strpart},
831 {"strridx", 2, 3, f_strridx},
832 {"strtrans", 1, 1, f_strtrans},
833 {"strwidth", 1, 1, f_strwidth},
834 {"submatch", 1, 2, f_submatch},
835 {"substitute", 4, 4, f_substitute},
836 {"synID", 3, 3, f_synID},
837 {"synIDattr", 2, 3, f_synIDattr},
838 {"synIDtrans", 1, 1, f_synIDtrans},
839 {"synconcealed", 2, 2, f_synconcealed},
840 {"synstack", 2, 2, f_synstack},
841 {"system", 1, 2, f_system},
842 {"systemlist", 1, 2, f_systemlist},
843 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
844 {"tabpagenr", 0, 1, f_tabpagenr},
845 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
846 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100847 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200848#ifdef FEAT_FLOAT
849 {"tan", 1, 1, f_tan},
850 {"tanh", 1, 1, f_tanh},
851#endif
852 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200853#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100854 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
855 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100856 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200857 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200858 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200859 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200860 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200861 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200862 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200863 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200864 {"term_getstatus", 1, 1, f_term_getstatus},
865 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200866 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200867 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200868 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200869 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100870 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200871 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200872 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200873#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200874 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
875 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200876 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200877 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100878 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200879#ifdef FEAT_JOB_CHANNEL
880 {"test_null_channel", 0, 0, f_test_null_channel},
881#endif
882 {"test_null_dict", 0, 0, f_test_null_dict},
883#ifdef FEAT_JOB_CHANNEL
884 {"test_null_job", 0, 0, f_test_null_job},
885#endif
886 {"test_null_list", 0, 0, f_test_null_list},
887 {"test_null_partial", 0, 0, f_test_null_partial},
888 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100889 {"test_override", 2, 2, f_test_override},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200890 {"test_settime", 1, 1, f_test_settime},
891#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200892 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200893 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200894 {"timer_start", 2, 3, f_timer_start},
895 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200896 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200897#endif
898 {"tolower", 1, 1, f_tolower},
899 {"toupper", 1, 1, f_toupper},
900 {"tr", 3, 3, f_tr},
901#ifdef FEAT_FLOAT
902 {"trunc", 1, 1, f_trunc},
903#endif
904 {"type", 1, 1, f_type},
905 {"undofile", 1, 1, f_undofile},
906 {"undotree", 0, 0, f_undotree},
907 {"uniq", 1, 3, f_uniq},
908 {"values", 1, 1, f_values},
909 {"virtcol", 1, 1, f_virtcol},
910 {"visualmode", 0, 1, f_visualmode},
911 {"wildmenumode", 0, 0, f_wildmenumode},
912 {"win_findbuf", 1, 1, f_win_findbuf},
913 {"win_getid", 0, 2, f_win_getid},
914 {"win_gotoid", 1, 1, f_win_gotoid},
915 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
916 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100917 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200918 {"winbufnr", 1, 1, f_winbufnr},
919 {"wincol", 0, 0, f_wincol},
920 {"winheight", 1, 1, f_winheight},
921 {"winline", 0, 0, f_winline},
922 {"winnr", 0, 1, f_winnr},
923 {"winrestcmd", 0, 0, f_winrestcmd},
924 {"winrestview", 1, 1, f_winrestview},
925 {"winsaveview", 0, 0, f_winsaveview},
926 {"winwidth", 1, 1, f_winwidth},
927 {"wordcount", 0, 0, f_wordcount},
928 {"writefile", 2, 3, f_writefile},
929 {"xor", 2, 2, f_xor},
930};
931
932#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
933
934/*
935 * Function given to ExpandGeneric() to obtain the list of internal
936 * or user defined function names.
937 */
938 char_u *
939get_function_name(expand_T *xp, int idx)
940{
941 static int intidx = -1;
942 char_u *name;
943
944 if (idx == 0)
945 intidx = -1;
946 if (intidx < 0)
947 {
948 name = get_user_func_name(xp, idx);
949 if (name != NULL)
950 return name;
951 }
952 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
953 {
954 STRCPY(IObuff, functions[intidx].f_name);
955 STRCAT(IObuff, "(");
956 if (functions[intidx].f_max_argc == 0)
957 STRCAT(IObuff, ")");
958 return IObuff;
959 }
960
961 return NULL;
962}
963
964/*
965 * Function given to ExpandGeneric() to obtain the list of internal or
966 * user defined variable or function names.
967 */
968 char_u *
969get_expr_name(expand_T *xp, int idx)
970{
971 static int intidx = -1;
972 char_u *name;
973
974 if (idx == 0)
975 intidx = -1;
976 if (intidx < 0)
977 {
978 name = get_function_name(xp, idx);
979 if (name != NULL)
980 return name;
981 }
982 return get_user_var_name(xp, ++intidx);
983}
984
985#endif /* FEAT_CMDL_COMPL */
986
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200987/*
988 * Find internal function in table above.
989 * Return index, or -1 if not found
990 */
991 int
992find_internal_func(
993 char_u *name) /* name of the function */
994{
995 int first = 0;
996 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
997 int cmp;
998 int x;
999
1000 /*
1001 * Find the function name in the table. Binary search.
1002 */
1003 while (first <= last)
1004 {
1005 x = first + ((unsigned)(last - first) >> 1);
1006 cmp = STRCMP(name, functions[x].f_name);
1007 if (cmp < 0)
1008 last = x - 1;
1009 else if (cmp > 0)
1010 first = x + 1;
1011 else
1012 return x;
1013 }
1014 return -1;
1015}
1016
1017 int
1018call_internal_func(
1019 char_u *name,
1020 int argcount,
1021 typval_T *argvars,
1022 typval_T *rettv)
1023{
1024 int i;
1025
1026 i = find_internal_func(name);
1027 if (i < 0)
1028 return ERROR_UNKNOWN;
1029 if (argcount < functions[i].f_min_argc)
1030 return ERROR_TOOFEW;
1031 if (argcount > functions[i].f_max_argc)
1032 return ERROR_TOOMANY;
1033 argvars[argcount].v_type = VAR_UNKNOWN;
1034 functions[i].f_func(argvars, rettv);
1035 return ERROR_NONE;
1036}
1037
1038/*
1039 * Return TRUE for a non-zero Number and a non-empty String.
1040 */
1041 static int
1042non_zero_arg(typval_T *argvars)
1043{
1044 return ((argvars[0].v_type == VAR_NUMBER
1045 && argvars[0].vval.v_number != 0)
1046 || (argvars[0].v_type == VAR_SPECIAL
1047 && argvars[0].vval.v_number == VVAL_TRUE)
1048 || (argvars[0].v_type == VAR_STRING
1049 && argvars[0].vval.v_string != NULL
1050 && *argvars[0].vval.v_string != NUL));
1051}
1052
1053/*
1054 * Get the lnum from the first argument.
1055 * Also accepts ".", "$", etc., but that only works for the current buffer.
1056 * Returns -1 on error.
1057 */
1058 static linenr_T
1059get_tv_lnum(typval_T *argvars)
1060{
1061 typval_T rettv;
1062 linenr_T lnum;
1063
1064 lnum = (linenr_T)get_tv_number_chk(&argvars[0], NULL);
1065 if (lnum == 0) /* no valid number, try using line() */
1066 {
1067 rettv.v_type = VAR_NUMBER;
1068 f_line(argvars, &rettv);
1069 lnum = (linenr_T)rettv.vval.v_number;
1070 clear_tv(&rettv);
1071 }
1072 return lnum;
1073}
1074
1075#ifdef FEAT_FLOAT
1076static int get_float_arg(typval_T *argvars, float_T *f);
1077
1078/*
1079 * Get the float value of "argvars[0]" into "f".
1080 * Returns FAIL when the argument is not a Number or Float.
1081 */
1082 static int
1083get_float_arg(typval_T *argvars, float_T *f)
1084{
1085 if (argvars[0].v_type == VAR_FLOAT)
1086 {
1087 *f = argvars[0].vval.v_float;
1088 return OK;
1089 }
1090 if (argvars[0].v_type == VAR_NUMBER)
1091 {
1092 *f = (float_T)argvars[0].vval.v_number;
1093 return OK;
1094 }
1095 EMSG(_("E808: Number or Float required"));
1096 return FAIL;
1097}
1098
1099/*
1100 * "abs(expr)" function
1101 */
1102 static void
1103f_abs(typval_T *argvars, typval_T *rettv)
1104{
1105 if (argvars[0].v_type == VAR_FLOAT)
1106 {
1107 rettv->v_type = VAR_FLOAT;
1108 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1109 }
1110 else
1111 {
1112 varnumber_T n;
1113 int error = FALSE;
1114
1115 n = get_tv_number_chk(&argvars[0], &error);
1116 if (error)
1117 rettv->vval.v_number = -1;
1118 else if (n > 0)
1119 rettv->vval.v_number = n;
1120 else
1121 rettv->vval.v_number = -n;
1122 }
1123}
1124
1125/*
1126 * "acos()" function
1127 */
1128 static void
1129f_acos(typval_T *argvars, typval_T *rettv)
1130{
1131 float_T f = 0.0;
1132
1133 rettv->v_type = VAR_FLOAT;
1134 if (get_float_arg(argvars, &f) == OK)
1135 rettv->vval.v_float = acos(f);
1136 else
1137 rettv->vval.v_float = 0.0;
1138}
1139#endif
1140
1141/*
1142 * "add(list, item)" function
1143 */
1144 static void
1145f_add(typval_T *argvars, typval_T *rettv)
1146{
1147 list_T *l;
1148
1149 rettv->vval.v_number = 1; /* Default: Failed */
1150 if (argvars[0].v_type == VAR_LIST)
1151 {
1152 if ((l = argvars[0].vval.v_list) != NULL
1153 && !tv_check_lock(l->lv_lock,
1154 (char_u *)N_("add() argument"), TRUE)
1155 && list_append_tv(l, &argvars[1]) == OK)
1156 copy_tv(&argvars[0], rettv);
1157 }
1158 else
1159 EMSG(_(e_listreq));
1160}
1161
1162/*
1163 * "and(expr, expr)" function
1164 */
1165 static void
1166f_and(typval_T *argvars, typval_T *rettv)
1167{
1168 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
1169 & get_tv_number_chk(&argvars[1], NULL);
1170}
1171
1172/*
1173 * "append(lnum, string/list)" function
1174 */
1175 static void
1176f_append(typval_T *argvars, typval_T *rettv)
1177{
1178 long lnum;
1179 char_u *line;
1180 list_T *l = NULL;
1181 listitem_T *li = NULL;
1182 typval_T *tv;
1183 long added = 0;
1184
1185 /* When coming here from Insert mode, sync undo, so that this can be
1186 * undone separately from what was previously inserted. */
1187 if (u_sync_once == 2)
1188 {
1189 u_sync_once = 1; /* notify that u_sync() was called */
1190 u_sync(TRUE);
1191 }
1192
1193 lnum = get_tv_lnum(argvars);
1194 if (lnum >= 0
1195 && lnum <= curbuf->b_ml.ml_line_count
1196 && u_save(lnum, lnum + 1) == OK)
1197 {
1198 if (argvars[1].v_type == VAR_LIST)
1199 {
1200 l = argvars[1].vval.v_list;
1201 if (l == NULL)
1202 return;
1203 li = l->lv_first;
1204 }
1205 for (;;)
1206 {
1207 if (l == NULL)
1208 tv = &argvars[1]; /* append a string */
1209 else if (li == NULL)
1210 break; /* end of list */
1211 else
1212 tv = &li->li_tv; /* append item from list */
1213 line = get_tv_string_chk(tv);
1214 if (line == NULL) /* type error */
1215 {
1216 rettv->vval.v_number = 1; /* Failed */
1217 break;
1218 }
1219 ml_append(lnum + added, line, (colnr_T)0, FALSE);
1220 ++added;
1221 if (l == NULL)
1222 break;
1223 li = li->li_next;
1224 }
1225
1226 appended_lines_mark(lnum, added);
1227 if (curwin->w_cursor.lnum > lnum)
1228 curwin->w_cursor.lnum += added;
1229 }
1230 else
1231 rettv->vval.v_number = 1; /* Failed */
1232}
1233
1234/*
1235 * "argc()" function
1236 */
1237 static void
1238f_argc(typval_T *argvars UNUSED, typval_T *rettv)
1239{
1240 rettv->vval.v_number = ARGCOUNT;
1241}
1242
1243/*
1244 * "argidx()" function
1245 */
1246 static void
1247f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1248{
1249 rettv->vval.v_number = curwin->w_arg_idx;
1250}
1251
1252/*
1253 * "arglistid()" function
1254 */
1255 static void
1256f_arglistid(typval_T *argvars, typval_T *rettv)
1257{
1258 win_T *wp;
1259
1260 rettv->vval.v_number = -1;
1261 wp = find_tabwin(&argvars[0], &argvars[1]);
1262 if (wp != NULL)
1263 rettv->vval.v_number = wp->w_alist->id;
1264}
1265
1266/*
1267 * "argv(nr)" function
1268 */
1269 static void
1270f_argv(typval_T *argvars, typval_T *rettv)
1271{
1272 int idx;
1273
1274 if (argvars[0].v_type != VAR_UNKNOWN)
1275 {
1276 idx = (int)get_tv_number_chk(&argvars[0], NULL);
1277 if (idx >= 0 && idx < ARGCOUNT)
1278 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
1279 else
1280 rettv->vval.v_string = NULL;
1281 rettv->v_type = VAR_STRING;
1282 }
1283 else if (rettv_list_alloc(rettv) == OK)
1284 for (idx = 0; idx < ARGCOUNT; ++idx)
1285 list_append_string(rettv->vval.v_list,
1286 alist_name(&ARGLIST[idx]), -1);
1287}
1288
1289/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001290 * "assert_beeps(cmd [, error])" function
1291 */
1292 static void
1293f_assert_beeps(typval_T *argvars, typval_T *rettv UNUSED)
1294{
1295 assert_beeps(argvars);
1296}
1297
1298/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001299 * "assert_equal(expected, actual[, msg])" function
1300 */
1301 static void
1302f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
1303{
1304 assert_equal_common(argvars, ASSERT_EQUAL);
1305}
1306
1307/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001308 * "assert_equalfile(fname-one, fname-two)" function
1309 */
1310 static void
1311f_assert_equalfile(typval_T *argvars, typval_T *rettv UNUSED)
1312{
1313 assert_equalfile(argvars);
1314}
1315
1316/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001317 * "assert_notequal(expected, actual[, msg])" function
1318 */
1319 static void
1320f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED)
1321{
1322 assert_equal_common(argvars, ASSERT_NOTEQUAL);
1323}
1324
1325/*
1326 * "assert_exception(string[, msg])" function
1327 */
1328 static void
1329f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
1330{
1331 assert_exception(argvars);
1332}
1333
1334/*
1335 * "assert_fails(cmd [, error])" function
1336 */
1337 static void
1338f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
1339{
1340 assert_fails(argvars);
1341}
1342
1343/*
1344 * "assert_false(actual[, msg])" function
1345 */
1346 static void
1347f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
1348{
1349 assert_bool(argvars, FALSE);
1350}
1351
1352/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001353 * "assert_inrange(lower, upper[, msg])" function
1354 */
1355 static void
1356f_assert_inrange(typval_T *argvars, typval_T *rettv UNUSED)
1357{
1358 assert_inrange(argvars);
1359}
1360
1361/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001362 * "assert_match(pattern, actual[, msg])" function
1363 */
1364 static void
1365f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
1366{
1367 assert_match_common(argvars, ASSERT_MATCH);
1368}
1369
1370/*
1371 * "assert_notmatch(pattern, actual[, msg])" function
1372 */
1373 static void
1374f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED)
1375{
1376 assert_match_common(argvars, ASSERT_NOTMATCH);
1377}
1378
1379/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001380 * "assert_report(msg)" function
1381 */
1382 static void
1383f_assert_report(typval_T *argvars, typval_T *rettv UNUSED)
1384{
1385 assert_report(argvars);
1386}
1387
1388/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001389 * "assert_true(actual[, msg])" function
1390 */
1391 static void
1392f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
1393{
1394 assert_bool(argvars, TRUE);
1395}
1396
1397#ifdef FEAT_FLOAT
1398/*
1399 * "asin()" function
1400 */
1401 static void
1402f_asin(typval_T *argvars, typval_T *rettv)
1403{
1404 float_T f = 0.0;
1405
1406 rettv->v_type = VAR_FLOAT;
1407 if (get_float_arg(argvars, &f) == OK)
1408 rettv->vval.v_float = asin(f);
1409 else
1410 rettv->vval.v_float = 0.0;
1411}
1412
1413/*
1414 * "atan()" function
1415 */
1416 static void
1417f_atan(typval_T *argvars, typval_T *rettv)
1418{
1419 float_T f = 0.0;
1420
1421 rettv->v_type = VAR_FLOAT;
1422 if (get_float_arg(argvars, &f) == OK)
1423 rettv->vval.v_float = atan(f);
1424 else
1425 rettv->vval.v_float = 0.0;
1426}
1427
1428/*
1429 * "atan2()" function
1430 */
1431 static void
1432f_atan2(typval_T *argvars, typval_T *rettv)
1433{
1434 float_T fx = 0.0, fy = 0.0;
1435
1436 rettv->v_type = VAR_FLOAT;
1437 if (get_float_arg(argvars, &fx) == OK
1438 && get_float_arg(&argvars[1], &fy) == OK)
1439 rettv->vval.v_float = atan2(fx, fy);
1440 else
1441 rettv->vval.v_float = 0.0;
1442}
1443#endif
1444
1445/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001446 * "balloon_show()" function
1447 */
1448#ifdef FEAT_BEVAL
1449 static void
1450f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1451{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001452 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001453 {
1454 if (argvars[0].v_type == VAR_LIST
1455# ifdef FEAT_GUI
1456 && !gui.in_use
1457# endif
1458 )
1459 post_balloon(balloonEval, NULL, argvars[0].vval.v_list);
1460 else
1461 post_balloon(balloonEval, get_tv_string_chk(&argvars[0]), NULL);
1462 }
1463}
1464
Bram Moolenaar669a8282017-11-19 20:13:05 +01001465# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001466 static void
1467f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1468{
1469 if (rettv_list_alloc(rettv) == OK)
1470 {
1471 char_u *msg = get_tv_string_chk(&argvars[0]);
1472
1473 if (msg != NULL)
1474 {
1475 pumitem_T *array;
1476 int size = split_message(msg, &array);
1477 int i;
1478
1479 /* Skip the first and last item, they are always empty. */
1480 for (i = 1; i < size - 1; ++i)
1481 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001482 while (size > 0)
1483 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001484 vim_free(array);
1485 }
1486 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001487}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001488# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001489#endif
1490
1491/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001492 * "browse(save, title, initdir, default)" function
1493 */
1494 static void
1495f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1496{
1497#ifdef FEAT_BROWSE
1498 int save;
1499 char_u *title;
1500 char_u *initdir;
1501 char_u *defname;
1502 char_u buf[NUMBUFLEN];
1503 char_u buf2[NUMBUFLEN];
1504 int error = FALSE;
1505
1506 save = (int)get_tv_number_chk(&argvars[0], &error);
1507 title = get_tv_string_chk(&argvars[1]);
1508 initdir = get_tv_string_buf_chk(&argvars[2], buf);
1509 defname = get_tv_string_buf_chk(&argvars[3], buf2);
1510
1511 if (error || title == NULL || initdir == NULL || defname == NULL)
1512 rettv->vval.v_string = NULL;
1513 else
1514 rettv->vval.v_string =
1515 do_browse(save ? BROWSE_SAVE : 0,
1516 title, defname, NULL, initdir, NULL, curbuf);
1517#else
1518 rettv->vval.v_string = NULL;
1519#endif
1520 rettv->v_type = VAR_STRING;
1521}
1522
1523/*
1524 * "browsedir(title, initdir)" function
1525 */
1526 static void
1527f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1528{
1529#ifdef FEAT_BROWSE
1530 char_u *title;
1531 char_u *initdir;
1532 char_u buf[NUMBUFLEN];
1533
1534 title = get_tv_string_chk(&argvars[0]);
1535 initdir = get_tv_string_buf_chk(&argvars[1], buf);
1536
1537 if (title == NULL || initdir == NULL)
1538 rettv->vval.v_string = NULL;
1539 else
1540 rettv->vval.v_string = do_browse(BROWSE_DIR,
1541 title, NULL, NULL, initdir, NULL, curbuf);
1542#else
1543 rettv->vval.v_string = NULL;
1544#endif
1545 rettv->v_type = VAR_STRING;
1546}
1547
1548static buf_T *find_buffer(typval_T *avar);
1549
1550/*
1551 * Find a buffer by number or exact name.
1552 */
1553 static buf_T *
1554find_buffer(typval_T *avar)
1555{
1556 buf_T *buf = NULL;
1557
1558 if (avar->v_type == VAR_NUMBER)
1559 buf = buflist_findnr((int)avar->vval.v_number);
1560 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1561 {
1562 buf = buflist_findname_exp(avar->vval.v_string);
1563 if (buf == NULL)
1564 {
1565 /* No full path name match, try a match with a URL or a "nofile"
1566 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001567 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001568 if (buf->b_fname != NULL
1569 && (path_with_url(buf->b_fname)
1570#ifdef FEAT_QUICKFIX
1571 || bt_nofile(buf)
1572#endif
1573 )
1574 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1575 break;
1576 }
1577 }
1578 return buf;
1579}
1580
1581/*
1582 * "bufexists(expr)" function
1583 */
1584 static void
1585f_bufexists(typval_T *argvars, typval_T *rettv)
1586{
1587 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1588}
1589
1590/*
1591 * "buflisted(expr)" function
1592 */
1593 static void
1594f_buflisted(typval_T *argvars, typval_T *rettv)
1595{
1596 buf_T *buf;
1597
1598 buf = find_buffer(&argvars[0]);
1599 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1600}
1601
1602/*
1603 * "bufloaded(expr)" function
1604 */
1605 static void
1606f_bufloaded(typval_T *argvars, typval_T *rettv)
1607{
1608 buf_T *buf;
1609
1610 buf = find_buffer(&argvars[0]);
1611 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1612}
1613
1614 buf_T *
1615buflist_find_by_name(char_u *name, int curtab_only)
1616{
1617 int save_magic;
1618 char_u *save_cpo;
1619 buf_T *buf;
1620
1621 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1622 save_magic = p_magic;
1623 p_magic = TRUE;
1624 save_cpo = p_cpo;
1625 p_cpo = (char_u *)"";
1626
1627 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1628 TRUE, FALSE, curtab_only));
1629
1630 p_magic = save_magic;
1631 p_cpo = save_cpo;
1632 return buf;
1633}
1634
1635/*
1636 * Get buffer by number or pattern.
1637 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001638 buf_T *
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001639get_buf_tv(typval_T *tv, int curtab_only)
1640{
1641 char_u *name = tv->vval.v_string;
1642 buf_T *buf;
1643
1644 if (tv->v_type == VAR_NUMBER)
1645 return buflist_findnr((int)tv->vval.v_number);
1646 if (tv->v_type != VAR_STRING)
1647 return NULL;
1648 if (name == NULL || *name == NUL)
1649 return curbuf;
1650 if (name[0] == '$' && name[1] == NUL)
1651 return lastbuf;
1652
1653 buf = buflist_find_by_name(name, curtab_only);
1654
1655 /* If not found, try expanding the name, like done for bufexists(). */
1656 if (buf == NULL)
1657 buf = find_buffer(tv);
1658
1659 return buf;
1660}
1661
1662/*
1663 * "bufname(expr)" function
1664 */
1665 static void
1666f_bufname(typval_T *argvars, typval_T *rettv)
1667{
1668 buf_T *buf;
1669
1670 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1671 ++emsg_off;
1672 buf = get_buf_tv(&argvars[0], FALSE);
1673 rettv->v_type = VAR_STRING;
1674 if (buf != NULL && buf->b_fname != NULL)
1675 rettv->vval.v_string = vim_strsave(buf->b_fname);
1676 else
1677 rettv->vval.v_string = NULL;
1678 --emsg_off;
1679}
1680
1681/*
1682 * "bufnr(expr)" function
1683 */
1684 static void
1685f_bufnr(typval_T *argvars, typval_T *rettv)
1686{
1687 buf_T *buf;
1688 int error = FALSE;
1689 char_u *name;
1690
1691 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1692 ++emsg_off;
1693 buf = get_buf_tv(&argvars[0], FALSE);
1694 --emsg_off;
1695
1696 /* If the buffer isn't found and the second argument is not zero create a
1697 * new buffer. */
1698 if (buf == NULL
1699 && argvars[1].v_type != VAR_UNKNOWN
1700 && get_tv_number_chk(&argvars[1], &error) != 0
1701 && !error
1702 && (name = get_tv_string_chk(&argvars[0])) != NULL
1703 && !error)
1704 buf = buflist_new(name, NULL, (linenr_T)1, 0);
1705
1706 if (buf != NULL)
1707 rettv->vval.v_number = buf->b_fnum;
1708 else
1709 rettv->vval.v_number = -1;
1710}
1711
1712 static void
1713buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
1714{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001715 win_T *wp;
1716 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001717 buf_T *buf;
1718
1719 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1720 ++emsg_off;
1721 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02001722 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001723 {
1724 ++winnr;
1725 if (wp->w_buffer == buf)
1726 break;
1727 }
1728 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001729 --emsg_off;
1730}
1731
1732/*
1733 * "bufwinid(nr)" function
1734 */
1735 static void
1736f_bufwinid(typval_T *argvars, typval_T *rettv)
1737{
1738 buf_win_common(argvars, rettv, FALSE);
1739}
1740
1741/*
1742 * "bufwinnr(nr)" function
1743 */
1744 static void
1745f_bufwinnr(typval_T *argvars, typval_T *rettv)
1746{
1747 buf_win_common(argvars, rettv, TRUE);
1748}
1749
1750/*
1751 * "byte2line(byte)" function
1752 */
1753 static void
1754f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1755{
1756#ifndef FEAT_BYTEOFF
1757 rettv->vval.v_number = -1;
1758#else
1759 long boff = 0;
1760
1761 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
1762 if (boff < 0)
1763 rettv->vval.v_number = -1;
1764 else
1765 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
1766 (linenr_T)0, &boff);
1767#endif
1768}
1769
1770 static void
1771byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
1772{
1773#ifdef FEAT_MBYTE
1774 char_u *t;
1775#endif
1776 char_u *str;
1777 varnumber_T idx;
1778
1779 str = get_tv_string_chk(&argvars[0]);
1780 idx = get_tv_number_chk(&argvars[1], NULL);
1781 rettv->vval.v_number = -1;
1782 if (str == NULL || idx < 0)
1783 return;
1784
1785#ifdef FEAT_MBYTE
1786 t = str;
1787 for ( ; idx > 0; idx--)
1788 {
1789 if (*t == NUL) /* EOL reached */
1790 return;
1791 if (enc_utf8 && comp)
1792 t += utf_ptr2len(t);
1793 else
1794 t += (*mb_ptr2len)(t);
1795 }
1796 rettv->vval.v_number = (varnumber_T)(t - str);
1797#else
1798 if ((size_t)idx <= STRLEN(str))
1799 rettv->vval.v_number = idx;
1800#endif
1801}
1802
1803/*
1804 * "byteidx()" function
1805 */
1806 static void
1807f_byteidx(typval_T *argvars, typval_T *rettv)
1808{
1809 byteidx(argvars, rettv, FALSE);
1810}
1811
1812/*
1813 * "byteidxcomp()" function
1814 */
1815 static void
1816f_byteidxcomp(typval_T *argvars, typval_T *rettv)
1817{
1818 byteidx(argvars, rettv, TRUE);
1819}
1820
1821/*
1822 * "call(func, arglist [, dict])" function
1823 */
1824 static void
1825f_call(typval_T *argvars, typval_T *rettv)
1826{
1827 char_u *func;
1828 partial_T *partial = NULL;
1829 dict_T *selfdict = NULL;
1830
1831 if (argvars[1].v_type != VAR_LIST)
1832 {
1833 EMSG(_(e_listreq));
1834 return;
1835 }
1836 if (argvars[1].vval.v_list == NULL)
1837 return;
1838
1839 if (argvars[0].v_type == VAR_FUNC)
1840 func = argvars[0].vval.v_string;
1841 else if (argvars[0].v_type == VAR_PARTIAL)
1842 {
1843 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001844 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001845 }
1846 else
1847 func = get_tv_string(&argvars[0]);
1848 if (*func == NUL)
1849 return; /* type error or empty name */
1850
1851 if (argvars[2].v_type != VAR_UNKNOWN)
1852 {
1853 if (argvars[2].v_type != VAR_DICT)
1854 {
1855 EMSG(_(e_dictreq));
1856 return;
1857 }
1858 selfdict = argvars[2].vval.v_dict;
1859 }
1860
1861 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
1862}
1863
1864#ifdef FEAT_FLOAT
1865/*
1866 * "ceil({float})" function
1867 */
1868 static void
1869f_ceil(typval_T *argvars, typval_T *rettv)
1870{
1871 float_T f = 0.0;
1872
1873 rettv->v_type = VAR_FLOAT;
1874 if (get_float_arg(argvars, &f) == OK)
1875 rettv->vval.v_float = ceil(f);
1876 else
1877 rettv->vval.v_float = 0.0;
1878}
1879#endif
1880
1881#ifdef FEAT_JOB_CHANNEL
1882/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01001883 * "ch_canread()" function
1884 */
1885 static void
1886f_ch_canread(typval_T *argvars, typval_T *rettv)
1887{
Bram Moolenaar958dc692016-12-01 15:34:12 +01001888 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01001889
1890 rettv->vval.v_number = 0;
1891 if (channel != NULL)
1892 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
1893 || channel_has_readahead(channel, PART_OUT)
1894 || channel_has_readahead(channel, PART_ERR);
1895}
1896
1897/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001898 * "ch_close()" function
1899 */
1900 static void
1901f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
1902{
1903 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
1904
1905 if (channel != NULL)
1906 {
1907 channel_close(channel, FALSE);
1908 channel_clear(channel);
1909 }
1910}
1911
1912/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02001913 * "ch_close()" function
1914 */
1915 static void
1916f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
1917{
1918 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
1919
1920 if (channel != NULL)
1921 channel_close_in(channel);
1922}
1923
1924/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001925 * "ch_getbufnr()" function
1926 */
1927 static void
1928f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
1929{
1930 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1931
1932 rettv->vval.v_number = -1;
1933 if (channel != NULL)
1934 {
1935 char_u *what = get_tv_string(&argvars[1]);
1936 int part;
1937
1938 if (STRCMP(what, "err") == 0)
1939 part = PART_ERR;
1940 else if (STRCMP(what, "out") == 0)
1941 part = PART_OUT;
1942 else if (STRCMP(what, "in") == 0)
1943 part = PART_IN;
1944 else
1945 part = PART_SOCK;
1946 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
1947 rettv->vval.v_number =
1948 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
1949 }
1950}
1951
1952/*
1953 * "ch_getjob()" function
1954 */
1955 static void
1956f_ch_getjob(typval_T *argvars, typval_T *rettv)
1957{
1958 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1959
1960 if (channel != NULL)
1961 {
1962 rettv->v_type = VAR_JOB;
1963 rettv->vval.v_job = channel->ch_job;
1964 if (channel->ch_job != NULL)
1965 ++channel->ch_job->jv_refcount;
1966 }
1967}
1968
1969/*
1970 * "ch_info()" function
1971 */
1972 static void
1973f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
1974{
1975 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1976
1977 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
1978 channel_info(channel, rettv->vval.v_dict);
1979}
1980
1981/*
1982 * "ch_log()" function
1983 */
1984 static void
1985f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
1986{
1987 char_u *msg = get_tv_string(&argvars[0]);
1988 channel_T *channel = NULL;
1989
1990 if (argvars[1].v_type != VAR_UNKNOWN)
1991 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
1992
1993 ch_log(channel, (char *)msg);
1994}
1995
1996/*
1997 * "ch_logfile()" function
1998 */
1999 static void
2000f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
2001{
2002 char_u *fname;
2003 char_u *opt = (char_u *)"";
2004 char_u buf[NUMBUFLEN];
2005
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02002006 /* Don't open a file in restricted mode. */
2007 if (check_restricted() || check_secure())
2008 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002009 fname = get_tv_string(&argvars[0]);
2010 if (argvars[1].v_type == VAR_STRING)
2011 opt = get_tv_string_buf(&argvars[1], buf);
2012 ch_logfile(fname, opt);
2013}
2014
2015/*
2016 * "ch_open()" function
2017 */
2018 static void
2019f_ch_open(typval_T *argvars, typval_T *rettv)
2020{
2021 rettv->v_type = VAR_CHANNEL;
2022 if (check_restricted() || check_secure())
2023 return;
2024 rettv->vval.v_channel = channel_open_func(argvars);
2025}
2026
2027/*
2028 * "ch_read()" function
2029 */
2030 static void
2031f_ch_read(typval_T *argvars, typval_T *rettv)
2032{
2033 common_channel_read(argvars, rettv, FALSE);
2034}
2035
2036/*
2037 * "ch_readraw()" function
2038 */
2039 static void
2040f_ch_readraw(typval_T *argvars, typval_T *rettv)
2041{
2042 common_channel_read(argvars, rettv, TRUE);
2043}
2044
2045/*
2046 * "ch_evalexpr()" function
2047 */
2048 static void
2049f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2050{
2051 ch_expr_common(argvars, rettv, TRUE);
2052}
2053
2054/*
2055 * "ch_sendexpr()" function
2056 */
2057 static void
2058f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2059{
2060 ch_expr_common(argvars, rettv, FALSE);
2061}
2062
2063/*
2064 * "ch_evalraw()" function
2065 */
2066 static void
2067f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2068{
2069 ch_raw_common(argvars, rettv, TRUE);
2070}
2071
2072/*
2073 * "ch_sendraw()" function
2074 */
2075 static void
2076f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2077{
2078 ch_raw_common(argvars, rettv, FALSE);
2079}
2080
2081/*
2082 * "ch_setoptions()" function
2083 */
2084 static void
2085f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2086{
2087 channel_T *channel;
2088 jobopt_T opt;
2089
2090 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2091 if (channel == NULL)
2092 return;
2093 clear_job_options(&opt);
2094 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002095 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002096 channel_set_options(channel, &opt);
2097 free_job_options(&opt);
2098}
2099
2100/*
2101 * "ch_status()" function
2102 */
2103 static void
2104f_ch_status(typval_T *argvars, typval_T *rettv)
2105{
2106 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002107 jobopt_T opt;
2108 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002109
2110 /* return an empty string by default */
2111 rettv->v_type = VAR_STRING;
2112 rettv->vval.v_string = NULL;
2113
2114 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002115
2116 if (argvars[1].v_type != VAR_UNKNOWN)
2117 {
2118 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002119 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002120 && (opt.jo_set & JO_PART))
2121 part = opt.jo_part;
2122 }
2123
2124 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002125}
2126#endif
2127
2128/*
2129 * "changenr()" function
2130 */
2131 static void
2132f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2133{
2134 rettv->vval.v_number = curbuf->b_u_seq_cur;
2135}
2136
2137/*
2138 * "char2nr(string)" function
2139 */
2140 static void
2141f_char2nr(typval_T *argvars, typval_T *rettv)
2142{
2143#ifdef FEAT_MBYTE
2144 if (has_mbyte)
2145 {
2146 int utf8 = 0;
2147
2148 if (argvars[1].v_type != VAR_UNKNOWN)
2149 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
2150
2151 if (utf8)
2152 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
2153 else
2154 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
2155 }
2156 else
2157#endif
2158 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
2159}
2160
2161/*
2162 * "cindent(lnum)" function
2163 */
2164 static void
2165f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2166{
2167#ifdef FEAT_CINDENT
2168 pos_T pos;
2169 linenr_T lnum;
2170
2171 pos = curwin->w_cursor;
2172 lnum = get_tv_lnum(argvars);
2173 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2174 {
2175 curwin->w_cursor.lnum = lnum;
2176 rettv->vval.v_number = get_c_indent();
2177 curwin->w_cursor = pos;
2178 }
2179 else
2180#endif
2181 rettv->vval.v_number = -1;
2182}
2183
2184/*
2185 * "clearmatches()" function
2186 */
2187 static void
2188f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2189{
2190#ifdef FEAT_SEARCH_EXTRA
2191 clear_matches(curwin);
2192#endif
2193}
2194
2195/*
2196 * "col(string)" function
2197 */
2198 static void
2199f_col(typval_T *argvars, typval_T *rettv)
2200{
2201 colnr_T col = 0;
2202 pos_T *fp;
2203 int fnum = curbuf->b_fnum;
2204
2205 fp = var2fpos(&argvars[0], FALSE, &fnum);
2206 if (fp != NULL && fnum == curbuf->b_fnum)
2207 {
2208 if (fp->col == MAXCOL)
2209 {
2210 /* '> can be MAXCOL, get the length of the line then */
2211 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2212 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2213 else
2214 col = MAXCOL;
2215 }
2216 else
2217 {
2218 col = fp->col + 1;
2219#ifdef FEAT_VIRTUALEDIT
2220 /* col(".") when the cursor is on the NUL at the end of the line
2221 * because of "coladd" can be seen as an extra column. */
2222 if (virtual_active() && fp == &curwin->w_cursor)
2223 {
2224 char_u *p = ml_get_cursor();
2225
2226 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2227 curwin->w_virtcol - curwin->w_cursor.coladd))
2228 {
2229# ifdef FEAT_MBYTE
2230 int l;
2231
2232 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2233 col += l;
2234# else
2235 if (*p != NUL && p[1] == NUL)
2236 ++col;
2237# endif
2238 }
2239 }
2240#endif
2241 }
2242 }
2243 rettv->vval.v_number = col;
2244}
2245
2246#if defined(FEAT_INS_EXPAND)
2247/*
2248 * "complete()" function
2249 */
2250 static void
2251f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2252{
2253 int startcol;
2254
2255 if ((State & INSERT) == 0)
2256 {
2257 EMSG(_("E785: complete() can only be used in Insert mode"));
2258 return;
2259 }
2260
2261 /* Check for undo allowed here, because if something was already inserted
2262 * the line was already saved for undo and this check isn't done. */
2263 if (!undo_allowed())
2264 return;
2265
2266 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2267 {
2268 EMSG(_(e_invarg));
2269 return;
2270 }
2271
2272 startcol = (int)get_tv_number_chk(&argvars[0], NULL);
2273 if (startcol <= 0)
2274 return;
2275
2276 set_completion(startcol - 1, argvars[1].vval.v_list);
2277}
2278
2279/*
2280 * "complete_add()" function
2281 */
2282 static void
2283f_complete_add(typval_T *argvars, typval_T *rettv)
2284{
2285 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2286}
2287
2288/*
2289 * "complete_check()" function
2290 */
2291 static void
2292f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2293{
2294 int saved = RedrawingDisabled;
2295
2296 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002297 ins_compl_check_keys(0, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002298 rettv->vval.v_number = compl_interrupted;
2299 RedrawingDisabled = saved;
2300}
2301#endif
2302
2303/*
2304 * "confirm(message, buttons[, default [, type]])" function
2305 */
2306 static void
2307f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2308{
2309#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2310 char_u *message;
2311 char_u *buttons = NULL;
2312 char_u buf[NUMBUFLEN];
2313 char_u buf2[NUMBUFLEN];
2314 int def = 1;
2315 int type = VIM_GENERIC;
2316 char_u *typestr;
2317 int error = FALSE;
2318
2319 message = get_tv_string_chk(&argvars[0]);
2320 if (message == NULL)
2321 error = TRUE;
2322 if (argvars[1].v_type != VAR_UNKNOWN)
2323 {
2324 buttons = get_tv_string_buf_chk(&argvars[1], buf);
2325 if (buttons == NULL)
2326 error = TRUE;
2327 if (argvars[2].v_type != VAR_UNKNOWN)
2328 {
2329 def = (int)get_tv_number_chk(&argvars[2], &error);
2330 if (argvars[3].v_type != VAR_UNKNOWN)
2331 {
2332 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
2333 if (typestr == NULL)
2334 error = TRUE;
2335 else
2336 {
2337 switch (TOUPPER_ASC(*typestr))
2338 {
2339 case 'E': type = VIM_ERROR; break;
2340 case 'Q': type = VIM_QUESTION; break;
2341 case 'I': type = VIM_INFO; break;
2342 case 'W': type = VIM_WARNING; break;
2343 case 'G': type = VIM_GENERIC; break;
2344 }
2345 }
2346 }
2347 }
2348 }
2349
2350 if (buttons == NULL || *buttons == NUL)
2351 buttons = (char_u *)_("&Ok");
2352
2353 if (!error)
2354 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2355 def, NULL, FALSE);
2356#endif
2357}
2358
2359/*
2360 * "copy()" function
2361 */
2362 static void
2363f_copy(typval_T *argvars, typval_T *rettv)
2364{
2365 item_copy(&argvars[0], rettv, FALSE, 0);
2366}
2367
2368#ifdef FEAT_FLOAT
2369/*
2370 * "cos()" function
2371 */
2372 static void
2373f_cos(typval_T *argvars, typval_T *rettv)
2374{
2375 float_T f = 0.0;
2376
2377 rettv->v_type = VAR_FLOAT;
2378 if (get_float_arg(argvars, &f) == OK)
2379 rettv->vval.v_float = cos(f);
2380 else
2381 rettv->vval.v_float = 0.0;
2382}
2383
2384/*
2385 * "cosh()" function
2386 */
2387 static void
2388f_cosh(typval_T *argvars, typval_T *rettv)
2389{
2390 float_T f = 0.0;
2391
2392 rettv->v_type = VAR_FLOAT;
2393 if (get_float_arg(argvars, &f) == OK)
2394 rettv->vval.v_float = cosh(f);
2395 else
2396 rettv->vval.v_float = 0.0;
2397}
2398#endif
2399
2400/*
2401 * "count()" function
2402 */
2403 static void
2404f_count(typval_T *argvars, typval_T *rettv)
2405{
2406 long n = 0;
2407 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002408 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002409
Bram Moolenaar9966b212017-07-28 16:46:57 +02002410 if (argvars[2].v_type != VAR_UNKNOWN)
2411 ic = (int)get_tv_number_chk(&argvars[2], &error);
2412
2413 if (argvars[0].v_type == VAR_STRING)
2414 {
2415 char_u *expr = get_tv_string_chk(&argvars[1]);
2416 char_u *p = argvars[0].vval.v_string;
2417 char_u *next;
2418
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002419 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002420 {
2421 if (ic)
2422 {
2423 size_t len = STRLEN(expr);
2424
2425 while (*p != NUL)
2426 {
2427 if (MB_STRNICMP(p, expr, len) == 0)
2428 {
2429 ++n;
2430 p += len;
2431 }
2432 else
2433 MB_PTR_ADV(p);
2434 }
2435 }
2436 else
2437 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2438 != NULL)
2439 {
2440 ++n;
2441 p = next + STRLEN(expr);
2442 }
2443 }
2444
2445 }
2446 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002447 {
2448 listitem_T *li;
2449 list_T *l;
2450 long idx;
2451
2452 if ((l = argvars[0].vval.v_list) != NULL)
2453 {
2454 li = l->lv_first;
2455 if (argvars[2].v_type != VAR_UNKNOWN)
2456 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002457 if (argvars[3].v_type != VAR_UNKNOWN)
2458 {
2459 idx = (long)get_tv_number_chk(&argvars[3], &error);
2460 if (!error)
2461 {
2462 li = list_find(l, idx);
2463 if (li == NULL)
2464 EMSGN(_(e_listidx), idx);
2465 }
2466 }
2467 if (error)
2468 li = NULL;
2469 }
2470
2471 for ( ; li != NULL; li = li->li_next)
2472 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2473 ++n;
2474 }
2475 }
2476 else if (argvars[0].v_type == VAR_DICT)
2477 {
2478 int todo;
2479 dict_T *d;
2480 hashitem_T *hi;
2481
2482 if ((d = argvars[0].vval.v_dict) != NULL)
2483 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002484 if (argvars[2].v_type != VAR_UNKNOWN)
2485 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002486 if (argvars[3].v_type != VAR_UNKNOWN)
2487 EMSG(_(e_invarg));
2488 }
2489
2490 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2491 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2492 {
2493 if (!HASHITEM_EMPTY(hi))
2494 {
2495 --todo;
2496 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2497 ++n;
2498 }
2499 }
2500 }
2501 }
2502 else
2503 EMSG2(_(e_listdictarg), "count()");
2504 rettv->vval.v_number = n;
2505}
2506
2507/*
2508 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2509 *
2510 * Checks the existence of a cscope connection.
2511 */
2512 static void
2513f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2514{
2515#ifdef FEAT_CSCOPE
2516 int num = 0;
2517 char_u *dbpath = NULL;
2518 char_u *prepend = NULL;
2519 char_u buf[NUMBUFLEN];
2520
2521 if (argvars[0].v_type != VAR_UNKNOWN
2522 && argvars[1].v_type != VAR_UNKNOWN)
2523 {
2524 num = (int)get_tv_number(&argvars[0]);
2525 dbpath = get_tv_string(&argvars[1]);
2526 if (argvars[2].v_type != VAR_UNKNOWN)
2527 prepend = get_tv_string_buf(&argvars[2], buf);
2528 }
2529
2530 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2531#endif
2532}
2533
2534/*
2535 * "cursor(lnum, col)" function, or
2536 * "cursor(list)"
2537 *
2538 * Moves the cursor to the specified line and column.
2539 * Returns 0 when the position could be set, -1 otherwise.
2540 */
2541 static void
2542f_cursor(typval_T *argvars, typval_T *rettv)
2543{
2544 long line, col;
2545#ifdef FEAT_VIRTUALEDIT
2546 long coladd = 0;
2547#endif
2548 int set_curswant = TRUE;
2549
2550 rettv->vval.v_number = -1;
2551 if (argvars[1].v_type == VAR_UNKNOWN)
2552 {
2553 pos_T pos;
2554 colnr_T curswant = -1;
2555
2556 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2557 {
2558 EMSG(_(e_invarg));
2559 return;
2560 }
2561 line = pos.lnum;
2562 col = pos.col;
2563#ifdef FEAT_VIRTUALEDIT
2564 coladd = pos.coladd;
2565#endif
2566 if (curswant >= 0)
2567 {
2568 curwin->w_curswant = curswant - 1;
2569 set_curswant = FALSE;
2570 }
2571 }
2572 else
2573 {
2574 line = get_tv_lnum(argvars);
2575 col = (long)get_tv_number_chk(&argvars[1], NULL);
2576#ifdef FEAT_VIRTUALEDIT
2577 if (argvars[2].v_type != VAR_UNKNOWN)
2578 coladd = (long)get_tv_number_chk(&argvars[2], NULL);
2579#endif
2580 }
2581 if (line < 0 || col < 0
2582#ifdef FEAT_VIRTUALEDIT
2583 || coladd < 0
2584#endif
2585 )
2586 return; /* type error; errmsg already given */
2587 if (line > 0)
2588 curwin->w_cursor.lnum = line;
2589 if (col > 0)
2590 curwin->w_cursor.col = col - 1;
2591#ifdef FEAT_VIRTUALEDIT
2592 curwin->w_cursor.coladd = coladd;
2593#endif
2594
2595 /* Make sure the cursor is in a valid position. */
2596 check_cursor();
2597#ifdef FEAT_MBYTE
2598 /* Correct cursor for multi-byte character. */
2599 if (has_mbyte)
2600 mb_adjust_cursor();
2601#endif
2602
2603 curwin->w_set_curswant = set_curswant;
2604 rettv->vval.v_number = 0;
2605}
2606
2607/*
2608 * "deepcopy()" function
2609 */
2610 static void
2611f_deepcopy(typval_T *argvars, typval_T *rettv)
2612{
2613 int noref = 0;
2614 int copyID;
2615
2616 if (argvars[1].v_type != VAR_UNKNOWN)
2617 noref = (int)get_tv_number_chk(&argvars[1], NULL);
2618 if (noref < 0 || noref > 1)
2619 EMSG(_(e_invarg));
2620 else
2621 {
2622 copyID = get_copyID();
2623 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2624 }
2625}
2626
2627/*
2628 * "delete()" function
2629 */
2630 static void
2631f_delete(typval_T *argvars, typval_T *rettv)
2632{
2633 char_u nbuf[NUMBUFLEN];
2634 char_u *name;
2635 char_u *flags;
2636
2637 rettv->vval.v_number = -1;
2638 if (check_restricted() || check_secure())
2639 return;
2640
2641 name = get_tv_string(&argvars[0]);
2642 if (name == NULL || *name == NUL)
2643 {
2644 EMSG(_(e_invarg));
2645 return;
2646 }
2647
2648 if (argvars[1].v_type != VAR_UNKNOWN)
2649 flags = get_tv_string_buf(&argvars[1], nbuf);
2650 else
2651 flags = (char_u *)"";
2652
2653 if (*flags == NUL)
2654 /* delete a file */
2655 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2656 else if (STRCMP(flags, "d") == 0)
2657 /* delete an empty directory */
2658 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2659 else if (STRCMP(flags, "rf") == 0)
2660 /* delete a directory recursively */
2661 rettv->vval.v_number = delete_recursive(name);
2662 else
2663 EMSG2(_(e_invexpr2), flags);
2664}
2665
2666/*
2667 * "did_filetype()" function
2668 */
2669 static void
2670f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2671{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002672 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002673}
2674
2675/*
2676 * "diff_filler()" function
2677 */
2678 static void
2679f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2680{
2681#ifdef FEAT_DIFF
2682 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
2683#endif
2684}
2685
2686/*
2687 * "diff_hlID()" function
2688 */
2689 static void
2690f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2691{
2692#ifdef FEAT_DIFF
2693 linenr_T lnum = get_tv_lnum(argvars);
2694 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01002695 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002696 static int fnum = 0;
2697 static int change_start = 0;
2698 static int change_end = 0;
2699 static hlf_T hlID = (hlf_T)0;
2700 int filler_lines;
2701 int col;
2702
2703 if (lnum < 0) /* ignore type error in {lnum} arg */
2704 lnum = 0;
2705 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002706 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002707 || fnum != curbuf->b_fnum)
2708 {
2709 /* New line, buffer, change: need to get the values. */
2710 filler_lines = diff_check(curwin, lnum);
2711 if (filler_lines < 0)
2712 {
2713 if (filler_lines == -1)
2714 {
2715 change_start = MAXCOL;
2716 change_end = -1;
2717 if (diff_find_change(curwin, lnum, &change_start, &change_end))
2718 hlID = HLF_ADD; /* added line */
2719 else
2720 hlID = HLF_CHD; /* changed line */
2721 }
2722 else
2723 hlID = HLF_ADD; /* added line */
2724 }
2725 else
2726 hlID = (hlf_T)0;
2727 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002728 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002729 fnum = curbuf->b_fnum;
2730 }
2731
2732 if (hlID == HLF_CHD || hlID == HLF_TXD)
2733 {
2734 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
2735 if (col >= change_start && col <= change_end)
2736 hlID = HLF_TXD; /* changed text */
2737 else
2738 hlID = HLF_CHD; /* changed line */
2739 }
2740 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
2741#endif
2742}
2743
2744/*
2745 * "empty({expr})" function
2746 */
2747 static void
2748f_empty(typval_T *argvars, typval_T *rettv)
2749{
2750 int n = FALSE;
2751
2752 switch (argvars[0].v_type)
2753 {
2754 case VAR_STRING:
2755 case VAR_FUNC:
2756 n = argvars[0].vval.v_string == NULL
2757 || *argvars[0].vval.v_string == NUL;
2758 break;
2759 case VAR_PARTIAL:
2760 n = FALSE;
2761 break;
2762 case VAR_NUMBER:
2763 n = argvars[0].vval.v_number == 0;
2764 break;
2765 case VAR_FLOAT:
2766#ifdef FEAT_FLOAT
2767 n = argvars[0].vval.v_float == 0.0;
2768 break;
2769#endif
2770 case VAR_LIST:
2771 n = argvars[0].vval.v_list == NULL
2772 || argvars[0].vval.v_list->lv_first == NULL;
2773 break;
2774 case VAR_DICT:
2775 n = argvars[0].vval.v_dict == NULL
2776 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2777 break;
2778 case VAR_SPECIAL:
2779 n = argvars[0].vval.v_number != VVAL_TRUE;
2780 break;
2781
2782 case VAR_JOB:
2783#ifdef FEAT_JOB_CHANNEL
2784 n = argvars[0].vval.v_job == NULL
2785 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2786 break;
2787#endif
2788 case VAR_CHANNEL:
2789#ifdef FEAT_JOB_CHANNEL
2790 n = argvars[0].vval.v_channel == NULL
2791 || !channel_is_open(argvars[0].vval.v_channel);
2792 break;
2793#endif
2794 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01002795 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002796 n = TRUE;
2797 break;
2798 }
2799
2800 rettv->vval.v_number = n;
2801}
2802
2803/*
2804 * "escape({string}, {chars})" function
2805 */
2806 static void
2807f_escape(typval_T *argvars, typval_T *rettv)
2808{
2809 char_u buf[NUMBUFLEN];
2810
2811 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
2812 get_tv_string_buf(&argvars[1], buf));
2813 rettv->v_type = VAR_STRING;
2814}
2815
2816/*
2817 * "eval()" function
2818 */
2819 static void
2820f_eval(typval_T *argvars, typval_T *rettv)
2821{
2822 char_u *s, *p;
2823
2824 s = get_tv_string_chk(&argvars[0]);
2825 if (s != NULL)
2826 s = skipwhite(s);
2827
2828 p = s;
2829 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
2830 {
2831 if (p != NULL && !aborting())
2832 EMSG2(_(e_invexpr2), p);
2833 need_clr_eos = FALSE;
2834 rettv->v_type = VAR_NUMBER;
2835 rettv->vval.v_number = 0;
2836 }
2837 else if (*s != NUL)
2838 EMSG(_(e_trailing));
2839}
2840
2841/*
2842 * "eventhandler()" function
2843 */
2844 static void
2845f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
2846{
2847 rettv->vval.v_number = vgetc_busy;
2848}
2849
2850/*
2851 * "executable()" function
2852 */
2853 static void
2854f_executable(typval_T *argvars, typval_T *rettv)
2855{
2856 char_u *name = get_tv_string(&argvars[0]);
2857
2858 /* Check in $PATH and also check directly if there is a directory name. */
2859 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
2860 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
2861}
2862
2863static garray_T redir_execute_ga;
2864
2865/*
2866 * Append "value[value_len]" to the execute() output.
2867 */
2868 void
2869execute_redir_str(char_u *value, int value_len)
2870{
2871 int len;
2872
2873 if (value_len == -1)
2874 len = (int)STRLEN(value); /* Append the entire string */
2875 else
2876 len = value_len; /* Append only "value_len" characters */
2877 if (ga_grow(&redir_execute_ga, len) == OK)
2878 {
2879 mch_memmove((char *)redir_execute_ga.ga_data
2880 + redir_execute_ga.ga_len, value, len);
2881 redir_execute_ga.ga_len += len;
2882 }
2883}
2884
2885/*
2886 * Get next line from a list.
2887 * Called by do_cmdline() to get the next line.
2888 * Returns allocated string, or NULL for end of function.
2889 */
2890
2891 static char_u *
2892get_list_line(
2893 int c UNUSED,
2894 void *cookie,
2895 int indent UNUSED)
2896{
2897 listitem_T **p = (listitem_T **)cookie;
2898 listitem_T *item = *p;
2899 char_u buf[NUMBUFLEN];
2900 char_u *s;
2901
2902 if (item == NULL)
2903 return NULL;
2904 s = get_tv_string_buf_chk(&item->li_tv, buf);
2905 *p = item->li_next;
2906 return s == NULL ? NULL : vim_strsave(s);
2907}
2908
2909/*
2910 * "execute()" function
2911 */
2912 static void
2913f_execute(typval_T *argvars, typval_T *rettv)
2914{
2915 char_u *cmd = NULL;
2916 list_T *list = NULL;
2917 int save_msg_silent = msg_silent;
2918 int save_emsg_silent = emsg_silent;
2919 int save_emsg_noredir = emsg_noredir;
2920 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01002921 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002922 garray_T save_ga;
2923
2924 rettv->vval.v_string = NULL;
2925 rettv->v_type = VAR_STRING;
2926
2927 if (argvars[0].v_type == VAR_LIST)
2928 {
2929 list = argvars[0].vval.v_list;
2930 if (list == NULL || list->lv_first == NULL)
2931 /* empty list, no commands, empty output */
2932 return;
2933 ++list->lv_refcount;
2934 }
2935 else
2936 {
2937 cmd = get_tv_string_chk(&argvars[0]);
2938 if (cmd == NULL)
2939 return;
2940 }
2941
2942 if (argvars[1].v_type != VAR_UNKNOWN)
2943 {
2944 char_u buf[NUMBUFLEN];
2945 char_u *s = get_tv_string_buf_chk(&argvars[1], buf);
2946
2947 if (s == NULL)
2948 return;
2949 if (STRNCMP(s, "silent", 6) == 0)
2950 ++msg_silent;
2951 if (STRCMP(s, "silent!") == 0)
2952 {
2953 emsg_silent = TRUE;
2954 emsg_noredir = TRUE;
2955 }
2956 }
2957 else
2958 ++msg_silent;
2959
2960 if (redir_execute)
2961 save_ga = redir_execute_ga;
2962 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
2963 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01002964 redir_off = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002965
2966 if (cmd != NULL)
2967 do_cmdline_cmd(cmd);
2968 else
2969 {
2970 listitem_T *item = list->lv_first;
2971
2972 do_cmdline(NULL, get_list_line, (void *)&item,
2973 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
2974 --list->lv_refcount;
2975 }
2976
Bram Moolenaard297f352017-01-29 20:31:21 +01002977 /* Need to append a NUL to the result. */
2978 if (ga_grow(&redir_execute_ga, 1) == OK)
2979 {
2980 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
2981 rettv->vval.v_string = redir_execute_ga.ga_data;
2982 }
2983 else
2984 {
2985 ga_clear(&redir_execute_ga);
2986 rettv->vval.v_string = NULL;
2987 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002988 msg_silent = save_msg_silent;
2989 emsg_silent = save_emsg_silent;
2990 emsg_noredir = save_emsg_noredir;
2991
2992 redir_execute = save_redir_execute;
2993 if (redir_execute)
2994 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01002995 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002996
2997 /* "silent reg" or "silent echo x" leaves msg_col somewhere in the
2998 * line. Put it back in the first column. */
2999 msg_col = 0;
3000}
3001
3002/*
3003 * "exepath()" function
3004 */
3005 static void
3006f_exepath(typval_T *argvars, typval_T *rettv)
3007{
3008 char_u *p = NULL;
3009
3010 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
3011 rettv->v_type = VAR_STRING;
3012 rettv->vval.v_string = p;
3013}
3014
3015/*
3016 * "exists()" function
3017 */
3018 static void
3019f_exists(typval_T *argvars, typval_T *rettv)
3020{
3021 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003022 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003023
3024 p = get_tv_string(&argvars[0]);
3025 if (*p == '$') /* environment variable */
3026 {
3027 /* first try "normal" environment variables (fast) */
3028 if (mch_getenv(p + 1) != NULL)
3029 n = TRUE;
3030 else
3031 {
3032 /* try expanding things like $VIM and ${HOME} */
3033 p = expand_env_save(p);
3034 if (p != NULL && *p != '$')
3035 n = TRUE;
3036 vim_free(p);
3037 }
3038 }
3039 else if (*p == '&' || *p == '+') /* option */
3040 {
3041 n = (get_option_tv(&p, NULL, TRUE) == OK);
3042 if (*skipwhite(p) != NUL)
3043 n = FALSE; /* trailing garbage */
3044 }
3045 else if (*p == '*') /* internal or user defined function */
3046 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003047 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003048 }
3049 else if (*p == ':')
3050 {
3051 n = cmd_exists(p + 1);
3052 }
3053 else if (*p == '#')
3054 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003055 if (p[1] == '#')
3056 n = autocmd_supported(p + 2);
3057 else
3058 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003059 }
3060 else /* internal variable */
3061 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003062 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003063 }
3064
3065 rettv->vval.v_number = n;
3066}
3067
3068#ifdef FEAT_FLOAT
3069/*
3070 * "exp()" function
3071 */
3072 static void
3073f_exp(typval_T *argvars, typval_T *rettv)
3074{
3075 float_T f = 0.0;
3076
3077 rettv->v_type = VAR_FLOAT;
3078 if (get_float_arg(argvars, &f) == OK)
3079 rettv->vval.v_float = exp(f);
3080 else
3081 rettv->vval.v_float = 0.0;
3082}
3083#endif
3084
3085/*
3086 * "expand()" function
3087 */
3088 static void
3089f_expand(typval_T *argvars, typval_T *rettv)
3090{
3091 char_u *s;
3092 int len;
3093 char_u *errormsg;
3094 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3095 expand_T xpc;
3096 int error = FALSE;
3097 char_u *result;
3098
3099 rettv->v_type = VAR_STRING;
3100 if (argvars[1].v_type != VAR_UNKNOWN
3101 && argvars[2].v_type != VAR_UNKNOWN
3102 && get_tv_number_chk(&argvars[2], &error)
3103 && !error)
3104 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003105 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003106 }
3107
3108 s = get_tv_string(&argvars[0]);
3109 if (*s == '%' || *s == '#' || *s == '<')
3110 {
3111 ++emsg_off;
3112 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3113 --emsg_off;
3114 if (rettv->v_type == VAR_LIST)
3115 {
3116 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3117 list_append_string(rettv->vval.v_list, result, -1);
3118 else
3119 vim_free(result);
3120 }
3121 else
3122 rettv->vval.v_string = result;
3123 }
3124 else
3125 {
3126 /* When the optional second argument is non-zero, don't remove matches
3127 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3128 if (argvars[1].v_type != VAR_UNKNOWN
3129 && get_tv_number_chk(&argvars[1], &error))
3130 options |= WILD_KEEP_ALL;
3131 if (!error)
3132 {
3133 ExpandInit(&xpc);
3134 xpc.xp_context = EXPAND_FILES;
3135 if (p_wic)
3136 options += WILD_ICASE;
3137 if (rettv->v_type == VAR_STRING)
3138 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3139 options, WILD_ALL);
3140 else if (rettv_list_alloc(rettv) != FAIL)
3141 {
3142 int i;
3143
3144 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3145 for (i = 0; i < xpc.xp_numfiles; i++)
3146 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3147 ExpandCleanup(&xpc);
3148 }
3149 }
3150 else
3151 rettv->vval.v_string = NULL;
3152 }
3153}
3154
3155/*
3156 * "extend(list, list [, idx])" function
3157 * "extend(dict, dict [, action])" function
3158 */
3159 static void
3160f_extend(typval_T *argvars, typval_T *rettv)
3161{
3162 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3163
3164 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3165 {
3166 list_T *l1, *l2;
3167 listitem_T *item;
3168 long before;
3169 int error = FALSE;
3170
3171 l1 = argvars[0].vval.v_list;
3172 l2 = argvars[1].vval.v_list;
3173 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
3174 && l2 != NULL)
3175 {
3176 if (argvars[2].v_type != VAR_UNKNOWN)
3177 {
3178 before = (long)get_tv_number_chk(&argvars[2], &error);
3179 if (error)
3180 return; /* type error; errmsg already given */
3181
3182 if (before == l1->lv_len)
3183 item = NULL;
3184 else
3185 {
3186 item = list_find(l1, before);
3187 if (item == NULL)
3188 {
3189 EMSGN(_(e_listidx), before);
3190 return;
3191 }
3192 }
3193 }
3194 else
3195 item = NULL;
3196 list_extend(l1, l2, item);
3197
3198 copy_tv(&argvars[0], rettv);
3199 }
3200 }
3201 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3202 {
3203 dict_T *d1, *d2;
3204 char_u *action;
3205 int i;
3206
3207 d1 = argvars[0].vval.v_dict;
3208 d2 = argvars[1].vval.v_dict;
3209 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
3210 && d2 != NULL)
3211 {
3212 /* Check the third argument. */
3213 if (argvars[2].v_type != VAR_UNKNOWN)
3214 {
3215 static char *(av[]) = {"keep", "force", "error"};
3216
3217 action = get_tv_string_chk(&argvars[2]);
3218 if (action == NULL)
3219 return; /* type error; errmsg already given */
3220 for (i = 0; i < 3; ++i)
3221 if (STRCMP(action, av[i]) == 0)
3222 break;
3223 if (i == 3)
3224 {
3225 EMSG2(_(e_invarg2), action);
3226 return;
3227 }
3228 }
3229 else
3230 action = (char_u *)"force";
3231
3232 dict_extend(d1, d2, action);
3233
3234 copy_tv(&argvars[0], rettv);
3235 }
3236 }
3237 else
3238 EMSG2(_(e_listdictarg), "extend()");
3239}
3240
3241/*
3242 * "feedkeys()" function
3243 */
3244 static void
3245f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3246{
3247 int remap = TRUE;
3248 int insert = FALSE;
3249 char_u *keys, *flags;
3250 char_u nbuf[NUMBUFLEN];
3251 int typed = FALSE;
3252 int execute = FALSE;
3253 int dangerous = FALSE;
3254 char_u *keys_esc;
3255
3256 /* This is not allowed in the sandbox. If the commands would still be
3257 * executed in the sandbox it would be OK, but it probably happens later,
3258 * when "sandbox" is no longer set. */
3259 if (check_secure())
3260 return;
3261
3262 keys = get_tv_string(&argvars[0]);
3263
3264 if (argvars[1].v_type != VAR_UNKNOWN)
3265 {
3266 flags = get_tv_string_buf(&argvars[1], nbuf);
3267 for ( ; *flags != NUL; ++flags)
3268 {
3269 switch (*flags)
3270 {
3271 case 'n': remap = FALSE; break;
3272 case 'm': remap = TRUE; break;
3273 case 't': typed = TRUE; break;
3274 case 'i': insert = TRUE; break;
3275 case 'x': execute = TRUE; break;
3276 case '!': dangerous = TRUE; break;
3277 }
3278 }
3279 }
3280
3281 if (*keys != NUL || execute)
3282 {
3283 /* Need to escape K_SPECIAL and CSI before putting the string in the
3284 * typeahead buffer. */
3285 keys_esc = vim_strsave_escape_csi(keys);
3286 if (keys_esc != NULL)
3287 {
3288 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
3289 insert ? 0 : typebuf.tb_len, !typed, FALSE);
3290 vim_free(keys_esc);
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003291 if (vgetc_busy
3292#ifdef FEAT_TIMERS
3293 || timer_busy
3294#endif
3295 )
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003296 typebuf_was_filled = TRUE;
3297 if (execute)
3298 {
3299 int save_msg_scroll = msg_scroll;
3300
3301 /* Avoid a 1 second delay when the keys start Insert mode. */
3302 msg_scroll = FALSE;
3303
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02003304#ifdef FEAT_TERMINAL
3305 if (term_use_loop())
3306 terminal_loop(FALSE);
3307 else
3308#endif
3309 {
3310 if (!dangerous)
3311 ++ex_normal_busy;
3312 exec_normal(TRUE);
3313 if (!dangerous)
3314 --ex_normal_busy;
3315 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003316 msg_scroll |= save_msg_scroll;
3317 }
3318 }
3319 }
3320}
3321
3322/*
3323 * "filereadable()" function
3324 */
3325 static void
3326f_filereadable(typval_T *argvars, typval_T *rettv)
3327{
3328 int fd;
3329 char_u *p;
3330 int n;
3331
3332#ifndef O_NONBLOCK
3333# define O_NONBLOCK 0
3334#endif
3335 p = get_tv_string(&argvars[0]);
3336 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3337 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3338 {
3339 n = TRUE;
3340 close(fd);
3341 }
3342 else
3343 n = FALSE;
3344
3345 rettv->vval.v_number = n;
3346}
3347
3348/*
3349 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3350 * rights to write into.
3351 */
3352 static void
3353f_filewritable(typval_T *argvars, typval_T *rettv)
3354{
3355 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
3356}
3357
3358 static void
3359findfilendir(
3360 typval_T *argvars UNUSED,
3361 typval_T *rettv,
3362 int find_what UNUSED)
3363{
3364#ifdef FEAT_SEARCHPATH
3365 char_u *fname;
3366 char_u *fresult = NULL;
3367 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3368 char_u *p;
3369 char_u pathbuf[NUMBUFLEN];
3370 int count = 1;
3371 int first = TRUE;
3372 int error = FALSE;
3373#endif
3374
3375 rettv->vval.v_string = NULL;
3376 rettv->v_type = VAR_STRING;
3377
3378#ifdef FEAT_SEARCHPATH
3379 fname = get_tv_string(&argvars[0]);
3380
3381 if (argvars[1].v_type != VAR_UNKNOWN)
3382 {
3383 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
3384 if (p == NULL)
3385 error = TRUE;
3386 else
3387 {
3388 if (*p != NUL)
3389 path = p;
3390
3391 if (argvars[2].v_type != VAR_UNKNOWN)
3392 count = (int)get_tv_number_chk(&argvars[2], &error);
3393 }
3394 }
3395
3396 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3397 error = TRUE;
3398
3399 if (*fname != NUL && !error)
3400 {
3401 do
3402 {
3403 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3404 vim_free(fresult);
3405 fresult = find_file_in_path_option(first ? fname : NULL,
3406 first ? (int)STRLEN(fname) : 0,
3407 0, first, path,
3408 find_what,
3409 curbuf->b_ffname,
3410 find_what == FINDFILE_DIR
3411 ? (char_u *)"" : curbuf->b_p_sua);
3412 first = FALSE;
3413
3414 if (fresult != NULL && rettv->v_type == VAR_LIST)
3415 list_append_string(rettv->vval.v_list, fresult, -1);
3416
3417 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3418 }
3419
3420 if (rettv->v_type == VAR_STRING)
3421 rettv->vval.v_string = fresult;
3422#endif
3423}
3424
3425/*
3426 * "filter()" function
3427 */
3428 static void
3429f_filter(typval_T *argvars, typval_T *rettv)
3430{
3431 filter_map(argvars, rettv, FALSE);
3432}
3433
3434/*
3435 * "finddir({fname}[, {path}[, {count}]])" function
3436 */
3437 static void
3438f_finddir(typval_T *argvars, typval_T *rettv)
3439{
3440 findfilendir(argvars, rettv, FINDFILE_DIR);
3441}
3442
3443/*
3444 * "findfile({fname}[, {path}[, {count}]])" function
3445 */
3446 static void
3447f_findfile(typval_T *argvars, typval_T *rettv)
3448{
3449 findfilendir(argvars, rettv, FINDFILE_FILE);
3450}
3451
3452#ifdef FEAT_FLOAT
3453/*
3454 * "float2nr({float})" function
3455 */
3456 static void
3457f_float2nr(typval_T *argvars, typval_T *rettv)
3458{
3459 float_T f = 0.0;
3460
3461 if (get_float_arg(argvars, &f) == OK)
3462 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003463 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003464 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003465 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003466 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003467 else
3468 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003469 }
3470}
3471
3472/*
3473 * "floor({float})" function
3474 */
3475 static void
3476f_floor(typval_T *argvars, typval_T *rettv)
3477{
3478 float_T f = 0.0;
3479
3480 rettv->v_type = VAR_FLOAT;
3481 if (get_float_arg(argvars, &f) == OK)
3482 rettv->vval.v_float = floor(f);
3483 else
3484 rettv->vval.v_float = 0.0;
3485}
3486
3487/*
3488 * "fmod()" function
3489 */
3490 static void
3491f_fmod(typval_T *argvars, typval_T *rettv)
3492{
3493 float_T fx = 0.0, fy = 0.0;
3494
3495 rettv->v_type = VAR_FLOAT;
3496 if (get_float_arg(argvars, &fx) == OK
3497 && get_float_arg(&argvars[1], &fy) == OK)
3498 rettv->vval.v_float = fmod(fx, fy);
3499 else
3500 rettv->vval.v_float = 0.0;
3501}
3502#endif
3503
3504/*
3505 * "fnameescape({string})" function
3506 */
3507 static void
3508f_fnameescape(typval_T *argvars, typval_T *rettv)
3509{
3510 rettv->vval.v_string = vim_strsave_fnameescape(
3511 get_tv_string(&argvars[0]), FALSE);
3512 rettv->v_type = VAR_STRING;
3513}
3514
3515/*
3516 * "fnamemodify({fname}, {mods})" function
3517 */
3518 static void
3519f_fnamemodify(typval_T *argvars, typval_T *rettv)
3520{
3521 char_u *fname;
3522 char_u *mods;
3523 int usedlen = 0;
3524 int len;
3525 char_u *fbuf = NULL;
3526 char_u buf[NUMBUFLEN];
3527
3528 fname = get_tv_string_chk(&argvars[0]);
3529 mods = get_tv_string_buf_chk(&argvars[1], buf);
3530 if (fname == NULL || mods == NULL)
3531 fname = NULL;
3532 else
3533 {
3534 len = (int)STRLEN(fname);
3535 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
3536 }
3537
3538 rettv->v_type = VAR_STRING;
3539 if (fname == NULL)
3540 rettv->vval.v_string = NULL;
3541 else
3542 rettv->vval.v_string = vim_strnsave(fname, len);
3543 vim_free(fbuf);
3544}
3545
3546static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
3547
3548/*
3549 * "foldclosed()" function
3550 */
3551 static void
3552foldclosed_both(
3553 typval_T *argvars UNUSED,
3554 typval_T *rettv,
3555 int end UNUSED)
3556{
3557#ifdef FEAT_FOLDING
3558 linenr_T lnum;
3559 linenr_T first, last;
3560
3561 lnum = get_tv_lnum(argvars);
3562 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3563 {
3564 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3565 {
3566 if (end)
3567 rettv->vval.v_number = (varnumber_T)last;
3568 else
3569 rettv->vval.v_number = (varnumber_T)first;
3570 return;
3571 }
3572 }
3573#endif
3574 rettv->vval.v_number = -1;
3575}
3576
3577/*
3578 * "foldclosed()" function
3579 */
3580 static void
3581f_foldclosed(typval_T *argvars, typval_T *rettv)
3582{
3583 foldclosed_both(argvars, rettv, FALSE);
3584}
3585
3586/*
3587 * "foldclosedend()" function
3588 */
3589 static void
3590f_foldclosedend(typval_T *argvars, typval_T *rettv)
3591{
3592 foldclosed_both(argvars, rettv, TRUE);
3593}
3594
3595/*
3596 * "foldlevel()" function
3597 */
3598 static void
3599f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3600{
3601#ifdef FEAT_FOLDING
3602 linenr_T lnum;
3603
3604 lnum = get_tv_lnum(argvars);
3605 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3606 rettv->vval.v_number = foldLevel(lnum);
3607#endif
3608}
3609
3610/*
3611 * "foldtext()" function
3612 */
3613 static void
3614f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
3615{
3616#ifdef FEAT_FOLDING
3617 linenr_T foldstart;
3618 linenr_T foldend;
3619 char_u *dashes;
3620 linenr_T lnum;
3621 char_u *s;
3622 char_u *r;
3623 int len;
3624 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003625 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003626#endif
3627
3628 rettv->v_type = VAR_STRING;
3629 rettv->vval.v_string = NULL;
3630#ifdef FEAT_FOLDING
3631 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
3632 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
3633 dashes = get_vim_var_str(VV_FOLDDASHES);
3634 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
3635 && dashes != NULL)
3636 {
3637 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02003638 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003639 if (!linewhite(lnum))
3640 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003641
3642 /* Find interesting text in this line. */
3643 s = skipwhite(ml_get(lnum));
3644 /* skip C comment-start */
3645 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
3646 {
3647 s = skipwhite(s + 2);
3648 if (*skipwhite(s) == NUL
3649 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
3650 {
3651 s = skipwhite(ml_get(lnum + 1));
3652 if (*s == '*')
3653 s = skipwhite(s + 1);
3654 }
3655 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02003656 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003657 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003658 r = alloc((unsigned)(STRLEN(txt)
3659 + STRLEN(dashes) /* for %s */
3660 + 20 /* for %3ld */
3661 + STRLEN(s))); /* concatenated */
3662 if (r != NULL)
3663 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02003664 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003665 len = (int)STRLEN(r);
3666 STRCAT(r, s);
3667 /* remove 'foldmarker' and 'commentstring' */
3668 foldtext_cleanup(r + len);
3669 rettv->vval.v_string = r;
3670 }
3671 }
3672#endif
3673}
3674
3675/*
3676 * "foldtextresult(lnum)" function
3677 */
3678 static void
3679f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
3680{
3681#ifdef FEAT_FOLDING
3682 linenr_T lnum;
3683 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003684 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003685 foldinfo_T foldinfo;
3686 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003687 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003688#endif
3689
3690 rettv->v_type = VAR_STRING;
3691 rettv->vval.v_string = NULL;
3692#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003693 if (entered)
3694 return; /* reject recursive use */
3695 entered = TRUE;
3696
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003697 lnum = get_tv_lnum(argvars);
3698 /* treat illegal types and illegal string values for {lnum} the same */
3699 if (lnum < 0)
3700 lnum = 0;
3701 fold_count = foldedCount(curwin, lnum, &foldinfo);
3702 if (fold_count > 0)
3703 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01003704 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
3705 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003706 if (text == buf)
3707 text = vim_strsave(text);
3708 rettv->vval.v_string = text;
3709 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003710
3711 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003712#endif
3713}
3714
3715/*
3716 * "foreground()" function
3717 */
3718 static void
3719f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3720{
3721#ifdef FEAT_GUI
3722 if (gui.in_use)
3723 gui_mch_set_foreground();
3724#else
3725# ifdef WIN32
3726 win32_set_foreground();
3727# endif
3728#endif
3729}
3730
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003731 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003732common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003733{
3734 char_u *s;
3735 char_u *name;
3736 int use_string = FALSE;
3737 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003738 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003739
3740 if (argvars[0].v_type == VAR_FUNC)
3741 {
3742 /* function(MyFunc, [arg], dict) */
3743 s = argvars[0].vval.v_string;
3744 }
3745 else if (argvars[0].v_type == VAR_PARTIAL
3746 && argvars[0].vval.v_partial != NULL)
3747 {
3748 /* function(dict.MyFunc, [arg]) */
3749 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003750 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003751 }
3752 else
3753 {
3754 /* function('MyFunc', [arg], dict) */
3755 s = get_tv_string(&argvars[0]);
3756 use_string = TRUE;
3757 }
3758
Bram Moolenaar843b8842016-08-21 14:36:15 +02003759 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003760 {
3761 name = s;
3762 trans_name = trans_function_name(&name, FALSE,
3763 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
3764 if (*name != NUL)
3765 s = NULL;
3766 }
3767
Bram Moolenaar843b8842016-08-21 14:36:15 +02003768 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
3769 || (is_funcref && trans_name == NULL))
Bram Moolenaar5582ef12016-09-14 22:16:13 +02003770 EMSG2(_(e_invarg2), use_string ? get_tv_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003771 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003772 else if (trans_name != NULL && (is_funcref
3773 ? find_func(trans_name) == NULL
3774 : !translated_function_exists(trans_name)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003775 EMSG2(_("E700: Unknown function: %s"), s);
3776 else
3777 {
3778 int dict_idx = 0;
3779 int arg_idx = 0;
3780 list_T *list = NULL;
3781
3782 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
3783 {
3784 char sid_buf[25];
3785 int off = *s == 's' ? 2 : 5;
3786
3787 /* Expand s: and <SID> into <SNR>nr_, so that the function can
3788 * also be called from another script. Using trans_function_name()
3789 * would also work, but some plugins depend on the name being
3790 * printable text. */
3791 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
3792 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
3793 if (name != NULL)
3794 {
3795 STRCPY(name, sid_buf);
3796 STRCAT(name, s + off);
3797 }
3798 }
3799 else
3800 name = vim_strsave(s);
3801
3802 if (argvars[1].v_type != VAR_UNKNOWN)
3803 {
3804 if (argvars[2].v_type != VAR_UNKNOWN)
3805 {
3806 /* function(name, [args], dict) */
3807 arg_idx = 1;
3808 dict_idx = 2;
3809 }
3810 else if (argvars[1].v_type == VAR_DICT)
3811 /* function(name, dict) */
3812 dict_idx = 1;
3813 else
3814 /* function(name, [args]) */
3815 arg_idx = 1;
3816 if (dict_idx > 0)
3817 {
3818 if (argvars[dict_idx].v_type != VAR_DICT)
3819 {
3820 EMSG(_("E922: expected a dict"));
3821 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003822 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003823 }
3824 if (argvars[dict_idx].vval.v_dict == NULL)
3825 dict_idx = 0;
3826 }
3827 if (arg_idx > 0)
3828 {
3829 if (argvars[arg_idx].v_type != VAR_LIST)
3830 {
3831 EMSG(_("E923: Second argument of function() must be a list or a dict"));
3832 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003833 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003834 }
3835 list = argvars[arg_idx].vval.v_list;
3836 if (list == NULL || list->lv_len == 0)
3837 arg_idx = 0;
3838 }
3839 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003840 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003841 {
3842 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
3843
3844 /* result is a VAR_PARTIAL */
3845 if (pt == NULL)
3846 vim_free(name);
3847 else
3848 {
3849 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
3850 {
3851 listitem_T *li;
3852 int i = 0;
3853 int arg_len = 0;
3854 int lv_len = 0;
3855
3856 if (arg_pt != NULL)
3857 arg_len = arg_pt->pt_argc;
3858 if (list != NULL)
3859 lv_len = list->lv_len;
3860 pt->pt_argc = arg_len + lv_len;
3861 pt->pt_argv = (typval_T *)alloc(
3862 sizeof(typval_T) * pt->pt_argc);
3863 if (pt->pt_argv == NULL)
3864 {
3865 vim_free(pt);
3866 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003867 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003868 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003869 for (i = 0; i < arg_len; i++)
3870 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
3871 if (lv_len > 0)
3872 for (li = list->lv_first; li != NULL;
3873 li = li->li_next)
3874 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003875 }
3876
3877 /* For "function(dict.func, [], dict)" and "func" is a partial
3878 * use "dict". That is backwards compatible. */
3879 if (dict_idx > 0)
3880 {
3881 /* The dict is bound explicitly, pt_auto is FALSE. */
3882 pt->pt_dict = argvars[dict_idx].vval.v_dict;
3883 ++pt->pt_dict->dv_refcount;
3884 }
3885 else if (arg_pt != NULL)
3886 {
3887 /* If the dict was bound automatically the result is also
3888 * bound automatically. */
3889 pt->pt_dict = arg_pt->pt_dict;
3890 pt->pt_auto = arg_pt->pt_auto;
3891 if (pt->pt_dict != NULL)
3892 ++pt->pt_dict->dv_refcount;
3893 }
3894
3895 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003896 if (arg_pt != NULL && arg_pt->pt_func != NULL)
3897 {
3898 pt->pt_func = arg_pt->pt_func;
3899 func_ptr_ref(pt->pt_func);
3900 vim_free(name);
3901 }
3902 else if (is_funcref)
3903 {
3904 pt->pt_func = find_func(trans_name);
3905 func_ptr_ref(pt->pt_func);
3906 vim_free(name);
3907 }
3908 else
3909 {
3910 pt->pt_name = name;
3911 func_ref(name);
3912 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003913 }
3914 rettv->v_type = VAR_PARTIAL;
3915 rettv->vval.v_partial = pt;
3916 }
3917 else
3918 {
3919 /* result is a VAR_FUNC */
3920 rettv->v_type = VAR_FUNC;
3921 rettv->vval.v_string = name;
3922 func_ref(name);
3923 }
3924 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003925theend:
3926 vim_free(trans_name);
3927}
3928
3929/*
3930 * "funcref()" function
3931 */
3932 static void
3933f_funcref(typval_T *argvars, typval_T *rettv)
3934{
3935 common_function(argvars, rettv, TRUE);
3936}
3937
3938/*
3939 * "function()" function
3940 */
3941 static void
3942f_function(typval_T *argvars, typval_T *rettv)
3943{
3944 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003945}
3946
3947/*
3948 * "garbagecollect()" function
3949 */
3950 static void
3951f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
3952{
3953 /* This is postponed until we are back at the toplevel, because we may be
3954 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
3955 want_garbage_collect = TRUE;
3956
3957 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
3958 garbage_collect_at_exit = TRUE;
3959}
3960
3961/*
3962 * "get()" function
3963 */
3964 static void
3965f_get(typval_T *argvars, typval_T *rettv)
3966{
3967 listitem_T *li;
3968 list_T *l;
3969 dictitem_T *di;
3970 dict_T *d;
3971 typval_T *tv = NULL;
3972
3973 if (argvars[0].v_type == VAR_LIST)
3974 {
3975 if ((l = argvars[0].vval.v_list) != NULL)
3976 {
3977 int error = FALSE;
3978
3979 li = list_find(l, (long)get_tv_number_chk(&argvars[1], &error));
3980 if (!error && li != NULL)
3981 tv = &li->li_tv;
3982 }
3983 }
3984 else if (argvars[0].v_type == VAR_DICT)
3985 {
3986 if ((d = argvars[0].vval.v_dict) != NULL)
3987 {
3988 di = dict_find(d, get_tv_string(&argvars[1]), -1);
3989 if (di != NULL)
3990 tv = &di->di_tv;
3991 }
3992 }
3993 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
3994 {
3995 partial_T *pt;
3996 partial_T fref_pt;
3997
3998 if (argvars[0].v_type == VAR_PARTIAL)
3999 pt = argvars[0].vval.v_partial;
4000 else
4001 {
4002 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4003 fref_pt.pt_name = argvars[0].vval.v_string;
4004 pt = &fref_pt;
4005 }
4006
4007 if (pt != NULL)
4008 {
4009 char_u *what = get_tv_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004010 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004011
4012 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4013 {
4014 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004015 n = partial_name(pt);
4016 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004017 rettv->vval.v_string = NULL;
4018 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004019 {
4020 rettv->vval.v_string = vim_strsave(n);
4021 if (rettv->v_type == VAR_FUNC)
4022 func_ref(rettv->vval.v_string);
4023 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004024 }
4025 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004026 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004027 else if (STRCMP(what, "args") == 0)
4028 {
4029 rettv->v_type = VAR_LIST;
4030 if (rettv_list_alloc(rettv) == OK)
4031 {
4032 int i;
4033
4034 for (i = 0; i < pt->pt_argc; ++i)
4035 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4036 }
4037 }
4038 else
4039 EMSG2(_(e_invarg2), what);
4040 return;
4041 }
4042 }
4043 else
4044 EMSG2(_(e_listdictarg), "get()");
4045
4046 if (tv == NULL)
4047 {
4048 if (argvars[2].v_type != VAR_UNKNOWN)
4049 copy_tv(&argvars[2], rettv);
4050 }
4051 else
4052 copy_tv(tv, rettv);
4053}
4054
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004055#ifdef FEAT_SIGNS
4056/*
4057 * Returns information about signs placed in a buffer as list of dicts.
4058 */
4059 static void
4060get_buffer_signs(buf_T *buf, list_T *l)
4061{
4062 signlist_T *sign;
4063
4064 for (sign = buf->b_signlist; sign; sign = sign->next)
4065 {
4066 dict_T *d = dict_alloc();
4067
4068 if (d != NULL)
4069 {
4070 dict_add_nr_str(d, "id", sign->id, NULL);
4071 dict_add_nr_str(d, "lnum", sign->lnum, NULL);
Bram Moolenaar6a402ed2016-08-28 14:11:24 +02004072 dict_add_nr_str(d, "name", 0L, sign_typenr2name(sign->typenr));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004073
4074 list_append_dict(l, d);
4075 }
4076 }
4077}
4078#endif
4079
4080/*
4081 * Returns buffer options, variables and other attributes in a dictionary.
4082 */
4083 static dict_T *
4084get_buffer_info(buf_T *buf)
4085{
4086 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004087 tabpage_T *tp;
4088 win_T *wp;
4089 list_T *windows;
4090
4091 dict = dict_alloc();
4092 if (dict == NULL)
4093 return NULL;
4094
Bram Moolenaar33928832016-08-18 21:22:04 +02004095 dict_add_nr_str(dict, "bufnr", buf->b_fnum, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004096 dict_add_nr_str(dict, "name", 0L,
4097 buf->b_ffname != NULL ? buf->b_ffname : (char_u *)"");
Bram Moolenaarf845b872017-01-06 14:04:54 +01004098 dict_add_nr_str(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4099 : buflist_findlnum(buf), NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004100 dict_add_nr_str(dict, "loaded", buf->b_ml.ml_mfp != NULL, NULL);
4101 dict_add_nr_str(dict, "listed", buf->b_p_bl, NULL);
4102 dict_add_nr_str(dict, "changed", bufIsChanged(buf), NULL);
Bram Moolenaar95c526e2017-02-25 14:59:34 +01004103 dict_add_nr_str(dict, "changedtick", CHANGEDTICK(buf), NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004104 dict_add_nr_str(dict, "hidden",
4105 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0,
4106 NULL);
4107
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004108 /* Get a reference to buffer variables */
4109 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004110
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004111 /* List of windows displaying this buffer */
4112 windows = list_alloc();
4113 if (windows != NULL)
4114 {
4115 FOR_ALL_TAB_WINDOWS(tp, wp)
4116 if (wp->w_buffer == buf)
4117 list_append_number(windows, (varnumber_T)wp->w_id);
4118 dict_add_list(dict, "windows", windows);
4119 }
4120
4121#ifdef FEAT_SIGNS
4122 if (buf->b_signlist != NULL)
4123 {
4124 /* List of signs placed in this buffer */
4125 list_T *signs = list_alloc();
4126 if (signs != NULL)
4127 {
4128 get_buffer_signs(buf, signs);
4129 dict_add_list(dict, "signs", signs);
4130 }
4131 }
4132#endif
4133
4134 return dict;
4135}
4136
4137/*
4138 * "getbufinfo()" function
4139 */
4140 static void
4141f_getbufinfo(typval_T *argvars, typval_T *rettv)
4142{
4143 buf_T *buf = NULL;
4144 buf_T *argbuf = NULL;
4145 dict_T *d;
4146 int filtered = FALSE;
4147 int sel_buflisted = FALSE;
4148 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004149 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004150
4151 if (rettv_list_alloc(rettv) != OK)
4152 return;
4153
4154 /* List of all the buffers or selected buffers */
4155 if (argvars[0].v_type == VAR_DICT)
4156 {
4157 dict_T *sel_d = argvars[0].vval.v_dict;
4158
4159 if (sel_d != NULL)
4160 {
4161 dictitem_T *di;
4162
4163 filtered = TRUE;
4164
4165 di = dict_find(sel_d, (char_u *)"buflisted", -1);
4166 if (di != NULL && get_tv_number(&di->di_tv))
4167 sel_buflisted = TRUE;
4168
4169 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
4170 if (di != NULL && get_tv_number(&di->di_tv))
4171 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004172
4173 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
4174 if (di != NULL && get_tv_number(&di->di_tv))
4175 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004176 }
4177 }
4178 else if (argvars[0].v_type != VAR_UNKNOWN)
4179 {
4180 /* Information about one buffer. Argument specifies the buffer */
4181 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4182 ++emsg_off;
4183 argbuf = get_buf_tv(&argvars[0], FALSE);
4184 --emsg_off;
4185 if (argbuf == NULL)
4186 return;
4187 }
4188
4189 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004190 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004191 {
4192 if (argbuf != NULL && argbuf != buf)
4193 continue;
4194 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004195 || (sel_buflisted && !buf->b_p_bl)
4196 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004197 continue;
4198
4199 d = get_buffer_info(buf);
4200 if (d != NULL)
4201 list_append_dict(rettv->vval.v_list, d);
4202 if (argbuf != NULL)
4203 return;
4204 }
4205}
4206
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004207static void get_buffer_lines(buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv);
4208
4209/*
4210 * Get line or list of lines from buffer "buf" into "rettv".
4211 * Return a range (from start to end) of lines in rettv from the specified
4212 * buffer.
4213 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4214 */
4215 static void
4216get_buffer_lines(
4217 buf_T *buf,
4218 linenr_T start,
4219 linenr_T end,
4220 int retlist,
4221 typval_T *rettv)
4222{
4223 char_u *p;
4224
4225 rettv->v_type = VAR_STRING;
4226 rettv->vval.v_string = NULL;
4227 if (retlist && rettv_list_alloc(rettv) == FAIL)
4228 return;
4229
4230 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4231 return;
4232
4233 if (!retlist)
4234 {
4235 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4236 p = ml_get_buf(buf, start, FALSE);
4237 else
4238 p = (char_u *)"";
4239 rettv->vval.v_string = vim_strsave(p);
4240 }
4241 else
4242 {
4243 if (end < start)
4244 return;
4245
4246 if (start < 1)
4247 start = 1;
4248 if (end > buf->b_ml.ml_line_count)
4249 end = buf->b_ml.ml_line_count;
4250 while (start <= end)
4251 if (list_append_string(rettv->vval.v_list,
4252 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4253 break;
4254 }
4255}
4256
4257/*
4258 * Get the lnum from the first argument.
4259 * Also accepts "$", then "buf" is used.
4260 * Returns 0 on error.
4261 */
4262 static linenr_T
4263get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
4264{
4265 if (argvars[0].v_type == VAR_STRING
4266 && argvars[0].vval.v_string != NULL
4267 && argvars[0].vval.v_string[0] == '$'
4268 && buf != NULL)
4269 return buf->b_ml.ml_line_count;
4270 return (linenr_T)get_tv_number_chk(&argvars[0], NULL);
4271}
4272
4273/*
4274 * "getbufline()" function
4275 */
4276 static void
4277f_getbufline(typval_T *argvars, typval_T *rettv)
4278{
4279 linenr_T lnum;
4280 linenr_T end;
4281 buf_T *buf;
4282
4283 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4284 ++emsg_off;
4285 buf = get_buf_tv(&argvars[0], FALSE);
4286 --emsg_off;
4287
4288 lnum = get_tv_lnum_buf(&argvars[1], buf);
4289 if (argvars[2].v_type == VAR_UNKNOWN)
4290 end = lnum;
4291 else
4292 end = get_tv_lnum_buf(&argvars[2], buf);
4293
4294 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4295}
4296
4297/*
4298 * "getbufvar()" function
4299 */
4300 static void
4301f_getbufvar(typval_T *argvars, typval_T *rettv)
4302{
4303 buf_T *buf;
4304 buf_T *save_curbuf;
4305 char_u *varname;
4306 dictitem_T *v;
4307 int done = FALSE;
4308
4309 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4310 varname = get_tv_string_chk(&argvars[1]);
4311 ++emsg_off;
4312 buf = get_buf_tv(&argvars[0], FALSE);
4313
4314 rettv->v_type = VAR_STRING;
4315 rettv->vval.v_string = NULL;
4316
4317 if (buf != NULL && varname != NULL)
4318 {
4319 /* set curbuf to be our buf, temporarily */
4320 save_curbuf = curbuf;
4321 curbuf = buf;
4322
Bram Moolenaar30567352016-08-27 21:25:44 +02004323 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004324 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004325 if (varname[1] == NUL)
4326 {
4327 /* get all buffer-local options in a dict */
4328 dict_T *opts = get_winbuf_options(TRUE);
4329
4330 if (opts != NULL)
4331 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004332 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004333 done = TRUE;
4334 }
4335 }
4336 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4337 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004338 done = TRUE;
4339 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004340 else
4341 {
4342 /* Look up the variable. */
4343 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4344 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4345 'b', varname, FALSE);
4346 if (v != NULL)
4347 {
4348 copy_tv(&v->di_tv, rettv);
4349 done = TRUE;
4350 }
4351 }
4352
4353 /* restore previous notion of curbuf */
4354 curbuf = save_curbuf;
4355 }
4356
4357 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4358 /* use the default value */
4359 copy_tv(&argvars[2], rettv);
4360
4361 --emsg_off;
4362}
4363
4364/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004365 * "getchangelist()" function
4366 */
4367 static void
4368f_getchangelist(typval_T *argvars, typval_T *rettv)
4369{
4370#ifdef FEAT_JUMPLIST
4371 buf_T *buf;
4372 int i;
4373 list_T *l;
4374 dict_T *d;
4375#endif
4376
4377 if (rettv_list_alloc(rettv) != OK)
4378 return;
4379
4380#ifdef FEAT_JUMPLIST
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004381 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4382 ++emsg_off;
4383 buf = get_buf_tv(&argvars[0], FALSE);
4384 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004385 if (buf == NULL)
4386 return;
4387
4388 l = list_alloc();
4389 if (l == NULL)
4390 return;
4391
4392 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4393 return;
4394 /*
4395 * The current window change list index tracks only the position in the
4396 * current buffer change list. For other buffers, use the change list
4397 * length as the current index.
4398 */
4399 list_append_number(rettv->vval.v_list,
4400 (varnumber_T)((buf == curwin->w_buffer)
4401 ? curwin->w_changelistidx : buf->b_changelistlen));
4402
4403 for (i = 0; i < buf->b_changelistlen; ++i)
4404 {
4405 if (buf->b_changelist[i].lnum == 0)
4406 continue;
4407 if ((d = dict_alloc()) == NULL)
4408 return;
4409 if (list_append_dict(l, d) == FAIL)
4410 return;
4411 dict_add_nr_str(d, "lnum", (long)buf->b_changelist[i].lnum, NULL);
4412 dict_add_nr_str(d, "col", (long)buf->b_changelist[i].col, NULL);
4413# ifdef FEAT_VIRTUALEDIT
4414 dict_add_nr_str(d, "coladd", (long)buf->b_changelist[i].coladd, NULL);
4415# endif
4416 }
4417#endif
4418}
4419/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004420 * "getchar()" function
4421 */
4422 static void
4423f_getchar(typval_T *argvars, typval_T *rettv)
4424{
4425 varnumber_T n;
4426 int error = FALSE;
4427
4428 /* Position the cursor. Needed after a message that ends in a space. */
4429 windgoto(msg_row, msg_col);
4430
4431 ++no_mapping;
4432 ++allow_keys;
4433 for (;;)
4434 {
4435 if (argvars[0].v_type == VAR_UNKNOWN)
4436 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004437 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004438 else if (get_tv_number_chk(&argvars[0], &error) == 1)
4439 /* getchar(1): only check if char avail */
4440 n = vpeekc_any();
4441 else if (error || vpeekc_any() == NUL)
4442 /* illegal argument or getchar(0) and no char avail: return zero */
4443 n = 0;
4444 else
4445 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004446 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004447
4448 if (n == K_IGNORE)
4449 continue;
4450 break;
4451 }
4452 --no_mapping;
4453 --allow_keys;
4454
4455 set_vim_var_nr(VV_MOUSE_WIN, 0);
4456 set_vim_var_nr(VV_MOUSE_WINID, 0);
4457 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4458 set_vim_var_nr(VV_MOUSE_COL, 0);
4459
4460 rettv->vval.v_number = n;
4461 if (IS_SPECIAL(n) || mod_mask != 0)
4462 {
4463 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4464 int i = 0;
4465
4466 /* Turn a special key into three bytes, plus modifier. */
4467 if (mod_mask != 0)
4468 {
4469 temp[i++] = K_SPECIAL;
4470 temp[i++] = KS_MODIFIER;
4471 temp[i++] = mod_mask;
4472 }
4473 if (IS_SPECIAL(n))
4474 {
4475 temp[i++] = K_SPECIAL;
4476 temp[i++] = K_SECOND(n);
4477 temp[i++] = K_THIRD(n);
4478 }
4479#ifdef FEAT_MBYTE
4480 else if (has_mbyte)
4481 i += (*mb_char2bytes)(n, temp + i);
4482#endif
4483 else
4484 temp[i++] = n;
4485 temp[i++] = NUL;
4486 rettv->v_type = VAR_STRING;
4487 rettv->vval.v_string = vim_strsave(temp);
4488
4489#ifdef FEAT_MOUSE
4490 if (is_mouse_key(n))
4491 {
4492 int row = mouse_row;
4493 int col = mouse_col;
4494 win_T *win;
4495 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004496 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004497 int winnr = 1;
4498
4499 if (row >= 0 && col >= 0)
4500 {
4501 /* Find the window at the mouse coordinates and compute the
4502 * text position. */
4503 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004504 if (win == NULL)
4505 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004506 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004507 for (wp = firstwin; wp != win; wp = wp->w_next)
4508 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004509 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4510 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4511 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4512 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4513 }
4514 }
4515#endif
4516 }
4517}
4518
4519/*
4520 * "getcharmod()" function
4521 */
4522 static void
4523f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4524{
4525 rettv->vval.v_number = mod_mask;
4526}
4527
4528/*
4529 * "getcharsearch()" function
4530 */
4531 static void
4532f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4533{
4534 if (rettv_dict_alloc(rettv) != FAIL)
4535 {
4536 dict_T *dict = rettv->vval.v_dict;
4537
4538 dict_add_nr_str(dict, "char", 0L, last_csearch());
4539 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
4540 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
4541 }
4542}
4543
4544/*
4545 * "getcmdline()" function
4546 */
4547 static void
4548f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4549{
4550 rettv->v_type = VAR_STRING;
4551 rettv->vval.v_string = get_cmdline_str();
4552}
4553
4554/*
4555 * "getcmdpos()" function
4556 */
4557 static void
4558f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4559{
4560 rettv->vval.v_number = get_cmdline_pos() + 1;
4561}
4562
4563/*
4564 * "getcmdtype()" function
4565 */
4566 static void
4567f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4568{
4569 rettv->v_type = VAR_STRING;
4570 rettv->vval.v_string = alloc(2);
4571 if (rettv->vval.v_string != NULL)
4572 {
4573 rettv->vval.v_string[0] = get_cmdline_type();
4574 rettv->vval.v_string[1] = NUL;
4575 }
4576}
4577
4578/*
4579 * "getcmdwintype()" function
4580 */
4581 static void
4582f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4583{
4584 rettv->v_type = VAR_STRING;
4585 rettv->vval.v_string = NULL;
4586#ifdef FEAT_CMDWIN
4587 rettv->vval.v_string = alloc(2);
4588 if (rettv->vval.v_string != NULL)
4589 {
4590 rettv->vval.v_string[0] = cmdwin_type;
4591 rettv->vval.v_string[1] = NUL;
4592 }
4593#endif
4594}
4595
4596#if defined(FEAT_CMDL_COMPL)
4597/*
4598 * "getcompletion()" function
4599 */
4600 static void
4601f_getcompletion(typval_T *argvars, typval_T *rettv)
4602{
4603 char_u *pat;
4604 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004605 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004606 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
4607 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004608
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004609 if (argvars[2].v_type != VAR_UNKNOWN)
4610 filtered = get_tv_number_chk(&argvars[2], NULL);
4611
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004612 if (p_wic)
4613 options |= WILD_ICASE;
4614
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004615 /* For filtered results, 'wildignore' is used */
4616 if (!filtered)
4617 options |= WILD_KEEP_ALL;
4618
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004619 ExpandInit(&xpc);
4620 xpc.xp_pattern = get_tv_string(&argvars[0]);
4621 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4622 xpc.xp_context = cmdcomplete_str_to_type(get_tv_string(&argvars[1]));
4623 if (xpc.xp_context == EXPAND_NOTHING)
4624 {
4625 if (argvars[1].v_type == VAR_STRING)
4626 EMSG2(_(e_invarg2), argvars[1].vval.v_string);
4627 else
4628 EMSG(_(e_invarg));
4629 return;
4630 }
4631
4632# if defined(FEAT_MENU)
4633 if (xpc.xp_context == EXPAND_MENUS)
4634 {
4635 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4636 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4637 }
4638# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02004639#ifdef FEAT_CSCOPE
4640 if (xpc.xp_context == EXPAND_CSCOPE)
4641 {
4642 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4643 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4644 }
4645#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02004646#ifdef FEAT_SIGNS
4647 if (xpc.xp_context == EXPAND_SIGN)
4648 {
4649 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4650 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4651 }
4652#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004653
4654 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4655 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
4656 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004657 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004658
4659 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4660
4661 for (i = 0; i < xpc.xp_numfiles; i++)
4662 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4663 }
4664 vim_free(pat);
4665 ExpandCleanup(&xpc);
4666}
4667#endif
4668
4669/*
4670 * "getcwd()" function
4671 */
4672 static void
4673f_getcwd(typval_T *argvars, typval_T *rettv)
4674{
4675 win_T *wp = NULL;
4676 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01004677 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004678
4679 rettv->v_type = VAR_STRING;
4680 rettv->vval.v_string = NULL;
4681
Bram Moolenaar54591292018-02-09 20:53:59 +01004682 if (argvars[0].v_type == VAR_NUMBER && argvars[0].vval.v_number == -1)
4683 global = TRUE;
4684 else
4685 wp = find_tabwin(&argvars[0], &argvars[1]);
4686
4687 if (wp != NULL && wp->w_localdir != NULL)
4688 rettv->vval.v_string = vim_strsave(wp->w_localdir);
4689 else if (wp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004690 {
Bram Moolenaar54591292018-02-09 20:53:59 +01004691 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004692 rettv->vval.v_string = vim_strsave(globaldir);
4693 else
4694 {
4695 cwd = alloc(MAXPATHL);
4696 if (cwd != NULL)
4697 {
4698 if (mch_dirname(cwd, MAXPATHL) != FAIL)
4699 rettv->vval.v_string = vim_strsave(cwd);
4700 vim_free(cwd);
4701 }
4702 }
4703#ifdef BACKSLASH_IN_FILENAME
4704 if (rettv->vval.v_string != NULL)
4705 slash_adjust(rettv->vval.v_string);
4706#endif
4707 }
4708}
4709
4710/*
4711 * "getfontname()" function
4712 */
4713 static void
4714f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4715{
4716 rettv->v_type = VAR_STRING;
4717 rettv->vval.v_string = NULL;
4718#ifdef FEAT_GUI
4719 if (gui.in_use)
4720 {
4721 GuiFont font;
4722 char_u *name = NULL;
4723
4724 if (argvars[0].v_type == VAR_UNKNOWN)
4725 {
4726 /* Get the "Normal" font. Either the name saved by
4727 * hl_set_font_name() or from the font ID. */
4728 font = gui.norm_font;
4729 name = hl_get_font_name();
4730 }
4731 else
4732 {
4733 name = get_tv_string(&argvars[0]);
4734 if (STRCMP(name, "*") == 0) /* don't use font dialog */
4735 return;
4736 font = gui_mch_get_font(name, FALSE);
4737 if (font == NOFONT)
4738 return; /* Invalid font name, return empty string. */
4739 }
4740 rettv->vval.v_string = gui_mch_get_fontname(font, name);
4741 if (argvars[0].v_type != VAR_UNKNOWN)
4742 gui_mch_free_font(font);
4743 }
4744#endif
4745}
4746
4747/*
4748 * "getfperm({fname})" function
4749 */
4750 static void
4751f_getfperm(typval_T *argvars, typval_T *rettv)
4752{
4753 char_u *fname;
4754 stat_T st;
4755 char_u *perm = NULL;
4756 char_u flags[] = "rwx";
4757 int i;
4758
4759 fname = get_tv_string(&argvars[0]);
4760
4761 rettv->v_type = VAR_STRING;
4762 if (mch_stat((char *)fname, &st) >= 0)
4763 {
4764 perm = vim_strsave((char_u *)"---------");
4765 if (perm != NULL)
4766 {
4767 for (i = 0; i < 9; i++)
4768 {
4769 if (st.st_mode & (1 << (8 - i)))
4770 perm[i] = flags[i % 3];
4771 }
4772 }
4773 }
4774 rettv->vval.v_string = perm;
4775}
4776
4777/*
4778 * "getfsize({fname})" function
4779 */
4780 static void
4781f_getfsize(typval_T *argvars, typval_T *rettv)
4782{
4783 char_u *fname;
4784 stat_T st;
4785
4786 fname = get_tv_string(&argvars[0]);
4787
4788 rettv->v_type = VAR_NUMBER;
4789
4790 if (mch_stat((char *)fname, &st) >= 0)
4791 {
4792 if (mch_isdir(fname))
4793 rettv->vval.v_number = 0;
4794 else
4795 {
4796 rettv->vval.v_number = (varnumber_T)st.st_size;
4797
4798 /* non-perfect check for overflow */
4799 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
4800 rettv->vval.v_number = -2;
4801 }
4802 }
4803 else
4804 rettv->vval.v_number = -1;
4805}
4806
4807/*
4808 * "getftime({fname})" function
4809 */
4810 static void
4811f_getftime(typval_T *argvars, typval_T *rettv)
4812{
4813 char_u *fname;
4814 stat_T st;
4815
4816 fname = get_tv_string(&argvars[0]);
4817
4818 if (mch_stat((char *)fname, &st) >= 0)
4819 rettv->vval.v_number = (varnumber_T)st.st_mtime;
4820 else
4821 rettv->vval.v_number = -1;
4822}
4823
4824/*
4825 * "getftype({fname})" function
4826 */
4827 static void
4828f_getftype(typval_T *argvars, typval_T *rettv)
4829{
4830 char_u *fname;
4831 stat_T st;
4832 char_u *type = NULL;
4833 char *t;
4834
4835 fname = get_tv_string(&argvars[0]);
4836
4837 rettv->v_type = VAR_STRING;
4838 if (mch_lstat((char *)fname, &st) >= 0)
4839 {
4840#ifdef S_ISREG
4841 if (S_ISREG(st.st_mode))
4842 t = "file";
4843 else if (S_ISDIR(st.st_mode))
4844 t = "dir";
4845# ifdef S_ISLNK
4846 else if (S_ISLNK(st.st_mode))
4847 t = "link";
4848# endif
4849# ifdef S_ISBLK
4850 else if (S_ISBLK(st.st_mode))
4851 t = "bdev";
4852# endif
4853# ifdef S_ISCHR
4854 else if (S_ISCHR(st.st_mode))
4855 t = "cdev";
4856# endif
4857# ifdef S_ISFIFO
4858 else if (S_ISFIFO(st.st_mode))
4859 t = "fifo";
4860# endif
4861# ifdef S_ISSOCK
4862 else if (S_ISSOCK(st.st_mode))
4863 t = "fifo";
4864# endif
4865 else
4866 t = "other";
4867#else
4868# ifdef S_IFMT
4869 switch (st.st_mode & S_IFMT)
4870 {
4871 case S_IFREG: t = "file"; break;
4872 case S_IFDIR: t = "dir"; break;
4873# ifdef S_IFLNK
4874 case S_IFLNK: t = "link"; break;
4875# endif
4876# ifdef S_IFBLK
4877 case S_IFBLK: t = "bdev"; break;
4878# endif
4879# ifdef S_IFCHR
4880 case S_IFCHR: t = "cdev"; break;
4881# endif
4882# ifdef S_IFIFO
4883 case S_IFIFO: t = "fifo"; break;
4884# endif
4885# ifdef S_IFSOCK
4886 case S_IFSOCK: t = "socket"; break;
4887# endif
4888 default: t = "other";
4889 }
4890# else
4891 if (mch_isdir(fname))
4892 t = "dir";
4893 else
4894 t = "file";
4895# endif
4896#endif
4897 type = vim_strsave((char_u *)t);
4898 }
4899 rettv->vval.v_string = type;
4900}
4901
4902/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01004903 * "getjumplist()" function
4904 */
4905 static void
4906f_getjumplist(typval_T *argvars, typval_T *rettv)
4907{
4908#ifdef FEAT_JUMPLIST
4909 win_T *wp;
4910 int i;
4911 list_T *l;
4912 dict_T *d;
4913#endif
4914
4915 if (rettv_list_alloc(rettv) != OK)
4916 return;
4917
4918#ifdef FEAT_JUMPLIST
4919 wp = find_tabwin(&argvars[0], &argvars[1]);
4920 if (wp == NULL)
4921 return;
4922
4923 l = list_alloc();
4924 if (l == NULL)
4925 return;
4926
4927 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4928 return;
4929 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
4930
Bram Moolenaar48679742018-02-13 13:33:29 +01004931 cleanup_jumplist(wp, TRUE);
4932
Bram Moolenaar4f505882018-02-10 21:06:32 +01004933 for (i = 0; i < wp->w_jumplistlen; ++i)
4934 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004935 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
4936 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01004937 if ((d = dict_alloc()) == NULL)
4938 return;
4939 if (list_append_dict(l, d) == FAIL)
4940 return;
4941 dict_add_nr_str(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum,
4942 NULL);
4943 dict_add_nr_str(d, "col", (long)wp->w_jumplist[i].fmark.mark.col,
4944 NULL);
4945# ifdef FEAT_VIRTUALEDIT
4946 dict_add_nr_str(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd,
4947 NULL);
4948# endif
4949 dict_add_nr_str(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum, NULL);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004950 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaar4f505882018-02-10 21:06:32 +01004951 dict_add_nr_str(d, "filename", 0L, wp->w_jumplist[i].fname);
4952 }
4953#endif
4954}
4955
4956/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004957 * "getline(lnum, [end])" function
4958 */
4959 static void
4960f_getline(typval_T *argvars, typval_T *rettv)
4961{
4962 linenr_T lnum;
4963 linenr_T end;
4964 int retlist;
4965
4966 lnum = get_tv_lnum(argvars);
4967 if (argvars[1].v_type == VAR_UNKNOWN)
4968 {
4969 end = 0;
4970 retlist = FALSE;
4971 }
4972 else
4973 {
4974 end = get_tv_lnum(&argvars[1]);
4975 retlist = TRUE;
4976 }
4977
4978 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
4979}
4980
Bram Moolenaar4ae20952016-08-13 15:29:14 +02004981#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02004982 static void
4983get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
4984{
Bram Moolenaard823fa92016-08-12 16:29:27 +02004985 if (what_arg->v_type == VAR_UNKNOWN)
4986 {
4987 if (rettv_list_alloc(rettv) == OK)
4988 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004989 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004990 }
4991 else
4992 {
4993 if (rettv_dict_alloc(rettv) == OK)
4994 if (is_qf || (wp != NULL))
4995 {
4996 if (what_arg->v_type == VAR_DICT)
4997 {
4998 dict_T *d = what_arg->vval.v_dict;
4999
5000 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005001 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005002 }
5003 else
5004 EMSG(_(e_dictreq));
5005 }
5006 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005007}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005008#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005009
5010/*
5011 * "getloclist()" function
5012 */
5013 static void
5014f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5015{
5016#ifdef FEAT_QUICKFIX
5017 win_T *wp;
5018
5019 wp = find_win_by_nr(&argvars[0], NULL);
5020 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5021#endif
5022}
5023
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005024/*
5025 * "getmatches()" function
5026 */
5027 static void
5028f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5029{
5030#ifdef FEAT_SEARCH_EXTRA
5031 dict_T *dict;
5032 matchitem_T *cur = curwin->w_match_head;
5033 int i;
5034
5035 if (rettv_list_alloc(rettv) == OK)
5036 {
5037 while (cur != NULL)
5038 {
5039 dict = dict_alloc();
5040 if (dict == NULL)
5041 return;
5042 if (cur->match.regprog == NULL)
5043 {
5044 /* match added with matchaddpos() */
5045 for (i = 0; i < MAXPOSMATCH; ++i)
5046 {
5047 llpos_T *llpos;
5048 char buf[6];
5049 list_T *l;
5050
5051 llpos = &cur->pos.pos[i];
5052 if (llpos->lnum == 0)
5053 break;
5054 l = list_alloc();
5055 if (l == NULL)
5056 break;
5057 list_append_number(l, (varnumber_T)llpos->lnum);
5058 if (llpos->col > 0)
5059 {
5060 list_append_number(l, (varnumber_T)llpos->col);
5061 list_append_number(l, (varnumber_T)llpos->len);
5062 }
5063 sprintf(buf, "pos%d", i + 1);
5064 dict_add_list(dict, buf, l);
5065 }
5066 }
5067 else
5068 {
5069 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
5070 }
5071 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
5072 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
5073 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
5074# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
5075 if (cur->conceal_char)
5076 {
5077 char_u buf[MB_MAXBYTES + 1];
5078
5079 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
5080 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
5081 }
5082# endif
5083 list_append_dict(rettv->vval.v_list, dict);
5084 cur = cur->next;
5085 }
5086 }
5087#endif
5088}
5089
5090/*
5091 * "getpid()" function
5092 */
5093 static void
5094f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5095{
5096 rettv->vval.v_number = mch_get_pid();
5097}
5098
5099 static void
5100getpos_both(
5101 typval_T *argvars,
5102 typval_T *rettv,
5103 int getcurpos)
5104{
5105 pos_T *fp;
5106 list_T *l;
5107 int fnum = -1;
5108
5109 if (rettv_list_alloc(rettv) == OK)
5110 {
5111 l = rettv->vval.v_list;
5112 if (getcurpos)
5113 fp = &curwin->w_cursor;
5114 else
5115 fp = var2fpos(&argvars[0], TRUE, &fnum);
5116 if (fnum != -1)
5117 list_append_number(l, (varnumber_T)fnum);
5118 else
5119 list_append_number(l, (varnumber_T)0);
5120 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5121 : (varnumber_T)0);
5122 list_append_number(l, (fp != NULL)
5123 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5124 : (varnumber_T)0);
5125 list_append_number(l,
5126#ifdef FEAT_VIRTUALEDIT
5127 (fp != NULL) ? (varnumber_T)fp->coladd :
5128#endif
5129 (varnumber_T)0);
5130 if (getcurpos)
5131 {
5132 update_curswant();
5133 list_append_number(l, curwin->w_curswant == MAXCOL ?
5134 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
5135 }
5136 }
5137 else
5138 rettv->vval.v_number = FALSE;
5139}
5140
5141
5142/*
5143 * "getcurpos()" function
5144 */
5145 static void
5146f_getcurpos(typval_T *argvars, typval_T *rettv)
5147{
5148 getpos_both(argvars, rettv, TRUE);
5149}
5150
5151/*
5152 * "getpos(string)" function
5153 */
5154 static void
5155f_getpos(typval_T *argvars, typval_T *rettv)
5156{
5157 getpos_both(argvars, rettv, FALSE);
5158}
5159
5160/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005161 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005162 */
5163 static void
5164f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5165{
5166#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005167 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005168#endif
5169}
5170
5171/*
5172 * "getreg()" function
5173 */
5174 static void
5175f_getreg(typval_T *argvars, typval_T *rettv)
5176{
5177 char_u *strregname;
5178 int regname;
5179 int arg2 = FALSE;
5180 int return_list = FALSE;
5181 int error = FALSE;
5182
5183 if (argvars[0].v_type != VAR_UNKNOWN)
5184 {
5185 strregname = get_tv_string_chk(&argvars[0]);
5186 error = strregname == NULL;
5187 if (argvars[1].v_type != VAR_UNKNOWN)
5188 {
5189 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
5190 if (!error && argvars[2].v_type != VAR_UNKNOWN)
5191 return_list = (int)get_tv_number_chk(&argvars[2], &error);
5192 }
5193 }
5194 else
5195 strregname = get_vim_var_str(VV_REG);
5196
5197 if (error)
5198 return;
5199
5200 regname = (strregname == NULL ? '"' : *strregname);
5201 if (regname == 0)
5202 regname = '"';
5203
5204 if (return_list)
5205 {
5206 rettv->v_type = VAR_LIST;
5207 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5208 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5209 if (rettv->vval.v_list == NULL)
5210 (void)rettv_list_alloc(rettv);
5211 else
5212 ++rettv->vval.v_list->lv_refcount;
5213 }
5214 else
5215 {
5216 rettv->v_type = VAR_STRING;
5217 rettv->vval.v_string = get_reg_contents(regname,
5218 arg2 ? GREG_EXPR_SRC : 0);
5219 }
5220}
5221
5222/*
5223 * "getregtype()" function
5224 */
5225 static void
5226f_getregtype(typval_T *argvars, typval_T *rettv)
5227{
5228 char_u *strregname;
5229 int regname;
5230 char_u buf[NUMBUFLEN + 2];
5231 long reglen = 0;
5232
5233 if (argvars[0].v_type != VAR_UNKNOWN)
5234 {
5235 strregname = get_tv_string_chk(&argvars[0]);
5236 if (strregname == NULL) /* type error; errmsg already given */
5237 {
5238 rettv->v_type = VAR_STRING;
5239 rettv->vval.v_string = NULL;
5240 return;
5241 }
5242 }
5243 else
5244 /* Default to v:register */
5245 strregname = get_vim_var_str(VV_REG);
5246
5247 regname = (strregname == NULL ? '"' : *strregname);
5248 if (regname == 0)
5249 regname = '"';
5250
5251 buf[0] = NUL;
5252 buf[1] = NUL;
5253 switch (get_reg_type(regname, &reglen))
5254 {
5255 case MLINE: buf[0] = 'V'; break;
5256 case MCHAR: buf[0] = 'v'; break;
5257 case MBLOCK:
5258 buf[0] = Ctrl_V;
5259 sprintf((char *)buf + 1, "%ld", reglen + 1);
5260 break;
5261 }
5262 rettv->v_type = VAR_STRING;
5263 rettv->vval.v_string = vim_strsave(buf);
5264}
5265
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005266/*
5267 * Returns information (variables, options, etc.) about a tab page
5268 * as a dictionary.
5269 */
5270 static dict_T *
5271get_tabpage_info(tabpage_T *tp, int tp_idx)
5272{
5273 win_T *wp;
5274 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005275 list_T *l;
5276
5277 dict = dict_alloc();
5278 if (dict == NULL)
5279 return NULL;
5280
Bram Moolenaar33928832016-08-18 21:22:04 +02005281 dict_add_nr_str(dict, "tabnr", tp_idx, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005282
5283 l = list_alloc();
5284 if (l != NULL)
5285 {
5286 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5287 wp; wp = wp->w_next)
5288 list_append_number(l, (varnumber_T)wp->w_id);
5289 dict_add_list(dict, "windows", l);
5290 }
5291
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005292 /* Make a reference to tabpage variables */
5293 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005294
5295 return dict;
5296}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005297
5298/*
5299 * "gettabinfo()" function
5300 */
5301 static void
5302f_gettabinfo(typval_T *argvars, typval_T *rettv)
5303{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005304 tabpage_T *tp, *tparg = NULL;
5305 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005306 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005307
5308 if (rettv_list_alloc(rettv) != OK)
5309 return;
5310
5311 if (argvars[0].v_type != VAR_UNKNOWN)
5312 {
5313 /* Information about one tab page */
5314 tparg = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5315 if (tparg == NULL)
5316 return;
5317 }
5318
5319 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005320 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005321 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005322 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005323 if (tparg != NULL && tp != tparg)
5324 continue;
5325 d = get_tabpage_info(tp, tpnr);
5326 if (d != NULL)
5327 list_append_dict(rettv->vval.v_list, d);
5328 if (tparg != NULL)
5329 return;
5330 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005331}
5332
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005333/*
5334 * "gettabvar()" function
5335 */
5336 static void
5337f_gettabvar(typval_T *argvars, typval_T *rettv)
5338{
5339 win_T *oldcurwin;
5340 tabpage_T *tp, *oldtabpage;
5341 dictitem_T *v;
5342 char_u *varname;
5343 int done = FALSE;
5344
5345 rettv->v_type = VAR_STRING;
5346 rettv->vval.v_string = NULL;
5347
5348 varname = get_tv_string_chk(&argvars[1]);
5349 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5350 if (tp != NULL && varname != NULL)
5351 {
5352 /* Set tp to be our tabpage, temporarily. Also set the window to the
5353 * first window in the tabpage, otherwise the window is not valid. */
5354 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005355 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5356 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005357 {
5358 /* look up the variable */
5359 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5360 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5361 if (v != NULL)
5362 {
5363 copy_tv(&v->di_tv, rettv);
5364 done = TRUE;
5365 }
5366 }
5367
5368 /* restore previous notion of curwin */
5369 restore_win(oldcurwin, oldtabpage, TRUE);
5370 }
5371
5372 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5373 copy_tv(&argvars[2], rettv);
5374}
5375
5376/*
5377 * "gettabwinvar()" function
5378 */
5379 static void
5380f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5381{
5382 getwinvar(argvars, rettv, 1);
5383}
5384
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005385/*
5386 * Returns information about a window as a dictionary.
5387 */
5388 static dict_T *
5389get_win_info(win_T *wp, short tpnr, short winnr)
5390{
5391 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005392
5393 dict = dict_alloc();
5394 if (dict == NULL)
5395 return NULL;
5396
Bram Moolenaar33928832016-08-18 21:22:04 +02005397 dict_add_nr_str(dict, "tabnr", tpnr, NULL);
5398 dict_add_nr_str(dict, "winnr", winnr, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005399 dict_add_nr_str(dict, "winid", wp->w_id, NULL);
5400 dict_add_nr_str(dict, "height", wp->w_height, NULL);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005401#ifdef FEAT_MENU
5402 dict_add_nr_str(dict, "winbar", wp->w_winbar_height, NULL);
5403#endif
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005404 dict_add_nr_str(dict, "width", wp->w_width, NULL);
Bram Moolenaar33928832016-08-18 21:22:04 +02005405 dict_add_nr_str(dict, "bufnr", wp->w_buffer->b_fnum, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005406
Bram Moolenaar69905d12017-08-13 18:14:47 +02005407#ifdef FEAT_TERMINAL
5408 dict_add_nr_str(dict, "terminal", bt_terminal(wp->w_buffer), NULL);
5409#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005410#ifdef FEAT_QUICKFIX
5411 dict_add_nr_str(dict, "quickfix", bt_quickfix(wp->w_buffer), NULL);
5412 dict_add_nr_str(dict, "loclist",
5413 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL), NULL);
5414#endif
5415
Bram Moolenaar30567352016-08-27 21:25:44 +02005416 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005417 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005418
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005419 return dict;
5420}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005421
5422/*
5423 * "getwininfo()" function
5424 */
5425 static void
5426f_getwininfo(typval_T *argvars, typval_T *rettv)
5427{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005428 tabpage_T *tp;
5429 win_T *wp = NULL, *wparg = NULL;
5430 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005431 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005432
5433 if (rettv_list_alloc(rettv) != OK)
5434 return;
5435
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005436 if (argvars[0].v_type != VAR_UNKNOWN)
5437 {
5438 wparg = win_id2wp(argvars);
5439 if (wparg == NULL)
5440 return;
5441 }
5442
5443 /* Collect information about either all the windows across all the tab
5444 * pages or one particular window.
5445 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005446 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005447 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005448 tabnr++;
5449 winnr = 0;
5450 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005451 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005452 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005453 if (wparg != NULL && wp != wparg)
5454 continue;
5455 d = get_win_info(wp, tabnr, winnr);
5456 if (d != NULL)
5457 list_append_dict(rettv->vval.v_list, d);
5458 if (wparg != NULL)
5459 /* found information about a specific window */
5460 return;
5461 }
5462 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005463}
5464
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005465/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005466 * "win_findbuf()" function
5467 */
5468 static void
5469f_win_findbuf(typval_T *argvars, typval_T *rettv)
5470{
5471 if (rettv_list_alloc(rettv) != FAIL)
5472 win_findbuf(argvars, rettv->vval.v_list);
5473}
5474
5475/*
5476 * "win_getid()" function
5477 */
5478 static void
5479f_win_getid(typval_T *argvars, typval_T *rettv)
5480{
5481 rettv->vval.v_number = win_getid(argvars);
5482}
5483
5484/*
5485 * "win_gotoid()" function
5486 */
5487 static void
5488f_win_gotoid(typval_T *argvars, typval_T *rettv)
5489{
5490 rettv->vval.v_number = win_gotoid(argvars);
5491}
5492
5493/*
5494 * "win_id2tabwin()" function
5495 */
5496 static void
5497f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5498{
5499 if (rettv_list_alloc(rettv) != FAIL)
5500 win_id2tabwin(argvars, rettv->vval.v_list);
5501}
5502
5503/*
5504 * "win_id2win()" function
5505 */
5506 static void
5507f_win_id2win(typval_T *argvars, typval_T *rettv)
5508{
5509 rettv->vval.v_number = win_id2win(argvars);
5510}
5511
5512/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005513 * "win_screenpos()" function
5514 */
5515 static void
5516f_win_screenpos(typval_T *argvars, typval_T *rettv)
5517{
5518 win_T *wp;
5519
5520 if (rettv_list_alloc(rettv) == FAIL)
5521 return;
5522
5523 wp = find_win_by_nr(&argvars[0], NULL);
5524 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5525 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5526}
5527
5528/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005529 * "getwinpos({timeout})" function
5530 */
5531 static void
5532f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
5533{
5534 int x = -1;
5535 int y = -1;
5536
5537 if (rettv_list_alloc(rettv) == FAIL)
5538 return;
5539#ifdef FEAT_GUI
5540 if (gui.in_use)
5541 gui_mch_get_winpos(&x, &y);
5542# if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5543 else
5544# endif
5545#endif
5546#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5547 {
5548 varnumber_T timeout = 100;
5549
5550 if (argvars[0].v_type != VAR_UNKNOWN)
5551 timeout = get_tv_number(&argvars[0]);
5552 term_get_winpos(&x, &y, timeout);
5553 }
5554#endif
5555 list_append_number(rettv->vval.v_list, (varnumber_T)x);
5556 list_append_number(rettv->vval.v_list, (varnumber_T)y);
5557}
5558
5559
5560/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005561 * "getwinposx()" function
5562 */
5563 static void
5564f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5565{
5566 rettv->vval.v_number = -1;
5567#ifdef FEAT_GUI
5568 if (gui.in_use)
5569 {
5570 int x, y;
5571
5572 if (gui_mch_get_winpos(&x, &y) == OK)
5573 rettv->vval.v_number = x;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005574 return;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005575 }
5576#endif
5577#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5578 {
5579 int x, y;
5580
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005581 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005582 rettv->vval.v_number = x;
5583 }
5584#endif
5585}
5586
5587/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005588 * "getwinposy()" function
5589 */
5590 static void
5591f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5592{
5593 rettv->vval.v_number = -1;
5594#ifdef FEAT_GUI
5595 if (gui.in_use)
5596 {
5597 int x, y;
5598
5599 if (gui_mch_get_winpos(&x, &y) == OK)
5600 rettv->vval.v_number = y;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005601 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005602 }
5603#endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005604#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5605 {
5606 int x, y;
5607
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005608 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005609 rettv->vval.v_number = y;
5610 }
5611#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005612}
5613
5614/*
5615 * "getwinvar()" function
5616 */
5617 static void
5618f_getwinvar(typval_T *argvars, typval_T *rettv)
5619{
5620 getwinvar(argvars, rettv, 0);
5621}
5622
5623/*
5624 * "glob()" function
5625 */
5626 static void
5627f_glob(typval_T *argvars, typval_T *rettv)
5628{
5629 int options = WILD_SILENT|WILD_USE_NL;
5630 expand_T xpc;
5631 int error = FALSE;
5632
5633 /* When the optional second argument is non-zero, don't remove matches
5634 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5635 rettv->v_type = VAR_STRING;
5636 if (argvars[1].v_type != VAR_UNKNOWN)
5637 {
5638 if (get_tv_number_chk(&argvars[1], &error))
5639 options |= WILD_KEEP_ALL;
5640 if (argvars[2].v_type != VAR_UNKNOWN)
5641 {
5642 if (get_tv_number_chk(&argvars[2], &error))
5643 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005644 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005645 }
5646 if (argvars[3].v_type != VAR_UNKNOWN
5647 && get_tv_number_chk(&argvars[3], &error))
5648 options |= WILD_ALLLINKS;
5649 }
5650 }
5651 if (!error)
5652 {
5653 ExpandInit(&xpc);
5654 xpc.xp_context = EXPAND_FILES;
5655 if (p_wic)
5656 options += WILD_ICASE;
5657 if (rettv->v_type == VAR_STRING)
5658 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
5659 NULL, options, WILD_ALL);
5660 else if (rettv_list_alloc(rettv) != FAIL)
5661 {
5662 int i;
5663
5664 ExpandOne(&xpc, get_tv_string(&argvars[0]),
5665 NULL, options, WILD_ALL_KEEP);
5666 for (i = 0; i < xpc.xp_numfiles; i++)
5667 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5668
5669 ExpandCleanup(&xpc);
5670 }
5671 }
5672 else
5673 rettv->vval.v_string = NULL;
5674}
5675
5676/*
5677 * "globpath()" function
5678 */
5679 static void
5680f_globpath(typval_T *argvars, typval_T *rettv)
5681{
5682 int flags = 0;
5683 char_u buf1[NUMBUFLEN];
5684 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
5685 int error = FALSE;
5686 garray_T ga;
5687 int i;
5688
5689 /* When the optional second argument is non-zero, don't remove matches
5690 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5691 rettv->v_type = VAR_STRING;
5692 if (argvars[2].v_type != VAR_UNKNOWN)
5693 {
5694 if (get_tv_number_chk(&argvars[2], &error))
5695 flags |= WILD_KEEP_ALL;
5696 if (argvars[3].v_type != VAR_UNKNOWN)
5697 {
5698 if (get_tv_number_chk(&argvars[3], &error))
5699 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005700 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005701 }
5702 if (argvars[4].v_type != VAR_UNKNOWN
5703 && get_tv_number_chk(&argvars[4], &error))
5704 flags |= WILD_ALLLINKS;
5705 }
5706 }
5707 if (file != NULL && !error)
5708 {
5709 ga_init2(&ga, (int)sizeof(char_u *), 10);
5710 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
5711 if (rettv->v_type == VAR_STRING)
5712 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
5713 else if (rettv_list_alloc(rettv) != FAIL)
5714 for (i = 0; i < ga.ga_len; ++i)
5715 list_append_string(rettv->vval.v_list,
5716 ((char_u **)(ga.ga_data))[i], -1);
5717 ga_clear_strings(&ga);
5718 }
5719 else
5720 rettv->vval.v_string = NULL;
5721}
5722
5723/*
5724 * "glob2regpat()" function
5725 */
5726 static void
5727f_glob2regpat(typval_T *argvars, typval_T *rettv)
5728{
5729 char_u *pat = get_tv_string_chk(&argvars[0]);
5730
5731 rettv->v_type = VAR_STRING;
5732 rettv->vval.v_string = (pat == NULL)
5733 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
5734}
5735
5736/* for VIM_VERSION_ defines */
5737#include "version.h"
5738
5739/*
5740 * "has()" function
5741 */
5742 static void
5743f_has(typval_T *argvars, typval_T *rettv)
5744{
5745 int i;
5746 char_u *name;
5747 int n = FALSE;
5748 static char *(has_list[]) =
5749 {
5750#ifdef AMIGA
5751 "amiga",
5752# ifdef FEAT_ARP
5753 "arp",
5754# endif
5755#endif
5756#ifdef __BEOS__
5757 "beos",
5758#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02005759#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01005760 "mac", /* Mac OS X (and, once, Mac OS Classic) */
5761 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02005762# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01005763 "macunix", /* Mac OS X, with the darwin feature */
5764 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02005765# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005766#endif
5767#ifdef __QNX__
5768 "qnx",
5769#endif
5770#ifdef UNIX
5771 "unix",
5772#endif
5773#ifdef VMS
5774 "vms",
5775#endif
5776#ifdef WIN32
5777 "win32",
5778#endif
5779#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
5780 "win32unix",
5781#endif
5782#if defined(WIN64) || defined(_WIN64)
5783 "win64",
5784#endif
5785#ifdef EBCDIC
5786 "ebcdic",
5787#endif
5788#ifndef CASE_INSENSITIVE_FILENAME
5789 "fname_case",
5790#endif
5791#ifdef HAVE_ACL
5792 "acl",
5793#endif
5794#ifdef FEAT_ARABIC
5795 "arabic",
5796#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005797 "autocmd",
Bram Moolenaare42a6d22017-11-12 19:21:51 +01005798#ifdef FEAT_AUTOSERVERNAME
5799 "autoservername",
5800#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01005801#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005802 "balloon_eval",
5803# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
5804 "balloon_multiline",
5805# endif
5806#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01005807#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01005808 "balloon_eval_term",
5809#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005810#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
5811 "builtin_terms",
5812# ifdef ALL_BUILTIN_TCAPS
5813 "all_builtin_terms",
5814# endif
5815#endif
5816#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
5817 || defined(FEAT_GUI_W32) \
5818 || defined(FEAT_GUI_MOTIF))
5819 "browsefilter",
5820#endif
5821#ifdef FEAT_BYTEOFF
5822 "byte_offset",
5823#endif
5824#ifdef FEAT_JOB_CHANNEL
5825 "channel",
5826#endif
5827#ifdef FEAT_CINDENT
5828 "cindent",
5829#endif
5830#ifdef FEAT_CLIENTSERVER
5831 "clientserver",
5832#endif
5833#ifdef FEAT_CLIPBOARD
5834 "clipboard",
5835#endif
5836#ifdef FEAT_CMDL_COMPL
5837 "cmdline_compl",
5838#endif
5839#ifdef FEAT_CMDHIST
5840 "cmdline_hist",
5841#endif
5842#ifdef FEAT_COMMENTS
5843 "comments",
5844#endif
5845#ifdef FEAT_CONCEAL
5846 "conceal",
5847#endif
5848#ifdef FEAT_CRYPT
5849 "cryptv",
5850 "crypt-blowfish",
5851 "crypt-blowfish2",
5852#endif
5853#ifdef FEAT_CSCOPE
5854 "cscope",
5855#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005856 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005857#ifdef CURSOR_SHAPE
5858 "cursorshape",
5859#endif
5860#ifdef DEBUG
5861 "debug",
5862#endif
5863#ifdef FEAT_CON_DIALOG
5864 "dialog_con",
5865#endif
5866#ifdef FEAT_GUI_DIALOG
5867 "dialog_gui",
5868#endif
5869#ifdef FEAT_DIFF
5870 "diff",
5871#endif
5872#ifdef FEAT_DIGRAPHS
5873 "digraphs",
5874#endif
5875#ifdef FEAT_DIRECTX
5876 "directx",
5877#endif
5878#ifdef FEAT_DND
5879 "dnd",
5880#endif
5881#ifdef FEAT_EMACS_TAGS
5882 "emacs_tags",
5883#endif
5884 "eval", /* always present, of course! */
5885 "ex_extra", /* graduated feature */
5886#ifdef FEAT_SEARCH_EXTRA
5887 "extra_search",
5888#endif
5889#ifdef FEAT_FKMAP
5890 "farsi",
5891#endif
5892#ifdef FEAT_SEARCHPATH
5893 "file_in_path",
5894#endif
5895#ifdef FEAT_FILTERPIPE
5896 "filterpipe",
5897#endif
5898#ifdef FEAT_FIND_ID
5899 "find_in_path",
5900#endif
5901#ifdef FEAT_FLOAT
5902 "float",
5903#endif
5904#ifdef FEAT_FOLDING
5905 "folding",
5906#endif
5907#ifdef FEAT_FOOTER
5908 "footer",
5909#endif
5910#if !defined(USE_SYSTEM) && defined(UNIX)
5911 "fork",
5912#endif
5913#ifdef FEAT_GETTEXT
5914 "gettext",
5915#endif
5916#ifdef FEAT_GUI
5917 "gui",
5918#endif
5919#ifdef FEAT_GUI_ATHENA
5920# ifdef FEAT_GUI_NEXTAW
5921 "gui_neXtaw",
5922# else
5923 "gui_athena",
5924# endif
5925#endif
5926#ifdef FEAT_GUI_GTK
5927 "gui_gtk",
5928# ifdef USE_GTK3
5929 "gui_gtk3",
5930# else
5931 "gui_gtk2",
5932# endif
5933#endif
5934#ifdef FEAT_GUI_GNOME
5935 "gui_gnome",
5936#endif
5937#ifdef FEAT_GUI_MAC
5938 "gui_mac",
5939#endif
5940#ifdef FEAT_GUI_MOTIF
5941 "gui_motif",
5942#endif
5943#ifdef FEAT_GUI_PHOTON
5944 "gui_photon",
5945#endif
5946#ifdef FEAT_GUI_W32
5947 "gui_win32",
5948#endif
5949#ifdef FEAT_HANGULIN
5950 "hangul_input",
5951#endif
5952#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
5953 "iconv",
5954#endif
5955#ifdef FEAT_INS_EXPAND
5956 "insert_expand",
5957#endif
5958#ifdef FEAT_JOB_CHANNEL
5959 "job",
5960#endif
5961#ifdef FEAT_JUMPLIST
5962 "jumplist",
5963#endif
5964#ifdef FEAT_KEYMAP
5965 "keymap",
5966#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02005967 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005968#ifdef FEAT_LANGMAP
5969 "langmap",
5970#endif
5971#ifdef FEAT_LIBCALL
5972 "libcall",
5973#endif
5974#ifdef FEAT_LINEBREAK
5975 "linebreak",
5976#endif
5977#ifdef FEAT_LISP
5978 "lispindent",
5979#endif
5980#ifdef FEAT_LISTCMDS
5981 "listcmds",
5982#endif
5983#ifdef FEAT_LOCALMAP
5984 "localmap",
5985#endif
5986#ifdef FEAT_LUA
5987# ifndef DYNAMIC_LUA
5988 "lua",
5989# endif
5990#endif
5991#ifdef FEAT_MENU
5992 "menu",
5993#endif
5994#ifdef FEAT_SESSION
5995 "mksession",
5996#endif
5997#ifdef FEAT_MODIFY_FNAME
5998 "modify_fname",
5999#endif
6000#ifdef FEAT_MOUSE
6001 "mouse",
6002#endif
6003#ifdef FEAT_MOUSESHAPE
6004 "mouseshape",
6005#endif
6006#if defined(UNIX) || defined(VMS)
6007# ifdef FEAT_MOUSE_DEC
6008 "mouse_dec",
6009# endif
6010# ifdef FEAT_MOUSE_GPM
6011 "mouse_gpm",
6012# endif
6013# ifdef FEAT_MOUSE_JSB
6014 "mouse_jsbterm",
6015# endif
6016# ifdef FEAT_MOUSE_NET
6017 "mouse_netterm",
6018# endif
6019# ifdef FEAT_MOUSE_PTERM
6020 "mouse_pterm",
6021# endif
6022# ifdef FEAT_MOUSE_SGR
6023 "mouse_sgr",
6024# endif
6025# ifdef FEAT_SYSMOUSE
6026 "mouse_sysmouse",
6027# endif
6028# ifdef FEAT_MOUSE_URXVT
6029 "mouse_urxvt",
6030# endif
6031# ifdef FEAT_MOUSE_XTERM
6032 "mouse_xterm",
6033# endif
6034#endif
6035#ifdef FEAT_MBYTE
6036 "multi_byte",
6037#endif
6038#ifdef FEAT_MBYTE_IME
6039 "multi_byte_ime",
6040#endif
6041#ifdef FEAT_MULTI_LANG
6042 "multi_lang",
6043#endif
6044#ifdef FEAT_MZSCHEME
6045#ifndef DYNAMIC_MZSCHEME
6046 "mzscheme",
6047#endif
6048#endif
6049#ifdef FEAT_NUM64
6050 "num64",
6051#endif
6052#ifdef FEAT_OLE
6053 "ole",
6054#endif
6055 "packages",
6056#ifdef FEAT_PATH_EXTRA
6057 "path_extra",
6058#endif
6059#ifdef FEAT_PERL
6060#ifndef DYNAMIC_PERL
6061 "perl",
6062#endif
6063#endif
6064#ifdef FEAT_PERSISTENT_UNDO
6065 "persistent_undo",
6066#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006067#if defined(FEAT_PYTHON)
6068 "python_compiled",
6069# if defined(DYNAMIC_PYTHON)
6070 "python_dynamic",
6071# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006072 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006073 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006074# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006075#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006076#if defined(FEAT_PYTHON3)
6077 "python3_compiled",
6078# if defined(DYNAMIC_PYTHON3)
6079 "python3_dynamic",
6080# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006081 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006082 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006083# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006084#endif
6085#ifdef FEAT_POSTSCRIPT
6086 "postscript",
6087#endif
6088#ifdef FEAT_PRINTER
6089 "printer",
6090#endif
6091#ifdef FEAT_PROFILE
6092 "profile",
6093#endif
6094#ifdef FEAT_RELTIME
6095 "reltime",
6096#endif
6097#ifdef FEAT_QUICKFIX
6098 "quickfix",
6099#endif
6100#ifdef FEAT_RIGHTLEFT
6101 "rightleft",
6102#endif
6103#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6104 "ruby",
6105#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006106 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006107#ifdef FEAT_CMDL_INFO
6108 "showcmd",
6109 "cmdline_info",
6110#endif
6111#ifdef FEAT_SIGNS
6112 "signs",
6113#endif
6114#ifdef FEAT_SMARTINDENT
6115 "smartindent",
6116#endif
6117#ifdef STARTUPTIME
6118 "startuptime",
6119#endif
6120#ifdef FEAT_STL_OPT
6121 "statusline",
6122#endif
6123#ifdef FEAT_SUN_WORKSHOP
6124 "sun_workshop",
6125#endif
6126#ifdef FEAT_NETBEANS_INTG
6127 "netbeans_intg",
6128#endif
6129#ifdef FEAT_SPELL
6130 "spell",
6131#endif
6132#ifdef FEAT_SYN_HL
6133 "syntax",
6134#endif
6135#if defined(USE_SYSTEM) || !defined(UNIX)
6136 "system",
6137#endif
6138#ifdef FEAT_TAG_BINS
6139 "tag_binary",
6140#endif
6141#ifdef FEAT_TAG_OLDSTATIC
6142 "tag_old_static",
6143#endif
6144#ifdef FEAT_TAG_ANYWHITE
6145 "tag_any_white",
6146#endif
6147#ifdef FEAT_TCL
6148# ifndef DYNAMIC_TCL
6149 "tcl",
6150# endif
6151#endif
6152#ifdef FEAT_TERMGUICOLORS
6153 "termguicolors",
6154#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006155#if defined(FEAT_TERMINAL) && !defined(WIN3264)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006156 "terminal",
6157#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006158#ifdef TERMINFO
6159 "terminfo",
6160#endif
6161#ifdef FEAT_TERMRESPONSE
6162 "termresponse",
6163#endif
6164#ifdef FEAT_TEXTOBJ
6165 "textobjects",
6166#endif
6167#ifdef HAVE_TGETENT
6168 "tgetent",
6169#endif
6170#ifdef FEAT_TIMERS
6171 "timers",
6172#endif
6173#ifdef FEAT_TITLE
6174 "title",
6175#endif
6176#ifdef FEAT_TOOLBAR
6177 "toolbar",
6178#endif
6179#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6180 "unnamedplus",
6181#endif
6182#ifdef FEAT_USR_CMDS
6183 "user-commands", /* was accidentally included in 5.4 */
6184 "user_commands",
6185#endif
6186#ifdef FEAT_VIMINFO
6187 "viminfo",
6188#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006189 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006190#ifdef FEAT_VIRTUALEDIT
6191 "virtualedit",
6192#endif
6193 "visual",
6194#ifdef FEAT_VISUALEXTRA
6195 "visualextra",
6196#endif
6197#ifdef FEAT_VREPLACE
6198 "vreplace",
6199#endif
6200#ifdef FEAT_WILDIGN
6201 "wildignore",
6202#endif
6203#ifdef FEAT_WILDMENU
6204 "wildmenu",
6205#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006206 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006207#ifdef FEAT_WAK
6208 "winaltkeys",
6209#endif
6210#ifdef FEAT_WRITEBACKUP
6211 "writebackup",
6212#endif
6213#ifdef FEAT_XIM
6214 "xim",
6215#endif
6216#ifdef FEAT_XFONTSET
6217 "xfontset",
6218#endif
6219#ifdef FEAT_XPM_W32
6220 "xpm",
6221 "xpm_w32", /* for backward compatibility */
6222#else
6223# if defined(HAVE_XPM)
6224 "xpm",
6225# endif
6226#endif
6227#ifdef USE_XSMP
6228 "xsmp",
6229#endif
6230#ifdef USE_XSMP_INTERACT
6231 "xsmp_interact",
6232#endif
6233#ifdef FEAT_XCLIPBOARD
6234 "xterm_clipboard",
6235#endif
6236#ifdef FEAT_XTERM_SAVE
6237 "xterm_save",
6238#endif
6239#if defined(UNIX) && defined(FEAT_X11)
6240 "X11",
6241#endif
6242 NULL
6243 };
6244
6245 name = get_tv_string(&argvars[0]);
6246 for (i = 0; has_list[i] != NULL; ++i)
6247 if (STRICMP(name, has_list[i]) == 0)
6248 {
6249 n = TRUE;
6250 break;
6251 }
6252
6253 if (n == FALSE)
6254 {
6255 if (STRNICMP(name, "patch", 5) == 0)
6256 {
6257 if (name[5] == '-'
6258 && STRLEN(name) >= 11
6259 && vim_isdigit(name[6])
6260 && vim_isdigit(name[8])
6261 && vim_isdigit(name[10]))
6262 {
6263 int major = atoi((char *)name + 6);
6264 int minor = atoi((char *)name + 8);
6265
6266 /* Expect "patch-9.9.01234". */
6267 n = (major < VIM_VERSION_MAJOR
6268 || (major == VIM_VERSION_MAJOR
6269 && (minor < VIM_VERSION_MINOR
6270 || (minor == VIM_VERSION_MINOR
6271 && has_patch(atoi((char *)name + 10))))));
6272 }
6273 else
6274 n = has_patch(atoi((char *)name + 5));
6275 }
6276 else if (STRICMP(name, "vim_starting") == 0)
6277 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006278 else if (STRICMP(name, "ttyin") == 0)
6279 n = mch_input_isatty();
6280 else if (STRICMP(name, "ttyout") == 0)
6281 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006282#ifdef FEAT_MBYTE
6283 else if (STRICMP(name, "multi_byte_encoding") == 0)
6284 n = has_mbyte;
6285#endif
6286#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
6287 else if (STRICMP(name, "balloon_multiline") == 0)
6288 n = multiline_balloon_available();
6289#endif
6290#ifdef DYNAMIC_TCL
6291 else if (STRICMP(name, "tcl") == 0)
6292 n = tcl_enabled(FALSE);
6293#endif
6294#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6295 else if (STRICMP(name, "iconv") == 0)
6296 n = iconv_enabled(FALSE);
6297#endif
6298#ifdef DYNAMIC_LUA
6299 else if (STRICMP(name, "lua") == 0)
6300 n = lua_enabled(FALSE);
6301#endif
6302#ifdef DYNAMIC_MZSCHEME
6303 else if (STRICMP(name, "mzscheme") == 0)
6304 n = mzscheme_enabled(FALSE);
6305#endif
6306#ifdef DYNAMIC_RUBY
6307 else if (STRICMP(name, "ruby") == 0)
6308 n = ruby_enabled(FALSE);
6309#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006310#ifdef DYNAMIC_PYTHON
6311 else if (STRICMP(name, "python") == 0)
6312 n = python_enabled(FALSE);
6313#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006314#ifdef DYNAMIC_PYTHON3
6315 else if (STRICMP(name, "python3") == 0)
6316 n = python3_enabled(FALSE);
6317#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006318#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6319 else if (STRICMP(name, "pythonx") == 0)
6320 {
6321# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6322 if (p_pyx == 0)
6323 n = python3_enabled(FALSE) || python_enabled(FALSE);
6324 else if (p_pyx == 3)
6325 n = python3_enabled(FALSE);
6326 else if (p_pyx == 2)
6327 n = python_enabled(FALSE);
6328# elif defined(DYNAMIC_PYTHON)
6329 n = python_enabled(FALSE);
6330# elif defined(DYNAMIC_PYTHON3)
6331 n = python3_enabled(FALSE);
6332# endif
6333 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006334#endif
6335#ifdef DYNAMIC_PERL
6336 else if (STRICMP(name, "perl") == 0)
6337 n = perl_enabled(FALSE);
6338#endif
6339#ifdef FEAT_GUI
6340 else if (STRICMP(name, "gui_running") == 0)
6341 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006342# ifdef FEAT_BROWSE
6343 else if (STRICMP(name, "browse") == 0)
6344 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6345# endif
6346#endif
6347#ifdef FEAT_SYN_HL
6348 else if (STRICMP(name, "syntax_items") == 0)
6349 n = syntax_present(curwin);
6350#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006351#ifdef FEAT_VTP
6352 else if (STRICMP(name, "vcon") == 0)
6353 n = has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006354#endif
6355#ifdef FEAT_NETBEANS_INTG
6356 else if (STRICMP(name, "netbeans_enabled") == 0)
6357 n = netbeans_active();
6358#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006359#if defined(FEAT_TERMINAL) && defined(WIN3264)
6360 else if (STRICMP(name, "terminal") == 0)
6361 n = terminal_enabled();
6362#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006363 }
6364
6365 rettv->vval.v_number = n;
6366}
6367
6368/*
6369 * "has_key()" function
6370 */
6371 static void
6372f_has_key(typval_T *argvars, typval_T *rettv)
6373{
6374 if (argvars[0].v_type != VAR_DICT)
6375 {
6376 EMSG(_(e_dictreq));
6377 return;
6378 }
6379 if (argvars[0].vval.v_dict == NULL)
6380 return;
6381
6382 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
6383 get_tv_string(&argvars[1]), -1) != NULL;
6384}
6385
6386/*
6387 * "haslocaldir()" function
6388 */
6389 static void
6390f_haslocaldir(typval_T *argvars, typval_T *rettv)
6391{
6392 win_T *wp = NULL;
6393
6394 wp = find_tabwin(&argvars[0], &argvars[1]);
6395 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6396}
6397
6398/*
6399 * "hasmapto()" function
6400 */
6401 static void
6402f_hasmapto(typval_T *argvars, typval_T *rettv)
6403{
6404 char_u *name;
6405 char_u *mode;
6406 char_u buf[NUMBUFLEN];
6407 int abbr = FALSE;
6408
6409 name = get_tv_string(&argvars[0]);
6410 if (argvars[1].v_type == VAR_UNKNOWN)
6411 mode = (char_u *)"nvo";
6412 else
6413 {
6414 mode = get_tv_string_buf(&argvars[1], buf);
6415 if (argvars[2].v_type != VAR_UNKNOWN)
6416 abbr = (int)get_tv_number(&argvars[2]);
6417 }
6418
6419 if (map_to_exists(name, mode, abbr))
6420 rettv->vval.v_number = TRUE;
6421 else
6422 rettv->vval.v_number = FALSE;
6423}
6424
6425/*
6426 * "histadd()" function
6427 */
6428 static void
6429f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6430{
6431#ifdef FEAT_CMDHIST
6432 int histype;
6433 char_u *str;
6434 char_u buf[NUMBUFLEN];
6435#endif
6436
6437 rettv->vval.v_number = FALSE;
6438 if (check_restricted() || check_secure())
6439 return;
6440#ifdef FEAT_CMDHIST
6441 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6442 histype = str != NULL ? get_histtype(str) : -1;
6443 if (histype >= 0)
6444 {
6445 str = get_tv_string_buf(&argvars[1], buf);
6446 if (*str != NUL)
6447 {
6448 init_history();
6449 add_to_history(histype, str, FALSE, NUL);
6450 rettv->vval.v_number = TRUE;
6451 return;
6452 }
6453 }
6454#endif
6455}
6456
6457/*
6458 * "histdel()" function
6459 */
6460 static void
6461f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6462{
6463#ifdef FEAT_CMDHIST
6464 int n;
6465 char_u buf[NUMBUFLEN];
6466 char_u *str;
6467
6468 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6469 if (str == NULL)
6470 n = 0;
6471 else if (argvars[1].v_type == VAR_UNKNOWN)
6472 /* only one argument: clear entire history */
6473 n = clr_history(get_histtype(str));
6474 else if (argvars[1].v_type == VAR_NUMBER)
6475 /* index given: remove that entry */
6476 n = del_history_idx(get_histtype(str),
6477 (int)get_tv_number(&argvars[1]));
6478 else
6479 /* string given: remove all matching entries */
6480 n = del_history_entry(get_histtype(str),
6481 get_tv_string_buf(&argvars[1], buf));
6482 rettv->vval.v_number = n;
6483#endif
6484}
6485
6486/*
6487 * "histget()" function
6488 */
6489 static void
6490f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6491{
6492#ifdef FEAT_CMDHIST
6493 int type;
6494 int idx;
6495 char_u *str;
6496
6497 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6498 if (str == NULL)
6499 rettv->vval.v_string = NULL;
6500 else
6501 {
6502 type = get_histtype(str);
6503 if (argvars[1].v_type == VAR_UNKNOWN)
6504 idx = get_history_idx(type);
6505 else
6506 idx = (int)get_tv_number_chk(&argvars[1], NULL);
6507 /* -1 on type error */
6508 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6509 }
6510#else
6511 rettv->vval.v_string = NULL;
6512#endif
6513 rettv->v_type = VAR_STRING;
6514}
6515
6516/*
6517 * "histnr()" function
6518 */
6519 static void
6520f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6521{
6522 int i;
6523
6524#ifdef FEAT_CMDHIST
6525 char_u *history = get_tv_string_chk(&argvars[0]);
6526
6527 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6528 if (i >= HIST_CMD && i < HIST_COUNT)
6529 i = get_history_idx(i);
6530 else
6531#endif
6532 i = -1;
6533 rettv->vval.v_number = i;
6534}
6535
6536/*
6537 * "highlightID(name)" function
6538 */
6539 static void
6540f_hlID(typval_T *argvars, typval_T *rettv)
6541{
6542 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
6543}
6544
6545/*
6546 * "highlight_exists()" function
6547 */
6548 static void
6549f_hlexists(typval_T *argvars, typval_T *rettv)
6550{
6551 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
6552}
6553
6554/*
6555 * "hostname()" function
6556 */
6557 static void
6558f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6559{
6560 char_u hostname[256];
6561
6562 mch_get_host_name(hostname, 256);
6563 rettv->v_type = VAR_STRING;
6564 rettv->vval.v_string = vim_strsave(hostname);
6565}
6566
6567/*
6568 * iconv() function
6569 */
6570 static void
6571f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6572{
6573#ifdef FEAT_MBYTE
6574 char_u buf1[NUMBUFLEN];
6575 char_u buf2[NUMBUFLEN];
6576 char_u *from, *to, *str;
6577 vimconv_T vimconv;
6578#endif
6579
6580 rettv->v_type = VAR_STRING;
6581 rettv->vval.v_string = NULL;
6582
6583#ifdef FEAT_MBYTE
6584 str = get_tv_string(&argvars[0]);
6585 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
6586 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
6587 vimconv.vc_type = CONV_NONE;
6588 convert_setup(&vimconv, from, to);
6589
6590 /* If the encodings are equal, no conversion needed. */
6591 if (vimconv.vc_type == CONV_NONE)
6592 rettv->vval.v_string = vim_strsave(str);
6593 else
6594 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6595
6596 convert_setup(&vimconv, NULL, NULL);
6597 vim_free(from);
6598 vim_free(to);
6599#endif
6600}
6601
6602/*
6603 * "indent()" function
6604 */
6605 static void
6606f_indent(typval_T *argvars, typval_T *rettv)
6607{
6608 linenr_T lnum;
6609
6610 lnum = get_tv_lnum(argvars);
6611 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6612 rettv->vval.v_number = get_indent_lnum(lnum);
6613 else
6614 rettv->vval.v_number = -1;
6615}
6616
6617/*
6618 * "index()" function
6619 */
6620 static void
6621f_index(typval_T *argvars, typval_T *rettv)
6622{
6623 list_T *l;
6624 listitem_T *item;
6625 long idx = 0;
6626 int ic = FALSE;
6627
6628 rettv->vval.v_number = -1;
6629 if (argvars[0].v_type != VAR_LIST)
6630 {
6631 EMSG(_(e_listreq));
6632 return;
6633 }
6634 l = argvars[0].vval.v_list;
6635 if (l != NULL)
6636 {
6637 item = l->lv_first;
6638 if (argvars[2].v_type != VAR_UNKNOWN)
6639 {
6640 int error = FALSE;
6641
6642 /* Start at specified item. Use the cached index that list_find()
6643 * sets, so that a negative number also works. */
6644 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
6645 idx = l->lv_idx;
6646 if (argvars[3].v_type != VAR_UNKNOWN)
6647 ic = (int)get_tv_number_chk(&argvars[3], &error);
6648 if (error)
6649 item = NULL;
6650 }
6651
6652 for ( ; item != NULL; item = item->li_next, ++idx)
6653 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6654 {
6655 rettv->vval.v_number = idx;
6656 break;
6657 }
6658 }
6659}
6660
6661static int inputsecret_flag = 0;
6662
6663/*
6664 * "input()" function
6665 * Also handles inputsecret() when inputsecret is set.
6666 */
6667 static void
6668f_input(typval_T *argvars, typval_T *rettv)
6669{
6670 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6671}
6672
6673/*
6674 * "inputdialog()" function
6675 */
6676 static void
6677f_inputdialog(typval_T *argvars, typval_T *rettv)
6678{
6679#if defined(FEAT_GUI_TEXTDIALOG)
6680 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
6681 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6682 {
6683 char_u *message;
6684 char_u buf[NUMBUFLEN];
6685 char_u *defstr = (char_u *)"";
6686
6687 message = get_tv_string_chk(&argvars[0]);
6688 if (argvars[1].v_type != VAR_UNKNOWN
6689 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
6690 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6691 else
6692 IObuff[0] = NUL;
6693 if (message != NULL && defstr != NULL
6694 && do_dialog(VIM_QUESTION, NULL, message,
6695 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6696 rettv->vval.v_string = vim_strsave(IObuff);
6697 else
6698 {
6699 if (message != NULL && defstr != NULL
6700 && argvars[1].v_type != VAR_UNKNOWN
6701 && argvars[2].v_type != VAR_UNKNOWN)
6702 rettv->vval.v_string = vim_strsave(
6703 get_tv_string_buf(&argvars[2], buf));
6704 else
6705 rettv->vval.v_string = NULL;
6706 }
6707 rettv->v_type = VAR_STRING;
6708 }
6709 else
6710#endif
6711 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6712}
6713
6714/*
6715 * "inputlist()" function
6716 */
6717 static void
6718f_inputlist(typval_T *argvars, typval_T *rettv)
6719{
6720 listitem_T *li;
6721 int selected;
6722 int mouse_used;
6723
6724#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02006725 /* While starting up, there is no place to enter text. When running tests
6726 * with --not-a-term we assume feedkeys() will be used. */
6727 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006728 return;
6729#endif
6730 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6731 {
6732 EMSG2(_(e_listarg), "inputlist()");
6733 return;
6734 }
6735
6736 msg_start();
6737 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
6738 lines_left = Rows; /* avoid more prompt */
6739 msg_scroll = TRUE;
6740 msg_clr_eos();
6741
6742 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
6743 {
6744 msg_puts(get_tv_string(&li->li_tv));
6745 msg_putchar('\n');
6746 }
6747
6748 /* Ask for choice. */
6749 selected = prompt_for_number(&mouse_used);
6750 if (mouse_used)
6751 selected -= lines_left;
6752
6753 rettv->vval.v_number = selected;
6754}
6755
6756
6757static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6758
6759/*
6760 * "inputrestore()" function
6761 */
6762 static void
6763f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6764{
6765 if (ga_userinput.ga_len > 0)
6766 {
6767 --ga_userinput.ga_len;
6768 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
6769 + ga_userinput.ga_len);
6770 /* default return is zero == OK */
6771 }
6772 else if (p_verbose > 1)
6773 {
6774 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
6775 rettv->vval.v_number = 1; /* Failed */
6776 }
6777}
6778
6779/*
6780 * "inputsave()" function
6781 */
6782 static void
6783f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
6784{
6785 /* Add an entry to the stack of typeahead storage. */
6786 if (ga_grow(&ga_userinput, 1) == OK)
6787 {
6788 save_typeahead((tasave_T *)(ga_userinput.ga_data)
6789 + ga_userinput.ga_len);
6790 ++ga_userinput.ga_len;
6791 /* default return is zero == OK */
6792 }
6793 else
6794 rettv->vval.v_number = 1; /* Failed */
6795}
6796
6797/*
6798 * "inputsecret()" function
6799 */
6800 static void
6801f_inputsecret(typval_T *argvars, typval_T *rettv)
6802{
6803 ++cmdline_star;
6804 ++inputsecret_flag;
6805 f_input(argvars, rettv);
6806 --cmdline_star;
6807 --inputsecret_flag;
6808}
6809
6810/*
6811 * "insert()" function
6812 */
6813 static void
6814f_insert(typval_T *argvars, typval_T *rettv)
6815{
6816 long before = 0;
6817 listitem_T *item;
6818 list_T *l;
6819 int error = FALSE;
6820
6821 if (argvars[0].v_type != VAR_LIST)
6822 EMSG2(_(e_listarg), "insert()");
6823 else if ((l = argvars[0].vval.v_list) != NULL
6824 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
6825 {
6826 if (argvars[2].v_type != VAR_UNKNOWN)
6827 before = (long)get_tv_number_chk(&argvars[2], &error);
6828 if (error)
6829 return; /* type error; errmsg already given */
6830
6831 if (before == l->lv_len)
6832 item = NULL;
6833 else
6834 {
6835 item = list_find(l, before);
6836 if (item == NULL)
6837 {
6838 EMSGN(_(e_listidx), before);
6839 l = NULL;
6840 }
6841 }
6842 if (l != NULL)
6843 {
6844 list_insert_tv(l, &argvars[1], item);
6845 copy_tv(&argvars[0], rettv);
6846 }
6847 }
6848}
6849
6850/*
6851 * "invert(expr)" function
6852 */
6853 static void
6854f_invert(typval_T *argvars, typval_T *rettv)
6855{
6856 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
6857}
6858
6859/*
6860 * "isdirectory()" function
6861 */
6862 static void
6863f_isdirectory(typval_T *argvars, typval_T *rettv)
6864{
6865 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
6866}
6867
6868/*
6869 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
6870 * or it refers to a List or Dictionary that is locked.
6871 */
6872 static int
6873tv_islocked(typval_T *tv)
6874{
6875 return (tv->v_lock & VAR_LOCKED)
6876 || (tv->v_type == VAR_LIST
6877 && tv->vval.v_list != NULL
6878 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
6879 || (tv->v_type == VAR_DICT
6880 && tv->vval.v_dict != NULL
6881 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
6882}
6883
6884/*
6885 * "islocked()" function
6886 */
6887 static void
6888f_islocked(typval_T *argvars, typval_T *rettv)
6889{
6890 lval_T lv;
6891 char_u *end;
6892 dictitem_T *di;
6893
6894 rettv->vval.v_number = -1;
6895 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01006896 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006897 if (end != NULL && lv.ll_name != NULL)
6898 {
6899 if (*end != NUL)
6900 EMSG(_(e_trailing));
6901 else
6902 {
6903 if (lv.ll_tv == NULL)
6904 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006905 di = find_var(lv.ll_name, NULL, TRUE);
6906 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006907 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006908 /* Consider a variable locked when:
6909 * 1. the variable itself is locked
6910 * 2. the value of the variable is locked.
6911 * 3. the List or Dict value is locked.
6912 */
6913 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
6914 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006915 }
6916 }
6917 else if (lv.ll_range)
6918 EMSG(_("E786: Range not allowed"));
6919 else if (lv.ll_newkey != NULL)
6920 EMSG2(_(e_dictkey), lv.ll_newkey);
6921 else if (lv.ll_list != NULL)
6922 /* List item. */
6923 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
6924 else
6925 /* Dictionary item. */
6926 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
6927 }
6928 }
6929
6930 clear_lval(&lv);
6931}
6932
6933#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
6934/*
6935 * "isnan()" function
6936 */
6937 static void
6938f_isnan(typval_T *argvars, typval_T *rettv)
6939{
6940 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
6941 && isnan(argvars[0].vval.v_float);
6942}
6943#endif
6944
6945/*
6946 * "items(dict)" function
6947 */
6948 static void
6949f_items(typval_T *argvars, typval_T *rettv)
6950{
6951 dict_list(argvars, rettv, 2);
6952}
6953
6954#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
6955/*
6956 * Get the job from the argument.
6957 * Returns NULL if the job is invalid.
6958 */
6959 static job_T *
6960get_job_arg(typval_T *tv)
6961{
6962 job_T *job;
6963
6964 if (tv->v_type != VAR_JOB)
6965 {
6966 EMSG2(_(e_invarg2), get_tv_string(tv));
6967 return NULL;
6968 }
6969 job = tv->vval.v_job;
6970
6971 if (job == NULL)
6972 EMSG(_("E916: not a valid job"));
6973 return job;
6974}
6975
6976/*
6977 * "job_getchannel()" function
6978 */
6979 static void
6980f_job_getchannel(typval_T *argvars, typval_T *rettv)
6981{
6982 job_T *job = get_job_arg(&argvars[0]);
6983
6984 if (job != NULL)
6985 {
6986 rettv->v_type = VAR_CHANNEL;
6987 rettv->vval.v_channel = job->jv_channel;
6988 if (job->jv_channel != NULL)
6989 ++job->jv_channel->ch_refcount;
6990 }
6991}
6992
6993/*
6994 * "job_info()" function
6995 */
6996 static void
6997f_job_info(typval_T *argvars, typval_T *rettv)
6998{
6999 job_T *job = get_job_arg(&argvars[0]);
7000
7001 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7002 job_info(job, rettv->vval.v_dict);
7003}
7004
7005/*
7006 * "job_setoptions()" function
7007 */
7008 static void
7009f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7010{
7011 job_T *job = get_job_arg(&argvars[0]);
7012 jobopt_T opt;
7013
7014 if (job == NULL)
7015 return;
7016 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007017 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007018 job_set_options(job, &opt);
7019 free_job_options(&opt);
7020}
7021
7022/*
7023 * "job_start()" function
7024 */
7025 static void
7026f_job_start(typval_T *argvars, typval_T *rettv)
7027{
7028 rettv->v_type = VAR_JOB;
7029 if (check_restricted() || check_secure())
7030 return;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02007031 rettv->vval.v_job = job_start(argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007032}
7033
7034/*
7035 * "job_status()" function
7036 */
7037 static void
7038f_job_status(typval_T *argvars, typval_T *rettv)
7039{
7040 job_T *job = get_job_arg(&argvars[0]);
7041
7042 if (job != NULL)
7043 {
7044 rettv->v_type = VAR_STRING;
7045 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7046 }
7047}
7048
7049/*
7050 * "job_stop()" function
7051 */
7052 static void
7053f_job_stop(typval_T *argvars, typval_T *rettv)
7054{
7055 job_T *job = get_job_arg(&argvars[0]);
7056
7057 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007058 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007059}
7060#endif
7061
7062/*
7063 * "join()" function
7064 */
7065 static void
7066f_join(typval_T *argvars, typval_T *rettv)
7067{
7068 garray_T ga;
7069 char_u *sep;
7070
7071 if (argvars[0].v_type != VAR_LIST)
7072 {
7073 EMSG(_(e_listreq));
7074 return;
7075 }
7076 if (argvars[0].vval.v_list == NULL)
7077 return;
7078 if (argvars[1].v_type == VAR_UNKNOWN)
7079 sep = (char_u *)" ";
7080 else
7081 sep = get_tv_string_chk(&argvars[1]);
7082
7083 rettv->v_type = VAR_STRING;
7084
7085 if (sep != NULL)
7086 {
7087 ga_init2(&ga, (int)sizeof(char), 80);
7088 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7089 ga_append(&ga, NUL);
7090 rettv->vval.v_string = (char_u *)ga.ga_data;
7091 }
7092 else
7093 rettv->vval.v_string = NULL;
7094}
7095
7096/*
7097 * "js_decode()" function
7098 */
7099 static void
7100f_js_decode(typval_T *argvars, typval_T *rettv)
7101{
7102 js_read_T reader;
7103
7104 reader.js_buf = get_tv_string(&argvars[0]);
7105 reader.js_fill = NULL;
7106 reader.js_used = 0;
7107 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
7108 EMSG(_(e_invarg));
7109}
7110
7111/*
7112 * "js_encode()" function
7113 */
7114 static void
7115f_js_encode(typval_T *argvars, typval_T *rettv)
7116{
7117 rettv->v_type = VAR_STRING;
7118 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7119}
7120
7121/*
7122 * "json_decode()" function
7123 */
7124 static void
7125f_json_decode(typval_T *argvars, typval_T *rettv)
7126{
7127 js_read_T reader;
7128
7129 reader.js_buf = get_tv_string(&argvars[0]);
7130 reader.js_fill = NULL;
7131 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007132 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007133}
7134
7135/*
7136 * "json_encode()" function
7137 */
7138 static void
7139f_json_encode(typval_T *argvars, typval_T *rettv)
7140{
7141 rettv->v_type = VAR_STRING;
7142 rettv->vval.v_string = json_encode(&argvars[0], 0);
7143}
7144
7145/*
7146 * "keys()" function
7147 */
7148 static void
7149f_keys(typval_T *argvars, typval_T *rettv)
7150{
7151 dict_list(argvars, rettv, 0);
7152}
7153
7154/*
7155 * "last_buffer_nr()" function.
7156 */
7157 static void
7158f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7159{
7160 int n = 0;
7161 buf_T *buf;
7162
Bram Moolenaar29323592016-07-24 22:04:11 +02007163 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007164 if (n < buf->b_fnum)
7165 n = buf->b_fnum;
7166
7167 rettv->vval.v_number = n;
7168}
7169
7170/*
7171 * "len()" function
7172 */
7173 static void
7174f_len(typval_T *argvars, typval_T *rettv)
7175{
7176 switch (argvars[0].v_type)
7177 {
7178 case VAR_STRING:
7179 case VAR_NUMBER:
7180 rettv->vval.v_number = (varnumber_T)STRLEN(
7181 get_tv_string(&argvars[0]));
7182 break;
7183 case VAR_LIST:
7184 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7185 break;
7186 case VAR_DICT:
7187 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7188 break;
7189 case VAR_UNKNOWN:
7190 case VAR_SPECIAL:
7191 case VAR_FLOAT:
7192 case VAR_FUNC:
7193 case VAR_PARTIAL:
7194 case VAR_JOB:
7195 case VAR_CHANNEL:
7196 EMSG(_("E701: Invalid type for len()"));
7197 break;
7198 }
7199}
7200
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007201 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007202libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007203{
7204#ifdef FEAT_LIBCALL
7205 char_u *string_in;
7206 char_u **string_result;
7207 int nr_result;
7208#endif
7209
7210 rettv->v_type = type;
7211 if (type != VAR_NUMBER)
7212 rettv->vval.v_string = NULL;
7213
7214 if (check_restricted() || check_secure())
7215 return;
7216
7217#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007218 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007219 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7220 {
7221 string_in = NULL;
7222 if (argvars[2].v_type == VAR_STRING)
7223 string_in = argvars[2].vval.v_string;
7224 if (type == VAR_NUMBER)
7225 string_result = NULL;
7226 else
7227 string_result = &rettv->vval.v_string;
7228 if (mch_libcall(argvars[0].vval.v_string,
7229 argvars[1].vval.v_string,
7230 string_in,
7231 argvars[2].vval.v_number,
7232 string_result,
7233 &nr_result) == OK
7234 && type == VAR_NUMBER)
7235 rettv->vval.v_number = nr_result;
7236 }
7237#endif
7238}
7239
7240/*
7241 * "libcall()" function
7242 */
7243 static void
7244f_libcall(typval_T *argvars, typval_T *rettv)
7245{
7246 libcall_common(argvars, rettv, VAR_STRING);
7247}
7248
7249/*
7250 * "libcallnr()" function
7251 */
7252 static void
7253f_libcallnr(typval_T *argvars, typval_T *rettv)
7254{
7255 libcall_common(argvars, rettv, VAR_NUMBER);
7256}
7257
7258/*
7259 * "line(string)" function
7260 */
7261 static void
7262f_line(typval_T *argvars, typval_T *rettv)
7263{
7264 linenr_T lnum = 0;
7265 pos_T *fp;
7266 int fnum;
7267
7268 fp = var2fpos(&argvars[0], TRUE, &fnum);
7269 if (fp != NULL)
7270 lnum = fp->lnum;
7271 rettv->vval.v_number = lnum;
7272}
7273
7274/*
7275 * "line2byte(lnum)" function
7276 */
7277 static void
7278f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7279{
7280#ifndef FEAT_BYTEOFF
7281 rettv->vval.v_number = -1;
7282#else
7283 linenr_T lnum;
7284
7285 lnum = get_tv_lnum(argvars);
7286 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7287 rettv->vval.v_number = -1;
7288 else
7289 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7290 if (rettv->vval.v_number >= 0)
7291 ++rettv->vval.v_number;
7292#endif
7293}
7294
7295/*
7296 * "lispindent(lnum)" function
7297 */
7298 static void
7299f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7300{
7301#ifdef FEAT_LISP
7302 pos_T pos;
7303 linenr_T lnum;
7304
7305 pos = curwin->w_cursor;
7306 lnum = get_tv_lnum(argvars);
7307 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7308 {
7309 curwin->w_cursor.lnum = lnum;
7310 rettv->vval.v_number = get_lisp_indent();
7311 curwin->w_cursor = pos;
7312 }
7313 else
7314#endif
7315 rettv->vval.v_number = -1;
7316}
7317
7318/*
7319 * "localtime()" function
7320 */
7321 static void
7322f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7323{
7324 rettv->vval.v_number = (varnumber_T)time(NULL);
7325}
7326
7327static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
7328
7329 static void
7330get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7331{
7332 char_u *keys;
7333 char_u *which;
7334 char_u buf[NUMBUFLEN];
7335 char_u *keys_buf = NULL;
7336 char_u *rhs;
7337 int mode;
7338 int abbr = FALSE;
7339 int get_dict = FALSE;
7340 mapblock_T *mp;
7341 int buffer_local;
7342
7343 /* return empty string for failure */
7344 rettv->v_type = VAR_STRING;
7345 rettv->vval.v_string = NULL;
7346
7347 keys = get_tv_string(&argvars[0]);
7348 if (*keys == NUL)
7349 return;
7350
7351 if (argvars[1].v_type != VAR_UNKNOWN)
7352 {
7353 which = get_tv_string_buf_chk(&argvars[1], buf);
7354 if (argvars[2].v_type != VAR_UNKNOWN)
7355 {
7356 abbr = (int)get_tv_number(&argvars[2]);
7357 if (argvars[3].v_type != VAR_UNKNOWN)
7358 get_dict = (int)get_tv_number(&argvars[3]);
7359 }
7360 }
7361 else
7362 which = (char_u *)"";
7363 if (which == NULL)
7364 return;
7365
7366 mode = get_map_mode(&which, 0);
7367
7368 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7369 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7370 vim_free(keys_buf);
7371
7372 if (!get_dict)
7373 {
7374 /* Return a string. */
7375 if (rhs != NULL)
7376 rettv->vval.v_string = str2special_save(rhs, FALSE);
7377
7378 }
7379 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7380 {
7381 /* Return a dictionary. */
7382 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7383 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7384 dict_T *dict = rettv->vval.v_dict;
7385
7386 dict_add_nr_str(dict, "lhs", 0L, lhs);
7387 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
7388 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
7389 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
7390 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
7391 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
7392 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
7393 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
7394 dict_add_nr_str(dict, "mode", 0L, mapmode);
7395
7396 vim_free(lhs);
7397 vim_free(mapmode);
7398 }
7399}
7400
7401#ifdef FEAT_FLOAT
7402/*
7403 * "log()" function
7404 */
7405 static void
7406f_log(typval_T *argvars, typval_T *rettv)
7407{
7408 float_T f = 0.0;
7409
7410 rettv->v_type = VAR_FLOAT;
7411 if (get_float_arg(argvars, &f) == OK)
7412 rettv->vval.v_float = log(f);
7413 else
7414 rettv->vval.v_float = 0.0;
7415}
7416
7417/*
7418 * "log10()" function
7419 */
7420 static void
7421f_log10(typval_T *argvars, typval_T *rettv)
7422{
7423 float_T f = 0.0;
7424
7425 rettv->v_type = VAR_FLOAT;
7426 if (get_float_arg(argvars, &f) == OK)
7427 rettv->vval.v_float = log10(f);
7428 else
7429 rettv->vval.v_float = 0.0;
7430}
7431#endif
7432
7433#ifdef FEAT_LUA
7434/*
7435 * "luaeval()" function
7436 */
7437 static void
7438f_luaeval(typval_T *argvars, typval_T *rettv)
7439{
7440 char_u *str;
7441 char_u buf[NUMBUFLEN];
7442
7443 str = get_tv_string_buf(&argvars[0], buf);
7444 do_luaeval(str, argvars + 1, rettv);
7445}
7446#endif
7447
7448/*
7449 * "map()" function
7450 */
7451 static void
7452f_map(typval_T *argvars, typval_T *rettv)
7453{
7454 filter_map(argvars, rettv, TRUE);
7455}
7456
7457/*
7458 * "maparg()" function
7459 */
7460 static void
7461f_maparg(typval_T *argvars, typval_T *rettv)
7462{
7463 get_maparg(argvars, rettv, TRUE);
7464}
7465
7466/*
7467 * "mapcheck()" function
7468 */
7469 static void
7470f_mapcheck(typval_T *argvars, typval_T *rettv)
7471{
7472 get_maparg(argvars, rettv, FALSE);
7473}
7474
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007475typedef enum
7476{
7477 MATCH_END, /* matchend() */
7478 MATCH_MATCH, /* match() */
7479 MATCH_STR, /* matchstr() */
7480 MATCH_LIST, /* matchlist() */
7481 MATCH_POS /* matchstrpos() */
7482} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007483
7484 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007485find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007486{
7487 char_u *str = NULL;
7488 long len = 0;
7489 char_u *expr = NULL;
7490 char_u *pat;
7491 regmatch_T regmatch;
7492 char_u patbuf[NUMBUFLEN];
7493 char_u strbuf[NUMBUFLEN];
7494 char_u *save_cpo;
7495 long start = 0;
7496 long nth = 1;
7497 colnr_T startcol = 0;
7498 int match = 0;
7499 list_T *l = NULL;
7500 listitem_T *li = NULL;
7501 long idx = 0;
7502 char_u *tofree = NULL;
7503
7504 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7505 save_cpo = p_cpo;
7506 p_cpo = (char_u *)"";
7507
7508 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007509 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007510 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007511 /* type MATCH_LIST: return empty list when there are no matches.
7512 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007513 if (rettv_list_alloc(rettv) == FAIL)
7514 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007515 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007516 && (list_append_string(rettv->vval.v_list,
7517 (char_u *)"", 0) == FAIL
7518 || list_append_number(rettv->vval.v_list,
7519 (varnumber_T)-1) == FAIL
7520 || list_append_number(rettv->vval.v_list,
7521 (varnumber_T)-1) == FAIL
7522 || list_append_number(rettv->vval.v_list,
7523 (varnumber_T)-1) == FAIL))
7524 {
7525 list_free(rettv->vval.v_list);
7526 rettv->vval.v_list = NULL;
7527 goto theend;
7528 }
7529 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007530 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007531 {
7532 rettv->v_type = VAR_STRING;
7533 rettv->vval.v_string = NULL;
7534 }
7535
7536 if (argvars[0].v_type == VAR_LIST)
7537 {
7538 if ((l = argvars[0].vval.v_list) == NULL)
7539 goto theend;
7540 li = l->lv_first;
7541 }
7542 else
7543 {
7544 expr = str = get_tv_string(&argvars[0]);
7545 len = (long)STRLEN(str);
7546 }
7547
7548 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
7549 if (pat == NULL)
7550 goto theend;
7551
7552 if (argvars[2].v_type != VAR_UNKNOWN)
7553 {
7554 int error = FALSE;
7555
7556 start = (long)get_tv_number_chk(&argvars[2], &error);
7557 if (error)
7558 goto theend;
7559 if (l != NULL)
7560 {
7561 li = list_find(l, start);
7562 if (li == NULL)
7563 goto theend;
7564 idx = l->lv_idx; /* use the cached index */
7565 }
7566 else
7567 {
7568 if (start < 0)
7569 start = 0;
7570 if (start > len)
7571 goto theend;
7572 /* When "count" argument is there ignore matches before "start",
7573 * otherwise skip part of the string. Differs when pattern is "^"
7574 * or "\<". */
7575 if (argvars[3].v_type != VAR_UNKNOWN)
7576 startcol = start;
7577 else
7578 {
7579 str += start;
7580 len -= start;
7581 }
7582 }
7583
7584 if (argvars[3].v_type != VAR_UNKNOWN)
7585 nth = (long)get_tv_number_chk(&argvars[3], &error);
7586 if (error)
7587 goto theend;
7588 }
7589
7590 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7591 if (regmatch.regprog != NULL)
7592 {
7593 regmatch.rm_ic = p_ic;
7594
7595 for (;;)
7596 {
7597 if (l != NULL)
7598 {
7599 if (li == NULL)
7600 {
7601 match = FALSE;
7602 break;
7603 }
7604 vim_free(tofree);
7605 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7606 if (str == NULL)
7607 break;
7608 }
7609
7610 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7611
7612 if (match && --nth <= 0)
7613 break;
7614 if (l == NULL && !match)
7615 break;
7616
7617 /* Advance to just after the match. */
7618 if (l != NULL)
7619 {
7620 li = li->li_next;
7621 ++idx;
7622 }
7623 else
7624 {
7625#ifdef FEAT_MBYTE
7626 startcol = (colnr_T)(regmatch.startp[0]
7627 + (*mb_ptr2len)(regmatch.startp[0]) - str);
7628#else
7629 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
7630#endif
7631 if (startcol > (colnr_T)len
7632 || str + startcol <= regmatch.startp[0])
7633 {
7634 match = FALSE;
7635 break;
7636 }
7637 }
7638 }
7639
7640 if (match)
7641 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007642 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007643 {
7644 listitem_T *li1 = rettv->vval.v_list->lv_first;
7645 listitem_T *li2 = li1->li_next;
7646 listitem_T *li3 = li2->li_next;
7647 listitem_T *li4 = li3->li_next;
7648
7649 vim_free(li1->li_tv.vval.v_string);
7650 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7651 (int)(regmatch.endp[0] - regmatch.startp[0]));
7652 li3->li_tv.vval.v_number =
7653 (varnumber_T)(regmatch.startp[0] - expr);
7654 li4->li_tv.vval.v_number =
7655 (varnumber_T)(regmatch.endp[0] - expr);
7656 if (l != NULL)
7657 li2->li_tv.vval.v_number = (varnumber_T)idx;
7658 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007659 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007660 {
7661 int i;
7662
7663 /* return list with matched string and submatches */
7664 for (i = 0; i < NSUBEXP; ++i)
7665 {
7666 if (regmatch.endp[i] == NULL)
7667 {
7668 if (list_append_string(rettv->vval.v_list,
7669 (char_u *)"", 0) == FAIL)
7670 break;
7671 }
7672 else if (list_append_string(rettv->vval.v_list,
7673 regmatch.startp[i],
7674 (int)(regmatch.endp[i] - regmatch.startp[i]))
7675 == FAIL)
7676 break;
7677 }
7678 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007679 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007680 {
7681 /* return matched string */
7682 if (l != NULL)
7683 copy_tv(&li->li_tv, rettv);
7684 else
7685 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
7686 (int)(regmatch.endp[0] - regmatch.startp[0]));
7687 }
7688 else if (l != NULL)
7689 rettv->vval.v_number = idx;
7690 else
7691 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007692 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007693 rettv->vval.v_number =
7694 (varnumber_T)(regmatch.startp[0] - str);
7695 else
7696 rettv->vval.v_number =
7697 (varnumber_T)(regmatch.endp[0] - str);
7698 rettv->vval.v_number += (varnumber_T)(str - expr);
7699 }
7700 }
7701 vim_regfree(regmatch.regprog);
7702 }
7703
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007704theend:
7705 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007706 /* matchstrpos() without a list: drop the second item. */
7707 listitem_remove(rettv->vval.v_list,
7708 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007709 vim_free(tofree);
7710 p_cpo = save_cpo;
7711}
7712
7713/*
7714 * "match()" function
7715 */
7716 static void
7717f_match(typval_T *argvars, typval_T *rettv)
7718{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007719 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007720}
7721
7722/*
7723 * "matchadd()" function
7724 */
7725 static void
7726f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7727{
7728#ifdef FEAT_SEARCH_EXTRA
7729 char_u buf[NUMBUFLEN];
7730 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
7731 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
7732 int prio = 10; /* default priority */
7733 int id = -1;
7734 int error = FALSE;
7735 char_u *conceal_char = NULL;
7736
7737 rettv->vval.v_number = -1;
7738
7739 if (grp == NULL || pat == NULL)
7740 return;
7741 if (argvars[2].v_type != VAR_UNKNOWN)
7742 {
7743 prio = (int)get_tv_number_chk(&argvars[2], &error);
7744 if (argvars[3].v_type != VAR_UNKNOWN)
7745 {
7746 id = (int)get_tv_number_chk(&argvars[3], &error);
7747 if (argvars[4].v_type != VAR_UNKNOWN)
7748 {
7749 if (argvars[4].v_type != VAR_DICT)
7750 {
7751 EMSG(_(e_dictreq));
7752 return;
7753 }
7754 if (dict_find(argvars[4].vval.v_dict,
7755 (char_u *)"conceal", -1) != NULL)
7756 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7757 (char_u *)"conceal", FALSE);
7758 }
7759 }
7760 }
7761 if (error == TRUE)
7762 return;
7763 if (id >= 1 && id <= 3)
7764 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007765 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007766 return;
7767 }
7768
7769 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
7770 conceal_char);
7771#endif
7772}
7773
7774/*
7775 * "matchaddpos()" function
7776 */
7777 static void
7778f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7779{
7780#ifdef FEAT_SEARCH_EXTRA
7781 char_u buf[NUMBUFLEN];
7782 char_u *group;
7783 int prio = 10;
7784 int id = -1;
7785 int error = FALSE;
7786 list_T *l;
7787 char_u *conceal_char = NULL;
7788
7789 rettv->vval.v_number = -1;
7790
7791 group = get_tv_string_buf_chk(&argvars[0], buf);
7792 if (group == NULL)
7793 return;
7794
7795 if (argvars[1].v_type != VAR_LIST)
7796 {
7797 EMSG2(_(e_listarg), "matchaddpos()");
7798 return;
7799 }
7800 l = argvars[1].vval.v_list;
7801 if (l == NULL)
7802 return;
7803
7804 if (argvars[2].v_type != VAR_UNKNOWN)
7805 {
7806 prio = (int)get_tv_number_chk(&argvars[2], &error);
7807 if (argvars[3].v_type != VAR_UNKNOWN)
7808 {
7809 id = (int)get_tv_number_chk(&argvars[3], &error);
7810 if (argvars[4].v_type != VAR_UNKNOWN)
7811 {
7812 if (argvars[4].v_type != VAR_DICT)
7813 {
7814 EMSG(_(e_dictreq));
7815 return;
7816 }
7817 if (dict_find(argvars[4].vval.v_dict,
7818 (char_u *)"conceal", -1) != NULL)
7819 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7820 (char_u *)"conceal", FALSE);
7821 }
7822 }
7823 }
7824 if (error == TRUE)
7825 return;
7826
7827 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
7828 if (id == 1 || id == 2)
7829 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007830 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007831 return;
7832 }
7833
7834 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
7835 conceal_char);
7836#endif
7837}
7838
7839/*
7840 * "matcharg()" function
7841 */
7842 static void
7843f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
7844{
7845 if (rettv_list_alloc(rettv) == OK)
7846 {
7847#ifdef FEAT_SEARCH_EXTRA
7848 int id = (int)get_tv_number(&argvars[0]);
7849 matchitem_T *m;
7850
7851 if (id >= 1 && id <= 3)
7852 {
7853 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
7854 {
7855 list_append_string(rettv->vval.v_list,
7856 syn_id2name(m->hlg_id), -1);
7857 list_append_string(rettv->vval.v_list, m->pattern, -1);
7858 }
7859 else
7860 {
7861 list_append_string(rettv->vval.v_list, NULL, -1);
7862 list_append_string(rettv->vval.v_list, NULL, -1);
7863 }
7864 }
7865#endif
7866 }
7867}
7868
7869/*
7870 * "matchdelete()" function
7871 */
7872 static void
7873f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7874{
7875#ifdef FEAT_SEARCH_EXTRA
7876 rettv->vval.v_number = match_delete(curwin,
7877 (int)get_tv_number(&argvars[0]), TRUE);
7878#endif
7879}
7880
7881/*
7882 * "matchend()" function
7883 */
7884 static void
7885f_matchend(typval_T *argvars, typval_T *rettv)
7886{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007887 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007888}
7889
7890/*
7891 * "matchlist()" function
7892 */
7893 static void
7894f_matchlist(typval_T *argvars, typval_T *rettv)
7895{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007896 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007897}
7898
7899/*
7900 * "matchstr()" function
7901 */
7902 static void
7903f_matchstr(typval_T *argvars, typval_T *rettv)
7904{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007905 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007906}
7907
7908/*
7909 * "matchstrpos()" function
7910 */
7911 static void
7912f_matchstrpos(typval_T *argvars, typval_T *rettv)
7913{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007914 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007915}
7916
7917static void max_min(typval_T *argvars, typval_T *rettv, int domax);
7918
7919 static void
7920max_min(typval_T *argvars, typval_T *rettv, int domax)
7921{
7922 varnumber_T n = 0;
7923 varnumber_T i;
7924 int error = FALSE;
7925
7926 if (argvars[0].v_type == VAR_LIST)
7927 {
7928 list_T *l;
7929 listitem_T *li;
7930
7931 l = argvars[0].vval.v_list;
7932 if (l != NULL)
7933 {
7934 li = l->lv_first;
7935 if (li != NULL)
7936 {
7937 n = get_tv_number_chk(&li->li_tv, &error);
7938 for (;;)
7939 {
7940 li = li->li_next;
7941 if (li == NULL)
7942 break;
7943 i = get_tv_number_chk(&li->li_tv, &error);
7944 if (domax ? i > n : i < n)
7945 n = i;
7946 }
7947 }
7948 }
7949 }
7950 else if (argvars[0].v_type == VAR_DICT)
7951 {
7952 dict_T *d;
7953 int first = TRUE;
7954 hashitem_T *hi;
7955 int todo;
7956
7957 d = argvars[0].vval.v_dict;
7958 if (d != NULL)
7959 {
7960 todo = (int)d->dv_hashtab.ht_used;
7961 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
7962 {
7963 if (!HASHITEM_EMPTY(hi))
7964 {
7965 --todo;
7966 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
7967 if (first)
7968 {
7969 n = i;
7970 first = FALSE;
7971 }
7972 else if (domax ? i > n : i < n)
7973 n = i;
7974 }
7975 }
7976 }
7977 }
7978 else
Bram Moolenaar26b84332016-09-04 21:42:36 +02007979 EMSG2(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007980 rettv->vval.v_number = error ? 0 : n;
7981}
7982
7983/*
7984 * "max()" function
7985 */
7986 static void
7987f_max(typval_T *argvars, typval_T *rettv)
7988{
7989 max_min(argvars, rettv, TRUE);
7990}
7991
7992/*
7993 * "min()" function
7994 */
7995 static void
7996f_min(typval_T *argvars, typval_T *rettv)
7997{
7998 max_min(argvars, rettv, FALSE);
7999}
8000
8001static int mkdir_recurse(char_u *dir, int prot);
8002
8003/*
8004 * Create the directory in which "dir" is located, and higher levels when
8005 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008006 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008007 */
8008 static int
8009mkdir_recurse(char_u *dir, int prot)
8010{
8011 char_u *p;
8012 char_u *updir;
8013 int r = FAIL;
8014
8015 /* Get end of directory name in "dir".
8016 * We're done when it's "/" or "c:/". */
8017 p = gettail_sep(dir);
8018 if (p <= get_past_head(dir))
8019 return OK;
8020
8021 /* If the directory exists we're done. Otherwise: create it.*/
8022 updir = vim_strnsave(dir, (int)(p - dir));
8023 if (updir == NULL)
8024 return FAIL;
8025 if (mch_isdir(updir))
8026 r = OK;
8027 else if (mkdir_recurse(updir, prot) == OK)
8028 r = vim_mkdir_emsg(updir, prot);
8029 vim_free(updir);
8030 return r;
8031}
8032
8033#ifdef vim_mkdir
8034/*
8035 * "mkdir()" function
8036 */
8037 static void
8038f_mkdir(typval_T *argvars, typval_T *rettv)
8039{
8040 char_u *dir;
8041 char_u buf[NUMBUFLEN];
8042 int prot = 0755;
8043
8044 rettv->vval.v_number = FAIL;
8045 if (check_restricted() || check_secure())
8046 return;
8047
8048 dir = get_tv_string_buf(&argvars[0], buf);
8049 if (*dir == NUL)
8050 rettv->vval.v_number = FAIL;
8051 else
8052 {
8053 if (*gettail(dir) == NUL)
8054 /* remove trailing slashes */
8055 *gettail_sep(dir) = NUL;
8056
8057 if (argvars[1].v_type != VAR_UNKNOWN)
8058 {
8059 if (argvars[2].v_type != VAR_UNKNOWN)
8060 prot = (int)get_tv_number_chk(&argvars[2], NULL);
8061 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
8062 mkdir_recurse(dir, prot);
8063 }
8064 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
8065 }
8066}
8067#endif
8068
8069/*
8070 * "mode()" function
8071 */
8072 static void
8073f_mode(typval_T *argvars, typval_T *rettv)
8074{
8075 char_u buf[3];
8076
8077 buf[1] = NUL;
8078 buf[2] = NUL;
8079
8080 if (time_for_testing == 93784)
8081 {
8082 /* Testing the two-character code. */
8083 buf[0] = 'x';
8084 buf[1] = '!';
8085 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008086#ifdef FEAT_TERMINAL
8087 else if (term_use_loop())
8088 buf[0] = 't';
8089#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008090 else if (VIsual_active)
8091 {
8092 if (VIsual_select)
8093 buf[0] = VIsual_mode + 's' - 'v';
8094 else
8095 buf[0] = VIsual_mode;
8096 }
8097 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8098 || State == CONFIRM)
8099 {
8100 buf[0] = 'r';
8101 if (State == ASKMORE)
8102 buf[1] = 'm';
8103 else if (State == CONFIRM)
8104 buf[1] = '?';
8105 }
8106 else if (State == EXTERNCMD)
8107 buf[0] = '!';
8108 else if (State & INSERT)
8109 {
8110#ifdef FEAT_VREPLACE
8111 if (State & VREPLACE_FLAG)
8112 {
8113 buf[0] = 'R';
8114 buf[1] = 'v';
8115 }
8116 else
8117#endif
Bram Moolenaare90858d2017-02-01 17:24:34 +01008118 {
8119 if (State & REPLACE_FLAG)
8120 buf[0] = 'R';
8121 else
8122 buf[0] = 'i';
8123#ifdef FEAT_INS_EXPAND
8124 if (ins_compl_active())
8125 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008126 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008127 buf[1] = 'x';
8128#endif
8129 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008130 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008131 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008132 {
8133 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008134 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008135 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008136 else if (exmode_active == EXMODE_NORMAL)
8137 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008138 }
8139 else
8140 {
8141 buf[0] = 'n';
8142 if (finish_op)
8143 buf[1] = 'o';
8144 }
8145
8146 /* Clear out the minor mode when the argument is not a non-zero number or
8147 * non-empty string. */
8148 if (!non_zero_arg(&argvars[0]))
8149 buf[1] = NUL;
8150
8151 rettv->vval.v_string = vim_strsave(buf);
8152 rettv->v_type = VAR_STRING;
8153}
8154
8155#if defined(FEAT_MZSCHEME) || defined(PROTO)
8156/*
8157 * "mzeval()" function
8158 */
8159 static void
8160f_mzeval(typval_T *argvars, typval_T *rettv)
8161{
8162 char_u *str;
8163 char_u buf[NUMBUFLEN];
8164
8165 str = get_tv_string_buf(&argvars[0], buf);
8166 do_mzeval(str, rettv);
8167}
8168
8169 void
8170mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8171{
8172 typval_T argvars[3];
8173
8174 argvars[0].v_type = VAR_STRING;
8175 argvars[0].vval.v_string = name;
8176 copy_tv(args, &argvars[1]);
8177 argvars[2].v_type = VAR_UNKNOWN;
8178 f_call(argvars, rettv);
8179 clear_tv(&argvars[1]);
8180}
8181#endif
8182
8183/*
8184 * "nextnonblank()" function
8185 */
8186 static void
8187f_nextnonblank(typval_T *argvars, typval_T *rettv)
8188{
8189 linenr_T lnum;
8190
8191 for (lnum = get_tv_lnum(argvars); ; ++lnum)
8192 {
8193 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8194 {
8195 lnum = 0;
8196 break;
8197 }
8198 if (*skipwhite(ml_get(lnum)) != NUL)
8199 break;
8200 }
8201 rettv->vval.v_number = lnum;
8202}
8203
8204/*
8205 * "nr2char()" function
8206 */
8207 static void
8208f_nr2char(typval_T *argvars, typval_T *rettv)
8209{
8210 char_u buf[NUMBUFLEN];
8211
8212#ifdef FEAT_MBYTE
8213 if (has_mbyte)
8214 {
8215 int utf8 = 0;
8216
8217 if (argvars[1].v_type != VAR_UNKNOWN)
8218 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
8219 if (utf8)
8220 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8221 else
8222 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8223 }
8224 else
8225#endif
8226 {
8227 buf[0] = (char_u)get_tv_number(&argvars[0]);
8228 buf[1] = NUL;
8229 }
8230 rettv->v_type = VAR_STRING;
8231 rettv->vval.v_string = vim_strsave(buf);
8232}
8233
8234/*
8235 * "or(expr, expr)" function
8236 */
8237 static void
8238f_or(typval_T *argvars, typval_T *rettv)
8239{
8240 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8241 | get_tv_number_chk(&argvars[1], NULL);
8242}
8243
8244/*
8245 * "pathshorten()" function
8246 */
8247 static void
8248f_pathshorten(typval_T *argvars, typval_T *rettv)
8249{
8250 char_u *p;
8251
8252 rettv->v_type = VAR_STRING;
8253 p = get_tv_string_chk(&argvars[0]);
8254 if (p == NULL)
8255 rettv->vval.v_string = NULL;
8256 else
8257 {
8258 p = vim_strsave(p);
8259 rettv->vval.v_string = p;
8260 if (p != NULL)
8261 shorten_dir(p);
8262 }
8263}
8264
8265#ifdef FEAT_PERL
8266/*
8267 * "perleval()" function
8268 */
8269 static void
8270f_perleval(typval_T *argvars, typval_T *rettv)
8271{
8272 char_u *str;
8273 char_u buf[NUMBUFLEN];
8274
8275 str = get_tv_string_buf(&argvars[0], buf);
8276 do_perleval(str, rettv);
8277}
8278#endif
8279
8280#ifdef FEAT_FLOAT
8281/*
8282 * "pow()" function
8283 */
8284 static void
8285f_pow(typval_T *argvars, typval_T *rettv)
8286{
8287 float_T fx = 0.0, fy = 0.0;
8288
8289 rettv->v_type = VAR_FLOAT;
8290 if (get_float_arg(argvars, &fx) == OK
8291 && get_float_arg(&argvars[1], &fy) == OK)
8292 rettv->vval.v_float = pow(fx, fy);
8293 else
8294 rettv->vval.v_float = 0.0;
8295}
8296#endif
8297
8298/*
8299 * "prevnonblank()" function
8300 */
8301 static void
8302f_prevnonblank(typval_T *argvars, typval_T *rettv)
8303{
8304 linenr_T lnum;
8305
8306 lnum = get_tv_lnum(argvars);
8307 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8308 lnum = 0;
8309 else
8310 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8311 --lnum;
8312 rettv->vval.v_number = lnum;
8313}
8314
8315/* This dummy va_list is here because:
8316 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8317 * - locally in the function results in a "used before set" warning
8318 * - using va_start() to initialize it gives "function with fixed args" error */
8319static va_list ap;
8320
8321/*
8322 * "printf()" function
8323 */
8324 static void
8325f_printf(typval_T *argvars, typval_T *rettv)
8326{
8327 char_u buf[NUMBUFLEN];
8328 int len;
8329 char_u *s;
8330 int saved_did_emsg = did_emsg;
8331 char *fmt;
8332
8333 rettv->v_type = VAR_STRING;
8334 rettv->vval.v_string = NULL;
8335
8336 /* Get the required length, allocate the buffer and do it for real. */
8337 did_emsg = FALSE;
8338 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008339 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008340 if (!did_emsg)
8341 {
8342 s = alloc(len + 1);
8343 if (s != NULL)
8344 {
8345 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008346 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8347 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008348 }
8349 }
8350 did_emsg |= saved_did_emsg;
8351}
8352
8353/*
8354 * "pumvisible()" function
8355 */
8356 static void
8357f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8358{
8359#ifdef FEAT_INS_EXPAND
8360 if (pum_visible())
8361 rettv->vval.v_number = 1;
8362#endif
8363}
8364
8365#ifdef FEAT_PYTHON3
8366/*
8367 * "py3eval()" function
8368 */
8369 static void
8370f_py3eval(typval_T *argvars, typval_T *rettv)
8371{
8372 char_u *str;
8373 char_u buf[NUMBUFLEN];
8374
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008375 if (p_pyx == 0)
8376 p_pyx = 3;
8377
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008378 str = get_tv_string_buf(&argvars[0], buf);
8379 do_py3eval(str, rettv);
8380}
8381#endif
8382
8383#ifdef FEAT_PYTHON
8384/*
8385 * "pyeval()" function
8386 */
8387 static void
8388f_pyeval(typval_T *argvars, typval_T *rettv)
8389{
8390 char_u *str;
8391 char_u buf[NUMBUFLEN];
8392
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008393 if (p_pyx == 0)
8394 p_pyx = 2;
8395
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008396 str = get_tv_string_buf(&argvars[0], buf);
8397 do_pyeval(str, rettv);
8398}
8399#endif
8400
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008401#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8402/*
8403 * "pyxeval()" function
8404 */
8405 static void
8406f_pyxeval(typval_T *argvars, typval_T *rettv)
8407{
8408# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8409 init_pyxversion();
8410 if (p_pyx == 2)
8411 f_pyeval(argvars, rettv);
8412 else
8413 f_py3eval(argvars, rettv);
8414# elif defined(FEAT_PYTHON)
8415 f_pyeval(argvars, rettv);
8416# elif defined(FEAT_PYTHON3)
8417 f_py3eval(argvars, rettv);
8418# endif
8419}
8420#endif
8421
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008422/*
8423 * "range()" function
8424 */
8425 static void
8426f_range(typval_T *argvars, typval_T *rettv)
8427{
8428 varnumber_T start;
8429 varnumber_T end;
8430 varnumber_T stride = 1;
8431 varnumber_T i;
8432 int error = FALSE;
8433
8434 start = get_tv_number_chk(&argvars[0], &error);
8435 if (argvars[1].v_type == VAR_UNKNOWN)
8436 {
8437 end = start - 1;
8438 start = 0;
8439 }
8440 else
8441 {
8442 end = get_tv_number_chk(&argvars[1], &error);
8443 if (argvars[2].v_type != VAR_UNKNOWN)
8444 stride = get_tv_number_chk(&argvars[2], &error);
8445 }
8446
8447 if (error)
8448 return; /* type error; errmsg already given */
8449 if (stride == 0)
8450 EMSG(_("E726: Stride is zero"));
8451 else if (stride > 0 ? end + 1 < start : end - 1 > start)
8452 EMSG(_("E727: Start past end"));
8453 else
8454 {
8455 if (rettv_list_alloc(rettv) == OK)
8456 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8457 if (list_append_number(rettv->vval.v_list,
8458 (varnumber_T)i) == FAIL)
8459 break;
8460 }
8461}
8462
8463/*
8464 * "readfile()" function
8465 */
8466 static void
8467f_readfile(typval_T *argvars, typval_T *rettv)
8468{
8469 int binary = FALSE;
8470 int failed = FALSE;
8471 char_u *fname;
8472 FILE *fd;
8473 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8474 int io_size = sizeof(buf);
8475 int readlen; /* size of last fread() */
8476 char_u *prev = NULL; /* previously read bytes, if any */
8477 long prevlen = 0; /* length of data in prev */
8478 long prevsize = 0; /* size of prev buffer */
8479 long maxline = MAXLNUM;
8480 long cnt = 0;
8481 char_u *p; /* position in buf */
8482 char_u *start; /* start of current line */
8483
8484 if (argvars[1].v_type != VAR_UNKNOWN)
8485 {
8486 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
8487 binary = TRUE;
8488 if (argvars[2].v_type != VAR_UNKNOWN)
8489 maxline = (long)get_tv_number(&argvars[2]);
8490 }
8491
8492 if (rettv_list_alloc(rettv) == FAIL)
8493 return;
8494
8495 /* Always open the file in binary mode, library functions have a mind of
8496 * their own about CR-LF conversion. */
8497 fname = get_tv_string(&argvars[0]);
8498 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8499 {
8500 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
8501 return;
8502 }
8503
8504 while (cnt < maxline || maxline < 0)
8505 {
8506 readlen = (int)fread(buf, 1, io_size, fd);
8507
8508 /* This for loop processes what was read, but is also entered at end
8509 * of file so that either:
8510 * - an incomplete line gets written
8511 * - a "binary" file gets an empty line at the end if it ends in a
8512 * newline. */
8513 for (p = buf, start = buf;
8514 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8515 ++p)
8516 {
8517 if (*p == '\n' || readlen <= 0)
8518 {
8519 listitem_T *li;
8520 char_u *s = NULL;
8521 long_u len = p - start;
8522
8523 /* Finished a line. Remove CRs before NL. */
8524 if (readlen > 0 && !binary)
8525 {
8526 while (len > 0 && start[len - 1] == '\r')
8527 --len;
8528 /* removal may cross back to the "prev" string */
8529 if (len == 0)
8530 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8531 --prevlen;
8532 }
8533 if (prevlen == 0)
8534 s = vim_strnsave(start, (int)len);
8535 else
8536 {
8537 /* Change "prev" buffer to be the right size. This way
8538 * the bytes are only copied once, and very long lines are
8539 * allocated only once. */
8540 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8541 {
8542 mch_memmove(s + prevlen, start, len);
8543 s[prevlen + len] = NUL;
8544 prev = NULL; /* the list will own the string */
8545 prevlen = prevsize = 0;
8546 }
8547 }
8548 if (s == NULL)
8549 {
8550 do_outofmem_msg((long_u) prevlen + len + 1);
8551 failed = TRUE;
8552 break;
8553 }
8554
8555 if ((li = listitem_alloc()) == NULL)
8556 {
8557 vim_free(s);
8558 failed = TRUE;
8559 break;
8560 }
8561 li->li_tv.v_type = VAR_STRING;
8562 li->li_tv.v_lock = 0;
8563 li->li_tv.vval.v_string = s;
8564 list_append(rettv->vval.v_list, li);
8565
8566 start = p + 1; /* step over newline */
8567 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
8568 break;
8569 }
8570 else if (*p == NUL)
8571 *p = '\n';
8572#ifdef FEAT_MBYTE
8573 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
8574 * when finding the BF and check the previous two bytes. */
8575 else if (*p == 0xbf && enc_utf8 && !binary)
8576 {
8577 /* Find the two bytes before the 0xbf. If p is at buf, or buf
8578 * + 1, these may be in the "prev" string. */
8579 char_u back1 = p >= buf + 1 ? p[-1]
8580 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
8581 char_u back2 = p >= buf + 2 ? p[-2]
8582 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
8583 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
8584
8585 if (back2 == 0xef && back1 == 0xbb)
8586 {
8587 char_u *dest = p - 2;
8588
8589 /* Usually a BOM is at the beginning of a file, and so at
8590 * the beginning of a line; then we can just step over it.
8591 */
8592 if (start == dest)
8593 start = p + 1;
8594 else
8595 {
8596 /* have to shuffle buf to close gap */
8597 int adjust_prevlen = 0;
8598
8599 if (dest < buf)
8600 {
8601 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
8602 dest = buf;
8603 }
8604 if (readlen > p - buf + 1)
8605 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
8606 readlen -= 3 - adjust_prevlen;
8607 prevlen -= adjust_prevlen;
8608 p = dest - 1;
8609 }
8610 }
8611 }
8612#endif
8613 } /* for */
8614
8615 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
8616 break;
8617 if (start < p)
8618 {
8619 /* There's part of a line in buf, store it in "prev". */
8620 if (p - start + prevlen >= prevsize)
8621 {
8622 /* need bigger "prev" buffer */
8623 char_u *newprev;
8624
8625 /* A common use case is ordinary text files and "prev" gets a
8626 * fragment of a line, so the first allocation is made
8627 * small, to avoid repeatedly 'allocing' large and
8628 * 'reallocing' small. */
8629 if (prevsize == 0)
8630 prevsize = (long)(p - start);
8631 else
8632 {
8633 long grow50pc = (prevsize * 3) / 2;
8634 long growmin = (long)((p - start) * 2 + prevlen);
8635 prevsize = grow50pc > growmin ? grow50pc : growmin;
8636 }
8637 newprev = prev == NULL ? alloc(prevsize)
8638 : vim_realloc(prev, prevsize);
8639 if (newprev == NULL)
8640 {
8641 do_outofmem_msg((long_u)prevsize);
8642 failed = TRUE;
8643 break;
8644 }
8645 prev = newprev;
8646 }
8647 /* Add the line part to end of "prev". */
8648 mch_memmove(prev + prevlen, start, p - start);
8649 prevlen += (long)(p - start);
8650 }
8651 } /* while */
8652
8653 /*
8654 * For a negative line count use only the lines at the end of the file,
8655 * free the rest.
8656 */
8657 if (!failed && maxline < 0)
8658 while (cnt > -maxline)
8659 {
8660 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
8661 --cnt;
8662 }
8663
8664 if (failed)
8665 {
8666 list_free(rettv->vval.v_list);
8667 /* readfile doc says an empty list is returned on error */
8668 rettv->vval.v_list = list_alloc();
8669 }
8670
8671 vim_free(prev);
8672 fclose(fd);
8673}
8674
8675#if defined(FEAT_RELTIME)
8676static int list2proftime(typval_T *arg, proftime_T *tm);
8677
8678/*
8679 * Convert a List to proftime_T.
8680 * Return FAIL when there is something wrong.
8681 */
8682 static int
8683list2proftime(typval_T *arg, proftime_T *tm)
8684{
8685 long n1, n2;
8686 int error = FALSE;
8687
8688 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
8689 || arg->vval.v_list->lv_len != 2)
8690 return FAIL;
8691 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
8692 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
8693# ifdef WIN3264
8694 tm->HighPart = n1;
8695 tm->LowPart = n2;
8696# else
8697 tm->tv_sec = n1;
8698 tm->tv_usec = n2;
8699# endif
8700 return error ? FAIL : OK;
8701}
8702#endif /* FEAT_RELTIME */
8703
8704/*
8705 * "reltime()" function
8706 */
8707 static void
8708f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8709{
8710#ifdef FEAT_RELTIME
8711 proftime_T res;
8712 proftime_T start;
8713
8714 if (argvars[0].v_type == VAR_UNKNOWN)
8715 {
8716 /* No arguments: get current time. */
8717 profile_start(&res);
8718 }
8719 else if (argvars[1].v_type == VAR_UNKNOWN)
8720 {
8721 if (list2proftime(&argvars[0], &res) == FAIL)
8722 return;
8723 profile_end(&res);
8724 }
8725 else
8726 {
8727 /* Two arguments: compute the difference. */
8728 if (list2proftime(&argvars[0], &start) == FAIL
8729 || list2proftime(&argvars[1], &res) == FAIL)
8730 return;
8731 profile_sub(&res, &start);
8732 }
8733
8734 if (rettv_list_alloc(rettv) == OK)
8735 {
8736 long n1, n2;
8737
8738# ifdef WIN3264
8739 n1 = res.HighPart;
8740 n2 = res.LowPart;
8741# else
8742 n1 = res.tv_sec;
8743 n2 = res.tv_usec;
8744# endif
8745 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
8746 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
8747 }
8748#endif
8749}
8750
8751#ifdef FEAT_FLOAT
8752/*
8753 * "reltimefloat()" function
8754 */
8755 static void
8756f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
8757{
8758# ifdef FEAT_RELTIME
8759 proftime_T tm;
8760# endif
8761
8762 rettv->v_type = VAR_FLOAT;
8763 rettv->vval.v_float = 0;
8764# ifdef FEAT_RELTIME
8765 if (list2proftime(&argvars[0], &tm) == OK)
8766 rettv->vval.v_float = profile_float(&tm);
8767# endif
8768}
8769#endif
8770
8771/*
8772 * "reltimestr()" function
8773 */
8774 static void
8775f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
8776{
8777#ifdef FEAT_RELTIME
8778 proftime_T tm;
8779#endif
8780
8781 rettv->v_type = VAR_STRING;
8782 rettv->vval.v_string = NULL;
8783#ifdef FEAT_RELTIME
8784 if (list2proftime(&argvars[0], &tm) == OK)
8785 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
8786#endif
8787}
8788
8789#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
8790static void make_connection(void);
8791static int check_connection(void);
8792
8793 static void
8794make_connection(void)
8795{
8796 if (X_DISPLAY == NULL
8797# ifdef FEAT_GUI
8798 && !gui.in_use
8799# endif
8800 )
8801 {
8802 x_force_connect = TRUE;
8803 setup_term_clip();
8804 x_force_connect = FALSE;
8805 }
8806}
8807
8808 static int
8809check_connection(void)
8810{
8811 make_connection();
8812 if (X_DISPLAY == NULL)
8813 {
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01008814 EMSG(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008815 return FAIL;
8816 }
8817 return OK;
8818}
8819#endif
8820
8821#ifdef FEAT_CLIENTSERVER
8822 static void
8823remote_common(typval_T *argvars, typval_T *rettv, int expr)
8824{
8825 char_u *server_name;
8826 char_u *keys;
8827 char_u *r = NULL;
8828 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008829 int timeout = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008830# ifdef WIN32
8831 HWND w;
8832# else
8833 Window w;
8834# endif
8835
8836 if (check_restricted() || check_secure())
8837 return;
8838
8839# ifdef FEAT_X11
8840 if (check_connection() == FAIL)
8841 return;
8842# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008843 if (argvars[2].v_type != VAR_UNKNOWN
8844 && argvars[3].v_type != VAR_UNKNOWN)
8845 timeout = get_tv_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008846
8847 server_name = get_tv_string_chk(&argvars[0]);
8848 if (server_name == NULL)
8849 return; /* type error; errmsg already given */
8850 keys = get_tv_string_buf(&argvars[1], buf);
8851# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008852 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008853# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008854 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
8855 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008856# endif
8857 {
8858 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008859 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008860 EMSG(r); /* sending worked but evaluation failed */
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008861 vim_free(r);
8862 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008863 else
8864 EMSG2(_("E241: Unable to send to %s"), server_name);
8865 return;
8866 }
8867
8868 rettv->vval.v_string = r;
8869
8870 if (argvars[2].v_type != VAR_UNKNOWN)
8871 {
8872 dictitem_T v;
8873 char_u str[30];
8874 char_u *idvar;
8875
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008876 idvar = get_tv_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008877 if (idvar != NULL && *idvar != NUL)
8878 {
8879 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
8880 v.di_tv.v_type = VAR_STRING;
8881 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008882 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008883 vim_free(v.di_tv.vval.v_string);
8884 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008885 }
8886}
8887#endif
8888
8889/*
8890 * "remote_expr()" function
8891 */
8892 static void
8893f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
8894{
8895 rettv->v_type = VAR_STRING;
8896 rettv->vval.v_string = NULL;
8897#ifdef FEAT_CLIENTSERVER
8898 remote_common(argvars, rettv, TRUE);
8899#endif
8900}
8901
8902/*
8903 * "remote_foreground()" function
8904 */
8905 static void
8906f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8907{
8908#ifdef FEAT_CLIENTSERVER
8909# ifdef WIN32
8910 /* On Win32 it's done in this application. */
8911 {
8912 char_u *server_name = get_tv_string_chk(&argvars[0]);
8913
8914 if (server_name != NULL)
8915 serverForeground(server_name);
8916 }
8917# else
8918 /* Send a foreground() expression to the server. */
8919 argvars[1].v_type = VAR_STRING;
8920 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
8921 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008922 rettv->v_type = VAR_STRING;
8923 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008924 remote_common(argvars, rettv, TRUE);
8925 vim_free(argvars[1].vval.v_string);
8926# endif
8927#endif
8928}
8929
8930 static void
8931f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
8932{
8933#ifdef FEAT_CLIENTSERVER
8934 dictitem_T v;
8935 char_u *s = NULL;
8936# ifdef WIN32
8937 long_u n = 0;
8938# endif
8939 char_u *serverid;
8940
8941 if (check_restricted() || check_secure())
8942 {
8943 rettv->vval.v_number = -1;
8944 return;
8945 }
8946 serverid = get_tv_string_chk(&argvars[0]);
8947 if (serverid == NULL)
8948 {
8949 rettv->vval.v_number = -1;
8950 return; /* type error; errmsg already given */
8951 }
8952# ifdef WIN32
8953 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
8954 if (n == 0)
8955 rettv->vval.v_number = -1;
8956 else
8957 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008958 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008959 rettv->vval.v_number = (s != NULL);
8960 }
8961# else
8962 if (check_connection() == FAIL)
8963 return;
8964
8965 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
8966 serverStrToWin(serverid), &s);
8967# endif
8968
8969 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
8970 {
8971 char_u *retvar;
8972
8973 v.di_tv.v_type = VAR_STRING;
8974 v.di_tv.vval.v_string = vim_strsave(s);
8975 retvar = get_tv_string_chk(&argvars[1]);
8976 if (retvar != NULL)
8977 set_var(retvar, &v.di_tv, FALSE);
8978 vim_free(v.di_tv.vval.v_string);
8979 }
8980#else
8981 rettv->vval.v_number = -1;
8982#endif
8983}
8984
8985 static void
8986f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
8987{
8988 char_u *r = NULL;
8989
8990#ifdef FEAT_CLIENTSERVER
8991 char_u *serverid = get_tv_string_chk(&argvars[0]);
8992
8993 if (serverid != NULL && !check_restricted() && !check_secure())
8994 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008995 int timeout = 0;
Bram Moolenaar1662ce12017-03-19 21:47:50 +01008996# ifdef WIN32
8997 /* The server's HWND is encoded in the 'id' parameter */
8998 long_u n = 0;
8999# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009000
9001 if (argvars[1].v_type != VAR_UNKNOWN)
9002 timeout = get_tv_number(&argvars[1]);
9003
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009004# ifdef WIN32
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009005 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9006 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009007 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009008 if (r == NULL)
9009# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009010 if (check_connection() == FAIL
9011 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9012 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009013# endif
9014 EMSG(_("E277: Unable to read a server reply"));
9015 }
9016#endif
9017 rettv->v_type = VAR_STRING;
9018 rettv->vval.v_string = r;
9019}
9020
9021/*
9022 * "remote_send()" function
9023 */
9024 static void
9025f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9026{
9027 rettv->v_type = VAR_STRING;
9028 rettv->vval.v_string = NULL;
9029#ifdef FEAT_CLIENTSERVER
9030 remote_common(argvars, rettv, FALSE);
9031#endif
9032}
9033
9034/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009035 * "remote_startserver()" function
9036 */
9037 static void
9038f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9039{
9040#ifdef FEAT_CLIENTSERVER
9041 char_u *server = get_tv_string_chk(&argvars[0]);
9042
9043 if (server == NULL)
9044 return; /* type error; errmsg already given */
9045 if (serverName != NULL)
9046 EMSG(_("E941: already started a server"));
9047 else
9048 {
9049# ifdef FEAT_X11
9050 if (check_connection() == OK)
9051 serverRegisterName(X_DISPLAY, server);
9052# else
9053 serverSetName(server);
9054# endif
9055 }
9056#else
9057 EMSG(_("E942: +clientserver feature not available"));
9058#endif
9059}
9060
9061/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009062 * "remove()" function
9063 */
9064 static void
9065f_remove(typval_T *argvars, typval_T *rettv)
9066{
9067 list_T *l;
9068 listitem_T *item, *item2;
9069 listitem_T *li;
9070 long idx;
9071 long end;
9072 char_u *key;
9073 dict_T *d;
9074 dictitem_T *di;
9075 char_u *arg_errmsg = (char_u *)N_("remove() argument");
9076
9077 if (argvars[0].v_type == VAR_DICT)
9078 {
9079 if (argvars[2].v_type != VAR_UNKNOWN)
9080 EMSG2(_(e_toomanyarg), "remove()");
9081 else if ((d = argvars[0].vval.v_dict) != NULL
9082 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
9083 {
9084 key = get_tv_string_chk(&argvars[1]);
9085 if (key != NULL)
9086 {
9087 di = dict_find(d, key, -1);
9088 if (di == NULL)
9089 EMSG2(_(e_dictkey), key);
9090 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
9091 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
9092 {
9093 *rettv = di->di_tv;
9094 init_tv(&di->di_tv);
9095 dictitem_remove(d, di);
9096 }
9097 }
9098 }
9099 }
9100 else if (argvars[0].v_type != VAR_LIST)
9101 EMSG2(_(e_listdictarg), "remove()");
9102 else if ((l = argvars[0].vval.v_list) != NULL
9103 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
9104 {
9105 int error = FALSE;
9106
9107 idx = (long)get_tv_number_chk(&argvars[1], &error);
9108 if (error)
9109 ; /* type error: do nothing, errmsg already given */
9110 else if ((item = list_find(l, idx)) == NULL)
9111 EMSGN(_(e_listidx), idx);
9112 else
9113 {
9114 if (argvars[2].v_type == VAR_UNKNOWN)
9115 {
9116 /* Remove one item, return its value. */
9117 vimlist_remove(l, item, item);
9118 *rettv = item->li_tv;
9119 vim_free(item);
9120 }
9121 else
9122 {
9123 /* Remove range of items, return list with values. */
9124 end = (long)get_tv_number_chk(&argvars[2], &error);
9125 if (error)
9126 ; /* type error: do nothing */
9127 else if ((item2 = list_find(l, end)) == NULL)
9128 EMSGN(_(e_listidx), end);
9129 else
9130 {
9131 int cnt = 0;
9132
9133 for (li = item; li != NULL; li = li->li_next)
9134 {
9135 ++cnt;
9136 if (li == item2)
9137 break;
9138 }
9139 if (li == NULL) /* didn't find "item2" after "item" */
9140 EMSG(_(e_invrange));
9141 else
9142 {
9143 vimlist_remove(l, item, item2);
9144 if (rettv_list_alloc(rettv) == OK)
9145 {
9146 l = rettv->vval.v_list;
9147 l->lv_first = item;
9148 l->lv_last = item2;
9149 item->li_prev = NULL;
9150 item2->li_next = NULL;
9151 l->lv_len = cnt;
9152 }
9153 }
9154 }
9155 }
9156 }
9157 }
9158}
9159
9160/*
9161 * "rename({from}, {to})" function
9162 */
9163 static void
9164f_rename(typval_T *argvars, typval_T *rettv)
9165{
9166 char_u buf[NUMBUFLEN];
9167
9168 if (check_restricted() || check_secure())
9169 rettv->vval.v_number = -1;
9170 else
9171 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
9172 get_tv_string_buf(&argvars[1], buf));
9173}
9174
9175/*
9176 * "repeat()" function
9177 */
9178 static void
9179f_repeat(typval_T *argvars, typval_T *rettv)
9180{
9181 char_u *p;
9182 int n;
9183 int slen;
9184 int len;
9185 char_u *r;
9186 int i;
9187
9188 n = (int)get_tv_number(&argvars[1]);
9189 if (argvars[0].v_type == VAR_LIST)
9190 {
9191 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9192 while (n-- > 0)
9193 if (list_extend(rettv->vval.v_list,
9194 argvars[0].vval.v_list, NULL) == FAIL)
9195 break;
9196 }
9197 else
9198 {
9199 p = get_tv_string(&argvars[0]);
9200 rettv->v_type = VAR_STRING;
9201 rettv->vval.v_string = NULL;
9202
9203 slen = (int)STRLEN(p);
9204 len = slen * n;
9205 if (len <= 0)
9206 return;
9207
9208 r = alloc(len + 1);
9209 if (r != NULL)
9210 {
9211 for (i = 0; i < n; i++)
9212 mch_memmove(r + i * slen, p, (size_t)slen);
9213 r[len] = NUL;
9214 }
9215
9216 rettv->vval.v_string = r;
9217 }
9218}
9219
9220/*
9221 * "resolve()" function
9222 */
9223 static void
9224f_resolve(typval_T *argvars, typval_T *rettv)
9225{
9226 char_u *p;
9227#ifdef HAVE_READLINK
9228 char_u *buf = NULL;
9229#endif
9230
9231 p = get_tv_string(&argvars[0]);
9232#ifdef FEAT_SHORTCUT
9233 {
9234 char_u *v = NULL;
9235
9236 v = mch_resolve_shortcut(p);
9237 if (v != NULL)
9238 rettv->vval.v_string = v;
9239 else
9240 rettv->vval.v_string = vim_strsave(p);
9241 }
9242#else
9243# ifdef HAVE_READLINK
9244 {
9245 char_u *cpy;
9246 int len;
9247 char_u *remain = NULL;
9248 char_u *q;
9249 int is_relative_to_current = FALSE;
9250 int has_trailing_pathsep = FALSE;
9251 int limit = 100;
9252
9253 p = vim_strsave(p);
9254
9255 if (p[0] == '.' && (vim_ispathsep(p[1])
9256 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9257 is_relative_to_current = TRUE;
9258
9259 len = STRLEN(p);
9260 if (len > 0 && after_pathsep(p, p + len))
9261 {
9262 has_trailing_pathsep = TRUE;
9263 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9264 }
9265
9266 q = getnextcomp(p);
9267 if (*q != NUL)
9268 {
9269 /* Separate the first path component in "p", and keep the
9270 * remainder (beginning with the path separator). */
9271 remain = vim_strsave(q - 1);
9272 q[-1] = NUL;
9273 }
9274
9275 buf = alloc(MAXPATHL + 1);
9276 if (buf == NULL)
9277 goto fail;
9278
9279 for (;;)
9280 {
9281 for (;;)
9282 {
9283 len = readlink((char *)p, (char *)buf, MAXPATHL);
9284 if (len <= 0)
9285 break;
9286 buf[len] = NUL;
9287
9288 if (limit-- == 0)
9289 {
9290 vim_free(p);
9291 vim_free(remain);
9292 EMSG(_("E655: Too many symbolic links (cycle?)"));
9293 rettv->vval.v_string = NULL;
9294 goto fail;
9295 }
9296
9297 /* Ensure that the result will have a trailing path separator
9298 * if the argument has one. */
9299 if (remain == NULL && has_trailing_pathsep)
9300 add_pathsep(buf);
9301
9302 /* Separate the first path component in the link value and
9303 * concatenate the remainders. */
9304 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9305 if (*q != NUL)
9306 {
9307 if (remain == NULL)
9308 remain = vim_strsave(q - 1);
9309 else
9310 {
9311 cpy = concat_str(q - 1, remain);
9312 if (cpy != NULL)
9313 {
9314 vim_free(remain);
9315 remain = cpy;
9316 }
9317 }
9318 q[-1] = NUL;
9319 }
9320
9321 q = gettail(p);
9322 if (q > p && *q == NUL)
9323 {
9324 /* Ignore trailing path separator. */
9325 q[-1] = NUL;
9326 q = gettail(p);
9327 }
9328 if (q > p && !mch_isFullName(buf))
9329 {
9330 /* symlink is relative to directory of argument */
9331 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
9332 if (cpy != NULL)
9333 {
9334 STRCPY(cpy, p);
9335 STRCPY(gettail(cpy), buf);
9336 vim_free(p);
9337 p = cpy;
9338 }
9339 }
9340 else
9341 {
9342 vim_free(p);
9343 p = vim_strsave(buf);
9344 }
9345 }
9346
9347 if (remain == NULL)
9348 break;
9349
9350 /* Append the first path component of "remain" to "p". */
9351 q = getnextcomp(remain + 1);
9352 len = q - remain - (*q != NUL);
9353 cpy = vim_strnsave(p, STRLEN(p) + len);
9354 if (cpy != NULL)
9355 {
9356 STRNCAT(cpy, remain, len);
9357 vim_free(p);
9358 p = cpy;
9359 }
9360 /* Shorten "remain". */
9361 if (*q != NUL)
9362 STRMOVE(remain, q - 1);
9363 else
Bram Moolenaard23a8232018-02-10 18:45:26 +01009364 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009365 }
9366
9367 /* If the result is a relative path name, make it explicitly relative to
9368 * the current directory if and only if the argument had this form. */
9369 if (!vim_ispathsep(*p))
9370 {
9371 if (is_relative_to_current
9372 && *p != NUL
9373 && !(p[0] == '.'
9374 && (p[1] == NUL
9375 || vim_ispathsep(p[1])
9376 || (p[1] == '.'
9377 && (p[2] == NUL
9378 || vim_ispathsep(p[2]))))))
9379 {
9380 /* Prepend "./". */
9381 cpy = concat_str((char_u *)"./", p);
9382 if (cpy != NULL)
9383 {
9384 vim_free(p);
9385 p = cpy;
9386 }
9387 }
9388 else if (!is_relative_to_current)
9389 {
9390 /* Strip leading "./". */
9391 q = p;
9392 while (q[0] == '.' && vim_ispathsep(q[1]))
9393 q += 2;
9394 if (q > p)
9395 STRMOVE(p, p + 2);
9396 }
9397 }
9398
9399 /* Ensure that the result will have no trailing path separator
9400 * if the argument had none. But keep "/" or "//". */
9401 if (!has_trailing_pathsep)
9402 {
9403 q = p + STRLEN(p);
9404 if (after_pathsep(p, q))
9405 *gettail_sep(p) = NUL;
9406 }
9407
9408 rettv->vval.v_string = p;
9409 }
9410# else
9411 rettv->vval.v_string = vim_strsave(p);
9412# endif
9413#endif
9414
9415 simplify_filename(rettv->vval.v_string);
9416
9417#ifdef HAVE_READLINK
9418fail:
9419 vim_free(buf);
9420#endif
9421 rettv->v_type = VAR_STRING;
9422}
9423
9424/*
9425 * "reverse({list})" function
9426 */
9427 static void
9428f_reverse(typval_T *argvars, typval_T *rettv)
9429{
9430 list_T *l;
9431 listitem_T *li, *ni;
9432
9433 if (argvars[0].v_type != VAR_LIST)
9434 EMSG2(_(e_listarg), "reverse()");
9435 else if ((l = argvars[0].vval.v_list) != NULL
9436 && !tv_check_lock(l->lv_lock,
9437 (char_u *)N_("reverse() argument"), TRUE))
9438 {
9439 li = l->lv_last;
9440 l->lv_first = l->lv_last = NULL;
9441 l->lv_len = 0;
9442 while (li != NULL)
9443 {
9444 ni = li->li_prev;
9445 list_append(l, li);
9446 li = ni;
9447 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009448 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009449 l->lv_idx = l->lv_len - l->lv_idx - 1;
9450 }
9451}
9452
9453#define SP_NOMOVE 0x01 /* don't move cursor */
9454#define SP_REPEAT 0x02 /* repeat to find outer pair */
9455#define SP_RETCOUNT 0x04 /* return matchcount */
9456#define SP_SETPCMARK 0x08 /* set previous context mark */
9457#define SP_START 0x10 /* accept match at start position */
9458#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9459#define SP_END 0x40 /* leave cursor at end of match */
9460#define SP_COLUMN 0x80 /* start at cursor column */
9461
9462static int get_search_arg(typval_T *varp, int *flagsp);
9463
9464/*
9465 * Get flags for a search function.
9466 * Possibly sets "p_ws".
9467 * Returns BACKWARD, FORWARD or zero (for an error).
9468 */
9469 static int
9470get_search_arg(typval_T *varp, int *flagsp)
9471{
9472 int dir = FORWARD;
9473 char_u *flags;
9474 char_u nbuf[NUMBUFLEN];
9475 int mask;
9476
9477 if (varp->v_type != VAR_UNKNOWN)
9478 {
9479 flags = get_tv_string_buf_chk(varp, nbuf);
9480 if (flags == NULL)
9481 return 0; /* type error; errmsg already given */
9482 while (*flags != NUL)
9483 {
9484 switch (*flags)
9485 {
9486 case 'b': dir = BACKWARD; break;
9487 case 'w': p_ws = TRUE; break;
9488 case 'W': p_ws = FALSE; break;
9489 default: mask = 0;
9490 if (flagsp != NULL)
9491 switch (*flags)
9492 {
9493 case 'c': mask = SP_START; break;
9494 case 'e': mask = SP_END; break;
9495 case 'm': mask = SP_RETCOUNT; break;
9496 case 'n': mask = SP_NOMOVE; break;
9497 case 'p': mask = SP_SUBPAT; break;
9498 case 'r': mask = SP_REPEAT; break;
9499 case 's': mask = SP_SETPCMARK; break;
9500 case 'z': mask = SP_COLUMN; break;
9501 }
9502 if (mask == 0)
9503 {
9504 EMSG2(_(e_invarg2), flags);
9505 dir = 0;
9506 }
9507 else
9508 *flagsp |= mask;
9509 }
9510 if (dir == 0)
9511 break;
9512 ++flags;
9513 }
9514 }
9515 return dir;
9516}
9517
9518/*
9519 * Shared by search() and searchpos() functions.
9520 */
9521 static int
9522search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9523{
9524 int flags;
9525 char_u *pat;
9526 pos_T pos;
9527 pos_T save_cursor;
9528 int save_p_ws = p_ws;
9529 int dir;
9530 int retval = 0; /* default: FAIL */
9531 long lnum_stop = 0;
9532 proftime_T tm;
9533#ifdef FEAT_RELTIME
9534 long time_limit = 0;
9535#endif
9536 int options = SEARCH_KEEP;
9537 int subpatnum;
9538
9539 pat = get_tv_string(&argvars[0]);
9540 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
9541 if (dir == 0)
9542 goto theend;
9543 flags = *flagsp;
9544 if (flags & SP_START)
9545 options |= SEARCH_START;
9546 if (flags & SP_END)
9547 options |= SEARCH_END;
9548 if (flags & SP_COLUMN)
9549 options |= SEARCH_COL;
9550
9551 /* Optional arguments: line number to stop searching and timeout. */
9552 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9553 {
9554 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
9555 if (lnum_stop < 0)
9556 goto theend;
9557#ifdef FEAT_RELTIME
9558 if (argvars[3].v_type != VAR_UNKNOWN)
9559 {
9560 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
9561 if (time_limit < 0)
9562 goto theend;
9563 }
9564#endif
9565 }
9566
9567#ifdef FEAT_RELTIME
9568 /* Set the time limit, if there is one. */
9569 profile_setlimit(time_limit, &tm);
9570#endif
9571
9572 /*
9573 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
9574 * Check to make sure only those flags are set.
9575 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
9576 * flags cannot be set. Check for that condition also.
9577 */
9578 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
9579 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9580 {
9581 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
9582 goto theend;
9583 }
9584
9585 pos = save_cursor = curwin->w_cursor;
9586 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009587 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009588 if (subpatnum != FAIL)
9589 {
9590 if (flags & SP_SUBPAT)
9591 retval = subpatnum;
9592 else
9593 retval = pos.lnum;
9594 if (flags & SP_SETPCMARK)
9595 setpcmark();
9596 curwin->w_cursor = pos;
9597 if (match_pos != NULL)
9598 {
9599 /* Store the match cursor position */
9600 match_pos->lnum = pos.lnum;
9601 match_pos->col = pos.col + 1;
9602 }
9603 /* "/$" will put the cursor after the end of the line, may need to
9604 * correct that here */
9605 check_cursor();
9606 }
9607
9608 /* If 'n' flag is used: restore cursor position. */
9609 if (flags & SP_NOMOVE)
9610 curwin->w_cursor = save_cursor;
9611 else
9612 curwin->w_set_curswant = TRUE;
9613theend:
9614 p_ws = save_p_ws;
9615
9616 return retval;
9617}
9618
9619#ifdef FEAT_FLOAT
9620
9621/*
9622 * round() is not in C90, use ceil() or floor() instead.
9623 */
9624 float_T
9625vim_round(float_T f)
9626{
9627 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
9628}
9629
9630/*
9631 * "round({float})" function
9632 */
9633 static void
9634f_round(typval_T *argvars, typval_T *rettv)
9635{
9636 float_T f = 0.0;
9637
9638 rettv->v_type = VAR_FLOAT;
9639 if (get_float_arg(argvars, &f) == OK)
9640 rettv->vval.v_float = vim_round(f);
9641 else
9642 rettv->vval.v_float = 0.0;
9643}
9644#endif
9645
9646/*
9647 * "screenattr()" function
9648 */
9649 static void
9650f_screenattr(typval_T *argvars, typval_T *rettv)
9651{
9652 int row;
9653 int col;
9654 int c;
9655
9656 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9657 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9658 if (row < 0 || row >= screen_Rows
9659 || col < 0 || col >= screen_Columns)
9660 c = -1;
9661 else
9662 c = ScreenAttrs[LineOffset[row] + col];
9663 rettv->vval.v_number = c;
9664}
9665
9666/*
9667 * "screenchar()" function
9668 */
9669 static void
9670f_screenchar(typval_T *argvars, typval_T *rettv)
9671{
9672 int row;
9673 int col;
9674 int off;
9675 int c;
9676
9677 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9678 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9679 if (row < 0 || row >= screen_Rows
9680 || col < 0 || col >= screen_Columns)
9681 c = -1;
9682 else
9683 {
9684 off = LineOffset[row] + col;
9685#ifdef FEAT_MBYTE
9686 if (enc_utf8 && ScreenLinesUC[off] != 0)
9687 c = ScreenLinesUC[off];
9688 else
9689#endif
9690 c = ScreenLines[off];
9691 }
9692 rettv->vval.v_number = c;
9693}
9694
9695/*
9696 * "screencol()" function
9697 *
9698 * First column is 1 to be consistent with virtcol().
9699 */
9700 static void
9701f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
9702{
9703 rettv->vval.v_number = screen_screencol() + 1;
9704}
9705
9706/*
9707 * "screenrow()" function
9708 */
9709 static void
9710f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
9711{
9712 rettv->vval.v_number = screen_screenrow() + 1;
9713}
9714
9715/*
9716 * "search()" function
9717 */
9718 static void
9719f_search(typval_T *argvars, typval_T *rettv)
9720{
9721 int flags = 0;
9722
9723 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
9724}
9725
9726/*
9727 * "searchdecl()" function
9728 */
9729 static void
9730f_searchdecl(typval_T *argvars, typval_T *rettv)
9731{
9732 int locally = 1;
9733 int thisblock = 0;
9734 int error = FALSE;
9735 char_u *name;
9736
9737 rettv->vval.v_number = 1; /* default: FAIL */
9738
9739 name = get_tv_string_chk(&argvars[0]);
9740 if (argvars[1].v_type != VAR_UNKNOWN)
9741 {
9742 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
9743 if (!error && argvars[2].v_type != VAR_UNKNOWN)
9744 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
9745 }
9746 if (!error && name != NULL)
9747 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
9748 locally, thisblock, SEARCH_KEEP) == FAIL;
9749}
9750
9751/*
9752 * Used by searchpair() and searchpairpos()
9753 */
9754 static int
9755searchpair_cmn(typval_T *argvars, pos_T *match_pos)
9756{
9757 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01009758 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009759 int save_p_ws = p_ws;
9760 int dir;
9761 int flags = 0;
9762 char_u nbuf1[NUMBUFLEN];
9763 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009764 int retval = 0; /* default: FAIL */
9765 long lnum_stop = 0;
9766 long time_limit = 0;
9767
9768 /* Get the three pattern arguments: start, middle, end. */
9769 spat = get_tv_string_chk(&argvars[0]);
9770 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
9771 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
9772 if (spat == NULL || mpat == NULL || epat == NULL)
9773 goto theend; /* type error */
9774
9775 /* Handle the optional fourth argument: flags */
9776 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
9777 if (dir == 0)
9778 goto theend;
9779
9780 /* Don't accept SP_END or SP_SUBPAT.
9781 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
9782 */
9783 if ((flags & (SP_END | SP_SUBPAT)) != 0
9784 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9785 {
9786 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
9787 goto theend;
9788 }
9789
9790 /* Using 'r' implies 'W', otherwise it doesn't work. */
9791 if (flags & SP_REPEAT)
9792 p_ws = FALSE;
9793
9794 /* Optional fifth argument: skip expression */
9795 if (argvars[3].v_type == VAR_UNKNOWN
9796 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01009797 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009798 else
9799 {
Bram Moolenaar48570482017-10-30 21:48:41 +01009800 skip = &argvars[4];
9801 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
9802 && skip->v_type != VAR_STRING)
9803 {
9804 /* Type error */
9805 goto theend;
9806 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009807 if (argvars[5].v_type != VAR_UNKNOWN)
9808 {
9809 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
9810 if (lnum_stop < 0)
9811 goto theend;
9812#ifdef FEAT_RELTIME
9813 if (argvars[6].v_type != VAR_UNKNOWN)
9814 {
9815 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
9816 if (time_limit < 0)
9817 goto theend;
9818 }
9819#endif
9820 }
9821 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009822
9823 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
9824 match_pos, lnum_stop, time_limit);
9825
9826theend:
9827 p_ws = save_p_ws;
9828
9829 return retval;
9830}
9831
9832/*
9833 * "searchpair()" function
9834 */
9835 static void
9836f_searchpair(typval_T *argvars, typval_T *rettv)
9837{
9838 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
9839}
9840
9841/*
9842 * "searchpairpos()" function
9843 */
9844 static void
9845f_searchpairpos(typval_T *argvars, typval_T *rettv)
9846{
9847 pos_T match_pos;
9848 int lnum = 0;
9849 int col = 0;
9850
9851 if (rettv_list_alloc(rettv) == FAIL)
9852 return;
9853
9854 if (searchpair_cmn(argvars, &match_pos) > 0)
9855 {
9856 lnum = match_pos.lnum;
9857 col = match_pos.col;
9858 }
9859
9860 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9861 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9862}
9863
9864/*
9865 * Search for a start/middle/end thing.
9866 * Used by searchpair(), see its documentation for the details.
9867 * Returns 0 or -1 for no match,
9868 */
9869 long
9870do_searchpair(
9871 char_u *spat, /* start pattern */
9872 char_u *mpat, /* middle pattern */
9873 char_u *epat, /* end pattern */
9874 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +01009875 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009876 int flags, /* SP_SETPCMARK and other SP_ values */
9877 pos_T *match_pos,
9878 linenr_T lnum_stop, /* stop at this line if not zero */
9879 long time_limit UNUSED) /* stop after this many msec */
9880{
9881 char_u *save_cpo;
9882 char_u *pat, *pat2 = NULL, *pat3 = NULL;
9883 long retval = 0;
9884 pos_T pos;
9885 pos_T firstpos;
9886 pos_T foundpos;
9887 pos_T save_cursor;
9888 pos_T save_pos;
9889 int n;
9890 int r;
9891 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01009892 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009893 int err;
9894 int options = SEARCH_KEEP;
9895 proftime_T tm;
9896
9897 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
9898 save_cpo = p_cpo;
9899 p_cpo = empty_option;
9900
9901#ifdef FEAT_RELTIME
9902 /* Set the time limit, if there is one. */
9903 profile_setlimit(time_limit, &tm);
9904#endif
9905
9906 /* Make two search patterns: start/end (pat2, for in nested pairs) and
9907 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009908 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
9909 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009910 if (pat2 == NULL || pat3 == NULL)
9911 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009912 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009913 if (*mpat == NUL)
9914 STRCPY(pat3, pat2);
9915 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009916 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009917 spat, epat, mpat);
9918 if (flags & SP_START)
9919 options |= SEARCH_START;
9920
Bram Moolenaar48570482017-10-30 21:48:41 +01009921 if (skip != NULL)
9922 {
9923 /* Empty string means to not use the skip expression. */
9924 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
9925 use_skip = skip->vval.v_string != NULL
9926 && *skip->vval.v_string != NUL;
9927 }
9928
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009929 save_cursor = curwin->w_cursor;
9930 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009931 CLEAR_POS(&firstpos);
9932 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009933 pat = pat3;
9934 for (;;)
9935 {
9936 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009937 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009938 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009939 /* didn't find it or found the first match again: FAIL */
9940 break;
9941
9942 if (firstpos.lnum == 0)
9943 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009944 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009945 {
9946 /* Found the same position again. Can happen with a pattern that
9947 * has "\zs" at the end and searching backwards. Advance one
9948 * character and try again. */
9949 if (dir == BACKWARD)
9950 decl(&pos);
9951 else
9952 incl(&pos);
9953 }
9954 foundpos = pos;
9955
9956 /* clear the start flag to avoid getting stuck here */
9957 options &= ~SEARCH_START;
9958
9959 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +01009960 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009961 {
9962 save_pos = curwin->w_cursor;
9963 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01009964 err = FALSE;
9965 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009966 curwin->w_cursor = save_pos;
9967 if (err)
9968 {
9969 /* Evaluating {skip} caused an error, break here. */
9970 curwin->w_cursor = save_cursor;
9971 retval = -1;
9972 break;
9973 }
9974 if (r)
9975 continue;
9976 }
9977
9978 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
9979 {
9980 /* Found end when searching backwards or start when searching
9981 * forward: nested pair. */
9982 ++nest;
9983 pat = pat2; /* nested, don't search for middle */
9984 }
9985 else
9986 {
9987 /* Found end when searching forward or start when searching
9988 * backward: end of (nested) pair; or found middle in outer pair. */
9989 if (--nest == 1)
9990 pat = pat3; /* outer level, search for middle */
9991 }
9992
9993 if (nest == 0)
9994 {
9995 /* Found the match: return matchcount or line number. */
9996 if (flags & SP_RETCOUNT)
9997 ++retval;
9998 else
9999 retval = pos.lnum;
10000 if (flags & SP_SETPCMARK)
10001 setpcmark();
10002 curwin->w_cursor = pos;
10003 if (!(flags & SP_REPEAT))
10004 break;
10005 nest = 1; /* search for next unmatched */
10006 }
10007 }
10008
10009 if (match_pos != NULL)
10010 {
10011 /* Store the match cursor position */
10012 match_pos->lnum = curwin->w_cursor.lnum;
10013 match_pos->col = curwin->w_cursor.col + 1;
10014 }
10015
10016 /* If 'n' flag is used or search failed: restore cursor position. */
10017 if ((flags & SP_NOMOVE) || retval == 0)
10018 curwin->w_cursor = save_cursor;
10019
10020theend:
10021 vim_free(pat2);
10022 vim_free(pat3);
10023 if (p_cpo == empty_option)
10024 p_cpo = save_cpo;
10025 else
10026 /* Darn, evaluating the {skip} expression changed the value. */
10027 free_string_option(save_cpo);
10028
10029 return retval;
10030}
10031
10032/*
10033 * "searchpos()" function
10034 */
10035 static void
10036f_searchpos(typval_T *argvars, typval_T *rettv)
10037{
10038 pos_T match_pos;
10039 int lnum = 0;
10040 int col = 0;
10041 int n;
10042 int flags = 0;
10043
10044 if (rettv_list_alloc(rettv) == FAIL)
10045 return;
10046
10047 n = search_cmn(argvars, &match_pos, &flags);
10048 if (n > 0)
10049 {
10050 lnum = match_pos.lnum;
10051 col = match_pos.col;
10052 }
10053
10054 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10055 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10056 if (flags & SP_SUBPAT)
10057 list_append_number(rettv->vval.v_list, (varnumber_T)n);
10058}
10059
10060 static void
10061f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
10062{
10063#ifdef FEAT_CLIENTSERVER
10064 char_u buf[NUMBUFLEN];
10065 char_u *server = get_tv_string_chk(&argvars[0]);
10066 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
10067
10068 rettv->vval.v_number = -1;
10069 if (server == NULL || reply == NULL)
10070 return;
10071 if (check_restricted() || check_secure())
10072 return;
10073# ifdef FEAT_X11
10074 if (check_connection() == FAIL)
10075 return;
10076# endif
10077
10078 if (serverSendReply(server, reply) < 0)
10079 {
10080 EMSG(_("E258: Unable to send to client"));
10081 return;
10082 }
10083 rettv->vval.v_number = 0;
10084#else
10085 rettv->vval.v_number = -1;
10086#endif
10087}
10088
10089 static void
10090f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10091{
10092 char_u *r = NULL;
10093
10094#ifdef FEAT_CLIENTSERVER
10095# ifdef WIN32
10096 r = serverGetVimNames();
10097# else
10098 make_connection();
10099 if (X_DISPLAY != NULL)
10100 r = serverGetVimNames(X_DISPLAY);
10101# endif
10102#endif
10103 rettv->v_type = VAR_STRING;
10104 rettv->vval.v_string = r;
10105}
10106
10107/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010108 * Set line or list of lines in buffer "buf".
10109 */
10110 static void
10111set_buffer_lines(buf_T *buf, linenr_T lnum, typval_T *lines, typval_T *rettv)
10112{
10113 char_u *line = NULL;
10114 list_T *l = NULL;
10115 listitem_T *li = NULL;
10116 long added = 0;
10117 linenr_T lcount;
Bram Moolenaar0c4dc882017-11-06 21:32:54 +010010118 buf_T *curbuf_save = NULL;
10119 win_T *curwin_save = NULL;
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010120 int is_curbuf = buf == curbuf;
10121
Bram Moolenaar9d954202017-09-04 20:34:19 +020010122 /* When using the current buffer ml_mfp will be set if needed. Useful when
10123 * setline() is used on startup. For other buffers the buffer must be
10124 * loaded. */
10125 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010126 {
10127 rettv->vval.v_number = 1; /* FAIL */
10128 return;
10129 }
10130
Bram Moolenaar0c4dc882017-11-06 21:32:54 +010010131 if (!is_curbuf)
10132 {
10133 wininfo_T *wip;
10134
10135 curbuf_save = curbuf;
10136 curwin_save = curwin;
10137 curbuf = buf;
10138 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
10139 {
10140 if (wip->wi_win != NULL)
10141 {
10142 curwin = wip->wi_win;
10143 break;
10144 }
10145 }
10146 }
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010147
10148 lcount = curbuf->b_ml.ml_line_count;
10149
10150 if (lines->v_type == VAR_LIST)
10151 {
10152 l = lines->vval.v_list;
10153 li = l->lv_first;
10154 }
10155 else
10156 line = get_tv_string_chk(lines);
10157
10158 /* default result is zero == OK */
10159 for (;;)
10160 {
10161 if (l != NULL)
10162 {
10163 /* list argument, get next string */
10164 if (li == NULL)
10165 break;
10166 line = get_tv_string_chk(&li->li_tv);
10167 li = li->li_next;
10168 }
10169
10170 rettv->vval.v_number = 1; /* FAIL */
10171 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
10172 break;
10173
10174 /* When coming here from Insert mode, sync undo, so that this can be
10175 * undone separately from what was previously inserted. */
10176 if (u_sync_once == 2)
10177 {
10178 u_sync_once = 1; /* notify that u_sync() was called */
10179 u_sync(TRUE);
10180 }
10181
10182 if (lnum <= curbuf->b_ml.ml_line_count)
10183 {
10184 /* existing line, replace it */
10185 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
10186 {
10187 changed_bytes(lnum, 0);
10188 if (is_curbuf && lnum == curwin->w_cursor.lnum)
10189 check_cursor_col();
10190 rettv->vval.v_number = 0; /* OK */
10191 }
10192 }
10193 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
10194 {
10195 /* lnum is one past the last line, append the line */
10196 ++added;
10197 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
10198 rettv->vval.v_number = 0; /* OK */
10199 }
10200
10201 if (l == NULL) /* only one string argument */
10202 break;
10203 ++lnum;
10204 }
10205
10206 if (added > 0)
10207 appended_lines_mark(lcount, added);
10208
Bram Moolenaar0c4dc882017-11-06 21:32:54 +010010209 if (!is_curbuf)
10210 {
10211 curbuf = curbuf_save;
10212 curwin = curwin_save;
10213 }
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010214}
10215
10216/*
10217 * "setbufline()" function
10218 */
10219 static void
10220f_setbufline(argvars, rettv)
10221 typval_T *argvars;
10222 typval_T *rettv;
10223{
10224 linenr_T lnum;
10225 buf_T *buf;
10226
10227 buf = get_buf_tv(&argvars[0], FALSE);
10228 if (buf == NULL)
10229 rettv->vval.v_number = 1; /* FAIL */
10230 else
10231 {
10232 lnum = get_tv_lnum_buf(&argvars[1], buf);
10233
10234 set_buffer_lines(buf, lnum, &argvars[2], rettv);
10235 }
10236}
10237
10238/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010239 * "setbufvar()" function
10240 */
10241 static void
10242f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10243{
10244 buf_T *buf;
10245 char_u *varname, *bufvarname;
10246 typval_T *varp;
10247 char_u nbuf[NUMBUFLEN];
10248
10249 if (check_restricted() || check_secure())
10250 return;
10251 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10252 varname = get_tv_string_chk(&argvars[1]);
10253 buf = get_buf_tv(&argvars[0], FALSE);
10254 varp = &argvars[2];
10255
10256 if (buf != NULL && varname != NULL && varp != NULL)
10257 {
10258 if (*varname == '&')
10259 {
10260 long numval;
10261 char_u *strval;
10262 int error = FALSE;
10263 aco_save_T aco;
10264
10265 /* set curbuf to be our buf, temporarily */
10266 aucmd_prepbuf(&aco, buf);
10267
10268 ++varname;
10269 numval = (long)get_tv_number_chk(varp, &error);
10270 strval = get_tv_string_buf_chk(varp, nbuf);
10271 if (!error && strval != NULL)
10272 set_option_value(varname, numval, strval, OPT_LOCAL);
10273
10274 /* reset notion of buffer */
10275 aucmd_restbuf(&aco);
10276 }
10277 else
10278 {
10279 buf_T *save_curbuf = curbuf;
10280
10281 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
10282 if (bufvarname != NULL)
10283 {
10284 curbuf = buf;
10285 STRCPY(bufvarname, "b:");
10286 STRCPY(bufvarname + 2, varname);
10287 set_var(bufvarname, varp, TRUE);
10288 vim_free(bufvarname);
10289 curbuf = save_curbuf;
10290 }
10291 }
10292 }
10293}
10294
10295 static void
10296f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10297{
10298 dict_T *d;
10299 dictitem_T *di;
10300 char_u *csearch;
10301
10302 if (argvars[0].v_type != VAR_DICT)
10303 {
10304 EMSG(_(e_dictreq));
10305 return;
10306 }
10307
10308 if ((d = argvars[0].vval.v_dict) != NULL)
10309 {
10310 csearch = get_dict_string(d, (char_u *)"char", FALSE);
10311 if (csearch != NULL)
10312 {
10313#ifdef FEAT_MBYTE
10314 if (enc_utf8)
10315 {
10316 int pcc[MAX_MCO];
10317 int c = utfc_ptr2char(csearch, pcc);
10318
10319 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10320 }
10321 else
10322#endif
10323 set_last_csearch(PTR2CHAR(csearch),
10324 csearch, MB_PTR2LEN(csearch));
10325 }
10326
10327 di = dict_find(d, (char_u *)"forward", -1);
10328 if (di != NULL)
10329 set_csearch_direction((int)get_tv_number(&di->di_tv)
10330 ? FORWARD : BACKWARD);
10331
10332 di = dict_find(d, (char_u *)"until", -1);
10333 if (di != NULL)
10334 set_csearch_until(!!get_tv_number(&di->di_tv));
10335 }
10336}
10337
10338/*
10339 * "setcmdpos()" function
10340 */
10341 static void
10342f_setcmdpos(typval_T *argvars, typval_T *rettv)
10343{
10344 int pos = (int)get_tv_number(&argvars[0]) - 1;
10345
10346 if (pos >= 0)
10347 rettv->vval.v_number = set_cmdline_pos(pos);
10348}
10349
10350/*
10351 * "setfperm({fname}, {mode})" function
10352 */
10353 static void
10354f_setfperm(typval_T *argvars, typval_T *rettv)
10355{
10356 char_u *fname;
10357 char_u modebuf[NUMBUFLEN];
10358 char_u *mode_str;
10359 int i;
10360 int mask;
10361 int mode = 0;
10362
10363 rettv->vval.v_number = 0;
10364 fname = get_tv_string_chk(&argvars[0]);
10365 if (fname == NULL)
10366 return;
10367 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
10368 if (mode_str == NULL)
10369 return;
10370 if (STRLEN(mode_str) != 9)
10371 {
10372 EMSG2(_(e_invarg2), mode_str);
10373 return;
10374 }
10375
10376 mask = 1;
10377 for (i = 8; i >= 0; --i)
10378 {
10379 if (mode_str[i] != '-')
10380 mode |= mask;
10381 mask = mask << 1;
10382 }
10383 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10384}
10385
10386/*
10387 * "setline()" function
10388 */
10389 static void
10390f_setline(typval_T *argvars, typval_T *rettv)
10391{
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010392 linenr_T lnum = get_tv_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010393
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010394 set_buffer_lines(curbuf, lnum, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010395}
10396
Bram Moolenaard823fa92016-08-12 16:29:27 +020010397static 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 +020010398
10399/*
10400 * Used by "setqflist()" and "setloclist()" functions
10401 */
10402 static void
10403set_qf_ll_list(
10404 win_T *wp UNUSED,
10405 typval_T *list_arg UNUSED,
10406 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010407 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010408 typval_T *rettv)
10409{
10410#ifdef FEAT_QUICKFIX
10411 static char *e_invact = N_("E927: Invalid action: '%s'");
10412 char_u *act;
10413 int action = 0;
10414#endif
10415
10416 rettv->vval.v_number = -1;
10417
10418#ifdef FEAT_QUICKFIX
10419 if (list_arg->v_type != VAR_LIST)
10420 EMSG(_(e_listreq));
10421 else
10422 {
10423 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010424 dict_T *d = NULL;
10425 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010426
10427 if (action_arg->v_type == VAR_STRING)
10428 {
10429 act = get_tv_string_chk(action_arg);
10430 if (act == NULL)
10431 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020010432 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
10433 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010434 action = *act;
10435 else
10436 EMSG2(_(e_invact), act);
10437 }
10438 else if (action_arg->v_type == VAR_UNKNOWN)
10439 action = ' ';
10440 else
10441 EMSG(_(e_stringreq));
10442
Bram Moolenaard823fa92016-08-12 16:29:27 +020010443 if (action_arg->v_type != VAR_UNKNOWN
10444 && what_arg->v_type != VAR_UNKNOWN)
10445 {
10446 if (what_arg->v_type == VAR_DICT)
10447 d = what_arg->vval.v_dict;
10448 else
10449 {
10450 EMSG(_(e_dictreq));
10451 valid_dict = FALSE;
10452 }
10453 }
10454
10455 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
10456 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()"), d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010457 rettv->vval.v_number = 0;
10458 }
10459#endif
10460}
10461
10462/*
10463 * "setloclist()" function
10464 */
10465 static void
10466f_setloclist(typval_T *argvars, typval_T *rettv)
10467{
10468 win_T *win;
10469
10470 rettv->vval.v_number = -1;
10471
10472 win = find_win_by_nr(&argvars[0], NULL);
10473 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020010474 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010475}
10476
10477/*
10478 * "setmatches()" function
10479 */
10480 static void
10481f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10482{
10483#ifdef FEAT_SEARCH_EXTRA
10484 list_T *l;
10485 listitem_T *li;
10486 dict_T *d;
10487 list_T *s = NULL;
10488
10489 rettv->vval.v_number = -1;
10490 if (argvars[0].v_type != VAR_LIST)
10491 {
10492 EMSG(_(e_listreq));
10493 return;
10494 }
10495 if ((l = argvars[0].vval.v_list) != NULL)
10496 {
10497
10498 /* To some extent make sure that we are dealing with a list from
10499 * "getmatches()". */
10500 li = l->lv_first;
10501 while (li != NULL)
10502 {
10503 if (li->li_tv.v_type != VAR_DICT
10504 || (d = li->li_tv.vval.v_dict) == NULL)
10505 {
10506 EMSG(_(e_invarg));
10507 return;
10508 }
10509 if (!(dict_find(d, (char_u *)"group", -1) != NULL
10510 && (dict_find(d, (char_u *)"pattern", -1) != NULL
10511 || dict_find(d, (char_u *)"pos1", -1) != NULL)
10512 && dict_find(d, (char_u *)"priority", -1) != NULL
10513 && dict_find(d, (char_u *)"id", -1) != NULL))
10514 {
10515 EMSG(_(e_invarg));
10516 return;
10517 }
10518 li = li->li_next;
10519 }
10520
10521 clear_matches(curwin);
10522 li = l->lv_first;
10523 while (li != NULL)
10524 {
10525 int i = 0;
10526 char_u buf[5];
10527 dictitem_T *di;
10528 char_u *group;
10529 int priority;
10530 int id;
10531 char_u *conceal;
10532
10533 d = li->li_tv.vval.v_dict;
10534 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
10535 {
10536 if (s == NULL)
10537 {
10538 s = list_alloc();
10539 if (s == NULL)
10540 return;
10541 }
10542
10543 /* match from matchaddpos() */
10544 for (i = 1; i < 9; i++)
10545 {
10546 sprintf((char *)buf, (char *)"pos%d", i);
10547 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
10548 {
10549 if (di->di_tv.v_type != VAR_LIST)
10550 return;
10551
10552 list_append_tv(s, &di->di_tv);
10553 s->lv_refcount++;
10554 }
10555 else
10556 break;
10557 }
10558 }
10559
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010560 group = get_dict_string(d, (char_u *)"group", TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010561 priority = (int)get_dict_number(d, (char_u *)"priority");
10562 id = (int)get_dict_number(d, (char_u *)"id");
10563 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010564 ? get_dict_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010565 : NULL;
10566 if (i == 0)
10567 {
10568 match_add(curwin, group,
10569 get_dict_string(d, (char_u *)"pattern", FALSE),
10570 priority, id, NULL, conceal);
10571 }
10572 else
10573 {
10574 match_add(curwin, group, NULL, priority, id, s, conceal);
10575 list_unref(s);
10576 s = NULL;
10577 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010578 vim_free(group);
10579 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010580
10581 li = li->li_next;
10582 }
10583 rettv->vval.v_number = 0;
10584 }
10585#endif
10586}
10587
10588/*
10589 * "setpos()" function
10590 */
10591 static void
10592f_setpos(typval_T *argvars, typval_T *rettv)
10593{
10594 pos_T pos;
10595 int fnum;
10596 char_u *name;
10597 colnr_T curswant = -1;
10598
10599 rettv->vval.v_number = -1;
10600 name = get_tv_string_chk(argvars);
10601 if (name != NULL)
10602 {
10603 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10604 {
10605 if (--pos.col < 0)
10606 pos.col = 0;
10607 if (name[0] == '.' && name[1] == NUL)
10608 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010609 /* set cursor; "fnum" is ignored */
10610 curwin->w_cursor = pos;
10611 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010612 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010613 curwin->w_curswant = curswant - 1;
10614 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010615 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010616 check_cursor();
10617 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010618 }
10619 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10620 {
10621 /* set mark */
10622 if (setmark_pos(name[1], &pos, fnum) == OK)
10623 rettv->vval.v_number = 0;
10624 }
10625 else
10626 EMSG(_(e_invarg));
10627 }
10628 }
10629}
10630
10631/*
10632 * "setqflist()" function
10633 */
10634 static void
10635f_setqflist(typval_T *argvars, typval_T *rettv)
10636{
Bram Moolenaard823fa92016-08-12 16:29:27 +020010637 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010638}
10639
10640/*
10641 * "setreg()" function
10642 */
10643 static void
10644f_setreg(typval_T *argvars, typval_T *rettv)
10645{
10646 int regname;
10647 char_u *strregname;
10648 char_u *stropt;
10649 char_u *strval;
10650 int append;
10651 char_u yank_type;
10652 long block_len;
10653
10654 block_len = -1;
10655 yank_type = MAUTO;
10656 append = FALSE;
10657
10658 strregname = get_tv_string_chk(argvars);
10659 rettv->vval.v_number = 1; /* FAIL is default */
10660
10661 if (strregname == NULL)
10662 return; /* type error; errmsg already given */
10663 regname = *strregname;
10664 if (regname == 0 || regname == '@')
10665 regname = '"';
10666
10667 if (argvars[2].v_type != VAR_UNKNOWN)
10668 {
10669 stropt = get_tv_string_chk(&argvars[2]);
10670 if (stropt == NULL)
10671 return; /* type error */
10672 for (; *stropt != NUL; ++stropt)
10673 switch (*stropt)
10674 {
10675 case 'a': case 'A': /* append */
10676 append = TRUE;
10677 break;
10678 case 'v': case 'c': /* character-wise selection */
10679 yank_type = MCHAR;
10680 break;
10681 case 'V': case 'l': /* line-wise selection */
10682 yank_type = MLINE;
10683 break;
10684 case 'b': case Ctrl_V: /* block-wise selection */
10685 yank_type = MBLOCK;
10686 if (VIM_ISDIGIT(stropt[1]))
10687 {
10688 ++stropt;
10689 block_len = getdigits(&stropt) - 1;
10690 --stropt;
10691 }
10692 break;
10693 }
10694 }
10695
10696 if (argvars[1].v_type == VAR_LIST)
10697 {
10698 char_u **lstval;
10699 char_u **allocval;
10700 char_u buf[NUMBUFLEN];
10701 char_u **curval;
10702 char_u **curallocval;
10703 list_T *ll = argvars[1].vval.v_list;
10704 listitem_T *li;
10705 int len;
10706
10707 /* If the list is NULL handle like an empty list. */
10708 len = ll == NULL ? 0 : ll->lv_len;
10709
10710 /* First half: use for pointers to result lines; second half: use for
10711 * pointers to allocated copies. */
10712 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
10713 if (lstval == NULL)
10714 return;
10715 curval = lstval;
10716 allocval = lstval + len + 2;
10717 curallocval = allocval;
10718
10719 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
10720 li = li->li_next)
10721 {
10722 strval = get_tv_string_buf_chk(&li->li_tv, buf);
10723 if (strval == NULL)
10724 goto free_lstval;
10725 if (strval == buf)
10726 {
10727 /* Need to make a copy, next get_tv_string_buf_chk() will
10728 * overwrite the string. */
10729 strval = vim_strsave(buf);
10730 if (strval == NULL)
10731 goto free_lstval;
10732 *curallocval++ = strval;
10733 }
10734 *curval++ = strval;
10735 }
10736 *curval++ = NULL;
10737
10738 write_reg_contents_lst(regname, lstval, -1,
10739 append, yank_type, block_len);
10740free_lstval:
10741 while (curallocval > allocval)
10742 vim_free(*--curallocval);
10743 vim_free(lstval);
10744 }
10745 else
10746 {
10747 strval = get_tv_string_chk(&argvars[1]);
10748 if (strval == NULL)
10749 return;
10750 write_reg_contents_ex(regname, strval, -1,
10751 append, yank_type, block_len);
10752 }
10753 rettv->vval.v_number = 0;
10754}
10755
10756/*
10757 * "settabvar()" function
10758 */
10759 static void
10760f_settabvar(typval_T *argvars, typval_T *rettv)
10761{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010762 tabpage_T *save_curtab;
10763 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010764 char_u *varname, *tabvarname;
10765 typval_T *varp;
10766
10767 rettv->vval.v_number = 0;
10768
10769 if (check_restricted() || check_secure())
10770 return;
10771
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010772 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010773 varname = get_tv_string_chk(&argvars[1]);
10774 varp = &argvars[2];
10775
Bram Moolenaar4033c552017-09-16 20:54:51 +020010776 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010777 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010778 save_curtab = curtab;
10779 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010780
10781 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
10782 if (tabvarname != NULL)
10783 {
10784 STRCPY(tabvarname, "t:");
10785 STRCPY(tabvarname + 2, varname);
10786 set_var(tabvarname, varp, TRUE);
10787 vim_free(tabvarname);
10788 }
10789
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010790 /* Restore current tabpage */
10791 if (valid_tabpage(save_curtab))
10792 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010793 }
10794}
10795
10796/*
10797 * "settabwinvar()" function
10798 */
10799 static void
10800f_settabwinvar(typval_T *argvars, typval_T *rettv)
10801{
10802 setwinvar(argvars, rettv, 1);
10803}
10804
10805/*
10806 * "setwinvar()" function
10807 */
10808 static void
10809f_setwinvar(typval_T *argvars, typval_T *rettv)
10810{
10811 setwinvar(argvars, rettv, 0);
10812}
10813
10814#ifdef FEAT_CRYPT
10815/*
10816 * "sha256({string})" function
10817 */
10818 static void
10819f_sha256(typval_T *argvars, typval_T *rettv)
10820{
10821 char_u *p;
10822
10823 p = get_tv_string(&argvars[0]);
10824 rettv->vval.v_string = vim_strsave(
10825 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
10826 rettv->v_type = VAR_STRING;
10827}
10828#endif /* FEAT_CRYPT */
10829
10830/*
10831 * "shellescape({string})" function
10832 */
10833 static void
10834f_shellescape(typval_T *argvars, typval_T *rettv)
10835{
Bram Moolenaar20615522017-06-05 18:46:26 +020010836 int do_special = non_zero_arg(&argvars[1]);
10837
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010838 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar20615522017-06-05 18:46:26 +020010839 get_tv_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010840 rettv->v_type = VAR_STRING;
10841}
10842
10843/*
10844 * shiftwidth() function
10845 */
10846 static void
10847f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
10848{
10849 rettv->vval.v_number = get_sw_value(curbuf);
10850}
10851
10852/*
10853 * "simplify()" function
10854 */
10855 static void
10856f_simplify(typval_T *argvars, typval_T *rettv)
10857{
10858 char_u *p;
10859
10860 p = get_tv_string(&argvars[0]);
10861 rettv->vval.v_string = vim_strsave(p);
10862 simplify_filename(rettv->vval.v_string); /* simplify in place */
10863 rettv->v_type = VAR_STRING;
10864}
10865
10866#ifdef FEAT_FLOAT
10867/*
10868 * "sin()" function
10869 */
10870 static void
10871f_sin(typval_T *argvars, typval_T *rettv)
10872{
10873 float_T f = 0.0;
10874
10875 rettv->v_type = VAR_FLOAT;
10876 if (get_float_arg(argvars, &f) == OK)
10877 rettv->vval.v_float = sin(f);
10878 else
10879 rettv->vval.v_float = 0.0;
10880}
10881
10882/*
10883 * "sinh()" function
10884 */
10885 static void
10886f_sinh(typval_T *argvars, typval_T *rettv)
10887{
10888 float_T f = 0.0;
10889
10890 rettv->v_type = VAR_FLOAT;
10891 if (get_float_arg(argvars, &f) == OK)
10892 rettv->vval.v_float = sinh(f);
10893 else
10894 rettv->vval.v_float = 0.0;
10895}
10896#endif
10897
10898static int
10899#ifdef __BORLANDC__
10900 _RTLENTRYF
10901#endif
10902 item_compare(const void *s1, const void *s2);
10903static int
10904#ifdef __BORLANDC__
10905 _RTLENTRYF
10906#endif
10907 item_compare2(const void *s1, const void *s2);
10908
10909/* struct used in the array that's given to qsort() */
10910typedef struct
10911{
10912 listitem_T *item;
10913 int idx;
10914} sortItem_T;
10915
10916/* struct storing information about current sort */
10917typedef struct
10918{
10919 int item_compare_ic;
10920 int item_compare_numeric;
10921 int item_compare_numbers;
10922#ifdef FEAT_FLOAT
10923 int item_compare_float;
10924#endif
10925 char_u *item_compare_func;
10926 partial_T *item_compare_partial;
10927 dict_T *item_compare_selfdict;
10928 int item_compare_func_err;
10929 int item_compare_keep_zero;
10930} sortinfo_T;
10931static sortinfo_T *sortinfo = NULL;
10932static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
10933#define ITEM_COMPARE_FAIL 999
10934
10935/*
10936 * Compare functions for f_sort() and f_uniq() below.
10937 */
10938 static int
10939#ifdef __BORLANDC__
10940_RTLENTRYF
10941#endif
10942item_compare(const void *s1, const void *s2)
10943{
10944 sortItem_T *si1, *si2;
10945 typval_T *tv1, *tv2;
10946 char_u *p1, *p2;
10947 char_u *tofree1 = NULL, *tofree2 = NULL;
10948 int res;
10949 char_u numbuf1[NUMBUFLEN];
10950 char_u numbuf2[NUMBUFLEN];
10951
10952 si1 = (sortItem_T *)s1;
10953 si2 = (sortItem_T *)s2;
10954 tv1 = &si1->item->li_tv;
10955 tv2 = &si2->item->li_tv;
10956
10957 if (sortinfo->item_compare_numbers)
10958 {
10959 varnumber_T v1 = get_tv_number(tv1);
10960 varnumber_T v2 = get_tv_number(tv2);
10961
10962 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10963 }
10964
10965#ifdef FEAT_FLOAT
10966 if (sortinfo->item_compare_float)
10967 {
10968 float_T v1 = get_tv_float(tv1);
10969 float_T v2 = get_tv_float(tv2);
10970
10971 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10972 }
10973#endif
10974
10975 /* tv2string() puts quotes around a string and allocates memory. Don't do
10976 * that for string variables. Use a single quote when comparing with a
10977 * non-string to do what the docs promise. */
10978 if (tv1->v_type == VAR_STRING)
10979 {
10980 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
10981 p1 = (char_u *)"'";
10982 else
10983 p1 = tv1->vval.v_string;
10984 }
10985 else
10986 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
10987 if (tv2->v_type == VAR_STRING)
10988 {
10989 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
10990 p2 = (char_u *)"'";
10991 else
10992 p2 = tv2->vval.v_string;
10993 }
10994 else
10995 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
10996 if (p1 == NULL)
10997 p1 = (char_u *)"";
10998 if (p2 == NULL)
10999 p2 = (char_u *)"";
11000 if (!sortinfo->item_compare_numeric)
11001 {
11002 if (sortinfo->item_compare_ic)
11003 res = STRICMP(p1, p2);
11004 else
11005 res = STRCMP(p1, p2);
11006 }
11007 else
11008 {
11009 double n1, n2;
11010 n1 = strtod((char *)p1, (char **)&p1);
11011 n2 = strtod((char *)p2, (char **)&p2);
11012 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
11013 }
11014
11015 /* When the result would be zero, compare the item indexes. Makes the
11016 * sort stable. */
11017 if (res == 0 && !sortinfo->item_compare_keep_zero)
11018 res = si1->idx > si2->idx ? 1 : -1;
11019
11020 vim_free(tofree1);
11021 vim_free(tofree2);
11022 return res;
11023}
11024
11025 static int
11026#ifdef __BORLANDC__
11027_RTLENTRYF
11028#endif
11029item_compare2(const void *s1, const void *s2)
11030{
11031 sortItem_T *si1, *si2;
11032 int res;
11033 typval_T rettv;
11034 typval_T argv[3];
11035 int dummy;
11036 char_u *func_name;
11037 partial_T *partial = sortinfo->item_compare_partial;
11038
11039 /* shortcut after failure in previous call; compare all items equal */
11040 if (sortinfo->item_compare_func_err)
11041 return 0;
11042
11043 si1 = (sortItem_T *)s1;
11044 si2 = (sortItem_T *)s2;
11045
11046 if (partial == NULL)
11047 func_name = sortinfo->item_compare_func;
11048 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020011049 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011050
11051 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
11052 * in the copy without changing the original list items. */
11053 copy_tv(&si1->item->li_tv, &argv[0]);
11054 copy_tv(&si2->item->li_tv, &argv[1]);
11055
11056 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
11057 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020011058 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011059 partial, sortinfo->item_compare_selfdict);
11060 clear_tv(&argv[0]);
11061 clear_tv(&argv[1]);
11062
11063 if (res == FAIL)
11064 res = ITEM_COMPARE_FAIL;
11065 else
11066 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
11067 if (sortinfo->item_compare_func_err)
11068 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
11069 clear_tv(&rettv);
11070
11071 /* When the result would be zero, compare the pointers themselves. Makes
11072 * the sort stable. */
11073 if (res == 0 && !sortinfo->item_compare_keep_zero)
11074 res = si1->idx > si2->idx ? 1 : -1;
11075
11076 return res;
11077}
11078
11079/*
11080 * "sort({list})" function
11081 */
11082 static void
11083do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
11084{
11085 list_T *l;
11086 listitem_T *li;
11087 sortItem_T *ptrs;
11088 sortinfo_T *old_sortinfo;
11089 sortinfo_T info;
11090 long len;
11091 long i;
11092
11093 /* Pointer to current info struct used in compare function. Save and
11094 * restore the current one for nested calls. */
11095 old_sortinfo = sortinfo;
11096 sortinfo = &info;
11097
11098 if (argvars[0].v_type != VAR_LIST)
11099 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
11100 else
11101 {
11102 l = argvars[0].vval.v_list;
11103 if (l == NULL || tv_check_lock(l->lv_lock,
11104 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
11105 TRUE))
11106 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011107 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011108
11109 len = list_len(l);
11110 if (len <= 1)
11111 goto theend; /* short list sorts pretty quickly */
11112
11113 info.item_compare_ic = FALSE;
11114 info.item_compare_numeric = FALSE;
11115 info.item_compare_numbers = FALSE;
11116#ifdef FEAT_FLOAT
11117 info.item_compare_float = FALSE;
11118#endif
11119 info.item_compare_func = NULL;
11120 info.item_compare_partial = NULL;
11121 info.item_compare_selfdict = NULL;
11122 if (argvars[1].v_type != VAR_UNKNOWN)
11123 {
11124 /* optional second argument: {func} */
11125 if (argvars[1].v_type == VAR_FUNC)
11126 info.item_compare_func = argvars[1].vval.v_string;
11127 else if (argvars[1].v_type == VAR_PARTIAL)
11128 info.item_compare_partial = argvars[1].vval.v_partial;
11129 else
11130 {
11131 int error = FALSE;
11132
11133 i = (long)get_tv_number_chk(&argvars[1], &error);
11134 if (error)
11135 goto theend; /* type error; errmsg already given */
11136 if (i == 1)
11137 info.item_compare_ic = TRUE;
11138 else if (argvars[1].v_type != VAR_NUMBER)
11139 info.item_compare_func = get_tv_string(&argvars[1]);
11140 else if (i != 0)
11141 {
11142 EMSG(_(e_invarg));
11143 goto theend;
11144 }
11145 if (info.item_compare_func != NULL)
11146 {
11147 if (*info.item_compare_func == NUL)
11148 {
11149 /* empty string means default sort */
11150 info.item_compare_func = NULL;
11151 }
11152 else if (STRCMP(info.item_compare_func, "n") == 0)
11153 {
11154 info.item_compare_func = NULL;
11155 info.item_compare_numeric = TRUE;
11156 }
11157 else if (STRCMP(info.item_compare_func, "N") == 0)
11158 {
11159 info.item_compare_func = NULL;
11160 info.item_compare_numbers = TRUE;
11161 }
11162#ifdef FEAT_FLOAT
11163 else if (STRCMP(info.item_compare_func, "f") == 0)
11164 {
11165 info.item_compare_func = NULL;
11166 info.item_compare_float = TRUE;
11167 }
11168#endif
11169 else if (STRCMP(info.item_compare_func, "i") == 0)
11170 {
11171 info.item_compare_func = NULL;
11172 info.item_compare_ic = TRUE;
11173 }
11174 }
11175 }
11176
11177 if (argvars[2].v_type != VAR_UNKNOWN)
11178 {
11179 /* optional third argument: {dict} */
11180 if (argvars[2].v_type != VAR_DICT)
11181 {
11182 EMSG(_(e_dictreq));
11183 goto theend;
11184 }
11185 info.item_compare_selfdict = argvars[2].vval.v_dict;
11186 }
11187 }
11188
11189 /* Make an array with each entry pointing to an item in the List. */
11190 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
11191 if (ptrs == NULL)
11192 goto theend;
11193
11194 i = 0;
11195 if (sort)
11196 {
11197 /* sort(): ptrs will be the list to sort */
11198 for (li = l->lv_first; li != NULL; li = li->li_next)
11199 {
11200 ptrs[i].item = li;
11201 ptrs[i].idx = i;
11202 ++i;
11203 }
11204
11205 info.item_compare_func_err = FALSE;
11206 info.item_compare_keep_zero = FALSE;
11207 /* test the compare function */
11208 if ((info.item_compare_func != NULL
11209 || info.item_compare_partial != NULL)
11210 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11211 == ITEM_COMPARE_FAIL)
11212 EMSG(_("E702: Sort compare function failed"));
11213 else
11214 {
11215 /* Sort the array with item pointers. */
11216 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
11217 info.item_compare_func == NULL
11218 && info.item_compare_partial == NULL
11219 ? item_compare : item_compare2);
11220
11221 if (!info.item_compare_func_err)
11222 {
11223 /* Clear the List and append the items in sorted order. */
11224 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
11225 l->lv_len = 0;
11226 for (i = 0; i < len; ++i)
11227 list_append(l, ptrs[i].item);
11228 }
11229 }
11230 }
11231 else
11232 {
11233 int (*item_compare_func_ptr)(const void *, const void *);
11234
11235 /* f_uniq(): ptrs will be a stack of items to remove */
11236 info.item_compare_func_err = FALSE;
11237 info.item_compare_keep_zero = TRUE;
11238 item_compare_func_ptr = info.item_compare_func != NULL
11239 || info.item_compare_partial != NULL
11240 ? item_compare2 : item_compare;
11241
11242 for (li = l->lv_first; li != NULL && li->li_next != NULL;
11243 li = li->li_next)
11244 {
11245 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
11246 == 0)
11247 ptrs[i++].item = li;
11248 if (info.item_compare_func_err)
11249 {
11250 EMSG(_("E882: Uniq compare function failed"));
11251 break;
11252 }
11253 }
11254
11255 if (!info.item_compare_func_err)
11256 {
11257 while (--i >= 0)
11258 {
11259 li = ptrs[i].item->li_next;
11260 ptrs[i].item->li_next = li->li_next;
11261 if (li->li_next != NULL)
11262 li->li_next->li_prev = ptrs[i].item;
11263 else
11264 l->lv_last = ptrs[i].item;
11265 list_fix_watch(l, li);
11266 listitem_free(li);
11267 l->lv_len--;
11268 }
11269 }
11270 }
11271
11272 vim_free(ptrs);
11273 }
11274theend:
11275 sortinfo = old_sortinfo;
11276}
11277
11278/*
11279 * "sort({list})" function
11280 */
11281 static void
11282f_sort(typval_T *argvars, typval_T *rettv)
11283{
11284 do_sort_uniq(argvars, rettv, TRUE);
11285}
11286
11287/*
11288 * "uniq({list})" function
11289 */
11290 static void
11291f_uniq(typval_T *argvars, typval_T *rettv)
11292{
11293 do_sort_uniq(argvars, rettv, FALSE);
11294}
11295
11296/*
11297 * "soundfold({word})" function
11298 */
11299 static void
11300f_soundfold(typval_T *argvars, typval_T *rettv)
11301{
11302 char_u *s;
11303
11304 rettv->v_type = VAR_STRING;
11305 s = get_tv_string(&argvars[0]);
11306#ifdef FEAT_SPELL
11307 rettv->vval.v_string = eval_soundfold(s);
11308#else
11309 rettv->vval.v_string = vim_strsave(s);
11310#endif
11311}
11312
11313/*
11314 * "spellbadword()" function
11315 */
11316 static void
11317f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
11318{
11319 char_u *word = (char_u *)"";
11320 hlf_T attr = HLF_COUNT;
11321 int len = 0;
11322
11323 if (rettv_list_alloc(rettv) == FAIL)
11324 return;
11325
11326#ifdef FEAT_SPELL
11327 if (argvars[0].v_type == VAR_UNKNOWN)
11328 {
11329 /* Find the start and length of the badly spelled word. */
11330 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
11331 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011332 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011333 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011334 curwin->w_set_curswant = TRUE;
11335 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011336 }
11337 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
11338 {
11339 char_u *str = get_tv_string_chk(&argvars[0]);
11340 int capcol = -1;
11341
11342 if (str != NULL)
11343 {
11344 /* Check the argument for spelling. */
11345 while (*str != NUL)
11346 {
11347 len = spell_check(curwin, str, &attr, &capcol, FALSE);
11348 if (attr != HLF_COUNT)
11349 {
11350 word = str;
11351 break;
11352 }
11353 str += len;
11354 }
11355 }
11356 }
11357#endif
11358
11359 list_append_string(rettv->vval.v_list, word, len);
11360 list_append_string(rettv->vval.v_list, (char_u *)(
11361 attr == HLF_SPB ? "bad" :
11362 attr == HLF_SPR ? "rare" :
11363 attr == HLF_SPL ? "local" :
11364 attr == HLF_SPC ? "caps" :
11365 ""), -1);
11366}
11367
11368/*
11369 * "spellsuggest()" function
11370 */
11371 static void
11372f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
11373{
11374#ifdef FEAT_SPELL
11375 char_u *str;
11376 int typeerr = FALSE;
11377 int maxcount;
11378 garray_T ga;
11379 int i;
11380 listitem_T *li;
11381 int need_capital = FALSE;
11382#endif
11383
11384 if (rettv_list_alloc(rettv) == FAIL)
11385 return;
11386
11387#ifdef FEAT_SPELL
11388 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
11389 {
11390 str = get_tv_string(&argvars[0]);
11391 if (argvars[1].v_type != VAR_UNKNOWN)
11392 {
11393 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
11394 if (maxcount <= 0)
11395 return;
11396 if (argvars[2].v_type != VAR_UNKNOWN)
11397 {
11398 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
11399 if (typeerr)
11400 return;
11401 }
11402 }
11403 else
11404 maxcount = 25;
11405
11406 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
11407
11408 for (i = 0; i < ga.ga_len; ++i)
11409 {
11410 str = ((char_u **)ga.ga_data)[i];
11411
11412 li = listitem_alloc();
11413 if (li == NULL)
11414 vim_free(str);
11415 else
11416 {
11417 li->li_tv.v_type = VAR_STRING;
11418 li->li_tv.v_lock = 0;
11419 li->li_tv.vval.v_string = str;
11420 list_append(rettv->vval.v_list, li);
11421 }
11422 }
11423 ga_clear(&ga);
11424 }
11425#endif
11426}
11427
11428 static void
11429f_split(typval_T *argvars, typval_T *rettv)
11430{
11431 char_u *str;
11432 char_u *end;
11433 char_u *pat = NULL;
11434 regmatch_T regmatch;
11435 char_u patbuf[NUMBUFLEN];
11436 char_u *save_cpo;
11437 int match;
11438 colnr_T col = 0;
11439 int keepempty = FALSE;
11440 int typeerr = FALSE;
11441
11442 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11443 save_cpo = p_cpo;
11444 p_cpo = (char_u *)"";
11445
11446 str = get_tv_string(&argvars[0]);
11447 if (argvars[1].v_type != VAR_UNKNOWN)
11448 {
11449 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11450 if (pat == NULL)
11451 typeerr = TRUE;
11452 if (argvars[2].v_type != VAR_UNKNOWN)
11453 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
11454 }
11455 if (pat == NULL || *pat == NUL)
11456 pat = (char_u *)"[\\x01- ]\\+";
11457
11458 if (rettv_list_alloc(rettv) == FAIL)
11459 return;
11460 if (typeerr)
11461 return;
11462
11463 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11464 if (regmatch.regprog != NULL)
11465 {
11466 regmatch.rm_ic = FALSE;
11467 while (*str != NUL || keepempty)
11468 {
11469 if (*str == NUL)
11470 match = FALSE; /* empty item at the end */
11471 else
11472 match = vim_regexec_nl(&regmatch, str, col);
11473 if (match)
11474 end = regmatch.startp[0];
11475 else
11476 end = str + STRLEN(str);
11477 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
11478 && *str != NUL && match && end < regmatch.endp[0]))
11479 {
11480 if (list_append_string(rettv->vval.v_list, str,
11481 (int)(end - str)) == FAIL)
11482 break;
11483 }
11484 if (!match)
11485 break;
11486 /* Advance to just after the match. */
11487 if (regmatch.endp[0] > str)
11488 col = 0;
11489 else
11490 {
11491 /* Don't get stuck at the same match. */
11492#ifdef FEAT_MBYTE
11493 col = (*mb_ptr2len)(regmatch.endp[0]);
11494#else
11495 col = 1;
11496#endif
11497 }
11498 str = regmatch.endp[0];
11499 }
11500
11501 vim_regfree(regmatch.regprog);
11502 }
11503
11504 p_cpo = save_cpo;
11505}
11506
11507#ifdef FEAT_FLOAT
11508/*
11509 * "sqrt()" function
11510 */
11511 static void
11512f_sqrt(typval_T *argvars, typval_T *rettv)
11513{
11514 float_T f = 0.0;
11515
11516 rettv->v_type = VAR_FLOAT;
11517 if (get_float_arg(argvars, &f) == OK)
11518 rettv->vval.v_float = sqrt(f);
11519 else
11520 rettv->vval.v_float = 0.0;
11521}
11522
11523/*
11524 * "str2float()" function
11525 */
11526 static void
11527f_str2float(typval_T *argvars, typval_T *rettv)
11528{
11529 char_u *p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011530 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011531
Bram Moolenaar08243d22017-01-10 16:12:29 +010011532 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011533 p = skipwhite(p + 1);
11534 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011535 if (isneg)
11536 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011537 rettv->v_type = VAR_FLOAT;
11538}
11539#endif
11540
11541/*
11542 * "str2nr()" function
11543 */
11544 static void
11545f_str2nr(typval_T *argvars, typval_T *rettv)
11546{
11547 int base = 10;
11548 char_u *p;
11549 varnumber_T n;
11550 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010011551 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011552
11553 if (argvars[1].v_type != VAR_UNKNOWN)
11554 {
11555 base = (int)get_tv_number(&argvars[1]);
11556 if (base != 2 && base != 8 && base != 10 && base != 16)
11557 {
11558 EMSG(_(e_invarg));
11559 return;
11560 }
11561 }
11562
11563 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011564 isneg = (*p == '-');
11565 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011566 p = skipwhite(p + 1);
11567 switch (base)
11568 {
11569 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
11570 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
11571 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
11572 default: what = 0;
11573 }
11574 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011575 if (isneg)
11576 rettv->vval.v_number = -n;
11577 else
11578 rettv->vval.v_number = n;
11579
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011580}
11581
11582#ifdef HAVE_STRFTIME
11583/*
11584 * "strftime({format}[, {time}])" function
11585 */
11586 static void
11587f_strftime(typval_T *argvars, typval_T *rettv)
11588{
11589 char_u result_buf[256];
11590 struct tm *curtime;
11591 time_t seconds;
11592 char_u *p;
11593
11594 rettv->v_type = VAR_STRING;
11595
11596 p = get_tv_string(&argvars[0]);
11597 if (argvars[1].v_type == VAR_UNKNOWN)
11598 seconds = time(NULL);
11599 else
11600 seconds = (time_t)get_tv_number(&argvars[1]);
11601 curtime = localtime(&seconds);
11602 /* MSVC returns NULL for an invalid value of seconds. */
11603 if (curtime == NULL)
11604 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
11605 else
11606 {
11607# ifdef FEAT_MBYTE
11608 vimconv_T conv;
11609 char_u *enc;
11610
11611 conv.vc_type = CONV_NONE;
11612 enc = enc_locale();
11613 convert_setup(&conv, p_enc, enc);
11614 if (conv.vc_type != CONV_NONE)
11615 p = string_convert(&conv, p, NULL);
11616# endif
11617 if (p != NULL)
11618 (void)strftime((char *)result_buf, sizeof(result_buf),
11619 (char *)p, curtime);
11620 else
11621 result_buf[0] = NUL;
11622
11623# ifdef FEAT_MBYTE
11624 if (conv.vc_type != CONV_NONE)
11625 vim_free(p);
11626 convert_setup(&conv, enc, p_enc);
11627 if (conv.vc_type != CONV_NONE)
11628 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
11629 else
11630# endif
11631 rettv->vval.v_string = vim_strsave(result_buf);
11632
11633# ifdef FEAT_MBYTE
11634 /* Release conversion descriptors */
11635 convert_setup(&conv, NULL, NULL);
11636 vim_free(enc);
11637# endif
11638 }
11639}
11640#endif
11641
11642/*
11643 * "strgetchar()" function
11644 */
11645 static void
11646f_strgetchar(typval_T *argvars, typval_T *rettv)
11647{
11648 char_u *str;
11649 int len;
11650 int error = FALSE;
11651 int charidx;
11652
11653 rettv->vval.v_number = -1;
11654 str = get_tv_string_chk(&argvars[0]);
11655 if (str == NULL)
11656 return;
11657 len = (int)STRLEN(str);
11658 charidx = (int)get_tv_number_chk(&argvars[1], &error);
11659 if (error)
11660 return;
11661#ifdef FEAT_MBYTE
11662 {
11663 int byteidx = 0;
11664
11665 while (charidx >= 0 && byteidx < len)
11666 {
11667 if (charidx == 0)
11668 {
11669 rettv->vval.v_number = mb_ptr2char(str + byteidx);
11670 break;
11671 }
11672 --charidx;
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011673 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011674 }
11675 }
11676#else
11677 if (charidx < len)
11678 rettv->vval.v_number = str[charidx];
11679#endif
11680}
11681
11682/*
11683 * "stridx()" function
11684 */
11685 static void
11686f_stridx(typval_T *argvars, typval_T *rettv)
11687{
11688 char_u buf[NUMBUFLEN];
11689 char_u *needle;
11690 char_u *haystack;
11691 char_u *save_haystack;
11692 char_u *pos;
11693 int start_idx;
11694
11695 needle = get_tv_string_chk(&argvars[1]);
11696 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
11697 rettv->vval.v_number = -1;
11698 if (needle == NULL || haystack == NULL)
11699 return; /* type error; errmsg already given */
11700
11701 if (argvars[2].v_type != VAR_UNKNOWN)
11702 {
11703 int error = FALSE;
11704
11705 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
11706 if (error || start_idx >= (int)STRLEN(haystack))
11707 return;
11708 if (start_idx >= 0)
11709 haystack += start_idx;
11710 }
11711
11712 pos = (char_u *)strstr((char *)haystack, (char *)needle);
11713 if (pos != NULL)
11714 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
11715}
11716
11717/*
11718 * "string()" function
11719 */
11720 static void
11721f_string(typval_T *argvars, typval_T *rettv)
11722{
11723 char_u *tofree;
11724 char_u numbuf[NUMBUFLEN];
11725
11726 rettv->v_type = VAR_STRING;
11727 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
11728 get_copyID());
11729 /* Make a copy if we have a value but it's not in allocated memory. */
11730 if (rettv->vval.v_string != NULL && tofree == NULL)
11731 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
11732}
11733
11734/*
11735 * "strlen()" function
11736 */
11737 static void
11738f_strlen(typval_T *argvars, typval_T *rettv)
11739{
11740 rettv->vval.v_number = (varnumber_T)(STRLEN(
11741 get_tv_string(&argvars[0])));
11742}
11743
11744/*
11745 * "strchars()" function
11746 */
11747 static void
11748f_strchars(typval_T *argvars, typval_T *rettv)
11749{
11750 char_u *s = get_tv_string(&argvars[0]);
11751 int skipcc = 0;
11752#ifdef FEAT_MBYTE
11753 varnumber_T len = 0;
11754 int (*func_mb_ptr2char_adv)(char_u **pp);
11755#endif
11756
11757 if (argvars[1].v_type != VAR_UNKNOWN)
11758 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
11759 if (skipcc < 0 || skipcc > 1)
11760 EMSG(_(e_invarg));
11761 else
11762 {
11763#ifdef FEAT_MBYTE
11764 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
11765 while (*s != NUL)
11766 {
11767 func_mb_ptr2char_adv(&s);
11768 ++len;
11769 }
11770 rettv->vval.v_number = len;
11771#else
11772 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
11773#endif
11774 }
11775}
11776
11777/*
11778 * "strdisplaywidth()" function
11779 */
11780 static void
11781f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
11782{
11783 char_u *s = get_tv_string(&argvars[0]);
11784 int col = 0;
11785
11786 if (argvars[1].v_type != VAR_UNKNOWN)
11787 col = (int)get_tv_number(&argvars[1]);
11788
11789 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
11790}
11791
11792/*
11793 * "strwidth()" function
11794 */
11795 static void
11796f_strwidth(typval_T *argvars, typval_T *rettv)
11797{
11798 char_u *s = get_tv_string(&argvars[0]);
11799
11800 rettv->vval.v_number = (varnumber_T)(
11801#ifdef FEAT_MBYTE
11802 mb_string2cells(s, -1)
11803#else
11804 STRLEN(s)
11805#endif
11806 );
11807}
11808
11809/*
11810 * "strcharpart()" function
11811 */
11812 static void
11813f_strcharpart(typval_T *argvars, typval_T *rettv)
11814{
11815#ifdef FEAT_MBYTE
11816 char_u *p;
11817 int nchar;
11818 int nbyte = 0;
11819 int charlen;
11820 int len = 0;
11821 int slen;
11822 int error = FALSE;
11823
11824 p = get_tv_string(&argvars[0]);
11825 slen = (int)STRLEN(p);
11826
11827 nchar = (int)get_tv_number_chk(&argvars[1], &error);
11828 if (!error)
11829 {
11830 if (nchar > 0)
11831 while (nchar > 0 && nbyte < slen)
11832 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011833 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011834 --nchar;
11835 }
11836 else
11837 nbyte = nchar;
11838 if (argvars[2].v_type != VAR_UNKNOWN)
11839 {
11840 charlen = (int)get_tv_number(&argvars[2]);
11841 while (charlen > 0 && nbyte + len < slen)
11842 {
11843 int off = nbyte + len;
11844
11845 if (off < 0)
11846 len += 1;
11847 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011848 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011849 --charlen;
11850 }
11851 }
11852 else
11853 len = slen - nbyte; /* default: all bytes that are available. */
11854 }
11855
11856 /*
11857 * Only return the overlap between the specified part and the actual
11858 * string.
11859 */
11860 if (nbyte < 0)
11861 {
11862 len += nbyte;
11863 nbyte = 0;
11864 }
11865 else if (nbyte > slen)
11866 nbyte = slen;
11867 if (len < 0)
11868 len = 0;
11869 else if (nbyte + len > slen)
11870 len = slen - nbyte;
11871
11872 rettv->v_type = VAR_STRING;
11873 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
11874#else
11875 f_strpart(argvars, rettv);
11876#endif
11877}
11878
11879/*
11880 * "strpart()" function
11881 */
11882 static void
11883f_strpart(typval_T *argvars, typval_T *rettv)
11884{
11885 char_u *p;
11886 int n;
11887 int len;
11888 int slen;
11889 int error = FALSE;
11890
11891 p = get_tv_string(&argvars[0]);
11892 slen = (int)STRLEN(p);
11893
11894 n = (int)get_tv_number_chk(&argvars[1], &error);
11895 if (error)
11896 len = 0;
11897 else if (argvars[2].v_type != VAR_UNKNOWN)
11898 len = (int)get_tv_number(&argvars[2]);
11899 else
11900 len = slen - n; /* default len: all bytes that are available. */
11901
11902 /*
11903 * Only return the overlap between the specified part and the actual
11904 * string.
11905 */
11906 if (n < 0)
11907 {
11908 len += n;
11909 n = 0;
11910 }
11911 else if (n > slen)
11912 n = slen;
11913 if (len < 0)
11914 len = 0;
11915 else if (n + len > slen)
11916 len = slen - n;
11917
11918 rettv->v_type = VAR_STRING;
11919 rettv->vval.v_string = vim_strnsave(p + n, len);
11920}
11921
11922/*
11923 * "strridx()" function
11924 */
11925 static void
11926f_strridx(typval_T *argvars, typval_T *rettv)
11927{
11928 char_u buf[NUMBUFLEN];
11929 char_u *needle;
11930 char_u *haystack;
11931 char_u *rest;
11932 char_u *lastmatch = NULL;
11933 int haystack_len, end_idx;
11934
11935 needle = get_tv_string_chk(&argvars[1]);
11936 haystack = get_tv_string_buf_chk(&argvars[0], buf);
11937
11938 rettv->vval.v_number = -1;
11939 if (needle == NULL || haystack == NULL)
11940 return; /* type error; errmsg already given */
11941
11942 haystack_len = (int)STRLEN(haystack);
11943 if (argvars[2].v_type != VAR_UNKNOWN)
11944 {
11945 /* Third argument: upper limit for index */
11946 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
11947 if (end_idx < 0)
11948 return; /* can never find a match */
11949 }
11950 else
11951 end_idx = haystack_len;
11952
11953 if (*needle == NUL)
11954 {
11955 /* Empty string matches past the end. */
11956 lastmatch = haystack + end_idx;
11957 }
11958 else
11959 {
11960 for (rest = haystack; *rest != '\0'; ++rest)
11961 {
11962 rest = (char_u *)strstr((char *)rest, (char *)needle);
11963 if (rest == NULL || rest > haystack + end_idx)
11964 break;
11965 lastmatch = rest;
11966 }
11967 }
11968
11969 if (lastmatch == NULL)
11970 rettv->vval.v_number = -1;
11971 else
11972 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
11973}
11974
11975/*
11976 * "strtrans()" function
11977 */
11978 static void
11979f_strtrans(typval_T *argvars, typval_T *rettv)
11980{
11981 rettv->v_type = VAR_STRING;
11982 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
11983}
11984
11985/*
11986 * "submatch()" function
11987 */
11988 static void
11989f_submatch(typval_T *argvars, typval_T *rettv)
11990{
11991 int error = FALSE;
11992 int no;
11993 int retList = 0;
11994
11995 no = (int)get_tv_number_chk(&argvars[0], &error);
11996 if (error)
11997 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020011998 if (no < 0 || no >= NSUBEXP)
11999 {
Bram Moolenaar79518e22017-02-17 16:31:35 +010012000 EMSGN(_("E935: invalid submatch number: %d"), no);
12001 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012002 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012003 if (argvars[1].v_type != VAR_UNKNOWN)
12004 retList = (int)get_tv_number_chk(&argvars[1], &error);
12005 if (error)
12006 return;
12007
12008 if (retList == 0)
12009 {
12010 rettv->v_type = VAR_STRING;
12011 rettv->vval.v_string = reg_submatch(no);
12012 }
12013 else
12014 {
12015 rettv->v_type = VAR_LIST;
12016 rettv->vval.v_list = reg_submatch_list(no);
12017 }
12018}
12019
12020/*
12021 * "substitute()" function
12022 */
12023 static void
12024f_substitute(typval_T *argvars, typval_T *rettv)
12025{
12026 char_u patbuf[NUMBUFLEN];
12027 char_u subbuf[NUMBUFLEN];
12028 char_u flagsbuf[NUMBUFLEN];
12029
12030 char_u *str = get_tv_string_chk(&argvars[0]);
12031 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012032 char_u *sub = NULL;
12033 typval_T *expr = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012034 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
12035
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012036 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
12037 expr = &argvars[2];
12038 else
12039 sub = get_tv_string_buf_chk(&argvars[2], subbuf);
12040
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012041 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012042 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
12043 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012044 rettv->vval.v_string = NULL;
12045 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012046 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012047}
12048
12049/*
12050 * "synID(lnum, col, trans)" function
12051 */
12052 static void
12053f_synID(typval_T *argvars UNUSED, typval_T *rettv)
12054{
12055 int id = 0;
12056#ifdef FEAT_SYN_HL
12057 linenr_T lnum;
12058 colnr_T col;
12059 int trans;
12060 int transerr = FALSE;
12061
12062 lnum = get_tv_lnum(argvars); /* -1 on type error */
12063 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12064 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
12065
12066 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12067 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
12068 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
12069#endif
12070
12071 rettv->vval.v_number = id;
12072}
12073
12074/*
12075 * "synIDattr(id, what [, mode])" function
12076 */
12077 static void
12078f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
12079{
12080 char_u *p = NULL;
12081#ifdef FEAT_SYN_HL
12082 int id;
12083 char_u *what;
12084 char_u *mode;
12085 char_u modebuf[NUMBUFLEN];
12086 int modec;
12087
12088 id = (int)get_tv_number(&argvars[0]);
12089 what = get_tv_string(&argvars[1]);
12090 if (argvars[2].v_type != VAR_UNKNOWN)
12091 {
12092 mode = get_tv_string_buf(&argvars[2], modebuf);
12093 modec = TOLOWER_ASC(mode[0]);
12094 if (modec != 't' && modec != 'c' && modec != 'g')
12095 modec = 0; /* replace invalid with current */
12096 }
12097 else
12098 {
12099#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
12100 if (USE_24BIT)
12101 modec = 'g';
12102 else
12103#endif
12104 if (t_colors > 1)
12105 modec = 'c';
12106 else
12107 modec = 't';
12108 }
12109
12110
12111 switch (TOLOWER_ASC(what[0]))
12112 {
12113 case 'b':
12114 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12115 p = highlight_color(id, what, modec);
12116 else /* bold */
12117 p = highlight_has_attr(id, HL_BOLD, modec);
12118 break;
12119
12120 case 'f': /* fg[#] or font */
12121 p = highlight_color(id, what, modec);
12122 break;
12123
12124 case 'i':
12125 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
12126 p = highlight_has_attr(id, HL_INVERSE, modec);
12127 else /* italic */
12128 p = highlight_has_attr(id, HL_ITALIC, modec);
12129 break;
12130
12131 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020012132 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012133 break;
12134
12135 case 'r': /* reverse */
12136 p = highlight_has_attr(id, HL_INVERSE, modec);
12137 break;
12138
12139 case 's':
12140 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
12141 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020012142 /* strikeout */
12143 else if (TOLOWER_ASC(what[1]) == 't' &&
12144 TOLOWER_ASC(what[2]) == 'r')
12145 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012146 else /* standout */
12147 p = highlight_has_attr(id, HL_STANDOUT, modec);
12148 break;
12149
12150 case 'u':
12151 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
12152 /* underline */
12153 p = highlight_has_attr(id, HL_UNDERLINE, modec);
12154 else
12155 /* undercurl */
12156 p = highlight_has_attr(id, HL_UNDERCURL, modec);
12157 break;
12158 }
12159
12160 if (p != NULL)
12161 p = vim_strsave(p);
12162#endif
12163 rettv->v_type = VAR_STRING;
12164 rettv->vval.v_string = p;
12165}
12166
12167/*
12168 * "synIDtrans(id)" function
12169 */
12170 static void
12171f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
12172{
12173 int id;
12174
12175#ifdef FEAT_SYN_HL
12176 id = (int)get_tv_number(&argvars[0]);
12177
12178 if (id > 0)
12179 id = syn_get_final_id(id);
12180 else
12181#endif
12182 id = 0;
12183
12184 rettv->vval.v_number = id;
12185}
12186
12187/*
12188 * "synconcealed(lnum, col)" function
12189 */
12190 static void
12191f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
12192{
12193#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12194 linenr_T lnum;
12195 colnr_T col;
12196 int syntax_flags = 0;
12197 int cchar;
12198 int matchid = 0;
12199 char_u str[NUMBUFLEN];
12200#endif
12201
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012202 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012203
12204#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12205 lnum = get_tv_lnum(argvars); /* -1 on type error */
12206 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12207
12208 vim_memset(str, NUL, sizeof(str));
12209
12210 if (rettv_list_alloc(rettv) != FAIL)
12211 {
12212 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12213 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12214 && curwin->w_p_cole > 0)
12215 {
12216 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
12217 syntax_flags = get_syntax_info(&matchid);
12218
12219 /* get the conceal character */
12220 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
12221 {
12222 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020012223 if (cchar == NUL && curwin->w_p_cole == 1)
12224 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012225 if (cchar != NUL)
12226 {
12227# ifdef FEAT_MBYTE
12228 if (has_mbyte)
12229 (*mb_char2bytes)(cchar, str);
12230 else
12231# endif
12232 str[0] = cchar;
12233 }
12234 }
12235 }
12236
12237 list_append_number(rettv->vval.v_list,
12238 (syntax_flags & HL_CONCEAL) != 0);
12239 /* -1 to auto-determine strlen */
12240 list_append_string(rettv->vval.v_list, str, -1);
12241 list_append_number(rettv->vval.v_list, matchid);
12242 }
12243#endif
12244}
12245
12246/*
12247 * "synstack(lnum, col)" function
12248 */
12249 static void
12250f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
12251{
12252#ifdef FEAT_SYN_HL
12253 linenr_T lnum;
12254 colnr_T col;
12255 int i;
12256 int id;
12257#endif
12258
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012259 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012260
12261#ifdef FEAT_SYN_HL
12262 lnum = get_tv_lnum(argvars); /* -1 on type error */
12263 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12264
12265 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12266 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12267 && rettv_list_alloc(rettv) != FAIL)
12268 {
12269 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
12270 for (i = 0; ; ++i)
12271 {
12272 id = syn_get_stack_item(i);
12273 if (id < 0)
12274 break;
12275 if (list_append_number(rettv->vval.v_list, id) == FAIL)
12276 break;
12277 }
12278 }
12279#endif
12280}
12281
12282 static void
12283get_cmd_output_as_rettv(
12284 typval_T *argvars,
12285 typval_T *rettv,
12286 int retlist)
12287{
12288 char_u *res = NULL;
12289 char_u *p;
12290 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012291 int err = FALSE;
12292 FILE *fd;
12293 list_T *list = NULL;
12294 int flags = SHELL_SILENT;
12295
12296 rettv->v_type = VAR_STRING;
12297 rettv->vval.v_string = NULL;
12298 if (check_restricted() || check_secure())
12299 goto errret;
12300
12301 if (argvars[1].v_type != VAR_UNKNOWN)
12302 {
12303 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010012304 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012305 * command.
12306 */
12307 if ((infile = vim_tempname('i', TRUE)) == NULL)
12308 {
12309 EMSG(_(e_notmp));
12310 goto errret;
12311 }
12312
12313 fd = mch_fopen((char *)infile, WRITEBIN);
12314 if (fd == NULL)
12315 {
12316 EMSG2(_(e_notopen), infile);
12317 goto errret;
12318 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010012319 if (argvars[1].v_type == VAR_NUMBER)
12320 {
12321 linenr_T lnum;
12322 buf_T *buf;
12323
12324 buf = buflist_findnr(argvars[1].vval.v_number);
12325 if (buf == NULL)
12326 {
12327 EMSGN(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010012328 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010012329 goto errret;
12330 }
12331
12332 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
12333 {
12334 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
12335 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
12336 {
12337 err = TRUE;
12338 break;
12339 }
12340 if (putc(NL, fd) == EOF)
12341 {
12342 err = TRUE;
12343 break;
12344 }
12345 }
12346 }
12347 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012348 {
12349 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
12350 err = TRUE;
12351 }
12352 else
12353 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010012354 size_t len;
12355 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012356
12357 p = get_tv_string_buf_chk(&argvars[1], buf);
12358 if (p == NULL)
12359 {
12360 fclose(fd);
12361 goto errret; /* type error; errmsg already given */
12362 }
12363 len = STRLEN(p);
12364 if (len > 0 && fwrite(p, len, 1, fd) != 1)
12365 err = TRUE;
12366 }
12367 if (fclose(fd) != 0)
12368 err = TRUE;
12369 if (err)
12370 {
12371 EMSG(_("E677: Error writing temp file"));
12372 goto errret;
12373 }
12374 }
12375
12376 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
12377 * echoes typeahead, that messes up the display. */
12378 if (!msg_silent)
12379 flags += SHELL_COOKED;
12380
12381 if (retlist)
12382 {
12383 int len;
12384 listitem_T *li;
12385 char_u *s = NULL;
12386 char_u *start;
12387 char_u *end;
12388 int i;
12389
12390 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
12391 if (res == NULL)
12392 goto errret;
12393
12394 list = list_alloc();
12395 if (list == NULL)
12396 goto errret;
12397
12398 for (i = 0; i < len; ++i)
12399 {
12400 start = res + i;
12401 while (i < len && res[i] != NL)
12402 ++i;
12403 end = res + i;
12404
12405 s = alloc((unsigned)(end - start + 1));
12406 if (s == NULL)
12407 goto errret;
12408
12409 for (p = s; start < end; ++p, ++start)
12410 *p = *start == NUL ? NL : *start;
12411 *p = NUL;
12412
12413 li = listitem_alloc();
12414 if (li == NULL)
12415 {
12416 vim_free(s);
12417 goto errret;
12418 }
12419 li->li_tv.v_type = VAR_STRING;
12420 li->li_tv.v_lock = 0;
12421 li->li_tv.vval.v_string = s;
12422 list_append(list, li);
12423 }
12424
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012425 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012426 list = NULL;
12427 }
12428 else
12429 {
12430 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
12431#ifdef USE_CR
12432 /* translate <CR> into <NL> */
12433 if (res != NULL)
12434 {
12435 char_u *s;
12436
12437 for (s = res; *s; ++s)
12438 {
12439 if (*s == CAR)
12440 *s = NL;
12441 }
12442 }
12443#else
12444# ifdef USE_CRNL
12445 /* translate <CR><NL> into <NL> */
12446 if (res != NULL)
12447 {
12448 char_u *s, *d;
12449
12450 d = res;
12451 for (s = res; *s; ++s)
12452 {
12453 if (s[0] == CAR && s[1] == NL)
12454 ++s;
12455 *d++ = *s;
12456 }
12457 *d = NUL;
12458 }
12459# endif
12460#endif
12461 rettv->vval.v_string = res;
12462 res = NULL;
12463 }
12464
12465errret:
12466 if (infile != NULL)
12467 {
12468 mch_remove(infile);
12469 vim_free(infile);
12470 }
12471 if (res != NULL)
12472 vim_free(res);
12473 if (list != NULL)
12474 list_free(list);
12475}
12476
12477/*
12478 * "system()" function
12479 */
12480 static void
12481f_system(typval_T *argvars, typval_T *rettv)
12482{
12483 get_cmd_output_as_rettv(argvars, rettv, FALSE);
12484}
12485
12486/*
12487 * "systemlist()" function
12488 */
12489 static void
12490f_systemlist(typval_T *argvars, typval_T *rettv)
12491{
12492 get_cmd_output_as_rettv(argvars, rettv, TRUE);
12493}
12494
12495/*
12496 * "tabpagebuflist()" function
12497 */
12498 static void
12499f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12500{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012501 tabpage_T *tp;
12502 win_T *wp = NULL;
12503
12504 if (argvars[0].v_type == VAR_UNKNOWN)
12505 wp = firstwin;
12506 else
12507 {
12508 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12509 if (tp != NULL)
12510 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12511 }
12512 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
12513 {
12514 for (; wp != NULL; wp = wp->w_next)
12515 if (list_append_number(rettv->vval.v_list,
12516 wp->w_buffer->b_fnum) == FAIL)
12517 break;
12518 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012519}
12520
12521
12522/*
12523 * "tabpagenr()" function
12524 */
12525 static void
12526f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
12527{
12528 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012529 char_u *arg;
12530
12531 if (argvars[0].v_type != VAR_UNKNOWN)
12532 {
12533 arg = get_tv_string_chk(&argvars[0]);
12534 nr = 0;
12535 if (arg != NULL)
12536 {
12537 if (STRCMP(arg, "$") == 0)
12538 nr = tabpage_index(NULL) - 1;
12539 else
12540 EMSG2(_(e_invexpr2), arg);
12541 }
12542 }
12543 else
12544 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012545 rettv->vval.v_number = nr;
12546}
12547
12548
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012549static int get_winnr(tabpage_T *tp, typval_T *argvar);
12550
12551/*
12552 * Common code for tabpagewinnr() and winnr().
12553 */
12554 static int
12555get_winnr(tabpage_T *tp, typval_T *argvar)
12556{
12557 win_T *twin;
12558 int nr = 1;
12559 win_T *wp;
12560 char_u *arg;
12561
12562 twin = (tp == curtab) ? curwin : tp->tp_curwin;
12563 if (argvar->v_type != VAR_UNKNOWN)
12564 {
12565 arg = get_tv_string_chk(argvar);
12566 if (arg == NULL)
12567 nr = 0; /* type error; errmsg already given */
12568 else if (STRCMP(arg, "$") == 0)
12569 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
12570 else if (STRCMP(arg, "#") == 0)
12571 {
12572 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
12573 if (twin == NULL)
12574 nr = 0;
12575 }
12576 else
12577 {
12578 EMSG2(_(e_invexpr2), arg);
12579 nr = 0;
12580 }
12581 }
12582
12583 if (nr > 0)
12584 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12585 wp != twin; wp = wp->w_next)
12586 {
12587 if (wp == NULL)
12588 {
12589 /* didn't find it in this tabpage */
12590 nr = 0;
12591 break;
12592 }
12593 ++nr;
12594 }
12595 return nr;
12596}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012597
12598/*
12599 * "tabpagewinnr()" function
12600 */
12601 static void
12602f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
12603{
12604 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012605 tabpage_T *tp;
12606
12607 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12608 if (tp == NULL)
12609 nr = 0;
12610 else
12611 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012612 rettv->vval.v_number = nr;
12613}
12614
12615
12616/*
12617 * "tagfiles()" function
12618 */
12619 static void
12620f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
12621{
12622 char_u *fname;
12623 tagname_T tn;
12624 int first;
12625
12626 if (rettv_list_alloc(rettv) == FAIL)
12627 return;
12628 fname = alloc(MAXPATHL);
12629 if (fname == NULL)
12630 return;
12631
12632 for (first = TRUE; ; first = FALSE)
12633 if (get_tagfname(&tn, first, fname) == FAIL
12634 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
12635 break;
12636 tagname_free(&tn);
12637 vim_free(fname);
12638}
12639
12640/*
12641 * "taglist()" function
12642 */
12643 static void
12644f_taglist(typval_T *argvars, typval_T *rettv)
12645{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012646 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012647 char_u *tag_pattern;
12648
12649 tag_pattern = get_tv_string(&argvars[0]);
12650
12651 rettv->vval.v_number = FALSE;
12652 if (*tag_pattern == NUL)
12653 return;
12654
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012655 if (argvars[1].v_type != VAR_UNKNOWN)
12656 fname = get_tv_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012657 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012658 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012659}
12660
12661/*
12662 * "tempname()" function
12663 */
12664 static void
12665f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
12666{
12667 static int x = 'A';
12668
12669 rettv->v_type = VAR_STRING;
12670 rettv->vval.v_string = vim_tempname(x, FALSE);
12671
12672 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
12673 * names. Skip 'I' and 'O', they are used for shell redirection. */
12674 do
12675 {
12676 if (x == 'Z')
12677 x = '0';
12678 else if (x == '9')
12679 x = 'A';
12680 else
12681 {
12682#ifdef EBCDIC
12683 if (x == 'I')
12684 x = 'J';
12685 else if (x == 'R')
12686 x = 'S';
12687 else
12688#endif
12689 ++x;
12690 }
12691 } while (x == 'I' || x == 'O');
12692}
12693
12694#ifdef FEAT_FLOAT
12695/*
12696 * "tan()" function
12697 */
12698 static void
12699f_tan(typval_T *argvars, typval_T *rettv)
12700{
12701 float_T f = 0.0;
12702
12703 rettv->v_type = VAR_FLOAT;
12704 if (get_float_arg(argvars, &f) == OK)
12705 rettv->vval.v_float = tan(f);
12706 else
12707 rettv->vval.v_float = 0.0;
12708}
12709
12710/*
12711 * "tanh()" function
12712 */
12713 static void
12714f_tanh(typval_T *argvars, typval_T *rettv)
12715{
12716 float_T f = 0.0;
12717
12718 rettv->v_type = VAR_FLOAT;
12719 if (get_float_arg(argvars, &f) == OK)
12720 rettv->vval.v_float = tanh(f);
12721 else
12722 rettv->vval.v_float = 0.0;
12723}
12724#endif
12725
12726/*
12727 * "test_alloc_fail(id, countdown, repeat)" function
12728 */
12729 static void
12730f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
12731{
12732 if (argvars[0].v_type != VAR_NUMBER
12733 || argvars[0].vval.v_number <= 0
12734 || argvars[1].v_type != VAR_NUMBER
12735 || argvars[1].vval.v_number < 0
12736 || argvars[2].v_type != VAR_NUMBER)
12737 EMSG(_(e_invarg));
12738 else
12739 {
12740 alloc_fail_id = argvars[0].vval.v_number;
12741 if (alloc_fail_id >= aid_last)
12742 EMSG(_(e_invarg));
12743 alloc_fail_countdown = argvars[1].vval.v_number;
12744 alloc_fail_repeat = argvars[2].vval.v_number;
12745 did_outofmem_msg = FALSE;
12746 }
12747}
12748
12749/*
12750 * "test_autochdir()"
12751 */
12752 static void
12753f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12754{
12755#if defined(FEAT_AUTOCHDIR)
12756 test_autochdir = TRUE;
12757#endif
12758}
12759
12760/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020012761 * "test_feedinput()"
12762 */
12763 static void
12764f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
12765{
12766#ifdef USE_INPUT_BUF
12767 char_u *val = get_tv_string_chk(&argvars[0]);
12768
12769 if (val != NULL)
12770 {
12771 trash_input_buf();
12772 add_to_input_buf_csi(val, (int)STRLEN(val));
12773 }
12774#endif
12775}
12776
12777/*
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012778 * "test_disable({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012779 */
12780 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012781f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012782{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012783 char_u *name = (char_u *)"";
12784 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012785 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012786
12787 if (argvars[0].v_type != VAR_STRING
12788 || (argvars[1].v_type) != VAR_NUMBER)
12789 EMSG(_(e_invarg));
12790 else
12791 {
12792 name = get_tv_string_chk(&argvars[0]);
12793 val = (int)get_tv_number(&argvars[1]);
12794
12795 if (STRCMP(name, (char_u *)"redraw") == 0)
12796 disable_redraw_for_testing = val;
12797 else if (STRCMP(name, (char_u *)"char_avail") == 0)
12798 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012799 else if (STRCMP(name, (char_u *)"starting") == 0)
12800 {
12801 if (val)
12802 {
12803 if (save_starting < 0)
12804 save_starting = starting;
12805 starting = 0;
12806 }
12807 else
12808 {
12809 starting = save_starting;
12810 save_starting = -1;
12811 }
12812 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012813 else if (STRCMP(name, (char_u *)"ALL") == 0)
12814 {
12815 disable_char_avail_for_testing = FALSE;
12816 disable_redraw_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012817 if (save_starting >= 0)
12818 {
12819 starting = save_starting;
12820 save_starting = -1;
12821 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012822 }
12823 else
12824 EMSG2(_(e_invarg2), name);
12825 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012826}
12827
12828/*
12829 * "test_garbagecollect_now()" function
12830 */
12831 static void
12832f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12833{
12834 /* This is dangerous, any Lists and Dicts used internally may be freed
12835 * while still in use. */
12836 garbage_collect(TRUE);
12837}
12838
Bram Moolenaare0c31f62017-03-01 15:07:05 +010012839/*
12840 * "test_ignore_error()" function
12841 */
12842 static void
12843f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
12844{
12845 ignore_error_for_testing(get_tv_string(&argvars[0]));
12846}
12847
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012848#ifdef FEAT_JOB_CHANNEL
12849 static void
12850f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
12851{
12852 rettv->v_type = VAR_CHANNEL;
12853 rettv->vval.v_channel = NULL;
12854}
12855#endif
12856
12857 static void
12858f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
12859{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012860 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012861}
12862
12863#ifdef FEAT_JOB_CHANNEL
12864 static void
12865f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
12866{
12867 rettv->v_type = VAR_JOB;
12868 rettv->vval.v_job = NULL;
12869}
12870#endif
12871
12872 static void
12873f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
12874{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012875 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012876}
12877
12878 static void
12879f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
12880{
12881 rettv->v_type = VAR_PARTIAL;
12882 rettv->vval.v_partial = NULL;
12883}
12884
12885 static void
12886f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
12887{
12888 rettv->v_type = VAR_STRING;
12889 rettv->vval.v_string = NULL;
12890}
12891
12892 static void
12893f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
12894{
12895 time_for_testing = (time_t)get_tv_number(&argvars[0]);
12896}
12897
12898#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
12899/*
12900 * Get a callback from "arg". It can be a Funcref or a function name.
12901 * When "arg" is zero return an empty string.
12902 * Return NULL for an invalid argument.
12903 */
12904 char_u *
12905get_callback(typval_T *arg, partial_T **pp)
12906{
12907 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
12908 {
12909 *pp = arg->vval.v_partial;
12910 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012911 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012912 }
12913 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012914 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012915 {
12916 func_ref(arg->vval.v_string);
12917 return arg->vval.v_string;
12918 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012919 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
12920 return (char_u *)"";
12921 EMSG(_("E921: Invalid callback argument"));
12922 return NULL;
12923}
12924
12925/*
12926 * Unref/free "callback" and "partial" retured by get_callback().
12927 */
12928 void
12929free_callback(char_u *callback, partial_T *partial)
12930{
12931 if (partial != NULL)
12932 partial_unref(partial);
12933 else if (callback != NULL)
12934 {
12935 func_unref(callback);
12936 vim_free(callback);
12937 }
12938}
12939#endif
12940
12941#ifdef FEAT_TIMERS
12942/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020012943 * "timer_info([timer])" function
12944 */
12945 static void
12946f_timer_info(typval_T *argvars, typval_T *rettv)
12947{
12948 timer_T *timer = NULL;
12949
12950 if (rettv_list_alloc(rettv) != OK)
12951 return;
12952 if (argvars[0].v_type != VAR_UNKNOWN)
12953 {
12954 if (argvars[0].v_type != VAR_NUMBER)
12955 EMSG(_(e_number_exp));
12956 else
12957 {
12958 timer = find_timer((int)get_tv_number(&argvars[0]));
12959 if (timer != NULL)
12960 add_timer_info(rettv, timer);
12961 }
12962 }
12963 else
12964 add_timer_info_all(rettv);
12965}
12966
12967/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012968 * "timer_pause(timer, paused)" function
12969 */
12970 static void
12971f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
12972{
12973 timer_T *timer = NULL;
12974 int paused = (int)get_tv_number(&argvars[1]);
12975
12976 if (argvars[0].v_type != VAR_NUMBER)
12977 EMSG(_(e_number_exp));
12978 else
12979 {
12980 timer = find_timer((int)get_tv_number(&argvars[0]));
12981 if (timer != NULL)
12982 timer->tr_paused = paused;
12983 }
12984}
12985
12986/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012987 * "timer_start(time, callback [, options])" function
12988 */
12989 static void
12990f_timer_start(typval_T *argvars, typval_T *rettv)
12991{
Bram Moolenaar75537a92016-09-05 22:45:28 +020012992 long msec = (long)get_tv_number(&argvars[0]);
12993 timer_T *timer;
12994 int repeat = 0;
12995 char_u *callback;
12996 dict_T *dict;
12997 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012998
Bram Moolenaar75537a92016-09-05 22:45:28 +020012999 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013000 if (check_secure())
13001 return;
13002 if (argvars[2].v_type != VAR_UNKNOWN)
13003 {
13004 if (argvars[2].v_type != VAR_DICT
13005 || (dict = argvars[2].vval.v_dict) == NULL)
13006 {
13007 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
13008 return;
13009 }
13010 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
13011 repeat = get_dict_number(dict, (char_u *)"repeat");
13012 }
13013
Bram Moolenaar75537a92016-09-05 22:45:28 +020013014 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013015 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020013016 return;
13017
13018 timer = create_timer(msec, repeat);
13019 if (timer == NULL)
13020 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013021 else
13022 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020013023 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020013024 timer->tr_callback = vim_strsave(callback);
13025 else
13026 /* pointer into the partial */
13027 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020013028 timer->tr_partial = partial;
13029 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013030 }
13031}
13032
13033/*
13034 * "timer_stop(timer)" function
13035 */
13036 static void
13037f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
13038{
13039 timer_T *timer;
13040
13041 if (argvars[0].v_type != VAR_NUMBER)
13042 {
13043 EMSG(_(e_number_exp));
13044 return;
13045 }
13046 timer = find_timer((int)get_tv_number(&argvars[0]));
13047 if (timer != NULL)
13048 stop_timer(timer);
13049}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013050
13051/*
13052 * "timer_stopall()" function
13053 */
13054 static void
13055f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13056{
13057 stop_all_timers();
13058}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013059#endif
13060
13061/*
13062 * "tolower(string)" function
13063 */
13064 static void
13065f_tolower(typval_T *argvars, typval_T *rettv)
13066{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013067 rettv->v_type = VAR_STRING;
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +010013068 rettv->vval.v_string = strlow_save(get_tv_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013069}
13070
13071/*
13072 * "toupper(string)" function
13073 */
13074 static void
13075f_toupper(typval_T *argvars, typval_T *rettv)
13076{
13077 rettv->v_type = VAR_STRING;
13078 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
13079}
13080
13081/*
13082 * "tr(string, fromstr, tostr)" function
13083 */
13084 static void
13085f_tr(typval_T *argvars, typval_T *rettv)
13086{
13087 char_u *in_str;
13088 char_u *fromstr;
13089 char_u *tostr;
13090 char_u *p;
13091#ifdef FEAT_MBYTE
13092 int inlen;
13093 int fromlen;
13094 int tolen;
13095 int idx;
13096 char_u *cpstr;
13097 int cplen;
13098 int first = TRUE;
13099#endif
13100 char_u buf[NUMBUFLEN];
13101 char_u buf2[NUMBUFLEN];
13102 garray_T ga;
13103
13104 in_str = get_tv_string(&argvars[0]);
13105 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
13106 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
13107
13108 /* Default return value: empty string. */
13109 rettv->v_type = VAR_STRING;
13110 rettv->vval.v_string = NULL;
13111 if (fromstr == NULL || tostr == NULL)
13112 return; /* type error; errmsg already given */
13113 ga_init2(&ga, (int)sizeof(char), 80);
13114
13115#ifdef FEAT_MBYTE
13116 if (!has_mbyte)
13117#endif
13118 /* not multi-byte: fromstr and tostr must be the same length */
13119 if (STRLEN(fromstr) != STRLEN(tostr))
13120 {
13121#ifdef FEAT_MBYTE
13122error:
13123#endif
13124 EMSG2(_(e_invarg2), fromstr);
13125 ga_clear(&ga);
13126 return;
13127 }
13128
13129 /* fromstr and tostr have to contain the same number of chars */
13130 while (*in_str != NUL)
13131 {
13132#ifdef FEAT_MBYTE
13133 if (has_mbyte)
13134 {
13135 inlen = (*mb_ptr2len)(in_str);
13136 cpstr = in_str;
13137 cplen = inlen;
13138 idx = 0;
13139 for (p = fromstr; *p != NUL; p += fromlen)
13140 {
13141 fromlen = (*mb_ptr2len)(p);
13142 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
13143 {
13144 for (p = tostr; *p != NUL; p += tolen)
13145 {
13146 tolen = (*mb_ptr2len)(p);
13147 if (idx-- == 0)
13148 {
13149 cplen = tolen;
13150 cpstr = p;
13151 break;
13152 }
13153 }
13154 if (*p == NUL) /* tostr is shorter than fromstr */
13155 goto error;
13156 break;
13157 }
13158 ++idx;
13159 }
13160
13161 if (first && cpstr == in_str)
13162 {
13163 /* Check that fromstr and tostr have the same number of
13164 * (multi-byte) characters. Done only once when a character
13165 * of in_str doesn't appear in fromstr. */
13166 first = FALSE;
13167 for (p = tostr; *p != NUL; p += tolen)
13168 {
13169 tolen = (*mb_ptr2len)(p);
13170 --idx;
13171 }
13172 if (idx != 0)
13173 goto error;
13174 }
13175
13176 (void)ga_grow(&ga, cplen);
13177 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
13178 ga.ga_len += cplen;
13179
13180 in_str += inlen;
13181 }
13182 else
13183#endif
13184 {
13185 /* When not using multi-byte chars we can do it faster. */
13186 p = vim_strchr(fromstr, *in_str);
13187 if (p != NULL)
13188 ga_append(&ga, tostr[p - fromstr]);
13189 else
13190 ga_append(&ga, *in_str);
13191 ++in_str;
13192 }
13193 }
13194
13195 /* add a terminating NUL */
13196 (void)ga_grow(&ga, 1);
13197 ga_append(&ga, NUL);
13198
13199 rettv->vval.v_string = ga.ga_data;
13200}
13201
13202#ifdef FEAT_FLOAT
13203/*
13204 * "trunc({float})" function
13205 */
13206 static void
13207f_trunc(typval_T *argvars, typval_T *rettv)
13208{
13209 float_T f = 0.0;
13210
13211 rettv->v_type = VAR_FLOAT;
13212 if (get_float_arg(argvars, &f) == OK)
13213 /* trunc() is not in C90, use floor() or ceil() instead. */
13214 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
13215 else
13216 rettv->vval.v_float = 0.0;
13217}
13218#endif
13219
13220/*
13221 * "type(expr)" function
13222 */
13223 static void
13224f_type(typval_T *argvars, typval_T *rettv)
13225{
13226 int n = -1;
13227
13228 switch (argvars[0].v_type)
13229 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020013230 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
13231 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013232 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020013233 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
13234 case VAR_LIST: n = VAR_TYPE_LIST; break;
13235 case VAR_DICT: n = VAR_TYPE_DICT; break;
13236 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013237 case VAR_SPECIAL:
13238 if (argvars[0].vval.v_number == VVAL_FALSE
13239 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020013240 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013241 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020013242 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013243 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020013244 case VAR_JOB: n = VAR_TYPE_JOB; break;
13245 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013246 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010013247 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013248 n = -1;
13249 break;
13250 }
13251 rettv->vval.v_number = n;
13252}
13253
13254/*
13255 * "undofile(name)" function
13256 */
13257 static void
13258f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
13259{
13260 rettv->v_type = VAR_STRING;
13261#ifdef FEAT_PERSISTENT_UNDO
13262 {
13263 char_u *fname = get_tv_string(&argvars[0]);
13264
13265 if (*fname == NUL)
13266 {
13267 /* If there is no file name there will be no undo file. */
13268 rettv->vval.v_string = NULL;
13269 }
13270 else
13271 {
13272 char_u *ffname = FullName_save(fname, FALSE);
13273
13274 if (ffname != NULL)
13275 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
13276 vim_free(ffname);
13277 }
13278 }
13279#else
13280 rettv->vval.v_string = NULL;
13281#endif
13282}
13283
13284/*
13285 * "undotree()" function
13286 */
13287 static void
13288f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
13289{
13290 if (rettv_dict_alloc(rettv) == OK)
13291 {
13292 dict_T *dict = rettv->vval.v_dict;
13293 list_T *list;
13294
13295 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
13296 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
13297 dict_add_nr_str(dict, "save_last",
13298 (long)curbuf->b_u_save_nr_last, NULL);
13299 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
13300 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
13301 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
13302
13303 list = list_alloc();
13304 if (list != NULL)
13305 {
13306 u_eval_tree(curbuf->b_u_oldhead, list);
13307 dict_add_list(dict, "entries", list);
13308 }
13309 }
13310}
13311
13312/*
13313 * "values(dict)" function
13314 */
13315 static void
13316f_values(typval_T *argvars, typval_T *rettv)
13317{
13318 dict_list(argvars, rettv, 1);
13319}
13320
13321/*
13322 * "virtcol(string)" function
13323 */
13324 static void
13325f_virtcol(typval_T *argvars, typval_T *rettv)
13326{
13327 colnr_T vcol = 0;
13328 pos_T *fp;
13329 int fnum = curbuf->b_fnum;
13330
13331 fp = var2fpos(&argvars[0], FALSE, &fnum);
13332 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
13333 && fnum == curbuf->b_fnum)
13334 {
13335 getvvcol(curwin, fp, NULL, NULL, &vcol);
13336 ++vcol;
13337 }
13338
13339 rettv->vval.v_number = vcol;
13340}
13341
13342/*
13343 * "visualmode()" function
13344 */
13345 static void
13346f_visualmode(typval_T *argvars, typval_T *rettv)
13347{
13348 char_u str[2];
13349
13350 rettv->v_type = VAR_STRING;
13351 str[0] = curbuf->b_visual_mode_eval;
13352 str[1] = NUL;
13353 rettv->vval.v_string = vim_strsave(str);
13354
13355 /* A non-zero number or non-empty string argument: reset mode. */
13356 if (non_zero_arg(&argvars[0]))
13357 curbuf->b_visual_mode_eval = NUL;
13358}
13359
13360/*
13361 * "wildmenumode()" function
13362 */
13363 static void
13364f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13365{
13366#ifdef FEAT_WILDMENU
13367 if (wild_menu_showing)
13368 rettv->vval.v_number = 1;
13369#endif
13370}
13371
13372/*
13373 * "winbufnr(nr)" function
13374 */
13375 static void
13376f_winbufnr(typval_T *argvars, typval_T *rettv)
13377{
13378 win_T *wp;
13379
13380 wp = find_win_by_nr(&argvars[0], NULL);
13381 if (wp == NULL)
13382 rettv->vval.v_number = -1;
13383 else
13384 rettv->vval.v_number = wp->w_buffer->b_fnum;
13385}
13386
13387/*
13388 * "wincol()" function
13389 */
13390 static void
13391f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
13392{
13393 validate_cursor();
13394 rettv->vval.v_number = curwin->w_wcol + 1;
13395}
13396
13397/*
13398 * "winheight(nr)" function
13399 */
13400 static void
13401f_winheight(typval_T *argvars, typval_T *rettv)
13402{
13403 win_T *wp;
13404
13405 wp = find_win_by_nr(&argvars[0], NULL);
13406 if (wp == NULL)
13407 rettv->vval.v_number = -1;
13408 else
13409 rettv->vval.v_number = wp->w_height;
13410}
13411
13412/*
13413 * "winline()" function
13414 */
13415 static void
13416f_winline(typval_T *argvars UNUSED, typval_T *rettv)
13417{
13418 validate_cursor();
13419 rettv->vval.v_number = curwin->w_wrow + 1;
13420}
13421
13422/*
13423 * "winnr()" function
13424 */
13425 static void
13426f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
13427{
13428 int nr = 1;
13429
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013430 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013431 rettv->vval.v_number = nr;
13432}
13433
13434/*
13435 * "winrestcmd()" function
13436 */
13437 static void
13438f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
13439{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013440 win_T *wp;
13441 int winnr = 1;
13442 garray_T ga;
13443 char_u buf[50];
13444
13445 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020013446 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013447 {
13448 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
13449 ga_concat(&ga, buf);
13450 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
13451 ga_concat(&ga, buf);
13452 ++winnr;
13453 }
13454 ga_append(&ga, NUL);
13455
13456 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013457 rettv->v_type = VAR_STRING;
13458}
13459
13460/*
13461 * "winrestview()" function
13462 */
13463 static void
13464f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
13465{
13466 dict_T *dict;
13467
13468 if (argvars[0].v_type != VAR_DICT
13469 || (dict = argvars[0].vval.v_dict) == NULL)
13470 EMSG(_(e_invarg));
13471 else
13472 {
13473 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
13474 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
13475 if (dict_find(dict, (char_u *)"col", -1) != NULL)
13476 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
13477#ifdef FEAT_VIRTUALEDIT
13478 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
13479 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
13480#endif
13481 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
13482 {
13483 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
13484 curwin->w_set_curswant = FALSE;
13485 }
13486
13487 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
13488 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
13489#ifdef FEAT_DIFF
13490 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
13491 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
13492#endif
13493 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
13494 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
13495 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
13496 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
13497
13498 check_cursor();
13499 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020013500 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013501 changed_window_setting();
13502
13503 if (curwin->w_topline <= 0)
13504 curwin->w_topline = 1;
13505 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
13506 curwin->w_topline = curbuf->b_ml.ml_line_count;
13507#ifdef FEAT_DIFF
13508 check_topfill(curwin, TRUE);
13509#endif
13510 }
13511}
13512
13513/*
13514 * "winsaveview()" function
13515 */
13516 static void
13517f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
13518{
13519 dict_T *dict;
13520
13521 if (rettv_dict_alloc(rettv) == FAIL)
13522 return;
13523 dict = rettv->vval.v_dict;
13524
13525 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
13526 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
13527#ifdef FEAT_VIRTUALEDIT
13528 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
13529#endif
13530 update_curswant();
13531 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
13532
13533 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
13534#ifdef FEAT_DIFF
13535 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
13536#endif
13537 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
13538 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
13539}
13540
13541/*
13542 * "winwidth(nr)" function
13543 */
13544 static void
13545f_winwidth(typval_T *argvars, typval_T *rettv)
13546{
13547 win_T *wp;
13548
13549 wp = find_win_by_nr(&argvars[0], NULL);
13550 if (wp == NULL)
13551 rettv->vval.v_number = -1;
13552 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013553 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013554}
13555
13556/*
13557 * "wordcount()" function
13558 */
13559 static void
13560f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
13561{
13562 if (rettv_dict_alloc(rettv) == FAIL)
13563 return;
13564 cursor_pos_info(rettv->vval.v_dict);
13565}
13566
13567/*
13568 * "writefile()" function
13569 */
13570 static void
13571f_writefile(typval_T *argvars, typval_T *rettv)
13572{
13573 int binary = FALSE;
13574 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013575#ifdef HAVE_FSYNC
13576 int do_fsync = p_fs;
13577#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013578 char_u *fname;
13579 FILE *fd;
13580 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013581 listitem_T *li;
13582 list_T *list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013583
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013584 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013585 if (check_restricted() || check_secure())
13586 return;
13587
13588 if (argvars[0].v_type != VAR_LIST)
13589 {
13590 EMSG2(_(e_listarg), "writefile()");
13591 return;
13592 }
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013593 list = argvars[0].vval.v_list;
13594 if (list == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013595 return;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013596 for (li = list->lv_first; li != NULL; li = li->li_next)
13597 if (get_tv_string_chk(&li->li_tv) == NULL)
13598 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013599
13600 if (argvars[2].v_type != VAR_UNKNOWN)
13601 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013602 char_u *arg2 = get_tv_string_chk(&argvars[2]);
13603
13604 if (arg2 == NULL)
13605 return;
13606 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013607 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013608 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013609 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013610#ifdef HAVE_FSYNC
13611 if (vim_strchr(arg2, 's') != NULL)
13612 do_fsync = TRUE;
13613 else if (vim_strchr(arg2, 'S') != NULL)
13614 do_fsync = FALSE;
13615#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013616 }
13617
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013618 fname = get_tv_string_chk(&argvars[1]);
13619 if (fname == NULL)
13620 return;
13621
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013622 /* Always open the file in binary mode, library functions have a mind of
13623 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013624 if (*fname == NUL || (fd = mch_fopen((char *)fname,
13625 append ? APPENDBIN : WRITEBIN)) == NULL)
13626 {
13627 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
13628 ret = -1;
13629 }
13630 else
13631 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013632 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013633 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013634#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010013635 else if (do_fsync)
13636 /* Ignore the error, the user wouldn't know what to do about it.
13637 * May happen for a device. */
13638 ignored = fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013639#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013640 fclose(fd);
13641 }
13642
13643 rettv->vval.v_number = ret;
13644}
13645
13646/*
13647 * "xor(expr, expr)" function
13648 */
13649 static void
13650f_xor(typval_T *argvars, typval_T *rettv)
13651{
13652 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
13653 ^ get_tv_number_chk(&argvars[1], NULL);
13654}
13655
13656
13657#endif /* FEAT_EVAL */