blob: 4b09951852b0aa740f979d23d754379ccdbc5b82 [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)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008060 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008061
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008062 if (*gettail(dir) == NUL)
8063 /* remove trailing slashes */
8064 *gettail_sep(dir) = NUL;
8065
8066 if (argvars[1].v_type != VAR_UNKNOWN)
8067 {
8068 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008069 {
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008070 prot = (int)get_tv_number_chk(&argvars[2], NULL);
8071 if (prot == -1)
8072 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008073 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008074 if (STRCMP(get_tv_string(&argvars[1]), "p") == 0)
8075 {
8076 if (mch_isdir(dir))
8077 {
8078 /* With the "p" flag it's OK if the dir already exists. */
8079 rettv->vval.v_number = OK;
8080 return;
8081 }
8082 mkdir_recurse(dir, prot);
8083 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008084 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008085 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008086}
8087#endif
8088
8089/*
8090 * "mode()" function
8091 */
8092 static void
8093f_mode(typval_T *argvars, typval_T *rettv)
8094{
8095 char_u buf[3];
8096
8097 buf[1] = NUL;
8098 buf[2] = NUL;
8099
8100 if (time_for_testing == 93784)
8101 {
8102 /* Testing the two-character code. */
8103 buf[0] = 'x';
8104 buf[1] = '!';
8105 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008106#ifdef FEAT_TERMINAL
8107 else if (term_use_loop())
8108 buf[0] = 't';
8109#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008110 else if (VIsual_active)
8111 {
8112 if (VIsual_select)
8113 buf[0] = VIsual_mode + 's' - 'v';
8114 else
8115 buf[0] = VIsual_mode;
8116 }
8117 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8118 || State == CONFIRM)
8119 {
8120 buf[0] = 'r';
8121 if (State == ASKMORE)
8122 buf[1] = 'm';
8123 else if (State == CONFIRM)
8124 buf[1] = '?';
8125 }
8126 else if (State == EXTERNCMD)
8127 buf[0] = '!';
8128 else if (State & INSERT)
8129 {
8130#ifdef FEAT_VREPLACE
8131 if (State & VREPLACE_FLAG)
8132 {
8133 buf[0] = 'R';
8134 buf[1] = 'v';
8135 }
8136 else
8137#endif
Bram Moolenaare90858d2017-02-01 17:24:34 +01008138 {
8139 if (State & REPLACE_FLAG)
8140 buf[0] = 'R';
8141 else
8142 buf[0] = 'i';
8143#ifdef FEAT_INS_EXPAND
8144 if (ins_compl_active())
8145 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008146 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008147 buf[1] = 'x';
8148#endif
8149 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008150 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008151 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008152 {
8153 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008154 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008155 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008156 else if (exmode_active == EXMODE_NORMAL)
8157 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008158 }
8159 else
8160 {
8161 buf[0] = 'n';
8162 if (finish_op)
8163 buf[1] = 'o';
8164 }
8165
8166 /* Clear out the minor mode when the argument is not a non-zero number or
8167 * non-empty string. */
8168 if (!non_zero_arg(&argvars[0]))
8169 buf[1] = NUL;
8170
8171 rettv->vval.v_string = vim_strsave(buf);
8172 rettv->v_type = VAR_STRING;
8173}
8174
8175#if defined(FEAT_MZSCHEME) || defined(PROTO)
8176/*
8177 * "mzeval()" function
8178 */
8179 static void
8180f_mzeval(typval_T *argvars, typval_T *rettv)
8181{
8182 char_u *str;
8183 char_u buf[NUMBUFLEN];
8184
8185 str = get_tv_string_buf(&argvars[0], buf);
8186 do_mzeval(str, rettv);
8187}
8188
8189 void
8190mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8191{
8192 typval_T argvars[3];
8193
8194 argvars[0].v_type = VAR_STRING;
8195 argvars[0].vval.v_string = name;
8196 copy_tv(args, &argvars[1]);
8197 argvars[2].v_type = VAR_UNKNOWN;
8198 f_call(argvars, rettv);
8199 clear_tv(&argvars[1]);
8200}
8201#endif
8202
8203/*
8204 * "nextnonblank()" function
8205 */
8206 static void
8207f_nextnonblank(typval_T *argvars, typval_T *rettv)
8208{
8209 linenr_T lnum;
8210
8211 for (lnum = get_tv_lnum(argvars); ; ++lnum)
8212 {
8213 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8214 {
8215 lnum = 0;
8216 break;
8217 }
8218 if (*skipwhite(ml_get(lnum)) != NUL)
8219 break;
8220 }
8221 rettv->vval.v_number = lnum;
8222}
8223
8224/*
8225 * "nr2char()" function
8226 */
8227 static void
8228f_nr2char(typval_T *argvars, typval_T *rettv)
8229{
8230 char_u buf[NUMBUFLEN];
8231
8232#ifdef FEAT_MBYTE
8233 if (has_mbyte)
8234 {
8235 int utf8 = 0;
8236
8237 if (argvars[1].v_type != VAR_UNKNOWN)
8238 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
8239 if (utf8)
8240 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8241 else
8242 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8243 }
8244 else
8245#endif
8246 {
8247 buf[0] = (char_u)get_tv_number(&argvars[0]);
8248 buf[1] = NUL;
8249 }
8250 rettv->v_type = VAR_STRING;
8251 rettv->vval.v_string = vim_strsave(buf);
8252}
8253
8254/*
8255 * "or(expr, expr)" function
8256 */
8257 static void
8258f_or(typval_T *argvars, typval_T *rettv)
8259{
8260 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8261 | get_tv_number_chk(&argvars[1], NULL);
8262}
8263
8264/*
8265 * "pathshorten()" function
8266 */
8267 static void
8268f_pathshorten(typval_T *argvars, typval_T *rettv)
8269{
8270 char_u *p;
8271
8272 rettv->v_type = VAR_STRING;
8273 p = get_tv_string_chk(&argvars[0]);
8274 if (p == NULL)
8275 rettv->vval.v_string = NULL;
8276 else
8277 {
8278 p = vim_strsave(p);
8279 rettv->vval.v_string = p;
8280 if (p != NULL)
8281 shorten_dir(p);
8282 }
8283}
8284
8285#ifdef FEAT_PERL
8286/*
8287 * "perleval()" function
8288 */
8289 static void
8290f_perleval(typval_T *argvars, typval_T *rettv)
8291{
8292 char_u *str;
8293 char_u buf[NUMBUFLEN];
8294
8295 str = get_tv_string_buf(&argvars[0], buf);
8296 do_perleval(str, rettv);
8297}
8298#endif
8299
8300#ifdef FEAT_FLOAT
8301/*
8302 * "pow()" function
8303 */
8304 static void
8305f_pow(typval_T *argvars, typval_T *rettv)
8306{
8307 float_T fx = 0.0, fy = 0.0;
8308
8309 rettv->v_type = VAR_FLOAT;
8310 if (get_float_arg(argvars, &fx) == OK
8311 && get_float_arg(&argvars[1], &fy) == OK)
8312 rettv->vval.v_float = pow(fx, fy);
8313 else
8314 rettv->vval.v_float = 0.0;
8315}
8316#endif
8317
8318/*
8319 * "prevnonblank()" function
8320 */
8321 static void
8322f_prevnonblank(typval_T *argvars, typval_T *rettv)
8323{
8324 linenr_T lnum;
8325
8326 lnum = get_tv_lnum(argvars);
8327 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8328 lnum = 0;
8329 else
8330 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8331 --lnum;
8332 rettv->vval.v_number = lnum;
8333}
8334
8335/* This dummy va_list is here because:
8336 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8337 * - locally in the function results in a "used before set" warning
8338 * - using va_start() to initialize it gives "function with fixed args" error */
8339static va_list ap;
8340
8341/*
8342 * "printf()" function
8343 */
8344 static void
8345f_printf(typval_T *argvars, typval_T *rettv)
8346{
8347 char_u buf[NUMBUFLEN];
8348 int len;
8349 char_u *s;
8350 int saved_did_emsg = did_emsg;
8351 char *fmt;
8352
8353 rettv->v_type = VAR_STRING;
8354 rettv->vval.v_string = NULL;
8355
8356 /* Get the required length, allocate the buffer and do it for real. */
8357 did_emsg = FALSE;
8358 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008359 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008360 if (!did_emsg)
8361 {
8362 s = alloc(len + 1);
8363 if (s != NULL)
8364 {
8365 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008366 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8367 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008368 }
8369 }
8370 did_emsg |= saved_did_emsg;
8371}
8372
8373/*
8374 * "pumvisible()" function
8375 */
8376 static void
8377f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8378{
8379#ifdef FEAT_INS_EXPAND
8380 if (pum_visible())
8381 rettv->vval.v_number = 1;
8382#endif
8383}
8384
8385#ifdef FEAT_PYTHON3
8386/*
8387 * "py3eval()" function
8388 */
8389 static void
8390f_py3eval(typval_T *argvars, typval_T *rettv)
8391{
8392 char_u *str;
8393 char_u buf[NUMBUFLEN];
8394
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008395 if (p_pyx == 0)
8396 p_pyx = 3;
8397
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008398 str = get_tv_string_buf(&argvars[0], buf);
8399 do_py3eval(str, rettv);
8400}
8401#endif
8402
8403#ifdef FEAT_PYTHON
8404/*
8405 * "pyeval()" function
8406 */
8407 static void
8408f_pyeval(typval_T *argvars, typval_T *rettv)
8409{
8410 char_u *str;
8411 char_u buf[NUMBUFLEN];
8412
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008413 if (p_pyx == 0)
8414 p_pyx = 2;
8415
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008416 str = get_tv_string_buf(&argvars[0], buf);
8417 do_pyeval(str, rettv);
8418}
8419#endif
8420
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008421#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8422/*
8423 * "pyxeval()" function
8424 */
8425 static void
8426f_pyxeval(typval_T *argvars, typval_T *rettv)
8427{
8428# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8429 init_pyxversion();
8430 if (p_pyx == 2)
8431 f_pyeval(argvars, rettv);
8432 else
8433 f_py3eval(argvars, rettv);
8434# elif defined(FEAT_PYTHON)
8435 f_pyeval(argvars, rettv);
8436# elif defined(FEAT_PYTHON3)
8437 f_py3eval(argvars, rettv);
8438# endif
8439}
8440#endif
8441
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008442/*
8443 * "range()" function
8444 */
8445 static void
8446f_range(typval_T *argvars, typval_T *rettv)
8447{
8448 varnumber_T start;
8449 varnumber_T end;
8450 varnumber_T stride = 1;
8451 varnumber_T i;
8452 int error = FALSE;
8453
8454 start = get_tv_number_chk(&argvars[0], &error);
8455 if (argvars[1].v_type == VAR_UNKNOWN)
8456 {
8457 end = start - 1;
8458 start = 0;
8459 }
8460 else
8461 {
8462 end = get_tv_number_chk(&argvars[1], &error);
8463 if (argvars[2].v_type != VAR_UNKNOWN)
8464 stride = get_tv_number_chk(&argvars[2], &error);
8465 }
8466
8467 if (error)
8468 return; /* type error; errmsg already given */
8469 if (stride == 0)
8470 EMSG(_("E726: Stride is zero"));
8471 else if (stride > 0 ? end + 1 < start : end - 1 > start)
8472 EMSG(_("E727: Start past end"));
8473 else
8474 {
8475 if (rettv_list_alloc(rettv) == OK)
8476 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8477 if (list_append_number(rettv->vval.v_list,
8478 (varnumber_T)i) == FAIL)
8479 break;
8480 }
8481}
8482
8483/*
8484 * "readfile()" function
8485 */
8486 static void
8487f_readfile(typval_T *argvars, typval_T *rettv)
8488{
8489 int binary = FALSE;
8490 int failed = FALSE;
8491 char_u *fname;
8492 FILE *fd;
8493 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8494 int io_size = sizeof(buf);
8495 int readlen; /* size of last fread() */
8496 char_u *prev = NULL; /* previously read bytes, if any */
8497 long prevlen = 0; /* length of data in prev */
8498 long prevsize = 0; /* size of prev buffer */
8499 long maxline = MAXLNUM;
8500 long cnt = 0;
8501 char_u *p; /* position in buf */
8502 char_u *start; /* start of current line */
8503
8504 if (argvars[1].v_type != VAR_UNKNOWN)
8505 {
8506 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
8507 binary = TRUE;
8508 if (argvars[2].v_type != VAR_UNKNOWN)
8509 maxline = (long)get_tv_number(&argvars[2]);
8510 }
8511
8512 if (rettv_list_alloc(rettv) == FAIL)
8513 return;
8514
8515 /* Always open the file in binary mode, library functions have a mind of
8516 * their own about CR-LF conversion. */
8517 fname = get_tv_string(&argvars[0]);
8518 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8519 {
8520 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
8521 return;
8522 }
8523
8524 while (cnt < maxline || maxline < 0)
8525 {
8526 readlen = (int)fread(buf, 1, io_size, fd);
8527
8528 /* This for loop processes what was read, but is also entered at end
8529 * of file so that either:
8530 * - an incomplete line gets written
8531 * - a "binary" file gets an empty line at the end if it ends in a
8532 * newline. */
8533 for (p = buf, start = buf;
8534 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8535 ++p)
8536 {
8537 if (*p == '\n' || readlen <= 0)
8538 {
8539 listitem_T *li;
8540 char_u *s = NULL;
8541 long_u len = p - start;
8542
8543 /* Finished a line. Remove CRs before NL. */
8544 if (readlen > 0 && !binary)
8545 {
8546 while (len > 0 && start[len - 1] == '\r')
8547 --len;
8548 /* removal may cross back to the "prev" string */
8549 if (len == 0)
8550 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8551 --prevlen;
8552 }
8553 if (prevlen == 0)
8554 s = vim_strnsave(start, (int)len);
8555 else
8556 {
8557 /* Change "prev" buffer to be the right size. This way
8558 * the bytes are only copied once, and very long lines are
8559 * allocated only once. */
8560 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8561 {
8562 mch_memmove(s + prevlen, start, len);
8563 s[prevlen + len] = NUL;
8564 prev = NULL; /* the list will own the string */
8565 prevlen = prevsize = 0;
8566 }
8567 }
8568 if (s == NULL)
8569 {
8570 do_outofmem_msg((long_u) prevlen + len + 1);
8571 failed = TRUE;
8572 break;
8573 }
8574
8575 if ((li = listitem_alloc()) == NULL)
8576 {
8577 vim_free(s);
8578 failed = TRUE;
8579 break;
8580 }
8581 li->li_tv.v_type = VAR_STRING;
8582 li->li_tv.v_lock = 0;
8583 li->li_tv.vval.v_string = s;
8584 list_append(rettv->vval.v_list, li);
8585
8586 start = p + 1; /* step over newline */
8587 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
8588 break;
8589 }
8590 else if (*p == NUL)
8591 *p = '\n';
8592#ifdef FEAT_MBYTE
8593 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
8594 * when finding the BF and check the previous two bytes. */
8595 else if (*p == 0xbf && enc_utf8 && !binary)
8596 {
8597 /* Find the two bytes before the 0xbf. If p is at buf, or buf
8598 * + 1, these may be in the "prev" string. */
8599 char_u back1 = p >= buf + 1 ? p[-1]
8600 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
8601 char_u back2 = p >= buf + 2 ? p[-2]
8602 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
8603 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
8604
8605 if (back2 == 0xef && back1 == 0xbb)
8606 {
8607 char_u *dest = p - 2;
8608
8609 /* Usually a BOM is at the beginning of a file, and so at
8610 * the beginning of a line; then we can just step over it.
8611 */
8612 if (start == dest)
8613 start = p + 1;
8614 else
8615 {
8616 /* have to shuffle buf to close gap */
8617 int adjust_prevlen = 0;
8618
8619 if (dest < buf)
8620 {
8621 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
8622 dest = buf;
8623 }
8624 if (readlen > p - buf + 1)
8625 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
8626 readlen -= 3 - adjust_prevlen;
8627 prevlen -= adjust_prevlen;
8628 p = dest - 1;
8629 }
8630 }
8631 }
8632#endif
8633 } /* for */
8634
8635 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
8636 break;
8637 if (start < p)
8638 {
8639 /* There's part of a line in buf, store it in "prev". */
8640 if (p - start + prevlen >= prevsize)
8641 {
8642 /* need bigger "prev" buffer */
8643 char_u *newprev;
8644
8645 /* A common use case is ordinary text files and "prev" gets a
8646 * fragment of a line, so the first allocation is made
8647 * small, to avoid repeatedly 'allocing' large and
8648 * 'reallocing' small. */
8649 if (prevsize == 0)
8650 prevsize = (long)(p - start);
8651 else
8652 {
8653 long grow50pc = (prevsize * 3) / 2;
8654 long growmin = (long)((p - start) * 2 + prevlen);
8655 prevsize = grow50pc > growmin ? grow50pc : growmin;
8656 }
8657 newprev = prev == NULL ? alloc(prevsize)
8658 : vim_realloc(prev, prevsize);
8659 if (newprev == NULL)
8660 {
8661 do_outofmem_msg((long_u)prevsize);
8662 failed = TRUE;
8663 break;
8664 }
8665 prev = newprev;
8666 }
8667 /* Add the line part to end of "prev". */
8668 mch_memmove(prev + prevlen, start, p - start);
8669 prevlen += (long)(p - start);
8670 }
8671 } /* while */
8672
8673 /*
8674 * For a negative line count use only the lines at the end of the file,
8675 * free the rest.
8676 */
8677 if (!failed && maxline < 0)
8678 while (cnt > -maxline)
8679 {
8680 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
8681 --cnt;
8682 }
8683
8684 if (failed)
8685 {
8686 list_free(rettv->vval.v_list);
8687 /* readfile doc says an empty list is returned on error */
8688 rettv->vval.v_list = list_alloc();
8689 }
8690
8691 vim_free(prev);
8692 fclose(fd);
8693}
8694
8695#if defined(FEAT_RELTIME)
8696static int list2proftime(typval_T *arg, proftime_T *tm);
8697
8698/*
8699 * Convert a List to proftime_T.
8700 * Return FAIL when there is something wrong.
8701 */
8702 static int
8703list2proftime(typval_T *arg, proftime_T *tm)
8704{
8705 long n1, n2;
8706 int error = FALSE;
8707
8708 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
8709 || arg->vval.v_list->lv_len != 2)
8710 return FAIL;
8711 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
8712 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
8713# ifdef WIN3264
8714 tm->HighPart = n1;
8715 tm->LowPart = n2;
8716# else
8717 tm->tv_sec = n1;
8718 tm->tv_usec = n2;
8719# endif
8720 return error ? FAIL : OK;
8721}
8722#endif /* FEAT_RELTIME */
8723
8724/*
8725 * "reltime()" function
8726 */
8727 static void
8728f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8729{
8730#ifdef FEAT_RELTIME
8731 proftime_T res;
8732 proftime_T start;
8733
8734 if (argvars[0].v_type == VAR_UNKNOWN)
8735 {
8736 /* No arguments: get current time. */
8737 profile_start(&res);
8738 }
8739 else if (argvars[1].v_type == VAR_UNKNOWN)
8740 {
8741 if (list2proftime(&argvars[0], &res) == FAIL)
8742 return;
8743 profile_end(&res);
8744 }
8745 else
8746 {
8747 /* Two arguments: compute the difference. */
8748 if (list2proftime(&argvars[0], &start) == FAIL
8749 || list2proftime(&argvars[1], &res) == FAIL)
8750 return;
8751 profile_sub(&res, &start);
8752 }
8753
8754 if (rettv_list_alloc(rettv) == OK)
8755 {
8756 long n1, n2;
8757
8758# ifdef WIN3264
8759 n1 = res.HighPart;
8760 n2 = res.LowPart;
8761# else
8762 n1 = res.tv_sec;
8763 n2 = res.tv_usec;
8764# endif
8765 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
8766 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
8767 }
8768#endif
8769}
8770
8771#ifdef FEAT_FLOAT
8772/*
8773 * "reltimefloat()" function
8774 */
8775 static void
8776f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
8777{
8778# ifdef FEAT_RELTIME
8779 proftime_T tm;
8780# endif
8781
8782 rettv->v_type = VAR_FLOAT;
8783 rettv->vval.v_float = 0;
8784# ifdef FEAT_RELTIME
8785 if (list2proftime(&argvars[0], &tm) == OK)
8786 rettv->vval.v_float = profile_float(&tm);
8787# endif
8788}
8789#endif
8790
8791/*
8792 * "reltimestr()" function
8793 */
8794 static void
8795f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
8796{
8797#ifdef FEAT_RELTIME
8798 proftime_T tm;
8799#endif
8800
8801 rettv->v_type = VAR_STRING;
8802 rettv->vval.v_string = NULL;
8803#ifdef FEAT_RELTIME
8804 if (list2proftime(&argvars[0], &tm) == OK)
8805 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
8806#endif
8807}
8808
8809#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
8810static void make_connection(void);
8811static int check_connection(void);
8812
8813 static void
8814make_connection(void)
8815{
8816 if (X_DISPLAY == NULL
8817# ifdef FEAT_GUI
8818 && !gui.in_use
8819# endif
8820 )
8821 {
8822 x_force_connect = TRUE;
8823 setup_term_clip();
8824 x_force_connect = FALSE;
8825 }
8826}
8827
8828 static int
8829check_connection(void)
8830{
8831 make_connection();
8832 if (X_DISPLAY == NULL)
8833 {
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01008834 EMSG(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008835 return FAIL;
8836 }
8837 return OK;
8838}
8839#endif
8840
8841#ifdef FEAT_CLIENTSERVER
8842 static void
8843remote_common(typval_T *argvars, typval_T *rettv, int expr)
8844{
8845 char_u *server_name;
8846 char_u *keys;
8847 char_u *r = NULL;
8848 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008849 int timeout = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008850# ifdef WIN32
8851 HWND w;
8852# else
8853 Window w;
8854# endif
8855
8856 if (check_restricted() || check_secure())
8857 return;
8858
8859# ifdef FEAT_X11
8860 if (check_connection() == FAIL)
8861 return;
8862# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008863 if (argvars[2].v_type != VAR_UNKNOWN
8864 && argvars[3].v_type != VAR_UNKNOWN)
8865 timeout = get_tv_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008866
8867 server_name = get_tv_string_chk(&argvars[0]);
8868 if (server_name == NULL)
8869 return; /* type error; errmsg already given */
8870 keys = get_tv_string_buf(&argvars[1], buf);
8871# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008872 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008873# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008874 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
8875 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008876# endif
8877 {
8878 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008879 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008880 EMSG(r); /* sending worked but evaluation failed */
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008881 vim_free(r);
8882 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008883 else
8884 EMSG2(_("E241: Unable to send to %s"), server_name);
8885 return;
8886 }
8887
8888 rettv->vval.v_string = r;
8889
8890 if (argvars[2].v_type != VAR_UNKNOWN)
8891 {
8892 dictitem_T v;
8893 char_u str[30];
8894 char_u *idvar;
8895
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008896 idvar = get_tv_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008897 if (idvar != NULL && *idvar != NUL)
8898 {
8899 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
8900 v.di_tv.v_type = VAR_STRING;
8901 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008902 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008903 vim_free(v.di_tv.vval.v_string);
8904 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008905 }
8906}
8907#endif
8908
8909/*
8910 * "remote_expr()" function
8911 */
8912 static void
8913f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
8914{
8915 rettv->v_type = VAR_STRING;
8916 rettv->vval.v_string = NULL;
8917#ifdef FEAT_CLIENTSERVER
8918 remote_common(argvars, rettv, TRUE);
8919#endif
8920}
8921
8922/*
8923 * "remote_foreground()" function
8924 */
8925 static void
8926f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8927{
8928#ifdef FEAT_CLIENTSERVER
8929# ifdef WIN32
8930 /* On Win32 it's done in this application. */
8931 {
8932 char_u *server_name = get_tv_string_chk(&argvars[0]);
8933
8934 if (server_name != NULL)
8935 serverForeground(server_name);
8936 }
8937# else
8938 /* Send a foreground() expression to the server. */
8939 argvars[1].v_type = VAR_STRING;
8940 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
8941 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008942 rettv->v_type = VAR_STRING;
8943 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008944 remote_common(argvars, rettv, TRUE);
8945 vim_free(argvars[1].vval.v_string);
8946# endif
8947#endif
8948}
8949
8950 static void
8951f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
8952{
8953#ifdef FEAT_CLIENTSERVER
8954 dictitem_T v;
8955 char_u *s = NULL;
8956# ifdef WIN32
8957 long_u n = 0;
8958# endif
8959 char_u *serverid;
8960
8961 if (check_restricted() || check_secure())
8962 {
8963 rettv->vval.v_number = -1;
8964 return;
8965 }
8966 serverid = get_tv_string_chk(&argvars[0]);
8967 if (serverid == NULL)
8968 {
8969 rettv->vval.v_number = -1;
8970 return; /* type error; errmsg already given */
8971 }
8972# ifdef WIN32
8973 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
8974 if (n == 0)
8975 rettv->vval.v_number = -1;
8976 else
8977 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008978 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008979 rettv->vval.v_number = (s != NULL);
8980 }
8981# else
8982 if (check_connection() == FAIL)
8983 return;
8984
8985 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
8986 serverStrToWin(serverid), &s);
8987# endif
8988
8989 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
8990 {
8991 char_u *retvar;
8992
8993 v.di_tv.v_type = VAR_STRING;
8994 v.di_tv.vval.v_string = vim_strsave(s);
8995 retvar = get_tv_string_chk(&argvars[1]);
8996 if (retvar != NULL)
8997 set_var(retvar, &v.di_tv, FALSE);
8998 vim_free(v.di_tv.vval.v_string);
8999 }
9000#else
9001 rettv->vval.v_number = -1;
9002#endif
9003}
9004
9005 static void
9006f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9007{
9008 char_u *r = NULL;
9009
9010#ifdef FEAT_CLIENTSERVER
9011 char_u *serverid = get_tv_string_chk(&argvars[0]);
9012
9013 if (serverid != NULL && !check_restricted() && !check_secure())
9014 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009015 int timeout = 0;
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009016# ifdef WIN32
9017 /* The server's HWND is encoded in the 'id' parameter */
9018 long_u n = 0;
9019# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009020
9021 if (argvars[1].v_type != VAR_UNKNOWN)
9022 timeout = get_tv_number(&argvars[1]);
9023
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009024# ifdef WIN32
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009025 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9026 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009027 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009028 if (r == NULL)
9029# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009030 if (check_connection() == FAIL
9031 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9032 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009033# endif
9034 EMSG(_("E277: Unable to read a server reply"));
9035 }
9036#endif
9037 rettv->v_type = VAR_STRING;
9038 rettv->vval.v_string = r;
9039}
9040
9041/*
9042 * "remote_send()" function
9043 */
9044 static void
9045f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9046{
9047 rettv->v_type = VAR_STRING;
9048 rettv->vval.v_string = NULL;
9049#ifdef FEAT_CLIENTSERVER
9050 remote_common(argvars, rettv, FALSE);
9051#endif
9052}
9053
9054/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009055 * "remote_startserver()" function
9056 */
9057 static void
9058f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9059{
9060#ifdef FEAT_CLIENTSERVER
9061 char_u *server = get_tv_string_chk(&argvars[0]);
9062
9063 if (server == NULL)
9064 return; /* type error; errmsg already given */
9065 if (serverName != NULL)
9066 EMSG(_("E941: already started a server"));
9067 else
9068 {
9069# ifdef FEAT_X11
9070 if (check_connection() == OK)
9071 serverRegisterName(X_DISPLAY, server);
9072# else
9073 serverSetName(server);
9074# endif
9075 }
9076#else
9077 EMSG(_("E942: +clientserver feature not available"));
9078#endif
9079}
9080
9081/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009082 * "remove()" function
9083 */
9084 static void
9085f_remove(typval_T *argvars, typval_T *rettv)
9086{
9087 list_T *l;
9088 listitem_T *item, *item2;
9089 listitem_T *li;
9090 long idx;
9091 long end;
9092 char_u *key;
9093 dict_T *d;
9094 dictitem_T *di;
9095 char_u *arg_errmsg = (char_u *)N_("remove() argument");
9096
9097 if (argvars[0].v_type == VAR_DICT)
9098 {
9099 if (argvars[2].v_type != VAR_UNKNOWN)
9100 EMSG2(_(e_toomanyarg), "remove()");
9101 else if ((d = argvars[0].vval.v_dict) != NULL
9102 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
9103 {
9104 key = get_tv_string_chk(&argvars[1]);
9105 if (key != NULL)
9106 {
9107 di = dict_find(d, key, -1);
9108 if (di == NULL)
9109 EMSG2(_(e_dictkey), key);
9110 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
9111 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
9112 {
9113 *rettv = di->di_tv;
9114 init_tv(&di->di_tv);
9115 dictitem_remove(d, di);
9116 }
9117 }
9118 }
9119 }
9120 else if (argvars[0].v_type != VAR_LIST)
9121 EMSG2(_(e_listdictarg), "remove()");
9122 else if ((l = argvars[0].vval.v_list) != NULL
9123 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
9124 {
9125 int error = FALSE;
9126
9127 idx = (long)get_tv_number_chk(&argvars[1], &error);
9128 if (error)
9129 ; /* type error: do nothing, errmsg already given */
9130 else if ((item = list_find(l, idx)) == NULL)
9131 EMSGN(_(e_listidx), idx);
9132 else
9133 {
9134 if (argvars[2].v_type == VAR_UNKNOWN)
9135 {
9136 /* Remove one item, return its value. */
9137 vimlist_remove(l, item, item);
9138 *rettv = item->li_tv;
9139 vim_free(item);
9140 }
9141 else
9142 {
9143 /* Remove range of items, return list with values. */
9144 end = (long)get_tv_number_chk(&argvars[2], &error);
9145 if (error)
9146 ; /* type error: do nothing */
9147 else if ((item2 = list_find(l, end)) == NULL)
9148 EMSGN(_(e_listidx), end);
9149 else
9150 {
9151 int cnt = 0;
9152
9153 for (li = item; li != NULL; li = li->li_next)
9154 {
9155 ++cnt;
9156 if (li == item2)
9157 break;
9158 }
9159 if (li == NULL) /* didn't find "item2" after "item" */
9160 EMSG(_(e_invrange));
9161 else
9162 {
9163 vimlist_remove(l, item, item2);
9164 if (rettv_list_alloc(rettv) == OK)
9165 {
9166 l = rettv->vval.v_list;
9167 l->lv_first = item;
9168 l->lv_last = item2;
9169 item->li_prev = NULL;
9170 item2->li_next = NULL;
9171 l->lv_len = cnt;
9172 }
9173 }
9174 }
9175 }
9176 }
9177 }
9178}
9179
9180/*
9181 * "rename({from}, {to})" function
9182 */
9183 static void
9184f_rename(typval_T *argvars, typval_T *rettv)
9185{
9186 char_u buf[NUMBUFLEN];
9187
9188 if (check_restricted() || check_secure())
9189 rettv->vval.v_number = -1;
9190 else
9191 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
9192 get_tv_string_buf(&argvars[1], buf));
9193}
9194
9195/*
9196 * "repeat()" function
9197 */
9198 static void
9199f_repeat(typval_T *argvars, typval_T *rettv)
9200{
9201 char_u *p;
9202 int n;
9203 int slen;
9204 int len;
9205 char_u *r;
9206 int i;
9207
9208 n = (int)get_tv_number(&argvars[1]);
9209 if (argvars[0].v_type == VAR_LIST)
9210 {
9211 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9212 while (n-- > 0)
9213 if (list_extend(rettv->vval.v_list,
9214 argvars[0].vval.v_list, NULL) == FAIL)
9215 break;
9216 }
9217 else
9218 {
9219 p = get_tv_string(&argvars[0]);
9220 rettv->v_type = VAR_STRING;
9221 rettv->vval.v_string = NULL;
9222
9223 slen = (int)STRLEN(p);
9224 len = slen * n;
9225 if (len <= 0)
9226 return;
9227
9228 r = alloc(len + 1);
9229 if (r != NULL)
9230 {
9231 for (i = 0; i < n; i++)
9232 mch_memmove(r + i * slen, p, (size_t)slen);
9233 r[len] = NUL;
9234 }
9235
9236 rettv->vval.v_string = r;
9237 }
9238}
9239
9240/*
9241 * "resolve()" function
9242 */
9243 static void
9244f_resolve(typval_T *argvars, typval_T *rettv)
9245{
9246 char_u *p;
9247#ifdef HAVE_READLINK
9248 char_u *buf = NULL;
9249#endif
9250
9251 p = get_tv_string(&argvars[0]);
9252#ifdef FEAT_SHORTCUT
9253 {
9254 char_u *v = NULL;
9255
9256 v = mch_resolve_shortcut(p);
9257 if (v != NULL)
9258 rettv->vval.v_string = v;
9259 else
9260 rettv->vval.v_string = vim_strsave(p);
9261 }
9262#else
9263# ifdef HAVE_READLINK
9264 {
9265 char_u *cpy;
9266 int len;
9267 char_u *remain = NULL;
9268 char_u *q;
9269 int is_relative_to_current = FALSE;
9270 int has_trailing_pathsep = FALSE;
9271 int limit = 100;
9272
9273 p = vim_strsave(p);
9274
9275 if (p[0] == '.' && (vim_ispathsep(p[1])
9276 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9277 is_relative_to_current = TRUE;
9278
9279 len = STRLEN(p);
9280 if (len > 0 && after_pathsep(p, p + len))
9281 {
9282 has_trailing_pathsep = TRUE;
9283 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9284 }
9285
9286 q = getnextcomp(p);
9287 if (*q != NUL)
9288 {
9289 /* Separate the first path component in "p", and keep the
9290 * remainder (beginning with the path separator). */
9291 remain = vim_strsave(q - 1);
9292 q[-1] = NUL;
9293 }
9294
9295 buf = alloc(MAXPATHL + 1);
9296 if (buf == NULL)
9297 goto fail;
9298
9299 for (;;)
9300 {
9301 for (;;)
9302 {
9303 len = readlink((char *)p, (char *)buf, MAXPATHL);
9304 if (len <= 0)
9305 break;
9306 buf[len] = NUL;
9307
9308 if (limit-- == 0)
9309 {
9310 vim_free(p);
9311 vim_free(remain);
9312 EMSG(_("E655: Too many symbolic links (cycle?)"));
9313 rettv->vval.v_string = NULL;
9314 goto fail;
9315 }
9316
9317 /* Ensure that the result will have a trailing path separator
9318 * if the argument has one. */
9319 if (remain == NULL && has_trailing_pathsep)
9320 add_pathsep(buf);
9321
9322 /* Separate the first path component in the link value and
9323 * concatenate the remainders. */
9324 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9325 if (*q != NUL)
9326 {
9327 if (remain == NULL)
9328 remain = vim_strsave(q - 1);
9329 else
9330 {
9331 cpy = concat_str(q - 1, remain);
9332 if (cpy != NULL)
9333 {
9334 vim_free(remain);
9335 remain = cpy;
9336 }
9337 }
9338 q[-1] = NUL;
9339 }
9340
9341 q = gettail(p);
9342 if (q > p && *q == NUL)
9343 {
9344 /* Ignore trailing path separator. */
9345 q[-1] = NUL;
9346 q = gettail(p);
9347 }
9348 if (q > p && !mch_isFullName(buf))
9349 {
9350 /* symlink is relative to directory of argument */
9351 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
9352 if (cpy != NULL)
9353 {
9354 STRCPY(cpy, p);
9355 STRCPY(gettail(cpy), buf);
9356 vim_free(p);
9357 p = cpy;
9358 }
9359 }
9360 else
9361 {
9362 vim_free(p);
9363 p = vim_strsave(buf);
9364 }
9365 }
9366
9367 if (remain == NULL)
9368 break;
9369
9370 /* Append the first path component of "remain" to "p". */
9371 q = getnextcomp(remain + 1);
9372 len = q - remain - (*q != NUL);
9373 cpy = vim_strnsave(p, STRLEN(p) + len);
9374 if (cpy != NULL)
9375 {
9376 STRNCAT(cpy, remain, len);
9377 vim_free(p);
9378 p = cpy;
9379 }
9380 /* Shorten "remain". */
9381 if (*q != NUL)
9382 STRMOVE(remain, q - 1);
9383 else
Bram Moolenaard23a8232018-02-10 18:45:26 +01009384 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009385 }
9386
9387 /* If the result is a relative path name, make it explicitly relative to
9388 * the current directory if and only if the argument had this form. */
9389 if (!vim_ispathsep(*p))
9390 {
9391 if (is_relative_to_current
9392 && *p != NUL
9393 && !(p[0] == '.'
9394 && (p[1] == NUL
9395 || vim_ispathsep(p[1])
9396 || (p[1] == '.'
9397 && (p[2] == NUL
9398 || vim_ispathsep(p[2]))))))
9399 {
9400 /* Prepend "./". */
9401 cpy = concat_str((char_u *)"./", p);
9402 if (cpy != NULL)
9403 {
9404 vim_free(p);
9405 p = cpy;
9406 }
9407 }
9408 else if (!is_relative_to_current)
9409 {
9410 /* Strip leading "./". */
9411 q = p;
9412 while (q[0] == '.' && vim_ispathsep(q[1]))
9413 q += 2;
9414 if (q > p)
9415 STRMOVE(p, p + 2);
9416 }
9417 }
9418
9419 /* Ensure that the result will have no trailing path separator
9420 * if the argument had none. But keep "/" or "//". */
9421 if (!has_trailing_pathsep)
9422 {
9423 q = p + STRLEN(p);
9424 if (after_pathsep(p, q))
9425 *gettail_sep(p) = NUL;
9426 }
9427
9428 rettv->vval.v_string = p;
9429 }
9430# else
9431 rettv->vval.v_string = vim_strsave(p);
9432# endif
9433#endif
9434
9435 simplify_filename(rettv->vval.v_string);
9436
9437#ifdef HAVE_READLINK
9438fail:
9439 vim_free(buf);
9440#endif
9441 rettv->v_type = VAR_STRING;
9442}
9443
9444/*
9445 * "reverse({list})" function
9446 */
9447 static void
9448f_reverse(typval_T *argvars, typval_T *rettv)
9449{
9450 list_T *l;
9451 listitem_T *li, *ni;
9452
9453 if (argvars[0].v_type != VAR_LIST)
9454 EMSG2(_(e_listarg), "reverse()");
9455 else if ((l = argvars[0].vval.v_list) != NULL
9456 && !tv_check_lock(l->lv_lock,
9457 (char_u *)N_("reverse() argument"), TRUE))
9458 {
9459 li = l->lv_last;
9460 l->lv_first = l->lv_last = NULL;
9461 l->lv_len = 0;
9462 while (li != NULL)
9463 {
9464 ni = li->li_prev;
9465 list_append(l, li);
9466 li = ni;
9467 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009468 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009469 l->lv_idx = l->lv_len - l->lv_idx - 1;
9470 }
9471}
9472
9473#define SP_NOMOVE 0x01 /* don't move cursor */
9474#define SP_REPEAT 0x02 /* repeat to find outer pair */
9475#define SP_RETCOUNT 0x04 /* return matchcount */
9476#define SP_SETPCMARK 0x08 /* set previous context mark */
9477#define SP_START 0x10 /* accept match at start position */
9478#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9479#define SP_END 0x40 /* leave cursor at end of match */
9480#define SP_COLUMN 0x80 /* start at cursor column */
9481
9482static int get_search_arg(typval_T *varp, int *flagsp);
9483
9484/*
9485 * Get flags for a search function.
9486 * Possibly sets "p_ws".
9487 * Returns BACKWARD, FORWARD or zero (for an error).
9488 */
9489 static int
9490get_search_arg(typval_T *varp, int *flagsp)
9491{
9492 int dir = FORWARD;
9493 char_u *flags;
9494 char_u nbuf[NUMBUFLEN];
9495 int mask;
9496
9497 if (varp->v_type != VAR_UNKNOWN)
9498 {
9499 flags = get_tv_string_buf_chk(varp, nbuf);
9500 if (flags == NULL)
9501 return 0; /* type error; errmsg already given */
9502 while (*flags != NUL)
9503 {
9504 switch (*flags)
9505 {
9506 case 'b': dir = BACKWARD; break;
9507 case 'w': p_ws = TRUE; break;
9508 case 'W': p_ws = FALSE; break;
9509 default: mask = 0;
9510 if (flagsp != NULL)
9511 switch (*flags)
9512 {
9513 case 'c': mask = SP_START; break;
9514 case 'e': mask = SP_END; break;
9515 case 'm': mask = SP_RETCOUNT; break;
9516 case 'n': mask = SP_NOMOVE; break;
9517 case 'p': mask = SP_SUBPAT; break;
9518 case 'r': mask = SP_REPEAT; break;
9519 case 's': mask = SP_SETPCMARK; break;
9520 case 'z': mask = SP_COLUMN; break;
9521 }
9522 if (mask == 0)
9523 {
9524 EMSG2(_(e_invarg2), flags);
9525 dir = 0;
9526 }
9527 else
9528 *flagsp |= mask;
9529 }
9530 if (dir == 0)
9531 break;
9532 ++flags;
9533 }
9534 }
9535 return dir;
9536}
9537
9538/*
9539 * Shared by search() and searchpos() functions.
9540 */
9541 static int
9542search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9543{
9544 int flags;
9545 char_u *pat;
9546 pos_T pos;
9547 pos_T save_cursor;
9548 int save_p_ws = p_ws;
9549 int dir;
9550 int retval = 0; /* default: FAIL */
9551 long lnum_stop = 0;
9552 proftime_T tm;
9553#ifdef FEAT_RELTIME
9554 long time_limit = 0;
9555#endif
9556 int options = SEARCH_KEEP;
9557 int subpatnum;
9558
9559 pat = get_tv_string(&argvars[0]);
9560 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
9561 if (dir == 0)
9562 goto theend;
9563 flags = *flagsp;
9564 if (flags & SP_START)
9565 options |= SEARCH_START;
9566 if (flags & SP_END)
9567 options |= SEARCH_END;
9568 if (flags & SP_COLUMN)
9569 options |= SEARCH_COL;
9570
9571 /* Optional arguments: line number to stop searching and timeout. */
9572 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9573 {
9574 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
9575 if (lnum_stop < 0)
9576 goto theend;
9577#ifdef FEAT_RELTIME
9578 if (argvars[3].v_type != VAR_UNKNOWN)
9579 {
9580 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
9581 if (time_limit < 0)
9582 goto theend;
9583 }
9584#endif
9585 }
9586
9587#ifdef FEAT_RELTIME
9588 /* Set the time limit, if there is one. */
9589 profile_setlimit(time_limit, &tm);
9590#endif
9591
9592 /*
9593 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
9594 * Check to make sure only those flags are set.
9595 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
9596 * flags cannot be set. Check for that condition also.
9597 */
9598 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
9599 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9600 {
9601 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
9602 goto theend;
9603 }
9604
9605 pos = save_cursor = curwin->w_cursor;
9606 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009607 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009608 if (subpatnum != FAIL)
9609 {
9610 if (flags & SP_SUBPAT)
9611 retval = subpatnum;
9612 else
9613 retval = pos.lnum;
9614 if (flags & SP_SETPCMARK)
9615 setpcmark();
9616 curwin->w_cursor = pos;
9617 if (match_pos != NULL)
9618 {
9619 /* Store the match cursor position */
9620 match_pos->lnum = pos.lnum;
9621 match_pos->col = pos.col + 1;
9622 }
9623 /* "/$" will put the cursor after the end of the line, may need to
9624 * correct that here */
9625 check_cursor();
9626 }
9627
9628 /* If 'n' flag is used: restore cursor position. */
9629 if (flags & SP_NOMOVE)
9630 curwin->w_cursor = save_cursor;
9631 else
9632 curwin->w_set_curswant = TRUE;
9633theend:
9634 p_ws = save_p_ws;
9635
9636 return retval;
9637}
9638
9639#ifdef FEAT_FLOAT
9640
9641/*
9642 * round() is not in C90, use ceil() or floor() instead.
9643 */
9644 float_T
9645vim_round(float_T f)
9646{
9647 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
9648}
9649
9650/*
9651 * "round({float})" function
9652 */
9653 static void
9654f_round(typval_T *argvars, typval_T *rettv)
9655{
9656 float_T f = 0.0;
9657
9658 rettv->v_type = VAR_FLOAT;
9659 if (get_float_arg(argvars, &f) == OK)
9660 rettv->vval.v_float = vim_round(f);
9661 else
9662 rettv->vval.v_float = 0.0;
9663}
9664#endif
9665
9666/*
9667 * "screenattr()" function
9668 */
9669 static void
9670f_screenattr(typval_T *argvars, typval_T *rettv)
9671{
9672 int row;
9673 int col;
9674 int c;
9675
9676 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9677 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9678 if (row < 0 || row >= screen_Rows
9679 || col < 0 || col >= screen_Columns)
9680 c = -1;
9681 else
9682 c = ScreenAttrs[LineOffset[row] + col];
9683 rettv->vval.v_number = c;
9684}
9685
9686/*
9687 * "screenchar()" function
9688 */
9689 static void
9690f_screenchar(typval_T *argvars, typval_T *rettv)
9691{
9692 int row;
9693 int col;
9694 int off;
9695 int c;
9696
9697 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9698 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9699 if (row < 0 || row >= screen_Rows
9700 || col < 0 || col >= screen_Columns)
9701 c = -1;
9702 else
9703 {
9704 off = LineOffset[row] + col;
9705#ifdef FEAT_MBYTE
9706 if (enc_utf8 && ScreenLinesUC[off] != 0)
9707 c = ScreenLinesUC[off];
9708 else
9709#endif
9710 c = ScreenLines[off];
9711 }
9712 rettv->vval.v_number = c;
9713}
9714
9715/*
9716 * "screencol()" function
9717 *
9718 * First column is 1 to be consistent with virtcol().
9719 */
9720 static void
9721f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
9722{
9723 rettv->vval.v_number = screen_screencol() + 1;
9724}
9725
9726/*
9727 * "screenrow()" function
9728 */
9729 static void
9730f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
9731{
9732 rettv->vval.v_number = screen_screenrow() + 1;
9733}
9734
9735/*
9736 * "search()" function
9737 */
9738 static void
9739f_search(typval_T *argvars, typval_T *rettv)
9740{
9741 int flags = 0;
9742
9743 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
9744}
9745
9746/*
9747 * "searchdecl()" function
9748 */
9749 static void
9750f_searchdecl(typval_T *argvars, typval_T *rettv)
9751{
9752 int locally = 1;
9753 int thisblock = 0;
9754 int error = FALSE;
9755 char_u *name;
9756
9757 rettv->vval.v_number = 1; /* default: FAIL */
9758
9759 name = get_tv_string_chk(&argvars[0]);
9760 if (argvars[1].v_type != VAR_UNKNOWN)
9761 {
9762 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
9763 if (!error && argvars[2].v_type != VAR_UNKNOWN)
9764 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
9765 }
9766 if (!error && name != NULL)
9767 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
9768 locally, thisblock, SEARCH_KEEP) == FAIL;
9769}
9770
9771/*
9772 * Used by searchpair() and searchpairpos()
9773 */
9774 static int
9775searchpair_cmn(typval_T *argvars, pos_T *match_pos)
9776{
9777 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01009778 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009779 int save_p_ws = p_ws;
9780 int dir;
9781 int flags = 0;
9782 char_u nbuf1[NUMBUFLEN];
9783 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009784 int retval = 0; /* default: FAIL */
9785 long lnum_stop = 0;
9786 long time_limit = 0;
9787
9788 /* Get the three pattern arguments: start, middle, end. */
9789 spat = get_tv_string_chk(&argvars[0]);
9790 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
9791 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
9792 if (spat == NULL || mpat == NULL || epat == NULL)
9793 goto theend; /* type error */
9794
9795 /* Handle the optional fourth argument: flags */
9796 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
9797 if (dir == 0)
9798 goto theend;
9799
9800 /* Don't accept SP_END or SP_SUBPAT.
9801 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
9802 */
9803 if ((flags & (SP_END | SP_SUBPAT)) != 0
9804 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9805 {
9806 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
9807 goto theend;
9808 }
9809
9810 /* Using 'r' implies 'W', otherwise it doesn't work. */
9811 if (flags & SP_REPEAT)
9812 p_ws = FALSE;
9813
9814 /* Optional fifth argument: skip expression */
9815 if (argvars[3].v_type == VAR_UNKNOWN
9816 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01009817 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009818 else
9819 {
Bram Moolenaar48570482017-10-30 21:48:41 +01009820 skip = &argvars[4];
9821 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
9822 && skip->v_type != VAR_STRING)
9823 {
9824 /* Type error */
9825 goto theend;
9826 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009827 if (argvars[5].v_type != VAR_UNKNOWN)
9828 {
9829 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
9830 if (lnum_stop < 0)
9831 goto theend;
9832#ifdef FEAT_RELTIME
9833 if (argvars[6].v_type != VAR_UNKNOWN)
9834 {
9835 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
9836 if (time_limit < 0)
9837 goto theend;
9838 }
9839#endif
9840 }
9841 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009842
9843 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
9844 match_pos, lnum_stop, time_limit);
9845
9846theend:
9847 p_ws = save_p_ws;
9848
9849 return retval;
9850}
9851
9852/*
9853 * "searchpair()" function
9854 */
9855 static void
9856f_searchpair(typval_T *argvars, typval_T *rettv)
9857{
9858 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
9859}
9860
9861/*
9862 * "searchpairpos()" function
9863 */
9864 static void
9865f_searchpairpos(typval_T *argvars, typval_T *rettv)
9866{
9867 pos_T match_pos;
9868 int lnum = 0;
9869 int col = 0;
9870
9871 if (rettv_list_alloc(rettv) == FAIL)
9872 return;
9873
9874 if (searchpair_cmn(argvars, &match_pos) > 0)
9875 {
9876 lnum = match_pos.lnum;
9877 col = match_pos.col;
9878 }
9879
9880 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9881 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9882}
9883
9884/*
9885 * Search for a start/middle/end thing.
9886 * Used by searchpair(), see its documentation for the details.
9887 * Returns 0 or -1 for no match,
9888 */
9889 long
9890do_searchpair(
9891 char_u *spat, /* start pattern */
9892 char_u *mpat, /* middle pattern */
9893 char_u *epat, /* end pattern */
9894 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +01009895 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009896 int flags, /* SP_SETPCMARK and other SP_ values */
9897 pos_T *match_pos,
9898 linenr_T lnum_stop, /* stop at this line if not zero */
9899 long time_limit UNUSED) /* stop after this many msec */
9900{
9901 char_u *save_cpo;
9902 char_u *pat, *pat2 = NULL, *pat3 = NULL;
9903 long retval = 0;
9904 pos_T pos;
9905 pos_T firstpos;
9906 pos_T foundpos;
9907 pos_T save_cursor;
9908 pos_T save_pos;
9909 int n;
9910 int r;
9911 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01009912 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009913 int err;
9914 int options = SEARCH_KEEP;
9915 proftime_T tm;
9916
9917 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
9918 save_cpo = p_cpo;
9919 p_cpo = empty_option;
9920
9921#ifdef FEAT_RELTIME
9922 /* Set the time limit, if there is one. */
9923 profile_setlimit(time_limit, &tm);
9924#endif
9925
9926 /* Make two search patterns: start/end (pat2, for in nested pairs) and
9927 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009928 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
9929 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009930 if (pat2 == NULL || pat3 == NULL)
9931 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009932 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009933 if (*mpat == NUL)
9934 STRCPY(pat3, pat2);
9935 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009936 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009937 spat, epat, mpat);
9938 if (flags & SP_START)
9939 options |= SEARCH_START;
9940
Bram Moolenaar48570482017-10-30 21:48:41 +01009941 if (skip != NULL)
9942 {
9943 /* Empty string means to not use the skip expression. */
9944 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
9945 use_skip = skip->vval.v_string != NULL
9946 && *skip->vval.v_string != NUL;
9947 }
9948
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009949 save_cursor = curwin->w_cursor;
9950 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009951 CLEAR_POS(&firstpos);
9952 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009953 pat = pat3;
9954 for (;;)
9955 {
9956 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009957 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009958 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009959 /* didn't find it or found the first match again: FAIL */
9960 break;
9961
9962 if (firstpos.lnum == 0)
9963 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009964 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009965 {
9966 /* Found the same position again. Can happen with a pattern that
9967 * has "\zs" at the end and searching backwards. Advance one
9968 * character and try again. */
9969 if (dir == BACKWARD)
9970 decl(&pos);
9971 else
9972 incl(&pos);
9973 }
9974 foundpos = pos;
9975
9976 /* clear the start flag to avoid getting stuck here */
9977 options &= ~SEARCH_START;
9978
9979 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +01009980 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009981 {
9982 save_pos = curwin->w_cursor;
9983 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01009984 err = FALSE;
9985 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009986 curwin->w_cursor = save_pos;
9987 if (err)
9988 {
9989 /* Evaluating {skip} caused an error, break here. */
9990 curwin->w_cursor = save_cursor;
9991 retval = -1;
9992 break;
9993 }
9994 if (r)
9995 continue;
9996 }
9997
9998 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
9999 {
10000 /* Found end when searching backwards or start when searching
10001 * forward: nested pair. */
10002 ++nest;
10003 pat = pat2; /* nested, don't search for middle */
10004 }
10005 else
10006 {
10007 /* Found end when searching forward or start when searching
10008 * backward: end of (nested) pair; or found middle in outer pair. */
10009 if (--nest == 1)
10010 pat = pat3; /* outer level, search for middle */
10011 }
10012
10013 if (nest == 0)
10014 {
10015 /* Found the match: return matchcount or line number. */
10016 if (flags & SP_RETCOUNT)
10017 ++retval;
10018 else
10019 retval = pos.lnum;
10020 if (flags & SP_SETPCMARK)
10021 setpcmark();
10022 curwin->w_cursor = pos;
10023 if (!(flags & SP_REPEAT))
10024 break;
10025 nest = 1; /* search for next unmatched */
10026 }
10027 }
10028
10029 if (match_pos != NULL)
10030 {
10031 /* Store the match cursor position */
10032 match_pos->lnum = curwin->w_cursor.lnum;
10033 match_pos->col = curwin->w_cursor.col + 1;
10034 }
10035
10036 /* If 'n' flag is used or search failed: restore cursor position. */
10037 if ((flags & SP_NOMOVE) || retval == 0)
10038 curwin->w_cursor = save_cursor;
10039
10040theend:
10041 vim_free(pat2);
10042 vim_free(pat3);
10043 if (p_cpo == empty_option)
10044 p_cpo = save_cpo;
10045 else
10046 /* Darn, evaluating the {skip} expression changed the value. */
10047 free_string_option(save_cpo);
10048
10049 return retval;
10050}
10051
10052/*
10053 * "searchpos()" function
10054 */
10055 static void
10056f_searchpos(typval_T *argvars, typval_T *rettv)
10057{
10058 pos_T match_pos;
10059 int lnum = 0;
10060 int col = 0;
10061 int n;
10062 int flags = 0;
10063
10064 if (rettv_list_alloc(rettv) == FAIL)
10065 return;
10066
10067 n = search_cmn(argvars, &match_pos, &flags);
10068 if (n > 0)
10069 {
10070 lnum = match_pos.lnum;
10071 col = match_pos.col;
10072 }
10073
10074 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10075 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10076 if (flags & SP_SUBPAT)
10077 list_append_number(rettv->vval.v_list, (varnumber_T)n);
10078}
10079
10080 static void
10081f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
10082{
10083#ifdef FEAT_CLIENTSERVER
10084 char_u buf[NUMBUFLEN];
10085 char_u *server = get_tv_string_chk(&argvars[0]);
10086 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
10087
10088 rettv->vval.v_number = -1;
10089 if (server == NULL || reply == NULL)
10090 return;
10091 if (check_restricted() || check_secure())
10092 return;
10093# ifdef FEAT_X11
10094 if (check_connection() == FAIL)
10095 return;
10096# endif
10097
10098 if (serverSendReply(server, reply) < 0)
10099 {
10100 EMSG(_("E258: Unable to send to client"));
10101 return;
10102 }
10103 rettv->vval.v_number = 0;
10104#else
10105 rettv->vval.v_number = -1;
10106#endif
10107}
10108
10109 static void
10110f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10111{
10112 char_u *r = NULL;
10113
10114#ifdef FEAT_CLIENTSERVER
10115# ifdef WIN32
10116 r = serverGetVimNames();
10117# else
10118 make_connection();
10119 if (X_DISPLAY != NULL)
10120 r = serverGetVimNames(X_DISPLAY);
10121# endif
10122#endif
10123 rettv->v_type = VAR_STRING;
10124 rettv->vval.v_string = r;
10125}
10126
10127/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010128 * Set line or list of lines in buffer "buf".
10129 */
10130 static void
10131set_buffer_lines(buf_T *buf, linenr_T lnum, typval_T *lines, typval_T *rettv)
10132{
10133 char_u *line = NULL;
10134 list_T *l = NULL;
10135 listitem_T *li = NULL;
10136 long added = 0;
10137 linenr_T lcount;
Bram Moolenaar0c4dc882017-11-06 21:32:54 +010010138 buf_T *curbuf_save = NULL;
10139 win_T *curwin_save = NULL;
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010140 int is_curbuf = buf == curbuf;
10141
Bram Moolenaar9d954202017-09-04 20:34:19 +020010142 /* When using the current buffer ml_mfp will be set if needed. Useful when
10143 * setline() is used on startup. For other buffers the buffer must be
10144 * loaded. */
10145 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010146 {
10147 rettv->vval.v_number = 1; /* FAIL */
10148 return;
10149 }
10150
Bram Moolenaar0c4dc882017-11-06 21:32:54 +010010151 if (!is_curbuf)
10152 {
10153 wininfo_T *wip;
10154
10155 curbuf_save = curbuf;
10156 curwin_save = curwin;
10157 curbuf = buf;
10158 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
10159 {
10160 if (wip->wi_win != NULL)
10161 {
10162 curwin = wip->wi_win;
10163 break;
10164 }
10165 }
10166 }
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010167
10168 lcount = curbuf->b_ml.ml_line_count;
10169
10170 if (lines->v_type == VAR_LIST)
10171 {
10172 l = lines->vval.v_list;
10173 li = l->lv_first;
10174 }
10175 else
10176 line = get_tv_string_chk(lines);
10177
10178 /* default result is zero == OK */
10179 for (;;)
10180 {
10181 if (l != NULL)
10182 {
10183 /* list argument, get next string */
10184 if (li == NULL)
10185 break;
10186 line = get_tv_string_chk(&li->li_tv);
10187 li = li->li_next;
10188 }
10189
10190 rettv->vval.v_number = 1; /* FAIL */
10191 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
10192 break;
10193
10194 /* When coming here from Insert mode, sync undo, so that this can be
10195 * undone separately from what was previously inserted. */
10196 if (u_sync_once == 2)
10197 {
10198 u_sync_once = 1; /* notify that u_sync() was called */
10199 u_sync(TRUE);
10200 }
10201
10202 if (lnum <= curbuf->b_ml.ml_line_count)
10203 {
10204 /* existing line, replace it */
10205 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
10206 {
10207 changed_bytes(lnum, 0);
10208 if (is_curbuf && lnum == curwin->w_cursor.lnum)
10209 check_cursor_col();
10210 rettv->vval.v_number = 0; /* OK */
10211 }
10212 }
10213 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
10214 {
10215 /* lnum is one past the last line, append the line */
10216 ++added;
10217 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
10218 rettv->vval.v_number = 0; /* OK */
10219 }
10220
10221 if (l == NULL) /* only one string argument */
10222 break;
10223 ++lnum;
10224 }
10225
10226 if (added > 0)
10227 appended_lines_mark(lcount, added);
10228
Bram Moolenaar0c4dc882017-11-06 21:32:54 +010010229 if (!is_curbuf)
10230 {
10231 curbuf = curbuf_save;
10232 curwin = curwin_save;
10233 }
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010234}
10235
10236/*
10237 * "setbufline()" function
10238 */
10239 static void
10240f_setbufline(argvars, rettv)
10241 typval_T *argvars;
10242 typval_T *rettv;
10243{
10244 linenr_T lnum;
10245 buf_T *buf;
10246
10247 buf = get_buf_tv(&argvars[0], FALSE);
10248 if (buf == NULL)
10249 rettv->vval.v_number = 1; /* FAIL */
10250 else
10251 {
10252 lnum = get_tv_lnum_buf(&argvars[1], buf);
10253
10254 set_buffer_lines(buf, lnum, &argvars[2], rettv);
10255 }
10256}
10257
10258/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010259 * "setbufvar()" function
10260 */
10261 static void
10262f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10263{
10264 buf_T *buf;
10265 char_u *varname, *bufvarname;
10266 typval_T *varp;
10267 char_u nbuf[NUMBUFLEN];
10268
10269 if (check_restricted() || check_secure())
10270 return;
10271 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10272 varname = get_tv_string_chk(&argvars[1]);
10273 buf = get_buf_tv(&argvars[0], FALSE);
10274 varp = &argvars[2];
10275
10276 if (buf != NULL && varname != NULL && varp != NULL)
10277 {
10278 if (*varname == '&')
10279 {
10280 long numval;
10281 char_u *strval;
10282 int error = FALSE;
10283 aco_save_T aco;
10284
10285 /* set curbuf to be our buf, temporarily */
10286 aucmd_prepbuf(&aco, buf);
10287
10288 ++varname;
10289 numval = (long)get_tv_number_chk(varp, &error);
10290 strval = get_tv_string_buf_chk(varp, nbuf);
10291 if (!error && strval != NULL)
10292 set_option_value(varname, numval, strval, OPT_LOCAL);
10293
10294 /* reset notion of buffer */
10295 aucmd_restbuf(&aco);
10296 }
10297 else
10298 {
10299 buf_T *save_curbuf = curbuf;
10300
10301 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
10302 if (bufvarname != NULL)
10303 {
10304 curbuf = buf;
10305 STRCPY(bufvarname, "b:");
10306 STRCPY(bufvarname + 2, varname);
10307 set_var(bufvarname, varp, TRUE);
10308 vim_free(bufvarname);
10309 curbuf = save_curbuf;
10310 }
10311 }
10312 }
10313}
10314
10315 static void
10316f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10317{
10318 dict_T *d;
10319 dictitem_T *di;
10320 char_u *csearch;
10321
10322 if (argvars[0].v_type != VAR_DICT)
10323 {
10324 EMSG(_(e_dictreq));
10325 return;
10326 }
10327
10328 if ((d = argvars[0].vval.v_dict) != NULL)
10329 {
10330 csearch = get_dict_string(d, (char_u *)"char", FALSE);
10331 if (csearch != NULL)
10332 {
10333#ifdef FEAT_MBYTE
10334 if (enc_utf8)
10335 {
10336 int pcc[MAX_MCO];
10337 int c = utfc_ptr2char(csearch, pcc);
10338
10339 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10340 }
10341 else
10342#endif
10343 set_last_csearch(PTR2CHAR(csearch),
10344 csearch, MB_PTR2LEN(csearch));
10345 }
10346
10347 di = dict_find(d, (char_u *)"forward", -1);
10348 if (di != NULL)
10349 set_csearch_direction((int)get_tv_number(&di->di_tv)
10350 ? FORWARD : BACKWARD);
10351
10352 di = dict_find(d, (char_u *)"until", -1);
10353 if (di != NULL)
10354 set_csearch_until(!!get_tv_number(&di->di_tv));
10355 }
10356}
10357
10358/*
10359 * "setcmdpos()" function
10360 */
10361 static void
10362f_setcmdpos(typval_T *argvars, typval_T *rettv)
10363{
10364 int pos = (int)get_tv_number(&argvars[0]) - 1;
10365
10366 if (pos >= 0)
10367 rettv->vval.v_number = set_cmdline_pos(pos);
10368}
10369
10370/*
10371 * "setfperm({fname}, {mode})" function
10372 */
10373 static void
10374f_setfperm(typval_T *argvars, typval_T *rettv)
10375{
10376 char_u *fname;
10377 char_u modebuf[NUMBUFLEN];
10378 char_u *mode_str;
10379 int i;
10380 int mask;
10381 int mode = 0;
10382
10383 rettv->vval.v_number = 0;
10384 fname = get_tv_string_chk(&argvars[0]);
10385 if (fname == NULL)
10386 return;
10387 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
10388 if (mode_str == NULL)
10389 return;
10390 if (STRLEN(mode_str) != 9)
10391 {
10392 EMSG2(_(e_invarg2), mode_str);
10393 return;
10394 }
10395
10396 mask = 1;
10397 for (i = 8; i >= 0; --i)
10398 {
10399 if (mode_str[i] != '-')
10400 mode |= mask;
10401 mask = mask << 1;
10402 }
10403 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10404}
10405
10406/*
10407 * "setline()" function
10408 */
10409 static void
10410f_setline(typval_T *argvars, typval_T *rettv)
10411{
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010412 linenr_T lnum = get_tv_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010413
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010414 set_buffer_lines(curbuf, lnum, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010415}
10416
Bram Moolenaard823fa92016-08-12 16:29:27 +020010417static 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 +020010418
10419/*
10420 * Used by "setqflist()" and "setloclist()" functions
10421 */
10422 static void
10423set_qf_ll_list(
10424 win_T *wp UNUSED,
10425 typval_T *list_arg UNUSED,
10426 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010427 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010428 typval_T *rettv)
10429{
10430#ifdef FEAT_QUICKFIX
10431 static char *e_invact = N_("E927: Invalid action: '%s'");
10432 char_u *act;
10433 int action = 0;
10434#endif
10435
10436 rettv->vval.v_number = -1;
10437
10438#ifdef FEAT_QUICKFIX
10439 if (list_arg->v_type != VAR_LIST)
10440 EMSG(_(e_listreq));
10441 else
10442 {
10443 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010444 dict_T *d = NULL;
10445 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010446
10447 if (action_arg->v_type == VAR_STRING)
10448 {
10449 act = get_tv_string_chk(action_arg);
10450 if (act == NULL)
10451 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020010452 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
10453 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010454 action = *act;
10455 else
10456 EMSG2(_(e_invact), act);
10457 }
10458 else if (action_arg->v_type == VAR_UNKNOWN)
10459 action = ' ';
10460 else
10461 EMSG(_(e_stringreq));
10462
Bram Moolenaard823fa92016-08-12 16:29:27 +020010463 if (action_arg->v_type != VAR_UNKNOWN
10464 && what_arg->v_type != VAR_UNKNOWN)
10465 {
10466 if (what_arg->v_type == VAR_DICT)
10467 d = what_arg->vval.v_dict;
10468 else
10469 {
10470 EMSG(_(e_dictreq));
10471 valid_dict = FALSE;
10472 }
10473 }
10474
10475 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
10476 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()"), d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010477 rettv->vval.v_number = 0;
10478 }
10479#endif
10480}
10481
10482/*
10483 * "setloclist()" function
10484 */
10485 static void
10486f_setloclist(typval_T *argvars, typval_T *rettv)
10487{
10488 win_T *win;
10489
10490 rettv->vval.v_number = -1;
10491
10492 win = find_win_by_nr(&argvars[0], NULL);
10493 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020010494 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010495}
10496
10497/*
10498 * "setmatches()" function
10499 */
10500 static void
10501f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10502{
10503#ifdef FEAT_SEARCH_EXTRA
10504 list_T *l;
10505 listitem_T *li;
10506 dict_T *d;
10507 list_T *s = NULL;
10508
10509 rettv->vval.v_number = -1;
10510 if (argvars[0].v_type != VAR_LIST)
10511 {
10512 EMSG(_(e_listreq));
10513 return;
10514 }
10515 if ((l = argvars[0].vval.v_list) != NULL)
10516 {
10517
10518 /* To some extent make sure that we are dealing with a list from
10519 * "getmatches()". */
10520 li = l->lv_first;
10521 while (li != NULL)
10522 {
10523 if (li->li_tv.v_type != VAR_DICT
10524 || (d = li->li_tv.vval.v_dict) == NULL)
10525 {
10526 EMSG(_(e_invarg));
10527 return;
10528 }
10529 if (!(dict_find(d, (char_u *)"group", -1) != NULL
10530 && (dict_find(d, (char_u *)"pattern", -1) != NULL
10531 || dict_find(d, (char_u *)"pos1", -1) != NULL)
10532 && dict_find(d, (char_u *)"priority", -1) != NULL
10533 && dict_find(d, (char_u *)"id", -1) != NULL))
10534 {
10535 EMSG(_(e_invarg));
10536 return;
10537 }
10538 li = li->li_next;
10539 }
10540
10541 clear_matches(curwin);
10542 li = l->lv_first;
10543 while (li != NULL)
10544 {
10545 int i = 0;
10546 char_u buf[5];
10547 dictitem_T *di;
10548 char_u *group;
10549 int priority;
10550 int id;
10551 char_u *conceal;
10552
10553 d = li->li_tv.vval.v_dict;
10554 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
10555 {
10556 if (s == NULL)
10557 {
10558 s = list_alloc();
10559 if (s == NULL)
10560 return;
10561 }
10562
10563 /* match from matchaddpos() */
10564 for (i = 1; i < 9; i++)
10565 {
10566 sprintf((char *)buf, (char *)"pos%d", i);
10567 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
10568 {
10569 if (di->di_tv.v_type != VAR_LIST)
10570 return;
10571
10572 list_append_tv(s, &di->di_tv);
10573 s->lv_refcount++;
10574 }
10575 else
10576 break;
10577 }
10578 }
10579
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010580 group = get_dict_string(d, (char_u *)"group", TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010581 priority = (int)get_dict_number(d, (char_u *)"priority");
10582 id = (int)get_dict_number(d, (char_u *)"id");
10583 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010584 ? get_dict_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010585 : NULL;
10586 if (i == 0)
10587 {
10588 match_add(curwin, group,
10589 get_dict_string(d, (char_u *)"pattern", FALSE),
10590 priority, id, NULL, conceal);
10591 }
10592 else
10593 {
10594 match_add(curwin, group, NULL, priority, id, s, conceal);
10595 list_unref(s);
10596 s = NULL;
10597 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010598 vim_free(group);
10599 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010600
10601 li = li->li_next;
10602 }
10603 rettv->vval.v_number = 0;
10604 }
10605#endif
10606}
10607
10608/*
10609 * "setpos()" function
10610 */
10611 static void
10612f_setpos(typval_T *argvars, typval_T *rettv)
10613{
10614 pos_T pos;
10615 int fnum;
10616 char_u *name;
10617 colnr_T curswant = -1;
10618
10619 rettv->vval.v_number = -1;
10620 name = get_tv_string_chk(argvars);
10621 if (name != NULL)
10622 {
10623 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10624 {
10625 if (--pos.col < 0)
10626 pos.col = 0;
10627 if (name[0] == '.' && name[1] == NUL)
10628 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010629 /* set cursor; "fnum" is ignored */
10630 curwin->w_cursor = pos;
10631 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010632 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010633 curwin->w_curswant = curswant - 1;
10634 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010635 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010636 check_cursor();
10637 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010638 }
10639 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10640 {
10641 /* set mark */
10642 if (setmark_pos(name[1], &pos, fnum) == OK)
10643 rettv->vval.v_number = 0;
10644 }
10645 else
10646 EMSG(_(e_invarg));
10647 }
10648 }
10649}
10650
10651/*
10652 * "setqflist()" function
10653 */
10654 static void
10655f_setqflist(typval_T *argvars, typval_T *rettv)
10656{
Bram Moolenaard823fa92016-08-12 16:29:27 +020010657 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010658}
10659
10660/*
10661 * "setreg()" function
10662 */
10663 static void
10664f_setreg(typval_T *argvars, typval_T *rettv)
10665{
10666 int regname;
10667 char_u *strregname;
10668 char_u *stropt;
10669 char_u *strval;
10670 int append;
10671 char_u yank_type;
10672 long block_len;
10673
10674 block_len = -1;
10675 yank_type = MAUTO;
10676 append = FALSE;
10677
10678 strregname = get_tv_string_chk(argvars);
10679 rettv->vval.v_number = 1; /* FAIL is default */
10680
10681 if (strregname == NULL)
10682 return; /* type error; errmsg already given */
10683 regname = *strregname;
10684 if (regname == 0 || regname == '@')
10685 regname = '"';
10686
10687 if (argvars[2].v_type != VAR_UNKNOWN)
10688 {
10689 stropt = get_tv_string_chk(&argvars[2]);
10690 if (stropt == NULL)
10691 return; /* type error */
10692 for (; *stropt != NUL; ++stropt)
10693 switch (*stropt)
10694 {
10695 case 'a': case 'A': /* append */
10696 append = TRUE;
10697 break;
10698 case 'v': case 'c': /* character-wise selection */
10699 yank_type = MCHAR;
10700 break;
10701 case 'V': case 'l': /* line-wise selection */
10702 yank_type = MLINE;
10703 break;
10704 case 'b': case Ctrl_V: /* block-wise selection */
10705 yank_type = MBLOCK;
10706 if (VIM_ISDIGIT(stropt[1]))
10707 {
10708 ++stropt;
10709 block_len = getdigits(&stropt) - 1;
10710 --stropt;
10711 }
10712 break;
10713 }
10714 }
10715
10716 if (argvars[1].v_type == VAR_LIST)
10717 {
10718 char_u **lstval;
10719 char_u **allocval;
10720 char_u buf[NUMBUFLEN];
10721 char_u **curval;
10722 char_u **curallocval;
10723 list_T *ll = argvars[1].vval.v_list;
10724 listitem_T *li;
10725 int len;
10726
10727 /* If the list is NULL handle like an empty list. */
10728 len = ll == NULL ? 0 : ll->lv_len;
10729
10730 /* First half: use for pointers to result lines; second half: use for
10731 * pointers to allocated copies. */
10732 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
10733 if (lstval == NULL)
10734 return;
10735 curval = lstval;
10736 allocval = lstval + len + 2;
10737 curallocval = allocval;
10738
10739 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
10740 li = li->li_next)
10741 {
10742 strval = get_tv_string_buf_chk(&li->li_tv, buf);
10743 if (strval == NULL)
10744 goto free_lstval;
10745 if (strval == buf)
10746 {
10747 /* Need to make a copy, next get_tv_string_buf_chk() will
10748 * overwrite the string. */
10749 strval = vim_strsave(buf);
10750 if (strval == NULL)
10751 goto free_lstval;
10752 *curallocval++ = strval;
10753 }
10754 *curval++ = strval;
10755 }
10756 *curval++ = NULL;
10757
10758 write_reg_contents_lst(regname, lstval, -1,
10759 append, yank_type, block_len);
10760free_lstval:
10761 while (curallocval > allocval)
10762 vim_free(*--curallocval);
10763 vim_free(lstval);
10764 }
10765 else
10766 {
10767 strval = get_tv_string_chk(&argvars[1]);
10768 if (strval == NULL)
10769 return;
10770 write_reg_contents_ex(regname, strval, -1,
10771 append, yank_type, block_len);
10772 }
10773 rettv->vval.v_number = 0;
10774}
10775
10776/*
10777 * "settabvar()" function
10778 */
10779 static void
10780f_settabvar(typval_T *argvars, typval_T *rettv)
10781{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010782 tabpage_T *save_curtab;
10783 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010784 char_u *varname, *tabvarname;
10785 typval_T *varp;
10786
10787 rettv->vval.v_number = 0;
10788
10789 if (check_restricted() || check_secure())
10790 return;
10791
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010792 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010793 varname = get_tv_string_chk(&argvars[1]);
10794 varp = &argvars[2];
10795
Bram Moolenaar4033c552017-09-16 20:54:51 +020010796 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010797 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010798 save_curtab = curtab;
10799 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010800
10801 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
10802 if (tabvarname != NULL)
10803 {
10804 STRCPY(tabvarname, "t:");
10805 STRCPY(tabvarname + 2, varname);
10806 set_var(tabvarname, varp, TRUE);
10807 vim_free(tabvarname);
10808 }
10809
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010810 /* Restore current tabpage */
10811 if (valid_tabpage(save_curtab))
10812 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010813 }
10814}
10815
10816/*
10817 * "settabwinvar()" function
10818 */
10819 static void
10820f_settabwinvar(typval_T *argvars, typval_T *rettv)
10821{
10822 setwinvar(argvars, rettv, 1);
10823}
10824
10825/*
10826 * "setwinvar()" function
10827 */
10828 static void
10829f_setwinvar(typval_T *argvars, typval_T *rettv)
10830{
10831 setwinvar(argvars, rettv, 0);
10832}
10833
10834#ifdef FEAT_CRYPT
10835/*
10836 * "sha256({string})" function
10837 */
10838 static void
10839f_sha256(typval_T *argvars, typval_T *rettv)
10840{
10841 char_u *p;
10842
10843 p = get_tv_string(&argvars[0]);
10844 rettv->vval.v_string = vim_strsave(
10845 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
10846 rettv->v_type = VAR_STRING;
10847}
10848#endif /* FEAT_CRYPT */
10849
10850/*
10851 * "shellescape({string})" function
10852 */
10853 static void
10854f_shellescape(typval_T *argvars, typval_T *rettv)
10855{
Bram Moolenaar20615522017-06-05 18:46:26 +020010856 int do_special = non_zero_arg(&argvars[1]);
10857
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010858 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar20615522017-06-05 18:46:26 +020010859 get_tv_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010860 rettv->v_type = VAR_STRING;
10861}
10862
10863/*
10864 * shiftwidth() function
10865 */
10866 static void
10867f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
10868{
10869 rettv->vval.v_number = get_sw_value(curbuf);
10870}
10871
10872/*
10873 * "simplify()" function
10874 */
10875 static void
10876f_simplify(typval_T *argvars, typval_T *rettv)
10877{
10878 char_u *p;
10879
10880 p = get_tv_string(&argvars[0]);
10881 rettv->vval.v_string = vim_strsave(p);
10882 simplify_filename(rettv->vval.v_string); /* simplify in place */
10883 rettv->v_type = VAR_STRING;
10884}
10885
10886#ifdef FEAT_FLOAT
10887/*
10888 * "sin()" function
10889 */
10890 static void
10891f_sin(typval_T *argvars, typval_T *rettv)
10892{
10893 float_T f = 0.0;
10894
10895 rettv->v_type = VAR_FLOAT;
10896 if (get_float_arg(argvars, &f) == OK)
10897 rettv->vval.v_float = sin(f);
10898 else
10899 rettv->vval.v_float = 0.0;
10900}
10901
10902/*
10903 * "sinh()" function
10904 */
10905 static void
10906f_sinh(typval_T *argvars, typval_T *rettv)
10907{
10908 float_T f = 0.0;
10909
10910 rettv->v_type = VAR_FLOAT;
10911 if (get_float_arg(argvars, &f) == OK)
10912 rettv->vval.v_float = sinh(f);
10913 else
10914 rettv->vval.v_float = 0.0;
10915}
10916#endif
10917
10918static int
10919#ifdef __BORLANDC__
10920 _RTLENTRYF
10921#endif
10922 item_compare(const void *s1, const void *s2);
10923static int
10924#ifdef __BORLANDC__
10925 _RTLENTRYF
10926#endif
10927 item_compare2(const void *s1, const void *s2);
10928
10929/* struct used in the array that's given to qsort() */
10930typedef struct
10931{
10932 listitem_T *item;
10933 int idx;
10934} sortItem_T;
10935
10936/* struct storing information about current sort */
10937typedef struct
10938{
10939 int item_compare_ic;
10940 int item_compare_numeric;
10941 int item_compare_numbers;
10942#ifdef FEAT_FLOAT
10943 int item_compare_float;
10944#endif
10945 char_u *item_compare_func;
10946 partial_T *item_compare_partial;
10947 dict_T *item_compare_selfdict;
10948 int item_compare_func_err;
10949 int item_compare_keep_zero;
10950} sortinfo_T;
10951static sortinfo_T *sortinfo = NULL;
10952static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
10953#define ITEM_COMPARE_FAIL 999
10954
10955/*
10956 * Compare functions for f_sort() and f_uniq() below.
10957 */
10958 static int
10959#ifdef __BORLANDC__
10960_RTLENTRYF
10961#endif
10962item_compare(const void *s1, const void *s2)
10963{
10964 sortItem_T *si1, *si2;
10965 typval_T *tv1, *tv2;
10966 char_u *p1, *p2;
10967 char_u *tofree1 = NULL, *tofree2 = NULL;
10968 int res;
10969 char_u numbuf1[NUMBUFLEN];
10970 char_u numbuf2[NUMBUFLEN];
10971
10972 si1 = (sortItem_T *)s1;
10973 si2 = (sortItem_T *)s2;
10974 tv1 = &si1->item->li_tv;
10975 tv2 = &si2->item->li_tv;
10976
10977 if (sortinfo->item_compare_numbers)
10978 {
10979 varnumber_T v1 = get_tv_number(tv1);
10980 varnumber_T v2 = get_tv_number(tv2);
10981
10982 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10983 }
10984
10985#ifdef FEAT_FLOAT
10986 if (sortinfo->item_compare_float)
10987 {
10988 float_T v1 = get_tv_float(tv1);
10989 float_T v2 = get_tv_float(tv2);
10990
10991 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10992 }
10993#endif
10994
10995 /* tv2string() puts quotes around a string and allocates memory. Don't do
10996 * that for string variables. Use a single quote when comparing with a
10997 * non-string to do what the docs promise. */
10998 if (tv1->v_type == VAR_STRING)
10999 {
11000 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11001 p1 = (char_u *)"'";
11002 else
11003 p1 = tv1->vval.v_string;
11004 }
11005 else
11006 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
11007 if (tv2->v_type == VAR_STRING)
11008 {
11009 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11010 p2 = (char_u *)"'";
11011 else
11012 p2 = tv2->vval.v_string;
11013 }
11014 else
11015 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
11016 if (p1 == NULL)
11017 p1 = (char_u *)"";
11018 if (p2 == NULL)
11019 p2 = (char_u *)"";
11020 if (!sortinfo->item_compare_numeric)
11021 {
11022 if (sortinfo->item_compare_ic)
11023 res = STRICMP(p1, p2);
11024 else
11025 res = STRCMP(p1, p2);
11026 }
11027 else
11028 {
11029 double n1, n2;
11030 n1 = strtod((char *)p1, (char **)&p1);
11031 n2 = strtod((char *)p2, (char **)&p2);
11032 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
11033 }
11034
11035 /* When the result would be zero, compare the item indexes. Makes the
11036 * sort stable. */
11037 if (res == 0 && !sortinfo->item_compare_keep_zero)
11038 res = si1->idx > si2->idx ? 1 : -1;
11039
11040 vim_free(tofree1);
11041 vim_free(tofree2);
11042 return res;
11043}
11044
11045 static int
11046#ifdef __BORLANDC__
11047_RTLENTRYF
11048#endif
11049item_compare2(const void *s1, const void *s2)
11050{
11051 sortItem_T *si1, *si2;
11052 int res;
11053 typval_T rettv;
11054 typval_T argv[3];
11055 int dummy;
11056 char_u *func_name;
11057 partial_T *partial = sortinfo->item_compare_partial;
11058
11059 /* shortcut after failure in previous call; compare all items equal */
11060 if (sortinfo->item_compare_func_err)
11061 return 0;
11062
11063 si1 = (sortItem_T *)s1;
11064 si2 = (sortItem_T *)s2;
11065
11066 if (partial == NULL)
11067 func_name = sortinfo->item_compare_func;
11068 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020011069 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011070
11071 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
11072 * in the copy without changing the original list items. */
11073 copy_tv(&si1->item->li_tv, &argv[0]);
11074 copy_tv(&si2->item->li_tv, &argv[1]);
11075
11076 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
11077 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020011078 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011079 partial, sortinfo->item_compare_selfdict);
11080 clear_tv(&argv[0]);
11081 clear_tv(&argv[1]);
11082
11083 if (res == FAIL)
11084 res = ITEM_COMPARE_FAIL;
11085 else
11086 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
11087 if (sortinfo->item_compare_func_err)
11088 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
11089 clear_tv(&rettv);
11090
11091 /* When the result would be zero, compare the pointers themselves. Makes
11092 * the sort stable. */
11093 if (res == 0 && !sortinfo->item_compare_keep_zero)
11094 res = si1->idx > si2->idx ? 1 : -1;
11095
11096 return res;
11097}
11098
11099/*
11100 * "sort({list})" function
11101 */
11102 static void
11103do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
11104{
11105 list_T *l;
11106 listitem_T *li;
11107 sortItem_T *ptrs;
11108 sortinfo_T *old_sortinfo;
11109 sortinfo_T info;
11110 long len;
11111 long i;
11112
11113 /* Pointer to current info struct used in compare function. Save and
11114 * restore the current one for nested calls. */
11115 old_sortinfo = sortinfo;
11116 sortinfo = &info;
11117
11118 if (argvars[0].v_type != VAR_LIST)
11119 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
11120 else
11121 {
11122 l = argvars[0].vval.v_list;
11123 if (l == NULL || tv_check_lock(l->lv_lock,
11124 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
11125 TRUE))
11126 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011127 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011128
11129 len = list_len(l);
11130 if (len <= 1)
11131 goto theend; /* short list sorts pretty quickly */
11132
11133 info.item_compare_ic = FALSE;
11134 info.item_compare_numeric = FALSE;
11135 info.item_compare_numbers = FALSE;
11136#ifdef FEAT_FLOAT
11137 info.item_compare_float = FALSE;
11138#endif
11139 info.item_compare_func = NULL;
11140 info.item_compare_partial = NULL;
11141 info.item_compare_selfdict = NULL;
11142 if (argvars[1].v_type != VAR_UNKNOWN)
11143 {
11144 /* optional second argument: {func} */
11145 if (argvars[1].v_type == VAR_FUNC)
11146 info.item_compare_func = argvars[1].vval.v_string;
11147 else if (argvars[1].v_type == VAR_PARTIAL)
11148 info.item_compare_partial = argvars[1].vval.v_partial;
11149 else
11150 {
11151 int error = FALSE;
11152
11153 i = (long)get_tv_number_chk(&argvars[1], &error);
11154 if (error)
11155 goto theend; /* type error; errmsg already given */
11156 if (i == 1)
11157 info.item_compare_ic = TRUE;
11158 else if (argvars[1].v_type != VAR_NUMBER)
11159 info.item_compare_func = get_tv_string(&argvars[1]);
11160 else if (i != 0)
11161 {
11162 EMSG(_(e_invarg));
11163 goto theend;
11164 }
11165 if (info.item_compare_func != NULL)
11166 {
11167 if (*info.item_compare_func == NUL)
11168 {
11169 /* empty string means default sort */
11170 info.item_compare_func = NULL;
11171 }
11172 else if (STRCMP(info.item_compare_func, "n") == 0)
11173 {
11174 info.item_compare_func = NULL;
11175 info.item_compare_numeric = TRUE;
11176 }
11177 else if (STRCMP(info.item_compare_func, "N") == 0)
11178 {
11179 info.item_compare_func = NULL;
11180 info.item_compare_numbers = TRUE;
11181 }
11182#ifdef FEAT_FLOAT
11183 else if (STRCMP(info.item_compare_func, "f") == 0)
11184 {
11185 info.item_compare_func = NULL;
11186 info.item_compare_float = TRUE;
11187 }
11188#endif
11189 else if (STRCMP(info.item_compare_func, "i") == 0)
11190 {
11191 info.item_compare_func = NULL;
11192 info.item_compare_ic = TRUE;
11193 }
11194 }
11195 }
11196
11197 if (argvars[2].v_type != VAR_UNKNOWN)
11198 {
11199 /* optional third argument: {dict} */
11200 if (argvars[2].v_type != VAR_DICT)
11201 {
11202 EMSG(_(e_dictreq));
11203 goto theend;
11204 }
11205 info.item_compare_selfdict = argvars[2].vval.v_dict;
11206 }
11207 }
11208
11209 /* Make an array with each entry pointing to an item in the List. */
11210 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
11211 if (ptrs == NULL)
11212 goto theend;
11213
11214 i = 0;
11215 if (sort)
11216 {
11217 /* sort(): ptrs will be the list to sort */
11218 for (li = l->lv_first; li != NULL; li = li->li_next)
11219 {
11220 ptrs[i].item = li;
11221 ptrs[i].idx = i;
11222 ++i;
11223 }
11224
11225 info.item_compare_func_err = FALSE;
11226 info.item_compare_keep_zero = FALSE;
11227 /* test the compare function */
11228 if ((info.item_compare_func != NULL
11229 || info.item_compare_partial != NULL)
11230 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11231 == ITEM_COMPARE_FAIL)
11232 EMSG(_("E702: Sort compare function failed"));
11233 else
11234 {
11235 /* Sort the array with item pointers. */
11236 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
11237 info.item_compare_func == NULL
11238 && info.item_compare_partial == NULL
11239 ? item_compare : item_compare2);
11240
11241 if (!info.item_compare_func_err)
11242 {
11243 /* Clear the List and append the items in sorted order. */
11244 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
11245 l->lv_len = 0;
11246 for (i = 0; i < len; ++i)
11247 list_append(l, ptrs[i].item);
11248 }
11249 }
11250 }
11251 else
11252 {
11253 int (*item_compare_func_ptr)(const void *, const void *);
11254
11255 /* f_uniq(): ptrs will be a stack of items to remove */
11256 info.item_compare_func_err = FALSE;
11257 info.item_compare_keep_zero = TRUE;
11258 item_compare_func_ptr = info.item_compare_func != NULL
11259 || info.item_compare_partial != NULL
11260 ? item_compare2 : item_compare;
11261
11262 for (li = l->lv_first; li != NULL && li->li_next != NULL;
11263 li = li->li_next)
11264 {
11265 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
11266 == 0)
11267 ptrs[i++].item = li;
11268 if (info.item_compare_func_err)
11269 {
11270 EMSG(_("E882: Uniq compare function failed"));
11271 break;
11272 }
11273 }
11274
11275 if (!info.item_compare_func_err)
11276 {
11277 while (--i >= 0)
11278 {
11279 li = ptrs[i].item->li_next;
11280 ptrs[i].item->li_next = li->li_next;
11281 if (li->li_next != NULL)
11282 li->li_next->li_prev = ptrs[i].item;
11283 else
11284 l->lv_last = ptrs[i].item;
11285 list_fix_watch(l, li);
11286 listitem_free(li);
11287 l->lv_len--;
11288 }
11289 }
11290 }
11291
11292 vim_free(ptrs);
11293 }
11294theend:
11295 sortinfo = old_sortinfo;
11296}
11297
11298/*
11299 * "sort({list})" function
11300 */
11301 static void
11302f_sort(typval_T *argvars, typval_T *rettv)
11303{
11304 do_sort_uniq(argvars, rettv, TRUE);
11305}
11306
11307/*
11308 * "uniq({list})" function
11309 */
11310 static void
11311f_uniq(typval_T *argvars, typval_T *rettv)
11312{
11313 do_sort_uniq(argvars, rettv, FALSE);
11314}
11315
11316/*
11317 * "soundfold({word})" function
11318 */
11319 static void
11320f_soundfold(typval_T *argvars, typval_T *rettv)
11321{
11322 char_u *s;
11323
11324 rettv->v_type = VAR_STRING;
11325 s = get_tv_string(&argvars[0]);
11326#ifdef FEAT_SPELL
11327 rettv->vval.v_string = eval_soundfold(s);
11328#else
11329 rettv->vval.v_string = vim_strsave(s);
11330#endif
11331}
11332
11333/*
11334 * "spellbadword()" function
11335 */
11336 static void
11337f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
11338{
11339 char_u *word = (char_u *)"";
11340 hlf_T attr = HLF_COUNT;
11341 int len = 0;
11342
11343 if (rettv_list_alloc(rettv) == FAIL)
11344 return;
11345
11346#ifdef FEAT_SPELL
11347 if (argvars[0].v_type == VAR_UNKNOWN)
11348 {
11349 /* Find the start and length of the badly spelled word. */
11350 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
11351 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011352 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011353 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011354 curwin->w_set_curswant = TRUE;
11355 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011356 }
11357 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
11358 {
11359 char_u *str = get_tv_string_chk(&argvars[0]);
11360 int capcol = -1;
11361
11362 if (str != NULL)
11363 {
11364 /* Check the argument for spelling. */
11365 while (*str != NUL)
11366 {
11367 len = spell_check(curwin, str, &attr, &capcol, FALSE);
11368 if (attr != HLF_COUNT)
11369 {
11370 word = str;
11371 break;
11372 }
11373 str += len;
11374 }
11375 }
11376 }
11377#endif
11378
11379 list_append_string(rettv->vval.v_list, word, len);
11380 list_append_string(rettv->vval.v_list, (char_u *)(
11381 attr == HLF_SPB ? "bad" :
11382 attr == HLF_SPR ? "rare" :
11383 attr == HLF_SPL ? "local" :
11384 attr == HLF_SPC ? "caps" :
11385 ""), -1);
11386}
11387
11388/*
11389 * "spellsuggest()" function
11390 */
11391 static void
11392f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
11393{
11394#ifdef FEAT_SPELL
11395 char_u *str;
11396 int typeerr = FALSE;
11397 int maxcount;
11398 garray_T ga;
11399 int i;
11400 listitem_T *li;
11401 int need_capital = FALSE;
11402#endif
11403
11404 if (rettv_list_alloc(rettv) == FAIL)
11405 return;
11406
11407#ifdef FEAT_SPELL
11408 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
11409 {
11410 str = get_tv_string(&argvars[0]);
11411 if (argvars[1].v_type != VAR_UNKNOWN)
11412 {
11413 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
11414 if (maxcount <= 0)
11415 return;
11416 if (argvars[2].v_type != VAR_UNKNOWN)
11417 {
11418 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
11419 if (typeerr)
11420 return;
11421 }
11422 }
11423 else
11424 maxcount = 25;
11425
11426 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
11427
11428 for (i = 0; i < ga.ga_len; ++i)
11429 {
11430 str = ((char_u **)ga.ga_data)[i];
11431
11432 li = listitem_alloc();
11433 if (li == NULL)
11434 vim_free(str);
11435 else
11436 {
11437 li->li_tv.v_type = VAR_STRING;
11438 li->li_tv.v_lock = 0;
11439 li->li_tv.vval.v_string = str;
11440 list_append(rettv->vval.v_list, li);
11441 }
11442 }
11443 ga_clear(&ga);
11444 }
11445#endif
11446}
11447
11448 static void
11449f_split(typval_T *argvars, typval_T *rettv)
11450{
11451 char_u *str;
11452 char_u *end;
11453 char_u *pat = NULL;
11454 regmatch_T regmatch;
11455 char_u patbuf[NUMBUFLEN];
11456 char_u *save_cpo;
11457 int match;
11458 colnr_T col = 0;
11459 int keepempty = FALSE;
11460 int typeerr = FALSE;
11461
11462 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11463 save_cpo = p_cpo;
11464 p_cpo = (char_u *)"";
11465
11466 str = get_tv_string(&argvars[0]);
11467 if (argvars[1].v_type != VAR_UNKNOWN)
11468 {
11469 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11470 if (pat == NULL)
11471 typeerr = TRUE;
11472 if (argvars[2].v_type != VAR_UNKNOWN)
11473 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
11474 }
11475 if (pat == NULL || *pat == NUL)
11476 pat = (char_u *)"[\\x01- ]\\+";
11477
11478 if (rettv_list_alloc(rettv) == FAIL)
11479 return;
11480 if (typeerr)
11481 return;
11482
11483 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11484 if (regmatch.regprog != NULL)
11485 {
11486 regmatch.rm_ic = FALSE;
11487 while (*str != NUL || keepempty)
11488 {
11489 if (*str == NUL)
11490 match = FALSE; /* empty item at the end */
11491 else
11492 match = vim_regexec_nl(&regmatch, str, col);
11493 if (match)
11494 end = regmatch.startp[0];
11495 else
11496 end = str + STRLEN(str);
11497 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
11498 && *str != NUL && match && end < regmatch.endp[0]))
11499 {
11500 if (list_append_string(rettv->vval.v_list, str,
11501 (int)(end - str)) == FAIL)
11502 break;
11503 }
11504 if (!match)
11505 break;
11506 /* Advance to just after the match. */
11507 if (regmatch.endp[0] > str)
11508 col = 0;
11509 else
11510 {
11511 /* Don't get stuck at the same match. */
11512#ifdef FEAT_MBYTE
11513 col = (*mb_ptr2len)(regmatch.endp[0]);
11514#else
11515 col = 1;
11516#endif
11517 }
11518 str = regmatch.endp[0];
11519 }
11520
11521 vim_regfree(regmatch.regprog);
11522 }
11523
11524 p_cpo = save_cpo;
11525}
11526
11527#ifdef FEAT_FLOAT
11528/*
11529 * "sqrt()" function
11530 */
11531 static void
11532f_sqrt(typval_T *argvars, typval_T *rettv)
11533{
11534 float_T f = 0.0;
11535
11536 rettv->v_type = VAR_FLOAT;
11537 if (get_float_arg(argvars, &f) == OK)
11538 rettv->vval.v_float = sqrt(f);
11539 else
11540 rettv->vval.v_float = 0.0;
11541}
11542
11543/*
11544 * "str2float()" function
11545 */
11546 static void
11547f_str2float(typval_T *argvars, typval_T *rettv)
11548{
11549 char_u *p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011550 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011551
Bram Moolenaar08243d22017-01-10 16:12:29 +010011552 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011553 p = skipwhite(p + 1);
11554 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011555 if (isneg)
11556 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011557 rettv->v_type = VAR_FLOAT;
11558}
11559#endif
11560
11561/*
11562 * "str2nr()" function
11563 */
11564 static void
11565f_str2nr(typval_T *argvars, typval_T *rettv)
11566{
11567 int base = 10;
11568 char_u *p;
11569 varnumber_T n;
11570 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010011571 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011572
11573 if (argvars[1].v_type != VAR_UNKNOWN)
11574 {
11575 base = (int)get_tv_number(&argvars[1]);
11576 if (base != 2 && base != 8 && base != 10 && base != 16)
11577 {
11578 EMSG(_(e_invarg));
11579 return;
11580 }
11581 }
11582
11583 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011584 isneg = (*p == '-');
11585 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011586 p = skipwhite(p + 1);
11587 switch (base)
11588 {
11589 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
11590 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
11591 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
11592 default: what = 0;
11593 }
11594 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011595 if (isneg)
11596 rettv->vval.v_number = -n;
11597 else
11598 rettv->vval.v_number = n;
11599
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011600}
11601
11602#ifdef HAVE_STRFTIME
11603/*
11604 * "strftime({format}[, {time}])" function
11605 */
11606 static void
11607f_strftime(typval_T *argvars, typval_T *rettv)
11608{
11609 char_u result_buf[256];
11610 struct tm *curtime;
11611 time_t seconds;
11612 char_u *p;
11613
11614 rettv->v_type = VAR_STRING;
11615
11616 p = get_tv_string(&argvars[0]);
11617 if (argvars[1].v_type == VAR_UNKNOWN)
11618 seconds = time(NULL);
11619 else
11620 seconds = (time_t)get_tv_number(&argvars[1]);
11621 curtime = localtime(&seconds);
11622 /* MSVC returns NULL for an invalid value of seconds. */
11623 if (curtime == NULL)
11624 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
11625 else
11626 {
11627# ifdef FEAT_MBYTE
11628 vimconv_T conv;
11629 char_u *enc;
11630
11631 conv.vc_type = CONV_NONE;
11632 enc = enc_locale();
11633 convert_setup(&conv, p_enc, enc);
11634 if (conv.vc_type != CONV_NONE)
11635 p = string_convert(&conv, p, NULL);
11636# endif
11637 if (p != NULL)
11638 (void)strftime((char *)result_buf, sizeof(result_buf),
11639 (char *)p, curtime);
11640 else
11641 result_buf[0] = NUL;
11642
11643# ifdef FEAT_MBYTE
11644 if (conv.vc_type != CONV_NONE)
11645 vim_free(p);
11646 convert_setup(&conv, enc, p_enc);
11647 if (conv.vc_type != CONV_NONE)
11648 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
11649 else
11650# endif
11651 rettv->vval.v_string = vim_strsave(result_buf);
11652
11653# ifdef FEAT_MBYTE
11654 /* Release conversion descriptors */
11655 convert_setup(&conv, NULL, NULL);
11656 vim_free(enc);
11657# endif
11658 }
11659}
11660#endif
11661
11662/*
11663 * "strgetchar()" function
11664 */
11665 static void
11666f_strgetchar(typval_T *argvars, typval_T *rettv)
11667{
11668 char_u *str;
11669 int len;
11670 int error = FALSE;
11671 int charidx;
11672
11673 rettv->vval.v_number = -1;
11674 str = get_tv_string_chk(&argvars[0]);
11675 if (str == NULL)
11676 return;
11677 len = (int)STRLEN(str);
11678 charidx = (int)get_tv_number_chk(&argvars[1], &error);
11679 if (error)
11680 return;
11681#ifdef FEAT_MBYTE
11682 {
11683 int byteidx = 0;
11684
11685 while (charidx >= 0 && byteidx < len)
11686 {
11687 if (charidx == 0)
11688 {
11689 rettv->vval.v_number = mb_ptr2char(str + byteidx);
11690 break;
11691 }
11692 --charidx;
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011693 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011694 }
11695 }
11696#else
11697 if (charidx < len)
11698 rettv->vval.v_number = str[charidx];
11699#endif
11700}
11701
11702/*
11703 * "stridx()" function
11704 */
11705 static void
11706f_stridx(typval_T *argvars, typval_T *rettv)
11707{
11708 char_u buf[NUMBUFLEN];
11709 char_u *needle;
11710 char_u *haystack;
11711 char_u *save_haystack;
11712 char_u *pos;
11713 int start_idx;
11714
11715 needle = get_tv_string_chk(&argvars[1]);
11716 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
11717 rettv->vval.v_number = -1;
11718 if (needle == NULL || haystack == NULL)
11719 return; /* type error; errmsg already given */
11720
11721 if (argvars[2].v_type != VAR_UNKNOWN)
11722 {
11723 int error = FALSE;
11724
11725 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
11726 if (error || start_idx >= (int)STRLEN(haystack))
11727 return;
11728 if (start_idx >= 0)
11729 haystack += start_idx;
11730 }
11731
11732 pos = (char_u *)strstr((char *)haystack, (char *)needle);
11733 if (pos != NULL)
11734 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
11735}
11736
11737/*
11738 * "string()" function
11739 */
11740 static void
11741f_string(typval_T *argvars, typval_T *rettv)
11742{
11743 char_u *tofree;
11744 char_u numbuf[NUMBUFLEN];
11745
11746 rettv->v_type = VAR_STRING;
11747 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
11748 get_copyID());
11749 /* Make a copy if we have a value but it's not in allocated memory. */
11750 if (rettv->vval.v_string != NULL && tofree == NULL)
11751 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
11752}
11753
11754/*
11755 * "strlen()" function
11756 */
11757 static void
11758f_strlen(typval_T *argvars, typval_T *rettv)
11759{
11760 rettv->vval.v_number = (varnumber_T)(STRLEN(
11761 get_tv_string(&argvars[0])));
11762}
11763
11764/*
11765 * "strchars()" function
11766 */
11767 static void
11768f_strchars(typval_T *argvars, typval_T *rettv)
11769{
11770 char_u *s = get_tv_string(&argvars[0]);
11771 int skipcc = 0;
11772#ifdef FEAT_MBYTE
11773 varnumber_T len = 0;
11774 int (*func_mb_ptr2char_adv)(char_u **pp);
11775#endif
11776
11777 if (argvars[1].v_type != VAR_UNKNOWN)
11778 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
11779 if (skipcc < 0 || skipcc > 1)
11780 EMSG(_(e_invarg));
11781 else
11782 {
11783#ifdef FEAT_MBYTE
11784 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
11785 while (*s != NUL)
11786 {
11787 func_mb_ptr2char_adv(&s);
11788 ++len;
11789 }
11790 rettv->vval.v_number = len;
11791#else
11792 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
11793#endif
11794 }
11795}
11796
11797/*
11798 * "strdisplaywidth()" function
11799 */
11800 static void
11801f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
11802{
11803 char_u *s = get_tv_string(&argvars[0]);
11804 int col = 0;
11805
11806 if (argvars[1].v_type != VAR_UNKNOWN)
11807 col = (int)get_tv_number(&argvars[1]);
11808
11809 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
11810}
11811
11812/*
11813 * "strwidth()" function
11814 */
11815 static void
11816f_strwidth(typval_T *argvars, typval_T *rettv)
11817{
11818 char_u *s = get_tv_string(&argvars[0]);
11819
11820 rettv->vval.v_number = (varnumber_T)(
11821#ifdef FEAT_MBYTE
11822 mb_string2cells(s, -1)
11823#else
11824 STRLEN(s)
11825#endif
11826 );
11827}
11828
11829/*
11830 * "strcharpart()" function
11831 */
11832 static void
11833f_strcharpart(typval_T *argvars, typval_T *rettv)
11834{
11835#ifdef FEAT_MBYTE
11836 char_u *p;
11837 int nchar;
11838 int nbyte = 0;
11839 int charlen;
11840 int len = 0;
11841 int slen;
11842 int error = FALSE;
11843
11844 p = get_tv_string(&argvars[0]);
11845 slen = (int)STRLEN(p);
11846
11847 nchar = (int)get_tv_number_chk(&argvars[1], &error);
11848 if (!error)
11849 {
11850 if (nchar > 0)
11851 while (nchar > 0 && nbyte < slen)
11852 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011853 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011854 --nchar;
11855 }
11856 else
11857 nbyte = nchar;
11858 if (argvars[2].v_type != VAR_UNKNOWN)
11859 {
11860 charlen = (int)get_tv_number(&argvars[2]);
11861 while (charlen > 0 && nbyte + len < slen)
11862 {
11863 int off = nbyte + len;
11864
11865 if (off < 0)
11866 len += 1;
11867 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011868 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011869 --charlen;
11870 }
11871 }
11872 else
11873 len = slen - nbyte; /* default: all bytes that are available. */
11874 }
11875
11876 /*
11877 * Only return the overlap between the specified part and the actual
11878 * string.
11879 */
11880 if (nbyte < 0)
11881 {
11882 len += nbyte;
11883 nbyte = 0;
11884 }
11885 else if (nbyte > slen)
11886 nbyte = slen;
11887 if (len < 0)
11888 len = 0;
11889 else if (nbyte + len > slen)
11890 len = slen - nbyte;
11891
11892 rettv->v_type = VAR_STRING;
11893 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
11894#else
11895 f_strpart(argvars, rettv);
11896#endif
11897}
11898
11899/*
11900 * "strpart()" function
11901 */
11902 static void
11903f_strpart(typval_T *argvars, typval_T *rettv)
11904{
11905 char_u *p;
11906 int n;
11907 int len;
11908 int slen;
11909 int error = FALSE;
11910
11911 p = get_tv_string(&argvars[0]);
11912 slen = (int)STRLEN(p);
11913
11914 n = (int)get_tv_number_chk(&argvars[1], &error);
11915 if (error)
11916 len = 0;
11917 else if (argvars[2].v_type != VAR_UNKNOWN)
11918 len = (int)get_tv_number(&argvars[2]);
11919 else
11920 len = slen - n; /* default len: all bytes that are available. */
11921
11922 /*
11923 * Only return the overlap between the specified part and the actual
11924 * string.
11925 */
11926 if (n < 0)
11927 {
11928 len += n;
11929 n = 0;
11930 }
11931 else if (n > slen)
11932 n = slen;
11933 if (len < 0)
11934 len = 0;
11935 else if (n + len > slen)
11936 len = slen - n;
11937
11938 rettv->v_type = VAR_STRING;
11939 rettv->vval.v_string = vim_strnsave(p + n, len);
11940}
11941
11942/*
11943 * "strridx()" function
11944 */
11945 static void
11946f_strridx(typval_T *argvars, typval_T *rettv)
11947{
11948 char_u buf[NUMBUFLEN];
11949 char_u *needle;
11950 char_u *haystack;
11951 char_u *rest;
11952 char_u *lastmatch = NULL;
11953 int haystack_len, end_idx;
11954
11955 needle = get_tv_string_chk(&argvars[1]);
11956 haystack = get_tv_string_buf_chk(&argvars[0], buf);
11957
11958 rettv->vval.v_number = -1;
11959 if (needle == NULL || haystack == NULL)
11960 return; /* type error; errmsg already given */
11961
11962 haystack_len = (int)STRLEN(haystack);
11963 if (argvars[2].v_type != VAR_UNKNOWN)
11964 {
11965 /* Third argument: upper limit for index */
11966 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
11967 if (end_idx < 0)
11968 return; /* can never find a match */
11969 }
11970 else
11971 end_idx = haystack_len;
11972
11973 if (*needle == NUL)
11974 {
11975 /* Empty string matches past the end. */
11976 lastmatch = haystack + end_idx;
11977 }
11978 else
11979 {
11980 for (rest = haystack; *rest != '\0'; ++rest)
11981 {
11982 rest = (char_u *)strstr((char *)rest, (char *)needle);
11983 if (rest == NULL || rest > haystack + end_idx)
11984 break;
11985 lastmatch = rest;
11986 }
11987 }
11988
11989 if (lastmatch == NULL)
11990 rettv->vval.v_number = -1;
11991 else
11992 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
11993}
11994
11995/*
11996 * "strtrans()" function
11997 */
11998 static void
11999f_strtrans(typval_T *argvars, typval_T *rettv)
12000{
12001 rettv->v_type = VAR_STRING;
12002 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
12003}
12004
12005/*
12006 * "submatch()" function
12007 */
12008 static void
12009f_submatch(typval_T *argvars, typval_T *rettv)
12010{
12011 int error = FALSE;
12012 int no;
12013 int retList = 0;
12014
12015 no = (int)get_tv_number_chk(&argvars[0], &error);
12016 if (error)
12017 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012018 if (no < 0 || no >= NSUBEXP)
12019 {
Bram Moolenaar79518e22017-02-17 16:31:35 +010012020 EMSGN(_("E935: invalid submatch number: %d"), no);
12021 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012022 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012023 if (argvars[1].v_type != VAR_UNKNOWN)
12024 retList = (int)get_tv_number_chk(&argvars[1], &error);
12025 if (error)
12026 return;
12027
12028 if (retList == 0)
12029 {
12030 rettv->v_type = VAR_STRING;
12031 rettv->vval.v_string = reg_submatch(no);
12032 }
12033 else
12034 {
12035 rettv->v_type = VAR_LIST;
12036 rettv->vval.v_list = reg_submatch_list(no);
12037 }
12038}
12039
12040/*
12041 * "substitute()" function
12042 */
12043 static void
12044f_substitute(typval_T *argvars, typval_T *rettv)
12045{
12046 char_u patbuf[NUMBUFLEN];
12047 char_u subbuf[NUMBUFLEN];
12048 char_u flagsbuf[NUMBUFLEN];
12049
12050 char_u *str = get_tv_string_chk(&argvars[0]);
12051 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012052 char_u *sub = NULL;
12053 typval_T *expr = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012054 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
12055
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012056 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
12057 expr = &argvars[2];
12058 else
12059 sub = get_tv_string_buf_chk(&argvars[2], subbuf);
12060
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012061 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012062 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
12063 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012064 rettv->vval.v_string = NULL;
12065 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012066 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012067}
12068
12069/*
12070 * "synID(lnum, col, trans)" function
12071 */
12072 static void
12073f_synID(typval_T *argvars UNUSED, typval_T *rettv)
12074{
12075 int id = 0;
12076#ifdef FEAT_SYN_HL
12077 linenr_T lnum;
12078 colnr_T col;
12079 int trans;
12080 int transerr = FALSE;
12081
12082 lnum = get_tv_lnum(argvars); /* -1 on type error */
12083 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12084 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
12085
12086 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12087 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
12088 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
12089#endif
12090
12091 rettv->vval.v_number = id;
12092}
12093
12094/*
12095 * "synIDattr(id, what [, mode])" function
12096 */
12097 static void
12098f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
12099{
12100 char_u *p = NULL;
12101#ifdef FEAT_SYN_HL
12102 int id;
12103 char_u *what;
12104 char_u *mode;
12105 char_u modebuf[NUMBUFLEN];
12106 int modec;
12107
12108 id = (int)get_tv_number(&argvars[0]);
12109 what = get_tv_string(&argvars[1]);
12110 if (argvars[2].v_type != VAR_UNKNOWN)
12111 {
12112 mode = get_tv_string_buf(&argvars[2], modebuf);
12113 modec = TOLOWER_ASC(mode[0]);
12114 if (modec != 't' && modec != 'c' && modec != 'g')
12115 modec = 0; /* replace invalid with current */
12116 }
12117 else
12118 {
12119#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
12120 if (USE_24BIT)
12121 modec = 'g';
12122 else
12123#endif
12124 if (t_colors > 1)
12125 modec = 'c';
12126 else
12127 modec = 't';
12128 }
12129
12130
12131 switch (TOLOWER_ASC(what[0]))
12132 {
12133 case 'b':
12134 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12135 p = highlight_color(id, what, modec);
12136 else /* bold */
12137 p = highlight_has_attr(id, HL_BOLD, modec);
12138 break;
12139
12140 case 'f': /* fg[#] or font */
12141 p = highlight_color(id, what, modec);
12142 break;
12143
12144 case 'i':
12145 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
12146 p = highlight_has_attr(id, HL_INVERSE, modec);
12147 else /* italic */
12148 p = highlight_has_attr(id, HL_ITALIC, modec);
12149 break;
12150
12151 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020012152 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012153 break;
12154
12155 case 'r': /* reverse */
12156 p = highlight_has_attr(id, HL_INVERSE, modec);
12157 break;
12158
12159 case 's':
12160 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
12161 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020012162 /* strikeout */
12163 else if (TOLOWER_ASC(what[1]) == 't' &&
12164 TOLOWER_ASC(what[2]) == 'r')
12165 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012166 else /* standout */
12167 p = highlight_has_attr(id, HL_STANDOUT, modec);
12168 break;
12169
12170 case 'u':
12171 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
12172 /* underline */
12173 p = highlight_has_attr(id, HL_UNDERLINE, modec);
12174 else
12175 /* undercurl */
12176 p = highlight_has_attr(id, HL_UNDERCURL, modec);
12177 break;
12178 }
12179
12180 if (p != NULL)
12181 p = vim_strsave(p);
12182#endif
12183 rettv->v_type = VAR_STRING;
12184 rettv->vval.v_string = p;
12185}
12186
12187/*
12188 * "synIDtrans(id)" function
12189 */
12190 static void
12191f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
12192{
12193 int id;
12194
12195#ifdef FEAT_SYN_HL
12196 id = (int)get_tv_number(&argvars[0]);
12197
12198 if (id > 0)
12199 id = syn_get_final_id(id);
12200 else
12201#endif
12202 id = 0;
12203
12204 rettv->vval.v_number = id;
12205}
12206
12207/*
12208 * "synconcealed(lnum, col)" function
12209 */
12210 static void
12211f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
12212{
12213#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12214 linenr_T lnum;
12215 colnr_T col;
12216 int syntax_flags = 0;
12217 int cchar;
12218 int matchid = 0;
12219 char_u str[NUMBUFLEN];
12220#endif
12221
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012222 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012223
12224#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12225 lnum = get_tv_lnum(argvars); /* -1 on type error */
12226 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12227
12228 vim_memset(str, NUL, sizeof(str));
12229
12230 if (rettv_list_alloc(rettv) != FAIL)
12231 {
12232 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12233 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12234 && curwin->w_p_cole > 0)
12235 {
12236 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
12237 syntax_flags = get_syntax_info(&matchid);
12238
12239 /* get the conceal character */
12240 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
12241 {
12242 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020012243 if (cchar == NUL && curwin->w_p_cole == 1)
12244 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012245 if (cchar != NUL)
12246 {
12247# ifdef FEAT_MBYTE
12248 if (has_mbyte)
12249 (*mb_char2bytes)(cchar, str);
12250 else
12251# endif
12252 str[0] = cchar;
12253 }
12254 }
12255 }
12256
12257 list_append_number(rettv->vval.v_list,
12258 (syntax_flags & HL_CONCEAL) != 0);
12259 /* -1 to auto-determine strlen */
12260 list_append_string(rettv->vval.v_list, str, -1);
12261 list_append_number(rettv->vval.v_list, matchid);
12262 }
12263#endif
12264}
12265
12266/*
12267 * "synstack(lnum, col)" function
12268 */
12269 static void
12270f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
12271{
12272#ifdef FEAT_SYN_HL
12273 linenr_T lnum;
12274 colnr_T col;
12275 int i;
12276 int id;
12277#endif
12278
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012279 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012280
12281#ifdef FEAT_SYN_HL
12282 lnum = get_tv_lnum(argvars); /* -1 on type error */
12283 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12284
12285 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12286 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12287 && rettv_list_alloc(rettv) != FAIL)
12288 {
12289 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
12290 for (i = 0; ; ++i)
12291 {
12292 id = syn_get_stack_item(i);
12293 if (id < 0)
12294 break;
12295 if (list_append_number(rettv->vval.v_list, id) == FAIL)
12296 break;
12297 }
12298 }
12299#endif
12300}
12301
12302 static void
12303get_cmd_output_as_rettv(
12304 typval_T *argvars,
12305 typval_T *rettv,
12306 int retlist)
12307{
12308 char_u *res = NULL;
12309 char_u *p;
12310 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012311 int err = FALSE;
12312 FILE *fd;
12313 list_T *list = NULL;
12314 int flags = SHELL_SILENT;
12315
12316 rettv->v_type = VAR_STRING;
12317 rettv->vval.v_string = NULL;
12318 if (check_restricted() || check_secure())
12319 goto errret;
12320
12321 if (argvars[1].v_type != VAR_UNKNOWN)
12322 {
12323 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010012324 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012325 * command.
12326 */
12327 if ((infile = vim_tempname('i', TRUE)) == NULL)
12328 {
12329 EMSG(_(e_notmp));
12330 goto errret;
12331 }
12332
12333 fd = mch_fopen((char *)infile, WRITEBIN);
12334 if (fd == NULL)
12335 {
12336 EMSG2(_(e_notopen), infile);
12337 goto errret;
12338 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010012339 if (argvars[1].v_type == VAR_NUMBER)
12340 {
12341 linenr_T lnum;
12342 buf_T *buf;
12343
12344 buf = buflist_findnr(argvars[1].vval.v_number);
12345 if (buf == NULL)
12346 {
12347 EMSGN(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010012348 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010012349 goto errret;
12350 }
12351
12352 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
12353 {
12354 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
12355 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
12356 {
12357 err = TRUE;
12358 break;
12359 }
12360 if (putc(NL, fd) == EOF)
12361 {
12362 err = TRUE;
12363 break;
12364 }
12365 }
12366 }
12367 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012368 {
12369 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
12370 err = TRUE;
12371 }
12372 else
12373 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010012374 size_t len;
12375 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012376
12377 p = get_tv_string_buf_chk(&argvars[1], buf);
12378 if (p == NULL)
12379 {
12380 fclose(fd);
12381 goto errret; /* type error; errmsg already given */
12382 }
12383 len = STRLEN(p);
12384 if (len > 0 && fwrite(p, len, 1, fd) != 1)
12385 err = TRUE;
12386 }
12387 if (fclose(fd) != 0)
12388 err = TRUE;
12389 if (err)
12390 {
12391 EMSG(_("E677: Error writing temp file"));
12392 goto errret;
12393 }
12394 }
12395
12396 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
12397 * echoes typeahead, that messes up the display. */
12398 if (!msg_silent)
12399 flags += SHELL_COOKED;
12400
12401 if (retlist)
12402 {
12403 int len;
12404 listitem_T *li;
12405 char_u *s = NULL;
12406 char_u *start;
12407 char_u *end;
12408 int i;
12409
12410 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
12411 if (res == NULL)
12412 goto errret;
12413
12414 list = list_alloc();
12415 if (list == NULL)
12416 goto errret;
12417
12418 for (i = 0; i < len; ++i)
12419 {
12420 start = res + i;
12421 while (i < len && res[i] != NL)
12422 ++i;
12423 end = res + i;
12424
12425 s = alloc((unsigned)(end - start + 1));
12426 if (s == NULL)
12427 goto errret;
12428
12429 for (p = s; start < end; ++p, ++start)
12430 *p = *start == NUL ? NL : *start;
12431 *p = NUL;
12432
12433 li = listitem_alloc();
12434 if (li == NULL)
12435 {
12436 vim_free(s);
12437 goto errret;
12438 }
12439 li->li_tv.v_type = VAR_STRING;
12440 li->li_tv.v_lock = 0;
12441 li->li_tv.vval.v_string = s;
12442 list_append(list, li);
12443 }
12444
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012445 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012446 list = NULL;
12447 }
12448 else
12449 {
12450 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
12451#ifdef USE_CR
12452 /* translate <CR> into <NL> */
12453 if (res != NULL)
12454 {
12455 char_u *s;
12456
12457 for (s = res; *s; ++s)
12458 {
12459 if (*s == CAR)
12460 *s = NL;
12461 }
12462 }
12463#else
12464# ifdef USE_CRNL
12465 /* translate <CR><NL> into <NL> */
12466 if (res != NULL)
12467 {
12468 char_u *s, *d;
12469
12470 d = res;
12471 for (s = res; *s; ++s)
12472 {
12473 if (s[0] == CAR && s[1] == NL)
12474 ++s;
12475 *d++ = *s;
12476 }
12477 *d = NUL;
12478 }
12479# endif
12480#endif
12481 rettv->vval.v_string = res;
12482 res = NULL;
12483 }
12484
12485errret:
12486 if (infile != NULL)
12487 {
12488 mch_remove(infile);
12489 vim_free(infile);
12490 }
12491 if (res != NULL)
12492 vim_free(res);
12493 if (list != NULL)
12494 list_free(list);
12495}
12496
12497/*
12498 * "system()" function
12499 */
12500 static void
12501f_system(typval_T *argvars, typval_T *rettv)
12502{
12503 get_cmd_output_as_rettv(argvars, rettv, FALSE);
12504}
12505
12506/*
12507 * "systemlist()" function
12508 */
12509 static void
12510f_systemlist(typval_T *argvars, typval_T *rettv)
12511{
12512 get_cmd_output_as_rettv(argvars, rettv, TRUE);
12513}
12514
12515/*
12516 * "tabpagebuflist()" function
12517 */
12518 static void
12519f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12520{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012521 tabpage_T *tp;
12522 win_T *wp = NULL;
12523
12524 if (argvars[0].v_type == VAR_UNKNOWN)
12525 wp = firstwin;
12526 else
12527 {
12528 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12529 if (tp != NULL)
12530 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12531 }
12532 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
12533 {
12534 for (; wp != NULL; wp = wp->w_next)
12535 if (list_append_number(rettv->vval.v_list,
12536 wp->w_buffer->b_fnum) == FAIL)
12537 break;
12538 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012539}
12540
12541
12542/*
12543 * "tabpagenr()" function
12544 */
12545 static void
12546f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
12547{
12548 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012549 char_u *arg;
12550
12551 if (argvars[0].v_type != VAR_UNKNOWN)
12552 {
12553 arg = get_tv_string_chk(&argvars[0]);
12554 nr = 0;
12555 if (arg != NULL)
12556 {
12557 if (STRCMP(arg, "$") == 0)
12558 nr = tabpage_index(NULL) - 1;
12559 else
12560 EMSG2(_(e_invexpr2), arg);
12561 }
12562 }
12563 else
12564 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012565 rettv->vval.v_number = nr;
12566}
12567
12568
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012569static int get_winnr(tabpage_T *tp, typval_T *argvar);
12570
12571/*
12572 * Common code for tabpagewinnr() and winnr().
12573 */
12574 static int
12575get_winnr(tabpage_T *tp, typval_T *argvar)
12576{
12577 win_T *twin;
12578 int nr = 1;
12579 win_T *wp;
12580 char_u *arg;
12581
12582 twin = (tp == curtab) ? curwin : tp->tp_curwin;
12583 if (argvar->v_type != VAR_UNKNOWN)
12584 {
12585 arg = get_tv_string_chk(argvar);
12586 if (arg == NULL)
12587 nr = 0; /* type error; errmsg already given */
12588 else if (STRCMP(arg, "$") == 0)
12589 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
12590 else if (STRCMP(arg, "#") == 0)
12591 {
12592 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
12593 if (twin == NULL)
12594 nr = 0;
12595 }
12596 else
12597 {
12598 EMSG2(_(e_invexpr2), arg);
12599 nr = 0;
12600 }
12601 }
12602
12603 if (nr > 0)
12604 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12605 wp != twin; wp = wp->w_next)
12606 {
12607 if (wp == NULL)
12608 {
12609 /* didn't find it in this tabpage */
12610 nr = 0;
12611 break;
12612 }
12613 ++nr;
12614 }
12615 return nr;
12616}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012617
12618/*
12619 * "tabpagewinnr()" function
12620 */
12621 static void
12622f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
12623{
12624 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012625 tabpage_T *tp;
12626
12627 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12628 if (tp == NULL)
12629 nr = 0;
12630 else
12631 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012632 rettv->vval.v_number = nr;
12633}
12634
12635
12636/*
12637 * "tagfiles()" function
12638 */
12639 static void
12640f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
12641{
12642 char_u *fname;
12643 tagname_T tn;
12644 int first;
12645
12646 if (rettv_list_alloc(rettv) == FAIL)
12647 return;
12648 fname = alloc(MAXPATHL);
12649 if (fname == NULL)
12650 return;
12651
12652 for (first = TRUE; ; first = FALSE)
12653 if (get_tagfname(&tn, first, fname) == FAIL
12654 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
12655 break;
12656 tagname_free(&tn);
12657 vim_free(fname);
12658}
12659
12660/*
12661 * "taglist()" function
12662 */
12663 static void
12664f_taglist(typval_T *argvars, typval_T *rettv)
12665{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012666 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012667 char_u *tag_pattern;
12668
12669 tag_pattern = get_tv_string(&argvars[0]);
12670
12671 rettv->vval.v_number = FALSE;
12672 if (*tag_pattern == NUL)
12673 return;
12674
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012675 if (argvars[1].v_type != VAR_UNKNOWN)
12676 fname = get_tv_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012677 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012678 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012679}
12680
12681/*
12682 * "tempname()" function
12683 */
12684 static void
12685f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
12686{
12687 static int x = 'A';
12688
12689 rettv->v_type = VAR_STRING;
12690 rettv->vval.v_string = vim_tempname(x, FALSE);
12691
12692 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
12693 * names. Skip 'I' and 'O', they are used for shell redirection. */
12694 do
12695 {
12696 if (x == 'Z')
12697 x = '0';
12698 else if (x == '9')
12699 x = 'A';
12700 else
12701 {
12702#ifdef EBCDIC
12703 if (x == 'I')
12704 x = 'J';
12705 else if (x == 'R')
12706 x = 'S';
12707 else
12708#endif
12709 ++x;
12710 }
12711 } while (x == 'I' || x == 'O');
12712}
12713
12714#ifdef FEAT_FLOAT
12715/*
12716 * "tan()" function
12717 */
12718 static void
12719f_tan(typval_T *argvars, typval_T *rettv)
12720{
12721 float_T f = 0.0;
12722
12723 rettv->v_type = VAR_FLOAT;
12724 if (get_float_arg(argvars, &f) == OK)
12725 rettv->vval.v_float = tan(f);
12726 else
12727 rettv->vval.v_float = 0.0;
12728}
12729
12730/*
12731 * "tanh()" function
12732 */
12733 static void
12734f_tanh(typval_T *argvars, typval_T *rettv)
12735{
12736 float_T f = 0.0;
12737
12738 rettv->v_type = VAR_FLOAT;
12739 if (get_float_arg(argvars, &f) == OK)
12740 rettv->vval.v_float = tanh(f);
12741 else
12742 rettv->vval.v_float = 0.0;
12743}
12744#endif
12745
12746/*
12747 * "test_alloc_fail(id, countdown, repeat)" function
12748 */
12749 static void
12750f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
12751{
12752 if (argvars[0].v_type != VAR_NUMBER
12753 || argvars[0].vval.v_number <= 0
12754 || argvars[1].v_type != VAR_NUMBER
12755 || argvars[1].vval.v_number < 0
12756 || argvars[2].v_type != VAR_NUMBER)
12757 EMSG(_(e_invarg));
12758 else
12759 {
12760 alloc_fail_id = argvars[0].vval.v_number;
12761 if (alloc_fail_id >= aid_last)
12762 EMSG(_(e_invarg));
12763 alloc_fail_countdown = argvars[1].vval.v_number;
12764 alloc_fail_repeat = argvars[2].vval.v_number;
12765 did_outofmem_msg = FALSE;
12766 }
12767}
12768
12769/*
12770 * "test_autochdir()"
12771 */
12772 static void
12773f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12774{
12775#if defined(FEAT_AUTOCHDIR)
12776 test_autochdir = TRUE;
12777#endif
12778}
12779
12780/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020012781 * "test_feedinput()"
12782 */
12783 static void
12784f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
12785{
12786#ifdef USE_INPUT_BUF
12787 char_u *val = get_tv_string_chk(&argvars[0]);
12788
12789 if (val != NULL)
12790 {
12791 trash_input_buf();
12792 add_to_input_buf_csi(val, (int)STRLEN(val));
12793 }
12794#endif
12795}
12796
12797/*
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012798 * "test_disable({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012799 */
12800 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012801f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012802{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012803 char_u *name = (char_u *)"";
12804 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012805 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012806
12807 if (argvars[0].v_type != VAR_STRING
12808 || (argvars[1].v_type) != VAR_NUMBER)
12809 EMSG(_(e_invarg));
12810 else
12811 {
12812 name = get_tv_string_chk(&argvars[0]);
12813 val = (int)get_tv_number(&argvars[1]);
12814
12815 if (STRCMP(name, (char_u *)"redraw") == 0)
12816 disable_redraw_for_testing = val;
12817 else if (STRCMP(name, (char_u *)"char_avail") == 0)
12818 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012819 else if (STRCMP(name, (char_u *)"starting") == 0)
12820 {
12821 if (val)
12822 {
12823 if (save_starting < 0)
12824 save_starting = starting;
12825 starting = 0;
12826 }
12827 else
12828 {
12829 starting = save_starting;
12830 save_starting = -1;
12831 }
12832 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012833 else if (STRCMP(name, (char_u *)"ALL") == 0)
12834 {
12835 disable_char_avail_for_testing = FALSE;
12836 disable_redraw_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012837 if (save_starting >= 0)
12838 {
12839 starting = save_starting;
12840 save_starting = -1;
12841 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012842 }
12843 else
12844 EMSG2(_(e_invarg2), name);
12845 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012846}
12847
12848/*
12849 * "test_garbagecollect_now()" function
12850 */
12851 static void
12852f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12853{
12854 /* This is dangerous, any Lists and Dicts used internally may be freed
12855 * while still in use. */
12856 garbage_collect(TRUE);
12857}
12858
Bram Moolenaare0c31f62017-03-01 15:07:05 +010012859/*
12860 * "test_ignore_error()" function
12861 */
12862 static void
12863f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
12864{
12865 ignore_error_for_testing(get_tv_string(&argvars[0]));
12866}
12867
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012868#ifdef FEAT_JOB_CHANNEL
12869 static void
12870f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
12871{
12872 rettv->v_type = VAR_CHANNEL;
12873 rettv->vval.v_channel = NULL;
12874}
12875#endif
12876
12877 static void
12878f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
12879{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012880 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012881}
12882
12883#ifdef FEAT_JOB_CHANNEL
12884 static void
12885f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
12886{
12887 rettv->v_type = VAR_JOB;
12888 rettv->vval.v_job = NULL;
12889}
12890#endif
12891
12892 static void
12893f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
12894{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012895 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012896}
12897
12898 static void
12899f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
12900{
12901 rettv->v_type = VAR_PARTIAL;
12902 rettv->vval.v_partial = NULL;
12903}
12904
12905 static void
12906f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
12907{
12908 rettv->v_type = VAR_STRING;
12909 rettv->vval.v_string = NULL;
12910}
12911
12912 static void
12913f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
12914{
12915 time_for_testing = (time_t)get_tv_number(&argvars[0]);
12916}
12917
12918#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
12919/*
12920 * Get a callback from "arg". It can be a Funcref or a function name.
12921 * When "arg" is zero return an empty string.
12922 * Return NULL for an invalid argument.
12923 */
12924 char_u *
12925get_callback(typval_T *arg, partial_T **pp)
12926{
12927 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
12928 {
12929 *pp = arg->vval.v_partial;
12930 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012931 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012932 }
12933 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012934 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012935 {
12936 func_ref(arg->vval.v_string);
12937 return arg->vval.v_string;
12938 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012939 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
12940 return (char_u *)"";
12941 EMSG(_("E921: Invalid callback argument"));
12942 return NULL;
12943}
12944
12945/*
Bram Moolenaard5359b22018-04-05 22:44:39 +020012946 * Unref/free "callback" and "partial" returned by get_callback().
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012947 */
12948 void
12949free_callback(char_u *callback, partial_T *partial)
12950{
12951 if (partial != NULL)
12952 partial_unref(partial);
12953 else if (callback != NULL)
12954 {
12955 func_unref(callback);
12956 vim_free(callback);
12957 }
12958}
12959#endif
12960
12961#ifdef FEAT_TIMERS
12962/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020012963 * "timer_info([timer])" function
12964 */
12965 static void
12966f_timer_info(typval_T *argvars, typval_T *rettv)
12967{
12968 timer_T *timer = NULL;
12969
12970 if (rettv_list_alloc(rettv) != OK)
12971 return;
12972 if (argvars[0].v_type != VAR_UNKNOWN)
12973 {
12974 if (argvars[0].v_type != VAR_NUMBER)
12975 EMSG(_(e_number_exp));
12976 else
12977 {
12978 timer = find_timer((int)get_tv_number(&argvars[0]));
12979 if (timer != NULL)
12980 add_timer_info(rettv, timer);
12981 }
12982 }
12983 else
12984 add_timer_info_all(rettv);
12985}
12986
12987/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012988 * "timer_pause(timer, paused)" function
12989 */
12990 static void
12991f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
12992{
12993 timer_T *timer = NULL;
12994 int paused = (int)get_tv_number(&argvars[1]);
12995
12996 if (argvars[0].v_type != VAR_NUMBER)
12997 EMSG(_(e_number_exp));
12998 else
12999 {
13000 timer = find_timer((int)get_tv_number(&argvars[0]));
13001 if (timer != NULL)
13002 timer->tr_paused = paused;
13003 }
13004}
13005
13006/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013007 * "timer_start(time, callback [, options])" function
13008 */
13009 static void
13010f_timer_start(typval_T *argvars, typval_T *rettv)
13011{
Bram Moolenaar75537a92016-09-05 22:45:28 +020013012 long msec = (long)get_tv_number(&argvars[0]);
13013 timer_T *timer;
13014 int repeat = 0;
13015 char_u *callback;
13016 dict_T *dict;
13017 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013018
Bram Moolenaar75537a92016-09-05 22:45:28 +020013019 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013020 if (check_secure())
13021 return;
13022 if (argvars[2].v_type != VAR_UNKNOWN)
13023 {
13024 if (argvars[2].v_type != VAR_DICT
13025 || (dict = argvars[2].vval.v_dict) == NULL)
13026 {
13027 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
13028 return;
13029 }
13030 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
13031 repeat = get_dict_number(dict, (char_u *)"repeat");
13032 }
13033
Bram Moolenaar75537a92016-09-05 22:45:28 +020013034 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013035 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020013036 return;
13037
13038 timer = create_timer(msec, repeat);
13039 if (timer == NULL)
13040 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013041 else
13042 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020013043 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020013044 timer->tr_callback = vim_strsave(callback);
13045 else
13046 /* pointer into the partial */
13047 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020013048 timer->tr_partial = partial;
13049 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013050 }
13051}
13052
13053/*
13054 * "timer_stop(timer)" function
13055 */
13056 static void
13057f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
13058{
13059 timer_T *timer;
13060
13061 if (argvars[0].v_type != VAR_NUMBER)
13062 {
13063 EMSG(_(e_number_exp));
13064 return;
13065 }
13066 timer = find_timer((int)get_tv_number(&argvars[0]));
13067 if (timer != NULL)
13068 stop_timer(timer);
13069}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013070
13071/*
13072 * "timer_stopall()" function
13073 */
13074 static void
13075f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13076{
13077 stop_all_timers();
13078}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013079#endif
13080
13081/*
13082 * "tolower(string)" function
13083 */
13084 static void
13085f_tolower(typval_T *argvars, typval_T *rettv)
13086{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013087 rettv->v_type = VAR_STRING;
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +010013088 rettv->vval.v_string = strlow_save(get_tv_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013089}
13090
13091/*
13092 * "toupper(string)" function
13093 */
13094 static void
13095f_toupper(typval_T *argvars, typval_T *rettv)
13096{
13097 rettv->v_type = VAR_STRING;
13098 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
13099}
13100
13101/*
13102 * "tr(string, fromstr, tostr)" function
13103 */
13104 static void
13105f_tr(typval_T *argvars, typval_T *rettv)
13106{
13107 char_u *in_str;
13108 char_u *fromstr;
13109 char_u *tostr;
13110 char_u *p;
13111#ifdef FEAT_MBYTE
13112 int inlen;
13113 int fromlen;
13114 int tolen;
13115 int idx;
13116 char_u *cpstr;
13117 int cplen;
13118 int first = TRUE;
13119#endif
13120 char_u buf[NUMBUFLEN];
13121 char_u buf2[NUMBUFLEN];
13122 garray_T ga;
13123
13124 in_str = get_tv_string(&argvars[0]);
13125 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
13126 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
13127
13128 /* Default return value: empty string. */
13129 rettv->v_type = VAR_STRING;
13130 rettv->vval.v_string = NULL;
13131 if (fromstr == NULL || tostr == NULL)
13132 return; /* type error; errmsg already given */
13133 ga_init2(&ga, (int)sizeof(char), 80);
13134
13135#ifdef FEAT_MBYTE
13136 if (!has_mbyte)
13137#endif
13138 /* not multi-byte: fromstr and tostr must be the same length */
13139 if (STRLEN(fromstr) != STRLEN(tostr))
13140 {
13141#ifdef FEAT_MBYTE
13142error:
13143#endif
13144 EMSG2(_(e_invarg2), fromstr);
13145 ga_clear(&ga);
13146 return;
13147 }
13148
13149 /* fromstr and tostr have to contain the same number of chars */
13150 while (*in_str != NUL)
13151 {
13152#ifdef FEAT_MBYTE
13153 if (has_mbyte)
13154 {
13155 inlen = (*mb_ptr2len)(in_str);
13156 cpstr = in_str;
13157 cplen = inlen;
13158 idx = 0;
13159 for (p = fromstr; *p != NUL; p += fromlen)
13160 {
13161 fromlen = (*mb_ptr2len)(p);
13162 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
13163 {
13164 for (p = tostr; *p != NUL; p += tolen)
13165 {
13166 tolen = (*mb_ptr2len)(p);
13167 if (idx-- == 0)
13168 {
13169 cplen = tolen;
13170 cpstr = p;
13171 break;
13172 }
13173 }
13174 if (*p == NUL) /* tostr is shorter than fromstr */
13175 goto error;
13176 break;
13177 }
13178 ++idx;
13179 }
13180
13181 if (first && cpstr == in_str)
13182 {
13183 /* Check that fromstr and tostr have the same number of
13184 * (multi-byte) characters. Done only once when a character
13185 * of in_str doesn't appear in fromstr. */
13186 first = FALSE;
13187 for (p = tostr; *p != NUL; p += tolen)
13188 {
13189 tolen = (*mb_ptr2len)(p);
13190 --idx;
13191 }
13192 if (idx != 0)
13193 goto error;
13194 }
13195
13196 (void)ga_grow(&ga, cplen);
13197 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
13198 ga.ga_len += cplen;
13199
13200 in_str += inlen;
13201 }
13202 else
13203#endif
13204 {
13205 /* When not using multi-byte chars we can do it faster. */
13206 p = vim_strchr(fromstr, *in_str);
13207 if (p != NULL)
13208 ga_append(&ga, tostr[p - fromstr]);
13209 else
13210 ga_append(&ga, *in_str);
13211 ++in_str;
13212 }
13213 }
13214
13215 /* add a terminating NUL */
13216 (void)ga_grow(&ga, 1);
13217 ga_append(&ga, NUL);
13218
13219 rettv->vval.v_string = ga.ga_data;
13220}
13221
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010013222/*
13223 * "trim({expr})" function
13224 */
13225 static void
13226f_trim(typval_T *argvars, typval_T *rettv)
13227{
13228 char_u buf1[NUMBUFLEN];
13229 char_u buf2[NUMBUFLEN];
13230 char_u *head = get_tv_string_buf_chk(&argvars[0], buf1);
13231 char_u *mask = NULL;
13232 char_u *tail;
13233 char_u *prev;
13234 char_u *p;
13235 int c1;
13236
13237 rettv->v_type = VAR_STRING;
13238 if (head == NULL)
13239 {
13240 rettv->vval.v_string = NULL;
13241 return;
13242 }
13243
13244 if (argvars[1].v_type == VAR_STRING)
13245 mask = get_tv_string_buf_chk(&argvars[1], buf2);
13246
13247 while (*head != NUL)
13248 {
13249 c1 = PTR2CHAR(head);
13250 if (mask == NULL)
13251 {
13252 if (c1 > ' ' && c1 != 0xa0)
13253 break;
13254 }
13255 else
13256 {
13257 for (p = mask; *p != NUL; MB_PTR_ADV(p))
13258 if (c1 == PTR2CHAR(p))
13259 break;
13260 if (*p == NUL)
13261 break;
13262 }
13263 MB_PTR_ADV(head);
13264 }
13265
13266 for (tail = head + STRLEN(head); tail > head; tail = prev)
13267 {
13268 prev = tail;
13269 MB_PTR_BACK(head, prev);
13270 c1 = PTR2CHAR(prev);
13271 if (mask == NULL)
13272 {
13273 if (c1 > ' ' && c1 != 0xa0)
13274 break;
13275 }
13276 else
13277 {
13278 for (p = mask; *p != NUL; MB_PTR_ADV(p))
13279 if (c1 == PTR2CHAR(p))
13280 break;
13281 if (*p == NUL)
13282 break;
13283 }
13284 }
13285 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
13286}
13287
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013288#ifdef FEAT_FLOAT
13289/*
13290 * "trunc({float})" function
13291 */
13292 static void
13293f_trunc(typval_T *argvars, typval_T *rettv)
13294{
13295 float_T f = 0.0;
13296
13297 rettv->v_type = VAR_FLOAT;
13298 if (get_float_arg(argvars, &f) == OK)
13299 /* trunc() is not in C90, use floor() or ceil() instead. */
13300 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
13301 else
13302 rettv->vval.v_float = 0.0;
13303}
13304#endif
13305
13306/*
13307 * "type(expr)" function
13308 */
13309 static void
13310f_type(typval_T *argvars, typval_T *rettv)
13311{
13312 int n = -1;
13313
13314 switch (argvars[0].v_type)
13315 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020013316 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
13317 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013318 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020013319 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
13320 case VAR_LIST: n = VAR_TYPE_LIST; break;
13321 case VAR_DICT: n = VAR_TYPE_DICT; break;
13322 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013323 case VAR_SPECIAL:
13324 if (argvars[0].vval.v_number == VVAL_FALSE
13325 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020013326 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013327 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020013328 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013329 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020013330 case VAR_JOB: n = VAR_TYPE_JOB; break;
13331 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013332 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010013333 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013334 n = -1;
13335 break;
13336 }
13337 rettv->vval.v_number = n;
13338}
13339
13340/*
13341 * "undofile(name)" function
13342 */
13343 static void
13344f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
13345{
13346 rettv->v_type = VAR_STRING;
13347#ifdef FEAT_PERSISTENT_UNDO
13348 {
13349 char_u *fname = get_tv_string(&argvars[0]);
13350
13351 if (*fname == NUL)
13352 {
13353 /* If there is no file name there will be no undo file. */
13354 rettv->vval.v_string = NULL;
13355 }
13356 else
13357 {
13358 char_u *ffname = FullName_save(fname, FALSE);
13359
13360 if (ffname != NULL)
13361 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
13362 vim_free(ffname);
13363 }
13364 }
13365#else
13366 rettv->vval.v_string = NULL;
13367#endif
13368}
13369
13370/*
13371 * "undotree()" function
13372 */
13373 static void
13374f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
13375{
13376 if (rettv_dict_alloc(rettv) == OK)
13377 {
13378 dict_T *dict = rettv->vval.v_dict;
13379 list_T *list;
13380
13381 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
13382 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
13383 dict_add_nr_str(dict, "save_last",
13384 (long)curbuf->b_u_save_nr_last, NULL);
13385 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
13386 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
13387 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
13388
13389 list = list_alloc();
13390 if (list != NULL)
13391 {
13392 u_eval_tree(curbuf->b_u_oldhead, list);
13393 dict_add_list(dict, "entries", list);
13394 }
13395 }
13396}
13397
13398/*
13399 * "values(dict)" function
13400 */
13401 static void
13402f_values(typval_T *argvars, typval_T *rettv)
13403{
13404 dict_list(argvars, rettv, 1);
13405}
13406
13407/*
13408 * "virtcol(string)" function
13409 */
13410 static void
13411f_virtcol(typval_T *argvars, typval_T *rettv)
13412{
13413 colnr_T vcol = 0;
13414 pos_T *fp;
13415 int fnum = curbuf->b_fnum;
13416
13417 fp = var2fpos(&argvars[0], FALSE, &fnum);
13418 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
13419 && fnum == curbuf->b_fnum)
13420 {
13421 getvvcol(curwin, fp, NULL, NULL, &vcol);
13422 ++vcol;
13423 }
13424
13425 rettv->vval.v_number = vcol;
13426}
13427
13428/*
13429 * "visualmode()" function
13430 */
13431 static void
13432f_visualmode(typval_T *argvars, typval_T *rettv)
13433{
13434 char_u str[2];
13435
13436 rettv->v_type = VAR_STRING;
13437 str[0] = curbuf->b_visual_mode_eval;
13438 str[1] = NUL;
13439 rettv->vval.v_string = vim_strsave(str);
13440
13441 /* A non-zero number or non-empty string argument: reset mode. */
13442 if (non_zero_arg(&argvars[0]))
13443 curbuf->b_visual_mode_eval = NUL;
13444}
13445
13446/*
13447 * "wildmenumode()" function
13448 */
13449 static void
13450f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13451{
13452#ifdef FEAT_WILDMENU
13453 if (wild_menu_showing)
13454 rettv->vval.v_number = 1;
13455#endif
13456}
13457
13458/*
13459 * "winbufnr(nr)" function
13460 */
13461 static void
13462f_winbufnr(typval_T *argvars, typval_T *rettv)
13463{
13464 win_T *wp;
13465
13466 wp = find_win_by_nr(&argvars[0], NULL);
13467 if (wp == NULL)
13468 rettv->vval.v_number = -1;
13469 else
13470 rettv->vval.v_number = wp->w_buffer->b_fnum;
13471}
13472
13473/*
13474 * "wincol()" function
13475 */
13476 static void
13477f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
13478{
13479 validate_cursor();
13480 rettv->vval.v_number = curwin->w_wcol + 1;
13481}
13482
13483/*
13484 * "winheight(nr)" function
13485 */
13486 static void
13487f_winheight(typval_T *argvars, typval_T *rettv)
13488{
13489 win_T *wp;
13490
13491 wp = find_win_by_nr(&argvars[0], NULL);
13492 if (wp == NULL)
13493 rettv->vval.v_number = -1;
13494 else
13495 rettv->vval.v_number = wp->w_height;
13496}
13497
13498/*
13499 * "winline()" function
13500 */
13501 static void
13502f_winline(typval_T *argvars UNUSED, typval_T *rettv)
13503{
13504 validate_cursor();
13505 rettv->vval.v_number = curwin->w_wrow + 1;
13506}
13507
13508/*
13509 * "winnr()" function
13510 */
13511 static void
13512f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
13513{
13514 int nr = 1;
13515
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013516 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013517 rettv->vval.v_number = nr;
13518}
13519
13520/*
13521 * "winrestcmd()" function
13522 */
13523 static void
13524f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
13525{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013526 win_T *wp;
13527 int winnr = 1;
13528 garray_T ga;
13529 char_u buf[50];
13530
13531 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020013532 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013533 {
13534 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
13535 ga_concat(&ga, buf);
13536 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
13537 ga_concat(&ga, buf);
13538 ++winnr;
13539 }
13540 ga_append(&ga, NUL);
13541
13542 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013543 rettv->v_type = VAR_STRING;
13544}
13545
13546/*
13547 * "winrestview()" function
13548 */
13549 static void
13550f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
13551{
13552 dict_T *dict;
13553
13554 if (argvars[0].v_type != VAR_DICT
13555 || (dict = argvars[0].vval.v_dict) == NULL)
13556 EMSG(_(e_invarg));
13557 else
13558 {
13559 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
13560 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
13561 if (dict_find(dict, (char_u *)"col", -1) != NULL)
13562 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
13563#ifdef FEAT_VIRTUALEDIT
13564 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
13565 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
13566#endif
13567 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
13568 {
13569 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
13570 curwin->w_set_curswant = FALSE;
13571 }
13572
13573 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
13574 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
13575#ifdef FEAT_DIFF
13576 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
13577 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
13578#endif
13579 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
13580 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
13581 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
13582 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
13583
13584 check_cursor();
13585 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020013586 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013587 changed_window_setting();
13588
13589 if (curwin->w_topline <= 0)
13590 curwin->w_topline = 1;
13591 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
13592 curwin->w_topline = curbuf->b_ml.ml_line_count;
13593#ifdef FEAT_DIFF
13594 check_topfill(curwin, TRUE);
13595#endif
13596 }
13597}
13598
13599/*
13600 * "winsaveview()" function
13601 */
13602 static void
13603f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
13604{
13605 dict_T *dict;
13606
13607 if (rettv_dict_alloc(rettv) == FAIL)
13608 return;
13609 dict = rettv->vval.v_dict;
13610
13611 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
13612 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
13613#ifdef FEAT_VIRTUALEDIT
13614 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
13615#endif
13616 update_curswant();
13617 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
13618
13619 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
13620#ifdef FEAT_DIFF
13621 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
13622#endif
13623 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
13624 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
13625}
13626
13627/*
13628 * "winwidth(nr)" function
13629 */
13630 static void
13631f_winwidth(typval_T *argvars, typval_T *rettv)
13632{
13633 win_T *wp;
13634
13635 wp = find_win_by_nr(&argvars[0], NULL);
13636 if (wp == NULL)
13637 rettv->vval.v_number = -1;
13638 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013639 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013640}
13641
13642/*
13643 * "wordcount()" function
13644 */
13645 static void
13646f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
13647{
13648 if (rettv_dict_alloc(rettv) == FAIL)
13649 return;
13650 cursor_pos_info(rettv->vval.v_dict);
13651}
13652
13653/*
13654 * "writefile()" function
13655 */
13656 static void
13657f_writefile(typval_T *argvars, typval_T *rettv)
13658{
13659 int binary = FALSE;
13660 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013661#ifdef HAVE_FSYNC
13662 int do_fsync = p_fs;
13663#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013664 char_u *fname;
13665 FILE *fd;
13666 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013667 listitem_T *li;
13668 list_T *list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013669
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013670 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013671 if (check_restricted() || check_secure())
13672 return;
13673
13674 if (argvars[0].v_type != VAR_LIST)
13675 {
13676 EMSG2(_(e_listarg), "writefile()");
13677 return;
13678 }
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013679 list = argvars[0].vval.v_list;
13680 if (list == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013681 return;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013682 for (li = list->lv_first; li != NULL; li = li->li_next)
13683 if (get_tv_string_chk(&li->li_tv) == NULL)
13684 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013685
13686 if (argvars[2].v_type != VAR_UNKNOWN)
13687 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013688 char_u *arg2 = get_tv_string_chk(&argvars[2]);
13689
13690 if (arg2 == NULL)
13691 return;
13692 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013693 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013694 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013695 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013696#ifdef HAVE_FSYNC
13697 if (vim_strchr(arg2, 's') != NULL)
13698 do_fsync = TRUE;
13699 else if (vim_strchr(arg2, 'S') != NULL)
13700 do_fsync = FALSE;
13701#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013702 }
13703
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013704 fname = get_tv_string_chk(&argvars[1]);
13705 if (fname == NULL)
13706 return;
13707
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013708 /* Always open the file in binary mode, library functions have a mind of
13709 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013710 if (*fname == NUL || (fd = mch_fopen((char *)fname,
13711 append ? APPENDBIN : WRITEBIN)) == NULL)
13712 {
13713 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
13714 ret = -1;
13715 }
13716 else
13717 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013718 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013719 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013720#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010013721 else if (do_fsync)
13722 /* Ignore the error, the user wouldn't know what to do about it.
13723 * May happen for a device. */
13724 ignored = fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013725#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013726 fclose(fd);
13727 }
13728
13729 rettv->vval.v_number = ret;
13730}
13731
13732/*
13733 * "xor(expr, expr)" function
13734 */
13735 static void
13736f_xor(typval_T *argvars, typval_T *rettv)
13737{
13738 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
13739 ^ get_tv_number_chk(&argvars[1], NULL);
13740}
13741
13742
13743#endif /* FEAT_EVAL */