blob: 5b684bd9469928bfff072829dd8c08bfc58a0106 [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 Moolenaar73dad1e2016-07-17 22:13:49 +0200200static void f_getwinposx(typval_T *argvars, typval_T *rettv);
201static void f_getwinposy(typval_T *argvars, typval_T *rettv);
202static void f_getwinvar(typval_T *argvars, typval_T *rettv);
203static void f_glob(typval_T *argvars, typval_T *rettv);
204static void f_globpath(typval_T *argvars, typval_T *rettv);
205static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
206static void f_has(typval_T *argvars, typval_T *rettv);
207static void f_has_key(typval_T *argvars, typval_T *rettv);
208static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
209static void f_hasmapto(typval_T *argvars, typval_T *rettv);
210static void f_histadd(typval_T *argvars, typval_T *rettv);
211static void f_histdel(typval_T *argvars, typval_T *rettv);
212static void f_histget(typval_T *argvars, typval_T *rettv);
213static void f_histnr(typval_T *argvars, typval_T *rettv);
214static void f_hlID(typval_T *argvars, typval_T *rettv);
215static void f_hlexists(typval_T *argvars, typval_T *rettv);
216static void f_hostname(typval_T *argvars, typval_T *rettv);
217static void f_iconv(typval_T *argvars, typval_T *rettv);
218static void f_indent(typval_T *argvars, typval_T *rettv);
219static void f_index(typval_T *argvars, typval_T *rettv);
220static void f_input(typval_T *argvars, typval_T *rettv);
221static void f_inputdialog(typval_T *argvars, typval_T *rettv);
222static void f_inputlist(typval_T *argvars, typval_T *rettv);
223static void f_inputrestore(typval_T *argvars, typval_T *rettv);
224static void f_inputsave(typval_T *argvars, typval_T *rettv);
225static void f_inputsecret(typval_T *argvars, typval_T *rettv);
226static void f_insert(typval_T *argvars, typval_T *rettv);
227static void f_invert(typval_T *argvars, typval_T *rettv);
228static void f_isdirectory(typval_T *argvars, typval_T *rettv);
229static void f_islocked(typval_T *argvars, typval_T *rettv);
230#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
231static void f_isnan(typval_T *argvars, typval_T *rettv);
232#endif
233static void f_items(typval_T *argvars, typval_T *rettv);
234#ifdef FEAT_JOB_CHANNEL
235static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
236static void f_job_info(typval_T *argvars, typval_T *rettv);
237static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
238static void f_job_start(typval_T *argvars, typval_T *rettv);
239static void f_job_stop(typval_T *argvars, typval_T *rettv);
240static void f_job_status(typval_T *argvars, typval_T *rettv);
241#endif
242static void f_join(typval_T *argvars, typval_T *rettv);
243static void f_js_decode(typval_T *argvars, typval_T *rettv);
244static void f_js_encode(typval_T *argvars, typval_T *rettv);
245static void f_json_decode(typval_T *argvars, typval_T *rettv);
246static void f_json_encode(typval_T *argvars, typval_T *rettv);
247static void f_keys(typval_T *argvars, typval_T *rettv);
248static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
249static void f_len(typval_T *argvars, typval_T *rettv);
250static void f_libcall(typval_T *argvars, typval_T *rettv);
251static void f_libcallnr(typval_T *argvars, typval_T *rettv);
252static void f_line(typval_T *argvars, typval_T *rettv);
253static void f_line2byte(typval_T *argvars, typval_T *rettv);
254static void f_lispindent(typval_T *argvars, typval_T *rettv);
255static void f_localtime(typval_T *argvars, typval_T *rettv);
256#ifdef FEAT_FLOAT
257static void f_log(typval_T *argvars, typval_T *rettv);
258static void f_log10(typval_T *argvars, typval_T *rettv);
259#endif
260#ifdef FEAT_LUA
261static void f_luaeval(typval_T *argvars, typval_T *rettv);
262#endif
263static void f_map(typval_T *argvars, typval_T *rettv);
264static void f_maparg(typval_T *argvars, typval_T *rettv);
265static void f_mapcheck(typval_T *argvars, typval_T *rettv);
266static void f_match(typval_T *argvars, typval_T *rettv);
267static void f_matchadd(typval_T *argvars, typval_T *rettv);
268static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
269static void f_matcharg(typval_T *argvars, typval_T *rettv);
270static void f_matchdelete(typval_T *argvars, typval_T *rettv);
271static void f_matchend(typval_T *argvars, typval_T *rettv);
272static void f_matchlist(typval_T *argvars, typval_T *rettv);
273static void f_matchstr(typval_T *argvars, typval_T *rettv);
274static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
275static void f_max(typval_T *argvars, typval_T *rettv);
276static void f_min(typval_T *argvars, typval_T *rettv);
277#ifdef vim_mkdir
278static void f_mkdir(typval_T *argvars, typval_T *rettv);
279#endif
280static void f_mode(typval_T *argvars, typval_T *rettv);
281#ifdef FEAT_MZSCHEME
282static void f_mzeval(typval_T *argvars, typval_T *rettv);
283#endif
284static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
285static void f_nr2char(typval_T *argvars, typval_T *rettv);
286static void f_or(typval_T *argvars, typval_T *rettv);
287static void f_pathshorten(typval_T *argvars, typval_T *rettv);
288#ifdef FEAT_PERL
289static void f_perleval(typval_T *argvars, typval_T *rettv);
290#endif
291#ifdef FEAT_FLOAT
292static void f_pow(typval_T *argvars, typval_T *rettv);
293#endif
294static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
295static void f_printf(typval_T *argvars, typval_T *rettv);
296static void f_pumvisible(typval_T *argvars, typval_T *rettv);
297#ifdef FEAT_PYTHON3
298static void f_py3eval(typval_T *argvars, typval_T *rettv);
299#endif
300#ifdef FEAT_PYTHON
301static void f_pyeval(typval_T *argvars, typval_T *rettv);
302#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100303#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
304static void f_pyxeval(typval_T *argvars, typval_T *rettv);
305#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200306static void f_range(typval_T *argvars, typval_T *rettv);
307static void f_readfile(typval_T *argvars, typval_T *rettv);
308static void f_reltime(typval_T *argvars, typval_T *rettv);
309#ifdef FEAT_FLOAT
310static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
311#endif
312static void f_reltimestr(typval_T *argvars, typval_T *rettv);
313static void f_remote_expr(typval_T *argvars, typval_T *rettv);
314static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
315static void f_remote_peek(typval_T *argvars, typval_T *rettv);
316static void f_remote_read(typval_T *argvars, typval_T *rettv);
317static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100318static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200319static void f_remove(typval_T *argvars, typval_T *rettv);
320static void f_rename(typval_T *argvars, typval_T *rettv);
321static void f_repeat(typval_T *argvars, typval_T *rettv);
322static void f_resolve(typval_T *argvars, typval_T *rettv);
323static void f_reverse(typval_T *argvars, typval_T *rettv);
324#ifdef FEAT_FLOAT
325static void f_round(typval_T *argvars, typval_T *rettv);
326#endif
327static void f_screenattr(typval_T *argvars, typval_T *rettv);
328static void f_screenchar(typval_T *argvars, typval_T *rettv);
329static void f_screencol(typval_T *argvars, typval_T *rettv);
330static void f_screenrow(typval_T *argvars, typval_T *rettv);
331static void f_search(typval_T *argvars, typval_T *rettv);
332static void f_searchdecl(typval_T *argvars, typval_T *rettv);
333static void f_searchpair(typval_T *argvars, typval_T *rettv);
334static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
335static void f_searchpos(typval_T *argvars, typval_T *rettv);
336static void f_server2client(typval_T *argvars, typval_T *rettv);
337static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200338static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200339static void f_setbufvar(typval_T *argvars, typval_T *rettv);
340static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
341static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
342static void f_setfperm(typval_T *argvars, typval_T *rettv);
343static void f_setline(typval_T *argvars, typval_T *rettv);
344static void f_setloclist(typval_T *argvars, typval_T *rettv);
345static void f_setmatches(typval_T *argvars, typval_T *rettv);
346static void f_setpos(typval_T *argvars, typval_T *rettv);
347static void f_setqflist(typval_T *argvars, typval_T *rettv);
348static void f_setreg(typval_T *argvars, typval_T *rettv);
349static void f_settabvar(typval_T *argvars, typval_T *rettv);
350static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
351static void f_setwinvar(typval_T *argvars, typval_T *rettv);
352#ifdef FEAT_CRYPT
353static void f_sha256(typval_T *argvars, typval_T *rettv);
354#endif /* FEAT_CRYPT */
355static void f_shellescape(typval_T *argvars, typval_T *rettv);
356static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
357static void f_simplify(typval_T *argvars, typval_T *rettv);
358#ifdef FEAT_FLOAT
359static void f_sin(typval_T *argvars, typval_T *rettv);
360static void f_sinh(typval_T *argvars, typval_T *rettv);
361#endif
362static void f_sort(typval_T *argvars, typval_T *rettv);
363static void f_soundfold(typval_T *argvars, typval_T *rettv);
364static void f_spellbadword(typval_T *argvars, typval_T *rettv);
365static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
366static void f_split(typval_T *argvars, typval_T *rettv);
367#ifdef FEAT_FLOAT
368static void f_sqrt(typval_T *argvars, typval_T *rettv);
369static void f_str2float(typval_T *argvars, typval_T *rettv);
370#endif
371static void f_str2nr(typval_T *argvars, typval_T *rettv);
372static void f_strchars(typval_T *argvars, typval_T *rettv);
373#ifdef HAVE_STRFTIME
374static void f_strftime(typval_T *argvars, typval_T *rettv);
375#endif
376static void f_strgetchar(typval_T *argvars, typval_T *rettv);
377static void f_stridx(typval_T *argvars, typval_T *rettv);
378static void f_string(typval_T *argvars, typval_T *rettv);
379static void f_strlen(typval_T *argvars, typval_T *rettv);
380static void f_strcharpart(typval_T *argvars, typval_T *rettv);
381static void f_strpart(typval_T *argvars, typval_T *rettv);
382static void f_strridx(typval_T *argvars, typval_T *rettv);
383static void f_strtrans(typval_T *argvars, typval_T *rettv);
384static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
385static void f_strwidth(typval_T *argvars, typval_T *rettv);
386static void f_submatch(typval_T *argvars, typval_T *rettv);
387static void f_substitute(typval_T *argvars, typval_T *rettv);
388static void f_synID(typval_T *argvars, typval_T *rettv);
389static void f_synIDattr(typval_T *argvars, typval_T *rettv);
390static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
391static void f_synstack(typval_T *argvars, typval_T *rettv);
392static void f_synconcealed(typval_T *argvars, typval_T *rettv);
393static void f_system(typval_T *argvars, typval_T *rettv);
394static void f_systemlist(typval_T *argvars, typval_T *rettv);
395static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
396static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
397static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
398static void f_taglist(typval_T *argvars, typval_T *rettv);
399static void f_tagfiles(typval_T *argvars, typval_T *rettv);
400static void f_tempname(typval_T *argvars, typval_T *rettv);
401static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
402static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200403static void f_test_feedinput(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100404static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200405static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100406static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200407#ifdef FEAT_JOB_CHANNEL
408static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
409#endif
410static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
411#ifdef FEAT_JOB_CHANNEL
412static void f_test_null_job(typval_T *argvars, typval_T *rettv);
413#endif
414static void f_test_null_list(typval_T *argvars, typval_T *rettv);
415static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
416static void f_test_null_string(typval_T *argvars, typval_T *rettv);
417static void f_test_settime(typval_T *argvars, typval_T *rettv);
418#ifdef FEAT_FLOAT
419static void f_tan(typval_T *argvars, typval_T *rettv);
420static void f_tanh(typval_T *argvars, typval_T *rettv);
421#endif
422#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200423static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200424static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200425static void f_timer_start(typval_T *argvars, typval_T *rettv);
426static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200427static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200428#endif
429static void f_tolower(typval_T *argvars, typval_T *rettv);
430static void f_toupper(typval_T *argvars, typval_T *rettv);
431static void f_tr(typval_T *argvars, typval_T *rettv);
432#ifdef FEAT_FLOAT
433static void f_trunc(typval_T *argvars, typval_T *rettv);
434#endif
435static void f_type(typval_T *argvars, typval_T *rettv);
436static void f_undofile(typval_T *argvars, typval_T *rettv);
437static void f_undotree(typval_T *argvars, typval_T *rettv);
438static void f_uniq(typval_T *argvars, typval_T *rettv);
439static void f_values(typval_T *argvars, typval_T *rettv);
440static void f_virtcol(typval_T *argvars, typval_T *rettv);
441static void f_visualmode(typval_T *argvars, typval_T *rettv);
442static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
443static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
444static void f_win_getid(typval_T *argvars, typval_T *rettv);
445static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
446static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
447static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100448static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200449static void f_winbufnr(typval_T *argvars, typval_T *rettv);
450static void f_wincol(typval_T *argvars, typval_T *rettv);
451static void f_winheight(typval_T *argvars, typval_T *rettv);
452static void f_winline(typval_T *argvars, typval_T *rettv);
453static void f_winnr(typval_T *argvars, typval_T *rettv);
454static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
455static void f_winrestview(typval_T *argvars, typval_T *rettv);
456static void f_winsaveview(typval_T *argvars, typval_T *rettv);
457static void f_winwidth(typval_T *argvars, typval_T *rettv);
458static void f_writefile(typval_T *argvars, typval_T *rettv);
459static void f_wordcount(typval_T *argvars, typval_T *rettv);
460static void f_xor(typval_T *argvars, typval_T *rettv);
461
462/*
463 * Array with names and number of arguments of all internal functions
464 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
465 */
466static struct fst
467{
468 char *f_name; /* function name */
469 char f_min_argc; /* minimal number of arguments */
470 char f_max_argc; /* maximal number of arguments */
471 void (*f_func)(typval_T *args, typval_T *rvar);
472 /* implementation of function */
473} functions[] =
474{
475#ifdef FEAT_FLOAT
476 {"abs", 1, 1, f_abs},
477 {"acos", 1, 1, f_acos}, /* WJMc */
478#endif
479 {"add", 2, 2, f_add},
480 {"and", 2, 2, f_and},
481 {"append", 2, 2, f_append},
482 {"argc", 0, 0, f_argc},
483 {"argidx", 0, 0, f_argidx},
484 {"arglistid", 0, 2, f_arglistid},
485 {"argv", 0, 1, f_argv},
486#ifdef FEAT_FLOAT
487 {"asin", 1, 1, f_asin}, /* WJMc */
488#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100489 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200490 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaard96ff162018-02-18 22:13:29 +0100491 {"assert_equalfile", 2, 2, f_assert_equalfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200492 {"assert_exception", 1, 2, f_assert_exception},
493 {"assert_fails", 1, 2, f_assert_fails},
494 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100495 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200496 {"assert_match", 2, 3, f_assert_match},
497 {"assert_notequal", 2, 3, f_assert_notequal},
498 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100499 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200500 {"assert_true", 1, 2, f_assert_true},
501#ifdef FEAT_FLOAT
502 {"atan", 1, 1, f_atan},
503 {"atan2", 2, 2, f_atan2},
504#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100505#ifdef FEAT_BEVAL
506 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100507# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100508 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100509# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100510#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200511 {"browse", 4, 4, f_browse},
512 {"browsedir", 2, 2, f_browsedir},
513 {"bufexists", 1, 1, f_bufexists},
514 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
515 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
516 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
517 {"buflisted", 1, 1, f_buflisted},
518 {"bufloaded", 1, 1, f_bufloaded},
519 {"bufname", 1, 1, f_bufname},
520 {"bufnr", 1, 2, f_bufnr},
521 {"bufwinid", 1, 1, f_bufwinid},
522 {"bufwinnr", 1, 1, f_bufwinnr},
523 {"byte2line", 1, 1, f_byte2line},
524 {"byteidx", 2, 2, f_byteidx},
525 {"byteidxcomp", 2, 2, f_byteidxcomp},
526 {"call", 2, 3, f_call},
527#ifdef FEAT_FLOAT
528 {"ceil", 1, 1, f_ceil},
529#endif
530#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100531 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200532 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200533 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200534 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
535 {"ch_evalraw", 2, 3, f_ch_evalraw},
536 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
537 {"ch_getjob", 1, 1, f_ch_getjob},
538 {"ch_info", 1, 1, f_ch_info},
539 {"ch_log", 1, 2, f_ch_log},
540 {"ch_logfile", 1, 2, f_ch_logfile},
541 {"ch_open", 1, 2, f_ch_open},
542 {"ch_read", 1, 2, f_ch_read},
543 {"ch_readraw", 1, 2, f_ch_readraw},
544 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
545 {"ch_sendraw", 2, 3, f_ch_sendraw},
546 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200547 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200548#endif
549 {"changenr", 0, 0, f_changenr},
550 {"char2nr", 1, 2, f_char2nr},
551 {"cindent", 1, 1, f_cindent},
552 {"clearmatches", 0, 0, f_clearmatches},
553 {"col", 1, 1, f_col},
554#if defined(FEAT_INS_EXPAND)
555 {"complete", 2, 2, f_complete},
556 {"complete_add", 1, 1, f_complete_add},
557 {"complete_check", 0, 0, f_complete_check},
558#endif
559 {"confirm", 1, 4, f_confirm},
560 {"copy", 1, 1, f_copy},
561#ifdef FEAT_FLOAT
562 {"cos", 1, 1, f_cos},
563 {"cosh", 1, 1, f_cosh},
564#endif
565 {"count", 2, 4, f_count},
566 {"cscope_connection",0,3, f_cscope_connection},
567 {"cursor", 1, 3, f_cursor},
568 {"deepcopy", 1, 2, f_deepcopy},
569 {"delete", 1, 2, f_delete},
570 {"did_filetype", 0, 0, f_did_filetype},
571 {"diff_filler", 1, 1, f_diff_filler},
572 {"diff_hlID", 2, 2, f_diff_hlID},
573 {"empty", 1, 1, f_empty},
574 {"escape", 2, 2, f_escape},
575 {"eval", 1, 1, f_eval},
576 {"eventhandler", 0, 0, f_eventhandler},
577 {"executable", 1, 1, f_executable},
578 {"execute", 1, 2, f_execute},
579 {"exepath", 1, 1, f_exepath},
580 {"exists", 1, 1, f_exists},
581#ifdef FEAT_FLOAT
582 {"exp", 1, 1, f_exp},
583#endif
584 {"expand", 1, 3, f_expand},
585 {"extend", 2, 3, f_extend},
586 {"feedkeys", 1, 2, f_feedkeys},
587 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
588 {"filereadable", 1, 1, f_filereadable},
589 {"filewritable", 1, 1, f_filewritable},
590 {"filter", 2, 2, f_filter},
591 {"finddir", 1, 3, f_finddir},
592 {"findfile", 1, 3, f_findfile},
593#ifdef FEAT_FLOAT
594 {"float2nr", 1, 1, f_float2nr},
595 {"floor", 1, 1, f_floor},
596 {"fmod", 2, 2, f_fmod},
597#endif
598 {"fnameescape", 1, 1, f_fnameescape},
599 {"fnamemodify", 2, 2, f_fnamemodify},
600 {"foldclosed", 1, 1, f_foldclosed},
601 {"foldclosedend", 1, 1, f_foldclosedend},
602 {"foldlevel", 1, 1, f_foldlevel},
603 {"foldtext", 0, 0, f_foldtext},
604 {"foldtextresult", 1, 1, f_foldtextresult},
605 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200606 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200607 {"function", 1, 3, f_function},
608 {"garbagecollect", 0, 1, f_garbagecollect},
609 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200610 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200611 {"getbufline", 2, 3, f_getbufline},
612 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100613 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200614 {"getchar", 0, 1, f_getchar},
615 {"getcharmod", 0, 0, f_getcharmod},
616 {"getcharsearch", 0, 0, f_getcharsearch},
617 {"getcmdline", 0, 0, f_getcmdline},
618 {"getcmdpos", 0, 0, f_getcmdpos},
619 {"getcmdtype", 0, 0, f_getcmdtype},
620 {"getcmdwintype", 0, 0, f_getcmdwintype},
621#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200622 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200623#endif
624 {"getcurpos", 0, 0, f_getcurpos},
625 {"getcwd", 0, 2, f_getcwd},
626 {"getfontname", 0, 1, f_getfontname},
627 {"getfperm", 1, 1, f_getfperm},
628 {"getfsize", 1, 1, f_getfsize},
629 {"getftime", 1, 1, f_getftime},
630 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100631 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200632 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200633 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200634 {"getmatches", 0, 0, f_getmatches},
635 {"getpid", 0, 0, f_getpid},
636 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200637 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200638 {"getreg", 0, 3, f_getreg},
639 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200640 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200641 {"gettabvar", 2, 3, f_gettabvar},
642 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200643 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200644 {"getwinposx", 0, 0, f_getwinposx},
645 {"getwinposy", 0, 0, f_getwinposy},
646 {"getwinvar", 2, 3, f_getwinvar},
647 {"glob", 1, 4, f_glob},
648 {"glob2regpat", 1, 1, f_glob2regpat},
649 {"globpath", 2, 5, f_globpath},
650 {"has", 1, 1, f_has},
651 {"has_key", 2, 2, f_has_key},
652 {"haslocaldir", 0, 2, f_haslocaldir},
653 {"hasmapto", 1, 3, f_hasmapto},
654 {"highlightID", 1, 1, f_hlID}, /* obsolete */
655 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
656 {"histadd", 2, 2, f_histadd},
657 {"histdel", 1, 2, f_histdel},
658 {"histget", 1, 2, f_histget},
659 {"histnr", 1, 1, f_histnr},
660 {"hlID", 1, 1, f_hlID},
661 {"hlexists", 1, 1, f_hlexists},
662 {"hostname", 0, 0, f_hostname},
663 {"iconv", 3, 3, f_iconv},
664 {"indent", 1, 1, f_indent},
665 {"index", 2, 4, f_index},
666 {"input", 1, 3, f_input},
667 {"inputdialog", 1, 3, f_inputdialog},
668 {"inputlist", 1, 1, f_inputlist},
669 {"inputrestore", 0, 0, f_inputrestore},
670 {"inputsave", 0, 0, f_inputsave},
671 {"inputsecret", 1, 2, f_inputsecret},
672 {"insert", 2, 3, f_insert},
673 {"invert", 1, 1, f_invert},
674 {"isdirectory", 1, 1, f_isdirectory},
675 {"islocked", 1, 1, f_islocked},
676#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
677 {"isnan", 1, 1, f_isnan},
678#endif
679 {"items", 1, 1, f_items},
680#ifdef FEAT_JOB_CHANNEL
681 {"job_getchannel", 1, 1, f_job_getchannel},
682 {"job_info", 1, 1, f_job_info},
683 {"job_setoptions", 2, 2, f_job_setoptions},
684 {"job_start", 1, 2, f_job_start},
685 {"job_status", 1, 1, f_job_status},
686 {"job_stop", 1, 2, f_job_stop},
687#endif
688 {"join", 1, 2, f_join},
689 {"js_decode", 1, 1, f_js_decode},
690 {"js_encode", 1, 1, f_js_encode},
691 {"json_decode", 1, 1, f_json_decode},
692 {"json_encode", 1, 1, f_json_encode},
693 {"keys", 1, 1, f_keys},
694 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
695 {"len", 1, 1, f_len},
696 {"libcall", 3, 3, f_libcall},
697 {"libcallnr", 3, 3, f_libcallnr},
698 {"line", 1, 1, f_line},
699 {"line2byte", 1, 1, f_line2byte},
700 {"lispindent", 1, 1, f_lispindent},
701 {"localtime", 0, 0, f_localtime},
702#ifdef FEAT_FLOAT
703 {"log", 1, 1, f_log},
704 {"log10", 1, 1, f_log10},
705#endif
706#ifdef FEAT_LUA
707 {"luaeval", 1, 2, f_luaeval},
708#endif
709 {"map", 2, 2, f_map},
710 {"maparg", 1, 4, f_maparg},
711 {"mapcheck", 1, 3, f_mapcheck},
712 {"match", 2, 4, f_match},
713 {"matchadd", 2, 5, f_matchadd},
714 {"matchaddpos", 2, 5, f_matchaddpos},
715 {"matcharg", 1, 1, f_matcharg},
716 {"matchdelete", 1, 1, f_matchdelete},
717 {"matchend", 2, 4, f_matchend},
718 {"matchlist", 2, 4, f_matchlist},
719 {"matchstr", 2, 4, f_matchstr},
720 {"matchstrpos", 2, 4, f_matchstrpos},
721 {"max", 1, 1, f_max},
722 {"min", 1, 1, f_min},
723#ifdef vim_mkdir
724 {"mkdir", 1, 3, f_mkdir},
725#endif
726 {"mode", 0, 1, f_mode},
727#ifdef FEAT_MZSCHEME
728 {"mzeval", 1, 1, f_mzeval},
729#endif
730 {"nextnonblank", 1, 1, f_nextnonblank},
731 {"nr2char", 1, 2, f_nr2char},
732 {"or", 2, 2, f_or},
733 {"pathshorten", 1, 1, f_pathshorten},
734#ifdef FEAT_PERL
735 {"perleval", 1, 1, f_perleval},
736#endif
737#ifdef FEAT_FLOAT
738 {"pow", 2, 2, f_pow},
739#endif
740 {"prevnonblank", 1, 1, f_prevnonblank},
741 {"printf", 2, 19, f_printf},
742 {"pumvisible", 0, 0, f_pumvisible},
743#ifdef FEAT_PYTHON3
744 {"py3eval", 1, 1, f_py3eval},
745#endif
746#ifdef FEAT_PYTHON
747 {"pyeval", 1, 1, f_pyeval},
748#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100749#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
750 {"pyxeval", 1, 1, f_pyxeval},
751#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200752 {"range", 1, 3, f_range},
753 {"readfile", 1, 3, f_readfile},
754 {"reltime", 0, 2, f_reltime},
755#ifdef FEAT_FLOAT
756 {"reltimefloat", 1, 1, f_reltimefloat},
757#endif
758 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100759 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200760 {"remote_foreground", 1, 1, f_remote_foreground},
761 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100762 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200763 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100764 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200765 {"remove", 2, 3, f_remove},
766 {"rename", 2, 2, f_rename},
767 {"repeat", 2, 2, f_repeat},
768 {"resolve", 1, 1, f_resolve},
769 {"reverse", 1, 1, f_reverse},
770#ifdef FEAT_FLOAT
771 {"round", 1, 1, f_round},
772#endif
773 {"screenattr", 2, 2, f_screenattr},
774 {"screenchar", 2, 2, f_screenchar},
775 {"screencol", 0, 0, f_screencol},
776 {"screenrow", 0, 0, f_screenrow},
777 {"search", 1, 4, f_search},
778 {"searchdecl", 1, 3, f_searchdecl},
779 {"searchpair", 3, 7, f_searchpair},
780 {"searchpairpos", 3, 7, f_searchpairpos},
781 {"searchpos", 1, 4, f_searchpos},
782 {"server2client", 2, 2, f_server2client},
783 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200784 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200785 {"setbufvar", 3, 3, f_setbufvar},
786 {"setcharsearch", 1, 1, f_setcharsearch},
787 {"setcmdpos", 1, 1, f_setcmdpos},
788 {"setfperm", 2, 2, f_setfperm},
789 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200790 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200791 {"setmatches", 1, 1, f_setmatches},
792 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200793 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200794 {"setreg", 2, 3, f_setreg},
795 {"settabvar", 3, 3, f_settabvar},
796 {"settabwinvar", 4, 4, f_settabwinvar},
797 {"setwinvar", 3, 3, f_setwinvar},
798#ifdef FEAT_CRYPT
799 {"sha256", 1, 1, f_sha256},
800#endif
801 {"shellescape", 1, 2, f_shellescape},
802 {"shiftwidth", 0, 0, f_shiftwidth},
803 {"simplify", 1, 1, f_simplify},
804#ifdef FEAT_FLOAT
805 {"sin", 1, 1, f_sin},
806 {"sinh", 1, 1, f_sinh},
807#endif
808 {"sort", 1, 3, f_sort},
809 {"soundfold", 1, 1, f_soundfold},
810 {"spellbadword", 0, 1, f_spellbadword},
811 {"spellsuggest", 1, 3, f_spellsuggest},
812 {"split", 1, 3, f_split},
813#ifdef FEAT_FLOAT
814 {"sqrt", 1, 1, f_sqrt},
815 {"str2float", 1, 1, f_str2float},
816#endif
817 {"str2nr", 1, 2, f_str2nr},
818 {"strcharpart", 2, 3, f_strcharpart},
819 {"strchars", 1, 2, f_strchars},
820 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
821#ifdef HAVE_STRFTIME
822 {"strftime", 1, 2, f_strftime},
823#endif
824 {"strgetchar", 2, 2, f_strgetchar},
825 {"stridx", 2, 3, f_stridx},
826 {"string", 1, 1, f_string},
827 {"strlen", 1, 1, f_strlen},
828 {"strpart", 2, 3, f_strpart},
829 {"strridx", 2, 3, f_strridx},
830 {"strtrans", 1, 1, f_strtrans},
831 {"strwidth", 1, 1, f_strwidth},
832 {"submatch", 1, 2, f_submatch},
833 {"substitute", 4, 4, f_substitute},
834 {"synID", 3, 3, f_synID},
835 {"synIDattr", 2, 3, f_synIDattr},
836 {"synIDtrans", 1, 1, f_synIDtrans},
837 {"synconcealed", 2, 2, f_synconcealed},
838 {"synstack", 2, 2, f_synstack},
839 {"system", 1, 2, f_system},
840 {"systemlist", 1, 2, f_systemlist},
841 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
842 {"tabpagenr", 0, 1, f_tabpagenr},
843 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
844 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100845 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200846#ifdef FEAT_FLOAT
847 {"tan", 1, 1, f_tan},
848 {"tanh", 1, 1, f_tanh},
849#endif
850 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200851#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100852 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
853 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100854 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200855 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200856 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200857 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200858 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200859 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200860 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200861 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200862 {"term_getstatus", 1, 1, f_term_getstatus},
863 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200864 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200865 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200866 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200867 {"term_sendkeys", 2, 2, f_term_sendkeys},
868 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200869 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200870#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200871 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
872 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200873 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200874 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100875 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200876#ifdef FEAT_JOB_CHANNEL
877 {"test_null_channel", 0, 0, f_test_null_channel},
878#endif
879 {"test_null_dict", 0, 0, f_test_null_dict},
880#ifdef FEAT_JOB_CHANNEL
881 {"test_null_job", 0, 0, f_test_null_job},
882#endif
883 {"test_null_list", 0, 0, f_test_null_list},
884 {"test_null_partial", 0, 0, f_test_null_partial},
885 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100886 {"test_override", 2, 2, f_test_override},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200887 {"test_settime", 1, 1, f_test_settime},
888#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200889 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200890 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200891 {"timer_start", 2, 3, f_timer_start},
892 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200893 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200894#endif
895 {"tolower", 1, 1, f_tolower},
896 {"toupper", 1, 1, f_toupper},
897 {"tr", 3, 3, f_tr},
898#ifdef FEAT_FLOAT
899 {"trunc", 1, 1, f_trunc},
900#endif
901 {"type", 1, 1, f_type},
902 {"undofile", 1, 1, f_undofile},
903 {"undotree", 0, 0, f_undotree},
904 {"uniq", 1, 3, f_uniq},
905 {"values", 1, 1, f_values},
906 {"virtcol", 1, 1, f_virtcol},
907 {"visualmode", 0, 1, f_visualmode},
908 {"wildmenumode", 0, 0, f_wildmenumode},
909 {"win_findbuf", 1, 1, f_win_findbuf},
910 {"win_getid", 0, 2, f_win_getid},
911 {"win_gotoid", 1, 1, f_win_gotoid},
912 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
913 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100914 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200915 {"winbufnr", 1, 1, f_winbufnr},
916 {"wincol", 0, 0, f_wincol},
917 {"winheight", 1, 1, f_winheight},
918 {"winline", 0, 0, f_winline},
919 {"winnr", 0, 1, f_winnr},
920 {"winrestcmd", 0, 0, f_winrestcmd},
921 {"winrestview", 1, 1, f_winrestview},
922 {"winsaveview", 0, 0, f_winsaveview},
923 {"winwidth", 1, 1, f_winwidth},
924 {"wordcount", 0, 0, f_wordcount},
925 {"writefile", 2, 3, f_writefile},
926 {"xor", 2, 2, f_xor},
927};
928
929#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
930
931/*
932 * Function given to ExpandGeneric() to obtain the list of internal
933 * or user defined function names.
934 */
935 char_u *
936get_function_name(expand_T *xp, int idx)
937{
938 static int intidx = -1;
939 char_u *name;
940
941 if (idx == 0)
942 intidx = -1;
943 if (intidx < 0)
944 {
945 name = get_user_func_name(xp, idx);
946 if (name != NULL)
947 return name;
948 }
949 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
950 {
951 STRCPY(IObuff, functions[intidx].f_name);
952 STRCAT(IObuff, "(");
953 if (functions[intidx].f_max_argc == 0)
954 STRCAT(IObuff, ")");
955 return IObuff;
956 }
957
958 return NULL;
959}
960
961/*
962 * Function given to ExpandGeneric() to obtain the list of internal or
963 * user defined variable or function names.
964 */
965 char_u *
966get_expr_name(expand_T *xp, int idx)
967{
968 static int intidx = -1;
969 char_u *name;
970
971 if (idx == 0)
972 intidx = -1;
973 if (intidx < 0)
974 {
975 name = get_function_name(xp, idx);
976 if (name != NULL)
977 return name;
978 }
979 return get_user_var_name(xp, ++intidx);
980}
981
982#endif /* FEAT_CMDL_COMPL */
983
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200984/*
985 * Find internal function in table above.
986 * Return index, or -1 if not found
987 */
988 int
989find_internal_func(
990 char_u *name) /* name of the function */
991{
992 int first = 0;
993 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
994 int cmp;
995 int x;
996
997 /*
998 * Find the function name in the table. Binary search.
999 */
1000 while (first <= last)
1001 {
1002 x = first + ((unsigned)(last - first) >> 1);
1003 cmp = STRCMP(name, functions[x].f_name);
1004 if (cmp < 0)
1005 last = x - 1;
1006 else if (cmp > 0)
1007 first = x + 1;
1008 else
1009 return x;
1010 }
1011 return -1;
1012}
1013
1014 int
1015call_internal_func(
1016 char_u *name,
1017 int argcount,
1018 typval_T *argvars,
1019 typval_T *rettv)
1020{
1021 int i;
1022
1023 i = find_internal_func(name);
1024 if (i < 0)
1025 return ERROR_UNKNOWN;
1026 if (argcount < functions[i].f_min_argc)
1027 return ERROR_TOOFEW;
1028 if (argcount > functions[i].f_max_argc)
1029 return ERROR_TOOMANY;
1030 argvars[argcount].v_type = VAR_UNKNOWN;
1031 functions[i].f_func(argvars, rettv);
1032 return ERROR_NONE;
1033}
1034
1035/*
1036 * Return TRUE for a non-zero Number and a non-empty String.
1037 */
1038 static int
1039non_zero_arg(typval_T *argvars)
1040{
1041 return ((argvars[0].v_type == VAR_NUMBER
1042 && argvars[0].vval.v_number != 0)
1043 || (argvars[0].v_type == VAR_SPECIAL
1044 && argvars[0].vval.v_number == VVAL_TRUE)
1045 || (argvars[0].v_type == VAR_STRING
1046 && argvars[0].vval.v_string != NULL
1047 && *argvars[0].vval.v_string != NUL));
1048}
1049
1050/*
1051 * Get the lnum from the first argument.
1052 * Also accepts ".", "$", etc., but that only works for the current buffer.
1053 * Returns -1 on error.
1054 */
1055 static linenr_T
1056get_tv_lnum(typval_T *argvars)
1057{
1058 typval_T rettv;
1059 linenr_T lnum;
1060
1061 lnum = (linenr_T)get_tv_number_chk(&argvars[0], NULL);
1062 if (lnum == 0) /* no valid number, try using line() */
1063 {
1064 rettv.v_type = VAR_NUMBER;
1065 f_line(argvars, &rettv);
1066 lnum = (linenr_T)rettv.vval.v_number;
1067 clear_tv(&rettv);
1068 }
1069 return lnum;
1070}
1071
1072#ifdef FEAT_FLOAT
1073static int get_float_arg(typval_T *argvars, float_T *f);
1074
1075/*
1076 * Get the float value of "argvars[0]" into "f".
1077 * Returns FAIL when the argument is not a Number or Float.
1078 */
1079 static int
1080get_float_arg(typval_T *argvars, float_T *f)
1081{
1082 if (argvars[0].v_type == VAR_FLOAT)
1083 {
1084 *f = argvars[0].vval.v_float;
1085 return OK;
1086 }
1087 if (argvars[0].v_type == VAR_NUMBER)
1088 {
1089 *f = (float_T)argvars[0].vval.v_number;
1090 return OK;
1091 }
1092 EMSG(_("E808: Number or Float required"));
1093 return FAIL;
1094}
1095
1096/*
1097 * "abs(expr)" function
1098 */
1099 static void
1100f_abs(typval_T *argvars, typval_T *rettv)
1101{
1102 if (argvars[0].v_type == VAR_FLOAT)
1103 {
1104 rettv->v_type = VAR_FLOAT;
1105 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1106 }
1107 else
1108 {
1109 varnumber_T n;
1110 int error = FALSE;
1111
1112 n = get_tv_number_chk(&argvars[0], &error);
1113 if (error)
1114 rettv->vval.v_number = -1;
1115 else if (n > 0)
1116 rettv->vval.v_number = n;
1117 else
1118 rettv->vval.v_number = -n;
1119 }
1120}
1121
1122/*
1123 * "acos()" function
1124 */
1125 static void
1126f_acos(typval_T *argvars, typval_T *rettv)
1127{
1128 float_T f = 0.0;
1129
1130 rettv->v_type = VAR_FLOAT;
1131 if (get_float_arg(argvars, &f) == OK)
1132 rettv->vval.v_float = acos(f);
1133 else
1134 rettv->vval.v_float = 0.0;
1135}
1136#endif
1137
1138/*
1139 * "add(list, item)" function
1140 */
1141 static void
1142f_add(typval_T *argvars, typval_T *rettv)
1143{
1144 list_T *l;
1145
1146 rettv->vval.v_number = 1; /* Default: Failed */
1147 if (argvars[0].v_type == VAR_LIST)
1148 {
1149 if ((l = argvars[0].vval.v_list) != NULL
1150 && !tv_check_lock(l->lv_lock,
1151 (char_u *)N_("add() argument"), TRUE)
1152 && list_append_tv(l, &argvars[1]) == OK)
1153 copy_tv(&argvars[0], rettv);
1154 }
1155 else
1156 EMSG(_(e_listreq));
1157}
1158
1159/*
1160 * "and(expr, expr)" function
1161 */
1162 static void
1163f_and(typval_T *argvars, typval_T *rettv)
1164{
1165 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
1166 & get_tv_number_chk(&argvars[1], NULL);
1167}
1168
1169/*
1170 * "append(lnum, string/list)" function
1171 */
1172 static void
1173f_append(typval_T *argvars, typval_T *rettv)
1174{
1175 long lnum;
1176 char_u *line;
1177 list_T *l = NULL;
1178 listitem_T *li = NULL;
1179 typval_T *tv;
1180 long added = 0;
1181
1182 /* When coming here from Insert mode, sync undo, so that this can be
1183 * undone separately from what was previously inserted. */
1184 if (u_sync_once == 2)
1185 {
1186 u_sync_once = 1; /* notify that u_sync() was called */
1187 u_sync(TRUE);
1188 }
1189
1190 lnum = get_tv_lnum(argvars);
1191 if (lnum >= 0
1192 && lnum <= curbuf->b_ml.ml_line_count
1193 && u_save(lnum, lnum + 1) == OK)
1194 {
1195 if (argvars[1].v_type == VAR_LIST)
1196 {
1197 l = argvars[1].vval.v_list;
1198 if (l == NULL)
1199 return;
1200 li = l->lv_first;
1201 }
1202 for (;;)
1203 {
1204 if (l == NULL)
1205 tv = &argvars[1]; /* append a string */
1206 else if (li == NULL)
1207 break; /* end of list */
1208 else
1209 tv = &li->li_tv; /* append item from list */
1210 line = get_tv_string_chk(tv);
1211 if (line == NULL) /* type error */
1212 {
1213 rettv->vval.v_number = 1; /* Failed */
1214 break;
1215 }
1216 ml_append(lnum + added, line, (colnr_T)0, FALSE);
1217 ++added;
1218 if (l == NULL)
1219 break;
1220 li = li->li_next;
1221 }
1222
1223 appended_lines_mark(lnum, added);
1224 if (curwin->w_cursor.lnum > lnum)
1225 curwin->w_cursor.lnum += added;
1226 }
1227 else
1228 rettv->vval.v_number = 1; /* Failed */
1229}
1230
1231/*
1232 * "argc()" function
1233 */
1234 static void
1235f_argc(typval_T *argvars UNUSED, typval_T *rettv)
1236{
1237 rettv->vval.v_number = ARGCOUNT;
1238}
1239
1240/*
1241 * "argidx()" function
1242 */
1243 static void
1244f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1245{
1246 rettv->vval.v_number = curwin->w_arg_idx;
1247}
1248
1249/*
1250 * "arglistid()" function
1251 */
1252 static void
1253f_arglistid(typval_T *argvars, typval_T *rettv)
1254{
1255 win_T *wp;
1256
1257 rettv->vval.v_number = -1;
1258 wp = find_tabwin(&argvars[0], &argvars[1]);
1259 if (wp != NULL)
1260 rettv->vval.v_number = wp->w_alist->id;
1261}
1262
1263/*
1264 * "argv(nr)" function
1265 */
1266 static void
1267f_argv(typval_T *argvars, typval_T *rettv)
1268{
1269 int idx;
1270
1271 if (argvars[0].v_type != VAR_UNKNOWN)
1272 {
1273 idx = (int)get_tv_number_chk(&argvars[0], NULL);
1274 if (idx >= 0 && idx < ARGCOUNT)
1275 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
1276 else
1277 rettv->vval.v_string = NULL;
1278 rettv->v_type = VAR_STRING;
1279 }
1280 else if (rettv_list_alloc(rettv) == OK)
1281 for (idx = 0; idx < ARGCOUNT; ++idx)
1282 list_append_string(rettv->vval.v_list,
1283 alist_name(&ARGLIST[idx]), -1);
1284}
1285
1286/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001287 * "assert_beeps(cmd [, error])" function
1288 */
1289 static void
1290f_assert_beeps(typval_T *argvars, typval_T *rettv UNUSED)
1291{
1292 assert_beeps(argvars);
1293}
1294
1295/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001296 * "assert_equal(expected, actual[, msg])" function
1297 */
1298 static void
1299f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
1300{
1301 assert_equal_common(argvars, ASSERT_EQUAL);
1302}
1303
1304/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001305 * "assert_equalfile(fname-one, fname-two)" function
1306 */
1307 static void
1308f_assert_equalfile(typval_T *argvars, typval_T *rettv UNUSED)
1309{
1310 assert_equalfile(argvars);
1311}
1312
1313/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001314 * "assert_notequal(expected, actual[, msg])" function
1315 */
1316 static void
1317f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED)
1318{
1319 assert_equal_common(argvars, ASSERT_NOTEQUAL);
1320}
1321
1322/*
1323 * "assert_exception(string[, msg])" function
1324 */
1325 static void
1326f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
1327{
1328 assert_exception(argvars);
1329}
1330
1331/*
1332 * "assert_fails(cmd [, error])" function
1333 */
1334 static void
1335f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
1336{
1337 assert_fails(argvars);
1338}
1339
1340/*
1341 * "assert_false(actual[, msg])" function
1342 */
1343 static void
1344f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
1345{
1346 assert_bool(argvars, FALSE);
1347}
1348
1349/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001350 * "assert_inrange(lower, upper[, msg])" function
1351 */
1352 static void
1353f_assert_inrange(typval_T *argvars, typval_T *rettv UNUSED)
1354{
1355 assert_inrange(argvars);
1356}
1357
1358/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001359 * "assert_match(pattern, actual[, msg])" function
1360 */
1361 static void
1362f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
1363{
1364 assert_match_common(argvars, ASSERT_MATCH);
1365}
1366
1367/*
1368 * "assert_notmatch(pattern, actual[, msg])" function
1369 */
1370 static void
1371f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED)
1372{
1373 assert_match_common(argvars, ASSERT_NOTMATCH);
1374}
1375
1376/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001377 * "assert_report(msg)" function
1378 */
1379 static void
1380f_assert_report(typval_T *argvars, typval_T *rettv UNUSED)
1381{
1382 assert_report(argvars);
1383}
1384
1385/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001386 * "assert_true(actual[, msg])" function
1387 */
1388 static void
1389f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
1390{
1391 assert_bool(argvars, TRUE);
1392}
1393
1394#ifdef FEAT_FLOAT
1395/*
1396 * "asin()" function
1397 */
1398 static void
1399f_asin(typval_T *argvars, typval_T *rettv)
1400{
1401 float_T f = 0.0;
1402
1403 rettv->v_type = VAR_FLOAT;
1404 if (get_float_arg(argvars, &f) == OK)
1405 rettv->vval.v_float = asin(f);
1406 else
1407 rettv->vval.v_float = 0.0;
1408}
1409
1410/*
1411 * "atan()" function
1412 */
1413 static void
1414f_atan(typval_T *argvars, typval_T *rettv)
1415{
1416 float_T f = 0.0;
1417
1418 rettv->v_type = VAR_FLOAT;
1419 if (get_float_arg(argvars, &f) == OK)
1420 rettv->vval.v_float = atan(f);
1421 else
1422 rettv->vval.v_float = 0.0;
1423}
1424
1425/*
1426 * "atan2()" function
1427 */
1428 static void
1429f_atan2(typval_T *argvars, typval_T *rettv)
1430{
1431 float_T fx = 0.0, fy = 0.0;
1432
1433 rettv->v_type = VAR_FLOAT;
1434 if (get_float_arg(argvars, &fx) == OK
1435 && get_float_arg(&argvars[1], &fy) == OK)
1436 rettv->vval.v_float = atan2(fx, fy);
1437 else
1438 rettv->vval.v_float = 0.0;
1439}
1440#endif
1441
1442/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001443 * "balloon_show()" function
1444 */
1445#ifdef FEAT_BEVAL
1446 static void
1447f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1448{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001449 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001450 {
1451 if (argvars[0].v_type == VAR_LIST
1452# ifdef FEAT_GUI
1453 && !gui.in_use
1454# endif
1455 )
1456 post_balloon(balloonEval, NULL, argvars[0].vval.v_list);
1457 else
1458 post_balloon(balloonEval, get_tv_string_chk(&argvars[0]), NULL);
1459 }
1460}
1461
Bram Moolenaar669a8282017-11-19 20:13:05 +01001462# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001463 static void
1464f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1465{
1466 if (rettv_list_alloc(rettv) == OK)
1467 {
1468 char_u *msg = get_tv_string_chk(&argvars[0]);
1469
1470 if (msg != NULL)
1471 {
1472 pumitem_T *array;
1473 int size = split_message(msg, &array);
1474 int i;
1475
1476 /* Skip the first and last item, they are always empty. */
1477 for (i = 1; i < size - 1; ++i)
1478 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001479 while (size > 0)
1480 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001481 vim_free(array);
1482 }
1483 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001484}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001485# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001486#endif
1487
1488/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001489 * "browse(save, title, initdir, default)" function
1490 */
1491 static void
1492f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1493{
1494#ifdef FEAT_BROWSE
1495 int save;
1496 char_u *title;
1497 char_u *initdir;
1498 char_u *defname;
1499 char_u buf[NUMBUFLEN];
1500 char_u buf2[NUMBUFLEN];
1501 int error = FALSE;
1502
1503 save = (int)get_tv_number_chk(&argvars[0], &error);
1504 title = get_tv_string_chk(&argvars[1]);
1505 initdir = get_tv_string_buf_chk(&argvars[2], buf);
1506 defname = get_tv_string_buf_chk(&argvars[3], buf2);
1507
1508 if (error || title == NULL || initdir == NULL || defname == NULL)
1509 rettv->vval.v_string = NULL;
1510 else
1511 rettv->vval.v_string =
1512 do_browse(save ? BROWSE_SAVE : 0,
1513 title, defname, NULL, initdir, NULL, curbuf);
1514#else
1515 rettv->vval.v_string = NULL;
1516#endif
1517 rettv->v_type = VAR_STRING;
1518}
1519
1520/*
1521 * "browsedir(title, initdir)" function
1522 */
1523 static void
1524f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1525{
1526#ifdef FEAT_BROWSE
1527 char_u *title;
1528 char_u *initdir;
1529 char_u buf[NUMBUFLEN];
1530
1531 title = get_tv_string_chk(&argvars[0]);
1532 initdir = get_tv_string_buf_chk(&argvars[1], buf);
1533
1534 if (title == NULL || initdir == NULL)
1535 rettv->vval.v_string = NULL;
1536 else
1537 rettv->vval.v_string = do_browse(BROWSE_DIR,
1538 title, NULL, NULL, initdir, NULL, curbuf);
1539#else
1540 rettv->vval.v_string = NULL;
1541#endif
1542 rettv->v_type = VAR_STRING;
1543}
1544
1545static buf_T *find_buffer(typval_T *avar);
1546
1547/*
1548 * Find a buffer by number or exact name.
1549 */
1550 static buf_T *
1551find_buffer(typval_T *avar)
1552{
1553 buf_T *buf = NULL;
1554
1555 if (avar->v_type == VAR_NUMBER)
1556 buf = buflist_findnr((int)avar->vval.v_number);
1557 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1558 {
1559 buf = buflist_findname_exp(avar->vval.v_string);
1560 if (buf == NULL)
1561 {
1562 /* No full path name match, try a match with a URL or a "nofile"
1563 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001564 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001565 if (buf->b_fname != NULL
1566 && (path_with_url(buf->b_fname)
1567#ifdef FEAT_QUICKFIX
1568 || bt_nofile(buf)
1569#endif
1570 )
1571 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1572 break;
1573 }
1574 }
1575 return buf;
1576}
1577
1578/*
1579 * "bufexists(expr)" function
1580 */
1581 static void
1582f_bufexists(typval_T *argvars, typval_T *rettv)
1583{
1584 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1585}
1586
1587/*
1588 * "buflisted(expr)" function
1589 */
1590 static void
1591f_buflisted(typval_T *argvars, typval_T *rettv)
1592{
1593 buf_T *buf;
1594
1595 buf = find_buffer(&argvars[0]);
1596 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1597}
1598
1599/*
1600 * "bufloaded(expr)" function
1601 */
1602 static void
1603f_bufloaded(typval_T *argvars, typval_T *rettv)
1604{
1605 buf_T *buf;
1606
1607 buf = find_buffer(&argvars[0]);
1608 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1609}
1610
1611 buf_T *
1612buflist_find_by_name(char_u *name, int curtab_only)
1613{
1614 int save_magic;
1615 char_u *save_cpo;
1616 buf_T *buf;
1617
1618 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1619 save_magic = p_magic;
1620 p_magic = TRUE;
1621 save_cpo = p_cpo;
1622 p_cpo = (char_u *)"";
1623
1624 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1625 TRUE, FALSE, curtab_only));
1626
1627 p_magic = save_magic;
1628 p_cpo = save_cpo;
1629 return buf;
1630}
1631
1632/*
1633 * Get buffer by number or pattern.
1634 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001635 buf_T *
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001636get_buf_tv(typval_T *tv, int curtab_only)
1637{
1638 char_u *name = tv->vval.v_string;
1639 buf_T *buf;
1640
1641 if (tv->v_type == VAR_NUMBER)
1642 return buflist_findnr((int)tv->vval.v_number);
1643 if (tv->v_type != VAR_STRING)
1644 return NULL;
1645 if (name == NULL || *name == NUL)
1646 return curbuf;
1647 if (name[0] == '$' && name[1] == NUL)
1648 return lastbuf;
1649
1650 buf = buflist_find_by_name(name, curtab_only);
1651
1652 /* If not found, try expanding the name, like done for bufexists(). */
1653 if (buf == NULL)
1654 buf = find_buffer(tv);
1655
1656 return buf;
1657}
1658
1659/*
1660 * "bufname(expr)" function
1661 */
1662 static void
1663f_bufname(typval_T *argvars, typval_T *rettv)
1664{
1665 buf_T *buf;
1666
1667 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1668 ++emsg_off;
1669 buf = get_buf_tv(&argvars[0], FALSE);
1670 rettv->v_type = VAR_STRING;
1671 if (buf != NULL && buf->b_fname != NULL)
1672 rettv->vval.v_string = vim_strsave(buf->b_fname);
1673 else
1674 rettv->vval.v_string = NULL;
1675 --emsg_off;
1676}
1677
1678/*
1679 * "bufnr(expr)" function
1680 */
1681 static void
1682f_bufnr(typval_T *argvars, typval_T *rettv)
1683{
1684 buf_T *buf;
1685 int error = FALSE;
1686 char_u *name;
1687
1688 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1689 ++emsg_off;
1690 buf = get_buf_tv(&argvars[0], FALSE);
1691 --emsg_off;
1692
1693 /* If the buffer isn't found and the second argument is not zero create a
1694 * new buffer. */
1695 if (buf == NULL
1696 && argvars[1].v_type != VAR_UNKNOWN
1697 && get_tv_number_chk(&argvars[1], &error) != 0
1698 && !error
1699 && (name = get_tv_string_chk(&argvars[0])) != NULL
1700 && !error)
1701 buf = buflist_new(name, NULL, (linenr_T)1, 0);
1702
1703 if (buf != NULL)
1704 rettv->vval.v_number = buf->b_fnum;
1705 else
1706 rettv->vval.v_number = -1;
1707}
1708
1709 static void
1710buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
1711{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001712 win_T *wp;
1713 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001714 buf_T *buf;
1715
1716 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1717 ++emsg_off;
1718 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02001719 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001720 {
1721 ++winnr;
1722 if (wp->w_buffer == buf)
1723 break;
1724 }
1725 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001726 --emsg_off;
1727}
1728
1729/*
1730 * "bufwinid(nr)" function
1731 */
1732 static void
1733f_bufwinid(typval_T *argvars, typval_T *rettv)
1734{
1735 buf_win_common(argvars, rettv, FALSE);
1736}
1737
1738/*
1739 * "bufwinnr(nr)" function
1740 */
1741 static void
1742f_bufwinnr(typval_T *argvars, typval_T *rettv)
1743{
1744 buf_win_common(argvars, rettv, TRUE);
1745}
1746
1747/*
1748 * "byte2line(byte)" function
1749 */
1750 static void
1751f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1752{
1753#ifndef FEAT_BYTEOFF
1754 rettv->vval.v_number = -1;
1755#else
1756 long boff = 0;
1757
1758 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
1759 if (boff < 0)
1760 rettv->vval.v_number = -1;
1761 else
1762 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
1763 (linenr_T)0, &boff);
1764#endif
1765}
1766
1767 static void
1768byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
1769{
1770#ifdef FEAT_MBYTE
1771 char_u *t;
1772#endif
1773 char_u *str;
1774 varnumber_T idx;
1775
1776 str = get_tv_string_chk(&argvars[0]);
1777 idx = get_tv_number_chk(&argvars[1], NULL);
1778 rettv->vval.v_number = -1;
1779 if (str == NULL || idx < 0)
1780 return;
1781
1782#ifdef FEAT_MBYTE
1783 t = str;
1784 for ( ; idx > 0; idx--)
1785 {
1786 if (*t == NUL) /* EOL reached */
1787 return;
1788 if (enc_utf8 && comp)
1789 t += utf_ptr2len(t);
1790 else
1791 t += (*mb_ptr2len)(t);
1792 }
1793 rettv->vval.v_number = (varnumber_T)(t - str);
1794#else
1795 if ((size_t)idx <= STRLEN(str))
1796 rettv->vval.v_number = idx;
1797#endif
1798}
1799
1800/*
1801 * "byteidx()" function
1802 */
1803 static void
1804f_byteidx(typval_T *argvars, typval_T *rettv)
1805{
1806 byteidx(argvars, rettv, FALSE);
1807}
1808
1809/*
1810 * "byteidxcomp()" function
1811 */
1812 static void
1813f_byteidxcomp(typval_T *argvars, typval_T *rettv)
1814{
1815 byteidx(argvars, rettv, TRUE);
1816}
1817
1818/*
1819 * "call(func, arglist [, dict])" function
1820 */
1821 static void
1822f_call(typval_T *argvars, typval_T *rettv)
1823{
1824 char_u *func;
1825 partial_T *partial = NULL;
1826 dict_T *selfdict = NULL;
1827
1828 if (argvars[1].v_type != VAR_LIST)
1829 {
1830 EMSG(_(e_listreq));
1831 return;
1832 }
1833 if (argvars[1].vval.v_list == NULL)
1834 return;
1835
1836 if (argvars[0].v_type == VAR_FUNC)
1837 func = argvars[0].vval.v_string;
1838 else if (argvars[0].v_type == VAR_PARTIAL)
1839 {
1840 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001841 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001842 }
1843 else
1844 func = get_tv_string(&argvars[0]);
1845 if (*func == NUL)
1846 return; /* type error or empty name */
1847
1848 if (argvars[2].v_type != VAR_UNKNOWN)
1849 {
1850 if (argvars[2].v_type != VAR_DICT)
1851 {
1852 EMSG(_(e_dictreq));
1853 return;
1854 }
1855 selfdict = argvars[2].vval.v_dict;
1856 }
1857
1858 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
1859}
1860
1861#ifdef FEAT_FLOAT
1862/*
1863 * "ceil({float})" function
1864 */
1865 static void
1866f_ceil(typval_T *argvars, typval_T *rettv)
1867{
1868 float_T f = 0.0;
1869
1870 rettv->v_type = VAR_FLOAT;
1871 if (get_float_arg(argvars, &f) == OK)
1872 rettv->vval.v_float = ceil(f);
1873 else
1874 rettv->vval.v_float = 0.0;
1875}
1876#endif
1877
1878#ifdef FEAT_JOB_CHANNEL
1879/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01001880 * "ch_canread()" function
1881 */
1882 static void
1883f_ch_canread(typval_T *argvars, typval_T *rettv)
1884{
Bram Moolenaar958dc692016-12-01 15:34:12 +01001885 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01001886
1887 rettv->vval.v_number = 0;
1888 if (channel != NULL)
1889 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
1890 || channel_has_readahead(channel, PART_OUT)
1891 || channel_has_readahead(channel, PART_ERR);
1892}
1893
1894/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001895 * "ch_close()" function
1896 */
1897 static void
1898f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
1899{
1900 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
1901
1902 if (channel != NULL)
1903 {
1904 channel_close(channel, FALSE);
1905 channel_clear(channel);
1906 }
1907}
1908
1909/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02001910 * "ch_close()" function
1911 */
1912 static void
1913f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
1914{
1915 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
1916
1917 if (channel != NULL)
1918 channel_close_in(channel);
1919}
1920
1921/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001922 * "ch_getbufnr()" function
1923 */
1924 static void
1925f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
1926{
1927 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1928
1929 rettv->vval.v_number = -1;
1930 if (channel != NULL)
1931 {
1932 char_u *what = get_tv_string(&argvars[1]);
1933 int part;
1934
1935 if (STRCMP(what, "err") == 0)
1936 part = PART_ERR;
1937 else if (STRCMP(what, "out") == 0)
1938 part = PART_OUT;
1939 else if (STRCMP(what, "in") == 0)
1940 part = PART_IN;
1941 else
1942 part = PART_SOCK;
1943 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
1944 rettv->vval.v_number =
1945 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
1946 }
1947}
1948
1949/*
1950 * "ch_getjob()" function
1951 */
1952 static void
1953f_ch_getjob(typval_T *argvars, typval_T *rettv)
1954{
1955 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1956
1957 if (channel != NULL)
1958 {
1959 rettv->v_type = VAR_JOB;
1960 rettv->vval.v_job = channel->ch_job;
1961 if (channel->ch_job != NULL)
1962 ++channel->ch_job->jv_refcount;
1963 }
1964}
1965
1966/*
1967 * "ch_info()" function
1968 */
1969 static void
1970f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
1971{
1972 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1973
1974 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
1975 channel_info(channel, rettv->vval.v_dict);
1976}
1977
1978/*
1979 * "ch_log()" function
1980 */
1981 static void
1982f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
1983{
1984 char_u *msg = get_tv_string(&argvars[0]);
1985 channel_T *channel = NULL;
1986
1987 if (argvars[1].v_type != VAR_UNKNOWN)
1988 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
1989
1990 ch_log(channel, (char *)msg);
1991}
1992
1993/*
1994 * "ch_logfile()" function
1995 */
1996 static void
1997f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
1998{
1999 char_u *fname;
2000 char_u *opt = (char_u *)"";
2001 char_u buf[NUMBUFLEN];
2002
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02002003 /* Don't open a file in restricted mode. */
2004 if (check_restricted() || check_secure())
2005 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002006 fname = get_tv_string(&argvars[0]);
2007 if (argvars[1].v_type == VAR_STRING)
2008 opt = get_tv_string_buf(&argvars[1], buf);
2009 ch_logfile(fname, opt);
2010}
2011
2012/*
2013 * "ch_open()" function
2014 */
2015 static void
2016f_ch_open(typval_T *argvars, typval_T *rettv)
2017{
2018 rettv->v_type = VAR_CHANNEL;
2019 if (check_restricted() || check_secure())
2020 return;
2021 rettv->vval.v_channel = channel_open_func(argvars);
2022}
2023
2024/*
2025 * "ch_read()" function
2026 */
2027 static void
2028f_ch_read(typval_T *argvars, typval_T *rettv)
2029{
2030 common_channel_read(argvars, rettv, FALSE);
2031}
2032
2033/*
2034 * "ch_readraw()" function
2035 */
2036 static void
2037f_ch_readraw(typval_T *argvars, typval_T *rettv)
2038{
2039 common_channel_read(argvars, rettv, TRUE);
2040}
2041
2042/*
2043 * "ch_evalexpr()" function
2044 */
2045 static void
2046f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2047{
2048 ch_expr_common(argvars, rettv, TRUE);
2049}
2050
2051/*
2052 * "ch_sendexpr()" function
2053 */
2054 static void
2055f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2056{
2057 ch_expr_common(argvars, rettv, FALSE);
2058}
2059
2060/*
2061 * "ch_evalraw()" function
2062 */
2063 static void
2064f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2065{
2066 ch_raw_common(argvars, rettv, TRUE);
2067}
2068
2069/*
2070 * "ch_sendraw()" function
2071 */
2072 static void
2073f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2074{
2075 ch_raw_common(argvars, rettv, FALSE);
2076}
2077
2078/*
2079 * "ch_setoptions()" function
2080 */
2081 static void
2082f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2083{
2084 channel_T *channel;
2085 jobopt_T opt;
2086
2087 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2088 if (channel == NULL)
2089 return;
2090 clear_job_options(&opt);
2091 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002092 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002093 channel_set_options(channel, &opt);
2094 free_job_options(&opt);
2095}
2096
2097/*
2098 * "ch_status()" function
2099 */
2100 static void
2101f_ch_status(typval_T *argvars, typval_T *rettv)
2102{
2103 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002104 jobopt_T opt;
2105 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002106
2107 /* return an empty string by default */
2108 rettv->v_type = VAR_STRING;
2109 rettv->vval.v_string = NULL;
2110
2111 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002112
2113 if (argvars[1].v_type != VAR_UNKNOWN)
2114 {
2115 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002116 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002117 && (opt.jo_set & JO_PART))
2118 part = opt.jo_part;
2119 }
2120
2121 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002122}
2123#endif
2124
2125/*
2126 * "changenr()" function
2127 */
2128 static void
2129f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2130{
2131 rettv->vval.v_number = curbuf->b_u_seq_cur;
2132}
2133
2134/*
2135 * "char2nr(string)" function
2136 */
2137 static void
2138f_char2nr(typval_T *argvars, typval_T *rettv)
2139{
2140#ifdef FEAT_MBYTE
2141 if (has_mbyte)
2142 {
2143 int utf8 = 0;
2144
2145 if (argvars[1].v_type != VAR_UNKNOWN)
2146 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
2147
2148 if (utf8)
2149 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
2150 else
2151 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
2152 }
2153 else
2154#endif
2155 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
2156}
2157
2158/*
2159 * "cindent(lnum)" function
2160 */
2161 static void
2162f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2163{
2164#ifdef FEAT_CINDENT
2165 pos_T pos;
2166 linenr_T lnum;
2167
2168 pos = curwin->w_cursor;
2169 lnum = get_tv_lnum(argvars);
2170 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2171 {
2172 curwin->w_cursor.lnum = lnum;
2173 rettv->vval.v_number = get_c_indent();
2174 curwin->w_cursor = pos;
2175 }
2176 else
2177#endif
2178 rettv->vval.v_number = -1;
2179}
2180
2181/*
2182 * "clearmatches()" function
2183 */
2184 static void
2185f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2186{
2187#ifdef FEAT_SEARCH_EXTRA
2188 clear_matches(curwin);
2189#endif
2190}
2191
2192/*
2193 * "col(string)" function
2194 */
2195 static void
2196f_col(typval_T *argvars, typval_T *rettv)
2197{
2198 colnr_T col = 0;
2199 pos_T *fp;
2200 int fnum = curbuf->b_fnum;
2201
2202 fp = var2fpos(&argvars[0], FALSE, &fnum);
2203 if (fp != NULL && fnum == curbuf->b_fnum)
2204 {
2205 if (fp->col == MAXCOL)
2206 {
2207 /* '> can be MAXCOL, get the length of the line then */
2208 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2209 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2210 else
2211 col = MAXCOL;
2212 }
2213 else
2214 {
2215 col = fp->col + 1;
2216#ifdef FEAT_VIRTUALEDIT
2217 /* col(".") when the cursor is on the NUL at the end of the line
2218 * because of "coladd" can be seen as an extra column. */
2219 if (virtual_active() && fp == &curwin->w_cursor)
2220 {
2221 char_u *p = ml_get_cursor();
2222
2223 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2224 curwin->w_virtcol - curwin->w_cursor.coladd))
2225 {
2226# ifdef FEAT_MBYTE
2227 int l;
2228
2229 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2230 col += l;
2231# else
2232 if (*p != NUL && p[1] == NUL)
2233 ++col;
2234# endif
2235 }
2236 }
2237#endif
2238 }
2239 }
2240 rettv->vval.v_number = col;
2241}
2242
2243#if defined(FEAT_INS_EXPAND)
2244/*
2245 * "complete()" function
2246 */
2247 static void
2248f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2249{
2250 int startcol;
2251
2252 if ((State & INSERT) == 0)
2253 {
2254 EMSG(_("E785: complete() can only be used in Insert mode"));
2255 return;
2256 }
2257
2258 /* Check for undo allowed here, because if something was already inserted
2259 * the line was already saved for undo and this check isn't done. */
2260 if (!undo_allowed())
2261 return;
2262
2263 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2264 {
2265 EMSG(_(e_invarg));
2266 return;
2267 }
2268
2269 startcol = (int)get_tv_number_chk(&argvars[0], NULL);
2270 if (startcol <= 0)
2271 return;
2272
2273 set_completion(startcol - 1, argvars[1].vval.v_list);
2274}
2275
2276/*
2277 * "complete_add()" function
2278 */
2279 static void
2280f_complete_add(typval_T *argvars, typval_T *rettv)
2281{
2282 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2283}
2284
2285/*
2286 * "complete_check()" function
2287 */
2288 static void
2289f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2290{
2291 int saved = RedrawingDisabled;
2292
2293 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002294 ins_compl_check_keys(0, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002295 rettv->vval.v_number = compl_interrupted;
2296 RedrawingDisabled = saved;
2297}
2298#endif
2299
2300/*
2301 * "confirm(message, buttons[, default [, type]])" function
2302 */
2303 static void
2304f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2305{
2306#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2307 char_u *message;
2308 char_u *buttons = NULL;
2309 char_u buf[NUMBUFLEN];
2310 char_u buf2[NUMBUFLEN];
2311 int def = 1;
2312 int type = VIM_GENERIC;
2313 char_u *typestr;
2314 int error = FALSE;
2315
2316 message = get_tv_string_chk(&argvars[0]);
2317 if (message == NULL)
2318 error = TRUE;
2319 if (argvars[1].v_type != VAR_UNKNOWN)
2320 {
2321 buttons = get_tv_string_buf_chk(&argvars[1], buf);
2322 if (buttons == NULL)
2323 error = TRUE;
2324 if (argvars[2].v_type != VAR_UNKNOWN)
2325 {
2326 def = (int)get_tv_number_chk(&argvars[2], &error);
2327 if (argvars[3].v_type != VAR_UNKNOWN)
2328 {
2329 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
2330 if (typestr == NULL)
2331 error = TRUE;
2332 else
2333 {
2334 switch (TOUPPER_ASC(*typestr))
2335 {
2336 case 'E': type = VIM_ERROR; break;
2337 case 'Q': type = VIM_QUESTION; break;
2338 case 'I': type = VIM_INFO; break;
2339 case 'W': type = VIM_WARNING; break;
2340 case 'G': type = VIM_GENERIC; break;
2341 }
2342 }
2343 }
2344 }
2345 }
2346
2347 if (buttons == NULL || *buttons == NUL)
2348 buttons = (char_u *)_("&Ok");
2349
2350 if (!error)
2351 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2352 def, NULL, FALSE);
2353#endif
2354}
2355
2356/*
2357 * "copy()" function
2358 */
2359 static void
2360f_copy(typval_T *argvars, typval_T *rettv)
2361{
2362 item_copy(&argvars[0], rettv, FALSE, 0);
2363}
2364
2365#ifdef FEAT_FLOAT
2366/*
2367 * "cos()" function
2368 */
2369 static void
2370f_cos(typval_T *argvars, typval_T *rettv)
2371{
2372 float_T f = 0.0;
2373
2374 rettv->v_type = VAR_FLOAT;
2375 if (get_float_arg(argvars, &f) == OK)
2376 rettv->vval.v_float = cos(f);
2377 else
2378 rettv->vval.v_float = 0.0;
2379}
2380
2381/*
2382 * "cosh()" function
2383 */
2384 static void
2385f_cosh(typval_T *argvars, typval_T *rettv)
2386{
2387 float_T f = 0.0;
2388
2389 rettv->v_type = VAR_FLOAT;
2390 if (get_float_arg(argvars, &f) == OK)
2391 rettv->vval.v_float = cosh(f);
2392 else
2393 rettv->vval.v_float = 0.0;
2394}
2395#endif
2396
2397/*
2398 * "count()" function
2399 */
2400 static void
2401f_count(typval_T *argvars, typval_T *rettv)
2402{
2403 long n = 0;
2404 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002405 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002406
Bram Moolenaar9966b212017-07-28 16:46:57 +02002407 if (argvars[2].v_type != VAR_UNKNOWN)
2408 ic = (int)get_tv_number_chk(&argvars[2], &error);
2409
2410 if (argvars[0].v_type == VAR_STRING)
2411 {
2412 char_u *expr = get_tv_string_chk(&argvars[1]);
2413 char_u *p = argvars[0].vval.v_string;
2414 char_u *next;
2415
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002416 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002417 {
2418 if (ic)
2419 {
2420 size_t len = STRLEN(expr);
2421
2422 while (*p != NUL)
2423 {
2424 if (MB_STRNICMP(p, expr, len) == 0)
2425 {
2426 ++n;
2427 p += len;
2428 }
2429 else
2430 MB_PTR_ADV(p);
2431 }
2432 }
2433 else
2434 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2435 != NULL)
2436 {
2437 ++n;
2438 p = next + STRLEN(expr);
2439 }
2440 }
2441
2442 }
2443 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002444 {
2445 listitem_T *li;
2446 list_T *l;
2447 long idx;
2448
2449 if ((l = argvars[0].vval.v_list) != NULL)
2450 {
2451 li = l->lv_first;
2452 if (argvars[2].v_type != VAR_UNKNOWN)
2453 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002454 if (argvars[3].v_type != VAR_UNKNOWN)
2455 {
2456 idx = (long)get_tv_number_chk(&argvars[3], &error);
2457 if (!error)
2458 {
2459 li = list_find(l, idx);
2460 if (li == NULL)
2461 EMSGN(_(e_listidx), idx);
2462 }
2463 }
2464 if (error)
2465 li = NULL;
2466 }
2467
2468 for ( ; li != NULL; li = li->li_next)
2469 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2470 ++n;
2471 }
2472 }
2473 else if (argvars[0].v_type == VAR_DICT)
2474 {
2475 int todo;
2476 dict_T *d;
2477 hashitem_T *hi;
2478
2479 if ((d = argvars[0].vval.v_dict) != NULL)
2480 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002481 if (argvars[2].v_type != VAR_UNKNOWN)
2482 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002483 if (argvars[3].v_type != VAR_UNKNOWN)
2484 EMSG(_(e_invarg));
2485 }
2486
2487 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2488 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2489 {
2490 if (!HASHITEM_EMPTY(hi))
2491 {
2492 --todo;
2493 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2494 ++n;
2495 }
2496 }
2497 }
2498 }
2499 else
2500 EMSG2(_(e_listdictarg), "count()");
2501 rettv->vval.v_number = n;
2502}
2503
2504/*
2505 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2506 *
2507 * Checks the existence of a cscope connection.
2508 */
2509 static void
2510f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2511{
2512#ifdef FEAT_CSCOPE
2513 int num = 0;
2514 char_u *dbpath = NULL;
2515 char_u *prepend = NULL;
2516 char_u buf[NUMBUFLEN];
2517
2518 if (argvars[0].v_type != VAR_UNKNOWN
2519 && argvars[1].v_type != VAR_UNKNOWN)
2520 {
2521 num = (int)get_tv_number(&argvars[0]);
2522 dbpath = get_tv_string(&argvars[1]);
2523 if (argvars[2].v_type != VAR_UNKNOWN)
2524 prepend = get_tv_string_buf(&argvars[2], buf);
2525 }
2526
2527 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2528#endif
2529}
2530
2531/*
2532 * "cursor(lnum, col)" function, or
2533 * "cursor(list)"
2534 *
2535 * Moves the cursor to the specified line and column.
2536 * Returns 0 when the position could be set, -1 otherwise.
2537 */
2538 static void
2539f_cursor(typval_T *argvars, typval_T *rettv)
2540{
2541 long line, col;
2542#ifdef FEAT_VIRTUALEDIT
2543 long coladd = 0;
2544#endif
2545 int set_curswant = TRUE;
2546
2547 rettv->vval.v_number = -1;
2548 if (argvars[1].v_type == VAR_UNKNOWN)
2549 {
2550 pos_T pos;
2551 colnr_T curswant = -1;
2552
2553 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2554 {
2555 EMSG(_(e_invarg));
2556 return;
2557 }
2558 line = pos.lnum;
2559 col = pos.col;
2560#ifdef FEAT_VIRTUALEDIT
2561 coladd = pos.coladd;
2562#endif
2563 if (curswant >= 0)
2564 {
2565 curwin->w_curswant = curswant - 1;
2566 set_curswant = FALSE;
2567 }
2568 }
2569 else
2570 {
2571 line = get_tv_lnum(argvars);
2572 col = (long)get_tv_number_chk(&argvars[1], NULL);
2573#ifdef FEAT_VIRTUALEDIT
2574 if (argvars[2].v_type != VAR_UNKNOWN)
2575 coladd = (long)get_tv_number_chk(&argvars[2], NULL);
2576#endif
2577 }
2578 if (line < 0 || col < 0
2579#ifdef FEAT_VIRTUALEDIT
2580 || coladd < 0
2581#endif
2582 )
2583 return; /* type error; errmsg already given */
2584 if (line > 0)
2585 curwin->w_cursor.lnum = line;
2586 if (col > 0)
2587 curwin->w_cursor.col = col - 1;
2588#ifdef FEAT_VIRTUALEDIT
2589 curwin->w_cursor.coladd = coladd;
2590#endif
2591
2592 /* Make sure the cursor is in a valid position. */
2593 check_cursor();
2594#ifdef FEAT_MBYTE
2595 /* Correct cursor for multi-byte character. */
2596 if (has_mbyte)
2597 mb_adjust_cursor();
2598#endif
2599
2600 curwin->w_set_curswant = set_curswant;
2601 rettv->vval.v_number = 0;
2602}
2603
2604/*
2605 * "deepcopy()" function
2606 */
2607 static void
2608f_deepcopy(typval_T *argvars, typval_T *rettv)
2609{
2610 int noref = 0;
2611 int copyID;
2612
2613 if (argvars[1].v_type != VAR_UNKNOWN)
2614 noref = (int)get_tv_number_chk(&argvars[1], NULL);
2615 if (noref < 0 || noref > 1)
2616 EMSG(_(e_invarg));
2617 else
2618 {
2619 copyID = get_copyID();
2620 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2621 }
2622}
2623
2624/*
2625 * "delete()" function
2626 */
2627 static void
2628f_delete(typval_T *argvars, typval_T *rettv)
2629{
2630 char_u nbuf[NUMBUFLEN];
2631 char_u *name;
2632 char_u *flags;
2633
2634 rettv->vval.v_number = -1;
2635 if (check_restricted() || check_secure())
2636 return;
2637
2638 name = get_tv_string(&argvars[0]);
2639 if (name == NULL || *name == NUL)
2640 {
2641 EMSG(_(e_invarg));
2642 return;
2643 }
2644
2645 if (argvars[1].v_type != VAR_UNKNOWN)
2646 flags = get_tv_string_buf(&argvars[1], nbuf);
2647 else
2648 flags = (char_u *)"";
2649
2650 if (*flags == NUL)
2651 /* delete a file */
2652 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2653 else if (STRCMP(flags, "d") == 0)
2654 /* delete an empty directory */
2655 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2656 else if (STRCMP(flags, "rf") == 0)
2657 /* delete a directory recursively */
2658 rettv->vval.v_number = delete_recursive(name);
2659 else
2660 EMSG2(_(e_invexpr2), flags);
2661}
2662
2663/*
2664 * "did_filetype()" function
2665 */
2666 static void
2667f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2668{
2669#ifdef FEAT_AUTOCMD
2670 rettv->vval.v_number = did_filetype;
2671#endif
2672}
2673
2674/*
2675 * "diff_filler()" function
2676 */
2677 static void
2678f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2679{
2680#ifdef FEAT_DIFF
2681 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
2682#endif
2683}
2684
2685/*
2686 * "diff_hlID()" function
2687 */
2688 static void
2689f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2690{
2691#ifdef FEAT_DIFF
2692 linenr_T lnum = get_tv_lnum(argvars);
2693 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01002694 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002695 static int fnum = 0;
2696 static int change_start = 0;
2697 static int change_end = 0;
2698 static hlf_T hlID = (hlf_T)0;
2699 int filler_lines;
2700 int col;
2701
2702 if (lnum < 0) /* ignore type error in {lnum} arg */
2703 lnum = 0;
2704 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002705 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002706 || fnum != curbuf->b_fnum)
2707 {
2708 /* New line, buffer, change: need to get the values. */
2709 filler_lines = diff_check(curwin, lnum);
2710 if (filler_lines < 0)
2711 {
2712 if (filler_lines == -1)
2713 {
2714 change_start = MAXCOL;
2715 change_end = -1;
2716 if (diff_find_change(curwin, lnum, &change_start, &change_end))
2717 hlID = HLF_ADD; /* added line */
2718 else
2719 hlID = HLF_CHD; /* changed line */
2720 }
2721 else
2722 hlID = HLF_ADD; /* added line */
2723 }
2724 else
2725 hlID = (hlf_T)0;
2726 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002727 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002728 fnum = curbuf->b_fnum;
2729 }
2730
2731 if (hlID == HLF_CHD || hlID == HLF_TXD)
2732 {
2733 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
2734 if (col >= change_start && col <= change_end)
2735 hlID = HLF_TXD; /* changed text */
2736 else
2737 hlID = HLF_CHD; /* changed line */
2738 }
2739 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
2740#endif
2741}
2742
2743/*
2744 * "empty({expr})" function
2745 */
2746 static void
2747f_empty(typval_T *argvars, typval_T *rettv)
2748{
2749 int n = FALSE;
2750
2751 switch (argvars[0].v_type)
2752 {
2753 case VAR_STRING:
2754 case VAR_FUNC:
2755 n = argvars[0].vval.v_string == NULL
2756 || *argvars[0].vval.v_string == NUL;
2757 break;
2758 case VAR_PARTIAL:
2759 n = FALSE;
2760 break;
2761 case VAR_NUMBER:
2762 n = argvars[0].vval.v_number == 0;
2763 break;
2764 case VAR_FLOAT:
2765#ifdef FEAT_FLOAT
2766 n = argvars[0].vval.v_float == 0.0;
2767 break;
2768#endif
2769 case VAR_LIST:
2770 n = argvars[0].vval.v_list == NULL
2771 || argvars[0].vval.v_list->lv_first == NULL;
2772 break;
2773 case VAR_DICT:
2774 n = argvars[0].vval.v_dict == NULL
2775 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2776 break;
2777 case VAR_SPECIAL:
2778 n = argvars[0].vval.v_number != VVAL_TRUE;
2779 break;
2780
2781 case VAR_JOB:
2782#ifdef FEAT_JOB_CHANNEL
2783 n = argvars[0].vval.v_job == NULL
2784 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2785 break;
2786#endif
2787 case VAR_CHANNEL:
2788#ifdef FEAT_JOB_CHANNEL
2789 n = argvars[0].vval.v_channel == NULL
2790 || !channel_is_open(argvars[0].vval.v_channel);
2791 break;
2792#endif
2793 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01002794 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002795 n = TRUE;
2796 break;
2797 }
2798
2799 rettv->vval.v_number = n;
2800}
2801
2802/*
2803 * "escape({string}, {chars})" function
2804 */
2805 static void
2806f_escape(typval_T *argvars, typval_T *rettv)
2807{
2808 char_u buf[NUMBUFLEN];
2809
2810 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
2811 get_tv_string_buf(&argvars[1], buf));
2812 rettv->v_type = VAR_STRING;
2813}
2814
2815/*
2816 * "eval()" function
2817 */
2818 static void
2819f_eval(typval_T *argvars, typval_T *rettv)
2820{
2821 char_u *s, *p;
2822
2823 s = get_tv_string_chk(&argvars[0]);
2824 if (s != NULL)
2825 s = skipwhite(s);
2826
2827 p = s;
2828 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
2829 {
2830 if (p != NULL && !aborting())
2831 EMSG2(_(e_invexpr2), p);
2832 need_clr_eos = FALSE;
2833 rettv->v_type = VAR_NUMBER;
2834 rettv->vval.v_number = 0;
2835 }
2836 else if (*s != NUL)
2837 EMSG(_(e_trailing));
2838}
2839
2840/*
2841 * "eventhandler()" function
2842 */
2843 static void
2844f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
2845{
2846 rettv->vval.v_number = vgetc_busy;
2847}
2848
2849/*
2850 * "executable()" function
2851 */
2852 static void
2853f_executable(typval_T *argvars, typval_T *rettv)
2854{
2855 char_u *name = get_tv_string(&argvars[0]);
2856
2857 /* Check in $PATH and also check directly if there is a directory name. */
2858 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
2859 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
2860}
2861
2862static garray_T redir_execute_ga;
2863
2864/*
2865 * Append "value[value_len]" to the execute() output.
2866 */
2867 void
2868execute_redir_str(char_u *value, int value_len)
2869{
2870 int len;
2871
2872 if (value_len == -1)
2873 len = (int)STRLEN(value); /* Append the entire string */
2874 else
2875 len = value_len; /* Append only "value_len" characters */
2876 if (ga_grow(&redir_execute_ga, len) == OK)
2877 {
2878 mch_memmove((char *)redir_execute_ga.ga_data
2879 + redir_execute_ga.ga_len, value, len);
2880 redir_execute_ga.ga_len += len;
2881 }
2882}
2883
2884/*
2885 * Get next line from a list.
2886 * Called by do_cmdline() to get the next line.
2887 * Returns allocated string, or NULL for end of function.
2888 */
2889
2890 static char_u *
2891get_list_line(
2892 int c UNUSED,
2893 void *cookie,
2894 int indent UNUSED)
2895{
2896 listitem_T **p = (listitem_T **)cookie;
2897 listitem_T *item = *p;
2898 char_u buf[NUMBUFLEN];
2899 char_u *s;
2900
2901 if (item == NULL)
2902 return NULL;
2903 s = get_tv_string_buf_chk(&item->li_tv, buf);
2904 *p = item->li_next;
2905 return s == NULL ? NULL : vim_strsave(s);
2906}
2907
2908/*
2909 * "execute()" function
2910 */
2911 static void
2912f_execute(typval_T *argvars, typval_T *rettv)
2913{
2914 char_u *cmd = NULL;
2915 list_T *list = NULL;
2916 int save_msg_silent = msg_silent;
2917 int save_emsg_silent = emsg_silent;
2918 int save_emsg_noredir = emsg_noredir;
2919 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01002920 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002921 garray_T save_ga;
2922
2923 rettv->vval.v_string = NULL;
2924 rettv->v_type = VAR_STRING;
2925
2926 if (argvars[0].v_type == VAR_LIST)
2927 {
2928 list = argvars[0].vval.v_list;
2929 if (list == NULL || list->lv_first == NULL)
2930 /* empty list, no commands, empty output */
2931 return;
2932 ++list->lv_refcount;
2933 }
2934 else
2935 {
2936 cmd = get_tv_string_chk(&argvars[0]);
2937 if (cmd == NULL)
2938 return;
2939 }
2940
2941 if (argvars[1].v_type != VAR_UNKNOWN)
2942 {
2943 char_u buf[NUMBUFLEN];
2944 char_u *s = get_tv_string_buf_chk(&argvars[1], buf);
2945
2946 if (s == NULL)
2947 return;
2948 if (STRNCMP(s, "silent", 6) == 0)
2949 ++msg_silent;
2950 if (STRCMP(s, "silent!") == 0)
2951 {
2952 emsg_silent = TRUE;
2953 emsg_noredir = TRUE;
2954 }
2955 }
2956 else
2957 ++msg_silent;
2958
2959 if (redir_execute)
2960 save_ga = redir_execute_ga;
2961 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
2962 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01002963 redir_off = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002964
2965 if (cmd != NULL)
2966 do_cmdline_cmd(cmd);
2967 else
2968 {
2969 listitem_T *item = list->lv_first;
2970
2971 do_cmdline(NULL, get_list_line, (void *)&item,
2972 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
2973 --list->lv_refcount;
2974 }
2975
Bram Moolenaard297f352017-01-29 20:31:21 +01002976 /* Need to append a NUL to the result. */
2977 if (ga_grow(&redir_execute_ga, 1) == OK)
2978 {
2979 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
2980 rettv->vval.v_string = redir_execute_ga.ga_data;
2981 }
2982 else
2983 {
2984 ga_clear(&redir_execute_ga);
2985 rettv->vval.v_string = NULL;
2986 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002987 msg_silent = save_msg_silent;
2988 emsg_silent = save_emsg_silent;
2989 emsg_noredir = save_emsg_noredir;
2990
2991 redir_execute = save_redir_execute;
2992 if (redir_execute)
2993 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01002994 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002995
2996 /* "silent reg" or "silent echo x" leaves msg_col somewhere in the
2997 * line. Put it back in the first column. */
2998 msg_col = 0;
2999}
3000
3001/*
3002 * "exepath()" function
3003 */
3004 static void
3005f_exepath(typval_T *argvars, typval_T *rettv)
3006{
3007 char_u *p = NULL;
3008
3009 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
3010 rettv->v_type = VAR_STRING;
3011 rettv->vval.v_string = p;
3012}
3013
3014/*
3015 * "exists()" function
3016 */
3017 static void
3018f_exists(typval_T *argvars, typval_T *rettv)
3019{
3020 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003021 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003022
3023 p = get_tv_string(&argvars[0]);
3024 if (*p == '$') /* environment variable */
3025 {
3026 /* first try "normal" environment variables (fast) */
3027 if (mch_getenv(p + 1) != NULL)
3028 n = TRUE;
3029 else
3030 {
3031 /* try expanding things like $VIM and ${HOME} */
3032 p = expand_env_save(p);
3033 if (p != NULL && *p != '$')
3034 n = TRUE;
3035 vim_free(p);
3036 }
3037 }
3038 else if (*p == '&' || *p == '+') /* option */
3039 {
3040 n = (get_option_tv(&p, NULL, TRUE) == OK);
3041 if (*skipwhite(p) != NUL)
3042 n = FALSE; /* trailing garbage */
3043 }
3044 else if (*p == '*') /* internal or user defined function */
3045 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003046 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003047 }
3048 else if (*p == ':')
3049 {
3050 n = cmd_exists(p + 1);
3051 }
3052 else if (*p == '#')
3053 {
3054#ifdef FEAT_AUTOCMD
3055 if (p[1] == '#')
3056 n = autocmd_supported(p + 2);
3057 else
3058 n = au_exists(p + 1);
3059#endif
3060 }
3061 else /* internal variable */
3062 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003063 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003064 }
3065
3066 rettv->vval.v_number = n;
3067}
3068
3069#ifdef FEAT_FLOAT
3070/*
3071 * "exp()" function
3072 */
3073 static void
3074f_exp(typval_T *argvars, typval_T *rettv)
3075{
3076 float_T f = 0.0;
3077
3078 rettv->v_type = VAR_FLOAT;
3079 if (get_float_arg(argvars, &f) == OK)
3080 rettv->vval.v_float = exp(f);
3081 else
3082 rettv->vval.v_float = 0.0;
3083}
3084#endif
3085
3086/*
3087 * "expand()" function
3088 */
3089 static void
3090f_expand(typval_T *argvars, typval_T *rettv)
3091{
3092 char_u *s;
3093 int len;
3094 char_u *errormsg;
3095 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3096 expand_T xpc;
3097 int error = FALSE;
3098 char_u *result;
3099
3100 rettv->v_type = VAR_STRING;
3101 if (argvars[1].v_type != VAR_UNKNOWN
3102 && argvars[2].v_type != VAR_UNKNOWN
3103 && get_tv_number_chk(&argvars[2], &error)
3104 && !error)
3105 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003106 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003107 }
3108
3109 s = get_tv_string(&argvars[0]);
3110 if (*s == '%' || *s == '#' || *s == '<')
3111 {
3112 ++emsg_off;
3113 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3114 --emsg_off;
3115 if (rettv->v_type == VAR_LIST)
3116 {
3117 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3118 list_append_string(rettv->vval.v_list, result, -1);
3119 else
3120 vim_free(result);
3121 }
3122 else
3123 rettv->vval.v_string = result;
3124 }
3125 else
3126 {
3127 /* When the optional second argument is non-zero, don't remove matches
3128 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3129 if (argvars[1].v_type != VAR_UNKNOWN
3130 && get_tv_number_chk(&argvars[1], &error))
3131 options |= WILD_KEEP_ALL;
3132 if (!error)
3133 {
3134 ExpandInit(&xpc);
3135 xpc.xp_context = EXPAND_FILES;
3136 if (p_wic)
3137 options += WILD_ICASE;
3138 if (rettv->v_type == VAR_STRING)
3139 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3140 options, WILD_ALL);
3141 else if (rettv_list_alloc(rettv) != FAIL)
3142 {
3143 int i;
3144
3145 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3146 for (i = 0; i < xpc.xp_numfiles; i++)
3147 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3148 ExpandCleanup(&xpc);
3149 }
3150 }
3151 else
3152 rettv->vval.v_string = NULL;
3153 }
3154}
3155
3156/*
3157 * "extend(list, list [, idx])" function
3158 * "extend(dict, dict [, action])" function
3159 */
3160 static void
3161f_extend(typval_T *argvars, typval_T *rettv)
3162{
3163 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3164
3165 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3166 {
3167 list_T *l1, *l2;
3168 listitem_T *item;
3169 long before;
3170 int error = FALSE;
3171
3172 l1 = argvars[0].vval.v_list;
3173 l2 = argvars[1].vval.v_list;
3174 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
3175 && l2 != NULL)
3176 {
3177 if (argvars[2].v_type != VAR_UNKNOWN)
3178 {
3179 before = (long)get_tv_number_chk(&argvars[2], &error);
3180 if (error)
3181 return; /* type error; errmsg already given */
3182
3183 if (before == l1->lv_len)
3184 item = NULL;
3185 else
3186 {
3187 item = list_find(l1, before);
3188 if (item == NULL)
3189 {
3190 EMSGN(_(e_listidx), before);
3191 return;
3192 }
3193 }
3194 }
3195 else
3196 item = NULL;
3197 list_extend(l1, l2, item);
3198
3199 copy_tv(&argvars[0], rettv);
3200 }
3201 }
3202 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3203 {
3204 dict_T *d1, *d2;
3205 char_u *action;
3206 int i;
3207
3208 d1 = argvars[0].vval.v_dict;
3209 d2 = argvars[1].vval.v_dict;
3210 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
3211 && d2 != NULL)
3212 {
3213 /* Check the third argument. */
3214 if (argvars[2].v_type != VAR_UNKNOWN)
3215 {
3216 static char *(av[]) = {"keep", "force", "error"};
3217
3218 action = get_tv_string_chk(&argvars[2]);
3219 if (action == NULL)
3220 return; /* type error; errmsg already given */
3221 for (i = 0; i < 3; ++i)
3222 if (STRCMP(action, av[i]) == 0)
3223 break;
3224 if (i == 3)
3225 {
3226 EMSG2(_(e_invarg2), action);
3227 return;
3228 }
3229 }
3230 else
3231 action = (char_u *)"force";
3232
3233 dict_extend(d1, d2, action);
3234
3235 copy_tv(&argvars[0], rettv);
3236 }
3237 }
3238 else
3239 EMSG2(_(e_listdictarg), "extend()");
3240}
3241
3242/*
3243 * "feedkeys()" function
3244 */
3245 static void
3246f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3247{
3248 int remap = TRUE;
3249 int insert = FALSE;
3250 char_u *keys, *flags;
3251 char_u nbuf[NUMBUFLEN];
3252 int typed = FALSE;
3253 int execute = FALSE;
3254 int dangerous = FALSE;
3255 char_u *keys_esc;
3256
3257 /* This is not allowed in the sandbox. If the commands would still be
3258 * executed in the sandbox it would be OK, but it probably happens later,
3259 * when "sandbox" is no longer set. */
3260 if (check_secure())
3261 return;
3262
3263 keys = get_tv_string(&argvars[0]);
3264
3265 if (argvars[1].v_type != VAR_UNKNOWN)
3266 {
3267 flags = get_tv_string_buf(&argvars[1], nbuf);
3268 for ( ; *flags != NUL; ++flags)
3269 {
3270 switch (*flags)
3271 {
3272 case 'n': remap = FALSE; break;
3273 case 'm': remap = TRUE; break;
3274 case 't': typed = TRUE; break;
3275 case 'i': insert = TRUE; break;
3276 case 'x': execute = TRUE; break;
3277 case '!': dangerous = TRUE; break;
3278 }
3279 }
3280 }
3281
3282 if (*keys != NUL || execute)
3283 {
3284 /* Need to escape K_SPECIAL and CSI before putting the string in the
3285 * typeahead buffer. */
3286 keys_esc = vim_strsave_escape_csi(keys);
3287 if (keys_esc != NULL)
3288 {
3289 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
3290 insert ? 0 : typebuf.tb_len, !typed, FALSE);
3291 vim_free(keys_esc);
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003292 if (vgetc_busy
3293#ifdef FEAT_TIMERS
3294 || timer_busy
3295#endif
3296 )
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003297 typebuf_was_filled = TRUE;
3298 if (execute)
3299 {
3300 int save_msg_scroll = msg_scroll;
3301
3302 /* Avoid a 1 second delay when the keys start Insert mode. */
3303 msg_scroll = FALSE;
3304
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02003305#ifdef FEAT_TERMINAL
3306 if (term_use_loop())
3307 terminal_loop(FALSE);
3308 else
3309#endif
3310 {
3311 if (!dangerous)
3312 ++ex_normal_busy;
3313 exec_normal(TRUE);
3314 if (!dangerous)
3315 --ex_normal_busy;
3316 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003317 msg_scroll |= save_msg_scroll;
3318 }
3319 }
3320 }
3321}
3322
3323/*
3324 * "filereadable()" function
3325 */
3326 static void
3327f_filereadable(typval_T *argvars, typval_T *rettv)
3328{
3329 int fd;
3330 char_u *p;
3331 int n;
3332
3333#ifndef O_NONBLOCK
3334# define O_NONBLOCK 0
3335#endif
3336 p = get_tv_string(&argvars[0]);
3337 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3338 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3339 {
3340 n = TRUE;
3341 close(fd);
3342 }
3343 else
3344 n = FALSE;
3345
3346 rettv->vval.v_number = n;
3347}
3348
3349/*
3350 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3351 * rights to write into.
3352 */
3353 static void
3354f_filewritable(typval_T *argvars, typval_T *rettv)
3355{
3356 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
3357}
3358
3359 static void
3360findfilendir(
3361 typval_T *argvars UNUSED,
3362 typval_T *rettv,
3363 int find_what UNUSED)
3364{
3365#ifdef FEAT_SEARCHPATH
3366 char_u *fname;
3367 char_u *fresult = NULL;
3368 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3369 char_u *p;
3370 char_u pathbuf[NUMBUFLEN];
3371 int count = 1;
3372 int first = TRUE;
3373 int error = FALSE;
3374#endif
3375
3376 rettv->vval.v_string = NULL;
3377 rettv->v_type = VAR_STRING;
3378
3379#ifdef FEAT_SEARCHPATH
3380 fname = get_tv_string(&argvars[0]);
3381
3382 if (argvars[1].v_type != VAR_UNKNOWN)
3383 {
3384 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
3385 if (p == NULL)
3386 error = TRUE;
3387 else
3388 {
3389 if (*p != NUL)
3390 path = p;
3391
3392 if (argvars[2].v_type != VAR_UNKNOWN)
3393 count = (int)get_tv_number_chk(&argvars[2], &error);
3394 }
3395 }
3396
3397 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3398 error = TRUE;
3399
3400 if (*fname != NUL && !error)
3401 {
3402 do
3403 {
3404 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3405 vim_free(fresult);
3406 fresult = find_file_in_path_option(first ? fname : NULL,
3407 first ? (int)STRLEN(fname) : 0,
3408 0, first, path,
3409 find_what,
3410 curbuf->b_ffname,
3411 find_what == FINDFILE_DIR
3412 ? (char_u *)"" : curbuf->b_p_sua);
3413 first = FALSE;
3414
3415 if (fresult != NULL && rettv->v_type == VAR_LIST)
3416 list_append_string(rettv->vval.v_list, fresult, -1);
3417
3418 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3419 }
3420
3421 if (rettv->v_type == VAR_STRING)
3422 rettv->vval.v_string = fresult;
3423#endif
3424}
3425
3426/*
3427 * "filter()" function
3428 */
3429 static void
3430f_filter(typval_T *argvars, typval_T *rettv)
3431{
3432 filter_map(argvars, rettv, FALSE);
3433}
3434
3435/*
3436 * "finddir({fname}[, {path}[, {count}]])" function
3437 */
3438 static void
3439f_finddir(typval_T *argvars, typval_T *rettv)
3440{
3441 findfilendir(argvars, rettv, FINDFILE_DIR);
3442}
3443
3444/*
3445 * "findfile({fname}[, {path}[, {count}]])" function
3446 */
3447 static void
3448f_findfile(typval_T *argvars, typval_T *rettv)
3449{
3450 findfilendir(argvars, rettv, FINDFILE_FILE);
3451}
3452
3453#ifdef FEAT_FLOAT
3454/*
3455 * "float2nr({float})" function
3456 */
3457 static void
3458f_float2nr(typval_T *argvars, typval_T *rettv)
3459{
3460 float_T f = 0.0;
3461
3462 if (get_float_arg(argvars, &f) == OK)
3463 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003464 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003465 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003466 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003467 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003468 else
3469 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003470 }
3471}
3472
3473/*
3474 * "floor({float})" function
3475 */
3476 static void
3477f_floor(typval_T *argvars, typval_T *rettv)
3478{
3479 float_T f = 0.0;
3480
3481 rettv->v_type = VAR_FLOAT;
3482 if (get_float_arg(argvars, &f) == OK)
3483 rettv->vval.v_float = floor(f);
3484 else
3485 rettv->vval.v_float = 0.0;
3486}
3487
3488/*
3489 * "fmod()" function
3490 */
3491 static void
3492f_fmod(typval_T *argvars, typval_T *rettv)
3493{
3494 float_T fx = 0.0, fy = 0.0;
3495
3496 rettv->v_type = VAR_FLOAT;
3497 if (get_float_arg(argvars, &fx) == OK
3498 && get_float_arg(&argvars[1], &fy) == OK)
3499 rettv->vval.v_float = fmod(fx, fy);
3500 else
3501 rettv->vval.v_float = 0.0;
3502}
3503#endif
3504
3505/*
3506 * "fnameescape({string})" function
3507 */
3508 static void
3509f_fnameescape(typval_T *argvars, typval_T *rettv)
3510{
3511 rettv->vval.v_string = vim_strsave_fnameescape(
3512 get_tv_string(&argvars[0]), FALSE);
3513 rettv->v_type = VAR_STRING;
3514}
3515
3516/*
3517 * "fnamemodify({fname}, {mods})" function
3518 */
3519 static void
3520f_fnamemodify(typval_T *argvars, typval_T *rettv)
3521{
3522 char_u *fname;
3523 char_u *mods;
3524 int usedlen = 0;
3525 int len;
3526 char_u *fbuf = NULL;
3527 char_u buf[NUMBUFLEN];
3528
3529 fname = get_tv_string_chk(&argvars[0]);
3530 mods = get_tv_string_buf_chk(&argvars[1], buf);
3531 if (fname == NULL || mods == NULL)
3532 fname = NULL;
3533 else
3534 {
3535 len = (int)STRLEN(fname);
3536 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
3537 }
3538
3539 rettv->v_type = VAR_STRING;
3540 if (fname == NULL)
3541 rettv->vval.v_string = NULL;
3542 else
3543 rettv->vval.v_string = vim_strnsave(fname, len);
3544 vim_free(fbuf);
3545}
3546
3547static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
3548
3549/*
3550 * "foldclosed()" function
3551 */
3552 static void
3553foldclosed_both(
3554 typval_T *argvars UNUSED,
3555 typval_T *rettv,
3556 int end UNUSED)
3557{
3558#ifdef FEAT_FOLDING
3559 linenr_T lnum;
3560 linenr_T first, last;
3561
3562 lnum = get_tv_lnum(argvars);
3563 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3564 {
3565 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3566 {
3567 if (end)
3568 rettv->vval.v_number = (varnumber_T)last;
3569 else
3570 rettv->vval.v_number = (varnumber_T)first;
3571 return;
3572 }
3573 }
3574#endif
3575 rettv->vval.v_number = -1;
3576}
3577
3578/*
3579 * "foldclosed()" function
3580 */
3581 static void
3582f_foldclosed(typval_T *argvars, typval_T *rettv)
3583{
3584 foldclosed_both(argvars, rettv, FALSE);
3585}
3586
3587/*
3588 * "foldclosedend()" function
3589 */
3590 static void
3591f_foldclosedend(typval_T *argvars, typval_T *rettv)
3592{
3593 foldclosed_both(argvars, rettv, TRUE);
3594}
3595
3596/*
3597 * "foldlevel()" function
3598 */
3599 static void
3600f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3601{
3602#ifdef FEAT_FOLDING
3603 linenr_T lnum;
3604
3605 lnum = get_tv_lnum(argvars);
3606 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3607 rettv->vval.v_number = foldLevel(lnum);
3608#endif
3609}
3610
3611/*
3612 * "foldtext()" function
3613 */
3614 static void
3615f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
3616{
3617#ifdef FEAT_FOLDING
3618 linenr_T foldstart;
3619 linenr_T foldend;
3620 char_u *dashes;
3621 linenr_T lnum;
3622 char_u *s;
3623 char_u *r;
3624 int len;
3625 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003626 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003627#endif
3628
3629 rettv->v_type = VAR_STRING;
3630 rettv->vval.v_string = NULL;
3631#ifdef FEAT_FOLDING
3632 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
3633 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
3634 dashes = get_vim_var_str(VV_FOLDDASHES);
3635 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
3636 && dashes != NULL)
3637 {
3638 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02003639 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003640 if (!linewhite(lnum))
3641 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003642
3643 /* Find interesting text in this line. */
3644 s = skipwhite(ml_get(lnum));
3645 /* skip C comment-start */
3646 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
3647 {
3648 s = skipwhite(s + 2);
3649 if (*skipwhite(s) == NUL
3650 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
3651 {
3652 s = skipwhite(ml_get(lnum + 1));
3653 if (*s == '*')
3654 s = skipwhite(s + 1);
3655 }
3656 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02003657 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003658 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003659 r = alloc((unsigned)(STRLEN(txt)
3660 + STRLEN(dashes) /* for %s */
3661 + 20 /* for %3ld */
3662 + STRLEN(s))); /* concatenated */
3663 if (r != NULL)
3664 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02003665 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003666 len = (int)STRLEN(r);
3667 STRCAT(r, s);
3668 /* remove 'foldmarker' and 'commentstring' */
3669 foldtext_cleanup(r + len);
3670 rettv->vval.v_string = r;
3671 }
3672 }
3673#endif
3674}
3675
3676/*
3677 * "foldtextresult(lnum)" function
3678 */
3679 static void
3680f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
3681{
3682#ifdef FEAT_FOLDING
3683 linenr_T lnum;
3684 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003685 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003686 foldinfo_T foldinfo;
3687 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003688 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003689#endif
3690
3691 rettv->v_type = VAR_STRING;
3692 rettv->vval.v_string = NULL;
3693#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003694 if (entered)
3695 return; /* reject recursive use */
3696 entered = TRUE;
3697
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003698 lnum = get_tv_lnum(argvars);
3699 /* treat illegal types and illegal string values for {lnum} the same */
3700 if (lnum < 0)
3701 lnum = 0;
3702 fold_count = foldedCount(curwin, lnum, &foldinfo);
3703 if (fold_count > 0)
3704 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01003705 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
3706 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003707 if (text == buf)
3708 text = vim_strsave(text);
3709 rettv->vval.v_string = text;
3710 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003711
3712 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003713#endif
3714}
3715
3716/*
3717 * "foreground()" function
3718 */
3719 static void
3720f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3721{
3722#ifdef FEAT_GUI
3723 if (gui.in_use)
3724 gui_mch_set_foreground();
3725#else
3726# ifdef WIN32
3727 win32_set_foreground();
3728# endif
3729#endif
3730}
3731
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003732 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003733common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003734{
3735 char_u *s;
3736 char_u *name;
3737 int use_string = FALSE;
3738 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003739 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003740
3741 if (argvars[0].v_type == VAR_FUNC)
3742 {
3743 /* function(MyFunc, [arg], dict) */
3744 s = argvars[0].vval.v_string;
3745 }
3746 else if (argvars[0].v_type == VAR_PARTIAL
3747 && argvars[0].vval.v_partial != NULL)
3748 {
3749 /* function(dict.MyFunc, [arg]) */
3750 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003751 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003752 }
3753 else
3754 {
3755 /* function('MyFunc', [arg], dict) */
3756 s = get_tv_string(&argvars[0]);
3757 use_string = TRUE;
3758 }
3759
Bram Moolenaar843b8842016-08-21 14:36:15 +02003760 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003761 {
3762 name = s;
3763 trans_name = trans_function_name(&name, FALSE,
3764 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
3765 if (*name != NUL)
3766 s = NULL;
3767 }
3768
Bram Moolenaar843b8842016-08-21 14:36:15 +02003769 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
3770 || (is_funcref && trans_name == NULL))
Bram Moolenaar5582ef12016-09-14 22:16:13 +02003771 EMSG2(_(e_invarg2), use_string ? get_tv_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003772 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003773 else if (trans_name != NULL && (is_funcref
3774 ? find_func(trans_name) == NULL
3775 : !translated_function_exists(trans_name)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003776 EMSG2(_("E700: Unknown function: %s"), s);
3777 else
3778 {
3779 int dict_idx = 0;
3780 int arg_idx = 0;
3781 list_T *list = NULL;
3782
3783 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
3784 {
3785 char sid_buf[25];
3786 int off = *s == 's' ? 2 : 5;
3787
3788 /* Expand s: and <SID> into <SNR>nr_, so that the function can
3789 * also be called from another script. Using trans_function_name()
3790 * would also work, but some plugins depend on the name being
3791 * printable text. */
3792 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
3793 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
3794 if (name != NULL)
3795 {
3796 STRCPY(name, sid_buf);
3797 STRCAT(name, s + off);
3798 }
3799 }
3800 else
3801 name = vim_strsave(s);
3802
3803 if (argvars[1].v_type != VAR_UNKNOWN)
3804 {
3805 if (argvars[2].v_type != VAR_UNKNOWN)
3806 {
3807 /* function(name, [args], dict) */
3808 arg_idx = 1;
3809 dict_idx = 2;
3810 }
3811 else if (argvars[1].v_type == VAR_DICT)
3812 /* function(name, dict) */
3813 dict_idx = 1;
3814 else
3815 /* function(name, [args]) */
3816 arg_idx = 1;
3817 if (dict_idx > 0)
3818 {
3819 if (argvars[dict_idx].v_type != VAR_DICT)
3820 {
3821 EMSG(_("E922: expected a dict"));
3822 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003823 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003824 }
3825 if (argvars[dict_idx].vval.v_dict == NULL)
3826 dict_idx = 0;
3827 }
3828 if (arg_idx > 0)
3829 {
3830 if (argvars[arg_idx].v_type != VAR_LIST)
3831 {
3832 EMSG(_("E923: Second argument of function() must be a list or a dict"));
3833 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003834 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003835 }
3836 list = argvars[arg_idx].vval.v_list;
3837 if (list == NULL || list->lv_len == 0)
3838 arg_idx = 0;
3839 }
3840 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003841 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003842 {
3843 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
3844
3845 /* result is a VAR_PARTIAL */
3846 if (pt == NULL)
3847 vim_free(name);
3848 else
3849 {
3850 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
3851 {
3852 listitem_T *li;
3853 int i = 0;
3854 int arg_len = 0;
3855 int lv_len = 0;
3856
3857 if (arg_pt != NULL)
3858 arg_len = arg_pt->pt_argc;
3859 if (list != NULL)
3860 lv_len = list->lv_len;
3861 pt->pt_argc = arg_len + lv_len;
3862 pt->pt_argv = (typval_T *)alloc(
3863 sizeof(typval_T) * pt->pt_argc);
3864 if (pt->pt_argv == NULL)
3865 {
3866 vim_free(pt);
3867 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003868 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003869 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003870 for (i = 0; i < arg_len; i++)
3871 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
3872 if (lv_len > 0)
3873 for (li = list->lv_first; li != NULL;
3874 li = li->li_next)
3875 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003876 }
3877
3878 /* For "function(dict.func, [], dict)" and "func" is a partial
3879 * use "dict". That is backwards compatible. */
3880 if (dict_idx > 0)
3881 {
3882 /* The dict is bound explicitly, pt_auto is FALSE. */
3883 pt->pt_dict = argvars[dict_idx].vval.v_dict;
3884 ++pt->pt_dict->dv_refcount;
3885 }
3886 else if (arg_pt != NULL)
3887 {
3888 /* If the dict was bound automatically the result is also
3889 * bound automatically. */
3890 pt->pt_dict = arg_pt->pt_dict;
3891 pt->pt_auto = arg_pt->pt_auto;
3892 if (pt->pt_dict != NULL)
3893 ++pt->pt_dict->dv_refcount;
3894 }
3895
3896 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003897 if (arg_pt != NULL && arg_pt->pt_func != NULL)
3898 {
3899 pt->pt_func = arg_pt->pt_func;
3900 func_ptr_ref(pt->pt_func);
3901 vim_free(name);
3902 }
3903 else if (is_funcref)
3904 {
3905 pt->pt_func = find_func(trans_name);
3906 func_ptr_ref(pt->pt_func);
3907 vim_free(name);
3908 }
3909 else
3910 {
3911 pt->pt_name = name;
3912 func_ref(name);
3913 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003914 }
3915 rettv->v_type = VAR_PARTIAL;
3916 rettv->vval.v_partial = pt;
3917 }
3918 else
3919 {
3920 /* result is a VAR_FUNC */
3921 rettv->v_type = VAR_FUNC;
3922 rettv->vval.v_string = name;
3923 func_ref(name);
3924 }
3925 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003926theend:
3927 vim_free(trans_name);
3928}
3929
3930/*
3931 * "funcref()" function
3932 */
3933 static void
3934f_funcref(typval_T *argvars, typval_T *rettv)
3935{
3936 common_function(argvars, rettv, TRUE);
3937}
3938
3939/*
3940 * "function()" function
3941 */
3942 static void
3943f_function(typval_T *argvars, typval_T *rettv)
3944{
3945 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003946}
3947
3948/*
3949 * "garbagecollect()" function
3950 */
3951 static void
3952f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
3953{
3954 /* This is postponed until we are back at the toplevel, because we may be
3955 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
3956 want_garbage_collect = TRUE;
3957
3958 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
3959 garbage_collect_at_exit = TRUE;
3960}
3961
3962/*
3963 * "get()" function
3964 */
3965 static void
3966f_get(typval_T *argvars, typval_T *rettv)
3967{
3968 listitem_T *li;
3969 list_T *l;
3970 dictitem_T *di;
3971 dict_T *d;
3972 typval_T *tv = NULL;
3973
3974 if (argvars[0].v_type == VAR_LIST)
3975 {
3976 if ((l = argvars[0].vval.v_list) != NULL)
3977 {
3978 int error = FALSE;
3979
3980 li = list_find(l, (long)get_tv_number_chk(&argvars[1], &error));
3981 if (!error && li != NULL)
3982 tv = &li->li_tv;
3983 }
3984 }
3985 else if (argvars[0].v_type == VAR_DICT)
3986 {
3987 if ((d = argvars[0].vval.v_dict) != NULL)
3988 {
3989 di = dict_find(d, get_tv_string(&argvars[1]), -1);
3990 if (di != NULL)
3991 tv = &di->di_tv;
3992 }
3993 }
3994 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
3995 {
3996 partial_T *pt;
3997 partial_T fref_pt;
3998
3999 if (argvars[0].v_type == VAR_PARTIAL)
4000 pt = argvars[0].vval.v_partial;
4001 else
4002 {
4003 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4004 fref_pt.pt_name = argvars[0].vval.v_string;
4005 pt = &fref_pt;
4006 }
4007
4008 if (pt != NULL)
4009 {
4010 char_u *what = get_tv_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004011 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004012
4013 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4014 {
4015 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004016 n = partial_name(pt);
4017 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004018 rettv->vval.v_string = NULL;
4019 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004020 {
4021 rettv->vval.v_string = vim_strsave(n);
4022 if (rettv->v_type == VAR_FUNC)
4023 func_ref(rettv->vval.v_string);
4024 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004025 }
4026 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004027 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004028 else if (STRCMP(what, "args") == 0)
4029 {
4030 rettv->v_type = VAR_LIST;
4031 if (rettv_list_alloc(rettv) == OK)
4032 {
4033 int i;
4034
4035 for (i = 0; i < pt->pt_argc; ++i)
4036 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4037 }
4038 }
4039 else
4040 EMSG2(_(e_invarg2), what);
4041 return;
4042 }
4043 }
4044 else
4045 EMSG2(_(e_listdictarg), "get()");
4046
4047 if (tv == NULL)
4048 {
4049 if (argvars[2].v_type != VAR_UNKNOWN)
4050 copy_tv(&argvars[2], rettv);
4051 }
4052 else
4053 copy_tv(tv, rettv);
4054}
4055
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004056#ifdef FEAT_SIGNS
4057/*
4058 * Returns information about signs placed in a buffer as list of dicts.
4059 */
4060 static void
4061get_buffer_signs(buf_T *buf, list_T *l)
4062{
4063 signlist_T *sign;
4064
4065 for (sign = buf->b_signlist; sign; sign = sign->next)
4066 {
4067 dict_T *d = dict_alloc();
4068
4069 if (d != NULL)
4070 {
4071 dict_add_nr_str(d, "id", sign->id, NULL);
4072 dict_add_nr_str(d, "lnum", sign->lnum, NULL);
Bram Moolenaar6a402ed2016-08-28 14:11:24 +02004073 dict_add_nr_str(d, "name", 0L, sign_typenr2name(sign->typenr));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004074
4075 list_append_dict(l, d);
4076 }
4077 }
4078}
4079#endif
4080
4081/*
4082 * Returns buffer options, variables and other attributes in a dictionary.
4083 */
4084 static dict_T *
4085get_buffer_info(buf_T *buf)
4086{
4087 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004088 tabpage_T *tp;
4089 win_T *wp;
4090 list_T *windows;
4091
4092 dict = dict_alloc();
4093 if (dict == NULL)
4094 return NULL;
4095
Bram Moolenaar33928832016-08-18 21:22:04 +02004096 dict_add_nr_str(dict, "bufnr", buf->b_fnum, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004097 dict_add_nr_str(dict, "name", 0L,
4098 buf->b_ffname != NULL ? buf->b_ffname : (char_u *)"");
Bram Moolenaarf845b872017-01-06 14:04:54 +01004099 dict_add_nr_str(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4100 : buflist_findlnum(buf), NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004101 dict_add_nr_str(dict, "loaded", buf->b_ml.ml_mfp != NULL, NULL);
4102 dict_add_nr_str(dict, "listed", buf->b_p_bl, NULL);
4103 dict_add_nr_str(dict, "changed", bufIsChanged(buf), NULL);
Bram Moolenaar95c526e2017-02-25 14:59:34 +01004104 dict_add_nr_str(dict, "changedtick", CHANGEDTICK(buf), NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004105 dict_add_nr_str(dict, "hidden",
4106 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0,
4107 NULL);
4108
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004109 /* Get a reference to buffer variables */
4110 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004111
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004112 /* List of windows displaying this buffer */
4113 windows = list_alloc();
4114 if (windows != NULL)
4115 {
4116 FOR_ALL_TAB_WINDOWS(tp, wp)
4117 if (wp->w_buffer == buf)
4118 list_append_number(windows, (varnumber_T)wp->w_id);
4119 dict_add_list(dict, "windows", windows);
4120 }
4121
4122#ifdef FEAT_SIGNS
4123 if (buf->b_signlist != NULL)
4124 {
4125 /* List of signs placed in this buffer */
4126 list_T *signs = list_alloc();
4127 if (signs != NULL)
4128 {
4129 get_buffer_signs(buf, signs);
4130 dict_add_list(dict, "signs", signs);
4131 }
4132 }
4133#endif
4134
4135 return dict;
4136}
4137
4138/*
4139 * "getbufinfo()" function
4140 */
4141 static void
4142f_getbufinfo(typval_T *argvars, typval_T *rettv)
4143{
4144 buf_T *buf = NULL;
4145 buf_T *argbuf = NULL;
4146 dict_T *d;
4147 int filtered = FALSE;
4148 int sel_buflisted = FALSE;
4149 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004150 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004151
4152 if (rettv_list_alloc(rettv) != OK)
4153 return;
4154
4155 /* List of all the buffers or selected buffers */
4156 if (argvars[0].v_type == VAR_DICT)
4157 {
4158 dict_T *sel_d = argvars[0].vval.v_dict;
4159
4160 if (sel_d != NULL)
4161 {
4162 dictitem_T *di;
4163
4164 filtered = TRUE;
4165
4166 di = dict_find(sel_d, (char_u *)"buflisted", -1);
4167 if (di != NULL && get_tv_number(&di->di_tv))
4168 sel_buflisted = TRUE;
4169
4170 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
4171 if (di != NULL && get_tv_number(&di->di_tv))
4172 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004173
4174 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
4175 if (di != NULL && get_tv_number(&di->di_tv))
4176 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004177 }
4178 }
4179 else if (argvars[0].v_type != VAR_UNKNOWN)
4180 {
4181 /* Information about one buffer. Argument specifies the buffer */
4182 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4183 ++emsg_off;
4184 argbuf = get_buf_tv(&argvars[0], FALSE);
4185 --emsg_off;
4186 if (argbuf == NULL)
4187 return;
4188 }
4189
4190 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004191 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004192 {
4193 if (argbuf != NULL && argbuf != buf)
4194 continue;
4195 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004196 || (sel_buflisted && !buf->b_p_bl)
4197 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004198 continue;
4199
4200 d = get_buffer_info(buf);
4201 if (d != NULL)
4202 list_append_dict(rettv->vval.v_list, d);
4203 if (argbuf != NULL)
4204 return;
4205 }
4206}
4207
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004208static void get_buffer_lines(buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv);
4209
4210/*
4211 * Get line or list of lines from buffer "buf" into "rettv".
4212 * Return a range (from start to end) of lines in rettv from the specified
4213 * buffer.
4214 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4215 */
4216 static void
4217get_buffer_lines(
4218 buf_T *buf,
4219 linenr_T start,
4220 linenr_T end,
4221 int retlist,
4222 typval_T *rettv)
4223{
4224 char_u *p;
4225
4226 rettv->v_type = VAR_STRING;
4227 rettv->vval.v_string = NULL;
4228 if (retlist && rettv_list_alloc(rettv) == FAIL)
4229 return;
4230
4231 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4232 return;
4233
4234 if (!retlist)
4235 {
4236 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4237 p = ml_get_buf(buf, start, FALSE);
4238 else
4239 p = (char_u *)"";
4240 rettv->vval.v_string = vim_strsave(p);
4241 }
4242 else
4243 {
4244 if (end < start)
4245 return;
4246
4247 if (start < 1)
4248 start = 1;
4249 if (end > buf->b_ml.ml_line_count)
4250 end = buf->b_ml.ml_line_count;
4251 while (start <= end)
4252 if (list_append_string(rettv->vval.v_list,
4253 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4254 break;
4255 }
4256}
4257
4258/*
4259 * Get the lnum from the first argument.
4260 * Also accepts "$", then "buf" is used.
4261 * Returns 0 on error.
4262 */
4263 static linenr_T
4264get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
4265{
4266 if (argvars[0].v_type == VAR_STRING
4267 && argvars[0].vval.v_string != NULL
4268 && argvars[0].vval.v_string[0] == '$'
4269 && buf != NULL)
4270 return buf->b_ml.ml_line_count;
4271 return (linenr_T)get_tv_number_chk(&argvars[0], NULL);
4272}
4273
4274/*
4275 * "getbufline()" function
4276 */
4277 static void
4278f_getbufline(typval_T *argvars, typval_T *rettv)
4279{
4280 linenr_T lnum;
4281 linenr_T end;
4282 buf_T *buf;
4283
4284 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4285 ++emsg_off;
4286 buf = get_buf_tv(&argvars[0], FALSE);
4287 --emsg_off;
4288
4289 lnum = get_tv_lnum_buf(&argvars[1], buf);
4290 if (argvars[2].v_type == VAR_UNKNOWN)
4291 end = lnum;
4292 else
4293 end = get_tv_lnum_buf(&argvars[2], buf);
4294
4295 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4296}
4297
4298/*
4299 * "getbufvar()" function
4300 */
4301 static void
4302f_getbufvar(typval_T *argvars, typval_T *rettv)
4303{
4304 buf_T *buf;
4305 buf_T *save_curbuf;
4306 char_u *varname;
4307 dictitem_T *v;
4308 int done = FALSE;
4309
4310 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4311 varname = get_tv_string_chk(&argvars[1]);
4312 ++emsg_off;
4313 buf = get_buf_tv(&argvars[0], FALSE);
4314
4315 rettv->v_type = VAR_STRING;
4316 rettv->vval.v_string = NULL;
4317
4318 if (buf != NULL && varname != NULL)
4319 {
4320 /* set curbuf to be our buf, temporarily */
4321 save_curbuf = curbuf;
4322 curbuf = buf;
4323
Bram Moolenaar30567352016-08-27 21:25:44 +02004324 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004325 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004326 if (varname[1] == NUL)
4327 {
4328 /* get all buffer-local options in a dict */
4329 dict_T *opts = get_winbuf_options(TRUE);
4330
4331 if (opts != NULL)
4332 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004333 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004334 done = TRUE;
4335 }
4336 }
4337 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4338 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004339 done = TRUE;
4340 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004341 else
4342 {
4343 /* Look up the variable. */
4344 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4345 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4346 'b', varname, FALSE);
4347 if (v != NULL)
4348 {
4349 copy_tv(&v->di_tv, rettv);
4350 done = TRUE;
4351 }
4352 }
4353
4354 /* restore previous notion of curbuf */
4355 curbuf = save_curbuf;
4356 }
4357
4358 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4359 /* use the default value */
4360 copy_tv(&argvars[2], rettv);
4361
4362 --emsg_off;
4363}
4364
4365/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004366 * "getchangelist()" function
4367 */
4368 static void
4369f_getchangelist(typval_T *argvars, typval_T *rettv)
4370{
4371#ifdef FEAT_JUMPLIST
4372 buf_T *buf;
4373 int i;
4374 list_T *l;
4375 dict_T *d;
4376#endif
4377
4378 if (rettv_list_alloc(rettv) != OK)
4379 return;
4380
4381#ifdef FEAT_JUMPLIST
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004382 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4383 ++emsg_off;
4384 buf = get_buf_tv(&argvars[0], FALSE);
4385 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004386 if (buf == NULL)
4387 return;
4388
4389 l = list_alloc();
4390 if (l == NULL)
4391 return;
4392
4393 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4394 return;
4395 /*
4396 * The current window change list index tracks only the position in the
4397 * current buffer change list. For other buffers, use the change list
4398 * length as the current index.
4399 */
4400 list_append_number(rettv->vval.v_list,
4401 (varnumber_T)((buf == curwin->w_buffer)
4402 ? curwin->w_changelistidx : buf->b_changelistlen));
4403
4404 for (i = 0; i < buf->b_changelistlen; ++i)
4405 {
4406 if (buf->b_changelist[i].lnum == 0)
4407 continue;
4408 if ((d = dict_alloc()) == NULL)
4409 return;
4410 if (list_append_dict(l, d) == FAIL)
4411 return;
4412 dict_add_nr_str(d, "lnum", (long)buf->b_changelist[i].lnum, NULL);
4413 dict_add_nr_str(d, "col", (long)buf->b_changelist[i].col, NULL);
4414# ifdef FEAT_VIRTUALEDIT
4415 dict_add_nr_str(d, "coladd", (long)buf->b_changelist[i].coladd, NULL);
4416# endif
4417 }
4418#endif
4419}
4420/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004421 * "getchar()" function
4422 */
4423 static void
4424f_getchar(typval_T *argvars, typval_T *rettv)
4425{
4426 varnumber_T n;
4427 int error = FALSE;
4428
4429 /* Position the cursor. Needed after a message that ends in a space. */
4430 windgoto(msg_row, msg_col);
4431
4432 ++no_mapping;
4433 ++allow_keys;
4434 for (;;)
4435 {
4436 if (argvars[0].v_type == VAR_UNKNOWN)
4437 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004438 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004439 else if (get_tv_number_chk(&argvars[0], &error) == 1)
4440 /* getchar(1): only check if char avail */
4441 n = vpeekc_any();
4442 else if (error || vpeekc_any() == NUL)
4443 /* illegal argument or getchar(0) and no char avail: return zero */
4444 n = 0;
4445 else
4446 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004447 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004448
4449 if (n == K_IGNORE)
4450 continue;
4451 break;
4452 }
4453 --no_mapping;
4454 --allow_keys;
4455
4456 set_vim_var_nr(VV_MOUSE_WIN, 0);
4457 set_vim_var_nr(VV_MOUSE_WINID, 0);
4458 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4459 set_vim_var_nr(VV_MOUSE_COL, 0);
4460
4461 rettv->vval.v_number = n;
4462 if (IS_SPECIAL(n) || mod_mask != 0)
4463 {
4464 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4465 int i = 0;
4466
4467 /* Turn a special key into three bytes, plus modifier. */
4468 if (mod_mask != 0)
4469 {
4470 temp[i++] = K_SPECIAL;
4471 temp[i++] = KS_MODIFIER;
4472 temp[i++] = mod_mask;
4473 }
4474 if (IS_SPECIAL(n))
4475 {
4476 temp[i++] = K_SPECIAL;
4477 temp[i++] = K_SECOND(n);
4478 temp[i++] = K_THIRD(n);
4479 }
4480#ifdef FEAT_MBYTE
4481 else if (has_mbyte)
4482 i += (*mb_char2bytes)(n, temp + i);
4483#endif
4484 else
4485 temp[i++] = n;
4486 temp[i++] = NUL;
4487 rettv->v_type = VAR_STRING;
4488 rettv->vval.v_string = vim_strsave(temp);
4489
4490#ifdef FEAT_MOUSE
4491 if (is_mouse_key(n))
4492 {
4493 int row = mouse_row;
4494 int col = mouse_col;
4495 win_T *win;
4496 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004497 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004498 int winnr = 1;
4499
4500 if (row >= 0 && col >= 0)
4501 {
4502 /* Find the window at the mouse coordinates and compute the
4503 * text position. */
4504 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004505 if (win == NULL)
4506 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004507 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004508 for (wp = firstwin; wp != win; wp = wp->w_next)
4509 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004510 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4511 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4512 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4513 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4514 }
4515 }
4516#endif
4517 }
4518}
4519
4520/*
4521 * "getcharmod()" function
4522 */
4523 static void
4524f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4525{
4526 rettv->vval.v_number = mod_mask;
4527}
4528
4529/*
4530 * "getcharsearch()" function
4531 */
4532 static void
4533f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4534{
4535 if (rettv_dict_alloc(rettv) != FAIL)
4536 {
4537 dict_T *dict = rettv->vval.v_dict;
4538
4539 dict_add_nr_str(dict, "char", 0L, last_csearch());
4540 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
4541 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
4542 }
4543}
4544
4545/*
4546 * "getcmdline()" function
4547 */
4548 static void
4549f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4550{
4551 rettv->v_type = VAR_STRING;
4552 rettv->vval.v_string = get_cmdline_str();
4553}
4554
4555/*
4556 * "getcmdpos()" function
4557 */
4558 static void
4559f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4560{
4561 rettv->vval.v_number = get_cmdline_pos() + 1;
4562}
4563
4564/*
4565 * "getcmdtype()" function
4566 */
4567 static void
4568f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4569{
4570 rettv->v_type = VAR_STRING;
4571 rettv->vval.v_string = alloc(2);
4572 if (rettv->vval.v_string != NULL)
4573 {
4574 rettv->vval.v_string[0] = get_cmdline_type();
4575 rettv->vval.v_string[1] = NUL;
4576 }
4577}
4578
4579/*
4580 * "getcmdwintype()" function
4581 */
4582 static void
4583f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4584{
4585 rettv->v_type = VAR_STRING;
4586 rettv->vval.v_string = NULL;
4587#ifdef FEAT_CMDWIN
4588 rettv->vval.v_string = alloc(2);
4589 if (rettv->vval.v_string != NULL)
4590 {
4591 rettv->vval.v_string[0] = cmdwin_type;
4592 rettv->vval.v_string[1] = NUL;
4593 }
4594#endif
4595}
4596
4597#if defined(FEAT_CMDL_COMPL)
4598/*
4599 * "getcompletion()" function
4600 */
4601 static void
4602f_getcompletion(typval_T *argvars, typval_T *rettv)
4603{
4604 char_u *pat;
4605 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004606 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004607 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
4608 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004609
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004610 if (argvars[2].v_type != VAR_UNKNOWN)
4611 filtered = get_tv_number_chk(&argvars[2], NULL);
4612
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004613 if (p_wic)
4614 options |= WILD_ICASE;
4615
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004616 /* For filtered results, 'wildignore' is used */
4617 if (!filtered)
4618 options |= WILD_KEEP_ALL;
4619
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004620 ExpandInit(&xpc);
4621 xpc.xp_pattern = get_tv_string(&argvars[0]);
4622 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4623 xpc.xp_context = cmdcomplete_str_to_type(get_tv_string(&argvars[1]));
4624 if (xpc.xp_context == EXPAND_NOTHING)
4625 {
4626 if (argvars[1].v_type == VAR_STRING)
4627 EMSG2(_(e_invarg2), argvars[1].vval.v_string);
4628 else
4629 EMSG(_(e_invarg));
4630 return;
4631 }
4632
4633# if defined(FEAT_MENU)
4634 if (xpc.xp_context == EXPAND_MENUS)
4635 {
4636 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4637 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4638 }
4639# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02004640#ifdef FEAT_CSCOPE
4641 if (xpc.xp_context == EXPAND_CSCOPE)
4642 {
4643 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4644 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4645 }
4646#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02004647#ifdef FEAT_SIGNS
4648 if (xpc.xp_context == EXPAND_SIGN)
4649 {
4650 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4651 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4652 }
4653#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004654
4655 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4656 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
4657 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004658 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004659
4660 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4661
4662 for (i = 0; i < xpc.xp_numfiles; i++)
4663 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4664 }
4665 vim_free(pat);
4666 ExpandCleanup(&xpc);
4667}
4668#endif
4669
4670/*
4671 * "getcwd()" function
4672 */
4673 static void
4674f_getcwd(typval_T *argvars, typval_T *rettv)
4675{
4676 win_T *wp = NULL;
4677 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01004678 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004679
4680 rettv->v_type = VAR_STRING;
4681 rettv->vval.v_string = NULL;
4682
Bram Moolenaar54591292018-02-09 20:53:59 +01004683 if (argvars[0].v_type == VAR_NUMBER && argvars[0].vval.v_number == -1)
4684 global = TRUE;
4685 else
4686 wp = find_tabwin(&argvars[0], &argvars[1]);
4687
4688 if (wp != NULL && wp->w_localdir != NULL)
4689 rettv->vval.v_string = vim_strsave(wp->w_localdir);
4690 else if (wp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004691 {
Bram Moolenaar54591292018-02-09 20:53:59 +01004692 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004693 rettv->vval.v_string = vim_strsave(globaldir);
4694 else
4695 {
4696 cwd = alloc(MAXPATHL);
4697 if (cwd != NULL)
4698 {
4699 if (mch_dirname(cwd, MAXPATHL) != FAIL)
4700 rettv->vval.v_string = vim_strsave(cwd);
4701 vim_free(cwd);
4702 }
4703 }
4704#ifdef BACKSLASH_IN_FILENAME
4705 if (rettv->vval.v_string != NULL)
4706 slash_adjust(rettv->vval.v_string);
4707#endif
4708 }
4709}
4710
4711/*
4712 * "getfontname()" function
4713 */
4714 static void
4715f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4716{
4717 rettv->v_type = VAR_STRING;
4718 rettv->vval.v_string = NULL;
4719#ifdef FEAT_GUI
4720 if (gui.in_use)
4721 {
4722 GuiFont font;
4723 char_u *name = NULL;
4724
4725 if (argvars[0].v_type == VAR_UNKNOWN)
4726 {
4727 /* Get the "Normal" font. Either the name saved by
4728 * hl_set_font_name() or from the font ID. */
4729 font = gui.norm_font;
4730 name = hl_get_font_name();
4731 }
4732 else
4733 {
4734 name = get_tv_string(&argvars[0]);
4735 if (STRCMP(name, "*") == 0) /* don't use font dialog */
4736 return;
4737 font = gui_mch_get_font(name, FALSE);
4738 if (font == NOFONT)
4739 return; /* Invalid font name, return empty string. */
4740 }
4741 rettv->vval.v_string = gui_mch_get_fontname(font, name);
4742 if (argvars[0].v_type != VAR_UNKNOWN)
4743 gui_mch_free_font(font);
4744 }
4745#endif
4746}
4747
4748/*
4749 * "getfperm({fname})" function
4750 */
4751 static void
4752f_getfperm(typval_T *argvars, typval_T *rettv)
4753{
4754 char_u *fname;
4755 stat_T st;
4756 char_u *perm = NULL;
4757 char_u flags[] = "rwx";
4758 int i;
4759
4760 fname = get_tv_string(&argvars[0]);
4761
4762 rettv->v_type = VAR_STRING;
4763 if (mch_stat((char *)fname, &st) >= 0)
4764 {
4765 perm = vim_strsave((char_u *)"---------");
4766 if (perm != NULL)
4767 {
4768 for (i = 0; i < 9; i++)
4769 {
4770 if (st.st_mode & (1 << (8 - i)))
4771 perm[i] = flags[i % 3];
4772 }
4773 }
4774 }
4775 rettv->vval.v_string = perm;
4776}
4777
4778/*
4779 * "getfsize({fname})" function
4780 */
4781 static void
4782f_getfsize(typval_T *argvars, typval_T *rettv)
4783{
4784 char_u *fname;
4785 stat_T st;
4786
4787 fname = get_tv_string(&argvars[0]);
4788
4789 rettv->v_type = VAR_NUMBER;
4790
4791 if (mch_stat((char *)fname, &st) >= 0)
4792 {
4793 if (mch_isdir(fname))
4794 rettv->vval.v_number = 0;
4795 else
4796 {
4797 rettv->vval.v_number = (varnumber_T)st.st_size;
4798
4799 /* non-perfect check for overflow */
4800 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
4801 rettv->vval.v_number = -2;
4802 }
4803 }
4804 else
4805 rettv->vval.v_number = -1;
4806}
4807
4808/*
4809 * "getftime({fname})" function
4810 */
4811 static void
4812f_getftime(typval_T *argvars, typval_T *rettv)
4813{
4814 char_u *fname;
4815 stat_T st;
4816
4817 fname = get_tv_string(&argvars[0]);
4818
4819 if (mch_stat((char *)fname, &st) >= 0)
4820 rettv->vval.v_number = (varnumber_T)st.st_mtime;
4821 else
4822 rettv->vval.v_number = -1;
4823}
4824
4825/*
4826 * "getftype({fname})" function
4827 */
4828 static void
4829f_getftype(typval_T *argvars, typval_T *rettv)
4830{
4831 char_u *fname;
4832 stat_T st;
4833 char_u *type = NULL;
4834 char *t;
4835
4836 fname = get_tv_string(&argvars[0]);
4837
4838 rettv->v_type = VAR_STRING;
4839 if (mch_lstat((char *)fname, &st) >= 0)
4840 {
4841#ifdef S_ISREG
4842 if (S_ISREG(st.st_mode))
4843 t = "file";
4844 else if (S_ISDIR(st.st_mode))
4845 t = "dir";
4846# ifdef S_ISLNK
4847 else if (S_ISLNK(st.st_mode))
4848 t = "link";
4849# endif
4850# ifdef S_ISBLK
4851 else if (S_ISBLK(st.st_mode))
4852 t = "bdev";
4853# endif
4854# ifdef S_ISCHR
4855 else if (S_ISCHR(st.st_mode))
4856 t = "cdev";
4857# endif
4858# ifdef S_ISFIFO
4859 else if (S_ISFIFO(st.st_mode))
4860 t = "fifo";
4861# endif
4862# ifdef S_ISSOCK
4863 else if (S_ISSOCK(st.st_mode))
4864 t = "fifo";
4865# endif
4866 else
4867 t = "other";
4868#else
4869# ifdef S_IFMT
4870 switch (st.st_mode & S_IFMT)
4871 {
4872 case S_IFREG: t = "file"; break;
4873 case S_IFDIR: t = "dir"; break;
4874# ifdef S_IFLNK
4875 case S_IFLNK: t = "link"; break;
4876# endif
4877# ifdef S_IFBLK
4878 case S_IFBLK: t = "bdev"; break;
4879# endif
4880# ifdef S_IFCHR
4881 case S_IFCHR: t = "cdev"; break;
4882# endif
4883# ifdef S_IFIFO
4884 case S_IFIFO: t = "fifo"; break;
4885# endif
4886# ifdef S_IFSOCK
4887 case S_IFSOCK: t = "socket"; break;
4888# endif
4889 default: t = "other";
4890 }
4891# else
4892 if (mch_isdir(fname))
4893 t = "dir";
4894 else
4895 t = "file";
4896# endif
4897#endif
4898 type = vim_strsave((char_u *)t);
4899 }
4900 rettv->vval.v_string = type;
4901}
4902
4903/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01004904 * "getjumplist()" function
4905 */
4906 static void
4907f_getjumplist(typval_T *argvars, typval_T *rettv)
4908{
4909#ifdef FEAT_JUMPLIST
4910 win_T *wp;
4911 int i;
4912 list_T *l;
4913 dict_T *d;
4914#endif
4915
4916 if (rettv_list_alloc(rettv) != OK)
4917 return;
4918
4919#ifdef FEAT_JUMPLIST
4920 wp = find_tabwin(&argvars[0], &argvars[1]);
4921 if (wp == NULL)
4922 return;
4923
4924 l = list_alloc();
4925 if (l == NULL)
4926 return;
4927
4928 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4929 return;
4930 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
4931
Bram Moolenaar48679742018-02-13 13:33:29 +01004932 cleanup_jumplist(wp, TRUE);
4933
Bram Moolenaar4f505882018-02-10 21:06:32 +01004934 for (i = 0; i < wp->w_jumplistlen; ++i)
4935 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004936 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
4937 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01004938 if ((d = dict_alloc()) == NULL)
4939 return;
4940 if (list_append_dict(l, d) == FAIL)
4941 return;
4942 dict_add_nr_str(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum,
4943 NULL);
4944 dict_add_nr_str(d, "col", (long)wp->w_jumplist[i].fmark.mark.col,
4945 NULL);
4946# ifdef FEAT_VIRTUALEDIT
4947 dict_add_nr_str(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd,
4948 NULL);
4949# endif
4950 dict_add_nr_str(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum, NULL);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004951 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaar4f505882018-02-10 21:06:32 +01004952 dict_add_nr_str(d, "filename", 0L, wp->w_jumplist[i].fname);
4953 }
4954#endif
4955}
4956
4957/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004958 * "getline(lnum, [end])" function
4959 */
4960 static void
4961f_getline(typval_T *argvars, typval_T *rettv)
4962{
4963 linenr_T lnum;
4964 linenr_T end;
4965 int retlist;
4966
4967 lnum = get_tv_lnum(argvars);
4968 if (argvars[1].v_type == VAR_UNKNOWN)
4969 {
4970 end = 0;
4971 retlist = FALSE;
4972 }
4973 else
4974 {
4975 end = get_tv_lnum(&argvars[1]);
4976 retlist = TRUE;
4977 }
4978
4979 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
4980}
4981
Bram Moolenaar4ae20952016-08-13 15:29:14 +02004982#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02004983 static void
4984get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
4985{
Bram Moolenaard823fa92016-08-12 16:29:27 +02004986 if (what_arg->v_type == VAR_UNKNOWN)
4987 {
4988 if (rettv_list_alloc(rettv) == OK)
4989 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004990 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004991 }
4992 else
4993 {
4994 if (rettv_dict_alloc(rettv) == OK)
4995 if (is_qf || (wp != NULL))
4996 {
4997 if (what_arg->v_type == VAR_DICT)
4998 {
4999 dict_T *d = what_arg->vval.v_dict;
5000
5001 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005002 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005003 }
5004 else
5005 EMSG(_(e_dictreq));
5006 }
5007 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005008}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005009#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005010
5011/*
5012 * "getloclist()" function
5013 */
5014 static void
5015f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5016{
5017#ifdef FEAT_QUICKFIX
5018 win_T *wp;
5019
5020 wp = find_win_by_nr(&argvars[0], NULL);
5021 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5022#endif
5023}
5024
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005025/*
5026 * "getmatches()" function
5027 */
5028 static void
5029f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5030{
5031#ifdef FEAT_SEARCH_EXTRA
5032 dict_T *dict;
5033 matchitem_T *cur = curwin->w_match_head;
5034 int i;
5035
5036 if (rettv_list_alloc(rettv) == OK)
5037 {
5038 while (cur != NULL)
5039 {
5040 dict = dict_alloc();
5041 if (dict == NULL)
5042 return;
5043 if (cur->match.regprog == NULL)
5044 {
5045 /* match added with matchaddpos() */
5046 for (i = 0; i < MAXPOSMATCH; ++i)
5047 {
5048 llpos_T *llpos;
5049 char buf[6];
5050 list_T *l;
5051
5052 llpos = &cur->pos.pos[i];
5053 if (llpos->lnum == 0)
5054 break;
5055 l = list_alloc();
5056 if (l == NULL)
5057 break;
5058 list_append_number(l, (varnumber_T)llpos->lnum);
5059 if (llpos->col > 0)
5060 {
5061 list_append_number(l, (varnumber_T)llpos->col);
5062 list_append_number(l, (varnumber_T)llpos->len);
5063 }
5064 sprintf(buf, "pos%d", i + 1);
5065 dict_add_list(dict, buf, l);
5066 }
5067 }
5068 else
5069 {
5070 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
5071 }
5072 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
5073 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
5074 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
5075# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
5076 if (cur->conceal_char)
5077 {
5078 char_u buf[MB_MAXBYTES + 1];
5079
5080 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
5081 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
5082 }
5083# endif
5084 list_append_dict(rettv->vval.v_list, dict);
5085 cur = cur->next;
5086 }
5087 }
5088#endif
5089}
5090
5091/*
5092 * "getpid()" function
5093 */
5094 static void
5095f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5096{
5097 rettv->vval.v_number = mch_get_pid();
5098}
5099
5100 static void
5101getpos_both(
5102 typval_T *argvars,
5103 typval_T *rettv,
5104 int getcurpos)
5105{
5106 pos_T *fp;
5107 list_T *l;
5108 int fnum = -1;
5109
5110 if (rettv_list_alloc(rettv) == OK)
5111 {
5112 l = rettv->vval.v_list;
5113 if (getcurpos)
5114 fp = &curwin->w_cursor;
5115 else
5116 fp = var2fpos(&argvars[0], TRUE, &fnum);
5117 if (fnum != -1)
5118 list_append_number(l, (varnumber_T)fnum);
5119 else
5120 list_append_number(l, (varnumber_T)0);
5121 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5122 : (varnumber_T)0);
5123 list_append_number(l, (fp != NULL)
5124 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5125 : (varnumber_T)0);
5126 list_append_number(l,
5127#ifdef FEAT_VIRTUALEDIT
5128 (fp != NULL) ? (varnumber_T)fp->coladd :
5129#endif
5130 (varnumber_T)0);
5131 if (getcurpos)
5132 {
5133 update_curswant();
5134 list_append_number(l, curwin->w_curswant == MAXCOL ?
5135 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
5136 }
5137 }
5138 else
5139 rettv->vval.v_number = FALSE;
5140}
5141
5142
5143/*
5144 * "getcurpos()" function
5145 */
5146 static void
5147f_getcurpos(typval_T *argvars, typval_T *rettv)
5148{
5149 getpos_both(argvars, rettv, TRUE);
5150}
5151
5152/*
5153 * "getpos(string)" function
5154 */
5155 static void
5156f_getpos(typval_T *argvars, typval_T *rettv)
5157{
5158 getpos_both(argvars, rettv, FALSE);
5159}
5160
5161/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005162 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005163 */
5164 static void
5165f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5166{
5167#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005168 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005169#endif
5170}
5171
5172/*
5173 * "getreg()" function
5174 */
5175 static void
5176f_getreg(typval_T *argvars, typval_T *rettv)
5177{
5178 char_u *strregname;
5179 int regname;
5180 int arg2 = FALSE;
5181 int return_list = FALSE;
5182 int error = FALSE;
5183
5184 if (argvars[0].v_type != VAR_UNKNOWN)
5185 {
5186 strregname = get_tv_string_chk(&argvars[0]);
5187 error = strregname == NULL;
5188 if (argvars[1].v_type != VAR_UNKNOWN)
5189 {
5190 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
5191 if (!error && argvars[2].v_type != VAR_UNKNOWN)
5192 return_list = (int)get_tv_number_chk(&argvars[2], &error);
5193 }
5194 }
5195 else
5196 strregname = get_vim_var_str(VV_REG);
5197
5198 if (error)
5199 return;
5200
5201 regname = (strregname == NULL ? '"' : *strregname);
5202 if (regname == 0)
5203 regname = '"';
5204
5205 if (return_list)
5206 {
5207 rettv->v_type = VAR_LIST;
5208 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5209 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5210 if (rettv->vval.v_list == NULL)
5211 (void)rettv_list_alloc(rettv);
5212 else
5213 ++rettv->vval.v_list->lv_refcount;
5214 }
5215 else
5216 {
5217 rettv->v_type = VAR_STRING;
5218 rettv->vval.v_string = get_reg_contents(regname,
5219 arg2 ? GREG_EXPR_SRC : 0);
5220 }
5221}
5222
5223/*
5224 * "getregtype()" function
5225 */
5226 static void
5227f_getregtype(typval_T *argvars, typval_T *rettv)
5228{
5229 char_u *strregname;
5230 int regname;
5231 char_u buf[NUMBUFLEN + 2];
5232 long reglen = 0;
5233
5234 if (argvars[0].v_type != VAR_UNKNOWN)
5235 {
5236 strregname = get_tv_string_chk(&argvars[0]);
5237 if (strregname == NULL) /* type error; errmsg already given */
5238 {
5239 rettv->v_type = VAR_STRING;
5240 rettv->vval.v_string = NULL;
5241 return;
5242 }
5243 }
5244 else
5245 /* Default to v:register */
5246 strregname = get_vim_var_str(VV_REG);
5247
5248 regname = (strregname == NULL ? '"' : *strregname);
5249 if (regname == 0)
5250 regname = '"';
5251
5252 buf[0] = NUL;
5253 buf[1] = NUL;
5254 switch (get_reg_type(regname, &reglen))
5255 {
5256 case MLINE: buf[0] = 'V'; break;
5257 case MCHAR: buf[0] = 'v'; break;
5258 case MBLOCK:
5259 buf[0] = Ctrl_V;
5260 sprintf((char *)buf + 1, "%ld", reglen + 1);
5261 break;
5262 }
5263 rettv->v_type = VAR_STRING;
5264 rettv->vval.v_string = vim_strsave(buf);
5265}
5266
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005267/*
5268 * Returns information (variables, options, etc.) about a tab page
5269 * as a dictionary.
5270 */
5271 static dict_T *
5272get_tabpage_info(tabpage_T *tp, int tp_idx)
5273{
5274 win_T *wp;
5275 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005276 list_T *l;
5277
5278 dict = dict_alloc();
5279 if (dict == NULL)
5280 return NULL;
5281
Bram Moolenaar33928832016-08-18 21:22:04 +02005282 dict_add_nr_str(dict, "tabnr", tp_idx, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005283
5284 l = list_alloc();
5285 if (l != NULL)
5286 {
5287 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5288 wp; wp = wp->w_next)
5289 list_append_number(l, (varnumber_T)wp->w_id);
5290 dict_add_list(dict, "windows", l);
5291 }
5292
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005293 /* Make a reference to tabpage variables */
5294 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005295
5296 return dict;
5297}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005298
5299/*
5300 * "gettabinfo()" function
5301 */
5302 static void
5303f_gettabinfo(typval_T *argvars, typval_T *rettv)
5304{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005305 tabpage_T *tp, *tparg = NULL;
5306 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005307 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005308
5309 if (rettv_list_alloc(rettv) != OK)
5310 return;
5311
5312 if (argvars[0].v_type != VAR_UNKNOWN)
5313 {
5314 /* Information about one tab page */
5315 tparg = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5316 if (tparg == NULL)
5317 return;
5318 }
5319
5320 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005321 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005322 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005323 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005324 if (tparg != NULL && tp != tparg)
5325 continue;
5326 d = get_tabpage_info(tp, tpnr);
5327 if (d != NULL)
5328 list_append_dict(rettv->vval.v_list, d);
5329 if (tparg != NULL)
5330 return;
5331 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005332}
5333
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005334/*
5335 * "gettabvar()" function
5336 */
5337 static void
5338f_gettabvar(typval_T *argvars, typval_T *rettv)
5339{
5340 win_T *oldcurwin;
5341 tabpage_T *tp, *oldtabpage;
5342 dictitem_T *v;
5343 char_u *varname;
5344 int done = FALSE;
5345
5346 rettv->v_type = VAR_STRING;
5347 rettv->vval.v_string = NULL;
5348
5349 varname = get_tv_string_chk(&argvars[1]);
5350 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5351 if (tp != NULL && varname != NULL)
5352 {
5353 /* Set tp to be our tabpage, temporarily. Also set the window to the
5354 * first window in the tabpage, otherwise the window is not valid. */
5355 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005356 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5357 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005358 {
5359 /* look up the variable */
5360 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5361 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5362 if (v != NULL)
5363 {
5364 copy_tv(&v->di_tv, rettv);
5365 done = TRUE;
5366 }
5367 }
5368
5369 /* restore previous notion of curwin */
5370 restore_win(oldcurwin, oldtabpage, TRUE);
5371 }
5372
5373 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5374 copy_tv(&argvars[2], rettv);
5375}
5376
5377/*
5378 * "gettabwinvar()" function
5379 */
5380 static void
5381f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5382{
5383 getwinvar(argvars, rettv, 1);
5384}
5385
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005386/*
5387 * Returns information about a window as a dictionary.
5388 */
5389 static dict_T *
5390get_win_info(win_T *wp, short tpnr, short winnr)
5391{
5392 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005393
5394 dict = dict_alloc();
5395 if (dict == NULL)
5396 return NULL;
5397
Bram Moolenaar33928832016-08-18 21:22:04 +02005398 dict_add_nr_str(dict, "tabnr", tpnr, NULL);
5399 dict_add_nr_str(dict, "winnr", winnr, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005400 dict_add_nr_str(dict, "winid", wp->w_id, NULL);
5401 dict_add_nr_str(dict, "height", wp->w_height, NULL);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005402#ifdef FEAT_MENU
5403 dict_add_nr_str(dict, "winbar", wp->w_winbar_height, NULL);
5404#endif
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005405 dict_add_nr_str(dict, "width", wp->w_width, NULL);
Bram Moolenaar33928832016-08-18 21:22:04 +02005406 dict_add_nr_str(dict, "bufnr", wp->w_buffer->b_fnum, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005407
Bram Moolenaar69905d12017-08-13 18:14:47 +02005408#ifdef FEAT_TERMINAL
5409 dict_add_nr_str(dict, "terminal", bt_terminal(wp->w_buffer), NULL);
5410#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005411#ifdef FEAT_QUICKFIX
5412 dict_add_nr_str(dict, "quickfix", bt_quickfix(wp->w_buffer), NULL);
5413 dict_add_nr_str(dict, "loclist",
5414 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL), NULL);
5415#endif
5416
Bram Moolenaar30567352016-08-27 21:25:44 +02005417 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005418 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005419
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005420 return dict;
5421}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005422
5423/*
5424 * "getwininfo()" function
5425 */
5426 static void
5427f_getwininfo(typval_T *argvars, typval_T *rettv)
5428{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005429 tabpage_T *tp;
5430 win_T *wp = NULL, *wparg = NULL;
5431 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005432 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005433
5434 if (rettv_list_alloc(rettv) != OK)
5435 return;
5436
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005437 if (argvars[0].v_type != VAR_UNKNOWN)
5438 {
5439 wparg = win_id2wp(argvars);
5440 if (wparg == NULL)
5441 return;
5442 }
5443
5444 /* Collect information about either all the windows across all the tab
5445 * pages or one particular window.
5446 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005447 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005448 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005449 tabnr++;
5450 winnr = 0;
5451 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005452 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005453 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005454 if (wparg != NULL && wp != wparg)
5455 continue;
5456 d = get_win_info(wp, tabnr, winnr);
5457 if (d != NULL)
5458 list_append_dict(rettv->vval.v_list, d);
5459 if (wparg != NULL)
5460 /* found information about a specific window */
5461 return;
5462 }
5463 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005464}
5465
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005466/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005467 * "win_findbuf()" function
5468 */
5469 static void
5470f_win_findbuf(typval_T *argvars, typval_T *rettv)
5471{
5472 if (rettv_list_alloc(rettv) != FAIL)
5473 win_findbuf(argvars, rettv->vval.v_list);
5474}
5475
5476/*
5477 * "win_getid()" function
5478 */
5479 static void
5480f_win_getid(typval_T *argvars, typval_T *rettv)
5481{
5482 rettv->vval.v_number = win_getid(argvars);
5483}
5484
5485/*
5486 * "win_gotoid()" function
5487 */
5488 static void
5489f_win_gotoid(typval_T *argvars, typval_T *rettv)
5490{
5491 rettv->vval.v_number = win_gotoid(argvars);
5492}
5493
5494/*
5495 * "win_id2tabwin()" function
5496 */
5497 static void
5498f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5499{
5500 if (rettv_list_alloc(rettv) != FAIL)
5501 win_id2tabwin(argvars, rettv->vval.v_list);
5502}
5503
5504/*
5505 * "win_id2win()" function
5506 */
5507 static void
5508f_win_id2win(typval_T *argvars, typval_T *rettv)
5509{
5510 rettv->vval.v_number = win_id2win(argvars);
5511}
5512
5513/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005514 * "win_screenpos()" function
5515 */
5516 static void
5517f_win_screenpos(typval_T *argvars, typval_T *rettv)
5518{
5519 win_T *wp;
5520
5521 if (rettv_list_alloc(rettv) == FAIL)
5522 return;
5523
5524 wp = find_win_by_nr(&argvars[0], NULL);
5525 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5526 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5527}
5528
5529/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005530 * "getwinposx()" function
5531 */
5532 static void
5533f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5534{
5535 rettv->vval.v_number = -1;
5536#ifdef FEAT_GUI
5537 if (gui.in_use)
5538 {
5539 int x, y;
5540
5541 if (gui_mch_get_winpos(&x, &y) == OK)
5542 rettv->vval.v_number = x;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005543 return;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005544 }
5545#endif
5546#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5547 {
5548 int x, y;
5549
5550 if (term_get_winpos(&x, &y) == OK)
5551 rettv->vval.v_number = x;
5552 }
5553#endif
5554}
5555
5556/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005557 * "getwinposy()" function
5558 */
5559 static void
5560f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5561{
5562 rettv->vval.v_number = -1;
5563#ifdef FEAT_GUI
5564 if (gui.in_use)
5565 {
5566 int x, y;
5567
5568 if (gui_mch_get_winpos(&x, &y) == OK)
5569 rettv->vval.v_number = y;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005570 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005571 }
5572#endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005573#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5574 {
5575 int x, y;
5576
5577 if (term_get_winpos(&x, &y) == OK)
5578 rettv->vval.v_number = y;
5579 }
5580#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005581}
5582
5583/*
5584 * "getwinvar()" function
5585 */
5586 static void
5587f_getwinvar(typval_T *argvars, typval_T *rettv)
5588{
5589 getwinvar(argvars, rettv, 0);
5590}
5591
5592/*
5593 * "glob()" function
5594 */
5595 static void
5596f_glob(typval_T *argvars, typval_T *rettv)
5597{
5598 int options = WILD_SILENT|WILD_USE_NL;
5599 expand_T xpc;
5600 int error = FALSE;
5601
5602 /* When the optional second argument is non-zero, don't remove matches
5603 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5604 rettv->v_type = VAR_STRING;
5605 if (argvars[1].v_type != VAR_UNKNOWN)
5606 {
5607 if (get_tv_number_chk(&argvars[1], &error))
5608 options |= WILD_KEEP_ALL;
5609 if (argvars[2].v_type != VAR_UNKNOWN)
5610 {
5611 if (get_tv_number_chk(&argvars[2], &error))
5612 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005613 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005614 }
5615 if (argvars[3].v_type != VAR_UNKNOWN
5616 && get_tv_number_chk(&argvars[3], &error))
5617 options |= WILD_ALLLINKS;
5618 }
5619 }
5620 if (!error)
5621 {
5622 ExpandInit(&xpc);
5623 xpc.xp_context = EXPAND_FILES;
5624 if (p_wic)
5625 options += WILD_ICASE;
5626 if (rettv->v_type == VAR_STRING)
5627 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
5628 NULL, options, WILD_ALL);
5629 else if (rettv_list_alloc(rettv) != FAIL)
5630 {
5631 int i;
5632
5633 ExpandOne(&xpc, get_tv_string(&argvars[0]),
5634 NULL, options, WILD_ALL_KEEP);
5635 for (i = 0; i < xpc.xp_numfiles; i++)
5636 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5637
5638 ExpandCleanup(&xpc);
5639 }
5640 }
5641 else
5642 rettv->vval.v_string = NULL;
5643}
5644
5645/*
5646 * "globpath()" function
5647 */
5648 static void
5649f_globpath(typval_T *argvars, typval_T *rettv)
5650{
5651 int flags = 0;
5652 char_u buf1[NUMBUFLEN];
5653 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
5654 int error = FALSE;
5655 garray_T ga;
5656 int i;
5657
5658 /* When the optional second argument is non-zero, don't remove matches
5659 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5660 rettv->v_type = VAR_STRING;
5661 if (argvars[2].v_type != VAR_UNKNOWN)
5662 {
5663 if (get_tv_number_chk(&argvars[2], &error))
5664 flags |= WILD_KEEP_ALL;
5665 if (argvars[3].v_type != VAR_UNKNOWN)
5666 {
5667 if (get_tv_number_chk(&argvars[3], &error))
5668 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005669 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005670 }
5671 if (argvars[4].v_type != VAR_UNKNOWN
5672 && get_tv_number_chk(&argvars[4], &error))
5673 flags |= WILD_ALLLINKS;
5674 }
5675 }
5676 if (file != NULL && !error)
5677 {
5678 ga_init2(&ga, (int)sizeof(char_u *), 10);
5679 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
5680 if (rettv->v_type == VAR_STRING)
5681 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
5682 else if (rettv_list_alloc(rettv) != FAIL)
5683 for (i = 0; i < ga.ga_len; ++i)
5684 list_append_string(rettv->vval.v_list,
5685 ((char_u **)(ga.ga_data))[i], -1);
5686 ga_clear_strings(&ga);
5687 }
5688 else
5689 rettv->vval.v_string = NULL;
5690}
5691
5692/*
5693 * "glob2regpat()" function
5694 */
5695 static void
5696f_glob2regpat(typval_T *argvars, typval_T *rettv)
5697{
5698 char_u *pat = get_tv_string_chk(&argvars[0]);
5699
5700 rettv->v_type = VAR_STRING;
5701 rettv->vval.v_string = (pat == NULL)
5702 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
5703}
5704
5705/* for VIM_VERSION_ defines */
5706#include "version.h"
5707
5708/*
5709 * "has()" function
5710 */
5711 static void
5712f_has(typval_T *argvars, typval_T *rettv)
5713{
5714 int i;
5715 char_u *name;
5716 int n = FALSE;
5717 static char *(has_list[]) =
5718 {
5719#ifdef AMIGA
5720 "amiga",
5721# ifdef FEAT_ARP
5722 "arp",
5723# endif
5724#endif
5725#ifdef __BEOS__
5726 "beos",
5727#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02005728#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01005729 "mac", /* Mac OS X (and, once, Mac OS Classic) */
5730 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02005731# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01005732 "macunix", /* Mac OS X, with the darwin feature */
5733 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02005734# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005735#endif
5736#ifdef __QNX__
5737 "qnx",
5738#endif
5739#ifdef UNIX
5740 "unix",
5741#endif
5742#ifdef VMS
5743 "vms",
5744#endif
5745#ifdef WIN32
5746 "win32",
5747#endif
5748#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
5749 "win32unix",
5750#endif
5751#if defined(WIN64) || defined(_WIN64)
5752 "win64",
5753#endif
5754#ifdef EBCDIC
5755 "ebcdic",
5756#endif
5757#ifndef CASE_INSENSITIVE_FILENAME
5758 "fname_case",
5759#endif
5760#ifdef HAVE_ACL
5761 "acl",
5762#endif
5763#ifdef FEAT_ARABIC
5764 "arabic",
5765#endif
5766#ifdef FEAT_AUTOCMD
5767 "autocmd",
5768#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01005769#ifdef FEAT_AUTOSERVERNAME
5770 "autoservername",
5771#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01005772#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005773 "balloon_eval",
5774# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
5775 "balloon_multiline",
5776# endif
5777#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01005778#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01005779 "balloon_eval_term",
5780#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005781#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
5782 "builtin_terms",
5783# ifdef ALL_BUILTIN_TCAPS
5784 "all_builtin_terms",
5785# endif
5786#endif
5787#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
5788 || defined(FEAT_GUI_W32) \
5789 || defined(FEAT_GUI_MOTIF))
5790 "browsefilter",
5791#endif
5792#ifdef FEAT_BYTEOFF
5793 "byte_offset",
5794#endif
5795#ifdef FEAT_JOB_CHANNEL
5796 "channel",
5797#endif
5798#ifdef FEAT_CINDENT
5799 "cindent",
5800#endif
5801#ifdef FEAT_CLIENTSERVER
5802 "clientserver",
5803#endif
5804#ifdef FEAT_CLIPBOARD
5805 "clipboard",
5806#endif
5807#ifdef FEAT_CMDL_COMPL
5808 "cmdline_compl",
5809#endif
5810#ifdef FEAT_CMDHIST
5811 "cmdline_hist",
5812#endif
5813#ifdef FEAT_COMMENTS
5814 "comments",
5815#endif
5816#ifdef FEAT_CONCEAL
5817 "conceal",
5818#endif
5819#ifdef FEAT_CRYPT
5820 "cryptv",
5821 "crypt-blowfish",
5822 "crypt-blowfish2",
5823#endif
5824#ifdef FEAT_CSCOPE
5825 "cscope",
5826#endif
5827#ifdef FEAT_CURSORBIND
5828 "cursorbind",
5829#endif
5830#ifdef CURSOR_SHAPE
5831 "cursorshape",
5832#endif
5833#ifdef DEBUG
5834 "debug",
5835#endif
5836#ifdef FEAT_CON_DIALOG
5837 "dialog_con",
5838#endif
5839#ifdef FEAT_GUI_DIALOG
5840 "dialog_gui",
5841#endif
5842#ifdef FEAT_DIFF
5843 "diff",
5844#endif
5845#ifdef FEAT_DIGRAPHS
5846 "digraphs",
5847#endif
5848#ifdef FEAT_DIRECTX
5849 "directx",
5850#endif
5851#ifdef FEAT_DND
5852 "dnd",
5853#endif
5854#ifdef FEAT_EMACS_TAGS
5855 "emacs_tags",
5856#endif
5857 "eval", /* always present, of course! */
5858 "ex_extra", /* graduated feature */
5859#ifdef FEAT_SEARCH_EXTRA
5860 "extra_search",
5861#endif
5862#ifdef FEAT_FKMAP
5863 "farsi",
5864#endif
5865#ifdef FEAT_SEARCHPATH
5866 "file_in_path",
5867#endif
5868#ifdef FEAT_FILTERPIPE
5869 "filterpipe",
5870#endif
5871#ifdef FEAT_FIND_ID
5872 "find_in_path",
5873#endif
5874#ifdef FEAT_FLOAT
5875 "float",
5876#endif
5877#ifdef FEAT_FOLDING
5878 "folding",
5879#endif
5880#ifdef FEAT_FOOTER
5881 "footer",
5882#endif
5883#if !defined(USE_SYSTEM) && defined(UNIX)
5884 "fork",
5885#endif
5886#ifdef FEAT_GETTEXT
5887 "gettext",
5888#endif
5889#ifdef FEAT_GUI
5890 "gui",
5891#endif
5892#ifdef FEAT_GUI_ATHENA
5893# ifdef FEAT_GUI_NEXTAW
5894 "gui_neXtaw",
5895# else
5896 "gui_athena",
5897# endif
5898#endif
5899#ifdef FEAT_GUI_GTK
5900 "gui_gtk",
5901# ifdef USE_GTK3
5902 "gui_gtk3",
5903# else
5904 "gui_gtk2",
5905# endif
5906#endif
5907#ifdef FEAT_GUI_GNOME
5908 "gui_gnome",
5909#endif
5910#ifdef FEAT_GUI_MAC
5911 "gui_mac",
5912#endif
5913#ifdef FEAT_GUI_MOTIF
5914 "gui_motif",
5915#endif
5916#ifdef FEAT_GUI_PHOTON
5917 "gui_photon",
5918#endif
5919#ifdef FEAT_GUI_W32
5920 "gui_win32",
5921#endif
5922#ifdef FEAT_HANGULIN
5923 "hangul_input",
5924#endif
5925#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
5926 "iconv",
5927#endif
5928#ifdef FEAT_INS_EXPAND
5929 "insert_expand",
5930#endif
5931#ifdef FEAT_JOB_CHANNEL
5932 "job",
5933#endif
5934#ifdef FEAT_JUMPLIST
5935 "jumplist",
5936#endif
5937#ifdef FEAT_KEYMAP
5938 "keymap",
5939#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02005940 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005941#ifdef FEAT_LANGMAP
5942 "langmap",
5943#endif
5944#ifdef FEAT_LIBCALL
5945 "libcall",
5946#endif
5947#ifdef FEAT_LINEBREAK
5948 "linebreak",
5949#endif
5950#ifdef FEAT_LISP
5951 "lispindent",
5952#endif
5953#ifdef FEAT_LISTCMDS
5954 "listcmds",
5955#endif
5956#ifdef FEAT_LOCALMAP
5957 "localmap",
5958#endif
5959#ifdef FEAT_LUA
5960# ifndef DYNAMIC_LUA
5961 "lua",
5962# endif
5963#endif
5964#ifdef FEAT_MENU
5965 "menu",
5966#endif
5967#ifdef FEAT_SESSION
5968 "mksession",
5969#endif
5970#ifdef FEAT_MODIFY_FNAME
5971 "modify_fname",
5972#endif
5973#ifdef FEAT_MOUSE
5974 "mouse",
5975#endif
5976#ifdef FEAT_MOUSESHAPE
5977 "mouseshape",
5978#endif
5979#if defined(UNIX) || defined(VMS)
5980# ifdef FEAT_MOUSE_DEC
5981 "mouse_dec",
5982# endif
5983# ifdef FEAT_MOUSE_GPM
5984 "mouse_gpm",
5985# endif
5986# ifdef FEAT_MOUSE_JSB
5987 "mouse_jsbterm",
5988# endif
5989# ifdef FEAT_MOUSE_NET
5990 "mouse_netterm",
5991# endif
5992# ifdef FEAT_MOUSE_PTERM
5993 "mouse_pterm",
5994# endif
5995# ifdef FEAT_MOUSE_SGR
5996 "mouse_sgr",
5997# endif
5998# ifdef FEAT_SYSMOUSE
5999 "mouse_sysmouse",
6000# endif
6001# ifdef FEAT_MOUSE_URXVT
6002 "mouse_urxvt",
6003# endif
6004# ifdef FEAT_MOUSE_XTERM
6005 "mouse_xterm",
6006# endif
6007#endif
6008#ifdef FEAT_MBYTE
6009 "multi_byte",
6010#endif
6011#ifdef FEAT_MBYTE_IME
6012 "multi_byte_ime",
6013#endif
6014#ifdef FEAT_MULTI_LANG
6015 "multi_lang",
6016#endif
6017#ifdef FEAT_MZSCHEME
6018#ifndef DYNAMIC_MZSCHEME
6019 "mzscheme",
6020#endif
6021#endif
6022#ifdef FEAT_NUM64
6023 "num64",
6024#endif
6025#ifdef FEAT_OLE
6026 "ole",
6027#endif
6028 "packages",
6029#ifdef FEAT_PATH_EXTRA
6030 "path_extra",
6031#endif
6032#ifdef FEAT_PERL
6033#ifndef DYNAMIC_PERL
6034 "perl",
6035#endif
6036#endif
6037#ifdef FEAT_PERSISTENT_UNDO
6038 "persistent_undo",
6039#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006040#if defined(FEAT_PYTHON)
6041 "python_compiled",
6042# if defined(DYNAMIC_PYTHON)
6043 "python_dynamic",
6044# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006045 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006046 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006047# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006048#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006049#if defined(FEAT_PYTHON3)
6050 "python3_compiled",
6051# if defined(DYNAMIC_PYTHON3)
6052 "python3_dynamic",
6053# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006054 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006055 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006056# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006057#endif
6058#ifdef FEAT_POSTSCRIPT
6059 "postscript",
6060#endif
6061#ifdef FEAT_PRINTER
6062 "printer",
6063#endif
6064#ifdef FEAT_PROFILE
6065 "profile",
6066#endif
6067#ifdef FEAT_RELTIME
6068 "reltime",
6069#endif
6070#ifdef FEAT_QUICKFIX
6071 "quickfix",
6072#endif
6073#ifdef FEAT_RIGHTLEFT
6074 "rightleft",
6075#endif
6076#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6077 "ruby",
6078#endif
6079#ifdef FEAT_SCROLLBIND
6080 "scrollbind",
6081#endif
6082#ifdef FEAT_CMDL_INFO
6083 "showcmd",
6084 "cmdline_info",
6085#endif
6086#ifdef FEAT_SIGNS
6087 "signs",
6088#endif
6089#ifdef FEAT_SMARTINDENT
6090 "smartindent",
6091#endif
6092#ifdef STARTUPTIME
6093 "startuptime",
6094#endif
6095#ifdef FEAT_STL_OPT
6096 "statusline",
6097#endif
6098#ifdef FEAT_SUN_WORKSHOP
6099 "sun_workshop",
6100#endif
6101#ifdef FEAT_NETBEANS_INTG
6102 "netbeans_intg",
6103#endif
6104#ifdef FEAT_SPELL
6105 "spell",
6106#endif
6107#ifdef FEAT_SYN_HL
6108 "syntax",
6109#endif
6110#if defined(USE_SYSTEM) || !defined(UNIX)
6111 "system",
6112#endif
6113#ifdef FEAT_TAG_BINS
6114 "tag_binary",
6115#endif
6116#ifdef FEAT_TAG_OLDSTATIC
6117 "tag_old_static",
6118#endif
6119#ifdef FEAT_TAG_ANYWHITE
6120 "tag_any_white",
6121#endif
6122#ifdef FEAT_TCL
6123# ifndef DYNAMIC_TCL
6124 "tcl",
6125# endif
6126#endif
6127#ifdef FEAT_TERMGUICOLORS
6128 "termguicolors",
6129#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006130#if defined(FEAT_TERMINAL) && !defined(WIN3264)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006131 "terminal",
6132#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006133#ifdef TERMINFO
6134 "terminfo",
6135#endif
6136#ifdef FEAT_TERMRESPONSE
6137 "termresponse",
6138#endif
6139#ifdef FEAT_TEXTOBJ
6140 "textobjects",
6141#endif
6142#ifdef HAVE_TGETENT
6143 "tgetent",
6144#endif
6145#ifdef FEAT_TIMERS
6146 "timers",
6147#endif
6148#ifdef FEAT_TITLE
6149 "title",
6150#endif
6151#ifdef FEAT_TOOLBAR
6152 "toolbar",
6153#endif
6154#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6155 "unnamedplus",
6156#endif
6157#ifdef FEAT_USR_CMDS
6158 "user-commands", /* was accidentally included in 5.4 */
6159 "user_commands",
6160#endif
6161#ifdef FEAT_VIMINFO
6162 "viminfo",
6163#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006164 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006165#ifdef FEAT_VIRTUALEDIT
6166 "virtualedit",
6167#endif
6168 "visual",
6169#ifdef FEAT_VISUALEXTRA
6170 "visualextra",
6171#endif
6172#ifdef FEAT_VREPLACE
6173 "vreplace",
6174#endif
6175#ifdef FEAT_WILDIGN
6176 "wildignore",
6177#endif
6178#ifdef FEAT_WILDMENU
6179 "wildmenu",
6180#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006181 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006182#ifdef FEAT_WAK
6183 "winaltkeys",
6184#endif
6185#ifdef FEAT_WRITEBACKUP
6186 "writebackup",
6187#endif
6188#ifdef FEAT_XIM
6189 "xim",
6190#endif
6191#ifdef FEAT_XFONTSET
6192 "xfontset",
6193#endif
6194#ifdef FEAT_XPM_W32
6195 "xpm",
6196 "xpm_w32", /* for backward compatibility */
6197#else
6198# if defined(HAVE_XPM)
6199 "xpm",
6200# endif
6201#endif
6202#ifdef USE_XSMP
6203 "xsmp",
6204#endif
6205#ifdef USE_XSMP_INTERACT
6206 "xsmp_interact",
6207#endif
6208#ifdef FEAT_XCLIPBOARD
6209 "xterm_clipboard",
6210#endif
6211#ifdef FEAT_XTERM_SAVE
6212 "xterm_save",
6213#endif
6214#if defined(UNIX) && defined(FEAT_X11)
6215 "X11",
6216#endif
6217 NULL
6218 };
6219
6220 name = get_tv_string(&argvars[0]);
6221 for (i = 0; has_list[i] != NULL; ++i)
6222 if (STRICMP(name, has_list[i]) == 0)
6223 {
6224 n = TRUE;
6225 break;
6226 }
6227
6228 if (n == FALSE)
6229 {
6230 if (STRNICMP(name, "patch", 5) == 0)
6231 {
6232 if (name[5] == '-'
6233 && STRLEN(name) >= 11
6234 && vim_isdigit(name[6])
6235 && vim_isdigit(name[8])
6236 && vim_isdigit(name[10]))
6237 {
6238 int major = atoi((char *)name + 6);
6239 int minor = atoi((char *)name + 8);
6240
6241 /* Expect "patch-9.9.01234". */
6242 n = (major < VIM_VERSION_MAJOR
6243 || (major == VIM_VERSION_MAJOR
6244 && (minor < VIM_VERSION_MINOR
6245 || (minor == VIM_VERSION_MINOR
6246 && has_patch(atoi((char *)name + 10))))));
6247 }
6248 else
6249 n = has_patch(atoi((char *)name + 5));
6250 }
6251 else if (STRICMP(name, "vim_starting") == 0)
6252 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006253 else if (STRICMP(name, "ttyin") == 0)
6254 n = mch_input_isatty();
6255 else if (STRICMP(name, "ttyout") == 0)
6256 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006257#ifdef FEAT_MBYTE
6258 else if (STRICMP(name, "multi_byte_encoding") == 0)
6259 n = has_mbyte;
6260#endif
6261#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
6262 else if (STRICMP(name, "balloon_multiline") == 0)
6263 n = multiline_balloon_available();
6264#endif
6265#ifdef DYNAMIC_TCL
6266 else if (STRICMP(name, "tcl") == 0)
6267 n = tcl_enabled(FALSE);
6268#endif
6269#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6270 else if (STRICMP(name, "iconv") == 0)
6271 n = iconv_enabled(FALSE);
6272#endif
6273#ifdef DYNAMIC_LUA
6274 else if (STRICMP(name, "lua") == 0)
6275 n = lua_enabled(FALSE);
6276#endif
6277#ifdef DYNAMIC_MZSCHEME
6278 else if (STRICMP(name, "mzscheme") == 0)
6279 n = mzscheme_enabled(FALSE);
6280#endif
6281#ifdef DYNAMIC_RUBY
6282 else if (STRICMP(name, "ruby") == 0)
6283 n = ruby_enabled(FALSE);
6284#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006285#ifdef DYNAMIC_PYTHON
6286 else if (STRICMP(name, "python") == 0)
6287 n = python_enabled(FALSE);
6288#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006289#ifdef DYNAMIC_PYTHON3
6290 else if (STRICMP(name, "python3") == 0)
6291 n = python3_enabled(FALSE);
6292#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006293#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6294 else if (STRICMP(name, "pythonx") == 0)
6295 {
6296# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6297 if (p_pyx == 0)
6298 n = python3_enabled(FALSE) || python_enabled(FALSE);
6299 else if (p_pyx == 3)
6300 n = python3_enabled(FALSE);
6301 else if (p_pyx == 2)
6302 n = python_enabled(FALSE);
6303# elif defined(DYNAMIC_PYTHON)
6304 n = python_enabled(FALSE);
6305# elif defined(DYNAMIC_PYTHON3)
6306 n = python3_enabled(FALSE);
6307# endif
6308 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006309#endif
6310#ifdef DYNAMIC_PERL
6311 else if (STRICMP(name, "perl") == 0)
6312 n = perl_enabled(FALSE);
6313#endif
6314#ifdef FEAT_GUI
6315 else if (STRICMP(name, "gui_running") == 0)
6316 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006317# ifdef FEAT_BROWSE
6318 else if (STRICMP(name, "browse") == 0)
6319 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6320# endif
6321#endif
6322#ifdef FEAT_SYN_HL
6323 else if (STRICMP(name, "syntax_items") == 0)
6324 n = syntax_present(curwin);
6325#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006326#ifdef FEAT_VTP
6327 else if (STRICMP(name, "vcon") == 0)
6328 n = has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006329#endif
6330#ifdef FEAT_NETBEANS_INTG
6331 else if (STRICMP(name, "netbeans_enabled") == 0)
6332 n = netbeans_active();
6333#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006334#if defined(FEAT_TERMINAL) && defined(WIN3264)
6335 else if (STRICMP(name, "terminal") == 0)
6336 n = terminal_enabled();
6337#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006338 }
6339
6340 rettv->vval.v_number = n;
6341}
6342
6343/*
6344 * "has_key()" function
6345 */
6346 static void
6347f_has_key(typval_T *argvars, typval_T *rettv)
6348{
6349 if (argvars[0].v_type != VAR_DICT)
6350 {
6351 EMSG(_(e_dictreq));
6352 return;
6353 }
6354 if (argvars[0].vval.v_dict == NULL)
6355 return;
6356
6357 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
6358 get_tv_string(&argvars[1]), -1) != NULL;
6359}
6360
6361/*
6362 * "haslocaldir()" function
6363 */
6364 static void
6365f_haslocaldir(typval_T *argvars, typval_T *rettv)
6366{
6367 win_T *wp = NULL;
6368
6369 wp = find_tabwin(&argvars[0], &argvars[1]);
6370 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6371}
6372
6373/*
6374 * "hasmapto()" function
6375 */
6376 static void
6377f_hasmapto(typval_T *argvars, typval_T *rettv)
6378{
6379 char_u *name;
6380 char_u *mode;
6381 char_u buf[NUMBUFLEN];
6382 int abbr = FALSE;
6383
6384 name = get_tv_string(&argvars[0]);
6385 if (argvars[1].v_type == VAR_UNKNOWN)
6386 mode = (char_u *)"nvo";
6387 else
6388 {
6389 mode = get_tv_string_buf(&argvars[1], buf);
6390 if (argvars[2].v_type != VAR_UNKNOWN)
6391 abbr = (int)get_tv_number(&argvars[2]);
6392 }
6393
6394 if (map_to_exists(name, mode, abbr))
6395 rettv->vval.v_number = TRUE;
6396 else
6397 rettv->vval.v_number = FALSE;
6398}
6399
6400/*
6401 * "histadd()" function
6402 */
6403 static void
6404f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6405{
6406#ifdef FEAT_CMDHIST
6407 int histype;
6408 char_u *str;
6409 char_u buf[NUMBUFLEN];
6410#endif
6411
6412 rettv->vval.v_number = FALSE;
6413 if (check_restricted() || check_secure())
6414 return;
6415#ifdef FEAT_CMDHIST
6416 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6417 histype = str != NULL ? get_histtype(str) : -1;
6418 if (histype >= 0)
6419 {
6420 str = get_tv_string_buf(&argvars[1], buf);
6421 if (*str != NUL)
6422 {
6423 init_history();
6424 add_to_history(histype, str, FALSE, NUL);
6425 rettv->vval.v_number = TRUE;
6426 return;
6427 }
6428 }
6429#endif
6430}
6431
6432/*
6433 * "histdel()" function
6434 */
6435 static void
6436f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6437{
6438#ifdef FEAT_CMDHIST
6439 int n;
6440 char_u buf[NUMBUFLEN];
6441 char_u *str;
6442
6443 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6444 if (str == NULL)
6445 n = 0;
6446 else if (argvars[1].v_type == VAR_UNKNOWN)
6447 /* only one argument: clear entire history */
6448 n = clr_history(get_histtype(str));
6449 else if (argvars[1].v_type == VAR_NUMBER)
6450 /* index given: remove that entry */
6451 n = del_history_idx(get_histtype(str),
6452 (int)get_tv_number(&argvars[1]));
6453 else
6454 /* string given: remove all matching entries */
6455 n = del_history_entry(get_histtype(str),
6456 get_tv_string_buf(&argvars[1], buf));
6457 rettv->vval.v_number = n;
6458#endif
6459}
6460
6461/*
6462 * "histget()" function
6463 */
6464 static void
6465f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6466{
6467#ifdef FEAT_CMDHIST
6468 int type;
6469 int idx;
6470 char_u *str;
6471
6472 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6473 if (str == NULL)
6474 rettv->vval.v_string = NULL;
6475 else
6476 {
6477 type = get_histtype(str);
6478 if (argvars[1].v_type == VAR_UNKNOWN)
6479 idx = get_history_idx(type);
6480 else
6481 idx = (int)get_tv_number_chk(&argvars[1], NULL);
6482 /* -1 on type error */
6483 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6484 }
6485#else
6486 rettv->vval.v_string = NULL;
6487#endif
6488 rettv->v_type = VAR_STRING;
6489}
6490
6491/*
6492 * "histnr()" function
6493 */
6494 static void
6495f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6496{
6497 int i;
6498
6499#ifdef FEAT_CMDHIST
6500 char_u *history = get_tv_string_chk(&argvars[0]);
6501
6502 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6503 if (i >= HIST_CMD && i < HIST_COUNT)
6504 i = get_history_idx(i);
6505 else
6506#endif
6507 i = -1;
6508 rettv->vval.v_number = i;
6509}
6510
6511/*
6512 * "highlightID(name)" function
6513 */
6514 static void
6515f_hlID(typval_T *argvars, typval_T *rettv)
6516{
6517 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
6518}
6519
6520/*
6521 * "highlight_exists()" function
6522 */
6523 static void
6524f_hlexists(typval_T *argvars, typval_T *rettv)
6525{
6526 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
6527}
6528
6529/*
6530 * "hostname()" function
6531 */
6532 static void
6533f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6534{
6535 char_u hostname[256];
6536
6537 mch_get_host_name(hostname, 256);
6538 rettv->v_type = VAR_STRING;
6539 rettv->vval.v_string = vim_strsave(hostname);
6540}
6541
6542/*
6543 * iconv() function
6544 */
6545 static void
6546f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6547{
6548#ifdef FEAT_MBYTE
6549 char_u buf1[NUMBUFLEN];
6550 char_u buf2[NUMBUFLEN];
6551 char_u *from, *to, *str;
6552 vimconv_T vimconv;
6553#endif
6554
6555 rettv->v_type = VAR_STRING;
6556 rettv->vval.v_string = NULL;
6557
6558#ifdef FEAT_MBYTE
6559 str = get_tv_string(&argvars[0]);
6560 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
6561 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
6562 vimconv.vc_type = CONV_NONE;
6563 convert_setup(&vimconv, from, to);
6564
6565 /* If the encodings are equal, no conversion needed. */
6566 if (vimconv.vc_type == CONV_NONE)
6567 rettv->vval.v_string = vim_strsave(str);
6568 else
6569 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6570
6571 convert_setup(&vimconv, NULL, NULL);
6572 vim_free(from);
6573 vim_free(to);
6574#endif
6575}
6576
6577/*
6578 * "indent()" function
6579 */
6580 static void
6581f_indent(typval_T *argvars, typval_T *rettv)
6582{
6583 linenr_T lnum;
6584
6585 lnum = get_tv_lnum(argvars);
6586 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6587 rettv->vval.v_number = get_indent_lnum(lnum);
6588 else
6589 rettv->vval.v_number = -1;
6590}
6591
6592/*
6593 * "index()" function
6594 */
6595 static void
6596f_index(typval_T *argvars, typval_T *rettv)
6597{
6598 list_T *l;
6599 listitem_T *item;
6600 long idx = 0;
6601 int ic = FALSE;
6602
6603 rettv->vval.v_number = -1;
6604 if (argvars[0].v_type != VAR_LIST)
6605 {
6606 EMSG(_(e_listreq));
6607 return;
6608 }
6609 l = argvars[0].vval.v_list;
6610 if (l != NULL)
6611 {
6612 item = l->lv_first;
6613 if (argvars[2].v_type != VAR_UNKNOWN)
6614 {
6615 int error = FALSE;
6616
6617 /* Start at specified item. Use the cached index that list_find()
6618 * sets, so that a negative number also works. */
6619 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
6620 idx = l->lv_idx;
6621 if (argvars[3].v_type != VAR_UNKNOWN)
6622 ic = (int)get_tv_number_chk(&argvars[3], &error);
6623 if (error)
6624 item = NULL;
6625 }
6626
6627 for ( ; item != NULL; item = item->li_next, ++idx)
6628 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6629 {
6630 rettv->vval.v_number = idx;
6631 break;
6632 }
6633 }
6634}
6635
6636static int inputsecret_flag = 0;
6637
6638/*
6639 * "input()" function
6640 * Also handles inputsecret() when inputsecret is set.
6641 */
6642 static void
6643f_input(typval_T *argvars, typval_T *rettv)
6644{
6645 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6646}
6647
6648/*
6649 * "inputdialog()" function
6650 */
6651 static void
6652f_inputdialog(typval_T *argvars, typval_T *rettv)
6653{
6654#if defined(FEAT_GUI_TEXTDIALOG)
6655 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
6656 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6657 {
6658 char_u *message;
6659 char_u buf[NUMBUFLEN];
6660 char_u *defstr = (char_u *)"";
6661
6662 message = get_tv_string_chk(&argvars[0]);
6663 if (argvars[1].v_type != VAR_UNKNOWN
6664 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
6665 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6666 else
6667 IObuff[0] = NUL;
6668 if (message != NULL && defstr != NULL
6669 && do_dialog(VIM_QUESTION, NULL, message,
6670 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6671 rettv->vval.v_string = vim_strsave(IObuff);
6672 else
6673 {
6674 if (message != NULL && defstr != NULL
6675 && argvars[1].v_type != VAR_UNKNOWN
6676 && argvars[2].v_type != VAR_UNKNOWN)
6677 rettv->vval.v_string = vim_strsave(
6678 get_tv_string_buf(&argvars[2], buf));
6679 else
6680 rettv->vval.v_string = NULL;
6681 }
6682 rettv->v_type = VAR_STRING;
6683 }
6684 else
6685#endif
6686 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6687}
6688
6689/*
6690 * "inputlist()" function
6691 */
6692 static void
6693f_inputlist(typval_T *argvars, typval_T *rettv)
6694{
6695 listitem_T *li;
6696 int selected;
6697 int mouse_used;
6698
6699#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02006700 /* While starting up, there is no place to enter text. When running tests
6701 * with --not-a-term we assume feedkeys() will be used. */
6702 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006703 return;
6704#endif
6705 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6706 {
6707 EMSG2(_(e_listarg), "inputlist()");
6708 return;
6709 }
6710
6711 msg_start();
6712 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
6713 lines_left = Rows; /* avoid more prompt */
6714 msg_scroll = TRUE;
6715 msg_clr_eos();
6716
6717 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
6718 {
6719 msg_puts(get_tv_string(&li->li_tv));
6720 msg_putchar('\n');
6721 }
6722
6723 /* Ask for choice. */
6724 selected = prompt_for_number(&mouse_used);
6725 if (mouse_used)
6726 selected -= lines_left;
6727
6728 rettv->vval.v_number = selected;
6729}
6730
6731
6732static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6733
6734/*
6735 * "inputrestore()" function
6736 */
6737 static void
6738f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6739{
6740 if (ga_userinput.ga_len > 0)
6741 {
6742 --ga_userinput.ga_len;
6743 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
6744 + ga_userinput.ga_len);
6745 /* default return is zero == OK */
6746 }
6747 else if (p_verbose > 1)
6748 {
6749 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
6750 rettv->vval.v_number = 1; /* Failed */
6751 }
6752}
6753
6754/*
6755 * "inputsave()" function
6756 */
6757 static void
6758f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
6759{
6760 /* Add an entry to the stack of typeahead storage. */
6761 if (ga_grow(&ga_userinput, 1) == OK)
6762 {
6763 save_typeahead((tasave_T *)(ga_userinput.ga_data)
6764 + ga_userinput.ga_len);
6765 ++ga_userinput.ga_len;
6766 /* default return is zero == OK */
6767 }
6768 else
6769 rettv->vval.v_number = 1; /* Failed */
6770}
6771
6772/*
6773 * "inputsecret()" function
6774 */
6775 static void
6776f_inputsecret(typval_T *argvars, typval_T *rettv)
6777{
6778 ++cmdline_star;
6779 ++inputsecret_flag;
6780 f_input(argvars, rettv);
6781 --cmdline_star;
6782 --inputsecret_flag;
6783}
6784
6785/*
6786 * "insert()" function
6787 */
6788 static void
6789f_insert(typval_T *argvars, typval_T *rettv)
6790{
6791 long before = 0;
6792 listitem_T *item;
6793 list_T *l;
6794 int error = FALSE;
6795
6796 if (argvars[0].v_type != VAR_LIST)
6797 EMSG2(_(e_listarg), "insert()");
6798 else if ((l = argvars[0].vval.v_list) != NULL
6799 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
6800 {
6801 if (argvars[2].v_type != VAR_UNKNOWN)
6802 before = (long)get_tv_number_chk(&argvars[2], &error);
6803 if (error)
6804 return; /* type error; errmsg already given */
6805
6806 if (before == l->lv_len)
6807 item = NULL;
6808 else
6809 {
6810 item = list_find(l, before);
6811 if (item == NULL)
6812 {
6813 EMSGN(_(e_listidx), before);
6814 l = NULL;
6815 }
6816 }
6817 if (l != NULL)
6818 {
6819 list_insert_tv(l, &argvars[1], item);
6820 copy_tv(&argvars[0], rettv);
6821 }
6822 }
6823}
6824
6825/*
6826 * "invert(expr)" function
6827 */
6828 static void
6829f_invert(typval_T *argvars, typval_T *rettv)
6830{
6831 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
6832}
6833
6834/*
6835 * "isdirectory()" function
6836 */
6837 static void
6838f_isdirectory(typval_T *argvars, typval_T *rettv)
6839{
6840 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
6841}
6842
6843/*
6844 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
6845 * or it refers to a List or Dictionary that is locked.
6846 */
6847 static int
6848tv_islocked(typval_T *tv)
6849{
6850 return (tv->v_lock & VAR_LOCKED)
6851 || (tv->v_type == VAR_LIST
6852 && tv->vval.v_list != NULL
6853 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
6854 || (tv->v_type == VAR_DICT
6855 && tv->vval.v_dict != NULL
6856 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
6857}
6858
6859/*
6860 * "islocked()" function
6861 */
6862 static void
6863f_islocked(typval_T *argvars, typval_T *rettv)
6864{
6865 lval_T lv;
6866 char_u *end;
6867 dictitem_T *di;
6868
6869 rettv->vval.v_number = -1;
6870 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01006871 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006872 if (end != NULL && lv.ll_name != NULL)
6873 {
6874 if (*end != NUL)
6875 EMSG(_(e_trailing));
6876 else
6877 {
6878 if (lv.ll_tv == NULL)
6879 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006880 di = find_var(lv.ll_name, NULL, TRUE);
6881 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006882 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006883 /* Consider a variable locked when:
6884 * 1. the variable itself is locked
6885 * 2. the value of the variable is locked.
6886 * 3. the List or Dict value is locked.
6887 */
6888 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
6889 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006890 }
6891 }
6892 else if (lv.ll_range)
6893 EMSG(_("E786: Range not allowed"));
6894 else if (lv.ll_newkey != NULL)
6895 EMSG2(_(e_dictkey), lv.ll_newkey);
6896 else if (lv.ll_list != NULL)
6897 /* List item. */
6898 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
6899 else
6900 /* Dictionary item. */
6901 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
6902 }
6903 }
6904
6905 clear_lval(&lv);
6906}
6907
6908#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
6909/*
6910 * "isnan()" function
6911 */
6912 static void
6913f_isnan(typval_T *argvars, typval_T *rettv)
6914{
6915 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
6916 && isnan(argvars[0].vval.v_float);
6917}
6918#endif
6919
6920/*
6921 * "items(dict)" function
6922 */
6923 static void
6924f_items(typval_T *argvars, typval_T *rettv)
6925{
6926 dict_list(argvars, rettv, 2);
6927}
6928
6929#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
6930/*
6931 * Get the job from the argument.
6932 * Returns NULL if the job is invalid.
6933 */
6934 static job_T *
6935get_job_arg(typval_T *tv)
6936{
6937 job_T *job;
6938
6939 if (tv->v_type != VAR_JOB)
6940 {
6941 EMSG2(_(e_invarg2), get_tv_string(tv));
6942 return NULL;
6943 }
6944 job = tv->vval.v_job;
6945
6946 if (job == NULL)
6947 EMSG(_("E916: not a valid job"));
6948 return job;
6949}
6950
6951/*
6952 * "job_getchannel()" function
6953 */
6954 static void
6955f_job_getchannel(typval_T *argvars, typval_T *rettv)
6956{
6957 job_T *job = get_job_arg(&argvars[0]);
6958
6959 if (job != NULL)
6960 {
6961 rettv->v_type = VAR_CHANNEL;
6962 rettv->vval.v_channel = job->jv_channel;
6963 if (job->jv_channel != NULL)
6964 ++job->jv_channel->ch_refcount;
6965 }
6966}
6967
6968/*
6969 * "job_info()" function
6970 */
6971 static void
6972f_job_info(typval_T *argvars, typval_T *rettv)
6973{
6974 job_T *job = get_job_arg(&argvars[0]);
6975
6976 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
6977 job_info(job, rettv->vval.v_dict);
6978}
6979
6980/*
6981 * "job_setoptions()" function
6982 */
6983 static void
6984f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
6985{
6986 job_T *job = get_job_arg(&argvars[0]);
6987 jobopt_T opt;
6988
6989 if (job == NULL)
6990 return;
6991 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02006992 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006993 job_set_options(job, &opt);
6994 free_job_options(&opt);
6995}
6996
6997/*
6998 * "job_start()" function
6999 */
7000 static void
7001f_job_start(typval_T *argvars, typval_T *rettv)
7002{
7003 rettv->v_type = VAR_JOB;
7004 if (check_restricted() || check_secure())
7005 return;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02007006 rettv->vval.v_job = job_start(argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007007}
7008
7009/*
7010 * "job_status()" function
7011 */
7012 static void
7013f_job_status(typval_T *argvars, typval_T *rettv)
7014{
7015 job_T *job = get_job_arg(&argvars[0]);
7016
7017 if (job != NULL)
7018 {
7019 rettv->v_type = VAR_STRING;
7020 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7021 }
7022}
7023
7024/*
7025 * "job_stop()" function
7026 */
7027 static void
7028f_job_stop(typval_T *argvars, typval_T *rettv)
7029{
7030 job_T *job = get_job_arg(&argvars[0]);
7031
7032 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007033 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007034}
7035#endif
7036
7037/*
7038 * "join()" function
7039 */
7040 static void
7041f_join(typval_T *argvars, typval_T *rettv)
7042{
7043 garray_T ga;
7044 char_u *sep;
7045
7046 if (argvars[0].v_type != VAR_LIST)
7047 {
7048 EMSG(_(e_listreq));
7049 return;
7050 }
7051 if (argvars[0].vval.v_list == NULL)
7052 return;
7053 if (argvars[1].v_type == VAR_UNKNOWN)
7054 sep = (char_u *)" ";
7055 else
7056 sep = get_tv_string_chk(&argvars[1]);
7057
7058 rettv->v_type = VAR_STRING;
7059
7060 if (sep != NULL)
7061 {
7062 ga_init2(&ga, (int)sizeof(char), 80);
7063 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7064 ga_append(&ga, NUL);
7065 rettv->vval.v_string = (char_u *)ga.ga_data;
7066 }
7067 else
7068 rettv->vval.v_string = NULL;
7069}
7070
7071/*
7072 * "js_decode()" function
7073 */
7074 static void
7075f_js_decode(typval_T *argvars, typval_T *rettv)
7076{
7077 js_read_T reader;
7078
7079 reader.js_buf = get_tv_string(&argvars[0]);
7080 reader.js_fill = NULL;
7081 reader.js_used = 0;
7082 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
7083 EMSG(_(e_invarg));
7084}
7085
7086/*
7087 * "js_encode()" function
7088 */
7089 static void
7090f_js_encode(typval_T *argvars, typval_T *rettv)
7091{
7092 rettv->v_type = VAR_STRING;
7093 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7094}
7095
7096/*
7097 * "json_decode()" function
7098 */
7099 static void
7100f_json_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;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007107 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007108}
7109
7110/*
7111 * "json_encode()" function
7112 */
7113 static void
7114f_json_encode(typval_T *argvars, typval_T *rettv)
7115{
7116 rettv->v_type = VAR_STRING;
7117 rettv->vval.v_string = json_encode(&argvars[0], 0);
7118}
7119
7120/*
7121 * "keys()" function
7122 */
7123 static void
7124f_keys(typval_T *argvars, typval_T *rettv)
7125{
7126 dict_list(argvars, rettv, 0);
7127}
7128
7129/*
7130 * "last_buffer_nr()" function.
7131 */
7132 static void
7133f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7134{
7135 int n = 0;
7136 buf_T *buf;
7137
Bram Moolenaar29323592016-07-24 22:04:11 +02007138 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007139 if (n < buf->b_fnum)
7140 n = buf->b_fnum;
7141
7142 rettv->vval.v_number = n;
7143}
7144
7145/*
7146 * "len()" function
7147 */
7148 static void
7149f_len(typval_T *argvars, typval_T *rettv)
7150{
7151 switch (argvars[0].v_type)
7152 {
7153 case VAR_STRING:
7154 case VAR_NUMBER:
7155 rettv->vval.v_number = (varnumber_T)STRLEN(
7156 get_tv_string(&argvars[0]));
7157 break;
7158 case VAR_LIST:
7159 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7160 break;
7161 case VAR_DICT:
7162 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7163 break;
7164 case VAR_UNKNOWN:
7165 case VAR_SPECIAL:
7166 case VAR_FLOAT:
7167 case VAR_FUNC:
7168 case VAR_PARTIAL:
7169 case VAR_JOB:
7170 case VAR_CHANNEL:
7171 EMSG(_("E701: Invalid type for len()"));
7172 break;
7173 }
7174}
7175
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007176 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007177libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007178{
7179#ifdef FEAT_LIBCALL
7180 char_u *string_in;
7181 char_u **string_result;
7182 int nr_result;
7183#endif
7184
7185 rettv->v_type = type;
7186 if (type != VAR_NUMBER)
7187 rettv->vval.v_string = NULL;
7188
7189 if (check_restricted() || check_secure())
7190 return;
7191
7192#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007193 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007194 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7195 {
7196 string_in = NULL;
7197 if (argvars[2].v_type == VAR_STRING)
7198 string_in = argvars[2].vval.v_string;
7199 if (type == VAR_NUMBER)
7200 string_result = NULL;
7201 else
7202 string_result = &rettv->vval.v_string;
7203 if (mch_libcall(argvars[0].vval.v_string,
7204 argvars[1].vval.v_string,
7205 string_in,
7206 argvars[2].vval.v_number,
7207 string_result,
7208 &nr_result) == OK
7209 && type == VAR_NUMBER)
7210 rettv->vval.v_number = nr_result;
7211 }
7212#endif
7213}
7214
7215/*
7216 * "libcall()" function
7217 */
7218 static void
7219f_libcall(typval_T *argvars, typval_T *rettv)
7220{
7221 libcall_common(argvars, rettv, VAR_STRING);
7222}
7223
7224/*
7225 * "libcallnr()" function
7226 */
7227 static void
7228f_libcallnr(typval_T *argvars, typval_T *rettv)
7229{
7230 libcall_common(argvars, rettv, VAR_NUMBER);
7231}
7232
7233/*
7234 * "line(string)" function
7235 */
7236 static void
7237f_line(typval_T *argvars, typval_T *rettv)
7238{
7239 linenr_T lnum = 0;
7240 pos_T *fp;
7241 int fnum;
7242
7243 fp = var2fpos(&argvars[0], TRUE, &fnum);
7244 if (fp != NULL)
7245 lnum = fp->lnum;
7246 rettv->vval.v_number = lnum;
7247}
7248
7249/*
7250 * "line2byte(lnum)" function
7251 */
7252 static void
7253f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7254{
7255#ifndef FEAT_BYTEOFF
7256 rettv->vval.v_number = -1;
7257#else
7258 linenr_T lnum;
7259
7260 lnum = get_tv_lnum(argvars);
7261 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7262 rettv->vval.v_number = -1;
7263 else
7264 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7265 if (rettv->vval.v_number >= 0)
7266 ++rettv->vval.v_number;
7267#endif
7268}
7269
7270/*
7271 * "lispindent(lnum)" function
7272 */
7273 static void
7274f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7275{
7276#ifdef FEAT_LISP
7277 pos_T pos;
7278 linenr_T lnum;
7279
7280 pos = curwin->w_cursor;
7281 lnum = get_tv_lnum(argvars);
7282 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7283 {
7284 curwin->w_cursor.lnum = lnum;
7285 rettv->vval.v_number = get_lisp_indent();
7286 curwin->w_cursor = pos;
7287 }
7288 else
7289#endif
7290 rettv->vval.v_number = -1;
7291}
7292
7293/*
7294 * "localtime()" function
7295 */
7296 static void
7297f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7298{
7299 rettv->vval.v_number = (varnumber_T)time(NULL);
7300}
7301
7302static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
7303
7304 static void
7305get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7306{
7307 char_u *keys;
7308 char_u *which;
7309 char_u buf[NUMBUFLEN];
7310 char_u *keys_buf = NULL;
7311 char_u *rhs;
7312 int mode;
7313 int abbr = FALSE;
7314 int get_dict = FALSE;
7315 mapblock_T *mp;
7316 int buffer_local;
7317
7318 /* return empty string for failure */
7319 rettv->v_type = VAR_STRING;
7320 rettv->vval.v_string = NULL;
7321
7322 keys = get_tv_string(&argvars[0]);
7323 if (*keys == NUL)
7324 return;
7325
7326 if (argvars[1].v_type != VAR_UNKNOWN)
7327 {
7328 which = get_tv_string_buf_chk(&argvars[1], buf);
7329 if (argvars[2].v_type != VAR_UNKNOWN)
7330 {
7331 abbr = (int)get_tv_number(&argvars[2]);
7332 if (argvars[3].v_type != VAR_UNKNOWN)
7333 get_dict = (int)get_tv_number(&argvars[3]);
7334 }
7335 }
7336 else
7337 which = (char_u *)"";
7338 if (which == NULL)
7339 return;
7340
7341 mode = get_map_mode(&which, 0);
7342
7343 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7344 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7345 vim_free(keys_buf);
7346
7347 if (!get_dict)
7348 {
7349 /* Return a string. */
7350 if (rhs != NULL)
7351 rettv->vval.v_string = str2special_save(rhs, FALSE);
7352
7353 }
7354 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7355 {
7356 /* Return a dictionary. */
7357 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7358 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7359 dict_T *dict = rettv->vval.v_dict;
7360
7361 dict_add_nr_str(dict, "lhs", 0L, lhs);
7362 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
7363 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
7364 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
7365 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
7366 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
7367 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
7368 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
7369 dict_add_nr_str(dict, "mode", 0L, mapmode);
7370
7371 vim_free(lhs);
7372 vim_free(mapmode);
7373 }
7374}
7375
7376#ifdef FEAT_FLOAT
7377/*
7378 * "log()" function
7379 */
7380 static void
7381f_log(typval_T *argvars, typval_T *rettv)
7382{
7383 float_T f = 0.0;
7384
7385 rettv->v_type = VAR_FLOAT;
7386 if (get_float_arg(argvars, &f) == OK)
7387 rettv->vval.v_float = log(f);
7388 else
7389 rettv->vval.v_float = 0.0;
7390}
7391
7392/*
7393 * "log10()" function
7394 */
7395 static void
7396f_log10(typval_T *argvars, typval_T *rettv)
7397{
7398 float_T f = 0.0;
7399
7400 rettv->v_type = VAR_FLOAT;
7401 if (get_float_arg(argvars, &f) == OK)
7402 rettv->vval.v_float = log10(f);
7403 else
7404 rettv->vval.v_float = 0.0;
7405}
7406#endif
7407
7408#ifdef FEAT_LUA
7409/*
7410 * "luaeval()" function
7411 */
7412 static void
7413f_luaeval(typval_T *argvars, typval_T *rettv)
7414{
7415 char_u *str;
7416 char_u buf[NUMBUFLEN];
7417
7418 str = get_tv_string_buf(&argvars[0], buf);
7419 do_luaeval(str, argvars + 1, rettv);
7420}
7421#endif
7422
7423/*
7424 * "map()" function
7425 */
7426 static void
7427f_map(typval_T *argvars, typval_T *rettv)
7428{
7429 filter_map(argvars, rettv, TRUE);
7430}
7431
7432/*
7433 * "maparg()" function
7434 */
7435 static void
7436f_maparg(typval_T *argvars, typval_T *rettv)
7437{
7438 get_maparg(argvars, rettv, TRUE);
7439}
7440
7441/*
7442 * "mapcheck()" function
7443 */
7444 static void
7445f_mapcheck(typval_T *argvars, typval_T *rettv)
7446{
7447 get_maparg(argvars, rettv, FALSE);
7448}
7449
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007450typedef enum
7451{
7452 MATCH_END, /* matchend() */
7453 MATCH_MATCH, /* match() */
7454 MATCH_STR, /* matchstr() */
7455 MATCH_LIST, /* matchlist() */
7456 MATCH_POS /* matchstrpos() */
7457} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007458
7459 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007460find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007461{
7462 char_u *str = NULL;
7463 long len = 0;
7464 char_u *expr = NULL;
7465 char_u *pat;
7466 regmatch_T regmatch;
7467 char_u patbuf[NUMBUFLEN];
7468 char_u strbuf[NUMBUFLEN];
7469 char_u *save_cpo;
7470 long start = 0;
7471 long nth = 1;
7472 colnr_T startcol = 0;
7473 int match = 0;
7474 list_T *l = NULL;
7475 listitem_T *li = NULL;
7476 long idx = 0;
7477 char_u *tofree = NULL;
7478
7479 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7480 save_cpo = p_cpo;
7481 p_cpo = (char_u *)"";
7482
7483 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007484 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007485 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007486 /* type MATCH_LIST: return empty list when there are no matches.
7487 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007488 if (rettv_list_alloc(rettv) == FAIL)
7489 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007490 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007491 && (list_append_string(rettv->vval.v_list,
7492 (char_u *)"", 0) == FAIL
7493 || list_append_number(rettv->vval.v_list,
7494 (varnumber_T)-1) == FAIL
7495 || list_append_number(rettv->vval.v_list,
7496 (varnumber_T)-1) == FAIL
7497 || list_append_number(rettv->vval.v_list,
7498 (varnumber_T)-1) == FAIL))
7499 {
7500 list_free(rettv->vval.v_list);
7501 rettv->vval.v_list = NULL;
7502 goto theend;
7503 }
7504 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007505 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007506 {
7507 rettv->v_type = VAR_STRING;
7508 rettv->vval.v_string = NULL;
7509 }
7510
7511 if (argvars[0].v_type == VAR_LIST)
7512 {
7513 if ((l = argvars[0].vval.v_list) == NULL)
7514 goto theend;
7515 li = l->lv_first;
7516 }
7517 else
7518 {
7519 expr = str = get_tv_string(&argvars[0]);
7520 len = (long)STRLEN(str);
7521 }
7522
7523 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
7524 if (pat == NULL)
7525 goto theend;
7526
7527 if (argvars[2].v_type != VAR_UNKNOWN)
7528 {
7529 int error = FALSE;
7530
7531 start = (long)get_tv_number_chk(&argvars[2], &error);
7532 if (error)
7533 goto theend;
7534 if (l != NULL)
7535 {
7536 li = list_find(l, start);
7537 if (li == NULL)
7538 goto theend;
7539 idx = l->lv_idx; /* use the cached index */
7540 }
7541 else
7542 {
7543 if (start < 0)
7544 start = 0;
7545 if (start > len)
7546 goto theend;
7547 /* When "count" argument is there ignore matches before "start",
7548 * otherwise skip part of the string. Differs when pattern is "^"
7549 * or "\<". */
7550 if (argvars[3].v_type != VAR_UNKNOWN)
7551 startcol = start;
7552 else
7553 {
7554 str += start;
7555 len -= start;
7556 }
7557 }
7558
7559 if (argvars[3].v_type != VAR_UNKNOWN)
7560 nth = (long)get_tv_number_chk(&argvars[3], &error);
7561 if (error)
7562 goto theend;
7563 }
7564
7565 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7566 if (regmatch.regprog != NULL)
7567 {
7568 regmatch.rm_ic = p_ic;
7569
7570 for (;;)
7571 {
7572 if (l != NULL)
7573 {
7574 if (li == NULL)
7575 {
7576 match = FALSE;
7577 break;
7578 }
7579 vim_free(tofree);
7580 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7581 if (str == NULL)
7582 break;
7583 }
7584
7585 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7586
7587 if (match && --nth <= 0)
7588 break;
7589 if (l == NULL && !match)
7590 break;
7591
7592 /* Advance to just after the match. */
7593 if (l != NULL)
7594 {
7595 li = li->li_next;
7596 ++idx;
7597 }
7598 else
7599 {
7600#ifdef FEAT_MBYTE
7601 startcol = (colnr_T)(regmatch.startp[0]
7602 + (*mb_ptr2len)(regmatch.startp[0]) - str);
7603#else
7604 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
7605#endif
7606 if (startcol > (colnr_T)len
7607 || str + startcol <= regmatch.startp[0])
7608 {
7609 match = FALSE;
7610 break;
7611 }
7612 }
7613 }
7614
7615 if (match)
7616 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007617 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007618 {
7619 listitem_T *li1 = rettv->vval.v_list->lv_first;
7620 listitem_T *li2 = li1->li_next;
7621 listitem_T *li3 = li2->li_next;
7622 listitem_T *li4 = li3->li_next;
7623
7624 vim_free(li1->li_tv.vval.v_string);
7625 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7626 (int)(regmatch.endp[0] - regmatch.startp[0]));
7627 li3->li_tv.vval.v_number =
7628 (varnumber_T)(regmatch.startp[0] - expr);
7629 li4->li_tv.vval.v_number =
7630 (varnumber_T)(regmatch.endp[0] - expr);
7631 if (l != NULL)
7632 li2->li_tv.vval.v_number = (varnumber_T)idx;
7633 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007634 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007635 {
7636 int i;
7637
7638 /* return list with matched string and submatches */
7639 for (i = 0; i < NSUBEXP; ++i)
7640 {
7641 if (regmatch.endp[i] == NULL)
7642 {
7643 if (list_append_string(rettv->vval.v_list,
7644 (char_u *)"", 0) == FAIL)
7645 break;
7646 }
7647 else if (list_append_string(rettv->vval.v_list,
7648 regmatch.startp[i],
7649 (int)(regmatch.endp[i] - regmatch.startp[i]))
7650 == FAIL)
7651 break;
7652 }
7653 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007654 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007655 {
7656 /* return matched string */
7657 if (l != NULL)
7658 copy_tv(&li->li_tv, rettv);
7659 else
7660 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
7661 (int)(regmatch.endp[0] - regmatch.startp[0]));
7662 }
7663 else if (l != NULL)
7664 rettv->vval.v_number = idx;
7665 else
7666 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007667 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007668 rettv->vval.v_number =
7669 (varnumber_T)(regmatch.startp[0] - str);
7670 else
7671 rettv->vval.v_number =
7672 (varnumber_T)(regmatch.endp[0] - str);
7673 rettv->vval.v_number += (varnumber_T)(str - expr);
7674 }
7675 }
7676 vim_regfree(regmatch.regprog);
7677 }
7678
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007679theend:
7680 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007681 /* matchstrpos() without a list: drop the second item. */
7682 listitem_remove(rettv->vval.v_list,
7683 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007684 vim_free(tofree);
7685 p_cpo = save_cpo;
7686}
7687
7688/*
7689 * "match()" function
7690 */
7691 static void
7692f_match(typval_T *argvars, typval_T *rettv)
7693{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007694 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007695}
7696
7697/*
7698 * "matchadd()" function
7699 */
7700 static void
7701f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7702{
7703#ifdef FEAT_SEARCH_EXTRA
7704 char_u buf[NUMBUFLEN];
7705 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
7706 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
7707 int prio = 10; /* default priority */
7708 int id = -1;
7709 int error = FALSE;
7710 char_u *conceal_char = NULL;
7711
7712 rettv->vval.v_number = -1;
7713
7714 if (grp == NULL || pat == NULL)
7715 return;
7716 if (argvars[2].v_type != VAR_UNKNOWN)
7717 {
7718 prio = (int)get_tv_number_chk(&argvars[2], &error);
7719 if (argvars[3].v_type != VAR_UNKNOWN)
7720 {
7721 id = (int)get_tv_number_chk(&argvars[3], &error);
7722 if (argvars[4].v_type != VAR_UNKNOWN)
7723 {
7724 if (argvars[4].v_type != VAR_DICT)
7725 {
7726 EMSG(_(e_dictreq));
7727 return;
7728 }
7729 if (dict_find(argvars[4].vval.v_dict,
7730 (char_u *)"conceal", -1) != NULL)
7731 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7732 (char_u *)"conceal", FALSE);
7733 }
7734 }
7735 }
7736 if (error == TRUE)
7737 return;
7738 if (id >= 1 && id <= 3)
7739 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007740 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007741 return;
7742 }
7743
7744 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
7745 conceal_char);
7746#endif
7747}
7748
7749/*
7750 * "matchaddpos()" function
7751 */
7752 static void
7753f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7754{
7755#ifdef FEAT_SEARCH_EXTRA
7756 char_u buf[NUMBUFLEN];
7757 char_u *group;
7758 int prio = 10;
7759 int id = -1;
7760 int error = FALSE;
7761 list_T *l;
7762 char_u *conceal_char = NULL;
7763
7764 rettv->vval.v_number = -1;
7765
7766 group = get_tv_string_buf_chk(&argvars[0], buf);
7767 if (group == NULL)
7768 return;
7769
7770 if (argvars[1].v_type != VAR_LIST)
7771 {
7772 EMSG2(_(e_listarg), "matchaddpos()");
7773 return;
7774 }
7775 l = argvars[1].vval.v_list;
7776 if (l == NULL)
7777 return;
7778
7779 if (argvars[2].v_type != VAR_UNKNOWN)
7780 {
7781 prio = (int)get_tv_number_chk(&argvars[2], &error);
7782 if (argvars[3].v_type != VAR_UNKNOWN)
7783 {
7784 id = (int)get_tv_number_chk(&argvars[3], &error);
7785 if (argvars[4].v_type != VAR_UNKNOWN)
7786 {
7787 if (argvars[4].v_type != VAR_DICT)
7788 {
7789 EMSG(_(e_dictreq));
7790 return;
7791 }
7792 if (dict_find(argvars[4].vval.v_dict,
7793 (char_u *)"conceal", -1) != NULL)
7794 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7795 (char_u *)"conceal", FALSE);
7796 }
7797 }
7798 }
7799 if (error == TRUE)
7800 return;
7801
7802 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
7803 if (id == 1 || id == 2)
7804 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007805 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007806 return;
7807 }
7808
7809 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
7810 conceal_char);
7811#endif
7812}
7813
7814/*
7815 * "matcharg()" function
7816 */
7817 static void
7818f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
7819{
7820 if (rettv_list_alloc(rettv) == OK)
7821 {
7822#ifdef FEAT_SEARCH_EXTRA
7823 int id = (int)get_tv_number(&argvars[0]);
7824 matchitem_T *m;
7825
7826 if (id >= 1 && id <= 3)
7827 {
7828 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
7829 {
7830 list_append_string(rettv->vval.v_list,
7831 syn_id2name(m->hlg_id), -1);
7832 list_append_string(rettv->vval.v_list, m->pattern, -1);
7833 }
7834 else
7835 {
7836 list_append_string(rettv->vval.v_list, NULL, -1);
7837 list_append_string(rettv->vval.v_list, NULL, -1);
7838 }
7839 }
7840#endif
7841 }
7842}
7843
7844/*
7845 * "matchdelete()" function
7846 */
7847 static void
7848f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7849{
7850#ifdef FEAT_SEARCH_EXTRA
7851 rettv->vval.v_number = match_delete(curwin,
7852 (int)get_tv_number(&argvars[0]), TRUE);
7853#endif
7854}
7855
7856/*
7857 * "matchend()" function
7858 */
7859 static void
7860f_matchend(typval_T *argvars, typval_T *rettv)
7861{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007862 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007863}
7864
7865/*
7866 * "matchlist()" function
7867 */
7868 static void
7869f_matchlist(typval_T *argvars, typval_T *rettv)
7870{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007871 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007872}
7873
7874/*
7875 * "matchstr()" function
7876 */
7877 static void
7878f_matchstr(typval_T *argvars, typval_T *rettv)
7879{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007880 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007881}
7882
7883/*
7884 * "matchstrpos()" function
7885 */
7886 static void
7887f_matchstrpos(typval_T *argvars, typval_T *rettv)
7888{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007889 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007890}
7891
7892static void max_min(typval_T *argvars, typval_T *rettv, int domax);
7893
7894 static void
7895max_min(typval_T *argvars, typval_T *rettv, int domax)
7896{
7897 varnumber_T n = 0;
7898 varnumber_T i;
7899 int error = FALSE;
7900
7901 if (argvars[0].v_type == VAR_LIST)
7902 {
7903 list_T *l;
7904 listitem_T *li;
7905
7906 l = argvars[0].vval.v_list;
7907 if (l != NULL)
7908 {
7909 li = l->lv_first;
7910 if (li != NULL)
7911 {
7912 n = get_tv_number_chk(&li->li_tv, &error);
7913 for (;;)
7914 {
7915 li = li->li_next;
7916 if (li == NULL)
7917 break;
7918 i = get_tv_number_chk(&li->li_tv, &error);
7919 if (domax ? i > n : i < n)
7920 n = i;
7921 }
7922 }
7923 }
7924 }
7925 else if (argvars[0].v_type == VAR_DICT)
7926 {
7927 dict_T *d;
7928 int first = TRUE;
7929 hashitem_T *hi;
7930 int todo;
7931
7932 d = argvars[0].vval.v_dict;
7933 if (d != NULL)
7934 {
7935 todo = (int)d->dv_hashtab.ht_used;
7936 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
7937 {
7938 if (!HASHITEM_EMPTY(hi))
7939 {
7940 --todo;
7941 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
7942 if (first)
7943 {
7944 n = i;
7945 first = FALSE;
7946 }
7947 else if (domax ? i > n : i < n)
7948 n = i;
7949 }
7950 }
7951 }
7952 }
7953 else
Bram Moolenaar26b84332016-09-04 21:42:36 +02007954 EMSG2(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007955 rettv->vval.v_number = error ? 0 : n;
7956}
7957
7958/*
7959 * "max()" function
7960 */
7961 static void
7962f_max(typval_T *argvars, typval_T *rettv)
7963{
7964 max_min(argvars, rettv, TRUE);
7965}
7966
7967/*
7968 * "min()" function
7969 */
7970 static void
7971f_min(typval_T *argvars, typval_T *rettv)
7972{
7973 max_min(argvars, rettv, FALSE);
7974}
7975
7976static int mkdir_recurse(char_u *dir, int prot);
7977
7978/*
7979 * Create the directory in which "dir" is located, and higher levels when
7980 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02007981 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007982 */
7983 static int
7984mkdir_recurse(char_u *dir, int prot)
7985{
7986 char_u *p;
7987 char_u *updir;
7988 int r = FAIL;
7989
7990 /* Get end of directory name in "dir".
7991 * We're done when it's "/" or "c:/". */
7992 p = gettail_sep(dir);
7993 if (p <= get_past_head(dir))
7994 return OK;
7995
7996 /* If the directory exists we're done. Otherwise: create it.*/
7997 updir = vim_strnsave(dir, (int)(p - dir));
7998 if (updir == NULL)
7999 return FAIL;
8000 if (mch_isdir(updir))
8001 r = OK;
8002 else if (mkdir_recurse(updir, prot) == OK)
8003 r = vim_mkdir_emsg(updir, prot);
8004 vim_free(updir);
8005 return r;
8006}
8007
8008#ifdef vim_mkdir
8009/*
8010 * "mkdir()" function
8011 */
8012 static void
8013f_mkdir(typval_T *argvars, typval_T *rettv)
8014{
8015 char_u *dir;
8016 char_u buf[NUMBUFLEN];
8017 int prot = 0755;
8018
8019 rettv->vval.v_number = FAIL;
8020 if (check_restricted() || check_secure())
8021 return;
8022
8023 dir = get_tv_string_buf(&argvars[0], buf);
8024 if (*dir == NUL)
8025 rettv->vval.v_number = FAIL;
8026 else
8027 {
8028 if (*gettail(dir) == NUL)
8029 /* remove trailing slashes */
8030 *gettail_sep(dir) = NUL;
8031
8032 if (argvars[1].v_type != VAR_UNKNOWN)
8033 {
8034 if (argvars[2].v_type != VAR_UNKNOWN)
8035 prot = (int)get_tv_number_chk(&argvars[2], NULL);
8036 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
8037 mkdir_recurse(dir, prot);
8038 }
8039 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
8040 }
8041}
8042#endif
8043
8044/*
8045 * "mode()" function
8046 */
8047 static void
8048f_mode(typval_T *argvars, typval_T *rettv)
8049{
8050 char_u buf[3];
8051
8052 buf[1] = NUL;
8053 buf[2] = NUL;
8054
8055 if (time_for_testing == 93784)
8056 {
8057 /* Testing the two-character code. */
8058 buf[0] = 'x';
8059 buf[1] = '!';
8060 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008061#ifdef FEAT_TERMINAL
8062 else if (term_use_loop())
8063 buf[0] = 't';
8064#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008065 else if (VIsual_active)
8066 {
8067 if (VIsual_select)
8068 buf[0] = VIsual_mode + 's' - 'v';
8069 else
8070 buf[0] = VIsual_mode;
8071 }
8072 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8073 || State == CONFIRM)
8074 {
8075 buf[0] = 'r';
8076 if (State == ASKMORE)
8077 buf[1] = 'm';
8078 else if (State == CONFIRM)
8079 buf[1] = '?';
8080 }
8081 else if (State == EXTERNCMD)
8082 buf[0] = '!';
8083 else if (State & INSERT)
8084 {
8085#ifdef FEAT_VREPLACE
8086 if (State & VREPLACE_FLAG)
8087 {
8088 buf[0] = 'R';
8089 buf[1] = 'v';
8090 }
8091 else
8092#endif
Bram Moolenaare90858d2017-02-01 17:24:34 +01008093 {
8094 if (State & REPLACE_FLAG)
8095 buf[0] = 'R';
8096 else
8097 buf[0] = 'i';
8098#ifdef FEAT_INS_EXPAND
8099 if (ins_compl_active())
8100 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008101 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008102 buf[1] = 'x';
8103#endif
8104 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008105 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008106 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008107 {
8108 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008109 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008110 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008111 else if (exmode_active == EXMODE_NORMAL)
8112 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008113 }
8114 else
8115 {
8116 buf[0] = 'n';
8117 if (finish_op)
8118 buf[1] = 'o';
8119 }
8120
8121 /* Clear out the minor mode when the argument is not a non-zero number or
8122 * non-empty string. */
8123 if (!non_zero_arg(&argvars[0]))
8124 buf[1] = NUL;
8125
8126 rettv->vval.v_string = vim_strsave(buf);
8127 rettv->v_type = VAR_STRING;
8128}
8129
8130#if defined(FEAT_MZSCHEME) || defined(PROTO)
8131/*
8132 * "mzeval()" function
8133 */
8134 static void
8135f_mzeval(typval_T *argvars, typval_T *rettv)
8136{
8137 char_u *str;
8138 char_u buf[NUMBUFLEN];
8139
8140 str = get_tv_string_buf(&argvars[0], buf);
8141 do_mzeval(str, rettv);
8142}
8143
8144 void
8145mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8146{
8147 typval_T argvars[3];
8148
8149 argvars[0].v_type = VAR_STRING;
8150 argvars[0].vval.v_string = name;
8151 copy_tv(args, &argvars[1]);
8152 argvars[2].v_type = VAR_UNKNOWN;
8153 f_call(argvars, rettv);
8154 clear_tv(&argvars[1]);
8155}
8156#endif
8157
8158/*
8159 * "nextnonblank()" function
8160 */
8161 static void
8162f_nextnonblank(typval_T *argvars, typval_T *rettv)
8163{
8164 linenr_T lnum;
8165
8166 for (lnum = get_tv_lnum(argvars); ; ++lnum)
8167 {
8168 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8169 {
8170 lnum = 0;
8171 break;
8172 }
8173 if (*skipwhite(ml_get(lnum)) != NUL)
8174 break;
8175 }
8176 rettv->vval.v_number = lnum;
8177}
8178
8179/*
8180 * "nr2char()" function
8181 */
8182 static void
8183f_nr2char(typval_T *argvars, typval_T *rettv)
8184{
8185 char_u buf[NUMBUFLEN];
8186
8187#ifdef FEAT_MBYTE
8188 if (has_mbyte)
8189 {
8190 int utf8 = 0;
8191
8192 if (argvars[1].v_type != VAR_UNKNOWN)
8193 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
8194 if (utf8)
8195 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8196 else
8197 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8198 }
8199 else
8200#endif
8201 {
8202 buf[0] = (char_u)get_tv_number(&argvars[0]);
8203 buf[1] = NUL;
8204 }
8205 rettv->v_type = VAR_STRING;
8206 rettv->vval.v_string = vim_strsave(buf);
8207}
8208
8209/*
8210 * "or(expr, expr)" function
8211 */
8212 static void
8213f_or(typval_T *argvars, typval_T *rettv)
8214{
8215 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8216 | get_tv_number_chk(&argvars[1], NULL);
8217}
8218
8219/*
8220 * "pathshorten()" function
8221 */
8222 static void
8223f_pathshorten(typval_T *argvars, typval_T *rettv)
8224{
8225 char_u *p;
8226
8227 rettv->v_type = VAR_STRING;
8228 p = get_tv_string_chk(&argvars[0]);
8229 if (p == NULL)
8230 rettv->vval.v_string = NULL;
8231 else
8232 {
8233 p = vim_strsave(p);
8234 rettv->vval.v_string = p;
8235 if (p != NULL)
8236 shorten_dir(p);
8237 }
8238}
8239
8240#ifdef FEAT_PERL
8241/*
8242 * "perleval()" function
8243 */
8244 static void
8245f_perleval(typval_T *argvars, typval_T *rettv)
8246{
8247 char_u *str;
8248 char_u buf[NUMBUFLEN];
8249
8250 str = get_tv_string_buf(&argvars[0], buf);
8251 do_perleval(str, rettv);
8252}
8253#endif
8254
8255#ifdef FEAT_FLOAT
8256/*
8257 * "pow()" function
8258 */
8259 static void
8260f_pow(typval_T *argvars, typval_T *rettv)
8261{
8262 float_T fx = 0.0, fy = 0.0;
8263
8264 rettv->v_type = VAR_FLOAT;
8265 if (get_float_arg(argvars, &fx) == OK
8266 && get_float_arg(&argvars[1], &fy) == OK)
8267 rettv->vval.v_float = pow(fx, fy);
8268 else
8269 rettv->vval.v_float = 0.0;
8270}
8271#endif
8272
8273/*
8274 * "prevnonblank()" function
8275 */
8276 static void
8277f_prevnonblank(typval_T *argvars, typval_T *rettv)
8278{
8279 linenr_T lnum;
8280
8281 lnum = get_tv_lnum(argvars);
8282 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8283 lnum = 0;
8284 else
8285 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8286 --lnum;
8287 rettv->vval.v_number = lnum;
8288}
8289
8290/* This dummy va_list is here because:
8291 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8292 * - locally in the function results in a "used before set" warning
8293 * - using va_start() to initialize it gives "function with fixed args" error */
8294static va_list ap;
8295
8296/*
8297 * "printf()" function
8298 */
8299 static void
8300f_printf(typval_T *argvars, typval_T *rettv)
8301{
8302 char_u buf[NUMBUFLEN];
8303 int len;
8304 char_u *s;
8305 int saved_did_emsg = did_emsg;
8306 char *fmt;
8307
8308 rettv->v_type = VAR_STRING;
8309 rettv->vval.v_string = NULL;
8310
8311 /* Get the required length, allocate the buffer and do it for real. */
8312 did_emsg = FALSE;
8313 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008314 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008315 if (!did_emsg)
8316 {
8317 s = alloc(len + 1);
8318 if (s != NULL)
8319 {
8320 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008321 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8322 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008323 }
8324 }
8325 did_emsg |= saved_did_emsg;
8326}
8327
8328/*
8329 * "pumvisible()" function
8330 */
8331 static void
8332f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8333{
8334#ifdef FEAT_INS_EXPAND
8335 if (pum_visible())
8336 rettv->vval.v_number = 1;
8337#endif
8338}
8339
8340#ifdef FEAT_PYTHON3
8341/*
8342 * "py3eval()" function
8343 */
8344 static void
8345f_py3eval(typval_T *argvars, typval_T *rettv)
8346{
8347 char_u *str;
8348 char_u buf[NUMBUFLEN];
8349
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008350 if (p_pyx == 0)
8351 p_pyx = 3;
8352
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008353 str = get_tv_string_buf(&argvars[0], buf);
8354 do_py3eval(str, rettv);
8355}
8356#endif
8357
8358#ifdef FEAT_PYTHON
8359/*
8360 * "pyeval()" function
8361 */
8362 static void
8363f_pyeval(typval_T *argvars, typval_T *rettv)
8364{
8365 char_u *str;
8366 char_u buf[NUMBUFLEN];
8367
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008368 if (p_pyx == 0)
8369 p_pyx = 2;
8370
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008371 str = get_tv_string_buf(&argvars[0], buf);
8372 do_pyeval(str, rettv);
8373}
8374#endif
8375
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008376#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8377/*
8378 * "pyxeval()" function
8379 */
8380 static void
8381f_pyxeval(typval_T *argvars, typval_T *rettv)
8382{
8383# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8384 init_pyxversion();
8385 if (p_pyx == 2)
8386 f_pyeval(argvars, rettv);
8387 else
8388 f_py3eval(argvars, rettv);
8389# elif defined(FEAT_PYTHON)
8390 f_pyeval(argvars, rettv);
8391# elif defined(FEAT_PYTHON3)
8392 f_py3eval(argvars, rettv);
8393# endif
8394}
8395#endif
8396
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008397/*
8398 * "range()" function
8399 */
8400 static void
8401f_range(typval_T *argvars, typval_T *rettv)
8402{
8403 varnumber_T start;
8404 varnumber_T end;
8405 varnumber_T stride = 1;
8406 varnumber_T i;
8407 int error = FALSE;
8408
8409 start = get_tv_number_chk(&argvars[0], &error);
8410 if (argvars[1].v_type == VAR_UNKNOWN)
8411 {
8412 end = start - 1;
8413 start = 0;
8414 }
8415 else
8416 {
8417 end = get_tv_number_chk(&argvars[1], &error);
8418 if (argvars[2].v_type != VAR_UNKNOWN)
8419 stride = get_tv_number_chk(&argvars[2], &error);
8420 }
8421
8422 if (error)
8423 return; /* type error; errmsg already given */
8424 if (stride == 0)
8425 EMSG(_("E726: Stride is zero"));
8426 else if (stride > 0 ? end + 1 < start : end - 1 > start)
8427 EMSG(_("E727: Start past end"));
8428 else
8429 {
8430 if (rettv_list_alloc(rettv) == OK)
8431 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8432 if (list_append_number(rettv->vval.v_list,
8433 (varnumber_T)i) == FAIL)
8434 break;
8435 }
8436}
8437
8438/*
8439 * "readfile()" function
8440 */
8441 static void
8442f_readfile(typval_T *argvars, typval_T *rettv)
8443{
8444 int binary = FALSE;
8445 int failed = FALSE;
8446 char_u *fname;
8447 FILE *fd;
8448 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8449 int io_size = sizeof(buf);
8450 int readlen; /* size of last fread() */
8451 char_u *prev = NULL; /* previously read bytes, if any */
8452 long prevlen = 0; /* length of data in prev */
8453 long prevsize = 0; /* size of prev buffer */
8454 long maxline = MAXLNUM;
8455 long cnt = 0;
8456 char_u *p; /* position in buf */
8457 char_u *start; /* start of current line */
8458
8459 if (argvars[1].v_type != VAR_UNKNOWN)
8460 {
8461 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
8462 binary = TRUE;
8463 if (argvars[2].v_type != VAR_UNKNOWN)
8464 maxline = (long)get_tv_number(&argvars[2]);
8465 }
8466
8467 if (rettv_list_alloc(rettv) == FAIL)
8468 return;
8469
8470 /* Always open the file in binary mode, library functions have a mind of
8471 * their own about CR-LF conversion. */
8472 fname = get_tv_string(&argvars[0]);
8473 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8474 {
8475 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
8476 return;
8477 }
8478
8479 while (cnt < maxline || maxline < 0)
8480 {
8481 readlen = (int)fread(buf, 1, io_size, fd);
8482
8483 /* This for loop processes what was read, but is also entered at end
8484 * of file so that either:
8485 * - an incomplete line gets written
8486 * - a "binary" file gets an empty line at the end if it ends in a
8487 * newline. */
8488 for (p = buf, start = buf;
8489 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8490 ++p)
8491 {
8492 if (*p == '\n' || readlen <= 0)
8493 {
8494 listitem_T *li;
8495 char_u *s = NULL;
8496 long_u len = p - start;
8497
8498 /* Finished a line. Remove CRs before NL. */
8499 if (readlen > 0 && !binary)
8500 {
8501 while (len > 0 && start[len - 1] == '\r')
8502 --len;
8503 /* removal may cross back to the "prev" string */
8504 if (len == 0)
8505 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8506 --prevlen;
8507 }
8508 if (prevlen == 0)
8509 s = vim_strnsave(start, (int)len);
8510 else
8511 {
8512 /* Change "prev" buffer to be the right size. This way
8513 * the bytes are only copied once, and very long lines are
8514 * allocated only once. */
8515 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8516 {
8517 mch_memmove(s + prevlen, start, len);
8518 s[prevlen + len] = NUL;
8519 prev = NULL; /* the list will own the string */
8520 prevlen = prevsize = 0;
8521 }
8522 }
8523 if (s == NULL)
8524 {
8525 do_outofmem_msg((long_u) prevlen + len + 1);
8526 failed = TRUE;
8527 break;
8528 }
8529
8530 if ((li = listitem_alloc()) == NULL)
8531 {
8532 vim_free(s);
8533 failed = TRUE;
8534 break;
8535 }
8536 li->li_tv.v_type = VAR_STRING;
8537 li->li_tv.v_lock = 0;
8538 li->li_tv.vval.v_string = s;
8539 list_append(rettv->vval.v_list, li);
8540
8541 start = p + 1; /* step over newline */
8542 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
8543 break;
8544 }
8545 else if (*p == NUL)
8546 *p = '\n';
8547#ifdef FEAT_MBYTE
8548 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
8549 * when finding the BF and check the previous two bytes. */
8550 else if (*p == 0xbf && enc_utf8 && !binary)
8551 {
8552 /* Find the two bytes before the 0xbf. If p is at buf, or buf
8553 * + 1, these may be in the "prev" string. */
8554 char_u back1 = p >= buf + 1 ? p[-1]
8555 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
8556 char_u back2 = p >= buf + 2 ? p[-2]
8557 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
8558 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
8559
8560 if (back2 == 0xef && back1 == 0xbb)
8561 {
8562 char_u *dest = p - 2;
8563
8564 /* Usually a BOM is at the beginning of a file, and so at
8565 * the beginning of a line; then we can just step over it.
8566 */
8567 if (start == dest)
8568 start = p + 1;
8569 else
8570 {
8571 /* have to shuffle buf to close gap */
8572 int adjust_prevlen = 0;
8573
8574 if (dest < buf)
8575 {
8576 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
8577 dest = buf;
8578 }
8579 if (readlen > p - buf + 1)
8580 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
8581 readlen -= 3 - adjust_prevlen;
8582 prevlen -= adjust_prevlen;
8583 p = dest - 1;
8584 }
8585 }
8586 }
8587#endif
8588 } /* for */
8589
8590 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
8591 break;
8592 if (start < p)
8593 {
8594 /* There's part of a line in buf, store it in "prev". */
8595 if (p - start + prevlen >= prevsize)
8596 {
8597 /* need bigger "prev" buffer */
8598 char_u *newprev;
8599
8600 /* A common use case is ordinary text files and "prev" gets a
8601 * fragment of a line, so the first allocation is made
8602 * small, to avoid repeatedly 'allocing' large and
8603 * 'reallocing' small. */
8604 if (prevsize == 0)
8605 prevsize = (long)(p - start);
8606 else
8607 {
8608 long grow50pc = (prevsize * 3) / 2;
8609 long growmin = (long)((p - start) * 2 + prevlen);
8610 prevsize = grow50pc > growmin ? grow50pc : growmin;
8611 }
8612 newprev = prev == NULL ? alloc(prevsize)
8613 : vim_realloc(prev, prevsize);
8614 if (newprev == NULL)
8615 {
8616 do_outofmem_msg((long_u)prevsize);
8617 failed = TRUE;
8618 break;
8619 }
8620 prev = newprev;
8621 }
8622 /* Add the line part to end of "prev". */
8623 mch_memmove(prev + prevlen, start, p - start);
8624 prevlen += (long)(p - start);
8625 }
8626 } /* while */
8627
8628 /*
8629 * For a negative line count use only the lines at the end of the file,
8630 * free the rest.
8631 */
8632 if (!failed && maxline < 0)
8633 while (cnt > -maxline)
8634 {
8635 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
8636 --cnt;
8637 }
8638
8639 if (failed)
8640 {
8641 list_free(rettv->vval.v_list);
8642 /* readfile doc says an empty list is returned on error */
8643 rettv->vval.v_list = list_alloc();
8644 }
8645
8646 vim_free(prev);
8647 fclose(fd);
8648}
8649
8650#if defined(FEAT_RELTIME)
8651static int list2proftime(typval_T *arg, proftime_T *tm);
8652
8653/*
8654 * Convert a List to proftime_T.
8655 * Return FAIL when there is something wrong.
8656 */
8657 static int
8658list2proftime(typval_T *arg, proftime_T *tm)
8659{
8660 long n1, n2;
8661 int error = FALSE;
8662
8663 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
8664 || arg->vval.v_list->lv_len != 2)
8665 return FAIL;
8666 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
8667 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
8668# ifdef WIN3264
8669 tm->HighPart = n1;
8670 tm->LowPart = n2;
8671# else
8672 tm->tv_sec = n1;
8673 tm->tv_usec = n2;
8674# endif
8675 return error ? FAIL : OK;
8676}
8677#endif /* FEAT_RELTIME */
8678
8679/*
8680 * "reltime()" function
8681 */
8682 static void
8683f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8684{
8685#ifdef FEAT_RELTIME
8686 proftime_T res;
8687 proftime_T start;
8688
8689 if (argvars[0].v_type == VAR_UNKNOWN)
8690 {
8691 /* No arguments: get current time. */
8692 profile_start(&res);
8693 }
8694 else if (argvars[1].v_type == VAR_UNKNOWN)
8695 {
8696 if (list2proftime(&argvars[0], &res) == FAIL)
8697 return;
8698 profile_end(&res);
8699 }
8700 else
8701 {
8702 /* Two arguments: compute the difference. */
8703 if (list2proftime(&argvars[0], &start) == FAIL
8704 || list2proftime(&argvars[1], &res) == FAIL)
8705 return;
8706 profile_sub(&res, &start);
8707 }
8708
8709 if (rettv_list_alloc(rettv) == OK)
8710 {
8711 long n1, n2;
8712
8713# ifdef WIN3264
8714 n1 = res.HighPart;
8715 n2 = res.LowPart;
8716# else
8717 n1 = res.tv_sec;
8718 n2 = res.tv_usec;
8719# endif
8720 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
8721 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
8722 }
8723#endif
8724}
8725
8726#ifdef FEAT_FLOAT
8727/*
8728 * "reltimefloat()" function
8729 */
8730 static void
8731f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
8732{
8733# ifdef FEAT_RELTIME
8734 proftime_T tm;
8735# endif
8736
8737 rettv->v_type = VAR_FLOAT;
8738 rettv->vval.v_float = 0;
8739# ifdef FEAT_RELTIME
8740 if (list2proftime(&argvars[0], &tm) == OK)
8741 rettv->vval.v_float = profile_float(&tm);
8742# endif
8743}
8744#endif
8745
8746/*
8747 * "reltimestr()" function
8748 */
8749 static void
8750f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
8751{
8752#ifdef FEAT_RELTIME
8753 proftime_T tm;
8754#endif
8755
8756 rettv->v_type = VAR_STRING;
8757 rettv->vval.v_string = NULL;
8758#ifdef FEAT_RELTIME
8759 if (list2proftime(&argvars[0], &tm) == OK)
8760 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
8761#endif
8762}
8763
8764#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
8765static void make_connection(void);
8766static int check_connection(void);
8767
8768 static void
8769make_connection(void)
8770{
8771 if (X_DISPLAY == NULL
8772# ifdef FEAT_GUI
8773 && !gui.in_use
8774# endif
8775 )
8776 {
8777 x_force_connect = TRUE;
8778 setup_term_clip();
8779 x_force_connect = FALSE;
8780 }
8781}
8782
8783 static int
8784check_connection(void)
8785{
8786 make_connection();
8787 if (X_DISPLAY == NULL)
8788 {
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01008789 EMSG(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008790 return FAIL;
8791 }
8792 return OK;
8793}
8794#endif
8795
8796#ifdef FEAT_CLIENTSERVER
8797 static void
8798remote_common(typval_T *argvars, typval_T *rettv, int expr)
8799{
8800 char_u *server_name;
8801 char_u *keys;
8802 char_u *r = NULL;
8803 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008804 int timeout = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008805# ifdef WIN32
8806 HWND w;
8807# else
8808 Window w;
8809# endif
8810
8811 if (check_restricted() || check_secure())
8812 return;
8813
8814# ifdef FEAT_X11
8815 if (check_connection() == FAIL)
8816 return;
8817# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008818 if (argvars[2].v_type != VAR_UNKNOWN
8819 && argvars[3].v_type != VAR_UNKNOWN)
8820 timeout = get_tv_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008821
8822 server_name = get_tv_string_chk(&argvars[0]);
8823 if (server_name == NULL)
8824 return; /* type error; errmsg already given */
8825 keys = get_tv_string_buf(&argvars[1], buf);
8826# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008827 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008828# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008829 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
8830 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008831# endif
8832 {
8833 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008834 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008835 EMSG(r); /* sending worked but evaluation failed */
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008836 vim_free(r);
8837 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008838 else
8839 EMSG2(_("E241: Unable to send to %s"), server_name);
8840 return;
8841 }
8842
8843 rettv->vval.v_string = r;
8844
8845 if (argvars[2].v_type != VAR_UNKNOWN)
8846 {
8847 dictitem_T v;
8848 char_u str[30];
8849 char_u *idvar;
8850
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008851 idvar = get_tv_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008852 if (idvar != NULL && *idvar != NUL)
8853 {
8854 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
8855 v.di_tv.v_type = VAR_STRING;
8856 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008857 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008858 vim_free(v.di_tv.vval.v_string);
8859 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008860 }
8861}
8862#endif
8863
8864/*
8865 * "remote_expr()" function
8866 */
8867 static void
8868f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
8869{
8870 rettv->v_type = VAR_STRING;
8871 rettv->vval.v_string = NULL;
8872#ifdef FEAT_CLIENTSERVER
8873 remote_common(argvars, rettv, TRUE);
8874#endif
8875}
8876
8877/*
8878 * "remote_foreground()" function
8879 */
8880 static void
8881f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8882{
8883#ifdef FEAT_CLIENTSERVER
8884# ifdef WIN32
8885 /* On Win32 it's done in this application. */
8886 {
8887 char_u *server_name = get_tv_string_chk(&argvars[0]);
8888
8889 if (server_name != NULL)
8890 serverForeground(server_name);
8891 }
8892# else
8893 /* Send a foreground() expression to the server. */
8894 argvars[1].v_type = VAR_STRING;
8895 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
8896 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008897 rettv->v_type = VAR_STRING;
8898 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008899 remote_common(argvars, rettv, TRUE);
8900 vim_free(argvars[1].vval.v_string);
8901# endif
8902#endif
8903}
8904
8905 static void
8906f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
8907{
8908#ifdef FEAT_CLIENTSERVER
8909 dictitem_T v;
8910 char_u *s = NULL;
8911# ifdef WIN32
8912 long_u n = 0;
8913# endif
8914 char_u *serverid;
8915
8916 if (check_restricted() || check_secure())
8917 {
8918 rettv->vval.v_number = -1;
8919 return;
8920 }
8921 serverid = get_tv_string_chk(&argvars[0]);
8922 if (serverid == NULL)
8923 {
8924 rettv->vval.v_number = -1;
8925 return; /* type error; errmsg already given */
8926 }
8927# ifdef WIN32
8928 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
8929 if (n == 0)
8930 rettv->vval.v_number = -1;
8931 else
8932 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008933 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008934 rettv->vval.v_number = (s != NULL);
8935 }
8936# else
8937 if (check_connection() == FAIL)
8938 return;
8939
8940 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
8941 serverStrToWin(serverid), &s);
8942# endif
8943
8944 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
8945 {
8946 char_u *retvar;
8947
8948 v.di_tv.v_type = VAR_STRING;
8949 v.di_tv.vval.v_string = vim_strsave(s);
8950 retvar = get_tv_string_chk(&argvars[1]);
8951 if (retvar != NULL)
8952 set_var(retvar, &v.di_tv, FALSE);
8953 vim_free(v.di_tv.vval.v_string);
8954 }
8955#else
8956 rettv->vval.v_number = -1;
8957#endif
8958}
8959
8960 static void
8961f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
8962{
8963 char_u *r = NULL;
8964
8965#ifdef FEAT_CLIENTSERVER
8966 char_u *serverid = get_tv_string_chk(&argvars[0]);
8967
8968 if (serverid != NULL && !check_restricted() && !check_secure())
8969 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008970 int timeout = 0;
Bram Moolenaar1662ce12017-03-19 21:47:50 +01008971# ifdef WIN32
8972 /* The server's HWND is encoded in the 'id' parameter */
8973 long_u n = 0;
8974# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008975
8976 if (argvars[1].v_type != VAR_UNKNOWN)
8977 timeout = get_tv_number(&argvars[1]);
8978
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008979# ifdef WIN32
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008980 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
8981 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008982 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008983 if (r == NULL)
8984# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008985 if (check_connection() == FAIL
8986 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
8987 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008988# endif
8989 EMSG(_("E277: Unable to read a server reply"));
8990 }
8991#endif
8992 rettv->v_type = VAR_STRING;
8993 rettv->vval.v_string = r;
8994}
8995
8996/*
8997 * "remote_send()" function
8998 */
8999 static void
9000f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9001{
9002 rettv->v_type = VAR_STRING;
9003 rettv->vval.v_string = NULL;
9004#ifdef FEAT_CLIENTSERVER
9005 remote_common(argvars, rettv, FALSE);
9006#endif
9007}
9008
9009/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009010 * "remote_startserver()" function
9011 */
9012 static void
9013f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9014{
9015#ifdef FEAT_CLIENTSERVER
9016 char_u *server = get_tv_string_chk(&argvars[0]);
9017
9018 if (server == NULL)
9019 return; /* type error; errmsg already given */
9020 if (serverName != NULL)
9021 EMSG(_("E941: already started a server"));
9022 else
9023 {
9024# ifdef FEAT_X11
9025 if (check_connection() == OK)
9026 serverRegisterName(X_DISPLAY, server);
9027# else
9028 serverSetName(server);
9029# endif
9030 }
9031#else
9032 EMSG(_("E942: +clientserver feature not available"));
9033#endif
9034}
9035
9036/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009037 * "remove()" function
9038 */
9039 static void
9040f_remove(typval_T *argvars, typval_T *rettv)
9041{
9042 list_T *l;
9043 listitem_T *item, *item2;
9044 listitem_T *li;
9045 long idx;
9046 long end;
9047 char_u *key;
9048 dict_T *d;
9049 dictitem_T *di;
9050 char_u *arg_errmsg = (char_u *)N_("remove() argument");
9051
9052 if (argvars[0].v_type == VAR_DICT)
9053 {
9054 if (argvars[2].v_type != VAR_UNKNOWN)
9055 EMSG2(_(e_toomanyarg), "remove()");
9056 else if ((d = argvars[0].vval.v_dict) != NULL
9057 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
9058 {
9059 key = get_tv_string_chk(&argvars[1]);
9060 if (key != NULL)
9061 {
9062 di = dict_find(d, key, -1);
9063 if (di == NULL)
9064 EMSG2(_(e_dictkey), key);
9065 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
9066 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
9067 {
9068 *rettv = di->di_tv;
9069 init_tv(&di->di_tv);
9070 dictitem_remove(d, di);
9071 }
9072 }
9073 }
9074 }
9075 else if (argvars[0].v_type != VAR_LIST)
9076 EMSG2(_(e_listdictarg), "remove()");
9077 else if ((l = argvars[0].vval.v_list) != NULL
9078 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
9079 {
9080 int error = FALSE;
9081
9082 idx = (long)get_tv_number_chk(&argvars[1], &error);
9083 if (error)
9084 ; /* type error: do nothing, errmsg already given */
9085 else if ((item = list_find(l, idx)) == NULL)
9086 EMSGN(_(e_listidx), idx);
9087 else
9088 {
9089 if (argvars[2].v_type == VAR_UNKNOWN)
9090 {
9091 /* Remove one item, return its value. */
9092 vimlist_remove(l, item, item);
9093 *rettv = item->li_tv;
9094 vim_free(item);
9095 }
9096 else
9097 {
9098 /* Remove range of items, return list with values. */
9099 end = (long)get_tv_number_chk(&argvars[2], &error);
9100 if (error)
9101 ; /* type error: do nothing */
9102 else if ((item2 = list_find(l, end)) == NULL)
9103 EMSGN(_(e_listidx), end);
9104 else
9105 {
9106 int cnt = 0;
9107
9108 for (li = item; li != NULL; li = li->li_next)
9109 {
9110 ++cnt;
9111 if (li == item2)
9112 break;
9113 }
9114 if (li == NULL) /* didn't find "item2" after "item" */
9115 EMSG(_(e_invrange));
9116 else
9117 {
9118 vimlist_remove(l, item, item2);
9119 if (rettv_list_alloc(rettv) == OK)
9120 {
9121 l = rettv->vval.v_list;
9122 l->lv_first = item;
9123 l->lv_last = item2;
9124 item->li_prev = NULL;
9125 item2->li_next = NULL;
9126 l->lv_len = cnt;
9127 }
9128 }
9129 }
9130 }
9131 }
9132 }
9133}
9134
9135/*
9136 * "rename({from}, {to})" function
9137 */
9138 static void
9139f_rename(typval_T *argvars, typval_T *rettv)
9140{
9141 char_u buf[NUMBUFLEN];
9142
9143 if (check_restricted() || check_secure())
9144 rettv->vval.v_number = -1;
9145 else
9146 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
9147 get_tv_string_buf(&argvars[1], buf));
9148}
9149
9150/*
9151 * "repeat()" function
9152 */
9153 static void
9154f_repeat(typval_T *argvars, typval_T *rettv)
9155{
9156 char_u *p;
9157 int n;
9158 int slen;
9159 int len;
9160 char_u *r;
9161 int i;
9162
9163 n = (int)get_tv_number(&argvars[1]);
9164 if (argvars[0].v_type == VAR_LIST)
9165 {
9166 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9167 while (n-- > 0)
9168 if (list_extend(rettv->vval.v_list,
9169 argvars[0].vval.v_list, NULL) == FAIL)
9170 break;
9171 }
9172 else
9173 {
9174 p = get_tv_string(&argvars[0]);
9175 rettv->v_type = VAR_STRING;
9176 rettv->vval.v_string = NULL;
9177
9178 slen = (int)STRLEN(p);
9179 len = slen * n;
9180 if (len <= 0)
9181 return;
9182
9183 r = alloc(len + 1);
9184 if (r != NULL)
9185 {
9186 for (i = 0; i < n; i++)
9187 mch_memmove(r + i * slen, p, (size_t)slen);
9188 r[len] = NUL;
9189 }
9190
9191 rettv->vval.v_string = r;
9192 }
9193}
9194
9195/*
9196 * "resolve()" function
9197 */
9198 static void
9199f_resolve(typval_T *argvars, typval_T *rettv)
9200{
9201 char_u *p;
9202#ifdef HAVE_READLINK
9203 char_u *buf = NULL;
9204#endif
9205
9206 p = get_tv_string(&argvars[0]);
9207#ifdef FEAT_SHORTCUT
9208 {
9209 char_u *v = NULL;
9210
9211 v = mch_resolve_shortcut(p);
9212 if (v != NULL)
9213 rettv->vval.v_string = v;
9214 else
9215 rettv->vval.v_string = vim_strsave(p);
9216 }
9217#else
9218# ifdef HAVE_READLINK
9219 {
9220 char_u *cpy;
9221 int len;
9222 char_u *remain = NULL;
9223 char_u *q;
9224 int is_relative_to_current = FALSE;
9225 int has_trailing_pathsep = FALSE;
9226 int limit = 100;
9227
9228 p = vim_strsave(p);
9229
9230 if (p[0] == '.' && (vim_ispathsep(p[1])
9231 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9232 is_relative_to_current = TRUE;
9233
9234 len = STRLEN(p);
9235 if (len > 0 && after_pathsep(p, p + len))
9236 {
9237 has_trailing_pathsep = TRUE;
9238 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9239 }
9240
9241 q = getnextcomp(p);
9242 if (*q != NUL)
9243 {
9244 /* Separate the first path component in "p", and keep the
9245 * remainder (beginning with the path separator). */
9246 remain = vim_strsave(q - 1);
9247 q[-1] = NUL;
9248 }
9249
9250 buf = alloc(MAXPATHL + 1);
9251 if (buf == NULL)
9252 goto fail;
9253
9254 for (;;)
9255 {
9256 for (;;)
9257 {
9258 len = readlink((char *)p, (char *)buf, MAXPATHL);
9259 if (len <= 0)
9260 break;
9261 buf[len] = NUL;
9262
9263 if (limit-- == 0)
9264 {
9265 vim_free(p);
9266 vim_free(remain);
9267 EMSG(_("E655: Too many symbolic links (cycle?)"));
9268 rettv->vval.v_string = NULL;
9269 goto fail;
9270 }
9271
9272 /* Ensure that the result will have a trailing path separator
9273 * if the argument has one. */
9274 if (remain == NULL && has_trailing_pathsep)
9275 add_pathsep(buf);
9276
9277 /* Separate the first path component in the link value and
9278 * concatenate the remainders. */
9279 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9280 if (*q != NUL)
9281 {
9282 if (remain == NULL)
9283 remain = vim_strsave(q - 1);
9284 else
9285 {
9286 cpy = concat_str(q - 1, remain);
9287 if (cpy != NULL)
9288 {
9289 vim_free(remain);
9290 remain = cpy;
9291 }
9292 }
9293 q[-1] = NUL;
9294 }
9295
9296 q = gettail(p);
9297 if (q > p && *q == NUL)
9298 {
9299 /* Ignore trailing path separator. */
9300 q[-1] = NUL;
9301 q = gettail(p);
9302 }
9303 if (q > p && !mch_isFullName(buf))
9304 {
9305 /* symlink is relative to directory of argument */
9306 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
9307 if (cpy != NULL)
9308 {
9309 STRCPY(cpy, p);
9310 STRCPY(gettail(cpy), buf);
9311 vim_free(p);
9312 p = cpy;
9313 }
9314 }
9315 else
9316 {
9317 vim_free(p);
9318 p = vim_strsave(buf);
9319 }
9320 }
9321
9322 if (remain == NULL)
9323 break;
9324
9325 /* Append the first path component of "remain" to "p". */
9326 q = getnextcomp(remain + 1);
9327 len = q - remain - (*q != NUL);
9328 cpy = vim_strnsave(p, STRLEN(p) + len);
9329 if (cpy != NULL)
9330 {
9331 STRNCAT(cpy, remain, len);
9332 vim_free(p);
9333 p = cpy;
9334 }
9335 /* Shorten "remain". */
9336 if (*q != NUL)
9337 STRMOVE(remain, q - 1);
9338 else
Bram Moolenaard23a8232018-02-10 18:45:26 +01009339 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009340 }
9341
9342 /* If the result is a relative path name, make it explicitly relative to
9343 * the current directory if and only if the argument had this form. */
9344 if (!vim_ispathsep(*p))
9345 {
9346 if (is_relative_to_current
9347 && *p != NUL
9348 && !(p[0] == '.'
9349 && (p[1] == NUL
9350 || vim_ispathsep(p[1])
9351 || (p[1] == '.'
9352 && (p[2] == NUL
9353 || vim_ispathsep(p[2]))))))
9354 {
9355 /* Prepend "./". */
9356 cpy = concat_str((char_u *)"./", p);
9357 if (cpy != NULL)
9358 {
9359 vim_free(p);
9360 p = cpy;
9361 }
9362 }
9363 else if (!is_relative_to_current)
9364 {
9365 /* Strip leading "./". */
9366 q = p;
9367 while (q[0] == '.' && vim_ispathsep(q[1]))
9368 q += 2;
9369 if (q > p)
9370 STRMOVE(p, p + 2);
9371 }
9372 }
9373
9374 /* Ensure that the result will have no trailing path separator
9375 * if the argument had none. But keep "/" or "//". */
9376 if (!has_trailing_pathsep)
9377 {
9378 q = p + STRLEN(p);
9379 if (after_pathsep(p, q))
9380 *gettail_sep(p) = NUL;
9381 }
9382
9383 rettv->vval.v_string = p;
9384 }
9385# else
9386 rettv->vval.v_string = vim_strsave(p);
9387# endif
9388#endif
9389
9390 simplify_filename(rettv->vval.v_string);
9391
9392#ifdef HAVE_READLINK
9393fail:
9394 vim_free(buf);
9395#endif
9396 rettv->v_type = VAR_STRING;
9397}
9398
9399/*
9400 * "reverse({list})" function
9401 */
9402 static void
9403f_reverse(typval_T *argvars, typval_T *rettv)
9404{
9405 list_T *l;
9406 listitem_T *li, *ni;
9407
9408 if (argvars[0].v_type != VAR_LIST)
9409 EMSG2(_(e_listarg), "reverse()");
9410 else if ((l = argvars[0].vval.v_list) != NULL
9411 && !tv_check_lock(l->lv_lock,
9412 (char_u *)N_("reverse() argument"), TRUE))
9413 {
9414 li = l->lv_last;
9415 l->lv_first = l->lv_last = NULL;
9416 l->lv_len = 0;
9417 while (li != NULL)
9418 {
9419 ni = li->li_prev;
9420 list_append(l, li);
9421 li = ni;
9422 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009423 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009424 l->lv_idx = l->lv_len - l->lv_idx - 1;
9425 }
9426}
9427
9428#define SP_NOMOVE 0x01 /* don't move cursor */
9429#define SP_REPEAT 0x02 /* repeat to find outer pair */
9430#define SP_RETCOUNT 0x04 /* return matchcount */
9431#define SP_SETPCMARK 0x08 /* set previous context mark */
9432#define SP_START 0x10 /* accept match at start position */
9433#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9434#define SP_END 0x40 /* leave cursor at end of match */
9435#define SP_COLUMN 0x80 /* start at cursor column */
9436
9437static int get_search_arg(typval_T *varp, int *flagsp);
9438
9439/*
9440 * Get flags for a search function.
9441 * Possibly sets "p_ws".
9442 * Returns BACKWARD, FORWARD or zero (for an error).
9443 */
9444 static int
9445get_search_arg(typval_T *varp, int *flagsp)
9446{
9447 int dir = FORWARD;
9448 char_u *flags;
9449 char_u nbuf[NUMBUFLEN];
9450 int mask;
9451
9452 if (varp->v_type != VAR_UNKNOWN)
9453 {
9454 flags = get_tv_string_buf_chk(varp, nbuf);
9455 if (flags == NULL)
9456 return 0; /* type error; errmsg already given */
9457 while (*flags != NUL)
9458 {
9459 switch (*flags)
9460 {
9461 case 'b': dir = BACKWARD; break;
9462 case 'w': p_ws = TRUE; break;
9463 case 'W': p_ws = FALSE; break;
9464 default: mask = 0;
9465 if (flagsp != NULL)
9466 switch (*flags)
9467 {
9468 case 'c': mask = SP_START; break;
9469 case 'e': mask = SP_END; break;
9470 case 'm': mask = SP_RETCOUNT; break;
9471 case 'n': mask = SP_NOMOVE; break;
9472 case 'p': mask = SP_SUBPAT; break;
9473 case 'r': mask = SP_REPEAT; break;
9474 case 's': mask = SP_SETPCMARK; break;
9475 case 'z': mask = SP_COLUMN; break;
9476 }
9477 if (mask == 0)
9478 {
9479 EMSG2(_(e_invarg2), flags);
9480 dir = 0;
9481 }
9482 else
9483 *flagsp |= mask;
9484 }
9485 if (dir == 0)
9486 break;
9487 ++flags;
9488 }
9489 }
9490 return dir;
9491}
9492
9493/*
9494 * Shared by search() and searchpos() functions.
9495 */
9496 static int
9497search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9498{
9499 int flags;
9500 char_u *pat;
9501 pos_T pos;
9502 pos_T save_cursor;
9503 int save_p_ws = p_ws;
9504 int dir;
9505 int retval = 0; /* default: FAIL */
9506 long lnum_stop = 0;
9507 proftime_T tm;
9508#ifdef FEAT_RELTIME
9509 long time_limit = 0;
9510#endif
9511 int options = SEARCH_KEEP;
9512 int subpatnum;
9513
9514 pat = get_tv_string(&argvars[0]);
9515 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
9516 if (dir == 0)
9517 goto theend;
9518 flags = *flagsp;
9519 if (flags & SP_START)
9520 options |= SEARCH_START;
9521 if (flags & SP_END)
9522 options |= SEARCH_END;
9523 if (flags & SP_COLUMN)
9524 options |= SEARCH_COL;
9525
9526 /* Optional arguments: line number to stop searching and timeout. */
9527 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9528 {
9529 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
9530 if (lnum_stop < 0)
9531 goto theend;
9532#ifdef FEAT_RELTIME
9533 if (argvars[3].v_type != VAR_UNKNOWN)
9534 {
9535 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
9536 if (time_limit < 0)
9537 goto theend;
9538 }
9539#endif
9540 }
9541
9542#ifdef FEAT_RELTIME
9543 /* Set the time limit, if there is one. */
9544 profile_setlimit(time_limit, &tm);
9545#endif
9546
9547 /*
9548 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
9549 * Check to make sure only those flags are set.
9550 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
9551 * flags cannot be set. Check for that condition also.
9552 */
9553 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
9554 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9555 {
9556 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
9557 goto theend;
9558 }
9559
9560 pos = save_cursor = curwin->w_cursor;
9561 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009562 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009563 if (subpatnum != FAIL)
9564 {
9565 if (flags & SP_SUBPAT)
9566 retval = subpatnum;
9567 else
9568 retval = pos.lnum;
9569 if (flags & SP_SETPCMARK)
9570 setpcmark();
9571 curwin->w_cursor = pos;
9572 if (match_pos != NULL)
9573 {
9574 /* Store the match cursor position */
9575 match_pos->lnum = pos.lnum;
9576 match_pos->col = pos.col + 1;
9577 }
9578 /* "/$" will put the cursor after the end of the line, may need to
9579 * correct that here */
9580 check_cursor();
9581 }
9582
9583 /* If 'n' flag is used: restore cursor position. */
9584 if (flags & SP_NOMOVE)
9585 curwin->w_cursor = save_cursor;
9586 else
9587 curwin->w_set_curswant = TRUE;
9588theend:
9589 p_ws = save_p_ws;
9590
9591 return retval;
9592}
9593
9594#ifdef FEAT_FLOAT
9595
9596/*
9597 * round() is not in C90, use ceil() or floor() instead.
9598 */
9599 float_T
9600vim_round(float_T f)
9601{
9602 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
9603}
9604
9605/*
9606 * "round({float})" function
9607 */
9608 static void
9609f_round(typval_T *argvars, typval_T *rettv)
9610{
9611 float_T f = 0.0;
9612
9613 rettv->v_type = VAR_FLOAT;
9614 if (get_float_arg(argvars, &f) == OK)
9615 rettv->vval.v_float = vim_round(f);
9616 else
9617 rettv->vval.v_float = 0.0;
9618}
9619#endif
9620
9621/*
9622 * "screenattr()" function
9623 */
9624 static void
9625f_screenattr(typval_T *argvars, typval_T *rettv)
9626{
9627 int row;
9628 int col;
9629 int c;
9630
9631 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9632 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9633 if (row < 0 || row >= screen_Rows
9634 || col < 0 || col >= screen_Columns)
9635 c = -1;
9636 else
9637 c = ScreenAttrs[LineOffset[row] + col];
9638 rettv->vval.v_number = c;
9639}
9640
9641/*
9642 * "screenchar()" function
9643 */
9644 static void
9645f_screenchar(typval_T *argvars, typval_T *rettv)
9646{
9647 int row;
9648 int col;
9649 int off;
9650 int c;
9651
9652 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9653 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9654 if (row < 0 || row >= screen_Rows
9655 || col < 0 || col >= screen_Columns)
9656 c = -1;
9657 else
9658 {
9659 off = LineOffset[row] + col;
9660#ifdef FEAT_MBYTE
9661 if (enc_utf8 && ScreenLinesUC[off] != 0)
9662 c = ScreenLinesUC[off];
9663 else
9664#endif
9665 c = ScreenLines[off];
9666 }
9667 rettv->vval.v_number = c;
9668}
9669
9670/*
9671 * "screencol()" function
9672 *
9673 * First column is 1 to be consistent with virtcol().
9674 */
9675 static void
9676f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
9677{
9678 rettv->vval.v_number = screen_screencol() + 1;
9679}
9680
9681/*
9682 * "screenrow()" function
9683 */
9684 static void
9685f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
9686{
9687 rettv->vval.v_number = screen_screenrow() + 1;
9688}
9689
9690/*
9691 * "search()" function
9692 */
9693 static void
9694f_search(typval_T *argvars, typval_T *rettv)
9695{
9696 int flags = 0;
9697
9698 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
9699}
9700
9701/*
9702 * "searchdecl()" function
9703 */
9704 static void
9705f_searchdecl(typval_T *argvars, typval_T *rettv)
9706{
9707 int locally = 1;
9708 int thisblock = 0;
9709 int error = FALSE;
9710 char_u *name;
9711
9712 rettv->vval.v_number = 1; /* default: FAIL */
9713
9714 name = get_tv_string_chk(&argvars[0]);
9715 if (argvars[1].v_type != VAR_UNKNOWN)
9716 {
9717 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
9718 if (!error && argvars[2].v_type != VAR_UNKNOWN)
9719 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
9720 }
9721 if (!error && name != NULL)
9722 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
9723 locally, thisblock, SEARCH_KEEP) == FAIL;
9724}
9725
9726/*
9727 * Used by searchpair() and searchpairpos()
9728 */
9729 static int
9730searchpair_cmn(typval_T *argvars, pos_T *match_pos)
9731{
9732 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01009733 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009734 int save_p_ws = p_ws;
9735 int dir;
9736 int flags = 0;
9737 char_u nbuf1[NUMBUFLEN];
9738 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009739 int retval = 0; /* default: FAIL */
9740 long lnum_stop = 0;
9741 long time_limit = 0;
9742
9743 /* Get the three pattern arguments: start, middle, end. */
9744 spat = get_tv_string_chk(&argvars[0]);
9745 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
9746 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
9747 if (spat == NULL || mpat == NULL || epat == NULL)
9748 goto theend; /* type error */
9749
9750 /* Handle the optional fourth argument: flags */
9751 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
9752 if (dir == 0)
9753 goto theend;
9754
9755 /* Don't accept SP_END or SP_SUBPAT.
9756 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
9757 */
9758 if ((flags & (SP_END | SP_SUBPAT)) != 0
9759 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9760 {
9761 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
9762 goto theend;
9763 }
9764
9765 /* Using 'r' implies 'W', otherwise it doesn't work. */
9766 if (flags & SP_REPEAT)
9767 p_ws = FALSE;
9768
9769 /* Optional fifth argument: skip expression */
9770 if (argvars[3].v_type == VAR_UNKNOWN
9771 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01009772 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009773 else
9774 {
Bram Moolenaar48570482017-10-30 21:48:41 +01009775 skip = &argvars[4];
9776 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
9777 && skip->v_type != VAR_STRING)
9778 {
9779 /* Type error */
9780 goto theend;
9781 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009782 if (argvars[5].v_type != VAR_UNKNOWN)
9783 {
9784 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
9785 if (lnum_stop < 0)
9786 goto theend;
9787#ifdef FEAT_RELTIME
9788 if (argvars[6].v_type != VAR_UNKNOWN)
9789 {
9790 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
9791 if (time_limit < 0)
9792 goto theend;
9793 }
9794#endif
9795 }
9796 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009797
9798 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
9799 match_pos, lnum_stop, time_limit);
9800
9801theend:
9802 p_ws = save_p_ws;
9803
9804 return retval;
9805}
9806
9807/*
9808 * "searchpair()" function
9809 */
9810 static void
9811f_searchpair(typval_T *argvars, typval_T *rettv)
9812{
9813 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
9814}
9815
9816/*
9817 * "searchpairpos()" function
9818 */
9819 static void
9820f_searchpairpos(typval_T *argvars, typval_T *rettv)
9821{
9822 pos_T match_pos;
9823 int lnum = 0;
9824 int col = 0;
9825
9826 if (rettv_list_alloc(rettv) == FAIL)
9827 return;
9828
9829 if (searchpair_cmn(argvars, &match_pos) > 0)
9830 {
9831 lnum = match_pos.lnum;
9832 col = match_pos.col;
9833 }
9834
9835 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9836 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9837}
9838
9839/*
9840 * Search for a start/middle/end thing.
9841 * Used by searchpair(), see its documentation for the details.
9842 * Returns 0 or -1 for no match,
9843 */
9844 long
9845do_searchpair(
9846 char_u *spat, /* start pattern */
9847 char_u *mpat, /* middle pattern */
9848 char_u *epat, /* end pattern */
9849 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +01009850 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009851 int flags, /* SP_SETPCMARK and other SP_ values */
9852 pos_T *match_pos,
9853 linenr_T lnum_stop, /* stop at this line if not zero */
9854 long time_limit UNUSED) /* stop after this many msec */
9855{
9856 char_u *save_cpo;
9857 char_u *pat, *pat2 = NULL, *pat3 = NULL;
9858 long retval = 0;
9859 pos_T pos;
9860 pos_T firstpos;
9861 pos_T foundpos;
9862 pos_T save_cursor;
9863 pos_T save_pos;
9864 int n;
9865 int r;
9866 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01009867 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009868 int err;
9869 int options = SEARCH_KEEP;
9870 proftime_T tm;
9871
9872 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
9873 save_cpo = p_cpo;
9874 p_cpo = empty_option;
9875
9876#ifdef FEAT_RELTIME
9877 /* Set the time limit, if there is one. */
9878 profile_setlimit(time_limit, &tm);
9879#endif
9880
9881 /* Make two search patterns: start/end (pat2, for in nested pairs) and
9882 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009883 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
9884 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009885 if (pat2 == NULL || pat3 == NULL)
9886 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009887 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009888 if (*mpat == NUL)
9889 STRCPY(pat3, pat2);
9890 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009891 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009892 spat, epat, mpat);
9893 if (flags & SP_START)
9894 options |= SEARCH_START;
9895
Bram Moolenaar48570482017-10-30 21:48:41 +01009896 if (skip != NULL)
9897 {
9898 /* Empty string means to not use the skip expression. */
9899 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
9900 use_skip = skip->vval.v_string != NULL
9901 && *skip->vval.v_string != NUL;
9902 }
9903
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009904 save_cursor = curwin->w_cursor;
9905 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009906 CLEAR_POS(&firstpos);
9907 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009908 pat = pat3;
9909 for (;;)
9910 {
9911 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009912 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009913 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009914 /* didn't find it or found the first match again: FAIL */
9915 break;
9916
9917 if (firstpos.lnum == 0)
9918 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009919 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009920 {
9921 /* Found the same position again. Can happen with a pattern that
9922 * has "\zs" at the end and searching backwards. Advance one
9923 * character and try again. */
9924 if (dir == BACKWARD)
9925 decl(&pos);
9926 else
9927 incl(&pos);
9928 }
9929 foundpos = pos;
9930
9931 /* clear the start flag to avoid getting stuck here */
9932 options &= ~SEARCH_START;
9933
9934 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +01009935 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009936 {
9937 save_pos = curwin->w_cursor;
9938 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01009939 err = FALSE;
9940 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009941 curwin->w_cursor = save_pos;
9942 if (err)
9943 {
9944 /* Evaluating {skip} caused an error, break here. */
9945 curwin->w_cursor = save_cursor;
9946 retval = -1;
9947 break;
9948 }
9949 if (r)
9950 continue;
9951 }
9952
9953 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
9954 {
9955 /* Found end when searching backwards or start when searching
9956 * forward: nested pair. */
9957 ++nest;
9958 pat = pat2; /* nested, don't search for middle */
9959 }
9960 else
9961 {
9962 /* Found end when searching forward or start when searching
9963 * backward: end of (nested) pair; or found middle in outer pair. */
9964 if (--nest == 1)
9965 pat = pat3; /* outer level, search for middle */
9966 }
9967
9968 if (nest == 0)
9969 {
9970 /* Found the match: return matchcount or line number. */
9971 if (flags & SP_RETCOUNT)
9972 ++retval;
9973 else
9974 retval = pos.lnum;
9975 if (flags & SP_SETPCMARK)
9976 setpcmark();
9977 curwin->w_cursor = pos;
9978 if (!(flags & SP_REPEAT))
9979 break;
9980 nest = 1; /* search for next unmatched */
9981 }
9982 }
9983
9984 if (match_pos != NULL)
9985 {
9986 /* Store the match cursor position */
9987 match_pos->lnum = curwin->w_cursor.lnum;
9988 match_pos->col = curwin->w_cursor.col + 1;
9989 }
9990
9991 /* If 'n' flag is used or search failed: restore cursor position. */
9992 if ((flags & SP_NOMOVE) || retval == 0)
9993 curwin->w_cursor = save_cursor;
9994
9995theend:
9996 vim_free(pat2);
9997 vim_free(pat3);
9998 if (p_cpo == empty_option)
9999 p_cpo = save_cpo;
10000 else
10001 /* Darn, evaluating the {skip} expression changed the value. */
10002 free_string_option(save_cpo);
10003
10004 return retval;
10005}
10006
10007/*
10008 * "searchpos()" function
10009 */
10010 static void
10011f_searchpos(typval_T *argvars, typval_T *rettv)
10012{
10013 pos_T match_pos;
10014 int lnum = 0;
10015 int col = 0;
10016 int n;
10017 int flags = 0;
10018
10019 if (rettv_list_alloc(rettv) == FAIL)
10020 return;
10021
10022 n = search_cmn(argvars, &match_pos, &flags);
10023 if (n > 0)
10024 {
10025 lnum = match_pos.lnum;
10026 col = match_pos.col;
10027 }
10028
10029 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10030 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10031 if (flags & SP_SUBPAT)
10032 list_append_number(rettv->vval.v_list, (varnumber_T)n);
10033}
10034
10035 static void
10036f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
10037{
10038#ifdef FEAT_CLIENTSERVER
10039 char_u buf[NUMBUFLEN];
10040 char_u *server = get_tv_string_chk(&argvars[0]);
10041 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
10042
10043 rettv->vval.v_number = -1;
10044 if (server == NULL || reply == NULL)
10045 return;
10046 if (check_restricted() || check_secure())
10047 return;
10048# ifdef FEAT_X11
10049 if (check_connection() == FAIL)
10050 return;
10051# endif
10052
10053 if (serverSendReply(server, reply) < 0)
10054 {
10055 EMSG(_("E258: Unable to send to client"));
10056 return;
10057 }
10058 rettv->vval.v_number = 0;
10059#else
10060 rettv->vval.v_number = -1;
10061#endif
10062}
10063
10064 static void
10065f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10066{
10067 char_u *r = NULL;
10068
10069#ifdef FEAT_CLIENTSERVER
10070# ifdef WIN32
10071 r = serverGetVimNames();
10072# else
10073 make_connection();
10074 if (X_DISPLAY != NULL)
10075 r = serverGetVimNames(X_DISPLAY);
10076# endif
10077#endif
10078 rettv->v_type = VAR_STRING;
10079 rettv->vval.v_string = r;
10080}
10081
10082/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010083 * Set line or list of lines in buffer "buf".
10084 */
10085 static void
10086set_buffer_lines(buf_T *buf, linenr_T lnum, typval_T *lines, typval_T *rettv)
10087{
10088 char_u *line = NULL;
10089 list_T *l = NULL;
10090 listitem_T *li = NULL;
10091 long added = 0;
10092 linenr_T lcount;
Bram Moolenaar0c4dc882017-11-06 21:32:54 +010010093 buf_T *curbuf_save = NULL;
10094 win_T *curwin_save = NULL;
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010095 int is_curbuf = buf == curbuf;
10096
Bram Moolenaar9d954202017-09-04 20:34:19 +020010097 /* When using the current buffer ml_mfp will be set if needed. Useful when
10098 * setline() is used on startup. For other buffers the buffer must be
10099 * loaded. */
10100 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010101 {
10102 rettv->vval.v_number = 1; /* FAIL */
10103 return;
10104 }
10105
Bram Moolenaar0c4dc882017-11-06 21:32:54 +010010106 if (!is_curbuf)
10107 {
10108 wininfo_T *wip;
10109
10110 curbuf_save = curbuf;
10111 curwin_save = curwin;
10112 curbuf = buf;
10113 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
10114 {
10115 if (wip->wi_win != NULL)
10116 {
10117 curwin = wip->wi_win;
10118 break;
10119 }
10120 }
10121 }
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010122
10123 lcount = curbuf->b_ml.ml_line_count;
10124
10125 if (lines->v_type == VAR_LIST)
10126 {
10127 l = lines->vval.v_list;
10128 li = l->lv_first;
10129 }
10130 else
10131 line = get_tv_string_chk(lines);
10132
10133 /* default result is zero == OK */
10134 for (;;)
10135 {
10136 if (l != NULL)
10137 {
10138 /* list argument, get next string */
10139 if (li == NULL)
10140 break;
10141 line = get_tv_string_chk(&li->li_tv);
10142 li = li->li_next;
10143 }
10144
10145 rettv->vval.v_number = 1; /* FAIL */
10146 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
10147 break;
10148
10149 /* When coming here from Insert mode, sync undo, so that this can be
10150 * undone separately from what was previously inserted. */
10151 if (u_sync_once == 2)
10152 {
10153 u_sync_once = 1; /* notify that u_sync() was called */
10154 u_sync(TRUE);
10155 }
10156
10157 if (lnum <= curbuf->b_ml.ml_line_count)
10158 {
10159 /* existing line, replace it */
10160 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
10161 {
10162 changed_bytes(lnum, 0);
10163 if (is_curbuf && lnum == curwin->w_cursor.lnum)
10164 check_cursor_col();
10165 rettv->vval.v_number = 0; /* OK */
10166 }
10167 }
10168 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
10169 {
10170 /* lnum is one past the last line, append the line */
10171 ++added;
10172 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
10173 rettv->vval.v_number = 0; /* OK */
10174 }
10175
10176 if (l == NULL) /* only one string argument */
10177 break;
10178 ++lnum;
10179 }
10180
10181 if (added > 0)
10182 appended_lines_mark(lcount, added);
10183
Bram Moolenaar0c4dc882017-11-06 21:32:54 +010010184 if (!is_curbuf)
10185 {
10186 curbuf = curbuf_save;
10187 curwin = curwin_save;
10188 }
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010189}
10190
10191/*
10192 * "setbufline()" function
10193 */
10194 static void
10195f_setbufline(argvars, rettv)
10196 typval_T *argvars;
10197 typval_T *rettv;
10198{
10199 linenr_T lnum;
10200 buf_T *buf;
10201
10202 buf = get_buf_tv(&argvars[0], FALSE);
10203 if (buf == NULL)
10204 rettv->vval.v_number = 1; /* FAIL */
10205 else
10206 {
10207 lnum = get_tv_lnum_buf(&argvars[1], buf);
10208
10209 set_buffer_lines(buf, lnum, &argvars[2], rettv);
10210 }
10211}
10212
10213/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010214 * "setbufvar()" function
10215 */
10216 static void
10217f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10218{
10219 buf_T *buf;
10220 char_u *varname, *bufvarname;
10221 typval_T *varp;
10222 char_u nbuf[NUMBUFLEN];
10223
10224 if (check_restricted() || check_secure())
10225 return;
10226 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10227 varname = get_tv_string_chk(&argvars[1]);
10228 buf = get_buf_tv(&argvars[0], FALSE);
10229 varp = &argvars[2];
10230
10231 if (buf != NULL && varname != NULL && varp != NULL)
10232 {
10233 if (*varname == '&')
10234 {
10235 long numval;
10236 char_u *strval;
10237 int error = FALSE;
10238 aco_save_T aco;
10239
10240 /* set curbuf to be our buf, temporarily */
10241 aucmd_prepbuf(&aco, buf);
10242
10243 ++varname;
10244 numval = (long)get_tv_number_chk(varp, &error);
10245 strval = get_tv_string_buf_chk(varp, nbuf);
10246 if (!error && strval != NULL)
10247 set_option_value(varname, numval, strval, OPT_LOCAL);
10248
10249 /* reset notion of buffer */
10250 aucmd_restbuf(&aco);
10251 }
10252 else
10253 {
10254 buf_T *save_curbuf = curbuf;
10255
10256 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
10257 if (bufvarname != NULL)
10258 {
10259 curbuf = buf;
10260 STRCPY(bufvarname, "b:");
10261 STRCPY(bufvarname + 2, varname);
10262 set_var(bufvarname, varp, TRUE);
10263 vim_free(bufvarname);
10264 curbuf = save_curbuf;
10265 }
10266 }
10267 }
10268}
10269
10270 static void
10271f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10272{
10273 dict_T *d;
10274 dictitem_T *di;
10275 char_u *csearch;
10276
10277 if (argvars[0].v_type != VAR_DICT)
10278 {
10279 EMSG(_(e_dictreq));
10280 return;
10281 }
10282
10283 if ((d = argvars[0].vval.v_dict) != NULL)
10284 {
10285 csearch = get_dict_string(d, (char_u *)"char", FALSE);
10286 if (csearch != NULL)
10287 {
10288#ifdef FEAT_MBYTE
10289 if (enc_utf8)
10290 {
10291 int pcc[MAX_MCO];
10292 int c = utfc_ptr2char(csearch, pcc);
10293
10294 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10295 }
10296 else
10297#endif
10298 set_last_csearch(PTR2CHAR(csearch),
10299 csearch, MB_PTR2LEN(csearch));
10300 }
10301
10302 di = dict_find(d, (char_u *)"forward", -1);
10303 if (di != NULL)
10304 set_csearch_direction((int)get_tv_number(&di->di_tv)
10305 ? FORWARD : BACKWARD);
10306
10307 di = dict_find(d, (char_u *)"until", -1);
10308 if (di != NULL)
10309 set_csearch_until(!!get_tv_number(&di->di_tv));
10310 }
10311}
10312
10313/*
10314 * "setcmdpos()" function
10315 */
10316 static void
10317f_setcmdpos(typval_T *argvars, typval_T *rettv)
10318{
10319 int pos = (int)get_tv_number(&argvars[0]) - 1;
10320
10321 if (pos >= 0)
10322 rettv->vval.v_number = set_cmdline_pos(pos);
10323}
10324
10325/*
10326 * "setfperm({fname}, {mode})" function
10327 */
10328 static void
10329f_setfperm(typval_T *argvars, typval_T *rettv)
10330{
10331 char_u *fname;
10332 char_u modebuf[NUMBUFLEN];
10333 char_u *mode_str;
10334 int i;
10335 int mask;
10336 int mode = 0;
10337
10338 rettv->vval.v_number = 0;
10339 fname = get_tv_string_chk(&argvars[0]);
10340 if (fname == NULL)
10341 return;
10342 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
10343 if (mode_str == NULL)
10344 return;
10345 if (STRLEN(mode_str) != 9)
10346 {
10347 EMSG2(_(e_invarg2), mode_str);
10348 return;
10349 }
10350
10351 mask = 1;
10352 for (i = 8; i >= 0; --i)
10353 {
10354 if (mode_str[i] != '-')
10355 mode |= mask;
10356 mask = mask << 1;
10357 }
10358 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10359}
10360
10361/*
10362 * "setline()" function
10363 */
10364 static void
10365f_setline(typval_T *argvars, typval_T *rettv)
10366{
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010367 linenr_T lnum = get_tv_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010368
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010369 set_buffer_lines(curbuf, lnum, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010370}
10371
Bram Moolenaard823fa92016-08-12 16:29:27 +020010372static 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 +020010373
10374/*
10375 * Used by "setqflist()" and "setloclist()" functions
10376 */
10377 static void
10378set_qf_ll_list(
10379 win_T *wp UNUSED,
10380 typval_T *list_arg UNUSED,
10381 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010382 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010383 typval_T *rettv)
10384{
10385#ifdef FEAT_QUICKFIX
10386 static char *e_invact = N_("E927: Invalid action: '%s'");
10387 char_u *act;
10388 int action = 0;
10389#endif
10390
10391 rettv->vval.v_number = -1;
10392
10393#ifdef FEAT_QUICKFIX
10394 if (list_arg->v_type != VAR_LIST)
10395 EMSG(_(e_listreq));
10396 else
10397 {
10398 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010399 dict_T *d = NULL;
10400 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010401
10402 if (action_arg->v_type == VAR_STRING)
10403 {
10404 act = get_tv_string_chk(action_arg);
10405 if (act == NULL)
10406 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020010407 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
10408 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010409 action = *act;
10410 else
10411 EMSG2(_(e_invact), act);
10412 }
10413 else if (action_arg->v_type == VAR_UNKNOWN)
10414 action = ' ';
10415 else
10416 EMSG(_(e_stringreq));
10417
Bram Moolenaard823fa92016-08-12 16:29:27 +020010418 if (action_arg->v_type != VAR_UNKNOWN
10419 && what_arg->v_type != VAR_UNKNOWN)
10420 {
10421 if (what_arg->v_type == VAR_DICT)
10422 d = what_arg->vval.v_dict;
10423 else
10424 {
10425 EMSG(_(e_dictreq));
10426 valid_dict = FALSE;
10427 }
10428 }
10429
10430 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
10431 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()"), d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010432 rettv->vval.v_number = 0;
10433 }
10434#endif
10435}
10436
10437/*
10438 * "setloclist()" function
10439 */
10440 static void
10441f_setloclist(typval_T *argvars, typval_T *rettv)
10442{
10443 win_T *win;
10444
10445 rettv->vval.v_number = -1;
10446
10447 win = find_win_by_nr(&argvars[0], NULL);
10448 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020010449 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010450}
10451
10452/*
10453 * "setmatches()" function
10454 */
10455 static void
10456f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10457{
10458#ifdef FEAT_SEARCH_EXTRA
10459 list_T *l;
10460 listitem_T *li;
10461 dict_T *d;
10462 list_T *s = NULL;
10463
10464 rettv->vval.v_number = -1;
10465 if (argvars[0].v_type != VAR_LIST)
10466 {
10467 EMSG(_(e_listreq));
10468 return;
10469 }
10470 if ((l = argvars[0].vval.v_list) != NULL)
10471 {
10472
10473 /* To some extent make sure that we are dealing with a list from
10474 * "getmatches()". */
10475 li = l->lv_first;
10476 while (li != NULL)
10477 {
10478 if (li->li_tv.v_type != VAR_DICT
10479 || (d = li->li_tv.vval.v_dict) == NULL)
10480 {
10481 EMSG(_(e_invarg));
10482 return;
10483 }
10484 if (!(dict_find(d, (char_u *)"group", -1) != NULL
10485 && (dict_find(d, (char_u *)"pattern", -1) != NULL
10486 || dict_find(d, (char_u *)"pos1", -1) != NULL)
10487 && dict_find(d, (char_u *)"priority", -1) != NULL
10488 && dict_find(d, (char_u *)"id", -1) != NULL))
10489 {
10490 EMSG(_(e_invarg));
10491 return;
10492 }
10493 li = li->li_next;
10494 }
10495
10496 clear_matches(curwin);
10497 li = l->lv_first;
10498 while (li != NULL)
10499 {
10500 int i = 0;
10501 char_u buf[5];
10502 dictitem_T *di;
10503 char_u *group;
10504 int priority;
10505 int id;
10506 char_u *conceal;
10507
10508 d = li->li_tv.vval.v_dict;
10509 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
10510 {
10511 if (s == NULL)
10512 {
10513 s = list_alloc();
10514 if (s == NULL)
10515 return;
10516 }
10517
10518 /* match from matchaddpos() */
10519 for (i = 1; i < 9; i++)
10520 {
10521 sprintf((char *)buf, (char *)"pos%d", i);
10522 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
10523 {
10524 if (di->di_tv.v_type != VAR_LIST)
10525 return;
10526
10527 list_append_tv(s, &di->di_tv);
10528 s->lv_refcount++;
10529 }
10530 else
10531 break;
10532 }
10533 }
10534
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010535 group = get_dict_string(d, (char_u *)"group", TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010536 priority = (int)get_dict_number(d, (char_u *)"priority");
10537 id = (int)get_dict_number(d, (char_u *)"id");
10538 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010539 ? get_dict_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010540 : NULL;
10541 if (i == 0)
10542 {
10543 match_add(curwin, group,
10544 get_dict_string(d, (char_u *)"pattern", FALSE),
10545 priority, id, NULL, conceal);
10546 }
10547 else
10548 {
10549 match_add(curwin, group, NULL, priority, id, s, conceal);
10550 list_unref(s);
10551 s = NULL;
10552 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010553 vim_free(group);
10554 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010555
10556 li = li->li_next;
10557 }
10558 rettv->vval.v_number = 0;
10559 }
10560#endif
10561}
10562
10563/*
10564 * "setpos()" function
10565 */
10566 static void
10567f_setpos(typval_T *argvars, typval_T *rettv)
10568{
10569 pos_T pos;
10570 int fnum;
10571 char_u *name;
10572 colnr_T curswant = -1;
10573
10574 rettv->vval.v_number = -1;
10575 name = get_tv_string_chk(argvars);
10576 if (name != NULL)
10577 {
10578 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10579 {
10580 if (--pos.col < 0)
10581 pos.col = 0;
10582 if (name[0] == '.' && name[1] == NUL)
10583 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010584 /* set cursor; "fnum" is ignored */
10585 curwin->w_cursor = pos;
10586 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010587 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010588 curwin->w_curswant = curswant - 1;
10589 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010590 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010591 check_cursor();
10592 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010593 }
10594 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10595 {
10596 /* set mark */
10597 if (setmark_pos(name[1], &pos, fnum) == OK)
10598 rettv->vval.v_number = 0;
10599 }
10600 else
10601 EMSG(_(e_invarg));
10602 }
10603 }
10604}
10605
10606/*
10607 * "setqflist()" function
10608 */
10609 static void
10610f_setqflist(typval_T *argvars, typval_T *rettv)
10611{
Bram Moolenaard823fa92016-08-12 16:29:27 +020010612 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010613}
10614
10615/*
10616 * "setreg()" function
10617 */
10618 static void
10619f_setreg(typval_T *argvars, typval_T *rettv)
10620{
10621 int regname;
10622 char_u *strregname;
10623 char_u *stropt;
10624 char_u *strval;
10625 int append;
10626 char_u yank_type;
10627 long block_len;
10628
10629 block_len = -1;
10630 yank_type = MAUTO;
10631 append = FALSE;
10632
10633 strregname = get_tv_string_chk(argvars);
10634 rettv->vval.v_number = 1; /* FAIL is default */
10635
10636 if (strregname == NULL)
10637 return; /* type error; errmsg already given */
10638 regname = *strregname;
10639 if (regname == 0 || regname == '@')
10640 regname = '"';
10641
10642 if (argvars[2].v_type != VAR_UNKNOWN)
10643 {
10644 stropt = get_tv_string_chk(&argvars[2]);
10645 if (stropt == NULL)
10646 return; /* type error */
10647 for (; *stropt != NUL; ++stropt)
10648 switch (*stropt)
10649 {
10650 case 'a': case 'A': /* append */
10651 append = TRUE;
10652 break;
10653 case 'v': case 'c': /* character-wise selection */
10654 yank_type = MCHAR;
10655 break;
10656 case 'V': case 'l': /* line-wise selection */
10657 yank_type = MLINE;
10658 break;
10659 case 'b': case Ctrl_V: /* block-wise selection */
10660 yank_type = MBLOCK;
10661 if (VIM_ISDIGIT(stropt[1]))
10662 {
10663 ++stropt;
10664 block_len = getdigits(&stropt) - 1;
10665 --stropt;
10666 }
10667 break;
10668 }
10669 }
10670
10671 if (argvars[1].v_type == VAR_LIST)
10672 {
10673 char_u **lstval;
10674 char_u **allocval;
10675 char_u buf[NUMBUFLEN];
10676 char_u **curval;
10677 char_u **curallocval;
10678 list_T *ll = argvars[1].vval.v_list;
10679 listitem_T *li;
10680 int len;
10681
10682 /* If the list is NULL handle like an empty list. */
10683 len = ll == NULL ? 0 : ll->lv_len;
10684
10685 /* First half: use for pointers to result lines; second half: use for
10686 * pointers to allocated copies. */
10687 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
10688 if (lstval == NULL)
10689 return;
10690 curval = lstval;
10691 allocval = lstval + len + 2;
10692 curallocval = allocval;
10693
10694 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
10695 li = li->li_next)
10696 {
10697 strval = get_tv_string_buf_chk(&li->li_tv, buf);
10698 if (strval == NULL)
10699 goto free_lstval;
10700 if (strval == buf)
10701 {
10702 /* Need to make a copy, next get_tv_string_buf_chk() will
10703 * overwrite the string. */
10704 strval = vim_strsave(buf);
10705 if (strval == NULL)
10706 goto free_lstval;
10707 *curallocval++ = strval;
10708 }
10709 *curval++ = strval;
10710 }
10711 *curval++ = NULL;
10712
10713 write_reg_contents_lst(regname, lstval, -1,
10714 append, yank_type, block_len);
10715free_lstval:
10716 while (curallocval > allocval)
10717 vim_free(*--curallocval);
10718 vim_free(lstval);
10719 }
10720 else
10721 {
10722 strval = get_tv_string_chk(&argvars[1]);
10723 if (strval == NULL)
10724 return;
10725 write_reg_contents_ex(regname, strval, -1,
10726 append, yank_type, block_len);
10727 }
10728 rettv->vval.v_number = 0;
10729}
10730
10731/*
10732 * "settabvar()" function
10733 */
10734 static void
10735f_settabvar(typval_T *argvars, typval_T *rettv)
10736{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010737 tabpage_T *save_curtab;
10738 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010739 char_u *varname, *tabvarname;
10740 typval_T *varp;
10741
10742 rettv->vval.v_number = 0;
10743
10744 if (check_restricted() || check_secure())
10745 return;
10746
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010747 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010748 varname = get_tv_string_chk(&argvars[1]);
10749 varp = &argvars[2];
10750
Bram Moolenaar4033c552017-09-16 20:54:51 +020010751 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010752 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010753 save_curtab = curtab;
10754 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010755
10756 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
10757 if (tabvarname != NULL)
10758 {
10759 STRCPY(tabvarname, "t:");
10760 STRCPY(tabvarname + 2, varname);
10761 set_var(tabvarname, varp, TRUE);
10762 vim_free(tabvarname);
10763 }
10764
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010765 /* Restore current tabpage */
10766 if (valid_tabpage(save_curtab))
10767 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010768 }
10769}
10770
10771/*
10772 * "settabwinvar()" function
10773 */
10774 static void
10775f_settabwinvar(typval_T *argvars, typval_T *rettv)
10776{
10777 setwinvar(argvars, rettv, 1);
10778}
10779
10780/*
10781 * "setwinvar()" function
10782 */
10783 static void
10784f_setwinvar(typval_T *argvars, typval_T *rettv)
10785{
10786 setwinvar(argvars, rettv, 0);
10787}
10788
10789#ifdef FEAT_CRYPT
10790/*
10791 * "sha256({string})" function
10792 */
10793 static void
10794f_sha256(typval_T *argvars, typval_T *rettv)
10795{
10796 char_u *p;
10797
10798 p = get_tv_string(&argvars[0]);
10799 rettv->vval.v_string = vim_strsave(
10800 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
10801 rettv->v_type = VAR_STRING;
10802}
10803#endif /* FEAT_CRYPT */
10804
10805/*
10806 * "shellescape({string})" function
10807 */
10808 static void
10809f_shellescape(typval_T *argvars, typval_T *rettv)
10810{
Bram Moolenaar20615522017-06-05 18:46:26 +020010811 int do_special = non_zero_arg(&argvars[1]);
10812
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010813 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar20615522017-06-05 18:46:26 +020010814 get_tv_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010815 rettv->v_type = VAR_STRING;
10816}
10817
10818/*
10819 * shiftwidth() function
10820 */
10821 static void
10822f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
10823{
10824 rettv->vval.v_number = get_sw_value(curbuf);
10825}
10826
10827/*
10828 * "simplify()" function
10829 */
10830 static void
10831f_simplify(typval_T *argvars, typval_T *rettv)
10832{
10833 char_u *p;
10834
10835 p = get_tv_string(&argvars[0]);
10836 rettv->vval.v_string = vim_strsave(p);
10837 simplify_filename(rettv->vval.v_string); /* simplify in place */
10838 rettv->v_type = VAR_STRING;
10839}
10840
10841#ifdef FEAT_FLOAT
10842/*
10843 * "sin()" function
10844 */
10845 static void
10846f_sin(typval_T *argvars, typval_T *rettv)
10847{
10848 float_T f = 0.0;
10849
10850 rettv->v_type = VAR_FLOAT;
10851 if (get_float_arg(argvars, &f) == OK)
10852 rettv->vval.v_float = sin(f);
10853 else
10854 rettv->vval.v_float = 0.0;
10855}
10856
10857/*
10858 * "sinh()" function
10859 */
10860 static void
10861f_sinh(typval_T *argvars, typval_T *rettv)
10862{
10863 float_T f = 0.0;
10864
10865 rettv->v_type = VAR_FLOAT;
10866 if (get_float_arg(argvars, &f) == OK)
10867 rettv->vval.v_float = sinh(f);
10868 else
10869 rettv->vval.v_float = 0.0;
10870}
10871#endif
10872
10873static int
10874#ifdef __BORLANDC__
10875 _RTLENTRYF
10876#endif
10877 item_compare(const void *s1, const void *s2);
10878static int
10879#ifdef __BORLANDC__
10880 _RTLENTRYF
10881#endif
10882 item_compare2(const void *s1, const void *s2);
10883
10884/* struct used in the array that's given to qsort() */
10885typedef struct
10886{
10887 listitem_T *item;
10888 int idx;
10889} sortItem_T;
10890
10891/* struct storing information about current sort */
10892typedef struct
10893{
10894 int item_compare_ic;
10895 int item_compare_numeric;
10896 int item_compare_numbers;
10897#ifdef FEAT_FLOAT
10898 int item_compare_float;
10899#endif
10900 char_u *item_compare_func;
10901 partial_T *item_compare_partial;
10902 dict_T *item_compare_selfdict;
10903 int item_compare_func_err;
10904 int item_compare_keep_zero;
10905} sortinfo_T;
10906static sortinfo_T *sortinfo = NULL;
10907static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
10908#define ITEM_COMPARE_FAIL 999
10909
10910/*
10911 * Compare functions for f_sort() and f_uniq() below.
10912 */
10913 static int
10914#ifdef __BORLANDC__
10915_RTLENTRYF
10916#endif
10917item_compare(const void *s1, const void *s2)
10918{
10919 sortItem_T *si1, *si2;
10920 typval_T *tv1, *tv2;
10921 char_u *p1, *p2;
10922 char_u *tofree1 = NULL, *tofree2 = NULL;
10923 int res;
10924 char_u numbuf1[NUMBUFLEN];
10925 char_u numbuf2[NUMBUFLEN];
10926
10927 si1 = (sortItem_T *)s1;
10928 si2 = (sortItem_T *)s2;
10929 tv1 = &si1->item->li_tv;
10930 tv2 = &si2->item->li_tv;
10931
10932 if (sortinfo->item_compare_numbers)
10933 {
10934 varnumber_T v1 = get_tv_number(tv1);
10935 varnumber_T v2 = get_tv_number(tv2);
10936
10937 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10938 }
10939
10940#ifdef FEAT_FLOAT
10941 if (sortinfo->item_compare_float)
10942 {
10943 float_T v1 = get_tv_float(tv1);
10944 float_T v2 = get_tv_float(tv2);
10945
10946 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10947 }
10948#endif
10949
10950 /* tv2string() puts quotes around a string and allocates memory. Don't do
10951 * that for string variables. Use a single quote when comparing with a
10952 * non-string to do what the docs promise. */
10953 if (tv1->v_type == VAR_STRING)
10954 {
10955 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
10956 p1 = (char_u *)"'";
10957 else
10958 p1 = tv1->vval.v_string;
10959 }
10960 else
10961 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
10962 if (tv2->v_type == VAR_STRING)
10963 {
10964 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
10965 p2 = (char_u *)"'";
10966 else
10967 p2 = tv2->vval.v_string;
10968 }
10969 else
10970 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
10971 if (p1 == NULL)
10972 p1 = (char_u *)"";
10973 if (p2 == NULL)
10974 p2 = (char_u *)"";
10975 if (!sortinfo->item_compare_numeric)
10976 {
10977 if (sortinfo->item_compare_ic)
10978 res = STRICMP(p1, p2);
10979 else
10980 res = STRCMP(p1, p2);
10981 }
10982 else
10983 {
10984 double n1, n2;
10985 n1 = strtod((char *)p1, (char **)&p1);
10986 n2 = strtod((char *)p2, (char **)&p2);
10987 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
10988 }
10989
10990 /* When the result would be zero, compare the item indexes. Makes the
10991 * sort stable. */
10992 if (res == 0 && !sortinfo->item_compare_keep_zero)
10993 res = si1->idx > si2->idx ? 1 : -1;
10994
10995 vim_free(tofree1);
10996 vim_free(tofree2);
10997 return res;
10998}
10999
11000 static int
11001#ifdef __BORLANDC__
11002_RTLENTRYF
11003#endif
11004item_compare2(const void *s1, const void *s2)
11005{
11006 sortItem_T *si1, *si2;
11007 int res;
11008 typval_T rettv;
11009 typval_T argv[3];
11010 int dummy;
11011 char_u *func_name;
11012 partial_T *partial = sortinfo->item_compare_partial;
11013
11014 /* shortcut after failure in previous call; compare all items equal */
11015 if (sortinfo->item_compare_func_err)
11016 return 0;
11017
11018 si1 = (sortItem_T *)s1;
11019 si2 = (sortItem_T *)s2;
11020
11021 if (partial == NULL)
11022 func_name = sortinfo->item_compare_func;
11023 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020011024 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011025
11026 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
11027 * in the copy without changing the original list items. */
11028 copy_tv(&si1->item->li_tv, &argv[0]);
11029 copy_tv(&si2->item->li_tv, &argv[1]);
11030
11031 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
11032 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020011033 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011034 partial, sortinfo->item_compare_selfdict);
11035 clear_tv(&argv[0]);
11036 clear_tv(&argv[1]);
11037
11038 if (res == FAIL)
11039 res = ITEM_COMPARE_FAIL;
11040 else
11041 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
11042 if (sortinfo->item_compare_func_err)
11043 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
11044 clear_tv(&rettv);
11045
11046 /* When the result would be zero, compare the pointers themselves. Makes
11047 * the sort stable. */
11048 if (res == 0 && !sortinfo->item_compare_keep_zero)
11049 res = si1->idx > si2->idx ? 1 : -1;
11050
11051 return res;
11052}
11053
11054/*
11055 * "sort({list})" function
11056 */
11057 static void
11058do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
11059{
11060 list_T *l;
11061 listitem_T *li;
11062 sortItem_T *ptrs;
11063 sortinfo_T *old_sortinfo;
11064 sortinfo_T info;
11065 long len;
11066 long i;
11067
11068 /* Pointer to current info struct used in compare function. Save and
11069 * restore the current one for nested calls. */
11070 old_sortinfo = sortinfo;
11071 sortinfo = &info;
11072
11073 if (argvars[0].v_type != VAR_LIST)
11074 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
11075 else
11076 {
11077 l = argvars[0].vval.v_list;
11078 if (l == NULL || tv_check_lock(l->lv_lock,
11079 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
11080 TRUE))
11081 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011082 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011083
11084 len = list_len(l);
11085 if (len <= 1)
11086 goto theend; /* short list sorts pretty quickly */
11087
11088 info.item_compare_ic = FALSE;
11089 info.item_compare_numeric = FALSE;
11090 info.item_compare_numbers = FALSE;
11091#ifdef FEAT_FLOAT
11092 info.item_compare_float = FALSE;
11093#endif
11094 info.item_compare_func = NULL;
11095 info.item_compare_partial = NULL;
11096 info.item_compare_selfdict = NULL;
11097 if (argvars[1].v_type != VAR_UNKNOWN)
11098 {
11099 /* optional second argument: {func} */
11100 if (argvars[1].v_type == VAR_FUNC)
11101 info.item_compare_func = argvars[1].vval.v_string;
11102 else if (argvars[1].v_type == VAR_PARTIAL)
11103 info.item_compare_partial = argvars[1].vval.v_partial;
11104 else
11105 {
11106 int error = FALSE;
11107
11108 i = (long)get_tv_number_chk(&argvars[1], &error);
11109 if (error)
11110 goto theend; /* type error; errmsg already given */
11111 if (i == 1)
11112 info.item_compare_ic = TRUE;
11113 else if (argvars[1].v_type != VAR_NUMBER)
11114 info.item_compare_func = get_tv_string(&argvars[1]);
11115 else if (i != 0)
11116 {
11117 EMSG(_(e_invarg));
11118 goto theend;
11119 }
11120 if (info.item_compare_func != NULL)
11121 {
11122 if (*info.item_compare_func == NUL)
11123 {
11124 /* empty string means default sort */
11125 info.item_compare_func = NULL;
11126 }
11127 else if (STRCMP(info.item_compare_func, "n") == 0)
11128 {
11129 info.item_compare_func = NULL;
11130 info.item_compare_numeric = TRUE;
11131 }
11132 else if (STRCMP(info.item_compare_func, "N") == 0)
11133 {
11134 info.item_compare_func = NULL;
11135 info.item_compare_numbers = TRUE;
11136 }
11137#ifdef FEAT_FLOAT
11138 else if (STRCMP(info.item_compare_func, "f") == 0)
11139 {
11140 info.item_compare_func = NULL;
11141 info.item_compare_float = TRUE;
11142 }
11143#endif
11144 else if (STRCMP(info.item_compare_func, "i") == 0)
11145 {
11146 info.item_compare_func = NULL;
11147 info.item_compare_ic = TRUE;
11148 }
11149 }
11150 }
11151
11152 if (argvars[2].v_type != VAR_UNKNOWN)
11153 {
11154 /* optional third argument: {dict} */
11155 if (argvars[2].v_type != VAR_DICT)
11156 {
11157 EMSG(_(e_dictreq));
11158 goto theend;
11159 }
11160 info.item_compare_selfdict = argvars[2].vval.v_dict;
11161 }
11162 }
11163
11164 /* Make an array with each entry pointing to an item in the List. */
11165 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
11166 if (ptrs == NULL)
11167 goto theend;
11168
11169 i = 0;
11170 if (sort)
11171 {
11172 /* sort(): ptrs will be the list to sort */
11173 for (li = l->lv_first; li != NULL; li = li->li_next)
11174 {
11175 ptrs[i].item = li;
11176 ptrs[i].idx = i;
11177 ++i;
11178 }
11179
11180 info.item_compare_func_err = FALSE;
11181 info.item_compare_keep_zero = FALSE;
11182 /* test the compare function */
11183 if ((info.item_compare_func != NULL
11184 || info.item_compare_partial != NULL)
11185 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11186 == ITEM_COMPARE_FAIL)
11187 EMSG(_("E702: Sort compare function failed"));
11188 else
11189 {
11190 /* Sort the array with item pointers. */
11191 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
11192 info.item_compare_func == NULL
11193 && info.item_compare_partial == NULL
11194 ? item_compare : item_compare2);
11195
11196 if (!info.item_compare_func_err)
11197 {
11198 /* Clear the List and append the items in sorted order. */
11199 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
11200 l->lv_len = 0;
11201 for (i = 0; i < len; ++i)
11202 list_append(l, ptrs[i].item);
11203 }
11204 }
11205 }
11206 else
11207 {
11208 int (*item_compare_func_ptr)(const void *, const void *);
11209
11210 /* f_uniq(): ptrs will be a stack of items to remove */
11211 info.item_compare_func_err = FALSE;
11212 info.item_compare_keep_zero = TRUE;
11213 item_compare_func_ptr = info.item_compare_func != NULL
11214 || info.item_compare_partial != NULL
11215 ? item_compare2 : item_compare;
11216
11217 for (li = l->lv_first; li != NULL && li->li_next != NULL;
11218 li = li->li_next)
11219 {
11220 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
11221 == 0)
11222 ptrs[i++].item = li;
11223 if (info.item_compare_func_err)
11224 {
11225 EMSG(_("E882: Uniq compare function failed"));
11226 break;
11227 }
11228 }
11229
11230 if (!info.item_compare_func_err)
11231 {
11232 while (--i >= 0)
11233 {
11234 li = ptrs[i].item->li_next;
11235 ptrs[i].item->li_next = li->li_next;
11236 if (li->li_next != NULL)
11237 li->li_next->li_prev = ptrs[i].item;
11238 else
11239 l->lv_last = ptrs[i].item;
11240 list_fix_watch(l, li);
11241 listitem_free(li);
11242 l->lv_len--;
11243 }
11244 }
11245 }
11246
11247 vim_free(ptrs);
11248 }
11249theend:
11250 sortinfo = old_sortinfo;
11251}
11252
11253/*
11254 * "sort({list})" function
11255 */
11256 static void
11257f_sort(typval_T *argvars, typval_T *rettv)
11258{
11259 do_sort_uniq(argvars, rettv, TRUE);
11260}
11261
11262/*
11263 * "uniq({list})" function
11264 */
11265 static void
11266f_uniq(typval_T *argvars, typval_T *rettv)
11267{
11268 do_sort_uniq(argvars, rettv, FALSE);
11269}
11270
11271/*
11272 * "soundfold({word})" function
11273 */
11274 static void
11275f_soundfold(typval_T *argvars, typval_T *rettv)
11276{
11277 char_u *s;
11278
11279 rettv->v_type = VAR_STRING;
11280 s = get_tv_string(&argvars[0]);
11281#ifdef FEAT_SPELL
11282 rettv->vval.v_string = eval_soundfold(s);
11283#else
11284 rettv->vval.v_string = vim_strsave(s);
11285#endif
11286}
11287
11288/*
11289 * "spellbadword()" function
11290 */
11291 static void
11292f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
11293{
11294 char_u *word = (char_u *)"";
11295 hlf_T attr = HLF_COUNT;
11296 int len = 0;
11297
11298 if (rettv_list_alloc(rettv) == FAIL)
11299 return;
11300
11301#ifdef FEAT_SPELL
11302 if (argvars[0].v_type == VAR_UNKNOWN)
11303 {
11304 /* Find the start and length of the badly spelled word. */
11305 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
11306 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011307 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011308 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011309 curwin->w_set_curswant = TRUE;
11310 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011311 }
11312 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
11313 {
11314 char_u *str = get_tv_string_chk(&argvars[0]);
11315 int capcol = -1;
11316
11317 if (str != NULL)
11318 {
11319 /* Check the argument for spelling. */
11320 while (*str != NUL)
11321 {
11322 len = spell_check(curwin, str, &attr, &capcol, FALSE);
11323 if (attr != HLF_COUNT)
11324 {
11325 word = str;
11326 break;
11327 }
11328 str += len;
11329 }
11330 }
11331 }
11332#endif
11333
11334 list_append_string(rettv->vval.v_list, word, len);
11335 list_append_string(rettv->vval.v_list, (char_u *)(
11336 attr == HLF_SPB ? "bad" :
11337 attr == HLF_SPR ? "rare" :
11338 attr == HLF_SPL ? "local" :
11339 attr == HLF_SPC ? "caps" :
11340 ""), -1);
11341}
11342
11343/*
11344 * "spellsuggest()" function
11345 */
11346 static void
11347f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
11348{
11349#ifdef FEAT_SPELL
11350 char_u *str;
11351 int typeerr = FALSE;
11352 int maxcount;
11353 garray_T ga;
11354 int i;
11355 listitem_T *li;
11356 int need_capital = FALSE;
11357#endif
11358
11359 if (rettv_list_alloc(rettv) == FAIL)
11360 return;
11361
11362#ifdef FEAT_SPELL
11363 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
11364 {
11365 str = get_tv_string(&argvars[0]);
11366 if (argvars[1].v_type != VAR_UNKNOWN)
11367 {
11368 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
11369 if (maxcount <= 0)
11370 return;
11371 if (argvars[2].v_type != VAR_UNKNOWN)
11372 {
11373 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
11374 if (typeerr)
11375 return;
11376 }
11377 }
11378 else
11379 maxcount = 25;
11380
11381 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
11382
11383 for (i = 0; i < ga.ga_len; ++i)
11384 {
11385 str = ((char_u **)ga.ga_data)[i];
11386
11387 li = listitem_alloc();
11388 if (li == NULL)
11389 vim_free(str);
11390 else
11391 {
11392 li->li_tv.v_type = VAR_STRING;
11393 li->li_tv.v_lock = 0;
11394 li->li_tv.vval.v_string = str;
11395 list_append(rettv->vval.v_list, li);
11396 }
11397 }
11398 ga_clear(&ga);
11399 }
11400#endif
11401}
11402
11403 static void
11404f_split(typval_T *argvars, typval_T *rettv)
11405{
11406 char_u *str;
11407 char_u *end;
11408 char_u *pat = NULL;
11409 regmatch_T regmatch;
11410 char_u patbuf[NUMBUFLEN];
11411 char_u *save_cpo;
11412 int match;
11413 colnr_T col = 0;
11414 int keepempty = FALSE;
11415 int typeerr = FALSE;
11416
11417 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11418 save_cpo = p_cpo;
11419 p_cpo = (char_u *)"";
11420
11421 str = get_tv_string(&argvars[0]);
11422 if (argvars[1].v_type != VAR_UNKNOWN)
11423 {
11424 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11425 if (pat == NULL)
11426 typeerr = TRUE;
11427 if (argvars[2].v_type != VAR_UNKNOWN)
11428 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
11429 }
11430 if (pat == NULL || *pat == NUL)
11431 pat = (char_u *)"[\\x01- ]\\+";
11432
11433 if (rettv_list_alloc(rettv) == FAIL)
11434 return;
11435 if (typeerr)
11436 return;
11437
11438 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11439 if (regmatch.regprog != NULL)
11440 {
11441 regmatch.rm_ic = FALSE;
11442 while (*str != NUL || keepempty)
11443 {
11444 if (*str == NUL)
11445 match = FALSE; /* empty item at the end */
11446 else
11447 match = vim_regexec_nl(&regmatch, str, col);
11448 if (match)
11449 end = regmatch.startp[0];
11450 else
11451 end = str + STRLEN(str);
11452 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
11453 && *str != NUL && match && end < regmatch.endp[0]))
11454 {
11455 if (list_append_string(rettv->vval.v_list, str,
11456 (int)(end - str)) == FAIL)
11457 break;
11458 }
11459 if (!match)
11460 break;
11461 /* Advance to just after the match. */
11462 if (regmatch.endp[0] > str)
11463 col = 0;
11464 else
11465 {
11466 /* Don't get stuck at the same match. */
11467#ifdef FEAT_MBYTE
11468 col = (*mb_ptr2len)(regmatch.endp[0]);
11469#else
11470 col = 1;
11471#endif
11472 }
11473 str = regmatch.endp[0];
11474 }
11475
11476 vim_regfree(regmatch.regprog);
11477 }
11478
11479 p_cpo = save_cpo;
11480}
11481
11482#ifdef FEAT_FLOAT
11483/*
11484 * "sqrt()" function
11485 */
11486 static void
11487f_sqrt(typval_T *argvars, typval_T *rettv)
11488{
11489 float_T f = 0.0;
11490
11491 rettv->v_type = VAR_FLOAT;
11492 if (get_float_arg(argvars, &f) == OK)
11493 rettv->vval.v_float = sqrt(f);
11494 else
11495 rettv->vval.v_float = 0.0;
11496}
11497
11498/*
11499 * "str2float()" function
11500 */
11501 static void
11502f_str2float(typval_T *argvars, typval_T *rettv)
11503{
11504 char_u *p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011505 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011506
Bram Moolenaar08243d22017-01-10 16:12:29 +010011507 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011508 p = skipwhite(p + 1);
11509 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011510 if (isneg)
11511 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011512 rettv->v_type = VAR_FLOAT;
11513}
11514#endif
11515
11516/*
11517 * "str2nr()" function
11518 */
11519 static void
11520f_str2nr(typval_T *argvars, typval_T *rettv)
11521{
11522 int base = 10;
11523 char_u *p;
11524 varnumber_T n;
11525 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010011526 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011527
11528 if (argvars[1].v_type != VAR_UNKNOWN)
11529 {
11530 base = (int)get_tv_number(&argvars[1]);
11531 if (base != 2 && base != 8 && base != 10 && base != 16)
11532 {
11533 EMSG(_(e_invarg));
11534 return;
11535 }
11536 }
11537
11538 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011539 isneg = (*p == '-');
11540 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011541 p = skipwhite(p + 1);
11542 switch (base)
11543 {
11544 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
11545 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
11546 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
11547 default: what = 0;
11548 }
11549 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011550 if (isneg)
11551 rettv->vval.v_number = -n;
11552 else
11553 rettv->vval.v_number = n;
11554
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011555}
11556
11557#ifdef HAVE_STRFTIME
11558/*
11559 * "strftime({format}[, {time}])" function
11560 */
11561 static void
11562f_strftime(typval_T *argvars, typval_T *rettv)
11563{
11564 char_u result_buf[256];
11565 struct tm *curtime;
11566 time_t seconds;
11567 char_u *p;
11568
11569 rettv->v_type = VAR_STRING;
11570
11571 p = get_tv_string(&argvars[0]);
11572 if (argvars[1].v_type == VAR_UNKNOWN)
11573 seconds = time(NULL);
11574 else
11575 seconds = (time_t)get_tv_number(&argvars[1]);
11576 curtime = localtime(&seconds);
11577 /* MSVC returns NULL for an invalid value of seconds. */
11578 if (curtime == NULL)
11579 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
11580 else
11581 {
11582# ifdef FEAT_MBYTE
11583 vimconv_T conv;
11584 char_u *enc;
11585
11586 conv.vc_type = CONV_NONE;
11587 enc = enc_locale();
11588 convert_setup(&conv, p_enc, enc);
11589 if (conv.vc_type != CONV_NONE)
11590 p = string_convert(&conv, p, NULL);
11591# endif
11592 if (p != NULL)
11593 (void)strftime((char *)result_buf, sizeof(result_buf),
11594 (char *)p, curtime);
11595 else
11596 result_buf[0] = NUL;
11597
11598# ifdef FEAT_MBYTE
11599 if (conv.vc_type != CONV_NONE)
11600 vim_free(p);
11601 convert_setup(&conv, enc, p_enc);
11602 if (conv.vc_type != CONV_NONE)
11603 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
11604 else
11605# endif
11606 rettv->vval.v_string = vim_strsave(result_buf);
11607
11608# ifdef FEAT_MBYTE
11609 /* Release conversion descriptors */
11610 convert_setup(&conv, NULL, NULL);
11611 vim_free(enc);
11612# endif
11613 }
11614}
11615#endif
11616
11617/*
11618 * "strgetchar()" function
11619 */
11620 static void
11621f_strgetchar(typval_T *argvars, typval_T *rettv)
11622{
11623 char_u *str;
11624 int len;
11625 int error = FALSE;
11626 int charidx;
11627
11628 rettv->vval.v_number = -1;
11629 str = get_tv_string_chk(&argvars[0]);
11630 if (str == NULL)
11631 return;
11632 len = (int)STRLEN(str);
11633 charidx = (int)get_tv_number_chk(&argvars[1], &error);
11634 if (error)
11635 return;
11636#ifdef FEAT_MBYTE
11637 {
11638 int byteidx = 0;
11639
11640 while (charidx >= 0 && byteidx < len)
11641 {
11642 if (charidx == 0)
11643 {
11644 rettv->vval.v_number = mb_ptr2char(str + byteidx);
11645 break;
11646 }
11647 --charidx;
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011648 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011649 }
11650 }
11651#else
11652 if (charidx < len)
11653 rettv->vval.v_number = str[charidx];
11654#endif
11655}
11656
11657/*
11658 * "stridx()" function
11659 */
11660 static void
11661f_stridx(typval_T *argvars, typval_T *rettv)
11662{
11663 char_u buf[NUMBUFLEN];
11664 char_u *needle;
11665 char_u *haystack;
11666 char_u *save_haystack;
11667 char_u *pos;
11668 int start_idx;
11669
11670 needle = get_tv_string_chk(&argvars[1]);
11671 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
11672 rettv->vval.v_number = -1;
11673 if (needle == NULL || haystack == NULL)
11674 return; /* type error; errmsg already given */
11675
11676 if (argvars[2].v_type != VAR_UNKNOWN)
11677 {
11678 int error = FALSE;
11679
11680 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
11681 if (error || start_idx >= (int)STRLEN(haystack))
11682 return;
11683 if (start_idx >= 0)
11684 haystack += start_idx;
11685 }
11686
11687 pos = (char_u *)strstr((char *)haystack, (char *)needle);
11688 if (pos != NULL)
11689 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
11690}
11691
11692/*
11693 * "string()" function
11694 */
11695 static void
11696f_string(typval_T *argvars, typval_T *rettv)
11697{
11698 char_u *tofree;
11699 char_u numbuf[NUMBUFLEN];
11700
11701 rettv->v_type = VAR_STRING;
11702 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
11703 get_copyID());
11704 /* Make a copy if we have a value but it's not in allocated memory. */
11705 if (rettv->vval.v_string != NULL && tofree == NULL)
11706 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
11707}
11708
11709/*
11710 * "strlen()" function
11711 */
11712 static void
11713f_strlen(typval_T *argvars, typval_T *rettv)
11714{
11715 rettv->vval.v_number = (varnumber_T)(STRLEN(
11716 get_tv_string(&argvars[0])));
11717}
11718
11719/*
11720 * "strchars()" function
11721 */
11722 static void
11723f_strchars(typval_T *argvars, typval_T *rettv)
11724{
11725 char_u *s = get_tv_string(&argvars[0]);
11726 int skipcc = 0;
11727#ifdef FEAT_MBYTE
11728 varnumber_T len = 0;
11729 int (*func_mb_ptr2char_adv)(char_u **pp);
11730#endif
11731
11732 if (argvars[1].v_type != VAR_UNKNOWN)
11733 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
11734 if (skipcc < 0 || skipcc > 1)
11735 EMSG(_(e_invarg));
11736 else
11737 {
11738#ifdef FEAT_MBYTE
11739 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
11740 while (*s != NUL)
11741 {
11742 func_mb_ptr2char_adv(&s);
11743 ++len;
11744 }
11745 rettv->vval.v_number = len;
11746#else
11747 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
11748#endif
11749 }
11750}
11751
11752/*
11753 * "strdisplaywidth()" function
11754 */
11755 static void
11756f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
11757{
11758 char_u *s = get_tv_string(&argvars[0]);
11759 int col = 0;
11760
11761 if (argvars[1].v_type != VAR_UNKNOWN)
11762 col = (int)get_tv_number(&argvars[1]);
11763
11764 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
11765}
11766
11767/*
11768 * "strwidth()" function
11769 */
11770 static void
11771f_strwidth(typval_T *argvars, typval_T *rettv)
11772{
11773 char_u *s = get_tv_string(&argvars[0]);
11774
11775 rettv->vval.v_number = (varnumber_T)(
11776#ifdef FEAT_MBYTE
11777 mb_string2cells(s, -1)
11778#else
11779 STRLEN(s)
11780#endif
11781 );
11782}
11783
11784/*
11785 * "strcharpart()" function
11786 */
11787 static void
11788f_strcharpart(typval_T *argvars, typval_T *rettv)
11789{
11790#ifdef FEAT_MBYTE
11791 char_u *p;
11792 int nchar;
11793 int nbyte = 0;
11794 int charlen;
11795 int len = 0;
11796 int slen;
11797 int error = FALSE;
11798
11799 p = get_tv_string(&argvars[0]);
11800 slen = (int)STRLEN(p);
11801
11802 nchar = (int)get_tv_number_chk(&argvars[1], &error);
11803 if (!error)
11804 {
11805 if (nchar > 0)
11806 while (nchar > 0 && nbyte < slen)
11807 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011808 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011809 --nchar;
11810 }
11811 else
11812 nbyte = nchar;
11813 if (argvars[2].v_type != VAR_UNKNOWN)
11814 {
11815 charlen = (int)get_tv_number(&argvars[2]);
11816 while (charlen > 0 && nbyte + len < slen)
11817 {
11818 int off = nbyte + len;
11819
11820 if (off < 0)
11821 len += 1;
11822 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011823 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011824 --charlen;
11825 }
11826 }
11827 else
11828 len = slen - nbyte; /* default: all bytes that are available. */
11829 }
11830
11831 /*
11832 * Only return the overlap between the specified part and the actual
11833 * string.
11834 */
11835 if (nbyte < 0)
11836 {
11837 len += nbyte;
11838 nbyte = 0;
11839 }
11840 else if (nbyte > slen)
11841 nbyte = slen;
11842 if (len < 0)
11843 len = 0;
11844 else if (nbyte + len > slen)
11845 len = slen - nbyte;
11846
11847 rettv->v_type = VAR_STRING;
11848 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
11849#else
11850 f_strpart(argvars, rettv);
11851#endif
11852}
11853
11854/*
11855 * "strpart()" function
11856 */
11857 static void
11858f_strpart(typval_T *argvars, typval_T *rettv)
11859{
11860 char_u *p;
11861 int n;
11862 int len;
11863 int slen;
11864 int error = FALSE;
11865
11866 p = get_tv_string(&argvars[0]);
11867 slen = (int)STRLEN(p);
11868
11869 n = (int)get_tv_number_chk(&argvars[1], &error);
11870 if (error)
11871 len = 0;
11872 else if (argvars[2].v_type != VAR_UNKNOWN)
11873 len = (int)get_tv_number(&argvars[2]);
11874 else
11875 len = slen - n; /* default len: all bytes that are available. */
11876
11877 /*
11878 * Only return the overlap between the specified part and the actual
11879 * string.
11880 */
11881 if (n < 0)
11882 {
11883 len += n;
11884 n = 0;
11885 }
11886 else if (n > slen)
11887 n = slen;
11888 if (len < 0)
11889 len = 0;
11890 else if (n + len > slen)
11891 len = slen - n;
11892
11893 rettv->v_type = VAR_STRING;
11894 rettv->vval.v_string = vim_strnsave(p + n, len);
11895}
11896
11897/*
11898 * "strridx()" function
11899 */
11900 static void
11901f_strridx(typval_T *argvars, typval_T *rettv)
11902{
11903 char_u buf[NUMBUFLEN];
11904 char_u *needle;
11905 char_u *haystack;
11906 char_u *rest;
11907 char_u *lastmatch = NULL;
11908 int haystack_len, end_idx;
11909
11910 needle = get_tv_string_chk(&argvars[1]);
11911 haystack = get_tv_string_buf_chk(&argvars[0], buf);
11912
11913 rettv->vval.v_number = -1;
11914 if (needle == NULL || haystack == NULL)
11915 return; /* type error; errmsg already given */
11916
11917 haystack_len = (int)STRLEN(haystack);
11918 if (argvars[2].v_type != VAR_UNKNOWN)
11919 {
11920 /* Third argument: upper limit for index */
11921 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
11922 if (end_idx < 0)
11923 return; /* can never find a match */
11924 }
11925 else
11926 end_idx = haystack_len;
11927
11928 if (*needle == NUL)
11929 {
11930 /* Empty string matches past the end. */
11931 lastmatch = haystack + end_idx;
11932 }
11933 else
11934 {
11935 for (rest = haystack; *rest != '\0'; ++rest)
11936 {
11937 rest = (char_u *)strstr((char *)rest, (char *)needle);
11938 if (rest == NULL || rest > haystack + end_idx)
11939 break;
11940 lastmatch = rest;
11941 }
11942 }
11943
11944 if (lastmatch == NULL)
11945 rettv->vval.v_number = -1;
11946 else
11947 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
11948}
11949
11950/*
11951 * "strtrans()" function
11952 */
11953 static void
11954f_strtrans(typval_T *argvars, typval_T *rettv)
11955{
11956 rettv->v_type = VAR_STRING;
11957 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
11958}
11959
11960/*
11961 * "submatch()" function
11962 */
11963 static void
11964f_submatch(typval_T *argvars, typval_T *rettv)
11965{
11966 int error = FALSE;
11967 int no;
11968 int retList = 0;
11969
11970 no = (int)get_tv_number_chk(&argvars[0], &error);
11971 if (error)
11972 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020011973 if (no < 0 || no >= NSUBEXP)
11974 {
Bram Moolenaar79518e22017-02-17 16:31:35 +010011975 EMSGN(_("E935: invalid submatch number: %d"), no);
11976 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020011977 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011978 if (argvars[1].v_type != VAR_UNKNOWN)
11979 retList = (int)get_tv_number_chk(&argvars[1], &error);
11980 if (error)
11981 return;
11982
11983 if (retList == 0)
11984 {
11985 rettv->v_type = VAR_STRING;
11986 rettv->vval.v_string = reg_submatch(no);
11987 }
11988 else
11989 {
11990 rettv->v_type = VAR_LIST;
11991 rettv->vval.v_list = reg_submatch_list(no);
11992 }
11993}
11994
11995/*
11996 * "substitute()" function
11997 */
11998 static void
11999f_substitute(typval_T *argvars, typval_T *rettv)
12000{
12001 char_u patbuf[NUMBUFLEN];
12002 char_u subbuf[NUMBUFLEN];
12003 char_u flagsbuf[NUMBUFLEN];
12004
12005 char_u *str = get_tv_string_chk(&argvars[0]);
12006 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012007 char_u *sub = NULL;
12008 typval_T *expr = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012009 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
12010
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012011 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
12012 expr = &argvars[2];
12013 else
12014 sub = get_tv_string_buf_chk(&argvars[2], subbuf);
12015
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012016 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012017 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
12018 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012019 rettv->vval.v_string = NULL;
12020 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012021 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012022}
12023
12024/*
12025 * "synID(lnum, col, trans)" function
12026 */
12027 static void
12028f_synID(typval_T *argvars UNUSED, typval_T *rettv)
12029{
12030 int id = 0;
12031#ifdef FEAT_SYN_HL
12032 linenr_T lnum;
12033 colnr_T col;
12034 int trans;
12035 int transerr = FALSE;
12036
12037 lnum = get_tv_lnum(argvars); /* -1 on type error */
12038 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12039 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
12040
12041 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12042 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
12043 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
12044#endif
12045
12046 rettv->vval.v_number = id;
12047}
12048
12049/*
12050 * "synIDattr(id, what [, mode])" function
12051 */
12052 static void
12053f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
12054{
12055 char_u *p = NULL;
12056#ifdef FEAT_SYN_HL
12057 int id;
12058 char_u *what;
12059 char_u *mode;
12060 char_u modebuf[NUMBUFLEN];
12061 int modec;
12062
12063 id = (int)get_tv_number(&argvars[0]);
12064 what = get_tv_string(&argvars[1]);
12065 if (argvars[2].v_type != VAR_UNKNOWN)
12066 {
12067 mode = get_tv_string_buf(&argvars[2], modebuf);
12068 modec = TOLOWER_ASC(mode[0]);
12069 if (modec != 't' && modec != 'c' && modec != 'g')
12070 modec = 0; /* replace invalid with current */
12071 }
12072 else
12073 {
12074#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
12075 if (USE_24BIT)
12076 modec = 'g';
12077 else
12078#endif
12079 if (t_colors > 1)
12080 modec = 'c';
12081 else
12082 modec = 't';
12083 }
12084
12085
12086 switch (TOLOWER_ASC(what[0]))
12087 {
12088 case 'b':
12089 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12090 p = highlight_color(id, what, modec);
12091 else /* bold */
12092 p = highlight_has_attr(id, HL_BOLD, modec);
12093 break;
12094
12095 case 'f': /* fg[#] or font */
12096 p = highlight_color(id, what, modec);
12097 break;
12098
12099 case 'i':
12100 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
12101 p = highlight_has_attr(id, HL_INVERSE, modec);
12102 else /* italic */
12103 p = highlight_has_attr(id, HL_ITALIC, modec);
12104 break;
12105
12106 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020012107 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012108 break;
12109
12110 case 'r': /* reverse */
12111 p = highlight_has_attr(id, HL_INVERSE, modec);
12112 break;
12113
12114 case 's':
12115 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
12116 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020012117 /* strikeout */
12118 else if (TOLOWER_ASC(what[1]) == 't' &&
12119 TOLOWER_ASC(what[2]) == 'r')
12120 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012121 else /* standout */
12122 p = highlight_has_attr(id, HL_STANDOUT, modec);
12123 break;
12124
12125 case 'u':
12126 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
12127 /* underline */
12128 p = highlight_has_attr(id, HL_UNDERLINE, modec);
12129 else
12130 /* undercurl */
12131 p = highlight_has_attr(id, HL_UNDERCURL, modec);
12132 break;
12133 }
12134
12135 if (p != NULL)
12136 p = vim_strsave(p);
12137#endif
12138 rettv->v_type = VAR_STRING;
12139 rettv->vval.v_string = p;
12140}
12141
12142/*
12143 * "synIDtrans(id)" function
12144 */
12145 static void
12146f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
12147{
12148 int id;
12149
12150#ifdef FEAT_SYN_HL
12151 id = (int)get_tv_number(&argvars[0]);
12152
12153 if (id > 0)
12154 id = syn_get_final_id(id);
12155 else
12156#endif
12157 id = 0;
12158
12159 rettv->vval.v_number = id;
12160}
12161
12162/*
12163 * "synconcealed(lnum, col)" function
12164 */
12165 static void
12166f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
12167{
12168#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12169 linenr_T lnum;
12170 colnr_T col;
12171 int syntax_flags = 0;
12172 int cchar;
12173 int matchid = 0;
12174 char_u str[NUMBUFLEN];
12175#endif
12176
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012177 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012178
12179#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12180 lnum = get_tv_lnum(argvars); /* -1 on type error */
12181 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12182
12183 vim_memset(str, NUL, sizeof(str));
12184
12185 if (rettv_list_alloc(rettv) != FAIL)
12186 {
12187 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12188 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12189 && curwin->w_p_cole > 0)
12190 {
12191 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
12192 syntax_flags = get_syntax_info(&matchid);
12193
12194 /* get the conceal character */
12195 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
12196 {
12197 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020012198 if (cchar == NUL && curwin->w_p_cole == 1)
12199 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012200 if (cchar != NUL)
12201 {
12202# ifdef FEAT_MBYTE
12203 if (has_mbyte)
12204 (*mb_char2bytes)(cchar, str);
12205 else
12206# endif
12207 str[0] = cchar;
12208 }
12209 }
12210 }
12211
12212 list_append_number(rettv->vval.v_list,
12213 (syntax_flags & HL_CONCEAL) != 0);
12214 /* -1 to auto-determine strlen */
12215 list_append_string(rettv->vval.v_list, str, -1);
12216 list_append_number(rettv->vval.v_list, matchid);
12217 }
12218#endif
12219}
12220
12221/*
12222 * "synstack(lnum, col)" function
12223 */
12224 static void
12225f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
12226{
12227#ifdef FEAT_SYN_HL
12228 linenr_T lnum;
12229 colnr_T col;
12230 int i;
12231 int id;
12232#endif
12233
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012234 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012235
12236#ifdef FEAT_SYN_HL
12237 lnum = get_tv_lnum(argvars); /* -1 on type error */
12238 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12239
12240 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12241 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12242 && rettv_list_alloc(rettv) != FAIL)
12243 {
12244 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
12245 for (i = 0; ; ++i)
12246 {
12247 id = syn_get_stack_item(i);
12248 if (id < 0)
12249 break;
12250 if (list_append_number(rettv->vval.v_list, id) == FAIL)
12251 break;
12252 }
12253 }
12254#endif
12255}
12256
12257 static void
12258get_cmd_output_as_rettv(
12259 typval_T *argvars,
12260 typval_T *rettv,
12261 int retlist)
12262{
12263 char_u *res = NULL;
12264 char_u *p;
12265 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012266 int err = FALSE;
12267 FILE *fd;
12268 list_T *list = NULL;
12269 int flags = SHELL_SILENT;
12270
12271 rettv->v_type = VAR_STRING;
12272 rettv->vval.v_string = NULL;
12273 if (check_restricted() || check_secure())
12274 goto errret;
12275
12276 if (argvars[1].v_type != VAR_UNKNOWN)
12277 {
12278 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010012279 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012280 * command.
12281 */
12282 if ((infile = vim_tempname('i', TRUE)) == NULL)
12283 {
12284 EMSG(_(e_notmp));
12285 goto errret;
12286 }
12287
12288 fd = mch_fopen((char *)infile, WRITEBIN);
12289 if (fd == NULL)
12290 {
12291 EMSG2(_(e_notopen), infile);
12292 goto errret;
12293 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010012294 if (argvars[1].v_type == VAR_NUMBER)
12295 {
12296 linenr_T lnum;
12297 buf_T *buf;
12298
12299 buf = buflist_findnr(argvars[1].vval.v_number);
12300 if (buf == NULL)
12301 {
12302 EMSGN(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010012303 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010012304 goto errret;
12305 }
12306
12307 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
12308 {
12309 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
12310 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
12311 {
12312 err = TRUE;
12313 break;
12314 }
12315 if (putc(NL, fd) == EOF)
12316 {
12317 err = TRUE;
12318 break;
12319 }
12320 }
12321 }
12322 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012323 {
12324 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
12325 err = TRUE;
12326 }
12327 else
12328 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010012329 size_t len;
12330 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012331
12332 p = get_tv_string_buf_chk(&argvars[1], buf);
12333 if (p == NULL)
12334 {
12335 fclose(fd);
12336 goto errret; /* type error; errmsg already given */
12337 }
12338 len = STRLEN(p);
12339 if (len > 0 && fwrite(p, len, 1, fd) != 1)
12340 err = TRUE;
12341 }
12342 if (fclose(fd) != 0)
12343 err = TRUE;
12344 if (err)
12345 {
12346 EMSG(_("E677: Error writing temp file"));
12347 goto errret;
12348 }
12349 }
12350
12351 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
12352 * echoes typeahead, that messes up the display. */
12353 if (!msg_silent)
12354 flags += SHELL_COOKED;
12355
12356 if (retlist)
12357 {
12358 int len;
12359 listitem_T *li;
12360 char_u *s = NULL;
12361 char_u *start;
12362 char_u *end;
12363 int i;
12364
12365 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
12366 if (res == NULL)
12367 goto errret;
12368
12369 list = list_alloc();
12370 if (list == NULL)
12371 goto errret;
12372
12373 for (i = 0; i < len; ++i)
12374 {
12375 start = res + i;
12376 while (i < len && res[i] != NL)
12377 ++i;
12378 end = res + i;
12379
12380 s = alloc((unsigned)(end - start + 1));
12381 if (s == NULL)
12382 goto errret;
12383
12384 for (p = s; start < end; ++p, ++start)
12385 *p = *start == NUL ? NL : *start;
12386 *p = NUL;
12387
12388 li = listitem_alloc();
12389 if (li == NULL)
12390 {
12391 vim_free(s);
12392 goto errret;
12393 }
12394 li->li_tv.v_type = VAR_STRING;
12395 li->li_tv.v_lock = 0;
12396 li->li_tv.vval.v_string = s;
12397 list_append(list, li);
12398 }
12399
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012400 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012401 list = NULL;
12402 }
12403 else
12404 {
12405 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
12406#ifdef USE_CR
12407 /* translate <CR> into <NL> */
12408 if (res != NULL)
12409 {
12410 char_u *s;
12411
12412 for (s = res; *s; ++s)
12413 {
12414 if (*s == CAR)
12415 *s = NL;
12416 }
12417 }
12418#else
12419# ifdef USE_CRNL
12420 /* translate <CR><NL> into <NL> */
12421 if (res != NULL)
12422 {
12423 char_u *s, *d;
12424
12425 d = res;
12426 for (s = res; *s; ++s)
12427 {
12428 if (s[0] == CAR && s[1] == NL)
12429 ++s;
12430 *d++ = *s;
12431 }
12432 *d = NUL;
12433 }
12434# endif
12435#endif
12436 rettv->vval.v_string = res;
12437 res = NULL;
12438 }
12439
12440errret:
12441 if (infile != NULL)
12442 {
12443 mch_remove(infile);
12444 vim_free(infile);
12445 }
12446 if (res != NULL)
12447 vim_free(res);
12448 if (list != NULL)
12449 list_free(list);
12450}
12451
12452/*
12453 * "system()" function
12454 */
12455 static void
12456f_system(typval_T *argvars, typval_T *rettv)
12457{
12458 get_cmd_output_as_rettv(argvars, rettv, FALSE);
12459}
12460
12461/*
12462 * "systemlist()" function
12463 */
12464 static void
12465f_systemlist(typval_T *argvars, typval_T *rettv)
12466{
12467 get_cmd_output_as_rettv(argvars, rettv, TRUE);
12468}
12469
12470/*
12471 * "tabpagebuflist()" function
12472 */
12473 static void
12474f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12475{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012476 tabpage_T *tp;
12477 win_T *wp = NULL;
12478
12479 if (argvars[0].v_type == VAR_UNKNOWN)
12480 wp = firstwin;
12481 else
12482 {
12483 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12484 if (tp != NULL)
12485 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12486 }
12487 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
12488 {
12489 for (; wp != NULL; wp = wp->w_next)
12490 if (list_append_number(rettv->vval.v_list,
12491 wp->w_buffer->b_fnum) == FAIL)
12492 break;
12493 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012494}
12495
12496
12497/*
12498 * "tabpagenr()" function
12499 */
12500 static void
12501f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
12502{
12503 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012504 char_u *arg;
12505
12506 if (argvars[0].v_type != VAR_UNKNOWN)
12507 {
12508 arg = get_tv_string_chk(&argvars[0]);
12509 nr = 0;
12510 if (arg != NULL)
12511 {
12512 if (STRCMP(arg, "$") == 0)
12513 nr = tabpage_index(NULL) - 1;
12514 else
12515 EMSG2(_(e_invexpr2), arg);
12516 }
12517 }
12518 else
12519 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012520 rettv->vval.v_number = nr;
12521}
12522
12523
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012524static int get_winnr(tabpage_T *tp, typval_T *argvar);
12525
12526/*
12527 * Common code for tabpagewinnr() and winnr().
12528 */
12529 static int
12530get_winnr(tabpage_T *tp, typval_T *argvar)
12531{
12532 win_T *twin;
12533 int nr = 1;
12534 win_T *wp;
12535 char_u *arg;
12536
12537 twin = (tp == curtab) ? curwin : tp->tp_curwin;
12538 if (argvar->v_type != VAR_UNKNOWN)
12539 {
12540 arg = get_tv_string_chk(argvar);
12541 if (arg == NULL)
12542 nr = 0; /* type error; errmsg already given */
12543 else if (STRCMP(arg, "$") == 0)
12544 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
12545 else if (STRCMP(arg, "#") == 0)
12546 {
12547 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
12548 if (twin == NULL)
12549 nr = 0;
12550 }
12551 else
12552 {
12553 EMSG2(_(e_invexpr2), arg);
12554 nr = 0;
12555 }
12556 }
12557
12558 if (nr > 0)
12559 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12560 wp != twin; wp = wp->w_next)
12561 {
12562 if (wp == NULL)
12563 {
12564 /* didn't find it in this tabpage */
12565 nr = 0;
12566 break;
12567 }
12568 ++nr;
12569 }
12570 return nr;
12571}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012572
12573/*
12574 * "tabpagewinnr()" function
12575 */
12576 static void
12577f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
12578{
12579 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012580 tabpage_T *tp;
12581
12582 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12583 if (tp == NULL)
12584 nr = 0;
12585 else
12586 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012587 rettv->vval.v_number = nr;
12588}
12589
12590
12591/*
12592 * "tagfiles()" function
12593 */
12594 static void
12595f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
12596{
12597 char_u *fname;
12598 tagname_T tn;
12599 int first;
12600
12601 if (rettv_list_alloc(rettv) == FAIL)
12602 return;
12603 fname = alloc(MAXPATHL);
12604 if (fname == NULL)
12605 return;
12606
12607 for (first = TRUE; ; first = FALSE)
12608 if (get_tagfname(&tn, first, fname) == FAIL
12609 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
12610 break;
12611 tagname_free(&tn);
12612 vim_free(fname);
12613}
12614
12615/*
12616 * "taglist()" function
12617 */
12618 static void
12619f_taglist(typval_T *argvars, typval_T *rettv)
12620{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012621 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012622 char_u *tag_pattern;
12623
12624 tag_pattern = get_tv_string(&argvars[0]);
12625
12626 rettv->vval.v_number = FALSE;
12627 if (*tag_pattern == NUL)
12628 return;
12629
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012630 if (argvars[1].v_type != VAR_UNKNOWN)
12631 fname = get_tv_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012632 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012633 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012634}
12635
12636/*
12637 * "tempname()" function
12638 */
12639 static void
12640f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
12641{
12642 static int x = 'A';
12643
12644 rettv->v_type = VAR_STRING;
12645 rettv->vval.v_string = vim_tempname(x, FALSE);
12646
12647 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
12648 * names. Skip 'I' and 'O', they are used for shell redirection. */
12649 do
12650 {
12651 if (x == 'Z')
12652 x = '0';
12653 else if (x == '9')
12654 x = 'A';
12655 else
12656 {
12657#ifdef EBCDIC
12658 if (x == 'I')
12659 x = 'J';
12660 else if (x == 'R')
12661 x = 'S';
12662 else
12663#endif
12664 ++x;
12665 }
12666 } while (x == 'I' || x == 'O');
12667}
12668
12669#ifdef FEAT_FLOAT
12670/*
12671 * "tan()" function
12672 */
12673 static void
12674f_tan(typval_T *argvars, typval_T *rettv)
12675{
12676 float_T f = 0.0;
12677
12678 rettv->v_type = VAR_FLOAT;
12679 if (get_float_arg(argvars, &f) == OK)
12680 rettv->vval.v_float = tan(f);
12681 else
12682 rettv->vval.v_float = 0.0;
12683}
12684
12685/*
12686 * "tanh()" function
12687 */
12688 static void
12689f_tanh(typval_T *argvars, typval_T *rettv)
12690{
12691 float_T f = 0.0;
12692
12693 rettv->v_type = VAR_FLOAT;
12694 if (get_float_arg(argvars, &f) == OK)
12695 rettv->vval.v_float = tanh(f);
12696 else
12697 rettv->vval.v_float = 0.0;
12698}
12699#endif
12700
12701/*
12702 * "test_alloc_fail(id, countdown, repeat)" function
12703 */
12704 static void
12705f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
12706{
12707 if (argvars[0].v_type != VAR_NUMBER
12708 || argvars[0].vval.v_number <= 0
12709 || argvars[1].v_type != VAR_NUMBER
12710 || argvars[1].vval.v_number < 0
12711 || argvars[2].v_type != VAR_NUMBER)
12712 EMSG(_(e_invarg));
12713 else
12714 {
12715 alloc_fail_id = argvars[0].vval.v_number;
12716 if (alloc_fail_id >= aid_last)
12717 EMSG(_(e_invarg));
12718 alloc_fail_countdown = argvars[1].vval.v_number;
12719 alloc_fail_repeat = argvars[2].vval.v_number;
12720 did_outofmem_msg = FALSE;
12721 }
12722}
12723
12724/*
12725 * "test_autochdir()"
12726 */
12727 static void
12728f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12729{
12730#if defined(FEAT_AUTOCHDIR)
12731 test_autochdir = TRUE;
12732#endif
12733}
12734
12735/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020012736 * "test_feedinput()"
12737 */
12738 static void
12739f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
12740{
12741#ifdef USE_INPUT_BUF
12742 char_u *val = get_tv_string_chk(&argvars[0]);
12743
12744 if (val != NULL)
12745 {
12746 trash_input_buf();
12747 add_to_input_buf_csi(val, (int)STRLEN(val));
12748 }
12749#endif
12750}
12751
12752/*
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012753 * "test_disable({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012754 */
12755 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012756f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012757{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012758 char_u *name = (char_u *)"";
12759 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012760 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012761
12762 if (argvars[0].v_type != VAR_STRING
12763 || (argvars[1].v_type) != VAR_NUMBER)
12764 EMSG(_(e_invarg));
12765 else
12766 {
12767 name = get_tv_string_chk(&argvars[0]);
12768 val = (int)get_tv_number(&argvars[1]);
12769
12770 if (STRCMP(name, (char_u *)"redraw") == 0)
12771 disable_redraw_for_testing = val;
12772 else if (STRCMP(name, (char_u *)"char_avail") == 0)
12773 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012774 else if (STRCMP(name, (char_u *)"starting") == 0)
12775 {
12776 if (val)
12777 {
12778 if (save_starting < 0)
12779 save_starting = starting;
12780 starting = 0;
12781 }
12782 else
12783 {
12784 starting = save_starting;
12785 save_starting = -1;
12786 }
12787 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012788 else if (STRCMP(name, (char_u *)"ALL") == 0)
12789 {
12790 disable_char_avail_for_testing = FALSE;
12791 disable_redraw_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012792 if (save_starting >= 0)
12793 {
12794 starting = save_starting;
12795 save_starting = -1;
12796 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012797 }
12798 else
12799 EMSG2(_(e_invarg2), name);
12800 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012801}
12802
12803/*
12804 * "test_garbagecollect_now()" function
12805 */
12806 static void
12807f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12808{
12809 /* This is dangerous, any Lists and Dicts used internally may be freed
12810 * while still in use. */
12811 garbage_collect(TRUE);
12812}
12813
Bram Moolenaare0c31f62017-03-01 15:07:05 +010012814/*
12815 * "test_ignore_error()" function
12816 */
12817 static void
12818f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
12819{
12820 ignore_error_for_testing(get_tv_string(&argvars[0]));
12821}
12822
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012823#ifdef FEAT_JOB_CHANNEL
12824 static void
12825f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
12826{
12827 rettv->v_type = VAR_CHANNEL;
12828 rettv->vval.v_channel = NULL;
12829}
12830#endif
12831
12832 static void
12833f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
12834{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012835 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012836}
12837
12838#ifdef FEAT_JOB_CHANNEL
12839 static void
12840f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
12841{
12842 rettv->v_type = VAR_JOB;
12843 rettv->vval.v_job = NULL;
12844}
12845#endif
12846
12847 static void
12848f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
12849{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012850 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012851}
12852
12853 static void
12854f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
12855{
12856 rettv->v_type = VAR_PARTIAL;
12857 rettv->vval.v_partial = NULL;
12858}
12859
12860 static void
12861f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
12862{
12863 rettv->v_type = VAR_STRING;
12864 rettv->vval.v_string = NULL;
12865}
12866
12867 static void
12868f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
12869{
12870 time_for_testing = (time_t)get_tv_number(&argvars[0]);
12871}
12872
12873#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
12874/*
12875 * Get a callback from "arg". It can be a Funcref or a function name.
12876 * When "arg" is zero return an empty string.
12877 * Return NULL for an invalid argument.
12878 */
12879 char_u *
12880get_callback(typval_T *arg, partial_T **pp)
12881{
12882 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
12883 {
12884 *pp = arg->vval.v_partial;
12885 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012886 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012887 }
12888 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012889 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012890 {
12891 func_ref(arg->vval.v_string);
12892 return arg->vval.v_string;
12893 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012894 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
12895 return (char_u *)"";
12896 EMSG(_("E921: Invalid callback argument"));
12897 return NULL;
12898}
12899
12900/*
12901 * Unref/free "callback" and "partial" retured by get_callback().
12902 */
12903 void
12904free_callback(char_u *callback, partial_T *partial)
12905{
12906 if (partial != NULL)
12907 partial_unref(partial);
12908 else if (callback != NULL)
12909 {
12910 func_unref(callback);
12911 vim_free(callback);
12912 }
12913}
12914#endif
12915
12916#ifdef FEAT_TIMERS
12917/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020012918 * "timer_info([timer])" function
12919 */
12920 static void
12921f_timer_info(typval_T *argvars, typval_T *rettv)
12922{
12923 timer_T *timer = NULL;
12924
12925 if (rettv_list_alloc(rettv) != OK)
12926 return;
12927 if (argvars[0].v_type != VAR_UNKNOWN)
12928 {
12929 if (argvars[0].v_type != VAR_NUMBER)
12930 EMSG(_(e_number_exp));
12931 else
12932 {
12933 timer = find_timer((int)get_tv_number(&argvars[0]));
12934 if (timer != NULL)
12935 add_timer_info(rettv, timer);
12936 }
12937 }
12938 else
12939 add_timer_info_all(rettv);
12940}
12941
12942/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012943 * "timer_pause(timer, paused)" function
12944 */
12945 static void
12946f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
12947{
12948 timer_T *timer = NULL;
12949 int paused = (int)get_tv_number(&argvars[1]);
12950
12951 if (argvars[0].v_type != VAR_NUMBER)
12952 EMSG(_(e_number_exp));
12953 else
12954 {
12955 timer = find_timer((int)get_tv_number(&argvars[0]));
12956 if (timer != NULL)
12957 timer->tr_paused = paused;
12958 }
12959}
12960
12961/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012962 * "timer_start(time, callback [, options])" function
12963 */
12964 static void
12965f_timer_start(typval_T *argvars, typval_T *rettv)
12966{
Bram Moolenaar75537a92016-09-05 22:45:28 +020012967 long msec = (long)get_tv_number(&argvars[0]);
12968 timer_T *timer;
12969 int repeat = 0;
12970 char_u *callback;
12971 dict_T *dict;
12972 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012973
Bram Moolenaar75537a92016-09-05 22:45:28 +020012974 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012975 if (check_secure())
12976 return;
12977 if (argvars[2].v_type != VAR_UNKNOWN)
12978 {
12979 if (argvars[2].v_type != VAR_DICT
12980 || (dict = argvars[2].vval.v_dict) == NULL)
12981 {
12982 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
12983 return;
12984 }
12985 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
12986 repeat = get_dict_number(dict, (char_u *)"repeat");
12987 }
12988
Bram Moolenaar75537a92016-09-05 22:45:28 +020012989 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012990 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020012991 return;
12992
12993 timer = create_timer(msec, repeat);
12994 if (timer == NULL)
12995 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012996 else
12997 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020012998 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020012999 timer->tr_callback = vim_strsave(callback);
13000 else
13001 /* pointer into the partial */
13002 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020013003 timer->tr_partial = partial;
13004 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013005 }
13006}
13007
13008/*
13009 * "timer_stop(timer)" function
13010 */
13011 static void
13012f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
13013{
13014 timer_T *timer;
13015
13016 if (argvars[0].v_type != VAR_NUMBER)
13017 {
13018 EMSG(_(e_number_exp));
13019 return;
13020 }
13021 timer = find_timer((int)get_tv_number(&argvars[0]));
13022 if (timer != NULL)
13023 stop_timer(timer);
13024}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013025
13026/*
13027 * "timer_stopall()" function
13028 */
13029 static void
13030f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13031{
13032 stop_all_timers();
13033}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013034#endif
13035
13036/*
13037 * "tolower(string)" function
13038 */
13039 static void
13040f_tolower(typval_T *argvars, typval_T *rettv)
13041{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013042 rettv->v_type = VAR_STRING;
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +010013043 rettv->vval.v_string = strlow_save(get_tv_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013044}
13045
13046/*
13047 * "toupper(string)" function
13048 */
13049 static void
13050f_toupper(typval_T *argvars, typval_T *rettv)
13051{
13052 rettv->v_type = VAR_STRING;
13053 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
13054}
13055
13056/*
13057 * "tr(string, fromstr, tostr)" function
13058 */
13059 static void
13060f_tr(typval_T *argvars, typval_T *rettv)
13061{
13062 char_u *in_str;
13063 char_u *fromstr;
13064 char_u *tostr;
13065 char_u *p;
13066#ifdef FEAT_MBYTE
13067 int inlen;
13068 int fromlen;
13069 int tolen;
13070 int idx;
13071 char_u *cpstr;
13072 int cplen;
13073 int first = TRUE;
13074#endif
13075 char_u buf[NUMBUFLEN];
13076 char_u buf2[NUMBUFLEN];
13077 garray_T ga;
13078
13079 in_str = get_tv_string(&argvars[0]);
13080 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
13081 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
13082
13083 /* Default return value: empty string. */
13084 rettv->v_type = VAR_STRING;
13085 rettv->vval.v_string = NULL;
13086 if (fromstr == NULL || tostr == NULL)
13087 return; /* type error; errmsg already given */
13088 ga_init2(&ga, (int)sizeof(char), 80);
13089
13090#ifdef FEAT_MBYTE
13091 if (!has_mbyte)
13092#endif
13093 /* not multi-byte: fromstr and tostr must be the same length */
13094 if (STRLEN(fromstr) != STRLEN(tostr))
13095 {
13096#ifdef FEAT_MBYTE
13097error:
13098#endif
13099 EMSG2(_(e_invarg2), fromstr);
13100 ga_clear(&ga);
13101 return;
13102 }
13103
13104 /* fromstr and tostr have to contain the same number of chars */
13105 while (*in_str != NUL)
13106 {
13107#ifdef FEAT_MBYTE
13108 if (has_mbyte)
13109 {
13110 inlen = (*mb_ptr2len)(in_str);
13111 cpstr = in_str;
13112 cplen = inlen;
13113 idx = 0;
13114 for (p = fromstr; *p != NUL; p += fromlen)
13115 {
13116 fromlen = (*mb_ptr2len)(p);
13117 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
13118 {
13119 for (p = tostr; *p != NUL; p += tolen)
13120 {
13121 tolen = (*mb_ptr2len)(p);
13122 if (idx-- == 0)
13123 {
13124 cplen = tolen;
13125 cpstr = p;
13126 break;
13127 }
13128 }
13129 if (*p == NUL) /* tostr is shorter than fromstr */
13130 goto error;
13131 break;
13132 }
13133 ++idx;
13134 }
13135
13136 if (first && cpstr == in_str)
13137 {
13138 /* Check that fromstr and tostr have the same number of
13139 * (multi-byte) characters. Done only once when a character
13140 * of in_str doesn't appear in fromstr. */
13141 first = FALSE;
13142 for (p = tostr; *p != NUL; p += tolen)
13143 {
13144 tolen = (*mb_ptr2len)(p);
13145 --idx;
13146 }
13147 if (idx != 0)
13148 goto error;
13149 }
13150
13151 (void)ga_grow(&ga, cplen);
13152 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
13153 ga.ga_len += cplen;
13154
13155 in_str += inlen;
13156 }
13157 else
13158#endif
13159 {
13160 /* When not using multi-byte chars we can do it faster. */
13161 p = vim_strchr(fromstr, *in_str);
13162 if (p != NULL)
13163 ga_append(&ga, tostr[p - fromstr]);
13164 else
13165 ga_append(&ga, *in_str);
13166 ++in_str;
13167 }
13168 }
13169
13170 /* add a terminating NUL */
13171 (void)ga_grow(&ga, 1);
13172 ga_append(&ga, NUL);
13173
13174 rettv->vval.v_string = ga.ga_data;
13175}
13176
13177#ifdef FEAT_FLOAT
13178/*
13179 * "trunc({float})" function
13180 */
13181 static void
13182f_trunc(typval_T *argvars, typval_T *rettv)
13183{
13184 float_T f = 0.0;
13185
13186 rettv->v_type = VAR_FLOAT;
13187 if (get_float_arg(argvars, &f) == OK)
13188 /* trunc() is not in C90, use floor() or ceil() instead. */
13189 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
13190 else
13191 rettv->vval.v_float = 0.0;
13192}
13193#endif
13194
13195/*
13196 * "type(expr)" function
13197 */
13198 static void
13199f_type(typval_T *argvars, typval_T *rettv)
13200{
13201 int n = -1;
13202
13203 switch (argvars[0].v_type)
13204 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020013205 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
13206 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013207 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020013208 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
13209 case VAR_LIST: n = VAR_TYPE_LIST; break;
13210 case VAR_DICT: n = VAR_TYPE_DICT; break;
13211 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013212 case VAR_SPECIAL:
13213 if (argvars[0].vval.v_number == VVAL_FALSE
13214 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020013215 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013216 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020013217 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013218 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020013219 case VAR_JOB: n = VAR_TYPE_JOB; break;
13220 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013221 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010013222 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013223 n = -1;
13224 break;
13225 }
13226 rettv->vval.v_number = n;
13227}
13228
13229/*
13230 * "undofile(name)" function
13231 */
13232 static void
13233f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
13234{
13235 rettv->v_type = VAR_STRING;
13236#ifdef FEAT_PERSISTENT_UNDO
13237 {
13238 char_u *fname = get_tv_string(&argvars[0]);
13239
13240 if (*fname == NUL)
13241 {
13242 /* If there is no file name there will be no undo file. */
13243 rettv->vval.v_string = NULL;
13244 }
13245 else
13246 {
13247 char_u *ffname = FullName_save(fname, FALSE);
13248
13249 if (ffname != NULL)
13250 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
13251 vim_free(ffname);
13252 }
13253 }
13254#else
13255 rettv->vval.v_string = NULL;
13256#endif
13257}
13258
13259/*
13260 * "undotree()" function
13261 */
13262 static void
13263f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
13264{
13265 if (rettv_dict_alloc(rettv) == OK)
13266 {
13267 dict_T *dict = rettv->vval.v_dict;
13268 list_T *list;
13269
13270 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
13271 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
13272 dict_add_nr_str(dict, "save_last",
13273 (long)curbuf->b_u_save_nr_last, NULL);
13274 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
13275 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
13276 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
13277
13278 list = list_alloc();
13279 if (list != NULL)
13280 {
13281 u_eval_tree(curbuf->b_u_oldhead, list);
13282 dict_add_list(dict, "entries", list);
13283 }
13284 }
13285}
13286
13287/*
13288 * "values(dict)" function
13289 */
13290 static void
13291f_values(typval_T *argvars, typval_T *rettv)
13292{
13293 dict_list(argvars, rettv, 1);
13294}
13295
13296/*
13297 * "virtcol(string)" function
13298 */
13299 static void
13300f_virtcol(typval_T *argvars, typval_T *rettv)
13301{
13302 colnr_T vcol = 0;
13303 pos_T *fp;
13304 int fnum = curbuf->b_fnum;
13305
13306 fp = var2fpos(&argvars[0], FALSE, &fnum);
13307 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
13308 && fnum == curbuf->b_fnum)
13309 {
13310 getvvcol(curwin, fp, NULL, NULL, &vcol);
13311 ++vcol;
13312 }
13313
13314 rettv->vval.v_number = vcol;
13315}
13316
13317/*
13318 * "visualmode()" function
13319 */
13320 static void
13321f_visualmode(typval_T *argvars, typval_T *rettv)
13322{
13323 char_u str[2];
13324
13325 rettv->v_type = VAR_STRING;
13326 str[0] = curbuf->b_visual_mode_eval;
13327 str[1] = NUL;
13328 rettv->vval.v_string = vim_strsave(str);
13329
13330 /* A non-zero number or non-empty string argument: reset mode. */
13331 if (non_zero_arg(&argvars[0]))
13332 curbuf->b_visual_mode_eval = NUL;
13333}
13334
13335/*
13336 * "wildmenumode()" function
13337 */
13338 static void
13339f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13340{
13341#ifdef FEAT_WILDMENU
13342 if (wild_menu_showing)
13343 rettv->vval.v_number = 1;
13344#endif
13345}
13346
13347/*
13348 * "winbufnr(nr)" function
13349 */
13350 static void
13351f_winbufnr(typval_T *argvars, typval_T *rettv)
13352{
13353 win_T *wp;
13354
13355 wp = find_win_by_nr(&argvars[0], NULL);
13356 if (wp == NULL)
13357 rettv->vval.v_number = -1;
13358 else
13359 rettv->vval.v_number = wp->w_buffer->b_fnum;
13360}
13361
13362/*
13363 * "wincol()" function
13364 */
13365 static void
13366f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
13367{
13368 validate_cursor();
13369 rettv->vval.v_number = curwin->w_wcol + 1;
13370}
13371
13372/*
13373 * "winheight(nr)" function
13374 */
13375 static void
13376f_winheight(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_height;
13385}
13386
13387/*
13388 * "winline()" function
13389 */
13390 static void
13391f_winline(typval_T *argvars UNUSED, typval_T *rettv)
13392{
13393 validate_cursor();
13394 rettv->vval.v_number = curwin->w_wrow + 1;
13395}
13396
13397/*
13398 * "winnr()" function
13399 */
13400 static void
13401f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
13402{
13403 int nr = 1;
13404
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013405 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013406 rettv->vval.v_number = nr;
13407}
13408
13409/*
13410 * "winrestcmd()" function
13411 */
13412 static void
13413f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
13414{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013415 win_T *wp;
13416 int winnr = 1;
13417 garray_T ga;
13418 char_u buf[50];
13419
13420 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020013421 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013422 {
13423 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
13424 ga_concat(&ga, buf);
13425 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
13426 ga_concat(&ga, buf);
13427 ++winnr;
13428 }
13429 ga_append(&ga, NUL);
13430
13431 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013432 rettv->v_type = VAR_STRING;
13433}
13434
13435/*
13436 * "winrestview()" function
13437 */
13438 static void
13439f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
13440{
13441 dict_T *dict;
13442
13443 if (argvars[0].v_type != VAR_DICT
13444 || (dict = argvars[0].vval.v_dict) == NULL)
13445 EMSG(_(e_invarg));
13446 else
13447 {
13448 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
13449 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
13450 if (dict_find(dict, (char_u *)"col", -1) != NULL)
13451 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
13452#ifdef FEAT_VIRTUALEDIT
13453 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
13454 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
13455#endif
13456 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
13457 {
13458 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
13459 curwin->w_set_curswant = FALSE;
13460 }
13461
13462 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
13463 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
13464#ifdef FEAT_DIFF
13465 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
13466 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
13467#endif
13468 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
13469 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
13470 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
13471 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
13472
13473 check_cursor();
13474 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020013475 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013476 changed_window_setting();
13477
13478 if (curwin->w_topline <= 0)
13479 curwin->w_topline = 1;
13480 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
13481 curwin->w_topline = curbuf->b_ml.ml_line_count;
13482#ifdef FEAT_DIFF
13483 check_topfill(curwin, TRUE);
13484#endif
13485 }
13486}
13487
13488/*
13489 * "winsaveview()" function
13490 */
13491 static void
13492f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
13493{
13494 dict_T *dict;
13495
13496 if (rettv_dict_alloc(rettv) == FAIL)
13497 return;
13498 dict = rettv->vval.v_dict;
13499
13500 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
13501 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
13502#ifdef FEAT_VIRTUALEDIT
13503 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
13504#endif
13505 update_curswant();
13506 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
13507
13508 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
13509#ifdef FEAT_DIFF
13510 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
13511#endif
13512 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
13513 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
13514}
13515
13516/*
13517 * "winwidth(nr)" function
13518 */
13519 static void
13520f_winwidth(typval_T *argvars, typval_T *rettv)
13521{
13522 win_T *wp;
13523
13524 wp = find_win_by_nr(&argvars[0], NULL);
13525 if (wp == NULL)
13526 rettv->vval.v_number = -1;
13527 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013528 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013529}
13530
13531/*
13532 * "wordcount()" function
13533 */
13534 static void
13535f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
13536{
13537 if (rettv_dict_alloc(rettv) == FAIL)
13538 return;
13539 cursor_pos_info(rettv->vval.v_dict);
13540}
13541
13542/*
13543 * "writefile()" function
13544 */
13545 static void
13546f_writefile(typval_T *argvars, typval_T *rettv)
13547{
13548 int binary = FALSE;
13549 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013550#ifdef HAVE_FSYNC
13551 int do_fsync = p_fs;
13552#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013553 char_u *fname;
13554 FILE *fd;
13555 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013556 listitem_T *li;
13557 list_T *list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013558
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013559 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013560 if (check_restricted() || check_secure())
13561 return;
13562
13563 if (argvars[0].v_type != VAR_LIST)
13564 {
13565 EMSG2(_(e_listarg), "writefile()");
13566 return;
13567 }
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013568 list = argvars[0].vval.v_list;
13569 if (list == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013570 return;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013571 for (li = list->lv_first; li != NULL; li = li->li_next)
13572 if (get_tv_string_chk(&li->li_tv) == NULL)
13573 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013574
13575 if (argvars[2].v_type != VAR_UNKNOWN)
13576 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013577 char_u *arg2 = get_tv_string_chk(&argvars[2]);
13578
13579 if (arg2 == NULL)
13580 return;
13581 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013582 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013583 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013584 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013585#ifdef HAVE_FSYNC
13586 if (vim_strchr(arg2, 's') != NULL)
13587 do_fsync = TRUE;
13588 else if (vim_strchr(arg2, 'S') != NULL)
13589 do_fsync = FALSE;
13590#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013591 }
13592
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013593 fname = get_tv_string_chk(&argvars[1]);
13594 if (fname == NULL)
13595 return;
13596
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013597 /* Always open the file in binary mode, library functions have a mind of
13598 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013599 if (*fname == NUL || (fd = mch_fopen((char *)fname,
13600 append ? APPENDBIN : WRITEBIN)) == NULL)
13601 {
13602 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
13603 ret = -1;
13604 }
13605 else
13606 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013607 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013608 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013609#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010013610 else if (do_fsync)
13611 /* Ignore the error, the user wouldn't know what to do about it.
13612 * May happen for a device. */
13613 ignored = fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013614#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013615 fclose(fd);
13616 }
13617
13618 rettv->vval.v_number = ret;
13619}
13620
13621/*
13622 * "xor(expr, expr)" function
13623 */
13624 static void
13625f_xor(typval_T *argvars, typval_T *rettv)
13626{
13627 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
13628 ^ get_tv_number_chk(&argvars[1], NULL);
13629}
13630
13631
13632#endif /* FEAT_EVAL */