blob: 65279a52f8b4e54ae08061c43b1bbba0bb13d10e [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);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100433static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200434#ifdef FEAT_FLOAT
435static void f_trunc(typval_T *argvars, typval_T *rettv);
436#endif
437static void f_type(typval_T *argvars, typval_T *rettv);
438static void f_undofile(typval_T *argvars, typval_T *rettv);
439static void f_undotree(typval_T *argvars, typval_T *rettv);
440static void f_uniq(typval_T *argvars, typval_T *rettv);
441static void f_values(typval_T *argvars, typval_T *rettv);
442static void f_virtcol(typval_T *argvars, typval_T *rettv);
443static void f_visualmode(typval_T *argvars, typval_T *rettv);
444static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
445static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
446static void f_win_getid(typval_T *argvars, typval_T *rettv);
447static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
448static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
449static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100450static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200451static void f_winbufnr(typval_T *argvars, typval_T *rettv);
452static void f_wincol(typval_T *argvars, typval_T *rettv);
453static void f_winheight(typval_T *argvars, typval_T *rettv);
454static void f_winline(typval_T *argvars, typval_T *rettv);
455static void f_winnr(typval_T *argvars, typval_T *rettv);
456static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
457static void f_winrestview(typval_T *argvars, typval_T *rettv);
458static void f_winsaveview(typval_T *argvars, typval_T *rettv);
459static void f_winwidth(typval_T *argvars, typval_T *rettv);
460static void f_writefile(typval_T *argvars, typval_T *rettv);
461static void f_wordcount(typval_T *argvars, typval_T *rettv);
462static void f_xor(typval_T *argvars, typval_T *rettv);
463
464/*
465 * Array with names and number of arguments of all internal functions
466 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
467 */
468static struct fst
469{
470 char *f_name; /* function name */
471 char f_min_argc; /* minimal number of arguments */
472 char f_max_argc; /* maximal number of arguments */
473 void (*f_func)(typval_T *args, typval_T *rvar);
474 /* implementation of function */
475} functions[] =
476{
477#ifdef FEAT_FLOAT
478 {"abs", 1, 1, f_abs},
479 {"acos", 1, 1, f_acos}, /* WJMc */
480#endif
481 {"add", 2, 2, f_add},
482 {"and", 2, 2, f_and},
483 {"append", 2, 2, f_append},
484 {"argc", 0, 0, f_argc},
485 {"argidx", 0, 0, f_argidx},
486 {"arglistid", 0, 2, f_arglistid},
487 {"argv", 0, 1, f_argv},
488#ifdef FEAT_FLOAT
489 {"asin", 1, 1, f_asin}, /* WJMc */
490#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100491 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200492 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaard96ff162018-02-18 22:13:29 +0100493 {"assert_equalfile", 2, 2, f_assert_equalfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200494 {"assert_exception", 1, 2, f_assert_exception},
495 {"assert_fails", 1, 2, f_assert_fails},
496 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100497 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200498 {"assert_match", 2, 3, f_assert_match},
499 {"assert_notequal", 2, 3, f_assert_notequal},
500 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100501 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200502 {"assert_true", 1, 2, f_assert_true},
503#ifdef FEAT_FLOAT
504 {"atan", 1, 1, f_atan},
505 {"atan2", 2, 2, f_atan2},
506#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100507#ifdef FEAT_BEVAL
508 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100509# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100510 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100511# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100512#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200513 {"browse", 4, 4, f_browse},
514 {"browsedir", 2, 2, f_browsedir},
515 {"bufexists", 1, 1, f_bufexists},
516 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
517 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
518 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
519 {"buflisted", 1, 1, f_buflisted},
520 {"bufloaded", 1, 1, f_bufloaded},
521 {"bufname", 1, 1, f_bufname},
522 {"bufnr", 1, 2, f_bufnr},
523 {"bufwinid", 1, 1, f_bufwinid},
524 {"bufwinnr", 1, 1, f_bufwinnr},
525 {"byte2line", 1, 1, f_byte2line},
526 {"byteidx", 2, 2, f_byteidx},
527 {"byteidxcomp", 2, 2, f_byteidxcomp},
528 {"call", 2, 3, f_call},
529#ifdef FEAT_FLOAT
530 {"ceil", 1, 1, f_ceil},
531#endif
532#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100533 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200534 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200535 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200536 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
537 {"ch_evalraw", 2, 3, f_ch_evalraw},
538 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
539 {"ch_getjob", 1, 1, f_ch_getjob},
540 {"ch_info", 1, 1, f_ch_info},
541 {"ch_log", 1, 2, f_ch_log},
542 {"ch_logfile", 1, 2, f_ch_logfile},
543 {"ch_open", 1, 2, f_ch_open},
544 {"ch_read", 1, 2, f_ch_read},
545 {"ch_readraw", 1, 2, f_ch_readraw},
546 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
547 {"ch_sendraw", 2, 3, f_ch_sendraw},
548 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200549 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200550#endif
551 {"changenr", 0, 0, f_changenr},
552 {"char2nr", 1, 2, f_char2nr},
553 {"cindent", 1, 1, f_cindent},
554 {"clearmatches", 0, 0, f_clearmatches},
555 {"col", 1, 1, f_col},
556#if defined(FEAT_INS_EXPAND)
557 {"complete", 2, 2, f_complete},
558 {"complete_add", 1, 1, f_complete_add},
559 {"complete_check", 0, 0, f_complete_check},
560#endif
561 {"confirm", 1, 4, f_confirm},
562 {"copy", 1, 1, f_copy},
563#ifdef FEAT_FLOAT
564 {"cos", 1, 1, f_cos},
565 {"cosh", 1, 1, f_cosh},
566#endif
567 {"count", 2, 4, f_count},
568 {"cscope_connection",0,3, f_cscope_connection},
569 {"cursor", 1, 3, f_cursor},
570 {"deepcopy", 1, 2, f_deepcopy},
571 {"delete", 1, 2, f_delete},
572 {"did_filetype", 0, 0, f_did_filetype},
573 {"diff_filler", 1, 1, f_diff_filler},
574 {"diff_hlID", 2, 2, f_diff_hlID},
575 {"empty", 1, 1, f_empty},
576 {"escape", 2, 2, f_escape},
577 {"eval", 1, 1, f_eval},
578 {"eventhandler", 0, 0, f_eventhandler},
579 {"executable", 1, 1, f_executable},
580 {"execute", 1, 2, f_execute},
581 {"exepath", 1, 1, f_exepath},
582 {"exists", 1, 1, f_exists},
583#ifdef FEAT_FLOAT
584 {"exp", 1, 1, f_exp},
585#endif
586 {"expand", 1, 3, f_expand},
587 {"extend", 2, 3, f_extend},
588 {"feedkeys", 1, 2, f_feedkeys},
589 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
590 {"filereadable", 1, 1, f_filereadable},
591 {"filewritable", 1, 1, f_filewritable},
592 {"filter", 2, 2, f_filter},
593 {"finddir", 1, 3, f_finddir},
594 {"findfile", 1, 3, f_findfile},
595#ifdef FEAT_FLOAT
596 {"float2nr", 1, 1, f_float2nr},
597 {"floor", 1, 1, f_floor},
598 {"fmod", 2, 2, f_fmod},
599#endif
600 {"fnameescape", 1, 1, f_fnameescape},
601 {"fnamemodify", 2, 2, f_fnamemodify},
602 {"foldclosed", 1, 1, f_foldclosed},
603 {"foldclosedend", 1, 1, f_foldclosedend},
604 {"foldlevel", 1, 1, f_foldlevel},
605 {"foldtext", 0, 0, f_foldtext},
606 {"foldtextresult", 1, 1, f_foldtextresult},
607 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200608 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200609 {"function", 1, 3, f_function},
610 {"garbagecollect", 0, 1, f_garbagecollect},
611 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200612 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200613 {"getbufline", 2, 3, f_getbufline},
614 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100615 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200616 {"getchar", 0, 1, f_getchar},
617 {"getcharmod", 0, 0, f_getcharmod},
618 {"getcharsearch", 0, 0, f_getcharsearch},
619 {"getcmdline", 0, 0, f_getcmdline},
620 {"getcmdpos", 0, 0, f_getcmdpos},
621 {"getcmdtype", 0, 0, f_getcmdtype},
622 {"getcmdwintype", 0, 0, f_getcmdwintype},
623#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200624 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200625#endif
626 {"getcurpos", 0, 0, f_getcurpos},
627 {"getcwd", 0, 2, f_getcwd},
628 {"getfontname", 0, 1, f_getfontname},
629 {"getfperm", 1, 1, f_getfperm},
630 {"getfsize", 1, 1, f_getfsize},
631 {"getftime", 1, 1, f_getftime},
632 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100633 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200634 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200635 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200636 {"getmatches", 0, 0, f_getmatches},
637 {"getpid", 0, 0, f_getpid},
638 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200639 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200640 {"getreg", 0, 3, f_getreg},
641 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200642 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200643 {"gettabvar", 2, 3, f_gettabvar},
644 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200645 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100646 {"getwinpos", 0, 1, f_getwinpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200647 {"getwinposx", 0, 0, f_getwinposx},
648 {"getwinposy", 0, 0, f_getwinposy},
649 {"getwinvar", 2, 3, f_getwinvar},
650 {"glob", 1, 4, f_glob},
651 {"glob2regpat", 1, 1, f_glob2regpat},
652 {"globpath", 2, 5, f_globpath},
653 {"has", 1, 1, f_has},
654 {"has_key", 2, 2, f_has_key},
655 {"haslocaldir", 0, 2, f_haslocaldir},
656 {"hasmapto", 1, 3, f_hasmapto},
657 {"highlightID", 1, 1, f_hlID}, /* obsolete */
658 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
659 {"histadd", 2, 2, f_histadd},
660 {"histdel", 1, 2, f_histdel},
661 {"histget", 1, 2, f_histget},
662 {"histnr", 1, 1, f_histnr},
663 {"hlID", 1, 1, f_hlID},
664 {"hlexists", 1, 1, f_hlexists},
665 {"hostname", 0, 0, f_hostname},
666 {"iconv", 3, 3, f_iconv},
667 {"indent", 1, 1, f_indent},
668 {"index", 2, 4, f_index},
669 {"input", 1, 3, f_input},
670 {"inputdialog", 1, 3, f_inputdialog},
671 {"inputlist", 1, 1, f_inputlist},
672 {"inputrestore", 0, 0, f_inputrestore},
673 {"inputsave", 0, 0, f_inputsave},
674 {"inputsecret", 1, 2, f_inputsecret},
675 {"insert", 2, 3, f_insert},
676 {"invert", 1, 1, f_invert},
677 {"isdirectory", 1, 1, f_isdirectory},
678 {"islocked", 1, 1, f_islocked},
679#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
680 {"isnan", 1, 1, f_isnan},
681#endif
682 {"items", 1, 1, f_items},
683#ifdef FEAT_JOB_CHANNEL
684 {"job_getchannel", 1, 1, f_job_getchannel},
685 {"job_info", 1, 1, f_job_info},
686 {"job_setoptions", 2, 2, f_job_setoptions},
687 {"job_start", 1, 2, f_job_start},
688 {"job_status", 1, 1, f_job_status},
689 {"job_stop", 1, 2, f_job_stop},
690#endif
691 {"join", 1, 2, f_join},
692 {"js_decode", 1, 1, f_js_decode},
693 {"js_encode", 1, 1, f_js_encode},
694 {"json_decode", 1, 1, f_json_decode},
695 {"json_encode", 1, 1, f_json_encode},
696 {"keys", 1, 1, f_keys},
697 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
698 {"len", 1, 1, f_len},
699 {"libcall", 3, 3, f_libcall},
700 {"libcallnr", 3, 3, f_libcallnr},
701 {"line", 1, 1, f_line},
702 {"line2byte", 1, 1, f_line2byte},
703 {"lispindent", 1, 1, f_lispindent},
704 {"localtime", 0, 0, f_localtime},
705#ifdef FEAT_FLOAT
706 {"log", 1, 1, f_log},
707 {"log10", 1, 1, f_log10},
708#endif
709#ifdef FEAT_LUA
710 {"luaeval", 1, 2, f_luaeval},
711#endif
712 {"map", 2, 2, f_map},
713 {"maparg", 1, 4, f_maparg},
714 {"mapcheck", 1, 3, f_mapcheck},
715 {"match", 2, 4, f_match},
716 {"matchadd", 2, 5, f_matchadd},
717 {"matchaddpos", 2, 5, f_matchaddpos},
718 {"matcharg", 1, 1, f_matcharg},
719 {"matchdelete", 1, 1, f_matchdelete},
720 {"matchend", 2, 4, f_matchend},
721 {"matchlist", 2, 4, f_matchlist},
722 {"matchstr", 2, 4, f_matchstr},
723 {"matchstrpos", 2, 4, f_matchstrpos},
724 {"max", 1, 1, f_max},
725 {"min", 1, 1, f_min},
726#ifdef vim_mkdir
727 {"mkdir", 1, 3, f_mkdir},
728#endif
729 {"mode", 0, 1, f_mode},
730#ifdef FEAT_MZSCHEME
731 {"mzeval", 1, 1, f_mzeval},
732#endif
733 {"nextnonblank", 1, 1, f_nextnonblank},
734 {"nr2char", 1, 2, f_nr2char},
735 {"or", 2, 2, f_or},
736 {"pathshorten", 1, 1, f_pathshorten},
737#ifdef FEAT_PERL
738 {"perleval", 1, 1, f_perleval},
739#endif
740#ifdef FEAT_FLOAT
741 {"pow", 2, 2, f_pow},
742#endif
743 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100744 {"printf", 1, 19, f_printf},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200745 {"pumvisible", 0, 0, f_pumvisible},
746#ifdef FEAT_PYTHON3
747 {"py3eval", 1, 1, f_py3eval},
748#endif
749#ifdef FEAT_PYTHON
750 {"pyeval", 1, 1, f_pyeval},
751#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100752#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
753 {"pyxeval", 1, 1, f_pyxeval},
754#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200755 {"range", 1, 3, f_range},
756 {"readfile", 1, 3, f_readfile},
757 {"reltime", 0, 2, f_reltime},
758#ifdef FEAT_FLOAT
759 {"reltimefloat", 1, 1, f_reltimefloat},
760#endif
761 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100762 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200763 {"remote_foreground", 1, 1, f_remote_foreground},
764 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100765 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200766 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100767 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200768 {"remove", 2, 3, f_remove},
769 {"rename", 2, 2, f_rename},
770 {"repeat", 2, 2, f_repeat},
771 {"resolve", 1, 1, f_resolve},
772 {"reverse", 1, 1, f_reverse},
773#ifdef FEAT_FLOAT
774 {"round", 1, 1, f_round},
775#endif
776 {"screenattr", 2, 2, f_screenattr},
777 {"screenchar", 2, 2, f_screenchar},
778 {"screencol", 0, 0, f_screencol},
779 {"screenrow", 0, 0, f_screenrow},
780 {"search", 1, 4, f_search},
781 {"searchdecl", 1, 3, f_searchdecl},
782 {"searchpair", 3, 7, f_searchpair},
783 {"searchpairpos", 3, 7, f_searchpairpos},
784 {"searchpos", 1, 4, f_searchpos},
785 {"server2client", 2, 2, f_server2client},
786 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200787 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200788 {"setbufvar", 3, 3, f_setbufvar},
789 {"setcharsearch", 1, 1, f_setcharsearch},
790 {"setcmdpos", 1, 1, f_setcmdpos},
791 {"setfperm", 2, 2, f_setfperm},
792 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200793 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200794 {"setmatches", 1, 1, f_setmatches},
795 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200796 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200797 {"setreg", 2, 3, f_setreg},
798 {"settabvar", 3, 3, f_settabvar},
799 {"settabwinvar", 4, 4, f_settabwinvar},
800 {"setwinvar", 3, 3, f_setwinvar},
801#ifdef FEAT_CRYPT
802 {"sha256", 1, 1, f_sha256},
803#endif
804 {"shellescape", 1, 2, f_shellescape},
805 {"shiftwidth", 0, 0, f_shiftwidth},
806 {"simplify", 1, 1, f_simplify},
807#ifdef FEAT_FLOAT
808 {"sin", 1, 1, f_sin},
809 {"sinh", 1, 1, f_sinh},
810#endif
811 {"sort", 1, 3, f_sort},
812 {"soundfold", 1, 1, f_soundfold},
813 {"spellbadword", 0, 1, f_spellbadword},
814 {"spellsuggest", 1, 3, f_spellsuggest},
815 {"split", 1, 3, f_split},
816#ifdef FEAT_FLOAT
817 {"sqrt", 1, 1, f_sqrt},
818 {"str2float", 1, 1, f_str2float},
819#endif
820 {"str2nr", 1, 2, f_str2nr},
821 {"strcharpart", 2, 3, f_strcharpart},
822 {"strchars", 1, 2, f_strchars},
823 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
824#ifdef HAVE_STRFTIME
825 {"strftime", 1, 2, f_strftime},
826#endif
827 {"strgetchar", 2, 2, f_strgetchar},
828 {"stridx", 2, 3, f_stridx},
829 {"string", 1, 1, f_string},
830 {"strlen", 1, 1, f_strlen},
831 {"strpart", 2, 3, f_strpart},
832 {"strridx", 2, 3, f_strridx},
833 {"strtrans", 1, 1, f_strtrans},
834 {"strwidth", 1, 1, f_strwidth},
835 {"submatch", 1, 2, f_submatch},
836 {"substitute", 4, 4, f_substitute},
837 {"synID", 3, 3, f_synID},
838 {"synIDattr", 2, 3, f_synIDattr},
839 {"synIDtrans", 1, 1, f_synIDtrans},
840 {"synconcealed", 2, 2, f_synconcealed},
841 {"synstack", 2, 2, f_synstack},
842 {"system", 1, 2, f_system},
843 {"systemlist", 1, 2, f_systemlist},
844 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
845 {"tabpagenr", 0, 1, f_tabpagenr},
846 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
847 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100848 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200849#ifdef FEAT_FLOAT
850 {"tan", 1, 1, f_tan},
851 {"tanh", 1, 1, f_tanh},
852#endif
853 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200854#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100855 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
856 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100857 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200858 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200859# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
860 {"term_getansicolors", 1, 1, f_term_getansicolors},
861# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200862 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200863 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200864 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200865 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200866 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200867 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200868 {"term_getstatus", 1, 1, f_term_getstatus},
869 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200870 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200871 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200872 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200873 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200874# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
875 {"term_setansicolors", 2, 2, f_term_setansicolors},
876# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100877 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100878 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200879 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200880 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200881#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200882 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
883 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200884 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200885 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100886 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200887#ifdef FEAT_JOB_CHANNEL
888 {"test_null_channel", 0, 0, f_test_null_channel},
889#endif
890 {"test_null_dict", 0, 0, f_test_null_dict},
891#ifdef FEAT_JOB_CHANNEL
892 {"test_null_job", 0, 0, f_test_null_job},
893#endif
894 {"test_null_list", 0, 0, f_test_null_list},
895 {"test_null_partial", 0, 0, f_test_null_partial},
896 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100897 {"test_override", 2, 2, f_test_override},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200898 {"test_settime", 1, 1, f_test_settime},
899#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200900 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200901 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200902 {"timer_start", 2, 3, f_timer_start},
903 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200904 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200905#endif
906 {"tolower", 1, 1, f_tolower},
907 {"toupper", 1, 1, f_toupper},
908 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100909 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200910#ifdef FEAT_FLOAT
911 {"trunc", 1, 1, f_trunc},
912#endif
913 {"type", 1, 1, f_type},
914 {"undofile", 1, 1, f_undofile},
915 {"undotree", 0, 0, f_undotree},
916 {"uniq", 1, 3, f_uniq},
917 {"values", 1, 1, f_values},
918 {"virtcol", 1, 1, f_virtcol},
919 {"visualmode", 0, 1, f_visualmode},
920 {"wildmenumode", 0, 0, f_wildmenumode},
921 {"win_findbuf", 1, 1, f_win_findbuf},
922 {"win_getid", 0, 2, f_win_getid},
923 {"win_gotoid", 1, 1, f_win_gotoid},
924 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
925 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100926 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200927 {"winbufnr", 1, 1, f_winbufnr},
928 {"wincol", 0, 0, f_wincol},
929 {"winheight", 1, 1, f_winheight},
930 {"winline", 0, 0, f_winline},
931 {"winnr", 0, 1, f_winnr},
932 {"winrestcmd", 0, 0, f_winrestcmd},
933 {"winrestview", 1, 1, f_winrestview},
934 {"winsaveview", 0, 0, f_winsaveview},
935 {"winwidth", 1, 1, f_winwidth},
936 {"wordcount", 0, 0, f_wordcount},
937 {"writefile", 2, 3, f_writefile},
938 {"xor", 2, 2, f_xor},
939};
940
941#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
942
943/*
944 * Function given to ExpandGeneric() to obtain the list of internal
945 * or user defined function names.
946 */
947 char_u *
948get_function_name(expand_T *xp, int idx)
949{
950 static int intidx = -1;
951 char_u *name;
952
953 if (idx == 0)
954 intidx = -1;
955 if (intidx < 0)
956 {
957 name = get_user_func_name(xp, idx);
958 if (name != NULL)
959 return name;
960 }
961 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
962 {
963 STRCPY(IObuff, functions[intidx].f_name);
964 STRCAT(IObuff, "(");
965 if (functions[intidx].f_max_argc == 0)
966 STRCAT(IObuff, ")");
967 return IObuff;
968 }
969
970 return NULL;
971}
972
973/*
974 * Function given to ExpandGeneric() to obtain the list of internal or
975 * user defined variable or function names.
976 */
977 char_u *
978get_expr_name(expand_T *xp, int idx)
979{
980 static int intidx = -1;
981 char_u *name;
982
983 if (idx == 0)
984 intidx = -1;
985 if (intidx < 0)
986 {
987 name = get_function_name(xp, idx);
988 if (name != NULL)
989 return name;
990 }
991 return get_user_var_name(xp, ++intidx);
992}
993
994#endif /* FEAT_CMDL_COMPL */
995
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200996/*
997 * Find internal function in table above.
998 * Return index, or -1 if not found
999 */
1000 int
1001find_internal_func(
1002 char_u *name) /* name of the function */
1003{
1004 int first = 0;
1005 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1006 int cmp;
1007 int x;
1008
1009 /*
1010 * Find the function name in the table. Binary search.
1011 */
1012 while (first <= last)
1013 {
1014 x = first + ((unsigned)(last - first) >> 1);
1015 cmp = STRCMP(name, functions[x].f_name);
1016 if (cmp < 0)
1017 last = x - 1;
1018 else if (cmp > 0)
1019 first = x + 1;
1020 else
1021 return x;
1022 }
1023 return -1;
1024}
1025
1026 int
1027call_internal_func(
1028 char_u *name,
1029 int argcount,
1030 typval_T *argvars,
1031 typval_T *rettv)
1032{
1033 int i;
1034
1035 i = find_internal_func(name);
1036 if (i < 0)
1037 return ERROR_UNKNOWN;
1038 if (argcount < functions[i].f_min_argc)
1039 return ERROR_TOOFEW;
1040 if (argcount > functions[i].f_max_argc)
1041 return ERROR_TOOMANY;
1042 argvars[argcount].v_type = VAR_UNKNOWN;
1043 functions[i].f_func(argvars, rettv);
1044 return ERROR_NONE;
1045}
1046
1047/*
1048 * Return TRUE for a non-zero Number and a non-empty String.
1049 */
1050 static int
1051non_zero_arg(typval_T *argvars)
1052{
1053 return ((argvars[0].v_type == VAR_NUMBER
1054 && argvars[0].vval.v_number != 0)
1055 || (argvars[0].v_type == VAR_SPECIAL
1056 && argvars[0].vval.v_number == VVAL_TRUE)
1057 || (argvars[0].v_type == VAR_STRING
1058 && argvars[0].vval.v_string != NULL
1059 && *argvars[0].vval.v_string != NUL));
1060}
1061
1062/*
1063 * Get the lnum from the first argument.
1064 * Also accepts ".", "$", etc., but that only works for the current buffer.
1065 * Returns -1 on error.
1066 */
1067 static linenr_T
1068get_tv_lnum(typval_T *argvars)
1069{
1070 typval_T rettv;
1071 linenr_T lnum;
1072
1073 lnum = (linenr_T)get_tv_number_chk(&argvars[0], NULL);
1074 if (lnum == 0) /* no valid number, try using line() */
1075 {
1076 rettv.v_type = VAR_NUMBER;
1077 f_line(argvars, &rettv);
1078 lnum = (linenr_T)rettv.vval.v_number;
1079 clear_tv(&rettv);
1080 }
1081 return lnum;
1082}
1083
1084#ifdef FEAT_FLOAT
1085static int get_float_arg(typval_T *argvars, float_T *f);
1086
1087/*
1088 * Get the float value of "argvars[0]" into "f".
1089 * Returns FAIL when the argument is not a Number or Float.
1090 */
1091 static int
1092get_float_arg(typval_T *argvars, float_T *f)
1093{
1094 if (argvars[0].v_type == VAR_FLOAT)
1095 {
1096 *f = argvars[0].vval.v_float;
1097 return OK;
1098 }
1099 if (argvars[0].v_type == VAR_NUMBER)
1100 {
1101 *f = (float_T)argvars[0].vval.v_number;
1102 return OK;
1103 }
1104 EMSG(_("E808: Number or Float required"));
1105 return FAIL;
1106}
1107
1108/*
1109 * "abs(expr)" function
1110 */
1111 static void
1112f_abs(typval_T *argvars, typval_T *rettv)
1113{
1114 if (argvars[0].v_type == VAR_FLOAT)
1115 {
1116 rettv->v_type = VAR_FLOAT;
1117 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1118 }
1119 else
1120 {
1121 varnumber_T n;
1122 int error = FALSE;
1123
1124 n = get_tv_number_chk(&argvars[0], &error);
1125 if (error)
1126 rettv->vval.v_number = -1;
1127 else if (n > 0)
1128 rettv->vval.v_number = n;
1129 else
1130 rettv->vval.v_number = -n;
1131 }
1132}
1133
1134/*
1135 * "acos()" function
1136 */
1137 static void
1138f_acos(typval_T *argvars, typval_T *rettv)
1139{
1140 float_T f = 0.0;
1141
1142 rettv->v_type = VAR_FLOAT;
1143 if (get_float_arg(argvars, &f) == OK)
1144 rettv->vval.v_float = acos(f);
1145 else
1146 rettv->vval.v_float = 0.0;
1147}
1148#endif
1149
1150/*
1151 * "add(list, item)" function
1152 */
1153 static void
1154f_add(typval_T *argvars, typval_T *rettv)
1155{
1156 list_T *l;
1157
1158 rettv->vval.v_number = 1; /* Default: Failed */
1159 if (argvars[0].v_type == VAR_LIST)
1160 {
1161 if ((l = argvars[0].vval.v_list) != NULL
1162 && !tv_check_lock(l->lv_lock,
1163 (char_u *)N_("add() argument"), TRUE)
1164 && list_append_tv(l, &argvars[1]) == OK)
1165 copy_tv(&argvars[0], rettv);
1166 }
1167 else
1168 EMSG(_(e_listreq));
1169}
1170
1171/*
1172 * "and(expr, expr)" function
1173 */
1174 static void
1175f_and(typval_T *argvars, typval_T *rettv)
1176{
1177 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
1178 & get_tv_number_chk(&argvars[1], NULL);
1179}
1180
1181/*
1182 * "append(lnum, string/list)" function
1183 */
1184 static void
1185f_append(typval_T *argvars, typval_T *rettv)
1186{
1187 long lnum;
1188 char_u *line;
1189 list_T *l = NULL;
1190 listitem_T *li = NULL;
1191 typval_T *tv;
1192 long added = 0;
1193
1194 /* When coming here from Insert mode, sync undo, so that this can be
1195 * undone separately from what was previously inserted. */
1196 if (u_sync_once == 2)
1197 {
1198 u_sync_once = 1; /* notify that u_sync() was called */
1199 u_sync(TRUE);
1200 }
1201
1202 lnum = get_tv_lnum(argvars);
1203 if (lnum >= 0
1204 && lnum <= curbuf->b_ml.ml_line_count
1205 && u_save(lnum, lnum + 1) == OK)
1206 {
1207 if (argvars[1].v_type == VAR_LIST)
1208 {
1209 l = argvars[1].vval.v_list;
1210 if (l == NULL)
1211 return;
1212 li = l->lv_first;
1213 }
1214 for (;;)
1215 {
1216 if (l == NULL)
1217 tv = &argvars[1]; /* append a string */
1218 else if (li == NULL)
1219 break; /* end of list */
1220 else
1221 tv = &li->li_tv; /* append item from list */
1222 line = get_tv_string_chk(tv);
1223 if (line == NULL) /* type error */
1224 {
1225 rettv->vval.v_number = 1; /* Failed */
1226 break;
1227 }
1228 ml_append(lnum + added, line, (colnr_T)0, FALSE);
1229 ++added;
1230 if (l == NULL)
1231 break;
1232 li = li->li_next;
1233 }
1234
1235 appended_lines_mark(lnum, added);
1236 if (curwin->w_cursor.lnum > lnum)
1237 curwin->w_cursor.lnum += added;
1238 }
1239 else
1240 rettv->vval.v_number = 1; /* Failed */
1241}
1242
1243/*
1244 * "argc()" function
1245 */
1246 static void
1247f_argc(typval_T *argvars UNUSED, typval_T *rettv)
1248{
1249 rettv->vval.v_number = ARGCOUNT;
1250}
1251
1252/*
1253 * "argidx()" function
1254 */
1255 static void
1256f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1257{
1258 rettv->vval.v_number = curwin->w_arg_idx;
1259}
1260
1261/*
1262 * "arglistid()" function
1263 */
1264 static void
1265f_arglistid(typval_T *argvars, typval_T *rettv)
1266{
1267 win_T *wp;
1268
1269 rettv->vval.v_number = -1;
1270 wp = find_tabwin(&argvars[0], &argvars[1]);
1271 if (wp != NULL)
1272 rettv->vval.v_number = wp->w_alist->id;
1273}
1274
1275/*
1276 * "argv(nr)" function
1277 */
1278 static void
1279f_argv(typval_T *argvars, typval_T *rettv)
1280{
1281 int idx;
1282
1283 if (argvars[0].v_type != VAR_UNKNOWN)
1284 {
1285 idx = (int)get_tv_number_chk(&argvars[0], NULL);
1286 if (idx >= 0 && idx < ARGCOUNT)
1287 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
1288 else
1289 rettv->vval.v_string = NULL;
1290 rettv->v_type = VAR_STRING;
1291 }
1292 else if (rettv_list_alloc(rettv) == OK)
1293 for (idx = 0; idx < ARGCOUNT; ++idx)
1294 list_append_string(rettv->vval.v_list,
1295 alist_name(&ARGLIST[idx]), -1);
1296}
1297
1298/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001299 * "assert_beeps(cmd [, error])" function
1300 */
1301 static void
1302f_assert_beeps(typval_T *argvars, typval_T *rettv UNUSED)
1303{
1304 assert_beeps(argvars);
1305}
1306
1307/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001308 * "assert_equal(expected, actual[, msg])" function
1309 */
1310 static void
1311f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
1312{
1313 assert_equal_common(argvars, ASSERT_EQUAL);
1314}
1315
1316/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001317 * "assert_equalfile(fname-one, fname-two)" function
1318 */
1319 static void
1320f_assert_equalfile(typval_T *argvars, typval_T *rettv UNUSED)
1321{
1322 assert_equalfile(argvars);
1323}
1324
1325/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001326 * "assert_notequal(expected, actual[, msg])" function
1327 */
1328 static void
1329f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED)
1330{
1331 assert_equal_common(argvars, ASSERT_NOTEQUAL);
1332}
1333
1334/*
1335 * "assert_exception(string[, msg])" function
1336 */
1337 static void
1338f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
1339{
1340 assert_exception(argvars);
1341}
1342
1343/*
1344 * "assert_fails(cmd [, error])" function
1345 */
1346 static void
1347f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
1348{
1349 assert_fails(argvars);
1350}
1351
1352/*
1353 * "assert_false(actual[, msg])" function
1354 */
1355 static void
1356f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
1357{
1358 assert_bool(argvars, FALSE);
1359}
1360
1361/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001362 * "assert_inrange(lower, upper[, msg])" function
1363 */
1364 static void
1365f_assert_inrange(typval_T *argvars, typval_T *rettv UNUSED)
1366{
1367 assert_inrange(argvars);
1368}
1369
1370/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001371 * "assert_match(pattern, actual[, msg])" function
1372 */
1373 static void
1374f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
1375{
1376 assert_match_common(argvars, ASSERT_MATCH);
1377}
1378
1379/*
1380 * "assert_notmatch(pattern, actual[, msg])" function
1381 */
1382 static void
1383f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED)
1384{
1385 assert_match_common(argvars, ASSERT_NOTMATCH);
1386}
1387
1388/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001389 * "assert_report(msg)" function
1390 */
1391 static void
1392f_assert_report(typval_T *argvars, typval_T *rettv UNUSED)
1393{
1394 assert_report(argvars);
1395}
1396
1397/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001398 * "assert_true(actual[, msg])" function
1399 */
1400 static void
1401f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
1402{
1403 assert_bool(argvars, TRUE);
1404}
1405
1406#ifdef FEAT_FLOAT
1407/*
1408 * "asin()" function
1409 */
1410 static void
1411f_asin(typval_T *argvars, typval_T *rettv)
1412{
1413 float_T f = 0.0;
1414
1415 rettv->v_type = VAR_FLOAT;
1416 if (get_float_arg(argvars, &f) == OK)
1417 rettv->vval.v_float = asin(f);
1418 else
1419 rettv->vval.v_float = 0.0;
1420}
1421
1422/*
1423 * "atan()" function
1424 */
1425 static void
1426f_atan(typval_T *argvars, typval_T *rettv)
1427{
1428 float_T f = 0.0;
1429
1430 rettv->v_type = VAR_FLOAT;
1431 if (get_float_arg(argvars, &f) == OK)
1432 rettv->vval.v_float = atan(f);
1433 else
1434 rettv->vval.v_float = 0.0;
1435}
1436
1437/*
1438 * "atan2()" function
1439 */
1440 static void
1441f_atan2(typval_T *argvars, typval_T *rettv)
1442{
1443 float_T fx = 0.0, fy = 0.0;
1444
1445 rettv->v_type = VAR_FLOAT;
1446 if (get_float_arg(argvars, &fx) == OK
1447 && get_float_arg(&argvars[1], &fy) == OK)
1448 rettv->vval.v_float = atan2(fx, fy);
1449 else
1450 rettv->vval.v_float = 0.0;
1451}
1452#endif
1453
1454/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001455 * "balloon_show()" function
1456 */
1457#ifdef FEAT_BEVAL
1458 static void
1459f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1460{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001461 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001462 {
1463 if (argvars[0].v_type == VAR_LIST
1464# ifdef FEAT_GUI
1465 && !gui.in_use
1466# endif
1467 )
1468 post_balloon(balloonEval, NULL, argvars[0].vval.v_list);
1469 else
1470 post_balloon(balloonEval, get_tv_string_chk(&argvars[0]), NULL);
1471 }
1472}
1473
Bram Moolenaar669a8282017-11-19 20:13:05 +01001474# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001475 static void
1476f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1477{
1478 if (rettv_list_alloc(rettv) == OK)
1479 {
1480 char_u *msg = get_tv_string_chk(&argvars[0]);
1481
1482 if (msg != NULL)
1483 {
1484 pumitem_T *array;
1485 int size = split_message(msg, &array);
1486 int i;
1487
1488 /* Skip the first and last item, they are always empty. */
1489 for (i = 1; i < size - 1; ++i)
1490 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001491 while (size > 0)
1492 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001493 vim_free(array);
1494 }
1495 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001496}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001497# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001498#endif
1499
1500/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001501 * "browse(save, title, initdir, default)" function
1502 */
1503 static void
1504f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1505{
1506#ifdef FEAT_BROWSE
1507 int save;
1508 char_u *title;
1509 char_u *initdir;
1510 char_u *defname;
1511 char_u buf[NUMBUFLEN];
1512 char_u buf2[NUMBUFLEN];
1513 int error = FALSE;
1514
1515 save = (int)get_tv_number_chk(&argvars[0], &error);
1516 title = get_tv_string_chk(&argvars[1]);
1517 initdir = get_tv_string_buf_chk(&argvars[2], buf);
1518 defname = get_tv_string_buf_chk(&argvars[3], buf2);
1519
1520 if (error || title == NULL || initdir == NULL || defname == NULL)
1521 rettv->vval.v_string = NULL;
1522 else
1523 rettv->vval.v_string =
1524 do_browse(save ? BROWSE_SAVE : 0,
1525 title, defname, NULL, initdir, NULL, curbuf);
1526#else
1527 rettv->vval.v_string = NULL;
1528#endif
1529 rettv->v_type = VAR_STRING;
1530}
1531
1532/*
1533 * "browsedir(title, initdir)" function
1534 */
1535 static void
1536f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1537{
1538#ifdef FEAT_BROWSE
1539 char_u *title;
1540 char_u *initdir;
1541 char_u buf[NUMBUFLEN];
1542
1543 title = get_tv_string_chk(&argvars[0]);
1544 initdir = get_tv_string_buf_chk(&argvars[1], buf);
1545
1546 if (title == NULL || initdir == NULL)
1547 rettv->vval.v_string = NULL;
1548 else
1549 rettv->vval.v_string = do_browse(BROWSE_DIR,
1550 title, NULL, NULL, initdir, NULL, curbuf);
1551#else
1552 rettv->vval.v_string = NULL;
1553#endif
1554 rettv->v_type = VAR_STRING;
1555}
1556
1557static buf_T *find_buffer(typval_T *avar);
1558
1559/*
1560 * Find a buffer by number or exact name.
1561 */
1562 static buf_T *
1563find_buffer(typval_T *avar)
1564{
1565 buf_T *buf = NULL;
1566
1567 if (avar->v_type == VAR_NUMBER)
1568 buf = buflist_findnr((int)avar->vval.v_number);
1569 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1570 {
1571 buf = buflist_findname_exp(avar->vval.v_string);
1572 if (buf == NULL)
1573 {
1574 /* No full path name match, try a match with a URL or a "nofile"
1575 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001576 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001577 if (buf->b_fname != NULL
1578 && (path_with_url(buf->b_fname)
1579#ifdef FEAT_QUICKFIX
1580 || bt_nofile(buf)
1581#endif
1582 )
1583 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1584 break;
1585 }
1586 }
1587 return buf;
1588}
1589
1590/*
1591 * "bufexists(expr)" function
1592 */
1593 static void
1594f_bufexists(typval_T *argvars, typval_T *rettv)
1595{
1596 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1597}
1598
1599/*
1600 * "buflisted(expr)" function
1601 */
1602 static void
1603f_buflisted(typval_T *argvars, typval_T *rettv)
1604{
1605 buf_T *buf;
1606
1607 buf = find_buffer(&argvars[0]);
1608 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1609}
1610
1611/*
1612 * "bufloaded(expr)" function
1613 */
1614 static void
1615f_bufloaded(typval_T *argvars, typval_T *rettv)
1616{
1617 buf_T *buf;
1618
1619 buf = find_buffer(&argvars[0]);
1620 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1621}
1622
1623 buf_T *
1624buflist_find_by_name(char_u *name, int curtab_only)
1625{
1626 int save_magic;
1627 char_u *save_cpo;
1628 buf_T *buf;
1629
1630 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1631 save_magic = p_magic;
1632 p_magic = TRUE;
1633 save_cpo = p_cpo;
1634 p_cpo = (char_u *)"";
1635
1636 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1637 TRUE, FALSE, curtab_only));
1638
1639 p_magic = save_magic;
1640 p_cpo = save_cpo;
1641 return buf;
1642}
1643
1644/*
1645 * Get buffer by number or pattern.
1646 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001647 buf_T *
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001648get_buf_tv(typval_T *tv, int curtab_only)
1649{
1650 char_u *name = tv->vval.v_string;
1651 buf_T *buf;
1652
1653 if (tv->v_type == VAR_NUMBER)
1654 return buflist_findnr((int)tv->vval.v_number);
1655 if (tv->v_type != VAR_STRING)
1656 return NULL;
1657 if (name == NULL || *name == NUL)
1658 return curbuf;
1659 if (name[0] == '$' && name[1] == NUL)
1660 return lastbuf;
1661
1662 buf = buflist_find_by_name(name, curtab_only);
1663
1664 /* If not found, try expanding the name, like done for bufexists(). */
1665 if (buf == NULL)
1666 buf = find_buffer(tv);
1667
1668 return buf;
1669}
1670
1671/*
1672 * "bufname(expr)" function
1673 */
1674 static void
1675f_bufname(typval_T *argvars, typval_T *rettv)
1676{
1677 buf_T *buf;
1678
1679 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1680 ++emsg_off;
1681 buf = get_buf_tv(&argvars[0], FALSE);
1682 rettv->v_type = VAR_STRING;
1683 if (buf != NULL && buf->b_fname != NULL)
1684 rettv->vval.v_string = vim_strsave(buf->b_fname);
1685 else
1686 rettv->vval.v_string = NULL;
1687 --emsg_off;
1688}
1689
1690/*
1691 * "bufnr(expr)" function
1692 */
1693 static void
1694f_bufnr(typval_T *argvars, typval_T *rettv)
1695{
1696 buf_T *buf;
1697 int error = FALSE;
1698 char_u *name;
1699
1700 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1701 ++emsg_off;
1702 buf = get_buf_tv(&argvars[0], FALSE);
1703 --emsg_off;
1704
1705 /* If the buffer isn't found and the second argument is not zero create a
1706 * new buffer. */
1707 if (buf == NULL
1708 && argvars[1].v_type != VAR_UNKNOWN
1709 && get_tv_number_chk(&argvars[1], &error) != 0
1710 && !error
1711 && (name = get_tv_string_chk(&argvars[0])) != NULL
1712 && !error)
1713 buf = buflist_new(name, NULL, (linenr_T)1, 0);
1714
1715 if (buf != NULL)
1716 rettv->vval.v_number = buf->b_fnum;
1717 else
1718 rettv->vval.v_number = -1;
1719}
1720
1721 static void
1722buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
1723{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001724 win_T *wp;
1725 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001726 buf_T *buf;
1727
1728 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1729 ++emsg_off;
1730 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02001731 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001732 {
1733 ++winnr;
1734 if (wp->w_buffer == buf)
1735 break;
1736 }
1737 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001738 --emsg_off;
1739}
1740
1741/*
1742 * "bufwinid(nr)" function
1743 */
1744 static void
1745f_bufwinid(typval_T *argvars, typval_T *rettv)
1746{
1747 buf_win_common(argvars, rettv, FALSE);
1748}
1749
1750/*
1751 * "bufwinnr(nr)" function
1752 */
1753 static void
1754f_bufwinnr(typval_T *argvars, typval_T *rettv)
1755{
1756 buf_win_common(argvars, rettv, TRUE);
1757}
1758
1759/*
1760 * "byte2line(byte)" function
1761 */
1762 static void
1763f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1764{
1765#ifndef FEAT_BYTEOFF
1766 rettv->vval.v_number = -1;
1767#else
1768 long boff = 0;
1769
1770 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
1771 if (boff < 0)
1772 rettv->vval.v_number = -1;
1773 else
1774 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
1775 (linenr_T)0, &boff);
1776#endif
1777}
1778
1779 static void
1780byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
1781{
1782#ifdef FEAT_MBYTE
1783 char_u *t;
1784#endif
1785 char_u *str;
1786 varnumber_T idx;
1787
1788 str = get_tv_string_chk(&argvars[0]);
1789 idx = get_tv_number_chk(&argvars[1], NULL);
1790 rettv->vval.v_number = -1;
1791 if (str == NULL || idx < 0)
1792 return;
1793
1794#ifdef FEAT_MBYTE
1795 t = str;
1796 for ( ; idx > 0; idx--)
1797 {
1798 if (*t == NUL) /* EOL reached */
1799 return;
1800 if (enc_utf8 && comp)
1801 t += utf_ptr2len(t);
1802 else
1803 t += (*mb_ptr2len)(t);
1804 }
1805 rettv->vval.v_number = (varnumber_T)(t - str);
1806#else
1807 if ((size_t)idx <= STRLEN(str))
1808 rettv->vval.v_number = idx;
1809#endif
1810}
1811
1812/*
1813 * "byteidx()" function
1814 */
1815 static void
1816f_byteidx(typval_T *argvars, typval_T *rettv)
1817{
1818 byteidx(argvars, rettv, FALSE);
1819}
1820
1821/*
1822 * "byteidxcomp()" function
1823 */
1824 static void
1825f_byteidxcomp(typval_T *argvars, typval_T *rettv)
1826{
1827 byteidx(argvars, rettv, TRUE);
1828}
1829
1830/*
1831 * "call(func, arglist [, dict])" function
1832 */
1833 static void
1834f_call(typval_T *argvars, typval_T *rettv)
1835{
1836 char_u *func;
1837 partial_T *partial = NULL;
1838 dict_T *selfdict = NULL;
1839
1840 if (argvars[1].v_type != VAR_LIST)
1841 {
1842 EMSG(_(e_listreq));
1843 return;
1844 }
1845 if (argvars[1].vval.v_list == NULL)
1846 return;
1847
1848 if (argvars[0].v_type == VAR_FUNC)
1849 func = argvars[0].vval.v_string;
1850 else if (argvars[0].v_type == VAR_PARTIAL)
1851 {
1852 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001853 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001854 }
1855 else
1856 func = get_tv_string(&argvars[0]);
1857 if (*func == NUL)
1858 return; /* type error or empty name */
1859
1860 if (argvars[2].v_type != VAR_UNKNOWN)
1861 {
1862 if (argvars[2].v_type != VAR_DICT)
1863 {
1864 EMSG(_(e_dictreq));
1865 return;
1866 }
1867 selfdict = argvars[2].vval.v_dict;
1868 }
1869
1870 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
1871}
1872
1873#ifdef FEAT_FLOAT
1874/*
1875 * "ceil({float})" function
1876 */
1877 static void
1878f_ceil(typval_T *argvars, typval_T *rettv)
1879{
1880 float_T f = 0.0;
1881
1882 rettv->v_type = VAR_FLOAT;
1883 if (get_float_arg(argvars, &f) == OK)
1884 rettv->vval.v_float = ceil(f);
1885 else
1886 rettv->vval.v_float = 0.0;
1887}
1888#endif
1889
1890#ifdef FEAT_JOB_CHANNEL
1891/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01001892 * "ch_canread()" function
1893 */
1894 static void
1895f_ch_canread(typval_T *argvars, typval_T *rettv)
1896{
Bram Moolenaar958dc692016-12-01 15:34:12 +01001897 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01001898
1899 rettv->vval.v_number = 0;
1900 if (channel != NULL)
1901 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
1902 || channel_has_readahead(channel, PART_OUT)
1903 || channel_has_readahead(channel, PART_ERR);
1904}
1905
1906/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001907 * "ch_close()" function
1908 */
1909 static void
1910f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
1911{
1912 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
1913
1914 if (channel != NULL)
1915 {
1916 channel_close(channel, FALSE);
1917 channel_clear(channel);
1918 }
1919}
1920
1921/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02001922 * "ch_close()" function
1923 */
1924 static void
1925f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
1926{
1927 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
1928
1929 if (channel != NULL)
1930 channel_close_in(channel);
1931}
1932
1933/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001934 * "ch_getbufnr()" function
1935 */
1936 static void
1937f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
1938{
1939 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1940
1941 rettv->vval.v_number = -1;
1942 if (channel != NULL)
1943 {
1944 char_u *what = get_tv_string(&argvars[1]);
1945 int part;
1946
1947 if (STRCMP(what, "err") == 0)
1948 part = PART_ERR;
1949 else if (STRCMP(what, "out") == 0)
1950 part = PART_OUT;
1951 else if (STRCMP(what, "in") == 0)
1952 part = PART_IN;
1953 else
1954 part = PART_SOCK;
1955 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
1956 rettv->vval.v_number =
1957 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
1958 }
1959}
1960
1961/*
1962 * "ch_getjob()" function
1963 */
1964 static void
1965f_ch_getjob(typval_T *argvars, typval_T *rettv)
1966{
1967 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1968
1969 if (channel != NULL)
1970 {
1971 rettv->v_type = VAR_JOB;
1972 rettv->vval.v_job = channel->ch_job;
1973 if (channel->ch_job != NULL)
1974 ++channel->ch_job->jv_refcount;
1975 }
1976}
1977
1978/*
1979 * "ch_info()" function
1980 */
1981 static void
1982f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
1983{
1984 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1985
1986 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
1987 channel_info(channel, rettv->vval.v_dict);
1988}
1989
1990/*
1991 * "ch_log()" function
1992 */
1993 static void
1994f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
1995{
1996 char_u *msg = get_tv_string(&argvars[0]);
1997 channel_T *channel = NULL;
1998
1999 if (argvars[1].v_type != VAR_UNKNOWN)
2000 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
2001
Bram Moolenaard5359b22018-04-05 22:44:39 +02002002 ch_log(channel, "%s", msg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002003}
2004
2005/*
2006 * "ch_logfile()" function
2007 */
2008 static void
2009f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
2010{
2011 char_u *fname;
2012 char_u *opt = (char_u *)"";
2013 char_u buf[NUMBUFLEN];
2014
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02002015 /* Don't open a file in restricted mode. */
2016 if (check_restricted() || check_secure())
2017 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002018 fname = get_tv_string(&argvars[0]);
2019 if (argvars[1].v_type == VAR_STRING)
2020 opt = get_tv_string_buf(&argvars[1], buf);
2021 ch_logfile(fname, opt);
2022}
2023
2024/*
2025 * "ch_open()" function
2026 */
2027 static void
2028f_ch_open(typval_T *argvars, typval_T *rettv)
2029{
2030 rettv->v_type = VAR_CHANNEL;
2031 if (check_restricted() || check_secure())
2032 return;
2033 rettv->vval.v_channel = channel_open_func(argvars);
2034}
2035
2036/*
2037 * "ch_read()" function
2038 */
2039 static void
2040f_ch_read(typval_T *argvars, typval_T *rettv)
2041{
2042 common_channel_read(argvars, rettv, FALSE);
2043}
2044
2045/*
2046 * "ch_readraw()" function
2047 */
2048 static void
2049f_ch_readraw(typval_T *argvars, typval_T *rettv)
2050{
2051 common_channel_read(argvars, rettv, TRUE);
2052}
2053
2054/*
2055 * "ch_evalexpr()" function
2056 */
2057 static void
2058f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2059{
2060 ch_expr_common(argvars, rettv, TRUE);
2061}
2062
2063/*
2064 * "ch_sendexpr()" function
2065 */
2066 static void
2067f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2068{
2069 ch_expr_common(argvars, rettv, FALSE);
2070}
2071
2072/*
2073 * "ch_evalraw()" function
2074 */
2075 static void
2076f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2077{
2078 ch_raw_common(argvars, rettv, TRUE);
2079}
2080
2081/*
2082 * "ch_sendraw()" function
2083 */
2084 static void
2085f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2086{
2087 ch_raw_common(argvars, rettv, FALSE);
2088}
2089
2090/*
2091 * "ch_setoptions()" function
2092 */
2093 static void
2094f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2095{
2096 channel_T *channel;
2097 jobopt_T opt;
2098
2099 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2100 if (channel == NULL)
2101 return;
2102 clear_job_options(&opt);
2103 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002104 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002105 channel_set_options(channel, &opt);
2106 free_job_options(&opt);
2107}
2108
2109/*
2110 * "ch_status()" function
2111 */
2112 static void
2113f_ch_status(typval_T *argvars, typval_T *rettv)
2114{
2115 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002116 jobopt_T opt;
2117 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002118
2119 /* return an empty string by default */
2120 rettv->v_type = VAR_STRING;
2121 rettv->vval.v_string = NULL;
2122
2123 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002124
2125 if (argvars[1].v_type != VAR_UNKNOWN)
2126 {
2127 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002128 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002129 && (opt.jo_set & JO_PART))
2130 part = opt.jo_part;
2131 }
2132
2133 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002134}
2135#endif
2136
2137/*
2138 * "changenr()" function
2139 */
2140 static void
2141f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2142{
2143 rettv->vval.v_number = curbuf->b_u_seq_cur;
2144}
2145
2146/*
2147 * "char2nr(string)" function
2148 */
2149 static void
2150f_char2nr(typval_T *argvars, typval_T *rettv)
2151{
2152#ifdef FEAT_MBYTE
2153 if (has_mbyte)
2154 {
2155 int utf8 = 0;
2156
2157 if (argvars[1].v_type != VAR_UNKNOWN)
2158 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
2159
2160 if (utf8)
2161 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
2162 else
2163 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
2164 }
2165 else
2166#endif
2167 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
2168}
2169
2170/*
2171 * "cindent(lnum)" function
2172 */
2173 static void
2174f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2175{
2176#ifdef FEAT_CINDENT
2177 pos_T pos;
2178 linenr_T lnum;
2179
2180 pos = curwin->w_cursor;
2181 lnum = get_tv_lnum(argvars);
2182 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2183 {
2184 curwin->w_cursor.lnum = lnum;
2185 rettv->vval.v_number = get_c_indent();
2186 curwin->w_cursor = pos;
2187 }
2188 else
2189#endif
2190 rettv->vval.v_number = -1;
2191}
2192
2193/*
2194 * "clearmatches()" function
2195 */
2196 static void
2197f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2198{
2199#ifdef FEAT_SEARCH_EXTRA
2200 clear_matches(curwin);
2201#endif
2202}
2203
2204/*
2205 * "col(string)" function
2206 */
2207 static void
2208f_col(typval_T *argvars, typval_T *rettv)
2209{
2210 colnr_T col = 0;
2211 pos_T *fp;
2212 int fnum = curbuf->b_fnum;
2213
2214 fp = var2fpos(&argvars[0], FALSE, &fnum);
2215 if (fp != NULL && fnum == curbuf->b_fnum)
2216 {
2217 if (fp->col == MAXCOL)
2218 {
2219 /* '> can be MAXCOL, get the length of the line then */
2220 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2221 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2222 else
2223 col = MAXCOL;
2224 }
2225 else
2226 {
2227 col = fp->col + 1;
2228#ifdef FEAT_VIRTUALEDIT
2229 /* col(".") when the cursor is on the NUL at the end of the line
2230 * because of "coladd" can be seen as an extra column. */
2231 if (virtual_active() && fp == &curwin->w_cursor)
2232 {
2233 char_u *p = ml_get_cursor();
2234
2235 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2236 curwin->w_virtcol - curwin->w_cursor.coladd))
2237 {
2238# ifdef FEAT_MBYTE
2239 int l;
2240
2241 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2242 col += l;
2243# else
2244 if (*p != NUL && p[1] == NUL)
2245 ++col;
2246# endif
2247 }
2248 }
2249#endif
2250 }
2251 }
2252 rettv->vval.v_number = col;
2253}
2254
2255#if defined(FEAT_INS_EXPAND)
2256/*
2257 * "complete()" function
2258 */
2259 static void
2260f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2261{
2262 int startcol;
2263
2264 if ((State & INSERT) == 0)
2265 {
2266 EMSG(_("E785: complete() can only be used in Insert mode"));
2267 return;
2268 }
2269
2270 /* Check for undo allowed here, because if something was already inserted
2271 * the line was already saved for undo and this check isn't done. */
2272 if (!undo_allowed())
2273 return;
2274
2275 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2276 {
2277 EMSG(_(e_invarg));
2278 return;
2279 }
2280
2281 startcol = (int)get_tv_number_chk(&argvars[0], NULL);
2282 if (startcol <= 0)
2283 return;
2284
2285 set_completion(startcol - 1, argvars[1].vval.v_list);
2286}
2287
2288/*
2289 * "complete_add()" function
2290 */
2291 static void
2292f_complete_add(typval_T *argvars, typval_T *rettv)
2293{
2294 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2295}
2296
2297/*
2298 * "complete_check()" function
2299 */
2300 static void
2301f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2302{
2303 int saved = RedrawingDisabled;
2304
2305 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002306 ins_compl_check_keys(0, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002307 rettv->vval.v_number = compl_interrupted;
2308 RedrawingDisabled = saved;
2309}
2310#endif
2311
2312/*
2313 * "confirm(message, buttons[, default [, type]])" function
2314 */
2315 static void
2316f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2317{
2318#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2319 char_u *message;
2320 char_u *buttons = NULL;
2321 char_u buf[NUMBUFLEN];
2322 char_u buf2[NUMBUFLEN];
2323 int def = 1;
2324 int type = VIM_GENERIC;
2325 char_u *typestr;
2326 int error = FALSE;
2327
2328 message = get_tv_string_chk(&argvars[0]);
2329 if (message == NULL)
2330 error = TRUE;
2331 if (argvars[1].v_type != VAR_UNKNOWN)
2332 {
2333 buttons = get_tv_string_buf_chk(&argvars[1], buf);
2334 if (buttons == NULL)
2335 error = TRUE;
2336 if (argvars[2].v_type != VAR_UNKNOWN)
2337 {
2338 def = (int)get_tv_number_chk(&argvars[2], &error);
2339 if (argvars[3].v_type != VAR_UNKNOWN)
2340 {
2341 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
2342 if (typestr == NULL)
2343 error = TRUE;
2344 else
2345 {
2346 switch (TOUPPER_ASC(*typestr))
2347 {
2348 case 'E': type = VIM_ERROR; break;
2349 case 'Q': type = VIM_QUESTION; break;
2350 case 'I': type = VIM_INFO; break;
2351 case 'W': type = VIM_WARNING; break;
2352 case 'G': type = VIM_GENERIC; break;
2353 }
2354 }
2355 }
2356 }
2357 }
2358
2359 if (buttons == NULL || *buttons == NUL)
2360 buttons = (char_u *)_("&Ok");
2361
2362 if (!error)
2363 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2364 def, NULL, FALSE);
2365#endif
2366}
2367
2368/*
2369 * "copy()" function
2370 */
2371 static void
2372f_copy(typval_T *argvars, typval_T *rettv)
2373{
2374 item_copy(&argvars[0], rettv, FALSE, 0);
2375}
2376
2377#ifdef FEAT_FLOAT
2378/*
2379 * "cos()" function
2380 */
2381 static void
2382f_cos(typval_T *argvars, typval_T *rettv)
2383{
2384 float_T f = 0.0;
2385
2386 rettv->v_type = VAR_FLOAT;
2387 if (get_float_arg(argvars, &f) == OK)
2388 rettv->vval.v_float = cos(f);
2389 else
2390 rettv->vval.v_float = 0.0;
2391}
2392
2393/*
2394 * "cosh()" function
2395 */
2396 static void
2397f_cosh(typval_T *argvars, typval_T *rettv)
2398{
2399 float_T f = 0.0;
2400
2401 rettv->v_type = VAR_FLOAT;
2402 if (get_float_arg(argvars, &f) == OK)
2403 rettv->vval.v_float = cosh(f);
2404 else
2405 rettv->vval.v_float = 0.0;
2406}
2407#endif
2408
2409/*
2410 * "count()" function
2411 */
2412 static void
2413f_count(typval_T *argvars, typval_T *rettv)
2414{
2415 long n = 0;
2416 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002417 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002418
Bram Moolenaar9966b212017-07-28 16:46:57 +02002419 if (argvars[2].v_type != VAR_UNKNOWN)
2420 ic = (int)get_tv_number_chk(&argvars[2], &error);
2421
2422 if (argvars[0].v_type == VAR_STRING)
2423 {
2424 char_u *expr = get_tv_string_chk(&argvars[1]);
2425 char_u *p = argvars[0].vval.v_string;
2426 char_u *next;
2427
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002428 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002429 {
2430 if (ic)
2431 {
2432 size_t len = STRLEN(expr);
2433
2434 while (*p != NUL)
2435 {
2436 if (MB_STRNICMP(p, expr, len) == 0)
2437 {
2438 ++n;
2439 p += len;
2440 }
2441 else
2442 MB_PTR_ADV(p);
2443 }
2444 }
2445 else
2446 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2447 != NULL)
2448 {
2449 ++n;
2450 p = next + STRLEN(expr);
2451 }
2452 }
2453
2454 }
2455 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002456 {
2457 listitem_T *li;
2458 list_T *l;
2459 long idx;
2460
2461 if ((l = argvars[0].vval.v_list) != NULL)
2462 {
2463 li = l->lv_first;
2464 if (argvars[2].v_type != VAR_UNKNOWN)
2465 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002466 if (argvars[3].v_type != VAR_UNKNOWN)
2467 {
2468 idx = (long)get_tv_number_chk(&argvars[3], &error);
2469 if (!error)
2470 {
2471 li = list_find(l, idx);
2472 if (li == NULL)
2473 EMSGN(_(e_listidx), idx);
2474 }
2475 }
2476 if (error)
2477 li = NULL;
2478 }
2479
2480 for ( ; li != NULL; li = li->li_next)
2481 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2482 ++n;
2483 }
2484 }
2485 else if (argvars[0].v_type == VAR_DICT)
2486 {
2487 int todo;
2488 dict_T *d;
2489 hashitem_T *hi;
2490
2491 if ((d = argvars[0].vval.v_dict) != NULL)
2492 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002493 if (argvars[2].v_type != VAR_UNKNOWN)
2494 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002495 if (argvars[3].v_type != VAR_UNKNOWN)
2496 EMSG(_(e_invarg));
2497 }
2498
2499 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2500 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2501 {
2502 if (!HASHITEM_EMPTY(hi))
2503 {
2504 --todo;
2505 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2506 ++n;
2507 }
2508 }
2509 }
2510 }
2511 else
2512 EMSG2(_(e_listdictarg), "count()");
2513 rettv->vval.v_number = n;
2514}
2515
2516/*
2517 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2518 *
2519 * Checks the existence of a cscope connection.
2520 */
2521 static void
2522f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2523{
2524#ifdef FEAT_CSCOPE
2525 int num = 0;
2526 char_u *dbpath = NULL;
2527 char_u *prepend = NULL;
2528 char_u buf[NUMBUFLEN];
2529
2530 if (argvars[0].v_type != VAR_UNKNOWN
2531 && argvars[1].v_type != VAR_UNKNOWN)
2532 {
2533 num = (int)get_tv_number(&argvars[0]);
2534 dbpath = get_tv_string(&argvars[1]);
2535 if (argvars[2].v_type != VAR_UNKNOWN)
2536 prepend = get_tv_string_buf(&argvars[2], buf);
2537 }
2538
2539 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2540#endif
2541}
2542
2543/*
2544 * "cursor(lnum, col)" function, or
2545 * "cursor(list)"
2546 *
2547 * Moves the cursor to the specified line and column.
2548 * Returns 0 when the position could be set, -1 otherwise.
2549 */
2550 static void
2551f_cursor(typval_T *argvars, typval_T *rettv)
2552{
2553 long line, col;
2554#ifdef FEAT_VIRTUALEDIT
2555 long coladd = 0;
2556#endif
2557 int set_curswant = TRUE;
2558
2559 rettv->vval.v_number = -1;
2560 if (argvars[1].v_type == VAR_UNKNOWN)
2561 {
2562 pos_T pos;
2563 colnr_T curswant = -1;
2564
2565 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2566 {
2567 EMSG(_(e_invarg));
2568 return;
2569 }
2570 line = pos.lnum;
2571 col = pos.col;
2572#ifdef FEAT_VIRTUALEDIT
2573 coladd = pos.coladd;
2574#endif
2575 if (curswant >= 0)
2576 {
2577 curwin->w_curswant = curswant - 1;
2578 set_curswant = FALSE;
2579 }
2580 }
2581 else
2582 {
2583 line = get_tv_lnum(argvars);
2584 col = (long)get_tv_number_chk(&argvars[1], NULL);
2585#ifdef FEAT_VIRTUALEDIT
2586 if (argvars[2].v_type != VAR_UNKNOWN)
2587 coladd = (long)get_tv_number_chk(&argvars[2], NULL);
2588#endif
2589 }
2590 if (line < 0 || col < 0
2591#ifdef FEAT_VIRTUALEDIT
2592 || coladd < 0
2593#endif
2594 )
2595 return; /* type error; errmsg already given */
2596 if (line > 0)
2597 curwin->w_cursor.lnum = line;
2598 if (col > 0)
2599 curwin->w_cursor.col = col - 1;
2600#ifdef FEAT_VIRTUALEDIT
2601 curwin->w_cursor.coladd = coladd;
2602#endif
2603
2604 /* Make sure the cursor is in a valid position. */
2605 check_cursor();
2606#ifdef FEAT_MBYTE
2607 /* Correct cursor for multi-byte character. */
2608 if (has_mbyte)
2609 mb_adjust_cursor();
2610#endif
2611
2612 curwin->w_set_curswant = set_curswant;
2613 rettv->vval.v_number = 0;
2614}
2615
2616/*
2617 * "deepcopy()" function
2618 */
2619 static void
2620f_deepcopy(typval_T *argvars, typval_T *rettv)
2621{
2622 int noref = 0;
2623 int copyID;
2624
2625 if (argvars[1].v_type != VAR_UNKNOWN)
2626 noref = (int)get_tv_number_chk(&argvars[1], NULL);
2627 if (noref < 0 || noref > 1)
2628 EMSG(_(e_invarg));
2629 else
2630 {
2631 copyID = get_copyID();
2632 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2633 }
2634}
2635
2636/*
2637 * "delete()" function
2638 */
2639 static void
2640f_delete(typval_T *argvars, typval_T *rettv)
2641{
2642 char_u nbuf[NUMBUFLEN];
2643 char_u *name;
2644 char_u *flags;
2645
2646 rettv->vval.v_number = -1;
2647 if (check_restricted() || check_secure())
2648 return;
2649
2650 name = get_tv_string(&argvars[0]);
2651 if (name == NULL || *name == NUL)
2652 {
2653 EMSG(_(e_invarg));
2654 return;
2655 }
2656
2657 if (argvars[1].v_type != VAR_UNKNOWN)
2658 flags = get_tv_string_buf(&argvars[1], nbuf);
2659 else
2660 flags = (char_u *)"";
2661
2662 if (*flags == NUL)
2663 /* delete a file */
2664 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2665 else if (STRCMP(flags, "d") == 0)
2666 /* delete an empty directory */
2667 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2668 else if (STRCMP(flags, "rf") == 0)
2669 /* delete a directory recursively */
2670 rettv->vval.v_number = delete_recursive(name);
2671 else
2672 EMSG2(_(e_invexpr2), flags);
2673}
2674
2675/*
2676 * "did_filetype()" function
2677 */
2678 static void
2679f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2680{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002681 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002682}
2683
2684/*
2685 * "diff_filler()" function
2686 */
2687 static void
2688f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2689{
2690#ifdef FEAT_DIFF
2691 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
2692#endif
2693}
2694
2695/*
2696 * "diff_hlID()" function
2697 */
2698 static void
2699f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2700{
2701#ifdef FEAT_DIFF
2702 linenr_T lnum = get_tv_lnum(argvars);
2703 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01002704 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002705 static int fnum = 0;
2706 static int change_start = 0;
2707 static int change_end = 0;
2708 static hlf_T hlID = (hlf_T)0;
2709 int filler_lines;
2710 int col;
2711
2712 if (lnum < 0) /* ignore type error in {lnum} arg */
2713 lnum = 0;
2714 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002715 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002716 || fnum != curbuf->b_fnum)
2717 {
2718 /* New line, buffer, change: need to get the values. */
2719 filler_lines = diff_check(curwin, lnum);
2720 if (filler_lines < 0)
2721 {
2722 if (filler_lines == -1)
2723 {
2724 change_start = MAXCOL;
2725 change_end = -1;
2726 if (diff_find_change(curwin, lnum, &change_start, &change_end))
2727 hlID = HLF_ADD; /* added line */
2728 else
2729 hlID = HLF_CHD; /* changed line */
2730 }
2731 else
2732 hlID = HLF_ADD; /* added line */
2733 }
2734 else
2735 hlID = (hlf_T)0;
2736 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002737 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002738 fnum = curbuf->b_fnum;
2739 }
2740
2741 if (hlID == HLF_CHD || hlID == HLF_TXD)
2742 {
2743 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
2744 if (col >= change_start && col <= change_end)
2745 hlID = HLF_TXD; /* changed text */
2746 else
2747 hlID = HLF_CHD; /* changed line */
2748 }
2749 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
2750#endif
2751}
2752
2753/*
2754 * "empty({expr})" function
2755 */
2756 static void
2757f_empty(typval_T *argvars, typval_T *rettv)
2758{
2759 int n = FALSE;
2760
2761 switch (argvars[0].v_type)
2762 {
2763 case VAR_STRING:
2764 case VAR_FUNC:
2765 n = argvars[0].vval.v_string == NULL
2766 || *argvars[0].vval.v_string == NUL;
2767 break;
2768 case VAR_PARTIAL:
2769 n = FALSE;
2770 break;
2771 case VAR_NUMBER:
2772 n = argvars[0].vval.v_number == 0;
2773 break;
2774 case VAR_FLOAT:
2775#ifdef FEAT_FLOAT
2776 n = argvars[0].vval.v_float == 0.0;
2777 break;
2778#endif
2779 case VAR_LIST:
2780 n = argvars[0].vval.v_list == NULL
2781 || argvars[0].vval.v_list->lv_first == NULL;
2782 break;
2783 case VAR_DICT:
2784 n = argvars[0].vval.v_dict == NULL
2785 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2786 break;
2787 case VAR_SPECIAL:
2788 n = argvars[0].vval.v_number != VVAL_TRUE;
2789 break;
2790
2791 case VAR_JOB:
2792#ifdef FEAT_JOB_CHANNEL
2793 n = argvars[0].vval.v_job == NULL
2794 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2795 break;
2796#endif
2797 case VAR_CHANNEL:
2798#ifdef FEAT_JOB_CHANNEL
2799 n = argvars[0].vval.v_channel == NULL
2800 || !channel_is_open(argvars[0].vval.v_channel);
2801 break;
2802#endif
2803 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01002804 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002805 n = TRUE;
2806 break;
2807 }
2808
2809 rettv->vval.v_number = n;
2810}
2811
2812/*
2813 * "escape({string}, {chars})" function
2814 */
2815 static void
2816f_escape(typval_T *argvars, typval_T *rettv)
2817{
2818 char_u buf[NUMBUFLEN];
2819
2820 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
2821 get_tv_string_buf(&argvars[1], buf));
2822 rettv->v_type = VAR_STRING;
2823}
2824
2825/*
2826 * "eval()" function
2827 */
2828 static void
2829f_eval(typval_T *argvars, typval_T *rettv)
2830{
2831 char_u *s, *p;
2832
2833 s = get_tv_string_chk(&argvars[0]);
2834 if (s != NULL)
2835 s = skipwhite(s);
2836
2837 p = s;
2838 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
2839 {
2840 if (p != NULL && !aborting())
2841 EMSG2(_(e_invexpr2), p);
2842 need_clr_eos = FALSE;
2843 rettv->v_type = VAR_NUMBER;
2844 rettv->vval.v_number = 0;
2845 }
2846 else if (*s != NUL)
2847 EMSG(_(e_trailing));
2848}
2849
2850/*
2851 * "eventhandler()" function
2852 */
2853 static void
2854f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
2855{
2856 rettv->vval.v_number = vgetc_busy;
2857}
2858
2859/*
2860 * "executable()" function
2861 */
2862 static void
2863f_executable(typval_T *argvars, typval_T *rettv)
2864{
2865 char_u *name = get_tv_string(&argvars[0]);
2866
2867 /* Check in $PATH and also check directly if there is a directory name. */
2868 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
2869 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
2870}
2871
2872static garray_T redir_execute_ga;
2873
2874/*
2875 * Append "value[value_len]" to the execute() output.
2876 */
2877 void
2878execute_redir_str(char_u *value, int value_len)
2879{
2880 int len;
2881
2882 if (value_len == -1)
2883 len = (int)STRLEN(value); /* Append the entire string */
2884 else
2885 len = value_len; /* Append only "value_len" characters */
2886 if (ga_grow(&redir_execute_ga, len) == OK)
2887 {
2888 mch_memmove((char *)redir_execute_ga.ga_data
2889 + redir_execute_ga.ga_len, value, len);
2890 redir_execute_ga.ga_len += len;
2891 }
2892}
2893
2894/*
2895 * Get next line from a list.
2896 * Called by do_cmdline() to get the next line.
2897 * Returns allocated string, or NULL for end of function.
2898 */
2899
2900 static char_u *
2901get_list_line(
2902 int c UNUSED,
2903 void *cookie,
2904 int indent UNUSED)
2905{
2906 listitem_T **p = (listitem_T **)cookie;
2907 listitem_T *item = *p;
2908 char_u buf[NUMBUFLEN];
2909 char_u *s;
2910
2911 if (item == NULL)
2912 return NULL;
2913 s = get_tv_string_buf_chk(&item->li_tv, buf);
2914 *p = item->li_next;
2915 return s == NULL ? NULL : vim_strsave(s);
2916}
2917
2918/*
2919 * "execute()" function
2920 */
2921 static void
2922f_execute(typval_T *argvars, typval_T *rettv)
2923{
2924 char_u *cmd = NULL;
2925 list_T *list = NULL;
2926 int save_msg_silent = msg_silent;
2927 int save_emsg_silent = emsg_silent;
2928 int save_emsg_noredir = emsg_noredir;
2929 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01002930 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002931 garray_T save_ga;
2932
2933 rettv->vval.v_string = NULL;
2934 rettv->v_type = VAR_STRING;
2935
2936 if (argvars[0].v_type == VAR_LIST)
2937 {
2938 list = argvars[0].vval.v_list;
2939 if (list == NULL || list->lv_first == NULL)
2940 /* empty list, no commands, empty output */
2941 return;
2942 ++list->lv_refcount;
2943 }
2944 else
2945 {
2946 cmd = get_tv_string_chk(&argvars[0]);
2947 if (cmd == NULL)
2948 return;
2949 }
2950
2951 if (argvars[1].v_type != VAR_UNKNOWN)
2952 {
2953 char_u buf[NUMBUFLEN];
2954 char_u *s = get_tv_string_buf_chk(&argvars[1], buf);
2955
2956 if (s == NULL)
2957 return;
2958 if (STRNCMP(s, "silent", 6) == 0)
2959 ++msg_silent;
2960 if (STRCMP(s, "silent!") == 0)
2961 {
2962 emsg_silent = TRUE;
2963 emsg_noredir = TRUE;
2964 }
2965 }
2966 else
2967 ++msg_silent;
2968
2969 if (redir_execute)
2970 save_ga = redir_execute_ga;
2971 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
2972 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01002973 redir_off = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002974
2975 if (cmd != NULL)
2976 do_cmdline_cmd(cmd);
2977 else
2978 {
2979 listitem_T *item = list->lv_first;
2980
2981 do_cmdline(NULL, get_list_line, (void *)&item,
2982 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
2983 --list->lv_refcount;
2984 }
2985
Bram Moolenaard297f352017-01-29 20:31:21 +01002986 /* Need to append a NUL to the result. */
2987 if (ga_grow(&redir_execute_ga, 1) == OK)
2988 {
2989 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
2990 rettv->vval.v_string = redir_execute_ga.ga_data;
2991 }
2992 else
2993 {
2994 ga_clear(&redir_execute_ga);
2995 rettv->vval.v_string = NULL;
2996 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002997 msg_silent = save_msg_silent;
2998 emsg_silent = save_emsg_silent;
2999 emsg_noredir = save_emsg_noredir;
3000
3001 redir_execute = save_redir_execute;
3002 if (redir_execute)
3003 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003004 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003005
3006 /* "silent reg" or "silent echo x" leaves msg_col somewhere in the
3007 * line. Put it back in the first column. */
3008 msg_col = 0;
3009}
3010
3011/*
3012 * "exepath()" function
3013 */
3014 static void
3015f_exepath(typval_T *argvars, typval_T *rettv)
3016{
3017 char_u *p = NULL;
3018
3019 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
3020 rettv->v_type = VAR_STRING;
3021 rettv->vval.v_string = p;
3022}
3023
3024/*
3025 * "exists()" function
3026 */
3027 static void
3028f_exists(typval_T *argvars, typval_T *rettv)
3029{
3030 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003031 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003032
3033 p = get_tv_string(&argvars[0]);
3034 if (*p == '$') /* environment variable */
3035 {
3036 /* first try "normal" environment variables (fast) */
3037 if (mch_getenv(p + 1) != NULL)
3038 n = TRUE;
3039 else
3040 {
3041 /* try expanding things like $VIM and ${HOME} */
3042 p = expand_env_save(p);
3043 if (p != NULL && *p != '$')
3044 n = TRUE;
3045 vim_free(p);
3046 }
3047 }
3048 else if (*p == '&' || *p == '+') /* option */
3049 {
3050 n = (get_option_tv(&p, NULL, TRUE) == OK);
3051 if (*skipwhite(p) != NUL)
3052 n = FALSE; /* trailing garbage */
3053 }
3054 else if (*p == '*') /* internal or user defined function */
3055 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003056 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003057 }
3058 else if (*p == ':')
3059 {
3060 n = cmd_exists(p + 1);
3061 }
3062 else if (*p == '#')
3063 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003064 if (p[1] == '#')
3065 n = autocmd_supported(p + 2);
3066 else
3067 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003068 }
3069 else /* internal variable */
3070 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003071 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003072 }
3073
3074 rettv->vval.v_number = n;
3075}
3076
3077#ifdef FEAT_FLOAT
3078/*
3079 * "exp()" function
3080 */
3081 static void
3082f_exp(typval_T *argvars, typval_T *rettv)
3083{
3084 float_T f = 0.0;
3085
3086 rettv->v_type = VAR_FLOAT;
3087 if (get_float_arg(argvars, &f) == OK)
3088 rettv->vval.v_float = exp(f);
3089 else
3090 rettv->vval.v_float = 0.0;
3091}
3092#endif
3093
3094/*
3095 * "expand()" function
3096 */
3097 static void
3098f_expand(typval_T *argvars, typval_T *rettv)
3099{
3100 char_u *s;
3101 int len;
3102 char_u *errormsg;
3103 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3104 expand_T xpc;
3105 int error = FALSE;
3106 char_u *result;
3107
3108 rettv->v_type = VAR_STRING;
3109 if (argvars[1].v_type != VAR_UNKNOWN
3110 && argvars[2].v_type != VAR_UNKNOWN
3111 && get_tv_number_chk(&argvars[2], &error)
3112 && !error)
3113 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003114 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003115 }
3116
3117 s = get_tv_string(&argvars[0]);
3118 if (*s == '%' || *s == '#' || *s == '<')
3119 {
3120 ++emsg_off;
3121 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3122 --emsg_off;
3123 if (rettv->v_type == VAR_LIST)
3124 {
3125 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3126 list_append_string(rettv->vval.v_list, result, -1);
3127 else
3128 vim_free(result);
3129 }
3130 else
3131 rettv->vval.v_string = result;
3132 }
3133 else
3134 {
3135 /* When the optional second argument is non-zero, don't remove matches
3136 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3137 if (argvars[1].v_type != VAR_UNKNOWN
3138 && get_tv_number_chk(&argvars[1], &error))
3139 options |= WILD_KEEP_ALL;
3140 if (!error)
3141 {
3142 ExpandInit(&xpc);
3143 xpc.xp_context = EXPAND_FILES;
3144 if (p_wic)
3145 options += WILD_ICASE;
3146 if (rettv->v_type == VAR_STRING)
3147 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3148 options, WILD_ALL);
3149 else if (rettv_list_alloc(rettv) != FAIL)
3150 {
3151 int i;
3152
3153 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3154 for (i = 0; i < xpc.xp_numfiles; i++)
3155 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3156 ExpandCleanup(&xpc);
3157 }
3158 }
3159 else
3160 rettv->vval.v_string = NULL;
3161 }
3162}
3163
3164/*
3165 * "extend(list, list [, idx])" function
3166 * "extend(dict, dict [, action])" function
3167 */
3168 static void
3169f_extend(typval_T *argvars, typval_T *rettv)
3170{
3171 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3172
3173 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3174 {
3175 list_T *l1, *l2;
3176 listitem_T *item;
3177 long before;
3178 int error = FALSE;
3179
3180 l1 = argvars[0].vval.v_list;
3181 l2 = argvars[1].vval.v_list;
3182 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
3183 && l2 != NULL)
3184 {
3185 if (argvars[2].v_type != VAR_UNKNOWN)
3186 {
3187 before = (long)get_tv_number_chk(&argvars[2], &error);
3188 if (error)
3189 return; /* type error; errmsg already given */
3190
3191 if (before == l1->lv_len)
3192 item = NULL;
3193 else
3194 {
3195 item = list_find(l1, before);
3196 if (item == NULL)
3197 {
3198 EMSGN(_(e_listidx), before);
3199 return;
3200 }
3201 }
3202 }
3203 else
3204 item = NULL;
3205 list_extend(l1, l2, item);
3206
3207 copy_tv(&argvars[0], rettv);
3208 }
3209 }
3210 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3211 {
3212 dict_T *d1, *d2;
3213 char_u *action;
3214 int i;
3215
3216 d1 = argvars[0].vval.v_dict;
3217 d2 = argvars[1].vval.v_dict;
3218 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
3219 && d2 != NULL)
3220 {
3221 /* Check the third argument. */
3222 if (argvars[2].v_type != VAR_UNKNOWN)
3223 {
3224 static char *(av[]) = {"keep", "force", "error"};
3225
3226 action = get_tv_string_chk(&argvars[2]);
3227 if (action == NULL)
3228 return; /* type error; errmsg already given */
3229 for (i = 0; i < 3; ++i)
3230 if (STRCMP(action, av[i]) == 0)
3231 break;
3232 if (i == 3)
3233 {
3234 EMSG2(_(e_invarg2), action);
3235 return;
3236 }
3237 }
3238 else
3239 action = (char_u *)"force";
3240
3241 dict_extend(d1, d2, action);
3242
3243 copy_tv(&argvars[0], rettv);
3244 }
3245 }
3246 else
3247 EMSG2(_(e_listdictarg), "extend()");
3248}
3249
3250/*
3251 * "feedkeys()" function
3252 */
3253 static void
3254f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3255{
3256 int remap = TRUE;
3257 int insert = FALSE;
3258 char_u *keys, *flags;
3259 char_u nbuf[NUMBUFLEN];
3260 int typed = FALSE;
3261 int execute = FALSE;
3262 int dangerous = FALSE;
3263 char_u *keys_esc;
3264
3265 /* This is not allowed in the sandbox. If the commands would still be
3266 * executed in the sandbox it would be OK, but it probably happens later,
3267 * when "sandbox" is no longer set. */
3268 if (check_secure())
3269 return;
3270
3271 keys = get_tv_string(&argvars[0]);
3272
3273 if (argvars[1].v_type != VAR_UNKNOWN)
3274 {
3275 flags = get_tv_string_buf(&argvars[1], nbuf);
3276 for ( ; *flags != NUL; ++flags)
3277 {
3278 switch (*flags)
3279 {
3280 case 'n': remap = FALSE; break;
3281 case 'm': remap = TRUE; break;
3282 case 't': typed = TRUE; break;
3283 case 'i': insert = TRUE; break;
3284 case 'x': execute = TRUE; break;
3285 case '!': dangerous = TRUE; break;
3286 }
3287 }
3288 }
3289
3290 if (*keys != NUL || execute)
3291 {
3292 /* Need to escape K_SPECIAL and CSI before putting the string in the
3293 * typeahead buffer. */
3294 keys_esc = vim_strsave_escape_csi(keys);
3295 if (keys_esc != NULL)
3296 {
3297 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
3298 insert ? 0 : typebuf.tb_len, !typed, FALSE);
3299 vim_free(keys_esc);
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003300 if (vgetc_busy
3301#ifdef FEAT_TIMERS
3302 || timer_busy
3303#endif
3304 )
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003305 typebuf_was_filled = TRUE;
3306 if (execute)
3307 {
3308 int save_msg_scroll = msg_scroll;
3309
3310 /* Avoid a 1 second delay when the keys start Insert mode. */
3311 msg_scroll = FALSE;
3312
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02003313#ifdef FEAT_TERMINAL
3314 if (term_use_loop())
3315 terminal_loop(FALSE);
3316 else
3317#endif
3318 {
3319 if (!dangerous)
3320 ++ex_normal_busy;
3321 exec_normal(TRUE);
3322 if (!dangerous)
3323 --ex_normal_busy;
3324 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003325 msg_scroll |= save_msg_scroll;
3326 }
3327 }
3328 }
3329}
3330
3331/*
3332 * "filereadable()" function
3333 */
3334 static void
3335f_filereadable(typval_T *argvars, typval_T *rettv)
3336{
3337 int fd;
3338 char_u *p;
3339 int n;
3340
3341#ifndef O_NONBLOCK
3342# define O_NONBLOCK 0
3343#endif
3344 p = get_tv_string(&argvars[0]);
3345 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3346 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3347 {
3348 n = TRUE;
3349 close(fd);
3350 }
3351 else
3352 n = FALSE;
3353
3354 rettv->vval.v_number = n;
3355}
3356
3357/*
3358 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3359 * rights to write into.
3360 */
3361 static void
3362f_filewritable(typval_T *argvars, typval_T *rettv)
3363{
3364 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
3365}
3366
3367 static void
3368findfilendir(
3369 typval_T *argvars UNUSED,
3370 typval_T *rettv,
3371 int find_what UNUSED)
3372{
3373#ifdef FEAT_SEARCHPATH
3374 char_u *fname;
3375 char_u *fresult = NULL;
3376 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3377 char_u *p;
3378 char_u pathbuf[NUMBUFLEN];
3379 int count = 1;
3380 int first = TRUE;
3381 int error = FALSE;
3382#endif
3383
3384 rettv->vval.v_string = NULL;
3385 rettv->v_type = VAR_STRING;
3386
3387#ifdef FEAT_SEARCHPATH
3388 fname = get_tv_string(&argvars[0]);
3389
3390 if (argvars[1].v_type != VAR_UNKNOWN)
3391 {
3392 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
3393 if (p == NULL)
3394 error = TRUE;
3395 else
3396 {
3397 if (*p != NUL)
3398 path = p;
3399
3400 if (argvars[2].v_type != VAR_UNKNOWN)
3401 count = (int)get_tv_number_chk(&argvars[2], &error);
3402 }
3403 }
3404
3405 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3406 error = TRUE;
3407
3408 if (*fname != NUL && !error)
3409 {
3410 do
3411 {
3412 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3413 vim_free(fresult);
3414 fresult = find_file_in_path_option(first ? fname : NULL,
3415 first ? (int)STRLEN(fname) : 0,
3416 0, first, path,
3417 find_what,
3418 curbuf->b_ffname,
3419 find_what == FINDFILE_DIR
3420 ? (char_u *)"" : curbuf->b_p_sua);
3421 first = FALSE;
3422
3423 if (fresult != NULL && rettv->v_type == VAR_LIST)
3424 list_append_string(rettv->vval.v_list, fresult, -1);
3425
3426 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3427 }
3428
3429 if (rettv->v_type == VAR_STRING)
3430 rettv->vval.v_string = fresult;
3431#endif
3432}
3433
3434/*
3435 * "filter()" function
3436 */
3437 static void
3438f_filter(typval_T *argvars, typval_T *rettv)
3439{
3440 filter_map(argvars, rettv, FALSE);
3441}
3442
3443/*
3444 * "finddir({fname}[, {path}[, {count}]])" function
3445 */
3446 static void
3447f_finddir(typval_T *argvars, typval_T *rettv)
3448{
3449 findfilendir(argvars, rettv, FINDFILE_DIR);
3450}
3451
3452/*
3453 * "findfile({fname}[, {path}[, {count}]])" function
3454 */
3455 static void
3456f_findfile(typval_T *argvars, typval_T *rettv)
3457{
3458 findfilendir(argvars, rettv, FINDFILE_FILE);
3459}
3460
3461#ifdef FEAT_FLOAT
3462/*
3463 * "float2nr({float})" function
3464 */
3465 static void
3466f_float2nr(typval_T *argvars, typval_T *rettv)
3467{
3468 float_T f = 0.0;
3469
3470 if (get_float_arg(argvars, &f) == OK)
3471 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003472 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003473 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003474 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003475 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003476 else
3477 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003478 }
3479}
3480
3481/*
3482 * "floor({float})" function
3483 */
3484 static void
3485f_floor(typval_T *argvars, typval_T *rettv)
3486{
3487 float_T f = 0.0;
3488
3489 rettv->v_type = VAR_FLOAT;
3490 if (get_float_arg(argvars, &f) == OK)
3491 rettv->vval.v_float = floor(f);
3492 else
3493 rettv->vval.v_float = 0.0;
3494}
3495
3496/*
3497 * "fmod()" function
3498 */
3499 static void
3500f_fmod(typval_T *argvars, typval_T *rettv)
3501{
3502 float_T fx = 0.0, fy = 0.0;
3503
3504 rettv->v_type = VAR_FLOAT;
3505 if (get_float_arg(argvars, &fx) == OK
3506 && get_float_arg(&argvars[1], &fy) == OK)
3507 rettv->vval.v_float = fmod(fx, fy);
3508 else
3509 rettv->vval.v_float = 0.0;
3510}
3511#endif
3512
3513/*
3514 * "fnameescape({string})" function
3515 */
3516 static void
3517f_fnameescape(typval_T *argvars, typval_T *rettv)
3518{
3519 rettv->vval.v_string = vim_strsave_fnameescape(
3520 get_tv_string(&argvars[0]), FALSE);
3521 rettv->v_type = VAR_STRING;
3522}
3523
3524/*
3525 * "fnamemodify({fname}, {mods})" function
3526 */
3527 static void
3528f_fnamemodify(typval_T *argvars, typval_T *rettv)
3529{
3530 char_u *fname;
3531 char_u *mods;
3532 int usedlen = 0;
3533 int len;
3534 char_u *fbuf = NULL;
3535 char_u buf[NUMBUFLEN];
3536
3537 fname = get_tv_string_chk(&argvars[0]);
3538 mods = get_tv_string_buf_chk(&argvars[1], buf);
3539 if (fname == NULL || mods == NULL)
3540 fname = NULL;
3541 else
3542 {
3543 len = (int)STRLEN(fname);
3544 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
3545 }
3546
3547 rettv->v_type = VAR_STRING;
3548 if (fname == NULL)
3549 rettv->vval.v_string = NULL;
3550 else
3551 rettv->vval.v_string = vim_strnsave(fname, len);
3552 vim_free(fbuf);
3553}
3554
3555static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
3556
3557/*
3558 * "foldclosed()" function
3559 */
3560 static void
3561foldclosed_both(
3562 typval_T *argvars UNUSED,
3563 typval_T *rettv,
3564 int end UNUSED)
3565{
3566#ifdef FEAT_FOLDING
3567 linenr_T lnum;
3568 linenr_T first, last;
3569
3570 lnum = get_tv_lnum(argvars);
3571 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3572 {
3573 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3574 {
3575 if (end)
3576 rettv->vval.v_number = (varnumber_T)last;
3577 else
3578 rettv->vval.v_number = (varnumber_T)first;
3579 return;
3580 }
3581 }
3582#endif
3583 rettv->vval.v_number = -1;
3584}
3585
3586/*
3587 * "foldclosed()" function
3588 */
3589 static void
3590f_foldclosed(typval_T *argvars, typval_T *rettv)
3591{
3592 foldclosed_both(argvars, rettv, FALSE);
3593}
3594
3595/*
3596 * "foldclosedend()" function
3597 */
3598 static void
3599f_foldclosedend(typval_T *argvars, typval_T *rettv)
3600{
3601 foldclosed_both(argvars, rettv, TRUE);
3602}
3603
3604/*
3605 * "foldlevel()" function
3606 */
3607 static void
3608f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3609{
3610#ifdef FEAT_FOLDING
3611 linenr_T lnum;
3612
3613 lnum = get_tv_lnum(argvars);
3614 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3615 rettv->vval.v_number = foldLevel(lnum);
3616#endif
3617}
3618
3619/*
3620 * "foldtext()" function
3621 */
3622 static void
3623f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
3624{
3625#ifdef FEAT_FOLDING
3626 linenr_T foldstart;
3627 linenr_T foldend;
3628 char_u *dashes;
3629 linenr_T lnum;
3630 char_u *s;
3631 char_u *r;
3632 int len;
3633 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003634 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003635#endif
3636
3637 rettv->v_type = VAR_STRING;
3638 rettv->vval.v_string = NULL;
3639#ifdef FEAT_FOLDING
3640 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
3641 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
3642 dashes = get_vim_var_str(VV_FOLDDASHES);
3643 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
3644 && dashes != NULL)
3645 {
3646 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02003647 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003648 if (!linewhite(lnum))
3649 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003650
3651 /* Find interesting text in this line. */
3652 s = skipwhite(ml_get(lnum));
3653 /* skip C comment-start */
3654 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
3655 {
3656 s = skipwhite(s + 2);
3657 if (*skipwhite(s) == NUL
3658 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
3659 {
3660 s = skipwhite(ml_get(lnum + 1));
3661 if (*s == '*')
3662 s = skipwhite(s + 1);
3663 }
3664 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02003665 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003666 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003667 r = alloc((unsigned)(STRLEN(txt)
3668 + STRLEN(dashes) /* for %s */
3669 + 20 /* for %3ld */
3670 + STRLEN(s))); /* concatenated */
3671 if (r != NULL)
3672 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02003673 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003674 len = (int)STRLEN(r);
3675 STRCAT(r, s);
3676 /* remove 'foldmarker' and 'commentstring' */
3677 foldtext_cleanup(r + len);
3678 rettv->vval.v_string = r;
3679 }
3680 }
3681#endif
3682}
3683
3684/*
3685 * "foldtextresult(lnum)" function
3686 */
3687 static void
3688f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
3689{
3690#ifdef FEAT_FOLDING
3691 linenr_T lnum;
3692 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003693 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003694 foldinfo_T foldinfo;
3695 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003696 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003697#endif
3698
3699 rettv->v_type = VAR_STRING;
3700 rettv->vval.v_string = NULL;
3701#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003702 if (entered)
3703 return; /* reject recursive use */
3704 entered = TRUE;
3705
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003706 lnum = get_tv_lnum(argvars);
3707 /* treat illegal types and illegal string values for {lnum} the same */
3708 if (lnum < 0)
3709 lnum = 0;
3710 fold_count = foldedCount(curwin, lnum, &foldinfo);
3711 if (fold_count > 0)
3712 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01003713 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
3714 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003715 if (text == buf)
3716 text = vim_strsave(text);
3717 rettv->vval.v_string = text;
3718 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003719
3720 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003721#endif
3722}
3723
3724/*
3725 * "foreground()" function
3726 */
3727 static void
3728f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3729{
3730#ifdef FEAT_GUI
3731 if (gui.in_use)
3732 gui_mch_set_foreground();
3733#else
3734# ifdef WIN32
3735 win32_set_foreground();
3736# endif
3737#endif
3738}
3739
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003740 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003741common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003742{
3743 char_u *s;
3744 char_u *name;
3745 int use_string = FALSE;
3746 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003747 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003748
3749 if (argvars[0].v_type == VAR_FUNC)
3750 {
3751 /* function(MyFunc, [arg], dict) */
3752 s = argvars[0].vval.v_string;
3753 }
3754 else if (argvars[0].v_type == VAR_PARTIAL
3755 && argvars[0].vval.v_partial != NULL)
3756 {
3757 /* function(dict.MyFunc, [arg]) */
3758 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003759 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003760 }
3761 else
3762 {
3763 /* function('MyFunc', [arg], dict) */
3764 s = get_tv_string(&argvars[0]);
3765 use_string = TRUE;
3766 }
3767
Bram Moolenaar843b8842016-08-21 14:36:15 +02003768 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003769 {
3770 name = s;
3771 trans_name = trans_function_name(&name, FALSE,
3772 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
3773 if (*name != NUL)
3774 s = NULL;
3775 }
3776
Bram Moolenaar843b8842016-08-21 14:36:15 +02003777 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
3778 || (is_funcref && trans_name == NULL))
Bram Moolenaar5582ef12016-09-14 22:16:13 +02003779 EMSG2(_(e_invarg2), use_string ? get_tv_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003780 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003781 else if (trans_name != NULL && (is_funcref
3782 ? find_func(trans_name) == NULL
3783 : !translated_function_exists(trans_name)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003784 EMSG2(_("E700: Unknown function: %s"), s);
3785 else
3786 {
3787 int dict_idx = 0;
3788 int arg_idx = 0;
3789 list_T *list = NULL;
3790
3791 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
3792 {
3793 char sid_buf[25];
3794 int off = *s == 's' ? 2 : 5;
3795
3796 /* Expand s: and <SID> into <SNR>nr_, so that the function can
3797 * also be called from another script. Using trans_function_name()
3798 * would also work, but some plugins depend on the name being
3799 * printable text. */
3800 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
3801 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
3802 if (name != NULL)
3803 {
3804 STRCPY(name, sid_buf);
3805 STRCAT(name, s + off);
3806 }
3807 }
3808 else
3809 name = vim_strsave(s);
3810
3811 if (argvars[1].v_type != VAR_UNKNOWN)
3812 {
3813 if (argvars[2].v_type != VAR_UNKNOWN)
3814 {
3815 /* function(name, [args], dict) */
3816 arg_idx = 1;
3817 dict_idx = 2;
3818 }
3819 else if (argvars[1].v_type == VAR_DICT)
3820 /* function(name, dict) */
3821 dict_idx = 1;
3822 else
3823 /* function(name, [args]) */
3824 arg_idx = 1;
3825 if (dict_idx > 0)
3826 {
3827 if (argvars[dict_idx].v_type != VAR_DICT)
3828 {
3829 EMSG(_("E922: expected a dict"));
3830 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003831 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003832 }
3833 if (argvars[dict_idx].vval.v_dict == NULL)
3834 dict_idx = 0;
3835 }
3836 if (arg_idx > 0)
3837 {
3838 if (argvars[arg_idx].v_type != VAR_LIST)
3839 {
3840 EMSG(_("E923: Second argument of function() must be a list or a dict"));
3841 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003842 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003843 }
3844 list = argvars[arg_idx].vval.v_list;
3845 if (list == NULL || list->lv_len == 0)
3846 arg_idx = 0;
3847 }
3848 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003849 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003850 {
3851 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
3852
3853 /* result is a VAR_PARTIAL */
3854 if (pt == NULL)
3855 vim_free(name);
3856 else
3857 {
3858 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
3859 {
3860 listitem_T *li;
3861 int i = 0;
3862 int arg_len = 0;
3863 int lv_len = 0;
3864
3865 if (arg_pt != NULL)
3866 arg_len = arg_pt->pt_argc;
3867 if (list != NULL)
3868 lv_len = list->lv_len;
3869 pt->pt_argc = arg_len + lv_len;
3870 pt->pt_argv = (typval_T *)alloc(
3871 sizeof(typval_T) * pt->pt_argc);
3872 if (pt->pt_argv == NULL)
3873 {
3874 vim_free(pt);
3875 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003876 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003877 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003878 for (i = 0; i < arg_len; i++)
3879 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
3880 if (lv_len > 0)
3881 for (li = list->lv_first; li != NULL;
3882 li = li->li_next)
3883 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003884 }
3885
3886 /* For "function(dict.func, [], dict)" and "func" is a partial
3887 * use "dict". That is backwards compatible. */
3888 if (dict_idx > 0)
3889 {
3890 /* The dict is bound explicitly, pt_auto is FALSE. */
3891 pt->pt_dict = argvars[dict_idx].vval.v_dict;
3892 ++pt->pt_dict->dv_refcount;
3893 }
3894 else if (arg_pt != NULL)
3895 {
3896 /* If the dict was bound automatically the result is also
3897 * bound automatically. */
3898 pt->pt_dict = arg_pt->pt_dict;
3899 pt->pt_auto = arg_pt->pt_auto;
3900 if (pt->pt_dict != NULL)
3901 ++pt->pt_dict->dv_refcount;
3902 }
3903
3904 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003905 if (arg_pt != NULL && arg_pt->pt_func != NULL)
3906 {
3907 pt->pt_func = arg_pt->pt_func;
3908 func_ptr_ref(pt->pt_func);
3909 vim_free(name);
3910 }
3911 else if (is_funcref)
3912 {
3913 pt->pt_func = find_func(trans_name);
3914 func_ptr_ref(pt->pt_func);
3915 vim_free(name);
3916 }
3917 else
3918 {
3919 pt->pt_name = name;
3920 func_ref(name);
3921 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003922 }
3923 rettv->v_type = VAR_PARTIAL;
3924 rettv->vval.v_partial = pt;
3925 }
3926 else
3927 {
3928 /* result is a VAR_FUNC */
3929 rettv->v_type = VAR_FUNC;
3930 rettv->vval.v_string = name;
3931 func_ref(name);
3932 }
3933 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003934theend:
3935 vim_free(trans_name);
3936}
3937
3938/*
3939 * "funcref()" function
3940 */
3941 static void
3942f_funcref(typval_T *argvars, typval_T *rettv)
3943{
3944 common_function(argvars, rettv, TRUE);
3945}
3946
3947/*
3948 * "function()" function
3949 */
3950 static void
3951f_function(typval_T *argvars, typval_T *rettv)
3952{
3953 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003954}
3955
3956/*
3957 * "garbagecollect()" function
3958 */
3959 static void
3960f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
3961{
3962 /* This is postponed until we are back at the toplevel, because we may be
3963 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
3964 want_garbage_collect = TRUE;
3965
3966 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
3967 garbage_collect_at_exit = TRUE;
3968}
3969
3970/*
3971 * "get()" function
3972 */
3973 static void
3974f_get(typval_T *argvars, typval_T *rettv)
3975{
3976 listitem_T *li;
3977 list_T *l;
3978 dictitem_T *di;
3979 dict_T *d;
3980 typval_T *tv = NULL;
3981
3982 if (argvars[0].v_type == VAR_LIST)
3983 {
3984 if ((l = argvars[0].vval.v_list) != NULL)
3985 {
3986 int error = FALSE;
3987
3988 li = list_find(l, (long)get_tv_number_chk(&argvars[1], &error));
3989 if (!error && li != NULL)
3990 tv = &li->li_tv;
3991 }
3992 }
3993 else if (argvars[0].v_type == VAR_DICT)
3994 {
3995 if ((d = argvars[0].vval.v_dict) != NULL)
3996 {
3997 di = dict_find(d, get_tv_string(&argvars[1]), -1);
3998 if (di != NULL)
3999 tv = &di->di_tv;
4000 }
4001 }
4002 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4003 {
4004 partial_T *pt;
4005 partial_T fref_pt;
4006
4007 if (argvars[0].v_type == VAR_PARTIAL)
4008 pt = argvars[0].vval.v_partial;
4009 else
4010 {
4011 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4012 fref_pt.pt_name = argvars[0].vval.v_string;
4013 pt = &fref_pt;
4014 }
4015
4016 if (pt != NULL)
4017 {
4018 char_u *what = get_tv_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004019 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004020
4021 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4022 {
4023 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004024 n = partial_name(pt);
4025 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004026 rettv->vval.v_string = NULL;
4027 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004028 {
4029 rettv->vval.v_string = vim_strsave(n);
4030 if (rettv->v_type == VAR_FUNC)
4031 func_ref(rettv->vval.v_string);
4032 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004033 }
4034 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004035 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004036 else if (STRCMP(what, "args") == 0)
4037 {
4038 rettv->v_type = VAR_LIST;
4039 if (rettv_list_alloc(rettv) == OK)
4040 {
4041 int i;
4042
4043 for (i = 0; i < pt->pt_argc; ++i)
4044 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4045 }
4046 }
4047 else
4048 EMSG2(_(e_invarg2), what);
4049 return;
4050 }
4051 }
4052 else
4053 EMSG2(_(e_listdictarg), "get()");
4054
4055 if (tv == NULL)
4056 {
4057 if (argvars[2].v_type != VAR_UNKNOWN)
4058 copy_tv(&argvars[2], rettv);
4059 }
4060 else
4061 copy_tv(tv, rettv);
4062}
4063
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004064#ifdef FEAT_SIGNS
4065/*
4066 * Returns information about signs placed in a buffer as list of dicts.
4067 */
4068 static void
4069get_buffer_signs(buf_T *buf, list_T *l)
4070{
4071 signlist_T *sign;
4072
4073 for (sign = buf->b_signlist; sign; sign = sign->next)
4074 {
4075 dict_T *d = dict_alloc();
4076
4077 if (d != NULL)
4078 {
4079 dict_add_nr_str(d, "id", sign->id, NULL);
4080 dict_add_nr_str(d, "lnum", sign->lnum, NULL);
Bram Moolenaar6a402ed2016-08-28 14:11:24 +02004081 dict_add_nr_str(d, "name", 0L, sign_typenr2name(sign->typenr));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004082
4083 list_append_dict(l, d);
4084 }
4085 }
4086}
4087#endif
4088
4089/*
4090 * Returns buffer options, variables and other attributes in a dictionary.
4091 */
4092 static dict_T *
4093get_buffer_info(buf_T *buf)
4094{
4095 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004096 tabpage_T *tp;
4097 win_T *wp;
4098 list_T *windows;
4099
4100 dict = dict_alloc();
4101 if (dict == NULL)
4102 return NULL;
4103
Bram Moolenaar33928832016-08-18 21:22:04 +02004104 dict_add_nr_str(dict, "bufnr", buf->b_fnum, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004105 dict_add_nr_str(dict, "name", 0L,
4106 buf->b_ffname != NULL ? buf->b_ffname : (char_u *)"");
Bram Moolenaarf845b872017-01-06 14:04:54 +01004107 dict_add_nr_str(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4108 : buflist_findlnum(buf), NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004109 dict_add_nr_str(dict, "loaded", buf->b_ml.ml_mfp != NULL, NULL);
4110 dict_add_nr_str(dict, "listed", buf->b_p_bl, NULL);
4111 dict_add_nr_str(dict, "changed", bufIsChanged(buf), NULL);
Bram Moolenaar95c526e2017-02-25 14:59:34 +01004112 dict_add_nr_str(dict, "changedtick", CHANGEDTICK(buf), NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004113 dict_add_nr_str(dict, "hidden",
4114 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0,
4115 NULL);
4116
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004117 /* Get a reference to buffer variables */
4118 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004119
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004120 /* List of windows displaying this buffer */
4121 windows = list_alloc();
4122 if (windows != NULL)
4123 {
4124 FOR_ALL_TAB_WINDOWS(tp, wp)
4125 if (wp->w_buffer == buf)
4126 list_append_number(windows, (varnumber_T)wp->w_id);
4127 dict_add_list(dict, "windows", windows);
4128 }
4129
4130#ifdef FEAT_SIGNS
4131 if (buf->b_signlist != NULL)
4132 {
4133 /* List of signs placed in this buffer */
4134 list_T *signs = list_alloc();
4135 if (signs != NULL)
4136 {
4137 get_buffer_signs(buf, signs);
4138 dict_add_list(dict, "signs", signs);
4139 }
4140 }
4141#endif
4142
4143 return dict;
4144}
4145
4146/*
4147 * "getbufinfo()" function
4148 */
4149 static void
4150f_getbufinfo(typval_T *argvars, typval_T *rettv)
4151{
4152 buf_T *buf = NULL;
4153 buf_T *argbuf = NULL;
4154 dict_T *d;
4155 int filtered = FALSE;
4156 int sel_buflisted = FALSE;
4157 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004158 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004159
4160 if (rettv_list_alloc(rettv) != OK)
4161 return;
4162
4163 /* List of all the buffers or selected buffers */
4164 if (argvars[0].v_type == VAR_DICT)
4165 {
4166 dict_T *sel_d = argvars[0].vval.v_dict;
4167
4168 if (sel_d != NULL)
4169 {
4170 dictitem_T *di;
4171
4172 filtered = TRUE;
4173
4174 di = dict_find(sel_d, (char_u *)"buflisted", -1);
4175 if (di != NULL && get_tv_number(&di->di_tv))
4176 sel_buflisted = TRUE;
4177
4178 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
4179 if (di != NULL && get_tv_number(&di->di_tv))
4180 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004181
4182 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
4183 if (di != NULL && get_tv_number(&di->di_tv))
4184 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004185 }
4186 }
4187 else if (argvars[0].v_type != VAR_UNKNOWN)
4188 {
4189 /* Information about one buffer. Argument specifies the buffer */
4190 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4191 ++emsg_off;
4192 argbuf = get_buf_tv(&argvars[0], FALSE);
4193 --emsg_off;
4194 if (argbuf == NULL)
4195 return;
4196 }
4197
4198 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004199 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004200 {
4201 if (argbuf != NULL && argbuf != buf)
4202 continue;
4203 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004204 || (sel_buflisted && !buf->b_p_bl)
4205 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004206 continue;
4207
4208 d = get_buffer_info(buf);
4209 if (d != NULL)
4210 list_append_dict(rettv->vval.v_list, d);
4211 if (argbuf != NULL)
4212 return;
4213 }
4214}
4215
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004216static void get_buffer_lines(buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv);
4217
4218/*
4219 * Get line or list of lines from buffer "buf" into "rettv".
4220 * Return a range (from start to end) of lines in rettv from the specified
4221 * buffer.
4222 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4223 */
4224 static void
4225get_buffer_lines(
4226 buf_T *buf,
4227 linenr_T start,
4228 linenr_T end,
4229 int retlist,
4230 typval_T *rettv)
4231{
4232 char_u *p;
4233
4234 rettv->v_type = VAR_STRING;
4235 rettv->vval.v_string = NULL;
4236 if (retlist && rettv_list_alloc(rettv) == FAIL)
4237 return;
4238
4239 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4240 return;
4241
4242 if (!retlist)
4243 {
4244 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4245 p = ml_get_buf(buf, start, FALSE);
4246 else
4247 p = (char_u *)"";
4248 rettv->vval.v_string = vim_strsave(p);
4249 }
4250 else
4251 {
4252 if (end < start)
4253 return;
4254
4255 if (start < 1)
4256 start = 1;
4257 if (end > buf->b_ml.ml_line_count)
4258 end = buf->b_ml.ml_line_count;
4259 while (start <= end)
4260 if (list_append_string(rettv->vval.v_list,
4261 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4262 break;
4263 }
4264}
4265
4266/*
4267 * Get the lnum from the first argument.
4268 * Also accepts "$", then "buf" is used.
4269 * Returns 0 on error.
4270 */
4271 static linenr_T
4272get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
4273{
4274 if (argvars[0].v_type == VAR_STRING
4275 && argvars[0].vval.v_string != NULL
4276 && argvars[0].vval.v_string[0] == '$'
4277 && buf != NULL)
4278 return buf->b_ml.ml_line_count;
4279 return (linenr_T)get_tv_number_chk(&argvars[0], NULL);
4280}
4281
4282/*
4283 * "getbufline()" function
4284 */
4285 static void
4286f_getbufline(typval_T *argvars, typval_T *rettv)
4287{
4288 linenr_T lnum;
4289 linenr_T end;
4290 buf_T *buf;
4291
4292 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4293 ++emsg_off;
4294 buf = get_buf_tv(&argvars[0], FALSE);
4295 --emsg_off;
4296
4297 lnum = get_tv_lnum_buf(&argvars[1], buf);
4298 if (argvars[2].v_type == VAR_UNKNOWN)
4299 end = lnum;
4300 else
4301 end = get_tv_lnum_buf(&argvars[2], buf);
4302
4303 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4304}
4305
4306/*
4307 * "getbufvar()" function
4308 */
4309 static void
4310f_getbufvar(typval_T *argvars, typval_T *rettv)
4311{
4312 buf_T *buf;
4313 buf_T *save_curbuf;
4314 char_u *varname;
4315 dictitem_T *v;
4316 int done = FALSE;
4317
4318 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4319 varname = get_tv_string_chk(&argvars[1]);
4320 ++emsg_off;
4321 buf = get_buf_tv(&argvars[0], FALSE);
4322
4323 rettv->v_type = VAR_STRING;
4324 rettv->vval.v_string = NULL;
4325
4326 if (buf != NULL && varname != NULL)
4327 {
4328 /* set curbuf to be our buf, temporarily */
4329 save_curbuf = curbuf;
4330 curbuf = buf;
4331
Bram Moolenaar30567352016-08-27 21:25:44 +02004332 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004333 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004334 if (varname[1] == NUL)
4335 {
4336 /* get all buffer-local options in a dict */
4337 dict_T *opts = get_winbuf_options(TRUE);
4338
4339 if (opts != NULL)
4340 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004341 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004342 done = TRUE;
4343 }
4344 }
4345 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4346 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004347 done = TRUE;
4348 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004349 else
4350 {
4351 /* Look up the variable. */
4352 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4353 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4354 'b', varname, FALSE);
4355 if (v != NULL)
4356 {
4357 copy_tv(&v->di_tv, rettv);
4358 done = TRUE;
4359 }
4360 }
4361
4362 /* restore previous notion of curbuf */
4363 curbuf = save_curbuf;
4364 }
4365
4366 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4367 /* use the default value */
4368 copy_tv(&argvars[2], rettv);
4369
4370 --emsg_off;
4371}
4372
4373/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004374 * "getchangelist()" function
4375 */
4376 static void
4377f_getchangelist(typval_T *argvars, typval_T *rettv)
4378{
4379#ifdef FEAT_JUMPLIST
4380 buf_T *buf;
4381 int i;
4382 list_T *l;
4383 dict_T *d;
4384#endif
4385
4386 if (rettv_list_alloc(rettv) != OK)
4387 return;
4388
4389#ifdef FEAT_JUMPLIST
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004390 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4391 ++emsg_off;
4392 buf = get_buf_tv(&argvars[0], FALSE);
4393 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004394 if (buf == NULL)
4395 return;
4396
4397 l = list_alloc();
4398 if (l == NULL)
4399 return;
4400
4401 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4402 return;
4403 /*
4404 * The current window change list index tracks only the position in the
4405 * current buffer change list. For other buffers, use the change list
4406 * length as the current index.
4407 */
4408 list_append_number(rettv->vval.v_list,
4409 (varnumber_T)((buf == curwin->w_buffer)
4410 ? curwin->w_changelistidx : buf->b_changelistlen));
4411
4412 for (i = 0; i < buf->b_changelistlen; ++i)
4413 {
4414 if (buf->b_changelist[i].lnum == 0)
4415 continue;
4416 if ((d = dict_alloc()) == NULL)
4417 return;
4418 if (list_append_dict(l, d) == FAIL)
4419 return;
4420 dict_add_nr_str(d, "lnum", (long)buf->b_changelist[i].lnum, NULL);
4421 dict_add_nr_str(d, "col", (long)buf->b_changelist[i].col, NULL);
4422# ifdef FEAT_VIRTUALEDIT
4423 dict_add_nr_str(d, "coladd", (long)buf->b_changelist[i].coladd, NULL);
4424# endif
4425 }
4426#endif
4427}
4428/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004429 * "getchar()" function
4430 */
4431 static void
4432f_getchar(typval_T *argvars, typval_T *rettv)
4433{
4434 varnumber_T n;
4435 int error = FALSE;
4436
4437 /* Position the cursor. Needed after a message that ends in a space. */
4438 windgoto(msg_row, msg_col);
4439
4440 ++no_mapping;
4441 ++allow_keys;
4442 for (;;)
4443 {
4444 if (argvars[0].v_type == VAR_UNKNOWN)
4445 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004446 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004447 else if (get_tv_number_chk(&argvars[0], &error) == 1)
4448 /* getchar(1): only check if char avail */
4449 n = vpeekc_any();
4450 else if (error || vpeekc_any() == NUL)
4451 /* illegal argument or getchar(0) and no char avail: return zero */
4452 n = 0;
4453 else
4454 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004455 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004456
4457 if (n == K_IGNORE)
4458 continue;
4459 break;
4460 }
4461 --no_mapping;
4462 --allow_keys;
4463
4464 set_vim_var_nr(VV_MOUSE_WIN, 0);
4465 set_vim_var_nr(VV_MOUSE_WINID, 0);
4466 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4467 set_vim_var_nr(VV_MOUSE_COL, 0);
4468
4469 rettv->vval.v_number = n;
4470 if (IS_SPECIAL(n) || mod_mask != 0)
4471 {
4472 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4473 int i = 0;
4474
4475 /* Turn a special key into three bytes, plus modifier. */
4476 if (mod_mask != 0)
4477 {
4478 temp[i++] = K_SPECIAL;
4479 temp[i++] = KS_MODIFIER;
4480 temp[i++] = mod_mask;
4481 }
4482 if (IS_SPECIAL(n))
4483 {
4484 temp[i++] = K_SPECIAL;
4485 temp[i++] = K_SECOND(n);
4486 temp[i++] = K_THIRD(n);
4487 }
4488#ifdef FEAT_MBYTE
4489 else if (has_mbyte)
4490 i += (*mb_char2bytes)(n, temp + i);
4491#endif
4492 else
4493 temp[i++] = n;
4494 temp[i++] = NUL;
4495 rettv->v_type = VAR_STRING;
4496 rettv->vval.v_string = vim_strsave(temp);
4497
4498#ifdef FEAT_MOUSE
4499 if (is_mouse_key(n))
4500 {
4501 int row = mouse_row;
4502 int col = mouse_col;
4503 win_T *win;
4504 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004505 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004506 int winnr = 1;
4507
4508 if (row >= 0 && col >= 0)
4509 {
4510 /* Find the window at the mouse coordinates and compute the
4511 * text position. */
4512 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004513 if (win == NULL)
4514 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004515 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004516 for (wp = firstwin; wp != win; wp = wp->w_next)
4517 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004518 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4519 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4520 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4521 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4522 }
4523 }
4524#endif
4525 }
4526}
4527
4528/*
4529 * "getcharmod()" function
4530 */
4531 static void
4532f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4533{
4534 rettv->vval.v_number = mod_mask;
4535}
4536
4537/*
4538 * "getcharsearch()" function
4539 */
4540 static void
4541f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4542{
4543 if (rettv_dict_alloc(rettv) != FAIL)
4544 {
4545 dict_T *dict = rettv->vval.v_dict;
4546
4547 dict_add_nr_str(dict, "char", 0L, last_csearch());
4548 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
4549 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
4550 }
4551}
4552
4553/*
4554 * "getcmdline()" function
4555 */
4556 static void
4557f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4558{
4559 rettv->v_type = VAR_STRING;
4560 rettv->vval.v_string = get_cmdline_str();
4561}
4562
4563/*
4564 * "getcmdpos()" function
4565 */
4566 static void
4567f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4568{
4569 rettv->vval.v_number = get_cmdline_pos() + 1;
4570}
4571
4572/*
4573 * "getcmdtype()" function
4574 */
4575 static void
4576f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4577{
4578 rettv->v_type = VAR_STRING;
4579 rettv->vval.v_string = alloc(2);
4580 if (rettv->vval.v_string != NULL)
4581 {
4582 rettv->vval.v_string[0] = get_cmdline_type();
4583 rettv->vval.v_string[1] = NUL;
4584 }
4585}
4586
4587/*
4588 * "getcmdwintype()" function
4589 */
4590 static void
4591f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4592{
4593 rettv->v_type = VAR_STRING;
4594 rettv->vval.v_string = NULL;
4595#ifdef FEAT_CMDWIN
4596 rettv->vval.v_string = alloc(2);
4597 if (rettv->vval.v_string != NULL)
4598 {
4599 rettv->vval.v_string[0] = cmdwin_type;
4600 rettv->vval.v_string[1] = NUL;
4601 }
4602#endif
4603}
4604
4605#if defined(FEAT_CMDL_COMPL)
4606/*
4607 * "getcompletion()" function
4608 */
4609 static void
4610f_getcompletion(typval_T *argvars, typval_T *rettv)
4611{
4612 char_u *pat;
4613 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004614 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004615 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
4616 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004617
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004618 if (argvars[2].v_type != VAR_UNKNOWN)
4619 filtered = get_tv_number_chk(&argvars[2], NULL);
4620
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004621 if (p_wic)
4622 options |= WILD_ICASE;
4623
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004624 /* For filtered results, 'wildignore' is used */
4625 if (!filtered)
4626 options |= WILD_KEEP_ALL;
4627
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004628 ExpandInit(&xpc);
4629 xpc.xp_pattern = get_tv_string(&argvars[0]);
4630 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4631 xpc.xp_context = cmdcomplete_str_to_type(get_tv_string(&argvars[1]));
4632 if (xpc.xp_context == EXPAND_NOTHING)
4633 {
4634 if (argvars[1].v_type == VAR_STRING)
4635 EMSG2(_(e_invarg2), argvars[1].vval.v_string);
4636 else
4637 EMSG(_(e_invarg));
4638 return;
4639 }
4640
4641# if defined(FEAT_MENU)
4642 if (xpc.xp_context == EXPAND_MENUS)
4643 {
4644 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4645 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4646 }
4647# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02004648#ifdef FEAT_CSCOPE
4649 if (xpc.xp_context == EXPAND_CSCOPE)
4650 {
4651 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4652 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4653 }
4654#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02004655#ifdef FEAT_SIGNS
4656 if (xpc.xp_context == EXPAND_SIGN)
4657 {
4658 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4659 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4660 }
4661#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004662
4663 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4664 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
4665 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004666 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004667
4668 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4669
4670 for (i = 0; i < xpc.xp_numfiles; i++)
4671 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4672 }
4673 vim_free(pat);
4674 ExpandCleanup(&xpc);
4675}
4676#endif
4677
4678/*
4679 * "getcwd()" function
4680 */
4681 static void
4682f_getcwd(typval_T *argvars, typval_T *rettv)
4683{
4684 win_T *wp = NULL;
4685 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01004686 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004687
4688 rettv->v_type = VAR_STRING;
4689 rettv->vval.v_string = NULL;
4690
Bram Moolenaar54591292018-02-09 20:53:59 +01004691 if (argvars[0].v_type == VAR_NUMBER && argvars[0].vval.v_number == -1)
4692 global = TRUE;
4693 else
4694 wp = find_tabwin(&argvars[0], &argvars[1]);
4695
4696 if (wp != NULL && wp->w_localdir != NULL)
4697 rettv->vval.v_string = vim_strsave(wp->w_localdir);
4698 else if (wp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004699 {
Bram Moolenaar54591292018-02-09 20:53:59 +01004700 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004701 rettv->vval.v_string = vim_strsave(globaldir);
4702 else
4703 {
4704 cwd = alloc(MAXPATHL);
4705 if (cwd != NULL)
4706 {
4707 if (mch_dirname(cwd, MAXPATHL) != FAIL)
4708 rettv->vval.v_string = vim_strsave(cwd);
4709 vim_free(cwd);
4710 }
4711 }
4712#ifdef BACKSLASH_IN_FILENAME
4713 if (rettv->vval.v_string != NULL)
4714 slash_adjust(rettv->vval.v_string);
4715#endif
4716 }
4717}
4718
4719/*
4720 * "getfontname()" function
4721 */
4722 static void
4723f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4724{
4725 rettv->v_type = VAR_STRING;
4726 rettv->vval.v_string = NULL;
4727#ifdef FEAT_GUI
4728 if (gui.in_use)
4729 {
4730 GuiFont font;
4731 char_u *name = NULL;
4732
4733 if (argvars[0].v_type == VAR_UNKNOWN)
4734 {
4735 /* Get the "Normal" font. Either the name saved by
4736 * hl_set_font_name() or from the font ID. */
4737 font = gui.norm_font;
4738 name = hl_get_font_name();
4739 }
4740 else
4741 {
4742 name = get_tv_string(&argvars[0]);
4743 if (STRCMP(name, "*") == 0) /* don't use font dialog */
4744 return;
4745 font = gui_mch_get_font(name, FALSE);
4746 if (font == NOFONT)
4747 return; /* Invalid font name, return empty string. */
4748 }
4749 rettv->vval.v_string = gui_mch_get_fontname(font, name);
4750 if (argvars[0].v_type != VAR_UNKNOWN)
4751 gui_mch_free_font(font);
4752 }
4753#endif
4754}
4755
4756/*
4757 * "getfperm({fname})" function
4758 */
4759 static void
4760f_getfperm(typval_T *argvars, typval_T *rettv)
4761{
4762 char_u *fname;
4763 stat_T st;
4764 char_u *perm = NULL;
4765 char_u flags[] = "rwx";
4766 int i;
4767
4768 fname = get_tv_string(&argvars[0]);
4769
4770 rettv->v_type = VAR_STRING;
4771 if (mch_stat((char *)fname, &st) >= 0)
4772 {
4773 perm = vim_strsave((char_u *)"---------");
4774 if (perm != NULL)
4775 {
4776 for (i = 0; i < 9; i++)
4777 {
4778 if (st.st_mode & (1 << (8 - i)))
4779 perm[i] = flags[i % 3];
4780 }
4781 }
4782 }
4783 rettv->vval.v_string = perm;
4784}
4785
4786/*
4787 * "getfsize({fname})" function
4788 */
4789 static void
4790f_getfsize(typval_T *argvars, typval_T *rettv)
4791{
4792 char_u *fname;
4793 stat_T st;
4794
4795 fname = get_tv_string(&argvars[0]);
4796
4797 rettv->v_type = VAR_NUMBER;
4798
4799 if (mch_stat((char *)fname, &st) >= 0)
4800 {
4801 if (mch_isdir(fname))
4802 rettv->vval.v_number = 0;
4803 else
4804 {
4805 rettv->vval.v_number = (varnumber_T)st.st_size;
4806
4807 /* non-perfect check for overflow */
4808 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
4809 rettv->vval.v_number = -2;
4810 }
4811 }
4812 else
4813 rettv->vval.v_number = -1;
4814}
4815
4816/*
4817 * "getftime({fname})" function
4818 */
4819 static void
4820f_getftime(typval_T *argvars, typval_T *rettv)
4821{
4822 char_u *fname;
4823 stat_T st;
4824
4825 fname = get_tv_string(&argvars[0]);
4826
4827 if (mch_stat((char *)fname, &st) >= 0)
4828 rettv->vval.v_number = (varnumber_T)st.st_mtime;
4829 else
4830 rettv->vval.v_number = -1;
4831}
4832
4833/*
4834 * "getftype({fname})" function
4835 */
4836 static void
4837f_getftype(typval_T *argvars, typval_T *rettv)
4838{
4839 char_u *fname;
4840 stat_T st;
4841 char_u *type = NULL;
4842 char *t;
4843
4844 fname = get_tv_string(&argvars[0]);
4845
4846 rettv->v_type = VAR_STRING;
4847 if (mch_lstat((char *)fname, &st) >= 0)
4848 {
4849#ifdef S_ISREG
4850 if (S_ISREG(st.st_mode))
4851 t = "file";
4852 else if (S_ISDIR(st.st_mode))
4853 t = "dir";
4854# ifdef S_ISLNK
4855 else if (S_ISLNK(st.st_mode))
4856 t = "link";
4857# endif
4858# ifdef S_ISBLK
4859 else if (S_ISBLK(st.st_mode))
4860 t = "bdev";
4861# endif
4862# ifdef S_ISCHR
4863 else if (S_ISCHR(st.st_mode))
4864 t = "cdev";
4865# endif
4866# ifdef S_ISFIFO
4867 else if (S_ISFIFO(st.st_mode))
4868 t = "fifo";
4869# endif
4870# ifdef S_ISSOCK
4871 else if (S_ISSOCK(st.st_mode))
4872 t = "fifo";
4873# endif
4874 else
4875 t = "other";
4876#else
4877# ifdef S_IFMT
4878 switch (st.st_mode & S_IFMT)
4879 {
4880 case S_IFREG: t = "file"; break;
4881 case S_IFDIR: t = "dir"; break;
4882# ifdef S_IFLNK
4883 case S_IFLNK: t = "link"; break;
4884# endif
4885# ifdef S_IFBLK
4886 case S_IFBLK: t = "bdev"; break;
4887# endif
4888# ifdef S_IFCHR
4889 case S_IFCHR: t = "cdev"; break;
4890# endif
4891# ifdef S_IFIFO
4892 case S_IFIFO: t = "fifo"; break;
4893# endif
4894# ifdef S_IFSOCK
4895 case S_IFSOCK: t = "socket"; break;
4896# endif
4897 default: t = "other";
4898 }
4899# else
4900 if (mch_isdir(fname))
4901 t = "dir";
4902 else
4903 t = "file";
4904# endif
4905#endif
4906 type = vim_strsave((char_u *)t);
4907 }
4908 rettv->vval.v_string = type;
4909}
4910
4911/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01004912 * "getjumplist()" function
4913 */
4914 static void
4915f_getjumplist(typval_T *argvars, typval_T *rettv)
4916{
4917#ifdef FEAT_JUMPLIST
4918 win_T *wp;
4919 int i;
4920 list_T *l;
4921 dict_T *d;
4922#endif
4923
4924 if (rettv_list_alloc(rettv) != OK)
4925 return;
4926
4927#ifdef FEAT_JUMPLIST
4928 wp = find_tabwin(&argvars[0], &argvars[1]);
4929 if (wp == NULL)
4930 return;
4931
4932 l = list_alloc();
4933 if (l == NULL)
4934 return;
4935
4936 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4937 return;
4938 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
4939
Bram Moolenaar48679742018-02-13 13:33:29 +01004940 cleanup_jumplist(wp, TRUE);
4941
Bram Moolenaar4f505882018-02-10 21:06:32 +01004942 for (i = 0; i < wp->w_jumplistlen; ++i)
4943 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004944 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
4945 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01004946 if ((d = dict_alloc()) == NULL)
4947 return;
4948 if (list_append_dict(l, d) == FAIL)
4949 return;
4950 dict_add_nr_str(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum,
4951 NULL);
4952 dict_add_nr_str(d, "col", (long)wp->w_jumplist[i].fmark.mark.col,
4953 NULL);
4954# ifdef FEAT_VIRTUALEDIT
4955 dict_add_nr_str(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd,
4956 NULL);
4957# endif
4958 dict_add_nr_str(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum, NULL);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004959 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaar4f505882018-02-10 21:06:32 +01004960 dict_add_nr_str(d, "filename", 0L, wp->w_jumplist[i].fname);
4961 }
4962#endif
4963}
4964
4965/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004966 * "getline(lnum, [end])" function
4967 */
4968 static void
4969f_getline(typval_T *argvars, typval_T *rettv)
4970{
4971 linenr_T lnum;
4972 linenr_T end;
4973 int retlist;
4974
4975 lnum = get_tv_lnum(argvars);
4976 if (argvars[1].v_type == VAR_UNKNOWN)
4977 {
4978 end = 0;
4979 retlist = FALSE;
4980 }
4981 else
4982 {
4983 end = get_tv_lnum(&argvars[1]);
4984 retlist = TRUE;
4985 }
4986
4987 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
4988}
4989
Bram Moolenaar4ae20952016-08-13 15:29:14 +02004990#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02004991 static void
4992get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
4993{
Bram Moolenaard823fa92016-08-12 16:29:27 +02004994 if (what_arg->v_type == VAR_UNKNOWN)
4995 {
4996 if (rettv_list_alloc(rettv) == OK)
4997 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004998 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004999 }
5000 else
5001 {
5002 if (rettv_dict_alloc(rettv) == OK)
5003 if (is_qf || (wp != NULL))
5004 {
5005 if (what_arg->v_type == VAR_DICT)
5006 {
5007 dict_T *d = what_arg->vval.v_dict;
5008
5009 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005010 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005011 }
5012 else
5013 EMSG(_(e_dictreq));
5014 }
5015 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005016}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005017#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005018
5019/*
5020 * "getloclist()" function
5021 */
5022 static void
5023f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5024{
5025#ifdef FEAT_QUICKFIX
5026 win_T *wp;
5027
5028 wp = find_win_by_nr(&argvars[0], NULL);
5029 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5030#endif
5031}
5032
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005033/*
5034 * "getmatches()" function
5035 */
5036 static void
5037f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5038{
5039#ifdef FEAT_SEARCH_EXTRA
5040 dict_T *dict;
5041 matchitem_T *cur = curwin->w_match_head;
5042 int i;
5043
5044 if (rettv_list_alloc(rettv) == OK)
5045 {
5046 while (cur != NULL)
5047 {
5048 dict = dict_alloc();
5049 if (dict == NULL)
5050 return;
5051 if (cur->match.regprog == NULL)
5052 {
5053 /* match added with matchaddpos() */
5054 for (i = 0; i < MAXPOSMATCH; ++i)
5055 {
5056 llpos_T *llpos;
5057 char buf[6];
5058 list_T *l;
5059
5060 llpos = &cur->pos.pos[i];
5061 if (llpos->lnum == 0)
5062 break;
5063 l = list_alloc();
5064 if (l == NULL)
5065 break;
5066 list_append_number(l, (varnumber_T)llpos->lnum);
5067 if (llpos->col > 0)
5068 {
5069 list_append_number(l, (varnumber_T)llpos->col);
5070 list_append_number(l, (varnumber_T)llpos->len);
5071 }
5072 sprintf(buf, "pos%d", i + 1);
5073 dict_add_list(dict, buf, l);
5074 }
5075 }
5076 else
5077 {
5078 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
5079 }
5080 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
5081 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
5082 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
5083# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
5084 if (cur->conceal_char)
5085 {
5086 char_u buf[MB_MAXBYTES + 1];
5087
5088 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
5089 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
5090 }
5091# endif
5092 list_append_dict(rettv->vval.v_list, dict);
5093 cur = cur->next;
5094 }
5095 }
5096#endif
5097}
5098
5099/*
5100 * "getpid()" function
5101 */
5102 static void
5103f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5104{
5105 rettv->vval.v_number = mch_get_pid();
5106}
5107
5108 static void
5109getpos_both(
5110 typval_T *argvars,
5111 typval_T *rettv,
5112 int getcurpos)
5113{
5114 pos_T *fp;
5115 list_T *l;
5116 int fnum = -1;
5117
5118 if (rettv_list_alloc(rettv) == OK)
5119 {
5120 l = rettv->vval.v_list;
5121 if (getcurpos)
5122 fp = &curwin->w_cursor;
5123 else
5124 fp = var2fpos(&argvars[0], TRUE, &fnum);
5125 if (fnum != -1)
5126 list_append_number(l, (varnumber_T)fnum);
5127 else
5128 list_append_number(l, (varnumber_T)0);
5129 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5130 : (varnumber_T)0);
5131 list_append_number(l, (fp != NULL)
5132 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5133 : (varnumber_T)0);
5134 list_append_number(l,
5135#ifdef FEAT_VIRTUALEDIT
5136 (fp != NULL) ? (varnumber_T)fp->coladd :
5137#endif
5138 (varnumber_T)0);
5139 if (getcurpos)
5140 {
5141 update_curswant();
5142 list_append_number(l, curwin->w_curswant == MAXCOL ?
5143 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
5144 }
5145 }
5146 else
5147 rettv->vval.v_number = FALSE;
5148}
5149
5150
5151/*
5152 * "getcurpos()" function
5153 */
5154 static void
5155f_getcurpos(typval_T *argvars, typval_T *rettv)
5156{
5157 getpos_both(argvars, rettv, TRUE);
5158}
5159
5160/*
5161 * "getpos(string)" function
5162 */
5163 static void
5164f_getpos(typval_T *argvars, typval_T *rettv)
5165{
5166 getpos_both(argvars, rettv, FALSE);
5167}
5168
5169/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005170 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005171 */
5172 static void
5173f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5174{
5175#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005176 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005177#endif
5178}
5179
5180/*
5181 * "getreg()" function
5182 */
5183 static void
5184f_getreg(typval_T *argvars, typval_T *rettv)
5185{
5186 char_u *strregname;
5187 int regname;
5188 int arg2 = FALSE;
5189 int return_list = FALSE;
5190 int error = FALSE;
5191
5192 if (argvars[0].v_type != VAR_UNKNOWN)
5193 {
5194 strregname = get_tv_string_chk(&argvars[0]);
5195 error = strregname == NULL;
5196 if (argvars[1].v_type != VAR_UNKNOWN)
5197 {
5198 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
5199 if (!error && argvars[2].v_type != VAR_UNKNOWN)
5200 return_list = (int)get_tv_number_chk(&argvars[2], &error);
5201 }
5202 }
5203 else
5204 strregname = get_vim_var_str(VV_REG);
5205
5206 if (error)
5207 return;
5208
5209 regname = (strregname == NULL ? '"' : *strregname);
5210 if (regname == 0)
5211 regname = '"';
5212
5213 if (return_list)
5214 {
5215 rettv->v_type = VAR_LIST;
5216 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5217 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5218 if (rettv->vval.v_list == NULL)
5219 (void)rettv_list_alloc(rettv);
5220 else
5221 ++rettv->vval.v_list->lv_refcount;
5222 }
5223 else
5224 {
5225 rettv->v_type = VAR_STRING;
5226 rettv->vval.v_string = get_reg_contents(regname,
5227 arg2 ? GREG_EXPR_SRC : 0);
5228 }
5229}
5230
5231/*
5232 * "getregtype()" function
5233 */
5234 static void
5235f_getregtype(typval_T *argvars, typval_T *rettv)
5236{
5237 char_u *strregname;
5238 int regname;
5239 char_u buf[NUMBUFLEN + 2];
5240 long reglen = 0;
5241
5242 if (argvars[0].v_type != VAR_UNKNOWN)
5243 {
5244 strregname = get_tv_string_chk(&argvars[0]);
5245 if (strregname == NULL) /* type error; errmsg already given */
5246 {
5247 rettv->v_type = VAR_STRING;
5248 rettv->vval.v_string = NULL;
5249 return;
5250 }
5251 }
5252 else
5253 /* Default to v:register */
5254 strregname = get_vim_var_str(VV_REG);
5255
5256 regname = (strregname == NULL ? '"' : *strregname);
5257 if (regname == 0)
5258 regname = '"';
5259
5260 buf[0] = NUL;
5261 buf[1] = NUL;
5262 switch (get_reg_type(regname, &reglen))
5263 {
5264 case MLINE: buf[0] = 'V'; break;
5265 case MCHAR: buf[0] = 'v'; break;
5266 case MBLOCK:
5267 buf[0] = Ctrl_V;
5268 sprintf((char *)buf + 1, "%ld", reglen + 1);
5269 break;
5270 }
5271 rettv->v_type = VAR_STRING;
5272 rettv->vval.v_string = vim_strsave(buf);
5273}
5274
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005275/*
5276 * Returns information (variables, options, etc.) about a tab page
5277 * as a dictionary.
5278 */
5279 static dict_T *
5280get_tabpage_info(tabpage_T *tp, int tp_idx)
5281{
5282 win_T *wp;
5283 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005284 list_T *l;
5285
5286 dict = dict_alloc();
5287 if (dict == NULL)
5288 return NULL;
5289
Bram Moolenaar33928832016-08-18 21:22:04 +02005290 dict_add_nr_str(dict, "tabnr", tp_idx, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005291
5292 l = list_alloc();
5293 if (l != NULL)
5294 {
5295 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5296 wp; wp = wp->w_next)
5297 list_append_number(l, (varnumber_T)wp->w_id);
5298 dict_add_list(dict, "windows", l);
5299 }
5300
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005301 /* Make a reference to tabpage variables */
5302 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005303
5304 return dict;
5305}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005306
5307/*
5308 * "gettabinfo()" function
5309 */
5310 static void
5311f_gettabinfo(typval_T *argvars, typval_T *rettv)
5312{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005313 tabpage_T *tp, *tparg = NULL;
5314 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005315 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005316
5317 if (rettv_list_alloc(rettv) != OK)
5318 return;
5319
5320 if (argvars[0].v_type != VAR_UNKNOWN)
5321 {
5322 /* Information about one tab page */
5323 tparg = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5324 if (tparg == NULL)
5325 return;
5326 }
5327
5328 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005329 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005330 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005331 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005332 if (tparg != NULL && tp != tparg)
5333 continue;
5334 d = get_tabpage_info(tp, tpnr);
5335 if (d != NULL)
5336 list_append_dict(rettv->vval.v_list, d);
5337 if (tparg != NULL)
5338 return;
5339 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005340}
5341
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005342/*
5343 * "gettabvar()" function
5344 */
5345 static void
5346f_gettabvar(typval_T *argvars, typval_T *rettv)
5347{
5348 win_T *oldcurwin;
5349 tabpage_T *tp, *oldtabpage;
5350 dictitem_T *v;
5351 char_u *varname;
5352 int done = FALSE;
5353
5354 rettv->v_type = VAR_STRING;
5355 rettv->vval.v_string = NULL;
5356
5357 varname = get_tv_string_chk(&argvars[1]);
5358 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5359 if (tp != NULL && varname != NULL)
5360 {
5361 /* Set tp to be our tabpage, temporarily. Also set the window to the
5362 * first window in the tabpage, otherwise the window is not valid. */
5363 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005364 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5365 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005366 {
5367 /* look up the variable */
5368 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5369 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5370 if (v != NULL)
5371 {
5372 copy_tv(&v->di_tv, rettv);
5373 done = TRUE;
5374 }
5375 }
5376
5377 /* restore previous notion of curwin */
5378 restore_win(oldcurwin, oldtabpage, TRUE);
5379 }
5380
5381 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5382 copy_tv(&argvars[2], rettv);
5383}
5384
5385/*
5386 * "gettabwinvar()" function
5387 */
5388 static void
5389f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5390{
5391 getwinvar(argvars, rettv, 1);
5392}
5393
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005394/*
5395 * Returns information about a window as a dictionary.
5396 */
5397 static dict_T *
5398get_win_info(win_T *wp, short tpnr, short winnr)
5399{
5400 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005401
5402 dict = dict_alloc();
5403 if (dict == NULL)
5404 return NULL;
5405
Bram Moolenaar33928832016-08-18 21:22:04 +02005406 dict_add_nr_str(dict, "tabnr", tpnr, NULL);
5407 dict_add_nr_str(dict, "winnr", winnr, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005408 dict_add_nr_str(dict, "winid", wp->w_id, NULL);
5409 dict_add_nr_str(dict, "height", wp->w_height, NULL);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005410#ifdef FEAT_MENU
5411 dict_add_nr_str(dict, "winbar", wp->w_winbar_height, NULL);
5412#endif
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005413 dict_add_nr_str(dict, "width", wp->w_width, NULL);
Bram Moolenaar33928832016-08-18 21:22:04 +02005414 dict_add_nr_str(dict, "bufnr", wp->w_buffer->b_fnum, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005415
Bram Moolenaar69905d12017-08-13 18:14:47 +02005416#ifdef FEAT_TERMINAL
5417 dict_add_nr_str(dict, "terminal", bt_terminal(wp->w_buffer), NULL);
5418#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005419#ifdef FEAT_QUICKFIX
5420 dict_add_nr_str(dict, "quickfix", bt_quickfix(wp->w_buffer), NULL);
5421 dict_add_nr_str(dict, "loclist",
5422 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL), NULL);
5423#endif
5424
Bram Moolenaar30567352016-08-27 21:25:44 +02005425 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005426 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005427
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005428 return dict;
5429}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005430
5431/*
5432 * "getwininfo()" function
5433 */
5434 static void
5435f_getwininfo(typval_T *argvars, typval_T *rettv)
5436{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005437 tabpage_T *tp;
5438 win_T *wp = NULL, *wparg = NULL;
5439 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005440 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005441
5442 if (rettv_list_alloc(rettv) != OK)
5443 return;
5444
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005445 if (argvars[0].v_type != VAR_UNKNOWN)
5446 {
5447 wparg = win_id2wp(argvars);
5448 if (wparg == NULL)
5449 return;
5450 }
5451
5452 /* Collect information about either all the windows across all the tab
5453 * pages or one particular window.
5454 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005455 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005456 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005457 tabnr++;
5458 winnr = 0;
5459 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005460 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005461 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005462 if (wparg != NULL && wp != wparg)
5463 continue;
5464 d = get_win_info(wp, tabnr, winnr);
5465 if (d != NULL)
5466 list_append_dict(rettv->vval.v_list, d);
5467 if (wparg != NULL)
5468 /* found information about a specific window */
5469 return;
5470 }
5471 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005472}
5473
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005474/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005475 * "win_findbuf()" function
5476 */
5477 static void
5478f_win_findbuf(typval_T *argvars, typval_T *rettv)
5479{
5480 if (rettv_list_alloc(rettv) != FAIL)
5481 win_findbuf(argvars, rettv->vval.v_list);
5482}
5483
5484/*
5485 * "win_getid()" function
5486 */
5487 static void
5488f_win_getid(typval_T *argvars, typval_T *rettv)
5489{
5490 rettv->vval.v_number = win_getid(argvars);
5491}
5492
5493/*
5494 * "win_gotoid()" function
5495 */
5496 static void
5497f_win_gotoid(typval_T *argvars, typval_T *rettv)
5498{
5499 rettv->vval.v_number = win_gotoid(argvars);
5500}
5501
5502/*
5503 * "win_id2tabwin()" function
5504 */
5505 static void
5506f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5507{
5508 if (rettv_list_alloc(rettv) != FAIL)
5509 win_id2tabwin(argvars, rettv->vval.v_list);
5510}
5511
5512/*
5513 * "win_id2win()" function
5514 */
5515 static void
5516f_win_id2win(typval_T *argvars, typval_T *rettv)
5517{
5518 rettv->vval.v_number = win_id2win(argvars);
5519}
5520
5521/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005522 * "win_screenpos()" function
5523 */
5524 static void
5525f_win_screenpos(typval_T *argvars, typval_T *rettv)
5526{
5527 win_T *wp;
5528
5529 if (rettv_list_alloc(rettv) == FAIL)
5530 return;
5531
5532 wp = find_win_by_nr(&argvars[0], NULL);
5533 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5534 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5535}
5536
5537/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005538 * "getwinpos({timeout})" function
5539 */
5540 static void
5541f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
5542{
5543 int x = -1;
5544 int y = -1;
5545
5546 if (rettv_list_alloc(rettv) == FAIL)
5547 return;
5548#ifdef FEAT_GUI
5549 if (gui.in_use)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01005550 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005551# if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5552 else
5553# endif
5554#endif
5555#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5556 {
5557 varnumber_T timeout = 100;
5558
5559 if (argvars[0].v_type != VAR_UNKNOWN)
5560 timeout = get_tv_number(&argvars[0]);
5561 term_get_winpos(&x, &y, timeout);
5562 }
5563#endif
5564 list_append_number(rettv->vval.v_list, (varnumber_T)x);
5565 list_append_number(rettv->vval.v_list, (varnumber_T)y);
5566}
5567
5568
5569/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005570 * "getwinposx()" function
5571 */
5572 static void
5573f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5574{
5575 rettv->vval.v_number = -1;
5576#ifdef FEAT_GUI
5577 if (gui.in_use)
5578 {
5579 int x, y;
5580
5581 if (gui_mch_get_winpos(&x, &y) == OK)
5582 rettv->vval.v_number = x;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005583 return;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005584 }
5585#endif
5586#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5587 {
5588 int x, y;
5589
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005590 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005591 rettv->vval.v_number = x;
5592 }
5593#endif
5594}
5595
5596/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005597 * "getwinposy()" function
5598 */
5599 static void
5600f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5601{
5602 rettv->vval.v_number = -1;
5603#ifdef FEAT_GUI
5604 if (gui.in_use)
5605 {
5606 int x, y;
5607
5608 if (gui_mch_get_winpos(&x, &y) == OK)
5609 rettv->vval.v_number = y;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005610 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005611 }
5612#endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005613#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5614 {
5615 int x, y;
5616
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005617 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005618 rettv->vval.v_number = y;
5619 }
5620#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005621}
5622
5623/*
5624 * "getwinvar()" function
5625 */
5626 static void
5627f_getwinvar(typval_T *argvars, typval_T *rettv)
5628{
5629 getwinvar(argvars, rettv, 0);
5630}
5631
5632/*
5633 * "glob()" function
5634 */
5635 static void
5636f_glob(typval_T *argvars, typval_T *rettv)
5637{
5638 int options = WILD_SILENT|WILD_USE_NL;
5639 expand_T xpc;
5640 int error = FALSE;
5641
5642 /* When the optional second argument is non-zero, don't remove matches
5643 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5644 rettv->v_type = VAR_STRING;
5645 if (argvars[1].v_type != VAR_UNKNOWN)
5646 {
5647 if (get_tv_number_chk(&argvars[1], &error))
5648 options |= WILD_KEEP_ALL;
5649 if (argvars[2].v_type != VAR_UNKNOWN)
5650 {
5651 if (get_tv_number_chk(&argvars[2], &error))
5652 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005653 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005654 }
5655 if (argvars[3].v_type != VAR_UNKNOWN
5656 && get_tv_number_chk(&argvars[3], &error))
5657 options |= WILD_ALLLINKS;
5658 }
5659 }
5660 if (!error)
5661 {
5662 ExpandInit(&xpc);
5663 xpc.xp_context = EXPAND_FILES;
5664 if (p_wic)
5665 options += WILD_ICASE;
5666 if (rettv->v_type == VAR_STRING)
5667 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
5668 NULL, options, WILD_ALL);
5669 else if (rettv_list_alloc(rettv) != FAIL)
5670 {
5671 int i;
5672
5673 ExpandOne(&xpc, get_tv_string(&argvars[0]),
5674 NULL, options, WILD_ALL_KEEP);
5675 for (i = 0; i < xpc.xp_numfiles; i++)
5676 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5677
5678 ExpandCleanup(&xpc);
5679 }
5680 }
5681 else
5682 rettv->vval.v_string = NULL;
5683}
5684
5685/*
5686 * "globpath()" function
5687 */
5688 static void
5689f_globpath(typval_T *argvars, typval_T *rettv)
5690{
5691 int flags = 0;
5692 char_u buf1[NUMBUFLEN];
5693 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
5694 int error = FALSE;
5695 garray_T ga;
5696 int i;
5697
5698 /* When the optional second argument is non-zero, don't remove matches
5699 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5700 rettv->v_type = VAR_STRING;
5701 if (argvars[2].v_type != VAR_UNKNOWN)
5702 {
5703 if (get_tv_number_chk(&argvars[2], &error))
5704 flags |= WILD_KEEP_ALL;
5705 if (argvars[3].v_type != VAR_UNKNOWN)
5706 {
5707 if (get_tv_number_chk(&argvars[3], &error))
5708 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005709 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005710 }
5711 if (argvars[4].v_type != VAR_UNKNOWN
5712 && get_tv_number_chk(&argvars[4], &error))
5713 flags |= WILD_ALLLINKS;
5714 }
5715 }
5716 if (file != NULL && !error)
5717 {
5718 ga_init2(&ga, (int)sizeof(char_u *), 10);
5719 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
5720 if (rettv->v_type == VAR_STRING)
5721 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
5722 else if (rettv_list_alloc(rettv) != FAIL)
5723 for (i = 0; i < ga.ga_len; ++i)
5724 list_append_string(rettv->vval.v_list,
5725 ((char_u **)(ga.ga_data))[i], -1);
5726 ga_clear_strings(&ga);
5727 }
5728 else
5729 rettv->vval.v_string = NULL;
5730}
5731
5732/*
5733 * "glob2regpat()" function
5734 */
5735 static void
5736f_glob2regpat(typval_T *argvars, typval_T *rettv)
5737{
5738 char_u *pat = get_tv_string_chk(&argvars[0]);
5739
5740 rettv->v_type = VAR_STRING;
5741 rettv->vval.v_string = (pat == NULL)
5742 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
5743}
5744
5745/* for VIM_VERSION_ defines */
5746#include "version.h"
5747
5748/*
5749 * "has()" function
5750 */
5751 static void
5752f_has(typval_T *argvars, typval_T *rettv)
5753{
5754 int i;
5755 char_u *name;
5756 int n = FALSE;
5757 static char *(has_list[]) =
5758 {
5759#ifdef AMIGA
5760 "amiga",
5761# ifdef FEAT_ARP
5762 "arp",
5763# endif
5764#endif
5765#ifdef __BEOS__
5766 "beos",
5767#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02005768#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01005769 "mac", /* Mac OS X (and, once, Mac OS Classic) */
5770 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02005771# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01005772 "macunix", /* Mac OS X, with the darwin feature */
5773 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02005774# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005775#endif
5776#ifdef __QNX__
5777 "qnx",
5778#endif
5779#ifdef UNIX
5780 "unix",
5781#endif
5782#ifdef VMS
5783 "vms",
5784#endif
5785#ifdef WIN32
5786 "win32",
5787#endif
5788#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
5789 "win32unix",
5790#endif
5791#if defined(WIN64) || defined(_WIN64)
5792 "win64",
5793#endif
5794#ifdef EBCDIC
5795 "ebcdic",
5796#endif
5797#ifndef CASE_INSENSITIVE_FILENAME
5798 "fname_case",
5799#endif
5800#ifdef HAVE_ACL
5801 "acl",
5802#endif
5803#ifdef FEAT_ARABIC
5804 "arabic",
5805#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005806 "autocmd",
Bram Moolenaare42a6d22017-11-12 19:21:51 +01005807#ifdef FEAT_AUTOSERVERNAME
5808 "autoservername",
5809#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01005810#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005811 "balloon_eval",
5812# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
5813 "balloon_multiline",
5814# endif
5815#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01005816#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01005817 "balloon_eval_term",
5818#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005819#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
5820 "builtin_terms",
5821# ifdef ALL_BUILTIN_TCAPS
5822 "all_builtin_terms",
5823# endif
5824#endif
5825#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
5826 || defined(FEAT_GUI_W32) \
5827 || defined(FEAT_GUI_MOTIF))
5828 "browsefilter",
5829#endif
5830#ifdef FEAT_BYTEOFF
5831 "byte_offset",
5832#endif
5833#ifdef FEAT_JOB_CHANNEL
5834 "channel",
5835#endif
5836#ifdef FEAT_CINDENT
5837 "cindent",
5838#endif
5839#ifdef FEAT_CLIENTSERVER
5840 "clientserver",
5841#endif
5842#ifdef FEAT_CLIPBOARD
5843 "clipboard",
5844#endif
5845#ifdef FEAT_CMDL_COMPL
5846 "cmdline_compl",
5847#endif
5848#ifdef FEAT_CMDHIST
5849 "cmdline_hist",
5850#endif
5851#ifdef FEAT_COMMENTS
5852 "comments",
5853#endif
5854#ifdef FEAT_CONCEAL
5855 "conceal",
5856#endif
5857#ifdef FEAT_CRYPT
5858 "cryptv",
5859 "crypt-blowfish",
5860 "crypt-blowfish2",
5861#endif
5862#ifdef FEAT_CSCOPE
5863 "cscope",
5864#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005865 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005866#ifdef CURSOR_SHAPE
5867 "cursorshape",
5868#endif
5869#ifdef DEBUG
5870 "debug",
5871#endif
5872#ifdef FEAT_CON_DIALOG
5873 "dialog_con",
5874#endif
5875#ifdef FEAT_GUI_DIALOG
5876 "dialog_gui",
5877#endif
5878#ifdef FEAT_DIFF
5879 "diff",
5880#endif
5881#ifdef FEAT_DIGRAPHS
5882 "digraphs",
5883#endif
5884#ifdef FEAT_DIRECTX
5885 "directx",
5886#endif
5887#ifdef FEAT_DND
5888 "dnd",
5889#endif
5890#ifdef FEAT_EMACS_TAGS
5891 "emacs_tags",
5892#endif
5893 "eval", /* always present, of course! */
5894 "ex_extra", /* graduated feature */
5895#ifdef FEAT_SEARCH_EXTRA
5896 "extra_search",
5897#endif
5898#ifdef FEAT_FKMAP
5899 "farsi",
5900#endif
5901#ifdef FEAT_SEARCHPATH
5902 "file_in_path",
5903#endif
5904#ifdef FEAT_FILTERPIPE
5905 "filterpipe",
5906#endif
5907#ifdef FEAT_FIND_ID
5908 "find_in_path",
5909#endif
5910#ifdef FEAT_FLOAT
5911 "float",
5912#endif
5913#ifdef FEAT_FOLDING
5914 "folding",
5915#endif
5916#ifdef FEAT_FOOTER
5917 "footer",
5918#endif
5919#if !defined(USE_SYSTEM) && defined(UNIX)
5920 "fork",
5921#endif
5922#ifdef FEAT_GETTEXT
5923 "gettext",
5924#endif
5925#ifdef FEAT_GUI
5926 "gui",
5927#endif
5928#ifdef FEAT_GUI_ATHENA
5929# ifdef FEAT_GUI_NEXTAW
5930 "gui_neXtaw",
5931# else
5932 "gui_athena",
5933# endif
5934#endif
5935#ifdef FEAT_GUI_GTK
5936 "gui_gtk",
5937# ifdef USE_GTK3
5938 "gui_gtk3",
5939# else
5940 "gui_gtk2",
5941# endif
5942#endif
5943#ifdef FEAT_GUI_GNOME
5944 "gui_gnome",
5945#endif
5946#ifdef FEAT_GUI_MAC
5947 "gui_mac",
5948#endif
5949#ifdef FEAT_GUI_MOTIF
5950 "gui_motif",
5951#endif
5952#ifdef FEAT_GUI_PHOTON
5953 "gui_photon",
5954#endif
5955#ifdef FEAT_GUI_W32
5956 "gui_win32",
5957#endif
5958#ifdef FEAT_HANGULIN
5959 "hangul_input",
5960#endif
5961#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
5962 "iconv",
5963#endif
5964#ifdef FEAT_INS_EXPAND
5965 "insert_expand",
5966#endif
5967#ifdef FEAT_JOB_CHANNEL
5968 "job",
5969#endif
5970#ifdef FEAT_JUMPLIST
5971 "jumplist",
5972#endif
5973#ifdef FEAT_KEYMAP
5974 "keymap",
5975#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02005976 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005977#ifdef FEAT_LANGMAP
5978 "langmap",
5979#endif
5980#ifdef FEAT_LIBCALL
5981 "libcall",
5982#endif
5983#ifdef FEAT_LINEBREAK
5984 "linebreak",
5985#endif
5986#ifdef FEAT_LISP
5987 "lispindent",
5988#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005989 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005990#ifdef FEAT_LOCALMAP
5991 "localmap",
5992#endif
5993#ifdef FEAT_LUA
5994# ifndef DYNAMIC_LUA
5995 "lua",
5996# endif
5997#endif
5998#ifdef FEAT_MENU
5999 "menu",
6000#endif
6001#ifdef FEAT_SESSION
6002 "mksession",
6003#endif
6004#ifdef FEAT_MODIFY_FNAME
6005 "modify_fname",
6006#endif
6007#ifdef FEAT_MOUSE
6008 "mouse",
6009#endif
6010#ifdef FEAT_MOUSESHAPE
6011 "mouseshape",
6012#endif
6013#if defined(UNIX) || defined(VMS)
6014# ifdef FEAT_MOUSE_DEC
6015 "mouse_dec",
6016# endif
6017# ifdef FEAT_MOUSE_GPM
6018 "mouse_gpm",
6019# endif
6020# ifdef FEAT_MOUSE_JSB
6021 "mouse_jsbterm",
6022# endif
6023# ifdef FEAT_MOUSE_NET
6024 "mouse_netterm",
6025# endif
6026# ifdef FEAT_MOUSE_PTERM
6027 "mouse_pterm",
6028# endif
6029# ifdef FEAT_MOUSE_SGR
6030 "mouse_sgr",
6031# endif
6032# ifdef FEAT_SYSMOUSE
6033 "mouse_sysmouse",
6034# endif
6035# ifdef FEAT_MOUSE_URXVT
6036 "mouse_urxvt",
6037# endif
6038# ifdef FEAT_MOUSE_XTERM
6039 "mouse_xterm",
6040# endif
6041#endif
6042#ifdef FEAT_MBYTE
6043 "multi_byte",
6044#endif
6045#ifdef FEAT_MBYTE_IME
6046 "multi_byte_ime",
6047#endif
6048#ifdef FEAT_MULTI_LANG
6049 "multi_lang",
6050#endif
6051#ifdef FEAT_MZSCHEME
6052#ifndef DYNAMIC_MZSCHEME
6053 "mzscheme",
6054#endif
6055#endif
6056#ifdef FEAT_NUM64
6057 "num64",
6058#endif
6059#ifdef FEAT_OLE
6060 "ole",
6061#endif
6062 "packages",
6063#ifdef FEAT_PATH_EXTRA
6064 "path_extra",
6065#endif
6066#ifdef FEAT_PERL
6067#ifndef DYNAMIC_PERL
6068 "perl",
6069#endif
6070#endif
6071#ifdef FEAT_PERSISTENT_UNDO
6072 "persistent_undo",
6073#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006074#if defined(FEAT_PYTHON)
6075 "python_compiled",
6076# if defined(DYNAMIC_PYTHON)
6077 "python_dynamic",
6078# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006079 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006080 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006081# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006082#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006083#if defined(FEAT_PYTHON3)
6084 "python3_compiled",
6085# if defined(DYNAMIC_PYTHON3)
6086 "python3_dynamic",
6087# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006088 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006089 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006090# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006091#endif
6092#ifdef FEAT_POSTSCRIPT
6093 "postscript",
6094#endif
6095#ifdef FEAT_PRINTER
6096 "printer",
6097#endif
6098#ifdef FEAT_PROFILE
6099 "profile",
6100#endif
6101#ifdef FEAT_RELTIME
6102 "reltime",
6103#endif
6104#ifdef FEAT_QUICKFIX
6105 "quickfix",
6106#endif
6107#ifdef FEAT_RIGHTLEFT
6108 "rightleft",
6109#endif
6110#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6111 "ruby",
6112#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006113 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006114#ifdef FEAT_CMDL_INFO
6115 "showcmd",
6116 "cmdline_info",
6117#endif
6118#ifdef FEAT_SIGNS
6119 "signs",
6120#endif
6121#ifdef FEAT_SMARTINDENT
6122 "smartindent",
6123#endif
6124#ifdef STARTUPTIME
6125 "startuptime",
6126#endif
6127#ifdef FEAT_STL_OPT
6128 "statusline",
6129#endif
6130#ifdef FEAT_SUN_WORKSHOP
6131 "sun_workshop",
6132#endif
6133#ifdef FEAT_NETBEANS_INTG
6134 "netbeans_intg",
6135#endif
6136#ifdef FEAT_SPELL
6137 "spell",
6138#endif
6139#ifdef FEAT_SYN_HL
6140 "syntax",
6141#endif
6142#if defined(USE_SYSTEM) || !defined(UNIX)
6143 "system",
6144#endif
6145#ifdef FEAT_TAG_BINS
6146 "tag_binary",
6147#endif
6148#ifdef FEAT_TAG_OLDSTATIC
6149 "tag_old_static",
6150#endif
6151#ifdef FEAT_TAG_ANYWHITE
6152 "tag_any_white",
6153#endif
6154#ifdef FEAT_TCL
6155# ifndef DYNAMIC_TCL
6156 "tcl",
6157# endif
6158#endif
6159#ifdef FEAT_TERMGUICOLORS
6160 "termguicolors",
6161#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006162#if defined(FEAT_TERMINAL) && !defined(WIN3264)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006163 "terminal",
6164#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006165#ifdef TERMINFO
6166 "terminfo",
6167#endif
6168#ifdef FEAT_TERMRESPONSE
6169 "termresponse",
6170#endif
6171#ifdef FEAT_TEXTOBJ
6172 "textobjects",
6173#endif
6174#ifdef HAVE_TGETENT
6175 "tgetent",
6176#endif
6177#ifdef FEAT_TIMERS
6178 "timers",
6179#endif
6180#ifdef FEAT_TITLE
6181 "title",
6182#endif
6183#ifdef FEAT_TOOLBAR
6184 "toolbar",
6185#endif
6186#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6187 "unnamedplus",
6188#endif
6189#ifdef FEAT_USR_CMDS
6190 "user-commands", /* was accidentally included in 5.4 */
6191 "user_commands",
6192#endif
6193#ifdef FEAT_VIMINFO
6194 "viminfo",
6195#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006196 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006197#ifdef FEAT_VIRTUALEDIT
6198 "virtualedit",
6199#endif
6200 "visual",
6201#ifdef FEAT_VISUALEXTRA
6202 "visualextra",
6203#endif
6204#ifdef FEAT_VREPLACE
6205 "vreplace",
6206#endif
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006207#ifdef FEAT_VTP
6208 "vtp",
6209#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006210#ifdef FEAT_WILDIGN
6211 "wildignore",
6212#endif
6213#ifdef FEAT_WILDMENU
6214 "wildmenu",
6215#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006216 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006217#ifdef FEAT_WAK
6218 "winaltkeys",
6219#endif
6220#ifdef FEAT_WRITEBACKUP
6221 "writebackup",
6222#endif
6223#ifdef FEAT_XIM
6224 "xim",
6225#endif
6226#ifdef FEAT_XFONTSET
6227 "xfontset",
6228#endif
6229#ifdef FEAT_XPM_W32
6230 "xpm",
6231 "xpm_w32", /* for backward compatibility */
6232#else
6233# if defined(HAVE_XPM)
6234 "xpm",
6235# endif
6236#endif
6237#ifdef USE_XSMP
6238 "xsmp",
6239#endif
6240#ifdef USE_XSMP_INTERACT
6241 "xsmp_interact",
6242#endif
6243#ifdef FEAT_XCLIPBOARD
6244 "xterm_clipboard",
6245#endif
6246#ifdef FEAT_XTERM_SAVE
6247 "xterm_save",
6248#endif
6249#if defined(UNIX) && defined(FEAT_X11)
6250 "X11",
6251#endif
6252 NULL
6253 };
6254
6255 name = get_tv_string(&argvars[0]);
6256 for (i = 0; has_list[i] != NULL; ++i)
6257 if (STRICMP(name, has_list[i]) == 0)
6258 {
6259 n = TRUE;
6260 break;
6261 }
6262
6263 if (n == FALSE)
6264 {
6265 if (STRNICMP(name, "patch", 5) == 0)
6266 {
6267 if (name[5] == '-'
6268 && STRLEN(name) >= 11
6269 && vim_isdigit(name[6])
6270 && vim_isdigit(name[8])
6271 && vim_isdigit(name[10]))
6272 {
6273 int major = atoi((char *)name + 6);
6274 int minor = atoi((char *)name + 8);
6275
6276 /* Expect "patch-9.9.01234". */
6277 n = (major < VIM_VERSION_MAJOR
6278 || (major == VIM_VERSION_MAJOR
6279 && (minor < VIM_VERSION_MINOR
6280 || (minor == VIM_VERSION_MINOR
6281 && has_patch(atoi((char *)name + 10))))));
6282 }
6283 else
6284 n = has_patch(atoi((char *)name + 5));
6285 }
6286 else if (STRICMP(name, "vim_starting") == 0)
6287 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006288 else if (STRICMP(name, "ttyin") == 0)
6289 n = mch_input_isatty();
6290 else if (STRICMP(name, "ttyout") == 0)
6291 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006292#ifdef FEAT_MBYTE
6293 else if (STRICMP(name, "multi_byte_encoding") == 0)
6294 n = has_mbyte;
6295#endif
6296#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
6297 else if (STRICMP(name, "balloon_multiline") == 0)
6298 n = multiline_balloon_available();
6299#endif
6300#ifdef DYNAMIC_TCL
6301 else if (STRICMP(name, "tcl") == 0)
6302 n = tcl_enabled(FALSE);
6303#endif
6304#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6305 else if (STRICMP(name, "iconv") == 0)
6306 n = iconv_enabled(FALSE);
6307#endif
6308#ifdef DYNAMIC_LUA
6309 else if (STRICMP(name, "lua") == 0)
6310 n = lua_enabled(FALSE);
6311#endif
6312#ifdef DYNAMIC_MZSCHEME
6313 else if (STRICMP(name, "mzscheme") == 0)
6314 n = mzscheme_enabled(FALSE);
6315#endif
6316#ifdef DYNAMIC_RUBY
6317 else if (STRICMP(name, "ruby") == 0)
6318 n = ruby_enabled(FALSE);
6319#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006320#ifdef DYNAMIC_PYTHON
6321 else if (STRICMP(name, "python") == 0)
6322 n = python_enabled(FALSE);
6323#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006324#ifdef DYNAMIC_PYTHON3
6325 else if (STRICMP(name, "python3") == 0)
6326 n = python3_enabled(FALSE);
6327#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006328#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6329 else if (STRICMP(name, "pythonx") == 0)
6330 {
6331# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6332 if (p_pyx == 0)
6333 n = python3_enabled(FALSE) || python_enabled(FALSE);
6334 else if (p_pyx == 3)
6335 n = python3_enabled(FALSE);
6336 else if (p_pyx == 2)
6337 n = python_enabled(FALSE);
6338# elif defined(DYNAMIC_PYTHON)
6339 n = python_enabled(FALSE);
6340# elif defined(DYNAMIC_PYTHON3)
6341 n = python3_enabled(FALSE);
6342# endif
6343 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006344#endif
6345#ifdef DYNAMIC_PERL
6346 else if (STRICMP(name, "perl") == 0)
6347 n = perl_enabled(FALSE);
6348#endif
6349#ifdef FEAT_GUI
6350 else if (STRICMP(name, "gui_running") == 0)
6351 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006352# ifdef FEAT_BROWSE
6353 else if (STRICMP(name, "browse") == 0)
6354 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6355# endif
6356#endif
6357#ifdef FEAT_SYN_HL
6358 else if (STRICMP(name, "syntax_items") == 0)
6359 n = syntax_present(curwin);
6360#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006361#ifdef FEAT_VTP
6362 else if (STRICMP(name, "vcon") == 0)
6363 n = has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006364#endif
6365#ifdef FEAT_NETBEANS_INTG
6366 else if (STRICMP(name, "netbeans_enabled") == 0)
6367 n = netbeans_active();
6368#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006369#if defined(FEAT_TERMINAL) && defined(WIN3264)
6370 else if (STRICMP(name, "terminal") == 0)
6371 n = terminal_enabled();
6372#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006373 }
6374
6375 rettv->vval.v_number = n;
6376}
6377
6378/*
6379 * "has_key()" function
6380 */
6381 static void
6382f_has_key(typval_T *argvars, typval_T *rettv)
6383{
6384 if (argvars[0].v_type != VAR_DICT)
6385 {
6386 EMSG(_(e_dictreq));
6387 return;
6388 }
6389 if (argvars[0].vval.v_dict == NULL)
6390 return;
6391
6392 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
6393 get_tv_string(&argvars[1]), -1) != NULL;
6394}
6395
6396/*
6397 * "haslocaldir()" function
6398 */
6399 static void
6400f_haslocaldir(typval_T *argvars, typval_T *rettv)
6401{
6402 win_T *wp = NULL;
6403
6404 wp = find_tabwin(&argvars[0], &argvars[1]);
6405 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6406}
6407
6408/*
6409 * "hasmapto()" function
6410 */
6411 static void
6412f_hasmapto(typval_T *argvars, typval_T *rettv)
6413{
6414 char_u *name;
6415 char_u *mode;
6416 char_u buf[NUMBUFLEN];
6417 int abbr = FALSE;
6418
6419 name = get_tv_string(&argvars[0]);
6420 if (argvars[1].v_type == VAR_UNKNOWN)
6421 mode = (char_u *)"nvo";
6422 else
6423 {
6424 mode = get_tv_string_buf(&argvars[1], buf);
6425 if (argvars[2].v_type != VAR_UNKNOWN)
6426 abbr = (int)get_tv_number(&argvars[2]);
6427 }
6428
6429 if (map_to_exists(name, mode, abbr))
6430 rettv->vval.v_number = TRUE;
6431 else
6432 rettv->vval.v_number = FALSE;
6433}
6434
6435/*
6436 * "histadd()" function
6437 */
6438 static void
6439f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6440{
6441#ifdef FEAT_CMDHIST
6442 int histype;
6443 char_u *str;
6444 char_u buf[NUMBUFLEN];
6445#endif
6446
6447 rettv->vval.v_number = FALSE;
6448 if (check_restricted() || check_secure())
6449 return;
6450#ifdef FEAT_CMDHIST
6451 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6452 histype = str != NULL ? get_histtype(str) : -1;
6453 if (histype >= 0)
6454 {
6455 str = get_tv_string_buf(&argvars[1], buf);
6456 if (*str != NUL)
6457 {
6458 init_history();
6459 add_to_history(histype, str, FALSE, NUL);
6460 rettv->vval.v_number = TRUE;
6461 return;
6462 }
6463 }
6464#endif
6465}
6466
6467/*
6468 * "histdel()" function
6469 */
6470 static void
6471f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6472{
6473#ifdef FEAT_CMDHIST
6474 int n;
6475 char_u buf[NUMBUFLEN];
6476 char_u *str;
6477
6478 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6479 if (str == NULL)
6480 n = 0;
6481 else if (argvars[1].v_type == VAR_UNKNOWN)
6482 /* only one argument: clear entire history */
6483 n = clr_history(get_histtype(str));
6484 else if (argvars[1].v_type == VAR_NUMBER)
6485 /* index given: remove that entry */
6486 n = del_history_idx(get_histtype(str),
6487 (int)get_tv_number(&argvars[1]));
6488 else
6489 /* string given: remove all matching entries */
6490 n = del_history_entry(get_histtype(str),
6491 get_tv_string_buf(&argvars[1], buf));
6492 rettv->vval.v_number = n;
6493#endif
6494}
6495
6496/*
6497 * "histget()" function
6498 */
6499 static void
6500f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6501{
6502#ifdef FEAT_CMDHIST
6503 int type;
6504 int idx;
6505 char_u *str;
6506
6507 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6508 if (str == NULL)
6509 rettv->vval.v_string = NULL;
6510 else
6511 {
6512 type = get_histtype(str);
6513 if (argvars[1].v_type == VAR_UNKNOWN)
6514 idx = get_history_idx(type);
6515 else
6516 idx = (int)get_tv_number_chk(&argvars[1], NULL);
6517 /* -1 on type error */
6518 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6519 }
6520#else
6521 rettv->vval.v_string = NULL;
6522#endif
6523 rettv->v_type = VAR_STRING;
6524}
6525
6526/*
6527 * "histnr()" function
6528 */
6529 static void
6530f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6531{
6532 int i;
6533
6534#ifdef FEAT_CMDHIST
6535 char_u *history = get_tv_string_chk(&argvars[0]);
6536
6537 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6538 if (i >= HIST_CMD && i < HIST_COUNT)
6539 i = get_history_idx(i);
6540 else
6541#endif
6542 i = -1;
6543 rettv->vval.v_number = i;
6544}
6545
6546/*
6547 * "highlightID(name)" function
6548 */
6549 static void
6550f_hlID(typval_T *argvars, typval_T *rettv)
6551{
6552 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
6553}
6554
6555/*
6556 * "highlight_exists()" function
6557 */
6558 static void
6559f_hlexists(typval_T *argvars, typval_T *rettv)
6560{
6561 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
6562}
6563
6564/*
6565 * "hostname()" function
6566 */
6567 static void
6568f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6569{
6570 char_u hostname[256];
6571
6572 mch_get_host_name(hostname, 256);
6573 rettv->v_type = VAR_STRING;
6574 rettv->vval.v_string = vim_strsave(hostname);
6575}
6576
6577/*
6578 * iconv() function
6579 */
6580 static void
6581f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6582{
6583#ifdef FEAT_MBYTE
6584 char_u buf1[NUMBUFLEN];
6585 char_u buf2[NUMBUFLEN];
6586 char_u *from, *to, *str;
6587 vimconv_T vimconv;
6588#endif
6589
6590 rettv->v_type = VAR_STRING;
6591 rettv->vval.v_string = NULL;
6592
6593#ifdef FEAT_MBYTE
6594 str = get_tv_string(&argvars[0]);
6595 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
6596 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
6597 vimconv.vc_type = CONV_NONE;
6598 convert_setup(&vimconv, from, to);
6599
6600 /* If the encodings are equal, no conversion needed. */
6601 if (vimconv.vc_type == CONV_NONE)
6602 rettv->vval.v_string = vim_strsave(str);
6603 else
6604 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6605
6606 convert_setup(&vimconv, NULL, NULL);
6607 vim_free(from);
6608 vim_free(to);
6609#endif
6610}
6611
6612/*
6613 * "indent()" function
6614 */
6615 static void
6616f_indent(typval_T *argvars, typval_T *rettv)
6617{
6618 linenr_T lnum;
6619
6620 lnum = get_tv_lnum(argvars);
6621 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6622 rettv->vval.v_number = get_indent_lnum(lnum);
6623 else
6624 rettv->vval.v_number = -1;
6625}
6626
6627/*
6628 * "index()" function
6629 */
6630 static void
6631f_index(typval_T *argvars, typval_T *rettv)
6632{
6633 list_T *l;
6634 listitem_T *item;
6635 long idx = 0;
6636 int ic = FALSE;
6637
6638 rettv->vval.v_number = -1;
6639 if (argvars[0].v_type != VAR_LIST)
6640 {
6641 EMSG(_(e_listreq));
6642 return;
6643 }
6644 l = argvars[0].vval.v_list;
6645 if (l != NULL)
6646 {
6647 item = l->lv_first;
6648 if (argvars[2].v_type != VAR_UNKNOWN)
6649 {
6650 int error = FALSE;
6651
6652 /* Start at specified item. Use the cached index that list_find()
6653 * sets, so that a negative number also works. */
6654 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
6655 idx = l->lv_idx;
6656 if (argvars[3].v_type != VAR_UNKNOWN)
6657 ic = (int)get_tv_number_chk(&argvars[3], &error);
6658 if (error)
6659 item = NULL;
6660 }
6661
6662 for ( ; item != NULL; item = item->li_next, ++idx)
6663 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6664 {
6665 rettv->vval.v_number = idx;
6666 break;
6667 }
6668 }
6669}
6670
6671static int inputsecret_flag = 0;
6672
6673/*
6674 * "input()" function
6675 * Also handles inputsecret() when inputsecret is set.
6676 */
6677 static void
6678f_input(typval_T *argvars, typval_T *rettv)
6679{
6680 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6681}
6682
6683/*
6684 * "inputdialog()" function
6685 */
6686 static void
6687f_inputdialog(typval_T *argvars, typval_T *rettv)
6688{
6689#if defined(FEAT_GUI_TEXTDIALOG)
6690 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
6691 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6692 {
6693 char_u *message;
6694 char_u buf[NUMBUFLEN];
6695 char_u *defstr = (char_u *)"";
6696
6697 message = get_tv_string_chk(&argvars[0]);
6698 if (argvars[1].v_type != VAR_UNKNOWN
6699 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
6700 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6701 else
6702 IObuff[0] = NUL;
6703 if (message != NULL && defstr != NULL
6704 && do_dialog(VIM_QUESTION, NULL, message,
6705 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6706 rettv->vval.v_string = vim_strsave(IObuff);
6707 else
6708 {
6709 if (message != NULL && defstr != NULL
6710 && argvars[1].v_type != VAR_UNKNOWN
6711 && argvars[2].v_type != VAR_UNKNOWN)
6712 rettv->vval.v_string = vim_strsave(
6713 get_tv_string_buf(&argvars[2], buf));
6714 else
6715 rettv->vval.v_string = NULL;
6716 }
6717 rettv->v_type = VAR_STRING;
6718 }
6719 else
6720#endif
6721 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6722}
6723
6724/*
6725 * "inputlist()" function
6726 */
6727 static void
6728f_inputlist(typval_T *argvars, typval_T *rettv)
6729{
6730 listitem_T *li;
6731 int selected;
6732 int mouse_used;
6733
6734#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02006735 /* While starting up, there is no place to enter text. When running tests
6736 * with --not-a-term we assume feedkeys() will be used. */
6737 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006738 return;
6739#endif
6740 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6741 {
6742 EMSG2(_(e_listarg), "inputlist()");
6743 return;
6744 }
6745
6746 msg_start();
6747 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
6748 lines_left = Rows; /* avoid more prompt */
6749 msg_scroll = TRUE;
6750 msg_clr_eos();
6751
6752 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
6753 {
6754 msg_puts(get_tv_string(&li->li_tv));
6755 msg_putchar('\n');
6756 }
6757
6758 /* Ask for choice. */
6759 selected = prompt_for_number(&mouse_used);
6760 if (mouse_used)
6761 selected -= lines_left;
6762
6763 rettv->vval.v_number = selected;
6764}
6765
6766
6767static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6768
6769/*
6770 * "inputrestore()" function
6771 */
6772 static void
6773f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6774{
6775 if (ga_userinput.ga_len > 0)
6776 {
6777 --ga_userinput.ga_len;
6778 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
6779 + ga_userinput.ga_len);
6780 /* default return is zero == OK */
6781 }
6782 else if (p_verbose > 1)
6783 {
6784 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
6785 rettv->vval.v_number = 1; /* Failed */
6786 }
6787}
6788
6789/*
6790 * "inputsave()" function
6791 */
6792 static void
6793f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
6794{
6795 /* Add an entry to the stack of typeahead storage. */
6796 if (ga_grow(&ga_userinput, 1) == OK)
6797 {
6798 save_typeahead((tasave_T *)(ga_userinput.ga_data)
6799 + ga_userinput.ga_len);
6800 ++ga_userinput.ga_len;
6801 /* default return is zero == OK */
6802 }
6803 else
6804 rettv->vval.v_number = 1; /* Failed */
6805}
6806
6807/*
6808 * "inputsecret()" function
6809 */
6810 static void
6811f_inputsecret(typval_T *argvars, typval_T *rettv)
6812{
6813 ++cmdline_star;
6814 ++inputsecret_flag;
6815 f_input(argvars, rettv);
6816 --cmdline_star;
6817 --inputsecret_flag;
6818}
6819
6820/*
6821 * "insert()" function
6822 */
6823 static void
6824f_insert(typval_T *argvars, typval_T *rettv)
6825{
6826 long before = 0;
6827 listitem_T *item;
6828 list_T *l;
6829 int error = FALSE;
6830
6831 if (argvars[0].v_type != VAR_LIST)
6832 EMSG2(_(e_listarg), "insert()");
6833 else if ((l = argvars[0].vval.v_list) != NULL
6834 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
6835 {
6836 if (argvars[2].v_type != VAR_UNKNOWN)
6837 before = (long)get_tv_number_chk(&argvars[2], &error);
6838 if (error)
6839 return; /* type error; errmsg already given */
6840
6841 if (before == l->lv_len)
6842 item = NULL;
6843 else
6844 {
6845 item = list_find(l, before);
6846 if (item == NULL)
6847 {
6848 EMSGN(_(e_listidx), before);
6849 l = NULL;
6850 }
6851 }
6852 if (l != NULL)
6853 {
6854 list_insert_tv(l, &argvars[1], item);
6855 copy_tv(&argvars[0], rettv);
6856 }
6857 }
6858}
6859
6860/*
6861 * "invert(expr)" function
6862 */
6863 static void
6864f_invert(typval_T *argvars, typval_T *rettv)
6865{
6866 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
6867}
6868
6869/*
6870 * "isdirectory()" function
6871 */
6872 static void
6873f_isdirectory(typval_T *argvars, typval_T *rettv)
6874{
6875 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
6876}
6877
6878/*
6879 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
6880 * or it refers to a List or Dictionary that is locked.
6881 */
6882 static int
6883tv_islocked(typval_T *tv)
6884{
6885 return (tv->v_lock & VAR_LOCKED)
6886 || (tv->v_type == VAR_LIST
6887 && tv->vval.v_list != NULL
6888 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
6889 || (tv->v_type == VAR_DICT
6890 && tv->vval.v_dict != NULL
6891 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
6892}
6893
6894/*
6895 * "islocked()" function
6896 */
6897 static void
6898f_islocked(typval_T *argvars, typval_T *rettv)
6899{
6900 lval_T lv;
6901 char_u *end;
6902 dictitem_T *di;
6903
6904 rettv->vval.v_number = -1;
6905 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01006906 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006907 if (end != NULL && lv.ll_name != NULL)
6908 {
6909 if (*end != NUL)
6910 EMSG(_(e_trailing));
6911 else
6912 {
6913 if (lv.ll_tv == NULL)
6914 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006915 di = find_var(lv.ll_name, NULL, TRUE);
6916 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006917 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006918 /* Consider a variable locked when:
6919 * 1. the variable itself is locked
6920 * 2. the value of the variable is locked.
6921 * 3. the List or Dict value is locked.
6922 */
6923 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
6924 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006925 }
6926 }
6927 else if (lv.ll_range)
6928 EMSG(_("E786: Range not allowed"));
6929 else if (lv.ll_newkey != NULL)
6930 EMSG2(_(e_dictkey), lv.ll_newkey);
6931 else if (lv.ll_list != NULL)
6932 /* List item. */
6933 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
6934 else
6935 /* Dictionary item. */
6936 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
6937 }
6938 }
6939
6940 clear_lval(&lv);
6941}
6942
6943#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
6944/*
6945 * "isnan()" function
6946 */
6947 static void
6948f_isnan(typval_T *argvars, typval_T *rettv)
6949{
6950 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
6951 && isnan(argvars[0].vval.v_float);
6952}
6953#endif
6954
6955/*
6956 * "items(dict)" function
6957 */
6958 static void
6959f_items(typval_T *argvars, typval_T *rettv)
6960{
6961 dict_list(argvars, rettv, 2);
6962}
6963
6964#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
6965/*
6966 * Get the job from the argument.
6967 * Returns NULL if the job is invalid.
6968 */
6969 static job_T *
6970get_job_arg(typval_T *tv)
6971{
6972 job_T *job;
6973
6974 if (tv->v_type != VAR_JOB)
6975 {
6976 EMSG2(_(e_invarg2), get_tv_string(tv));
6977 return NULL;
6978 }
6979 job = tv->vval.v_job;
6980
6981 if (job == NULL)
6982 EMSG(_("E916: not a valid job"));
6983 return job;
6984}
6985
6986/*
6987 * "job_getchannel()" function
6988 */
6989 static void
6990f_job_getchannel(typval_T *argvars, typval_T *rettv)
6991{
6992 job_T *job = get_job_arg(&argvars[0]);
6993
6994 if (job != NULL)
6995 {
6996 rettv->v_type = VAR_CHANNEL;
6997 rettv->vval.v_channel = job->jv_channel;
6998 if (job->jv_channel != NULL)
6999 ++job->jv_channel->ch_refcount;
7000 }
7001}
7002
7003/*
7004 * "job_info()" function
7005 */
7006 static void
7007f_job_info(typval_T *argvars, typval_T *rettv)
7008{
7009 job_T *job = get_job_arg(&argvars[0]);
7010
7011 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7012 job_info(job, rettv->vval.v_dict);
7013}
7014
7015/*
7016 * "job_setoptions()" function
7017 */
7018 static void
7019f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7020{
7021 job_T *job = get_job_arg(&argvars[0]);
7022 jobopt_T opt;
7023
7024 if (job == NULL)
7025 return;
7026 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007027 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007028 job_set_options(job, &opt);
7029 free_job_options(&opt);
7030}
7031
7032/*
7033 * "job_start()" function
7034 */
7035 static void
7036f_job_start(typval_T *argvars, typval_T *rettv)
7037{
7038 rettv->v_type = VAR_JOB;
7039 if (check_restricted() || check_secure())
7040 return;
Bram Moolenaar13568252018-03-16 20:46:58 +01007041 rettv->vval.v_job = job_start(argvars, NULL, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007042}
7043
7044/*
7045 * "job_status()" function
7046 */
7047 static void
7048f_job_status(typval_T *argvars, typval_T *rettv)
7049{
7050 job_T *job = get_job_arg(&argvars[0]);
7051
7052 if (job != NULL)
7053 {
7054 rettv->v_type = VAR_STRING;
7055 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7056 }
7057}
7058
7059/*
7060 * "job_stop()" function
7061 */
7062 static void
7063f_job_stop(typval_T *argvars, typval_T *rettv)
7064{
7065 job_T *job = get_job_arg(&argvars[0]);
7066
7067 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007068 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007069}
7070#endif
7071
7072/*
7073 * "join()" function
7074 */
7075 static void
7076f_join(typval_T *argvars, typval_T *rettv)
7077{
7078 garray_T ga;
7079 char_u *sep;
7080
7081 if (argvars[0].v_type != VAR_LIST)
7082 {
7083 EMSG(_(e_listreq));
7084 return;
7085 }
7086 if (argvars[0].vval.v_list == NULL)
7087 return;
7088 if (argvars[1].v_type == VAR_UNKNOWN)
7089 sep = (char_u *)" ";
7090 else
7091 sep = get_tv_string_chk(&argvars[1]);
7092
7093 rettv->v_type = VAR_STRING;
7094
7095 if (sep != NULL)
7096 {
7097 ga_init2(&ga, (int)sizeof(char), 80);
7098 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7099 ga_append(&ga, NUL);
7100 rettv->vval.v_string = (char_u *)ga.ga_data;
7101 }
7102 else
7103 rettv->vval.v_string = NULL;
7104}
7105
7106/*
7107 * "js_decode()" function
7108 */
7109 static void
7110f_js_decode(typval_T *argvars, typval_T *rettv)
7111{
7112 js_read_T reader;
7113
7114 reader.js_buf = get_tv_string(&argvars[0]);
7115 reader.js_fill = NULL;
7116 reader.js_used = 0;
7117 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
7118 EMSG(_(e_invarg));
7119}
7120
7121/*
7122 * "js_encode()" function
7123 */
7124 static void
7125f_js_encode(typval_T *argvars, typval_T *rettv)
7126{
7127 rettv->v_type = VAR_STRING;
7128 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7129}
7130
7131/*
7132 * "json_decode()" function
7133 */
7134 static void
7135f_json_decode(typval_T *argvars, typval_T *rettv)
7136{
7137 js_read_T reader;
7138
7139 reader.js_buf = get_tv_string(&argvars[0]);
7140 reader.js_fill = NULL;
7141 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007142 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007143}
7144
7145/*
7146 * "json_encode()" function
7147 */
7148 static void
7149f_json_encode(typval_T *argvars, typval_T *rettv)
7150{
7151 rettv->v_type = VAR_STRING;
7152 rettv->vval.v_string = json_encode(&argvars[0], 0);
7153}
7154
7155/*
7156 * "keys()" function
7157 */
7158 static void
7159f_keys(typval_T *argvars, typval_T *rettv)
7160{
7161 dict_list(argvars, rettv, 0);
7162}
7163
7164/*
7165 * "last_buffer_nr()" function.
7166 */
7167 static void
7168f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7169{
7170 int n = 0;
7171 buf_T *buf;
7172
Bram Moolenaar29323592016-07-24 22:04:11 +02007173 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007174 if (n < buf->b_fnum)
7175 n = buf->b_fnum;
7176
7177 rettv->vval.v_number = n;
7178}
7179
7180/*
7181 * "len()" function
7182 */
7183 static void
7184f_len(typval_T *argvars, typval_T *rettv)
7185{
7186 switch (argvars[0].v_type)
7187 {
7188 case VAR_STRING:
7189 case VAR_NUMBER:
7190 rettv->vval.v_number = (varnumber_T)STRLEN(
7191 get_tv_string(&argvars[0]));
7192 break;
7193 case VAR_LIST:
7194 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7195 break;
7196 case VAR_DICT:
7197 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7198 break;
7199 case VAR_UNKNOWN:
7200 case VAR_SPECIAL:
7201 case VAR_FLOAT:
7202 case VAR_FUNC:
7203 case VAR_PARTIAL:
7204 case VAR_JOB:
7205 case VAR_CHANNEL:
7206 EMSG(_("E701: Invalid type for len()"));
7207 break;
7208 }
7209}
7210
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007211 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007212libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007213{
7214#ifdef FEAT_LIBCALL
7215 char_u *string_in;
7216 char_u **string_result;
7217 int nr_result;
7218#endif
7219
7220 rettv->v_type = type;
7221 if (type != VAR_NUMBER)
7222 rettv->vval.v_string = NULL;
7223
7224 if (check_restricted() || check_secure())
7225 return;
7226
7227#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007228 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007229 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7230 {
7231 string_in = NULL;
7232 if (argvars[2].v_type == VAR_STRING)
7233 string_in = argvars[2].vval.v_string;
7234 if (type == VAR_NUMBER)
7235 string_result = NULL;
7236 else
7237 string_result = &rettv->vval.v_string;
7238 if (mch_libcall(argvars[0].vval.v_string,
7239 argvars[1].vval.v_string,
7240 string_in,
7241 argvars[2].vval.v_number,
7242 string_result,
7243 &nr_result) == OK
7244 && type == VAR_NUMBER)
7245 rettv->vval.v_number = nr_result;
7246 }
7247#endif
7248}
7249
7250/*
7251 * "libcall()" function
7252 */
7253 static void
7254f_libcall(typval_T *argvars, typval_T *rettv)
7255{
7256 libcall_common(argvars, rettv, VAR_STRING);
7257}
7258
7259/*
7260 * "libcallnr()" function
7261 */
7262 static void
7263f_libcallnr(typval_T *argvars, typval_T *rettv)
7264{
7265 libcall_common(argvars, rettv, VAR_NUMBER);
7266}
7267
7268/*
7269 * "line(string)" function
7270 */
7271 static void
7272f_line(typval_T *argvars, typval_T *rettv)
7273{
7274 linenr_T lnum = 0;
7275 pos_T *fp;
7276 int fnum;
7277
7278 fp = var2fpos(&argvars[0], TRUE, &fnum);
7279 if (fp != NULL)
7280 lnum = fp->lnum;
7281 rettv->vval.v_number = lnum;
7282}
7283
7284/*
7285 * "line2byte(lnum)" function
7286 */
7287 static void
7288f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7289{
7290#ifndef FEAT_BYTEOFF
7291 rettv->vval.v_number = -1;
7292#else
7293 linenr_T lnum;
7294
7295 lnum = get_tv_lnum(argvars);
7296 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7297 rettv->vval.v_number = -1;
7298 else
7299 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7300 if (rettv->vval.v_number >= 0)
7301 ++rettv->vval.v_number;
7302#endif
7303}
7304
7305/*
7306 * "lispindent(lnum)" function
7307 */
7308 static void
7309f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7310{
7311#ifdef FEAT_LISP
7312 pos_T pos;
7313 linenr_T lnum;
7314
7315 pos = curwin->w_cursor;
7316 lnum = get_tv_lnum(argvars);
7317 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7318 {
7319 curwin->w_cursor.lnum = lnum;
7320 rettv->vval.v_number = get_lisp_indent();
7321 curwin->w_cursor = pos;
7322 }
7323 else
7324#endif
7325 rettv->vval.v_number = -1;
7326}
7327
7328/*
7329 * "localtime()" function
7330 */
7331 static void
7332f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7333{
7334 rettv->vval.v_number = (varnumber_T)time(NULL);
7335}
7336
7337static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
7338
7339 static void
7340get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7341{
7342 char_u *keys;
7343 char_u *which;
7344 char_u buf[NUMBUFLEN];
7345 char_u *keys_buf = NULL;
7346 char_u *rhs;
7347 int mode;
7348 int abbr = FALSE;
7349 int get_dict = FALSE;
7350 mapblock_T *mp;
7351 int buffer_local;
7352
7353 /* return empty string for failure */
7354 rettv->v_type = VAR_STRING;
7355 rettv->vval.v_string = NULL;
7356
7357 keys = get_tv_string(&argvars[0]);
7358 if (*keys == NUL)
7359 return;
7360
7361 if (argvars[1].v_type != VAR_UNKNOWN)
7362 {
7363 which = get_tv_string_buf_chk(&argvars[1], buf);
7364 if (argvars[2].v_type != VAR_UNKNOWN)
7365 {
7366 abbr = (int)get_tv_number(&argvars[2]);
7367 if (argvars[3].v_type != VAR_UNKNOWN)
7368 get_dict = (int)get_tv_number(&argvars[3]);
7369 }
7370 }
7371 else
7372 which = (char_u *)"";
7373 if (which == NULL)
7374 return;
7375
7376 mode = get_map_mode(&which, 0);
7377
7378 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7379 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7380 vim_free(keys_buf);
7381
7382 if (!get_dict)
7383 {
7384 /* Return a string. */
7385 if (rhs != NULL)
7386 rettv->vval.v_string = str2special_save(rhs, FALSE);
7387
7388 }
7389 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7390 {
7391 /* Return a dictionary. */
7392 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7393 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7394 dict_T *dict = rettv->vval.v_dict;
7395
7396 dict_add_nr_str(dict, "lhs", 0L, lhs);
7397 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
7398 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
7399 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
7400 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
7401 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
7402 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
7403 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
7404 dict_add_nr_str(dict, "mode", 0L, mapmode);
7405
7406 vim_free(lhs);
7407 vim_free(mapmode);
7408 }
7409}
7410
7411#ifdef FEAT_FLOAT
7412/*
7413 * "log()" function
7414 */
7415 static void
7416f_log(typval_T *argvars, typval_T *rettv)
7417{
7418 float_T f = 0.0;
7419
7420 rettv->v_type = VAR_FLOAT;
7421 if (get_float_arg(argvars, &f) == OK)
7422 rettv->vval.v_float = log(f);
7423 else
7424 rettv->vval.v_float = 0.0;
7425}
7426
7427/*
7428 * "log10()" function
7429 */
7430 static void
7431f_log10(typval_T *argvars, typval_T *rettv)
7432{
7433 float_T f = 0.0;
7434
7435 rettv->v_type = VAR_FLOAT;
7436 if (get_float_arg(argvars, &f) == OK)
7437 rettv->vval.v_float = log10(f);
7438 else
7439 rettv->vval.v_float = 0.0;
7440}
7441#endif
7442
7443#ifdef FEAT_LUA
7444/*
7445 * "luaeval()" function
7446 */
7447 static void
7448f_luaeval(typval_T *argvars, typval_T *rettv)
7449{
7450 char_u *str;
7451 char_u buf[NUMBUFLEN];
7452
7453 str = get_tv_string_buf(&argvars[0], buf);
7454 do_luaeval(str, argvars + 1, rettv);
7455}
7456#endif
7457
7458/*
7459 * "map()" function
7460 */
7461 static void
7462f_map(typval_T *argvars, typval_T *rettv)
7463{
7464 filter_map(argvars, rettv, TRUE);
7465}
7466
7467/*
7468 * "maparg()" function
7469 */
7470 static void
7471f_maparg(typval_T *argvars, typval_T *rettv)
7472{
7473 get_maparg(argvars, rettv, TRUE);
7474}
7475
7476/*
7477 * "mapcheck()" function
7478 */
7479 static void
7480f_mapcheck(typval_T *argvars, typval_T *rettv)
7481{
7482 get_maparg(argvars, rettv, FALSE);
7483}
7484
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007485typedef enum
7486{
7487 MATCH_END, /* matchend() */
7488 MATCH_MATCH, /* match() */
7489 MATCH_STR, /* matchstr() */
7490 MATCH_LIST, /* matchlist() */
7491 MATCH_POS /* matchstrpos() */
7492} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007493
7494 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007495find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007496{
7497 char_u *str = NULL;
7498 long len = 0;
7499 char_u *expr = NULL;
7500 char_u *pat;
7501 regmatch_T regmatch;
7502 char_u patbuf[NUMBUFLEN];
7503 char_u strbuf[NUMBUFLEN];
7504 char_u *save_cpo;
7505 long start = 0;
7506 long nth = 1;
7507 colnr_T startcol = 0;
7508 int match = 0;
7509 list_T *l = NULL;
7510 listitem_T *li = NULL;
7511 long idx = 0;
7512 char_u *tofree = NULL;
7513
7514 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7515 save_cpo = p_cpo;
7516 p_cpo = (char_u *)"";
7517
7518 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007519 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007520 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007521 /* type MATCH_LIST: return empty list when there are no matches.
7522 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007523 if (rettv_list_alloc(rettv) == FAIL)
7524 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007525 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007526 && (list_append_string(rettv->vval.v_list,
7527 (char_u *)"", 0) == FAIL
7528 || list_append_number(rettv->vval.v_list,
7529 (varnumber_T)-1) == FAIL
7530 || list_append_number(rettv->vval.v_list,
7531 (varnumber_T)-1) == FAIL
7532 || list_append_number(rettv->vval.v_list,
7533 (varnumber_T)-1) == FAIL))
7534 {
7535 list_free(rettv->vval.v_list);
7536 rettv->vval.v_list = NULL;
7537 goto theend;
7538 }
7539 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007540 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007541 {
7542 rettv->v_type = VAR_STRING;
7543 rettv->vval.v_string = NULL;
7544 }
7545
7546 if (argvars[0].v_type == VAR_LIST)
7547 {
7548 if ((l = argvars[0].vval.v_list) == NULL)
7549 goto theend;
7550 li = l->lv_first;
7551 }
7552 else
7553 {
7554 expr = str = get_tv_string(&argvars[0]);
7555 len = (long)STRLEN(str);
7556 }
7557
7558 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
7559 if (pat == NULL)
7560 goto theend;
7561
7562 if (argvars[2].v_type != VAR_UNKNOWN)
7563 {
7564 int error = FALSE;
7565
7566 start = (long)get_tv_number_chk(&argvars[2], &error);
7567 if (error)
7568 goto theend;
7569 if (l != NULL)
7570 {
7571 li = list_find(l, start);
7572 if (li == NULL)
7573 goto theend;
7574 idx = l->lv_idx; /* use the cached index */
7575 }
7576 else
7577 {
7578 if (start < 0)
7579 start = 0;
7580 if (start > len)
7581 goto theend;
7582 /* When "count" argument is there ignore matches before "start",
7583 * otherwise skip part of the string. Differs when pattern is "^"
7584 * or "\<". */
7585 if (argvars[3].v_type != VAR_UNKNOWN)
7586 startcol = start;
7587 else
7588 {
7589 str += start;
7590 len -= start;
7591 }
7592 }
7593
7594 if (argvars[3].v_type != VAR_UNKNOWN)
7595 nth = (long)get_tv_number_chk(&argvars[3], &error);
7596 if (error)
7597 goto theend;
7598 }
7599
7600 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7601 if (regmatch.regprog != NULL)
7602 {
7603 regmatch.rm_ic = p_ic;
7604
7605 for (;;)
7606 {
7607 if (l != NULL)
7608 {
7609 if (li == NULL)
7610 {
7611 match = FALSE;
7612 break;
7613 }
7614 vim_free(tofree);
7615 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7616 if (str == NULL)
7617 break;
7618 }
7619
7620 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7621
7622 if (match && --nth <= 0)
7623 break;
7624 if (l == NULL && !match)
7625 break;
7626
7627 /* Advance to just after the match. */
7628 if (l != NULL)
7629 {
7630 li = li->li_next;
7631 ++idx;
7632 }
7633 else
7634 {
7635#ifdef FEAT_MBYTE
7636 startcol = (colnr_T)(regmatch.startp[0]
7637 + (*mb_ptr2len)(regmatch.startp[0]) - str);
7638#else
7639 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
7640#endif
7641 if (startcol > (colnr_T)len
7642 || str + startcol <= regmatch.startp[0])
7643 {
7644 match = FALSE;
7645 break;
7646 }
7647 }
7648 }
7649
7650 if (match)
7651 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007652 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007653 {
7654 listitem_T *li1 = rettv->vval.v_list->lv_first;
7655 listitem_T *li2 = li1->li_next;
7656 listitem_T *li3 = li2->li_next;
7657 listitem_T *li4 = li3->li_next;
7658
7659 vim_free(li1->li_tv.vval.v_string);
7660 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7661 (int)(regmatch.endp[0] - regmatch.startp[0]));
7662 li3->li_tv.vval.v_number =
7663 (varnumber_T)(regmatch.startp[0] - expr);
7664 li4->li_tv.vval.v_number =
7665 (varnumber_T)(regmatch.endp[0] - expr);
7666 if (l != NULL)
7667 li2->li_tv.vval.v_number = (varnumber_T)idx;
7668 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007669 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007670 {
7671 int i;
7672
7673 /* return list with matched string and submatches */
7674 for (i = 0; i < NSUBEXP; ++i)
7675 {
7676 if (regmatch.endp[i] == NULL)
7677 {
7678 if (list_append_string(rettv->vval.v_list,
7679 (char_u *)"", 0) == FAIL)
7680 break;
7681 }
7682 else if (list_append_string(rettv->vval.v_list,
7683 regmatch.startp[i],
7684 (int)(regmatch.endp[i] - regmatch.startp[i]))
7685 == FAIL)
7686 break;
7687 }
7688 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007689 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007690 {
7691 /* return matched string */
7692 if (l != NULL)
7693 copy_tv(&li->li_tv, rettv);
7694 else
7695 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
7696 (int)(regmatch.endp[0] - regmatch.startp[0]));
7697 }
7698 else if (l != NULL)
7699 rettv->vval.v_number = idx;
7700 else
7701 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007702 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007703 rettv->vval.v_number =
7704 (varnumber_T)(regmatch.startp[0] - str);
7705 else
7706 rettv->vval.v_number =
7707 (varnumber_T)(regmatch.endp[0] - str);
7708 rettv->vval.v_number += (varnumber_T)(str - expr);
7709 }
7710 }
7711 vim_regfree(regmatch.regprog);
7712 }
7713
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007714theend:
7715 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007716 /* matchstrpos() without a list: drop the second item. */
7717 listitem_remove(rettv->vval.v_list,
7718 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007719 vim_free(tofree);
7720 p_cpo = save_cpo;
7721}
7722
7723/*
7724 * "match()" function
7725 */
7726 static void
7727f_match(typval_T *argvars, typval_T *rettv)
7728{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007729 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007730}
7731
7732/*
7733 * "matchadd()" function
7734 */
7735 static void
7736f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7737{
7738#ifdef FEAT_SEARCH_EXTRA
7739 char_u buf[NUMBUFLEN];
7740 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
7741 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
7742 int prio = 10; /* default priority */
7743 int id = -1;
7744 int error = FALSE;
7745 char_u *conceal_char = NULL;
7746
7747 rettv->vval.v_number = -1;
7748
7749 if (grp == NULL || pat == NULL)
7750 return;
7751 if (argvars[2].v_type != VAR_UNKNOWN)
7752 {
7753 prio = (int)get_tv_number_chk(&argvars[2], &error);
7754 if (argvars[3].v_type != VAR_UNKNOWN)
7755 {
7756 id = (int)get_tv_number_chk(&argvars[3], &error);
7757 if (argvars[4].v_type != VAR_UNKNOWN)
7758 {
7759 if (argvars[4].v_type != VAR_DICT)
7760 {
7761 EMSG(_(e_dictreq));
7762 return;
7763 }
7764 if (dict_find(argvars[4].vval.v_dict,
7765 (char_u *)"conceal", -1) != NULL)
7766 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7767 (char_u *)"conceal", FALSE);
7768 }
7769 }
7770 }
7771 if (error == TRUE)
7772 return;
7773 if (id >= 1 && id <= 3)
7774 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007775 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007776 return;
7777 }
7778
7779 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
7780 conceal_char);
7781#endif
7782}
7783
7784/*
7785 * "matchaddpos()" function
7786 */
7787 static void
7788f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7789{
7790#ifdef FEAT_SEARCH_EXTRA
7791 char_u buf[NUMBUFLEN];
7792 char_u *group;
7793 int prio = 10;
7794 int id = -1;
7795 int error = FALSE;
7796 list_T *l;
7797 char_u *conceal_char = NULL;
7798
7799 rettv->vval.v_number = -1;
7800
7801 group = get_tv_string_buf_chk(&argvars[0], buf);
7802 if (group == NULL)
7803 return;
7804
7805 if (argvars[1].v_type != VAR_LIST)
7806 {
7807 EMSG2(_(e_listarg), "matchaddpos()");
7808 return;
7809 }
7810 l = argvars[1].vval.v_list;
7811 if (l == NULL)
7812 return;
7813
7814 if (argvars[2].v_type != VAR_UNKNOWN)
7815 {
7816 prio = (int)get_tv_number_chk(&argvars[2], &error);
7817 if (argvars[3].v_type != VAR_UNKNOWN)
7818 {
7819 id = (int)get_tv_number_chk(&argvars[3], &error);
7820 if (argvars[4].v_type != VAR_UNKNOWN)
7821 {
7822 if (argvars[4].v_type != VAR_DICT)
7823 {
7824 EMSG(_(e_dictreq));
7825 return;
7826 }
7827 if (dict_find(argvars[4].vval.v_dict,
7828 (char_u *)"conceal", -1) != NULL)
7829 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7830 (char_u *)"conceal", FALSE);
7831 }
7832 }
7833 }
7834 if (error == TRUE)
7835 return;
7836
7837 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
7838 if (id == 1 || id == 2)
7839 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007840 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007841 return;
7842 }
7843
7844 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
7845 conceal_char);
7846#endif
7847}
7848
7849/*
7850 * "matcharg()" function
7851 */
7852 static void
7853f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
7854{
7855 if (rettv_list_alloc(rettv) == OK)
7856 {
7857#ifdef FEAT_SEARCH_EXTRA
7858 int id = (int)get_tv_number(&argvars[0]);
7859 matchitem_T *m;
7860
7861 if (id >= 1 && id <= 3)
7862 {
7863 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
7864 {
7865 list_append_string(rettv->vval.v_list,
7866 syn_id2name(m->hlg_id), -1);
7867 list_append_string(rettv->vval.v_list, m->pattern, -1);
7868 }
7869 else
7870 {
7871 list_append_string(rettv->vval.v_list, NULL, -1);
7872 list_append_string(rettv->vval.v_list, NULL, -1);
7873 }
7874 }
7875#endif
7876 }
7877}
7878
7879/*
7880 * "matchdelete()" function
7881 */
7882 static void
7883f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7884{
7885#ifdef FEAT_SEARCH_EXTRA
7886 rettv->vval.v_number = match_delete(curwin,
7887 (int)get_tv_number(&argvars[0]), TRUE);
7888#endif
7889}
7890
7891/*
7892 * "matchend()" function
7893 */
7894 static void
7895f_matchend(typval_T *argvars, typval_T *rettv)
7896{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007897 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007898}
7899
7900/*
7901 * "matchlist()" function
7902 */
7903 static void
7904f_matchlist(typval_T *argvars, typval_T *rettv)
7905{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007906 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007907}
7908
7909/*
7910 * "matchstr()" function
7911 */
7912 static void
7913f_matchstr(typval_T *argvars, typval_T *rettv)
7914{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007915 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007916}
7917
7918/*
7919 * "matchstrpos()" function
7920 */
7921 static void
7922f_matchstrpos(typval_T *argvars, typval_T *rettv)
7923{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007924 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007925}
7926
7927static void max_min(typval_T *argvars, typval_T *rettv, int domax);
7928
7929 static void
7930max_min(typval_T *argvars, typval_T *rettv, int domax)
7931{
7932 varnumber_T n = 0;
7933 varnumber_T i;
7934 int error = FALSE;
7935
7936 if (argvars[0].v_type == VAR_LIST)
7937 {
7938 list_T *l;
7939 listitem_T *li;
7940
7941 l = argvars[0].vval.v_list;
7942 if (l != NULL)
7943 {
7944 li = l->lv_first;
7945 if (li != NULL)
7946 {
7947 n = get_tv_number_chk(&li->li_tv, &error);
7948 for (;;)
7949 {
7950 li = li->li_next;
7951 if (li == NULL)
7952 break;
7953 i = get_tv_number_chk(&li->li_tv, &error);
7954 if (domax ? i > n : i < n)
7955 n = i;
7956 }
7957 }
7958 }
7959 }
7960 else if (argvars[0].v_type == VAR_DICT)
7961 {
7962 dict_T *d;
7963 int first = TRUE;
7964 hashitem_T *hi;
7965 int todo;
7966
7967 d = argvars[0].vval.v_dict;
7968 if (d != NULL)
7969 {
7970 todo = (int)d->dv_hashtab.ht_used;
7971 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
7972 {
7973 if (!HASHITEM_EMPTY(hi))
7974 {
7975 --todo;
7976 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
7977 if (first)
7978 {
7979 n = i;
7980 first = FALSE;
7981 }
7982 else if (domax ? i > n : i < n)
7983 n = i;
7984 }
7985 }
7986 }
7987 }
7988 else
Bram Moolenaar26b84332016-09-04 21:42:36 +02007989 EMSG2(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007990 rettv->vval.v_number = error ? 0 : n;
7991}
7992
7993/*
7994 * "max()" function
7995 */
7996 static void
7997f_max(typval_T *argvars, typval_T *rettv)
7998{
7999 max_min(argvars, rettv, TRUE);
8000}
8001
8002/*
8003 * "min()" function
8004 */
8005 static void
8006f_min(typval_T *argvars, typval_T *rettv)
8007{
8008 max_min(argvars, rettv, FALSE);
8009}
8010
8011static int mkdir_recurse(char_u *dir, int prot);
8012
8013/*
8014 * Create the directory in which "dir" is located, and higher levels when
8015 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008016 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008017 */
8018 static int
8019mkdir_recurse(char_u *dir, int prot)
8020{
8021 char_u *p;
8022 char_u *updir;
8023 int r = FAIL;
8024
8025 /* Get end of directory name in "dir".
8026 * We're done when it's "/" or "c:/". */
8027 p = gettail_sep(dir);
8028 if (p <= get_past_head(dir))
8029 return OK;
8030
8031 /* If the directory exists we're done. Otherwise: create it.*/
8032 updir = vim_strnsave(dir, (int)(p - dir));
8033 if (updir == NULL)
8034 return FAIL;
8035 if (mch_isdir(updir))
8036 r = OK;
8037 else if (mkdir_recurse(updir, prot) == OK)
8038 r = vim_mkdir_emsg(updir, prot);
8039 vim_free(updir);
8040 return r;
8041}
8042
8043#ifdef vim_mkdir
8044/*
8045 * "mkdir()" function
8046 */
8047 static void
8048f_mkdir(typval_T *argvars, typval_T *rettv)
8049{
8050 char_u *dir;
8051 char_u buf[NUMBUFLEN];
8052 int prot = 0755;
8053
8054 rettv->vval.v_number = FAIL;
8055 if (check_restricted() || check_secure())
8056 return;
8057
8058 dir = get_tv_string_buf(&argvars[0], buf);
8059 if (*dir == NUL)
8060 rettv->vval.v_number = FAIL;
8061 else
8062 {
8063 if (*gettail(dir) == NUL)
8064 /* remove trailing slashes */
8065 *gettail_sep(dir) = NUL;
8066
8067 if (argvars[1].v_type != VAR_UNKNOWN)
8068 {
8069 if (argvars[2].v_type != VAR_UNKNOWN)
8070 prot = (int)get_tv_number_chk(&argvars[2], NULL);
8071 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
8072 mkdir_recurse(dir, prot);
8073 }
8074 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
8075 }
8076}
8077#endif
8078
8079/*
8080 * "mode()" function
8081 */
8082 static void
8083f_mode(typval_T *argvars, typval_T *rettv)
8084{
8085 char_u buf[3];
8086
8087 buf[1] = NUL;
8088 buf[2] = NUL;
8089
8090 if (time_for_testing == 93784)
8091 {
8092 /* Testing the two-character code. */
8093 buf[0] = 'x';
8094 buf[1] = '!';
8095 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008096#ifdef FEAT_TERMINAL
8097 else if (term_use_loop())
8098 buf[0] = 't';
8099#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008100 else if (VIsual_active)
8101 {
8102 if (VIsual_select)
8103 buf[0] = VIsual_mode + 's' - 'v';
8104 else
8105 buf[0] = VIsual_mode;
8106 }
8107 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8108 || State == CONFIRM)
8109 {
8110 buf[0] = 'r';
8111 if (State == ASKMORE)
8112 buf[1] = 'm';
8113 else if (State == CONFIRM)
8114 buf[1] = '?';
8115 }
8116 else if (State == EXTERNCMD)
8117 buf[0] = '!';
8118 else if (State & INSERT)
8119 {
8120#ifdef FEAT_VREPLACE
8121 if (State & VREPLACE_FLAG)
8122 {
8123 buf[0] = 'R';
8124 buf[1] = 'v';
8125 }
8126 else
8127#endif
Bram Moolenaare90858d2017-02-01 17:24:34 +01008128 {
8129 if (State & REPLACE_FLAG)
8130 buf[0] = 'R';
8131 else
8132 buf[0] = 'i';
8133#ifdef FEAT_INS_EXPAND
8134 if (ins_compl_active())
8135 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008136 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008137 buf[1] = 'x';
8138#endif
8139 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008140 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008141 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008142 {
8143 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008144 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008145 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008146 else if (exmode_active == EXMODE_NORMAL)
8147 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008148 }
8149 else
8150 {
8151 buf[0] = 'n';
8152 if (finish_op)
8153 buf[1] = 'o';
8154 }
8155
8156 /* Clear out the minor mode when the argument is not a non-zero number or
8157 * non-empty string. */
8158 if (!non_zero_arg(&argvars[0]))
8159 buf[1] = NUL;
8160
8161 rettv->vval.v_string = vim_strsave(buf);
8162 rettv->v_type = VAR_STRING;
8163}
8164
8165#if defined(FEAT_MZSCHEME) || defined(PROTO)
8166/*
8167 * "mzeval()" function
8168 */
8169 static void
8170f_mzeval(typval_T *argvars, typval_T *rettv)
8171{
8172 char_u *str;
8173 char_u buf[NUMBUFLEN];
8174
8175 str = get_tv_string_buf(&argvars[0], buf);
8176 do_mzeval(str, rettv);
8177}
8178
8179 void
8180mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8181{
8182 typval_T argvars[3];
8183
8184 argvars[0].v_type = VAR_STRING;
8185 argvars[0].vval.v_string = name;
8186 copy_tv(args, &argvars[1]);
8187 argvars[2].v_type = VAR_UNKNOWN;
8188 f_call(argvars, rettv);
8189 clear_tv(&argvars[1]);
8190}
8191#endif
8192
8193/*
8194 * "nextnonblank()" function
8195 */
8196 static void
8197f_nextnonblank(typval_T *argvars, typval_T *rettv)
8198{
8199 linenr_T lnum;
8200
8201 for (lnum = get_tv_lnum(argvars); ; ++lnum)
8202 {
8203 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8204 {
8205 lnum = 0;
8206 break;
8207 }
8208 if (*skipwhite(ml_get(lnum)) != NUL)
8209 break;
8210 }
8211 rettv->vval.v_number = lnum;
8212}
8213
8214/*
8215 * "nr2char()" function
8216 */
8217 static void
8218f_nr2char(typval_T *argvars, typval_T *rettv)
8219{
8220 char_u buf[NUMBUFLEN];
8221
8222#ifdef FEAT_MBYTE
8223 if (has_mbyte)
8224 {
8225 int utf8 = 0;
8226
8227 if (argvars[1].v_type != VAR_UNKNOWN)
8228 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
8229 if (utf8)
8230 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8231 else
8232 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8233 }
8234 else
8235#endif
8236 {
8237 buf[0] = (char_u)get_tv_number(&argvars[0]);
8238 buf[1] = NUL;
8239 }
8240 rettv->v_type = VAR_STRING;
8241 rettv->vval.v_string = vim_strsave(buf);
8242}
8243
8244/*
8245 * "or(expr, expr)" function
8246 */
8247 static void
8248f_or(typval_T *argvars, typval_T *rettv)
8249{
8250 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8251 | get_tv_number_chk(&argvars[1], NULL);
8252}
8253
8254/*
8255 * "pathshorten()" function
8256 */
8257 static void
8258f_pathshorten(typval_T *argvars, typval_T *rettv)
8259{
8260 char_u *p;
8261
8262 rettv->v_type = VAR_STRING;
8263 p = get_tv_string_chk(&argvars[0]);
8264 if (p == NULL)
8265 rettv->vval.v_string = NULL;
8266 else
8267 {
8268 p = vim_strsave(p);
8269 rettv->vval.v_string = p;
8270 if (p != NULL)
8271 shorten_dir(p);
8272 }
8273}
8274
8275#ifdef FEAT_PERL
8276/*
8277 * "perleval()" function
8278 */
8279 static void
8280f_perleval(typval_T *argvars, typval_T *rettv)
8281{
8282 char_u *str;
8283 char_u buf[NUMBUFLEN];
8284
8285 str = get_tv_string_buf(&argvars[0], buf);
8286 do_perleval(str, rettv);
8287}
8288#endif
8289
8290#ifdef FEAT_FLOAT
8291/*
8292 * "pow()" function
8293 */
8294 static void
8295f_pow(typval_T *argvars, typval_T *rettv)
8296{
8297 float_T fx = 0.0, fy = 0.0;
8298
8299 rettv->v_type = VAR_FLOAT;
8300 if (get_float_arg(argvars, &fx) == OK
8301 && get_float_arg(&argvars[1], &fy) == OK)
8302 rettv->vval.v_float = pow(fx, fy);
8303 else
8304 rettv->vval.v_float = 0.0;
8305}
8306#endif
8307
8308/*
8309 * "prevnonblank()" function
8310 */
8311 static void
8312f_prevnonblank(typval_T *argvars, typval_T *rettv)
8313{
8314 linenr_T lnum;
8315
8316 lnum = get_tv_lnum(argvars);
8317 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8318 lnum = 0;
8319 else
8320 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8321 --lnum;
8322 rettv->vval.v_number = lnum;
8323}
8324
8325/* This dummy va_list is here because:
8326 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8327 * - locally in the function results in a "used before set" warning
8328 * - using va_start() to initialize it gives "function with fixed args" error */
8329static va_list ap;
8330
8331/*
8332 * "printf()" function
8333 */
8334 static void
8335f_printf(typval_T *argvars, typval_T *rettv)
8336{
8337 char_u buf[NUMBUFLEN];
8338 int len;
8339 char_u *s;
8340 int saved_did_emsg = did_emsg;
8341 char *fmt;
8342
8343 rettv->v_type = VAR_STRING;
8344 rettv->vval.v_string = NULL;
8345
8346 /* Get the required length, allocate the buffer and do it for real. */
8347 did_emsg = FALSE;
8348 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008349 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008350 if (!did_emsg)
8351 {
8352 s = alloc(len + 1);
8353 if (s != NULL)
8354 {
8355 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008356 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8357 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008358 }
8359 }
8360 did_emsg |= saved_did_emsg;
8361}
8362
8363/*
8364 * "pumvisible()" function
8365 */
8366 static void
8367f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8368{
8369#ifdef FEAT_INS_EXPAND
8370 if (pum_visible())
8371 rettv->vval.v_number = 1;
8372#endif
8373}
8374
8375#ifdef FEAT_PYTHON3
8376/*
8377 * "py3eval()" function
8378 */
8379 static void
8380f_py3eval(typval_T *argvars, typval_T *rettv)
8381{
8382 char_u *str;
8383 char_u buf[NUMBUFLEN];
8384
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008385 if (p_pyx == 0)
8386 p_pyx = 3;
8387
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008388 str = get_tv_string_buf(&argvars[0], buf);
8389 do_py3eval(str, rettv);
8390}
8391#endif
8392
8393#ifdef FEAT_PYTHON
8394/*
8395 * "pyeval()" function
8396 */
8397 static void
8398f_pyeval(typval_T *argvars, typval_T *rettv)
8399{
8400 char_u *str;
8401 char_u buf[NUMBUFLEN];
8402
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008403 if (p_pyx == 0)
8404 p_pyx = 2;
8405
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008406 str = get_tv_string_buf(&argvars[0], buf);
8407 do_pyeval(str, rettv);
8408}
8409#endif
8410
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008411#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8412/*
8413 * "pyxeval()" function
8414 */
8415 static void
8416f_pyxeval(typval_T *argvars, typval_T *rettv)
8417{
8418# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8419 init_pyxversion();
8420 if (p_pyx == 2)
8421 f_pyeval(argvars, rettv);
8422 else
8423 f_py3eval(argvars, rettv);
8424# elif defined(FEAT_PYTHON)
8425 f_pyeval(argvars, rettv);
8426# elif defined(FEAT_PYTHON3)
8427 f_py3eval(argvars, rettv);
8428# endif
8429}
8430#endif
8431
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008432/*
8433 * "range()" function
8434 */
8435 static void
8436f_range(typval_T *argvars, typval_T *rettv)
8437{
8438 varnumber_T start;
8439 varnumber_T end;
8440 varnumber_T stride = 1;
8441 varnumber_T i;
8442 int error = FALSE;
8443
8444 start = get_tv_number_chk(&argvars[0], &error);
8445 if (argvars[1].v_type == VAR_UNKNOWN)
8446 {
8447 end = start - 1;
8448 start = 0;
8449 }
8450 else
8451 {
8452 end = get_tv_number_chk(&argvars[1], &error);
8453 if (argvars[2].v_type != VAR_UNKNOWN)
8454 stride = get_tv_number_chk(&argvars[2], &error);
8455 }
8456
8457 if (error)
8458 return; /* type error; errmsg already given */
8459 if (stride == 0)
8460 EMSG(_("E726: Stride is zero"));
8461 else if (stride > 0 ? end + 1 < start : end - 1 > start)
8462 EMSG(_("E727: Start past end"));
8463 else
8464 {
8465 if (rettv_list_alloc(rettv) == OK)
8466 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8467 if (list_append_number(rettv->vval.v_list,
8468 (varnumber_T)i) == FAIL)
8469 break;
8470 }
8471}
8472
8473/*
8474 * "readfile()" function
8475 */
8476 static void
8477f_readfile(typval_T *argvars, typval_T *rettv)
8478{
8479 int binary = FALSE;
8480 int failed = FALSE;
8481 char_u *fname;
8482 FILE *fd;
8483 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8484 int io_size = sizeof(buf);
8485 int readlen; /* size of last fread() */
8486 char_u *prev = NULL; /* previously read bytes, if any */
8487 long prevlen = 0; /* length of data in prev */
8488 long prevsize = 0; /* size of prev buffer */
8489 long maxline = MAXLNUM;
8490 long cnt = 0;
8491 char_u *p; /* position in buf */
8492 char_u *start; /* start of current line */
8493
8494 if (argvars[1].v_type != VAR_UNKNOWN)
8495 {
8496 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
8497 binary = TRUE;
8498 if (argvars[2].v_type != VAR_UNKNOWN)
8499 maxline = (long)get_tv_number(&argvars[2]);
8500 }
8501
8502 if (rettv_list_alloc(rettv) == FAIL)
8503 return;
8504
8505 /* Always open the file in binary mode, library functions have a mind of
8506 * their own about CR-LF conversion. */
8507 fname = get_tv_string(&argvars[0]);
8508 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8509 {
8510 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
8511 return;
8512 }
8513
8514 while (cnt < maxline || maxline < 0)
8515 {
8516 readlen = (int)fread(buf, 1, io_size, fd);
8517
8518 /* This for loop processes what was read, but is also entered at end
8519 * of file so that either:
8520 * - an incomplete line gets written
8521 * - a "binary" file gets an empty line at the end if it ends in a
8522 * newline. */
8523 for (p = buf, start = buf;
8524 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8525 ++p)
8526 {
8527 if (*p == '\n' || readlen <= 0)
8528 {
8529 listitem_T *li;
8530 char_u *s = NULL;
8531 long_u len = p - start;
8532
8533 /* Finished a line. Remove CRs before NL. */
8534 if (readlen > 0 && !binary)
8535 {
8536 while (len > 0 && start[len - 1] == '\r')
8537 --len;
8538 /* removal may cross back to the "prev" string */
8539 if (len == 0)
8540 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8541 --prevlen;
8542 }
8543 if (prevlen == 0)
8544 s = vim_strnsave(start, (int)len);
8545 else
8546 {
8547 /* Change "prev" buffer to be the right size. This way
8548 * the bytes are only copied once, and very long lines are
8549 * allocated only once. */
8550 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8551 {
8552 mch_memmove(s + prevlen, start, len);
8553 s[prevlen + len] = NUL;
8554 prev = NULL; /* the list will own the string */
8555 prevlen = prevsize = 0;
8556 }
8557 }
8558 if (s == NULL)
8559 {
8560 do_outofmem_msg((long_u) prevlen + len + 1);
8561 failed = TRUE;
8562 break;
8563 }
8564
8565 if ((li = listitem_alloc()) == NULL)
8566 {
8567 vim_free(s);
8568 failed = TRUE;
8569 break;
8570 }
8571 li->li_tv.v_type = VAR_STRING;
8572 li->li_tv.v_lock = 0;
8573 li->li_tv.vval.v_string = s;
8574 list_append(rettv->vval.v_list, li);
8575
8576 start = p + 1; /* step over newline */
8577 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
8578 break;
8579 }
8580 else if (*p == NUL)
8581 *p = '\n';
8582#ifdef FEAT_MBYTE
8583 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
8584 * when finding the BF and check the previous two bytes. */
8585 else if (*p == 0xbf && enc_utf8 && !binary)
8586 {
8587 /* Find the two bytes before the 0xbf. If p is at buf, or buf
8588 * + 1, these may be in the "prev" string. */
8589 char_u back1 = p >= buf + 1 ? p[-1]
8590 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
8591 char_u back2 = p >= buf + 2 ? p[-2]
8592 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
8593 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
8594
8595 if (back2 == 0xef && back1 == 0xbb)
8596 {
8597 char_u *dest = p - 2;
8598
8599 /* Usually a BOM is at the beginning of a file, and so at
8600 * the beginning of a line; then we can just step over it.
8601 */
8602 if (start == dest)
8603 start = p + 1;
8604 else
8605 {
8606 /* have to shuffle buf to close gap */
8607 int adjust_prevlen = 0;
8608
8609 if (dest < buf)
8610 {
8611 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
8612 dest = buf;
8613 }
8614 if (readlen > p - buf + 1)
8615 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
8616 readlen -= 3 - adjust_prevlen;
8617 prevlen -= adjust_prevlen;
8618 p = dest - 1;
8619 }
8620 }
8621 }
8622#endif
8623 } /* for */
8624
8625 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
8626 break;
8627 if (start < p)
8628 {
8629 /* There's part of a line in buf, store it in "prev". */
8630 if (p - start + prevlen >= prevsize)
8631 {
8632 /* need bigger "prev" buffer */
8633 char_u *newprev;
8634
8635 /* A common use case is ordinary text files and "prev" gets a
8636 * fragment of a line, so the first allocation is made
8637 * small, to avoid repeatedly 'allocing' large and
8638 * 'reallocing' small. */
8639 if (prevsize == 0)
8640 prevsize = (long)(p - start);
8641 else
8642 {
8643 long grow50pc = (prevsize * 3) / 2;
8644 long growmin = (long)((p - start) * 2 + prevlen);
8645 prevsize = grow50pc > growmin ? grow50pc : growmin;
8646 }
8647 newprev = prev == NULL ? alloc(prevsize)
8648 : vim_realloc(prev, prevsize);
8649 if (newprev == NULL)
8650 {
8651 do_outofmem_msg((long_u)prevsize);
8652 failed = TRUE;
8653 break;
8654 }
8655 prev = newprev;
8656 }
8657 /* Add the line part to end of "prev". */
8658 mch_memmove(prev + prevlen, start, p - start);
8659 prevlen += (long)(p - start);
8660 }
8661 } /* while */
8662
8663 /*
8664 * For a negative line count use only the lines at the end of the file,
8665 * free the rest.
8666 */
8667 if (!failed && maxline < 0)
8668 while (cnt > -maxline)
8669 {
8670 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
8671 --cnt;
8672 }
8673
8674 if (failed)
8675 {
8676 list_free(rettv->vval.v_list);
8677 /* readfile doc says an empty list is returned on error */
8678 rettv->vval.v_list = list_alloc();
8679 }
8680
8681 vim_free(prev);
8682 fclose(fd);
8683}
8684
8685#if defined(FEAT_RELTIME)
8686static int list2proftime(typval_T *arg, proftime_T *tm);
8687
8688/*
8689 * Convert a List to proftime_T.
8690 * Return FAIL when there is something wrong.
8691 */
8692 static int
8693list2proftime(typval_T *arg, proftime_T *tm)
8694{
8695 long n1, n2;
8696 int error = FALSE;
8697
8698 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
8699 || arg->vval.v_list->lv_len != 2)
8700 return FAIL;
8701 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
8702 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
8703# ifdef WIN3264
8704 tm->HighPart = n1;
8705 tm->LowPart = n2;
8706# else
8707 tm->tv_sec = n1;
8708 tm->tv_usec = n2;
8709# endif
8710 return error ? FAIL : OK;
8711}
8712#endif /* FEAT_RELTIME */
8713
8714/*
8715 * "reltime()" function
8716 */
8717 static void
8718f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8719{
8720#ifdef FEAT_RELTIME
8721 proftime_T res;
8722 proftime_T start;
8723
8724 if (argvars[0].v_type == VAR_UNKNOWN)
8725 {
8726 /* No arguments: get current time. */
8727 profile_start(&res);
8728 }
8729 else if (argvars[1].v_type == VAR_UNKNOWN)
8730 {
8731 if (list2proftime(&argvars[0], &res) == FAIL)
8732 return;
8733 profile_end(&res);
8734 }
8735 else
8736 {
8737 /* Two arguments: compute the difference. */
8738 if (list2proftime(&argvars[0], &start) == FAIL
8739 || list2proftime(&argvars[1], &res) == FAIL)
8740 return;
8741 profile_sub(&res, &start);
8742 }
8743
8744 if (rettv_list_alloc(rettv) == OK)
8745 {
8746 long n1, n2;
8747
8748# ifdef WIN3264
8749 n1 = res.HighPart;
8750 n2 = res.LowPart;
8751# else
8752 n1 = res.tv_sec;
8753 n2 = res.tv_usec;
8754# endif
8755 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
8756 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
8757 }
8758#endif
8759}
8760
8761#ifdef FEAT_FLOAT
8762/*
8763 * "reltimefloat()" function
8764 */
8765 static void
8766f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
8767{
8768# ifdef FEAT_RELTIME
8769 proftime_T tm;
8770# endif
8771
8772 rettv->v_type = VAR_FLOAT;
8773 rettv->vval.v_float = 0;
8774# ifdef FEAT_RELTIME
8775 if (list2proftime(&argvars[0], &tm) == OK)
8776 rettv->vval.v_float = profile_float(&tm);
8777# endif
8778}
8779#endif
8780
8781/*
8782 * "reltimestr()" function
8783 */
8784 static void
8785f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
8786{
8787#ifdef FEAT_RELTIME
8788 proftime_T tm;
8789#endif
8790
8791 rettv->v_type = VAR_STRING;
8792 rettv->vval.v_string = NULL;
8793#ifdef FEAT_RELTIME
8794 if (list2proftime(&argvars[0], &tm) == OK)
8795 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
8796#endif
8797}
8798
8799#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
8800static void make_connection(void);
8801static int check_connection(void);
8802
8803 static void
8804make_connection(void)
8805{
8806 if (X_DISPLAY == NULL
8807# ifdef FEAT_GUI
8808 && !gui.in_use
8809# endif
8810 )
8811 {
8812 x_force_connect = TRUE;
8813 setup_term_clip();
8814 x_force_connect = FALSE;
8815 }
8816}
8817
8818 static int
8819check_connection(void)
8820{
8821 make_connection();
8822 if (X_DISPLAY == NULL)
8823 {
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01008824 EMSG(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008825 return FAIL;
8826 }
8827 return OK;
8828}
8829#endif
8830
8831#ifdef FEAT_CLIENTSERVER
8832 static void
8833remote_common(typval_T *argvars, typval_T *rettv, int expr)
8834{
8835 char_u *server_name;
8836 char_u *keys;
8837 char_u *r = NULL;
8838 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008839 int timeout = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008840# ifdef WIN32
8841 HWND w;
8842# else
8843 Window w;
8844# endif
8845
8846 if (check_restricted() || check_secure())
8847 return;
8848
8849# ifdef FEAT_X11
8850 if (check_connection() == FAIL)
8851 return;
8852# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008853 if (argvars[2].v_type != VAR_UNKNOWN
8854 && argvars[3].v_type != VAR_UNKNOWN)
8855 timeout = get_tv_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008856
8857 server_name = get_tv_string_chk(&argvars[0]);
8858 if (server_name == NULL)
8859 return; /* type error; errmsg already given */
8860 keys = get_tv_string_buf(&argvars[1], buf);
8861# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008862 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008863# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008864 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
8865 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008866# endif
8867 {
8868 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008869 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008870 EMSG(r); /* sending worked but evaluation failed */
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008871 vim_free(r);
8872 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008873 else
8874 EMSG2(_("E241: Unable to send to %s"), server_name);
8875 return;
8876 }
8877
8878 rettv->vval.v_string = r;
8879
8880 if (argvars[2].v_type != VAR_UNKNOWN)
8881 {
8882 dictitem_T v;
8883 char_u str[30];
8884 char_u *idvar;
8885
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008886 idvar = get_tv_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008887 if (idvar != NULL && *idvar != NUL)
8888 {
8889 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
8890 v.di_tv.v_type = VAR_STRING;
8891 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008892 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008893 vim_free(v.di_tv.vval.v_string);
8894 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008895 }
8896}
8897#endif
8898
8899/*
8900 * "remote_expr()" function
8901 */
8902 static void
8903f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
8904{
8905 rettv->v_type = VAR_STRING;
8906 rettv->vval.v_string = NULL;
8907#ifdef FEAT_CLIENTSERVER
8908 remote_common(argvars, rettv, TRUE);
8909#endif
8910}
8911
8912/*
8913 * "remote_foreground()" function
8914 */
8915 static void
8916f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8917{
8918#ifdef FEAT_CLIENTSERVER
8919# ifdef WIN32
8920 /* On Win32 it's done in this application. */
8921 {
8922 char_u *server_name = get_tv_string_chk(&argvars[0]);
8923
8924 if (server_name != NULL)
8925 serverForeground(server_name);
8926 }
8927# else
8928 /* Send a foreground() expression to the server. */
8929 argvars[1].v_type = VAR_STRING;
8930 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
8931 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008932 rettv->v_type = VAR_STRING;
8933 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008934 remote_common(argvars, rettv, TRUE);
8935 vim_free(argvars[1].vval.v_string);
8936# endif
8937#endif
8938}
8939
8940 static void
8941f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
8942{
8943#ifdef FEAT_CLIENTSERVER
8944 dictitem_T v;
8945 char_u *s = NULL;
8946# ifdef WIN32
8947 long_u n = 0;
8948# endif
8949 char_u *serverid;
8950
8951 if (check_restricted() || check_secure())
8952 {
8953 rettv->vval.v_number = -1;
8954 return;
8955 }
8956 serverid = get_tv_string_chk(&argvars[0]);
8957 if (serverid == NULL)
8958 {
8959 rettv->vval.v_number = -1;
8960 return; /* type error; errmsg already given */
8961 }
8962# ifdef WIN32
8963 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
8964 if (n == 0)
8965 rettv->vval.v_number = -1;
8966 else
8967 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008968 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008969 rettv->vval.v_number = (s != NULL);
8970 }
8971# else
8972 if (check_connection() == FAIL)
8973 return;
8974
8975 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
8976 serverStrToWin(serverid), &s);
8977# endif
8978
8979 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
8980 {
8981 char_u *retvar;
8982
8983 v.di_tv.v_type = VAR_STRING;
8984 v.di_tv.vval.v_string = vim_strsave(s);
8985 retvar = get_tv_string_chk(&argvars[1]);
8986 if (retvar != NULL)
8987 set_var(retvar, &v.di_tv, FALSE);
8988 vim_free(v.di_tv.vval.v_string);
8989 }
8990#else
8991 rettv->vval.v_number = -1;
8992#endif
8993}
8994
8995 static void
8996f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
8997{
8998 char_u *r = NULL;
8999
9000#ifdef FEAT_CLIENTSERVER
9001 char_u *serverid = get_tv_string_chk(&argvars[0]);
9002
9003 if (serverid != NULL && !check_restricted() && !check_secure())
9004 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009005 int timeout = 0;
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009006# ifdef WIN32
9007 /* The server's HWND is encoded in the 'id' parameter */
9008 long_u n = 0;
9009# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009010
9011 if (argvars[1].v_type != VAR_UNKNOWN)
9012 timeout = get_tv_number(&argvars[1]);
9013
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009014# ifdef WIN32
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009015 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9016 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009017 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009018 if (r == NULL)
9019# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009020 if (check_connection() == FAIL
9021 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9022 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009023# endif
9024 EMSG(_("E277: Unable to read a server reply"));
9025 }
9026#endif
9027 rettv->v_type = VAR_STRING;
9028 rettv->vval.v_string = r;
9029}
9030
9031/*
9032 * "remote_send()" function
9033 */
9034 static void
9035f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9036{
9037 rettv->v_type = VAR_STRING;
9038 rettv->vval.v_string = NULL;
9039#ifdef FEAT_CLIENTSERVER
9040 remote_common(argvars, rettv, FALSE);
9041#endif
9042}
9043
9044/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009045 * "remote_startserver()" function
9046 */
9047 static void
9048f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9049{
9050#ifdef FEAT_CLIENTSERVER
9051 char_u *server = get_tv_string_chk(&argvars[0]);
9052
9053 if (server == NULL)
9054 return; /* type error; errmsg already given */
9055 if (serverName != NULL)
9056 EMSG(_("E941: already started a server"));
9057 else
9058 {
9059# ifdef FEAT_X11
9060 if (check_connection() == OK)
9061 serverRegisterName(X_DISPLAY, server);
9062# else
9063 serverSetName(server);
9064# endif
9065 }
9066#else
9067 EMSG(_("E942: +clientserver feature not available"));
9068#endif
9069}
9070
9071/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009072 * "remove()" function
9073 */
9074 static void
9075f_remove(typval_T *argvars, typval_T *rettv)
9076{
9077 list_T *l;
9078 listitem_T *item, *item2;
9079 listitem_T *li;
9080 long idx;
9081 long end;
9082 char_u *key;
9083 dict_T *d;
9084 dictitem_T *di;
9085 char_u *arg_errmsg = (char_u *)N_("remove() argument");
9086
9087 if (argvars[0].v_type == VAR_DICT)
9088 {
9089 if (argvars[2].v_type != VAR_UNKNOWN)
9090 EMSG2(_(e_toomanyarg), "remove()");
9091 else if ((d = argvars[0].vval.v_dict) != NULL
9092 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
9093 {
9094 key = get_tv_string_chk(&argvars[1]);
9095 if (key != NULL)
9096 {
9097 di = dict_find(d, key, -1);
9098 if (di == NULL)
9099 EMSG2(_(e_dictkey), key);
9100 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
9101 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
9102 {
9103 *rettv = di->di_tv;
9104 init_tv(&di->di_tv);
9105 dictitem_remove(d, di);
9106 }
9107 }
9108 }
9109 }
9110 else if (argvars[0].v_type != VAR_LIST)
9111 EMSG2(_(e_listdictarg), "remove()");
9112 else if ((l = argvars[0].vval.v_list) != NULL
9113 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
9114 {
9115 int error = FALSE;
9116
9117 idx = (long)get_tv_number_chk(&argvars[1], &error);
9118 if (error)
9119 ; /* type error: do nothing, errmsg already given */
9120 else if ((item = list_find(l, idx)) == NULL)
9121 EMSGN(_(e_listidx), idx);
9122 else
9123 {
9124 if (argvars[2].v_type == VAR_UNKNOWN)
9125 {
9126 /* Remove one item, return its value. */
9127 vimlist_remove(l, item, item);
9128 *rettv = item->li_tv;
9129 vim_free(item);
9130 }
9131 else
9132 {
9133 /* Remove range of items, return list with values. */
9134 end = (long)get_tv_number_chk(&argvars[2], &error);
9135 if (error)
9136 ; /* type error: do nothing */
9137 else if ((item2 = list_find(l, end)) == NULL)
9138 EMSGN(_(e_listidx), end);
9139 else
9140 {
9141 int cnt = 0;
9142
9143 for (li = item; li != NULL; li = li->li_next)
9144 {
9145 ++cnt;
9146 if (li == item2)
9147 break;
9148 }
9149 if (li == NULL) /* didn't find "item2" after "item" */
9150 EMSG(_(e_invrange));
9151 else
9152 {
9153 vimlist_remove(l, item, item2);
9154 if (rettv_list_alloc(rettv) == OK)
9155 {
9156 l = rettv->vval.v_list;
9157 l->lv_first = item;
9158 l->lv_last = item2;
9159 item->li_prev = NULL;
9160 item2->li_next = NULL;
9161 l->lv_len = cnt;
9162 }
9163 }
9164 }
9165 }
9166 }
9167 }
9168}
9169
9170/*
9171 * "rename({from}, {to})" function
9172 */
9173 static void
9174f_rename(typval_T *argvars, typval_T *rettv)
9175{
9176 char_u buf[NUMBUFLEN];
9177
9178 if (check_restricted() || check_secure())
9179 rettv->vval.v_number = -1;
9180 else
9181 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
9182 get_tv_string_buf(&argvars[1], buf));
9183}
9184
9185/*
9186 * "repeat()" function
9187 */
9188 static void
9189f_repeat(typval_T *argvars, typval_T *rettv)
9190{
9191 char_u *p;
9192 int n;
9193 int slen;
9194 int len;
9195 char_u *r;
9196 int i;
9197
9198 n = (int)get_tv_number(&argvars[1]);
9199 if (argvars[0].v_type == VAR_LIST)
9200 {
9201 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9202 while (n-- > 0)
9203 if (list_extend(rettv->vval.v_list,
9204 argvars[0].vval.v_list, NULL) == FAIL)
9205 break;
9206 }
9207 else
9208 {
9209 p = get_tv_string(&argvars[0]);
9210 rettv->v_type = VAR_STRING;
9211 rettv->vval.v_string = NULL;
9212
9213 slen = (int)STRLEN(p);
9214 len = slen * n;
9215 if (len <= 0)
9216 return;
9217
9218 r = alloc(len + 1);
9219 if (r != NULL)
9220 {
9221 for (i = 0; i < n; i++)
9222 mch_memmove(r + i * slen, p, (size_t)slen);
9223 r[len] = NUL;
9224 }
9225
9226 rettv->vval.v_string = r;
9227 }
9228}
9229
9230/*
9231 * "resolve()" function
9232 */
9233 static void
9234f_resolve(typval_T *argvars, typval_T *rettv)
9235{
9236 char_u *p;
9237#ifdef HAVE_READLINK
9238 char_u *buf = NULL;
9239#endif
9240
9241 p = get_tv_string(&argvars[0]);
9242#ifdef FEAT_SHORTCUT
9243 {
9244 char_u *v = NULL;
9245
9246 v = mch_resolve_shortcut(p);
9247 if (v != NULL)
9248 rettv->vval.v_string = v;
9249 else
9250 rettv->vval.v_string = vim_strsave(p);
9251 }
9252#else
9253# ifdef HAVE_READLINK
9254 {
9255 char_u *cpy;
9256 int len;
9257 char_u *remain = NULL;
9258 char_u *q;
9259 int is_relative_to_current = FALSE;
9260 int has_trailing_pathsep = FALSE;
9261 int limit = 100;
9262
9263 p = vim_strsave(p);
9264
9265 if (p[0] == '.' && (vim_ispathsep(p[1])
9266 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9267 is_relative_to_current = TRUE;
9268
9269 len = STRLEN(p);
9270 if (len > 0 && after_pathsep(p, p + len))
9271 {
9272 has_trailing_pathsep = TRUE;
9273 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9274 }
9275
9276 q = getnextcomp(p);
9277 if (*q != NUL)
9278 {
9279 /* Separate the first path component in "p", and keep the
9280 * remainder (beginning with the path separator). */
9281 remain = vim_strsave(q - 1);
9282 q[-1] = NUL;
9283 }
9284
9285 buf = alloc(MAXPATHL + 1);
9286 if (buf == NULL)
9287 goto fail;
9288
9289 for (;;)
9290 {
9291 for (;;)
9292 {
9293 len = readlink((char *)p, (char *)buf, MAXPATHL);
9294 if (len <= 0)
9295 break;
9296 buf[len] = NUL;
9297
9298 if (limit-- == 0)
9299 {
9300 vim_free(p);
9301 vim_free(remain);
9302 EMSG(_("E655: Too many symbolic links (cycle?)"));
9303 rettv->vval.v_string = NULL;
9304 goto fail;
9305 }
9306
9307 /* Ensure that the result will have a trailing path separator
9308 * if the argument has one. */
9309 if (remain == NULL && has_trailing_pathsep)
9310 add_pathsep(buf);
9311
9312 /* Separate the first path component in the link value and
9313 * concatenate the remainders. */
9314 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9315 if (*q != NUL)
9316 {
9317 if (remain == NULL)
9318 remain = vim_strsave(q - 1);
9319 else
9320 {
9321 cpy = concat_str(q - 1, remain);
9322 if (cpy != NULL)
9323 {
9324 vim_free(remain);
9325 remain = cpy;
9326 }
9327 }
9328 q[-1] = NUL;
9329 }
9330
9331 q = gettail(p);
9332 if (q > p && *q == NUL)
9333 {
9334 /* Ignore trailing path separator. */
9335 q[-1] = NUL;
9336 q = gettail(p);
9337 }
9338 if (q > p && !mch_isFullName(buf))
9339 {
9340 /* symlink is relative to directory of argument */
9341 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
9342 if (cpy != NULL)
9343 {
9344 STRCPY(cpy, p);
9345 STRCPY(gettail(cpy), buf);
9346 vim_free(p);
9347 p = cpy;
9348 }
9349 }
9350 else
9351 {
9352 vim_free(p);
9353 p = vim_strsave(buf);
9354 }
9355 }
9356
9357 if (remain == NULL)
9358 break;
9359
9360 /* Append the first path component of "remain" to "p". */
9361 q = getnextcomp(remain + 1);
9362 len = q - remain - (*q != NUL);
9363 cpy = vim_strnsave(p, STRLEN(p) + len);
9364 if (cpy != NULL)
9365 {
9366 STRNCAT(cpy, remain, len);
9367 vim_free(p);
9368 p = cpy;
9369 }
9370 /* Shorten "remain". */
9371 if (*q != NUL)
9372 STRMOVE(remain, q - 1);
9373 else
Bram Moolenaard23a8232018-02-10 18:45:26 +01009374 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009375 }
9376
9377 /* If the result is a relative path name, make it explicitly relative to
9378 * the current directory if and only if the argument had this form. */
9379 if (!vim_ispathsep(*p))
9380 {
9381 if (is_relative_to_current
9382 && *p != NUL
9383 && !(p[0] == '.'
9384 && (p[1] == NUL
9385 || vim_ispathsep(p[1])
9386 || (p[1] == '.'
9387 && (p[2] == NUL
9388 || vim_ispathsep(p[2]))))))
9389 {
9390 /* Prepend "./". */
9391 cpy = concat_str((char_u *)"./", p);
9392 if (cpy != NULL)
9393 {
9394 vim_free(p);
9395 p = cpy;
9396 }
9397 }
9398 else if (!is_relative_to_current)
9399 {
9400 /* Strip leading "./". */
9401 q = p;
9402 while (q[0] == '.' && vim_ispathsep(q[1]))
9403 q += 2;
9404 if (q > p)
9405 STRMOVE(p, p + 2);
9406 }
9407 }
9408
9409 /* Ensure that the result will have no trailing path separator
9410 * if the argument had none. But keep "/" or "//". */
9411 if (!has_trailing_pathsep)
9412 {
9413 q = p + STRLEN(p);
9414 if (after_pathsep(p, q))
9415 *gettail_sep(p) = NUL;
9416 }
9417
9418 rettv->vval.v_string = p;
9419 }
9420# else
9421 rettv->vval.v_string = vim_strsave(p);
9422# endif
9423#endif
9424
9425 simplify_filename(rettv->vval.v_string);
9426
9427#ifdef HAVE_READLINK
9428fail:
9429 vim_free(buf);
9430#endif
9431 rettv->v_type = VAR_STRING;
9432}
9433
9434/*
9435 * "reverse({list})" function
9436 */
9437 static void
9438f_reverse(typval_T *argvars, typval_T *rettv)
9439{
9440 list_T *l;
9441 listitem_T *li, *ni;
9442
9443 if (argvars[0].v_type != VAR_LIST)
9444 EMSG2(_(e_listarg), "reverse()");
9445 else if ((l = argvars[0].vval.v_list) != NULL
9446 && !tv_check_lock(l->lv_lock,
9447 (char_u *)N_("reverse() argument"), TRUE))
9448 {
9449 li = l->lv_last;
9450 l->lv_first = l->lv_last = NULL;
9451 l->lv_len = 0;
9452 while (li != NULL)
9453 {
9454 ni = li->li_prev;
9455 list_append(l, li);
9456 li = ni;
9457 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009458 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009459 l->lv_idx = l->lv_len - l->lv_idx - 1;
9460 }
9461}
9462
9463#define SP_NOMOVE 0x01 /* don't move cursor */
9464#define SP_REPEAT 0x02 /* repeat to find outer pair */
9465#define SP_RETCOUNT 0x04 /* return matchcount */
9466#define SP_SETPCMARK 0x08 /* set previous context mark */
9467#define SP_START 0x10 /* accept match at start position */
9468#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9469#define SP_END 0x40 /* leave cursor at end of match */
9470#define SP_COLUMN 0x80 /* start at cursor column */
9471
9472static int get_search_arg(typval_T *varp, int *flagsp);
9473
9474/*
9475 * Get flags for a search function.
9476 * Possibly sets "p_ws".
9477 * Returns BACKWARD, FORWARD or zero (for an error).
9478 */
9479 static int
9480get_search_arg(typval_T *varp, int *flagsp)
9481{
9482 int dir = FORWARD;
9483 char_u *flags;
9484 char_u nbuf[NUMBUFLEN];
9485 int mask;
9486
9487 if (varp->v_type != VAR_UNKNOWN)
9488 {
9489 flags = get_tv_string_buf_chk(varp, nbuf);
9490 if (flags == NULL)
9491 return 0; /* type error; errmsg already given */
9492 while (*flags != NUL)
9493 {
9494 switch (*flags)
9495 {
9496 case 'b': dir = BACKWARD; break;
9497 case 'w': p_ws = TRUE; break;
9498 case 'W': p_ws = FALSE; break;
9499 default: mask = 0;
9500 if (flagsp != NULL)
9501 switch (*flags)
9502 {
9503 case 'c': mask = SP_START; break;
9504 case 'e': mask = SP_END; break;
9505 case 'm': mask = SP_RETCOUNT; break;
9506 case 'n': mask = SP_NOMOVE; break;
9507 case 'p': mask = SP_SUBPAT; break;
9508 case 'r': mask = SP_REPEAT; break;
9509 case 's': mask = SP_SETPCMARK; break;
9510 case 'z': mask = SP_COLUMN; break;
9511 }
9512 if (mask == 0)
9513 {
9514 EMSG2(_(e_invarg2), flags);
9515 dir = 0;
9516 }
9517 else
9518 *flagsp |= mask;
9519 }
9520 if (dir == 0)
9521 break;
9522 ++flags;
9523 }
9524 }
9525 return dir;
9526}
9527
9528/*
9529 * Shared by search() and searchpos() functions.
9530 */
9531 static int
9532search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9533{
9534 int flags;
9535 char_u *pat;
9536 pos_T pos;
9537 pos_T save_cursor;
9538 int save_p_ws = p_ws;
9539 int dir;
9540 int retval = 0; /* default: FAIL */
9541 long lnum_stop = 0;
9542 proftime_T tm;
9543#ifdef FEAT_RELTIME
9544 long time_limit = 0;
9545#endif
9546 int options = SEARCH_KEEP;
9547 int subpatnum;
9548
9549 pat = get_tv_string(&argvars[0]);
9550 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
9551 if (dir == 0)
9552 goto theend;
9553 flags = *flagsp;
9554 if (flags & SP_START)
9555 options |= SEARCH_START;
9556 if (flags & SP_END)
9557 options |= SEARCH_END;
9558 if (flags & SP_COLUMN)
9559 options |= SEARCH_COL;
9560
9561 /* Optional arguments: line number to stop searching and timeout. */
9562 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9563 {
9564 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
9565 if (lnum_stop < 0)
9566 goto theend;
9567#ifdef FEAT_RELTIME
9568 if (argvars[3].v_type != VAR_UNKNOWN)
9569 {
9570 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
9571 if (time_limit < 0)
9572 goto theend;
9573 }
9574#endif
9575 }
9576
9577#ifdef FEAT_RELTIME
9578 /* Set the time limit, if there is one. */
9579 profile_setlimit(time_limit, &tm);
9580#endif
9581
9582 /*
9583 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
9584 * Check to make sure only those flags are set.
9585 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
9586 * flags cannot be set. Check for that condition also.
9587 */
9588 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
9589 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9590 {
9591 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
9592 goto theend;
9593 }
9594
9595 pos = save_cursor = curwin->w_cursor;
9596 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009597 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009598 if (subpatnum != FAIL)
9599 {
9600 if (flags & SP_SUBPAT)
9601 retval = subpatnum;
9602 else
9603 retval = pos.lnum;
9604 if (flags & SP_SETPCMARK)
9605 setpcmark();
9606 curwin->w_cursor = pos;
9607 if (match_pos != NULL)
9608 {
9609 /* Store the match cursor position */
9610 match_pos->lnum = pos.lnum;
9611 match_pos->col = pos.col + 1;
9612 }
9613 /* "/$" will put the cursor after the end of the line, may need to
9614 * correct that here */
9615 check_cursor();
9616 }
9617
9618 /* If 'n' flag is used: restore cursor position. */
9619 if (flags & SP_NOMOVE)
9620 curwin->w_cursor = save_cursor;
9621 else
9622 curwin->w_set_curswant = TRUE;
9623theend:
9624 p_ws = save_p_ws;
9625
9626 return retval;
9627}
9628
9629#ifdef FEAT_FLOAT
9630
9631/*
9632 * round() is not in C90, use ceil() or floor() instead.
9633 */
9634 float_T
9635vim_round(float_T f)
9636{
9637 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
9638}
9639
9640/*
9641 * "round({float})" function
9642 */
9643 static void
9644f_round(typval_T *argvars, typval_T *rettv)
9645{
9646 float_T f = 0.0;
9647
9648 rettv->v_type = VAR_FLOAT;
9649 if (get_float_arg(argvars, &f) == OK)
9650 rettv->vval.v_float = vim_round(f);
9651 else
9652 rettv->vval.v_float = 0.0;
9653}
9654#endif
9655
9656/*
9657 * "screenattr()" function
9658 */
9659 static void
9660f_screenattr(typval_T *argvars, typval_T *rettv)
9661{
9662 int row;
9663 int col;
9664 int c;
9665
9666 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9667 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9668 if (row < 0 || row >= screen_Rows
9669 || col < 0 || col >= screen_Columns)
9670 c = -1;
9671 else
9672 c = ScreenAttrs[LineOffset[row] + col];
9673 rettv->vval.v_number = c;
9674}
9675
9676/*
9677 * "screenchar()" function
9678 */
9679 static void
9680f_screenchar(typval_T *argvars, typval_T *rettv)
9681{
9682 int row;
9683 int col;
9684 int off;
9685 int c;
9686
9687 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9688 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9689 if (row < 0 || row >= screen_Rows
9690 || col < 0 || col >= screen_Columns)
9691 c = -1;
9692 else
9693 {
9694 off = LineOffset[row] + col;
9695#ifdef FEAT_MBYTE
9696 if (enc_utf8 && ScreenLinesUC[off] != 0)
9697 c = ScreenLinesUC[off];
9698 else
9699#endif
9700 c = ScreenLines[off];
9701 }
9702 rettv->vval.v_number = c;
9703}
9704
9705/*
9706 * "screencol()" function
9707 *
9708 * First column is 1 to be consistent with virtcol().
9709 */
9710 static void
9711f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
9712{
9713 rettv->vval.v_number = screen_screencol() + 1;
9714}
9715
9716/*
9717 * "screenrow()" function
9718 */
9719 static void
9720f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
9721{
9722 rettv->vval.v_number = screen_screenrow() + 1;
9723}
9724
9725/*
9726 * "search()" function
9727 */
9728 static void
9729f_search(typval_T *argvars, typval_T *rettv)
9730{
9731 int flags = 0;
9732
9733 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
9734}
9735
9736/*
9737 * "searchdecl()" function
9738 */
9739 static void
9740f_searchdecl(typval_T *argvars, typval_T *rettv)
9741{
9742 int locally = 1;
9743 int thisblock = 0;
9744 int error = FALSE;
9745 char_u *name;
9746
9747 rettv->vval.v_number = 1; /* default: FAIL */
9748
9749 name = get_tv_string_chk(&argvars[0]);
9750 if (argvars[1].v_type != VAR_UNKNOWN)
9751 {
9752 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
9753 if (!error && argvars[2].v_type != VAR_UNKNOWN)
9754 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
9755 }
9756 if (!error && name != NULL)
9757 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
9758 locally, thisblock, SEARCH_KEEP) == FAIL;
9759}
9760
9761/*
9762 * Used by searchpair() and searchpairpos()
9763 */
9764 static int
9765searchpair_cmn(typval_T *argvars, pos_T *match_pos)
9766{
9767 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01009768 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009769 int save_p_ws = p_ws;
9770 int dir;
9771 int flags = 0;
9772 char_u nbuf1[NUMBUFLEN];
9773 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009774 int retval = 0; /* default: FAIL */
9775 long lnum_stop = 0;
9776 long time_limit = 0;
9777
9778 /* Get the three pattern arguments: start, middle, end. */
9779 spat = get_tv_string_chk(&argvars[0]);
9780 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
9781 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
9782 if (spat == NULL || mpat == NULL || epat == NULL)
9783 goto theend; /* type error */
9784
9785 /* Handle the optional fourth argument: flags */
9786 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
9787 if (dir == 0)
9788 goto theend;
9789
9790 /* Don't accept SP_END or SP_SUBPAT.
9791 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
9792 */
9793 if ((flags & (SP_END | SP_SUBPAT)) != 0
9794 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9795 {
9796 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
9797 goto theend;
9798 }
9799
9800 /* Using 'r' implies 'W', otherwise it doesn't work. */
9801 if (flags & SP_REPEAT)
9802 p_ws = FALSE;
9803
9804 /* Optional fifth argument: skip expression */
9805 if (argvars[3].v_type == VAR_UNKNOWN
9806 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01009807 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009808 else
9809 {
Bram Moolenaar48570482017-10-30 21:48:41 +01009810 skip = &argvars[4];
9811 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
9812 && skip->v_type != VAR_STRING)
9813 {
9814 /* Type error */
9815 goto theend;
9816 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009817 if (argvars[5].v_type != VAR_UNKNOWN)
9818 {
9819 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
9820 if (lnum_stop < 0)
9821 goto theend;
9822#ifdef FEAT_RELTIME
9823 if (argvars[6].v_type != VAR_UNKNOWN)
9824 {
9825 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
9826 if (time_limit < 0)
9827 goto theend;
9828 }
9829#endif
9830 }
9831 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009832
9833 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
9834 match_pos, lnum_stop, time_limit);
9835
9836theend:
9837 p_ws = save_p_ws;
9838
9839 return retval;
9840}
9841
9842/*
9843 * "searchpair()" function
9844 */
9845 static void
9846f_searchpair(typval_T *argvars, typval_T *rettv)
9847{
9848 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
9849}
9850
9851/*
9852 * "searchpairpos()" function
9853 */
9854 static void
9855f_searchpairpos(typval_T *argvars, typval_T *rettv)
9856{
9857 pos_T match_pos;
9858 int lnum = 0;
9859 int col = 0;
9860
9861 if (rettv_list_alloc(rettv) == FAIL)
9862 return;
9863
9864 if (searchpair_cmn(argvars, &match_pos) > 0)
9865 {
9866 lnum = match_pos.lnum;
9867 col = match_pos.col;
9868 }
9869
9870 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9871 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9872}
9873
9874/*
9875 * Search for a start/middle/end thing.
9876 * Used by searchpair(), see its documentation for the details.
9877 * Returns 0 or -1 for no match,
9878 */
9879 long
9880do_searchpair(
9881 char_u *spat, /* start pattern */
9882 char_u *mpat, /* middle pattern */
9883 char_u *epat, /* end pattern */
9884 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +01009885 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009886 int flags, /* SP_SETPCMARK and other SP_ values */
9887 pos_T *match_pos,
9888 linenr_T lnum_stop, /* stop at this line if not zero */
9889 long time_limit UNUSED) /* stop after this many msec */
9890{
9891 char_u *save_cpo;
9892 char_u *pat, *pat2 = NULL, *pat3 = NULL;
9893 long retval = 0;
9894 pos_T pos;
9895 pos_T firstpos;
9896 pos_T foundpos;
9897 pos_T save_cursor;
9898 pos_T save_pos;
9899 int n;
9900 int r;
9901 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01009902 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009903 int err;
9904 int options = SEARCH_KEEP;
9905 proftime_T tm;
9906
9907 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
9908 save_cpo = p_cpo;
9909 p_cpo = empty_option;
9910
9911#ifdef FEAT_RELTIME
9912 /* Set the time limit, if there is one. */
9913 profile_setlimit(time_limit, &tm);
9914#endif
9915
9916 /* Make two search patterns: start/end (pat2, for in nested pairs) and
9917 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009918 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
9919 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009920 if (pat2 == NULL || pat3 == NULL)
9921 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009922 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009923 if (*mpat == NUL)
9924 STRCPY(pat3, pat2);
9925 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009926 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009927 spat, epat, mpat);
9928 if (flags & SP_START)
9929 options |= SEARCH_START;
9930
Bram Moolenaar48570482017-10-30 21:48:41 +01009931 if (skip != NULL)
9932 {
9933 /* Empty string means to not use the skip expression. */
9934 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
9935 use_skip = skip->vval.v_string != NULL
9936 && *skip->vval.v_string != NUL;
9937 }
9938
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009939 save_cursor = curwin->w_cursor;
9940 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009941 CLEAR_POS(&firstpos);
9942 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009943 pat = pat3;
9944 for (;;)
9945 {
9946 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009947 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009948 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009949 /* didn't find it or found the first match again: FAIL */
9950 break;
9951
9952 if (firstpos.lnum == 0)
9953 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009954 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009955 {
9956 /* Found the same position again. Can happen with a pattern that
9957 * has "\zs" at the end and searching backwards. Advance one
9958 * character and try again. */
9959 if (dir == BACKWARD)
9960 decl(&pos);
9961 else
9962 incl(&pos);
9963 }
9964 foundpos = pos;
9965
9966 /* clear the start flag to avoid getting stuck here */
9967 options &= ~SEARCH_START;
9968
9969 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +01009970 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009971 {
9972 save_pos = curwin->w_cursor;
9973 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01009974 err = FALSE;
9975 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009976 curwin->w_cursor = save_pos;
9977 if (err)
9978 {
9979 /* Evaluating {skip} caused an error, break here. */
9980 curwin->w_cursor = save_cursor;
9981 retval = -1;
9982 break;
9983 }
9984 if (r)
9985 continue;
9986 }
9987
9988 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
9989 {
9990 /* Found end when searching backwards or start when searching
9991 * forward: nested pair. */
9992 ++nest;
9993 pat = pat2; /* nested, don't search for middle */
9994 }
9995 else
9996 {
9997 /* Found end when searching forward or start when searching
9998 * backward: end of (nested) pair; or found middle in outer pair. */
9999 if (--nest == 1)
10000 pat = pat3; /* outer level, search for middle */
10001 }
10002
10003 if (nest == 0)
10004 {
10005 /* Found the match: return matchcount or line number. */
10006 if (flags & SP_RETCOUNT)
10007 ++retval;
10008 else
10009 retval = pos.lnum;
10010 if (flags & SP_SETPCMARK)
10011 setpcmark();
10012 curwin->w_cursor = pos;
10013 if (!(flags & SP_REPEAT))
10014 break;
10015 nest = 1; /* search for next unmatched */
10016 }
10017 }
10018
10019 if (match_pos != NULL)
10020 {
10021 /* Store the match cursor position */
10022 match_pos->lnum = curwin->w_cursor.lnum;
10023 match_pos->col = curwin->w_cursor.col + 1;
10024 }
10025
10026 /* If 'n' flag is used or search failed: restore cursor position. */
10027 if ((flags & SP_NOMOVE) || retval == 0)
10028 curwin->w_cursor = save_cursor;
10029
10030theend:
10031 vim_free(pat2);
10032 vim_free(pat3);
10033 if (p_cpo == empty_option)
10034 p_cpo = save_cpo;
10035 else
10036 /* Darn, evaluating the {skip} expression changed the value. */
10037 free_string_option(save_cpo);
10038
10039 return retval;
10040}
10041
10042/*
10043 * "searchpos()" function
10044 */
10045 static void
10046f_searchpos(typval_T *argvars, typval_T *rettv)
10047{
10048 pos_T match_pos;
10049 int lnum = 0;
10050 int col = 0;
10051 int n;
10052 int flags = 0;
10053
10054 if (rettv_list_alloc(rettv) == FAIL)
10055 return;
10056
10057 n = search_cmn(argvars, &match_pos, &flags);
10058 if (n > 0)
10059 {
10060 lnum = match_pos.lnum;
10061 col = match_pos.col;
10062 }
10063
10064 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10065 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10066 if (flags & SP_SUBPAT)
10067 list_append_number(rettv->vval.v_list, (varnumber_T)n);
10068}
10069
10070 static void
10071f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
10072{
10073#ifdef FEAT_CLIENTSERVER
10074 char_u buf[NUMBUFLEN];
10075 char_u *server = get_tv_string_chk(&argvars[0]);
10076 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
10077
10078 rettv->vval.v_number = -1;
10079 if (server == NULL || reply == NULL)
10080 return;
10081 if (check_restricted() || check_secure())
10082 return;
10083# ifdef FEAT_X11
10084 if (check_connection() == FAIL)
10085 return;
10086# endif
10087
10088 if (serverSendReply(server, reply) < 0)
10089 {
10090 EMSG(_("E258: Unable to send to client"));
10091 return;
10092 }
10093 rettv->vval.v_number = 0;
10094#else
10095 rettv->vval.v_number = -1;
10096#endif
10097}
10098
10099 static void
10100f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10101{
10102 char_u *r = NULL;
10103
10104#ifdef FEAT_CLIENTSERVER
10105# ifdef WIN32
10106 r = serverGetVimNames();
10107# else
10108 make_connection();
10109 if (X_DISPLAY != NULL)
10110 r = serverGetVimNames(X_DISPLAY);
10111# endif
10112#endif
10113 rettv->v_type = VAR_STRING;
10114 rettv->vval.v_string = r;
10115}
10116
10117/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010118 * Set line or list of lines in buffer "buf".
10119 */
10120 static void
10121set_buffer_lines(buf_T *buf, linenr_T lnum, typval_T *lines, typval_T *rettv)
10122{
10123 char_u *line = NULL;
10124 list_T *l = NULL;
10125 listitem_T *li = NULL;
10126 long added = 0;
10127 linenr_T lcount;
Bram Moolenaar0c4dc882017-11-06 21:32:54 +010010128 buf_T *curbuf_save = NULL;
10129 win_T *curwin_save = NULL;
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010130 int is_curbuf = buf == curbuf;
10131
Bram Moolenaar9d954202017-09-04 20:34:19 +020010132 /* When using the current buffer ml_mfp will be set if needed. Useful when
10133 * setline() is used on startup. For other buffers the buffer must be
10134 * loaded. */
10135 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010136 {
10137 rettv->vval.v_number = 1; /* FAIL */
10138 return;
10139 }
10140
Bram Moolenaar0c4dc882017-11-06 21:32:54 +010010141 if (!is_curbuf)
10142 {
10143 wininfo_T *wip;
10144
10145 curbuf_save = curbuf;
10146 curwin_save = curwin;
10147 curbuf = buf;
10148 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
10149 {
10150 if (wip->wi_win != NULL)
10151 {
10152 curwin = wip->wi_win;
10153 break;
10154 }
10155 }
10156 }
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010157
10158 lcount = curbuf->b_ml.ml_line_count;
10159
10160 if (lines->v_type == VAR_LIST)
10161 {
10162 l = lines->vval.v_list;
10163 li = l->lv_first;
10164 }
10165 else
10166 line = get_tv_string_chk(lines);
10167
10168 /* default result is zero == OK */
10169 for (;;)
10170 {
10171 if (l != NULL)
10172 {
10173 /* list argument, get next string */
10174 if (li == NULL)
10175 break;
10176 line = get_tv_string_chk(&li->li_tv);
10177 li = li->li_next;
10178 }
10179
10180 rettv->vval.v_number = 1; /* FAIL */
10181 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
10182 break;
10183
10184 /* When coming here from Insert mode, sync undo, so that this can be
10185 * undone separately from what was previously inserted. */
10186 if (u_sync_once == 2)
10187 {
10188 u_sync_once = 1; /* notify that u_sync() was called */
10189 u_sync(TRUE);
10190 }
10191
10192 if (lnum <= curbuf->b_ml.ml_line_count)
10193 {
10194 /* existing line, replace it */
10195 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
10196 {
10197 changed_bytes(lnum, 0);
10198 if (is_curbuf && lnum == curwin->w_cursor.lnum)
10199 check_cursor_col();
10200 rettv->vval.v_number = 0; /* OK */
10201 }
10202 }
10203 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
10204 {
10205 /* lnum is one past the last line, append the line */
10206 ++added;
10207 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
10208 rettv->vval.v_number = 0; /* OK */
10209 }
10210
10211 if (l == NULL) /* only one string argument */
10212 break;
10213 ++lnum;
10214 }
10215
10216 if (added > 0)
10217 appended_lines_mark(lcount, added);
10218
Bram Moolenaar0c4dc882017-11-06 21:32:54 +010010219 if (!is_curbuf)
10220 {
10221 curbuf = curbuf_save;
10222 curwin = curwin_save;
10223 }
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010224}
10225
10226/*
10227 * "setbufline()" function
10228 */
10229 static void
10230f_setbufline(argvars, rettv)
10231 typval_T *argvars;
10232 typval_T *rettv;
10233{
10234 linenr_T lnum;
10235 buf_T *buf;
10236
10237 buf = get_buf_tv(&argvars[0], FALSE);
10238 if (buf == NULL)
10239 rettv->vval.v_number = 1; /* FAIL */
10240 else
10241 {
10242 lnum = get_tv_lnum_buf(&argvars[1], buf);
10243
10244 set_buffer_lines(buf, lnum, &argvars[2], rettv);
10245 }
10246}
10247
10248/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010249 * "setbufvar()" function
10250 */
10251 static void
10252f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10253{
10254 buf_T *buf;
10255 char_u *varname, *bufvarname;
10256 typval_T *varp;
10257 char_u nbuf[NUMBUFLEN];
10258
10259 if (check_restricted() || check_secure())
10260 return;
10261 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10262 varname = get_tv_string_chk(&argvars[1]);
10263 buf = get_buf_tv(&argvars[0], FALSE);
10264 varp = &argvars[2];
10265
10266 if (buf != NULL && varname != NULL && varp != NULL)
10267 {
10268 if (*varname == '&')
10269 {
10270 long numval;
10271 char_u *strval;
10272 int error = FALSE;
10273 aco_save_T aco;
10274
10275 /* set curbuf to be our buf, temporarily */
10276 aucmd_prepbuf(&aco, buf);
10277
10278 ++varname;
10279 numval = (long)get_tv_number_chk(varp, &error);
10280 strval = get_tv_string_buf_chk(varp, nbuf);
10281 if (!error && strval != NULL)
10282 set_option_value(varname, numval, strval, OPT_LOCAL);
10283
10284 /* reset notion of buffer */
10285 aucmd_restbuf(&aco);
10286 }
10287 else
10288 {
10289 buf_T *save_curbuf = curbuf;
10290
10291 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
10292 if (bufvarname != NULL)
10293 {
10294 curbuf = buf;
10295 STRCPY(bufvarname, "b:");
10296 STRCPY(bufvarname + 2, varname);
10297 set_var(bufvarname, varp, TRUE);
10298 vim_free(bufvarname);
10299 curbuf = save_curbuf;
10300 }
10301 }
10302 }
10303}
10304
10305 static void
10306f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10307{
10308 dict_T *d;
10309 dictitem_T *di;
10310 char_u *csearch;
10311
10312 if (argvars[0].v_type != VAR_DICT)
10313 {
10314 EMSG(_(e_dictreq));
10315 return;
10316 }
10317
10318 if ((d = argvars[0].vval.v_dict) != NULL)
10319 {
10320 csearch = get_dict_string(d, (char_u *)"char", FALSE);
10321 if (csearch != NULL)
10322 {
10323#ifdef FEAT_MBYTE
10324 if (enc_utf8)
10325 {
10326 int pcc[MAX_MCO];
10327 int c = utfc_ptr2char(csearch, pcc);
10328
10329 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10330 }
10331 else
10332#endif
10333 set_last_csearch(PTR2CHAR(csearch),
10334 csearch, MB_PTR2LEN(csearch));
10335 }
10336
10337 di = dict_find(d, (char_u *)"forward", -1);
10338 if (di != NULL)
10339 set_csearch_direction((int)get_tv_number(&di->di_tv)
10340 ? FORWARD : BACKWARD);
10341
10342 di = dict_find(d, (char_u *)"until", -1);
10343 if (di != NULL)
10344 set_csearch_until(!!get_tv_number(&di->di_tv));
10345 }
10346}
10347
10348/*
10349 * "setcmdpos()" function
10350 */
10351 static void
10352f_setcmdpos(typval_T *argvars, typval_T *rettv)
10353{
10354 int pos = (int)get_tv_number(&argvars[0]) - 1;
10355
10356 if (pos >= 0)
10357 rettv->vval.v_number = set_cmdline_pos(pos);
10358}
10359
10360/*
10361 * "setfperm({fname}, {mode})" function
10362 */
10363 static void
10364f_setfperm(typval_T *argvars, typval_T *rettv)
10365{
10366 char_u *fname;
10367 char_u modebuf[NUMBUFLEN];
10368 char_u *mode_str;
10369 int i;
10370 int mask;
10371 int mode = 0;
10372
10373 rettv->vval.v_number = 0;
10374 fname = get_tv_string_chk(&argvars[0]);
10375 if (fname == NULL)
10376 return;
10377 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
10378 if (mode_str == NULL)
10379 return;
10380 if (STRLEN(mode_str) != 9)
10381 {
10382 EMSG2(_(e_invarg2), mode_str);
10383 return;
10384 }
10385
10386 mask = 1;
10387 for (i = 8; i >= 0; --i)
10388 {
10389 if (mode_str[i] != '-')
10390 mode |= mask;
10391 mask = mask << 1;
10392 }
10393 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10394}
10395
10396/*
10397 * "setline()" function
10398 */
10399 static void
10400f_setline(typval_T *argvars, typval_T *rettv)
10401{
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010402 linenr_T lnum = get_tv_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010403
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010404 set_buffer_lines(curbuf, lnum, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010405}
10406
Bram Moolenaard823fa92016-08-12 16:29:27 +020010407static 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 +020010408
10409/*
10410 * Used by "setqflist()" and "setloclist()" functions
10411 */
10412 static void
10413set_qf_ll_list(
10414 win_T *wp UNUSED,
10415 typval_T *list_arg UNUSED,
10416 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010417 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010418 typval_T *rettv)
10419{
10420#ifdef FEAT_QUICKFIX
10421 static char *e_invact = N_("E927: Invalid action: '%s'");
10422 char_u *act;
10423 int action = 0;
10424#endif
10425
10426 rettv->vval.v_number = -1;
10427
10428#ifdef FEAT_QUICKFIX
10429 if (list_arg->v_type != VAR_LIST)
10430 EMSG(_(e_listreq));
10431 else
10432 {
10433 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010434 dict_T *d = NULL;
10435 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010436
10437 if (action_arg->v_type == VAR_STRING)
10438 {
10439 act = get_tv_string_chk(action_arg);
10440 if (act == NULL)
10441 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020010442 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
10443 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010444 action = *act;
10445 else
10446 EMSG2(_(e_invact), act);
10447 }
10448 else if (action_arg->v_type == VAR_UNKNOWN)
10449 action = ' ';
10450 else
10451 EMSG(_(e_stringreq));
10452
Bram Moolenaard823fa92016-08-12 16:29:27 +020010453 if (action_arg->v_type != VAR_UNKNOWN
10454 && what_arg->v_type != VAR_UNKNOWN)
10455 {
10456 if (what_arg->v_type == VAR_DICT)
10457 d = what_arg->vval.v_dict;
10458 else
10459 {
10460 EMSG(_(e_dictreq));
10461 valid_dict = FALSE;
10462 }
10463 }
10464
10465 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
10466 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()"), d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010467 rettv->vval.v_number = 0;
10468 }
10469#endif
10470}
10471
10472/*
10473 * "setloclist()" function
10474 */
10475 static void
10476f_setloclist(typval_T *argvars, typval_T *rettv)
10477{
10478 win_T *win;
10479
10480 rettv->vval.v_number = -1;
10481
10482 win = find_win_by_nr(&argvars[0], NULL);
10483 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020010484 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010485}
10486
10487/*
10488 * "setmatches()" function
10489 */
10490 static void
10491f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10492{
10493#ifdef FEAT_SEARCH_EXTRA
10494 list_T *l;
10495 listitem_T *li;
10496 dict_T *d;
10497 list_T *s = NULL;
10498
10499 rettv->vval.v_number = -1;
10500 if (argvars[0].v_type != VAR_LIST)
10501 {
10502 EMSG(_(e_listreq));
10503 return;
10504 }
10505 if ((l = argvars[0].vval.v_list) != NULL)
10506 {
10507
10508 /* To some extent make sure that we are dealing with a list from
10509 * "getmatches()". */
10510 li = l->lv_first;
10511 while (li != NULL)
10512 {
10513 if (li->li_tv.v_type != VAR_DICT
10514 || (d = li->li_tv.vval.v_dict) == NULL)
10515 {
10516 EMSG(_(e_invarg));
10517 return;
10518 }
10519 if (!(dict_find(d, (char_u *)"group", -1) != NULL
10520 && (dict_find(d, (char_u *)"pattern", -1) != NULL
10521 || dict_find(d, (char_u *)"pos1", -1) != NULL)
10522 && dict_find(d, (char_u *)"priority", -1) != NULL
10523 && dict_find(d, (char_u *)"id", -1) != NULL))
10524 {
10525 EMSG(_(e_invarg));
10526 return;
10527 }
10528 li = li->li_next;
10529 }
10530
10531 clear_matches(curwin);
10532 li = l->lv_first;
10533 while (li != NULL)
10534 {
10535 int i = 0;
10536 char_u buf[5];
10537 dictitem_T *di;
10538 char_u *group;
10539 int priority;
10540 int id;
10541 char_u *conceal;
10542
10543 d = li->li_tv.vval.v_dict;
10544 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
10545 {
10546 if (s == NULL)
10547 {
10548 s = list_alloc();
10549 if (s == NULL)
10550 return;
10551 }
10552
10553 /* match from matchaddpos() */
10554 for (i = 1; i < 9; i++)
10555 {
10556 sprintf((char *)buf, (char *)"pos%d", i);
10557 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
10558 {
10559 if (di->di_tv.v_type != VAR_LIST)
10560 return;
10561
10562 list_append_tv(s, &di->di_tv);
10563 s->lv_refcount++;
10564 }
10565 else
10566 break;
10567 }
10568 }
10569
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010570 group = get_dict_string(d, (char_u *)"group", TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010571 priority = (int)get_dict_number(d, (char_u *)"priority");
10572 id = (int)get_dict_number(d, (char_u *)"id");
10573 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010574 ? get_dict_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010575 : NULL;
10576 if (i == 0)
10577 {
10578 match_add(curwin, group,
10579 get_dict_string(d, (char_u *)"pattern", FALSE),
10580 priority, id, NULL, conceal);
10581 }
10582 else
10583 {
10584 match_add(curwin, group, NULL, priority, id, s, conceal);
10585 list_unref(s);
10586 s = NULL;
10587 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010588 vim_free(group);
10589 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010590
10591 li = li->li_next;
10592 }
10593 rettv->vval.v_number = 0;
10594 }
10595#endif
10596}
10597
10598/*
10599 * "setpos()" function
10600 */
10601 static void
10602f_setpos(typval_T *argvars, typval_T *rettv)
10603{
10604 pos_T pos;
10605 int fnum;
10606 char_u *name;
10607 colnr_T curswant = -1;
10608
10609 rettv->vval.v_number = -1;
10610 name = get_tv_string_chk(argvars);
10611 if (name != NULL)
10612 {
10613 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10614 {
10615 if (--pos.col < 0)
10616 pos.col = 0;
10617 if (name[0] == '.' && name[1] == NUL)
10618 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010619 /* set cursor; "fnum" is ignored */
10620 curwin->w_cursor = pos;
10621 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010622 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010623 curwin->w_curswant = curswant - 1;
10624 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010625 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010626 check_cursor();
10627 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010628 }
10629 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10630 {
10631 /* set mark */
10632 if (setmark_pos(name[1], &pos, fnum) == OK)
10633 rettv->vval.v_number = 0;
10634 }
10635 else
10636 EMSG(_(e_invarg));
10637 }
10638 }
10639}
10640
10641/*
10642 * "setqflist()" function
10643 */
10644 static void
10645f_setqflist(typval_T *argvars, typval_T *rettv)
10646{
Bram Moolenaard823fa92016-08-12 16:29:27 +020010647 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010648}
10649
10650/*
10651 * "setreg()" function
10652 */
10653 static void
10654f_setreg(typval_T *argvars, typval_T *rettv)
10655{
10656 int regname;
10657 char_u *strregname;
10658 char_u *stropt;
10659 char_u *strval;
10660 int append;
10661 char_u yank_type;
10662 long block_len;
10663
10664 block_len = -1;
10665 yank_type = MAUTO;
10666 append = FALSE;
10667
10668 strregname = get_tv_string_chk(argvars);
10669 rettv->vval.v_number = 1; /* FAIL is default */
10670
10671 if (strregname == NULL)
10672 return; /* type error; errmsg already given */
10673 regname = *strregname;
10674 if (regname == 0 || regname == '@')
10675 regname = '"';
10676
10677 if (argvars[2].v_type != VAR_UNKNOWN)
10678 {
10679 stropt = get_tv_string_chk(&argvars[2]);
10680 if (stropt == NULL)
10681 return; /* type error */
10682 for (; *stropt != NUL; ++stropt)
10683 switch (*stropt)
10684 {
10685 case 'a': case 'A': /* append */
10686 append = TRUE;
10687 break;
10688 case 'v': case 'c': /* character-wise selection */
10689 yank_type = MCHAR;
10690 break;
10691 case 'V': case 'l': /* line-wise selection */
10692 yank_type = MLINE;
10693 break;
10694 case 'b': case Ctrl_V: /* block-wise selection */
10695 yank_type = MBLOCK;
10696 if (VIM_ISDIGIT(stropt[1]))
10697 {
10698 ++stropt;
10699 block_len = getdigits(&stropt) - 1;
10700 --stropt;
10701 }
10702 break;
10703 }
10704 }
10705
10706 if (argvars[1].v_type == VAR_LIST)
10707 {
10708 char_u **lstval;
10709 char_u **allocval;
10710 char_u buf[NUMBUFLEN];
10711 char_u **curval;
10712 char_u **curallocval;
10713 list_T *ll = argvars[1].vval.v_list;
10714 listitem_T *li;
10715 int len;
10716
10717 /* If the list is NULL handle like an empty list. */
10718 len = ll == NULL ? 0 : ll->lv_len;
10719
10720 /* First half: use for pointers to result lines; second half: use for
10721 * pointers to allocated copies. */
10722 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
10723 if (lstval == NULL)
10724 return;
10725 curval = lstval;
10726 allocval = lstval + len + 2;
10727 curallocval = allocval;
10728
10729 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
10730 li = li->li_next)
10731 {
10732 strval = get_tv_string_buf_chk(&li->li_tv, buf);
10733 if (strval == NULL)
10734 goto free_lstval;
10735 if (strval == buf)
10736 {
10737 /* Need to make a copy, next get_tv_string_buf_chk() will
10738 * overwrite the string. */
10739 strval = vim_strsave(buf);
10740 if (strval == NULL)
10741 goto free_lstval;
10742 *curallocval++ = strval;
10743 }
10744 *curval++ = strval;
10745 }
10746 *curval++ = NULL;
10747
10748 write_reg_contents_lst(regname, lstval, -1,
10749 append, yank_type, block_len);
10750free_lstval:
10751 while (curallocval > allocval)
10752 vim_free(*--curallocval);
10753 vim_free(lstval);
10754 }
10755 else
10756 {
10757 strval = get_tv_string_chk(&argvars[1]);
10758 if (strval == NULL)
10759 return;
10760 write_reg_contents_ex(regname, strval, -1,
10761 append, yank_type, block_len);
10762 }
10763 rettv->vval.v_number = 0;
10764}
10765
10766/*
10767 * "settabvar()" function
10768 */
10769 static void
10770f_settabvar(typval_T *argvars, typval_T *rettv)
10771{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010772 tabpage_T *save_curtab;
10773 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010774 char_u *varname, *tabvarname;
10775 typval_T *varp;
10776
10777 rettv->vval.v_number = 0;
10778
10779 if (check_restricted() || check_secure())
10780 return;
10781
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010782 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010783 varname = get_tv_string_chk(&argvars[1]);
10784 varp = &argvars[2];
10785
Bram Moolenaar4033c552017-09-16 20:54:51 +020010786 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010787 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010788 save_curtab = curtab;
10789 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010790
10791 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
10792 if (tabvarname != NULL)
10793 {
10794 STRCPY(tabvarname, "t:");
10795 STRCPY(tabvarname + 2, varname);
10796 set_var(tabvarname, varp, TRUE);
10797 vim_free(tabvarname);
10798 }
10799
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010800 /* Restore current tabpage */
10801 if (valid_tabpage(save_curtab))
10802 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010803 }
10804}
10805
10806/*
10807 * "settabwinvar()" function
10808 */
10809 static void
10810f_settabwinvar(typval_T *argvars, typval_T *rettv)
10811{
10812 setwinvar(argvars, rettv, 1);
10813}
10814
10815/*
10816 * "setwinvar()" function
10817 */
10818 static void
10819f_setwinvar(typval_T *argvars, typval_T *rettv)
10820{
10821 setwinvar(argvars, rettv, 0);
10822}
10823
10824#ifdef FEAT_CRYPT
10825/*
10826 * "sha256({string})" function
10827 */
10828 static void
10829f_sha256(typval_T *argvars, typval_T *rettv)
10830{
10831 char_u *p;
10832
10833 p = get_tv_string(&argvars[0]);
10834 rettv->vval.v_string = vim_strsave(
10835 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
10836 rettv->v_type = VAR_STRING;
10837}
10838#endif /* FEAT_CRYPT */
10839
10840/*
10841 * "shellescape({string})" function
10842 */
10843 static void
10844f_shellescape(typval_T *argvars, typval_T *rettv)
10845{
Bram Moolenaar20615522017-06-05 18:46:26 +020010846 int do_special = non_zero_arg(&argvars[1]);
10847
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010848 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar20615522017-06-05 18:46:26 +020010849 get_tv_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010850 rettv->v_type = VAR_STRING;
10851}
10852
10853/*
10854 * shiftwidth() function
10855 */
10856 static void
10857f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
10858{
10859 rettv->vval.v_number = get_sw_value(curbuf);
10860}
10861
10862/*
10863 * "simplify()" function
10864 */
10865 static void
10866f_simplify(typval_T *argvars, typval_T *rettv)
10867{
10868 char_u *p;
10869
10870 p = get_tv_string(&argvars[0]);
10871 rettv->vval.v_string = vim_strsave(p);
10872 simplify_filename(rettv->vval.v_string); /* simplify in place */
10873 rettv->v_type = VAR_STRING;
10874}
10875
10876#ifdef FEAT_FLOAT
10877/*
10878 * "sin()" function
10879 */
10880 static void
10881f_sin(typval_T *argvars, typval_T *rettv)
10882{
10883 float_T f = 0.0;
10884
10885 rettv->v_type = VAR_FLOAT;
10886 if (get_float_arg(argvars, &f) == OK)
10887 rettv->vval.v_float = sin(f);
10888 else
10889 rettv->vval.v_float = 0.0;
10890}
10891
10892/*
10893 * "sinh()" function
10894 */
10895 static void
10896f_sinh(typval_T *argvars, typval_T *rettv)
10897{
10898 float_T f = 0.0;
10899
10900 rettv->v_type = VAR_FLOAT;
10901 if (get_float_arg(argvars, &f) == OK)
10902 rettv->vval.v_float = sinh(f);
10903 else
10904 rettv->vval.v_float = 0.0;
10905}
10906#endif
10907
10908static int
10909#ifdef __BORLANDC__
10910 _RTLENTRYF
10911#endif
10912 item_compare(const void *s1, const void *s2);
10913static int
10914#ifdef __BORLANDC__
10915 _RTLENTRYF
10916#endif
10917 item_compare2(const void *s1, const void *s2);
10918
10919/* struct used in the array that's given to qsort() */
10920typedef struct
10921{
10922 listitem_T *item;
10923 int idx;
10924} sortItem_T;
10925
10926/* struct storing information about current sort */
10927typedef struct
10928{
10929 int item_compare_ic;
10930 int item_compare_numeric;
10931 int item_compare_numbers;
10932#ifdef FEAT_FLOAT
10933 int item_compare_float;
10934#endif
10935 char_u *item_compare_func;
10936 partial_T *item_compare_partial;
10937 dict_T *item_compare_selfdict;
10938 int item_compare_func_err;
10939 int item_compare_keep_zero;
10940} sortinfo_T;
10941static sortinfo_T *sortinfo = NULL;
10942static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
10943#define ITEM_COMPARE_FAIL 999
10944
10945/*
10946 * Compare functions for f_sort() and f_uniq() below.
10947 */
10948 static int
10949#ifdef __BORLANDC__
10950_RTLENTRYF
10951#endif
10952item_compare(const void *s1, const void *s2)
10953{
10954 sortItem_T *si1, *si2;
10955 typval_T *tv1, *tv2;
10956 char_u *p1, *p2;
10957 char_u *tofree1 = NULL, *tofree2 = NULL;
10958 int res;
10959 char_u numbuf1[NUMBUFLEN];
10960 char_u numbuf2[NUMBUFLEN];
10961
10962 si1 = (sortItem_T *)s1;
10963 si2 = (sortItem_T *)s2;
10964 tv1 = &si1->item->li_tv;
10965 tv2 = &si2->item->li_tv;
10966
10967 if (sortinfo->item_compare_numbers)
10968 {
10969 varnumber_T v1 = get_tv_number(tv1);
10970 varnumber_T v2 = get_tv_number(tv2);
10971
10972 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10973 }
10974
10975#ifdef FEAT_FLOAT
10976 if (sortinfo->item_compare_float)
10977 {
10978 float_T v1 = get_tv_float(tv1);
10979 float_T v2 = get_tv_float(tv2);
10980
10981 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10982 }
10983#endif
10984
10985 /* tv2string() puts quotes around a string and allocates memory. Don't do
10986 * that for string variables. Use a single quote when comparing with a
10987 * non-string to do what the docs promise. */
10988 if (tv1->v_type == VAR_STRING)
10989 {
10990 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
10991 p1 = (char_u *)"'";
10992 else
10993 p1 = tv1->vval.v_string;
10994 }
10995 else
10996 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
10997 if (tv2->v_type == VAR_STRING)
10998 {
10999 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11000 p2 = (char_u *)"'";
11001 else
11002 p2 = tv2->vval.v_string;
11003 }
11004 else
11005 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
11006 if (p1 == NULL)
11007 p1 = (char_u *)"";
11008 if (p2 == NULL)
11009 p2 = (char_u *)"";
11010 if (!sortinfo->item_compare_numeric)
11011 {
11012 if (sortinfo->item_compare_ic)
11013 res = STRICMP(p1, p2);
11014 else
11015 res = STRCMP(p1, p2);
11016 }
11017 else
11018 {
11019 double n1, n2;
11020 n1 = strtod((char *)p1, (char **)&p1);
11021 n2 = strtod((char *)p2, (char **)&p2);
11022 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
11023 }
11024
11025 /* When the result would be zero, compare the item indexes. Makes the
11026 * sort stable. */
11027 if (res == 0 && !sortinfo->item_compare_keep_zero)
11028 res = si1->idx > si2->idx ? 1 : -1;
11029
11030 vim_free(tofree1);
11031 vim_free(tofree2);
11032 return res;
11033}
11034
11035 static int
11036#ifdef __BORLANDC__
11037_RTLENTRYF
11038#endif
11039item_compare2(const void *s1, const void *s2)
11040{
11041 sortItem_T *si1, *si2;
11042 int res;
11043 typval_T rettv;
11044 typval_T argv[3];
11045 int dummy;
11046 char_u *func_name;
11047 partial_T *partial = sortinfo->item_compare_partial;
11048
11049 /* shortcut after failure in previous call; compare all items equal */
11050 if (sortinfo->item_compare_func_err)
11051 return 0;
11052
11053 si1 = (sortItem_T *)s1;
11054 si2 = (sortItem_T *)s2;
11055
11056 if (partial == NULL)
11057 func_name = sortinfo->item_compare_func;
11058 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020011059 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011060
11061 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
11062 * in the copy without changing the original list items. */
11063 copy_tv(&si1->item->li_tv, &argv[0]);
11064 copy_tv(&si2->item->li_tv, &argv[1]);
11065
11066 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
11067 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020011068 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011069 partial, sortinfo->item_compare_selfdict);
11070 clear_tv(&argv[0]);
11071 clear_tv(&argv[1]);
11072
11073 if (res == FAIL)
11074 res = ITEM_COMPARE_FAIL;
11075 else
11076 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
11077 if (sortinfo->item_compare_func_err)
11078 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
11079 clear_tv(&rettv);
11080
11081 /* When the result would be zero, compare the pointers themselves. Makes
11082 * the sort stable. */
11083 if (res == 0 && !sortinfo->item_compare_keep_zero)
11084 res = si1->idx > si2->idx ? 1 : -1;
11085
11086 return res;
11087}
11088
11089/*
11090 * "sort({list})" function
11091 */
11092 static void
11093do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
11094{
11095 list_T *l;
11096 listitem_T *li;
11097 sortItem_T *ptrs;
11098 sortinfo_T *old_sortinfo;
11099 sortinfo_T info;
11100 long len;
11101 long i;
11102
11103 /* Pointer to current info struct used in compare function. Save and
11104 * restore the current one for nested calls. */
11105 old_sortinfo = sortinfo;
11106 sortinfo = &info;
11107
11108 if (argvars[0].v_type != VAR_LIST)
11109 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
11110 else
11111 {
11112 l = argvars[0].vval.v_list;
11113 if (l == NULL || tv_check_lock(l->lv_lock,
11114 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
11115 TRUE))
11116 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011117 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011118
11119 len = list_len(l);
11120 if (len <= 1)
11121 goto theend; /* short list sorts pretty quickly */
11122
11123 info.item_compare_ic = FALSE;
11124 info.item_compare_numeric = FALSE;
11125 info.item_compare_numbers = FALSE;
11126#ifdef FEAT_FLOAT
11127 info.item_compare_float = FALSE;
11128#endif
11129 info.item_compare_func = NULL;
11130 info.item_compare_partial = NULL;
11131 info.item_compare_selfdict = NULL;
11132 if (argvars[1].v_type != VAR_UNKNOWN)
11133 {
11134 /* optional second argument: {func} */
11135 if (argvars[1].v_type == VAR_FUNC)
11136 info.item_compare_func = argvars[1].vval.v_string;
11137 else if (argvars[1].v_type == VAR_PARTIAL)
11138 info.item_compare_partial = argvars[1].vval.v_partial;
11139 else
11140 {
11141 int error = FALSE;
11142
11143 i = (long)get_tv_number_chk(&argvars[1], &error);
11144 if (error)
11145 goto theend; /* type error; errmsg already given */
11146 if (i == 1)
11147 info.item_compare_ic = TRUE;
11148 else if (argvars[1].v_type != VAR_NUMBER)
11149 info.item_compare_func = get_tv_string(&argvars[1]);
11150 else if (i != 0)
11151 {
11152 EMSG(_(e_invarg));
11153 goto theend;
11154 }
11155 if (info.item_compare_func != NULL)
11156 {
11157 if (*info.item_compare_func == NUL)
11158 {
11159 /* empty string means default sort */
11160 info.item_compare_func = NULL;
11161 }
11162 else if (STRCMP(info.item_compare_func, "n") == 0)
11163 {
11164 info.item_compare_func = NULL;
11165 info.item_compare_numeric = TRUE;
11166 }
11167 else if (STRCMP(info.item_compare_func, "N") == 0)
11168 {
11169 info.item_compare_func = NULL;
11170 info.item_compare_numbers = TRUE;
11171 }
11172#ifdef FEAT_FLOAT
11173 else if (STRCMP(info.item_compare_func, "f") == 0)
11174 {
11175 info.item_compare_func = NULL;
11176 info.item_compare_float = TRUE;
11177 }
11178#endif
11179 else if (STRCMP(info.item_compare_func, "i") == 0)
11180 {
11181 info.item_compare_func = NULL;
11182 info.item_compare_ic = TRUE;
11183 }
11184 }
11185 }
11186
11187 if (argvars[2].v_type != VAR_UNKNOWN)
11188 {
11189 /* optional third argument: {dict} */
11190 if (argvars[2].v_type != VAR_DICT)
11191 {
11192 EMSG(_(e_dictreq));
11193 goto theend;
11194 }
11195 info.item_compare_selfdict = argvars[2].vval.v_dict;
11196 }
11197 }
11198
11199 /* Make an array with each entry pointing to an item in the List. */
11200 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
11201 if (ptrs == NULL)
11202 goto theend;
11203
11204 i = 0;
11205 if (sort)
11206 {
11207 /* sort(): ptrs will be the list to sort */
11208 for (li = l->lv_first; li != NULL; li = li->li_next)
11209 {
11210 ptrs[i].item = li;
11211 ptrs[i].idx = i;
11212 ++i;
11213 }
11214
11215 info.item_compare_func_err = FALSE;
11216 info.item_compare_keep_zero = FALSE;
11217 /* test the compare function */
11218 if ((info.item_compare_func != NULL
11219 || info.item_compare_partial != NULL)
11220 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11221 == ITEM_COMPARE_FAIL)
11222 EMSG(_("E702: Sort compare function failed"));
11223 else
11224 {
11225 /* Sort the array with item pointers. */
11226 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
11227 info.item_compare_func == NULL
11228 && info.item_compare_partial == NULL
11229 ? item_compare : item_compare2);
11230
11231 if (!info.item_compare_func_err)
11232 {
11233 /* Clear the List and append the items in sorted order. */
11234 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
11235 l->lv_len = 0;
11236 for (i = 0; i < len; ++i)
11237 list_append(l, ptrs[i].item);
11238 }
11239 }
11240 }
11241 else
11242 {
11243 int (*item_compare_func_ptr)(const void *, const void *);
11244
11245 /* f_uniq(): ptrs will be a stack of items to remove */
11246 info.item_compare_func_err = FALSE;
11247 info.item_compare_keep_zero = TRUE;
11248 item_compare_func_ptr = info.item_compare_func != NULL
11249 || info.item_compare_partial != NULL
11250 ? item_compare2 : item_compare;
11251
11252 for (li = l->lv_first; li != NULL && li->li_next != NULL;
11253 li = li->li_next)
11254 {
11255 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
11256 == 0)
11257 ptrs[i++].item = li;
11258 if (info.item_compare_func_err)
11259 {
11260 EMSG(_("E882: Uniq compare function failed"));
11261 break;
11262 }
11263 }
11264
11265 if (!info.item_compare_func_err)
11266 {
11267 while (--i >= 0)
11268 {
11269 li = ptrs[i].item->li_next;
11270 ptrs[i].item->li_next = li->li_next;
11271 if (li->li_next != NULL)
11272 li->li_next->li_prev = ptrs[i].item;
11273 else
11274 l->lv_last = ptrs[i].item;
11275 list_fix_watch(l, li);
11276 listitem_free(li);
11277 l->lv_len--;
11278 }
11279 }
11280 }
11281
11282 vim_free(ptrs);
11283 }
11284theend:
11285 sortinfo = old_sortinfo;
11286}
11287
11288/*
11289 * "sort({list})" function
11290 */
11291 static void
11292f_sort(typval_T *argvars, typval_T *rettv)
11293{
11294 do_sort_uniq(argvars, rettv, TRUE);
11295}
11296
11297/*
11298 * "uniq({list})" function
11299 */
11300 static void
11301f_uniq(typval_T *argvars, typval_T *rettv)
11302{
11303 do_sort_uniq(argvars, rettv, FALSE);
11304}
11305
11306/*
11307 * "soundfold({word})" function
11308 */
11309 static void
11310f_soundfold(typval_T *argvars, typval_T *rettv)
11311{
11312 char_u *s;
11313
11314 rettv->v_type = VAR_STRING;
11315 s = get_tv_string(&argvars[0]);
11316#ifdef FEAT_SPELL
11317 rettv->vval.v_string = eval_soundfold(s);
11318#else
11319 rettv->vval.v_string = vim_strsave(s);
11320#endif
11321}
11322
11323/*
11324 * "spellbadword()" function
11325 */
11326 static void
11327f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
11328{
11329 char_u *word = (char_u *)"";
11330 hlf_T attr = HLF_COUNT;
11331 int len = 0;
11332
11333 if (rettv_list_alloc(rettv) == FAIL)
11334 return;
11335
11336#ifdef FEAT_SPELL
11337 if (argvars[0].v_type == VAR_UNKNOWN)
11338 {
11339 /* Find the start and length of the badly spelled word. */
11340 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
11341 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011342 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011343 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011344 curwin->w_set_curswant = TRUE;
11345 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011346 }
11347 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
11348 {
11349 char_u *str = get_tv_string_chk(&argvars[0]);
11350 int capcol = -1;
11351
11352 if (str != NULL)
11353 {
11354 /* Check the argument for spelling. */
11355 while (*str != NUL)
11356 {
11357 len = spell_check(curwin, str, &attr, &capcol, FALSE);
11358 if (attr != HLF_COUNT)
11359 {
11360 word = str;
11361 break;
11362 }
11363 str += len;
11364 }
11365 }
11366 }
11367#endif
11368
11369 list_append_string(rettv->vval.v_list, word, len);
11370 list_append_string(rettv->vval.v_list, (char_u *)(
11371 attr == HLF_SPB ? "bad" :
11372 attr == HLF_SPR ? "rare" :
11373 attr == HLF_SPL ? "local" :
11374 attr == HLF_SPC ? "caps" :
11375 ""), -1);
11376}
11377
11378/*
11379 * "spellsuggest()" function
11380 */
11381 static void
11382f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
11383{
11384#ifdef FEAT_SPELL
11385 char_u *str;
11386 int typeerr = FALSE;
11387 int maxcount;
11388 garray_T ga;
11389 int i;
11390 listitem_T *li;
11391 int need_capital = FALSE;
11392#endif
11393
11394 if (rettv_list_alloc(rettv) == FAIL)
11395 return;
11396
11397#ifdef FEAT_SPELL
11398 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
11399 {
11400 str = get_tv_string(&argvars[0]);
11401 if (argvars[1].v_type != VAR_UNKNOWN)
11402 {
11403 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
11404 if (maxcount <= 0)
11405 return;
11406 if (argvars[2].v_type != VAR_UNKNOWN)
11407 {
11408 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
11409 if (typeerr)
11410 return;
11411 }
11412 }
11413 else
11414 maxcount = 25;
11415
11416 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
11417
11418 for (i = 0; i < ga.ga_len; ++i)
11419 {
11420 str = ((char_u **)ga.ga_data)[i];
11421
11422 li = listitem_alloc();
11423 if (li == NULL)
11424 vim_free(str);
11425 else
11426 {
11427 li->li_tv.v_type = VAR_STRING;
11428 li->li_tv.v_lock = 0;
11429 li->li_tv.vval.v_string = str;
11430 list_append(rettv->vval.v_list, li);
11431 }
11432 }
11433 ga_clear(&ga);
11434 }
11435#endif
11436}
11437
11438 static void
11439f_split(typval_T *argvars, typval_T *rettv)
11440{
11441 char_u *str;
11442 char_u *end;
11443 char_u *pat = NULL;
11444 regmatch_T regmatch;
11445 char_u patbuf[NUMBUFLEN];
11446 char_u *save_cpo;
11447 int match;
11448 colnr_T col = 0;
11449 int keepempty = FALSE;
11450 int typeerr = FALSE;
11451
11452 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11453 save_cpo = p_cpo;
11454 p_cpo = (char_u *)"";
11455
11456 str = get_tv_string(&argvars[0]);
11457 if (argvars[1].v_type != VAR_UNKNOWN)
11458 {
11459 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11460 if (pat == NULL)
11461 typeerr = TRUE;
11462 if (argvars[2].v_type != VAR_UNKNOWN)
11463 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
11464 }
11465 if (pat == NULL || *pat == NUL)
11466 pat = (char_u *)"[\\x01- ]\\+";
11467
11468 if (rettv_list_alloc(rettv) == FAIL)
11469 return;
11470 if (typeerr)
11471 return;
11472
11473 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11474 if (regmatch.regprog != NULL)
11475 {
11476 regmatch.rm_ic = FALSE;
11477 while (*str != NUL || keepempty)
11478 {
11479 if (*str == NUL)
11480 match = FALSE; /* empty item at the end */
11481 else
11482 match = vim_regexec_nl(&regmatch, str, col);
11483 if (match)
11484 end = regmatch.startp[0];
11485 else
11486 end = str + STRLEN(str);
11487 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
11488 && *str != NUL && match && end < regmatch.endp[0]))
11489 {
11490 if (list_append_string(rettv->vval.v_list, str,
11491 (int)(end - str)) == FAIL)
11492 break;
11493 }
11494 if (!match)
11495 break;
11496 /* Advance to just after the match. */
11497 if (regmatch.endp[0] > str)
11498 col = 0;
11499 else
11500 {
11501 /* Don't get stuck at the same match. */
11502#ifdef FEAT_MBYTE
11503 col = (*mb_ptr2len)(regmatch.endp[0]);
11504#else
11505 col = 1;
11506#endif
11507 }
11508 str = regmatch.endp[0];
11509 }
11510
11511 vim_regfree(regmatch.regprog);
11512 }
11513
11514 p_cpo = save_cpo;
11515}
11516
11517#ifdef FEAT_FLOAT
11518/*
11519 * "sqrt()" function
11520 */
11521 static void
11522f_sqrt(typval_T *argvars, typval_T *rettv)
11523{
11524 float_T f = 0.0;
11525
11526 rettv->v_type = VAR_FLOAT;
11527 if (get_float_arg(argvars, &f) == OK)
11528 rettv->vval.v_float = sqrt(f);
11529 else
11530 rettv->vval.v_float = 0.0;
11531}
11532
11533/*
11534 * "str2float()" function
11535 */
11536 static void
11537f_str2float(typval_T *argvars, typval_T *rettv)
11538{
11539 char_u *p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011540 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011541
Bram Moolenaar08243d22017-01-10 16:12:29 +010011542 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011543 p = skipwhite(p + 1);
11544 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011545 if (isneg)
11546 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011547 rettv->v_type = VAR_FLOAT;
11548}
11549#endif
11550
11551/*
11552 * "str2nr()" function
11553 */
11554 static void
11555f_str2nr(typval_T *argvars, typval_T *rettv)
11556{
11557 int base = 10;
11558 char_u *p;
11559 varnumber_T n;
11560 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010011561 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011562
11563 if (argvars[1].v_type != VAR_UNKNOWN)
11564 {
11565 base = (int)get_tv_number(&argvars[1]);
11566 if (base != 2 && base != 8 && base != 10 && base != 16)
11567 {
11568 EMSG(_(e_invarg));
11569 return;
11570 }
11571 }
11572
11573 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011574 isneg = (*p == '-');
11575 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011576 p = skipwhite(p + 1);
11577 switch (base)
11578 {
11579 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
11580 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
11581 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
11582 default: what = 0;
11583 }
11584 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011585 if (isneg)
11586 rettv->vval.v_number = -n;
11587 else
11588 rettv->vval.v_number = n;
11589
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011590}
11591
11592#ifdef HAVE_STRFTIME
11593/*
11594 * "strftime({format}[, {time}])" function
11595 */
11596 static void
11597f_strftime(typval_T *argvars, typval_T *rettv)
11598{
11599 char_u result_buf[256];
11600 struct tm *curtime;
11601 time_t seconds;
11602 char_u *p;
11603
11604 rettv->v_type = VAR_STRING;
11605
11606 p = get_tv_string(&argvars[0]);
11607 if (argvars[1].v_type == VAR_UNKNOWN)
11608 seconds = time(NULL);
11609 else
11610 seconds = (time_t)get_tv_number(&argvars[1]);
11611 curtime = localtime(&seconds);
11612 /* MSVC returns NULL for an invalid value of seconds. */
11613 if (curtime == NULL)
11614 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
11615 else
11616 {
11617# ifdef FEAT_MBYTE
11618 vimconv_T conv;
11619 char_u *enc;
11620
11621 conv.vc_type = CONV_NONE;
11622 enc = enc_locale();
11623 convert_setup(&conv, p_enc, enc);
11624 if (conv.vc_type != CONV_NONE)
11625 p = string_convert(&conv, p, NULL);
11626# endif
11627 if (p != NULL)
11628 (void)strftime((char *)result_buf, sizeof(result_buf),
11629 (char *)p, curtime);
11630 else
11631 result_buf[0] = NUL;
11632
11633# ifdef FEAT_MBYTE
11634 if (conv.vc_type != CONV_NONE)
11635 vim_free(p);
11636 convert_setup(&conv, enc, p_enc);
11637 if (conv.vc_type != CONV_NONE)
11638 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
11639 else
11640# endif
11641 rettv->vval.v_string = vim_strsave(result_buf);
11642
11643# ifdef FEAT_MBYTE
11644 /* Release conversion descriptors */
11645 convert_setup(&conv, NULL, NULL);
11646 vim_free(enc);
11647# endif
11648 }
11649}
11650#endif
11651
11652/*
11653 * "strgetchar()" function
11654 */
11655 static void
11656f_strgetchar(typval_T *argvars, typval_T *rettv)
11657{
11658 char_u *str;
11659 int len;
11660 int error = FALSE;
11661 int charidx;
11662
11663 rettv->vval.v_number = -1;
11664 str = get_tv_string_chk(&argvars[0]);
11665 if (str == NULL)
11666 return;
11667 len = (int)STRLEN(str);
11668 charidx = (int)get_tv_number_chk(&argvars[1], &error);
11669 if (error)
11670 return;
11671#ifdef FEAT_MBYTE
11672 {
11673 int byteidx = 0;
11674
11675 while (charidx >= 0 && byteidx < len)
11676 {
11677 if (charidx == 0)
11678 {
11679 rettv->vval.v_number = mb_ptr2char(str + byteidx);
11680 break;
11681 }
11682 --charidx;
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011683 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011684 }
11685 }
11686#else
11687 if (charidx < len)
11688 rettv->vval.v_number = str[charidx];
11689#endif
11690}
11691
11692/*
11693 * "stridx()" function
11694 */
11695 static void
11696f_stridx(typval_T *argvars, typval_T *rettv)
11697{
11698 char_u buf[NUMBUFLEN];
11699 char_u *needle;
11700 char_u *haystack;
11701 char_u *save_haystack;
11702 char_u *pos;
11703 int start_idx;
11704
11705 needle = get_tv_string_chk(&argvars[1]);
11706 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
11707 rettv->vval.v_number = -1;
11708 if (needle == NULL || haystack == NULL)
11709 return; /* type error; errmsg already given */
11710
11711 if (argvars[2].v_type != VAR_UNKNOWN)
11712 {
11713 int error = FALSE;
11714
11715 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
11716 if (error || start_idx >= (int)STRLEN(haystack))
11717 return;
11718 if (start_idx >= 0)
11719 haystack += start_idx;
11720 }
11721
11722 pos = (char_u *)strstr((char *)haystack, (char *)needle);
11723 if (pos != NULL)
11724 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
11725}
11726
11727/*
11728 * "string()" function
11729 */
11730 static void
11731f_string(typval_T *argvars, typval_T *rettv)
11732{
11733 char_u *tofree;
11734 char_u numbuf[NUMBUFLEN];
11735
11736 rettv->v_type = VAR_STRING;
11737 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
11738 get_copyID());
11739 /* Make a copy if we have a value but it's not in allocated memory. */
11740 if (rettv->vval.v_string != NULL && tofree == NULL)
11741 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
11742}
11743
11744/*
11745 * "strlen()" function
11746 */
11747 static void
11748f_strlen(typval_T *argvars, typval_T *rettv)
11749{
11750 rettv->vval.v_number = (varnumber_T)(STRLEN(
11751 get_tv_string(&argvars[0])));
11752}
11753
11754/*
11755 * "strchars()" function
11756 */
11757 static void
11758f_strchars(typval_T *argvars, typval_T *rettv)
11759{
11760 char_u *s = get_tv_string(&argvars[0]);
11761 int skipcc = 0;
11762#ifdef FEAT_MBYTE
11763 varnumber_T len = 0;
11764 int (*func_mb_ptr2char_adv)(char_u **pp);
11765#endif
11766
11767 if (argvars[1].v_type != VAR_UNKNOWN)
11768 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
11769 if (skipcc < 0 || skipcc > 1)
11770 EMSG(_(e_invarg));
11771 else
11772 {
11773#ifdef FEAT_MBYTE
11774 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
11775 while (*s != NUL)
11776 {
11777 func_mb_ptr2char_adv(&s);
11778 ++len;
11779 }
11780 rettv->vval.v_number = len;
11781#else
11782 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
11783#endif
11784 }
11785}
11786
11787/*
11788 * "strdisplaywidth()" function
11789 */
11790 static void
11791f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
11792{
11793 char_u *s = get_tv_string(&argvars[0]);
11794 int col = 0;
11795
11796 if (argvars[1].v_type != VAR_UNKNOWN)
11797 col = (int)get_tv_number(&argvars[1]);
11798
11799 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
11800}
11801
11802/*
11803 * "strwidth()" function
11804 */
11805 static void
11806f_strwidth(typval_T *argvars, typval_T *rettv)
11807{
11808 char_u *s = get_tv_string(&argvars[0]);
11809
11810 rettv->vval.v_number = (varnumber_T)(
11811#ifdef FEAT_MBYTE
11812 mb_string2cells(s, -1)
11813#else
11814 STRLEN(s)
11815#endif
11816 );
11817}
11818
11819/*
11820 * "strcharpart()" function
11821 */
11822 static void
11823f_strcharpart(typval_T *argvars, typval_T *rettv)
11824{
11825#ifdef FEAT_MBYTE
11826 char_u *p;
11827 int nchar;
11828 int nbyte = 0;
11829 int charlen;
11830 int len = 0;
11831 int slen;
11832 int error = FALSE;
11833
11834 p = get_tv_string(&argvars[0]);
11835 slen = (int)STRLEN(p);
11836
11837 nchar = (int)get_tv_number_chk(&argvars[1], &error);
11838 if (!error)
11839 {
11840 if (nchar > 0)
11841 while (nchar > 0 && nbyte < slen)
11842 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011843 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011844 --nchar;
11845 }
11846 else
11847 nbyte = nchar;
11848 if (argvars[2].v_type != VAR_UNKNOWN)
11849 {
11850 charlen = (int)get_tv_number(&argvars[2]);
11851 while (charlen > 0 && nbyte + len < slen)
11852 {
11853 int off = nbyte + len;
11854
11855 if (off < 0)
11856 len += 1;
11857 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011858 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011859 --charlen;
11860 }
11861 }
11862 else
11863 len = slen - nbyte; /* default: all bytes that are available. */
11864 }
11865
11866 /*
11867 * Only return the overlap between the specified part and the actual
11868 * string.
11869 */
11870 if (nbyte < 0)
11871 {
11872 len += nbyte;
11873 nbyte = 0;
11874 }
11875 else if (nbyte > slen)
11876 nbyte = slen;
11877 if (len < 0)
11878 len = 0;
11879 else if (nbyte + len > slen)
11880 len = slen - nbyte;
11881
11882 rettv->v_type = VAR_STRING;
11883 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
11884#else
11885 f_strpart(argvars, rettv);
11886#endif
11887}
11888
11889/*
11890 * "strpart()" function
11891 */
11892 static void
11893f_strpart(typval_T *argvars, typval_T *rettv)
11894{
11895 char_u *p;
11896 int n;
11897 int len;
11898 int slen;
11899 int error = FALSE;
11900
11901 p = get_tv_string(&argvars[0]);
11902 slen = (int)STRLEN(p);
11903
11904 n = (int)get_tv_number_chk(&argvars[1], &error);
11905 if (error)
11906 len = 0;
11907 else if (argvars[2].v_type != VAR_UNKNOWN)
11908 len = (int)get_tv_number(&argvars[2]);
11909 else
11910 len = slen - n; /* default len: all bytes that are available. */
11911
11912 /*
11913 * Only return the overlap between the specified part and the actual
11914 * string.
11915 */
11916 if (n < 0)
11917 {
11918 len += n;
11919 n = 0;
11920 }
11921 else if (n > slen)
11922 n = slen;
11923 if (len < 0)
11924 len = 0;
11925 else if (n + len > slen)
11926 len = slen - n;
11927
11928 rettv->v_type = VAR_STRING;
11929 rettv->vval.v_string = vim_strnsave(p + n, len);
11930}
11931
11932/*
11933 * "strridx()" function
11934 */
11935 static void
11936f_strridx(typval_T *argvars, typval_T *rettv)
11937{
11938 char_u buf[NUMBUFLEN];
11939 char_u *needle;
11940 char_u *haystack;
11941 char_u *rest;
11942 char_u *lastmatch = NULL;
11943 int haystack_len, end_idx;
11944
11945 needle = get_tv_string_chk(&argvars[1]);
11946 haystack = get_tv_string_buf_chk(&argvars[0], buf);
11947
11948 rettv->vval.v_number = -1;
11949 if (needle == NULL || haystack == NULL)
11950 return; /* type error; errmsg already given */
11951
11952 haystack_len = (int)STRLEN(haystack);
11953 if (argvars[2].v_type != VAR_UNKNOWN)
11954 {
11955 /* Third argument: upper limit for index */
11956 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
11957 if (end_idx < 0)
11958 return; /* can never find a match */
11959 }
11960 else
11961 end_idx = haystack_len;
11962
11963 if (*needle == NUL)
11964 {
11965 /* Empty string matches past the end. */
11966 lastmatch = haystack + end_idx;
11967 }
11968 else
11969 {
11970 for (rest = haystack; *rest != '\0'; ++rest)
11971 {
11972 rest = (char_u *)strstr((char *)rest, (char *)needle);
11973 if (rest == NULL || rest > haystack + end_idx)
11974 break;
11975 lastmatch = rest;
11976 }
11977 }
11978
11979 if (lastmatch == NULL)
11980 rettv->vval.v_number = -1;
11981 else
11982 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
11983}
11984
11985/*
11986 * "strtrans()" function
11987 */
11988 static void
11989f_strtrans(typval_T *argvars, typval_T *rettv)
11990{
11991 rettv->v_type = VAR_STRING;
11992 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
11993}
11994
11995/*
11996 * "submatch()" function
11997 */
11998 static void
11999f_submatch(typval_T *argvars, typval_T *rettv)
12000{
12001 int error = FALSE;
12002 int no;
12003 int retList = 0;
12004
12005 no = (int)get_tv_number_chk(&argvars[0], &error);
12006 if (error)
12007 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012008 if (no < 0 || no >= NSUBEXP)
12009 {
Bram Moolenaar79518e22017-02-17 16:31:35 +010012010 EMSGN(_("E935: invalid submatch number: %d"), no);
12011 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012012 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012013 if (argvars[1].v_type != VAR_UNKNOWN)
12014 retList = (int)get_tv_number_chk(&argvars[1], &error);
12015 if (error)
12016 return;
12017
12018 if (retList == 0)
12019 {
12020 rettv->v_type = VAR_STRING;
12021 rettv->vval.v_string = reg_submatch(no);
12022 }
12023 else
12024 {
12025 rettv->v_type = VAR_LIST;
12026 rettv->vval.v_list = reg_submatch_list(no);
12027 }
12028}
12029
12030/*
12031 * "substitute()" function
12032 */
12033 static void
12034f_substitute(typval_T *argvars, typval_T *rettv)
12035{
12036 char_u patbuf[NUMBUFLEN];
12037 char_u subbuf[NUMBUFLEN];
12038 char_u flagsbuf[NUMBUFLEN];
12039
12040 char_u *str = get_tv_string_chk(&argvars[0]);
12041 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012042 char_u *sub = NULL;
12043 typval_T *expr = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012044 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
12045
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012046 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
12047 expr = &argvars[2];
12048 else
12049 sub = get_tv_string_buf_chk(&argvars[2], subbuf);
12050
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012051 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012052 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
12053 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012054 rettv->vval.v_string = NULL;
12055 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012056 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012057}
12058
12059/*
12060 * "synID(lnum, col, trans)" function
12061 */
12062 static void
12063f_synID(typval_T *argvars UNUSED, typval_T *rettv)
12064{
12065 int id = 0;
12066#ifdef FEAT_SYN_HL
12067 linenr_T lnum;
12068 colnr_T col;
12069 int trans;
12070 int transerr = FALSE;
12071
12072 lnum = get_tv_lnum(argvars); /* -1 on type error */
12073 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12074 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
12075
12076 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12077 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
12078 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
12079#endif
12080
12081 rettv->vval.v_number = id;
12082}
12083
12084/*
12085 * "synIDattr(id, what [, mode])" function
12086 */
12087 static void
12088f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
12089{
12090 char_u *p = NULL;
12091#ifdef FEAT_SYN_HL
12092 int id;
12093 char_u *what;
12094 char_u *mode;
12095 char_u modebuf[NUMBUFLEN];
12096 int modec;
12097
12098 id = (int)get_tv_number(&argvars[0]);
12099 what = get_tv_string(&argvars[1]);
12100 if (argvars[2].v_type != VAR_UNKNOWN)
12101 {
12102 mode = get_tv_string_buf(&argvars[2], modebuf);
12103 modec = TOLOWER_ASC(mode[0]);
12104 if (modec != 't' && modec != 'c' && modec != 'g')
12105 modec = 0; /* replace invalid with current */
12106 }
12107 else
12108 {
12109#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
12110 if (USE_24BIT)
12111 modec = 'g';
12112 else
12113#endif
12114 if (t_colors > 1)
12115 modec = 'c';
12116 else
12117 modec = 't';
12118 }
12119
12120
12121 switch (TOLOWER_ASC(what[0]))
12122 {
12123 case 'b':
12124 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12125 p = highlight_color(id, what, modec);
12126 else /* bold */
12127 p = highlight_has_attr(id, HL_BOLD, modec);
12128 break;
12129
12130 case 'f': /* fg[#] or font */
12131 p = highlight_color(id, what, modec);
12132 break;
12133
12134 case 'i':
12135 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
12136 p = highlight_has_attr(id, HL_INVERSE, modec);
12137 else /* italic */
12138 p = highlight_has_attr(id, HL_ITALIC, modec);
12139 break;
12140
12141 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020012142 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012143 break;
12144
12145 case 'r': /* reverse */
12146 p = highlight_has_attr(id, HL_INVERSE, modec);
12147 break;
12148
12149 case 's':
12150 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
12151 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020012152 /* strikeout */
12153 else if (TOLOWER_ASC(what[1]) == 't' &&
12154 TOLOWER_ASC(what[2]) == 'r')
12155 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012156 else /* standout */
12157 p = highlight_has_attr(id, HL_STANDOUT, modec);
12158 break;
12159
12160 case 'u':
12161 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
12162 /* underline */
12163 p = highlight_has_attr(id, HL_UNDERLINE, modec);
12164 else
12165 /* undercurl */
12166 p = highlight_has_attr(id, HL_UNDERCURL, modec);
12167 break;
12168 }
12169
12170 if (p != NULL)
12171 p = vim_strsave(p);
12172#endif
12173 rettv->v_type = VAR_STRING;
12174 rettv->vval.v_string = p;
12175}
12176
12177/*
12178 * "synIDtrans(id)" function
12179 */
12180 static void
12181f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
12182{
12183 int id;
12184
12185#ifdef FEAT_SYN_HL
12186 id = (int)get_tv_number(&argvars[0]);
12187
12188 if (id > 0)
12189 id = syn_get_final_id(id);
12190 else
12191#endif
12192 id = 0;
12193
12194 rettv->vval.v_number = id;
12195}
12196
12197/*
12198 * "synconcealed(lnum, col)" function
12199 */
12200 static void
12201f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
12202{
12203#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12204 linenr_T lnum;
12205 colnr_T col;
12206 int syntax_flags = 0;
12207 int cchar;
12208 int matchid = 0;
12209 char_u str[NUMBUFLEN];
12210#endif
12211
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012212 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012213
12214#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12215 lnum = get_tv_lnum(argvars); /* -1 on type error */
12216 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12217
12218 vim_memset(str, NUL, sizeof(str));
12219
12220 if (rettv_list_alloc(rettv) != FAIL)
12221 {
12222 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12223 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12224 && curwin->w_p_cole > 0)
12225 {
12226 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
12227 syntax_flags = get_syntax_info(&matchid);
12228
12229 /* get the conceal character */
12230 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
12231 {
12232 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020012233 if (cchar == NUL && curwin->w_p_cole == 1)
12234 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012235 if (cchar != NUL)
12236 {
12237# ifdef FEAT_MBYTE
12238 if (has_mbyte)
12239 (*mb_char2bytes)(cchar, str);
12240 else
12241# endif
12242 str[0] = cchar;
12243 }
12244 }
12245 }
12246
12247 list_append_number(rettv->vval.v_list,
12248 (syntax_flags & HL_CONCEAL) != 0);
12249 /* -1 to auto-determine strlen */
12250 list_append_string(rettv->vval.v_list, str, -1);
12251 list_append_number(rettv->vval.v_list, matchid);
12252 }
12253#endif
12254}
12255
12256/*
12257 * "synstack(lnum, col)" function
12258 */
12259 static void
12260f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
12261{
12262#ifdef FEAT_SYN_HL
12263 linenr_T lnum;
12264 colnr_T col;
12265 int i;
12266 int id;
12267#endif
12268
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012269 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012270
12271#ifdef FEAT_SYN_HL
12272 lnum = get_tv_lnum(argvars); /* -1 on type error */
12273 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12274
12275 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12276 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12277 && rettv_list_alloc(rettv) != FAIL)
12278 {
12279 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
12280 for (i = 0; ; ++i)
12281 {
12282 id = syn_get_stack_item(i);
12283 if (id < 0)
12284 break;
12285 if (list_append_number(rettv->vval.v_list, id) == FAIL)
12286 break;
12287 }
12288 }
12289#endif
12290}
12291
12292 static void
12293get_cmd_output_as_rettv(
12294 typval_T *argvars,
12295 typval_T *rettv,
12296 int retlist)
12297{
12298 char_u *res = NULL;
12299 char_u *p;
12300 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012301 int err = FALSE;
12302 FILE *fd;
12303 list_T *list = NULL;
12304 int flags = SHELL_SILENT;
12305
12306 rettv->v_type = VAR_STRING;
12307 rettv->vval.v_string = NULL;
12308 if (check_restricted() || check_secure())
12309 goto errret;
12310
12311 if (argvars[1].v_type != VAR_UNKNOWN)
12312 {
12313 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010012314 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012315 * command.
12316 */
12317 if ((infile = vim_tempname('i', TRUE)) == NULL)
12318 {
12319 EMSG(_(e_notmp));
12320 goto errret;
12321 }
12322
12323 fd = mch_fopen((char *)infile, WRITEBIN);
12324 if (fd == NULL)
12325 {
12326 EMSG2(_(e_notopen), infile);
12327 goto errret;
12328 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010012329 if (argvars[1].v_type == VAR_NUMBER)
12330 {
12331 linenr_T lnum;
12332 buf_T *buf;
12333
12334 buf = buflist_findnr(argvars[1].vval.v_number);
12335 if (buf == NULL)
12336 {
12337 EMSGN(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010012338 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010012339 goto errret;
12340 }
12341
12342 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
12343 {
12344 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
12345 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
12346 {
12347 err = TRUE;
12348 break;
12349 }
12350 if (putc(NL, fd) == EOF)
12351 {
12352 err = TRUE;
12353 break;
12354 }
12355 }
12356 }
12357 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012358 {
12359 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
12360 err = TRUE;
12361 }
12362 else
12363 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010012364 size_t len;
12365 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012366
12367 p = get_tv_string_buf_chk(&argvars[1], buf);
12368 if (p == NULL)
12369 {
12370 fclose(fd);
12371 goto errret; /* type error; errmsg already given */
12372 }
12373 len = STRLEN(p);
12374 if (len > 0 && fwrite(p, len, 1, fd) != 1)
12375 err = TRUE;
12376 }
12377 if (fclose(fd) != 0)
12378 err = TRUE;
12379 if (err)
12380 {
12381 EMSG(_("E677: Error writing temp file"));
12382 goto errret;
12383 }
12384 }
12385
12386 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
12387 * echoes typeahead, that messes up the display. */
12388 if (!msg_silent)
12389 flags += SHELL_COOKED;
12390
12391 if (retlist)
12392 {
12393 int len;
12394 listitem_T *li;
12395 char_u *s = NULL;
12396 char_u *start;
12397 char_u *end;
12398 int i;
12399
12400 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
12401 if (res == NULL)
12402 goto errret;
12403
12404 list = list_alloc();
12405 if (list == NULL)
12406 goto errret;
12407
12408 for (i = 0; i < len; ++i)
12409 {
12410 start = res + i;
12411 while (i < len && res[i] != NL)
12412 ++i;
12413 end = res + i;
12414
12415 s = alloc((unsigned)(end - start + 1));
12416 if (s == NULL)
12417 goto errret;
12418
12419 for (p = s; start < end; ++p, ++start)
12420 *p = *start == NUL ? NL : *start;
12421 *p = NUL;
12422
12423 li = listitem_alloc();
12424 if (li == NULL)
12425 {
12426 vim_free(s);
12427 goto errret;
12428 }
12429 li->li_tv.v_type = VAR_STRING;
12430 li->li_tv.v_lock = 0;
12431 li->li_tv.vval.v_string = s;
12432 list_append(list, li);
12433 }
12434
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012435 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012436 list = NULL;
12437 }
12438 else
12439 {
12440 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
12441#ifdef USE_CR
12442 /* translate <CR> into <NL> */
12443 if (res != NULL)
12444 {
12445 char_u *s;
12446
12447 for (s = res; *s; ++s)
12448 {
12449 if (*s == CAR)
12450 *s = NL;
12451 }
12452 }
12453#else
12454# ifdef USE_CRNL
12455 /* translate <CR><NL> into <NL> */
12456 if (res != NULL)
12457 {
12458 char_u *s, *d;
12459
12460 d = res;
12461 for (s = res; *s; ++s)
12462 {
12463 if (s[0] == CAR && s[1] == NL)
12464 ++s;
12465 *d++ = *s;
12466 }
12467 *d = NUL;
12468 }
12469# endif
12470#endif
12471 rettv->vval.v_string = res;
12472 res = NULL;
12473 }
12474
12475errret:
12476 if (infile != NULL)
12477 {
12478 mch_remove(infile);
12479 vim_free(infile);
12480 }
12481 if (res != NULL)
12482 vim_free(res);
12483 if (list != NULL)
12484 list_free(list);
12485}
12486
12487/*
12488 * "system()" function
12489 */
12490 static void
12491f_system(typval_T *argvars, typval_T *rettv)
12492{
12493 get_cmd_output_as_rettv(argvars, rettv, FALSE);
12494}
12495
12496/*
12497 * "systemlist()" function
12498 */
12499 static void
12500f_systemlist(typval_T *argvars, typval_T *rettv)
12501{
12502 get_cmd_output_as_rettv(argvars, rettv, TRUE);
12503}
12504
12505/*
12506 * "tabpagebuflist()" function
12507 */
12508 static void
12509f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12510{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012511 tabpage_T *tp;
12512 win_T *wp = NULL;
12513
12514 if (argvars[0].v_type == VAR_UNKNOWN)
12515 wp = firstwin;
12516 else
12517 {
12518 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12519 if (tp != NULL)
12520 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12521 }
12522 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
12523 {
12524 for (; wp != NULL; wp = wp->w_next)
12525 if (list_append_number(rettv->vval.v_list,
12526 wp->w_buffer->b_fnum) == FAIL)
12527 break;
12528 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012529}
12530
12531
12532/*
12533 * "tabpagenr()" function
12534 */
12535 static void
12536f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
12537{
12538 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012539 char_u *arg;
12540
12541 if (argvars[0].v_type != VAR_UNKNOWN)
12542 {
12543 arg = get_tv_string_chk(&argvars[0]);
12544 nr = 0;
12545 if (arg != NULL)
12546 {
12547 if (STRCMP(arg, "$") == 0)
12548 nr = tabpage_index(NULL) - 1;
12549 else
12550 EMSG2(_(e_invexpr2), arg);
12551 }
12552 }
12553 else
12554 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012555 rettv->vval.v_number = nr;
12556}
12557
12558
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012559static int get_winnr(tabpage_T *tp, typval_T *argvar);
12560
12561/*
12562 * Common code for tabpagewinnr() and winnr().
12563 */
12564 static int
12565get_winnr(tabpage_T *tp, typval_T *argvar)
12566{
12567 win_T *twin;
12568 int nr = 1;
12569 win_T *wp;
12570 char_u *arg;
12571
12572 twin = (tp == curtab) ? curwin : tp->tp_curwin;
12573 if (argvar->v_type != VAR_UNKNOWN)
12574 {
12575 arg = get_tv_string_chk(argvar);
12576 if (arg == NULL)
12577 nr = 0; /* type error; errmsg already given */
12578 else if (STRCMP(arg, "$") == 0)
12579 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
12580 else if (STRCMP(arg, "#") == 0)
12581 {
12582 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
12583 if (twin == NULL)
12584 nr = 0;
12585 }
12586 else
12587 {
12588 EMSG2(_(e_invexpr2), arg);
12589 nr = 0;
12590 }
12591 }
12592
12593 if (nr > 0)
12594 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12595 wp != twin; wp = wp->w_next)
12596 {
12597 if (wp == NULL)
12598 {
12599 /* didn't find it in this tabpage */
12600 nr = 0;
12601 break;
12602 }
12603 ++nr;
12604 }
12605 return nr;
12606}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012607
12608/*
12609 * "tabpagewinnr()" function
12610 */
12611 static void
12612f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
12613{
12614 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012615 tabpage_T *tp;
12616
12617 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12618 if (tp == NULL)
12619 nr = 0;
12620 else
12621 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012622 rettv->vval.v_number = nr;
12623}
12624
12625
12626/*
12627 * "tagfiles()" function
12628 */
12629 static void
12630f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
12631{
12632 char_u *fname;
12633 tagname_T tn;
12634 int first;
12635
12636 if (rettv_list_alloc(rettv) == FAIL)
12637 return;
12638 fname = alloc(MAXPATHL);
12639 if (fname == NULL)
12640 return;
12641
12642 for (first = TRUE; ; first = FALSE)
12643 if (get_tagfname(&tn, first, fname) == FAIL
12644 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
12645 break;
12646 tagname_free(&tn);
12647 vim_free(fname);
12648}
12649
12650/*
12651 * "taglist()" function
12652 */
12653 static void
12654f_taglist(typval_T *argvars, typval_T *rettv)
12655{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012656 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012657 char_u *tag_pattern;
12658
12659 tag_pattern = get_tv_string(&argvars[0]);
12660
12661 rettv->vval.v_number = FALSE;
12662 if (*tag_pattern == NUL)
12663 return;
12664
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012665 if (argvars[1].v_type != VAR_UNKNOWN)
12666 fname = get_tv_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012667 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012668 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012669}
12670
12671/*
12672 * "tempname()" function
12673 */
12674 static void
12675f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
12676{
12677 static int x = 'A';
12678
12679 rettv->v_type = VAR_STRING;
12680 rettv->vval.v_string = vim_tempname(x, FALSE);
12681
12682 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
12683 * names. Skip 'I' and 'O', they are used for shell redirection. */
12684 do
12685 {
12686 if (x == 'Z')
12687 x = '0';
12688 else if (x == '9')
12689 x = 'A';
12690 else
12691 {
12692#ifdef EBCDIC
12693 if (x == 'I')
12694 x = 'J';
12695 else if (x == 'R')
12696 x = 'S';
12697 else
12698#endif
12699 ++x;
12700 }
12701 } while (x == 'I' || x == 'O');
12702}
12703
12704#ifdef FEAT_FLOAT
12705/*
12706 * "tan()" function
12707 */
12708 static void
12709f_tan(typval_T *argvars, typval_T *rettv)
12710{
12711 float_T f = 0.0;
12712
12713 rettv->v_type = VAR_FLOAT;
12714 if (get_float_arg(argvars, &f) == OK)
12715 rettv->vval.v_float = tan(f);
12716 else
12717 rettv->vval.v_float = 0.0;
12718}
12719
12720/*
12721 * "tanh()" function
12722 */
12723 static void
12724f_tanh(typval_T *argvars, typval_T *rettv)
12725{
12726 float_T f = 0.0;
12727
12728 rettv->v_type = VAR_FLOAT;
12729 if (get_float_arg(argvars, &f) == OK)
12730 rettv->vval.v_float = tanh(f);
12731 else
12732 rettv->vval.v_float = 0.0;
12733}
12734#endif
12735
12736/*
12737 * "test_alloc_fail(id, countdown, repeat)" function
12738 */
12739 static void
12740f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
12741{
12742 if (argvars[0].v_type != VAR_NUMBER
12743 || argvars[0].vval.v_number <= 0
12744 || argvars[1].v_type != VAR_NUMBER
12745 || argvars[1].vval.v_number < 0
12746 || argvars[2].v_type != VAR_NUMBER)
12747 EMSG(_(e_invarg));
12748 else
12749 {
12750 alloc_fail_id = argvars[0].vval.v_number;
12751 if (alloc_fail_id >= aid_last)
12752 EMSG(_(e_invarg));
12753 alloc_fail_countdown = argvars[1].vval.v_number;
12754 alloc_fail_repeat = argvars[2].vval.v_number;
12755 did_outofmem_msg = FALSE;
12756 }
12757}
12758
12759/*
12760 * "test_autochdir()"
12761 */
12762 static void
12763f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12764{
12765#if defined(FEAT_AUTOCHDIR)
12766 test_autochdir = TRUE;
12767#endif
12768}
12769
12770/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020012771 * "test_feedinput()"
12772 */
12773 static void
12774f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
12775{
12776#ifdef USE_INPUT_BUF
12777 char_u *val = get_tv_string_chk(&argvars[0]);
12778
12779 if (val != NULL)
12780 {
12781 trash_input_buf();
12782 add_to_input_buf_csi(val, (int)STRLEN(val));
12783 }
12784#endif
12785}
12786
12787/*
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012788 * "test_disable({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012789 */
12790 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012791f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012792{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012793 char_u *name = (char_u *)"";
12794 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012795 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012796
12797 if (argvars[0].v_type != VAR_STRING
12798 || (argvars[1].v_type) != VAR_NUMBER)
12799 EMSG(_(e_invarg));
12800 else
12801 {
12802 name = get_tv_string_chk(&argvars[0]);
12803 val = (int)get_tv_number(&argvars[1]);
12804
12805 if (STRCMP(name, (char_u *)"redraw") == 0)
12806 disable_redraw_for_testing = val;
12807 else if (STRCMP(name, (char_u *)"char_avail") == 0)
12808 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012809 else if (STRCMP(name, (char_u *)"starting") == 0)
12810 {
12811 if (val)
12812 {
12813 if (save_starting < 0)
12814 save_starting = starting;
12815 starting = 0;
12816 }
12817 else
12818 {
12819 starting = save_starting;
12820 save_starting = -1;
12821 }
12822 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012823 else if (STRCMP(name, (char_u *)"ALL") == 0)
12824 {
12825 disable_char_avail_for_testing = FALSE;
12826 disable_redraw_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012827 if (save_starting >= 0)
12828 {
12829 starting = save_starting;
12830 save_starting = -1;
12831 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012832 }
12833 else
12834 EMSG2(_(e_invarg2), name);
12835 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012836}
12837
12838/*
12839 * "test_garbagecollect_now()" function
12840 */
12841 static void
12842f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12843{
12844 /* This is dangerous, any Lists and Dicts used internally may be freed
12845 * while still in use. */
12846 garbage_collect(TRUE);
12847}
12848
Bram Moolenaare0c31f62017-03-01 15:07:05 +010012849/*
12850 * "test_ignore_error()" function
12851 */
12852 static void
12853f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
12854{
12855 ignore_error_for_testing(get_tv_string(&argvars[0]));
12856}
12857
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012858#ifdef FEAT_JOB_CHANNEL
12859 static void
12860f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
12861{
12862 rettv->v_type = VAR_CHANNEL;
12863 rettv->vval.v_channel = NULL;
12864}
12865#endif
12866
12867 static void
12868f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
12869{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012870 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012871}
12872
12873#ifdef FEAT_JOB_CHANNEL
12874 static void
12875f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
12876{
12877 rettv->v_type = VAR_JOB;
12878 rettv->vval.v_job = NULL;
12879}
12880#endif
12881
12882 static void
12883f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
12884{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012885 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012886}
12887
12888 static void
12889f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
12890{
12891 rettv->v_type = VAR_PARTIAL;
12892 rettv->vval.v_partial = NULL;
12893}
12894
12895 static void
12896f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
12897{
12898 rettv->v_type = VAR_STRING;
12899 rettv->vval.v_string = NULL;
12900}
12901
12902 static void
12903f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
12904{
12905 time_for_testing = (time_t)get_tv_number(&argvars[0]);
12906}
12907
12908#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
12909/*
12910 * Get a callback from "arg". It can be a Funcref or a function name.
12911 * When "arg" is zero return an empty string.
12912 * Return NULL for an invalid argument.
12913 */
12914 char_u *
12915get_callback(typval_T *arg, partial_T **pp)
12916{
12917 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
12918 {
12919 *pp = arg->vval.v_partial;
12920 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012921 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012922 }
12923 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012924 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012925 {
12926 func_ref(arg->vval.v_string);
12927 return arg->vval.v_string;
12928 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012929 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
12930 return (char_u *)"";
12931 EMSG(_("E921: Invalid callback argument"));
12932 return NULL;
12933}
12934
12935/*
Bram Moolenaard5359b22018-04-05 22:44:39 +020012936 * Unref/free "callback" and "partial" returned by get_callback().
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012937 */
12938 void
12939free_callback(char_u *callback, partial_T *partial)
12940{
12941 if (partial != NULL)
12942 partial_unref(partial);
12943 else if (callback != NULL)
12944 {
12945 func_unref(callback);
12946 vim_free(callback);
12947 }
12948}
12949#endif
12950
12951#ifdef FEAT_TIMERS
12952/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020012953 * "timer_info([timer])" function
12954 */
12955 static void
12956f_timer_info(typval_T *argvars, typval_T *rettv)
12957{
12958 timer_T *timer = NULL;
12959
12960 if (rettv_list_alloc(rettv) != OK)
12961 return;
12962 if (argvars[0].v_type != VAR_UNKNOWN)
12963 {
12964 if (argvars[0].v_type != VAR_NUMBER)
12965 EMSG(_(e_number_exp));
12966 else
12967 {
12968 timer = find_timer((int)get_tv_number(&argvars[0]));
12969 if (timer != NULL)
12970 add_timer_info(rettv, timer);
12971 }
12972 }
12973 else
12974 add_timer_info_all(rettv);
12975}
12976
12977/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012978 * "timer_pause(timer, paused)" function
12979 */
12980 static void
12981f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
12982{
12983 timer_T *timer = NULL;
12984 int paused = (int)get_tv_number(&argvars[1]);
12985
12986 if (argvars[0].v_type != VAR_NUMBER)
12987 EMSG(_(e_number_exp));
12988 else
12989 {
12990 timer = find_timer((int)get_tv_number(&argvars[0]));
12991 if (timer != NULL)
12992 timer->tr_paused = paused;
12993 }
12994}
12995
12996/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012997 * "timer_start(time, callback [, options])" function
12998 */
12999 static void
13000f_timer_start(typval_T *argvars, typval_T *rettv)
13001{
Bram Moolenaar75537a92016-09-05 22:45:28 +020013002 long msec = (long)get_tv_number(&argvars[0]);
13003 timer_T *timer;
13004 int repeat = 0;
13005 char_u *callback;
13006 dict_T *dict;
13007 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013008
Bram Moolenaar75537a92016-09-05 22:45:28 +020013009 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013010 if (check_secure())
13011 return;
13012 if (argvars[2].v_type != VAR_UNKNOWN)
13013 {
13014 if (argvars[2].v_type != VAR_DICT
13015 || (dict = argvars[2].vval.v_dict) == NULL)
13016 {
13017 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
13018 return;
13019 }
13020 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
13021 repeat = get_dict_number(dict, (char_u *)"repeat");
13022 }
13023
Bram Moolenaar75537a92016-09-05 22:45:28 +020013024 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013025 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020013026 return;
13027
13028 timer = create_timer(msec, repeat);
13029 if (timer == NULL)
13030 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013031 else
13032 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020013033 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020013034 timer->tr_callback = vim_strsave(callback);
13035 else
13036 /* pointer into the partial */
13037 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020013038 timer->tr_partial = partial;
13039 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013040 }
13041}
13042
13043/*
13044 * "timer_stop(timer)" function
13045 */
13046 static void
13047f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
13048{
13049 timer_T *timer;
13050
13051 if (argvars[0].v_type != VAR_NUMBER)
13052 {
13053 EMSG(_(e_number_exp));
13054 return;
13055 }
13056 timer = find_timer((int)get_tv_number(&argvars[0]));
13057 if (timer != NULL)
13058 stop_timer(timer);
13059}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013060
13061/*
13062 * "timer_stopall()" function
13063 */
13064 static void
13065f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13066{
13067 stop_all_timers();
13068}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013069#endif
13070
13071/*
13072 * "tolower(string)" function
13073 */
13074 static void
13075f_tolower(typval_T *argvars, typval_T *rettv)
13076{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013077 rettv->v_type = VAR_STRING;
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +010013078 rettv->vval.v_string = strlow_save(get_tv_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013079}
13080
13081/*
13082 * "toupper(string)" function
13083 */
13084 static void
13085f_toupper(typval_T *argvars, typval_T *rettv)
13086{
13087 rettv->v_type = VAR_STRING;
13088 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
13089}
13090
13091/*
13092 * "tr(string, fromstr, tostr)" function
13093 */
13094 static void
13095f_tr(typval_T *argvars, typval_T *rettv)
13096{
13097 char_u *in_str;
13098 char_u *fromstr;
13099 char_u *tostr;
13100 char_u *p;
13101#ifdef FEAT_MBYTE
13102 int inlen;
13103 int fromlen;
13104 int tolen;
13105 int idx;
13106 char_u *cpstr;
13107 int cplen;
13108 int first = TRUE;
13109#endif
13110 char_u buf[NUMBUFLEN];
13111 char_u buf2[NUMBUFLEN];
13112 garray_T ga;
13113
13114 in_str = get_tv_string(&argvars[0]);
13115 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
13116 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
13117
13118 /* Default return value: empty string. */
13119 rettv->v_type = VAR_STRING;
13120 rettv->vval.v_string = NULL;
13121 if (fromstr == NULL || tostr == NULL)
13122 return; /* type error; errmsg already given */
13123 ga_init2(&ga, (int)sizeof(char), 80);
13124
13125#ifdef FEAT_MBYTE
13126 if (!has_mbyte)
13127#endif
13128 /* not multi-byte: fromstr and tostr must be the same length */
13129 if (STRLEN(fromstr) != STRLEN(tostr))
13130 {
13131#ifdef FEAT_MBYTE
13132error:
13133#endif
13134 EMSG2(_(e_invarg2), fromstr);
13135 ga_clear(&ga);
13136 return;
13137 }
13138
13139 /* fromstr and tostr have to contain the same number of chars */
13140 while (*in_str != NUL)
13141 {
13142#ifdef FEAT_MBYTE
13143 if (has_mbyte)
13144 {
13145 inlen = (*mb_ptr2len)(in_str);
13146 cpstr = in_str;
13147 cplen = inlen;
13148 idx = 0;
13149 for (p = fromstr; *p != NUL; p += fromlen)
13150 {
13151 fromlen = (*mb_ptr2len)(p);
13152 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
13153 {
13154 for (p = tostr; *p != NUL; p += tolen)
13155 {
13156 tolen = (*mb_ptr2len)(p);
13157 if (idx-- == 0)
13158 {
13159 cplen = tolen;
13160 cpstr = p;
13161 break;
13162 }
13163 }
13164 if (*p == NUL) /* tostr is shorter than fromstr */
13165 goto error;
13166 break;
13167 }
13168 ++idx;
13169 }
13170
13171 if (first && cpstr == in_str)
13172 {
13173 /* Check that fromstr and tostr have the same number of
13174 * (multi-byte) characters. Done only once when a character
13175 * of in_str doesn't appear in fromstr. */
13176 first = FALSE;
13177 for (p = tostr; *p != NUL; p += tolen)
13178 {
13179 tolen = (*mb_ptr2len)(p);
13180 --idx;
13181 }
13182 if (idx != 0)
13183 goto error;
13184 }
13185
13186 (void)ga_grow(&ga, cplen);
13187 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
13188 ga.ga_len += cplen;
13189
13190 in_str += inlen;
13191 }
13192 else
13193#endif
13194 {
13195 /* When not using multi-byte chars we can do it faster. */
13196 p = vim_strchr(fromstr, *in_str);
13197 if (p != NULL)
13198 ga_append(&ga, tostr[p - fromstr]);
13199 else
13200 ga_append(&ga, *in_str);
13201 ++in_str;
13202 }
13203 }
13204
13205 /* add a terminating NUL */
13206 (void)ga_grow(&ga, 1);
13207 ga_append(&ga, NUL);
13208
13209 rettv->vval.v_string = ga.ga_data;
13210}
13211
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010013212/*
13213 * "trim({expr})" function
13214 */
13215 static void
13216f_trim(typval_T *argvars, typval_T *rettv)
13217{
13218 char_u buf1[NUMBUFLEN];
13219 char_u buf2[NUMBUFLEN];
13220 char_u *head = get_tv_string_buf_chk(&argvars[0], buf1);
13221 char_u *mask = NULL;
13222 char_u *tail;
13223 char_u *prev;
13224 char_u *p;
13225 int c1;
13226
13227 rettv->v_type = VAR_STRING;
13228 if (head == NULL)
13229 {
13230 rettv->vval.v_string = NULL;
13231 return;
13232 }
13233
13234 if (argvars[1].v_type == VAR_STRING)
13235 mask = get_tv_string_buf_chk(&argvars[1], buf2);
13236
13237 while (*head != NUL)
13238 {
13239 c1 = PTR2CHAR(head);
13240 if (mask == NULL)
13241 {
13242 if (c1 > ' ' && c1 != 0xa0)
13243 break;
13244 }
13245 else
13246 {
13247 for (p = mask; *p != NUL; MB_PTR_ADV(p))
13248 if (c1 == PTR2CHAR(p))
13249 break;
13250 if (*p == NUL)
13251 break;
13252 }
13253 MB_PTR_ADV(head);
13254 }
13255
13256 for (tail = head + STRLEN(head); tail > head; tail = prev)
13257 {
13258 prev = tail;
13259 MB_PTR_BACK(head, prev);
13260 c1 = PTR2CHAR(prev);
13261 if (mask == NULL)
13262 {
13263 if (c1 > ' ' && c1 != 0xa0)
13264 break;
13265 }
13266 else
13267 {
13268 for (p = mask; *p != NUL; MB_PTR_ADV(p))
13269 if (c1 == PTR2CHAR(p))
13270 break;
13271 if (*p == NUL)
13272 break;
13273 }
13274 }
13275 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
13276}
13277
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013278#ifdef FEAT_FLOAT
13279/*
13280 * "trunc({float})" function
13281 */
13282 static void
13283f_trunc(typval_T *argvars, typval_T *rettv)
13284{
13285 float_T f = 0.0;
13286
13287 rettv->v_type = VAR_FLOAT;
13288 if (get_float_arg(argvars, &f) == OK)
13289 /* trunc() is not in C90, use floor() or ceil() instead. */
13290 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
13291 else
13292 rettv->vval.v_float = 0.0;
13293}
13294#endif
13295
13296/*
13297 * "type(expr)" function
13298 */
13299 static void
13300f_type(typval_T *argvars, typval_T *rettv)
13301{
13302 int n = -1;
13303
13304 switch (argvars[0].v_type)
13305 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020013306 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
13307 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013308 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020013309 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
13310 case VAR_LIST: n = VAR_TYPE_LIST; break;
13311 case VAR_DICT: n = VAR_TYPE_DICT; break;
13312 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013313 case VAR_SPECIAL:
13314 if (argvars[0].vval.v_number == VVAL_FALSE
13315 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020013316 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013317 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020013318 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013319 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020013320 case VAR_JOB: n = VAR_TYPE_JOB; break;
13321 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013322 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010013323 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013324 n = -1;
13325 break;
13326 }
13327 rettv->vval.v_number = n;
13328}
13329
13330/*
13331 * "undofile(name)" function
13332 */
13333 static void
13334f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
13335{
13336 rettv->v_type = VAR_STRING;
13337#ifdef FEAT_PERSISTENT_UNDO
13338 {
13339 char_u *fname = get_tv_string(&argvars[0]);
13340
13341 if (*fname == NUL)
13342 {
13343 /* If there is no file name there will be no undo file. */
13344 rettv->vval.v_string = NULL;
13345 }
13346 else
13347 {
13348 char_u *ffname = FullName_save(fname, FALSE);
13349
13350 if (ffname != NULL)
13351 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
13352 vim_free(ffname);
13353 }
13354 }
13355#else
13356 rettv->vval.v_string = NULL;
13357#endif
13358}
13359
13360/*
13361 * "undotree()" function
13362 */
13363 static void
13364f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
13365{
13366 if (rettv_dict_alloc(rettv) == OK)
13367 {
13368 dict_T *dict = rettv->vval.v_dict;
13369 list_T *list;
13370
13371 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
13372 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
13373 dict_add_nr_str(dict, "save_last",
13374 (long)curbuf->b_u_save_nr_last, NULL);
13375 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
13376 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
13377 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
13378
13379 list = list_alloc();
13380 if (list != NULL)
13381 {
13382 u_eval_tree(curbuf->b_u_oldhead, list);
13383 dict_add_list(dict, "entries", list);
13384 }
13385 }
13386}
13387
13388/*
13389 * "values(dict)" function
13390 */
13391 static void
13392f_values(typval_T *argvars, typval_T *rettv)
13393{
13394 dict_list(argvars, rettv, 1);
13395}
13396
13397/*
13398 * "virtcol(string)" function
13399 */
13400 static void
13401f_virtcol(typval_T *argvars, typval_T *rettv)
13402{
13403 colnr_T vcol = 0;
13404 pos_T *fp;
13405 int fnum = curbuf->b_fnum;
13406
13407 fp = var2fpos(&argvars[0], FALSE, &fnum);
13408 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
13409 && fnum == curbuf->b_fnum)
13410 {
13411 getvvcol(curwin, fp, NULL, NULL, &vcol);
13412 ++vcol;
13413 }
13414
13415 rettv->vval.v_number = vcol;
13416}
13417
13418/*
13419 * "visualmode()" function
13420 */
13421 static void
13422f_visualmode(typval_T *argvars, typval_T *rettv)
13423{
13424 char_u str[2];
13425
13426 rettv->v_type = VAR_STRING;
13427 str[0] = curbuf->b_visual_mode_eval;
13428 str[1] = NUL;
13429 rettv->vval.v_string = vim_strsave(str);
13430
13431 /* A non-zero number or non-empty string argument: reset mode. */
13432 if (non_zero_arg(&argvars[0]))
13433 curbuf->b_visual_mode_eval = NUL;
13434}
13435
13436/*
13437 * "wildmenumode()" function
13438 */
13439 static void
13440f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13441{
13442#ifdef FEAT_WILDMENU
13443 if (wild_menu_showing)
13444 rettv->vval.v_number = 1;
13445#endif
13446}
13447
13448/*
13449 * "winbufnr(nr)" function
13450 */
13451 static void
13452f_winbufnr(typval_T *argvars, typval_T *rettv)
13453{
13454 win_T *wp;
13455
13456 wp = find_win_by_nr(&argvars[0], NULL);
13457 if (wp == NULL)
13458 rettv->vval.v_number = -1;
13459 else
13460 rettv->vval.v_number = wp->w_buffer->b_fnum;
13461}
13462
13463/*
13464 * "wincol()" function
13465 */
13466 static void
13467f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
13468{
13469 validate_cursor();
13470 rettv->vval.v_number = curwin->w_wcol + 1;
13471}
13472
13473/*
13474 * "winheight(nr)" function
13475 */
13476 static void
13477f_winheight(typval_T *argvars, typval_T *rettv)
13478{
13479 win_T *wp;
13480
13481 wp = find_win_by_nr(&argvars[0], NULL);
13482 if (wp == NULL)
13483 rettv->vval.v_number = -1;
13484 else
13485 rettv->vval.v_number = wp->w_height;
13486}
13487
13488/*
13489 * "winline()" function
13490 */
13491 static void
13492f_winline(typval_T *argvars UNUSED, typval_T *rettv)
13493{
13494 validate_cursor();
13495 rettv->vval.v_number = curwin->w_wrow + 1;
13496}
13497
13498/*
13499 * "winnr()" function
13500 */
13501 static void
13502f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
13503{
13504 int nr = 1;
13505
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013506 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013507 rettv->vval.v_number = nr;
13508}
13509
13510/*
13511 * "winrestcmd()" function
13512 */
13513 static void
13514f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
13515{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013516 win_T *wp;
13517 int winnr = 1;
13518 garray_T ga;
13519 char_u buf[50];
13520
13521 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020013522 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013523 {
13524 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
13525 ga_concat(&ga, buf);
13526 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
13527 ga_concat(&ga, buf);
13528 ++winnr;
13529 }
13530 ga_append(&ga, NUL);
13531
13532 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013533 rettv->v_type = VAR_STRING;
13534}
13535
13536/*
13537 * "winrestview()" function
13538 */
13539 static void
13540f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
13541{
13542 dict_T *dict;
13543
13544 if (argvars[0].v_type != VAR_DICT
13545 || (dict = argvars[0].vval.v_dict) == NULL)
13546 EMSG(_(e_invarg));
13547 else
13548 {
13549 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
13550 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
13551 if (dict_find(dict, (char_u *)"col", -1) != NULL)
13552 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
13553#ifdef FEAT_VIRTUALEDIT
13554 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
13555 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
13556#endif
13557 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
13558 {
13559 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
13560 curwin->w_set_curswant = FALSE;
13561 }
13562
13563 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
13564 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
13565#ifdef FEAT_DIFF
13566 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
13567 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
13568#endif
13569 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
13570 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
13571 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
13572 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
13573
13574 check_cursor();
13575 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020013576 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013577 changed_window_setting();
13578
13579 if (curwin->w_topline <= 0)
13580 curwin->w_topline = 1;
13581 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
13582 curwin->w_topline = curbuf->b_ml.ml_line_count;
13583#ifdef FEAT_DIFF
13584 check_topfill(curwin, TRUE);
13585#endif
13586 }
13587}
13588
13589/*
13590 * "winsaveview()" function
13591 */
13592 static void
13593f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
13594{
13595 dict_T *dict;
13596
13597 if (rettv_dict_alloc(rettv) == FAIL)
13598 return;
13599 dict = rettv->vval.v_dict;
13600
13601 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
13602 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
13603#ifdef FEAT_VIRTUALEDIT
13604 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
13605#endif
13606 update_curswant();
13607 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
13608
13609 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
13610#ifdef FEAT_DIFF
13611 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
13612#endif
13613 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
13614 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
13615}
13616
13617/*
13618 * "winwidth(nr)" function
13619 */
13620 static void
13621f_winwidth(typval_T *argvars, typval_T *rettv)
13622{
13623 win_T *wp;
13624
13625 wp = find_win_by_nr(&argvars[0], NULL);
13626 if (wp == NULL)
13627 rettv->vval.v_number = -1;
13628 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013629 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013630}
13631
13632/*
13633 * "wordcount()" function
13634 */
13635 static void
13636f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
13637{
13638 if (rettv_dict_alloc(rettv) == FAIL)
13639 return;
13640 cursor_pos_info(rettv->vval.v_dict);
13641}
13642
13643/*
13644 * "writefile()" function
13645 */
13646 static void
13647f_writefile(typval_T *argvars, typval_T *rettv)
13648{
13649 int binary = FALSE;
13650 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013651#ifdef HAVE_FSYNC
13652 int do_fsync = p_fs;
13653#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013654 char_u *fname;
13655 FILE *fd;
13656 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013657 listitem_T *li;
13658 list_T *list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013659
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013660 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013661 if (check_restricted() || check_secure())
13662 return;
13663
13664 if (argvars[0].v_type != VAR_LIST)
13665 {
13666 EMSG2(_(e_listarg), "writefile()");
13667 return;
13668 }
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013669 list = argvars[0].vval.v_list;
13670 if (list == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013671 return;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013672 for (li = list->lv_first; li != NULL; li = li->li_next)
13673 if (get_tv_string_chk(&li->li_tv) == NULL)
13674 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013675
13676 if (argvars[2].v_type != VAR_UNKNOWN)
13677 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013678 char_u *arg2 = get_tv_string_chk(&argvars[2]);
13679
13680 if (arg2 == NULL)
13681 return;
13682 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013683 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013684 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013685 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013686#ifdef HAVE_FSYNC
13687 if (vim_strchr(arg2, 's') != NULL)
13688 do_fsync = TRUE;
13689 else if (vim_strchr(arg2, 'S') != NULL)
13690 do_fsync = FALSE;
13691#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013692 }
13693
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013694 fname = get_tv_string_chk(&argvars[1]);
13695 if (fname == NULL)
13696 return;
13697
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013698 /* Always open the file in binary mode, library functions have a mind of
13699 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013700 if (*fname == NUL || (fd = mch_fopen((char *)fname,
13701 append ? APPENDBIN : WRITEBIN)) == NULL)
13702 {
13703 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
13704 ret = -1;
13705 }
13706 else
13707 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013708 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013709 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013710#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010013711 else if (do_fsync)
13712 /* Ignore the error, the user wouldn't know what to do about it.
13713 * May happen for a device. */
13714 ignored = fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013715#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013716 fclose(fd);
13717 }
13718
13719 rettv->vval.v_number = ret;
13720}
13721
13722/*
13723 * "xor(expr, expr)" function
13724 */
13725 static void
13726f_xor(typval_T *argvars, typval_T *rettv)
13727{
13728 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
13729 ^ get_tv_number_chk(&argvars[1], NULL);
13730}
13731
13732
13733#endif /* FEAT_EVAL */