blob: 6076b035aba2aeade077b8bfe90310570b015822 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalfunc.c: Builtin functions
12 */
13#define USING_FLOAT_STUFF
14
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef VMS
24# include <float.h>
25#endif
26
Bram Moolenaard0573012017-10-28 21:11:06 +020027#ifdef MACOS_X
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020028# include <time.h> /* for time_t */
29#endif
30
31static char *e_listarg = N_("E686: Argument of %s must be a List");
32#ifdef FEAT_QUICKFIX
33static char *e_stringreq = N_("E928: String required");
34#endif
35
36#ifdef FEAT_FLOAT
37static void f_abs(typval_T *argvars, typval_T *rettv);
38static void f_acos(typval_T *argvars, typval_T *rettv);
39#endif
40static void f_add(typval_T *argvars, typval_T *rettv);
41static void f_and(typval_T *argvars, typval_T *rettv);
42static void f_append(typval_T *argvars, typval_T *rettv);
43static void f_argc(typval_T *argvars, typval_T *rettv);
44static void f_argidx(typval_T *argvars, typval_T *rettv);
45static void f_arglistid(typval_T *argvars, typval_T *rettv);
46static void f_argv(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +010047static void f_assert_beeps(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020048static void f_assert_equal(typval_T *argvars, typval_T *rettv);
Bram Moolenaard96ff162018-02-18 22:13:29 +010049static void f_assert_equalfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020050static void f_assert_exception(typval_T *argvars, typval_T *rettv);
51static void f_assert_fails(typval_T *argvars, typval_T *rettv);
52static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaar61c04492016-07-23 15:35:35 +020053static void f_assert_inrange(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020054static void f_assert_match(typval_T *argvars, typval_T *rettv);
55static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
56static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar42205552017-03-18 19:42:22 +010057static void f_assert_report(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020058static void f_assert_true(typval_T *argvars, typval_T *rettv);
59#ifdef FEAT_FLOAT
60static void f_asin(typval_T *argvars, typval_T *rettv);
61static void f_atan(typval_T *argvars, typval_T *rettv);
62static void f_atan2(typval_T *argvars, typval_T *rettv);
63#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010064#ifdef FEAT_BEVAL
65static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010066# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010067static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010068# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010069#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020070static void f_browse(typval_T *argvars, typval_T *rettv);
71static void f_browsedir(typval_T *argvars, typval_T *rettv);
72static void f_bufexists(typval_T *argvars, typval_T *rettv);
73static void f_buflisted(typval_T *argvars, typval_T *rettv);
74static void f_bufloaded(typval_T *argvars, typval_T *rettv);
75static void f_bufname(typval_T *argvars, typval_T *rettv);
76static void f_bufnr(typval_T *argvars, typval_T *rettv);
77static void f_bufwinid(typval_T *argvars, typval_T *rettv);
78static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
79static void f_byte2line(typval_T *argvars, typval_T *rettv);
80static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
81static void f_byteidx(typval_T *argvars, typval_T *rettv);
82static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
83static void f_call(typval_T *argvars, typval_T *rettv);
84#ifdef FEAT_FLOAT
85static void f_ceil(typval_T *argvars, typval_T *rettv);
86#endif
87#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +010088static void f_ch_canread(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020089static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0874a832016-09-01 15:11:51 +020090static void f_ch_close_in(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020091static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
92static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
93static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
94static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
95static void f_ch_info(typval_T *argvars, typval_T *rettv);
96static void f_ch_log(typval_T *argvars, typval_T *rettv);
97static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
98static void f_ch_open(typval_T *argvars, typval_T *rettv);
99static void f_ch_read(typval_T *argvars, typval_T *rettv);
100static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
101static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
102static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
103static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
104static void f_ch_status(typval_T *argvars, typval_T *rettv);
105#endif
106static void f_changenr(typval_T *argvars, typval_T *rettv);
107static void f_char2nr(typval_T *argvars, typval_T *rettv);
108static void f_cindent(typval_T *argvars, typval_T *rettv);
109static void f_clearmatches(typval_T *argvars, typval_T *rettv);
110static void f_col(typval_T *argvars, typval_T *rettv);
111#if defined(FEAT_INS_EXPAND)
112static void f_complete(typval_T *argvars, typval_T *rettv);
113static void f_complete_add(typval_T *argvars, typval_T *rettv);
114static void f_complete_check(typval_T *argvars, typval_T *rettv);
115#endif
116static void f_confirm(typval_T *argvars, typval_T *rettv);
117static void f_copy(typval_T *argvars, typval_T *rettv);
118#ifdef FEAT_FLOAT
119static void f_cos(typval_T *argvars, typval_T *rettv);
120static void f_cosh(typval_T *argvars, typval_T *rettv);
121#endif
122static void f_count(typval_T *argvars, typval_T *rettv);
123static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
124static void f_cursor(typval_T *argsvars, typval_T *rettv);
125static void f_deepcopy(typval_T *argvars, typval_T *rettv);
126static void f_delete(typval_T *argvars, typval_T *rettv);
127static void f_did_filetype(typval_T *argvars, typval_T *rettv);
128static void f_diff_filler(typval_T *argvars, typval_T *rettv);
129static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
130static void f_empty(typval_T *argvars, typval_T *rettv);
131static void f_escape(typval_T *argvars, typval_T *rettv);
132static void f_eval(typval_T *argvars, typval_T *rettv);
133static void f_eventhandler(typval_T *argvars, typval_T *rettv);
134static void f_executable(typval_T *argvars, typval_T *rettv);
135static void f_execute(typval_T *argvars, typval_T *rettv);
136static void f_exepath(typval_T *argvars, typval_T *rettv);
137static void f_exists(typval_T *argvars, typval_T *rettv);
138#ifdef FEAT_FLOAT
139static void f_exp(typval_T *argvars, typval_T *rettv);
140#endif
141static void f_expand(typval_T *argvars, typval_T *rettv);
142static void f_extend(typval_T *argvars, typval_T *rettv);
143static void f_feedkeys(typval_T *argvars, typval_T *rettv);
144static void f_filereadable(typval_T *argvars, typval_T *rettv);
145static void f_filewritable(typval_T *argvars, typval_T *rettv);
146static void f_filter(typval_T *argvars, typval_T *rettv);
147static void f_finddir(typval_T *argvars, typval_T *rettv);
148static void f_findfile(typval_T *argvars, typval_T *rettv);
149#ifdef FEAT_FLOAT
150static void f_float2nr(typval_T *argvars, typval_T *rettv);
151static void f_floor(typval_T *argvars, typval_T *rettv);
152static void f_fmod(typval_T *argvars, typval_T *rettv);
153#endif
154static void f_fnameescape(typval_T *argvars, typval_T *rettv);
155static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
156static void f_foldclosed(typval_T *argvars, typval_T *rettv);
157static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
158static void f_foldlevel(typval_T *argvars, typval_T *rettv);
159static void f_foldtext(typval_T *argvars, typval_T *rettv);
160static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
161static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200162static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200163static void f_function(typval_T *argvars, typval_T *rettv);
164static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
165static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200166static void f_getbufinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200167static void f_getbufline(typval_T *argvars, typval_T *rettv);
168static void f_getbufvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100169static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200170static void f_getchar(typval_T *argvars, typval_T *rettv);
171static void f_getcharmod(typval_T *argvars, typval_T *rettv);
172static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
173static void f_getcmdline(typval_T *argvars, typval_T *rettv);
174#if defined(FEAT_CMDL_COMPL)
175static void f_getcompletion(typval_T *argvars, typval_T *rettv);
176#endif
177static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
178static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
179static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
180static void f_getcwd(typval_T *argvars, typval_T *rettv);
181static void f_getfontname(typval_T *argvars, typval_T *rettv);
182static void f_getfperm(typval_T *argvars, typval_T *rettv);
183static void f_getfsize(typval_T *argvars, typval_T *rettv);
184static void f_getftime(typval_T *argvars, typval_T *rettv);
185static void f_getftype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +0100186static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200187static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200188static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200189static void f_getmatches(typval_T *argvars, typval_T *rettv);
190static void f_getpid(typval_T *argvars, typval_T *rettv);
191static void f_getcurpos(typval_T *argvars, typval_T *rettv);
192static void f_getpos(typval_T *argvars, typval_T *rettv);
193static void f_getqflist(typval_T *argvars, typval_T *rettv);
194static void f_getreg(typval_T *argvars, typval_T *rettv);
195static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200196static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200197static void f_gettabvar(typval_T *argvars, typval_T *rettv);
198static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200199static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100200static void f_getwinpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200201static void f_getwinposx(typval_T *argvars, typval_T *rettv);
202static void f_getwinposy(typval_T *argvars, typval_T *rettv);
203static void f_getwinvar(typval_T *argvars, typval_T *rettv);
204static void f_glob(typval_T *argvars, typval_T *rettv);
205static void f_globpath(typval_T *argvars, typval_T *rettv);
206static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
207static void f_has(typval_T *argvars, typval_T *rettv);
208static void f_has_key(typval_T *argvars, typval_T *rettv);
209static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
210static void f_hasmapto(typval_T *argvars, typval_T *rettv);
211static void f_histadd(typval_T *argvars, typval_T *rettv);
212static void f_histdel(typval_T *argvars, typval_T *rettv);
213static void f_histget(typval_T *argvars, typval_T *rettv);
214static void f_histnr(typval_T *argvars, typval_T *rettv);
215static void f_hlID(typval_T *argvars, typval_T *rettv);
216static void f_hlexists(typval_T *argvars, typval_T *rettv);
217static void f_hostname(typval_T *argvars, typval_T *rettv);
218static void f_iconv(typval_T *argvars, typval_T *rettv);
219static void f_indent(typval_T *argvars, typval_T *rettv);
220static void f_index(typval_T *argvars, typval_T *rettv);
221static void f_input(typval_T *argvars, typval_T *rettv);
222static void f_inputdialog(typval_T *argvars, typval_T *rettv);
223static void f_inputlist(typval_T *argvars, typval_T *rettv);
224static void f_inputrestore(typval_T *argvars, typval_T *rettv);
225static void f_inputsave(typval_T *argvars, typval_T *rettv);
226static void f_inputsecret(typval_T *argvars, typval_T *rettv);
227static void f_insert(typval_T *argvars, typval_T *rettv);
228static void f_invert(typval_T *argvars, typval_T *rettv);
229static void f_isdirectory(typval_T *argvars, typval_T *rettv);
230static void f_islocked(typval_T *argvars, typval_T *rettv);
231#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
232static void f_isnan(typval_T *argvars, typval_T *rettv);
233#endif
234static void f_items(typval_T *argvars, typval_T *rettv);
235#ifdef FEAT_JOB_CHANNEL
236static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
237static void f_job_info(typval_T *argvars, typval_T *rettv);
238static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
239static void f_job_start(typval_T *argvars, typval_T *rettv);
240static void f_job_stop(typval_T *argvars, typval_T *rettv);
241static void f_job_status(typval_T *argvars, typval_T *rettv);
242#endif
243static void f_join(typval_T *argvars, typval_T *rettv);
244static void f_js_decode(typval_T *argvars, typval_T *rettv);
245static void f_js_encode(typval_T *argvars, typval_T *rettv);
246static void f_json_decode(typval_T *argvars, typval_T *rettv);
247static void f_json_encode(typval_T *argvars, typval_T *rettv);
248static void f_keys(typval_T *argvars, typval_T *rettv);
249static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
250static void f_len(typval_T *argvars, typval_T *rettv);
251static void f_libcall(typval_T *argvars, typval_T *rettv);
252static void f_libcallnr(typval_T *argvars, typval_T *rettv);
253static void f_line(typval_T *argvars, typval_T *rettv);
254static void f_line2byte(typval_T *argvars, typval_T *rettv);
255static void f_lispindent(typval_T *argvars, typval_T *rettv);
256static void f_localtime(typval_T *argvars, typval_T *rettv);
257#ifdef FEAT_FLOAT
258static void f_log(typval_T *argvars, typval_T *rettv);
259static void f_log10(typval_T *argvars, typval_T *rettv);
260#endif
261#ifdef FEAT_LUA
262static void f_luaeval(typval_T *argvars, typval_T *rettv);
263#endif
264static void f_map(typval_T *argvars, typval_T *rettv);
265static void f_maparg(typval_T *argvars, typval_T *rettv);
266static void f_mapcheck(typval_T *argvars, typval_T *rettv);
267static void f_match(typval_T *argvars, typval_T *rettv);
268static void f_matchadd(typval_T *argvars, typval_T *rettv);
269static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
270static void f_matcharg(typval_T *argvars, typval_T *rettv);
271static void f_matchdelete(typval_T *argvars, typval_T *rettv);
272static void f_matchend(typval_T *argvars, typval_T *rettv);
273static void f_matchlist(typval_T *argvars, typval_T *rettv);
274static void f_matchstr(typval_T *argvars, typval_T *rettv);
275static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
276static void f_max(typval_T *argvars, typval_T *rettv);
277static void f_min(typval_T *argvars, typval_T *rettv);
278#ifdef vim_mkdir
279static void f_mkdir(typval_T *argvars, typval_T *rettv);
280#endif
281static void f_mode(typval_T *argvars, typval_T *rettv);
282#ifdef FEAT_MZSCHEME
283static void f_mzeval(typval_T *argvars, typval_T *rettv);
284#endif
285static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
286static void f_nr2char(typval_T *argvars, typval_T *rettv);
287static void f_or(typval_T *argvars, typval_T *rettv);
288static void f_pathshorten(typval_T *argvars, typval_T *rettv);
289#ifdef FEAT_PERL
290static void f_perleval(typval_T *argvars, typval_T *rettv);
291#endif
292#ifdef FEAT_FLOAT
293static void f_pow(typval_T *argvars, typval_T *rettv);
294#endif
295static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
296static void f_printf(typval_T *argvars, typval_T *rettv);
297static void f_pumvisible(typval_T *argvars, typval_T *rettv);
298#ifdef FEAT_PYTHON3
299static void f_py3eval(typval_T *argvars, typval_T *rettv);
300#endif
301#ifdef FEAT_PYTHON
302static void f_pyeval(typval_T *argvars, typval_T *rettv);
303#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100304#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
305static void f_pyxeval(typval_T *argvars, typval_T *rettv);
306#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200307static void f_range(typval_T *argvars, typval_T *rettv);
308static void f_readfile(typval_T *argvars, typval_T *rettv);
309static void f_reltime(typval_T *argvars, typval_T *rettv);
310#ifdef FEAT_FLOAT
311static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
312#endif
313static void f_reltimestr(typval_T *argvars, typval_T *rettv);
314static void f_remote_expr(typval_T *argvars, typval_T *rettv);
315static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
316static void f_remote_peek(typval_T *argvars, typval_T *rettv);
317static void f_remote_read(typval_T *argvars, typval_T *rettv);
318static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100319static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200320static void f_remove(typval_T *argvars, typval_T *rettv);
321static void f_rename(typval_T *argvars, typval_T *rettv);
322static void f_repeat(typval_T *argvars, typval_T *rettv);
323static void f_resolve(typval_T *argvars, typval_T *rettv);
324static void f_reverse(typval_T *argvars, typval_T *rettv);
325#ifdef FEAT_FLOAT
326static void f_round(typval_T *argvars, typval_T *rettv);
327#endif
328static void f_screenattr(typval_T *argvars, typval_T *rettv);
329static void f_screenchar(typval_T *argvars, typval_T *rettv);
330static void f_screencol(typval_T *argvars, typval_T *rettv);
331static void f_screenrow(typval_T *argvars, typval_T *rettv);
332static void f_search(typval_T *argvars, typval_T *rettv);
333static void f_searchdecl(typval_T *argvars, typval_T *rettv);
334static void f_searchpair(typval_T *argvars, typval_T *rettv);
335static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
336static void f_searchpos(typval_T *argvars, typval_T *rettv);
337static void f_server2client(typval_T *argvars, typval_T *rettv);
338static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200339static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200340static void f_setbufvar(typval_T *argvars, typval_T *rettv);
341static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
342static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
343static void f_setfperm(typval_T *argvars, typval_T *rettv);
344static void f_setline(typval_T *argvars, typval_T *rettv);
345static void f_setloclist(typval_T *argvars, typval_T *rettv);
346static void f_setmatches(typval_T *argvars, typval_T *rettv);
347static void f_setpos(typval_T *argvars, typval_T *rettv);
348static void f_setqflist(typval_T *argvars, typval_T *rettv);
349static void f_setreg(typval_T *argvars, typval_T *rettv);
350static void f_settabvar(typval_T *argvars, typval_T *rettv);
351static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
352static void f_setwinvar(typval_T *argvars, typval_T *rettv);
353#ifdef FEAT_CRYPT
354static void f_sha256(typval_T *argvars, typval_T *rettv);
355#endif /* FEAT_CRYPT */
356static void f_shellescape(typval_T *argvars, typval_T *rettv);
357static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
358static void f_simplify(typval_T *argvars, typval_T *rettv);
359#ifdef FEAT_FLOAT
360static void f_sin(typval_T *argvars, typval_T *rettv);
361static void f_sinh(typval_T *argvars, typval_T *rettv);
362#endif
363static void f_sort(typval_T *argvars, typval_T *rettv);
364static void f_soundfold(typval_T *argvars, typval_T *rettv);
365static void f_spellbadword(typval_T *argvars, typval_T *rettv);
366static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
367static void f_split(typval_T *argvars, typval_T *rettv);
368#ifdef FEAT_FLOAT
369static void f_sqrt(typval_T *argvars, typval_T *rettv);
370static void f_str2float(typval_T *argvars, typval_T *rettv);
371#endif
372static void f_str2nr(typval_T *argvars, typval_T *rettv);
373static void f_strchars(typval_T *argvars, typval_T *rettv);
374#ifdef HAVE_STRFTIME
375static void f_strftime(typval_T *argvars, typval_T *rettv);
376#endif
377static void f_strgetchar(typval_T *argvars, typval_T *rettv);
378static void f_stridx(typval_T *argvars, typval_T *rettv);
379static void f_string(typval_T *argvars, typval_T *rettv);
380static void f_strlen(typval_T *argvars, typval_T *rettv);
381static void f_strcharpart(typval_T *argvars, typval_T *rettv);
382static void f_strpart(typval_T *argvars, typval_T *rettv);
383static void f_strridx(typval_T *argvars, typval_T *rettv);
384static void f_strtrans(typval_T *argvars, typval_T *rettv);
385static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
386static void f_strwidth(typval_T *argvars, typval_T *rettv);
387static void f_submatch(typval_T *argvars, typval_T *rettv);
388static void f_substitute(typval_T *argvars, typval_T *rettv);
389static void f_synID(typval_T *argvars, typval_T *rettv);
390static void f_synIDattr(typval_T *argvars, typval_T *rettv);
391static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
392static void f_synstack(typval_T *argvars, typval_T *rettv);
393static void f_synconcealed(typval_T *argvars, typval_T *rettv);
394static void f_system(typval_T *argvars, typval_T *rettv);
395static void f_systemlist(typval_T *argvars, typval_T *rettv);
396static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
397static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
398static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
399static void f_taglist(typval_T *argvars, typval_T *rettv);
400static void f_tagfiles(typval_T *argvars, typval_T *rettv);
401static void f_tempname(typval_T *argvars, typval_T *rettv);
402static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
403static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200404static void f_test_feedinput(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100405static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200406static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100407static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200408#ifdef FEAT_JOB_CHANNEL
409static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
410#endif
411static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
412#ifdef FEAT_JOB_CHANNEL
413static void f_test_null_job(typval_T *argvars, typval_T *rettv);
414#endif
415static void f_test_null_list(typval_T *argvars, typval_T *rettv);
416static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
417static void f_test_null_string(typval_T *argvars, typval_T *rettv);
418static void f_test_settime(typval_T *argvars, typval_T *rettv);
419#ifdef FEAT_FLOAT
420static void f_tan(typval_T *argvars, typval_T *rettv);
421static void f_tanh(typval_T *argvars, typval_T *rettv);
422#endif
423#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200424static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200425static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200426static void f_timer_start(typval_T *argvars, typval_T *rettv);
427static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200428static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200429#endif
430static void f_tolower(typval_T *argvars, typval_T *rettv);
431static void f_toupper(typval_T *argvars, typval_T *rettv);
432static void f_tr(typval_T *argvars, typval_T *rettv);
433#ifdef FEAT_FLOAT
434static void f_trunc(typval_T *argvars, typval_T *rettv);
435#endif
436static void f_type(typval_T *argvars, typval_T *rettv);
437static void f_undofile(typval_T *argvars, typval_T *rettv);
438static void f_undotree(typval_T *argvars, typval_T *rettv);
439static void f_uniq(typval_T *argvars, typval_T *rettv);
440static void f_values(typval_T *argvars, typval_T *rettv);
441static void f_virtcol(typval_T *argvars, typval_T *rettv);
442static void f_visualmode(typval_T *argvars, typval_T *rettv);
443static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
444static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
445static void f_win_getid(typval_T *argvars, typval_T *rettv);
446static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
447static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
448static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100449static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200450static void f_winbufnr(typval_T *argvars, typval_T *rettv);
451static void f_wincol(typval_T *argvars, typval_T *rettv);
452static void f_winheight(typval_T *argvars, typval_T *rettv);
453static void f_winline(typval_T *argvars, typval_T *rettv);
454static void f_winnr(typval_T *argvars, typval_T *rettv);
455static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
456static void f_winrestview(typval_T *argvars, typval_T *rettv);
457static void f_winsaveview(typval_T *argvars, typval_T *rettv);
458static void f_winwidth(typval_T *argvars, typval_T *rettv);
459static void f_writefile(typval_T *argvars, typval_T *rettv);
460static void f_wordcount(typval_T *argvars, typval_T *rettv);
461static void f_xor(typval_T *argvars, typval_T *rettv);
462
463/*
464 * Array with names and number of arguments of all internal functions
465 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
466 */
467static struct fst
468{
469 char *f_name; /* function name */
470 char f_min_argc; /* minimal number of arguments */
471 char f_max_argc; /* maximal number of arguments */
472 void (*f_func)(typval_T *args, typval_T *rvar);
473 /* implementation of function */
474} functions[] =
475{
476#ifdef FEAT_FLOAT
477 {"abs", 1, 1, f_abs},
478 {"acos", 1, 1, f_acos}, /* WJMc */
479#endif
480 {"add", 2, 2, f_add},
481 {"and", 2, 2, f_and},
482 {"append", 2, 2, f_append},
483 {"argc", 0, 0, f_argc},
484 {"argidx", 0, 0, f_argidx},
485 {"arglistid", 0, 2, f_arglistid},
486 {"argv", 0, 1, f_argv},
487#ifdef FEAT_FLOAT
488 {"asin", 1, 1, f_asin}, /* WJMc */
489#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100490 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200491 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaard96ff162018-02-18 22:13:29 +0100492 {"assert_equalfile", 2, 2, f_assert_equalfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200493 {"assert_exception", 1, 2, f_assert_exception},
494 {"assert_fails", 1, 2, f_assert_fails},
495 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100496 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200497 {"assert_match", 2, 3, f_assert_match},
498 {"assert_notequal", 2, 3, f_assert_notequal},
499 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100500 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200501 {"assert_true", 1, 2, f_assert_true},
502#ifdef FEAT_FLOAT
503 {"atan", 1, 1, f_atan},
504 {"atan2", 2, 2, f_atan2},
505#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100506#ifdef FEAT_BEVAL
507 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100508# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100509 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100510# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100511#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200512 {"browse", 4, 4, f_browse},
513 {"browsedir", 2, 2, f_browsedir},
514 {"bufexists", 1, 1, f_bufexists},
515 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
516 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
517 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
518 {"buflisted", 1, 1, f_buflisted},
519 {"bufloaded", 1, 1, f_bufloaded},
520 {"bufname", 1, 1, f_bufname},
521 {"bufnr", 1, 2, f_bufnr},
522 {"bufwinid", 1, 1, f_bufwinid},
523 {"bufwinnr", 1, 1, f_bufwinnr},
524 {"byte2line", 1, 1, f_byte2line},
525 {"byteidx", 2, 2, f_byteidx},
526 {"byteidxcomp", 2, 2, f_byteidxcomp},
527 {"call", 2, 3, f_call},
528#ifdef FEAT_FLOAT
529 {"ceil", 1, 1, f_ceil},
530#endif
531#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100532 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200533 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200534 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200535 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
536 {"ch_evalraw", 2, 3, f_ch_evalraw},
537 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
538 {"ch_getjob", 1, 1, f_ch_getjob},
539 {"ch_info", 1, 1, f_ch_info},
540 {"ch_log", 1, 2, f_ch_log},
541 {"ch_logfile", 1, 2, f_ch_logfile},
542 {"ch_open", 1, 2, f_ch_open},
543 {"ch_read", 1, 2, f_ch_read},
544 {"ch_readraw", 1, 2, f_ch_readraw},
545 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
546 {"ch_sendraw", 2, 3, f_ch_sendraw},
547 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200548 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200549#endif
550 {"changenr", 0, 0, f_changenr},
551 {"char2nr", 1, 2, f_char2nr},
552 {"cindent", 1, 1, f_cindent},
553 {"clearmatches", 0, 0, f_clearmatches},
554 {"col", 1, 1, f_col},
555#if defined(FEAT_INS_EXPAND)
556 {"complete", 2, 2, f_complete},
557 {"complete_add", 1, 1, f_complete_add},
558 {"complete_check", 0, 0, f_complete_check},
559#endif
560 {"confirm", 1, 4, f_confirm},
561 {"copy", 1, 1, f_copy},
562#ifdef FEAT_FLOAT
563 {"cos", 1, 1, f_cos},
564 {"cosh", 1, 1, f_cosh},
565#endif
566 {"count", 2, 4, f_count},
567 {"cscope_connection",0,3, f_cscope_connection},
568 {"cursor", 1, 3, f_cursor},
569 {"deepcopy", 1, 2, f_deepcopy},
570 {"delete", 1, 2, f_delete},
571 {"did_filetype", 0, 0, f_did_filetype},
572 {"diff_filler", 1, 1, f_diff_filler},
573 {"diff_hlID", 2, 2, f_diff_hlID},
574 {"empty", 1, 1, f_empty},
575 {"escape", 2, 2, f_escape},
576 {"eval", 1, 1, f_eval},
577 {"eventhandler", 0, 0, f_eventhandler},
578 {"executable", 1, 1, f_executable},
579 {"execute", 1, 2, f_execute},
580 {"exepath", 1, 1, f_exepath},
581 {"exists", 1, 1, f_exists},
582#ifdef FEAT_FLOAT
583 {"exp", 1, 1, f_exp},
584#endif
585 {"expand", 1, 3, f_expand},
586 {"extend", 2, 3, f_extend},
587 {"feedkeys", 1, 2, f_feedkeys},
588 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
589 {"filereadable", 1, 1, f_filereadable},
590 {"filewritable", 1, 1, f_filewritable},
591 {"filter", 2, 2, f_filter},
592 {"finddir", 1, 3, f_finddir},
593 {"findfile", 1, 3, f_findfile},
594#ifdef FEAT_FLOAT
595 {"float2nr", 1, 1, f_float2nr},
596 {"floor", 1, 1, f_floor},
597 {"fmod", 2, 2, f_fmod},
598#endif
599 {"fnameescape", 1, 1, f_fnameescape},
600 {"fnamemodify", 2, 2, f_fnamemodify},
601 {"foldclosed", 1, 1, f_foldclosed},
602 {"foldclosedend", 1, 1, f_foldclosedend},
603 {"foldlevel", 1, 1, f_foldlevel},
604 {"foldtext", 0, 0, f_foldtext},
605 {"foldtextresult", 1, 1, f_foldtextresult},
606 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200607 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200608 {"function", 1, 3, f_function},
609 {"garbagecollect", 0, 1, f_garbagecollect},
610 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200611 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200612 {"getbufline", 2, 3, f_getbufline},
613 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100614 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200615 {"getchar", 0, 1, f_getchar},
616 {"getcharmod", 0, 0, f_getcharmod},
617 {"getcharsearch", 0, 0, f_getcharsearch},
618 {"getcmdline", 0, 0, f_getcmdline},
619 {"getcmdpos", 0, 0, f_getcmdpos},
620 {"getcmdtype", 0, 0, f_getcmdtype},
621 {"getcmdwintype", 0, 0, f_getcmdwintype},
622#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200623 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200624#endif
625 {"getcurpos", 0, 0, f_getcurpos},
626 {"getcwd", 0, 2, f_getcwd},
627 {"getfontname", 0, 1, f_getfontname},
628 {"getfperm", 1, 1, f_getfperm},
629 {"getfsize", 1, 1, f_getfsize},
630 {"getftime", 1, 1, f_getftime},
631 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100632 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200633 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200634 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200635 {"getmatches", 0, 0, f_getmatches},
636 {"getpid", 0, 0, f_getpid},
637 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200638 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200639 {"getreg", 0, 3, f_getreg},
640 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200641 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200642 {"gettabvar", 2, 3, f_gettabvar},
643 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200644 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100645 {"getwinpos", 0, 1, f_getwinpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200646 {"getwinposx", 0, 0, f_getwinposx},
647 {"getwinposy", 0, 0, f_getwinposy},
648 {"getwinvar", 2, 3, f_getwinvar},
649 {"glob", 1, 4, f_glob},
650 {"glob2regpat", 1, 1, f_glob2regpat},
651 {"globpath", 2, 5, f_globpath},
652 {"has", 1, 1, f_has},
653 {"has_key", 2, 2, f_has_key},
654 {"haslocaldir", 0, 2, f_haslocaldir},
655 {"hasmapto", 1, 3, f_hasmapto},
656 {"highlightID", 1, 1, f_hlID}, /* obsolete */
657 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
658 {"histadd", 2, 2, f_histadd},
659 {"histdel", 1, 2, f_histdel},
660 {"histget", 1, 2, f_histget},
661 {"histnr", 1, 1, f_histnr},
662 {"hlID", 1, 1, f_hlID},
663 {"hlexists", 1, 1, f_hlexists},
664 {"hostname", 0, 0, f_hostname},
665 {"iconv", 3, 3, f_iconv},
666 {"indent", 1, 1, f_indent},
667 {"index", 2, 4, f_index},
668 {"input", 1, 3, f_input},
669 {"inputdialog", 1, 3, f_inputdialog},
670 {"inputlist", 1, 1, f_inputlist},
671 {"inputrestore", 0, 0, f_inputrestore},
672 {"inputsave", 0, 0, f_inputsave},
673 {"inputsecret", 1, 2, f_inputsecret},
674 {"insert", 2, 3, f_insert},
675 {"invert", 1, 1, f_invert},
676 {"isdirectory", 1, 1, f_isdirectory},
677 {"islocked", 1, 1, f_islocked},
678#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
679 {"isnan", 1, 1, f_isnan},
680#endif
681 {"items", 1, 1, f_items},
682#ifdef FEAT_JOB_CHANNEL
683 {"job_getchannel", 1, 1, f_job_getchannel},
684 {"job_info", 1, 1, f_job_info},
685 {"job_setoptions", 2, 2, f_job_setoptions},
686 {"job_start", 1, 2, f_job_start},
687 {"job_status", 1, 1, f_job_status},
688 {"job_stop", 1, 2, f_job_stop},
689#endif
690 {"join", 1, 2, f_join},
691 {"js_decode", 1, 1, f_js_decode},
692 {"js_encode", 1, 1, f_js_encode},
693 {"json_decode", 1, 1, f_json_decode},
694 {"json_encode", 1, 1, f_json_encode},
695 {"keys", 1, 1, f_keys},
696 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
697 {"len", 1, 1, f_len},
698 {"libcall", 3, 3, f_libcall},
699 {"libcallnr", 3, 3, f_libcallnr},
700 {"line", 1, 1, f_line},
701 {"line2byte", 1, 1, f_line2byte},
702 {"lispindent", 1, 1, f_lispindent},
703 {"localtime", 0, 0, f_localtime},
704#ifdef FEAT_FLOAT
705 {"log", 1, 1, f_log},
706 {"log10", 1, 1, f_log10},
707#endif
708#ifdef FEAT_LUA
709 {"luaeval", 1, 2, f_luaeval},
710#endif
711 {"map", 2, 2, f_map},
712 {"maparg", 1, 4, f_maparg},
713 {"mapcheck", 1, 3, f_mapcheck},
714 {"match", 2, 4, f_match},
715 {"matchadd", 2, 5, f_matchadd},
716 {"matchaddpos", 2, 5, f_matchaddpos},
717 {"matcharg", 1, 1, f_matcharg},
718 {"matchdelete", 1, 1, f_matchdelete},
719 {"matchend", 2, 4, f_matchend},
720 {"matchlist", 2, 4, f_matchlist},
721 {"matchstr", 2, 4, f_matchstr},
722 {"matchstrpos", 2, 4, f_matchstrpos},
723 {"max", 1, 1, f_max},
724 {"min", 1, 1, f_min},
725#ifdef vim_mkdir
726 {"mkdir", 1, 3, f_mkdir},
727#endif
728 {"mode", 0, 1, f_mode},
729#ifdef FEAT_MZSCHEME
730 {"mzeval", 1, 1, f_mzeval},
731#endif
732 {"nextnonblank", 1, 1, f_nextnonblank},
733 {"nr2char", 1, 2, f_nr2char},
734 {"or", 2, 2, f_or},
735 {"pathshorten", 1, 1, f_pathshorten},
736#ifdef FEAT_PERL
737 {"perleval", 1, 1, f_perleval},
738#endif
739#ifdef FEAT_FLOAT
740 {"pow", 2, 2, f_pow},
741#endif
742 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100743 {"printf", 1, 19, f_printf},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200744 {"pumvisible", 0, 0, f_pumvisible},
745#ifdef FEAT_PYTHON3
746 {"py3eval", 1, 1, f_py3eval},
747#endif
748#ifdef FEAT_PYTHON
749 {"pyeval", 1, 1, f_pyeval},
750#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100751#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
752 {"pyxeval", 1, 1, f_pyxeval},
753#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200754 {"range", 1, 3, f_range},
755 {"readfile", 1, 3, f_readfile},
756 {"reltime", 0, 2, f_reltime},
757#ifdef FEAT_FLOAT
758 {"reltimefloat", 1, 1, f_reltimefloat},
759#endif
760 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100761 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200762 {"remote_foreground", 1, 1, f_remote_foreground},
763 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100764 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200765 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100766 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200767 {"remove", 2, 3, f_remove},
768 {"rename", 2, 2, f_rename},
769 {"repeat", 2, 2, f_repeat},
770 {"resolve", 1, 1, f_resolve},
771 {"reverse", 1, 1, f_reverse},
772#ifdef FEAT_FLOAT
773 {"round", 1, 1, f_round},
774#endif
775 {"screenattr", 2, 2, f_screenattr},
776 {"screenchar", 2, 2, f_screenchar},
777 {"screencol", 0, 0, f_screencol},
778 {"screenrow", 0, 0, f_screenrow},
779 {"search", 1, 4, f_search},
780 {"searchdecl", 1, 3, f_searchdecl},
781 {"searchpair", 3, 7, f_searchpair},
782 {"searchpairpos", 3, 7, f_searchpairpos},
783 {"searchpos", 1, 4, f_searchpos},
784 {"server2client", 2, 2, f_server2client},
785 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200786 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200787 {"setbufvar", 3, 3, f_setbufvar},
788 {"setcharsearch", 1, 1, f_setcharsearch},
789 {"setcmdpos", 1, 1, f_setcmdpos},
790 {"setfperm", 2, 2, f_setfperm},
791 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200792 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200793 {"setmatches", 1, 1, f_setmatches},
794 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200795 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200796 {"setreg", 2, 3, f_setreg},
797 {"settabvar", 3, 3, f_settabvar},
798 {"settabwinvar", 4, 4, f_settabwinvar},
799 {"setwinvar", 3, 3, f_setwinvar},
800#ifdef FEAT_CRYPT
801 {"sha256", 1, 1, f_sha256},
802#endif
803 {"shellescape", 1, 2, f_shellescape},
804 {"shiftwidth", 0, 0, f_shiftwidth},
805 {"simplify", 1, 1, f_simplify},
806#ifdef FEAT_FLOAT
807 {"sin", 1, 1, f_sin},
808 {"sinh", 1, 1, f_sinh},
809#endif
810 {"sort", 1, 3, f_sort},
811 {"soundfold", 1, 1, f_soundfold},
812 {"spellbadword", 0, 1, f_spellbadword},
813 {"spellsuggest", 1, 3, f_spellsuggest},
814 {"split", 1, 3, f_split},
815#ifdef FEAT_FLOAT
816 {"sqrt", 1, 1, f_sqrt},
817 {"str2float", 1, 1, f_str2float},
818#endif
819 {"str2nr", 1, 2, f_str2nr},
820 {"strcharpart", 2, 3, f_strcharpart},
821 {"strchars", 1, 2, f_strchars},
822 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
823#ifdef HAVE_STRFTIME
824 {"strftime", 1, 2, f_strftime},
825#endif
826 {"strgetchar", 2, 2, f_strgetchar},
827 {"stridx", 2, 3, f_stridx},
828 {"string", 1, 1, f_string},
829 {"strlen", 1, 1, f_strlen},
830 {"strpart", 2, 3, f_strpart},
831 {"strridx", 2, 3, f_strridx},
832 {"strtrans", 1, 1, f_strtrans},
833 {"strwidth", 1, 1, f_strwidth},
834 {"submatch", 1, 2, f_submatch},
835 {"substitute", 4, 4, f_substitute},
836 {"synID", 3, 3, f_synID},
837 {"synIDattr", 2, 3, f_synIDattr},
838 {"synIDtrans", 1, 1, f_synIDtrans},
839 {"synconcealed", 2, 2, f_synconcealed},
840 {"synstack", 2, 2, f_synstack},
841 {"system", 1, 2, f_system},
842 {"systemlist", 1, 2, f_systemlist},
843 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
844 {"tabpagenr", 0, 1, f_tabpagenr},
845 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
846 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100847 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200848#ifdef FEAT_FLOAT
849 {"tan", 1, 1, f_tan},
850 {"tanh", 1, 1, f_tanh},
851#endif
852 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200853#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100854 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
855 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100856 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200857 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200858 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200859 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200860 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200861 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200862 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200863 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200864 {"term_getstatus", 1, 1, f_term_getstatus},
865 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200866 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200867 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200868 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200869 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100870 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100871 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200872 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200873 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200874#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200875 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
876 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200877 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200878 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100879 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200880#ifdef FEAT_JOB_CHANNEL
881 {"test_null_channel", 0, 0, f_test_null_channel},
882#endif
883 {"test_null_dict", 0, 0, f_test_null_dict},
884#ifdef FEAT_JOB_CHANNEL
885 {"test_null_job", 0, 0, f_test_null_job},
886#endif
887 {"test_null_list", 0, 0, f_test_null_list},
888 {"test_null_partial", 0, 0, f_test_null_partial},
889 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100890 {"test_override", 2, 2, f_test_override},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200891 {"test_settime", 1, 1, f_test_settime},
892#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200893 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200894 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200895 {"timer_start", 2, 3, f_timer_start},
896 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200897 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200898#endif
899 {"tolower", 1, 1, f_tolower},
900 {"toupper", 1, 1, f_toupper},
901 {"tr", 3, 3, f_tr},
902#ifdef FEAT_FLOAT
903 {"trunc", 1, 1, f_trunc},
904#endif
905 {"type", 1, 1, f_type},
906 {"undofile", 1, 1, f_undofile},
907 {"undotree", 0, 0, f_undotree},
908 {"uniq", 1, 3, f_uniq},
909 {"values", 1, 1, f_values},
910 {"virtcol", 1, 1, f_virtcol},
911 {"visualmode", 0, 1, f_visualmode},
912 {"wildmenumode", 0, 0, f_wildmenumode},
913 {"win_findbuf", 1, 1, f_win_findbuf},
914 {"win_getid", 0, 2, f_win_getid},
915 {"win_gotoid", 1, 1, f_win_gotoid},
916 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
917 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100918 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200919 {"winbufnr", 1, 1, f_winbufnr},
920 {"wincol", 0, 0, f_wincol},
921 {"winheight", 1, 1, f_winheight},
922 {"winline", 0, 0, f_winline},
923 {"winnr", 0, 1, f_winnr},
924 {"winrestcmd", 0, 0, f_winrestcmd},
925 {"winrestview", 1, 1, f_winrestview},
926 {"winsaveview", 0, 0, f_winsaveview},
927 {"winwidth", 1, 1, f_winwidth},
928 {"wordcount", 0, 0, f_wordcount},
929 {"writefile", 2, 3, f_writefile},
930 {"xor", 2, 2, f_xor},
931};
932
933#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
934
935/*
936 * Function given to ExpandGeneric() to obtain the list of internal
937 * or user defined function names.
938 */
939 char_u *
940get_function_name(expand_T *xp, int idx)
941{
942 static int intidx = -1;
943 char_u *name;
944
945 if (idx == 0)
946 intidx = -1;
947 if (intidx < 0)
948 {
949 name = get_user_func_name(xp, idx);
950 if (name != NULL)
951 return name;
952 }
953 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
954 {
955 STRCPY(IObuff, functions[intidx].f_name);
956 STRCAT(IObuff, "(");
957 if (functions[intidx].f_max_argc == 0)
958 STRCAT(IObuff, ")");
959 return IObuff;
960 }
961
962 return NULL;
963}
964
965/*
966 * Function given to ExpandGeneric() to obtain the list of internal or
967 * user defined variable or function names.
968 */
969 char_u *
970get_expr_name(expand_T *xp, int idx)
971{
972 static int intidx = -1;
973 char_u *name;
974
975 if (idx == 0)
976 intidx = -1;
977 if (intidx < 0)
978 {
979 name = get_function_name(xp, idx);
980 if (name != NULL)
981 return name;
982 }
983 return get_user_var_name(xp, ++intidx);
984}
985
986#endif /* FEAT_CMDL_COMPL */
987
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200988/*
989 * Find internal function in table above.
990 * Return index, or -1 if not found
991 */
992 int
993find_internal_func(
994 char_u *name) /* name of the function */
995{
996 int first = 0;
997 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
998 int cmp;
999 int x;
1000
1001 /*
1002 * Find the function name in the table. Binary search.
1003 */
1004 while (first <= last)
1005 {
1006 x = first + ((unsigned)(last - first) >> 1);
1007 cmp = STRCMP(name, functions[x].f_name);
1008 if (cmp < 0)
1009 last = x - 1;
1010 else if (cmp > 0)
1011 first = x + 1;
1012 else
1013 return x;
1014 }
1015 return -1;
1016}
1017
1018 int
1019call_internal_func(
1020 char_u *name,
1021 int argcount,
1022 typval_T *argvars,
1023 typval_T *rettv)
1024{
1025 int i;
1026
1027 i = find_internal_func(name);
1028 if (i < 0)
1029 return ERROR_UNKNOWN;
1030 if (argcount < functions[i].f_min_argc)
1031 return ERROR_TOOFEW;
1032 if (argcount > functions[i].f_max_argc)
1033 return ERROR_TOOMANY;
1034 argvars[argcount].v_type = VAR_UNKNOWN;
1035 functions[i].f_func(argvars, rettv);
1036 return ERROR_NONE;
1037}
1038
1039/*
1040 * Return TRUE for a non-zero Number and a non-empty String.
1041 */
1042 static int
1043non_zero_arg(typval_T *argvars)
1044{
1045 return ((argvars[0].v_type == VAR_NUMBER
1046 && argvars[0].vval.v_number != 0)
1047 || (argvars[0].v_type == VAR_SPECIAL
1048 && argvars[0].vval.v_number == VVAL_TRUE)
1049 || (argvars[0].v_type == VAR_STRING
1050 && argvars[0].vval.v_string != NULL
1051 && *argvars[0].vval.v_string != NUL));
1052}
1053
1054/*
1055 * Get the lnum from the first argument.
1056 * Also accepts ".", "$", etc., but that only works for the current buffer.
1057 * Returns -1 on error.
1058 */
1059 static linenr_T
1060get_tv_lnum(typval_T *argvars)
1061{
1062 typval_T rettv;
1063 linenr_T lnum;
1064
1065 lnum = (linenr_T)get_tv_number_chk(&argvars[0], NULL);
1066 if (lnum == 0) /* no valid number, try using line() */
1067 {
1068 rettv.v_type = VAR_NUMBER;
1069 f_line(argvars, &rettv);
1070 lnum = (linenr_T)rettv.vval.v_number;
1071 clear_tv(&rettv);
1072 }
1073 return lnum;
1074}
1075
1076#ifdef FEAT_FLOAT
1077static int get_float_arg(typval_T *argvars, float_T *f);
1078
1079/*
1080 * Get the float value of "argvars[0]" into "f".
1081 * Returns FAIL when the argument is not a Number or Float.
1082 */
1083 static int
1084get_float_arg(typval_T *argvars, float_T *f)
1085{
1086 if (argvars[0].v_type == VAR_FLOAT)
1087 {
1088 *f = argvars[0].vval.v_float;
1089 return OK;
1090 }
1091 if (argvars[0].v_type == VAR_NUMBER)
1092 {
1093 *f = (float_T)argvars[0].vval.v_number;
1094 return OK;
1095 }
1096 EMSG(_("E808: Number or Float required"));
1097 return FAIL;
1098}
1099
1100/*
1101 * "abs(expr)" function
1102 */
1103 static void
1104f_abs(typval_T *argvars, typval_T *rettv)
1105{
1106 if (argvars[0].v_type == VAR_FLOAT)
1107 {
1108 rettv->v_type = VAR_FLOAT;
1109 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1110 }
1111 else
1112 {
1113 varnumber_T n;
1114 int error = FALSE;
1115
1116 n = get_tv_number_chk(&argvars[0], &error);
1117 if (error)
1118 rettv->vval.v_number = -1;
1119 else if (n > 0)
1120 rettv->vval.v_number = n;
1121 else
1122 rettv->vval.v_number = -n;
1123 }
1124}
1125
1126/*
1127 * "acos()" function
1128 */
1129 static void
1130f_acos(typval_T *argvars, typval_T *rettv)
1131{
1132 float_T f = 0.0;
1133
1134 rettv->v_type = VAR_FLOAT;
1135 if (get_float_arg(argvars, &f) == OK)
1136 rettv->vval.v_float = acos(f);
1137 else
1138 rettv->vval.v_float = 0.0;
1139}
1140#endif
1141
1142/*
1143 * "add(list, item)" function
1144 */
1145 static void
1146f_add(typval_T *argvars, typval_T *rettv)
1147{
1148 list_T *l;
1149
1150 rettv->vval.v_number = 1; /* Default: Failed */
1151 if (argvars[0].v_type == VAR_LIST)
1152 {
1153 if ((l = argvars[0].vval.v_list) != NULL
1154 && !tv_check_lock(l->lv_lock,
1155 (char_u *)N_("add() argument"), TRUE)
1156 && list_append_tv(l, &argvars[1]) == OK)
1157 copy_tv(&argvars[0], rettv);
1158 }
1159 else
1160 EMSG(_(e_listreq));
1161}
1162
1163/*
1164 * "and(expr, expr)" function
1165 */
1166 static void
1167f_and(typval_T *argvars, typval_T *rettv)
1168{
1169 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
1170 & get_tv_number_chk(&argvars[1], NULL);
1171}
1172
1173/*
1174 * "append(lnum, string/list)" function
1175 */
1176 static void
1177f_append(typval_T *argvars, typval_T *rettv)
1178{
1179 long lnum;
1180 char_u *line;
1181 list_T *l = NULL;
1182 listitem_T *li = NULL;
1183 typval_T *tv;
1184 long added = 0;
1185
1186 /* When coming here from Insert mode, sync undo, so that this can be
1187 * undone separately from what was previously inserted. */
1188 if (u_sync_once == 2)
1189 {
1190 u_sync_once = 1; /* notify that u_sync() was called */
1191 u_sync(TRUE);
1192 }
1193
1194 lnum = get_tv_lnum(argvars);
1195 if (lnum >= 0
1196 && lnum <= curbuf->b_ml.ml_line_count
1197 && u_save(lnum, lnum + 1) == OK)
1198 {
1199 if (argvars[1].v_type == VAR_LIST)
1200 {
1201 l = argvars[1].vval.v_list;
1202 if (l == NULL)
1203 return;
1204 li = l->lv_first;
1205 }
1206 for (;;)
1207 {
1208 if (l == NULL)
1209 tv = &argvars[1]; /* append a string */
1210 else if (li == NULL)
1211 break; /* end of list */
1212 else
1213 tv = &li->li_tv; /* append item from list */
1214 line = get_tv_string_chk(tv);
1215 if (line == NULL) /* type error */
1216 {
1217 rettv->vval.v_number = 1; /* Failed */
1218 break;
1219 }
1220 ml_append(lnum + added, line, (colnr_T)0, FALSE);
1221 ++added;
1222 if (l == NULL)
1223 break;
1224 li = li->li_next;
1225 }
1226
1227 appended_lines_mark(lnum, added);
1228 if (curwin->w_cursor.lnum > lnum)
1229 curwin->w_cursor.lnum += added;
1230 }
1231 else
1232 rettv->vval.v_number = 1; /* Failed */
1233}
1234
1235/*
1236 * "argc()" function
1237 */
1238 static void
1239f_argc(typval_T *argvars UNUSED, typval_T *rettv)
1240{
1241 rettv->vval.v_number = ARGCOUNT;
1242}
1243
1244/*
1245 * "argidx()" function
1246 */
1247 static void
1248f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1249{
1250 rettv->vval.v_number = curwin->w_arg_idx;
1251}
1252
1253/*
1254 * "arglistid()" function
1255 */
1256 static void
1257f_arglistid(typval_T *argvars, typval_T *rettv)
1258{
1259 win_T *wp;
1260
1261 rettv->vval.v_number = -1;
1262 wp = find_tabwin(&argvars[0], &argvars[1]);
1263 if (wp != NULL)
1264 rettv->vval.v_number = wp->w_alist->id;
1265}
1266
1267/*
1268 * "argv(nr)" function
1269 */
1270 static void
1271f_argv(typval_T *argvars, typval_T *rettv)
1272{
1273 int idx;
1274
1275 if (argvars[0].v_type != VAR_UNKNOWN)
1276 {
1277 idx = (int)get_tv_number_chk(&argvars[0], NULL);
1278 if (idx >= 0 && idx < ARGCOUNT)
1279 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
1280 else
1281 rettv->vval.v_string = NULL;
1282 rettv->v_type = VAR_STRING;
1283 }
1284 else if (rettv_list_alloc(rettv) == OK)
1285 for (idx = 0; idx < ARGCOUNT; ++idx)
1286 list_append_string(rettv->vval.v_list,
1287 alist_name(&ARGLIST[idx]), -1);
1288}
1289
1290/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001291 * "assert_beeps(cmd [, error])" function
1292 */
1293 static void
1294f_assert_beeps(typval_T *argvars, typval_T *rettv UNUSED)
1295{
1296 assert_beeps(argvars);
1297}
1298
1299/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001300 * "assert_equal(expected, actual[, msg])" function
1301 */
1302 static void
1303f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
1304{
1305 assert_equal_common(argvars, ASSERT_EQUAL);
1306}
1307
1308/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001309 * "assert_equalfile(fname-one, fname-two)" function
1310 */
1311 static void
1312f_assert_equalfile(typval_T *argvars, typval_T *rettv UNUSED)
1313{
1314 assert_equalfile(argvars);
1315}
1316
1317/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001318 * "assert_notequal(expected, actual[, msg])" function
1319 */
1320 static void
1321f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED)
1322{
1323 assert_equal_common(argvars, ASSERT_NOTEQUAL);
1324}
1325
1326/*
1327 * "assert_exception(string[, msg])" function
1328 */
1329 static void
1330f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
1331{
1332 assert_exception(argvars);
1333}
1334
1335/*
1336 * "assert_fails(cmd [, error])" function
1337 */
1338 static void
1339f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
1340{
1341 assert_fails(argvars);
1342}
1343
1344/*
1345 * "assert_false(actual[, msg])" function
1346 */
1347 static void
1348f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
1349{
1350 assert_bool(argvars, FALSE);
1351}
1352
1353/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001354 * "assert_inrange(lower, upper[, msg])" function
1355 */
1356 static void
1357f_assert_inrange(typval_T *argvars, typval_T *rettv UNUSED)
1358{
1359 assert_inrange(argvars);
1360}
1361
1362/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001363 * "assert_match(pattern, actual[, msg])" function
1364 */
1365 static void
1366f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
1367{
1368 assert_match_common(argvars, ASSERT_MATCH);
1369}
1370
1371/*
1372 * "assert_notmatch(pattern, actual[, msg])" function
1373 */
1374 static void
1375f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED)
1376{
1377 assert_match_common(argvars, ASSERT_NOTMATCH);
1378}
1379
1380/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001381 * "assert_report(msg)" function
1382 */
1383 static void
1384f_assert_report(typval_T *argvars, typval_T *rettv UNUSED)
1385{
1386 assert_report(argvars);
1387}
1388
1389/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001390 * "assert_true(actual[, msg])" function
1391 */
1392 static void
1393f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
1394{
1395 assert_bool(argvars, TRUE);
1396}
1397
1398#ifdef FEAT_FLOAT
1399/*
1400 * "asin()" function
1401 */
1402 static void
1403f_asin(typval_T *argvars, typval_T *rettv)
1404{
1405 float_T f = 0.0;
1406
1407 rettv->v_type = VAR_FLOAT;
1408 if (get_float_arg(argvars, &f) == OK)
1409 rettv->vval.v_float = asin(f);
1410 else
1411 rettv->vval.v_float = 0.0;
1412}
1413
1414/*
1415 * "atan()" function
1416 */
1417 static void
1418f_atan(typval_T *argvars, typval_T *rettv)
1419{
1420 float_T f = 0.0;
1421
1422 rettv->v_type = VAR_FLOAT;
1423 if (get_float_arg(argvars, &f) == OK)
1424 rettv->vval.v_float = atan(f);
1425 else
1426 rettv->vval.v_float = 0.0;
1427}
1428
1429/*
1430 * "atan2()" function
1431 */
1432 static void
1433f_atan2(typval_T *argvars, typval_T *rettv)
1434{
1435 float_T fx = 0.0, fy = 0.0;
1436
1437 rettv->v_type = VAR_FLOAT;
1438 if (get_float_arg(argvars, &fx) == OK
1439 && get_float_arg(&argvars[1], &fy) == OK)
1440 rettv->vval.v_float = atan2(fx, fy);
1441 else
1442 rettv->vval.v_float = 0.0;
1443}
1444#endif
1445
1446/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001447 * "balloon_show()" function
1448 */
1449#ifdef FEAT_BEVAL
1450 static void
1451f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1452{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001453 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001454 {
1455 if (argvars[0].v_type == VAR_LIST
1456# ifdef FEAT_GUI
1457 && !gui.in_use
1458# endif
1459 )
1460 post_balloon(balloonEval, NULL, argvars[0].vval.v_list);
1461 else
1462 post_balloon(balloonEval, get_tv_string_chk(&argvars[0]), NULL);
1463 }
1464}
1465
Bram Moolenaar669a8282017-11-19 20:13:05 +01001466# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001467 static void
1468f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1469{
1470 if (rettv_list_alloc(rettv) == OK)
1471 {
1472 char_u *msg = get_tv_string_chk(&argvars[0]);
1473
1474 if (msg != NULL)
1475 {
1476 pumitem_T *array;
1477 int size = split_message(msg, &array);
1478 int i;
1479
1480 /* Skip the first and last item, they are always empty. */
1481 for (i = 1; i < size - 1; ++i)
1482 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001483 while (size > 0)
1484 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001485 vim_free(array);
1486 }
1487 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001488}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001489# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001490#endif
1491
1492/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001493 * "browse(save, title, initdir, default)" function
1494 */
1495 static void
1496f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1497{
1498#ifdef FEAT_BROWSE
1499 int save;
1500 char_u *title;
1501 char_u *initdir;
1502 char_u *defname;
1503 char_u buf[NUMBUFLEN];
1504 char_u buf2[NUMBUFLEN];
1505 int error = FALSE;
1506
1507 save = (int)get_tv_number_chk(&argvars[0], &error);
1508 title = get_tv_string_chk(&argvars[1]);
1509 initdir = get_tv_string_buf_chk(&argvars[2], buf);
1510 defname = get_tv_string_buf_chk(&argvars[3], buf2);
1511
1512 if (error || title == NULL || initdir == NULL || defname == NULL)
1513 rettv->vval.v_string = NULL;
1514 else
1515 rettv->vval.v_string =
1516 do_browse(save ? BROWSE_SAVE : 0,
1517 title, defname, NULL, initdir, NULL, curbuf);
1518#else
1519 rettv->vval.v_string = NULL;
1520#endif
1521 rettv->v_type = VAR_STRING;
1522}
1523
1524/*
1525 * "browsedir(title, initdir)" function
1526 */
1527 static void
1528f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1529{
1530#ifdef FEAT_BROWSE
1531 char_u *title;
1532 char_u *initdir;
1533 char_u buf[NUMBUFLEN];
1534
1535 title = get_tv_string_chk(&argvars[0]);
1536 initdir = get_tv_string_buf_chk(&argvars[1], buf);
1537
1538 if (title == NULL || initdir == NULL)
1539 rettv->vval.v_string = NULL;
1540 else
1541 rettv->vval.v_string = do_browse(BROWSE_DIR,
1542 title, NULL, NULL, initdir, NULL, curbuf);
1543#else
1544 rettv->vval.v_string = NULL;
1545#endif
1546 rettv->v_type = VAR_STRING;
1547}
1548
1549static buf_T *find_buffer(typval_T *avar);
1550
1551/*
1552 * Find a buffer by number or exact name.
1553 */
1554 static buf_T *
1555find_buffer(typval_T *avar)
1556{
1557 buf_T *buf = NULL;
1558
1559 if (avar->v_type == VAR_NUMBER)
1560 buf = buflist_findnr((int)avar->vval.v_number);
1561 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1562 {
1563 buf = buflist_findname_exp(avar->vval.v_string);
1564 if (buf == NULL)
1565 {
1566 /* No full path name match, try a match with a URL or a "nofile"
1567 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001568 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001569 if (buf->b_fname != NULL
1570 && (path_with_url(buf->b_fname)
1571#ifdef FEAT_QUICKFIX
1572 || bt_nofile(buf)
1573#endif
1574 )
1575 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1576 break;
1577 }
1578 }
1579 return buf;
1580}
1581
1582/*
1583 * "bufexists(expr)" function
1584 */
1585 static void
1586f_bufexists(typval_T *argvars, typval_T *rettv)
1587{
1588 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1589}
1590
1591/*
1592 * "buflisted(expr)" function
1593 */
1594 static void
1595f_buflisted(typval_T *argvars, typval_T *rettv)
1596{
1597 buf_T *buf;
1598
1599 buf = find_buffer(&argvars[0]);
1600 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1601}
1602
1603/*
1604 * "bufloaded(expr)" function
1605 */
1606 static void
1607f_bufloaded(typval_T *argvars, typval_T *rettv)
1608{
1609 buf_T *buf;
1610
1611 buf = find_buffer(&argvars[0]);
1612 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1613}
1614
1615 buf_T *
1616buflist_find_by_name(char_u *name, int curtab_only)
1617{
1618 int save_magic;
1619 char_u *save_cpo;
1620 buf_T *buf;
1621
1622 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1623 save_magic = p_magic;
1624 p_magic = TRUE;
1625 save_cpo = p_cpo;
1626 p_cpo = (char_u *)"";
1627
1628 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1629 TRUE, FALSE, curtab_only));
1630
1631 p_magic = save_magic;
1632 p_cpo = save_cpo;
1633 return buf;
1634}
1635
1636/*
1637 * Get buffer by number or pattern.
1638 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001639 buf_T *
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001640get_buf_tv(typval_T *tv, int curtab_only)
1641{
1642 char_u *name = tv->vval.v_string;
1643 buf_T *buf;
1644
1645 if (tv->v_type == VAR_NUMBER)
1646 return buflist_findnr((int)tv->vval.v_number);
1647 if (tv->v_type != VAR_STRING)
1648 return NULL;
1649 if (name == NULL || *name == NUL)
1650 return curbuf;
1651 if (name[0] == '$' && name[1] == NUL)
1652 return lastbuf;
1653
1654 buf = buflist_find_by_name(name, curtab_only);
1655
1656 /* If not found, try expanding the name, like done for bufexists(). */
1657 if (buf == NULL)
1658 buf = find_buffer(tv);
1659
1660 return buf;
1661}
1662
1663/*
1664 * "bufname(expr)" function
1665 */
1666 static void
1667f_bufname(typval_T *argvars, typval_T *rettv)
1668{
1669 buf_T *buf;
1670
1671 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1672 ++emsg_off;
1673 buf = get_buf_tv(&argvars[0], FALSE);
1674 rettv->v_type = VAR_STRING;
1675 if (buf != NULL && buf->b_fname != NULL)
1676 rettv->vval.v_string = vim_strsave(buf->b_fname);
1677 else
1678 rettv->vval.v_string = NULL;
1679 --emsg_off;
1680}
1681
1682/*
1683 * "bufnr(expr)" function
1684 */
1685 static void
1686f_bufnr(typval_T *argvars, typval_T *rettv)
1687{
1688 buf_T *buf;
1689 int error = FALSE;
1690 char_u *name;
1691
1692 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1693 ++emsg_off;
1694 buf = get_buf_tv(&argvars[0], FALSE);
1695 --emsg_off;
1696
1697 /* If the buffer isn't found and the second argument is not zero create a
1698 * new buffer. */
1699 if (buf == NULL
1700 && argvars[1].v_type != VAR_UNKNOWN
1701 && get_tv_number_chk(&argvars[1], &error) != 0
1702 && !error
1703 && (name = get_tv_string_chk(&argvars[0])) != NULL
1704 && !error)
1705 buf = buflist_new(name, NULL, (linenr_T)1, 0);
1706
1707 if (buf != NULL)
1708 rettv->vval.v_number = buf->b_fnum;
1709 else
1710 rettv->vval.v_number = -1;
1711}
1712
1713 static void
1714buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
1715{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001716 win_T *wp;
1717 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001718 buf_T *buf;
1719
1720 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1721 ++emsg_off;
1722 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02001723 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001724 {
1725 ++winnr;
1726 if (wp->w_buffer == buf)
1727 break;
1728 }
1729 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001730 --emsg_off;
1731}
1732
1733/*
1734 * "bufwinid(nr)" function
1735 */
1736 static void
1737f_bufwinid(typval_T *argvars, typval_T *rettv)
1738{
1739 buf_win_common(argvars, rettv, FALSE);
1740}
1741
1742/*
1743 * "bufwinnr(nr)" function
1744 */
1745 static void
1746f_bufwinnr(typval_T *argvars, typval_T *rettv)
1747{
1748 buf_win_common(argvars, rettv, TRUE);
1749}
1750
1751/*
1752 * "byte2line(byte)" function
1753 */
1754 static void
1755f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1756{
1757#ifndef FEAT_BYTEOFF
1758 rettv->vval.v_number = -1;
1759#else
1760 long boff = 0;
1761
1762 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
1763 if (boff < 0)
1764 rettv->vval.v_number = -1;
1765 else
1766 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
1767 (linenr_T)0, &boff);
1768#endif
1769}
1770
1771 static void
1772byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
1773{
1774#ifdef FEAT_MBYTE
1775 char_u *t;
1776#endif
1777 char_u *str;
1778 varnumber_T idx;
1779
1780 str = get_tv_string_chk(&argvars[0]);
1781 idx = get_tv_number_chk(&argvars[1], NULL);
1782 rettv->vval.v_number = -1;
1783 if (str == NULL || idx < 0)
1784 return;
1785
1786#ifdef FEAT_MBYTE
1787 t = str;
1788 for ( ; idx > 0; idx--)
1789 {
1790 if (*t == NUL) /* EOL reached */
1791 return;
1792 if (enc_utf8 && comp)
1793 t += utf_ptr2len(t);
1794 else
1795 t += (*mb_ptr2len)(t);
1796 }
1797 rettv->vval.v_number = (varnumber_T)(t - str);
1798#else
1799 if ((size_t)idx <= STRLEN(str))
1800 rettv->vval.v_number = idx;
1801#endif
1802}
1803
1804/*
1805 * "byteidx()" function
1806 */
1807 static void
1808f_byteidx(typval_T *argvars, typval_T *rettv)
1809{
1810 byteidx(argvars, rettv, FALSE);
1811}
1812
1813/*
1814 * "byteidxcomp()" function
1815 */
1816 static void
1817f_byteidxcomp(typval_T *argvars, typval_T *rettv)
1818{
1819 byteidx(argvars, rettv, TRUE);
1820}
1821
1822/*
1823 * "call(func, arglist [, dict])" function
1824 */
1825 static void
1826f_call(typval_T *argvars, typval_T *rettv)
1827{
1828 char_u *func;
1829 partial_T *partial = NULL;
1830 dict_T *selfdict = NULL;
1831
1832 if (argvars[1].v_type != VAR_LIST)
1833 {
1834 EMSG(_(e_listreq));
1835 return;
1836 }
1837 if (argvars[1].vval.v_list == NULL)
1838 return;
1839
1840 if (argvars[0].v_type == VAR_FUNC)
1841 func = argvars[0].vval.v_string;
1842 else if (argvars[0].v_type == VAR_PARTIAL)
1843 {
1844 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001845 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001846 }
1847 else
1848 func = get_tv_string(&argvars[0]);
1849 if (*func == NUL)
1850 return; /* type error or empty name */
1851
1852 if (argvars[2].v_type != VAR_UNKNOWN)
1853 {
1854 if (argvars[2].v_type != VAR_DICT)
1855 {
1856 EMSG(_(e_dictreq));
1857 return;
1858 }
1859 selfdict = argvars[2].vval.v_dict;
1860 }
1861
1862 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
1863}
1864
1865#ifdef FEAT_FLOAT
1866/*
1867 * "ceil({float})" function
1868 */
1869 static void
1870f_ceil(typval_T *argvars, typval_T *rettv)
1871{
1872 float_T f = 0.0;
1873
1874 rettv->v_type = VAR_FLOAT;
1875 if (get_float_arg(argvars, &f) == OK)
1876 rettv->vval.v_float = ceil(f);
1877 else
1878 rettv->vval.v_float = 0.0;
1879}
1880#endif
1881
1882#ifdef FEAT_JOB_CHANNEL
1883/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01001884 * "ch_canread()" function
1885 */
1886 static void
1887f_ch_canread(typval_T *argvars, typval_T *rettv)
1888{
Bram Moolenaar958dc692016-12-01 15:34:12 +01001889 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01001890
1891 rettv->vval.v_number = 0;
1892 if (channel != NULL)
1893 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
1894 || channel_has_readahead(channel, PART_OUT)
1895 || channel_has_readahead(channel, PART_ERR);
1896}
1897
1898/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001899 * "ch_close()" function
1900 */
1901 static void
1902f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
1903{
1904 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
1905
1906 if (channel != NULL)
1907 {
1908 channel_close(channel, FALSE);
1909 channel_clear(channel);
1910 }
1911}
1912
1913/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02001914 * "ch_close()" function
1915 */
1916 static void
1917f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
1918{
1919 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
1920
1921 if (channel != NULL)
1922 channel_close_in(channel);
1923}
1924
1925/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001926 * "ch_getbufnr()" function
1927 */
1928 static void
1929f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
1930{
1931 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1932
1933 rettv->vval.v_number = -1;
1934 if (channel != NULL)
1935 {
1936 char_u *what = get_tv_string(&argvars[1]);
1937 int part;
1938
1939 if (STRCMP(what, "err") == 0)
1940 part = PART_ERR;
1941 else if (STRCMP(what, "out") == 0)
1942 part = PART_OUT;
1943 else if (STRCMP(what, "in") == 0)
1944 part = PART_IN;
1945 else
1946 part = PART_SOCK;
1947 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
1948 rettv->vval.v_number =
1949 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
1950 }
1951}
1952
1953/*
1954 * "ch_getjob()" function
1955 */
1956 static void
1957f_ch_getjob(typval_T *argvars, typval_T *rettv)
1958{
1959 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1960
1961 if (channel != NULL)
1962 {
1963 rettv->v_type = VAR_JOB;
1964 rettv->vval.v_job = channel->ch_job;
1965 if (channel->ch_job != NULL)
1966 ++channel->ch_job->jv_refcount;
1967 }
1968}
1969
1970/*
1971 * "ch_info()" function
1972 */
1973 static void
1974f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
1975{
1976 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1977
1978 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
1979 channel_info(channel, rettv->vval.v_dict);
1980}
1981
1982/*
1983 * "ch_log()" function
1984 */
1985 static void
1986f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
1987{
1988 char_u *msg = get_tv_string(&argvars[0]);
1989 channel_T *channel = NULL;
1990
1991 if (argvars[1].v_type != VAR_UNKNOWN)
1992 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
1993
1994 ch_log(channel, (char *)msg);
1995}
1996
1997/*
1998 * "ch_logfile()" function
1999 */
2000 static void
2001f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
2002{
2003 char_u *fname;
2004 char_u *opt = (char_u *)"";
2005 char_u buf[NUMBUFLEN];
2006
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02002007 /* Don't open a file in restricted mode. */
2008 if (check_restricted() || check_secure())
2009 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002010 fname = get_tv_string(&argvars[0]);
2011 if (argvars[1].v_type == VAR_STRING)
2012 opt = get_tv_string_buf(&argvars[1], buf);
2013 ch_logfile(fname, opt);
2014}
2015
2016/*
2017 * "ch_open()" function
2018 */
2019 static void
2020f_ch_open(typval_T *argvars, typval_T *rettv)
2021{
2022 rettv->v_type = VAR_CHANNEL;
2023 if (check_restricted() || check_secure())
2024 return;
2025 rettv->vval.v_channel = channel_open_func(argvars);
2026}
2027
2028/*
2029 * "ch_read()" function
2030 */
2031 static void
2032f_ch_read(typval_T *argvars, typval_T *rettv)
2033{
2034 common_channel_read(argvars, rettv, FALSE);
2035}
2036
2037/*
2038 * "ch_readraw()" function
2039 */
2040 static void
2041f_ch_readraw(typval_T *argvars, typval_T *rettv)
2042{
2043 common_channel_read(argvars, rettv, TRUE);
2044}
2045
2046/*
2047 * "ch_evalexpr()" function
2048 */
2049 static void
2050f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2051{
2052 ch_expr_common(argvars, rettv, TRUE);
2053}
2054
2055/*
2056 * "ch_sendexpr()" function
2057 */
2058 static void
2059f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2060{
2061 ch_expr_common(argvars, rettv, FALSE);
2062}
2063
2064/*
2065 * "ch_evalraw()" function
2066 */
2067 static void
2068f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2069{
2070 ch_raw_common(argvars, rettv, TRUE);
2071}
2072
2073/*
2074 * "ch_sendraw()" function
2075 */
2076 static void
2077f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2078{
2079 ch_raw_common(argvars, rettv, FALSE);
2080}
2081
2082/*
2083 * "ch_setoptions()" function
2084 */
2085 static void
2086f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2087{
2088 channel_T *channel;
2089 jobopt_T opt;
2090
2091 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2092 if (channel == NULL)
2093 return;
2094 clear_job_options(&opt);
2095 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002096 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002097 channel_set_options(channel, &opt);
2098 free_job_options(&opt);
2099}
2100
2101/*
2102 * "ch_status()" function
2103 */
2104 static void
2105f_ch_status(typval_T *argvars, typval_T *rettv)
2106{
2107 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002108 jobopt_T opt;
2109 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002110
2111 /* return an empty string by default */
2112 rettv->v_type = VAR_STRING;
2113 rettv->vval.v_string = NULL;
2114
2115 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002116
2117 if (argvars[1].v_type != VAR_UNKNOWN)
2118 {
2119 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002120 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002121 && (opt.jo_set & JO_PART))
2122 part = opt.jo_part;
2123 }
2124
2125 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002126}
2127#endif
2128
2129/*
2130 * "changenr()" function
2131 */
2132 static void
2133f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2134{
2135 rettv->vval.v_number = curbuf->b_u_seq_cur;
2136}
2137
2138/*
2139 * "char2nr(string)" function
2140 */
2141 static void
2142f_char2nr(typval_T *argvars, typval_T *rettv)
2143{
2144#ifdef FEAT_MBYTE
2145 if (has_mbyte)
2146 {
2147 int utf8 = 0;
2148
2149 if (argvars[1].v_type != VAR_UNKNOWN)
2150 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
2151
2152 if (utf8)
2153 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
2154 else
2155 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
2156 }
2157 else
2158#endif
2159 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
2160}
2161
2162/*
2163 * "cindent(lnum)" function
2164 */
2165 static void
2166f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2167{
2168#ifdef FEAT_CINDENT
2169 pos_T pos;
2170 linenr_T lnum;
2171
2172 pos = curwin->w_cursor;
2173 lnum = get_tv_lnum(argvars);
2174 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2175 {
2176 curwin->w_cursor.lnum = lnum;
2177 rettv->vval.v_number = get_c_indent();
2178 curwin->w_cursor = pos;
2179 }
2180 else
2181#endif
2182 rettv->vval.v_number = -1;
2183}
2184
2185/*
2186 * "clearmatches()" function
2187 */
2188 static void
2189f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2190{
2191#ifdef FEAT_SEARCH_EXTRA
2192 clear_matches(curwin);
2193#endif
2194}
2195
2196/*
2197 * "col(string)" function
2198 */
2199 static void
2200f_col(typval_T *argvars, typval_T *rettv)
2201{
2202 colnr_T col = 0;
2203 pos_T *fp;
2204 int fnum = curbuf->b_fnum;
2205
2206 fp = var2fpos(&argvars[0], FALSE, &fnum);
2207 if (fp != NULL && fnum == curbuf->b_fnum)
2208 {
2209 if (fp->col == MAXCOL)
2210 {
2211 /* '> can be MAXCOL, get the length of the line then */
2212 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2213 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2214 else
2215 col = MAXCOL;
2216 }
2217 else
2218 {
2219 col = fp->col + 1;
2220#ifdef FEAT_VIRTUALEDIT
2221 /* col(".") when the cursor is on the NUL at the end of the line
2222 * because of "coladd" can be seen as an extra column. */
2223 if (virtual_active() && fp == &curwin->w_cursor)
2224 {
2225 char_u *p = ml_get_cursor();
2226
2227 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2228 curwin->w_virtcol - curwin->w_cursor.coladd))
2229 {
2230# ifdef FEAT_MBYTE
2231 int l;
2232
2233 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2234 col += l;
2235# else
2236 if (*p != NUL && p[1] == NUL)
2237 ++col;
2238# endif
2239 }
2240 }
2241#endif
2242 }
2243 }
2244 rettv->vval.v_number = col;
2245}
2246
2247#if defined(FEAT_INS_EXPAND)
2248/*
2249 * "complete()" function
2250 */
2251 static void
2252f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2253{
2254 int startcol;
2255
2256 if ((State & INSERT) == 0)
2257 {
2258 EMSG(_("E785: complete() can only be used in Insert mode"));
2259 return;
2260 }
2261
2262 /* Check for undo allowed here, because if something was already inserted
2263 * the line was already saved for undo and this check isn't done. */
2264 if (!undo_allowed())
2265 return;
2266
2267 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2268 {
2269 EMSG(_(e_invarg));
2270 return;
2271 }
2272
2273 startcol = (int)get_tv_number_chk(&argvars[0], NULL);
2274 if (startcol <= 0)
2275 return;
2276
2277 set_completion(startcol - 1, argvars[1].vval.v_list);
2278}
2279
2280/*
2281 * "complete_add()" function
2282 */
2283 static void
2284f_complete_add(typval_T *argvars, typval_T *rettv)
2285{
2286 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2287}
2288
2289/*
2290 * "complete_check()" function
2291 */
2292 static void
2293f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2294{
2295 int saved = RedrawingDisabled;
2296
2297 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002298 ins_compl_check_keys(0, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002299 rettv->vval.v_number = compl_interrupted;
2300 RedrawingDisabled = saved;
2301}
2302#endif
2303
2304/*
2305 * "confirm(message, buttons[, default [, type]])" function
2306 */
2307 static void
2308f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2309{
2310#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2311 char_u *message;
2312 char_u *buttons = NULL;
2313 char_u buf[NUMBUFLEN];
2314 char_u buf2[NUMBUFLEN];
2315 int def = 1;
2316 int type = VIM_GENERIC;
2317 char_u *typestr;
2318 int error = FALSE;
2319
2320 message = get_tv_string_chk(&argvars[0]);
2321 if (message == NULL)
2322 error = TRUE;
2323 if (argvars[1].v_type != VAR_UNKNOWN)
2324 {
2325 buttons = get_tv_string_buf_chk(&argvars[1], buf);
2326 if (buttons == NULL)
2327 error = TRUE;
2328 if (argvars[2].v_type != VAR_UNKNOWN)
2329 {
2330 def = (int)get_tv_number_chk(&argvars[2], &error);
2331 if (argvars[3].v_type != VAR_UNKNOWN)
2332 {
2333 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
2334 if (typestr == NULL)
2335 error = TRUE;
2336 else
2337 {
2338 switch (TOUPPER_ASC(*typestr))
2339 {
2340 case 'E': type = VIM_ERROR; break;
2341 case 'Q': type = VIM_QUESTION; break;
2342 case 'I': type = VIM_INFO; break;
2343 case 'W': type = VIM_WARNING; break;
2344 case 'G': type = VIM_GENERIC; break;
2345 }
2346 }
2347 }
2348 }
2349 }
2350
2351 if (buttons == NULL || *buttons == NUL)
2352 buttons = (char_u *)_("&Ok");
2353
2354 if (!error)
2355 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2356 def, NULL, FALSE);
2357#endif
2358}
2359
2360/*
2361 * "copy()" function
2362 */
2363 static void
2364f_copy(typval_T *argvars, typval_T *rettv)
2365{
2366 item_copy(&argvars[0], rettv, FALSE, 0);
2367}
2368
2369#ifdef FEAT_FLOAT
2370/*
2371 * "cos()" function
2372 */
2373 static void
2374f_cos(typval_T *argvars, typval_T *rettv)
2375{
2376 float_T f = 0.0;
2377
2378 rettv->v_type = VAR_FLOAT;
2379 if (get_float_arg(argvars, &f) == OK)
2380 rettv->vval.v_float = cos(f);
2381 else
2382 rettv->vval.v_float = 0.0;
2383}
2384
2385/*
2386 * "cosh()" function
2387 */
2388 static void
2389f_cosh(typval_T *argvars, typval_T *rettv)
2390{
2391 float_T f = 0.0;
2392
2393 rettv->v_type = VAR_FLOAT;
2394 if (get_float_arg(argvars, &f) == OK)
2395 rettv->vval.v_float = cosh(f);
2396 else
2397 rettv->vval.v_float = 0.0;
2398}
2399#endif
2400
2401/*
2402 * "count()" function
2403 */
2404 static void
2405f_count(typval_T *argvars, typval_T *rettv)
2406{
2407 long n = 0;
2408 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002409 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002410
Bram Moolenaar9966b212017-07-28 16:46:57 +02002411 if (argvars[2].v_type != VAR_UNKNOWN)
2412 ic = (int)get_tv_number_chk(&argvars[2], &error);
2413
2414 if (argvars[0].v_type == VAR_STRING)
2415 {
2416 char_u *expr = get_tv_string_chk(&argvars[1]);
2417 char_u *p = argvars[0].vval.v_string;
2418 char_u *next;
2419
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002420 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002421 {
2422 if (ic)
2423 {
2424 size_t len = STRLEN(expr);
2425
2426 while (*p != NUL)
2427 {
2428 if (MB_STRNICMP(p, expr, len) == 0)
2429 {
2430 ++n;
2431 p += len;
2432 }
2433 else
2434 MB_PTR_ADV(p);
2435 }
2436 }
2437 else
2438 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2439 != NULL)
2440 {
2441 ++n;
2442 p = next + STRLEN(expr);
2443 }
2444 }
2445
2446 }
2447 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002448 {
2449 listitem_T *li;
2450 list_T *l;
2451 long idx;
2452
2453 if ((l = argvars[0].vval.v_list) != NULL)
2454 {
2455 li = l->lv_first;
2456 if (argvars[2].v_type != VAR_UNKNOWN)
2457 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002458 if (argvars[3].v_type != VAR_UNKNOWN)
2459 {
2460 idx = (long)get_tv_number_chk(&argvars[3], &error);
2461 if (!error)
2462 {
2463 li = list_find(l, idx);
2464 if (li == NULL)
2465 EMSGN(_(e_listidx), idx);
2466 }
2467 }
2468 if (error)
2469 li = NULL;
2470 }
2471
2472 for ( ; li != NULL; li = li->li_next)
2473 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2474 ++n;
2475 }
2476 }
2477 else if (argvars[0].v_type == VAR_DICT)
2478 {
2479 int todo;
2480 dict_T *d;
2481 hashitem_T *hi;
2482
2483 if ((d = argvars[0].vval.v_dict) != NULL)
2484 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002485 if (argvars[2].v_type != VAR_UNKNOWN)
2486 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002487 if (argvars[3].v_type != VAR_UNKNOWN)
2488 EMSG(_(e_invarg));
2489 }
2490
2491 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2492 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2493 {
2494 if (!HASHITEM_EMPTY(hi))
2495 {
2496 --todo;
2497 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2498 ++n;
2499 }
2500 }
2501 }
2502 }
2503 else
2504 EMSG2(_(e_listdictarg), "count()");
2505 rettv->vval.v_number = n;
2506}
2507
2508/*
2509 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2510 *
2511 * Checks the existence of a cscope connection.
2512 */
2513 static void
2514f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2515{
2516#ifdef FEAT_CSCOPE
2517 int num = 0;
2518 char_u *dbpath = NULL;
2519 char_u *prepend = NULL;
2520 char_u buf[NUMBUFLEN];
2521
2522 if (argvars[0].v_type != VAR_UNKNOWN
2523 && argvars[1].v_type != VAR_UNKNOWN)
2524 {
2525 num = (int)get_tv_number(&argvars[0]);
2526 dbpath = get_tv_string(&argvars[1]);
2527 if (argvars[2].v_type != VAR_UNKNOWN)
2528 prepend = get_tv_string_buf(&argvars[2], buf);
2529 }
2530
2531 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2532#endif
2533}
2534
2535/*
2536 * "cursor(lnum, col)" function, or
2537 * "cursor(list)"
2538 *
2539 * Moves the cursor to the specified line and column.
2540 * Returns 0 when the position could be set, -1 otherwise.
2541 */
2542 static void
2543f_cursor(typval_T *argvars, typval_T *rettv)
2544{
2545 long line, col;
2546#ifdef FEAT_VIRTUALEDIT
2547 long coladd = 0;
2548#endif
2549 int set_curswant = TRUE;
2550
2551 rettv->vval.v_number = -1;
2552 if (argvars[1].v_type == VAR_UNKNOWN)
2553 {
2554 pos_T pos;
2555 colnr_T curswant = -1;
2556
2557 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2558 {
2559 EMSG(_(e_invarg));
2560 return;
2561 }
2562 line = pos.lnum;
2563 col = pos.col;
2564#ifdef FEAT_VIRTUALEDIT
2565 coladd = pos.coladd;
2566#endif
2567 if (curswant >= 0)
2568 {
2569 curwin->w_curswant = curswant - 1;
2570 set_curswant = FALSE;
2571 }
2572 }
2573 else
2574 {
2575 line = get_tv_lnum(argvars);
2576 col = (long)get_tv_number_chk(&argvars[1], NULL);
2577#ifdef FEAT_VIRTUALEDIT
2578 if (argvars[2].v_type != VAR_UNKNOWN)
2579 coladd = (long)get_tv_number_chk(&argvars[2], NULL);
2580#endif
2581 }
2582 if (line < 0 || col < 0
2583#ifdef FEAT_VIRTUALEDIT
2584 || coladd < 0
2585#endif
2586 )
2587 return; /* type error; errmsg already given */
2588 if (line > 0)
2589 curwin->w_cursor.lnum = line;
2590 if (col > 0)
2591 curwin->w_cursor.col = col - 1;
2592#ifdef FEAT_VIRTUALEDIT
2593 curwin->w_cursor.coladd = coladd;
2594#endif
2595
2596 /* Make sure the cursor is in a valid position. */
2597 check_cursor();
2598#ifdef FEAT_MBYTE
2599 /* Correct cursor for multi-byte character. */
2600 if (has_mbyte)
2601 mb_adjust_cursor();
2602#endif
2603
2604 curwin->w_set_curswant = set_curswant;
2605 rettv->vval.v_number = 0;
2606}
2607
2608/*
2609 * "deepcopy()" function
2610 */
2611 static void
2612f_deepcopy(typval_T *argvars, typval_T *rettv)
2613{
2614 int noref = 0;
2615 int copyID;
2616
2617 if (argvars[1].v_type != VAR_UNKNOWN)
2618 noref = (int)get_tv_number_chk(&argvars[1], NULL);
2619 if (noref < 0 || noref > 1)
2620 EMSG(_(e_invarg));
2621 else
2622 {
2623 copyID = get_copyID();
2624 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2625 }
2626}
2627
2628/*
2629 * "delete()" function
2630 */
2631 static void
2632f_delete(typval_T *argvars, typval_T *rettv)
2633{
2634 char_u nbuf[NUMBUFLEN];
2635 char_u *name;
2636 char_u *flags;
2637
2638 rettv->vval.v_number = -1;
2639 if (check_restricted() || check_secure())
2640 return;
2641
2642 name = get_tv_string(&argvars[0]);
2643 if (name == NULL || *name == NUL)
2644 {
2645 EMSG(_(e_invarg));
2646 return;
2647 }
2648
2649 if (argvars[1].v_type != VAR_UNKNOWN)
2650 flags = get_tv_string_buf(&argvars[1], nbuf);
2651 else
2652 flags = (char_u *)"";
2653
2654 if (*flags == NUL)
2655 /* delete a file */
2656 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2657 else if (STRCMP(flags, "d") == 0)
2658 /* delete an empty directory */
2659 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2660 else if (STRCMP(flags, "rf") == 0)
2661 /* delete a directory recursively */
2662 rettv->vval.v_number = delete_recursive(name);
2663 else
2664 EMSG2(_(e_invexpr2), flags);
2665}
2666
2667/*
2668 * "did_filetype()" function
2669 */
2670 static void
2671f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2672{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002673 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002674}
2675
2676/*
2677 * "diff_filler()" function
2678 */
2679 static void
2680f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2681{
2682#ifdef FEAT_DIFF
2683 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
2684#endif
2685}
2686
2687/*
2688 * "diff_hlID()" function
2689 */
2690 static void
2691f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2692{
2693#ifdef FEAT_DIFF
2694 linenr_T lnum = get_tv_lnum(argvars);
2695 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01002696 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002697 static int fnum = 0;
2698 static int change_start = 0;
2699 static int change_end = 0;
2700 static hlf_T hlID = (hlf_T)0;
2701 int filler_lines;
2702 int col;
2703
2704 if (lnum < 0) /* ignore type error in {lnum} arg */
2705 lnum = 0;
2706 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002707 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002708 || fnum != curbuf->b_fnum)
2709 {
2710 /* New line, buffer, change: need to get the values. */
2711 filler_lines = diff_check(curwin, lnum);
2712 if (filler_lines < 0)
2713 {
2714 if (filler_lines == -1)
2715 {
2716 change_start = MAXCOL;
2717 change_end = -1;
2718 if (diff_find_change(curwin, lnum, &change_start, &change_end))
2719 hlID = HLF_ADD; /* added line */
2720 else
2721 hlID = HLF_CHD; /* changed line */
2722 }
2723 else
2724 hlID = HLF_ADD; /* added line */
2725 }
2726 else
2727 hlID = (hlf_T)0;
2728 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002729 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002730 fnum = curbuf->b_fnum;
2731 }
2732
2733 if (hlID == HLF_CHD || hlID == HLF_TXD)
2734 {
2735 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
2736 if (col >= change_start && col <= change_end)
2737 hlID = HLF_TXD; /* changed text */
2738 else
2739 hlID = HLF_CHD; /* changed line */
2740 }
2741 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
2742#endif
2743}
2744
2745/*
2746 * "empty({expr})" function
2747 */
2748 static void
2749f_empty(typval_T *argvars, typval_T *rettv)
2750{
2751 int n = FALSE;
2752
2753 switch (argvars[0].v_type)
2754 {
2755 case VAR_STRING:
2756 case VAR_FUNC:
2757 n = argvars[0].vval.v_string == NULL
2758 || *argvars[0].vval.v_string == NUL;
2759 break;
2760 case VAR_PARTIAL:
2761 n = FALSE;
2762 break;
2763 case VAR_NUMBER:
2764 n = argvars[0].vval.v_number == 0;
2765 break;
2766 case VAR_FLOAT:
2767#ifdef FEAT_FLOAT
2768 n = argvars[0].vval.v_float == 0.0;
2769 break;
2770#endif
2771 case VAR_LIST:
2772 n = argvars[0].vval.v_list == NULL
2773 || argvars[0].vval.v_list->lv_first == NULL;
2774 break;
2775 case VAR_DICT:
2776 n = argvars[0].vval.v_dict == NULL
2777 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2778 break;
2779 case VAR_SPECIAL:
2780 n = argvars[0].vval.v_number != VVAL_TRUE;
2781 break;
2782
2783 case VAR_JOB:
2784#ifdef FEAT_JOB_CHANNEL
2785 n = argvars[0].vval.v_job == NULL
2786 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2787 break;
2788#endif
2789 case VAR_CHANNEL:
2790#ifdef FEAT_JOB_CHANNEL
2791 n = argvars[0].vval.v_channel == NULL
2792 || !channel_is_open(argvars[0].vval.v_channel);
2793 break;
2794#endif
2795 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01002796 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002797 n = TRUE;
2798 break;
2799 }
2800
2801 rettv->vval.v_number = n;
2802}
2803
2804/*
2805 * "escape({string}, {chars})" function
2806 */
2807 static void
2808f_escape(typval_T *argvars, typval_T *rettv)
2809{
2810 char_u buf[NUMBUFLEN];
2811
2812 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
2813 get_tv_string_buf(&argvars[1], buf));
2814 rettv->v_type = VAR_STRING;
2815}
2816
2817/*
2818 * "eval()" function
2819 */
2820 static void
2821f_eval(typval_T *argvars, typval_T *rettv)
2822{
2823 char_u *s, *p;
2824
2825 s = get_tv_string_chk(&argvars[0]);
2826 if (s != NULL)
2827 s = skipwhite(s);
2828
2829 p = s;
2830 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
2831 {
2832 if (p != NULL && !aborting())
2833 EMSG2(_(e_invexpr2), p);
2834 need_clr_eos = FALSE;
2835 rettv->v_type = VAR_NUMBER;
2836 rettv->vval.v_number = 0;
2837 }
2838 else if (*s != NUL)
2839 EMSG(_(e_trailing));
2840}
2841
2842/*
2843 * "eventhandler()" function
2844 */
2845 static void
2846f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
2847{
2848 rettv->vval.v_number = vgetc_busy;
2849}
2850
2851/*
2852 * "executable()" function
2853 */
2854 static void
2855f_executable(typval_T *argvars, typval_T *rettv)
2856{
2857 char_u *name = get_tv_string(&argvars[0]);
2858
2859 /* Check in $PATH and also check directly if there is a directory name. */
2860 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
2861 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
2862}
2863
2864static garray_T redir_execute_ga;
2865
2866/*
2867 * Append "value[value_len]" to the execute() output.
2868 */
2869 void
2870execute_redir_str(char_u *value, int value_len)
2871{
2872 int len;
2873
2874 if (value_len == -1)
2875 len = (int)STRLEN(value); /* Append the entire string */
2876 else
2877 len = value_len; /* Append only "value_len" characters */
2878 if (ga_grow(&redir_execute_ga, len) == OK)
2879 {
2880 mch_memmove((char *)redir_execute_ga.ga_data
2881 + redir_execute_ga.ga_len, value, len);
2882 redir_execute_ga.ga_len += len;
2883 }
2884}
2885
2886/*
2887 * Get next line from a list.
2888 * Called by do_cmdline() to get the next line.
2889 * Returns allocated string, or NULL for end of function.
2890 */
2891
2892 static char_u *
2893get_list_line(
2894 int c UNUSED,
2895 void *cookie,
2896 int indent UNUSED)
2897{
2898 listitem_T **p = (listitem_T **)cookie;
2899 listitem_T *item = *p;
2900 char_u buf[NUMBUFLEN];
2901 char_u *s;
2902
2903 if (item == NULL)
2904 return NULL;
2905 s = get_tv_string_buf_chk(&item->li_tv, buf);
2906 *p = item->li_next;
2907 return s == NULL ? NULL : vim_strsave(s);
2908}
2909
2910/*
2911 * "execute()" function
2912 */
2913 static void
2914f_execute(typval_T *argvars, typval_T *rettv)
2915{
2916 char_u *cmd = NULL;
2917 list_T *list = NULL;
2918 int save_msg_silent = msg_silent;
2919 int save_emsg_silent = emsg_silent;
2920 int save_emsg_noredir = emsg_noredir;
2921 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01002922 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002923 garray_T save_ga;
2924
2925 rettv->vval.v_string = NULL;
2926 rettv->v_type = VAR_STRING;
2927
2928 if (argvars[0].v_type == VAR_LIST)
2929 {
2930 list = argvars[0].vval.v_list;
2931 if (list == NULL || list->lv_first == NULL)
2932 /* empty list, no commands, empty output */
2933 return;
2934 ++list->lv_refcount;
2935 }
2936 else
2937 {
2938 cmd = get_tv_string_chk(&argvars[0]);
2939 if (cmd == NULL)
2940 return;
2941 }
2942
2943 if (argvars[1].v_type != VAR_UNKNOWN)
2944 {
2945 char_u buf[NUMBUFLEN];
2946 char_u *s = get_tv_string_buf_chk(&argvars[1], buf);
2947
2948 if (s == NULL)
2949 return;
2950 if (STRNCMP(s, "silent", 6) == 0)
2951 ++msg_silent;
2952 if (STRCMP(s, "silent!") == 0)
2953 {
2954 emsg_silent = TRUE;
2955 emsg_noredir = TRUE;
2956 }
2957 }
2958 else
2959 ++msg_silent;
2960
2961 if (redir_execute)
2962 save_ga = redir_execute_ga;
2963 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
2964 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01002965 redir_off = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002966
2967 if (cmd != NULL)
2968 do_cmdline_cmd(cmd);
2969 else
2970 {
2971 listitem_T *item = list->lv_first;
2972
2973 do_cmdline(NULL, get_list_line, (void *)&item,
2974 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
2975 --list->lv_refcount;
2976 }
2977
Bram Moolenaard297f352017-01-29 20:31:21 +01002978 /* Need to append a NUL to the result. */
2979 if (ga_grow(&redir_execute_ga, 1) == OK)
2980 {
2981 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
2982 rettv->vval.v_string = redir_execute_ga.ga_data;
2983 }
2984 else
2985 {
2986 ga_clear(&redir_execute_ga);
2987 rettv->vval.v_string = NULL;
2988 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002989 msg_silent = save_msg_silent;
2990 emsg_silent = save_emsg_silent;
2991 emsg_noredir = save_emsg_noredir;
2992
2993 redir_execute = save_redir_execute;
2994 if (redir_execute)
2995 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01002996 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002997
2998 /* "silent reg" or "silent echo x" leaves msg_col somewhere in the
2999 * line. Put it back in the first column. */
3000 msg_col = 0;
3001}
3002
3003/*
3004 * "exepath()" function
3005 */
3006 static void
3007f_exepath(typval_T *argvars, typval_T *rettv)
3008{
3009 char_u *p = NULL;
3010
3011 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
3012 rettv->v_type = VAR_STRING;
3013 rettv->vval.v_string = p;
3014}
3015
3016/*
3017 * "exists()" function
3018 */
3019 static void
3020f_exists(typval_T *argvars, typval_T *rettv)
3021{
3022 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003023 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003024
3025 p = get_tv_string(&argvars[0]);
3026 if (*p == '$') /* environment variable */
3027 {
3028 /* first try "normal" environment variables (fast) */
3029 if (mch_getenv(p + 1) != NULL)
3030 n = TRUE;
3031 else
3032 {
3033 /* try expanding things like $VIM and ${HOME} */
3034 p = expand_env_save(p);
3035 if (p != NULL && *p != '$')
3036 n = TRUE;
3037 vim_free(p);
3038 }
3039 }
3040 else if (*p == '&' || *p == '+') /* option */
3041 {
3042 n = (get_option_tv(&p, NULL, TRUE) == OK);
3043 if (*skipwhite(p) != NUL)
3044 n = FALSE; /* trailing garbage */
3045 }
3046 else if (*p == '*') /* internal or user defined function */
3047 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003048 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003049 }
3050 else if (*p == ':')
3051 {
3052 n = cmd_exists(p + 1);
3053 }
3054 else if (*p == '#')
3055 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003056 if (p[1] == '#')
3057 n = autocmd_supported(p + 2);
3058 else
3059 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003060 }
3061 else /* internal variable */
3062 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003063 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003064 }
3065
3066 rettv->vval.v_number = n;
3067}
3068
3069#ifdef FEAT_FLOAT
3070/*
3071 * "exp()" function
3072 */
3073 static void
3074f_exp(typval_T *argvars, typval_T *rettv)
3075{
3076 float_T f = 0.0;
3077
3078 rettv->v_type = VAR_FLOAT;
3079 if (get_float_arg(argvars, &f) == OK)
3080 rettv->vval.v_float = exp(f);
3081 else
3082 rettv->vval.v_float = 0.0;
3083}
3084#endif
3085
3086/*
3087 * "expand()" function
3088 */
3089 static void
3090f_expand(typval_T *argvars, typval_T *rettv)
3091{
3092 char_u *s;
3093 int len;
3094 char_u *errormsg;
3095 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3096 expand_T xpc;
3097 int error = FALSE;
3098 char_u *result;
3099
3100 rettv->v_type = VAR_STRING;
3101 if (argvars[1].v_type != VAR_UNKNOWN
3102 && argvars[2].v_type != VAR_UNKNOWN
3103 && get_tv_number_chk(&argvars[2], &error)
3104 && !error)
3105 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003106 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003107 }
3108
3109 s = get_tv_string(&argvars[0]);
3110 if (*s == '%' || *s == '#' || *s == '<')
3111 {
3112 ++emsg_off;
3113 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3114 --emsg_off;
3115 if (rettv->v_type == VAR_LIST)
3116 {
3117 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3118 list_append_string(rettv->vval.v_list, result, -1);
3119 else
3120 vim_free(result);
3121 }
3122 else
3123 rettv->vval.v_string = result;
3124 }
3125 else
3126 {
3127 /* When the optional second argument is non-zero, don't remove matches
3128 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3129 if (argvars[1].v_type != VAR_UNKNOWN
3130 && get_tv_number_chk(&argvars[1], &error))
3131 options |= WILD_KEEP_ALL;
3132 if (!error)
3133 {
3134 ExpandInit(&xpc);
3135 xpc.xp_context = EXPAND_FILES;
3136 if (p_wic)
3137 options += WILD_ICASE;
3138 if (rettv->v_type == VAR_STRING)
3139 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3140 options, WILD_ALL);
3141 else if (rettv_list_alloc(rettv) != FAIL)
3142 {
3143 int i;
3144
3145 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3146 for (i = 0; i < xpc.xp_numfiles; i++)
3147 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3148 ExpandCleanup(&xpc);
3149 }
3150 }
3151 else
3152 rettv->vval.v_string = NULL;
3153 }
3154}
3155
3156/*
3157 * "extend(list, list [, idx])" function
3158 * "extend(dict, dict [, action])" function
3159 */
3160 static void
3161f_extend(typval_T *argvars, typval_T *rettv)
3162{
3163 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3164
3165 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3166 {
3167 list_T *l1, *l2;
3168 listitem_T *item;
3169 long before;
3170 int error = FALSE;
3171
3172 l1 = argvars[0].vval.v_list;
3173 l2 = argvars[1].vval.v_list;
3174 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
3175 && l2 != NULL)
3176 {
3177 if (argvars[2].v_type != VAR_UNKNOWN)
3178 {
3179 before = (long)get_tv_number_chk(&argvars[2], &error);
3180 if (error)
3181 return; /* type error; errmsg already given */
3182
3183 if (before == l1->lv_len)
3184 item = NULL;
3185 else
3186 {
3187 item = list_find(l1, before);
3188 if (item == NULL)
3189 {
3190 EMSGN(_(e_listidx), before);
3191 return;
3192 }
3193 }
3194 }
3195 else
3196 item = NULL;
3197 list_extend(l1, l2, item);
3198
3199 copy_tv(&argvars[0], rettv);
3200 }
3201 }
3202 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3203 {
3204 dict_T *d1, *d2;
3205 char_u *action;
3206 int i;
3207
3208 d1 = argvars[0].vval.v_dict;
3209 d2 = argvars[1].vval.v_dict;
3210 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
3211 && d2 != NULL)
3212 {
3213 /* Check the third argument. */
3214 if (argvars[2].v_type != VAR_UNKNOWN)
3215 {
3216 static char *(av[]) = {"keep", "force", "error"};
3217
3218 action = get_tv_string_chk(&argvars[2]);
3219 if (action == NULL)
3220 return; /* type error; errmsg already given */
3221 for (i = 0; i < 3; ++i)
3222 if (STRCMP(action, av[i]) == 0)
3223 break;
3224 if (i == 3)
3225 {
3226 EMSG2(_(e_invarg2), action);
3227 return;
3228 }
3229 }
3230 else
3231 action = (char_u *)"force";
3232
3233 dict_extend(d1, d2, action);
3234
3235 copy_tv(&argvars[0], rettv);
3236 }
3237 }
3238 else
3239 EMSG2(_(e_listdictarg), "extend()");
3240}
3241
3242/*
3243 * "feedkeys()" function
3244 */
3245 static void
3246f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3247{
3248 int remap = TRUE;
3249 int insert = FALSE;
3250 char_u *keys, *flags;
3251 char_u nbuf[NUMBUFLEN];
3252 int typed = FALSE;
3253 int execute = FALSE;
3254 int dangerous = FALSE;
3255 char_u *keys_esc;
3256
3257 /* This is not allowed in the sandbox. If the commands would still be
3258 * executed in the sandbox it would be OK, but it probably happens later,
3259 * when "sandbox" is no longer set. */
3260 if (check_secure())
3261 return;
3262
3263 keys = get_tv_string(&argvars[0]);
3264
3265 if (argvars[1].v_type != VAR_UNKNOWN)
3266 {
3267 flags = get_tv_string_buf(&argvars[1], nbuf);
3268 for ( ; *flags != NUL; ++flags)
3269 {
3270 switch (*flags)
3271 {
3272 case 'n': remap = FALSE; break;
3273 case 'm': remap = TRUE; break;
3274 case 't': typed = TRUE; break;
3275 case 'i': insert = TRUE; break;
3276 case 'x': execute = TRUE; break;
3277 case '!': dangerous = TRUE; break;
3278 }
3279 }
3280 }
3281
3282 if (*keys != NUL || execute)
3283 {
3284 /* Need to escape K_SPECIAL and CSI before putting the string in the
3285 * typeahead buffer. */
3286 keys_esc = vim_strsave_escape_csi(keys);
3287 if (keys_esc != NULL)
3288 {
3289 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
3290 insert ? 0 : typebuf.tb_len, !typed, FALSE);
3291 vim_free(keys_esc);
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003292 if (vgetc_busy
3293#ifdef FEAT_TIMERS
3294 || timer_busy
3295#endif
3296 )
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003297 typebuf_was_filled = TRUE;
3298 if (execute)
3299 {
3300 int save_msg_scroll = msg_scroll;
3301
3302 /* Avoid a 1 second delay when the keys start Insert mode. */
3303 msg_scroll = FALSE;
3304
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02003305#ifdef FEAT_TERMINAL
3306 if (term_use_loop())
3307 terminal_loop(FALSE);
3308 else
3309#endif
3310 {
3311 if (!dangerous)
3312 ++ex_normal_busy;
3313 exec_normal(TRUE);
3314 if (!dangerous)
3315 --ex_normal_busy;
3316 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003317 msg_scroll |= save_msg_scroll;
3318 }
3319 }
3320 }
3321}
3322
3323/*
3324 * "filereadable()" function
3325 */
3326 static void
3327f_filereadable(typval_T *argvars, typval_T *rettv)
3328{
3329 int fd;
3330 char_u *p;
3331 int n;
3332
3333#ifndef O_NONBLOCK
3334# define O_NONBLOCK 0
3335#endif
3336 p = get_tv_string(&argvars[0]);
3337 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3338 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3339 {
3340 n = TRUE;
3341 close(fd);
3342 }
3343 else
3344 n = FALSE;
3345
3346 rettv->vval.v_number = n;
3347}
3348
3349/*
3350 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3351 * rights to write into.
3352 */
3353 static void
3354f_filewritable(typval_T *argvars, typval_T *rettv)
3355{
3356 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
3357}
3358
3359 static void
3360findfilendir(
3361 typval_T *argvars UNUSED,
3362 typval_T *rettv,
3363 int find_what UNUSED)
3364{
3365#ifdef FEAT_SEARCHPATH
3366 char_u *fname;
3367 char_u *fresult = NULL;
3368 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3369 char_u *p;
3370 char_u pathbuf[NUMBUFLEN];
3371 int count = 1;
3372 int first = TRUE;
3373 int error = FALSE;
3374#endif
3375
3376 rettv->vval.v_string = NULL;
3377 rettv->v_type = VAR_STRING;
3378
3379#ifdef FEAT_SEARCHPATH
3380 fname = get_tv_string(&argvars[0]);
3381
3382 if (argvars[1].v_type != VAR_UNKNOWN)
3383 {
3384 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
3385 if (p == NULL)
3386 error = TRUE;
3387 else
3388 {
3389 if (*p != NUL)
3390 path = p;
3391
3392 if (argvars[2].v_type != VAR_UNKNOWN)
3393 count = (int)get_tv_number_chk(&argvars[2], &error);
3394 }
3395 }
3396
3397 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3398 error = TRUE;
3399
3400 if (*fname != NUL && !error)
3401 {
3402 do
3403 {
3404 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3405 vim_free(fresult);
3406 fresult = find_file_in_path_option(first ? fname : NULL,
3407 first ? (int)STRLEN(fname) : 0,
3408 0, first, path,
3409 find_what,
3410 curbuf->b_ffname,
3411 find_what == FINDFILE_DIR
3412 ? (char_u *)"" : curbuf->b_p_sua);
3413 first = FALSE;
3414
3415 if (fresult != NULL && rettv->v_type == VAR_LIST)
3416 list_append_string(rettv->vval.v_list, fresult, -1);
3417
3418 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3419 }
3420
3421 if (rettv->v_type == VAR_STRING)
3422 rettv->vval.v_string = fresult;
3423#endif
3424}
3425
3426/*
3427 * "filter()" function
3428 */
3429 static void
3430f_filter(typval_T *argvars, typval_T *rettv)
3431{
3432 filter_map(argvars, rettv, FALSE);
3433}
3434
3435/*
3436 * "finddir({fname}[, {path}[, {count}]])" function
3437 */
3438 static void
3439f_finddir(typval_T *argvars, typval_T *rettv)
3440{
3441 findfilendir(argvars, rettv, FINDFILE_DIR);
3442}
3443
3444/*
3445 * "findfile({fname}[, {path}[, {count}]])" function
3446 */
3447 static void
3448f_findfile(typval_T *argvars, typval_T *rettv)
3449{
3450 findfilendir(argvars, rettv, FINDFILE_FILE);
3451}
3452
3453#ifdef FEAT_FLOAT
3454/*
3455 * "float2nr({float})" function
3456 */
3457 static void
3458f_float2nr(typval_T *argvars, typval_T *rettv)
3459{
3460 float_T f = 0.0;
3461
3462 if (get_float_arg(argvars, &f) == OK)
3463 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003464 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003465 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003466 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003467 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003468 else
3469 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003470 }
3471}
3472
3473/*
3474 * "floor({float})" function
3475 */
3476 static void
3477f_floor(typval_T *argvars, typval_T *rettv)
3478{
3479 float_T f = 0.0;
3480
3481 rettv->v_type = VAR_FLOAT;
3482 if (get_float_arg(argvars, &f) == OK)
3483 rettv->vval.v_float = floor(f);
3484 else
3485 rettv->vval.v_float = 0.0;
3486}
3487
3488/*
3489 * "fmod()" function
3490 */
3491 static void
3492f_fmod(typval_T *argvars, typval_T *rettv)
3493{
3494 float_T fx = 0.0, fy = 0.0;
3495
3496 rettv->v_type = VAR_FLOAT;
3497 if (get_float_arg(argvars, &fx) == OK
3498 && get_float_arg(&argvars[1], &fy) == OK)
3499 rettv->vval.v_float = fmod(fx, fy);
3500 else
3501 rettv->vval.v_float = 0.0;
3502}
3503#endif
3504
3505/*
3506 * "fnameescape({string})" function
3507 */
3508 static void
3509f_fnameescape(typval_T *argvars, typval_T *rettv)
3510{
3511 rettv->vval.v_string = vim_strsave_fnameescape(
3512 get_tv_string(&argvars[0]), FALSE);
3513 rettv->v_type = VAR_STRING;
3514}
3515
3516/*
3517 * "fnamemodify({fname}, {mods})" function
3518 */
3519 static void
3520f_fnamemodify(typval_T *argvars, typval_T *rettv)
3521{
3522 char_u *fname;
3523 char_u *mods;
3524 int usedlen = 0;
3525 int len;
3526 char_u *fbuf = NULL;
3527 char_u buf[NUMBUFLEN];
3528
3529 fname = get_tv_string_chk(&argvars[0]);
3530 mods = get_tv_string_buf_chk(&argvars[1], buf);
3531 if (fname == NULL || mods == NULL)
3532 fname = NULL;
3533 else
3534 {
3535 len = (int)STRLEN(fname);
3536 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
3537 }
3538
3539 rettv->v_type = VAR_STRING;
3540 if (fname == NULL)
3541 rettv->vval.v_string = NULL;
3542 else
3543 rettv->vval.v_string = vim_strnsave(fname, len);
3544 vim_free(fbuf);
3545}
3546
3547static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
3548
3549/*
3550 * "foldclosed()" function
3551 */
3552 static void
3553foldclosed_both(
3554 typval_T *argvars UNUSED,
3555 typval_T *rettv,
3556 int end UNUSED)
3557{
3558#ifdef FEAT_FOLDING
3559 linenr_T lnum;
3560 linenr_T first, last;
3561
3562 lnum = get_tv_lnum(argvars);
3563 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3564 {
3565 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3566 {
3567 if (end)
3568 rettv->vval.v_number = (varnumber_T)last;
3569 else
3570 rettv->vval.v_number = (varnumber_T)first;
3571 return;
3572 }
3573 }
3574#endif
3575 rettv->vval.v_number = -1;
3576}
3577
3578/*
3579 * "foldclosed()" function
3580 */
3581 static void
3582f_foldclosed(typval_T *argvars, typval_T *rettv)
3583{
3584 foldclosed_both(argvars, rettv, FALSE);
3585}
3586
3587/*
3588 * "foldclosedend()" function
3589 */
3590 static void
3591f_foldclosedend(typval_T *argvars, typval_T *rettv)
3592{
3593 foldclosed_both(argvars, rettv, TRUE);
3594}
3595
3596/*
3597 * "foldlevel()" function
3598 */
3599 static void
3600f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3601{
3602#ifdef FEAT_FOLDING
3603 linenr_T lnum;
3604
3605 lnum = get_tv_lnum(argvars);
3606 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3607 rettv->vval.v_number = foldLevel(lnum);
3608#endif
3609}
3610
3611/*
3612 * "foldtext()" function
3613 */
3614 static void
3615f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
3616{
3617#ifdef FEAT_FOLDING
3618 linenr_T foldstart;
3619 linenr_T foldend;
3620 char_u *dashes;
3621 linenr_T lnum;
3622 char_u *s;
3623 char_u *r;
3624 int len;
3625 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003626 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003627#endif
3628
3629 rettv->v_type = VAR_STRING;
3630 rettv->vval.v_string = NULL;
3631#ifdef FEAT_FOLDING
3632 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
3633 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
3634 dashes = get_vim_var_str(VV_FOLDDASHES);
3635 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
3636 && dashes != NULL)
3637 {
3638 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02003639 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003640 if (!linewhite(lnum))
3641 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003642
3643 /* Find interesting text in this line. */
3644 s = skipwhite(ml_get(lnum));
3645 /* skip C comment-start */
3646 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
3647 {
3648 s = skipwhite(s + 2);
3649 if (*skipwhite(s) == NUL
3650 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
3651 {
3652 s = skipwhite(ml_get(lnum + 1));
3653 if (*s == '*')
3654 s = skipwhite(s + 1);
3655 }
3656 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02003657 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003658 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003659 r = alloc((unsigned)(STRLEN(txt)
3660 + STRLEN(dashes) /* for %s */
3661 + 20 /* for %3ld */
3662 + STRLEN(s))); /* concatenated */
3663 if (r != NULL)
3664 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02003665 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003666 len = (int)STRLEN(r);
3667 STRCAT(r, s);
3668 /* remove 'foldmarker' and 'commentstring' */
3669 foldtext_cleanup(r + len);
3670 rettv->vval.v_string = r;
3671 }
3672 }
3673#endif
3674}
3675
3676/*
3677 * "foldtextresult(lnum)" function
3678 */
3679 static void
3680f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
3681{
3682#ifdef FEAT_FOLDING
3683 linenr_T lnum;
3684 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003685 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003686 foldinfo_T foldinfo;
3687 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003688 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003689#endif
3690
3691 rettv->v_type = VAR_STRING;
3692 rettv->vval.v_string = NULL;
3693#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003694 if (entered)
3695 return; /* reject recursive use */
3696 entered = TRUE;
3697
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003698 lnum = get_tv_lnum(argvars);
3699 /* treat illegal types and illegal string values for {lnum} the same */
3700 if (lnum < 0)
3701 lnum = 0;
3702 fold_count = foldedCount(curwin, lnum, &foldinfo);
3703 if (fold_count > 0)
3704 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01003705 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
3706 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003707 if (text == buf)
3708 text = vim_strsave(text);
3709 rettv->vval.v_string = text;
3710 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003711
3712 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003713#endif
3714}
3715
3716/*
3717 * "foreground()" function
3718 */
3719 static void
3720f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3721{
3722#ifdef FEAT_GUI
3723 if (gui.in_use)
3724 gui_mch_set_foreground();
3725#else
3726# ifdef WIN32
3727 win32_set_foreground();
3728# endif
3729#endif
3730}
3731
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003732 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003733common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003734{
3735 char_u *s;
3736 char_u *name;
3737 int use_string = FALSE;
3738 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003739 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003740
3741 if (argvars[0].v_type == VAR_FUNC)
3742 {
3743 /* function(MyFunc, [arg], dict) */
3744 s = argvars[0].vval.v_string;
3745 }
3746 else if (argvars[0].v_type == VAR_PARTIAL
3747 && argvars[0].vval.v_partial != NULL)
3748 {
3749 /* function(dict.MyFunc, [arg]) */
3750 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003751 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003752 }
3753 else
3754 {
3755 /* function('MyFunc', [arg], dict) */
3756 s = get_tv_string(&argvars[0]);
3757 use_string = TRUE;
3758 }
3759
Bram Moolenaar843b8842016-08-21 14:36:15 +02003760 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003761 {
3762 name = s;
3763 trans_name = trans_function_name(&name, FALSE,
3764 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
3765 if (*name != NUL)
3766 s = NULL;
3767 }
3768
Bram Moolenaar843b8842016-08-21 14:36:15 +02003769 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
3770 || (is_funcref && trans_name == NULL))
Bram Moolenaar5582ef12016-09-14 22:16:13 +02003771 EMSG2(_(e_invarg2), use_string ? get_tv_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003772 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003773 else if (trans_name != NULL && (is_funcref
3774 ? find_func(trans_name) == NULL
3775 : !translated_function_exists(trans_name)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003776 EMSG2(_("E700: Unknown function: %s"), s);
3777 else
3778 {
3779 int dict_idx = 0;
3780 int arg_idx = 0;
3781 list_T *list = NULL;
3782
3783 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
3784 {
3785 char sid_buf[25];
3786 int off = *s == 's' ? 2 : 5;
3787
3788 /* Expand s: and <SID> into <SNR>nr_, so that the function can
3789 * also be called from another script. Using trans_function_name()
3790 * would also work, but some plugins depend on the name being
3791 * printable text. */
3792 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
3793 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
3794 if (name != NULL)
3795 {
3796 STRCPY(name, sid_buf);
3797 STRCAT(name, s + off);
3798 }
3799 }
3800 else
3801 name = vim_strsave(s);
3802
3803 if (argvars[1].v_type != VAR_UNKNOWN)
3804 {
3805 if (argvars[2].v_type != VAR_UNKNOWN)
3806 {
3807 /* function(name, [args], dict) */
3808 arg_idx = 1;
3809 dict_idx = 2;
3810 }
3811 else if (argvars[1].v_type == VAR_DICT)
3812 /* function(name, dict) */
3813 dict_idx = 1;
3814 else
3815 /* function(name, [args]) */
3816 arg_idx = 1;
3817 if (dict_idx > 0)
3818 {
3819 if (argvars[dict_idx].v_type != VAR_DICT)
3820 {
3821 EMSG(_("E922: expected a dict"));
3822 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003823 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003824 }
3825 if (argvars[dict_idx].vval.v_dict == NULL)
3826 dict_idx = 0;
3827 }
3828 if (arg_idx > 0)
3829 {
3830 if (argvars[arg_idx].v_type != VAR_LIST)
3831 {
3832 EMSG(_("E923: Second argument of function() must be a list or a dict"));
3833 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003834 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003835 }
3836 list = argvars[arg_idx].vval.v_list;
3837 if (list == NULL || list->lv_len == 0)
3838 arg_idx = 0;
3839 }
3840 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003841 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003842 {
3843 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
3844
3845 /* result is a VAR_PARTIAL */
3846 if (pt == NULL)
3847 vim_free(name);
3848 else
3849 {
3850 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
3851 {
3852 listitem_T *li;
3853 int i = 0;
3854 int arg_len = 0;
3855 int lv_len = 0;
3856
3857 if (arg_pt != NULL)
3858 arg_len = arg_pt->pt_argc;
3859 if (list != NULL)
3860 lv_len = list->lv_len;
3861 pt->pt_argc = arg_len + lv_len;
3862 pt->pt_argv = (typval_T *)alloc(
3863 sizeof(typval_T) * pt->pt_argc);
3864 if (pt->pt_argv == NULL)
3865 {
3866 vim_free(pt);
3867 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003868 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003869 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003870 for (i = 0; i < arg_len; i++)
3871 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
3872 if (lv_len > 0)
3873 for (li = list->lv_first; li != NULL;
3874 li = li->li_next)
3875 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003876 }
3877
3878 /* For "function(dict.func, [], dict)" and "func" is a partial
3879 * use "dict". That is backwards compatible. */
3880 if (dict_idx > 0)
3881 {
3882 /* The dict is bound explicitly, pt_auto is FALSE. */
3883 pt->pt_dict = argvars[dict_idx].vval.v_dict;
3884 ++pt->pt_dict->dv_refcount;
3885 }
3886 else if (arg_pt != NULL)
3887 {
3888 /* If the dict was bound automatically the result is also
3889 * bound automatically. */
3890 pt->pt_dict = arg_pt->pt_dict;
3891 pt->pt_auto = arg_pt->pt_auto;
3892 if (pt->pt_dict != NULL)
3893 ++pt->pt_dict->dv_refcount;
3894 }
3895
3896 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003897 if (arg_pt != NULL && arg_pt->pt_func != NULL)
3898 {
3899 pt->pt_func = arg_pt->pt_func;
3900 func_ptr_ref(pt->pt_func);
3901 vim_free(name);
3902 }
3903 else if (is_funcref)
3904 {
3905 pt->pt_func = find_func(trans_name);
3906 func_ptr_ref(pt->pt_func);
3907 vim_free(name);
3908 }
3909 else
3910 {
3911 pt->pt_name = name;
3912 func_ref(name);
3913 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003914 }
3915 rettv->v_type = VAR_PARTIAL;
3916 rettv->vval.v_partial = pt;
3917 }
3918 else
3919 {
3920 /* result is a VAR_FUNC */
3921 rettv->v_type = VAR_FUNC;
3922 rettv->vval.v_string = name;
3923 func_ref(name);
3924 }
3925 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003926theend:
3927 vim_free(trans_name);
3928}
3929
3930/*
3931 * "funcref()" function
3932 */
3933 static void
3934f_funcref(typval_T *argvars, typval_T *rettv)
3935{
3936 common_function(argvars, rettv, TRUE);
3937}
3938
3939/*
3940 * "function()" function
3941 */
3942 static void
3943f_function(typval_T *argvars, typval_T *rettv)
3944{
3945 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003946}
3947
3948/*
3949 * "garbagecollect()" function
3950 */
3951 static void
3952f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
3953{
3954 /* This is postponed until we are back at the toplevel, because we may be
3955 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
3956 want_garbage_collect = TRUE;
3957
3958 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
3959 garbage_collect_at_exit = TRUE;
3960}
3961
3962/*
3963 * "get()" function
3964 */
3965 static void
3966f_get(typval_T *argvars, typval_T *rettv)
3967{
3968 listitem_T *li;
3969 list_T *l;
3970 dictitem_T *di;
3971 dict_T *d;
3972 typval_T *tv = NULL;
3973
3974 if (argvars[0].v_type == VAR_LIST)
3975 {
3976 if ((l = argvars[0].vval.v_list) != NULL)
3977 {
3978 int error = FALSE;
3979
3980 li = list_find(l, (long)get_tv_number_chk(&argvars[1], &error));
3981 if (!error && li != NULL)
3982 tv = &li->li_tv;
3983 }
3984 }
3985 else if (argvars[0].v_type == VAR_DICT)
3986 {
3987 if ((d = argvars[0].vval.v_dict) != NULL)
3988 {
3989 di = dict_find(d, get_tv_string(&argvars[1]), -1);
3990 if (di != NULL)
3991 tv = &di->di_tv;
3992 }
3993 }
3994 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
3995 {
3996 partial_T *pt;
3997 partial_T fref_pt;
3998
3999 if (argvars[0].v_type == VAR_PARTIAL)
4000 pt = argvars[0].vval.v_partial;
4001 else
4002 {
4003 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4004 fref_pt.pt_name = argvars[0].vval.v_string;
4005 pt = &fref_pt;
4006 }
4007
4008 if (pt != NULL)
4009 {
4010 char_u *what = get_tv_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004011 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004012
4013 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4014 {
4015 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004016 n = partial_name(pt);
4017 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004018 rettv->vval.v_string = NULL;
4019 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004020 {
4021 rettv->vval.v_string = vim_strsave(n);
4022 if (rettv->v_type == VAR_FUNC)
4023 func_ref(rettv->vval.v_string);
4024 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004025 }
4026 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004027 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004028 else if (STRCMP(what, "args") == 0)
4029 {
4030 rettv->v_type = VAR_LIST;
4031 if (rettv_list_alloc(rettv) == OK)
4032 {
4033 int i;
4034
4035 for (i = 0; i < pt->pt_argc; ++i)
4036 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4037 }
4038 }
4039 else
4040 EMSG2(_(e_invarg2), what);
4041 return;
4042 }
4043 }
4044 else
4045 EMSG2(_(e_listdictarg), "get()");
4046
4047 if (tv == NULL)
4048 {
4049 if (argvars[2].v_type != VAR_UNKNOWN)
4050 copy_tv(&argvars[2], rettv);
4051 }
4052 else
4053 copy_tv(tv, rettv);
4054}
4055
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004056#ifdef FEAT_SIGNS
4057/*
4058 * Returns information about signs placed in a buffer as list of dicts.
4059 */
4060 static void
4061get_buffer_signs(buf_T *buf, list_T *l)
4062{
4063 signlist_T *sign;
4064
4065 for (sign = buf->b_signlist; sign; sign = sign->next)
4066 {
4067 dict_T *d = dict_alloc();
4068
4069 if (d != NULL)
4070 {
4071 dict_add_nr_str(d, "id", sign->id, NULL);
4072 dict_add_nr_str(d, "lnum", sign->lnum, NULL);
Bram Moolenaar6a402ed2016-08-28 14:11:24 +02004073 dict_add_nr_str(d, "name", 0L, sign_typenr2name(sign->typenr));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004074
4075 list_append_dict(l, d);
4076 }
4077 }
4078}
4079#endif
4080
4081/*
4082 * Returns buffer options, variables and other attributes in a dictionary.
4083 */
4084 static dict_T *
4085get_buffer_info(buf_T *buf)
4086{
4087 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004088 tabpage_T *tp;
4089 win_T *wp;
4090 list_T *windows;
4091
4092 dict = dict_alloc();
4093 if (dict == NULL)
4094 return NULL;
4095
Bram Moolenaar33928832016-08-18 21:22:04 +02004096 dict_add_nr_str(dict, "bufnr", buf->b_fnum, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004097 dict_add_nr_str(dict, "name", 0L,
4098 buf->b_ffname != NULL ? buf->b_ffname : (char_u *)"");
Bram Moolenaarf845b872017-01-06 14:04:54 +01004099 dict_add_nr_str(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4100 : buflist_findlnum(buf), NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004101 dict_add_nr_str(dict, "loaded", buf->b_ml.ml_mfp != NULL, NULL);
4102 dict_add_nr_str(dict, "listed", buf->b_p_bl, NULL);
4103 dict_add_nr_str(dict, "changed", bufIsChanged(buf), NULL);
Bram Moolenaar95c526e2017-02-25 14:59:34 +01004104 dict_add_nr_str(dict, "changedtick", CHANGEDTICK(buf), NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004105 dict_add_nr_str(dict, "hidden",
4106 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0,
4107 NULL);
4108
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004109 /* Get a reference to buffer variables */
4110 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004111
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004112 /* List of windows displaying this buffer */
4113 windows = list_alloc();
4114 if (windows != NULL)
4115 {
4116 FOR_ALL_TAB_WINDOWS(tp, wp)
4117 if (wp->w_buffer == buf)
4118 list_append_number(windows, (varnumber_T)wp->w_id);
4119 dict_add_list(dict, "windows", windows);
4120 }
4121
4122#ifdef FEAT_SIGNS
4123 if (buf->b_signlist != NULL)
4124 {
4125 /* List of signs placed in this buffer */
4126 list_T *signs = list_alloc();
4127 if (signs != NULL)
4128 {
4129 get_buffer_signs(buf, signs);
4130 dict_add_list(dict, "signs", signs);
4131 }
4132 }
4133#endif
4134
4135 return dict;
4136}
4137
4138/*
4139 * "getbufinfo()" function
4140 */
4141 static void
4142f_getbufinfo(typval_T *argvars, typval_T *rettv)
4143{
4144 buf_T *buf = NULL;
4145 buf_T *argbuf = NULL;
4146 dict_T *d;
4147 int filtered = FALSE;
4148 int sel_buflisted = FALSE;
4149 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004150 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004151
4152 if (rettv_list_alloc(rettv) != OK)
4153 return;
4154
4155 /* List of all the buffers or selected buffers */
4156 if (argvars[0].v_type == VAR_DICT)
4157 {
4158 dict_T *sel_d = argvars[0].vval.v_dict;
4159
4160 if (sel_d != NULL)
4161 {
4162 dictitem_T *di;
4163
4164 filtered = TRUE;
4165
4166 di = dict_find(sel_d, (char_u *)"buflisted", -1);
4167 if (di != NULL && get_tv_number(&di->di_tv))
4168 sel_buflisted = TRUE;
4169
4170 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
4171 if (di != NULL && get_tv_number(&di->di_tv))
4172 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004173
4174 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
4175 if (di != NULL && get_tv_number(&di->di_tv))
4176 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004177 }
4178 }
4179 else if (argvars[0].v_type != VAR_UNKNOWN)
4180 {
4181 /* Information about one buffer. Argument specifies the buffer */
4182 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4183 ++emsg_off;
4184 argbuf = get_buf_tv(&argvars[0], FALSE);
4185 --emsg_off;
4186 if (argbuf == NULL)
4187 return;
4188 }
4189
4190 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004191 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004192 {
4193 if (argbuf != NULL && argbuf != buf)
4194 continue;
4195 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004196 || (sel_buflisted && !buf->b_p_bl)
4197 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004198 continue;
4199
4200 d = get_buffer_info(buf);
4201 if (d != NULL)
4202 list_append_dict(rettv->vval.v_list, d);
4203 if (argbuf != NULL)
4204 return;
4205 }
4206}
4207
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004208static void get_buffer_lines(buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv);
4209
4210/*
4211 * Get line or list of lines from buffer "buf" into "rettv".
4212 * Return a range (from start to end) of lines in rettv from the specified
4213 * buffer.
4214 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4215 */
4216 static void
4217get_buffer_lines(
4218 buf_T *buf,
4219 linenr_T start,
4220 linenr_T end,
4221 int retlist,
4222 typval_T *rettv)
4223{
4224 char_u *p;
4225
4226 rettv->v_type = VAR_STRING;
4227 rettv->vval.v_string = NULL;
4228 if (retlist && rettv_list_alloc(rettv) == FAIL)
4229 return;
4230
4231 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4232 return;
4233
4234 if (!retlist)
4235 {
4236 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4237 p = ml_get_buf(buf, start, FALSE);
4238 else
4239 p = (char_u *)"";
4240 rettv->vval.v_string = vim_strsave(p);
4241 }
4242 else
4243 {
4244 if (end < start)
4245 return;
4246
4247 if (start < 1)
4248 start = 1;
4249 if (end > buf->b_ml.ml_line_count)
4250 end = buf->b_ml.ml_line_count;
4251 while (start <= end)
4252 if (list_append_string(rettv->vval.v_list,
4253 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4254 break;
4255 }
4256}
4257
4258/*
4259 * Get the lnum from the first argument.
4260 * Also accepts "$", then "buf" is used.
4261 * Returns 0 on error.
4262 */
4263 static linenr_T
4264get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
4265{
4266 if (argvars[0].v_type == VAR_STRING
4267 && argvars[0].vval.v_string != NULL
4268 && argvars[0].vval.v_string[0] == '$'
4269 && buf != NULL)
4270 return buf->b_ml.ml_line_count;
4271 return (linenr_T)get_tv_number_chk(&argvars[0], NULL);
4272}
4273
4274/*
4275 * "getbufline()" function
4276 */
4277 static void
4278f_getbufline(typval_T *argvars, typval_T *rettv)
4279{
4280 linenr_T lnum;
4281 linenr_T end;
4282 buf_T *buf;
4283
4284 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4285 ++emsg_off;
4286 buf = get_buf_tv(&argvars[0], FALSE);
4287 --emsg_off;
4288
4289 lnum = get_tv_lnum_buf(&argvars[1], buf);
4290 if (argvars[2].v_type == VAR_UNKNOWN)
4291 end = lnum;
4292 else
4293 end = get_tv_lnum_buf(&argvars[2], buf);
4294
4295 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4296}
4297
4298/*
4299 * "getbufvar()" function
4300 */
4301 static void
4302f_getbufvar(typval_T *argvars, typval_T *rettv)
4303{
4304 buf_T *buf;
4305 buf_T *save_curbuf;
4306 char_u *varname;
4307 dictitem_T *v;
4308 int done = FALSE;
4309
4310 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4311 varname = get_tv_string_chk(&argvars[1]);
4312 ++emsg_off;
4313 buf = get_buf_tv(&argvars[0], FALSE);
4314
4315 rettv->v_type = VAR_STRING;
4316 rettv->vval.v_string = NULL;
4317
4318 if (buf != NULL && varname != NULL)
4319 {
4320 /* set curbuf to be our buf, temporarily */
4321 save_curbuf = curbuf;
4322 curbuf = buf;
4323
Bram Moolenaar30567352016-08-27 21:25:44 +02004324 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004325 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004326 if (varname[1] == NUL)
4327 {
4328 /* get all buffer-local options in a dict */
4329 dict_T *opts = get_winbuf_options(TRUE);
4330
4331 if (opts != NULL)
4332 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004333 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004334 done = TRUE;
4335 }
4336 }
4337 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4338 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004339 done = TRUE;
4340 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004341 else
4342 {
4343 /* Look up the variable. */
4344 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4345 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4346 'b', varname, FALSE);
4347 if (v != NULL)
4348 {
4349 copy_tv(&v->di_tv, rettv);
4350 done = TRUE;
4351 }
4352 }
4353
4354 /* restore previous notion of curbuf */
4355 curbuf = save_curbuf;
4356 }
4357
4358 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4359 /* use the default value */
4360 copy_tv(&argvars[2], rettv);
4361
4362 --emsg_off;
4363}
4364
4365/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004366 * "getchangelist()" function
4367 */
4368 static void
4369f_getchangelist(typval_T *argvars, typval_T *rettv)
4370{
4371#ifdef FEAT_JUMPLIST
4372 buf_T *buf;
4373 int i;
4374 list_T *l;
4375 dict_T *d;
4376#endif
4377
4378 if (rettv_list_alloc(rettv) != OK)
4379 return;
4380
4381#ifdef FEAT_JUMPLIST
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004382 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4383 ++emsg_off;
4384 buf = get_buf_tv(&argvars[0], FALSE);
4385 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004386 if (buf == NULL)
4387 return;
4388
4389 l = list_alloc();
4390 if (l == NULL)
4391 return;
4392
4393 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4394 return;
4395 /*
4396 * The current window change list index tracks only the position in the
4397 * current buffer change list. For other buffers, use the change list
4398 * length as the current index.
4399 */
4400 list_append_number(rettv->vval.v_list,
4401 (varnumber_T)((buf == curwin->w_buffer)
4402 ? curwin->w_changelistidx : buf->b_changelistlen));
4403
4404 for (i = 0; i < buf->b_changelistlen; ++i)
4405 {
4406 if (buf->b_changelist[i].lnum == 0)
4407 continue;
4408 if ((d = dict_alloc()) == NULL)
4409 return;
4410 if (list_append_dict(l, d) == FAIL)
4411 return;
4412 dict_add_nr_str(d, "lnum", (long)buf->b_changelist[i].lnum, NULL);
4413 dict_add_nr_str(d, "col", (long)buf->b_changelist[i].col, NULL);
4414# ifdef FEAT_VIRTUALEDIT
4415 dict_add_nr_str(d, "coladd", (long)buf->b_changelist[i].coladd, NULL);
4416# endif
4417 }
4418#endif
4419}
4420/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004421 * "getchar()" function
4422 */
4423 static void
4424f_getchar(typval_T *argvars, typval_T *rettv)
4425{
4426 varnumber_T n;
4427 int error = FALSE;
4428
4429 /* Position the cursor. Needed after a message that ends in a space. */
4430 windgoto(msg_row, msg_col);
4431
4432 ++no_mapping;
4433 ++allow_keys;
4434 for (;;)
4435 {
4436 if (argvars[0].v_type == VAR_UNKNOWN)
4437 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004438 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004439 else if (get_tv_number_chk(&argvars[0], &error) == 1)
4440 /* getchar(1): only check if char avail */
4441 n = vpeekc_any();
4442 else if (error || vpeekc_any() == NUL)
4443 /* illegal argument or getchar(0) and no char avail: return zero */
4444 n = 0;
4445 else
4446 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004447 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004448
4449 if (n == K_IGNORE)
4450 continue;
4451 break;
4452 }
4453 --no_mapping;
4454 --allow_keys;
4455
4456 set_vim_var_nr(VV_MOUSE_WIN, 0);
4457 set_vim_var_nr(VV_MOUSE_WINID, 0);
4458 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4459 set_vim_var_nr(VV_MOUSE_COL, 0);
4460
4461 rettv->vval.v_number = n;
4462 if (IS_SPECIAL(n) || mod_mask != 0)
4463 {
4464 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4465 int i = 0;
4466
4467 /* Turn a special key into three bytes, plus modifier. */
4468 if (mod_mask != 0)
4469 {
4470 temp[i++] = K_SPECIAL;
4471 temp[i++] = KS_MODIFIER;
4472 temp[i++] = mod_mask;
4473 }
4474 if (IS_SPECIAL(n))
4475 {
4476 temp[i++] = K_SPECIAL;
4477 temp[i++] = K_SECOND(n);
4478 temp[i++] = K_THIRD(n);
4479 }
4480#ifdef FEAT_MBYTE
4481 else if (has_mbyte)
4482 i += (*mb_char2bytes)(n, temp + i);
4483#endif
4484 else
4485 temp[i++] = n;
4486 temp[i++] = NUL;
4487 rettv->v_type = VAR_STRING;
4488 rettv->vval.v_string = vim_strsave(temp);
4489
4490#ifdef FEAT_MOUSE
4491 if (is_mouse_key(n))
4492 {
4493 int row = mouse_row;
4494 int col = mouse_col;
4495 win_T *win;
4496 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004497 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004498 int winnr = 1;
4499
4500 if (row >= 0 && col >= 0)
4501 {
4502 /* Find the window at the mouse coordinates and compute the
4503 * text position. */
4504 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004505 if (win == NULL)
4506 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004507 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004508 for (wp = firstwin; wp != win; wp = wp->w_next)
4509 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004510 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4511 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4512 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4513 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4514 }
4515 }
4516#endif
4517 }
4518}
4519
4520/*
4521 * "getcharmod()" function
4522 */
4523 static void
4524f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4525{
4526 rettv->vval.v_number = mod_mask;
4527}
4528
4529/*
4530 * "getcharsearch()" function
4531 */
4532 static void
4533f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4534{
4535 if (rettv_dict_alloc(rettv) != FAIL)
4536 {
4537 dict_T *dict = rettv->vval.v_dict;
4538
4539 dict_add_nr_str(dict, "char", 0L, last_csearch());
4540 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
4541 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
4542 }
4543}
4544
4545/*
4546 * "getcmdline()" function
4547 */
4548 static void
4549f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4550{
4551 rettv->v_type = VAR_STRING;
4552 rettv->vval.v_string = get_cmdline_str();
4553}
4554
4555/*
4556 * "getcmdpos()" function
4557 */
4558 static void
4559f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4560{
4561 rettv->vval.v_number = get_cmdline_pos() + 1;
4562}
4563
4564/*
4565 * "getcmdtype()" function
4566 */
4567 static void
4568f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4569{
4570 rettv->v_type = VAR_STRING;
4571 rettv->vval.v_string = alloc(2);
4572 if (rettv->vval.v_string != NULL)
4573 {
4574 rettv->vval.v_string[0] = get_cmdline_type();
4575 rettv->vval.v_string[1] = NUL;
4576 }
4577}
4578
4579/*
4580 * "getcmdwintype()" function
4581 */
4582 static void
4583f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4584{
4585 rettv->v_type = VAR_STRING;
4586 rettv->vval.v_string = NULL;
4587#ifdef FEAT_CMDWIN
4588 rettv->vval.v_string = alloc(2);
4589 if (rettv->vval.v_string != NULL)
4590 {
4591 rettv->vval.v_string[0] = cmdwin_type;
4592 rettv->vval.v_string[1] = NUL;
4593 }
4594#endif
4595}
4596
4597#if defined(FEAT_CMDL_COMPL)
4598/*
4599 * "getcompletion()" function
4600 */
4601 static void
4602f_getcompletion(typval_T *argvars, typval_T *rettv)
4603{
4604 char_u *pat;
4605 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004606 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004607 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
4608 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004609
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004610 if (argvars[2].v_type != VAR_UNKNOWN)
4611 filtered = get_tv_number_chk(&argvars[2], NULL);
4612
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004613 if (p_wic)
4614 options |= WILD_ICASE;
4615
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004616 /* For filtered results, 'wildignore' is used */
4617 if (!filtered)
4618 options |= WILD_KEEP_ALL;
4619
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004620 ExpandInit(&xpc);
4621 xpc.xp_pattern = get_tv_string(&argvars[0]);
4622 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4623 xpc.xp_context = cmdcomplete_str_to_type(get_tv_string(&argvars[1]));
4624 if (xpc.xp_context == EXPAND_NOTHING)
4625 {
4626 if (argvars[1].v_type == VAR_STRING)
4627 EMSG2(_(e_invarg2), argvars[1].vval.v_string);
4628 else
4629 EMSG(_(e_invarg));
4630 return;
4631 }
4632
4633# if defined(FEAT_MENU)
4634 if (xpc.xp_context == EXPAND_MENUS)
4635 {
4636 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4637 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4638 }
4639# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02004640#ifdef FEAT_CSCOPE
4641 if (xpc.xp_context == EXPAND_CSCOPE)
4642 {
4643 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4644 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4645 }
4646#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02004647#ifdef FEAT_SIGNS
4648 if (xpc.xp_context == EXPAND_SIGN)
4649 {
4650 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4651 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4652 }
4653#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004654
4655 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4656 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
4657 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004658 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004659
4660 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4661
4662 for (i = 0; i < xpc.xp_numfiles; i++)
4663 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4664 }
4665 vim_free(pat);
4666 ExpandCleanup(&xpc);
4667}
4668#endif
4669
4670/*
4671 * "getcwd()" function
4672 */
4673 static void
4674f_getcwd(typval_T *argvars, typval_T *rettv)
4675{
4676 win_T *wp = NULL;
4677 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01004678 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004679
4680 rettv->v_type = VAR_STRING;
4681 rettv->vval.v_string = NULL;
4682
Bram Moolenaar54591292018-02-09 20:53:59 +01004683 if (argvars[0].v_type == VAR_NUMBER && argvars[0].vval.v_number == -1)
4684 global = TRUE;
4685 else
4686 wp = find_tabwin(&argvars[0], &argvars[1]);
4687
4688 if (wp != NULL && wp->w_localdir != NULL)
4689 rettv->vval.v_string = vim_strsave(wp->w_localdir);
4690 else if (wp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004691 {
Bram Moolenaar54591292018-02-09 20:53:59 +01004692 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004693 rettv->vval.v_string = vim_strsave(globaldir);
4694 else
4695 {
4696 cwd = alloc(MAXPATHL);
4697 if (cwd != NULL)
4698 {
4699 if (mch_dirname(cwd, MAXPATHL) != FAIL)
4700 rettv->vval.v_string = vim_strsave(cwd);
4701 vim_free(cwd);
4702 }
4703 }
4704#ifdef BACKSLASH_IN_FILENAME
4705 if (rettv->vval.v_string != NULL)
4706 slash_adjust(rettv->vval.v_string);
4707#endif
4708 }
4709}
4710
4711/*
4712 * "getfontname()" function
4713 */
4714 static void
4715f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4716{
4717 rettv->v_type = VAR_STRING;
4718 rettv->vval.v_string = NULL;
4719#ifdef FEAT_GUI
4720 if (gui.in_use)
4721 {
4722 GuiFont font;
4723 char_u *name = NULL;
4724
4725 if (argvars[0].v_type == VAR_UNKNOWN)
4726 {
4727 /* Get the "Normal" font. Either the name saved by
4728 * hl_set_font_name() or from the font ID. */
4729 font = gui.norm_font;
4730 name = hl_get_font_name();
4731 }
4732 else
4733 {
4734 name = get_tv_string(&argvars[0]);
4735 if (STRCMP(name, "*") == 0) /* don't use font dialog */
4736 return;
4737 font = gui_mch_get_font(name, FALSE);
4738 if (font == NOFONT)
4739 return; /* Invalid font name, return empty string. */
4740 }
4741 rettv->vval.v_string = gui_mch_get_fontname(font, name);
4742 if (argvars[0].v_type != VAR_UNKNOWN)
4743 gui_mch_free_font(font);
4744 }
4745#endif
4746}
4747
4748/*
4749 * "getfperm({fname})" function
4750 */
4751 static void
4752f_getfperm(typval_T *argvars, typval_T *rettv)
4753{
4754 char_u *fname;
4755 stat_T st;
4756 char_u *perm = NULL;
4757 char_u flags[] = "rwx";
4758 int i;
4759
4760 fname = get_tv_string(&argvars[0]);
4761
4762 rettv->v_type = VAR_STRING;
4763 if (mch_stat((char *)fname, &st) >= 0)
4764 {
4765 perm = vim_strsave((char_u *)"---------");
4766 if (perm != NULL)
4767 {
4768 for (i = 0; i < 9; i++)
4769 {
4770 if (st.st_mode & (1 << (8 - i)))
4771 perm[i] = flags[i % 3];
4772 }
4773 }
4774 }
4775 rettv->vval.v_string = perm;
4776}
4777
4778/*
4779 * "getfsize({fname})" function
4780 */
4781 static void
4782f_getfsize(typval_T *argvars, typval_T *rettv)
4783{
4784 char_u *fname;
4785 stat_T st;
4786
4787 fname = get_tv_string(&argvars[0]);
4788
4789 rettv->v_type = VAR_NUMBER;
4790
4791 if (mch_stat((char *)fname, &st) >= 0)
4792 {
4793 if (mch_isdir(fname))
4794 rettv->vval.v_number = 0;
4795 else
4796 {
4797 rettv->vval.v_number = (varnumber_T)st.st_size;
4798
4799 /* non-perfect check for overflow */
4800 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
4801 rettv->vval.v_number = -2;
4802 }
4803 }
4804 else
4805 rettv->vval.v_number = -1;
4806}
4807
4808/*
4809 * "getftime({fname})" function
4810 */
4811 static void
4812f_getftime(typval_T *argvars, typval_T *rettv)
4813{
4814 char_u *fname;
4815 stat_T st;
4816
4817 fname = get_tv_string(&argvars[0]);
4818
4819 if (mch_stat((char *)fname, &st) >= 0)
4820 rettv->vval.v_number = (varnumber_T)st.st_mtime;
4821 else
4822 rettv->vval.v_number = -1;
4823}
4824
4825/*
4826 * "getftype({fname})" function
4827 */
4828 static void
4829f_getftype(typval_T *argvars, typval_T *rettv)
4830{
4831 char_u *fname;
4832 stat_T st;
4833 char_u *type = NULL;
4834 char *t;
4835
4836 fname = get_tv_string(&argvars[0]);
4837
4838 rettv->v_type = VAR_STRING;
4839 if (mch_lstat((char *)fname, &st) >= 0)
4840 {
4841#ifdef S_ISREG
4842 if (S_ISREG(st.st_mode))
4843 t = "file";
4844 else if (S_ISDIR(st.st_mode))
4845 t = "dir";
4846# ifdef S_ISLNK
4847 else if (S_ISLNK(st.st_mode))
4848 t = "link";
4849# endif
4850# ifdef S_ISBLK
4851 else if (S_ISBLK(st.st_mode))
4852 t = "bdev";
4853# endif
4854# ifdef S_ISCHR
4855 else if (S_ISCHR(st.st_mode))
4856 t = "cdev";
4857# endif
4858# ifdef S_ISFIFO
4859 else if (S_ISFIFO(st.st_mode))
4860 t = "fifo";
4861# endif
4862# ifdef S_ISSOCK
4863 else if (S_ISSOCK(st.st_mode))
4864 t = "fifo";
4865# endif
4866 else
4867 t = "other";
4868#else
4869# ifdef S_IFMT
4870 switch (st.st_mode & S_IFMT)
4871 {
4872 case S_IFREG: t = "file"; break;
4873 case S_IFDIR: t = "dir"; break;
4874# ifdef S_IFLNK
4875 case S_IFLNK: t = "link"; break;
4876# endif
4877# ifdef S_IFBLK
4878 case S_IFBLK: t = "bdev"; break;
4879# endif
4880# ifdef S_IFCHR
4881 case S_IFCHR: t = "cdev"; break;
4882# endif
4883# ifdef S_IFIFO
4884 case S_IFIFO: t = "fifo"; break;
4885# endif
4886# ifdef S_IFSOCK
4887 case S_IFSOCK: t = "socket"; break;
4888# endif
4889 default: t = "other";
4890 }
4891# else
4892 if (mch_isdir(fname))
4893 t = "dir";
4894 else
4895 t = "file";
4896# endif
4897#endif
4898 type = vim_strsave((char_u *)t);
4899 }
4900 rettv->vval.v_string = type;
4901}
4902
4903/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01004904 * "getjumplist()" function
4905 */
4906 static void
4907f_getjumplist(typval_T *argvars, typval_T *rettv)
4908{
4909#ifdef FEAT_JUMPLIST
4910 win_T *wp;
4911 int i;
4912 list_T *l;
4913 dict_T *d;
4914#endif
4915
4916 if (rettv_list_alloc(rettv) != OK)
4917 return;
4918
4919#ifdef FEAT_JUMPLIST
4920 wp = find_tabwin(&argvars[0], &argvars[1]);
4921 if (wp == NULL)
4922 return;
4923
4924 l = list_alloc();
4925 if (l == NULL)
4926 return;
4927
4928 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4929 return;
4930 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
4931
Bram Moolenaar48679742018-02-13 13:33:29 +01004932 cleanup_jumplist(wp, TRUE);
4933
Bram Moolenaar4f505882018-02-10 21:06:32 +01004934 for (i = 0; i < wp->w_jumplistlen; ++i)
4935 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004936 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
4937 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01004938 if ((d = dict_alloc()) == NULL)
4939 return;
4940 if (list_append_dict(l, d) == FAIL)
4941 return;
4942 dict_add_nr_str(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum,
4943 NULL);
4944 dict_add_nr_str(d, "col", (long)wp->w_jumplist[i].fmark.mark.col,
4945 NULL);
4946# ifdef FEAT_VIRTUALEDIT
4947 dict_add_nr_str(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd,
4948 NULL);
4949# endif
4950 dict_add_nr_str(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum, NULL);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004951 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaar4f505882018-02-10 21:06:32 +01004952 dict_add_nr_str(d, "filename", 0L, wp->w_jumplist[i].fname);
4953 }
4954#endif
4955}
4956
4957/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004958 * "getline(lnum, [end])" function
4959 */
4960 static void
4961f_getline(typval_T *argvars, typval_T *rettv)
4962{
4963 linenr_T lnum;
4964 linenr_T end;
4965 int retlist;
4966
4967 lnum = get_tv_lnum(argvars);
4968 if (argvars[1].v_type == VAR_UNKNOWN)
4969 {
4970 end = 0;
4971 retlist = FALSE;
4972 }
4973 else
4974 {
4975 end = get_tv_lnum(&argvars[1]);
4976 retlist = TRUE;
4977 }
4978
4979 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
4980}
4981
Bram Moolenaar4ae20952016-08-13 15:29:14 +02004982#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02004983 static void
4984get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
4985{
Bram Moolenaard823fa92016-08-12 16:29:27 +02004986 if (what_arg->v_type == VAR_UNKNOWN)
4987 {
4988 if (rettv_list_alloc(rettv) == OK)
4989 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004990 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004991 }
4992 else
4993 {
4994 if (rettv_dict_alloc(rettv) == OK)
4995 if (is_qf || (wp != NULL))
4996 {
4997 if (what_arg->v_type == VAR_DICT)
4998 {
4999 dict_T *d = what_arg->vval.v_dict;
5000
5001 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005002 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005003 }
5004 else
5005 EMSG(_(e_dictreq));
5006 }
5007 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005008}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005009#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005010
5011/*
5012 * "getloclist()" function
5013 */
5014 static void
5015f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5016{
5017#ifdef FEAT_QUICKFIX
5018 win_T *wp;
5019
5020 wp = find_win_by_nr(&argvars[0], NULL);
5021 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5022#endif
5023}
5024
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005025/*
5026 * "getmatches()" function
5027 */
5028 static void
5029f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5030{
5031#ifdef FEAT_SEARCH_EXTRA
5032 dict_T *dict;
5033 matchitem_T *cur = curwin->w_match_head;
5034 int i;
5035
5036 if (rettv_list_alloc(rettv) == OK)
5037 {
5038 while (cur != NULL)
5039 {
5040 dict = dict_alloc();
5041 if (dict == NULL)
5042 return;
5043 if (cur->match.regprog == NULL)
5044 {
5045 /* match added with matchaddpos() */
5046 for (i = 0; i < MAXPOSMATCH; ++i)
5047 {
5048 llpos_T *llpos;
5049 char buf[6];
5050 list_T *l;
5051
5052 llpos = &cur->pos.pos[i];
5053 if (llpos->lnum == 0)
5054 break;
5055 l = list_alloc();
5056 if (l == NULL)
5057 break;
5058 list_append_number(l, (varnumber_T)llpos->lnum);
5059 if (llpos->col > 0)
5060 {
5061 list_append_number(l, (varnumber_T)llpos->col);
5062 list_append_number(l, (varnumber_T)llpos->len);
5063 }
5064 sprintf(buf, "pos%d", i + 1);
5065 dict_add_list(dict, buf, l);
5066 }
5067 }
5068 else
5069 {
5070 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
5071 }
5072 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
5073 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
5074 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
5075# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
5076 if (cur->conceal_char)
5077 {
5078 char_u buf[MB_MAXBYTES + 1];
5079
5080 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
5081 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
5082 }
5083# endif
5084 list_append_dict(rettv->vval.v_list, dict);
5085 cur = cur->next;
5086 }
5087 }
5088#endif
5089}
5090
5091/*
5092 * "getpid()" function
5093 */
5094 static void
5095f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5096{
5097 rettv->vval.v_number = mch_get_pid();
5098}
5099
5100 static void
5101getpos_both(
5102 typval_T *argvars,
5103 typval_T *rettv,
5104 int getcurpos)
5105{
5106 pos_T *fp;
5107 list_T *l;
5108 int fnum = -1;
5109
5110 if (rettv_list_alloc(rettv) == OK)
5111 {
5112 l = rettv->vval.v_list;
5113 if (getcurpos)
5114 fp = &curwin->w_cursor;
5115 else
5116 fp = var2fpos(&argvars[0], TRUE, &fnum);
5117 if (fnum != -1)
5118 list_append_number(l, (varnumber_T)fnum);
5119 else
5120 list_append_number(l, (varnumber_T)0);
5121 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5122 : (varnumber_T)0);
5123 list_append_number(l, (fp != NULL)
5124 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5125 : (varnumber_T)0);
5126 list_append_number(l,
5127#ifdef FEAT_VIRTUALEDIT
5128 (fp != NULL) ? (varnumber_T)fp->coladd :
5129#endif
5130 (varnumber_T)0);
5131 if (getcurpos)
5132 {
5133 update_curswant();
5134 list_append_number(l, curwin->w_curswant == MAXCOL ?
5135 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
5136 }
5137 }
5138 else
5139 rettv->vval.v_number = FALSE;
5140}
5141
5142
5143/*
5144 * "getcurpos()" function
5145 */
5146 static void
5147f_getcurpos(typval_T *argvars, typval_T *rettv)
5148{
5149 getpos_both(argvars, rettv, TRUE);
5150}
5151
5152/*
5153 * "getpos(string)" function
5154 */
5155 static void
5156f_getpos(typval_T *argvars, typval_T *rettv)
5157{
5158 getpos_both(argvars, rettv, FALSE);
5159}
5160
5161/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005162 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005163 */
5164 static void
5165f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5166{
5167#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005168 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005169#endif
5170}
5171
5172/*
5173 * "getreg()" function
5174 */
5175 static void
5176f_getreg(typval_T *argvars, typval_T *rettv)
5177{
5178 char_u *strregname;
5179 int regname;
5180 int arg2 = FALSE;
5181 int return_list = FALSE;
5182 int error = FALSE;
5183
5184 if (argvars[0].v_type != VAR_UNKNOWN)
5185 {
5186 strregname = get_tv_string_chk(&argvars[0]);
5187 error = strregname == NULL;
5188 if (argvars[1].v_type != VAR_UNKNOWN)
5189 {
5190 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
5191 if (!error && argvars[2].v_type != VAR_UNKNOWN)
5192 return_list = (int)get_tv_number_chk(&argvars[2], &error);
5193 }
5194 }
5195 else
5196 strregname = get_vim_var_str(VV_REG);
5197
5198 if (error)
5199 return;
5200
5201 regname = (strregname == NULL ? '"' : *strregname);
5202 if (regname == 0)
5203 regname = '"';
5204
5205 if (return_list)
5206 {
5207 rettv->v_type = VAR_LIST;
5208 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5209 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5210 if (rettv->vval.v_list == NULL)
5211 (void)rettv_list_alloc(rettv);
5212 else
5213 ++rettv->vval.v_list->lv_refcount;
5214 }
5215 else
5216 {
5217 rettv->v_type = VAR_STRING;
5218 rettv->vval.v_string = get_reg_contents(regname,
5219 arg2 ? GREG_EXPR_SRC : 0);
5220 }
5221}
5222
5223/*
5224 * "getregtype()" function
5225 */
5226 static void
5227f_getregtype(typval_T *argvars, typval_T *rettv)
5228{
5229 char_u *strregname;
5230 int regname;
5231 char_u buf[NUMBUFLEN + 2];
5232 long reglen = 0;
5233
5234 if (argvars[0].v_type != VAR_UNKNOWN)
5235 {
5236 strregname = get_tv_string_chk(&argvars[0]);
5237 if (strregname == NULL) /* type error; errmsg already given */
5238 {
5239 rettv->v_type = VAR_STRING;
5240 rettv->vval.v_string = NULL;
5241 return;
5242 }
5243 }
5244 else
5245 /* Default to v:register */
5246 strregname = get_vim_var_str(VV_REG);
5247
5248 regname = (strregname == NULL ? '"' : *strregname);
5249 if (regname == 0)
5250 regname = '"';
5251
5252 buf[0] = NUL;
5253 buf[1] = NUL;
5254 switch (get_reg_type(regname, &reglen))
5255 {
5256 case MLINE: buf[0] = 'V'; break;
5257 case MCHAR: buf[0] = 'v'; break;
5258 case MBLOCK:
5259 buf[0] = Ctrl_V;
5260 sprintf((char *)buf + 1, "%ld", reglen + 1);
5261 break;
5262 }
5263 rettv->v_type = VAR_STRING;
5264 rettv->vval.v_string = vim_strsave(buf);
5265}
5266
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005267/*
5268 * Returns information (variables, options, etc.) about a tab page
5269 * as a dictionary.
5270 */
5271 static dict_T *
5272get_tabpage_info(tabpage_T *tp, int tp_idx)
5273{
5274 win_T *wp;
5275 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005276 list_T *l;
5277
5278 dict = dict_alloc();
5279 if (dict == NULL)
5280 return NULL;
5281
Bram Moolenaar33928832016-08-18 21:22:04 +02005282 dict_add_nr_str(dict, "tabnr", tp_idx, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005283
5284 l = list_alloc();
5285 if (l != NULL)
5286 {
5287 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5288 wp; wp = wp->w_next)
5289 list_append_number(l, (varnumber_T)wp->w_id);
5290 dict_add_list(dict, "windows", l);
5291 }
5292
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005293 /* Make a reference to tabpage variables */
5294 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005295
5296 return dict;
5297}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005298
5299/*
5300 * "gettabinfo()" function
5301 */
5302 static void
5303f_gettabinfo(typval_T *argvars, typval_T *rettv)
5304{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005305 tabpage_T *tp, *tparg = NULL;
5306 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005307 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005308
5309 if (rettv_list_alloc(rettv) != OK)
5310 return;
5311
5312 if (argvars[0].v_type != VAR_UNKNOWN)
5313 {
5314 /* Information about one tab page */
5315 tparg = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5316 if (tparg == NULL)
5317 return;
5318 }
5319
5320 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005321 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005322 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005323 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005324 if (tparg != NULL && tp != tparg)
5325 continue;
5326 d = get_tabpage_info(tp, tpnr);
5327 if (d != NULL)
5328 list_append_dict(rettv->vval.v_list, d);
5329 if (tparg != NULL)
5330 return;
5331 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005332}
5333
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005334/*
5335 * "gettabvar()" function
5336 */
5337 static void
5338f_gettabvar(typval_T *argvars, typval_T *rettv)
5339{
5340 win_T *oldcurwin;
5341 tabpage_T *tp, *oldtabpage;
5342 dictitem_T *v;
5343 char_u *varname;
5344 int done = FALSE;
5345
5346 rettv->v_type = VAR_STRING;
5347 rettv->vval.v_string = NULL;
5348
5349 varname = get_tv_string_chk(&argvars[1]);
5350 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5351 if (tp != NULL && varname != NULL)
5352 {
5353 /* Set tp to be our tabpage, temporarily. Also set the window to the
5354 * first window in the tabpage, otherwise the window is not valid. */
5355 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005356 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5357 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005358 {
5359 /* look up the variable */
5360 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5361 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5362 if (v != NULL)
5363 {
5364 copy_tv(&v->di_tv, rettv);
5365 done = TRUE;
5366 }
5367 }
5368
5369 /* restore previous notion of curwin */
5370 restore_win(oldcurwin, oldtabpage, TRUE);
5371 }
5372
5373 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5374 copy_tv(&argvars[2], rettv);
5375}
5376
5377/*
5378 * "gettabwinvar()" function
5379 */
5380 static void
5381f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5382{
5383 getwinvar(argvars, rettv, 1);
5384}
5385
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005386/*
5387 * Returns information about a window as a dictionary.
5388 */
5389 static dict_T *
5390get_win_info(win_T *wp, short tpnr, short winnr)
5391{
5392 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005393
5394 dict = dict_alloc();
5395 if (dict == NULL)
5396 return NULL;
5397
Bram Moolenaar33928832016-08-18 21:22:04 +02005398 dict_add_nr_str(dict, "tabnr", tpnr, NULL);
5399 dict_add_nr_str(dict, "winnr", winnr, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005400 dict_add_nr_str(dict, "winid", wp->w_id, NULL);
5401 dict_add_nr_str(dict, "height", wp->w_height, NULL);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005402#ifdef FEAT_MENU
5403 dict_add_nr_str(dict, "winbar", wp->w_winbar_height, NULL);
5404#endif
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005405 dict_add_nr_str(dict, "width", wp->w_width, NULL);
Bram Moolenaar33928832016-08-18 21:22:04 +02005406 dict_add_nr_str(dict, "bufnr", wp->w_buffer->b_fnum, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005407
Bram Moolenaar69905d12017-08-13 18:14:47 +02005408#ifdef FEAT_TERMINAL
5409 dict_add_nr_str(dict, "terminal", bt_terminal(wp->w_buffer), NULL);
5410#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005411#ifdef FEAT_QUICKFIX
5412 dict_add_nr_str(dict, "quickfix", bt_quickfix(wp->w_buffer), NULL);
5413 dict_add_nr_str(dict, "loclist",
5414 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL), NULL);
5415#endif
5416
Bram Moolenaar30567352016-08-27 21:25:44 +02005417 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005418 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005419
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005420 return dict;
5421}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005422
5423/*
5424 * "getwininfo()" function
5425 */
5426 static void
5427f_getwininfo(typval_T *argvars, typval_T *rettv)
5428{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005429 tabpage_T *tp;
5430 win_T *wp = NULL, *wparg = NULL;
5431 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005432 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005433
5434 if (rettv_list_alloc(rettv) != OK)
5435 return;
5436
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005437 if (argvars[0].v_type != VAR_UNKNOWN)
5438 {
5439 wparg = win_id2wp(argvars);
5440 if (wparg == NULL)
5441 return;
5442 }
5443
5444 /* Collect information about either all the windows across all the tab
5445 * pages or one particular window.
5446 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005447 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005448 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005449 tabnr++;
5450 winnr = 0;
5451 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005452 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005453 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005454 if (wparg != NULL && wp != wparg)
5455 continue;
5456 d = get_win_info(wp, tabnr, winnr);
5457 if (d != NULL)
5458 list_append_dict(rettv->vval.v_list, d);
5459 if (wparg != NULL)
5460 /* found information about a specific window */
5461 return;
5462 }
5463 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005464}
5465
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005466/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005467 * "win_findbuf()" function
5468 */
5469 static void
5470f_win_findbuf(typval_T *argvars, typval_T *rettv)
5471{
5472 if (rettv_list_alloc(rettv) != FAIL)
5473 win_findbuf(argvars, rettv->vval.v_list);
5474}
5475
5476/*
5477 * "win_getid()" function
5478 */
5479 static void
5480f_win_getid(typval_T *argvars, typval_T *rettv)
5481{
5482 rettv->vval.v_number = win_getid(argvars);
5483}
5484
5485/*
5486 * "win_gotoid()" function
5487 */
5488 static void
5489f_win_gotoid(typval_T *argvars, typval_T *rettv)
5490{
5491 rettv->vval.v_number = win_gotoid(argvars);
5492}
5493
5494/*
5495 * "win_id2tabwin()" function
5496 */
5497 static void
5498f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5499{
5500 if (rettv_list_alloc(rettv) != FAIL)
5501 win_id2tabwin(argvars, rettv->vval.v_list);
5502}
5503
5504/*
5505 * "win_id2win()" function
5506 */
5507 static void
5508f_win_id2win(typval_T *argvars, typval_T *rettv)
5509{
5510 rettv->vval.v_number = win_id2win(argvars);
5511}
5512
5513/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005514 * "win_screenpos()" function
5515 */
5516 static void
5517f_win_screenpos(typval_T *argvars, typval_T *rettv)
5518{
5519 win_T *wp;
5520
5521 if (rettv_list_alloc(rettv) == FAIL)
5522 return;
5523
5524 wp = find_win_by_nr(&argvars[0], NULL);
5525 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5526 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5527}
5528
5529/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005530 * "getwinpos({timeout})" function
5531 */
5532 static void
5533f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
5534{
5535 int x = -1;
5536 int y = -1;
5537
5538 if (rettv_list_alloc(rettv) == FAIL)
5539 return;
5540#ifdef FEAT_GUI
5541 if (gui.in_use)
5542 gui_mch_get_winpos(&x, &y);
5543# if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5544 else
5545# endif
5546#endif
5547#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5548 {
5549 varnumber_T timeout = 100;
5550
5551 if (argvars[0].v_type != VAR_UNKNOWN)
5552 timeout = get_tv_number(&argvars[0]);
5553 term_get_winpos(&x, &y, timeout);
5554 }
5555#endif
5556 list_append_number(rettv->vval.v_list, (varnumber_T)x);
5557 list_append_number(rettv->vval.v_list, (varnumber_T)y);
5558}
5559
5560
5561/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005562 * "getwinposx()" function
5563 */
5564 static void
5565f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5566{
5567 rettv->vval.v_number = -1;
5568#ifdef FEAT_GUI
5569 if (gui.in_use)
5570 {
5571 int x, y;
5572
5573 if (gui_mch_get_winpos(&x, &y) == OK)
5574 rettv->vval.v_number = x;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005575 return;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005576 }
5577#endif
5578#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5579 {
5580 int x, y;
5581
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005582 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005583 rettv->vval.v_number = x;
5584 }
5585#endif
5586}
5587
5588/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005589 * "getwinposy()" function
5590 */
5591 static void
5592f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5593{
5594 rettv->vval.v_number = -1;
5595#ifdef FEAT_GUI
5596 if (gui.in_use)
5597 {
5598 int x, y;
5599
5600 if (gui_mch_get_winpos(&x, &y) == OK)
5601 rettv->vval.v_number = y;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005602 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005603 }
5604#endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005605#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5606 {
5607 int x, y;
5608
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005609 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005610 rettv->vval.v_number = y;
5611 }
5612#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005613}
5614
5615/*
5616 * "getwinvar()" function
5617 */
5618 static void
5619f_getwinvar(typval_T *argvars, typval_T *rettv)
5620{
5621 getwinvar(argvars, rettv, 0);
5622}
5623
5624/*
5625 * "glob()" function
5626 */
5627 static void
5628f_glob(typval_T *argvars, typval_T *rettv)
5629{
5630 int options = WILD_SILENT|WILD_USE_NL;
5631 expand_T xpc;
5632 int error = FALSE;
5633
5634 /* When the optional second argument is non-zero, don't remove matches
5635 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5636 rettv->v_type = VAR_STRING;
5637 if (argvars[1].v_type != VAR_UNKNOWN)
5638 {
5639 if (get_tv_number_chk(&argvars[1], &error))
5640 options |= WILD_KEEP_ALL;
5641 if (argvars[2].v_type != VAR_UNKNOWN)
5642 {
5643 if (get_tv_number_chk(&argvars[2], &error))
5644 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005645 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005646 }
5647 if (argvars[3].v_type != VAR_UNKNOWN
5648 && get_tv_number_chk(&argvars[3], &error))
5649 options |= WILD_ALLLINKS;
5650 }
5651 }
5652 if (!error)
5653 {
5654 ExpandInit(&xpc);
5655 xpc.xp_context = EXPAND_FILES;
5656 if (p_wic)
5657 options += WILD_ICASE;
5658 if (rettv->v_type == VAR_STRING)
5659 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
5660 NULL, options, WILD_ALL);
5661 else if (rettv_list_alloc(rettv) != FAIL)
5662 {
5663 int i;
5664
5665 ExpandOne(&xpc, get_tv_string(&argvars[0]),
5666 NULL, options, WILD_ALL_KEEP);
5667 for (i = 0; i < xpc.xp_numfiles; i++)
5668 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5669
5670 ExpandCleanup(&xpc);
5671 }
5672 }
5673 else
5674 rettv->vval.v_string = NULL;
5675}
5676
5677/*
5678 * "globpath()" function
5679 */
5680 static void
5681f_globpath(typval_T *argvars, typval_T *rettv)
5682{
5683 int flags = 0;
5684 char_u buf1[NUMBUFLEN];
5685 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
5686 int error = FALSE;
5687 garray_T ga;
5688 int i;
5689
5690 /* When the optional second argument is non-zero, don't remove matches
5691 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5692 rettv->v_type = VAR_STRING;
5693 if (argvars[2].v_type != VAR_UNKNOWN)
5694 {
5695 if (get_tv_number_chk(&argvars[2], &error))
5696 flags |= WILD_KEEP_ALL;
5697 if (argvars[3].v_type != VAR_UNKNOWN)
5698 {
5699 if (get_tv_number_chk(&argvars[3], &error))
5700 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005701 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005702 }
5703 if (argvars[4].v_type != VAR_UNKNOWN
5704 && get_tv_number_chk(&argvars[4], &error))
5705 flags |= WILD_ALLLINKS;
5706 }
5707 }
5708 if (file != NULL && !error)
5709 {
5710 ga_init2(&ga, (int)sizeof(char_u *), 10);
5711 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
5712 if (rettv->v_type == VAR_STRING)
5713 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
5714 else if (rettv_list_alloc(rettv) != FAIL)
5715 for (i = 0; i < ga.ga_len; ++i)
5716 list_append_string(rettv->vval.v_list,
5717 ((char_u **)(ga.ga_data))[i], -1);
5718 ga_clear_strings(&ga);
5719 }
5720 else
5721 rettv->vval.v_string = NULL;
5722}
5723
5724/*
5725 * "glob2regpat()" function
5726 */
5727 static void
5728f_glob2regpat(typval_T *argvars, typval_T *rettv)
5729{
5730 char_u *pat = get_tv_string_chk(&argvars[0]);
5731
5732 rettv->v_type = VAR_STRING;
5733 rettv->vval.v_string = (pat == NULL)
5734 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
5735}
5736
5737/* for VIM_VERSION_ defines */
5738#include "version.h"
5739
5740/*
5741 * "has()" function
5742 */
5743 static void
5744f_has(typval_T *argvars, typval_T *rettv)
5745{
5746 int i;
5747 char_u *name;
5748 int n = FALSE;
5749 static char *(has_list[]) =
5750 {
5751#ifdef AMIGA
5752 "amiga",
5753# ifdef FEAT_ARP
5754 "arp",
5755# endif
5756#endif
5757#ifdef __BEOS__
5758 "beos",
5759#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02005760#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01005761 "mac", /* Mac OS X (and, once, Mac OS Classic) */
5762 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02005763# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01005764 "macunix", /* Mac OS X, with the darwin feature */
5765 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02005766# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005767#endif
5768#ifdef __QNX__
5769 "qnx",
5770#endif
5771#ifdef UNIX
5772 "unix",
5773#endif
5774#ifdef VMS
5775 "vms",
5776#endif
5777#ifdef WIN32
5778 "win32",
5779#endif
5780#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
5781 "win32unix",
5782#endif
5783#if defined(WIN64) || defined(_WIN64)
5784 "win64",
5785#endif
5786#ifdef EBCDIC
5787 "ebcdic",
5788#endif
5789#ifndef CASE_INSENSITIVE_FILENAME
5790 "fname_case",
5791#endif
5792#ifdef HAVE_ACL
5793 "acl",
5794#endif
5795#ifdef FEAT_ARABIC
5796 "arabic",
5797#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005798 "autocmd",
Bram Moolenaare42a6d22017-11-12 19:21:51 +01005799#ifdef FEAT_AUTOSERVERNAME
5800 "autoservername",
5801#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01005802#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005803 "balloon_eval",
5804# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
5805 "balloon_multiline",
5806# endif
5807#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01005808#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01005809 "balloon_eval_term",
5810#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005811#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
5812 "builtin_terms",
5813# ifdef ALL_BUILTIN_TCAPS
5814 "all_builtin_terms",
5815# endif
5816#endif
5817#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
5818 || defined(FEAT_GUI_W32) \
5819 || defined(FEAT_GUI_MOTIF))
5820 "browsefilter",
5821#endif
5822#ifdef FEAT_BYTEOFF
5823 "byte_offset",
5824#endif
5825#ifdef FEAT_JOB_CHANNEL
5826 "channel",
5827#endif
5828#ifdef FEAT_CINDENT
5829 "cindent",
5830#endif
5831#ifdef FEAT_CLIENTSERVER
5832 "clientserver",
5833#endif
5834#ifdef FEAT_CLIPBOARD
5835 "clipboard",
5836#endif
5837#ifdef FEAT_CMDL_COMPL
5838 "cmdline_compl",
5839#endif
5840#ifdef FEAT_CMDHIST
5841 "cmdline_hist",
5842#endif
5843#ifdef FEAT_COMMENTS
5844 "comments",
5845#endif
5846#ifdef FEAT_CONCEAL
5847 "conceal",
5848#endif
5849#ifdef FEAT_CRYPT
5850 "cryptv",
5851 "crypt-blowfish",
5852 "crypt-blowfish2",
5853#endif
5854#ifdef FEAT_CSCOPE
5855 "cscope",
5856#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005857 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005858#ifdef CURSOR_SHAPE
5859 "cursorshape",
5860#endif
5861#ifdef DEBUG
5862 "debug",
5863#endif
5864#ifdef FEAT_CON_DIALOG
5865 "dialog_con",
5866#endif
5867#ifdef FEAT_GUI_DIALOG
5868 "dialog_gui",
5869#endif
5870#ifdef FEAT_DIFF
5871 "diff",
5872#endif
5873#ifdef FEAT_DIGRAPHS
5874 "digraphs",
5875#endif
5876#ifdef FEAT_DIRECTX
5877 "directx",
5878#endif
5879#ifdef FEAT_DND
5880 "dnd",
5881#endif
5882#ifdef FEAT_EMACS_TAGS
5883 "emacs_tags",
5884#endif
5885 "eval", /* always present, of course! */
5886 "ex_extra", /* graduated feature */
5887#ifdef FEAT_SEARCH_EXTRA
5888 "extra_search",
5889#endif
5890#ifdef FEAT_FKMAP
5891 "farsi",
5892#endif
5893#ifdef FEAT_SEARCHPATH
5894 "file_in_path",
5895#endif
5896#ifdef FEAT_FILTERPIPE
5897 "filterpipe",
5898#endif
5899#ifdef FEAT_FIND_ID
5900 "find_in_path",
5901#endif
5902#ifdef FEAT_FLOAT
5903 "float",
5904#endif
5905#ifdef FEAT_FOLDING
5906 "folding",
5907#endif
5908#ifdef FEAT_FOOTER
5909 "footer",
5910#endif
5911#if !defined(USE_SYSTEM) && defined(UNIX)
5912 "fork",
5913#endif
5914#ifdef FEAT_GETTEXT
5915 "gettext",
5916#endif
5917#ifdef FEAT_GUI
5918 "gui",
5919#endif
5920#ifdef FEAT_GUI_ATHENA
5921# ifdef FEAT_GUI_NEXTAW
5922 "gui_neXtaw",
5923# else
5924 "gui_athena",
5925# endif
5926#endif
5927#ifdef FEAT_GUI_GTK
5928 "gui_gtk",
5929# ifdef USE_GTK3
5930 "gui_gtk3",
5931# else
5932 "gui_gtk2",
5933# endif
5934#endif
5935#ifdef FEAT_GUI_GNOME
5936 "gui_gnome",
5937#endif
5938#ifdef FEAT_GUI_MAC
5939 "gui_mac",
5940#endif
5941#ifdef FEAT_GUI_MOTIF
5942 "gui_motif",
5943#endif
5944#ifdef FEAT_GUI_PHOTON
5945 "gui_photon",
5946#endif
5947#ifdef FEAT_GUI_W32
5948 "gui_win32",
5949#endif
5950#ifdef FEAT_HANGULIN
5951 "hangul_input",
5952#endif
5953#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
5954 "iconv",
5955#endif
5956#ifdef FEAT_INS_EXPAND
5957 "insert_expand",
5958#endif
5959#ifdef FEAT_JOB_CHANNEL
5960 "job",
5961#endif
5962#ifdef FEAT_JUMPLIST
5963 "jumplist",
5964#endif
5965#ifdef FEAT_KEYMAP
5966 "keymap",
5967#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02005968 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005969#ifdef FEAT_LANGMAP
5970 "langmap",
5971#endif
5972#ifdef FEAT_LIBCALL
5973 "libcall",
5974#endif
5975#ifdef FEAT_LINEBREAK
5976 "linebreak",
5977#endif
5978#ifdef FEAT_LISP
5979 "lispindent",
5980#endif
5981#ifdef FEAT_LISTCMDS
5982 "listcmds",
5983#endif
5984#ifdef FEAT_LOCALMAP
5985 "localmap",
5986#endif
5987#ifdef FEAT_LUA
5988# ifndef DYNAMIC_LUA
5989 "lua",
5990# endif
5991#endif
5992#ifdef FEAT_MENU
5993 "menu",
5994#endif
5995#ifdef FEAT_SESSION
5996 "mksession",
5997#endif
5998#ifdef FEAT_MODIFY_FNAME
5999 "modify_fname",
6000#endif
6001#ifdef FEAT_MOUSE
6002 "mouse",
6003#endif
6004#ifdef FEAT_MOUSESHAPE
6005 "mouseshape",
6006#endif
6007#if defined(UNIX) || defined(VMS)
6008# ifdef FEAT_MOUSE_DEC
6009 "mouse_dec",
6010# endif
6011# ifdef FEAT_MOUSE_GPM
6012 "mouse_gpm",
6013# endif
6014# ifdef FEAT_MOUSE_JSB
6015 "mouse_jsbterm",
6016# endif
6017# ifdef FEAT_MOUSE_NET
6018 "mouse_netterm",
6019# endif
6020# ifdef FEAT_MOUSE_PTERM
6021 "mouse_pterm",
6022# endif
6023# ifdef FEAT_MOUSE_SGR
6024 "mouse_sgr",
6025# endif
6026# ifdef FEAT_SYSMOUSE
6027 "mouse_sysmouse",
6028# endif
6029# ifdef FEAT_MOUSE_URXVT
6030 "mouse_urxvt",
6031# endif
6032# ifdef FEAT_MOUSE_XTERM
6033 "mouse_xterm",
6034# endif
6035#endif
6036#ifdef FEAT_MBYTE
6037 "multi_byte",
6038#endif
6039#ifdef FEAT_MBYTE_IME
6040 "multi_byte_ime",
6041#endif
6042#ifdef FEAT_MULTI_LANG
6043 "multi_lang",
6044#endif
6045#ifdef FEAT_MZSCHEME
6046#ifndef DYNAMIC_MZSCHEME
6047 "mzscheme",
6048#endif
6049#endif
6050#ifdef FEAT_NUM64
6051 "num64",
6052#endif
6053#ifdef FEAT_OLE
6054 "ole",
6055#endif
6056 "packages",
6057#ifdef FEAT_PATH_EXTRA
6058 "path_extra",
6059#endif
6060#ifdef FEAT_PERL
6061#ifndef DYNAMIC_PERL
6062 "perl",
6063#endif
6064#endif
6065#ifdef FEAT_PERSISTENT_UNDO
6066 "persistent_undo",
6067#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006068#if defined(FEAT_PYTHON)
6069 "python_compiled",
6070# if defined(DYNAMIC_PYTHON)
6071 "python_dynamic",
6072# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006073 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006074 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006075# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006076#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006077#if defined(FEAT_PYTHON3)
6078 "python3_compiled",
6079# if defined(DYNAMIC_PYTHON3)
6080 "python3_dynamic",
6081# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006082 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006083 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006084# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006085#endif
6086#ifdef FEAT_POSTSCRIPT
6087 "postscript",
6088#endif
6089#ifdef FEAT_PRINTER
6090 "printer",
6091#endif
6092#ifdef FEAT_PROFILE
6093 "profile",
6094#endif
6095#ifdef FEAT_RELTIME
6096 "reltime",
6097#endif
6098#ifdef FEAT_QUICKFIX
6099 "quickfix",
6100#endif
6101#ifdef FEAT_RIGHTLEFT
6102 "rightleft",
6103#endif
6104#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6105 "ruby",
6106#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006107 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006108#ifdef FEAT_CMDL_INFO
6109 "showcmd",
6110 "cmdline_info",
6111#endif
6112#ifdef FEAT_SIGNS
6113 "signs",
6114#endif
6115#ifdef FEAT_SMARTINDENT
6116 "smartindent",
6117#endif
6118#ifdef STARTUPTIME
6119 "startuptime",
6120#endif
6121#ifdef FEAT_STL_OPT
6122 "statusline",
6123#endif
6124#ifdef FEAT_SUN_WORKSHOP
6125 "sun_workshop",
6126#endif
6127#ifdef FEAT_NETBEANS_INTG
6128 "netbeans_intg",
6129#endif
6130#ifdef FEAT_SPELL
6131 "spell",
6132#endif
6133#ifdef FEAT_SYN_HL
6134 "syntax",
6135#endif
6136#if defined(USE_SYSTEM) || !defined(UNIX)
6137 "system",
6138#endif
6139#ifdef FEAT_TAG_BINS
6140 "tag_binary",
6141#endif
6142#ifdef FEAT_TAG_OLDSTATIC
6143 "tag_old_static",
6144#endif
6145#ifdef FEAT_TAG_ANYWHITE
6146 "tag_any_white",
6147#endif
6148#ifdef FEAT_TCL
6149# ifndef DYNAMIC_TCL
6150 "tcl",
6151# endif
6152#endif
6153#ifdef FEAT_TERMGUICOLORS
6154 "termguicolors",
6155#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006156#if defined(FEAT_TERMINAL) && !defined(WIN3264)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006157 "terminal",
6158#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006159#ifdef TERMINFO
6160 "terminfo",
6161#endif
6162#ifdef FEAT_TERMRESPONSE
6163 "termresponse",
6164#endif
6165#ifdef FEAT_TEXTOBJ
6166 "textobjects",
6167#endif
6168#ifdef HAVE_TGETENT
6169 "tgetent",
6170#endif
6171#ifdef FEAT_TIMERS
6172 "timers",
6173#endif
6174#ifdef FEAT_TITLE
6175 "title",
6176#endif
6177#ifdef FEAT_TOOLBAR
6178 "toolbar",
6179#endif
6180#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6181 "unnamedplus",
6182#endif
6183#ifdef FEAT_USR_CMDS
6184 "user-commands", /* was accidentally included in 5.4 */
6185 "user_commands",
6186#endif
6187#ifdef FEAT_VIMINFO
6188 "viminfo",
6189#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006190 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006191#ifdef FEAT_VIRTUALEDIT
6192 "virtualedit",
6193#endif
6194 "visual",
6195#ifdef FEAT_VISUALEXTRA
6196 "visualextra",
6197#endif
6198#ifdef FEAT_VREPLACE
6199 "vreplace",
6200#endif
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006201#ifdef FEAT_VTP
6202 "vtp",
6203#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006204#ifdef FEAT_WILDIGN
6205 "wildignore",
6206#endif
6207#ifdef FEAT_WILDMENU
6208 "wildmenu",
6209#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006210 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006211#ifdef FEAT_WAK
6212 "winaltkeys",
6213#endif
6214#ifdef FEAT_WRITEBACKUP
6215 "writebackup",
6216#endif
6217#ifdef FEAT_XIM
6218 "xim",
6219#endif
6220#ifdef FEAT_XFONTSET
6221 "xfontset",
6222#endif
6223#ifdef FEAT_XPM_W32
6224 "xpm",
6225 "xpm_w32", /* for backward compatibility */
6226#else
6227# if defined(HAVE_XPM)
6228 "xpm",
6229# endif
6230#endif
6231#ifdef USE_XSMP
6232 "xsmp",
6233#endif
6234#ifdef USE_XSMP_INTERACT
6235 "xsmp_interact",
6236#endif
6237#ifdef FEAT_XCLIPBOARD
6238 "xterm_clipboard",
6239#endif
6240#ifdef FEAT_XTERM_SAVE
6241 "xterm_save",
6242#endif
6243#if defined(UNIX) && defined(FEAT_X11)
6244 "X11",
6245#endif
6246 NULL
6247 };
6248
6249 name = get_tv_string(&argvars[0]);
6250 for (i = 0; has_list[i] != NULL; ++i)
6251 if (STRICMP(name, has_list[i]) == 0)
6252 {
6253 n = TRUE;
6254 break;
6255 }
6256
6257 if (n == FALSE)
6258 {
6259 if (STRNICMP(name, "patch", 5) == 0)
6260 {
6261 if (name[5] == '-'
6262 && STRLEN(name) >= 11
6263 && vim_isdigit(name[6])
6264 && vim_isdigit(name[8])
6265 && vim_isdigit(name[10]))
6266 {
6267 int major = atoi((char *)name + 6);
6268 int minor = atoi((char *)name + 8);
6269
6270 /* Expect "patch-9.9.01234". */
6271 n = (major < VIM_VERSION_MAJOR
6272 || (major == VIM_VERSION_MAJOR
6273 && (minor < VIM_VERSION_MINOR
6274 || (minor == VIM_VERSION_MINOR
6275 && has_patch(atoi((char *)name + 10))))));
6276 }
6277 else
6278 n = has_patch(atoi((char *)name + 5));
6279 }
6280 else if (STRICMP(name, "vim_starting") == 0)
6281 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006282 else if (STRICMP(name, "ttyin") == 0)
6283 n = mch_input_isatty();
6284 else if (STRICMP(name, "ttyout") == 0)
6285 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006286#ifdef FEAT_MBYTE
6287 else if (STRICMP(name, "multi_byte_encoding") == 0)
6288 n = has_mbyte;
6289#endif
6290#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
6291 else if (STRICMP(name, "balloon_multiline") == 0)
6292 n = multiline_balloon_available();
6293#endif
6294#ifdef DYNAMIC_TCL
6295 else if (STRICMP(name, "tcl") == 0)
6296 n = tcl_enabled(FALSE);
6297#endif
6298#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6299 else if (STRICMP(name, "iconv") == 0)
6300 n = iconv_enabled(FALSE);
6301#endif
6302#ifdef DYNAMIC_LUA
6303 else if (STRICMP(name, "lua") == 0)
6304 n = lua_enabled(FALSE);
6305#endif
6306#ifdef DYNAMIC_MZSCHEME
6307 else if (STRICMP(name, "mzscheme") == 0)
6308 n = mzscheme_enabled(FALSE);
6309#endif
6310#ifdef DYNAMIC_RUBY
6311 else if (STRICMP(name, "ruby") == 0)
6312 n = ruby_enabled(FALSE);
6313#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006314#ifdef DYNAMIC_PYTHON
6315 else if (STRICMP(name, "python") == 0)
6316 n = python_enabled(FALSE);
6317#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006318#ifdef DYNAMIC_PYTHON3
6319 else if (STRICMP(name, "python3") == 0)
6320 n = python3_enabled(FALSE);
6321#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006322#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6323 else if (STRICMP(name, "pythonx") == 0)
6324 {
6325# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6326 if (p_pyx == 0)
6327 n = python3_enabled(FALSE) || python_enabled(FALSE);
6328 else if (p_pyx == 3)
6329 n = python3_enabled(FALSE);
6330 else if (p_pyx == 2)
6331 n = python_enabled(FALSE);
6332# elif defined(DYNAMIC_PYTHON)
6333 n = python_enabled(FALSE);
6334# elif defined(DYNAMIC_PYTHON3)
6335 n = python3_enabled(FALSE);
6336# endif
6337 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006338#endif
6339#ifdef DYNAMIC_PERL
6340 else if (STRICMP(name, "perl") == 0)
6341 n = perl_enabled(FALSE);
6342#endif
6343#ifdef FEAT_GUI
6344 else if (STRICMP(name, "gui_running") == 0)
6345 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006346# ifdef FEAT_BROWSE
6347 else if (STRICMP(name, "browse") == 0)
6348 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6349# endif
6350#endif
6351#ifdef FEAT_SYN_HL
6352 else if (STRICMP(name, "syntax_items") == 0)
6353 n = syntax_present(curwin);
6354#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006355#ifdef FEAT_VTP
6356 else if (STRICMP(name, "vcon") == 0)
6357 n = has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006358#endif
6359#ifdef FEAT_NETBEANS_INTG
6360 else if (STRICMP(name, "netbeans_enabled") == 0)
6361 n = netbeans_active();
6362#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006363#if defined(FEAT_TERMINAL) && defined(WIN3264)
6364 else if (STRICMP(name, "terminal") == 0)
6365 n = terminal_enabled();
6366#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006367 }
6368
6369 rettv->vval.v_number = n;
6370}
6371
6372/*
6373 * "has_key()" function
6374 */
6375 static void
6376f_has_key(typval_T *argvars, typval_T *rettv)
6377{
6378 if (argvars[0].v_type != VAR_DICT)
6379 {
6380 EMSG(_(e_dictreq));
6381 return;
6382 }
6383 if (argvars[0].vval.v_dict == NULL)
6384 return;
6385
6386 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
6387 get_tv_string(&argvars[1]), -1) != NULL;
6388}
6389
6390/*
6391 * "haslocaldir()" function
6392 */
6393 static void
6394f_haslocaldir(typval_T *argvars, typval_T *rettv)
6395{
6396 win_T *wp = NULL;
6397
6398 wp = find_tabwin(&argvars[0], &argvars[1]);
6399 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6400}
6401
6402/*
6403 * "hasmapto()" function
6404 */
6405 static void
6406f_hasmapto(typval_T *argvars, typval_T *rettv)
6407{
6408 char_u *name;
6409 char_u *mode;
6410 char_u buf[NUMBUFLEN];
6411 int abbr = FALSE;
6412
6413 name = get_tv_string(&argvars[0]);
6414 if (argvars[1].v_type == VAR_UNKNOWN)
6415 mode = (char_u *)"nvo";
6416 else
6417 {
6418 mode = get_tv_string_buf(&argvars[1], buf);
6419 if (argvars[2].v_type != VAR_UNKNOWN)
6420 abbr = (int)get_tv_number(&argvars[2]);
6421 }
6422
6423 if (map_to_exists(name, mode, abbr))
6424 rettv->vval.v_number = TRUE;
6425 else
6426 rettv->vval.v_number = FALSE;
6427}
6428
6429/*
6430 * "histadd()" function
6431 */
6432 static void
6433f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6434{
6435#ifdef FEAT_CMDHIST
6436 int histype;
6437 char_u *str;
6438 char_u buf[NUMBUFLEN];
6439#endif
6440
6441 rettv->vval.v_number = FALSE;
6442 if (check_restricted() || check_secure())
6443 return;
6444#ifdef FEAT_CMDHIST
6445 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6446 histype = str != NULL ? get_histtype(str) : -1;
6447 if (histype >= 0)
6448 {
6449 str = get_tv_string_buf(&argvars[1], buf);
6450 if (*str != NUL)
6451 {
6452 init_history();
6453 add_to_history(histype, str, FALSE, NUL);
6454 rettv->vval.v_number = TRUE;
6455 return;
6456 }
6457 }
6458#endif
6459}
6460
6461/*
6462 * "histdel()" function
6463 */
6464 static void
6465f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6466{
6467#ifdef FEAT_CMDHIST
6468 int n;
6469 char_u buf[NUMBUFLEN];
6470 char_u *str;
6471
6472 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6473 if (str == NULL)
6474 n = 0;
6475 else if (argvars[1].v_type == VAR_UNKNOWN)
6476 /* only one argument: clear entire history */
6477 n = clr_history(get_histtype(str));
6478 else if (argvars[1].v_type == VAR_NUMBER)
6479 /* index given: remove that entry */
6480 n = del_history_idx(get_histtype(str),
6481 (int)get_tv_number(&argvars[1]));
6482 else
6483 /* string given: remove all matching entries */
6484 n = del_history_entry(get_histtype(str),
6485 get_tv_string_buf(&argvars[1], buf));
6486 rettv->vval.v_number = n;
6487#endif
6488}
6489
6490/*
6491 * "histget()" function
6492 */
6493 static void
6494f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6495{
6496#ifdef FEAT_CMDHIST
6497 int type;
6498 int idx;
6499 char_u *str;
6500
6501 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6502 if (str == NULL)
6503 rettv->vval.v_string = NULL;
6504 else
6505 {
6506 type = get_histtype(str);
6507 if (argvars[1].v_type == VAR_UNKNOWN)
6508 idx = get_history_idx(type);
6509 else
6510 idx = (int)get_tv_number_chk(&argvars[1], NULL);
6511 /* -1 on type error */
6512 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6513 }
6514#else
6515 rettv->vval.v_string = NULL;
6516#endif
6517 rettv->v_type = VAR_STRING;
6518}
6519
6520/*
6521 * "histnr()" function
6522 */
6523 static void
6524f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6525{
6526 int i;
6527
6528#ifdef FEAT_CMDHIST
6529 char_u *history = get_tv_string_chk(&argvars[0]);
6530
6531 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6532 if (i >= HIST_CMD && i < HIST_COUNT)
6533 i = get_history_idx(i);
6534 else
6535#endif
6536 i = -1;
6537 rettv->vval.v_number = i;
6538}
6539
6540/*
6541 * "highlightID(name)" function
6542 */
6543 static void
6544f_hlID(typval_T *argvars, typval_T *rettv)
6545{
6546 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
6547}
6548
6549/*
6550 * "highlight_exists()" function
6551 */
6552 static void
6553f_hlexists(typval_T *argvars, typval_T *rettv)
6554{
6555 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
6556}
6557
6558/*
6559 * "hostname()" function
6560 */
6561 static void
6562f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6563{
6564 char_u hostname[256];
6565
6566 mch_get_host_name(hostname, 256);
6567 rettv->v_type = VAR_STRING;
6568 rettv->vval.v_string = vim_strsave(hostname);
6569}
6570
6571/*
6572 * iconv() function
6573 */
6574 static void
6575f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6576{
6577#ifdef FEAT_MBYTE
6578 char_u buf1[NUMBUFLEN];
6579 char_u buf2[NUMBUFLEN];
6580 char_u *from, *to, *str;
6581 vimconv_T vimconv;
6582#endif
6583
6584 rettv->v_type = VAR_STRING;
6585 rettv->vval.v_string = NULL;
6586
6587#ifdef FEAT_MBYTE
6588 str = get_tv_string(&argvars[0]);
6589 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
6590 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
6591 vimconv.vc_type = CONV_NONE;
6592 convert_setup(&vimconv, from, to);
6593
6594 /* If the encodings are equal, no conversion needed. */
6595 if (vimconv.vc_type == CONV_NONE)
6596 rettv->vval.v_string = vim_strsave(str);
6597 else
6598 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6599
6600 convert_setup(&vimconv, NULL, NULL);
6601 vim_free(from);
6602 vim_free(to);
6603#endif
6604}
6605
6606/*
6607 * "indent()" function
6608 */
6609 static void
6610f_indent(typval_T *argvars, typval_T *rettv)
6611{
6612 linenr_T lnum;
6613
6614 lnum = get_tv_lnum(argvars);
6615 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6616 rettv->vval.v_number = get_indent_lnum(lnum);
6617 else
6618 rettv->vval.v_number = -1;
6619}
6620
6621/*
6622 * "index()" function
6623 */
6624 static void
6625f_index(typval_T *argvars, typval_T *rettv)
6626{
6627 list_T *l;
6628 listitem_T *item;
6629 long idx = 0;
6630 int ic = FALSE;
6631
6632 rettv->vval.v_number = -1;
6633 if (argvars[0].v_type != VAR_LIST)
6634 {
6635 EMSG(_(e_listreq));
6636 return;
6637 }
6638 l = argvars[0].vval.v_list;
6639 if (l != NULL)
6640 {
6641 item = l->lv_first;
6642 if (argvars[2].v_type != VAR_UNKNOWN)
6643 {
6644 int error = FALSE;
6645
6646 /* Start at specified item. Use the cached index that list_find()
6647 * sets, so that a negative number also works. */
6648 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
6649 idx = l->lv_idx;
6650 if (argvars[3].v_type != VAR_UNKNOWN)
6651 ic = (int)get_tv_number_chk(&argvars[3], &error);
6652 if (error)
6653 item = NULL;
6654 }
6655
6656 for ( ; item != NULL; item = item->li_next, ++idx)
6657 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6658 {
6659 rettv->vval.v_number = idx;
6660 break;
6661 }
6662 }
6663}
6664
6665static int inputsecret_flag = 0;
6666
6667/*
6668 * "input()" function
6669 * Also handles inputsecret() when inputsecret is set.
6670 */
6671 static void
6672f_input(typval_T *argvars, typval_T *rettv)
6673{
6674 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6675}
6676
6677/*
6678 * "inputdialog()" function
6679 */
6680 static void
6681f_inputdialog(typval_T *argvars, typval_T *rettv)
6682{
6683#if defined(FEAT_GUI_TEXTDIALOG)
6684 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
6685 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6686 {
6687 char_u *message;
6688 char_u buf[NUMBUFLEN];
6689 char_u *defstr = (char_u *)"";
6690
6691 message = get_tv_string_chk(&argvars[0]);
6692 if (argvars[1].v_type != VAR_UNKNOWN
6693 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
6694 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6695 else
6696 IObuff[0] = NUL;
6697 if (message != NULL && defstr != NULL
6698 && do_dialog(VIM_QUESTION, NULL, message,
6699 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6700 rettv->vval.v_string = vim_strsave(IObuff);
6701 else
6702 {
6703 if (message != NULL && defstr != NULL
6704 && argvars[1].v_type != VAR_UNKNOWN
6705 && argvars[2].v_type != VAR_UNKNOWN)
6706 rettv->vval.v_string = vim_strsave(
6707 get_tv_string_buf(&argvars[2], buf));
6708 else
6709 rettv->vval.v_string = NULL;
6710 }
6711 rettv->v_type = VAR_STRING;
6712 }
6713 else
6714#endif
6715 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6716}
6717
6718/*
6719 * "inputlist()" function
6720 */
6721 static void
6722f_inputlist(typval_T *argvars, typval_T *rettv)
6723{
6724 listitem_T *li;
6725 int selected;
6726 int mouse_used;
6727
6728#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02006729 /* While starting up, there is no place to enter text. When running tests
6730 * with --not-a-term we assume feedkeys() will be used. */
6731 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006732 return;
6733#endif
6734 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6735 {
6736 EMSG2(_(e_listarg), "inputlist()");
6737 return;
6738 }
6739
6740 msg_start();
6741 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
6742 lines_left = Rows; /* avoid more prompt */
6743 msg_scroll = TRUE;
6744 msg_clr_eos();
6745
6746 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
6747 {
6748 msg_puts(get_tv_string(&li->li_tv));
6749 msg_putchar('\n');
6750 }
6751
6752 /* Ask for choice. */
6753 selected = prompt_for_number(&mouse_used);
6754 if (mouse_used)
6755 selected -= lines_left;
6756
6757 rettv->vval.v_number = selected;
6758}
6759
6760
6761static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6762
6763/*
6764 * "inputrestore()" function
6765 */
6766 static void
6767f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6768{
6769 if (ga_userinput.ga_len > 0)
6770 {
6771 --ga_userinput.ga_len;
6772 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
6773 + ga_userinput.ga_len);
6774 /* default return is zero == OK */
6775 }
6776 else if (p_verbose > 1)
6777 {
6778 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
6779 rettv->vval.v_number = 1; /* Failed */
6780 }
6781}
6782
6783/*
6784 * "inputsave()" function
6785 */
6786 static void
6787f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
6788{
6789 /* Add an entry to the stack of typeahead storage. */
6790 if (ga_grow(&ga_userinput, 1) == OK)
6791 {
6792 save_typeahead((tasave_T *)(ga_userinput.ga_data)
6793 + ga_userinput.ga_len);
6794 ++ga_userinput.ga_len;
6795 /* default return is zero == OK */
6796 }
6797 else
6798 rettv->vval.v_number = 1; /* Failed */
6799}
6800
6801/*
6802 * "inputsecret()" function
6803 */
6804 static void
6805f_inputsecret(typval_T *argvars, typval_T *rettv)
6806{
6807 ++cmdline_star;
6808 ++inputsecret_flag;
6809 f_input(argvars, rettv);
6810 --cmdline_star;
6811 --inputsecret_flag;
6812}
6813
6814/*
6815 * "insert()" function
6816 */
6817 static void
6818f_insert(typval_T *argvars, typval_T *rettv)
6819{
6820 long before = 0;
6821 listitem_T *item;
6822 list_T *l;
6823 int error = FALSE;
6824
6825 if (argvars[0].v_type != VAR_LIST)
6826 EMSG2(_(e_listarg), "insert()");
6827 else if ((l = argvars[0].vval.v_list) != NULL
6828 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
6829 {
6830 if (argvars[2].v_type != VAR_UNKNOWN)
6831 before = (long)get_tv_number_chk(&argvars[2], &error);
6832 if (error)
6833 return; /* type error; errmsg already given */
6834
6835 if (before == l->lv_len)
6836 item = NULL;
6837 else
6838 {
6839 item = list_find(l, before);
6840 if (item == NULL)
6841 {
6842 EMSGN(_(e_listidx), before);
6843 l = NULL;
6844 }
6845 }
6846 if (l != NULL)
6847 {
6848 list_insert_tv(l, &argvars[1], item);
6849 copy_tv(&argvars[0], rettv);
6850 }
6851 }
6852}
6853
6854/*
6855 * "invert(expr)" function
6856 */
6857 static void
6858f_invert(typval_T *argvars, typval_T *rettv)
6859{
6860 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
6861}
6862
6863/*
6864 * "isdirectory()" function
6865 */
6866 static void
6867f_isdirectory(typval_T *argvars, typval_T *rettv)
6868{
6869 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
6870}
6871
6872/*
6873 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
6874 * or it refers to a List or Dictionary that is locked.
6875 */
6876 static int
6877tv_islocked(typval_T *tv)
6878{
6879 return (tv->v_lock & VAR_LOCKED)
6880 || (tv->v_type == VAR_LIST
6881 && tv->vval.v_list != NULL
6882 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
6883 || (tv->v_type == VAR_DICT
6884 && tv->vval.v_dict != NULL
6885 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
6886}
6887
6888/*
6889 * "islocked()" function
6890 */
6891 static void
6892f_islocked(typval_T *argvars, typval_T *rettv)
6893{
6894 lval_T lv;
6895 char_u *end;
6896 dictitem_T *di;
6897
6898 rettv->vval.v_number = -1;
6899 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01006900 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006901 if (end != NULL && lv.ll_name != NULL)
6902 {
6903 if (*end != NUL)
6904 EMSG(_(e_trailing));
6905 else
6906 {
6907 if (lv.ll_tv == NULL)
6908 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006909 di = find_var(lv.ll_name, NULL, TRUE);
6910 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006911 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006912 /* Consider a variable locked when:
6913 * 1. the variable itself is locked
6914 * 2. the value of the variable is locked.
6915 * 3. the List or Dict value is locked.
6916 */
6917 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
6918 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006919 }
6920 }
6921 else if (lv.ll_range)
6922 EMSG(_("E786: Range not allowed"));
6923 else if (lv.ll_newkey != NULL)
6924 EMSG2(_(e_dictkey), lv.ll_newkey);
6925 else if (lv.ll_list != NULL)
6926 /* List item. */
6927 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
6928 else
6929 /* Dictionary item. */
6930 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
6931 }
6932 }
6933
6934 clear_lval(&lv);
6935}
6936
6937#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
6938/*
6939 * "isnan()" function
6940 */
6941 static void
6942f_isnan(typval_T *argvars, typval_T *rettv)
6943{
6944 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
6945 && isnan(argvars[0].vval.v_float);
6946}
6947#endif
6948
6949/*
6950 * "items(dict)" function
6951 */
6952 static void
6953f_items(typval_T *argvars, typval_T *rettv)
6954{
6955 dict_list(argvars, rettv, 2);
6956}
6957
6958#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
6959/*
6960 * Get the job from the argument.
6961 * Returns NULL if the job is invalid.
6962 */
6963 static job_T *
6964get_job_arg(typval_T *tv)
6965{
6966 job_T *job;
6967
6968 if (tv->v_type != VAR_JOB)
6969 {
6970 EMSG2(_(e_invarg2), get_tv_string(tv));
6971 return NULL;
6972 }
6973 job = tv->vval.v_job;
6974
6975 if (job == NULL)
6976 EMSG(_("E916: not a valid job"));
6977 return job;
6978}
6979
6980/*
6981 * "job_getchannel()" function
6982 */
6983 static void
6984f_job_getchannel(typval_T *argvars, typval_T *rettv)
6985{
6986 job_T *job = get_job_arg(&argvars[0]);
6987
6988 if (job != NULL)
6989 {
6990 rettv->v_type = VAR_CHANNEL;
6991 rettv->vval.v_channel = job->jv_channel;
6992 if (job->jv_channel != NULL)
6993 ++job->jv_channel->ch_refcount;
6994 }
6995}
6996
6997/*
6998 * "job_info()" function
6999 */
7000 static void
7001f_job_info(typval_T *argvars, typval_T *rettv)
7002{
7003 job_T *job = get_job_arg(&argvars[0]);
7004
7005 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7006 job_info(job, rettv->vval.v_dict);
7007}
7008
7009/*
7010 * "job_setoptions()" function
7011 */
7012 static void
7013f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7014{
7015 job_T *job = get_job_arg(&argvars[0]);
7016 jobopt_T opt;
7017
7018 if (job == NULL)
7019 return;
7020 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007021 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007022 job_set_options(job, &opt);
7023 free_job_options(&opt);
7024}
7025
7026/*
7027 * "job_start()" function
7028 */
7029 static void
7030f_job_start(typval_T *argvars, typval_T *rettv)
7031{
7032 rettv->v_type = VAR_JOB;
7033 if (check_restricted() || check_secure())
7034 return;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02007035 rettv->vval.v_job = job_start(argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007036}
7037
7038/*
7039 * "job_status()" function
7040 */
7041 static void
7042f_job_status(typval_T *argvars, typval_T *rettv)
7043{
7044 job_T *job = get_job_arg(&argvars[0]);
7045
7046 if (job != NULL)
7047 {
7048 rettv->v_type = VAR_STRING;
7049 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7050 }
7051}
7052
7053/*
7054 * "job_stop()" function
7055 */
7056 static void
7057f_job_stop(typval_T *argvars, typval_T *rettv)
7058{
7059 job_T *job = get_job_arg(&argvars[0]);
7060
7061 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007062 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007063}
7064#endif
7065
7066/*
7067 * "join()" function
7068 */
7069 static void
7070f_join(typval_T *argvars, typval_T *rettv)
7071{
7072 garray_T ga;
7073 char_u *sep;
7074
7075 if (argvars[0].v_type != VAR_LIST)
7076 {
7077 EMSG(_(e_listreq));
7078 return;
7079 }
7080 if (argvars[0].vval.v_list == NULL)
7081 return;
7082 if (argvars[1].v_type == VAR_UNKNOWN)
7083 sep = (char_u *)" ";
7084 else
7085 sep = get_tv_string_chk(&argvars[1]);
7086
7087 rettv->v_type = VAR_STRING;
7088
7089 if (sep != NULL)
7090 {
7091 ga_init2(&ga, (int)sizeof(char), 80);
7092 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7093 ga_append(&ga, NUL);
7094 rettv->vval.v_string = (char_u *)ga.ga_data;
7095 }
7096 else
7097 rettv->vval.v_string = NULL;
7098}
7099
7100/*
7101 * "js_decode()" function
7102 */
7103 static void
7104f_js_decode(typval_T *argvars, typval_T *rettv)
7105{
7106 js_read_T reader;
7107
7108 reader.js_buf = get_tv_string(&argvars[0]);
7109 reader.js_fill = NULL;
7110 reader.js_used = 0;
7111 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
7112 EMSG(_(e_invarg));
7113}
7114
7115/*
7116 * "js_encode()" function
7117 */
7118 static void
7119f_js_encode(typval_T *argvars, typval_T *rettv)
7120{
7121 rettv->v_type = VAR_STRING;
7122 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7123}
7124
7125/*
7126 * "json_decode()" function
7127 */
7128 static void
7129f_json_decode(typval_T *argvars, typval_T *rettv)
7130{
7131 js_read_T reader;
7132
7133 reader.js_buf = get_tv_string(&argvars[0]);
7134 reader.js_fill = NULL;
7135 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007136 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007137}
7138
7139/*
7140 * "json_encode()" function
7141 */
7142 static void
7143f_json_encode(typval_T *argvars, typval_T *rettv)
7144{
7145 rettv->v_type = VAR_STRING;
7146 rettv->vval.v_string = json_encode(&argvars[0], 0);
7147}
7148
7149/*
7150 * "keys()" function
7151 */
7152 static void
7153f_keys(typval_T *argvars, typval_T *rettv)
7154{
7155 dict_list(argvars, rettv, 0);
7156}
7157
7158/*
7159 * "last_buffer_nr()" function.
7160 */
7161 static void
7162f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7163{
7164 int n = 0;
7165 buf_T *buf;
7166
Bram Moolenaar29323592016-07-24 22:04:11 +02007167 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007168 if (n < buf->b_fnum)
7169 n = buf->b_fnum;
7170
7171 rettv->vval.v_number = n;
7172}
7173
7174/*
7175 * "len()" function
7176 */
7177 static void
7178f_len(typval_T *argvars, typval_T *rettv)
7179{
7180 switch (argvars[0].v_type)
7181 {
7182 case VAR_STRING:
7183 case VAR_NUMBER:
7184 rettv->vval.v_number = (varnumber_T)STRLEN(
7185 get_tv_string(&argvars[0]));
7186 break;
7187 case VAR_LIST:
7188 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7189 break;
7190 case VAR_DICT:
7191 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7192 break;
7193 case VAR_UNKNOWN:
7194 case VAR_SPECIAL:
7195 case VAR_FLOAT:
7196 case VAR_FUNC:
7197 case VAR_PARTIAL:
7198 case VAR_JOB:
7199 case VAR_CHANNEL:
7200 EMSG(_("E701: Invalid type for len()"));
7201 break;
7202 }
7203}
7204
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007205 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007206libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007207{
7208#ifdef FEAT_LIBCALL
7209 char_u *string_in;
7210 char_u **string_result;
7211 int nr_result;
7212#endif
7213
7214 rettv->v_type = type;
7215 if (type != VAR_NUMBER)
7216 rettv->vval.v_string = NULL;
7217
7218 if (check_restricted() || check_secure())
7219 return;
7220
7221#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007222 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007223 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7224 {
7225 string_in = NULL;
7226 if (argvars[2].v_type == VAR_STRING)
7227 string_in = argvars[2].vval.v_string;
7228 if (type == VAR_NUMBER)
7229 string_result = NULL;
7230 else
7231 string_result = &rettv->vval.v_string;
7232 if (mch_libcall(argvars[0].vval.v_string,
7233 argvars[1].vval.v_string,
7234 string_in,
7235 argvars[2].vval.v_number,
7236 string_result,
7237 &nr_result) == OK
7238 && type == VAR_NUMBER)
7239 rettv->vval.v_number = nr_result;
7240 }
7241#endif
7242}
7243
7244/*
7245 * "libcall()" function
7246 */
7247 static void
7248f_libcall(typval_T *argvars, typval_T *rettv)
7249{
7250 libcall_common(argvars, rettv, VAR_STRING);
7251}
7252
7253/*
7254 * "libcallnr()" function
7255 */
7256 static void
7257f_libcallnr(typval_T *argvars, typval_T *rettv)
7258{
7259 libcall_common(argvars, rettv, VAR_NUMBER);
7260}
7261
7262/*
7263 * "line(string)" function
7264 */
7265 static void
7266f_line(typval_T *argvars, typval_T *rettv)
7267{
7268 linenr_T lnum = 0;
7269 pos_T *fp;
7270 int fnum;
7271
7272 fp = var2fpos(&argvars[0], TRUE, &fnum);
7273 if (fp != NULL)
7274 lnum = fp->lnum;
7275 rettv->vval.v_number = lnum;
7276}
7277
7278/*
7279 * "line2byte(lnum)" function
7280 */
7281 static void
7282f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7283{
7284#ifndef FEAT_BYTEOFF
7285 rettv->vval.v_number = -1;
7286#else
7287 linenr_T lnum;
7288
7289 lnum = get_tv_lnum(argvars);
7290 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7291 rettv->vval.v_number = -1;
7292 else
7293 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7294 if (rettv->vval.v_number >= 0)
7295 ++rettv->vval.v_number;
7296#endif
7297}
7298
7299/*
7300 * "lispindent(lnum)" function
7301 */
7302 static void
7303f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7304{
7305#ifdef FEAT_LISP
7306 pos_T pos;
7307 linenr_T lnum;
7308
7309 pos = curwin->w_cursor;
7310 lnum = get_tv_lnum(argvars);
7311 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7312 {
7313 curwin->w_cursor.lnum = lnum;
7314 rettv->vval.v_number = get_lisp_indent();
7315 curwin->w_cursor = pos;
7316 }
7317 else
7318#endif
7319 rettv->vval.v_number = -1;
7320}
7321
7322/*
7323 * "localtime()" function
7324 */
7325 static void
7326f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7327{
7328 rettv->vval.v_number = (varnumber_T)time(NULL);
7329}
7330
7331static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
7332
7333 static void
7334get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7335{
7336 char_u *keys;
7337 char_u *which;
7338 char_u buf[NUMBUFLEN];
7339 char_u *keys_buf = NULL;
7340 char_u *rhs;
7341 int mode;
7342 int abbr = FALSE;
7343 int get_dict = FALSE;
7344 mapblock_T *mp;
7345 int buffer_local;
7346
7347 /* return empty string for failure */
7348 rettv->v_type = VAR_STRING;
7349 rettv->vval.v_string = NULL;
7350
7351 keys = get_tv_string(&argvars[0]);
7352 if (*keys == NUL)
7353 return;
7354
7355 if (argvars[1].v_type != VAR_UNKNOWN)
7356 {
7357 which = get_tv_string_buf_chk(&argvars[1], buf);
7358 if (argvars[2].v_type != VAR_UNKNOWN)
7359 {
7360 abbr = (int)get_tv_number(&argvars[2]);
7361 if (argvars[3].v_type != VAR_UNKNOWN)
7362 get_dict = (int)get_tv_number(&argvars[3]);
7363 }
7364 }
7365 else
7366 which = (char_u *)"";
7367 if (which == NULL)
7368 return;
7369
7370 mode = get_map_mode(&which, 0);
7371
7372 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7373 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7374 vim_free(keys_buf);
7375
7376 if (!get_dict)
7377 {
7378 /* Return a string. */
7379 if (rhs != NULL)
7380 rettv->vval.v_string = str2special_save(rhs, FALSE);
7381
7382 }
7383 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7384 {
7385 /* Return a dictionary. */
7386 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7387 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7388 dict_T *dict = rettv->vval.v_dict;
7389
7390 dict_add_nr_str(dict, "lhs", 0L, lhs);
7391 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
7392 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
7393 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
7394 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
7395 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
7396 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
7397 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
7398 dict_add_nr_str(dict, "mode", 0L, mapmode);
7399
7400 vim_free(lhs);
7401 vim_free(mapmode);
7402 }
7403}
7404
7405#ifdef FEAT_FLOAT
7406/*
7407 * "log()" function
7408 */
7409 static void
7410f_log(typval_T *argvars, typval_T *rettv)
7411{
7412 float_T f = 0.0;
7413
7414 rettv->v_type = VAR_FLOAT;
7415 if (get_float_arg(argvars, &f) == OK)
7416 rettv->vval.v_float = log(f);
7417 else
7418 rettv->vval.v_float = 0.0;
7419}
7420
7421/*
7422 * "log10()" function
7423 */
7424 static void
7425f_log10(typval_T *argvars, typval_T *rettv)
7426{
7427 float_T f = 0.0;
7428
7429 rettv->v_type = VAR_FLOAT;
7430 if (get_float_arg(argvars, &f) == OK)
7431 rettv->vval.v_float = log10(f);
7432 else
7433 rettv->vval.v_float = 0.0;
7434}
7435#endif
7436
7437#ifdef FEAT_LUA
7438/*
7439 * "luaeval()" function
7440 */
7441 static void
7442f_luaeval(typval_T *argvars, typval_T *rettv)
7443{
7444 char_u *str;
7445 char_u buf[NUMBUFLEN];
7446
7447 str = get_tv_string_buf(&argvars[0], buf);
7448 do_luaeval(str, argvars + 1, rettv);
7449}
7450#endif
7451
7452/*
7453 * "map()" function
7454 */
7455 static void
7456f_map(typval_T *argvars, typval_T *rettv)
7457{
7458 filter_map(argvars, rettv, TRUE);
7459}
7460
7461/*
7462 * "maparg()" function
7463 */
7464 static void
7465f_maparg(typval_T *argvars, typval_T *rettv)
7466{
7467 get_maparg(argvars, rettv, TRUE);
7468}
7469
7470/*
7471 * "mapcheck()" function
7472 */
7473 static void
7474f_mapcheck(typval_T *argvars, typval_T *rettv)
7475{
7476 get_maparg(argvars, rettv, FALSE);
7477}
7478
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007479typedef enum
7480{
7481 MATCH_END, /* matchend() */
7482 MATCH_MATCH, /* match() */
7483 MATCH_STR, /* matchstr() */
7484 MATCH_LIST, /* matchlist() */
7485 MATCH_POS /* matchstrpos() */
7486} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007487
7488 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007489find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007490{
7491 char_u *str = NULL;
7492 long len = 0;
7493 char_u *expr = NULL;
7494 char_u *pat;
7495 regmatch_T regmatch;
7496 char_u patbuf[NUMBUFLEN];
7497 char_u strbuf[NUMBUFLEN];
7498 char_u *save_cpo;
7499 long start = 0;
7500 long nth = 1;
7501 colnr_T startcol = 0;
7502 int match = 0;
7503 list_T *l = NULL;
7504 listitem_T *li = NULL;
7505 long idx = 0;
7506 char_u *tofree = NULL;
7507
7508 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7509 save_cpo = p_cpo;
7510 p_cpo = (char_u *)"";
7511
7512 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007513 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007514 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007515 /* type MATCH_LIST: return empty list when there are no matches.
7516 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007517 if (rettv_list_alloc(rettv) == FAIL)
7518 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007519 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007520 && (list_append_string(rettv->vval.v_list,
7521 (char_u *)"", 0) == FAIL
7522 || list_append_number(rettv->vval.v_list,
7523 (varnumber_T)-1) == FAIL
7524 || list_append_number(rettv->vval.v_list,
7525 (varnumber_T)-1) == FAIL
7526 || list_append_number(rettv->vval.v_list,
7527 (varnumber_T)-1) == FAIL))
7528 {
7529 list_free(rettv->vval.v_list);
7530 rettv->vval.v_list = NULL;
7531 goto theend;
7532 }
7533 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007534 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007535 {
7536 rettv->v_type = VAR_STRING;
7537 rettv->vval.v_string = NULL;
7538 }
7539
7540 if (argvars[0].v_type == VAR_LIST)
7541 {
7542 if ((l = argvars[0].vval.v_list) == NULL)
7543 goto theend;
7544 li = l->lv_first;
7545 }
7546 else
7547 {
7548 expr = str = get_tv_string(&argvars[0]);
7549 len = (long)STRLEN(str);
7550 }
7551
7552 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
7553 if (pat == NULL)
7554 goto theend;
7555
7556 if (argvars[2].v_type != VAR_UNKNOWN)
7557 {
7558 int error = FALSE;
7559
7560 start = (long)get_tv_number_chk(&argvars[2], &error);
7561 if (error)
7562 goto theend;
7563 if (l != NULL)
7564 {
7565 li = list_find(l, start);
7566 if (li == NULL)
7567 goto theend;
7568 idx = l->lv_idx; /* use the cached index */
7569 }
7570 else
7571 {
7572 if (start < 0)
7573 start = 0;
7574 if (start > len)
7575 goto theend;
7576 /* When "count" argument is there ignore matches before "start",
7577 * otherwise skip part of the string. Differs when pattern is "^"
7578 * or "\<". */
7579 if (argvars[3].v_type != VAR_UNKNOWN)
7580 startcol = start;
7581 else
7582 {
7583 str += start;
7584 len -= start;
7585 }
7586 }
7587
7588 if (argvars[3].v_type != VAR_UNKNOWN)
7589 nth = (long)get_tv_number_chk(&argvars[3], &error);
7590 if (error)
7591 goto theend;
7592 }
7593
7594 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7595 if (regmatch.regprog != NULL)
7596 {
7597 regmatch.rm_ic = p_ic;
7598
7599 for (;;)
7600 {
7601 if (l != NULL)
7602 {
7603 if (li == NULL)
7604 {
7605 match = FALSE;
7606 break;
7607 }
7608 vim_free(tofree);
7609 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7610 if (str == NULL)
7611 break;
7612 }
7613
7614 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7615
7616 if (match && --nth <= 0)
7617 break;
7618 if (l == NULL && !match)
7619 break;
7620
7621 /* Advance to just after the match. */
7622 if (l != NULL)
7623 {
7624 li = li->li_next;
7625 ++idx;
7626 }
7627 else
7628 {
7629#ifdef FEAT_MBYTE
7630 startcol = (colnr_T)(regmatch.startp[0]
7631 + (*mb_ptr2len)(regmatch.startp[0]) - str);
7632#else
7633 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
7634#endif
7635 if (startcol > (colnr_T)len
7636 || str + startcol <= regmatch.startp[0])
7637 {
7638 match = FALSE;
7639 break;
7640 }
7641 }
7642 }
7643
7644 if (match)
7645 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007646 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007647 {
7648 listitem_T *li1 = rettv->vval.v_list->lv_first;
7649 listitem_T *li2 = li1->li_next;
7650 listitem_T *li3 = li2->li_next;
7651 listitem_T *li4 = li3->li_next;
7652
7653 vim_free(li1->li_tv.vval.v_string);
7654 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7655 (int)(regmatch.endp[0] - regmatch.startp[0]));
7656 li3->li_tv.vval.v_number =
7657 (varnumber_T)(regmatch.startp[0] - expr);
7658 li4->li_tv.vval.v_number =
7659 (varnumber_T)(regmatch.endp[0] - expr);
7660 if (l != NULL)
7661 li2->li_tv.vval.v_number = (varnumber_T)idx;
7662 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007663 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007664 {
7665 int i;
7666
7667 /* return list with matched string and submatches */
7668 for (i = 0; i < NSUBEXP; ++i)
7669 {
7670 if (regmatch.endp[i] == NULL)
7671 {
7672 if (list_append_string(rettv->vval.v_list,
7673 (char_u *)"", 0) == FAIL)
7674 break;
7675 }
7676 else if (list_append_string(rettv->vval.v_list,
7677 regmatch.startp[i],
7678 (int)(regmatch.endp[i] - regmatch.startp[i]))
7679 == FAIL)
7680 break;
7681 }
7682 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007683 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007684 {
7685 /* return matched string */
7686 if (l != NULL)
7687 copy_tv(&li->li_tv, rettv);
7688 else
7689 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
7690 (int)(regmatch.endp[0] - regmatch.startp[0]));
7691 }
7692 else if (l != NULL)
7693 rettv->vval.v_number = idx;
7694 else
7695 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007696 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007697 rettv->vval.v_number =
7698 (varnumber_T)(regmatch.startp[0] - str);
7699 else
7700 rettv->vval.v_number =
7701 (varnumber_T)(regmatch.endp[0] - str);
7702 rettv->vval.v_number += (varnumber_T)(str - expr);
7703 }
7704 }
7705 vim_regfree(regmatch.regprog);
7706 }
7707
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007708theend:
7709 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007710 /* matchstrpos() without a list: drop the second item. */
7711 listitem_remove(rettv->vval.v_list,
7712 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007713 vim_free(tofree);
7714 p_cpo = save_cpo;
7715}
7716
7717/*
7718 * "match()" function
7719 */
7720 static void
7721f_match(typval_T *argvars, typval_T *rettv)
7722{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007723 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007724}
7725
7726/*
7727 * "matchadd()" function
7728 */
7729 static void
7730f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7731{
7732#ifdef FEAT_SEARCH_EXTRA
7733 char_u buf[NUMBUFLEN];
7734 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
7735 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
7736 int prio = 10; /* default priority */
7737 int id = -1;
7738 int error = FALSE;
7739 char_u *conceal_char = NULL;
7740
7741 rettv->vval.v_number = -1;
7742
7743 if (grp == NULL || pat == NULL)
7744 return;
7745 if (argvars[2].v_type != VAR_UNKNOWN)
7746 {
7747 prio = (int)get_tv_number_chk(&argvars[2], &error);
7748 if (argvars[3].v_type != VAR_UNKNOWN)
7749 {
7750 id = (int)get_tv_number_chk(&argvars[3], &error);
7751 if (argvars[4].v_type != VAR_UNKNOWN)
7752 {
7753 if (argvars[4].v_type != VAR_DICT)
7754 {
7755 EMSG(_(e_dictreq));
7756 return;
7757 }
7758 if (dict_find(argvars[4].vval.v_dict,
7759 (char_u *)"conceal", -1) != NULL)
7760 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7761 (char_u *)"conceal", FALSE);
7762 }
7763 }
7764 }
7765 if (error == TRUE)
7766 return;
7767 if (id >= 1 && id <= 3)
7768 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007769 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007770 return;
7771 }
7772
7773 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
7774 conceal_char);
7775#endif
7776}
7777
7778/*
7779 * "matchaddpos()" function
7780 */
7781 static void
7782f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7783{
7784#ifdef FEAT_SEARCH_EXTRA
7785 char_u buf[NUMBUFLEN];
7786 char_u *group;
7787 int prio = 10;
7788 int id = -1;
7789 int error = FALSE;
7790 list_T *l;
7791 char_u *conceal_char = NULL;
7792
7793 rettv->vval.v_number = -1;
7794
7795 group = get_tv_string_buf_chk(&argvars[0], buf);
7796 if (group == NULL)
7797 return;
7798
7799 if (argvars[1].v_type != VAR_LIST)
7800 {
7801 EMSG2(_(e_listarg), "matchaddpos()");
7802 return;
7803 }
7804 l = argvars[1].vval.v_list;
7805 if (l == NULL)
7806 return;
7807
7808 if (argvars[2].v_type != VAR_UNKNOWN)
7809 {
7810 prio = (int)get_tv_number_chk(&argvars[2], &error);
7811 if (argvars[3].v_type != VAR_UNKNOWN)
7812 {
7813 id = (int)get_tv_number_chk(&argvars[3], &error);
7814 if (argvars[4].v_type != VAR_UNKNOWN)
7815 {
7816 if (argvars[4].v_type != VAR_DICT)
7817 {
7818 EMSG(_(e_dictreq));
7819 return;
7820 }
7821 if (dict_find(argvars[4].vval.v_dict,
7822 (char_u *)"conceal", -1) != NULL)
7823 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7824 (char_u *)"conceal", FALSE);
7825 }
7826 }
7827 }
7828 if (error == TRUE)
7829 return;
7830
7831 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
7832 if (id == 1 || id == 2)
7833 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007834 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007835 return;
7836 }
7837
7838 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
7839 conceal_char);
7840#endif
7841}
7842
7843/*
7844 * "matcharg()" function
7845 */
7846 static void
7847f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
7848{
7849 if (rettv_list_alloc(rettv) == OK)
7850 {
7851#ifdef FEAT_SEARCH_EXTRA
7852 int id = (int)get_tv_number(&argvars[0]);
7853 matchitem_T *m;
7854
7855 if (id >= 1 && id <= 3)
7856 {
7857 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
7858 {
7859 list_append_string(rettv->vval.v_list,
7860 syn_id2name(m->hlg_id), -1);
7861 list_append_string(rettv->vval.v_list, m->pattern, -1);
7862 }
7863 else
7864 {
7865 list_append_string(rettv->vval.v_list, NULL, -1);
7866 list_append_string(rettv->vval.v_list, NULL, -1);
7867 }
7868 }
7869#endif
7870 }
7871}
7872
7873/*
7874 * "matchdelete()" function
7875 */
7876 static void
7877f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7878{
7879#ifdef FEAT_SEARCH_EXTRA
7880 rettv->vval.v_number = match_delete(curwin,
7881 (int)get_tv_number(&argvars[0]), TRUE);
7882#endif
7883}
7884
7885/*
7886 * "matchend()" function
7887 */
7888 static void
7889f_matchend(typval_T *argvars, typval_T *rettv)
7890{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007891 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007892}
7893
7894/*
7895 * "matchlist()" function
7896 */
7897 static void
7898f_matchlist(typval_T *argvars, typval_T *rettv)
7899{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007900 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007901}
7902
7903/*
7904 * "matchstr()" function
7905 */
7906 static void
7907f_matchstr(typval_T *argvars, typval_T *rettv)
7908{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007909 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007910}
7911
7912/*
7913 * "matchstrpos()" function
7914 */
7915 static void
7916f_matchstrpos(typval_T *argvars, typval_T *rettv)
7917{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007918 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007919}
7920
7921static void max_min(typval_T *argvars, typval_T *rettv, int domax);
7922
7923 static void
7924max_min(typval_T *argvars, typval_T *rettv, int domax)
7925{
7926 varnumber_T n = 0;
7927 varnumber_T i;
7928 int error = FALSE;
7929
7930 if (argvars[0].v_type == VAR_LIST)
7931 {
7932 list_T *l;
7933 listitem_T *li;
7934
7935 l = argvars[0].vval.v_list;
7936 if (l != NULL)
7937 {
7938 li = l->lv_first;
7939 if (li != NULL)
7940 {
7941 n = get_tv_number_chk(&li->li_tv, &error);
7942 for (;;)
7943 {
7944 li = li->li_next;
7945 if (li == NULL)
7946 break;
7947 i = get_tv_number_chk(&li->li_tv, &error);
7948 if (domax ? i > n : i < n)
7949 n = i;
7950 }
7951 }
7952 }
7953 }
7954 else if (argvars[0].v_type == VAR_DICT)
7955 {
7956 dict_T *d;
7957 int first = TRUE;
7958 hashitem_T *hi;
7959 int todo;
7960
7961 d = argvars[0].vval.v_dict;
7962 if (d != NULL)
7963 {
7964 todo = (int)d->dv_hashtab.ht_used;
7965 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
7966 {
7967 if (!HASHITEM_EMPTY(hi))
7968 {
7969 --todo;
7970 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
7971 if (first)
7972 {
7973 n = i;
7974 first = FALSE;
7975 }
7976 else if (domax ? i > n : i < n)
7977 n = i;
7978 }
7979 }
7980 }
7981 }
7982 else
Bram Moolenaar26b84332016-09-04 21:42:36 +02007983 EMSG2(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007984 rettv->vval.v_number = error ? 0 : n;
7985}
7986
7987/*
7988 * "max()" function
7989 */
7990 static void
7991f_max(typval_T *argvars, typval_T *rettv)
7992{
7993 max_min(argvars, rettv, TRUE);
7994}
7995
7996/*
7997 * "min()" function
7998 */
7999 static void
8000f_min(typval_T *argvars, typval_T *rettv)
8001{
8002 max_min(argvars, rettv, FALSE);
8003}
8004
8005static int mkdir_recurse(char_u *dir, int prot);
8006
8007/*
8008 * Create the directory in which "dir" is located, and higher levels when
8009 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008010 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008011 */
8012 static int
8013mkdir_recurse(char_u *dir, int prot)
8014{
8015 char_u *p;
8016 char_u *updir;
8017 int r = FAIL;
8018
8019 /* Get end of directory name in "dir".
8020 * We're done when it's "/" or "c:/". */
8021 p = gettail_sep(dir);
8022 if (p <= get_past_head(dir))
8023 return OK;
8024
8025 /* If the directory exists we're done. Otherwise: create it.*/
8026 updir = vim_strnsave(dir, (int)(p - dir));
8027 if (updir == NULL)
8028 return FAIL;
8029 if (mch_isdir(updir))
8030 r = OK;
8031 else if (mkdir_recurse(updir, prot) == OK)
8032 r = vim_mkdir_emsg(updir, prot);
8033 vim_free(updir);
8034 return r;
8035}
8036
8037#ifdef vim_mkdir
8038/*
8039 * "mkdir()" function
8040 */
8041 static void
8042f_mkdir(typval_T *argvars, typval_T *rettv)
8043{
8044 char_u *dir;
8045 char_u buf[NUMBUFLEN];
8046 int prot = 0755;
8047
8048 rettv->vval.v_number = FAIL;
8049 if (check_restricted() || check_secure())
8050 return;
8051
8052 dir = get_tv_string_buf(&argvars[0], buf);
8053 if (*dir == NUL)
8054 rettv->vval.v_number = FAIL;
8055 else
8056 {
8057 if (*gettail(dir) == NUL)
8058 /* remove trailing slashes */
8059 *gettail_sep(dir) = NUL;
8060
8061 if (argvars[1].v_type != VAR_UNKNOWN)
8062 {
8063 if (argvars[2].v_type != VAR_UNKNOWN)
8064 prot = (int)get_tv_number_chk(&argvars[2], NULL);
8065 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
8066 mkdir_recurse(dir, prot);
8067 }
8068 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
8069 }
8070}
8071#endif
8072
8073/*
8074 * "mode()" function
8075 */
8076 static void
8077f_mode(typval_T *argvars, typval_T *rettv)
8078{
8079 char_u buf[3];
8080
8081 buf[1] = NUL;
8082 buf[2] = NUL;
8083
8084 if (time_for_testing == 93784)
8085 {
8086 /* Testing the two-character code. */
8087 buf[0] = 'x';
8088 buf[1] = '!';
8089 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008090#ifdef FEAT_TERMINAL
8091 else if (term_use_loop())
8092 buf[0] = 't';
8093#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008094 else if (VIsual_active)
8095 {
8096 if (VIsual_select)
8097 buf[0] = VIsual_mode + 's' - 'v';
8098 else
8099 buf[0] = VIsual_mode;
8100 }
8101 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8102 || State == CONFIRM)
8103 {
8104 buf[0] = 'r';
8105 if (State == ASKMORE)
8106 buf[1] = 'm';
8107 else if (State == CONFIRM)
8108 buf[1] = '?';
8109 }
8110 else if (State == EXTERNCMD)
8111 buf[0] = '!';
8112 else if (State & INSERT)
8113 {
8114#ifdef FEAT_VREPLACE
8115 if (State & VREPLACE_FLAG)
8116 {
8117 buf[0] = 'R';
8118 buf[1] = 'v';
8119 }
8120 else
8121#endif
Bram Moolenaare90858d2017-02-01 17:24:34 +01008122 {
8123 if (State & REPLACE_FLAG)
8124 buf[0] = 'R';
8125 else
8126 buf[0] = 'i';
8127#ifdef FEAT_INS_EXPAND
8128 if (ins_compl_active())
8129 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008130 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008131 buf[1] = 'x';
8132#endif
8133 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008134 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008135 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008136 {
8137 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008138 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008139 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008140 else if (exmode_active == EXMODE_NORMAL)
8141 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008142 }
8143 else
8144 {
8145 buf[0] = 'n';
8146 if (finish_op)
8147 buf[1] = 'o';
8148 }
8149
8150 /* Clear out the minor mode when the argument is not a non-zero number or
8151 * non-empty string. */
8152 if (!non_zero_arg(&argvars[0]))
8153 buf[1] = NUL;
8154
8155 rettv->vval.v_string = vim_strsave(buf);
8156 rettv->v_type = VAR_STRING;
8157}
8158
8159#if defined(FEAT_MZSCHEME) || defined(PROTO)
8160/*
8161 * "mzeval()" function
8162 */
8163 static void
8164f_mzeval(typval_T *argvars, typval_T *rettv)
8165{
8166 char_u *str;
8167 char_u buf[NUMBUFLEN];
8168
8169 str = get_tv_string_buf(&argvars[0], buf);
8170 do_mzeval(str, rettv);
8171}
8172
8173 void
8174mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8175{
8176 typval_T argvars[3];
8177
8178 argvars[0].v_type = VAR_STRING;
8179 argvars[0].vval.v_string = name;
8180 copy_tv(args, &argvars[1]);
8181 argvars[2].v_type = VAR_UNKNOWN;
8182 f_call(argvars, rettv);
8183 clear_tv(&argvars[1]);
8184}
8185#endif
8186
8187/*
8188 * "nextnonblank()" function
8189 */
8190 static void
8191f_nextnonblank(typval_T *argvars, typval_T *rettv)
8192{
8193 linenr_T lnum;
8194
8195 for (lnum = get_tv_lnum(argvars); ; ++lnum)
8196 {
8197 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8198 {
8199 lnum = 0;
8200 break;
8201 }
8202 if (*skipwhite(ml_get(lnum)) != NUL)
8203 break;
8204 }
8205 rettv->vval.v_number = lnum;
8206}
8207
8208/*
8209 * "nr2char()" function
8210 */
8211 static void
8212f_nr2char(typval_T *argvars, typval_T *rettv)
8213{
8214 char_u buf[NUMBUFLEN];
8215
8216#ifdef FEAT_MBYTE
8217 if (has_mbyte)
8218 {
8219 int utf8 = 0;
8220
8221 if (argvars[1].v_type != VAR_UNKNOWN)
8222 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
8223 if (utf8)
8224 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8225 else
8226 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8227 }
8228 else
8229#endif
8230 {
8231 buf[0] = (char_u)get_tv_number(&argvars[0]);
8232 buf[1] = NUL;
8233 }
8234 rettv->v_type = VAR_STRING;
8235 rettv->vval.v_string = vim_strsave(buf);
8236}
8237
8238/*
8239 * "or(expr, expr)" function
8240 */
8241 static void
8242f_or(typval_T *argvars, typval_T *rettv)
8243{
8244 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8245 | get_tv_number_chk(&argvars[1], NULL);
8246}
8247
8248/*
8249 * "pathshorten()" function
8250 */
8251 static void
8252f_pathshorten(typval_T *argvars, typval_T *rettv)
8253{
8254 char_u *p;
8255
8256 rettv->v_type = VAR_STRING;
8257 p = get_tv_string_chk(&argvars[0]);
8258 if (p == NULL)
8259 rettv->vval.v_string = NULL;
8260 else
8261 {
8262 p = vim_strsave(p);
8263 rettv->vval.v_string = p;
8264 if (p != NULL)
8265 shorten_dir(p);
8266 }
8267}
8268
8269#ifdef FEAT_PERL
8270/*
8271 * "perleval()" function
8272 */
8273 static void
8274f_perleval(typval_T *argvars, typval_T *rettv)
8275{
8276 char_u *str;
8277 char_u buf[NUMBUFLEN];
8278
8279 str = get_tv_string_buf(&argvars[0], buf);
8280 do_perleval(str, rettv);
8281}
8282#endif
8283
8284#ifdef FEAT_FLOAT
8285/*
8286 * "pow()" function
8287 */
8288 static void
8289f_pow(typval_T *argvars, typval_T *rettv)
8290{
8291 float_T fx = 0.0, fy = 0.0;
8292
8293 rettv->v_type = VAR_FLOAT;
8294 if (get_float_arg(argvars, &fx) == OK
8295 && get_float_arg(&argvars[1], &fy) == OK)
8296 rettv->vval.v_float = pow(fx, fy);
8297 else
8298 rettv->vval.v_float = 0.0;
8299}
8300#endif
8301
8302/*
8303 * "prevnonblank()" function
8304 */
8305 static void
8306f_prevnonblank(typval_T *argvars, typval_T *rettv)
8307{
8308 linenr_T lnum;
8309
8310 lnum = get_tv_lnum(argvars);
8311 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8312 lnum = 0;
8313 else
8314 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8315 --lnum;
8316 rettv->vval.v_number = lnum;
8317}
8318
8319/* This dummy va_list is here because:
8320 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8321 * - locally in the function results in a "used before set" warning
8322 * - using va_start() to initialize it gives "function with fixed args" error */
8323static va_list ap;
8324
8325/*
8326 * "printf()" function
8327 */
8328 static void
8329f_printf(typval_T *argvars, typval_T *rettv)
8330{
8331 char_u buf[NUMBUFLEN];
8332 int len;
8333 char_u *s;
8334 int saved_did_emsg = did_emsg;
8335 char *fmt;
8336
8337 rettv->v_type = VAR_STRING;
8338 rettv->vval.v_string = NULL;
8339
8340 /* Get the required length, allocate the buffer and do it for real. */
8341 did_emsg = FALSE;
8342 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008343 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008344 if (!did_emsg)
8345 {
8346 s = alloc(len + 1);
8347 if (s != NULL)
8348 {
8349 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008350 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8351 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008352 }
8353 }
8354 did_emsg |= saved_did_emsg;
8355}
8356
8357/*
8358 * "pumvisible()" function
8359 */
8360 static void
8361f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8362{
8363#ifdef FEAT_INS_EXPAND
8364 if (pum_visible())
8365 rettv->vval.v_number = 1;
8366#endif
8367}
8368
8369#ifdef FEAT_PYTHON3
8370/*
8371 * "py3eval()" function
8372 */
8373 static void
8374f_py3eval(typval_T *argvars, typval_T *rettv)
8375{
8376 char_u *str;
8377 char_u buf[NUMBUFLEN];
8378
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008379 if (p_pyx == 0)
8380 p_pyx = 3;
8381
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008382 str = get_tv_string_buf(&argvars[0], buf);
8383 do_py3eval(str, rettv);
8384}
8385#endif
8386
8387#ifdef FEAT_PYTHON
8388/*
8389 * "pyeval()" function
8390 */
8391 static void
8392f_pyeval(typval_T *argvars, typval_T *rettv)
8393{
8394 char_u *str;
8395 char_u buf[NUMBUFLEN];
8396
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008397 if (p_pyx == 0)
8398 p_pyx = 2;
8399
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008400 str = get_tv_string_buf(&argvars[0], buf);
8401 do_pyeval(str, rettv);
8402}
8403#endif
8404
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008405#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8406/*
8407 * "pyxeval()" function
8408 */
8409 static void
8410f_pyxeval(typval_T *argvars, typval_T *rettv)
8411{
8412# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8413 init_pyxversion();
8414 if (p_pyx == 2)
8415 f_pyeval(argvars, rettv);
8416 else
8417 f_py3eval(argvars, rettv);
8418# elif defined(FEAT_PYTHON)
8419 f_pyeval(argvars, rettv);
8420# elif defined(FEAT_PYTHON3)
8421 f_py3eval(argvars, rettv);
8422# endif
8423}
8424#endif
8425
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008426/*
8427 * "range()" function
8428 */
8429 static void
8430f_range(typval_T *argvars, typval_T *rettv)
8431{
8432 varnumber_T start;
8433 varnumber_T end;
8434 varnumber_T stride = 1;
8435 varnumber_T i;
8436 int error = FALSE;
8437
8438 start = get_tv_number_chk(&argvars[0], &error);
8439 if (argvars[1].v_type == VAR_UNKNOWN)
8440 {
8441 end = start - 1;
8442 start = 0;
8443 }
8444 else
8445 {
8446 end = get_tv_number_chk(&argvars[1], &error);
8447 if (argvars[2].v_type != VAR_UNKNOWN)
8448 stride = get_tv_number_chk(&argvars[2], &error);
8449 }
8450
8451 if (error)
8452 return; /* type error; errmsg already given */
8453 if (stride == 0)
8454 EMSG(_("E726: Stride is zero"));
8455 else if (stride > 0 ? end + 1 < start : end - 1 > start)
8456 EMSG(_("E727: Start past end"));
8457 else
8458 {
8459 if (rettv_list_alloc(rettv) == OK)
8460 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8461 if (list_append_number(rettv->vval.v_list,
8462 (varnumber_T)i) == FAIL)
8463 break;
8464 }
8465}
8466
8467/*
8468 * "readfile()" function
8469 */
8470 static void
8471f_readfile(typval_T *argvars, typval_T *rettv)
8472{
8473 int binary = FALSE;
8474 int failed = FALSE;
8475 char_u *fname;
8476 FILE *fd;
8477 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8478 int io_size = sizeof(buf);
8479 int readlen; /* size of last fread() */
8480 char_u *prev = NULL; /* previously read bytes, if any */
8481 long prevlen = 0; /* length of data in prev */
8482 long prevsize = 0; /* size of prev buffer */
8483 long maxline = MAXLNUM;
8484 long cnt = 0;
8485 char_u *p; /* position in buf */
8486 char_u *start; /* start of current line */
8487
8488 if (argvars[1].v_type != VAR_UNKNOWN)
8489 {
8490 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
8491 binary = TRUE;
8492 if (argvars[2].v_type != VAR_UNKNOWN)
8493 maxline = (long)get_tv_number(&argvars[2]);
8494 }
8495
8496 if (rettv_list_alloc(rettv) == FAIL)
8497 return;
8498
8499 /* Always open the file in binary mode, library functions have a mind of
8500 * their own about CR-LF conversion. */
8501 fname = get_tv_string(&argvars[0]);
8502 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8503 {
8504 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
8505 return;
8506 }
8507
8508 while (cnt < maxline || maxline < 0)
8509 {
8510 readlen = (int)fread(buf, 1, io_size, fd);
8511
8512 /* This for loop processes what was read, but is also entered at end
8513 * of file so that either:
8514 * - an incomplete line gets written
8515 * - a "binary" file gets an empty line at the end if it ends in a
8516 * newline. */
8517 for (p = buf, start = buf;
8518 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8519 ++p)
8520 {
8521 if (*p == '\n' || readlen <= 0)
8522 {
8523 listitem_T *li;
8524 char_u *s = NULL;
8525 long_u len = p - start;
8526
8527 /* Finished a line. Remove CRs before NL. */
8528 if (readlen > 0 && !binary)
8529 {
8530 while (len > 0 && start[len - 1] == '\r')
8531 --len;
8532 /* removal may cross back to the "prev" string */
8533 if (len == 0)
8534 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8535 --prevlen;
8536 }
8537 if (prevlen == 0)
8538 s = vim_strnsave(start, (int)len);
8539 else
8540 {
8541 /* Change "prev" buffer to be the right size. This way
8542 * the bytes are only copied once, and very long lines are
8543 * allocated only once. */
8544 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8545 {
8546 mch_memmove(s + prevlen, start, len);
8547 s[prevlen + len] = NUL;
8548 prev = NULL; /* the list will own the string */
8549 prevlen = prevsize = 0;
8550 }
8551 }
8552 if (s == NULL)
8553 {
8554 do_outofmem_msg((long_u) prevlen + len + 1);
8555 failed = TRUE;
8556 break;
8557 }
8558
8559 if ((li = listitem_alloc()) == NULL)
8560 {
8561 vim_free(s);
8562 failed = TRUE;
8563 break;
8564 }
8565 li->li_tv.v_type = VAR_STRING;
8566 li->li_tv.v_lock = 0;
8567 li->li_tv.vval.v_string = s;
8568 list_append(rettv->vval.v_list, li);
8569
8570 start = p + 1; /* step over newline */
8571 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
8572 break;
8573 }
8574 else if (*p == NUL)
8575 *p = '\n';
8576#ifdef FEAT_MBYTE
8577 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
8578 * when finding the BF and check the previous two bytes. */
8579 else if (*p == 0xbf && enc_utf8 && !binary)
8580 {
8581 /* Find the two bytes before the 0xbf. If p is at buf, or buf
8582 * + 1, these may be in the "prev" string. */
8583 char_u back1 = p >= buf + 1 ? p[-1]
8584 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
8585 char_u back2 = p >= buf + 2 ? p[-2]
8586 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
8587 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
8588
8589 if (back2 == 0xef && back1 == 0xbb)
8590 {
8591 char_u *dest = p - 2;
8592
8593 /* Usually a BOM is at the beginning of a file, and so at
8594 * the beginning of a line; then we can just step over it.
8595 */
8596 if (start == dest)
8597 start = p + 1;
8598 else
8599 {
8600 /* have to shuffle buf to close gap */
8601 int adjust_prevlen = 0;
8602
8603 if (dest < buf)
8604 {
8605 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
8606 dest = buf;
8607 }
8608 if (readlen > p - buf + 1)
8609 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
8610 readlen -= 3 - adjust_prevlen;
8611 prevlen -= adjust_prevlen;
8612 p = dest - 1;
8613 }
8614 }
8615 }
8616#endif
8617 } /* for */
8618
8619 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
8620 break;
8621 if (start < p)
8622 {
8623 /* There's part of a line in buf, store it in "prev". */
8624 if (p - start + prevlen >= prevsize)
8625 {
8626 /* need bigger "prev" buffer */
8627 char_u *newprev;
8628
8629 /* A common use case is ordinary text files and "prev" gets a
8630 * fragment of a line, so the first allocation is made
8631 * small, to avoid repeatedly 'allocing' large and
8632 * 'reallocing' small. */
8633 if (prevsize == 0)
8634 prevsize = (long)(p - start);
8635 else
8636 {
8637 long grow50pc = (prevsize * 3) / 2;
8638 long growmin = (long)((p - start) * 2 + prevlen);
8639 prevsize = grow50pc > growmin ? grow50pc : growmin;
8640 }
8641 newprev = prev == NULL ? alloc(prevsize)
8642 : vim_realloc(prev, prevsize);
8643 if (newprev == NULL)
8644 {
8645 do_outofmem_msg((long_u)prevsize);
8646 failed = TRUE;
8647 break;
8648 }
8649 prev = newprev;
8650 }
8651 /* Add the line part to end of "prev". */
8652 mch_memmove(prev + prevlen, start, p - start);
8653 prevlen += (long)(p - start);
8654 }
8655 } /* while */
8656
8657 /*
8658 * For a negative line count use only the lines at the end of the file,
8659 * free the rest.
8660 */
8661 if (!failed && maxline < 0)
8662 while (cnt > -maxline)
8663 {
8664 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
8665 --cnt;
8666 }
8667
8668 if (failed)
8669 {
8670 list_free(rettv->vval.v_list);
8671 /* readfile doc says an empty list is returned on error */
8672 rettv->vval.v_list = list_alloc();
8673 }
8674
8675 vim_free(prev);
8676 fclose(fd);
8677}
8678
8679#if defined(FEAT_RELTIME)
8680static int list2proftime(typval_T *arg, proftime_T *tm);
8681
8682/*
8683 * Convert a List to proftime_T.
8684 * Return FAIL when there is something wrong.
8685 */
8686 static int
8687list2proftime(typval_T *arg, proftime_T *tm)
8688{
8689 long n1, n2;
8690 int error = FALSE;
8691
8692 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
8693 || arg->vval.v_list->lv_len != 2)
8694 return FAIL;
8695 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
8696 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
8697# ifdef WIN3264
8698 tm->HighPart = n1;
8699 tm->LowPart = n2;
8700# else
8701 tm->tv_sec = n1;
8702 tm->tv_usec = n2;
8703# endif
8704 return error ? FAIL : OK;
8705}
8706#endif /* FEAT_RELTIME */
8707
8708/*
8709 * "reltime()" function
8710 */
8711 static void
8712f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8713{
8714#ifdef FEAT_RELTIME
8715 proftime_T res;
8716 proftime_T start;
8717
8718 if (argvars[0].v_type == VAR_UNKNOWN)
8719 {
8720 /* No arguments: get current time. */
8721 profile_start(&res);
8722 }
8723 else if (argvars[1].v_type == VAR_UNKNOWN)
8724 {
8725 if (list2proftime(&argvars[0], &res) == FAIL)
8726 return;
8727 profile_end(&res);
8728 }
8729 else
8730 {
8731 /* Two arguments: compute the difference. */
8732 if (list2proftime(&argvars[0], &start) == FAIL
8733 || list2proftime(&argvars[1], &res) == FAIL)
8734 return;
8735 profile_sub(&res, &start);
8736 }
8737
8738 if (rettv_list_alloc(rettv) == OK)
8739 {
8740 long n1, n2;
8741
8742# ifdef WIN3264
8743 n1 = res.HighPart;
8744 n2 = res.LowPart;
8745# else
8746 n1 = res.tv_sec;
8747 n2 = res.tv_usec;
8748# endif
8749 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
8750 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
8751 }
8752#endif
8753}
8754
8755#ifdef FEAT_FLOAT
8756/*
8757 * "reltimefloat()" function
8758 */
8759 static void
8760f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
8761{
8762# ifdef FEAT_RELTIME
8763 proftime_T tm;
8764# endif
8765
8766 rettv->v_type = VAR_FLOAT;
8767 rettv->vval.v_float = 0;
8768# ifdef FEAT_RELTIME
8769 if (list2proftime(&argvars[0], &tm) == OK)
8770 rettv->vval.v_float = profile_float(&tm);
8771# endif
8772}
8773#endif
8774
8775/*
8776 * "reltimestr()" function
8777 */
8778 static void
8779f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
8780{
8781#ifdef FEAT_RELTIME
8782 proftime_T tm;
8783#endif
8784
8785 rettv->v_type = VAR_STRING;
8786 rettv->vval.v_string = NULL;
8787#ifdef FEAT_RELTIME
8788 if (list2proftime(&argvars[0], &tm) == OK)
8789 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
8790#endif
8791}
8792
8793#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
8794static void make_connection(void);
8795static int check_connection(void);
8796
8797 static void
8798make_connection(void)
8799{
8800 if (X_DISPLAY == NULL
8801# ifdef FEAT_GUI
8802 && !gui.in_use
8803# endif
8804 )
8805 {
8806 x_force_connect = TRUE;
8807 setup_term_clip();
8808 x_force_connect = FALSE;
8809 }
8810}
8811
8812 static int
8813check_connection(void)
8814{
8815 make_connection();
8816 if (X_DISPLAY == NULL)
8817 {
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01008818 EMSG(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008819 return FAIL;
8820 }
8821 return OK;
8822}
8823#endif
8824
8825#ifdef FEAT_CLIENTSERVER
8826 static void
8827remote_common(typval_T *argvars, typval_T *rettv, int expr)
8828{
8829 char_u *server_name;
8830 char_u *keys;
8831 char_u *r = NULL;
8832 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008833 int timeout = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008834# ifdef WIN32
8835 HWND w;
8836# else
8837 Window w;
8838# endif
8839
8840 if (check_restricted() || check_secure())
8841 return;
8842
8843# ifdef FEAT_X11
8844 if (check_connection() == FAIL)
8845 return;
8846# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008847 if (argvars[2].v_type != VAR_UNKNOWN
8848 && argvars[3].v_type != VAR_UNKNOWN)
8849 timeout = get_tv_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008850
8851 server_name = get_tv_string_chk(&argvars[0]);
8852 if (server_name == NULL)
8853 return; /* type error; errmsg already given */
8854 keys = get_tv_string_buf(&argvars[1], buf);
8855# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008856 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008857# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008858 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
8859 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008860# endif
8861 {
8862 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008863 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008864 EMSG(r); /* sending worked but evaluation failed */
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008865 vim_free(r);
8866 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008867 else
8868 EMSG2(_("E241: Unable to send to %s"), server_name);
8869 return;
8870 }
8871
8872 rettv->vval.v_string = r;
8873
8874 if (argvars[2].v_type != VAR_UNKNOWN)
8875 {
8876 dictitem_T v;
8877 char_u str[30];
8878 char_u *idvar;
8879
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008880 idvar = get_tv_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008881 if (idvar != NULL && *idvar != NUL)
8882 {
8883 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
8884 v.di_tv.v_type = VAR_STRING;
8885 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008886 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008887 vim_free(v.di_tv.vval.v_string);
8888 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008889 }
8890}
8891#endif
8892
8893/*
8894 * "remote_expr()" function
8895 */
8896 static void
8897f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
8898{
8899 rettv->v_type = VAR_STRING;
8900 rettv->vval.v_string = NULL;
8901#ifdef FEAT_CLIENTSERVER
8902 remote_common(argvars, rettv, TRUE);
8903#endif
8904}
8905
8906/*
8907 * "remote_foreground()" function
8908 */
8909 static void
8910f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8911{
8912#ifdef FEAT_CLIENTSERVER
8913# ifdef WIN32
8914 /* On Win32 it's done in this application. */
8915 {
8916 char_u *server_name = get_tv_string_chk(&argvars[0]);
8917
8918 if (server_name != NULL)
8919 serverForeground(server_name);
8920 }
8921# else
8922 /* Send a foreground() expression to the server. */
8923 argvars[1].v_type = VAR_STRING;
8924 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
8925 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008926 rettv->v_type = VAR_STRING;
8927 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008928 remote_common(argvars, rettv, TRUE);
8929 vim_free(argvars[1].vval.v_string);
8930# endif
8931#endif
8932}
8933
8934 static void
8935f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
8936{
8937#ifdef FEAT_CLIENTSERVER
8938 dictitem_T v;
8939 char_u *s = NULL;
8940# ifdef WIN32
8941 long_u n = 0;
8942# endif
8943 char_u *serverid;
8944
8945 if (check_restricted() || check_secure())
8946 {
8947 rettv->vval.v_number = -1;
8948 return;
8949 }
8950 serverid = get_tv_string_chk(&argvars[0]);
8951 if (serverid == NULL)
8952 {
8953 rettv->vval.v_number = -1;
8954 return; /* type error; errmsg already given */
8955 }
8956# ifdef WIN32
8957 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
8958 if (n == 0)
8959 rettv->vval.v_number = -1;
8960 else
8961 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008962 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008963 rettv->vval.v_number = (s != NULL);
8964 }
8965# else
8966 if (check_connection() == FAIL)
8967 return;
8968
8969 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
8970 serverStrToWin(serverid), &s);
8971# endif
8972
8973 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
8974 {
8975 char_u *retvar;
8976
8977 v.di_tv.v_type = VAR_STRING;
8978 v.di_tv.vval.v_string = vim_strsave(s);
8979 retvar = get_tv_string_chk(&argvars[1]);
8980 if (retvar != NULL)
8981 set_var(retvar, &v.di_tv, FALSE);
8982 vim_free(v.di_tv.vval.v_string);
8983 }
8984#else
8985 rettv->vval.v_number = -1;
8986#endif
8987}
8988
8989 static void
8990f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
8991{
8992 char_u *r = NULL;
8993
8994#ifdef FEAT_CLIENTSERVER
8995 char_u *serverid = get_tv_string_chk(&argvars[0]);
8996
8997 if (serverid != NULL && !check_restricted() && !check_secure())
8998 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008999 int timeout = 0;
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009000# ifdef WIN32
9001 /* The server's HWND is encoded in the 'id' parameter */
9002 long_u n = 0;
9003# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009004
9005 if (argvars[1].v_type != VAR_UNKNOWN)
9006 timeout = get_tv_number(&argvars[1]);
9007
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009008# ifdef WIN32
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009009 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9010 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009011 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009012 if (r == NULL)
9013# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009014 if (check_connection() == FAIL
9015 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9016 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009017# endif
9018 EMSG(_("E277: Unable to read a server reply"));
9019 }
9020#endif
9021 rettv->v_type = VAR_STRING;
9022 rettv->vval.v_string = r;
9023}
9024
9025/*
9026 * "remote_send()" function
9027 */
9028 static void
9029f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9030{
9031 rettv->v_type = VAR_STRING;
9032 rettv->vval.v_string = NULL;
9033#ifdef FEAT_CLIENTSERVER
9034 remote_common(argvars, rettv, FALSE);
9035#endif
9036}
9037
9038/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009039 * "remote_startserver()" function
9040 */
9041 static void
9042f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9043{
9044#ifdef FEAT_CLIENTSERVER
9045 char_u *server = get_tv_string_chk(&argvars[0]);
9046
9047 if (server == NULL)
9048 return; /* type error; errmsg already given */
9049 if (serverName != NULL)
9050 EMSG(_("E941: already started a server"));
9051 else
9052 {
9053# ifdef FEAT_X11
9054 if (check_connection() == OK)
9055 serverRegisterName(X_DISPLAY, server);
9056# else
9057 serverSetName(server);
9058# endif
9059 }
9060#else
9061 EMSG(_("E942: +clientserver feature not available"));
9062#endif
9063}
9064
9065/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009066 * "remove()" function
9067 */
9068 static void
9069f_remove(typval_T *argvars, typval_T *rettv)
9070{
9071 list_T *l;
9072 listitem_T *item, *item2;
9073 listitem_T *li;
9074 long idx;
9075 long end;
9076 char_u *key;
9077 dict_T *d;
9078 dictitem_T *di;
9079 char_u *arg_errmsg = (char_u *)N_("remove() argument");
9080
9081 if (argvars[0].v_type == VAR_DICT)
9082 {
9083 if (argvars[2].v_type != VAR_UNKNOWN)
9084 EMSG2(_(e_toomanyarg), "remove()");
9085 else if ((d = argvars[0].vval.v_dict) != NULL
9086 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
9087 {
9088 key = get_tv_string_chk(&argvars[1]);
9089 if (key != NULL)
9090 {
9091 di = dict_find(d, key, -1);
9092 if (di == NULL)
9093 EMSG2(_(e_dictkey), key);
9094 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
9095 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
9096 {
9097 *rettv = di->di_tv;
9098 init_tv(&di->di_tv);
9099 dictitem_remove(d, di);
9100 }
9101 }
9102 }
9103 }
9104 else if (argvars[0].v_type != VAR_LIST)
9105 EMSG2(_(e_listdictarg), "remove()");
9106 else if ((l = argvars[0].vval.v_list) != NULL
9107 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
9108 {
9109 int error = FALSE;
9110
9111 idx = (long)get_tv_number_chk(&argvars[1], &error);
9112 if (error)
9113 ; /* type error: do nothing, errmsg already given */
9114 else if ((item = list_find(l, idx)) == NULL)
9115 EMSGN(_(e_listidx), idx);
9116 else
9117 {
9118 if (argvars[2].v_type == VAR_UNKNOWN)
9119 {
9120 /* Remove one item, return its value. */
9121 vimlist_remove(l, item, item);
9122 *rettv = item->li_tv;
9123 vim_free(item);
9124 }
9125 else
9126 {
9127 /* Remove range of items, return list with values. */
9128 end = (long)get_tv_number_chk(&argvars[2], &error);
9129 if (error)
9130 ; /* type error: do nothing */
9131 else if ((item2 = list_find(l, end)) == NULL)
9132 EMSGN(_(e_listidx), end);
9133 else
9134 {
9135 int cnt = 0;
9136
9137 for (li = item; li != NULL; li = li->li_next)
9138 {
9139 ++cnt;
9140 if (li == item2)
9141 break;
9142 }
9143 if (li == NULL) /* didn't find "item2" after "item" */
9144 EMSG(_(e_invrange));
9145 else
9146 {
9147 vimlist_remove(l, item, item2);
9148 if (rettv_list_alloc(rettv) == OK)
9149 {
9150 l = rettv->vval.v_list;
9151 l->lv_first = item;
9152 l->lv_last = item2;
9153 item->li_prev = NULL;
9154 item2->li_next = NULL;
9155 l->lv_len = cnt;
9156 }
9157 }
9158 }
9159 }
9160 }
9161 }
9162}
9163
9164/*
9165 * "rename({from}, {to})" function
9166 */
9167 static void
9168f_rename(typval_T *argvars, typval_T *rettv)
9169{
9170 char_u buf[NUMBUFLEN];
9171
9172 if (check_restricted() || check_secure())
9173 rettv->vval.v_number = -1;
9174 else
9175 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
9176 get_tv_string_buf(&argvars[1], buf));
9177}
9178
9179/*
9180 * "repeat()" function
9181 */
9182 static void
9183f_repeat(typval_T *argvars, typval_T *rettv)
9184{
9185 char_u *p;
9186 int n;
9187 int slen;
9188 int len;
9189 char_u *r;
9190 int i;
9191
9192 n = (int)get_tv_number(&argvars[1]);
9193 if (argvars[0].v_type == VAR_LIST)
9194 {
9195 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9196 while (n-- > 0)
9197 if (list_extend(rettv->vval.v_list,
9198 argvars[0].vval.v_list, NULL) == FAIL)
9199 break;
9200 }
9201 else
9202 {
9203 p = get_tv_string(&argvars[0]);
9204 rettv->v_type = VAR_STRING;
9205 rettv->vval.v_string = NULL;
9206
9207 slen = (int)STRLEN(p);
9208 len = slen * n;
9209 if (len <= 0)
9210 return;
9211
9212 r = alloc(len + 1);
9213 if (r != NULL)
9214 {
9215 for (i = 0; i < n; i++)
9216 mch_memmove(r + i * slen, p, (size_t)slen);
9217 r[len] = NUL;
9218 }
9219
9220 rettv->vval.v_string = r;
9221 }
9222}
9223
9224/*
9225 * "resolve()" function
9226 */
9227 static void
9228f_resolve(typval_T *argvars, typval_T *rettv)
9229{
9230 char_u *p;
9231#ifdef HAVE_READLINK
9232 char_u *buf = NULL;
9233#endif
9234
9235 p = get_tv_string(&argvars[0]);
9236#ifdef FEAT_SHORTCUT
9237 {
9238 char_u *v = NULL;
9239
9240 v = mch_resolve_shortcut(p);
9241 if (v != NULL)
9242 rettv->vval.v_string = v;
9243 else
9244 rettv->vval.v_string = vim_strsave(p);
9245 }
9246#else
9247# ifdef HAVE_READLINK
9248 {
9249 char_u *cpy;
9250 int len;
9251 char_u *remain = NULL;
9252 char_u *q;
9253 int is_relative_to_current = FALSE;
9254 int has_trailing_pathsep = FALSE;
9255 int limit = 100;
9256
9257 p = vim_strsave(p);
9258
9259 if (p[0] == '.' && (vim_ispathsep(p[1])
9260 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9261 is_relative_to_current = TRUE;
9262
9263 len = STRLEN(p);
9264 if (len > 0 && after_pathsep(p, p + len))
9265 {
9266 has_trailing_pathsep = TRUE;
9267 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9268 }
9269
9270 q = getnextcomp(p);
9271 if (*q != NUL)
9272 {
9273 /* Separate the first path component in "p", and keep the
9274 * remainder (beginning with the path separator). */
9275 remain = vim_strsave(q - 1);
9276 q[-1] = NUL;
9277 }
9278
9279 buf = alloc(MAXPATHL + 1);
9280 if (buf == NULL)
9281 goto fail;
9282
9283 for (;;)
9284 {
9285 for (;;)
9286 {
9287 len = readlink((char *)p, (char *)buf, MAXPATHL);
9288 if (len <= 0)
9289 break;
9290 buf[len] = NUL;
9291
9292 if (limit-- == 0)
9293 {
9294 vim_free(p);
9295 vim_free(remain);
9296 EMSG(_("E655: Too many symbolic links (cycle?)"));
9297 rettv->vval.v_string = NULL;
9298 goto fail;
9299 }
9300
9301 /* Ensure that the result will have a trailing path separator
9302 * if the argument has one. */
9303 if (remain == NULL && has_trailing_pathsep)
9304 add_pathsep(buf);
9305
9306 /* Separate the first path component in the link value and
9307 * concatenate the remainders. */
9308 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9309 if (*q != NUL)
9310 {
9311 if (remain == NULL)
9312 remain = vim_strsave(q - 1);
9313 else
9314 {
9315 cpy = concat_str(q - 1, remain);
9316 if (cpy != NULL)
9317 {
9318 vim_free(remain);
9319 remain = cpy;
9320 }
9321 }
9322 q[-1] = NUL;
9323 }
9324
9325 q = gettail(p);
9326 if (q > p && *q == NUL)
9327 {
9328 /* Ignore trailing path separator. */
9329 q[-1] = NUL;
9330 q = gettail(p);
9331 }
9332 if (q > p && !mch_isFullName(buf))
9333 {
9334 /* symlink is relative to directory of argument */
9335 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
9336 if (cpy != NULL)
9337 {
9338 STRCPY(cpy, p);
9339 STRCPY(gettail(cpy), buf);
9340 vim_free(p);
9341 p = cpy;
9342 }
9343 }
9344 else
9345 {
9346 vim_free(p);
9347 p = vim_strsave(buf);
9348 }
9349 }
9350
9351 if (remain == NULL)
9352 break;
9353
9354 /* Append the first path component of "remain" to "p". */
9355 q = getnextcomp(remain + 1);
9356 len = q - remain - (*q != NUL);
9357 cpy = vim_strnsave(p, STRLEN(p) + len);
9358 if (cpy != NULL)
9359 {
9360 STRNCAT(cpy, remain, len);
9361 vim_free(p);
9362 p = cpy;
9363 }
9364 /* Shorten "remain". */
9365 if (*q != NUL)
9366 STRMOVE(remain, q - 1);
9367 else
Bram Moolenaard23a8232018-02-10 18:45:26 +01009368 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009369 }
9370
9371 /* If the result is a relative path name, make it explicitly relative to
9372 * the current directory if and only if the argument had this form. */
9373 if (!vim_ispathsep(*p))
9374 {
9375 if (is_relative_to_current
9376 && *p != NUL
9377 && !(p[0] == '.'
9378 && (p[1] == NUL
9379 || vim_ispathsep(p[1])
9380 || (p[1] == '.'
9381 && (p[2] == NUL
9382 || vim_ispathsep(p[2]))))))
9383 {
9384 /* Prepend "./". */
9385 cpy = concat_str((char_u *)"./", p);
9386 if (cpy != NULL)
9387 {
9388 vim_free(p);
9389 p = cpy;
9390 }
9391 }
9392 else if (!is_relative_to_current)
9393 {
9394 /* Strip leading "./". */
9395 q = p;
9396 while (q[0] == '.' && vim_ispathsep(q[1]))
9397 q += 2;
9398 if (q > p)
9399 STRMOVE(p, p + 2);
9400 }
9401 }
9402
9403 /* Ensure that the result will have no trailing path separator
9404 * if the argument had none. But keep "/" or "//". */
9405 if (!has_trailing_pathsep)
9406 {
9407 q = p + STRLEN(p);
9408 if (after_pathsep(p, q))
9409 *gettail_sep(p) = NUL;
9410 }
9411
9412 rettv->vval.v_string = p;
9413 }
9414# else
9415 rettv->vval.v_string = vim_strsave(p);
9416# endif
9417#endif
9418
9419 simplify_filename(rettv->vval.v_string);
9420
9421#ifdef HAVE_READLINK
9422fail:
9423 vim_free(buf);
9424#endif
9425 rettv->v_type = VAR_STRING;
9426}
9427
9428/*
9429 * "reverse({list})" function
9430 */
9431 static void
9432f_reverse(typval_T *argvars, typval_T *rettv)
9433{
9434 list_T *l;
9435 listitem_T *li, *ni;
9436
9437 if (argvars[0].v_type != VAR_LIST)
9438 EMSG2(_(e_listarg), "reverse()");
9439 else if ((l = argvars[0].vval.v_list) != NULL
9440 && !tv_check_lock(l->lv_lock,
9441 (char_u *)N_("reverse() argument"), TRUE))
9442 {
9443 li = l->lv_last;
9444 l->lv_first = l->lv_last = NULL;
9445 l->lv_len = 0;
9446 while (li != NULL)
9447 {
9448 ni = li->li_prev;
9449 list_append(l, li);
9450 li = ni;
9451 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009452 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009453 l->lv_idx = l->lv_len - l->lv_idx - 1;
9454 }
9455}
9456
9457#define SP_NOMOVE 0x01 /* don't move cursor */
9458#define SP_REPEAT 0x02 /* repeat to find outer pair */
9459#define SP_RETCOUNT 0x04 /* return matchcount */
9460#define SP_SETPCMARK 0x08 /* set previous context mark */
9461#define SP_START 0x10 /* accept match at start position */
9462#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9463#define SP_END 0x40 /* leave cursor at end of match */
9464#define SP_COLUMN 0x80 /* start at cursor column */
9465
9466static int get_search_arg(typval_T *varp, int *flagsp);
9467
9468/*
9469 * Get flags for a search function.
9470 * Possibly sets "p_ws".
9471 * Returns BACKWARD, FORWARD or zero (for an error).
9472 */
9473 static int
9474get_search_arg(typval_T *varp, int *flagsp)
9475{
9476 int dir = FORWARD;
9477 char_u *flags;
9478 char_u nbuf[NUMBUFLEN];
9479 int mask;
9480
9481 if (varp->v_type != VAR_UNKNOWN)
9482 {
9483 flags = get_tv_string_buf_chk(varp, nbuf);
9484 if (flags == NULL)
9485 return 0; /* type error; errmsg already given */
9486 while (*flags != NUL)
9487 {
9488 switch (*flags)
9489 {
9490 case 'b': dir = BACKWARD; break;
9491 case 'w': p_ws = TRUE; break;
9492 case 'W': p_ws = FALSE; break;
9493 default: mask = 0;
9494 if (flagsp != NULL)
9495 switch (*flags)
9496 {
9497 case 'c': mask = SP_START; break;
9498 case 'e': mask = SP_END; break;
9499 case 'm': mask = SP_RETCOUNT; break;
9500 case 'n': mask = SP_NOMOVE; break;
9501 case 'p': mask = SP_SUBPAT; break;
9502 case 'r': mask = SP_REPEAT; break;
9503 case 's': mask = SP_SETPCMARK; break;
9504 case 'z': mask = SP_COLUMN; break;
9505 }
9506 if (mask == 0)
9507 {
9508 EMSG2(_(e_invarg2), flags);
9509 dir = 0;
9510 }
9511 else
9512 *flagsp |= mask;
9513 }
9514 if (dir == 0)
9515 break;
9516 ++flags;
9517 }
9518 }
9519 return dir;
9520}
9521
9522/*
9523 * Shared by search() and searchpos() functions.
9524 */
9525 static int
9526search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9527{
9528 int flags;
9529 char_u *pat;
9530 pos_T pos;
9531 pos_T save_cursor;
9532 int save_p_ws = p_ws;
9533 int dir;
9534 int retval = 0; /* default: FAIL */
9535 long lnum_stop = 0;
9536 proftime_T tm;
9537#ifdef FEAT_RELTIME
9538 long time_limit = 0;
9539#endif
9540 int options = SEARCH_KEEP;
9541 int subpatnum;
9542
9543 pat = get_tv_string(&argvars[0]);
9544 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
9545 if (dir == 0)
9546 goto theend;
9547 flags = *flagsp;
9548 if (flags & SP_START)
9549 options |= SEARCH_START;
9550 if (flags & SP_END)
9551 options |= SEARCH_END;
9552 if (flags & SP_COLUMN)
9553 options |= SEARCH_COL;
9554
9555 /* Optional arguments: line number to stop searching and timeout. */
9556 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9557 {
9558 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
9559 if (lnum_stop < 0)
9560 goto theend;
9561#ifdef FEAT_RELTIME
9562 if (argvars[3].v_type != VAR_UNKNOWN)
9563 {
9564 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
9565 if (time_limit < 0)
9566 goto theend;
9567 }
9568#endif
9569 }
9570
9571#ifdef FEAT_RELTIME
9572 /* Set the time limit, if there is one. */
9573 profile_setlimit(time_limit, &tm);
9574#endif
9575
9576 /*
9577 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
9578 * Check to make sure only those flags are set.
9579 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
9580 * flags cannot be set. Check for that condition also.
9581 */
9582 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
9583 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9584 {
9585 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
9586 goto theend;
9587 }
9588
9589 pos = save_cursor = curwin->w_cursor;
9590 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009591 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009592 if (subpatnum != FAIL)
9593 {
9594 if (flags & SP_SUBPAT)
9595 retval = subpatnum;
9596 else
9597 retval = pos.lnum;
9598 if (flags & SP_SETPCMARK)
9599 setpcmark();
9600 curwin->w_cursor = pos;
9601 if (match_pos != NULL)
9602 {
9603 /* Store the match cursor position */
9604 match_pos->lnum = pos.lnum;
9605 match_pos->col = pos.col + 1;
9606 }
9607 /* "/$" will put the cursor after the end of the line, may need to
9608 * correct that here */
9609 check_cursor();
9610 }
9611
9612 /* If 'n' flag is used: restore cursor position. */
9613 if (flags & SP_NOMOVE)
9614 curwin->w_cursor = save_cursor;
9615 else
9616 curwin->w_set_curswant = TRUE;
9617theend:
9618 p_ws = save_p_ws;
9619
9620 return retval;
9621}
9622
9623#ifdef FEAT_FLOAT
9624
9625/*
9626 * round() is not in C90, use ceil() or floor() instead.
9627 */
9628 float_T
9629vim_round(float_T f)
9630{
9631 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
9632}
9633
9634/*
9635 * "round({float})" function
9636 */
9637 static void
9638f_round(typval_T *argvars, typval_T *rettv)
9639{
9640 float_T f = 0.0;
9641
9642 rettv->v_type = VAR_FLOAT;
9643 if (get_float_arg(argvars, &f) == OK)
9644 rettv->vval.v_float = vim_round(f);
9645 else
9646 rettv->vval.v_float = 0.0;
9647}
9648#endif
9649
9650/*
9651 * "screenattr()" function
9652 */
9653 static void
9654f_screenattr(typval_T *argvars, typval_T *rettv)
9655{
9656 int row;
9657 int col;
9658 int c;
9659
9660 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9661 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9662 if (row < 0 || row >= screen_Rows
9663 || col < 0 || col >= screen_Columns)
9664 c = -1;
9665 else
9666 c = ScreenAttrs[LineOffset[row] + col];
9667 rettv->vval.v_number = c;
9668}
9669
9670/*
9671 * "screenchar()" function
9672 */
9673 static void
9674f_screenchar(typval_T *argvars, typval_T *rettv)
9675{
9676 int row;
9677 int col;
9678 int off;
9679 int c;
9680
9681 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9682 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9683 if (row < 0 || row >= screen_Rows
9684 || col < 0 || col >= screen_Columns)
9685 c = -1;
9686 else
9687 {
9688 off = LineOffset[row] + col;
9689#ifdef FEAT_MBYTE
9690 if (enc_utf8 && ScreenLinesUC[off] != 0)
9691 c = ScreenLinesUC[off];
9692 else
9693#endif
9694 c = ScreenLines[off];
9695 }
9696 rettv->vval.v_number = c;
9697}
9698
9699/*
9700 * "screencol()" function
9701 *
9702 * First column is 1 to be consistent with virtcol().
9703 */
9704 static void
9705f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
9706{
9707 rettv->vval.v_number = screen_screencol() + 1;
9708}
9709
9710/*
9711 * "screenrow()" function
9712 */
9713 static void
9714f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
9715{
9716 rettv->vval.v_number = screen_screenrow() + 1;
9717}
9718
9719/*
9720 * "search()" function
9721 */
9722 static void
9723f_search(typval_T *argvars, typval_T *rettv)
9724{
9725 int flags = 0;
9726
9727 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
9728}
9729
9730/*
9731 * "searchdecl()" function
9732 */
9733 static void
9734f_searchdecl(typval_T *argvars, typval_T *rettv)
9735{
9736 int locally = 1;
9737 int thisblock = 0;
9738 int error = FALSE;
9739 char_u *name;
9740
9741 rettv->vval.v_number = 1; /* default: FAIL */
9742
9743 name = get_tv_string_chk(&argvars[0]);
9744 if (argvars[1].v_type != VAR_UNKNOWN)
9745 {
9746 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
9747 if (!error && argvars[2].v_type != VAR_UNKNOWN)
9748 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
9749 }
9750 if (!error && name != NULL)
9751 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
9752 locally, thisblock, SEARCH_KEEP) == FAIL;
9753}
9754
9755/*
9756 * Used by searchpair() and searchpairpos()
9757 */
9758 static int
9759searchpair_cmn(typval_T *argvars, pos_T *match_pos)
9760{
9761 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01009762 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009763 int save_p_ws = p_ws;
9764 int dir;
9765 int flags = 0;
9766 char_u nbuf1[NUMBUFLEN];
9767 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009768 int retval = 0; /* default: FAIL */
9769 long lnum_stop = 0;
9770 long time_limit = 0;
9771
9772 /* Get the three pattern arguments: start, middle, end. */
9773 spat = get_tv_string_chk(&argvars[0]);
9774 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
9775 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
9776 if (spat == NULL || mpat == NULL || epat == NULL)
9777 goto theend; /* type error */
9778
9779 /* Handle the optional fourth argument: flags */
9780 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
9781 if (dir == 0)
9782 goto theend;
9783
9784 /* Don't accept SP_END or SP_SUBPAT.
9785 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
9786 */
9787 if ((flags & (SP_END | SP_SUBPAT)) != 0
9788 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9789 {
9790 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
9791 goto theend;
9792 }
9793
9794 /* Using 'r' implies 'W', otherwise it doesn't work. */
9795 if (flags & SP_REPEAT)
9796 p_ws = FALSE;
9797
9798 /* Optional fifth argument: skip expression */
9799 if (argvars[3].v_type == VAR_UNKNOWN
9800 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01009801 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009802 else
9803 {
Bram Moolenaar48570482017-10-30 21:48:41 +01009804 skip = &argvars[4];
9805 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
9806 && skip->v_type != VAR_STRING)
9807 {
9808 /* Type error */
9809 goto theend;
9810 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009811 if (argvars[5].v_type != VAR_UNKNOWN)
9812 {
9813 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
9814 if (lnum_stop < 0)
9815 goto theend;
9816#ifdef FEAT_RELTIME
9817 if (argvars[6].v_type != VAR_UNKNOWN)
9818 {
9819 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
9820 if (time_limit < 0)
9821 goto theend;
9822 }
9823#endif
9824 }
9825 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009826
9827 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
9828 match_pos, lnum_stop, time_limit);
9829
9830theend:
9831 p_ws = save_p_ws;
9832
9833 return retval;
9834}
9835
9836/*
9837 * "searchpair()" function
9838 */
9839 static void
9840f_searchpair(typval_T *argvars, typval_T *rettv)
9841{
9842 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
9843}
9844
9845/*
9846 * "searchpairpos()" function
9847 */
9848 static void
9849f_searchpairpos(typval_T *argvars, typval_T *rettv)
9850{
9851 pos_T match_pos;
9852 int lnum = 0;
9853 int col = 0;
9854
9855 if (rettv_list_alloc(rettv) == FAIL)
9856 return;
9857
9858 if (searchpair_cmn(argvars, &match_pos) > 0)
9859 {
9860 lnum = match_pos.lnum;
9861 col = match_pos.col;
9862 }
9863
9864 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9865 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9866}
9867
9868/*
9869 * Search for a start/middle/end thing.
9870 * Used by searchpair(), see its documentation for the details.
9871 * Returns 0 or -1 for no match,
9872 */
9873 long
9874do_searchpair(
9875 char_u *spat, /* start pattern */
9876 char_u *mpat, /* middle pattern */
9877 char_u *epat, /* end pattern */
9878 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +01009879 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009880 int flags, /* SP_SETPCMARK and other SP_ values */
9881 pos_T *match_pos,
9882 linenr_T lnum_stop, /* stop at this line if not zero */
9883 long time_limit UNUSED) /* stop after this many msec */
9884{
9885 char_u *save_cpo;
9886 char_u *pat, *pat2 = NULL, *pat3 = NULL;
9887 long retval = 0;
9888 pos_T pos;
9889 pos_T firstpos;
9890 pos_T foundpos;
9891 pos_T save_cursor;
9892 pos_T save_pos;
9893 int n;
9894 int r;
9895 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01009896 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009897 int err;
9898 int options = SEARCH_KEEP;
9899 proftime_T tm;
9900
9901 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
9902 save_cpo = p_cpo;
9903 p_cpo = empty_option;
9904
9905#ifdef FEAT_RELTIME
9906 /* Set the time limit, if there is one. */
9907 profile_setlimit(time_limit, &tm);
9908#endif
9909
9910 /* Make two search patterns: start/end (pat2, for in nested pairs) and
9911 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009912 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
9913 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009914 if (pat2 == NULL || pat3 == NULL)
9915 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009916 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009917 if (*mpat == NUL)
9918 STRCPY(pat3, pat2);
9919 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009920 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009921 spat, epat, mpat);
9922 if (flags & SP_START)
9923 options |= SEARCH_START;
9924
Bram Moolenaar48570482017-10-30 21:48:41 +01009925 if (skip != NULL)
9926 {
9927 /* Empty string means to not use the skip expression. */
9928 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
9929 use_skip = skip->vval.v_string != NULL
9930 && *skip->vval.v_string != NUL;
9931 }
9932
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009933 save_cursor = curwin->w_cursor;
9934 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009935 CLEAR_POS(&firstpos);
9936 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009937 pat = pat3;
9938 for (;;)
9939 {
9940 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009941 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009942 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009943 /* didn't find it or found the first match again: FAIL */
9944 break;
9945
9946 if (firstpos.lnum == 0)
9947 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009948 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009949 {
9950 /* Found the same position again. Can happen with a pattern that
9951 * has "\zs" at the end and searching backwards. Advance one
9952 * character and try again. */
9953 if (dir == BACKWARD)
9954 decl(&pos);
9955 else
9956 incl(&pos);
9957 }
9958 foundpos = pos;
9959
9960 /* clear the start flag to avoid getting stuck here */
9961 options &= ~SEARCH_START;
9962
9963 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +01009964 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009965 {
9966 save_pos = curwin->w_cursor;
9967 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01009968 err = FALSE;
9969 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009970 curwin->w_cursor = save_pos;
9971 if (err)
9972 {
9973 /* Evaluating {skip} caused an error, break here. */
9974 curwin->w_cursor = save_cursor;
9975 retval = -1;
9976 break;
9977 }
9978 if (r)
9979 continue;
9980 }
9981
9982 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
9983 {
9984 /* Found end when searching backwards or start when searching
9985 * forward: nested pair. */
9986 ++nest;
9987 pat = pat2; /* nested, don't search for middle */
9988 }
9989 else
9990 {
9991 /* Found end when searching forward or start when searching
9992 * backward: end of (nested) pair; or found middle in outer pair. */
9993 if (--nest == 1)
9994 pat = pat3; /* outer level, search for middle */
9995 }
9996
9997 if (nest == 0)
9998 {
9999 /* Found the match: return matchcount or line number. */
10000 if (flags & SP_RETCOUNT)
10001 ++retval;
10002 else
10003 retval = pos.lnum;
10004 if (flags & SP_SETPCMARK)
10005 setpcmark();
10006 curwin->w_cursor = pos;
10007 if (!(flags & SP_REPEAT))
10008 break;
10009 nest = 1; /* search for next unmatched */
10010 }
10011 }
10012
10013 if (match_pos != NULL)
10014 {
10015 /* Store the match cursor position */
10016 match_pos->lnum = curwin->w_cursor.lnum;
10017 match_pos->col = curwin->w_cursor.col + 1;
10018 }
10019
10020 /* If 'n' flag is used or search failed: restore cursor position. */
10021 if ((flags & SP_NOMOVE) || retval == 0)
10022 curwin->w_cursor = save_cursor;
10023
10024theend:
10025 vim_free(pat2);
10026 vim_free(pat3);
10027 if (p_cpo == empty_option)
10028 p_cpo = save_cpo;
10029 else
10030 /* Darn, evaluating the {skip} expression changed the value. */
10031 free_string_option(save_cpo);
10032
10033 return retval;
10034}
10035
10036/*
10037 * "searchpos()" function
10038 */
10039 static void
10040f_searchpos(typval_T *argvars, typval_T *rettv)
10041{
10042 pos_T match_pos;
10043 int lnum = 0;
10044 int col = 0;
10045 int n;
10046 int flags = 0;
10047
10048 if (rettv_list_alloc(rettv) == FAIL)
10049 return;
10050
10051 n = search_cmn(argvars, &match_pos, &flags);
10052 if (n > 0)
10053 {
10054 lnum = match_pos.lnum;
10055 col = match_pos.col;
10056 }
10057
10058 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10059 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10060 if (flags & SP_SUBPAT)
10061 list_append_number(rettv->vval.v_list, (varnumber_T)n);
10062}
10063
10064 static void
10065f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
10066{
10067#ifdef FEAT_CLIENTSERVER
10068 char_u buf[NUMBUFLEN];
10069 char_u *server = get_tv_string_chk(&argvars[0]);
10070 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
10071
10072 rettv->vval.v_number = -1;
10073 if (server == NULL || reply == NULL)
10074 return;
10075 if (check_restricted() || check_secure())
10076 return;
10077# ifdef FEAT_X11
10078 if (check_connection() == FAIL)
10079 return;
10080# endif
10081
10082 if (serverSendReply(server, reply) < 0)
10083 {
10084 EMSG(_("E258: Unable to send to client"));
10085 return;
10086 }
10087 rettv->vval.v_number = 0;
10088#else
10089 rettv->vval.v_number = -1;
10090#endif
10091}
10092
10093 static void
10094f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10095{
10096 char_u *r = NULL;
10097
10098#ifdef FEAT_CLIENTSERVER
10099# ifdef WIN32
10100 r = serverGetVimNames();
10101# else
10102 make_connection();
10103 if (X_DISPLAY != NULL)
10104 r = serverGetVimNames(X_DISPLAY);
10105# endif
10106#endif
10107 rettv->v_type = VAR_STRING;
10108 rettv->vval.v_string = r;
10109}
10110
10111/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010112 * Set line or list of lines in buffer "buf".
10113 */
10114 static void
10115set_buffer_lines(buf_T *buf, linenr_T lnum, typval_T *lines, typval_T *rettv)
10116{
10117 char_u *line = NULL;
10118 list_T *l = NULL;
10119 listitem_T *li = NULL;
10120 long added = 0;
10121 linenr_T lcount;
Bram Moolenaar0c4dc882017-11-06 21:32:54 +010010122 buf_T *curbuf_save = NULL;
10123 win_T *curwin_save = NULL;
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010124 int is_curbuf = buf == curbuf;
10125
Bram Moolenaar9d954202017-09-04 20:34:19 +020010126 /* When using the current buffer ml_mfp will be set if needed. Useful when
10127 * setline() is used on startup. For other buffers the buffer must be
10128 * loaded. */
10129 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010130 {
10131 rettv->vval.v_number = 1; /* FAIL */
10132 return;
10133 }
10134
Bram Moolenaar0c4dc882017-11-06 21:32:54 +010010135 if (!is_curbuf)
10136 {
10137 wininfo_T *wip;
10138
10139 curbuf_save = curbuf;
10140 curwin_save = curwin;
10141 curbuf = buf;
10142 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
10143 {
10144 if (wip->wi_win != NULL)
10145 {
10146 curwin = wip->wi_win;
10147 break;
10148 }
10149 }
10150 }
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010151
10152 lcount = curbuf->b_ml.ml_line_count;
10153
10154 if (lines->v_type == VAR_LIST)
10155 {
10156 l = lines->vval.v_list;
10157 li = l->lv_first;
10158 }
10159 else
10160 line = get_tv_string_chk(lines);
10161
10162 /* default result is zero == OK */
10163 for (;;)
10164 {
10165 if (l != NULL)
10166 {
10167 /* list argument, get next string */
10168 if (li == NULL)
10169 break;
10170 line = get_tv_string_chk(&li->li_tv);
10171 li = li->li_next;
10172 }
10173
10174 rettv->vval.v_number = 1; /* FAIL */
10175 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
10176 break;
10177
10178 /* When coming here from Insert mode, sync undo, so that this can be
10179 * undone separately from what was previously inserted. */
10180 if (u_sync_once == 2)
10181 {
10182 u_sync_once = 1; /* notify that u_sync() was called */
10183 u_sync(TRUE);
10184 }
10185
10186 if (lnum <= curbuf->b_ml.ml_line_count)
10187 {
10188 /* existing line, replace it */
10189 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
10190 {
10191 changed_bytes(lnum, 0);
10192 if (is_curbuf && lnum == curwin->w_cursor.lnum)
10193 check_cursor_col();
10194 rettv->vval.v_number = 0; /* OK */
10195 }
10196 }
10197 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
10198 {
10199 /* lnum is one past the last line, append the line */
10200 ++added;
10201 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
10202 rettv->vval.v_number = 0; /* OK */
10203 }
10204
10205 if (l == NULL) /* only one string argument */
10206 break;
10207 ++lnum;
10208 }
10209
10210 if (added > 0)
10211 appended_lines_mark(lcount, added);
10212
Bram Moolenaar0c4dc882017-11-06 21:32:54 +010010213 if (!is_curbuf)
10214 {
10215 curbuf = curbuf_save;
10216 curwin = curwin_save;
10217 }
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010218}
10219
10220/*
10221 * "setbufline()" function
10222 */
10223 static void
10224f_setbufline(argvars, rettv)
10225 typval_T *argvars;
10226 typval_T *rettv;
10227{
10228 linenr_T lnum;
10229 buf_T *buf;
10230
10231 buf = get_buf_tv(&argvars[0], FALSE);
10232 if (buf == NULL)
10233 rettv->vval.v_number = 1; /* FAIL */
10234 else
10235 {
10236 lnum = get_tv_lnum_buf(&argvars[1], buf);
10237
10238 set_buffer_lines(buf, lnum, &argvars[2], rettv);
10239 }
10240}
10241
10242/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010243 * "setbufvar()" function
10244 */
10245 static void
10246f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10247{
10248 buf_T *buf;
10249 char_u *varname, *bufvarname;
10250 typval_T *varp;
10251 char_u nbuf[NUMBUFLEN];
10252
10253 if (check_restricted() || check_secure())
10254 return;
10255 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10256 varname = get_tv_string_chk(&argvars[1]);
10257 buf = get_buf_tv(&argvars[0], FALSE);
10258 varp = &argvars[2];
10259
10260 if (buf != NULL && varname != NULL && varp != NULL)
10261 {
10262 if (*varname == '&')
10263 {
10264 long numval;
10265 char_u *strval;
10266 int error = FALSE;
10267 aco_save_T aco;
10268
10269 /* set curbuf to be our buf, temporarily */
10270 aucmd_prepbuf(&aco, buf);
10271
10272 ++varname;
10273 numval = (long)get_tv_number_chk(varp, &error);
10274 strval = get_tv_string_buf_chk(varp, nbuf);
10275 if (!error && strval != NULL)
10276 set_option_value(varname, numval, strval, OPT_LOCAL);
10277
10278 /* reset notion of buffer */
10279 aucmd_restbuf(&aco);
10280 }
10281 else
10282 {
10283 buf_T *save_curbuf = curbuf;
10284
10285 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
10286 if (bufvarname != NULL)
10287 {
10288 curbuf = buf;
10289 STRCPY(bufvarname, "b:");
10290 STRCPY(bufvarname + 2, varname);
10291 set_var(bufvarname, varp, TRUE);
10292 vim_free(bufvarname);
10293 curbuf = save_curbuf;
10294 }
10295 }
10296 }
10297}
10298
10299 static void
10300f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10301{
10302 dict_T *d;
10303 dictitem_T *di;
10304 char_u *csearch;
10305
10306 if (argvars[0].v_type != VAR_DICT)
10307 {
10308 EMSG(_(e_dictreq));
10309 return;
10310 }
10311
10312 if ((d = argvars[0].vval.v_dict) != NULL)
10313 {
10314 csearch = get_dict_string(d, (char_u *)"char", FALSE);
10315 if (csearch != NULL)
10316 {
10317#ifdef FEAT_MBYTE
10318 if (enc_utf8)
10319 {
10320 int pcc[MAX_MCO];
10321 int c = utfc_ptr2char(csearch, pcc);
10322
10323 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10324 }
10325 else
10326#endif
10327 set_last_csearch(PTR2CHAR(csearch),
10328 csearch, MB_PTR2LEN(csearch));
10329 }
10330
10331 di = dict_find(d, (char_u *)"forward", -1);
10332 if (di != NULL)
10333 set_csearch_direction((int)get_tv_number(&di->di_tv)
10334 ? FORWARD : BACKWARD);
10335
10336 di = dict_find(d, (char_u *)"until", -1);
10337 if (di != NULL)
10338 set_csearch_until(!!get_tv_number(&di->di_tv));
10339 }
10340}
10341
10342/*
10343 * "setcmdpos()" function
10344 */
10345 static void
10346f_setcmdpos(typval_T *argvars, typval_T *rettv)
10347{
10348 int pos = (int)get_tv_number(&argvars[0]) - 1;
10349
10350 if (pos >= 0)
10351 rettv->vval.v_number = set_cmdline_pos(pos);
10352}
10353
10354/*
10355 * "setfperm({fname}, {mode})" function
10356 */
10357 static void
10358f_setfperm(typval_T *argvars, typval_T *rettv)
10359{
10360 char_u *fname;
10361 char_u modebuf[NUMBUFLEN];
10362 char_u *mode_str;
10363 int i;
10364 int mask;
10365 int mode = 0;
10366
10367 rettv->vval.v_number = 0;
10368 fname = get_tv_string_chk(&argvars[0]);
10369 if (fname == NULL)
10370 return;
10371 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
10372 if (mode_str == NULL)
10373 return;
10374 if (STRLEN(mode_str) != 9)
10375 {
10376 EMSG2(_(e_invarg2), mode_str);
10377 return;
10378 }
10379
10380 mask = 1;
10381 for (i = 8; i >= 0; --i)
10382 {
10383 if (mode_str[i] != '-')
10384 mode |= mask;
10385 mask = mask << 1;
10386 }
10387 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10388}
10389
10390/*
10391 * "setline()" function
10392 */
10393 static void
10394f_setline(typval_T *argvars, typval_T *rettv)
10395{
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010396 linenr_T lnum = get_tv_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010397
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010398 set_buffer_lines(curbuf, lnum, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010399}
10400
Bram Moolenaard823fa92016-08-12 16:29:27 +020010401static 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 +020010402
10403/*
10404 * Used by "setqflist()" and "setloclist()" functions
10405 */
10406 static void
10407set_qf_ll_list(
10408 win_T *wp UNUSED,
10409 typval_T *list_arg UNUSED,
10410 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010411 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010412 typval_T *rettv)
10413{
10414#ifdef FEAT_QUICKFIX
10415 static char *e_invact = N_("E927: Invalid action: '%s'");
10416 char_u *act;
10417 int action = 0;
10418#endif
10419
10420 rettv->vval.v_number = -1;
10421
10422#ifdef FEAT_QUICKFIX
10423 if (list_arg->v_type != VAR_LIST)
10424 EMSG(_(e_listreq));
10425 else
10426 {
10427 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010428 dict_T *d = NULL;
10429 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010430
10431 if (action_arg->v_type == VAR_STRING)
10432 {
10433 act = get_tv_string_chk(action_arg);
10434 if (act == NULL)
10435 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020010436 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
10437 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010438 action = *act;
10439 else
10440 EMSG2(_(e_invact), act);
10441 }
10442 else if (action_arg->v_type == VAR_UNKNOWN)
10443 action = ' ';
10444 else
10445 EMSG(_(e_stringreq));
10446
Bram Moolenaard823fa92016-08-12 16:29:27 +020010447 if (action_arg->v_type != VAR_UNKNOWN
10448 && what_arg->v_type != VAR_UNKNOWN)
10449 {
10450 if (what_arg->v_type == VAR_DICT)
10451 d = what_arg->vval.v_dict;
10452 else
10453 {
10454 EMSG(_(e_dictreq));
10455 valid_dict = FALSE;
10456 }
10457 }
10458
10459 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
10460 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()"), d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010461 rettv->vval.v_number = 0;
10462 }
10463#endif
10464}
10465
10466/*
10467 * "setloclist()" function
10468 */
10469 static void
10470f_setloclist(typval_T *argvars, typval_T *rettv)
10471{
10472 win_T *win;
10473
10474 rettv->vval.v_number = -1;
10475
10476 win = find_win_by_nr(&argvars[0], NULL);
10477 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020010478 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010479}
10480
10481/*
10482 * "setmatches()" function
10483 */
10484 static void
10485f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10486{
10487#ifdef FEAT_SEARCH_EXTRA
10488 list_T *l;
10489 listitem_T *li;
10490 dict_T *d;
10491 list_T *s = NULL;
10492
10493 rettv->vval.v_number = -1;
10494 if (argvars[0].v_type != VAR_LIST)
10495 {
10496 EMSG(_(e_listreq));
10497 return;
10498 }
10499 if ((l = argvars[0].vval.v_list) != NULL)
10500 {
10501
10502 /* To some extent make sure that we are dealing with a list from
10503 * "getmatches()". */
10504 li = l->lv_first;
10505 while (li != NULL)
10506 {
10507 if (li->li_tv.v_type != VAR_DICT
10508 || (d = li->li_tv.vval.v_dict) == NULL)
10509 {
10510 EMSG(_(e_invarg));
10511 return;
10512 }
10513 if (!(dict_find(d, (char_u *)"group", -1) != NULL
10514 && (dict_find(d, (char_u *)"pattern", -1) != NULL
10515 || dict_find(d, (char_u *)"pos1", -1) != NULL)
10516 && dict_find(d, (char_u *)"priority", -1) != NULL
10517 && dict_find(d, (char_u *)"id", -1) != NULL))
10518 {
10519 EMSG(_(e_invarg));
10520 return;
10521 }
10522 li = li->li_next;
10523 }
10524
10525 clear_matches(curwin);
10526 li = l->lv_first;
10527 while (li != NULL)
10528 {
10529 int i = 0;
10530 char_u buf[5];
10531 dictitem_T *di;
10532 char_u *group;
10533 int priority;
10534 int id;
10535 char_u *conceal;
10536
10537 d = li->li_tv.vval.v_dict;
10538 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
10539 {
10540 if (s == NULL)
10541 {
10542 s = list_alloc();
10543 if (s == NULL)
10544 return;
10545 }
10546
10547 /* match from matchaddpos() */
10548 for (i = 1; i < 9; i++)
10549 {
10550 sprintf((char *)buf, (char *)"pos%d", i);
10551 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
10552 {
10553 if (di->di_tv.v_type != VAR_LIST)
10554 return;
10555
10556 list_append_tv(s, &di->di_tv);
10557 s->lv_refcount++;
10558 }
10559 else
10560 break;
10561 }
10562 }
10563
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010564 group = get_dict_string(d, (char_u *)"group", TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010565 priority = (int)get_dict_number(d, (char_u *)"priority");
10566 id = (int)get_dict_number(d, (char_u *)"id");
10567 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010568 ? get_dict_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010569 : NULL;
10570 if (i == 0)
10571 {
10572 match_add(curwin, group,
10573 get_dict_string(d, (char_u *)"pattern", FALSE),
10574 priority, id, NULL, conceal);
10575 }
10576 else
10577 {
10578 match_add(curwin, group, NULL, priority, id, s, conceal);
10579 list_unref(s);
10580 s = NULL;
10581 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010582 vim_free(group);
10583 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010584
10585 li = li->li_next;
10586 }
10587 rettv->vval.v_number = 0;
10588 }
10589#endif
10590}
10591
10592/*
10593 * "setpos()" function
10594 */
10595 static void
10596f_setpos(typval_T *argvars, typval_T *rettv)
10597{
10598 pos_T pos;
10599 int fnum;
10600 char_u *name;
10601 colnr_T curswant = -1;
10602
10603 rettv->vval.v_number = -1;
10604 name = get_tv_string_chk(argvars);
10605 if (name != NULL)
10606 {
10607 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10608 {
10609 if (--pos.col < 0)
10610 pos.col = 0;
10611 if (name[0] == '.' && name[1] == NUL)
10612 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010613 /* set cursor; "fnum" is ignored */
10614 curwin->w_cursor = pos;
10615 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010616 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010617 curwin->w_curswant = curswant - 1;
10618 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010619 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010620 check_cursor();
10621 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010622 }
10623 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10624 {
10625 /* set mark */
10626 if (setmark_pos(name[1], &pos, fnum) == OK)
10627 rettv->vval.v_number = 0;
10628 }
10629 else
10630 EMSG(_(e_invarg));
10631 }
10632 }
10633}
10634
10635/*
10636 * "setqflist()" function
10637 */
10638 static void
10639f_setqflist(typval_T *argvars, typval_T *rettv)
10640{
Bram Moolenaard823fa92016-08-12 16:29:27 +020010641 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010642}
10643
10644/*
10645 * "setreg()" function
10646 */
10647 static void
10648f_setreg(typval_T *argvars, typval_T *rettv)
10649{
10650 int regname;
10651 char_u *strregname;
10652 char_u *stropt;
10653 char_u *strval;
10654 int append;
10655 char_u yank_type;
10656 long block_len;
10657
10658 block_len = -1;
10659 yank_type = MAUTO;
10660 append = FALSE;
10661
10662 strregname = get_tv_string_chk(argvars);
10663 rettv->vval.v_number = 1; /* FAIL is default */
10664
10665 if (strregname == NULL)
10666 return; /* type error; errmsg already given */
10667 regname = *strregname;
10668 if (regname == 0 || regname == '@')
10669 regname = '"';
10670
10671 if (argvars[2].v_type != VAR_UNKNOWN)
10672 {
10673 stropt = get_tv_string_chk(&argvars[2]);
10674 if (stropt == NULL)
10675 return; /* type error */
10676 for (; *stropt != NUL; ++stropt)
10677 switch (*stropt)
10678 {
10679 case 'a': case 'A': /* append */
10680 append = TRUE;
10681 break;
10682 case 'v': case 'c': /* character-wise selection */
10683 yank_type = MCHAR;
10684 break;
10685 case 'V': case 'l': /* line-wise selection */
10686 yank_type = MLINE;
10687 break;
10688 case 'b': case Ctrl_V: /* block-wise selection */
10689 yank_type = MBLOCK;
10690 if (VIM_ISDIGIT(stropt[1]))
10691 {
10692 ++stropt;
10693 block_len = getdigits(&stropt) - 1;
10694 --stropt;
10695 }
10696 break;
10697 }
10698 }
10699
10700 if (argvars[1].v_type == VAR_LIST)
10701 {
10702 char_u **lstval;
10703 char_u **allocval;
10704 char_u buf[NUMBUFLEN];
10705 char_u **curval;
10706 char_u **curallocval;
10707 list_T *ll = argvars[1].vval.v_list;
10708 listitem_T *li;
10709 int len;
10710
10711 /* If the list is NULL handle like an empty list. */
10712 len = ll == NULL ? 0 : ll->lv_len;
10713
10714 /* First half: use for pointers to result lines; second half: use for
10715 * pointers to allocated copies. */
10716 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
10717 if (lstval == NULL)
10718 return;
10719 curval = lstval;
10720 allocval = lstval + len + 2;
10721 curallocval = allocval;
10722
10723 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
10724 li = li->li_next)
10725 {
10726 strval = get_tv_string_buf_chk(&li->li_tv, buf);
10727 if (strval == NULL)
10728 goto free_lstval;
10729 if (strval == buf)
10730 {
10731 /* Need to make a copy, next get_tv_string_buf_chk() will
10732 * overwrite the string. */
10733 strval = vim_strsave(buf);
10734 if (strval == NULL)
10735 goto free_lstval;
10736 *curallocval++ = strval;
10737 }
10738 *curval++ = strval;
10739 }
10740 *curval++ = NULL;
10741
10742 write_reg_contents_lst(regname, lstval, -1,
10743 append, yank_type, block_len);
10744free_lstval:
10745 while (curallocval > allocval)
10746 vim_free(*--curallocval);
10747 vim_free(lstval);
10748 }
10749 else
10750 {
10751 strval = get_tv_string_chk(&argvars[1]);
10752 if (strval == NULL)
10753 return;
10754 write_reg_contents_ex(regname, strval, -1,
10755 append, yank_type, block_len);
10756 }
10757 rettv->vval.v_number = 0;
10758}
10759
10760/*
10761 * "settabvar()" function
10762 */
10763 static void
10764f_settabvar(typval_T *argvars, typval_T *rettv)
10765{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010766 tabpage_T *save_curtab;
10767 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010768 char_u *varname, *tabvarname;
10769 typval_T *varp;
10770
10771 rettv->vval.v_number = 0;
10772
10773 if (check_restricted() || check_secure())
10774 return;
10775
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010776 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010777 varname = get_tv_string_chk(&argvars[1]);
10778 varp = &argvars[2];
10779
Bram Moolenaar4033c552017-09-16 20:54:51 +020010780 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010781 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010782 save_curtab = curtab;
10783 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010784
10785 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
10786 if (tabvarname != NULL)
10787 {
10788 STRCPY(tabvarname, "t:");
10789 STRCPY(tabvarname + 2, varname);
10790 set_var(tabvarname, varp, TRUE);
10791 vim_free(tabvarname);
10792 }
10793
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010794 /* Restore current tabpage */
10795 if (valid_tabpage(save_curtab))
10796 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010797 }
10798}
10799
10800/*
10801 * "settabwinvar()" function
10802 */
10803 static void
10804f_settabwinvar(typval_T *argvars, typval_T *rettv)
10805{
10806 setwinvar(argvars, rettv, 1);
10807}
10808
10809/*
10810 * "setwinvar()" function
10811 */
10812 static void
10813f_setwinvar(typval_T *argvars, typval_T *rettv)
10814{
10815 setwinvar(argvars, rettv, 0);
10816}
10817
10818#ifdef FEAT_CRYPT
10819/*
10820 * "sha256({string})" function
10821 */
10822 static void
10823f_sha256(typval_T *argvars, typval_T *rettv)
10824{
10825 char_u *p;
10826
10827 p = get_tv_string(&argvars[0]);
10828 rettv->vval.v_string = vim_strsave(
10829 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
10830 rettv->v_type = VAR_STRING;
10831}
10832#endif /* FEAT_CRYPT */
10833
10834/*
10835 * "shellescape({string})" function
10836 */
10837 static void
10838f_shellescape(typval_T *argvars, typval_T *rettv)
10839{
Bram Moolenaar20615522017-06-05 18:46:26 +020010840 int do_special = non_zero_arg(&argvars[1]);
10841
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010842 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar20615522017-06-05 18:46:26 +020010843 get_tv_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010844 rettv->v_type = VAR_STRING;
10845}
10846
10847/*
10848 * shiftwidth() function
10849 */
10850 static void
10851f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
10852{
10853 rettv->vval.v_number = get_sw_value(curbuf);
10854}
10855
10856/*
10857 * "simplify()" function
10858 */
10859 static void
10860f_simplify(typval_T *argvars, typval_T *rettv)
10861{
10862 char_u *p;
10863
10864 p = get_tv_string(&argvars[0]);
10865 rettv->vval.v_string = vim_strsave(p);
10866 simplify_filename(rettv->vval.v_string); /* simplify in place */
10867 rettv->v_type = VAR_STRING;
10868}
10869
10870#ifdef FEAT_FLOAT
10871/*
10872 * "sin()" function
10873 */
10874 static void
10875f_sin(typval_T *argvars, typval_T *rettv)
10876{
10877 float_T f = 0.0;
10878
10879 rettv->v_type = VAR_FLOAT;
10880 if (get_float_arg(argvars, &f) == OK)
10881 rettv->vval.v_float = sin(f);
10882 else
10883 rettv->vval.v_float = 0.0;
10884}
10885
10886/*
10887 * "sinh()" function
10888 */
10889 static void
10890f_sinh(typval_T *argvars, typval_T *rettv)
10891{
10892 float_T f = 0.0;
10893
10894 rettv->v_type = VAR_FLOAT;
10895 if (get_float_arg(argvars, &f) == OK)
10896 rettv->vval.v_float = sinh(f);
10897 else
10898 rettv->vval.v_float = 0.0;
10899}
10900#endif
10901
10902static int
10903#ifdef __BORLANDC__
10904 _RTLENTRYF
10905#endif
10906 item_compare(const void *s1, const void *s2);
10907static int
10908#ifdef __BORLANDC__
10909 _RTLENTRYF
10910#endif
10911 item_compare2(const void *s1, const void *s2);
10912
10913/* struct used in the array that's given to qsort() */
10914typedef struct
10915{
10916 listitem_T *item;
10917 int idx;
10918} sortItem_T;
10919
10920/* struct storing information about current sort */
10921typedef struct
10922{
10923 int item_compare_ic;
10924 int item_compare_numeric;
10925 int item_compare_numbers;
10926#ifdef FEAT_FLOAT
10927 int item_compare_float;
10928#endif
10929 char_u *item_compare_func;
10930 partial_T *item_compare_partial;
10931 dict_T *item_compare_selfdict;
10932 int item_compare_func_err;
10933 int item_compare_keep_zero;
10934} sortinfo_T;
10935static sortinfo_T *sortinfo = NULL;
10936static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
10937#define ITEM_COMPARE_FAIL 999
10938
10939/*
10940 * Compare functions for f_sort() and f_uniq() below.
10941 */
10942 static int
10943#ifdef __BORLANDC__
10944_RTLENTRYF
10945#endif
10946item_compare(const void *s1, const void *s2)
10947{
10948 sortItem_T *si1, *si2;
10949 typval_T *tv1, *tv2;
10950 char_u *p1, *p2;
10951 char_u *tofree1 = NULL, *tofree2 = NULL;
10952 int res;
10953 char_u numbuf1[NUMBUFLEN];
10954 char_u numbuf2[NUMBUFLEN];
10955
10956 si1 = (sortItem_T *)s1;
10957 si2 = (sortItem_T *)s2;
10958 tv1 = &si1->item->li_tv;
10959 tv2 = &si2->item->li_tv;
10960
10961 if (sortinfo->item_compare_numbers)
10962 {
10963 varnumber_T v1 = get_tv_number(tv1);
10964 varnumber_T v2 = get_tv_number(tv2);
10965
10966 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10967 }
10968
10969#ifdef FEAT_FLOAT
10970 if (sortinfo->item_compare_float)
10971 {
10972 float_T v1 = get_tv_float(tv1);
10973 float_T v2 = get_tv_float(tv2);
10974
10975 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10976 }
10977#endif
10978
10979 /* tv2string() puts quotes around a string and allocates memory. Don't do
10980 * that for string variables. Use a single quote when comparing with a
10981 * non-string to do what the docs promise. */
10982 if (tv1->v_type == VAR_STRING)
10983 {
10984 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
10985 p1 = (char_u *)"'";
10986 else
10987 p1 = tv1->vval.v_string;
10988 }
10989 else
10990 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
10991 if (tv2->v_type == VAR_STRING)
10992 {
10993 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
10994 p2 = (char_u *)"'";
10995 else
10996 p2 = tv2->vval.v_string;
10997 }
10998 else
10999 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
11000 if (p1 == NULL)
11001 p1 = (char_u *)"";
11002 if (p2 == NULL)
11003 p2 = (char_u *)"";
11004 if (!sortinfo->item_compare_numeric)
11005 {
11006 if (sortinfo->item_compare_ic)
11007 res = STRICMP(p1, p2);
11008 else
11009 res = STRCMP(p1, p2);
11010 }
11011 else
11012 {
11013 double n1, n2;
11014 n1 = strtod((char *)p1, (char **)&p1);
11015 n2 = strtod((char *)p2, (char **)&p2);
11016 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
11017 }
11018
11019 /* When the result would be zero, compare the item indexes. Makes the
11020 * sort stable. */
11021 if (res == 0 && !sortinfo->item_compare_keep_zero)
11022 res = si1->idx > si2->idx ? 1 : -1;
11023
11024 vim_free(tofree1);
11025 vim_free(tofree2);
11026 return res;
11027}
11028
11029 static int
11030#ifdef __BORLANDC__
11031_RTLENTRYF
11032#endif
11033item_compare2(const void *s1, const void *s2)
11034{
11035 sortItem_T *si1, *si2;
11036 int res;
11037 typval_T rettv;
11038 typval_T argv[3];
11039 int dummy;
11040 char_u *func_name;
11041 partial_T *partial = sortinfo->item_compare_partial;
11042
11043 /* shortcut after failure in previous call; compare all items equal */
11044 if (sortinfo->item_compare_func_err)
11045 return 0;
11046
11047 si1 = (sortItem_T *)s1;
11048 si2 = (sortItem_T *)s2;
11049
11050 if (partial == NULL)
11051 func_name = sortinfo->item_compare_func;
11052 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020011053 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011054
11055 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
11056 * in the copy without changing the original list items. */
11057 copy_tv(&si1->item->li_tv, &argv[0]);
11058 copy_tv(&si2->item->li_tv, &argv[1]);
11059
11060 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
11061 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020011062 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011063 partial, sortinfo->item_compare_selfdict);
11064 clear_tv(&argv[0]);
11065 clear_tv(&argv[1]);
11066
11067 if (res == FAIL)
11068 res = ITEM_COMPARE_FAIL;
11069 else
11070 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
11071 if (sortinfo->item_compare_func_err)
11072 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
11073 clear_tv(&rettv);
11074
11075 /* When the result would be zero, compare the pointers themselves. Makes
11076 * the sort stable. */
11077 if (res == 0 && !sortinfo->item_compare_keep_zero)
11078 res = si1->idx > si2->idx ? 1 : -1;
11079
11080 return res;
11081}
11082
11083/*
11084 * "sort({list})" function
11085 */
11086 static void
11087do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
11088{
11089 list_T *l;
11090 listitem_T *li;
11091 sortItem_T *ptrs;
11092 sortinfo_T *old_sortinfo;
11093 sortinfo_T info;
11094 long len;
11095 long i;
11096
11097 /* Pointer to current info struct used in compare function. Save and
11098 * restore the current one for nested calls. */
11099 old_sortinfo = sortinfo;
11100 sortinfo = &info;
11101
11102 if (argvars[0].v_type != VAR_LIST)
11103 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
11104 else
11105 {
11106 l = argvars[0].vval.v_list;
11107 if (l == NULL || tv_check_lock(l->lv_lock,
11108 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
11109 TRUE))
11110 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011111 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011112
11113 len = list_len(l);
11114 if (len <= 1)
11115 goto theend; /* short list sorts pretty quickly */
11116
11117 info.item_compare_ic = FALSE;
11118 info.item_compare_numeric = FALSE;
11119 info.item_compare_numbers = FALSE;
11120#ifdef FEAT_FLOAT
11121 info.item_compare_float = FALSE;
11122#endif
11123 info.item_compare_func = NULL;
11124 info.item_compare_partial = NULL;
11125 info.item_compare_selfdict = NULL;
11126 if (argvars[1].v_type != VAR_UNKNOWN)
11127 {
11128 /* optional second argument: {func} */
11129 if (argvars[1].v_type == VAR_FUNC)
11130 info.item_compare_func = argvars[1].vval.v_string;
11131 else if (argvars[1].v_type == VAR_PARTIAL)
11132 info.item_compare_partial = argvars[1].vval.v_partial;
11133 else
11134 {
11135 int error = FALSE;
11136
11137 i = (long)get_tv_number_chk(&argvars[1], &error);
11138 if (error)
11139 goto theend; /* type error; errmsg already given */
11140 if (i == 1)
11141 info.item_compare_ic = TRUE;
11142 else if (argvars[1].v_type != VAR_NUMBER)
11143 info.item_compare_func = get_tv_string(&argvars[1]);
11144 else if (i != 0)
11145 {
11146 EMSG(_(e_invarg));
11147 goto theend;
11148 }
11149 if (info.item_compare_func != NULL)
11150 {
11151 if (*info.item_compare_func == NUL)
11152 {
11153 /* empty string means default sort */
11154 info.item_compare_func = NULL;
11155 }
11156 else if (STRCMP(info.item_compare_func, "n") == 0)
11157 {
11158 info.item_compare_func = NULL;
11159 info.item_compare_numeric = TRUE;
11160 }
11161 else if (STRCMP(info.item_compare_func, "N") == 0)
11162 {
11163 info.item_compare_func = NULL;
11164 info.item_compare_numbers = TRUE;
11165 }
11166#ifdef FEAT_FLOAT
11167 else if (STRCMP(info.item_compare_func, "f") == 0)
11168 {
11169 info.item_compare_func = NULL;
11170 info.item_compare_float = TRUE;
11171 }
11172#endif
11173 else if (STRCMP(info.item_compare_func, "i") == 0)
11174 {
11175 info.item_compare_func = NULL;
11176 info.item_compare_ic = TRUE;
11177 }
11178 }
11179 }
11180
11181 if (argvars[2].v_type != VAR_UNKNOWN)
11182 {
11183 /* optional third argument: {dict} */
11184 if (argvars[2].v_type != VAR_DICT)
11185 {
11186 EMSG(_(e_dictreq));
11187 goto theend;
11188 }
11189 info.item_compare_selfdict = argvars[2].vval.v_dict;
11190 }
11191 }
11192
11193 /* Make an array with each entry pointing to an item in the List. */
11194 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
11195 if (ptrs == NULL)
11196 goto theend;
11197
11198 i = 0;
11199 if (sort)
11200 {
11201 /* sort(): ptrs will be the list to sort */
11202 for (li = l->lv_first; li != NULL; li = li->li_next)
11203 {
11204 ptrs[i].item = li;
11205 ptrs[i].idx = i;
11206 ++i;
11207 }
11208
11209 info.item_compare_func_err = FALSE;
11210 info.item_compare_keep_zero = FALSE;
11211 /* test the compare function */
11212 if ((info.item_compare_func != NULL
11213 || info.item_compare_partial != NULL)
11214 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11215 == ITEM_COMPARE_FAIL)
11216 EMSG(_("E702: Sort compare function failed"));
11217 else
11218 {
11219 /* Sort the array with item pointers. */
11220 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
11221 info.item_compare_func == NULL
11222 && info.item_compare_partial == NULL
11223 ? item_compare : item_compare2);
11224
11225 if (!info.item_compare_func_err)
11226 {
11227 /* Clear the List and append the items in sorted order. */
11228 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
11229 l->lv_len = 0;
11230 for (i = 0; i < len; ++i)
11231 list_append(l, ptrs[i].item);
11232 }
11233 }
11234 }
11235 else
11236 {
11237 int (*item_compare_func_ptr)(const void *, const void *);
11238
11239 /* f_uniq(): ptrs will be a stack of items to remove */
11240 info.item_compare_func_err = FALSE;
11241 info.item_compare_keep_zero = TRUE;
11242 item_compare_func_ptr = info.item_compare_func != NULL
11243 || info.item_compare_partial != NULL
11244 ? item_compare2 : item_compare;
11245
11246 for (li = l->lv_first; li != NULL && li->li_next != NULL;
11247 li = li->li_next)
11248 {
11249 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
11250 == 0)
11251 ptrs[i++].item = li;
11252 if (info.item_compare_func_err)
11253 {
11254 EMSG(_("E882: Uniq compare function failed"));
11255 break;
11256 }
11257 }
11258
11259 if (!info.item_compare_func_err)
11260 {
11261 while (--i >= 0)
11262 {
11263 li = ptrs[i].item->li_next;
11264 ptrs[i].item->li_next = li->li_next;
11265 if (li->li_next != NULL)
11266 li->li_next->li_prev = ptrs[i].item;
11267 else
11268 l->lv_last = ptrs[i].item;
11269 list_fix_watch(l, li);
11270 listitem_free(li);
11271 l->lv_len--;
11272 }
11273 }
11274 }
11275
11276 vim_free(ptrs);
11277 }
11278theend:
11279 sortinfo = old_sortinfo;
11280}
11281
11282/*
11283 * "sort({list})" function
11284 */
11285 static void
11286f_sort(typval_T *argvars, typval_T *rettv)
11287{
11288 do_sort_uniq(argvars, rettv, TRUE);
11289}
11290
11291/*
11292 * "uniq({list})" function
11293 */
11294 static void
11295f_uniq(typval_T *argvars, typval_T *rettv)
11296{
11297 do_sort_uniq(argvars, rettv, FALSE);
11298}
11299
11300/*
11301 * "soundfold({word})" function
11302 */
11303 static void
11304f_soundfold(typval_T *argvars, typval_T *rettv)
11305{
11306 char_u *s;
11307
11308 rettv->v_type = VAR_STRING;
11309 s = get_tv_string(&argvars[0]);
11310#ifdef FEAT_SPELL
11311 rettv->vval.v_string = eval_soundfold(s);
11312#else
11313 rettv->vval.v_string = vim_strsave(s);
11314#endif
11315}
11316
11317/*
11318 * "spellbadword()" function
11319 */
11320 static void
11321f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
11322{
11323 char_u *word = (char_u *)"";
11324 hlf_T attr = HLF_COUNT;
11325 int len = 0;
11326
11327 if (rettv_list_alloc(rettv) == FAIL)
11328 return;
11329
11330#ifdef FEAT_SPELL
11331 if (argvars[0].v_type == VAR_UNKNOWN)
11332 {
11333 /* Find the start and length of the badly spelled word. */
11334 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
11335 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011336 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011337 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011338 curwin->w_set_curswant = TRUE;
11339 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011340 }
11341 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
11342 {
11343 char_u *str = get_tv_string_chk(&argvars[0]);
11344 int capcol = -1;
11345
11346 if (str != NULL)
11347 {
11348 /* Check the argument for spelling. */
11349 while (*str != NUL)
11350 {
11351 len = spell_check(curwin, str, &attr, &capcol, FALSE);
11352 if (attr != HLF_COUNT)
11353 {
11354 word = str;
11355 break;
11356 }
11357 str += len;
11358 }
11359 }
11360 }
11361#endif
11362
11363 list_append_string(rettv->vval.v_list, word, len);
11364 list_append_string(rettv->vval.v_list, (char_u *)(
11365 attr == HLF_SPB ? "bad" :
11366 attr == HLF_SPR ? "rare" :
11367 attr == HLF_SPL ? "local" :
11368 attr == HLF_SPC ? "caps" :
11369 ""), -1);
11370}
11371
11372/*
11373 * "spellsuggest()" function
11374 */
11375 static void
11376f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
11377{
11378#ifdef FEAT_SPELL
11379 char_u *str;
11380 int typeerr = FALSE;
11381 int maxcount;
11382 garray_T ga;
11383 int i;
11384 listitem_T *li;
11385 int need_capital = FALSE;
11386#endif
11387
11388 if (rettv_list_alloc(rettv) == FAIL)
11389 return;
11390
11391#ifdef FEAT_SPELL
11392 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
11393 {
11394 str = get_tv_string(&argvars[0]);
11395 if (argvars[1].v_type != VAR_UNKNOWN)
11396 {
11397 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
11398 if (maxcount <= 0)
11399 return;
11400 if (argvars[2].v_type != VAR_UNKNOWN)
11401 {
11402 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
11403 if (typeerr)
11404 return;
11405 }
11406 }
11407 else
11408 maxcount = 25;
11409
11410 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
11411
11412 for (i = 0; i < ga.ga_len; ++i)
11413 {
11414 str = ((char_u **)ga.ga_data)[i];
11415
11416 li = listitem_alloc();
11417 if (li == NULL)
11418 vim_free(str);
11419 else
11420 {
11421 li->li_tv.v_type = VAR_STRING;
11422 li->li_tv.v_lock = 0;
11423 li->li_tv.vval.v_string = str;
11424 list_append(rettv->vval.v_list, li);
11425 }
11426 }
11427 ga_clear(&ga);
11428 }
11429#endif
11430}
11431
11432 static void
11433f_split(typval_T *argvars, typval_T *rettv)
11434{
11435 char_u *str;
11436 char_u *end;
11437 char_u *pat = NULL;
11438 regmatch_T regmatch;
11439 char_u patbuf[NUMBUFLEN];
11440 char_u *save_cpo;
11441 int match;
11442 colnr_T col = 0;
11443 int keepempty = FALSE;
11444 int typeerr = FALSE;
11445
11446 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11447 save_cpo = p_cpo;
11448 p_cpo = (char_u *)"";
11449
11450 str = get_tv_string(&argvars[0]);
11451 if (argvars[1].v_type != VAR_UNKNOWN)
11452 {
11453 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11454 if (pat == NULL)
11455 typeerr = TRUE;
11456 if (argvars[2].v_type != VAR_UNKNOWN)
11457 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
11458 }
11459 if (pat == NULL || *pat == NUL)
11460 pat = (char_u *)"[\\x01- ]\\+";
11461
11462 if (rettv_list_alloc(rettv) == FAIL)
11463 return;
11464 if (typeerr)
11465 return;
11466
11467 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11468 if (regmatch.regprog != NULL)
11469 {
11470 regmatch.rm_ic = FALSE;
11471 while (*str != NUL || keepempty)
11472 {
11473 if (*str == NUL)
11474 match = FALSE; /* empty item at the end */
11475 else
11476 match = vim_regexec_nl(&regmatch, str, col);
11477 if (match)
11478 end = regmatch.startp[0];
11479 else
11480 end = str + STRLEN(str);
11481 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
11482 && *str != NUL && match && end < regmatch.endp[0]))
11483 {
11484 if (list_append_string(rettv->vval.v_list, str,
11485 (int)(end - str)) == FAIL)
11486 break;
11487 }
11488 if (!match)
11489 break;
11490 /* Advance to just after the match. */
11491 if (regmatch.endp[0] > str)
11492 col = 0;
11493 else
11494 {
11495 /* Don't get stuck at the same match. */
11496#ifdef FEAT_MBYTE
11497 col = (*mb_ptr2len)(regmatch.endp[0]);
11498#else
11499 col = 1;
11500#endif
11501 }
11502 str = regmatch.endp[0];
11503 }
11504
11505 vim_regfree(regmatch.regprog);
11506 }
11507
11508 p_cpo = save_cpo;
11509}
11510
11511#ifdef FEAT_FLOAT
11512/*
11513 * "sqrt()" function
11514 */
11515 static void
11516f_sqrt(typval_T *argvars, typval_T *rettv)
11517{
11518 float_T f = 0.0;
11519
11520 rettv->v_type = VAR_FLOAT;
11521 if (get_float_arg(argvars, &f) == OK)
11522 rettv->vval.v_float = sqrt(f);
11523 else
11524 rettv->vval.v_float = 0.0;
11525}
11526
11527/*
11528 * "str2float()" function
11529 */
11530 static void
11531f_str2float(typval_T *argvars, typval_T *rettv)
11532{
11533 char_u *p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011534 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011535
Bram Moolenaar08243d22017-01-10 16:12:29 +010011536 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011537 p = skipwhite(p + 1);
11538 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011539 if (isneg)
11540 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011541 rettv->v_type = VAR_FLOAT;
11542}
11543#endif
11544
11545/*
11546 * "str2nr()" function
11547 */
11548 static void
11549f_str2nr(typval_T *argvars, typval_T *rettv)
11550{
11551 int base = 10;
11552 char_u *p;
11553 varnumber_T n;
11554 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010011555 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011556
11557 if (argvars[1].v_type != VAR_UNKNOWN)
11558 {
11559 base = (int)get_tv_number(&argvars[1]);
11560 if (base != 2 && base != 8 && base != 10 && base != 16)
11561 {
11562 EMSG(_(e_invarg));
11563 return;
11564 }
11565 }
11566
11567 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011568 isneg = (*p == '-');
11569 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011570 p = skipwhite(p + 1);
11571 switch (base)
11572 {
11573 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
11574 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
11575 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
11576 default: what = 0;
11577 }
11578 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011579 if (isneg)
11580 rettv->vval.v_number = -n;
11581 else
11582 rettv->vval.v_number = n;
11583
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011584}
11585
11586#ifdef HAVE_STRFTIME
11587/*
11588 * "strftime({format}[, {time}])" function
11589 */
11590 static void
11591f_strftime(typval_T *argvars, typval_T *rettv)
11592{
11593 char_u result_buf[256];
11594 struct tm *curtime;
11595 time_t seconds;
11596 char_u *p;
11597
11598 rettv->v_type = VAR_STRING;
11599
11600 p = get_tv_string(&argvars[0]);
11601 if (argvars[1].v_type == VAR_UNKNOWN)
11602 seconds = time(NULL);
11603 else
11604 seconds = (time_t)get_tv_number(&argvars[1]);
11605 curtime = localtime(&seconds);
11606 /* MSVC returns NULL for an invalid value of seconds. */
11607 if (curtime == NULL)
11608 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
11609 else
11610 {
11611# ifdef FEAT_MBYTE
11612 vimconv_T conv;
11613 char_u *enc;
11614
11615 conv.vc_type = CONV_NONE;
11616 enc = enc_locale();
11617 convert_setup(&conv, p_enc, enc);
11618 if (conv.vc_type != CONV_NONE)
11619 p = string_convert(&conv, p, NULL);
11620# endif
11621 if (p != NULL)
11622 (void)strftime((char *)result_buf, sizeof(result_buf),
11623 (char *)p, curtime);
11624 else
11625 result_buf[0] = NUL;
11626
11627# ifdef FEAT_MBYTE
11628 if (conv.vc_type != CONV_NONE)
11629 vim_free(p);
11630 convert_setup(&conv, enc, p_enc);
11631 if (conv.vc_type != CONV_NONE)
11632 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
11633 else
11634# endif
11635 rettv->vval.v_string = vim_strsave(result_buf);
11636
11637# ifdef FEAT_MBYTE
11638 /* Release conversion descriptors */
11639 convert_setup(&conv, NULL, NULL);
11640 vim_free(enc);
11641# endif
11642 }
11643}
11644#endif
11645
11646/*
11647 * "strgetchar()" function
11648 */
11649 static void
11650f_strgetchar(typval_T *argvars, typval_T *rettv)
11651{
11652 char_u *str;
11653 int len;
11654 int error = FALSE;
11655 int charidx;
11656
11657 rettv->vval.v_number = -1;
11658 str = get_tv_string_chk(&argvars[0]);
11659 if (str == NULL)
11660 return;
11661 len = (int)STRLEN(str);
11662 charidx = (int)get_tv_number_chk(&argvars[1], &error);
11663 if (error)
11664 return;
11665#ifdef FEAT_MBYTE
11666 {
11667 int byteidx = 0;
11668
11669 while (charidx >= 0 && byteidx < len)
11670 {
11671 if (charidx == 0)
11672 {
11673 rettv->vval.v_number = mb_ptr2char(str + byteidx);
11674 break;
11675 }
11676 --charidx;
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011677 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011678 }
11679 }
11680#else
11681 if (charidx < len)
11682 rettv->vval.v_number = str[charidx];
11683#endif
11684}
11685
11686/*
11687 * "stridx()" function
11688 */
11689 static void
11690f_stridx(typval_T *argvars, typval_T *rettv)
11691{
11692 char_u buf[NUMBUFLEN];
11693 char_u *needle;
11694 char_u *haystack;
11695 char_u *save_haystack;
11696 char_u *pos;
11697 int start_idx;
11698
11699 needle = get_tv_string_chk(&argvars[1]);
11700 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
11701 rettv->vval.v_number = -1;
11702 if (needle == NULL || haystack == NULL)
11703 return; /* type error; errmsg already given */
11704
11705 if (argvars[2].v_type != VAR_UNKNOWN)
11706 {
11707 int error = FALSE;
11708
11709 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
11710 if (error || start_idx >= (int)STRLEN(haystack))
11711 return;
11712 if (start_idx >= 0)
11713 haystack += start_idx;
11714 }
11715
11716 pos = (char_u *)strstr((char *)haystack, (char *)needle);
11717 if (pos != NULL)
11718 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
11719}
11720
11721/*
11722 * "string()" function
11723 */
11724 static void
11725f_string(typval_T *argvars, typval_T *rettv)
11726{
11727 char_u *tofree;
11728 char_u numbuf[NUMBUFLEN];
11729
11730 rettv->v_type = VAR_STRING;
11731 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
11732 get_copyID());
11733 /* Make a copy if we have a value but it's not in allocated memory. */
11734 if (rettv->vval.v_string != NULL && tofree == NULL)
11735 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
11736}
11737
11738/*
11739 * "strlen()" function
11740 */
11741 static void
11742f_strlen(typval_T *argvars, typval_T *rettv)
11743{
11744 rettv->vval.v_number = (varnumber_T)(STRLEN(
11745 get_tv_string(&argvars[0])));
11746}
11747
11748/*
11749 * "strchars()" function
11750 */
11751 static void
11752f_strchars(typval_T *argvars, typval_T *rettv)
11753{
11754 char_u *s = get_tv_string(&argvars[0]);
11755 int skipcc = 0;
11756#ifdef FEAT_MBYTE
11757 varnumber_T len = 0;
11758 int (*func_mb_ptr2char_adv)(char_u **pp);
11759#endif
11760
11761 if (argvars[1].v_type != VAR_UNKNOWN)
11762 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
11763 if (skipcc < 0 || skipcc > 1)
11764 EMSG(_(e_invarg));
11765 else
11766 {
11767#ifdef FEAT_MBYTE
11768 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
11769 while (*s != NUL)
11770 {
11771 func_mb_ptr2char_adv(&s);
11772 ++len;
11773 }
11774 rettv->vval.v_number = len;
11775#else
11776 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
11777#endif
11778 }
11779}
11780
11781/*
11782 * "strdisplaywidth()" function
11783 */
11784 static void
11785f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
11786{
11787 char_u *s = get_tv_string(&argvars[0]);
11788 int col = 0;
11789
11790 if (argvars[1].v_type != VAR_UNKNOWN)
11791 col = (int)get_tv_number(&argvars[1]);
11792
11793 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
11794}
11795
11796/*
11797 * "strwidth()" function
11798 */
11799 static void
11800f_strwidth(typval_T *argvars, typval_T *rettv)
11801{
11802 char_u *s = get_tv_string(&argvars[0]);
11803
11804 rettv->vval.v_number = (varnumber_T)(
11805#ifdef FEAT_MBYTE
11806 mb_string2cells(s, -1)
11807#else
11808 STRLEN(s)
11809#endif
11810 );
11811}
11812
11813/*
11814 * "strcharpart()" function
11815 */
11816 static void
11817f_strcharpart(typval_T *argvars, typval_T *rettv)
11818{
11819#ifdef FEAT_MBYTE
11820 char_u *p;
11821 int nchar;
11822 int nbyte = 0;
11823 int charlen;
11824 int len = 0;
11825 int slen;
11826 int error = FALSE;
11827
11828 p = get_tv_string(&argvars[0]);
11829 slen = (int)STRLEN(p);
11830
11831 nchar = (int)get_tv_number_chk(&argvars[1], &error);
11832 if (!error)
11833 {
11834 if (nchar > 0)
11835 while (nchar > 0 && nbyte < slen)
11836 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011837 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011838 --nchar;
11839 }
11840 else
11841 nbyte = nchar;
11842 if (argvars[2].v_type != VAR_UNKNOWN)
11843 {
11844 charlen = (int)get_tv_number(&argvars[2]);
11845 while (charlen > 0 && nbyte + len < slen)
11846 {
11847 int off = nbyte + len;
11848
11849 if (off < 0)
11850 len += 1;
11851 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011852 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011853 --charlen;
11854 }
11855 }
11856 else
11857 len = slen - nbyte; /* default: all bytes that are available. */
11858 }
11859
11860 /*
11861 * Only return the overlap between the specified part and the actual
11862 * string.
11863 */
11864 if (nbyte < 0)
11865 {
11866 len += nbyte;
11867 nbyte = 0;
11868 }
11869 else if (nbyte > slen)
11870 nbyte = slen;
11871 if (len < 0)
11872 len = 0;
11873 else if (nbyte + len > slen)
11874 len = slen - nbyte;
11875
11876 rettv->v_type = VAR_STRING;
11877 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
11878#else
11879 f_strpart(argvars, rettv);
11880#endif
11881}
11882
11883/*
11884 * "strpart()" function
11885 */
11886 static void
11887f_strpart(typval_T *argvars, typval_T *rettv)
11888{
11889 char_u *p;
11890 int n;
11891 int len;
11892 int slen;
11893 int error = FALSE;
11894
11895 p = get_tv_string(&argvars[0]);
11896 slen = (int)STRLEN(p);
11897
11898 n = (int)get_tv_number_chk(&argvars[1], &error);
11899 if (error)
11900 len = 0;
11901 else if (argvars[2].v_type != VAR_UNKNOWN)
11902 len = (int)get_tv_number(&argvars[2]);
11903 else
11904 len = slen - n; /* default len: all bytes that are available. */
11905
11906 /*
11907 * Only return the overlap between the specified part and the actual
11908 * string.
11909 */
11910 if (n < 0)
11911 {
11912 len += n;
11913 n = 0;
11914 }
11915 else if (n > slen)
11916 n = slen;
11917 if (len < 0)
11918 len = 0;
11919 else if (n + len > slen)
11920 len = slen - n;
11921
11922 rettv->v_type = VAR_STRING;
11923 rettv->vval.v_string = vim_strnsave(p + n, len);
11924}
11925
11926/*
11927 * "strridx()" function
11928 */
11929 static void
11930f_strridx(typval_T *argvars, typval_T *rettv)
11931{
11932 char_u buf[NUMBUFLEN];
11933 char_u *needle;
11934 char_u *haystack;
11935 char_u *rest;
11936 char_u *lastmatch = NULL;
11937 int haystack_len, end_idx;
11938
11939 needle = get_tv_string_chk(&argvars[1]);
11940 haystack = get_tv_string_buf_chk(&argvars[0], buf);
11941
11942 rettv->vval.v_number = -1;
11943 if (needle == NULL || haystack == NULL)
11944 return; /* type error; errmsg already given */
11945
11946 haystack_len = (int)STRLEN(haystack);
11947 if (argvars[2].v_type != VAR_UNKNOWN)
11948 {
11949 /* Third argument: upper limit for index */
11950 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
11951 if (end_idx < 0)
11952 return; /* can never find a match */
11953 }
11954 else
11955 end_idx = haystack_len;
11956
11957 if (*needle == NUL)
11958 {
11959 /* Empty string matches past the end. */
11960 lastmatch = haystack + end_idx;
11961 }
11962 else
11963 {
11964 for (rest = haystack; *rest != '\0'; ++rest)
11965 {
11966 rest = (char_u *)strstr((char *)rest, (char *)needle);
11967 if (rest == NULL || rest > haystack + end_idx)
11968 break;
11969 lastmatch = rest;
11970 }
11971 }
11972
11973 if (lastmatch == NULL)
11974 rettv->vval.v_number = -1;
11975 else
11976 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
11977}
11978
11979/*
11980 * "strtrans()" function
11981 */
11982 static void
11983f_strtrans(typval_T *argvars, typval_T *rettv)
11984{
11985 rettv->v_type = VAR_STRING;
11986 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
11987}
11988
11989/*
11990 * "submatch()" function
11991 */
11992 static void
11993f_submatch(typval_T *argvars, typval_T *rettv)
11994{
11995 int error = FALSE;
11996 int no;
11997 int retList = 0;
11998
11999 no = (int)get_tv_number_chk(&argvars[0], &error);
12000 if (error)
12001 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012002 if (no < 0 || no >= NSUBEXP)
12003 {
Bram Moolenaar79518e22017-02-17 16:31:35 +010012004 EMSGN(_("E935: invalid submatch number: %d"), no);
12005 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012006 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012007 if (argvars[1].v_type != VAR_UNKNOWN)
12008 retList = (int)get_tv_number_chk(&argvars[1], &error);
12009 if (error)
12010 return;
12011
12012 if (retList == 0)
12013 {
12014 rettv->v_type = VAR_STRING;
12015 rettv->vval.v_string = reg_submatch(no);
12016 }
12017 else
12018 {
12019 rettv->v_type = VAR_LIST;
12020 rettv->vval.v_list = reg_submatch_list(no);
12021 }
12022}
12023
12024/*
12025 * "substitute()" function
12026 */
12027 static void
12028f_substitute(typval_T *argvars, typval_T *rettv)
12029{
12030 char_u patbuf[NUMBUFLEN];
12031 char_u subbuf[NUMBUFLEN];
12032 char_u flagsbuf[NUMBUFLEN];
12033
12034 char_u *str = get_tv_string_chk(&argvars[0]);
12035 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012036 char_u *sub = NULL;
12037 typval_T *expr = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012038 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
12039
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012040 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
12041 expr = &argvars[2];
12042 else
12043 sub = get_tv_string_buf_chk(&argvars[2], subbuf);
12044
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012045 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012046 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
12047 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012048 rettv->vval.v_string = NULL;
12049 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012050 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012051}
12052
12053/*
12054 * "synID(lnum, col, trans)" function
12055 */
12056 static void
12057f_synID(typval_T *argvars UNUSED, typval_T *rettv)
12058{
12059 int id = 0;
12060#ifdef FEAT_SYN_HL
12061 linenr_T lnum;
12062 colnr_T col;
12063 int trans;
12064 int transerr = FALSE;
12065
12066 lnum = get_tv_lnum(argvars); /* -1 on type error */
12067 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12068 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
12069
12070 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12071 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
12072 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
12073#endif
12074
12075 rettv->vval.v_number = id;
12076}
12077
12078/*
12079 * "synIDattr(id, what [, mode])" function
12080 */
12081 static void
12082f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
12083{
12084 char_u *p = NULL;
12085#ifdef FEAT_SYN_HL
12086 int id;
12087 char_u *what;
12088 char_u *mode;
12089 char_u modebuf[NUMBUFLEN];
12090 int modec;
12091
12092 id = (int)get_tv_number(&argvars[0]);
12093 what = get_tv_string(&argvars[1]);
12094 if (argvars[2].v_type != VAR_UNKNOWN)
12095 {
12096 mode = get_tv_string_buf(&argvars[2], modebuf);
12097 modec = TOLOWER_ASC(mode[0]);
12098 if (modec != 't' && modec != 'c' && modec != 'g')
12099 modec = 0; /* replace invalid with current */
12100 }
12101 else
12102 {
12103#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
12104 if (USE_24BIT)
12105 modec = 'g';
12106 else
12107#endif
12108 if (t_colors > 1)
12109 modec = 'c';
12110 else
12111 modec = 't';
12112 }
12113
12114
12115 switch (TOLOWER_ASC(what[0]))
12116 {
12117 case 'b':
12118 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12119 p = highlight_color(id, what, modec);
12120 else /* bold */
12121 p = highlight_has_attr(id, HL_BOLD, modec);
12122 break;
12123
12124 case 'f': /* fg[#] or font */
12125 p = highlight_color(id, what, modec);
12126 break;
12127
12128 case 'i':
12129 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
12130 p = highlight_has_attr(id, HL_INVERSE, modec);
12131 else /* italic */
12132 p = highlight_has_attr(id, HL_ITALIC, modec);
12133 break;
12134
12135 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020012136 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012137 break;
12138
12139 case 'r': /* reverse */
12140 p = highlight_has_attr(id, HL_INVERSE, modec);
12141 break;
12142
12143 case 's':
12144 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
12145 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020012146 /* strikeout */
12147 else if (TOLOWER_ASC(what[1]) == 't' &&
12148 TOLOWER_ASC(what[2]) == 'r')
12149 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012150 else /* standout */
12151 p = highlight_has_attr(id, HL_STANDOUT, modec);
12152 break;
12153
12154 case 'u':
12155 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
12156 /* underline */
12157 p = highlight_has_attr(id, HL_UNDERLINE, modec);
12158 else
12159 /* undercurl */
12160 p = highlight_has_attr(id, HL_UNDERCURL, modec);
12161 break;
12162 }
12163
12164 if (p != NULL)
12165 p = vim_strsave(p);
12166#endif
12167 rettv->v_type = VAR_STRING;
12168 rettv->vval.v_string = p;
12169}
12170
12171/*
12172 * "synIDtrans(id)" function
12173 */
12174 static void
12175f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
12176{
12177 int id;
12178
12179#ifdef FEAT_SYN_HL
12180 id = (int)get_tv_number(&argvars[0]);
12181
12182 if (id > 0)
12183 id = syn_get_final_id(id);
12184 else
12185#endif
12186 id = 0;
12187
12188 rettv->vval.v_number = id;
12189}
12190
12191/*
12192 * "synconcealed(lnum, col)" function
12193 */
12194 static void
12195f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
12196{
12197#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12198 linenr_T lnum;
12199 colnr_T col;
12200 int syntax_flags = 0;
12201 int cchar;
12202 int matchid = 0;
12203 char_u str[NUMBUFLEN];
12204#endif
12205
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012206 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012207
12208#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12209 lnum = get_tv_lnum(argvars); /* -1 on type error */
12210 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12211
12212 vim_memset(str, NUL, sizeof(str));
12213
12214 if (rettv_list_alloc(rettv) != FAIL)
12215 {
12216 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12217 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12218 && curwin->w_p_cole > 0)
12219 {
12220 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
12221 syntax_flags = get_syntax_info(&matchid);
12222
12223 /* get the conceal character */
12224 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
12225 {
12226 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020012227 if (cchar == NUL && curwin->w_p_cole == 1)
12228 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012229 if (cchar != NUL)
12230 {
12231# ifdef FEAT_MBYTE
12232 if (has_mbyte)
12233 (*mb_char2bytes)(cchar, str);
12234 else
12235# endif
12236 str[0] = cchar;
12237 }
12238 }
12239 }
12240
12241 list_append_number(rettv->vval.v_list,
12242 (syntax_flags & HL_CONCEAL) != 0);
12243 /* -1 to auto-determine strlen */
12244 list_append_string(rettv->vval.v_list, str, -1);
12245 list_append_number(rettv->vval.v_list, matchid);
12246 }
12247#endif
12248}
12249
12250/*
12251 * "synstack(lnum, col)" function
12252 */
12253 static void
12254f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
12255{
12256#ifdef FEAT_SYN_HL
12257 linenr_T lnum;
12258 colnr_T col;
12259 int i;
12260 int id;
12261#endif
12262
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012263 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012264
12265#ifdef FEAT_SYN_HL
12266 lnum = get_tv_lnum(argvars); /* -1 on type error */
12267 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12268
12269 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12270 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12271 && rettv_list_alloc(rettv) != FAIL)
12272 {
12273 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
12274 for (i = 0; ; ++i)
12275 {
12276 id = syn_get_stack_item(i);
12277 if (id < 0)
12278 break;
12279 if (list_append_number(rettv->vval.v_list, id) == FAIL)
12280 break;
12281 }
12282 }
12283#endif
12284}
12285
12286 static void
12287get_cmd_output_as_rettv(
12288 typval_T *argvars,
12289 typval_T *rettv,
12290 int retlist)
12291{
12292 char_u *res = NULL;
12293 char_u *p;
12294 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012295 int err = FALSE;
12296 FILE *fd;
12297 list_T *list = NULL;
12298 int flags = SHELL_SILENT;
12299
12300 rettv->v_type = VAR_STRING;
12301 rettv->vval.v_string = NULL;
12302 if (check_restricted() || check_secure())
12303 goto errret;
12304
12305 if (argvars[1].v_type != VAR_UNKNOWN)
12306 {
12307 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010012308 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012309 * command.
12310 */
12311 if ((infile = vim_tempname('i', TRUE)) == NULL)
12312 {
12313 EMSG(_(e_notmp));
12314 goto errret;
12315 }
12316
12317 fd = mch_fopen((char *)infile, WRITEBIN);
12318 if (fd == NULL)
12319 {
12320 EMSG2(_(e_notopen), infile);
12321 goto errret;
12322 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010012323 if (argvars[1].v_type == VAR_NUMBER)
12324 {
12325 linenr_T lnum;
12326 buf_T *buf;
12327
12328 buf = buflist_findnr(argvars[1].vval.v_number);
12329 if (buf == NULL)
12330 {
12331 EMSGN(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010012332 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010012333 goto errret;
12334 }
12335
12336 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
12337 {
12338 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
12339 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
12340 {
12341 err = TRUE;
12342 break;
12343 }
12344 if (putc(NL, fd) == EOF)
12345 {
12346 err = TRUE;
12347 break;
12348 }
12349 }
12350 }
12351 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012352 {
12353 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
12354 err = TRUE;
12355 }
12356 else
12357 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010012358 size_t len;
12359 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012360
12361 p = get_tv_string_buf_chk(&argvars[1], buf);
12362 if (p == NULL)
12363 {
12364 fclose(fd);
12365 goto errret; /* type error; errmsg already given */
12366 }
12367 len = STRLEN(p);
12368 if (len > 0 && fwrite(p, len, 1, fd) != 1)
12369 err = TRUE;
12370 }
12371 if (fclose(fd) != 0)
12372 err = TRUE;
12373 if (err)
12374 {
12375 EMSG(_("E677: Error writing temp file"));
12376 goto errret;
12377 }
12378 }
12379
12380 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
12381 * echoes typeahead, that messes up the display. */
12382 if (!msg_silent)
12383 flags += SHELL_COOKED;
12384
12385 if (retlist)
12386 {
12387 int len;
12388 listitem_T *li;
12389 char_u *s = NULL;
12390 char_u *start;
12391 char_u *end;
12392 int i;
12393
12394 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
12395 if (res == NULL)
12396 goto errret;
12397
12398 list = list_alloc();
12399 if (list == NULL)
12400 goto errret;
12401
12402 for (i = 0; i < len; ++i)
12403 {
12404 start = res + i;
12405 while (i < len && res[i] != NL)
12406 ++i;
12407 end = res + i;
12408
12409 s = alloc((unsigned)(end - start + 1));
12410 if (s == NULL)
12411 goto errret;
12412
12413 for (p = s; start < end; ++p, ++start)
12414 *p = *start == NUL ? NL : *start;
12415 *p = NUL;
12416
12417 li = listitem_alloc();
12418 if (li == NULL)
12419 {
12420 vim_free(s);
12421 goto errret;
12422 }
12423 li->li_tv.v_type = VAR_STRING;
12424 li->li_tv.v_lock = 0;
12425 li->li_tv.vval.v_string = s;
12426 list_append(list, li);
12427 }
12428
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012429 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012430 list = NULL;
12431 }
12432 else
12433 {
12434 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
12435#ifdef USE_CR
12436 /* translate <CR> into <NL> */
12437 if (res != NULL)
12438 {
12439 char_u *s;
12440
12441 for (s = res; *s; ++s)
12442 {
12443 if (*s == CAR)
12444 *s = NL;
12445 }
12446 }
12447#else
12448# ifdef USE_CRNL
12449 /* translate <CR><NL> into <NL> */
12450 if (res != NULL)
12451 {
12452 char_u *s, *d;
12453
12454 d = res;
12455 for (s = res; *s; ++s)
12456 {
12457 if (s[0] == CAR && s[1] == NL)
12458 ++s;
12459 *d++ = *s;
12460 }
12461 *d = NUL;
12462 }
12463# endif
12464#endif
12465 rettv->vval.v_string = res;
12466 res = NULL;
12467 }
12468
12469errret:
12470 if (infile != NULL)
12471 {
12472 mch_remove(infile);
12473 vim_free(infile);
12474 }
12475 if (res != NULL)
12476 vim_free(res);
12477 if (list != NULL)
12478 list_free(list);
12479}
12480
12481/*
12482 * "system()" function
12483 */
12484 static void
12485f_system(typval_T *argvars, typval_T *rettv)
12486{
12487 get_cmd_output_as_rettv(argvars, rettv, FALSE);
12488}
12489
12490/*
12491 * "systemlist()" function
12492 */
12493 static void
12494f_systemlist(typval_T *argvars, typval_T *rettv)
12495{
12496 get_cmd_output_as_rettv(argvars, rettv, TRUE);
12497}
12498
12499/*
12500 * "tabpagebuflist()" function
12501 */
12502 static void
12503f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12504{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012505 tabpage_T *tp;
12506 win_T *wp = NULL;
12507
12508 if (argvars[0].v_type == VAR_UNKNOWN)
12509 wp = firstwin;
12510 else
12511 {
12512 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12513 if (tp != NULL)
12514 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12515 }
12516 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
12517 {
12518 for (; wp != NULL; wp = wp->w_next)
12519 if (list_append_number(rettv->vval.v_list,
12520 wp->w_buffer->b_fnum) == FAIL)
12521 break;
12522 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012523}
12524
12525
12526/*
12527 * "tabpagenr()" function
12528 */
12529 static void
12530f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
12531{
12532 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012533 char_u *arg;
12534
12535 if (argvars[0].v_type != VAR_UNKNOWN)
12536 {
12537 arg = get_tv_string_chk(&argvars[0]);
12538 nr = 0;
12539 if (arg != NULL)
12540 {
12541 if (STRCMP(arg, "$") == 0)
12542 nr = tabpage_index(NULL) - 1;
12543 else
12544 EMSG2(_(e_invexpr2), arg);
12545 }
12546 }
12547 else
12548 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012549 rettv->vval.v_number = nr;
12550}
12551
12552
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012553static int get_winnr(tabpage_T *tp, typval_T *argvar);
12554
12555/*
12556 * Common code for tabpagewinnr() and winnr().
12557 */
12558 static int
12559get_winnr(tabpage_T *tp, typval_T *argvar)
12560{
12561 win_T *twin;
12562 int nr = 1;
12563 win_T *wp;
12564 char_u *arg;
12565
12566 twin = (tp == curtab) ? curwin : tp->tp_curwin;
12567 if (argvar->v_type != VAR_UNKNOWN)
12568 {
12569 arg = get_tv_string_chk(argvar);
12570 if (arg == NULL)
12571 nr = 0; /* type error; errmsg already given */
12572 else if (STRCMP(arg, "$") == 0)
12573 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
12574 else if (STRCMP(arg, "#") == 0)
12575 {
12576 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
12577 if (twin == NULL)
12578 nr = 0;
12579 }
12580 else
12581 {
12582 EMSG2(_(e_invexpr2), arg);
12583 nr = 0;
12584 }
12585 }
12586
12587 if (nr > 0)
12588 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12589 wp != twin; wp = wp->w_next)
12590 {
12591 if (wp == NULL)
12592 {
12593 /* didn't find it in this tabpage */
12594 nr = 0;
12595 break;
12596 }
12597 ++nr;
12598 }
12599 return nr;
12600}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012601
12602/*
12603 * "tabpagewinnr()" function
12604 */
12605 static void
12606f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
12607{
12608 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012609 tabpage_T *tp;
12610
12611 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12612 if (tp == NULL)
12613 nr = 0;
12614 else
12615 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012616 rettv->vval.v_number = nr;
12617}
12618
12619
12620/*
12621 * "tagfiles()" function
12622 */
12623 static void
12624f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
12625{
12626 char_u *fname;
12627 tagname_T tn;
12628 int first;
12629
12630 if (rettv_list_alloc(rettv) == FAIL)
12631 return;
12632 fname = alloc(MAXPATHL);
12633 if (fname == NULL)
12634 return;
12635
12636 for (first = TRUE; ; first = FALSE)
12637 if (get_tagfname(&tn, first, fname) == FAIL
12638 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
12639 break;
12640 tagname_free(&tn);
12641 vim_free(fname);
12642}
12643
12644/*
12645 * "taglist()" function
12646 */
12647 static void
12648f_taglist(typval_T *argvars, typval_T *rettv)
12649{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012650 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012651 char_u *tag_pattern;
12652
12653 tag_pattern = get_tv_string(&argvars[0]);
12654
12655 rettv->vval.v_number = FALSE;
12656 if (*tag_pattern == NUL)
12657 return;
12658
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012659 if (argvars[1].v_type != VAR_UNKNOWN)
12660 fname = get_tv_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012661 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012662 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012663}
12664
12665/*
12666 * "tempname()" function
12667 */
12668 static void
12669f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
12670{
12671 static int x = 'A';
12672
12673 rettv->v_type = VAR_STRING;
12674 rettv->vval.v_string = vim_tempname(x, FALSE);
12675
12676 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
12677 * names. Skip 'I' and 'O', they are used for shell redirection. */
12678 do
12679 {
12680 if (x == 'Z')
12681 x = '0';
12682 else if (x == '9')
12683 x = 'A';
12684 else
12685 {
12686#ifdef EBCDIC
12687 if (x == 'I')
12688 x = 'J';
12689 else if (x == 'R')
12690 x = 'S';
12691 else
12692#endif
12693 ++x;
12694 }
12695 } while (x == 'I' || x == 'O');
12696}
12697
12698#ifdef FEAT_FLOAT
12699/*
12700 * "tan()" function
12701 */
12702 static void
12703f_tan(typval_T *argvars, typval_T *rettv)
12704{
12705 float_T f = 0.0;
12706
12707 rettv->v_type = VAR_FLOAT;
12708 if (get_float_arg(argvars, &f) == OK)
12709 rettv->vval.v_float = tan(f);
12710 else
12711 rettv->vval.v_float = 0.0;
12712}
12713
12714/*
12715 * "tanh()" function
12716 */
12717 static void
12718f_tanh(typval_T *argvars, typval_T *rettv)
12719{
12720 float_T f = 0.0;
12721
12722 rettv->v_type = VAR_FLOAT;
12723 if (get_float_arg(argvars, &f) == OK)
12724 rettv->vval.v_float = tanh(f);
12725 else
12726 rettv->vval.v_float = 0.0;
12727}
12728#endif
12729
12730/*
12731 * "test_alloc_fail(id, countdown, repeat)" function
12732 */
12733 static void
12734f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
12735{
12736 if (argvars[0].v_type != VAR_NUMBER
12737 || argvars[0].vval.v_number <= 0
12738 || argvars[1].v_type != VAR_NUMBER
12739 || argvars[1].vval.v_number < 0
12740 || argvars[2].v_type != VAR_NUMBER)
12741 EMSG(_(e_invarg));
12742 else
12743 {
12744 alloc_fail_id = argvars[0].vval.v_number;
12745 if (alloc_fail_id >= aid_last)
12746 EMSG(_(e_invarg));
12747 alloc_fail_countdown = argvars[1].vval.v_number;
12748 alloc_fail_repeat = argvars[2].vval.v_number;
12749 did_outofmem_msg = FALSE;
12750 }
12751}
12752
12753/*
12754 * "test_autochdir()"
12755 */
12756 static void
12757f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12758{
12759#if defined(FEAT_AUTOCHDIR)
12760 test_autochdir = TRUE;
12761#endif
12762}
12763
12764/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020012765 * "test_feedinput()"
12766 */
12767 static void
12768f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
12769{
12770#ifdef USE_INPUT_BUF
12771 char_u *val = get_tv_string_chk(&argvars[0]);
12772
12773 if (val != NULL)
12774 {
12775 trash_input_buf();
12776 add_to_input_buf_csi(val, (int)STRLEN(val));
12777 }
12778#endif
12779}
12780
12781/*
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012782 * "test_disable({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012783 */
12784 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012785f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012786{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012787 char_u *name = (char_u *)"";
12788 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012789 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012790
12791 if (argvars[0].v_type != VAR_STRING
12792 || (argvars[1].v_type) != VAR_NUMBER)
12793 EMSG(_(e_invarg));
12794 else
12795 {
12796 name = get_tv_string_chk(&argvars[0]);
12797 val = (int)get_tv_number(&argvars[1]);
12798
12799 if (STRCMP(name, (char_u *)"redraw") == 0)
12800 disable_redraw_for_testing = val;
12801 else if (STRCMP(name, (char_u *)"char_avail") == 0)
12802 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012803 else if (STRCMP(name, (char_u *)"starting") == 0)
12804 {
12805 if (val)
12806 {
12807 if (save_starting < 0)
12808 save_starting = starting;
12809 starting = 0;
12810 }
12811 else
12812 {
12813 starting = save_starting;
12814 save_starting = -1;
12815 }
12816 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012817 else if (STRCMP(name, (char_u *)"ALL") == 0)
12818 {
12819 disable_char_avail_for_testing = FALSE;
12820 disable_redraw_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012821 if (save_starting >= 0)
12822 {
12823 starting = save_starting;
12824 save_starting = -1;
12825 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012826 }
12827 else
12828 EMSG2(_(e_invarg2), name);
12829 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012830}
12831
12832/*
12833 * "test_garbagecollect_now()" function
12834 */
12835 static void
12836f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12837{
12838 /* This is dangerous, any Lists and Dicts used internally may be freed
12839 * while still in use. */
12840 garbage_collect(TRUE);
12841}
12842
Bram Moolenaare0c31f62017-03-01 15:07:05 +010012843/*
12844 * "test_ignore_error()" function
12845 */
12846 static void
12847f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
12848{
12849 ignore_error_for_testing(get_tv_string(&argvars[0]));
12850}
12851
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012852#ifdef FEAT_JOB_CHANNEL
12853 static void
12854f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
12855{
12856 rettv->v_type = VAR_CHANNEL;
12857 rettv->vval.v_channel = NULL;
12858}
12859#endif
12860
12861 static void
12862f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
12863{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012864 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012865}
12866
12867#ifdef FEAT_JOB_CHANNEL
12868 static void
12869f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
12870{
12871 rettv->v_type = VAR_JOB;
12872 rettv->vval.v_job = NULL;
12873}
12874#endif
12875
12876 static void
12877f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
12878{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012879 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012880}
12881
12882 static void
12883f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
12884{
12885 rettv->v_type = VAR_PARTIAL;
12886 rettv->vval.v_partial = NULL;
12887}
12888
12889 static void
12890f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
12891{
12892 rettv->v_type = VAR_STRING;
12893 rettv->vval.v_string = NULL;
12894}
12895
12896 static void
12897f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
12898{
12899 time_for_testing = (time_t)get_tv_number(&argvars[0]);
12900}
12901
12902#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
12903/*
12904 * Get a callback from "arg". It can be a Funcref or a function name.
12905 * When "arg" is zero return an empty string.
12906 * Return NULL for an invalid argument.
12907 */
12908 char_u *
12909get_callback(typval_T *arg, partial_T **pp)
12910{
12911 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
12912 {
12913 *pp = arg->vval.v_partial;
12914 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012915 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012916 }
12917 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012918 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012919 {
12920 func_ref(arg->vval.v_string);
12921 return arg->vval.v_string;
12922 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012923 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
12924 return (char_u *)"";
12925 EMSG(_("E921: Invalid callback argument"));
12926 return NULL;
12927}
12928
12929/*
12930 * Unref/free "callback" and "partial" retured by get_callback().
12931 */
12932 void
12933free_callback(char_u *callback, partial_T *partial)
12934{
12935 if (partial != NULL)
12936 partial_unref(partial);
12937 else if (callback != NULL)
12938 {
12939 func_unref(callback);
12940 vim_free(callback);
12941 }
12942}
12943#endif
12944
12945#ifdef FEAT_TIMERS
12946/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020012947 * "timer_info([timer])" function
12948 */
12949 static void
12950f_timer_info(typval_T *argvars, typval_T *rettv)
12951{
12952 timer_T *timer = NULL;
12953
12954 if (rettv_list_alloc(rettv) != OK)
12955 return;
12956 if (argvars[0].v_type != VAR_UNKNOWN)
12957 {
12958 if (argvars[0].v_type != VAR_NUMBER)
12959 EMSG(_(e_number_exp));
12960 else
12961 {
12962 timer = find_timer((int)get_tv_number(&argvars[0]));
12963 if (timer != NULL)
12964 add_timer_info(rettv, timer);
12965 }
12966 }
12967 else
12968 add_timer_info_all(rettv);
12969}
12970
12971/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012972 * "timer_pause(timer, paused)" function
12973 */
12974 static void
12975f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
12976{
12977 timer_T *timer = NULL;
12978 int paused = (int)get_tv_number(&argvars[1]);
12979
12980 if (argvars[0].v_type != VAR_NUMBER)
12981 EMSG(_(e_number_exp));
12982 else
12983 {
12984 timer = find_timer((int)get_tv_number(&argvars[0]));
12985 if (timer != NULL)
12986 timer->tr_paused = paused;
12987 }
12988}
12989
12990/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012991 * "timer_start(time, callback [, options])" function
12992 */
12993 static void
12994f_timer_start(typval_T *argvars, typval_T *rettv)
12995{
Bram Moolenaar75537a92016-09-05 22:45:28 +020012996 long msec = (long)get_tv_number(&argvars[0]);
12997 timer_T *timer;
12998 int repeat = 0;
12999 char_u *callback;
13000 dict_T *dict;
13001 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013002
Bram Moolenaar75537a92016-09-05 22:45:28 +020013003 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013004 if (check_secure())
13005 return;
13006 if (argvars[2].v_type != VAR_UNKNOWN)
13007 {
13008 if (argvars[2].v_type != VAR_DICT
13009 || (dict = argvars[2].vval.v_dict) == NULL)
13010 {
13011 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
13012 return;
13013 }
13014 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
13015 repeat = get_dict_number(dict, (char_u *)"repeat");
13016 }
13017
Bram Moolenaar75537a92016-09-05 22:45:28 +020013018 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013019 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020013020 return;
13021
13022 timer = create_timer(msec, repeat);
13023 if (timer == NULL)
13024 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013025 else
13026 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020013027 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020013028 timer->tr_callback = vim_strsave(callback);
13029 else
13030 /* pointer into the partial */
13031 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020013032 timer->tr_partial = partial;
13033 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013034 }
13035}
13036
13037/*
13038 * "timer_stop(timer)" function
13039 */
13040 static void
13041f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
13042{
13043 timer_T *timer;
13044
13045 if (argvars[0].v_type != VAR_NUMBER)
13046 {
13047 EMSG(_(e_number_exp));
13048 return;
13049 }
13050 timer = find_timer((int)get_tv_number(&argvars[0]));
13051 if (timer != NULL)
13052 stop_timer(timer);
13053}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013054
13055/*
13056 * "timer_stopall()" function
13057 */
13058 static void
13059f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13060{
13061 stop_all_timers();
13062}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013063#endif
13064
13065/*
13066 * "tolower(string)" function
13067 */
13068 static void
13069f_tolower(typval_T *argvars, typval_T *rettv)
13070{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013071 rettv->v_type = VAR_STRING;
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +010013072 rettv->vval.v_string = strlow_save(get_tv_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013073}
13074
13075/*
13076 * "toupper(string)" function
13077 */
13078 static void
13079f_toupper(typval_T *argvars, typval_T *rettv)
13080{
13081 rettv->v_type = VAR_STRING;
13082 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
13083}
13084
13085/*
13086 * "tr(string, fromstr, tostr)" function
13087 */
13088 static void
13089f_tr(typval_T *argvars, typval_T *rettv)
13090{
13091 char_u *in_str;
13092 char_u *fromstr;
13093 char_u *tostr;
13094 char_u *p;
13095#ifdef FEAT_MBYTE
13096 int inlen;
13097 int fromlen;
13098 int tolen;
13099 int idx;
13100 char_u *cpstr;
13101 int cplen;
13102 int first = TRUE;
13103#endif
13104 char_u buf[NUMBUFLEN];
13105 char_u buf2[NUMBUFLEN];
13106 garray_T ga;
13107
13108 in_str = get_tv_string(&argvars[0]);
13109 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
13110 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
13111
13112 /* Default return value: empty string. */
13113 rettv->v_type = VAR_STRING;
13114 rettv->vval.v_string = NULL;
13115 if (fromstr == NULL || tostr == NULL)
13116 return; /* type error; errmsg already given */
13117 ga_init2(&ga, (int)sizeof(char), 80);
13118
13119#ifdef FEAT_MBYTE
13120 if (!has_mbyte)
13121#endif
13122 /* not multi-byte: fromstr and tostr must be the same length */
13123 if (STRLEN(fromstr) != STRLEN(tostr))
13124 {
13125#ifdef FEAT_MBYTE
13126error:
13127#endif
13128 EMSG2(_(e_invarg2), fromstr);
13129 ga_clear(&ga);
13130 return;
13131 }
13132
13133 /* fromstr and tostr have to contain the same number of chars */
13134 while (*in_str != NUL)
13135 {
13136#ifdef FEAT_MBYTE
13137 if (has_mbyte)
13138 {
13139 inlen = (*mb_ptr2len)(in_str);
13140 cpstr = in_str;
13141 cplen = inlen;
13142 idx = 0;
13143 for (p = fromstr; *p != NUL; p += fromlen)
13144 {
13145 fromlen = (*mb_ptr2len)(p);
13146 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
13147 {
13148 for (p = tostr; *p != NUL; p += tolen)
13149 {
13150 tolen = (*mb_ptr2len)(p);
13151 if (idx-- == 0)
13152 {
13153 cplen = tolen;
13154 cpstr = p;
13155 break;
13156 }
13157 }
13158 if (*p == NUL) /* tostr is shorter than fromstr */
13159 goto error;
13160 break;
13161 }
13162 ++idx;
13163 }
13164
13165 if (first && cpstr == in_str)
13166 {
13167 /* Check that fromstr and tostr have the same number of
13168 * (multi-byte) characters. Done only once when a character
13169 * of in_str doesn't appear in fromstr. */
13170 first = FALSE;
13171 for (p = tostr; *p != NUL; p += tolen)
13172 {
13173 tolen = (*mb_ptr2len)(p);
13174 --idx;
13175 }
13176 if (idx != 0)
13177 goto error;
13178 }
13179
13180 (void)ga_grow(&ga, cplen);
13181 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
13182 ga.ga_len += cplen;
13183
13184 in_str += inlen;
13185 }
13186 else
13187#endif
13188 {
13189 /* When not using multi-byte chars we can do it faster. */
13190 p = vim_strchr(fromstr, *in_str);
13191 if (p != NULL)
13192 ga_append(&ga, tostr[p - fromstr]);
13193 else
13194 ga_append(&ga, *in_str);
13195 ++in_str;
13196 }
13197 }
13198
13199 /* add a terminating NUL */
13200 (void)ga_grow(&ga, 1);
13201 ga_append(&ga, NUL);
13202
13203 rettv->vval.v_string = ga.ga_data;
13204}
13205
13206#ifdef FEAT_FLOAT
13207/*
13208 * "trunc({float})" function
13209 */
13210 static void
13211f_trunc(typval_T *argvars, typval_T *rettv)
13212{
13213 float_T f = 0.0;
13214
13215 rettv->v_type = VAR_FLOAT;
13216 if (get_float_arg(argvars, &f) == OK)
13217 /* trunc() is not in C90, use floor() or ceil() instead. */
13218 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
13219 else
13220 rettv->vval.v_float = 0.0;
13221}
13222#endif
13223
13224/*
13225 * "type(expr)" function
13226 */
13227 static void
13228f_type(typval_T *argvars, typval_T *rettv)
13229{
13230 int n = -1;
13231
13232 switch (argvars[0].v_type)
13233 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020013234 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
13235 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013236 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020013237 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
13238 case VAR_LIST: n = VAR_TYPE_LIST; break;
13239 case VAR_DICT: n = VAR_TYPE_DICT; break;
13240 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013241 case VAR_SPECIAL:
13242 if (argvars[0].vval.v_number == VVAL_FALSE
13243 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020013244 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013245 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020013246 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013247 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020013248 case VAR_JOB: n = VAR_TYPE_JOB; break;
13249 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013250 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010013251 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013252 n = -1;
13253 break;
13254 }
13255 rettv->vval.v_number = n;
13256}
13257
13258/*
13259 * "undofile(name)" function
13260 */
13261 static void
13262f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
13263{
13264 rettv->v_type = VAR_STRING;
13265#ifdef FEAT_PERSISTENT_UNDO
13266 {
13267 char_u *fname = get_tv_string(&argvars[0]);
13268
13269 if (*fname == NUL)
13270 {
13271 /* If there is no file name there will be no undo file. */
13272 rettv->vval.v_string = NULL;
13273 }
13274 else
13275 {
13276 char_u *ffname = FullName_save(fname, FALSE);
13277
13278 if (ffname != NULL)
13279 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
13280 vim_free(ffname);
13281 }
13282 }
13283#else
13284 rettv->vval.v_string = NULL;
13285#endif
13286}
13287
13288/*
13289 * "undotree()" function
13290 */
13291 static void
13292f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
13293{
13294 if (rettv_dict_alloc(rettv) == OK)
13295 {
13296 dict_T *dict = rettv->vval.v_dict;
13297 list_T *list;
13298
13299 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
13300 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
13301 dict_add_nr_str(dict, "save_last",
13302 (long)curbuf->b_u_save_nr_last, NULL);
13303 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
13304 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
13305 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
13306
13307 list = list_alloc();
13308 if (list != NULL)
13309 {
13310 u_eval_tree(curbuf->b_u_oldhead, list);
13311 dict_add_list(dict, "entries", list);
13312 }
13313 }
13314}
13315
13316/*
13317 * "values(dict)" function
13318 */
13319 static void
13320f_values(typval_T *argvars, typval_T *rettv)
13321{
13322 dict_list(argvars, rettv, 1);
13323}
13324
13325/*
13326 * "virtcol(string)" function
13327 */
13328 static void
13329f_virtcol(typval_T *argvars, typval_T *rettv)
13330{
13331 colnr_T vcol = 0;
13332 pos_T *fp;
13333 int fnum = curbuf->b_fnum;
13334
13335 fp = var2fpos(&argvars[0], FALSE, &fnum);
13336 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
13337 && fnum == curbuf->b_fnum)
13338 {
13339 getvvcol(curwin, fp, NULL, NULL, &vcol);
13340 ++vcol;
13341 }
13342
13343 rettv->vval.v_number = vcol;
13344}
13345
13346/*
13347 * "visualmode()" function
13348 */
13349 static void
13350f_visualmode(typval_T *argvars, typval_T *rettv)
13351{
13352 char_u str[2];
13353
13354 rettv->v_type = VAR_STRING;
13355 str[0] = curbuf->b_visual_mode_eval;
13356 str[1] = NUL;
13357 rettv->vval.v_string = vim_strsave(str);
13358
13359 /* A non-zero number or non-empty string argument: reset mode. */
13360 if (non_zero_arg(&argvars[0]))
13361 curbuf->b_visual_mode_eval = NUL;
13362}
13363
13364/*
13365 * "wildmenumode()" function
13366 */
13367 static void
13368f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13369{
13370#ifdef FEAT_WILDMENU
13371 if (wild_menu_showing)
13372 rettv->vval.v_number = 1;
13373#endif
13374}
13375
13376/*
13377 * "winbufnr(nr)" function
13378 */
13379 static void
13380f_winbufnr(typval_T *argvars, typval_T *rettv)
13381{
13382 win_T *wp;
13383
13384 wp = find_win_by_nr(&argvars[0], NULL);
13385 if (wp == NULL)
13386 rettv->vval.v_number = -1;
13387 else
13388 rettv->vval.v_number = wp->w_buffer->b_fnum;
13389}
13390
13391/*
13392 * "wincol()" function
13393 */
13394 static void
13395f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
13396{
13397 validate_cursor();
13398 rettv->vval.v_number = curwin->w_wcol + 1;
13399}
13400
13401/*
13402 * "winheight(nr)" function
13403 */
13404 static void
13405f_winheight(typval_T *argvars, typval_T *rettv)
13406{
13407 win_T *wp;
13408
13409 wp = find_win_by_nr(&argvars[0], NULL);
13410 if (wp == NULL)
13411 rettv->vval.v_number = -1;
13412 else
13413 rettv->vval.v_number = wp->w_height;
13414}
13415
13416/*
13417 * "winline()" function
13418 */
13419 static void
13420f_winline(typval_T *argvars UNUSED, typval_T *rettv)
13421{
13422 validate_cursor();
13423 rettv->vval.v_number = curwin->w_wrow + 1;
13424}
13425
13426/*
13427 * "winnr()" function
13428 */
13429 static void
13430f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
13431{
13432 int nr = 1;
13433
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013434 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013435 rettv->vval.v_number = nr;
13436}
13437
13438/*
13439 * "winrestcmd()" function
13440 */
13441 static void
13442f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
13443{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013444 win_T *wp;
13445 int winnr = 1;
13446 garray_T ga;
13447 char_u buf[50];
13448
13449 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020013450 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013451 {
13452 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
13453 ga_concat(&ga, buf);
13454 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
13455 ga_concat(&ga, buf);
13456 ++winnr;
13457 }
13458 ga_append(&ga, NUL);
13459
13460 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013461 rettv->v_type = VAR_STRING;
13462}
13463
13464/*
13465 * "winrestview()" function
13466 */
13467 static void
13468f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
13469{
13470 dict_T *dict;
13471
13472 if (argvars[0].v_type != VAR_DICT
13473 || (dict = argvars[0].vval.v_dict) == NULL)
13474 EMSG(_(e_invarg));
13475 else
13476 {
13477 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
13478 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
13479 if (dict_find(dict, (char_u *)"col", -1) != NULL)
13480 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
13481#ifdef FEAT_VIRTUALEDIT
13482 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
13483 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
13484#endif
13485 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
13486 {
13487 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
13488 curwin->w_set_curswant = FALSE;
13489 }
13490
13491 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
13492 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
13493#ifdef FEAT_DIFF
13494 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
13495 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
13496#endif
13497 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
13498 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
13499 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
13500 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
13501
13502 check_cursor();
13503 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020013504 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013505 changed_window_setting();
13506
13507 if (curwin->w_topline <= 0)
13508 curwin->w_topline = 1;
13509 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
13510 curwin->w_topline = curbuf->b_ml.ml_line_count;
13511#ifdef FEAT_DIFF
13512 check_topfill(curwin, TRUE);
13513#endif
13514 }
13515}
13516
13517/*
13518 * "winsaveview()" function
13519 */
13520 static void
13521f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
13522{
13523 dict_T *dict;
13524
13525 if (rettv_dict_alloc(rettv) == FAIL)
13526 return;
13527 dict = rettv->vval.v_dict;
13528
13529 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
13530 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
13531#ifdef FEAT_VIRTUALEDIT
13532 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
13533#endif
13534 update_curswant();
13535 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
13536
13537 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
13538#ifdef FEAT_DIFF
13539 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
13540#endif
13541 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
13542 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
13543}
13544
13545/*
13546 * "winwidth(nr)" function
13547 */
13548 static void
13549f_winwidth(typval_T *argvars, typval_T *rettv)
13550{
13551 win_T *wp;
13552
13553 wp = find_win_by_nr(&argvars[0], NULL);
13554 if (wp == NULL)
13555 rettv->vval.v_number = -1;
13556 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013557 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013558}
13559
13560/*
13561 * "wordcount()" function
13562 */
13563 static void
13564f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
13565{
13566 if (rettv_dict_alloc(rettv) == FAIL)
13567 return;
13568 cursor_pos_info(rettv->vval.v_dict);
13569}
13570
13571/*
13572 * "writefile()" function
13573 */
13574 static void
13575f_writefile(typval_T *argvars, typval_T *rettv)
13576{
13577 int binary = FALSE;
13578 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013579#ifdef HAVE_FSYNC
13580 int do_fsync = p_fs;
13581#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013582 char_u *fname;
13583 FILE *fd;
13584 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013585 listitem_T *li;
13586 list_T *list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013587
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013588 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013589 if (check_restricted() || check_secure())
13590 return;
13591
13592 if (argvars[0].v_type != VAR_LIST)
13593 {
13594 EMSG2(_(e_listarg), "writefile()");
13595 return;
13596 }
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013597 list = argvars[0].vval.v_list;
13598 if (list == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013599 return;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013600 for (li = list->lv_first; li != NULL; li = li->li_next)
13601 if (get_tv_string_chk(&li->li_tv) == NULL)
13602 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013603
13604 if (argvars[2].v_type != VAR_UNKNOWN)
13605 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013606 char_u *arg2 = get_tv_string_chk(&argvars[2]);
13607
13608 if (arg2 == NULL)
13609 return;
13610 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013611 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013612 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013613 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013614#ifdef HAVE_FSYNC
13615 if (vim_strchr(arg2, 's') != NULL)
13616 do_fsync = TRUE;
13617 else if (vim_strchr(arg2, 'S') != NULL)
13618 do_fsync = FALSE;
13619#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013620 }
13621
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013622 fname = get_tv_string_chk(&argvars[1]);
13623 if (fname == NULL)
13624 return;
13625
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013626 /* Always open the file in binary mode, library functions have a mind of
13627 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013628 if (*fname == NUL || (fd = mch_fopen((char *)fname,
13629 append ? APPENDBIN : WRITEBIN)) == NULL)
13630 {
13631 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
13632 ret = -1;
13633 }
13634 else
13635 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013636 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013637 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013638#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010013639 else if (do_fsync)
13640 /* Ignore the error, the user wouldn't know what to do about it.
13641 * May happen for a device. */
13642 ignored = fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013643#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013644 fclose(fd);
13645 }
13646
13647 rettv->vval.v_number = ret;
13648}
13649
13650/*
13651 * "xor(expr, expr)" function
13652 */
13653 static void
13654f_xor(typval_T *argvars, typval_T *rettv)
13655{
13656 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
13657 ^ get_tv_number_chk(&argvars[1], NULL);
13658}
13659
13660
13661#endif /* FEAT_EVAL */