blob: ac232a3b3e1be1c8879e86240d3e864cb85b61dc [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
27#ifdef MACOS
28# 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);
47static void f_assert_equal(typval_T *argvars, typval_T *rettv);
48static void f_assert_exception(typval_T *argvars, typval_T *rettv);
49static void f_assert_fails(typval_T *argvars, typval_T *rettv);
50static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaar61c04492016-07-23 15:35:35 +020051static void f_assert_inrange(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020052static void f_assert_match(typval_T *argvars, typval_T *rettv);
53static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
54static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar42205552017-03-18 19:42:22 +010055static void f_assert_report(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020056static void f_assert_true(typval_T *argvars, typval_T *rettv);
57#ifdef FEAT_FLOAT
58static void f_asin(typval_T *argvars, typval_T *rettv);
59static void f_atan(typval_T *argvars, typval_T *rettv);
60static void f_atan2(typval_T *argvars, typval_T *rettv);
61#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010062#ifdef FEAT_BEVAL
63static void f_balloon_show(typval_T *argvars, typval_T *rettv);
64#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020065static void f_browse(typval_T *argvars, typval_T *rettv);
66static void f_browsedir(typval_T *argvars, typval_T *rettv);
67static void f_bufexists(typval_T *argvars, typval_T *rettv);
68static void f_buflisted(typval_T *argvars, typval_T *rettv);
69static void f_bufloaded(typval_T *argvars, typval_T *rettv);
70static void f_bufname(typval_T *argvars, typval_T *rettv);
71static void f_bufnr(typval_T *argvars, typval_T *rettv);
72static void f_bufwinid(typval_T *argvars, typval_T *rettv);
73static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
74static void f_byte2line(typval_T *argvars, typval_T *rettv);
75static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
76static void f_byteidx(typval_T *argvars, typval_T *rettv);
77static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
78static void f_call(typval_T *argvars, typval_T *rettv);
79#ifdef FEAT_FLOAT
80static void f_ceil(typval_T *argvars, typval_T *rettv);
81#endif
82#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +010083static void f_ch_canread(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020084static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0874a832016-09-01 15:11:51 +020085static void f_ch_close_in(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020086static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
87static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
88static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
89static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
90static void f_ch_info(typval_T *argvars, typval_T *rettv);
91static void f_ch_log(typval_T *argvars, typval_T *rettv);
92static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
93static void f_ch_open(typval_T *argvars, typval_T *rettv);
94static void f_ch_read(typval_T *argvars, typval_T *rettv);
95static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
96static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
97static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
98static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
99static void f_ch_status(typval_T *argvars, typval_T *rettv);
100#endif
101static void f_changenr(typval_T *argvars, typval_T *rettv);
102static void f_char2nr(typval_T *argvars, typval_T *rettv);
103static void f_cindent(typval_T *argvars, typval_T *rettv);
104static void f_clearmatches(typval_T *argvars, typval_T *rettv);
105static void f_col(typval_T *argvars, typval_T *rettv);
106#if defined(FEAT_INS_EXPAND)
107static void f_complete(typval_T *argvars, typval_T *rettv);
108static void f_complete_add(typval_T *argvars, typval_T *rettv);
109static void f_complete_check(typval_T *argvars, typval_T *rettv);
110#endif
111static void f_confirm(typval_T *argvars, typval_T *rettv);
112static void f_copy(typval_T *argvars, typval_T *rettv);
113#ifdef FEAT_FLOAT
114static void f_cos(typval_T *argvars, typval_T *rettv);
115static void f_cosh(typval_T *argvars, typval_T *rettv);
116#endif
117static void f_count(typval_T *argvars, typval_T *rettv);
118static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
119static void f_cursor(typval_T *argsvars, typval_T *rettv);
120static void f_deepcopy(typval_T *argvars, typval_T *rettv);
121static void f_delete(typval_T *argvars, typval_T *rettv);
122static void f_did_filetype(typval_T *argvars, typval_T *rettv);
123static void f_diff_filler(typval_T *argvars, typval_T *rettv);
124static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
125static void f_empty(typval_T *argvars, typval_T *rettv);
126static void f_escape(typval_T *argvars, typval_T *rettv);
127static void f_eval(typval_T *argvars, typval_T *rettv);
128static void f_eventhandler(typval_T *argvars, typval_T *rettv);
129static void f_executable(typval_T *argvars, typval_T *rettv);
130static void f_execute(typval_T *argvars, typval_T *rettv);
131static void f_exepath(typval_T *argvars, typval_T *rettv);
132static void f_exists(typval_T *argvars, typval_T *rettv);
133#ifdef FEAT_FLOAT
134static void f_exp(typval_T *argvars, typval_T *rettv);
135#endif
136static void f_expand(typval_T *argvars, typval_T *rettv);
137static void f_extend(typval_T *argvars, typval_T *rettv);
138static void f_feedkeys(typval_T *argvars, typval_T *rettv);
139static void f_filereadable(typval_T *argvars, typval_T *rettv);
140static void f_filewritable(typval_T *argvars, typval_T *rettv);
141static void f_filter(typval_T *argvars, typval_T *rettv);
142static void f_finddir(typval_T *argvars, typval_T *rettv);
143static void f_findfile(typval_T *argvars, typval_T *rettv);
144#ifdef FEAT_FLOAT
145static void f_float2nr(typval_T *argvars, typval_T *rettv);
146static void f_floor(typval_T *argvars, typval_T *rettv);
147static void f_fmod(typval_T *argvars, typval_T *rettv);
148#endif
149static void f_fnameescape(typval_T *argvars, typval_T *rettv);
150static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
151static void f_foldclosed(typval_T *argvars, typval_T *rettv);
152static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
153static void f_foldlevel(typval_T *argvars, typval_T *rettv);
154static void f_foldtext(typval_T *argvars, typval_T *rettv);
155static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
156static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200157static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200158static void f_function(typval_T *argvars, typval_T *rettv);
159static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
160static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200161static void f_getbufinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200162static void f_getbufline(typval_T *argvars, typval_T *rettv);
163static void f_getbufvar(typval_T *argvars, typval_T *rettv);
164static void f_getchar(typval_T *argvars, typval_T *rettv);
165static void f_getcharmod(typval_T *argvars, typval_T *rettv);
166static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
167static void f_getcmdline(typval_T *argvars, typval_T *rettv);
168#if defined(FEAT_CMDL_COMPL)
169static void f_getcompletion(typval_T *argvars, typval_T *rettv);
170#endif
171static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
172static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
173static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
174static void f_getcwd(typval_T *argvars, typval_T *rettv);
175static void f_getfontname(typval_T *argvars, typval_T *rettv);
176static void f_getfperm(typval_T *argvars, typval_T *rettv);
177static void f_getfsize(typval_T *argvars, typval_T *rettv);
178static void f_getftime(typval_T *argvars, typval_T *rettv);
179static void f_getftype(typval_T *argvars, typval_T *rettv);
180static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200181static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200182static void f_getmatches(typval_T *argvars, typval_T *rettv);
183static void f_getpid(typval_T *argvars, typval_T *rettv);
184static void f_getcurpos(typval_T *argvars, typval_T *rettv);
185static void f_getpos(typval_T *argvars, typval_T *rettv);
186static void f_getqflist(typval_T *argvars, typval_T *rettv);
187static void f_getreg(typval_T *argvars, typval_T *rettv);
188static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200189static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200190static void f_gettabvar(typval_T *argvars, typval_T *rettv);
191static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200192static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200193static void f_getwinposx(typval_T *argvars, typval_T *rettv);
194static void f_getwinposy(typval_T *argvars, typval_T *rettv);
195static void f_getwinvar(typval_T *argvars, typval_T *rettv);
196static void f_glob(typval_T *argvars, typval_T *rettv);
197static void f_globpath(typval_T *argvars, typval_T *rettv);
198static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
199static void f_has(typval_T *argvars, typval_T *rettv);
200static void f_has_key(typval_T *argvars, typval_T *rettv);
201static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
202static void f_hasmapto(typval_T *argvars, typval_T *rettv);
203static void f_histadd(typval_T *argvars, typval_T *rettv);
204static void f_histdel(typval_T *argvars, typval_T *rettv);
205static void f_histget(typval_T *argvars, typval_T *rettv);
206static void f_histnr(typval_T *argvars, typval_T *rettv);
207static void f_hlID(typval_T *argvars, typval_T *rettv);
208static void f_hlexists(typval_T *argvars, typval_T *rettv);
209static void f_hostname(typval_T *argvars, typval_T *rettv);
210static void f_iconv(typval_T *argvars, typval_T *rettv);
211static void f_indent(typval_T *argvars, typval_T *rettv);
212static void f_index(typval_T *argvars, typval_T *rettv);
213static void f_input(typval_T *argvars, typval_T *rettv);
214static void f_inputdialog(typval_T *argvars, typval_T *rettv);
215static void f_inputlist(typval_T *argvars, typval_T *rettv);
216static void f_inputrestore(typval_T *argvars, typval_T *rettv);
217static void f_inputsave(typval_T *argvars, typval_T *rettv);
218static void f_inputsecret(typval_T *argvars, typval_T *rettv);
219static void f_insert(typval_T *argvars, typval_T *rettv);
220static void f_invert(typval_T *argvars, typval_T *rettv);
221static void f_isdirectory(typval_T *argvars, typval_T *rettv);
222static void f_islocked(typval_T *argvars, typval_T *rettv);
223#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
224static void f_isnan(typval_T *argvars, typval_T *rettv);
225#endif
226static void f_items(typval_T *argvars, typval_T *rettv);
227#ifdef FEAT_JOB_CHANNEL
228static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
229static void f_job_info(typval_T *argvars, typval_T *rettv);
230static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
231static void f_job_start(typval_T *argvars, typval_T *rettv);
232static void f_job_stop(typval_T *argvars, typval_T *rettv);
233static void f_job_status(typval_T *argvars, typval_T *rettv);
234#endif
235static void f_join(typval_T *argvars, typval_T *rettv);
236static void f_js_decode(typval_T *argvars, typval_T *rettv);
237static void f_js_encode(typval_T *argvars, typval_T *rettv);
238static void f_json_decode(typval_T *argvars, typval_T *rettv);
239static void f_json_encode(typval_T *argvars, typval_T *rettv);
240static void f_keys(typval_T *argvars, typval_T *rettv);
241static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
242static void f_len(typval_T *argvars, typval_T *rettv);
243static void f_libcall(typval_T *argvars, typval_T *rettv);
244static void f_libcallnr(typval_T *argvars, typval_T *rettv);
245static void f_line(typval_T *argvars, typval_T *rettv);
246static void f_line2byte(typval_T *argvars, typval_T *rettv);
247static void f_lispindent(typval_T *argvars, typval_T *rettv);
248static void f_localtime(typval_T *argvars, typval_T *rettv);
249#ifdef FEAT_FLOAT
250static void f_log(typval_T *argvars, typval_T *rettv);
251static void f_log10(typval_T *argvars, typval_T *rettv);
252#endif
253#ifdef FEAT_LUA
254static void f_luaeval(typval_T *argvars, typval_T *rettv);
255#endif
256static void f_map(typval_T *argvars, typval_T *rettv);
257static void f_maparg(typval_T *argvars, typval_T *rettv);
258static void f_mapcheck(typval_T *argvars, typval_T *rettv);
259static void f_match(typval_T *argvars, typval_T *rettv);
260static void f_matchadd(typval_T *argvars, typval_T *rettv);
261static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
262static void f_matcharg(typval_T *argvars, typval_T *rettv);
263static void f_matchdelete(typval_T *argvars, typval_T *rettv);
264static void f_matchend(typval_T *argvars, typval_T *rettv);
265static void f_matchlist(typval_T *argvars, typval_T *rettv);
266static void f_matchstr(typval_T *argvars, typval_T *rettv);
267static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
268static void f_max(typval_T *argvars, typval_T *rettv);
269static void f_min(typval_T *argvars, typval_T *rettv);
270#ifdef vim_mkdir
271static void f_mkdir(typval_T *argvars, typval_T *rettv);
272#endif
273static void f_mode(typval_T *argvars, typval_T *rettv);
274#ifdef FEAT_MZSCHEME
275static void f_mzeval(typval_T *argvars, typval_T *rettv);
276#endif
277static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
278static void f_nr2char(typval_T *argvars, typval_T *rettv);
279static void f_or(typval_T *argvars, typval_T *rettv);
280static void f_pathshorten(typval_T *argvars, typval_T *rettv);
281#ifdef FEAT_PERL
282static void f_perleval(typval_T *argvars, typval_T *rettv);
283#endif
284#ifdef FEAT_FLOAT
285static void f_pow(typval_T *argvars, typval_T *rettv);
286#endif
287static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
288static void f_printf(typval_T *argvars, typval_T *rettv);
289static void f_pumvisible(typval_T *argvars, typval_T *rettv);
290#ifdef FEAT_PYTHON3
291static void f_py3eval(typval_T *argvars, typval_T *rettv);
292#endif
293#ifdef FEAT_PYTHON
294static void f_pyeval(typval_T *argvars, typval_T *rettv);
295#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100296#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
297static void f_pyxeval(typval_T *argvars, typval_T *rettv);
298#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200299static void f_range(typval_T *argvars, typval_T *rettv);
300static void f_readfile(typval_T *argvars, typval_T *rettv);
301static void f_reltime(typval_T *argvars, typval_T *rettv);
302#ifdef FEAT_FLOAT
303static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
304#endif
305static void f_reltimestr(typval_T *argvars, typval_T *rettv);
306static void f_remote_expr(typval_T *argvars, typval_T *rettv);
307static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
308static void f_remote_peek(typval_T *argvars, typval_T *rettv);
309static void f_remote_read(typval_T *argvars, typval_T *rettv);
310static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100311static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200312static void f_remove(typval_T *argvars, typval_T *rettv);
313static void f_rename(typval_T *argvars, typval_T *rettv);
314static void f_repeat(typval_T *argvars, typval_T *rettv);
315static void f_resolve(typval_T *argvars, typval_T *rettv);
316static void f_reverse(typval_T *argvars, typval_T *rettv);
317#ifdef FEAT_FLOAT
318static void f_round(typval_T *argvars, typval_T *rettv);
319#endif
320static void f_screenattr(typval_T *argvars, typval_T *rettv);
321static void f_screenchar(typval_T *argvars, typval_T *rettv);
322static void f_screencol(typval_T *argvars, typval_T *rettv);
323static void f_screenrow(typval_T *argvars, typval_T *rettv);
324static void f_search(typval_T *argvars, typval_T *rettv);
325static void f_searchdecl(typval_T *argvars, typval_T *rettv);
326static void f_searchpair(typval_T *argvars, typval_T *rettv);
327static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
328static void f_searchpos(typval_T *argvars, typval_T *rettv);
329static void f_server2client(typval_T *argvars, typval_T *rettv);
330static void f_serverlist(typval_T *argvars, typval_T *rettv);
331static void f_setbufvar(typval_T *argvars, typval_T *rettv);
332static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
333static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
334static void f_setfperm(typval_T *argvars, typval_T *rettv);
335static void f_setline(typval_T *argvars, typval_T *rettv);
336static void f_setloclist(typval_T *argvars, typval_T *rettv);
337static void f_setmatches(typval_T *argvars, typval_T *rettv);
338static void f_setpos(typval_T *argvars, typval_T *rettv);
339static void f_setqflist(typval_T *argvars, typval_T *rettv);
340static void f_setreg(typval_T *argvars, typval_T *rettv);
341static void f_settabvar(typval_T *argvars, typval_T *rettv);
342static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
343static void f_setwinvar(typval_T *argvars, typval_T *rettv);
344#ifdef FEAT_CRYPT
345static void f_sha256(typval_T *argvars, typval_T *rettv);
346#endif /* FEAT_CRYPT */
347static void f_shellescape(typval_T *argvars, typval_T *rettv);
348static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
349static void f_simplify(typval_T *argvars, typval_T *rettv);
350#ifdef FEAT_FLOAT
351static void f_sin(typval_T *argvars, typval_T *rettv);
352static void f_sinh(typval_T *argvars, typval_T *rettv);
353#endif
354static void f_sort(typval_T *argvars, typval_T *rettv);
355static void f_soundfold(typval_T *argvars, typval_T *rettv);
356static void f_spellbadword(typval_T *argvars, typval_T *rettv);
357static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
358static void f_split(typval_T *argvars, typval_T *rettv);
359#ifdef FEAT_FLOAT
360static void f_sqrt(typval_T *argvars, typval_T *rettv);
361static void f_str2float(typval_T *argvars, typval_T *rettv);
362#endif
363static void f_str2nr(typval_T *argvars, typval_T *rettv);
364static void f_strchars(typval_T *argvars, typval_T *rettv);
365#ifdef HAVE_STRFTIME
366static void f_strftime(typval_T *argvars, typval_T *rettv);
367#endif
368static void f_strgetchar(typval_T *argvars, typval_T *rettv);
369static void f_stridx(typval_T *argvars, typval_T *rettv);
370static void f_string(typval_T *argvars, typval_T *rettv);
371static void f_strlen(typval_T *argvars, typval_T *rettv);
372static void f_strcharpart(typval_T *argvars, typval_T *rettv);
373static void f_strpart(typval_T *argvars, typval_T *rettv);
374static void f_strridx(typval_T *argvars, typval_T *rettv);
375static void f_strtrans(typval_T *argvars, typval_T *rettv);
376static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
377static void f_strwidth(typval_T *argvars, typval_T *rettv);
378static void f_submatch(typval_T *argvars, typval_T *rettv);
379static void f_substitute(typval_T *argvars, typval_T *rettv);
380static void f_synID(typval_T *argvars, typval_T *rettv);
381static void f_synIDattr(typval_T *argvars, typval_T *rettv);
382static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
383static void f_synstack(typval_T *argvars, typval_T *rettv);
384static void f_synconcealed(typval_T *argvars, typval_T *rettv);
385static void f_system(typval_T *argvars, typval_T *rettv);
386static void f_systemlist(typval_T *argvars, typval_T *rettv);
387static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
388static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
389static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
390static void f_taglist(typval_T *argvars, typval_T *rettv);
391static void f_tagfiles(typval_T *argvars, typval_T *rettv);
392static void f_tempname(typval_T *argvars, typval_T *rettv);
393static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
394static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100395static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200396static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100397static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200398#ifdef FEAT_JOB_CHANNEL
399static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
400#endif
401static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
402#ifdef FEAT_JOB_CHANNEL
403static void f_test_null_job(typval_T *argvars, typval_T *rettv);
404#endif
405static void f_test_null_list(typval_T *argvars, typval_T *rettv);
406static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
407static void f_test_null_string(typval_T *argvars, typval_T *rettv);
408static void f_test_settime(typval_T *argvars, typval_T *rettv);
409#ifdef FEAT_FLOAT
410static void f_tan(typval_T *argvars, typval_T *rettv);
411static void f_tanh(typval_T *argvars, typval_T *rettv);
412#endif
413#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200414static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200415static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200416static void f_timer_start(typval_T *argvars, typval_T *rettv);
417static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200418static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200419#endif
420static void f_tolower(typval_T *argvars, typval_T *rettv);
421static void f_toupper(typval_T *argvars, typval_T *rettv);
422static void f_tr(typval_T *argvars, typval_T *rettv);
423#ifdef FEAT_FLOAT
424static void f_trunc(typval_T *argvars, typval_T *rettv);
425#endif
426static void f_type(typval_T *argvars, typval_T *rettv);
427static void f_undofile(typval_T *argvars, typval_T *rettv);
428static void f_undotree(typval_T *argvars, typval_T *rettv);
429static void f_uniq(typval_T *argvars, typval_T *rettv);
430static void f_values(typval_T *argvars, typval_T *rettv);
431static void f_virtcol(typval_T *argvars, typval_T *rettv);
432static void f_visualmode(typval_T *argvars, typval_T *rettv);
433static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
434static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
435static void f_win_getid(typval_T *argvars, typval_T *rettv);
436static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
437static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
438static void f_win_id2win(typval_T *argvars, typval_T *rettv);
439static void f_winbufnr(typval_T *argvars, typval_T *rettv);
440static void f_wincol(typval_T *argvars, typval_T *rettv);
441static void f_winheight(typval_T *argvars, typval_T *rettv);
442static void f_winline(typval_T *argvars, typval_T *rettv);
443static void f_winnr(typval_T *argvars, typval_T *rettv);
444static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
445static void f_winrestview(typval_T *argvars, typval_T *rettv);
446static void f_winsaveview(typval_T *argvars, typval_T *rettv);
447static void f_winwidth(typval_T *argvars, typval_T *rettv);
448static void f_writefile(typval_T *argvars, typval_T *rettv);
449static void f_wordcount(typval_T *argvars, typval_T *rettv);
450static void f_xor(typval_T *argvars, typval_T *rettv);
451
452/*
453 * Array with names and number of arguments of all internal functions
454 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
455 */
456static struct fst
457{
458 char *f_name; /* function name */
459 char f_min_argc; /* minimal number of arguments */
460 char f_max_argc; /* maximal number of arguments */
461 void (*f_func)(typval_T *args, typval_T *rvar);
462 /* implementation of function */
463} functions[] =
464{
465#ifdef FEAT_FLOAT
466 {"abs", 1, 1, f_abs},
467 {"acos", 1, 1, f_acos}, /* WJMc */
468#endif
469 {"add", 2, 2, f_add},
470 {"and", 2, 2, f_and},
471 {"append", 2, 2, f_append},
472 {"argc", 0, 0, f_argc},
473 {"argidx", 0, 0, f_argidx},
474 {"arglistid", 0, 2, f_arglistid},
475 {"argv", 0, 1, f_argv},
476#ifdef FEAT_FLOAT
477 {"asin", 1, 1, f_asin}, /* WJMc */
478#endif
479 {"assert_equal", 2, 3, f_assert_equal},
480 {"assert_exception", 1, 2, f_assert_exception},
481 {"assert_fails", 1, 2, f_assert_fails},
482 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100483 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200484 {"assert_match", 2, 3, f_assert_match},
485 {"assert_notequal", 2, 3, f_assert_notequal},
486 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100487 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200488 {"assert_true", 1, 2, f_assert_true},
489#ifdef FEAT_FLOAT
490 {"atan", 1, 1, f_atan},
491 {"atan2", 2, 2, f_atan2},
492#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100493#ifdef FEAT_BEVAL
494 {"balloon_show", 1, 1, f_balloon_show},
495#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200496 {"browse", 4, 4, f_browse},
497 {"browsedir", 2, 2, f_browsedir},
498 {"bufexists", 1, 1, f_bufexists},
499 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
500 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
501 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
502 {"buflisted", 1, 1, f_buflisted},
503 {"bufloaded", 1, 1, f_bufloaded},
504 {"bufname", 1, 1, f_bufname},
505 {"bufnr", 1, 2, f_bufnr},
506 {"bufwinid", 1, 1, f_bufwinid},
507 {"bufwinnr", 1, 1, f_bufwinnr},
508 {"byte2line", 1, 1, f_byte2line},
509 {"byteidx", 2, 2, f_byteidx},
510 {"byteidxcomp", 2, 2, f_byteidxcomp},
511 {"call", 2, 3, f_call},
512#ifdef FEAT_FLOAT
513 {"ceil", 1, 1, f_ceil},
514#endif
515#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100516 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200517 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200518 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200519 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
520 {"ch_evalraw", 2, 3, f_ch_evalraw},
521 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
522 {"ch_getjob", 1, 1, f_ch_getjob},
523 {"ch_info", 1, 1, f_ch_info},
524 {"ch_log", 1, 2, f_ch_log},
525 {"ch_logfile", 1, 2, f_ch_logfile},
526 {"ch_open", 1, 2, f_ch_open},
527 {"ch_read", 1, 2, f_ch_read},
528 {"ch_readraw", 1, 2, f_ch_readraw},
529 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
530 {"ch_sendraw", 2, 3, f_ch_sendraw},
531 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200532 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200533#endif
534 {"changenr", 0, 0, f_changenr},
535 {"char2nr", 1, 2, f_char2nr},
536 {"cindent", 1, 1, f_cindent},
537 {"clearmatches", 0, 0, f_clearmatches},
538 {"col", 1, 1, f_col},
539#if defined(FEAT_INS_EXPAND)
540 {"complete", 2, 2, f_complete},
541 {"complete_add", 1, 1, f_complete_add},
542 {"complete_check", 0, 0, f_complete_check},
543#endif
544 {"confirm", 1, 4, f_confirm},
545 {"copy", 1, 1, f_copy},
546#ifdef FEAT_FLOAT
547 {"cos", 1, 1, f_cos},
548 {"cosh", 1, 1, f_cosh},
549#endif
550 {"count", 2, 4, f_count},
551 {"cscope_connection",0,3, f_cscope_connection},
552 {"cursor", 1, 3, f_cursor},
553 {"deepcopy", 1, 2, f_deepcopy},
554 {"delete", 1, 2, f_delete},
555 {"did_filetype", 0, 0, f_did_filetype},
556 {"diff_filler", 1, 1, f_diff_filler},
557 {"diff_hlID", 2, 2, f_diff_hlID},
558 {"empty", 1, 1, f_empty},
559 {"escape", 2, 2, f_escape},
560 {"eval", 1, 1, f_eval},
561 {"eventhandler", 0, 0, f_eventhandler},
562 {"executable", 1, 1, f_executable},
563 {"execute", 1, 2, f_execute},
564 {"exepath", 1, 1, f_exepath},
565 {"exists", 1, 1, f_exists},
566#ifdef FEAT_FLOAT
567 {"exp", 1, 1, f_exp},
568#endif
569 {"expand", 1, 3, f_expand},
570 {"extend", 2, 3, f_extend},
571 {"feedkeys", 1, 2, f_feedkeys},
572 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
573 {"filereadable", 1, 1, f_filereadable},
574 {"filewritable", 1, 1, f_filewritable},
575 {"filter", 2, 2, f_filter},
576 {"finddir", 1, 3, f_finddir},
577 {"findfile", 1, 3, f_findfile},
578#ifdef FEAT_FLOAT
579 {"float2nr", 1, 1, f_float2nr},
580 {"floor", 1, 1, f_floor},
581 {"fmod", 2, 2, f_fmod},
582#endif
583 {"fnameescape", 1, 1, f_fnameescape},
584 {"fnamemodify", 2, 2, f_fnamemodify},
585 {"foldclosed", 1, 1, f_foldclosed},
586 {"foldclosedend", 1, 1, f_foldclosedend},
587 {"foldlevel", 1, 1, f_foldlevel},
588 {"foldtext", 0, 0, f_foldtext},
589 {"foldtextresult", 1, 1, f_foldtextresult},
590 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200591 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200592 {"function", 1, 3, f_function},
593 {"garbagecollect", 0, 1, f_garbagecollect},
594 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200595 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200596 {"getbufline", 2, 3, f_getbufline},
597 {"getbufvar", 2, 3, f_getbufvar},
598 {"getchar", 0, 1, f_getchar},
599 {"getcharmod", 0, 0, f_getcharmod},
600 {"getcharsearch", 0, 0, f_getcharsearch},
601 {"getcmdline", 0, 0, f_getcmdline},
602 {"getcmdpos", 0, 0, f_getcmdpos},
603 {"getcmdtype", 0, 0, f_getcmdtype},
604 {"getcmdwintype", 0, 0, f_getcmdwintype},
605#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200606 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200607#endif
608 {"getcurpos", 0, 0, f_getcurpos},
609 {"getcwd", 0, 2, f_getcwd},
610 {"getfontname", 0, 1, f_getfontname},
611 {"getfperm", 1, 1, f_getfperm},
612 {"getfsize", 1, 1, f_getfsize},
613 {"getftime", 1, 1, f_getftime},
614 {"getftype", 1, 1, f_getftype},
615 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200616 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200617 {"getmatches", 0, 0, f_getmatches},
618 {"getpid", 0, 0, f_getpid},
619 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200620 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200621 {"getreg", 0, 3, f_getreg},
622 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200623 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200624 {"gettabvar", 2, 3, f_gettabvar},
625 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200626 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200627 {"getwinposx", 0, 0, f_getwinposx},
628 {"getwinposy", 0, 0, f_getwinposy},
629 {"getwinvar", 2, 3, f_getwinvar},
630 {"glob", 1, 4, f_glob},
631 {"glob2regpat", 1, 1, f_glob2regpat},
632 {"globpath", 2, 5, f_globpath},
633 {"has", 1, 1, f_has},
634 {"has_key", 2, 2, f_has_key},
635 {"haslocaldir", 0, 2, f_haslocaldir},
636 {"hasmapto", 1, 3, f_hasmapto},
637 {"highlightID", 1, 1, f_hlID}, /* obsolete */
638 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
639 {"histadd", 2, 2, f_histadd},
640 {"histdel", 1, 2, f_histdel},
641 {"histget", 1, 2, f_histget},
642 {"histnr", 1, 1, f_histnr},
643 {"hlID", 1, 1, f_hlID},
644 {"hlexists", 1, 1, f_hlexists},
645 {"hostname", 0, 0, f_hostname},
646 {"iconv", 3, 3, f_iconv},
647 {"indent", 1, 1, f_indent},
648 {"index", 2, 4, f_index},
649 {"input", 1, 3, f_input},
650 {"inputdialog", 1, 3, f_inputdialog},
651 {"inputlist", 1, 1, f_inputlist},
652 {"inputrestore", 0, 0, f_inputrestore},
653 {"inputsave", 0, 0, f_inputsave},
654 {"inputsecret", 1, 2, f_inputsecret},
655 {"insert", 2, 3, f_insert},
656 {"invert", 1, 1, f_invert},
657 {"isdirectory", 1, 1, f_isdirectory},
658 {"islocked", 1, 1, f_islocked},
659#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
660 {"isnan", 1, 1, f_isnan},
661#endif
662 {"items", 1, 1, f_items},
663#ifdef FEAT_JOB_CHANNEL
664 {"job_getchannel", 1, 1, f_job_getchannel},
665 {"job_info", 1, 1, f_job_info},
666 {"job_setoptions", 2, 2, f_job_setoptions},
667 {"job_start", 1, 2, f_job_start},
668 {"job_status", 1, 1, f_job_status},
669 {"job_stop", 1, 2, f_job_stop},
670#endif
671 {"join", 1, 2, f_join},
672 {"js_decode", 1, 1, f_js_decode},
673 {"js_encode", 1, 1, f_js_encode},
674 {"json_decode", 1, 1, f_json_decode},
675 {"json_encode", 1, 1, f_json_encode},
676 {"keys", 1, 1, f_keys},
677 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
678 {"len", 1, 1, f_len},
679 {"libcall", 3, 3, f_libcall},
680 {"libcallnr", 3, 3, f_libcallnr},
681 {"line", 1, 1, f_line},
682 {"line2byte", 1, 1, f_line2byte},
683 {"lispindent", 1, 1, f_lispindent},
684 {"localtime", 0, 0, f_localtime},
685#ifdef FEAT_FLOAT
686 {"log", 1, 1, f_log},
687 {"log10", 1, 1, f_log10},
688#endif
689#ifdef FEAT_LUA
690 {"luaeval", 1, 2, f_luaeval},
691#endif
692 {"map", 2, 2, f_map},
693 {"maparg", 1, 4, f_maparg},
694 {"mapcheck", 1, 3, f_mapcheck},
695 {"match", 2, 4, f_match},
696 {"matchadd", 2, 5, f_matchadd},
697 {"matchaddpos", 2, 5, f_matchaddpos},
698 {"matcharg", 1, 1, f_matcharg},
699 {"matchdelete", 1, 1, f_matchdelete},
700 {"matchend", 2, 4, f_matchend},
701 {"matchlist", 2, 4, f_matchlist},
702 {"matchstr", 2, 4, f_matchstr},
703 {"matchstrpos", 2, 4, f_matchstrpos},
704 {"max", 1, 1, f_max},
705 {"min", 1, 1, f_min},
706#ifdef vim_mkdir
707 {"mkdir", 1, 3, f_mkdir},
708#endif
709 {"mode", 0, 1, f_mode},
710#ifdef FEAT_MZSCHEME
711 {"mzeval", 1, 1, f_mzeval},
712#endif
713 {"nextnonblank", 1, 1, f_nextnonblank},
714 {"nr2char", 1, 2, f_nr2char},
715 {"or", 2, 2, f_or},
716 {"pathshorten", 1, 1, f_pathshorten},
717#ifdef FEAT_PERL
718 {"perleval", 1, 1, f_perleval},
719#endif
720#ifdef FEAT_FLOAT
721 {"pow", 2, 2, f_pow},
722#endif
723 {"prevnonblank", 1, 1, f_prevnonblank},
724 {"printf", 2, 19, f_printf},
725 {"pumvisible", 0, 0, f_pumvisible},
726#ifdef FEAT_PYTHON3
727 {"py3eval", 1, 1, f_py3eval},
728#endif
729#ifdef FEAT_PYTHON
730 {"pyeval", 1, 1, f_pyeval},
731#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100732#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
733 {"pyxeval", 1, 1, f_pyxeval},
734#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200735 {"range", 1, 3, f_range},
736 {"readfile", 1, 3, f_readfile},
737 {"reltime", 0, 2, f_reltime},
738#ifdef FEAT_FLOAT
739 {"reltimefloat", 1, 1, f_reltimefloat},
740#endif
741 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100742 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200743 {"remote_foreground", 1, 1, f_remote_foreground},
744 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100745 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200746 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100747 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200748 {"remove", 2, 3, f_remove},
749 {"rename", 2, 2, f_rename},
750 {"repeat", 2, 2, f_repeat},
751 {"resolve", 1, 1, f_resolve},
752 {"reverse", 1, 1, f_reverse},
753#ifdef FEAT_FLOAT
754 {"round", 1, 1, f_round},
755#endif
756 {"screenattr", 2, 2, f_screenattr},
757 {"screenchar", 2, 2, f_screenchar},
758 {"screencol", 0, 0, f_screencol},
759 {"screenrow", 0, 0, f_screenrow},
760 {"search", 1, 4, f_search},
761 {"searchdecl", 1, 3, f_searchdecl},
762 {"searchpair", 3, 7, f_searchpair},
763 {"searchpairpos", 3, 7, f_searchpairpos},
764 {"searchpos", 1, 4, f_searchpos},
765 {"server2client", 2, 2, f_server2client},
766 {"serverlist", 0, 0, f_serverlist},
767 {"setbufvar", 3, 3, f_setbufvar},
768 {"setcharsearch", 1, 1, f_setcharsearch},
769 {"setcmdpos", 1, 1, f_setcmdpos},
770 {"setfperm", 2, 2, f_setfperm},
771 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200772 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200773 {"setmatches", 1, 1, f_setmatches},
774 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200775 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200776 {"setreg", 2, 3, f_setreg},
777 {"settabvar", 3, 3, f_settabvar},
778 {"settabwinvar", 4, 4, f_settabwinvar},
779 {"setwinvar", 3, 3, f_setwinvar},
780#ifdef FEAT_CRYPT
781 {"sha256", 1, 1, f_sha256},
782#endif
783 {"shellescape", 1, 2, f_shellescape},
784 {"shiftwidth", 0, 0, f_shiftwidth},
785 {"simplify", 1, 1, f_simplify},
786#ifdef FEAT_FLOAT
787 {"sin", 1, 1, f_sin},
788 {"sinh", 1, 1, f_sinh},
789#endif
790 {"sort", 1, 3, f_sort},
791 {"soundfold", 1, 1, f_soundfold},
792 {"spellbadword", 0, 1, f_spellbadword},
793 {"spellsuggest", 1, 3, f_spellsuggest},
794 {"split", 1, 3, f_split},
795#ifdef FEAT_FLOAT
796 {"sqrt", 1, 1, f_sqrt},
797 {"str2float", 1, 1, f_str2float},
798#endif
799 {"str2nr", 1, 2, f_str2nr},
800 {"strcharpart", 2, 3, f_strcharpart},
801 {"strchars", 1, 2, f_strchars},
802 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
803#ifdef HAVE_STRFTIME
804 {"strftime", 1, 2, f_strftime},
805#endif
806 {"strgetchar", 2, 2, f_strgetchar},
807 {"stridx", 2, 3, f_stridx},
808 {"string", 1, 1, f_string},
809 {"strlen", 1, 1, f_strlen},
810 {"strpart", 2, 3, f_strpart},
811 {"strridx", 2, 3, f_strridx},
812 {"strtrans", 1, 1, f_strtrans},
813 {"strwidth", 1, 1, f_strwidth},
814 {"submatch", 1, 2, f_submatch},
815 {"substitute", 4, 4, f_substitute},
816 {"synID", 3, 3, f_synID},
817 {"synIDattr", 2, 3, f_synIDattr},
818 {"synIDtrans", 1, 1, f_synIDtrans},
819 {"synconcealed", 2, 2, f_synconcealed},
820 {"synstack", 2, 2, f_synstack},
821 {"system", 1, 2, f_system},
822 {"systemlist", 1, 2, f_systemlist},
823 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
824 {"tabpagenr", 0, 1, f_tabpagenr},
825 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
826 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100827 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200828#ifdef FEAT_FLOAT
829 {"tan", 1, 1, f_tan},
830 {"tanh", 1, 1, f_tanh},
831#endif
832 {"tempname", 0, 0, f_tempname},
833 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
834 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200835 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100836 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200837#ifdef FEAT_JOB_CHANNEL
838 {"test_null_channel", 0, 0, f_test_null_channel},
839#endif
840 {"test_null_dict", 0, 0, f_test_null_dict},
841#ifdef FEAT_JOB_CHANNEL
842 {"test_null_job", 0, 0, f_test_null_job},
843#endif
844 {"test_null_list", 0, 0, f_test_null_list},
845 {"test_null_partial", 0, 0, f_test_null_partial},
846 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100847 {"test_override", 2, 2, f_test_override},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200848 {"test_settime", 1, 1, f_test_settime},
849#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200850 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200851 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200852 {"timer_start", 2, 3, f_timer_start},
853 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200854 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200855#endif
856 {"tolower", 1, 1, f_tolower},
857 {"toupper", 1, 1, f_toupper},
858 {"tr", 3, 3, f_tr},
859#ifdef FEAT_FLOAT
860 {"trunc", 1, 1, f_trunc},
861#endif
862 {"type", 1, 1, f_type},
863 {"undofile", 1, 1, f_undofile},
864 {"undotree", 0, 0, f_undotree},
865 {"uniq", 1, 3, f_uniq},
866 {"values", 1, 1, f_values},
867 {"virtcol", 1, 1, f_virtcol},
868 {"visualmode", 0, 1, f_visualmode},
869 {"wildmenumode", 0, 0, f_wildmenumode},
870 {"win_findbuf", 1, 1, f_win_findbuf},
871 {"win_getid", 0, 2, f_win_getid},
872 {"win_gotoid", 1, 1, f_win_gotoid},
873 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
874 {"win_id2win", 1, 1, f_win_id2win},
875 {"winbufnr", 1, 1, f_winbufnr},
876 {"wincol", 0, 0, f_wincol},
877 {"winheight", 1, 1, f_winheight},
878 {"winline", 0, 0, f_winline},
879 {"winnr", 0, 1, f_winnr},
880 {"winrestcmd", 0, 0, f_winrestcmd},
881 {"winrestview", 1, 1, f_winrestview},
882 {"winsaveview", 0, 0, f_winsaveview},
883 {"winwidth", 1, 1, f_winwidth},
884 {"wordcount", 0, 0, f_wordcount},
885 {"writefile", 2, 3, f_writefile},
886 {"xor", 2, 2, f_xor},
887};
888
889#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
890
891/*
892 * Function given to ExpandGeneric() to obtain the list of internal
893 * or user defined function names.
894 */
895 char_u *
896get_function_name(expand_T *xp, int idx)
897{
898 static int intidx = -1;
899 char_u *name;
900
901 if (idx == 0)
902 intidx = -1;
903 if (intidx < 0)
904 {
905 name = get_user_func_name(xp, idx);
906 if (name != NULL)
907 return name;
908 }
909 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
910 {
911 STRCPY(IObuff, functions[intidx].f_name);
912 STRCAT(IObuff, "(");
913 if (functions[intidx].f_max_argc == 0)
914 STRCAT(IObuff, ")");
915 return IObuff;
916 }
917
918 return NULL;
919}
920
921/*
922 * Function given to ExpandGeneric() to obtain the list of internal or
923 * user defined variable or function names.
924 */
925 char_u *
926get_expr_name(expand_T *xp, int idx)
927{
928 static int intidx = -1;
929 char_u *name;
930
931 if (idx == 0)
932 intidx = -1;
933 if (intidx < 0)
934 {
935 name = get_function_name(xp, idx);
936 if (name != NULL)
937 return name;
938 }
939 return get_user_var_name(xp, ++intidx);
940}
941
942#endif /* FEAT_CMDL_COMPL */
943
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200944/*
945 * Find internal function in table above.
946 * Return index, or -1 if not found
947 */
948 int
949find_internal_func(
950 char_u *name) /* name of the function */
951{
952 int first = 0;
953 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
954 int cmp;
955 int x;
956
957 /*
958 * Find the function name in the table. Binary search.
959 */
960 while (first <= last)
961 {
962 x = first + ((unsigned)(last - first) >> 1);
963 cmp = STRCMP(name, functions[x].f_name);
964 if (cmp < 0)
965 last = x - 1;
966 else if (cmp > 0)
967 first = x + 1;
968 else
969 return x;
970 }
971 return -1;
972}
973
974 int
975call_internal_func(
976 char_u *name,
977 int argcount,
978 typval_T *argvars,
979 typval_T *rettv)
980{
981 int i;
982
983 i = find_internal_func(name);
984 if (i < 0)
985 return ERROR_UNKNOWN;
986 if (argcount < functions[i].f_min_argc)
987 return ERROR_TOOFEW;
988 if (argcount > functions[i].f_max_argc)
989 return ERROR_TOOMANY;
990 argvars[argcount].v_type = VAR_UNKNOWN;
991 functions[i].f_func(argvars, rettv);
992 return ERROR_NONE;
993}
994
995/*
996 * Return TRUE for a non-zero Number and a non-empty String.
997 */
998 static int
999non_zero_arg(typval_T *argvars)
1000{
1001 return ((argvars[0].v_type == VAR_NUMBER
1002 && argvars[0].vval.v_number != 0)
1003 || (argvars[0].v_type == VAR_SPECIAL
1004 && argvars[0].vval.v_number == VVAL_TRUE)
1005 || (argvars[0].v_type == VAR_STRING
1006 && argvars[0].vval.v_string != NULL
1007 && *argvars[0].vval.v_string != NUL));
1008}
1009
1010/*
1011 * Get the lnum from the first argument.
1012 * Also accepts ".", "$", etc., but that only works for the current buffer.
1013 * Returns -1 on error.
1014 */
1015 static linenr_T
1016get_tv_lnum(typval_T *argvars)
1017{
1018 typval_T rettv;
1019 linenr_T lnum;
1020
1021 lnum = (linenr_T)get_tv_number_chk(&argvars[0], NULL);
1022 if (lnum == 0) /* no valid number, try using line() */
1023 {
1024 rettv.v_type = VAR_NUMBER;
1025 f_line(argvars, &rettv);
1026 lnum = (linenr_T)rettv.vval.v_number;
1027 clear_tv(&rettv);
1028 }
1029 return lnum;
1030}
1031
1032#ifdef FEAT_FLOAT
1033static int get_float_arg(typval_T *argvars, float_T *f);
1034
1035/*
1036 * Get the float value of "argvars[0]" into "f".
1037 * Returns FAIL when the argument is not a Number or Float.
1038 */
1039 static int
1040get_float_arg(typval_T *argvars, float_T *f)
1041{
1042 if (argvars[0].v_type == VAR_FLOAT)
1043 {
1044 *f = argvars[0].vval.v_float;
1045 return OK;
1046 }
1047 if (argvars[0].v_type == VAR_NUMBER)
1048 {
1049 *f = (float_T)argvars[0].vval.v_number;
1050 return OK;
1051 }
1052 EMSG(_("E808: Number or Float required"));
1053 return FAIL;
1054}
1055
1056/*
1057 * "abs(expr)" function
1058 */
1059 static void
1060f_abs(typval_T *argvars, typval_T *rettv)
1061{
1062 if (argvars[0].v_type == VAR_FLOAT)
1063 {
1064 rettv->v_type = VAR_FLOAT;
1065 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1066 }
1067 else
1068 {
1069 varnumber_T n;
1070 int error = FALSE;
1071
1072 n = get_tv_number_chk(&argvars[0], &error);
1073 if (error)
1074 rettv->vval.v_number = -1;
1075 else if (n > 0)
1076 rettv->vval.v_number = n;
1077 else
1078 rettv->vval.v_number = -n;
1079 }
1080}
1081
1082/*
1083 * "acos()" function
1084 */
1085 static void
1086f_acos(typval_T *argvars, typval_T *rettv)
1087{
1088 float_T f = 0.0;
1089
1090 rettv->v_type = VAR_FLOAT;
1091 if (get_float_arg(argvars, &f) == OK)
1092 rettv->vval.v_float = acos(f);
1093 else
1094 rettv->vval.v_float = 0.0;
1095}
1096#endif
1097
1098/*
1099 * "add(list, item)" function
1100 */
1101 static void
1102f_add(typval_T *argvars, typval_T *rettv)
1103{
1104 list_T *l;
1105
1106 rettv->vval.v_number = 1; /* Default: Failed */
1107 if (argvars[0].v_type == VAR_LIST)
1108 {
1109 if ((l = argvars[0].vval.v_list) != NULL
1110 && !tv_check_lock(l->lv_lock,
1111 (char_u *)N_("add() argument"), TRUE)
1112 && list_append_tv(l, &argvars[1]) == OK)
1113 copy_tv(&argvars[0], rettv);
1114 }
1115 else
1116 EMSG(_(e_listreq));
1117}
1118
1119/*
1120 * "and(expr, expr)" function
1121 */
1122 static void
1123f_and(typval_T *argvars, typval_T *rettv)
1124{
1125 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
1126 & get_tv_number_chk(&argvars[1], NULL);
1127}
1128
1129/*
1130 * "append(lnum, string/list)" function
1131 */
1132 static void
1133f_append(typval_T *argvars, typval_T *rettv)
1134{
1135 long lnum;
1136 char_u *line;
1137 list_T *l = NULL;
1138 listitem_T *li = NULL;
1139 typval_T *tv;
1140 long added = 0;
1141
1142 /* When coming here from Insert mode, sync undo, so that this can be
1143 * undone separately from what was previously inserted. */
1144 if (u_sync_once == 2)
1145 {
1146 u_sync_once = 1; /* notify that u_sync() was called */
1147 u_sync(TRUE);
1148 }
1149
1150 lnum = get_tv_lnum(argvars);
1151 if (lnum >= 0
1152 && lnum <= curbuf->b_ml.ml_line_count
1153 && u_save(lnum, lnum + 1) == OK)
1154 {
1155 if (argvars[1].v_type == VAR_LIST)
1156 {
1157 l = argvars[1].vval.v_list;
1158 if (l == NULL)
1159 return;
1160 li = l->lv_first;
1161 }
1162 for (;;)
1163 {
1164 if (l == NULL)
1165 tv = &argvars[1]; /* append a string */
1166 else if (li == NULL)
1167 break; /* end of list */
1168 else
1169 tv = &li->li_tv; /* append item from list */
1170 line = get_tv_string_chk(tv);
1171 if (line == NULL) /* type error */
1172 {
1173 rettv->vval.v_number = 1; /* Failed */
1174 break;
1175 }
1176 ml_append(lnum + added, line, (colnr_T)0, FALSE);
1177 ++added;
1178 if (l == NULL)
1179 break;
1180 li = li->li_next;
1181 }
1182
1183 appended_lines_mark(lnum, added);
1184 if (curwin->w_cursor.lnum > lnum)
1185 curwin->w_cursor.lnum += added;
1186 }
1187 else
1188 rettv->vval.v_number = 1; /* Failed */
1189}
1190
1191/*
1192 * "argc()" function
1193 */
1194 static void
1195f_argc(typval_T *argvars UNUSED, typval_T *rettv)
1196{
1197 rettv->vval.v_number = ARGCOUNT;
1198}
1199
1200/*
1201 * "argidx()" function
1202 */
1203 static void
1204f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1205{
1206 rettv->vval.v_number = curwin->w_arg_idx;
1207}
1208
1209/*
1210 * "arglistid()" function
1211 */
1212 static void
1213f_arglistid(typval_T *argvars, typval_T *rettv)
1214{
1215 win_T *wp;
1216
1217 rettv->vval.v_number = -1;
1218 wp = find_tabwin(&argvars[0], &argvars[1]);
1219 if (wp != NULL)
1220 rettv->vval.v_number = wp->w_alist->id;
1221}
1222
1223/*
1224 * "argv(nr)" function
1225 */
1226 static void
1227f_argv(typval_T *argvars, typval_T *rettv)
1228{
1229 int idx;
1230
1231 if (argvars[0].v_type != VAR_UNKNOWN)
1232 {
1233 idx = (int)get_tv_number_chk(&argvars[0], NULL);
1234 if (idx >= 0 && idx < ARGCOUNT)
1235 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
1236 else
1237 rettv->vval.v_string = NULL;
1238 rettv->v_type = VAR_STRING;
1239 }
1240 else if (rettv_list_alloc(rettv) == OK)
1241 for (idx = 0; idx < ARGCOUNT; ++idx)
1242 list_append_string(rettv->vval.v_list,
1243 alist_name(&ARGLIST[idx]), -1);
1244}
1245
1246/*
1247 * "assert_equal(expected, actual[, msg])" function
1248 */
1249 static void
1250f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
1251{
1252 assert_equal_common(argvars, ASSERT_EQUAL);
1253}
1254
1255/*
1256 * "assert_notequal(expected, actual[, msg])" function
1257 */
1258 static void
1259f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED)
1260{
1261 assert_equal_common(argvars, ASSERT_NOTEQUAL);
1262}
1263
1264/*
1265 * "assert_exception(string[, msg])" function
1266 */
1267 static void
1268f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
1269{
1270 assert_exception(argvars);
1271}
1272
1273/*
1274 * "assert_fails(cmd [, error])" function
1275 */
1276 static void
1277f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
1278{
1279 assert_fails(argvars);
1280}
1281
1282/*
1283 * "assert_false(actual[, msg])" function
1284 */
1285 static void
1286f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
1287{
1288 assert_bool(argvars, FALSE);
1289}
1290
1291/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001292 * "assert_inrange(lower, upper[, msg])" function
1293 */
1294 static void
1295f_assert_inrange(typval_T *argvars, typval_T *rettv UNUSED)
1296{
1297 assert_inrange(argvars);
1298}
1299
1300/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001301 * "assert_match(pattern, actual[, msg])" function
1302 */
1303 static void
1304f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
1305{
1306 assert_match_common(argvars, ASSERT_MATCH);
1307}
1308
1309/*
1310 * "assert_notmatch(pattern, actual[, msg])" function
1311 */
1312 static void
1313f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED)
1314{
1315 assert_match_common(argvars, ASSERT_NOTMATCH);
1316}
1317
1318/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001319 * "assert_report(msg)" function
1320 */
1321 static void
1322f_assert_report(typval_T *argvars, typval_T *rettv UNUSED)
1323{
1324 assert_report(argvars);
1325}
1326
1327/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001328 * "assert_true(actual[, msg])" function
1329 */
1330 static void
1331f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
1332{
1333 assert_bool(argvars, TRUE);
1334}
1335
1336#ifdef FEAT_FLOAT
1337/*
1338 * "asin()" function
1339 */
1340 static void
1341f_asin(typval_T *argvars, typval_T *rettv)
1342{
1343 float_T f = 0.0;
1344
1345 rettv->v_type = VAR_FLOAT;
1346 if (get_float_arg(argvars, &f) == OK)
1347 rettv->vval.v_float = asin(f);
1348 else
1349 rettv->vval.v_float = 0.0;
1350}
1351
1352/*
1353 * "atan()" function
1354 */
1355 static void
1356f_atan(typval_T *argvars, typval_T *rettv)
1357{
1358 float_T f = 0.0;
1359
1360 rettv->v_type = VAR_FLOAT;
1361 if (get_float_arg(argvars, &f) == OK)
1362 rettv->vval.v_float = atan(f);
1363 else
1364 rettv->vval.v_float = 0.0;
1365}
1366
1367/*
1368 * "atan2()" function
1369 */
1370 static void
1371f_atan2(typval_T *argvars, typval_T *rettv)
1372{
1373 float_T fx = 0.0, fy = 0.0;
1374
1375 rettv->v_type = VAR_FLOAT;
1376 if (get_float_arg(argvars, &fx) == OK
1377 && get_float_arg(&argvars[1], &fy) == OK)
1378 rettv->vval.v_float = atan2(fx, fy);
1379 else
1380 rettv->vval.v_float = 0.0;
1381}
1382#endif
1383
1384/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001385 * "balloon_show()" function
1386 */
1387#ifdef FEAT_BEVAL
1388 static void
1389f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1390{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001391 if (balloonEval != NULL)
1392 gui_mch_post_balloon(balloonEval, get_tv_string_chk(&argvars[0]));
Bram Moolenaar59716a22017-03-01 20:32:44 +01001393}
1394#endif
1395
1396/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001397 * "browse(save, title, initdir, default)" function
1398 */
1399 static void
1400f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1401{
1402#ifdef FEAT_BROWSE
1403 int save;
1404 char_u *title;
1405 char_u *initdir;
1406 char_u *defname;
1407 char_u buf[NUMBUFLEN];
1408 char_u buf2[NUMBUFLEN];
1409 int error = FALSE;
1410
1411 save = (int)get_tv_number_chk(&argvars[0], &error);
1412 title = get_tv_string_chk(&argvars[1]);
1413 initdir = get_tv_string_buf_chk(&argvars[2], buf);
1414 defname = get_tv_string_buf_chk(&argvars[3], buf2);
1415
1416 if (error || title == NULL || initdir == NULL || defname == NULL)
1417 rettv->vval.v_string = NULL;
1418 else
1419 rettv->vval.v_string =
1420 do_browse(save ? BROWSE_SAVE : 0,
1421 title, defname, NULL, initdir, NULL, curbuf);
1422#else
1423 rettv->vval.v_string = NULL;
1424#endif
1425 rettv->v_type = VAR_STRING;
1426}
1427
1428/*
1429 * "browsedir(title, initdir)" function
1430 */
1431 static void
1432f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1433{
1434#ifdef FEAT_BROWSE
1435 char_u *title;
1436 char_u *initdir;
1437 char_u buf[NUMBUFLEN];
1438
1439 title = get_tv_string_chk(&argvars[0]);
1440 initdir = get_tv_string_buf_chk(&argvars[1], buf);
1441
1442 if (title == NULL || initdir == NULL)
1443 rettv->vval.v_string = NULL;
1444 else
1445 rettv->vval.v_string = do_browse(BROWSE_DIR,
1446 title, NULL, NULL, initdir, NULL, curbuf);
1447#else
1448 rettv->vval.v_string = NULL;
1449#endif
1450 rettv->v_type = VAR_STRING;
1451}
1452
1453static buf_T *find_buffer(typval_T *avar);
1454
1455/*
1456 * Find a buffer by number or exact name.
1457 */
1458 static buf_T *
1459find_buffer(typval_T *avar)
1460{
1461 buf_T *buf = NULL;
1462
1463 if (avar->v_type == VAR_NUMBER)
1464 buf = buflist_findnr((int)avar->vval.v_number);
1465 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1466 {
1467 buf = buflist_findname_exp(avar->vval.v_string);
1468 if (buf == NULL)
1469 {
1470 /* No full path name match, try a match with a URL or a "nofile"
1471 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001472 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001473 if (buf->b_fname != NULL
1474 && (path_with_url(buf->b_fname)
1475#ifdef FEAT_QUICKFIX
1476 || bt_nofile(buf)
1477#endif
1478 )
1479 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1480 break;
1481 }
1482 }
1483 return buf;
1484}
1485
1486/*
1487 * "bufexists(expr)" function
1488 */
1489 static void
1490f_bufexists(typval_T *argvars, typval_T *rettv)
1491{
1492 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1493}
1494
1495/*
1496 * "buflisted(expr)" function
1497 */
1498 static void
1499f_buflisted(typval_T *argvars, typval_T *rettv)
1500{
1501 buf_T *buf;
1502
1503 buf = find_buffer(&argvars[0]);
1504 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1505}
1506
1507/*
1508 * "bufloaded(expr)" function
1509 */
1510 static void
1511f_bufloaded(typval_T *argvars, typval_T *rettv)
1512{
1513 buf_T *buf;
1514
1515 buf = find_buffer(&argvars[0]);
1516 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1517}
1518
1519 buf_T *
1520buflist_find_by_name(char_u *name, int curtab_only)
1521{
1522 int save_magic;
1523 char_u *save_cpo;
1524 buf_T *buf;
1525
1526 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1527 save_magic = p_magic;
1528 p_magic = TRUE;
1529 save_cpo = p_cpo;
1530 p_cpo = (char_u *)"";
1531
1532 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1533 TRUE, FALSE, curtab_only));
1534
1535 p_magic = save_magic;
1536 p_cpo = save_cpo;
1537 return buf;
1538}
1539
1540/*
1541 * Get buffer by number or pattern.
1542 */
1543 static buf_T *
1544get_buf_tv(typval_T *tv, int curtab_only)
1545{
1546 char_u *name = tv->vval.v_string;
1547 buf_T *buf;
1548
1549 if (tv->v_type == VAR_NUMBER)
1550 return buflist_findnr((int)tv->vval.v_number);
1551 if (tv->v_type != VAR_STRING)
1552 return NULL;
1553 if (name == NULL || *name == NUL)
1554 return curbuf;
1555 if (name[0] == '$' && name[1] == NUL)
1556 return lastbuf;
1557
1558 buf = buflist_find_by_name(name, curtab_only);
1559
1560 /* If not found, try expanding the name, like done for bufexists(). */
1561 if (buf == NULL)
1562 buf = find_buffer(tv);
1563
1564 return buf;
1565}
1566
1567/*
1568 * "bufname(expr)" function
1569 */
1570 static void
1571f_bufname(typval_T *argvars, typval_T *rettv)
1572{
1573 buf_T *buf;
1574
1575 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1576 ++emsg_off;
1577 buf = get_buf_tv(&argvars[0], FALSE);
1578 rettv->v_type = VAR_STRING;
1579 if (buf != NULL && buf->b_fname != NULL)
1580 rettv->vval.v_string = vim_strsave(buf->b_fname);
1581 else
1582 rettv->vval.v_string = NULL;
1583 --emsg_off;
1584}
1585
1586/*
1587 * "bufnr(expr)" function
1588 */
1589 static void
1590f_bufnr(typval_T *argvars, typval_T *rettv)
1591{
1592 buf_T *buf;
1593 int error = FALSE;
1594 char_u *name;
1595
1596 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1597 ++emsg_off;
1598 buf = get_buf_tv(&argvars[0], FALSE);
1599 --emsg_off;
1600
1601 /* If the buffer isn't found and the second argument is not zero create a
1602 * new buffer. */
1603 if (buf == NULL
1604 && argvars[1].v_type != VAR_UNKNOWN
1605 && get_tv_number_chk(&argvars[1], &error) != 0
1606 && !error
1607 && (name = get_tv_string_chk(&argvars[0])) != NULL
1608 && !error)
1609 buf = buflist_new(name, NULL, (linenr_T)1, 0);
1610
1611 if (buf != NULL)
1612 rettv->vval.v_number = buf->b_fnum;
1613 else
1614 rettv->vval.v_number = -1;
1615}
1616
1617 static void
1618buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
1619{
1620#ifdef FEAT_WINDOWS
1621 win_T *wp;
1622 int winnr = 0;
1623#endif
1624 buf_T *buf;
1625
1626 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1627 ++emsg_off;
1628 buf = get_buf_tv(&argvars[0], TRUE);
1629#ifdef FEAT_WINDOWS
Bram Moolenaar29323592016-07-24 22:04:11 +02001630 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001631 {
1632 ++winnr;
1633 if (wp->w_buffer == buf)
1634 break;
1635 }
1636 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
1637#else
1638 rettv->vval.v_number = (curwin->w_buffer == buf
1639 ? (get_nr ? 1 : curwin->w_id) : -1);
1640#endif
1641 --emsg_off;
1642}
1643
1644/*
1645 * "bufwinid(nr)" function
1646 */
1647 static void
1648f_bufwinid(typval_T *argvars, typval_T *rettv)
1649{
1650 buf_win_common(argvars, rettv, FALSE);
1651}
1652
1653/*
1654 * "bufwinnr(nr)" function
1655 */
1656 static void
1657f_bufwinnr(typval_T *argvars, typval_T *rettv)
1658{
1659 buf_win_common(argvars, rettv, TRUE);
1660}
1661
1662/*
1663 * "byte2line(byte)" function
1664 */
1665 static void
1666f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1667{
1668#ifndef FEAT_BYTEOFF
1669 rettv->vval.v_number = -1;
1670#else
1671 long boff = 0;
1672
1673 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
1674 if (boff < 0)
1675 rettv->vval.v_number = -1;
1676 else
1677 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
1678 (linenr_T)0, &boff);
1679#endif
1680}
1681
1682 static void
1683byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
1684{
1685#ifdef FEAT_MBYTE
1686 char_u *t;
1687#endif
1688 char_u *str;
1689 varnumber_T idx;
1690
1691 str = get_tv_string_chk(&argvars[0]);
1692 idx = get_tv_number_chk(&argvars[1], NULL);
1693 rettv->vval.v_number = -1;
1694 if (str == NULL || idx < 0)
1695 return;
1696
1697#ifdef FEAT_MBYTE
1698 t = str;
1699 for ( ; idx > 0; idx--)
1700 {
1701 if (*t == NUL) /* EOL reached */
1702 return;
1703 if (enc_utf8 && comp)
1704 t += utf_ptr2len(t);
1705 else
1706 t += (*mb_ptr2len)(t);
1707 }
1708 rettv->vval.v_number = (varnumber_T)(t - str);
1709#else
1710 if ((size_t)idx <= STRLEN(str))
1711 rettv->vval.v_number = idx;
1712#endif
1713}
1714
1715/*
1716 * "byteidx()" function
1717 */
1718 static void
1719f_byteidx(typval_T *argvars, typval_T *rettv)
1720{
1721 byteidx(argvars, rettv, FALSE);
1722}
1723
1724/*
1725 * "byteidxcomp()" function
1726 */
1727 static void
1728f_byteidxcomp(typval_T *argvars, typval_T *rettv)
1729{
1730 byteidx(argvars, rettv, TRUE);
1731}
1732
1733/*
1734 * "call(func, arglist [, dict])" function
1735 */
1736 static void
1737f_call(typval_T *argvars, typval_T *rettv)
1738{
1739 char_u *func;
1740 partial_T *partial = NULL;
1741 dict_T *selfdict = NULL;
1742
1743 if (argvars[1].v_type != VAR_LIST)
1744 {
1745 EMSG(_(e_listreq));
1746 return;
1747 }
1748 if (argvars[1].vval.v_list == NULL)
1749 return;
1750
1751 if (argvars[0].v_type == VAR_FUNC)
1752 func = argvars[0].vval.v_string;
1753 else if (argvars[0].v_type == VAR_PARTIAL)
1754 {
1755 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001756 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001757 }
1758 else
1759 func = get_tv_string(&argvars[0]);
1760 if (*func == NUL)
1761 return; /* type error or empty name */
1762
1763 if (argvars[2].v_type != VAR_UNKNOWN)
1764 {
1765 if (argvars[2].v_type != VAR_DICT)
1766 {
1767 EMSG(_(e_dictreq));
1768 return;
1769 }
1770 selfdict = argvars[2].vval.v_dict;
1771 }
1772
1773 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
1774}
1775
1776#ifdef FEAT_FLOAT
1777/*
1778 * "ceil({float})" function
1779 */
1780 static void
1781f_ceil(typval_T *argvars, typval_T *rettv)
1782{
1783 float_T f = 0.0;
1784
1785 rettv->v_type = VAR_FLOAT;
1786 if (get_float_arg(argvars, &f) == OK)
1787 rettv->vval.v_float = ceil(f);
1788 else
1789 rettv->vval.v_float = 0.0;
1790}
1791#endif
1792
1793#ifdef FEAT_JOB_CHANNEL
1794/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01001795 * "ch_canread()" function
1796 */
1797 static void
1798f_ch_canread(typval_T *argvars, typval_T *rettv)
1799{
Bram Moolenaar958dc692016-12-01 15:34:12 +01001800 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01001801
1802 rettv->vval.v_number = 0;
1803 if (channel != NULL)
1804 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
1805 || channel_has_readahead(channel, PART_OUT)
1806 || channel_has_readahead(channel, PART_ERR);
1807}
1808
1809/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001810 * "ch_close()" function
1811 */
1812 static void
1813f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
1814{
1815 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
1816
1817 if (channel != NULL)
1818 {
1819 channel_close(channel, FALSE);
1820 channel_clear(channel);
1821 }
1822}
1823
1824/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02001825 * "ch_close()" function
1826 */
1827 static void
1828f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
1829{
1830 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
1831
1832 if (channel != NULL)
1833 channel_close_in(channel);
1834}
1835
1836/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001837 * "ch_getbufnr()" function
1838 */
1839 static void
1840f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
1841{
1842 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1843
1844 rettv->vval.v_number = -1;
1845 if (channel != NULL)
1846 {
1847 char_u *what = get_tv_string(&argvars[1]);
1848 int part;
1849
1850 if (STRCMP(what, "err") == 0)
1851 part = PART_ERR;
1852 else if (STRCMP(what, "out") == 0)
1853 part = PART_OUT;
1854 else if (STRCMP(what, "in") == 0)
1855 part = PART_IN;
1856 else
1857 part = PART_SOCK;
1858 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
1859 rettv->vval.v_number =
1860 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
1861 }
1862}
1863
1864/*
1865 * "ch_getjob()" function
1866 */
1867 static void
1868f_ch_getjob(typval_T *argvars, typval_T *rettv)
1869{
1870 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1871
1872 if (channel != NULL)
1873 {
1874 rettv->v_type = VAR_JOB;
1875 rettv->vval.v_job = channel->ch_job;
1876 if (channel->ch_job != NULL)
1877 ++channel->ch_job->jv_refcount;
1878 }
1879}
1880
1881/*
1882 * "ch_info()" function
1883 */
1884 static void
1885f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
1886{
1887 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1888
1889 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
1890 channel_info(channel, rettv->vval.v_dict);
1891}
1892
1893/*
1894 * "ch_log()" function
1895 */
1896 static void
1897f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
1898{
1899 char_u *msg = get_tv_string(&argvars[0]);
1900 channel_T *channel = NULL;
1901
1902 if (argvars[1].v_type != VAR_UNKNOWN)
1903 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
1904
1905 ch_log(channel, (char *)msg);
1906}
1907
1908/*
1909 * "ch_logfile()" function
1910 */
1911 static void
1912f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
1913{
1914 char_u *fname;
1915 char_u *opt = (char_u *)"";
1916 char_u buf[NUMBUFLEN];
1917
1918 fname = get_tv_string(&argvars[0]);
1919 if (argvars[1].v_type == VAR_STRING)
1920 opt = get_tv_string_buf(&argvars[1], buf);
1921 ch_logfile(fname, opt);
1922}
1923
1924/*
1925 * "ch_open()" function
1926 */
1927 static void
1928f_ch_open(typval_T *argvars, typval_T *rettv)
1929{
1930 rettv->v_type = VAR_CHANNEL;
1931 if (check_restricted() || check_secure())
1932 return;
1933 rettv->vval.v_channel = channel_open_func(argvars);
1934}
1935
1936/*
1937 * "ch_read()" function
1938 */
1939 static void
1940f_ch_read(typval_T *argvars, typval_T *rettv)
1941{
1942 common_channel_read(argvars, rettv, FALSE);
1943}
1944
1945/*
1946 * "ch_readraw()" function
1947 */
1948 static void
1949f_ch_readraw(typval_T *argvars, typval_T *rettv)
1950{
1951 common_channel_read(argvars, rettv, TRUE);
1952}
1953
1954/*
1955 * "ch_evalexpr()" function
1956 */
1957 static void
1958f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
1959{
1960 ch_expr_common(argvars, rettv, TRUE);
1961}
1962
1963/*
1964 * "ch_sendexpr()" function
1965 */
1966 static void
1967f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
1968{
1969 ch_expr_common(argvars, rettv, FALSE);
1970}
1971
1972/*
1973 * "ch_evalraw()" function
1974 */
1975 static void
1976f_ch_evalraw(typval_T *argvars, typval_T *rettv)
1977{
1978 ch_raw_common(argvars, rettv, TRUE);
1979}
1980
1981/*
1982 * "ch_sendraw()" function
1983 */
1984 static void
1985f_ch_sendraw(typval_T *argvars, typval_T *rettv)
1986{
1987 ch_raw_common(argvars, rettv, FALSE);
1988}
1989
1990/*
1991 * "ch_setoptions()" function
1992 */
1993 static void
1994f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
1995{
1996 channel_T *channel;
1997 jobopt_T opt;
1998
1999 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2000 if (channel == NULL)
2001 return;
2002 clear_job_options(&opt);
2003 if (get_job_options(&argvars[1], &opt,
2004 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == OK)
2005 channel_set_options(channel, &opt);
2006 free_job_options(&opt);
2007}
2008
2009/*
2010 * "ch_status()" function
2011 */
2012 static void
2013f_ch_status(typval_T *argvars, typval_T *rettv)
2014{
2015 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002016 jobopt_T opt;
2017 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002018
2019 /* return an empty string by default */
2020 rettv->v_type = VAR_STRING;
2021 rettv->vval.v_string = NULL;
2022
2023 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002024
2025 if (argvars[1].v_type != VAR_UNKNOWN)
2026 {
2027 clear_job_options(&opt);
2028 if (get_job_options(&argvars[1], &opt, JO_PART) == OK
2029 && (opt.jo_set & JO_PART))
2030 part = opt.jo_part;
2031 }
2032
2033 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002034}
2035#endif
2036
2037/*
2038 * "changenr()" function
2039 */
2040 static void
2041f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2042{
2043 rettv->vval.v_number = curbuf->b_u_seq_cur;
2044}
2045
2046/*
2047 * "char2nr(string)" function
2048 */
2049 static void
2050f_char2nr(typval_T *argvars, typval_T *rettv)
2051{
2052#ifdef FEAT_MBYTE
2053 if (has_mbyte)
2054 {
2055 int utf8 = 0;
2056
2057 if (argvars[1].v_type != VAR_UNKNOWN)
2058 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
2059
2060 if (utf8)
2061 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
2062 else
2063 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
2064 }
2065 else
2066#endif
2067 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
2068}
2069
2070/*
2071 * "cindent(lnum)" function
2072 */
2073 static void
2074f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2075{
2076#ifdef FEAT_CINDENT
2077 pos_T pos;
2078 linenr_T lnum;
2079
2080 pos = curwin->w_cursor;
2081 lnum = get_tv_lnum(argvars);
2082 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2083 {
2084 curwin->w_cursor.lnum = lnum;
2085 rettv->vval.v_number = get_c_indent();
2086 curwin->w_cursor = pos;
2087 }
2088 else
2089#endif
2090 rettv->vval.v_number = -1;
2091}
2092
2093/*
2094 * "clearmatches()" function
2095 */
2096 static void
2097f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2098{
2099#ifdef FEAT_SEARCH_EXTRA
2100 clear_matches(curwin);
2101#endif
2102}
2103
2104/*
2105 * "col(string)" function
2106 */
2107 static void
2108f_col(typval_T *argvars, typval_T *rettv)
2109{
2110 colnr_T col = 0;
2111 pos_T *fp;
2112 int fnum = curbuf->b_fnum;
2113
2114 fp = var2fpos(&argvars[0], FALSE, &fnum);
2115 if (fp != NULL && fnum == curbuf->b_fnum)
2116 {
2117 if (fp->col == MAXCOL)
2118 {
2119 /* '> can be MAXCOL, get the length of the line then */
2120 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2121 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2122 else
2123 col = MAXCOL;
2124 }
2125 else
2126 {
2127 col = fp->col + 1;
2128#ifdef FEAT_VIRTUALEDIT
2129 /* col(".") when the cursor is on the NUL at the end of the line
2130 * because of "coladd" can be seen as an extra column. */
2131 if (virtual_active() && fp == &curwin->w_cursor)
2132 {
2133 char_u *p = ml_get_cursor();
2134
2135 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2136 curwin->w_virtcol - curwin->w_cursor.coladd))
2137 {
2138# ifdef FEAT_MBYTE
2139 int l;
2140
2141 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2142 col += l;
2143# else
2144 if (*p != NUL && p[1] == NUL)
2145 ++col;
2146# endif
2147 }
2148 }
2149#endif
2150 }
2151 }
2152 rettv->vval.v_number = col;
2153}
2154
2155#if defined(FEAT_INS_EXPAND)
2156/*
2157 * "complete()" function
2158 */
2159 static void
2160f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2161{
2162 int startcol;
2163
2164 if ((State & INSERT) == 0)
2165 {
2166 EMSG(_("E785: complete() can only be used in Insert mode"));
2167 return;
2168 }
2169
2170 /* Check for undo allowed here, because if something was already inserted
2171 * the line was already saved for undo and this check isn't done. */
2172 if (!undo_allowed())
2173 return;
2174
2175 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2176 {
2177 EMSG(_(e_invarg));
2178 return;
2179 }
2180
2181 startcol = (int)get_tv_number_chk(&argvars[0], NULL);
2182 if (startcol <= 0)
2183 return;
2184
2185 set_completion(startcol - 1, argvars[1].vval.v_list);
2186}
2187
2188/*
2189 * "complete_add()" function
2190 */
2191 static void
2192f_complete_add(typval_T *argvars, typval_T *rettv)
2193{
2194 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2195}
2196
2197/*
2198 * "complete_check()" function
2199 */
2200 static void
2201f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2202{
2203 int saved = RedrawingDisabled;
2204
2205 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002206 ins_compl_check_keys(0, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002207 rettv->vval.v_number = compl_interrupted;
2208 RedrawingDisabled = saved;
2209}
2210#endif
2211
2212/*
2213 * "confirm(message, buttons[, default [, type]])" function
2214 */
2215 static void
2216f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2217{
2218#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2219 char_u *message;
2220 char_u *buttons = NULL;
2221 char_u buf[NUMBUFLEN];
2222 char_u buf2[NUMBUFLEN];
2223 int def = 1;
2224 int type = VIM_GENERIC;
2225 char_u *typestr;
2226 int error = FALSE;
2227
2228 message = get_tv_string_chk(&argvars[0]);
2229 if (message == NULL)
2230 error = TRUE;
2231 if (argvars[1].v_type != VAR_UNKNOWN)
2232 {
2233 buttons = get_tv_string_buf_chk(&argvars[1], buf);
2234 if (buttons == NULL)
2235 error = TRUE;
2236 if (argvars[2].v_type != VAR_UNKNOWN)
2237 {
2238 def = (int)get_tv_number_chk(&argvars[2], &error);
2239 if (argvars[3].v_type != VAR_UNKNOWN)
2240 {
2241 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
2242 if (typestr == NULL)
2243 error = TRUE;
2244 else
2245 {
2246 switch (TOUPPER_ASC(*typestr))
2247 {
2248 case 'E': type = VIM_ERROR; break;
2249 case 'Q': type = VIM_QUESTION; break;
2250 case 'I': type = VIM_INFO; break;
2251 case 'W': type = VIM_WARNING; break;
2252 case 'G': type = VIM_GENERIC; break;
2253 }
2254 }
2255 }
2256 }
2257 }
2258
2259 if (buttons == NULL || *buttons == NUL)
2260 buttons = (char_u *)_("&Ok");
2261
2262 if (!error)
2263 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2264 def, NULL, FALSE);
2265#endif
2266}
2267
2268/*
2269 * "copy()" function
2270 */
2271 static void
2272f_copy(typval_T *argvars, typval_T *rettv)
2273{
2274 item_copy(&argvars[0], rettv, FALSE, 0);
2275}
2276
2277#ifdef FEAT_FLOAT
2278/*
2279 * "cos()" function
2280 */
2281 static void
2282f_cos(typval_T *argvars, typval_T *rettv)
2283{
2284 float_T f = 0.0;
2285
2286 rettv->v_type = VAR_FLOAT;
2287 if (get_float_arg(argvars, &f) == OK)
2288 rettv->vval.v_float = cos(f);
2289 else
2290 rettv->vval.v_float = 0.0;
2291}
2292
2293/*
2294 * "cosh()" function
2295 */
2296 static void
2297f_cosh(typval_T *argvars, typval_T *rettv)
2298{
2299 float_T f = 0.0;
2300
2301 rettv->v_type = VAR_FLOAT;
2302 if (get_float_arg(argvars, &f) == OK)
2303 rettv->vval.v_float = cosh(f);
2304 else
2305 rettv->vval.v_float = 0.0;
2306}
2307#endif
2308
2309/*
2310 * "count()" function
2311 */
2312 static void
2313f_count(typval_T *argvars, typval_T *rettv)
2314{
2315 long n = 0;
2316 int ic = FALSE;
2317
2318 if (argvars[0].v_type == VAR_LIST)
2319 {
2320 listitem_T *li;
2321 list_T *l;
2322 long idx;
2323
2324 if ((l = argvars[0].vval.v_list) != NULL)
2325 {
2326 li = l->lv_first;
2327 if (argvars[2].v_type != VAR_UNKNOWN)
2328 {
2329 int error = FALSE;
2330
2331 ic = (int)get_tv_number_chk(&argvars[2], &error);
2332 if (argvars[3].v_type != VAR_UNKNOWN)
2333 {
2334 idx = (long)get_tv_number_chk(&argvars[3], &error);
2335 if (!error)
2336 {
2337 li = list_find(l, idx);
2338 if (li == NULL)
2339 EMSGN(_(e_listidx), idx);
2340 }
2341 }
2342 if (error)
2343 li = NULL;
2344 }
2345
2346 for ( ; li != NULL; li = li->li_next)
2347 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2348 ++n;
2349 }
2350 }
2351 else if (argvars[0].v_type == VAR_DICT)
2352 {
2353 int todo;
2354 dict_T *d;
2355 hashitem_T *hi;
2356
2357 if ((d = argvars[0].vval.v_dict) != NULL)
2358 {
2359 int error = FALSE;
2360
2361 if (argvars[2].v_type != VAR_UNKNOWN)
2362 {
2363 ic = (int)get_tv_number_chk(&argvars[2], &error);
2364 if (argvars[3].v_type != VAR_UNKNOWN)
2365 EMSG(_(e_invarg));
2366 }
2367
2368 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2369 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2370 {
2371 if (!HASHITEM_EMPTY(hi))
2372 {
2373 --todo;
2374 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2375 ++n;
2376 }
2377 }
2378 }
2379 }
2380 else
2381 EMSG2(_(e_listdictarg), "count()");
2382 rettv->vval.v_number = n;
2383}
2384
2385/*
2386 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2387 *
2388 * Checks the existence of a cscope connection.
2389 */
2390 static void
2391f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2392{
2393#ifdef FEAT_CSCOPE
2394 int num = 0;
2395 char_u *dbpath = NULL;
2396 char_u *prepend = NULL;
2397 char_u buf[NUMBUFLEN];
2398
2399 if (argvars[0].v_type != VAR_UNKNOWN
2400 && argvars[1].v_type != VAR_UNKNOWN)
2401 {
2402 num = (int)get_tv_number(&argvars[0]);
2403 dbpath = get_tv_string(&argvars[1]);
2404 if (argvars[2].v_type != VAR_UNKNOWN)
2405 prepend = get_tv_string_buf(&argvars[2], buf);
2406 }
2407
2408 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2409#endif
2410}
2411
2412/*
2413 * "cursor(lnum, col)" function, or
2414 * "cursor(list)"
2415 *
2416 * Moves the cursor to the specified line and column.
2417 * Returns 0 when the position could be set, -1 otherwise.
2418 */
2419 static void
2420f_cursor(typval_T *argvars, typval_T *rettv)
2421{
2422 long line, col;
2423#ifdef FEAT_VIRTUALEDIT
2424 long coladd = 0;
2425#endif
2426 int set_curswant = TRUE;
2427
2428 rettv->vval.v_number = -1;
2429 if (argvars[1].v_type == VAR_UNKNOWN)
2430 {
2431 pos_T pos;
2432 colnr_T curswant = -1;
2433
2434 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2435 {
2436 EMSG(_(e_invarg));
2437 return;
2438 }
2439 line = pos.lnum;
2440 col = pos.col;
2441#ifdef FEAT_VIRTUALEDIT
2442 coladd = pos.coladd;
2443#endif
2444 if (curswant >= 0)
2445 {
2446 curwin->w_curswant = curswant - 1;
2447 set_curswant = FALSE;
2448 }
2449 }
2450 else
2451 {
2452 line = get_tv_lnum(argvars);
2453 col = (long)get_tv_number_chk(&argvars[1], NULL);
2454#ifdef FEAT_VIRTUALEDIT
2455 if (argvars[2].v_type != VAR_UNKNOWN)
2456 coladd = (long)get_tv_number_chk(&argvars[2], NULL);
2457#endif
2458 }
2459 if (line < 0 || col < 0
2460#ifdef FEAT_VIRTUALEDIT
2461 || coladd < 0
2462#endif
2463 )
2464 return; /* type error; errmsg already given */
2465 if (line > 0)
2466 curwin->w_cursor.lnum = line;
2467 if (col > 0)
2468 curwin->w_cursor.col = col - 1;
2469#ifdef FEAT_VIRTUALEDIT
2470 curwin->w_cursor.coladd = coladd;
2471#endif
2472
2473 /* Make sure the cursor is in a valid position. */
2474 check_cursor();
2475#ifdef FEAT_MBYTE
2476 /* Correct cursor for multi-byte character. */
2477 if (has_mbyte)
2478 mb_adjust_cursor();
2479#endif
2480
2481 curwin->w_set_curswant = set_curswant;
2482 rettv->vval.v_number = 0;
2483}
2484
2485/*
2486 * "deepcopy()" function
2487 */
2488 static void
2489f_deepcopy(typval_T *argvars, typval_T *rettv)
2490{
2491 int noref = 0;
2492 int copyID;
2493
2494 if (argvars[1].v_type != VAR_UNKNOWN)
2495 noref = (int)get_tv_number_chk(&argvars[1], NULL);
2496 if (noref < 0 || noref > 1)
2497 EMSG(_(e_invarg));
2498 else
2499 {
2500 copyID = get_copyID();
2501 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2502 }
2503}
2504
2505/*
2506 * "delete()" function
2507 */
2508 static void
2509f_delete(typval_T *argvars, typval_T *rettv)
2510{
2511 char_u nbuf[NUMBUFLEN];
2512 char_u *name;
2513 char_u *flags;
2514
2515 rettv->vval.v_number = -1;
2516 if (check_restricted() || check_secure())
2517 return;
2518
2519 name = get_tv_string(&argvars[0]);
2520 if (name == NULL || *name == NUL)
2521 {
2522 EMSG(_(e_invarg));
2523 return;
2524 }
2525
2526 if (argvars[1].v_type != VAR_UNKNOWN)
2527 flags = get_tv_string_buf(&argvars[1], nbuf);
2528 else
2529 flags = (char_u *)"";
2530
2531 if (*flags == NUL)
2532 /* delete a file */
2533 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2534 else if (STRCMP(flags, "d") == 0)
2535 /* delete an empty directory */
2536 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2537 else if (STRCMP(flags, "rf") == 0)
2538 /* delete a directory recursively */
2539 rettv->vval.v_number = delete_recursive(name);
2540 else
2541 EMSG2(_(e_invexpr2), flags);
2542}
2543
2544/*
2545 * "did_filetype()" function
2546 */
2547 static void
2548f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2549{
2550#ifdef FEAT_AUTOCMD
2551 rettv->vval.v_number = did_filetype;
2552#endif
2553}
2554
2555/*
2556 * "diff_filler()" function
2557 */
2558 static void
2559f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2560{
2561#ifdef FEAT_DIFF
2562 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
2563#endif
2564}
2565
2566/*
2567 * "diff_hlID()" function
2568 */
2569 static void
2570f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2571{
2572#ifdef FEAT_DIFF
2573 linenr_T lnum = get_tv_lnum(argvars);
2574 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01002575 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002576 static int fnum = 0;
2577 static int change_start = 0;
2578 static int change_end = 0;
2579 static hlf_T hlID = (hlf_T)0;
2580 int filler_lines;
2581 int col;
2582
2583 if (lnum < 0) /* ignore type error in {lnum} arg */
2584 lnum = 0;
2585 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002586 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002587 || fnum != curbuf->b_fnum)
2588 {
2589 /* New line, buffer, change: need to get the values. */
2590 filler_lines = diff_check(curwin, lnum);
2591 if (filler_lines < 0)
2592 {
2593 if (filler_lines == -1)
2594 {
2595 change_start = MAXCOL;
2596 change_end = -1;
2597 if (diff_find_change(curwin, lnum, &change_start, &change_end))
2598 hlID = HLF_ADD; /* added line */
2599 else
2600 hlID = HLF_CHD; /* changed line */
2601 }
2602 else
2603 hlID = HLF_ADD; /* added line */
2604 }
2605 else
2606 hlID = (hlf_T)0;
2607 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002608 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002609 fnum = curbuf->b_fnum;
2610 }
2611
2612 if (hlID == HLF_CHD || hlID == HLF_TXD)
2613 {
2614 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
2615 if (col >= change_start && col <= change_end)
2616 hlID = HLF_TXD; /* changed text */
2617 else
2618 hlID = HLF_CHD; /* changed line */
2619 }
2620 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
2621#endif
2622}
2623
2624/*
2625 * "empty({expr})" function
2626 */
2627 static void
2628f_empty(typval_T *argvars, typval_T *rettv)
2629{
2630 int n = FALSE;
2631
2632 switch (argvars[0].v_type)
2633 {
2634 case VAR_STRING:
2635 case VAR_FUNC:
2636 n = argvars[0].vval.v_string == NULL
2637 || *argvars[0].vval.v_string == NUL;
2638 break;
2639 case VAR_PARTIAL:
2640 n = FALSE;
2641 break;
2642 case VAR_NUMBER:
2643 n = argvars[0].vval.v_number == 0;
2644 break;
2645 case VAR_FLOAT:
2646#ifdef FEAT_FLOAT
2647 n = argvars[0].vval.v_float == 0.0;
2648 break;
2649#endif
2650 case VAR_LIST:
2651 n = argvars[0].vval.v_list == NULL
2652 || argvars[0].vval.v_list->lv_first == NULL;
2653 break;
2654 case VAR_DICT:
2655 n = argvars[0].vval.v_dict == NULL
2656 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2657 break;
2658 case VAR_SPECIAL:
2659 n = argvars[0].vval.v_number != VVAL_TRUE;
2660 break;
2661
2662 case VAR_JOB:
2663#ifdef FEAT_JOB_CHANNEL
2664 n = argvars[0].vval.v_job == NULL
2665 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2666 break;
2667#endif
2668 case VAR_CHANNEL:
2669#ifdef FEAT_JOB_CHANNEL
2670 n = argvars[0].vval.v_channel == NULL
2671 || !channel_is_open(argvars[0].vval.v_channel);
2672 break;
2673#endif
2674 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01002675 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002676 n = TRUE;
2677 break;
2678 }
2679
2680 rettv->vval.v_number = n;
2681}
2682
2683/*
2684 * "escape({string}, {chars})" function
2685 */
2686 static void
2687f_escape(typval_T *argvars, typval_T *rettv)
2688{
2689 char_u buf[NUMBUFLEN];
2690
2691 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
2692 get_tv_string_buf(&argvars[1], buf));
2693 rettv->v_type = VAR_STRING;
2694}
2695
2696/*
2697 * "eval()" function
2698 */
2699 static void
2700f_eval(typval_T *argvars, typval_T *rettv)
2701{
2702 char_u *s, *p;
2703
2704 s = get_tv_string_chk(&argvars[0]);
2705 if (s != NULL)
2706 s = skipwhite(s);
2707
2708 p = s;
2709 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
2710 {
2711 if (p != NULL && !aborting())
2712 EMSG2(_(e_invexpr2), p);
2713 need_clr_eos = FALSE;
2714 rettv->v_type = VAR_NUMBER;
2715 rettv->vval.v_number = 0;
2716 }
2717 else if (*s != NUL)
2718 EMSG(_(e_trailing));
2719}
2720
2721/*
2722 * "eventhandler()" function
2723 */
2724 static void
2725f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
2726{
2727 rettv->vval.v_number = vgetc_busy;
2728}
2729
2730/*
2731 * "executable()" function
2732 */
2733 static void
2734f_executable(typval_T *argvars, typval_T *rettv)
2735{
2736 char_u *name = get_tv_string(&argvars[0]);
2737
2738 /* Check in $PATH and also check directly if there is a directory name. */
2739 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
2740 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
2741}
2742
2743static garray_T redir_execute_ga;
2744
2745/*
2746 * Append "value[value_len]" to the execute() output.
2747 */
2748 void
2749execute_redir_str(char_u *value, int value_len)
2750{
2751 int len;
2752
2753 if (value_len == -1)
2754 len = (int)STRLEN(value); /* Append the entire string */
2755 else
2756 len = value_len; /* Append only "value_len" characters */
2757 if (ga_grow(&redir_execute_ga, len) == OK)
2758 {
2759 mch_memmove((char *)redir_execute_ga.ga_data
2760 + redir_execute_ga.ga_len, value, len);
2761 redir_execute_ga.ga_len += len;
2762 }
2763}
2764
2765/*
2766 * Get next line from a list.
2767 * Called by do_cmdline() to get the next line.
2768 * Returns allocated string, or NULL for end of function.
2769 */
2770
2771 static char_u *
2772get_list_line(
2773 int c UNUSED,
2774 void *cookie,
2775 int indent UNUSED)
2776{
2777 listitem_T **p = (listitem_T **)cookie;
2778 listitem_T *item = *p;
2779 char_u buf[NUMBUFLEN];
2780 char_u *s;
2781
2782 if (item == NULL)
2783 return NULL;
2784 s = get_tv_string_buf_chk(&item->li_tv, buf);
2785 *p = item->li_next;
2786 return s == NULL ? NULL : vim_strsave(s);
2787}
2788
2789/*
2790 * "execute()" function
2791 */
2792 static void
2793f_execute(typval_T *argvars, typval_T *rettv)
2794{
2795 char_u *cmd = NULL;
2796 list_T *list = NULL;
2797 int save_msg_silent = msg_silent;
2798 int save_emsg_silent = emsg_silent;
2799 int save_emsg_noredir = emsg_noredir;
2800 int save_redir_execute = redir_execute;
2801 garray_T save_ga;
2802
2803 rettv->vval.v_string = NULL;
2804 rettv->v_type = VAR_STRING;
2805
2806 if (argvars[0].v_type == VAR_LIST)
2807 {
2808 list = argvars[0].vval.v_list;
2809 if (list == NULL || list->lv_first == NULL)
2810 /* empty list, no commands, empty output */
2811 return;
2812 ++list->lv_refcount;
2813 }
2814 else
2815 {
2816 cmd = get_tv_string_chk(&argvars[0]);
2817 if (cmd == NULL)
2818 return;
2819 }
2820
2821 if (argvars[1].v_type != VAR_UNKNOWN)
2822 {
2823 char_u buf[NUMBUFLEN];
2824 char_u *s = get_tv_string_buf_chk(&argvars[1], buf);
2825
2826 if (s == NULL)
2827 return;
2828 if (STRNCMP(s, "silent", 6) == 0)
2829 ++msg_silent;
2830 if (STRCMP(s, "silent!") == 0)
2831 {
2832 emsg_silent = TRUE;
2833 emsg_noredir = TRUE;
2834 }
2835 }
2836 else
2837 ++msg_silent;
2838
2839 if (redir_execute)
2840 save_ga = redir_execute_ga;
2841 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
2842 redir_execute = TRUE;
2843
2844 if (cmd != NULL)
2845 do_cmdline_cmd(cmd);
2846 else
2847 {
2848 listitem_T *item = list->lv_first;
2849
2850 do_cmdline(NULL, get_list_line, (void *)&item,
2851 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
2852 --list->lv_refcount;
2853 }
2854
Bram Moolenaard297f352017-01-29 20:31:21 +01002855 /* Need to append a NUL to the result. */
2856 if (ga_grow(&redir_execute_ga, 1) == OK)
2857 {
2858 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
2859 rettv->vval.v_string = redir_execute_ga.ga_data;
2860 }
2861 else
2862 {
2863 ga_clear(&redir_execute_ga);
2864 rettv->vval.v_string = NULL;
2865 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002866 msg_silent = save_msg_silent;
2867 emsg_silent = save_emsg_silent;
2868 emsg_noredir = save_emsg_noredir;
2869
2870 redir_execute = save_redir_execute;
2871 if (redir_execute)
2872 redir_execute_ga = save_ga;
2873
2874 /* "silent reg" or "silent echo x" leaves msg_col somewhere in the
2875 * line. Put it back in the first column. */
2876 msg_col = 0;
2877}
2878
2879/*
2880 * "exepath()" function
2881 */
2882 static void
2883f_exepath(typval_T *argvars, typval_T *rettv)
2884{
2885 char_u *p = NULL;
2886
2887 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
2888 rettv->v_type = VAR_STRING;
2889 rettv->vval.v_string = p;
2890}
2891
2892/*
2893 * "exists()" function
2894 */
2895 static void
2896f_exists(typval_T *argvars, typval_T *rettv)
2897{
2898 char_u *p;
2899 char_u *name;
2900 int n = FALSE;
2901 int len = 0;
2902
2903 p = get_tv_string(&argvars[0]);
2904 if (*p == '$') /* environment variable */
2905 {
2906 /* first try "normal" environment variables (fast) */
2907 if (mch_getenv(p + 1) != NULL)
2908 n = TRUE;
2909 else
2910 {
2911 /* try expanding things like $VIM and ${HOME} */
2912 p = expand_env_save(p);
2913 if (p != NULL && *p != '$')
2914 n = TRUE;
2915 vim_free(p);
2916 }
2917 }
2918 else if (*p == '&' || *p == '+') /* option */
2919 {
2920 n = (get_option_tv(&p, NULL, TRUE) == OK);
2921 if (*skipwhite(p) != NUL)
2922 n = FALSE; /* trailing garbage */
2923 }
2924 else if (*p == '*') /* internal or user defined function */
2925 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002926 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002927 }
2928 else if (*p == ':')
2929 {
2930 n = cmd_exists(p + 1);
2931 }
2932 else if (*p == '#')
2933 {
2934#ifdef FEAT_AUTOCMD
2935 if (p[1] == '#')
2936 n = autocmd_supported(p + 2);
2937 else
2938 n = au_exists(p + 1);
2939#endif
2940 }
2941 else /* internal variable */
2942 {
2943 char_u *tofree;
2944 typval_T tv;
2945
2946 /* get_name_len() takes care of expanding curly braces */
2947 name = p;
2948 len = get_name_len(&p, &tofree, TRUE, FALSE);
2949 if (len > 0)
2950 {
2951 if (tofree != NULL)
2952 name = tofree;
2953 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
2954 if (n)
2955 {
2956 /* handle d.key, l[idx], f(expr) */
2957 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
2958 if (n)
2959 clear_tv(&tv);
2960 }
2961 }
2962 if (*p != NUL)
2963 n = FALSE;
2964
2965 vim_free(tofree);
2966 }
2967
2968 rettv->vval.v_number = n;
2969}
2970
2971#ifdef FEAT_FLOAT
2972/*
2973 * "exp()" function
2974 */
2975 static void
2976f_exp(typval_T *argvars, typval_T *rettv)
2977{
2978 float_T f = 0.0;
2979
2980 rettv->v_type = VAR_FLOAT;
2981 if (get_float_arg(argvars, &f) == OK)
2982 rettv->vval.v_float = exp(f);
2983 else
2984 rettv->vval.v_float = 0.0;
2985}
2986#endif
2987
2988/*
2989 * "expand()" function
2990 */
2991 static void
2992f_expand(typval_T *argvars, typval_T *rettv)
2993{
2994 char_u *s;
2995 int len;
2996 char_u *errormsg;
2997 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
2998 expand_T xpc;
2999 int error = FALSE;
3000 char_u *result;
3001
3002 rettv->v_type = VAR_STRING;
3003 if (argvars[1].v_type != VAR_UNKNOWN
3004 && argvars[2].v_type != VAR_UNKNOWN
3005 && get_tv_number_chk(&argvars[2], &error)
3006 && !error)
3007 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003008 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003009 }
3010
3011 s = get_tv_string(&argvars[0]);
3012 if (*s == '%' || *s == '#' || *s == '<')
3013 {
3014 ++emsg_off;
3015 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3016 --emsg_off;
3017 if (rettv->v_type == VAR_LIST)
3018 {
3019 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3020 list_append_string(rettv->vval.v_list, result, -1);
3021 else
3022 vim_free(result);
3023 }
3024 else
3025 rettv->vval.v_string = result;
3026 }
3027 else
3028 {
3029 /* When the optional second argument is non-zero, don't remove matches
3030 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3031 if (argvars[1].v_type != VAR_UNKNOWN
3032 && get_tv_number_chk(&argvars[1], &error))
3033 options |= WILD_KEEP_ALL;
3034 if (!error)
3035 {
3036 ExpandInit(&xpc);
3037 xpc.xp_context = EXPAND_FILES;
3038 if (p_wic)
3039 options += WILD_ICASE;
3040 if (rettv->v_type == VAR_STRING)
3041 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3042 options, WILD_ALL);
3043 else if (rettv_list_alloc(rettv) != FAIL)
3044 {
3045 int i;
3046
3047 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3048 for (i = 0; i < xpc.xp_numfiles; i++)
3049 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3050 ExpandCleanup(&xpc);
3051 }
3052 }
3053 else
3054 rettv->vval.v_string = NULL;
3055 }
3056}
3057
3058/*
3059 * "extend(list, list [, idx])" function
3060 * "extend(dict, dict [, action])" function
3061 */
3062 static void
3063f_extend(typval_T *argvars, typval_T *rettv)
3064{
3065 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3066
3067 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3068 {
3069 list_T *l1, *l2;
3070 listitem_T *item;
3071 long before;
3072 int error = FALSE;
3073
3074 l1 = argvars[0].vval.v_list;
3075 l2 = argvars[1].vval.v_list;
3076 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
3077 && l2 != NULL)
3078 {
3079 if (argvars[2].v_type != VAR_UNKNOWN)
3080 {
3081 before = (long)get_tv_number_chk(&argvars[2], &error);
3082 if (error)
3083 return; /* type error; errmsg already given */
3084
3085 if (before == l1->lv_len)
3086 item = NULL;
3087 else
3088 {
3089 item = list_find(l1, before);
3090 if (item == NULL)
3091 {
3092 EMSGN(_(e_listidx), before);
3093 return;
3094 }
3095 }
3096 }
3097 else
3098 item = NULL;
3099 list_extend(l1, l2, item);
3100
3101 copy_tv(&argvars[0], rettv);
3102 }
3103 }
3104 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3105 {
3106 dict_T *d1, *d2;
3107 char_u *action;
3108 int i;
3109
3110 d1 = argvars[0].vval.v_dict;
3111 d2 = argvars[1].vval.v_dict;
3112 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
3113 && d2 != NULL)
3114 {
3115 /* Check the third argument. */
3116 if (argvars[2].v_type != VAR_UNKNOWN)
3117 {
3118 static char *(av[]) = {"keep", "force", "error"};
3119
3120 action = get_tv_string_chk(&argvars[2]);
3121 if (action == NULL)
3122 return; /* type error; errmsg already given */
3123 for (i = 0; i < 3; ++i)
3124 if (STRCMP(action, av[i]) == 0)
3125 break;
3126 if (i == 3)
3127 {
3128 EMSG2(_(e_invarg2), action);
3129 return;
3130 }
3131 }
3132 else
3133 action = (char_u *)"force";
3134
3135 dict_extend(d1, d2, action);
3136
3137 copy_tv(&argvars[0], rettv);
3138 }
3139 }
3140 else
3141 EMSG2(_(e_listdictarg), "extend()");
3142}
3143
3144/*
3145 * "feedkeys()" function
3146 */
3147 static void
3148f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3149{
3150 int remap = TRUE;
3151 int insert = FALSE;
3152 char_u *keys, *flags;
3153 char_u nbuf[NUMBUFLEN];
3154 int typed = FALSE;
3155 int execute = FALSE;
3156 int dangerous = FALSE;
3157 char_u *keys_esc;
3158
3159 /* This is not allowed in the sandbox. If the commands would still be
3160 * executed in the sandbox it would be OK, but it probably happens later,
3161 * when "sandbox" is no longer set. */
3162 if (check_secure())
3163 return;
3164
3165 keys = get_tv_string(&argvars[0]);
3166
3167 if (argvars[1].v_type != VAR_UNKNOWN)
3168 {
3169 flags = get_tv_string_buf(&argvars[1], nbuf);
3170 for ( ; *flags != NUL; ++flags)
3171 {
3172 switch (*flags)
3173 {
3174 case 'n': remap = FALSE; break;
3175 case 'm': remap = TRUE; break;
3176 case 't': typed = TRUE; break;
3177 case 'i': insert = TRUE; break;
3178 case 'x': execute = TRUE; break;
3179 case '!': dangerous = TRUE; break;
3180 }
3181 }
3182 }
3183
3184 if (*keys != NUL || execute)
3185 {
3186 /* Need to escape K_SPECIAL and CSI before putting the string in the
3187 * typeahead buffer. */
3188 keys_esc = vim_strsave_escape_csi(keys);
3189 if (keys_esc != NULL)
3190 {
3191 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
3192 insert ? 0 : typebuf.tb_len, !typed, FALSE);
3193 vim_free(keys_esc);
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003194 if (vgetc_busy
3195#ifdef FEAT_TIMERS
3196 || timer_busy
3197#endif
3198 )
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003199 typebuf_was_filled = TRUE;
3200 if (execute)
3201 {
3202 int save_msg_scroll = msg_scroll;
3203
3204 /* Avoid a 1 second delay when the keys start Insert mode. */
3205 msg_scroll = FALSE;
3206
3207 if (!dangerous)
3208 ++ex_normal_busy;
3209 exec_normal(TRUE);
3210 if (!dangerous)
3211 --ex_normal_busy;
3212 msg_scroll |= save_msg_scroll;
3213 }
3214 }
3215 }
3216}
3217
3218/*
3219 * "filereadable()" function
3220 */
3221 static void
3222f_filereadable(typval_T *argvars, typval_T *rettv)
3223{
3224 int fd;
3225 char_u *p;
3226 int n;
3227
3228#ifndef O_NONBLOCK
3229# define O_NONBLOCK 0
3230#endif
3231 p = get_tv_string(&argvars[0]);
3232 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3233 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3234 {
3235 n = TRUE;
3236 close(fd);
3237 }
3238 else
3239 n = FALSE;
3240
3241 rettv->vval.v_number = n;
3242}
3243
3244/*
3245 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3246 * rights to write into.
3247 */
3248 static void
3249f_filewritable(typval_T *argvars, typval_T *rettv)
3250{
3251 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
3252}
3253
3254 static void
3255findfilendir(
3256 typval_T *argvars UNUSED,
3257 typval_T *rettv,
3258 int find_what UNUSED)
3259{
3260#ifdef FEAT_SEARCHPATH
3261 char_u *fname;
3262 char_u *fresult = NULL;
3263 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3264 char_u *p;
3265 char_u pathbuf[NUMBUFLEN];
3266 int count = 1;
3267 int first = TRUE;
3268 int error = FALSE;
3269#endif
3270
3271 rettv->vval.v_string = NULL;
3272 rettv->v_type = VAR_STRING;
3273
3274#ifdef FEAT_SEARCHPATH
3275 fname = get_tv_string(&argvars[0]);
3276
3277 if (argvars[1].v_type != VAR_UNKNOWN)
3278 {
3279 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
3280 if (p == NULL)
3281 error = TRUE;
3282 else
3283 {
3284 if (*p != NUL)
3285 path = p;
3286
3287 if (argvars[2].v_type != VAR_UNKNOWN)
3288 count = (int)get_tv_number_chk(&argvars[2], &error);
3289 }
3290 }
3291
3292 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3293 error = TRUE;
3294
3295 if (*fname != NUL && !error)
3296 {
3297 do
3298 {
3299 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3300 vim_free(fresult);
3301 fresult = find_file_in_path_option(first ? fname : NULL,
3302 first ? (int)STRLEN(fname) : 0,
3303 0, first, path,
3304 find_what,
3305 curbuf->b_ffname,
3306 find_what == FINDFILE_DIR
3307 ? (char_u *)"" : curbuf->b_p_sua);
3308 first = FALSE;
3309
3310 if (fresult != NULL && rettv->v_type == VAR_LIST)
3311 list_append_string(rettv->vval.v_list, fresult, -1);
3312
3313 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3314 }
3315
3316 if (rettv->v_type == VAR_STRING)
3317 rettv->vval.v_string = fresult;
3318#endif
3319}
3320
3321/*
3322 * "filter()" function
3323 */
3324 static void
3325f_filter(typval_T *argvars, typval_T *rettv)
3326{
3327 filter_map(argvars, rettv, FALSE);
3328}
3329
3330/*
3331 * "finddir({fname}[, {path}[, {count}]])" function
3332 */
3333 static void
3334f_finddir(typval_T *argvars, typval_T *rettv)
3335{
3336 findfilendir(argvars, rettv, FINDFILE_DIR);
3337}
3338
3339/*
3340 * "findfile({fname}[, {path}[, {count}]])" function
3341 */
3342 static void
3343f_findfile(typval_T *argvars, typval_T *rettv)
3344{
3345 findfilendir(argvars, rettv, FINDFILE_FILE);
3346}
3347
3348#ifdef FEAT_FLOAT
3349/*
3350 * "float2nr({float})" function
3351 */
3352 static void
3353f_float2nr(typval_T *argvars, typval_T *rettv)
3354{
3355 float_T f = 0.0;
3356
3357 if (get_float_arg(argvars, &f) == OK)
3358 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003359 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003360 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003361 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003362 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003363 else
3364 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003365 }
3366}
3367
3368/*
3369 * "floor({float})" function
3370 */
3371 static void
3372f_floor(typval_T *argvars, typval_T *rettv)
3373{
3374 float_T f = 0.0;
3375
3376 rettv->v_type = VAR_FLOAT;
3377 if (get_float_arg(argvars, &f) == OK)
3378 rettv->vval.v_float = floor(f);
3379 else
3380 rettv->vval.v_float = 0.0;
3381}
3382
3383/*
3384 * "fmod()" function
3385 */
3386 static void
3387f_fmod(typval_T *argvars, typval_T *rettv)
3388{
3389 float_T fx = 0.0, fy = 0.0;
3390
3391 rettv->v_type = VAR_FLOAT;
3392 if (get_float_arg(argvars, &fx) == OK
3393 && get_float_arg(&argvars[1], &fy) == OK)
3394 rettv->vval.v_float = fmod(fx, fy);
3395 else
3396 rettv->vval.v_float = 0.0;
3397}
3398#endif
3399
3400/*
3401 * "fnameescape({string})" function
3402 */
3403 static void
3404f_fnameescape(typval_T *argvars, typval_T *rettv)
3405{
3406 rettv->vval.v_string = vim_strsave_fnameescape(
3407 get_tv_string(&argvars[0]), FALSE);
3408 rettv->v_type = VAR_STRING;
3409}
3410
3411/*
3412 * "fnamemodify({fname}, {mods})" function
3413 */
3414 static void
3415f_fnamemodify(typval_T *argvars, typval_T *rettv)
3416{
3417 char_u *fname;
3418 char_u *mods;
3419 int usedlen = 0;
3420 int len;
3421 char_u *fbuf = NULL;
3422 char_u buf[NUMBUFLEN];
3423
3424 fname = get_tv_string_chk(&argvars[0]);
3425 mods = get_tv_string_buf_chk(&argvars[1], buf);
3426 if (fname == NULL || mods == NULL)
3427 fname = NULL;
3428 else
3429 {
3430 len = (int)STRLEN(fname);
3431 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
3432 }
3433
3434 rettv->v_type = VAR_STRING;
3435 if (fname == NULL)
3436 rettv->vval.v_string = NULL;
3437 else
3438 rettv->vval.v_string = vim_strnsave(fname, len);
3439 vim_free(fbuf);
3440}
3441
3442static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
3443
3444/*
3445 * "foldclosed()" function
3446 */
3447 static void
3448foldclosed_both(
3449 typval_T *argvars UNUSED,
3450 typval_T *rettv,
3451 int end UNUSED)
3452{
3453#ifdef FEAT_FOLDING
3454 linenr_T lnum;
3455 linenr_T first, last;
3456
3457 lnum = get_tv_lnum(argvars);
3458 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3459 {
3460 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3461 {
3462 if (end)
3463 rettv->vval.v_number = (varnumber_T)last;
3464 else
3465 rettv->vval.v_number = (varnumber_T)first;
3466 return;
3467 }
3468 }
3469#endif
3470 rettv->vval.v_number = -1;
3471}
3472
3473/*
3474 * "foldclosed()" function
3475 */
3476 static void
3477f_foldclosed(typval_T *argvars, typval_T *rettv)
3478{
3479 foldclosed_both(argvars, rettv, FALSE);
3480}
3481
3482/*
3483 * "foldclosedend()" function
3484 */
3485 static void
3486f_foldclosedend(typval_T *argvars, typval_T *rettv)
3487{
3488 foldclosed_both(argvars, rettv, TRUE);
3489}
3490
3491/*
3492 * "foldlevel()" function
3493 */
3494 static void
3495f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3496{
3497#ifdef FEAT_FOLDING
3498 linenr_T lnum;
3499
3500 lnum = get_tv_lnum(argvars);
3501 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3502 rettv->vval.v_number = foldLevel(lnum);
3503#endif
3504}
3505
3506/*
3507 * "foldtext()" function
3508 */
3509 static void
3510f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
3511{
3512#ifdef FEAT_FOLDING
3513 linenr_T foldstart;
3514 linenr_T foldend;
3515 char_u *dashes;
3516 linenr_T lnum;
3517 char_u *s;
3518 char_u *r;
3519 int len;
3520 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003521 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003522#endif
3523
3524 rettv->v_type = VAR_STRING;
3525 rettv->vval.v_string = NULL;
3526#ifdef FEAT_FOLDING
3527 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
3528 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
3529 dashes = get_vim_var_str(VV_FOLDDASHES);
3530 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
3531 && dashes != NULL)
3532 {
3533 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02003534 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003535 if (!linewhite(lnum))
3536 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003537
3538 /* Find interesting text in this line. */
3539 s = skipwhite(ml_get(lnum));
3540 /* skip C comment-start */
3541 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
3542 {
3543 s = skipwhite(s + 2);
3544 if (*skipwhite(s) == NUL
3545 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
3546 {
3547 s = skipwhite(ml_get(lnum + 1));
3548 if (*s == '*')
3549 s = skipwhite(s + 1);
3550 }
3551 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02003552 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003553 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003554 r = alloc((unsigned)(STRLEN(txt)
3555 + STRLEN(dashes) /* for %s */
3556 + 20 /* for %3ld */
3557 + STRLEN(s))); /* concatenated */
3558 if (r != NULL)
3559 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02003560 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003561 len = (int)STRLEN(r);
3562 STRCAT(r, s);
3563 /* remove 'foldmarker' and 'commentstring' */
3564 foldtext_cleanup(r + len);
3565 rettv->vval.v_string = r;
3566 }
3567 }
3568#endif
3569}
3570
3571/*
3572 * "foldtextresult(lnum)" function
3573 */
3574 static void
3575f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
3576{
3577#ifdef FEAT_FOLDING
3578 linenr_T lnum;
3579 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003580 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003581 foldinfo_T foldinfo;
3582 int fold_count;
3583#endif
3584
3585 rettv->v_type = VAR_STRING;
3586 rettv->vval.v_string = NULL;
3587#ifdef FEAT_FOLDING
3588 lnum = get_tv_lnum(argvars);
3589 /* treat illegal types and illegal string values for {lnum} the same */
3590 if (lnum < 0)
3591 lnum = 0;
3592 fold_count = foldedCount(curwin, lnum, &foldinfo);
3593 if (fold_count > 0)
3594 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01003595 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
3596 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003597 if (text == buf)
3598 text = vim_strsave(text);
3599 rettv->vval.v_string = text;
3600 }
3601#endif
3602}
3603
3604/*
3605 * "foreground()" function
3606 */
3607 static void
3608f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3609{
3610#ifdef FEAT_GUI
3611 if (gui.in_use)
3612 gui_mch_set_foreground();
3613#else
3614# ifdef WIN32
3615 win32_set_foreground();
3616# endif
3617#endif
3618}
3619
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003620 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003621common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003622{
3623 char_u *s;
3624 char_u *name;
3625 int use_string = FALSE;
3626 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003627 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003628
3629 if (argvars[0].v_type == VAR_FUNC)
3630 {
3631 /* function(MyFunc, [arg], dict) */
3632 s = argvars[0].vval.v_string;
3633 }
3634 else if (argvars[0].v_type == VAR_PARTIAL
3635 && argvars[0].vval.v_partial != NULL)
3636 {
3637 /* function(dict.MyFunc, [arg]) */
3638 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003639 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003640 }
3641 else
3642 {
3643 /* function('MyFunc', [arg], dict) */
3644 s = get_tv_string(&argvars[0]);
3645 use_string = TRUE;
3646 }
3647
Bram Moolenaar843b8842016-08-21 14:36:15 +02003648 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003649 {
3650 name = s;
3651 trans_name = trans_function_name(&name, FALSE,
3652 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
3653 if (*name != NUL)
3654 s = NULL;
3655 }
3656
Bram Moolenaar843b8842016-08-21 14:36:15 +02003657 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
3658 || (is_funcref && trans_name == NULL))
Bram Moolenaar5582ef12016-09-14 22:16:13 +02003659 EMSG2(_(e_invarg2), use_string ? get_tv_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003660 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003661 else if (trans_name != NULL && (is_funcref
3662 ? find_func(trans_name) == NULL
3663 : !translated_function_exists(trans_name)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003664 EMSG2(_("E700: Unknown function: %s"), s);
3665 else
3666 {
3667 int dict_idx = 0;
3668 int arg_idx = 0;
3669 list_T *list = NULL;
3670
3671 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
3672 {
3673 char sid_buf[25];
3674 int off = *s == 's' ? 2 : 5;
3675
3676 /* Expand s: and <SID> into <SNR>nr_, so that the function can
3677 * also be called from another script. Using trans_function_name()
3678 * would also work, but some plugins depend on the name being
3679 * printable text. */
3680 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
3681 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
3682 if (name != NULL)
3683 {
3684 STRCPY(name, sid_buf);
3685 STRCAT(name, s + off);
3686 }
3687 }
3688 else
3689 name = vim_strsave(s);
3690
3691 if (argvars[1].v_type != VAR_UNKNOWN)
3692 {
3693 if (argvars[2].v_type != VAR_UNKNOWN)
3694 {
3695 /* function(name, [args], dict) */
3696 arg_idx = 1;
3697 dict_idx = 2;
3698 }
3699 else if (argvars[1].v_type == VAR_DICT)
3700 /* function(name, dict) */
3701 dict_idx = 1;
3702 else
3703 /* function(name, [args]) */
3704 arg_idx = 1;
3705 if (dict_idx > 0)
3706 {
3707 if (argvars[dict_idx].v_type != VAR_DICT)
3708 {
3709 EMSG(_("E922: expected a dict"));
3710 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003711 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003712 }
3713 if (argvars[dict_idx].vval.v_dict == NULL)
3714 dict_idx = 0;
3715 }
3716 if (arg_idx > 0)
3717 {
3718 if (argvars[arg_idx].v_type != VAR_LIST)
3719 {
3720 EMSG(_("E923: Second argument of function() must be a list or a dict"));
3721 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003722 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003723 }
3724 list = argvars[arg_idx].vval.v_list;
3725 if (list == NULL || list->lv_len == 0)
3726 arg_idx = 0;
3727 }
3728 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003729 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003730 {
3731 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
3732
3733 /* result is a VAR_PARTIAL */
3734 if (pt == NULL)
3735 vim_free(name);
3736 else
3737 {
3738 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
3739 {
3740 listitem_T *li;
3741 int i = 0;
3742 int arg_len = 0;
3743 int lv_len = 0;
3744
3745 if (arg_pt != NULL)
3746 arg_len = arg_pt->pt_argc;
3747 if (list != NULL)
3748 lv_len = list->lv_len;
3749 pt->pt_argc = arg_len + lv_len;
3750 pt->pt_argv = (typval_T *)alloc(
3751 sizeof(typval_T) * pt->pt_argc);
3752 if (pt->pt_argv == NULL)
3753 {
3754 vim_free(pt);
3755 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003756 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003757 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003758 for (i = 0; i < arg_len; i++)
3759 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
3760 if (lv_len > 0)
3761 for (li = list->lv_first; li != NULL;
3762 li = li->li_next)
3763 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003764 }
3765
3766 /* For "function(dict.func, [], dict)" and "func" is a partial
3767 * use "dict". That is backwards compatible. */
3768 if (dict_idx > 0)
3769 {
3770 /* The dict is bound explicitly, pt_auto is FALSE. */
3771 pt->pt_dict = argvars[dict_idx].vval.v_dict;
3772 ++pt->pt_dict->dv_refcount;
3773 }
3774 else if (arg_pt != NULL)
3775 {
3776 /* If the dict was bound automatically the result is also
3777 * bound automatically. */
3778 pt->pt_dict = arg_pt->pt_dict;
3779 pt->pt_auto = arg_pt->pt_auto;
3780 if (pt->pt_dict != NULL)
3781 ++pt->pt_dict->dv_refcount;
3782 }
3783
3784 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003785 if (arg_pt != NULL && arg_pt->pt_func != NULL)
3786 {
3787 pt->pt_func = arg_pt->pt_func;
3788 func_ptr_ref(pt->pt_func);
3789 vim_free(name);
3790 }
3791 else if (is_funcref)
3792 {
3793 pt->pt_func = find_func(trans_name);
3794 func_ptr_ref(pt->pt_func);
3795 vim_free(name);
3796 }
3797 else
3798 {
3799 pt->pt_name = name;
3800 func_ref(name);
3801 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003802 }
3803 rettv->v_type = VAR_PARTIAL;
3804 rettv->vval.v_partial = pt;
3805 }
3806 else
3807 {
3808 /* result is a VAR_FUNC */
3809 rettv->v_type = VAR_FUNC;
3810 rettv->vval.v_string = name;
3811 func_ref(name);
3812 }
3813 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003814theend:
3815 vim_free(trans_name);
3816}
3817
3818/*
3819 * "funcref()" function
3820 */
3821 static void
3822f_funcref(typval_T *argvars, typval_T *rettv)
3823{
3824 common_function(argvars, rettv, TRUE);
3825}
3826
3827/*
3828 * "function()" function
3829 */
3830 static void
3831f_function(typval_T *argvars, typval_T *rettv)
3832{
3833 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003834}
3835
3836/*
3837 * "garbagecollect()" function
3838 */
3839 static void
3840f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
3841{
3842 /* This is postponed until we are back at the toplevel, because we may be
3843 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
3844 want_garbage_collect = TRUE;
3845
3846 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
3847 garbage_collect_at_exit = TRUE;
3848}
3849
3850/*
3851 * "get()" function
3852 */
3853 static void
3854f_get(typval_T *argvars, typval_T *rettv)
3855{
3856 listitem_T *li;
3857 list_T *l;
3858 dictitem_T *di;
3859 dict_T *d;
3860 typval_T *tv = NULL;
3861
3862 if (argvars[0].v_type == VAR_LIST)
3863 {
3864 if ((l = argvars[0].vval.v_list) != NULL)
3865 {
3866 int error = FALSE;
3867
3868 li = list_find(l, (long)get_tv_number_chk(&argvars[1], &error));
3869 if (!error && li != NULL)
3870 tv = &li->li_tv;
3871 }
3872 }
3873 else if (argvars[0].v_type == VAR_DICT)
3874 {
3875 if ((d = argvars[0].vval.v_dict) != NULL)
3876 {
3877 di = dict_find(d, get_tv_string(&argvars[1]), -1);
3878 if (di != NULL)
3879 tv = &di->di_tv;
3880 }
3881 }
3882 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
3883 {
3884 partial_T *pt;
3885 partial_T fref_pt;
3886
3887 if (argvars[0].v_type == VAR_PARTIAL)
3888 pt = argvars[0].vval.v_partial;
3889 else
3890 {
3891 vim_memset(&fref_pt, 0, sizeof(fref_pt));
3892 fref_pt.pt_name = argvars[0].vval.v_string;
3893 pt = &fref_pt;
3894 }
3895
3896 if (pt != NULL)
3897 {
3898 char_u *what = get_tv_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003899 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003900
3901 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
3902 {
3903 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003904 n = partial_name(pt);
3905 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003906 rettv->vval.v_string = NULL;
3907 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003908 {
3909 rettv->vval.v_string = vim_strsave(n);
3910 if (rettv->v_type == VAR_FUNC)
3911 func_ref(rettv->vval.v_string);
3912 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003913 }
3914 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003915 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003916 else if (STRCMP(what, "args") == 0)
3917 {
3918 rettv->v_type = VAR_LIST;
3919 if (rettv_list_alloc(rettv) == OK)
3920 {
3921 int i;
3922
3923 for (i = 0; i < pt->pt_argc; ++i)
3924 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
3925 }
3926 }
3927 else
3928 EMSG2(_(e_invarg2), what);
3929 return;
3930 }
3931 }
3932 else
3933 EMSG2(_(e_listdictarg), "get()");
3934
3935 if (tv == NULL)
3936 {
3937 if (argvars[2].v_type != VAR_UNKNOWN)
3938 copy_tv(&argvars[2], rettv);
3939 }
3940 else
3941 copy_tv(tv, rettv);
3942}
3943
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003944#ifdef FEAT_SIGNS
3945/*
3946 * Returns information about signs placed in a buffer as list of dicts.
3947 */
3948 static void
3949get_buffer_signs(buf_T *buf, list_T *l)
3950{
3951 signlist_T *sign;
3952
3953 for (sign = buf->b_signlist; sign; sign = sign->next)
3954 {
3955 dict_T *d = dict_alloc();
3956
3957 if (d != NULL)
3958 {
3959 dict_add_nr_str(d, "id", sign->id, NULL);
3960 dict_add_nr_str(d, "lnum", sign->lnum, NULL);
Bram Moolenaar6a402ed2016-08-28 14:11:24 +02003961 dict_add_nr_str(d, "name", 0L, sign_typenr2name(sign->typenr));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003962
3963 list_append_dict(l, d);
3964 }
3965 }
3966}
3967#endif
3968
3969/*
3970 * Returns buffer options, variables and other attributes in a dictionary.
3971 */
3972 static dict_T *
3973get_buffer_info(buf_T *buf)
3974{
3975 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003976 tabpage_T *tp;
3977 win_T *wp;
3978 list_T *windows;
3979
3980 dict = dict_alloc();
3981 if (dict == NULL)
3982 return NULL;
3983
Bram Moolenaar33928832016-08-18 21:22:04 +02003984 dict_add_nr_str(dict, "bufnr", buf->b_fnum, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003985 dict_add_nr_str(dict, "name", 0L,
3986 buf->b_ffname != NULL ? buf->b_ffname : (char_u *)"");
Bram Moolenaarf845b872017-01-06 14:04:54 +01003987 dict_add_nr_str(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
3988 : buflist_findlnum(buf), NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003989 dict_add_nr_str(dict, "loaded", buf->b_ml.ml_mfp != NULL, NULL);
3990 dict_add_nr_str(dict, "listed", buf->b_p_bl, NULL);
3991 dict_add_nr_str(dict, "changed", bufIsChanged(buf), NULL);
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003992 dict_add_nr_str(dict, "changedtick", CHANGEDTICK(buf), NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003993 dict_add_nr_str(dict, "hidden",
3994 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0,
3995 NULL);
3996
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02003997 /* Get a reference to buffer variables */
3998 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003999
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004000 /* List of windows displaying this buffer */
4001 windows = list_alloc();
4002 if (windows != NULL)
4003 {
4004 FOR_ALL_TAB_WINDOWS(tp, wp)
4005 if (wp->w_buffer == buf)
4006 list_append_number(windows, (varnumber_T)wp->w_id);
4007 dict_add_list(dict, "windows", windows);
4008 }
4009
4010#ifdef FEAT_SIGNS
4011 if (buf->b_signlist != NULL)
4012 {
4013 /* List of signs placed in this buffer */
4014 list_T *signs = list_alloc();
4015 if (signs != NULL)
4016 {
4017 get_buffer_signs(buf, signs);
4018 dict_add_list(dict, "signs", signs);
4019 }
4020 }
4021#endif
4022
4023 return dict;
4024}
4025
4026/*
4027 * "getbufinfo()" function
4028 */
4029 static void
4030f_getbufinfo(typval_T *argvars, typval_T *rettv)
4031{
4032 buf_T *buf = NULL;
4033 buf_T *argbuf = NULL;
4034 dict_T *d;
4035 int filtered = FALSE;
4036 int sel_buflisted = FALSE;
4037 int sel_bufloaded = FALSE;
4038
4039 if (rettv_list_alloc(rettv) != OK)
4040 return;
4041
4042 /* List of all the buffers or selected buffers */
4043 if (argvars[0].v_type == VAR_DICT)
4044 {
4045 dict_T *sel_d = argvars[0].vval.v_dict;
4046
4047 if (sel_d != NULL)
4048 {
4049 dictitem_T *di;
4050
4051 filtered = TRUE;
4052
4053 di = dict_find(sel_d, (char_u *)"buflisted", -1);
4054 if (di != NULL && get_tv_number(&di->di_tv))
4055 sel_buflisted = TRUE;
4056
4057 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
4058 if (di != NULL && get_tv_number(&di->di_tv))
4059 sel_bufloaded = TRUE;
4060 }
4061 }
4062 else if (argvars[0].v_type != VAR_UNKNOWN)
4063 {
4064 /* Information about one buffer. Argument specifies the buffer */
4065 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4066 ++emsg_off;
4067 argbuf = get_buf_tv(&argvars[0], FALSE);
4068 --emsg_off;
4069 if (argbuf == NULL)
4070 return;
4071 }
4072
4073 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004074 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004075 {
4076 if (argbuf != NULL && argbuf != buf)
4077 continue;
4078 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
4079 || (sel_buflisted && !buf->b_p_bl)))
4080 continue;
4081
4082 d = get_buffer_info(buf);
4083 if (d != NULL)
4084 list_append_dict(rettv->vval.v_list, d);
4085 if (argbuf != NULL)
4086 return;
4087 }
4088}
4089
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004090static void get_buffer_lines(buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv);
4091
4092/*
4093 * Get line or list of lines from buffer "buf" into "rettv".
4094 * Return a range (from start to end) of lines in rettv from the specified
4095 * buffer.
4096 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4097 */
4098 static void
4099get_buffer_lines(
4100 buf_T *buf,
4101 linenr_T start,
4102 linenr_T end,
4103 int retlist,
4104 typval_T *rettv)
4105{
4106 char_u *p;
4107
4108 rettv->v_type = VAR_STRING;
4109 rettv->vval.v_string = NULL;
4110 if (retlist && rettv_list_alloc(rettv) == FAIL)
4111 return;
4112
4113 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4114 return;
4115
4116 if (!retlist)
4117 {
4118 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4119 p = ml_get_buf(buf, start, FALSE);
4120 else
4121 p = (char_u *)"";
4122 rettv->vval.v_string = vim_strsave(p);
4123 }
4124 else
4125 {
4126 if (end < start)
4127 return;
4128
4129 if (start < 1)
4130 start = 1;
4131 if (end > buf->b_ml.ml_line_count)
4132 end = buf->b_ml.ml_line_count;
4133 while (start <= end)
4134 if (list_append_string(rettv->vval.v_list,
4135 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4136 break;
4137 }
4138}
4139
4140/*
4141 * Get the lnum from the first argument.
4142 * Also accepts "$", then "buf" is used.
4143 * Returns 0 on error.
4144 */
4145 static linenr_T
4146get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
4147{
4148 if (argvars[0].v_type == VAR_STRING
4149 && argvars[0].vval.v_string != NULL
4150 && argvars[0].vval.v_string[0] == '$'
4151 && buf != NULL)
4152 return buf->b_ml.ml_line_count;
4153 return (linenr_T)get_tv_number_chk(&argvars[0], NULL);
4154}
4155
4156/*
4157 * "getbufline()" function
4158 */
4159 static void
4160f_getbufline(typval_T *argvars, typval_T *rettv)
4161{
4162 linenr_T lnum;
4163 linenr_T end;
4164 buf_T *buf;
4165
4166 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4167 ++emsg_off;
4168 buf = get_buf_tv(&argvars[0], FALSE);
4169 --emsg_off;
4170
4171 lnum = get_tv_lnum_buf(&argvars[1], buf);
4172 if (argvars[2].v_type == VAR_UNKNOWN)
4173 end = lnum;
4174 else
4175 end = get_tv_lnum_buf(&argvars[2], buf);
4176
4177 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4178}
4179
4180/*
4181 * "getbufvar()" function
4182 */
4183 static void
4184f_getbufvar(typval_T *argvars, typval_T *rettv)
4185{
4186 buf_T *buf;
4187 buf_T *save_curbuf;
4188 char_u *varname;
4189 dictitem_T *v;
4190 int done = FALSE;
4191
4192 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4193 varname = get_tv_string_chk(&argvars[1]);
4194 ++emsg_off;
4195 buf = get_buf_tv(&argvars[0], FALSE);
4196
4197 rettv->v_type = VAR_STRING;
4198 rettv->vval.v_string = NULL;
4199
4200 if (buf != NULL && varname != NULL)
4201 {
4202 /* set curbuf to be our buf, temporarily */
4203 save_curbuf = curbuf;
4204 curbuf = buf;
4205
Bram Moolenaar30567352016-08-27 21:25:44 +02004206 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004207 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004208 if (varname[1] == NUL)
4209 {
4210 /* get all buffer-local options in a dict */
4211 dict_T *opts = get_winbuf_options(TRUE);
4212
4213 if (opts != NULL)
4214 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004215 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004216 done = TRUE;
4217 }
4218 }
4219 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4220 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004221 done = TRUE;
4222 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004223 else
4224 {
4225 /* Look up the variable. */
4226 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4227 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4228 'b', varname, FALSE);
4229 if (v != NULL)
4230 {
4231 copy_tv(&v->di_tv, rettv);
4232 done = TRUE;
4233 }
4234 }
4235
4236 /* restore previous notion of curbuf */
4237 curbuf = save_curbuf;
4238 }
4239
4240 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4241 /* use the default value */
4242 copy_tv(&argvars[2], rettv);
4243
4244 --emsg_off;
4245}
4246
4247/*
4248 * "getchar()" function
4249 */
4250 static void
4251f_getchar(typval_T *argvars, typval_T *rettv)
4252{
4253 varnumber_T n;
4254 int error = FALSE;
4255
4256 /* Position the cursor. Needed after a message that ends in a space. */
4257 windgoto(msg_row, msg_col);
4258
4259 ++no_mapping;
4260 ++allow_keys;
4261 for (;;)
4262 {
4263 if (argvars[0].v_type == VAR_UNKNOWN)
4264 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004265 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004266 else if (get_tv_number_chk(&argvars[0], &error) == 1)
4267 /* getchar(1): only check if char avail */
4268 n = vpeekc_any();
4269 else if (error || vpeekc_any() == NUL)
4270 /* illegal argument or getchar(0) and no char avail: return zero */
4271 n = 0;
4272 else
4273 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004274 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004275
4276 if (n == K_IGNORE)
4277 continue;
4278 break;
4279 }
4280 --no_mapping;
4281 --allow_keys;
4282
4283 set_vim_var_nr(VV_MOUSE_WIN, 0);
4284 set_vim_var_nr(VV_MOUSE_WINID, 0);
4285 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4286 set_vim_var_nr(VV_MOUSE_COL, 0);
4287
4288 rettv->vval.v_number = n;
4289 if (IS_SPECIAL(n) || mod_mask != 0)
4290 {
4291 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4292 int i = 0;
4293
4294 /* Turn a special key into three bytes, plus modifier. */
4295 if (mod_mask != 0)
4296 {
4297 temp[i++] = K_SPECIAL;
4298 temp[i++] = KS_MODIFIER;
4299 temp[i++] = mod_mask;
4300 }
4301 if (IS_SPECIAL(n))
4302 {
4303 temp[i++] = K_SPECIAL;
4304 temp[i++] = K_SECOND(n);
4305 temp[i++] = K_THIRD(n);
4306 }
4307#ifdef FEAT_MBYTE
4308 else if (has_mbyte)
4309 i += (*mb_char2bytes)(n, temp + i);
4310#endif
4311 else
4312 temp[i++] = n;
4313 temp[i++] = NUL;
4314 rettv->v_type = VAR_STRING;
4315 rettv->vval.v_string = vim_strsave(temp);
4316
4317#ifdef FEAT_MOUSE
4318 if (is_mouse_key(n))
4319 {
4320 int row = mouse_row;
4321 int col = mouse_col;
4322 win_T *win;
4323 linenr_T lnum;
4324# ifdef FEAT_WINDOWS
4325 win_T *wp;
4326# endif
4327 int winnr = 1;
4328
4329 if (row >= 0 && col >= 0)
4330 {
4331 /* Find the window at the mouse coordinates and compute the
4332 * text position. */
4333 win = mouse_find_win(&row, &col);
4334 (void)mouse_comp_pos(win, &row, &col, &lnum);
4335# ifdef FEAT_WINDOWS
4336 for (wp = firstwin; wp != win; wp = wp->w_next)
4337 ++winnr;
4338# endif
4339 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4340 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4341 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4342 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4343 }
4344 }
4345#endif
4346 }
4347}
4348
4349/*
4350 * "getcharmod()" function
4351 */
4352 static void
4353f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4354{
4355 rettv->vval.v_number = mod_mask;
4356}
4357
4358/*
4359 * "getcharsearch()" function
4360 */
4361 static void
4362f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4363{
4364 if (rettv_dict_alloc(rettv) != FAIL)
4365 {
4366 dict_T *dict = rettv->vval.v_dict;
4367
4368 dict_add_nr_str(dict, "char", 0L, last_csearch());
4369 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
4370 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
4371 }
4372}
4373
4374/*
4375 * "getcmdline()" function
4376 */
4377 static void
4378f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4379{
4380 rettv->v_type = VAR_STRING;
4381 rettv->vval.v_string = get_cmdline_str();
4382}
4383
4384/*
4385 * "getcmdpos()" function
4386 */
4387 static void
4388f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4389{
4390 rettv->vval.v_number = get_cmdline_pos() + 1;
4391}
4392
4393/*
4394 * "getcmdtype()" function
4395 */
4396 static void
4397f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4398{
4399 rettv->v_type = VAR_STRING;
4400 rettv->vval.v_string = alloc(2);
4401 if (rettv->vval.v_string != NULL)
4402 {
4403 rettv->vval.v_string[0] = get_cmdline_type();
4404 rettv->vval.v_string[1] = NUL;
4405 }
4406}
4407
4408/*
4409 * "getcmdwintype()" function
4410 */
4411 static void
4412f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4413{
4414 rettv->v_type = VAR_STRING;
4415 rettv->vval.v_string = NULL;
4416#ifdef FEAT_CMDWIN
4417 rettv->vval.v_string = alloc(2);
4418 if (rettv->vval.v_string != NULL)
4419 {
4420 rettv->vval.v_string[0] = cmdwin_type;
4421 rettv->vval.v_string[1] = NUL;
4422 }
4423#endif
4424}
4425
4426#if defined(FEAT_CMDL_COMPL)
4427/*
4428 * "getcompletion()" function
4429 */
4430 static void
4431f_getcompletion(typval_T *argvars, typval_T *rettv)
4432{
4433 char_u *pat;
4434 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004435 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004436 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
4437 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004438
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004439 if (argvars[2].v_type != VAR_UNKNOWN)
4440 filtered = get_tv_number_chk(&argvars[2], NULL);
4441
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004442 if (p_wic)
4443 options |= WILD_ICASE;
4444
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004445 /* For filtered results, 'wildignore' is used */
4446 if (!filtered)
4447 options |= WILD_KEEP_ALL;
4448
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004449 ExpandInit(&xpc);
4450 xpc.xp_pattern = get_tv_string(&argvars[0]);
4451 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4452 xpc.xp_context = cmdcomplete_str_to_type(get_tv_string(&argvars[1]));
4453 if (xpc.xp_context == EXPAND_NOTHING)
4454 {
4455 if (argvars[1].v_type == VAR_STRING)
4456 EMSG2(_(e_invarg2), argvars[1].vval.v_string);
4457 else
4458 EMSG(_(e_invarg));
4459 return;
4460 }
4461
4462# if defined(FEAT_MENU)
4463 if (xpc.xp_context == EXPAND_MENUS)
4464 {
4465 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4466 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4467 }
4468# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02004469#ifdef FEAT_CSCOPE
4470 if (xpc.xp_context == EXPAND_CSCOPE)
4471 {
4472 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4473 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4474 }
4475#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02004476#ifdef FEAT_SIGNS
4477 if (xpc.xp_context == EXPAND_SIGN)
4478 {
4479 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4480 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4481 }
4482#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004483
4484 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4485 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
4486 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004487 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004488
4489 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4490
4491 for (i = 0; i < xpc.xp_numfiles; i++)
4492 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4493 }
4494 vim_free(pat);
4495 ExpandCleanup(&xpc);
4496}
4497#endif
4498
4499/*
4500 * "getcwd()" function
4501 */
4502 static void
4503f_getcwd(typval_T *argvars, typval_T *rettv)
4504{
4505 win_T *wp = NULL;
4506 char_u *cwd;
4507
4508 rettv->v_type = VAR_STRING;
4509 rettv->vval.v_string = NULL;
4510
4511 wp = find_tabwin(&argvars[0], &argvars[1]);
4512 if (wp != NULL)
4513 {
4514 if (wp->w_localdir != NULL)
4515 rettv->vval.v_string = vim_strsave(wp->w_localdir);
4516 else if (globaldir != NULL)
4517 rettv->vval.v_string = vim_strsave(globaldir);
4518 else
4519 {
4520 cwd = alloc(MAXPATHL);
4521 if (cwd != NULL)
4522 {
4523 if (mch_dirname(cwd, MAXPATHL) != FAIL)
4524 rettv->vval.v_string = vim_strsave(cwd);
4525 vim_free(cwd);
4526 }
4527 }
4528#ifdef BACKSLASH_IN_FILENAME
4529 if (rettv->vval.v_string != NULL)
4530 slash_adjust(rettv->vval.v_string);
4531#endif
4532 }
4533}
4534
4535/*
4536 * "getfontname()" function
4537 */
4538 static void
4539f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4540{
4541 rettv->v_type = VAR_STRING;
4542 rettv->vval.v_string = NULL;
4543#ifdef FEAT_GUI
4544 if (gui.in_use)
4545 {
4546 GuiFont font;
4547 char_u *name = NULL;
4548
4549 if (argvars[0].v_type == VAR_UNKNOWN)
4550 {
4551 /* Get the "Normal" font. Either the name saved by
4552 * hl_set_font_name() or from the font ID. */
4553 font = gui.norm_font;
4554 name = hl_get_font_name();
4555 }
4556 else
4557 {
4558 name = get_tv_string(&argvars[0]);
4559 if (STRCMP(name, "*") == 0) /* don't use font dialog */
4560 return;
4561 font = gui_mch_get_font(name, FALSE);
4562 if (font == NOFONT)
4563 return; /* Invalid font name, return empty string. */
4564 }
4565 rettv->vval.v_string = gui_mch_get_fontname(font, name);
4566 if (argvars[0].v_type != VAR_UNKNOWN)
4567 gui_mch_free_font(font);
4568 }
4569#endif
4570}
4571
4572/*
4573 * "getfperm({fname})" function
4574 */
4575 static void
4576f_getfperm(typval_T *argvars, typval_T *rettv)
4577{
4578 char_u *fname;
4579 stat_T st;
4580 char_u *perm = NULL;
4581 char_u flags[] = "rwx";
4582 int i;
4583
4584 fname = get_tv_string(&argvars[0]);
4585
4586 rettv->v_type = VAR_STRING;
4587 if (mch_stat((char *)fname, &st) >= 0)
4588 {
4589 perm = vim_strsave((char_u *)"---------");
4590 if (perm != NULL)
4591 {
4592 for (i = 0; i < 9; i++)
4593 {
4594 if (st.st_mode & (1 << (8 - i)))
4595 perm[i] = flags[i % 3];
4596 }
4597 }
4598 }
4599 rettv->vval.v_string = perm;
4600}
4601
4602/*
4603 * "getfsize({fname})" function
4604 */
4605 static void
4606f_getfsize(typval_T *argvars, typval_T *rettv)
4607{
4608 char_u *fname;
4609 stat_T st;
4610
4611 fname = get_tv_string(&argvars[0]);
4612
4613 rettv->v_type = VAR_NUMBER;
4614
4615 if (mch_stat((char *)fname, &st) >= 0)
4616 {
4617 if (mch_isdir(fname))
4618 rettv->vval.v_number = 0;
4619 else
4620 {
4621 rettv->vval.v_number = (varnumber_T)st.st_size;
4622
4623 /* non-perfect check for overflow */
4624 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
4625 rettv->vval.v_number = -2;
4626 }
4627 }
4628 else
4629 rettv->vval.v_number = -1;
4630}
4631
4632/*
4633 * "getftime({fname})" function
4634 */
4635 static void
4636f_getftime(typval_T *argvars, typval_T *rettv)
4637{
4638 char_u *fname;
4639 stat_T st;
4640
4641 fname = get_tv_string(&argvars[0]);
4642
4643 if (mch_stat((char *)fname, &st) >= 0)
4644 rettv->vval.v_number = (varnumber_T)st.st_mtime;
4645 else
4646 rettv->vval.v_number = -1;
4647}
4648
4649/*
4650 * "getftype({fname})" function
4651 */
4652 static void
4653f_getftype(typval_T *argvars, typval_T *rettv)
4654{
4655 char_u *fname;
4656 stat_T st;
4657 char_u *type = NULL;
4658 char *t;
4659
4660 fname = get_tv_string(&argvars[0]);
4661
4662 rettv->v_type = VAR_STRING;
4663 if (mch_lstat((char *)fname, &st) >= 0)
4664 {
4665#ifdef S_ISREG
4666 if (S_ISREG(st.st_mode))
4667 t = "file";
4668 else if (S_ISDIR(st.st_mode))
4669 t = "dir";
4670# ifdef S_ISLNK
4671 else if (S_ISLNK(st.st_mode))
4672 t = "link";
4673# endif
4674# ifdef S_ISBLK
4675 else if (S_ISBLK(st.st_mode))
4676 t = "bdev";
4677# endif
4678# ifdef S_ISCHR
4679 else if (S_ISCHR(st.st_mode))
4680 t = "cdev";
4681# endif
4682# ifdef S_ISFIFO
4683 else if (S_ISFIFO(st.st_mode))
4684 t = "fifo";
4685# endif
4686# ifdef S_ISSOCK
4687 else if (S_ISSOCK(st.st_mode))
4688 t = "fifo";
4689# endif
4690 else
4691 t = "other";
4692#else
4693# ifdef S_IFMT
4694 switch (st.st_mode & S_IFMT)
4695 {
4696 case S_IFREG: t = "file"; break;
4697 case S_IFDIR: t = "dir"; break;
4698# ifdef S_IFLNK
4699 case S_IFLNK: t = "link"; break;
4700# endif
4701# ifdef S_IFBLK
4702 case S_IFBLK: t = "bdev"; break;
4703# endif
4704# ifdef S_IFCHR
4705 case S_IFCHR: t = "cdev"; break;
4706# endif
4707# ifdef S_IFIFO
4708 case S_IFIFO: t = "fifo"; break;
4709# endif
4710# ifdef S_IFSOCK
4711 case S_IFSOCK: t = "socket"; break;
4712# endif
4713 default: t = "other";
4714 }
4715# else
4716 if (mch_isdir(fname))
4717 t = "dir";
4718 else
4719 t = "file";
4720# endif
4721#endif
4722 type = vim_strsave((char_u *)t);
4723 }
4724 rettv->vval.v_string = type;
4725}
4726
4727/*
4728 * "getline(lnum, [end])" function
4729 */
4730 static void
4731f_getline(typval_T *argvars, typval_T *rettv)
4732{
4733 linenr_T lnum;
4734 linenr_T end;
4735 int retlist;
4736
4737 lnum = get_tv_lnum(argvars);
4738 if (argvars[1].v_type == VAR_UNKNOWN)
4739 {
4740 end = 0;
4741 retlist = FALSE;
4742 }
4743 else
4744 {
4745 end = get_tv_lnum(&argvars[1]);
4746 retlist = TRUE;
4747 }
4748
4749 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
4750}
4751
Bram Moolenaar4ae20952016-08-13 15:29:14 +02004752#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02004753 static void
4754get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
4755{
Bram Moolenaard823fa92016-08-12 16:29:27 +02004756 if (what_arg->v_type == VAR_UNKNOWN)
4757 {
4758 if (rettv_list_alloc(rettv) == OK)
4759 if (is_qf || wp != NULL)
4760 (void)get_errorlist(wp, -1, rettv->vval.v_list);
4761 }
4762 else
4763 {
4764 if (rettv_dict_alloc(rettv) == OK)
4765 if (is_qf || (wp != NULL))
4766 {
4767 if (what_arg->v_type == VAR_DICT)
4768 {
4769 dict_T *d = what_arg->vval.v_dict;
4770
4771 if (d != NULL)
4772 get_errorlist_properties(wp, d, rettv->vval.v_dict);
4773 }
4774 else
4775 EMSG(_(e_dictreq));
4776 }
4777 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004778}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02004779#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02004780
4781/*
4782 * "getloclist()" function
4783 */
4784 static void
4785f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4786{
4787#ifdef FEAT_QUICKFIX
4788 win_T *wp;
4789
4790 wp = find_win_by_nr(&argvars[0], NULL);
4791 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
4792#endif
4793}
4794
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004795/*
4796 * "getmatches()" function
4797 */
4798 static void
4799f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4800{
4801#ifdef FEAT_SEARCH_EXTRA
4802 dict_T *dict;
4803 matchitem_T *cur = curwin->w_match_head;
4804 int i;
4805
4806 if (rettv_list_alloc(rettv) == OK)
4807 {
4808 while (cur != NULL)
4809 {
4810 dict = dict_alloc();
4811 if (dict == NULL)
4812 return;
4813 if (cur->match.regprog == NULL)
4814 {
4815 /* match added with matchaddpos() */
4816 for (i = 0; i < MAXPOSMATCH; ++i)
4817 {
4818 llpos_T *llpos;
4819 char buf[6];
4820 list_T *l;
4821
4822 llpos = &cur->pos.pos[i];
4823 if (llpos->lnum == 0)
4824 break;
4825 l = list_alloc();
4826 if (l == NULL)
4827 break;
4828 list_append_number(l, (varnumber_T)llpos->lnum);
4829 if (llpos->col > 0)
4830 {
4831 list_append_number(l, (varnumber_T)llpos->col);
4832 list_append_number(l, (varnumber_T)llpos->len);
4833 }
4834 sprintf(buf, "pos%d", i + 1);
4835 dict_add_list(dict, buf, l);
4836 }
4837 }
4838 else
4839 {
4840 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
4841 }
4842 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
4843 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
4844 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
4845# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
4846 if (cur->conceal_char)
4847 {
4848 char_u buf[MB_MAXBYTES + 1];
4849
4850 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
4851 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
4852 }
4853# endif
4854 list_append_dict(rettv->vval.v_list, dict);
4855 cur = cur->next;
4856 }
4857 }
4858#endif
4859}
4860
4861/*
4862 * "getpid()" function
4863 */
4864 static void
4865f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
4866{
4867 rettv->vval.v_number = mch_get_pid();
4868}
4869
4870 static void
4871getpos_both(
4872 typval_T *argvars,
4873 typval_T *rettv,
4874 int getcurpos)
4875{
4876 pos_T *fp;
4877 list_T *l;
4878 int fnum = -1;
4879
4880 if (rettv_list_alloc(rettv) == OK)
4881 {
4882 l = rettv->vval.v_list;
4883 if (getcurpos)
4884 fp = &curwin->w_cursor;
4885 else
4886 fp = var2fpos(&argvars[0], TRUE, &fnum);
4887 if (fnum != -1)
4888 list_append_number(l, (varnumber_T)fnum);
4889 else
4890 list_append_number(l, (varnumber_T)0);
4891 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
4892 : (varnumber_T)0);
4893 list_append_number(l, (fp != NULL)
4894 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
4895 : (varnumber_T)0);
4896 list_append_number(l,
4897#ifdef FEAT_VIRTUALEDIT
4898 (fp != NULL) ? (varnumber_T)fp->coladd :
4899#endif
4900 (varnumber_T)0);
4901 if (getcurpos)
4902 {
4903 update_curswant();
4904 list_append_number(l, curwin->w_curswant == MAXCOL ?
4905 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
4906 }
4907 }
4908 else
4909 rettv->vval.v_number = FALSE;
4910}
4911
4912
4913/*
4914 * "getcurpos()" function
4915 */
4916 static void
4917f_getcurpos(typval_T *argvars, typval_T *rettv)
4918{
4919 getpos_both(argvars, rettv, TRUE);
4920}
4921
4922/*
4923 * "getpos(string)" function
4924 */
4925 static void
4926f_getpos(typval_T *argvars, typval_T *rettv)
4927{
4928 getpos_both(argvars, rettv, FALSE);
4929}
4930
4931/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004932 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004933 */
4934 static void
4935f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4936{
4937#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02004938 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004939#endif
4940}
4941
4942/*
4943 * "getreg()" function
4944 */
4945 static void
4946f_getreg(typval_T *argvars, typval_T *rettv)
4947{
4948 char_u *strregname;
4949 int regname;
4950 int arg2 = FALSE;
4951 int return_list = FALSE;
4952 int error = FALSE;
4953
4954 if (argvars[0].v_type != VAR_UNKNOWN)
4955 {
4956 strregname = get_tv_string_chk(&argvars[0]);
4957 error = strregname == NULL;
4958 if (argvars[1].v_type != VAR_UNKNOWN)
4959 {
4960 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
4961 if (!error && argvars[2].v_type != VAR_UNKNOWN)
4962 return_list = (int)get_tv_number_chk(&argvars[2], &error);
4963 }
4964 }
4965 else
4966 strregname = get_vim_var_str(VV_REG);
4967
4968 if (error)
4969 return;
4970
4971 regname = (strregname == NULL ? '"' : *strregname);
4972 if (regname == 0)
4973 regname = '"';
4974
4975 if (return_list)
4976 {
4977 rettv->v_type = VAR_LIST;
4978 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
4979 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
4980 if (rettv->vval.v_list == NULL)
4981 (void)rettv_list_alloc(rettv);
4982 else
4983 ++rettv->vval.v_list->lv_refcount;
4984 }
4985 else
4986 {
4987 rettv->v_type = VAR_STRING;
4988 rettv->vval.v_string = get_reg_contents(regname,
4989 arg2 ? GREG_EXPR_SRC : 0);
4990 }
4991}
4992
4993/*
4994 * "getregtype()" function
4995 */
4996 static void
4997f_getregtype(typval_T *argvars, typval_T *rettv)
4998{
4999 char_u *strregname;
5000 int regname;
5001 char_u buf[NUMBUFLEN + 2];
5002 long reglen = 0;
5003
5004 if (argvars[0].v_type != VAR_UNKNOWN)
5005 {
5006 strregname = get_tv_string_chk(&argvars[0]);
5007 if (strregname == NULL) /* type error; errmsg already given */
5008 {
5009 rettv->v_type = VAR_STRING;
5010 rettv->vval.v_string = NULL;
5011 return;
5012 }
5013 }
5014 else
5015 /* Default to v:register */
5016 strregname = get_vim_var_str(VV_REG);
5017
5018 regname = (strregname == NULL ? '"' : *strregname);
5019 if (regname == 0)
5020 regname = '"';
5021
5022 buf[0] = NUL;
5023 buf[1] = NUL;
5024 switch (get_reg_type(regname, &reglen))
5025 {
5026 case MLINE: buf[0] = 'V'; break;
5027 case MCHAR: buf[0] = 'v'; break;
5028 case MBLOCK:
5029 buf[0] = Ctrl_V;
5030 sprintf((char *)buf + 1, "%ld", reglen + 1);
5031 break;
5032 }
5033 rettv->v_type = VAR_STRING;
5034 rettv->vval.v_string = vim_strsave(buf);
5035}
5036
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005037#ifdef FEAT_WINDOWS
5038/*
5039 * Returns information (variables, options, etc.) about a tab page
5040 * as a dictionary.
5041 */
5042 static dict_T *
5043get_tabpage_info(tabpage_T *tp, int tp_idx)
5044{
5045 win_T *wp;
5046 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005047 list_T *l;
5048
5049 dict = dict_alloc();
5050 if (dict == NULL)
5051 return NULL;
5052
Bram Moolenaar33928832016-08-18 21:22:04 +02005053 dict_add_nr_str(dict, "tabnr", tp_idx, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005054
5055 l = list_alloc();
5056 if (l != NULL)
5057 {
5058 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5059 wp; wp = wp->w_next)
5060 list_append_number(l, (varnumber_T)wp->w_id);
5061 dict_add_list(dict, "windows", l);
5062 }
5063
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005064 /* Make a reference to tabpage variables */
5065 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005066
5067 return dict;
5068}
5069#endif
5070
5071/*
5072 * "gettabinfo()" function
5073 */
5074 static void
5075f_gettabinfo(typval_T *argvars, typval_T *rettv)
5076{
5077#ifdef FEAT_WINDOWS
5078 tabpage_T *tp, *tparg = NULL;
5079 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005080 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005081
5082 if (rettv_list_alloc(rettv) != OK)
5083 return;
5084
5085 if (argvars[0].v_type != VAR_UNKNOWN)
5086 {
5087 /* Information about one tab page */
5088 tparg = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5089 if (tparg == NULL)
5090 return;
5091 }
5092
5093 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005094 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005095 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005096 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005097 if (tparg != NULL && tp != tparg)
5098 continue;
5099 d = get_tabpage_info(tp, tpnr);
5100 if (d != NULL)
5101 list_append_dict(rettv->vval.v_list, d);
5102 if (tparg != NULL)
5103 return;
5104 }
5105#endif
5106}
5107
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005108/*
5109 * "gettabvar()" function
5110 */
5111 static void
5112f_gettabvar(typval_T *argvars, typval_T *rettv)
5113{
5114 win_T *oldcurwin;
5115 tabpage_T *tp, *oldtabpage;
5116 dictitem_T *v;
5117 char_u *varname;
5118 int done = FALSE;
5119
5120 rettv->v_type = VAR_STRING;
5121 rettv->vval.v_string = NULL;
5122
5123 varname = get_tv_string_chk(&argvars[1]);
5124 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5125 if (tp != NULL && varname != NULL)
5126 {
5127 /* Set tp to be our tabpage, temporarily. Also set the window to the
5128 * first window in the tabpage, otherwise the window is not valid. */
5129 if (switch_win(&oldcurwin, &oldtabpage,
5130 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
5131 == OK)
5132 {
5133 /* look up the variable */
5134 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5135 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5136 if (v != NULL)
5137 {
5138 copy_tv(&v->di_tv, rettv);
5139 done = TRUE;
5140 }
5141 }
5142
5143 /* restore previous notion of curwin */
5144 restore_win(oldcurwin, oldtabpage, TRUE);
5145 }
5146
5147 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5148 copy_tv(&argvars[2], rettv);
5149}
5150
5151/*
5152 * "gettabwinvar()" function
5153 */
5154 static void
5155f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5156{
5157 getwinvar(argvars, rettv, 1);
5158}
5159
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005160#ifdef FEAT_WINDOWS
5161/*
5162 * Returns information about a window as a dictionary.
5163 */
5164 static dict_T *
5165get_win_info(win_T *wp, short tpnr, short winnr)
5166{
5167 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005168
5169 dict = dict_alloc();
5170 if (dict == NULL)
5171 return NULL;
5172
Bram Moolenaar33928832016-08-18 21:22:04 +02005173 dict_add_nr_str(dict, "tabnr", tpnr, NULL);
5174 dict_add_nr_str(dict, "winnr", winnr, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005175 dict_add_nr_str(dict, "winid", wp->w_id, NULL);
5176 dict_add_nr_str(dict, "height", wp->w_height, NULL);
5177 dict_add_nr_str(dict, "width", wp->w_width, NULL);
Bram Moolenaar33928832016-08-18 21:22:04 +02005178 dict_add_nr_str(dict, "bufnr", wp->w_buffer->b_fnum, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005179
Bram Moolenaar386600f2016-08-15 22:16:25 +02005180#ifdef FEAT_QUICKFIX
5181 dict_add_nr_str(dict, "quickfix", bt_quickfix(wp->w_buffer), NULL);
5182 dict_add_nr_str(dict, "loclist",
5183 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL), NULL);
5184#endif
5185
Bram Moolenaar30567352016-08-27 21:25:44 +02005186 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005187 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005188
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005189 return dict;
5190}
5191#endif
5192
5193/*
5194 * "getwininfo()" function
5195 */
5196 static void
5197f_getwininfo(typval_T *argvars, typval_T *rettv)
5198{
5199#ifdef FEAT_WINDOWS
5200 tabpage_T *tp;
5201 win_T *wp = NULL, *wparg = NULL;
5202 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005203 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005204#endif
5205
5206 if (rettv_list_alloc(rettv) != OK)
5207 return;
5208
5209#ifdef FEAT_WINDOWS
5210 if (argvars[0].v_type != VAR_UNKNOWN)
5211 {
5212 wparg = win_id2wp(argvars);
5213 if (wparg == NULL)
5214 return;
5215 }
5216
5217 /* Collect information about either all the windows across all the tab
5218 * pages or one particular window.
5219 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005220 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005221 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005222 tabnr++;
5223 winnr = 0;
5224 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005225 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005226 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005227 if (wparg != NULL && wp != wparg)
5228 continue;
5229 d = get_win_info(wp, tabnr, winnr);
5230 if (d != NULL)
5231 list_append_dict(rettv->vval.v_list, d);
5232 if (wparg != NULL)
5233 /* found information about a specific window */
5234 return;
5235 }
5236 }
5237#endif
5238}
5239
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005240/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005241 * "win_findbuf()" function
5242 */
5243 static void
5244f_win_findbuf(typval_T *argvars, typval_T *rettv)
5245{
5246 if (rettv_list_alloc(rettv) != FAIL)
5247 win_findbuf(argvars, rettv->vval.v_list);
5248}
5249
5250/*
5251 * "win_getid()" function
5252 */
5253 static void
5254f_win_getid(typval_T *argvars, typval_T *rettv)
5255{
5256 rettv->vval.v_number = win_getid(argvars);
5257}
5258
5259/*
5260 * "win_gotoid()" function
5261 */
5262 static void
5263f_win_gotoid(typval_T *argvars, typval_T *rettv)
5264{
5265 rettv->vval.v_number = win_gotoid(argvars);
5266}
5267
5268/*
5269 * "win_id2tabwin()" function
5270 */
5271 static void
5272f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5273{
5274 if (rettv_list_alloc(rettv) != FAIL)
5275 win_id2tabwin(argvars, rettv->vval.v_list);
5276}
5277
5278/*
5279 * "win_id2win()" function
5280 */
5281 static void
5282f_win_id2win(typval_T *argvars, typval_T *rettv)
5283{
5284 rettv->vval.v_number = win_id2win(argvars);
5285}
5286
5287/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005288 * "getwinposx()" function
5289 */
5290 static void
5291f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5292{
5293 rettv->vval.v_number = -1;
5294#ifdef FEAT_GUI
5295 if (gui.in_use)
5296 {
5297 int x, y;
5298
5299 if (gui_mch_get_winpos(&x, &y) == OK)
5300 rettv->vval.v_number = x;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005301 return;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005302 }
5303#endif
5304#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5305 {
5306 int x, y;
5307
5308 if (term_get_winpos(&x, &y) == OK)
5309 rettv->vval.v_number = x;
5310 }
5311#endif
5312}
5313
5314/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005315 * "getwinposy()" function
5316 */
5317 static void
5318f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5319{
5320 rettv->vval.v_number = -1;
5321#ifdef FEAT_GUI
5322 if (gui.in_use)
5323 {
5324 int x, y;
5325
5326 if (gui_mch_get_winpos(&x, &y) == OK)
5327 rettv->vval.v_number = y;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005328 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005329 }
5330#endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005331#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5332 {
5333 int x, y;
5334
5335 if (term_get_winpos(&x, &y) == OK)
5336 rettv->vval.v_number = y;
5337 }
5338#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005339}
5340
5341/*
5342 * "getwinvar()" function
5343 */
5344 static void
5345f_getwinvar(typval_T *argvars, typval_T *rettv)
5346{
5347 getwinvar(argvars, rettv, 0);
5348}
5349
5350/*
5351 * "glob()" function
5352 */
5353 static void
5354f_glob(typval_T *argvars, typval_T *rettv)
5355{
5356 int options = WILD_SILENT|WILD_USE_NL;
5357 expand_T xpc;
5358 int error = FALSE;
5359
5360 /* When the optional second argument is non-zero, don't remove matches
5361 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5362 rettv->v_type = VAR_STRING;
5363 if (argvars[1].v_type != VAR_UNKNOWN)
5364 {
5365 if (get_tv_number_chk(&argvars[1], &error))
5366 options |= WILD_KEEP_ALL;
5367 if (argvars[2].v_type != VAR_UNKNOWN)
5368 {
5369 if (get_tv_number_chk(&argvars[2], &error))
5370 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005371 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005372 }
5373 if (argvars[3].v_type != VAR_UNKNOWN
5374 && get_tv_number_chk(&argvars[3], &error))
5375 options |= WILD_ALLLINKS;
5376 }
5377 }
5378 if (!error)
5379 {
5380 ExpandInit(&xpc);
5381 xpc.xp_context = EXPAND_FILES;
5382 if (p_wic)
5383 options += WILD_ICASE;
5384 if (rettv->v_type == VAR_STRING)
5385 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
5386 NULL, options, WILD_ALL);
5387 else if (rettv_list_alloc(rettv) != FAIL)
5388 {
5389 int i;
5390
5391 ExpandOne(&xpc, get_tv_string(&argvars[0]),
5392 NULL, options, WILD_ALL_KEEP);
5393 for (i = 0; i < xpc.xp_numfiles; i++)
5394 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5395
5396 ExpandCleanup(&xpc);
5397 }
5398 }
5399 else
5400 rettv->vval.v_string = NULL;
5401}
5402
5403/*
5404 * "globpath()" function
5405 */
5406 static void
5407f_globpath(typval_T *argvars, typval_T *rettv)
5408{
5409 int flags = 0;
5410 char_u buf1[NUMBUFLEN];
5411 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
5412 int error = FALSE;
5413 garray_T ga;
5414 int i;
5415
5416 /* When the optional second argument is non-zero, don't remove matches
5417 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5418 rettv->v_type = VAR_STRING;
5419 if (argvars[2].v_type != VAR_UNKNOWN)
5420 {
5421 if (get_tv_number_chk(&argvars[2], &error))
5422 flags |= WILD_KEEP_ALL;
5423 if (argvars[3].v_type != VAR_UNKNOWN)
5424 {
5425 if (get_tv_number_chk(&argvars[3], &error))
5426 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005427 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005428 }
5429 if (argvars[4].v_type != VAR_UNKNOWN
5430 && get_tv_number_chk(&argvars[4], &error))
5431 flags |= WILD_ALLLINKS;
5432 }
5433 }
5434 if (file != NULL && !error)
5435 {
5436 ga_init2(&ga, (int)sizeof(char_u *), 10);
5437 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
5438 if (rettv->v_type == VAR_STRING)
5439 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
5440 else if (rettv_list_alloc(rettv) != FAIL)
5441 for (i = 0; i < ga.ga_len; ++i)
5442 list_append_string(rettv->vval.v_list,
5443 ((char_u **)(ga.ga_data))[i], -1);
5444 ga_clear_strings(&ga);
5445 }
5446 else
5447 rettv->vval.v_string = NULL;
5448}
5449
5450/*
5451 * "glob2regpat()" function
5452 */
5453 static void
5454f_glob2regpat(typval_T *argvars, typval_T *rettv)
5455{
5456 char_u *pat = get_tv_string_chk(&argvars[0]);
5457
5458 rettv->v_type = VAR_STRING;
5459 rettv->vval.v_string = (pat == NULL)
5460 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
5461}
5462
5463/* for VIM_VERSION_ defines */
5464#include "version.h"
5465
5466/*
5467 * "has()" function
5468 */
5469 static void
5470f_has(typval_T *argvars, typval_T *rettv)
5471{
5472 int i;
5473 char_u *name;
5474 int n = FALSE;
5475 static char *(has_list[]) =
5476 {
5477#ifdef AMIGA
5478 "amiga",
5479# ifdef FEAT_ARP
5480 "arp",
5481# endif
5482#endif
5483#ifdef __BEOS__
5484 "beos",
5485#endif
5486#ifdef MACOS
5487 "mac",
5488#endif
5489#if defined(MACOS_X_UNIX)
5490 "macunix", /* built with 'darwin' enabled */
5491#endif
5492#if defined(__APPLE__) && __APPLE__ == 1
5493 "osx", /* built with or without 'darwin' enabled */
5494#endif
5495#ifdef __QNX__
5496 "qnx",
5497#endif
5498#ifdef UNIX
5499 "unix",
5500#endif
5501#ifdef VMS
5502 "vms",
5503#endif
5504#ifdef WIN32
5505 "win32",
5506#endif
5507#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
5508 "win32unix",
5509#endif
5510#if defined(WIN64) || defined(_WIN64)
5511 "win64",
5512#endif
5513#ifdef EBCDIC
5514 "ebcdic",
5515#endif
5516#ifndef CASE_INSENSITIVE_FILENAME
5517 "fname_case",
5518#endif
5519#ifdef HAVE_ACL
5520 "acl",
5521#endif
5522#ifdef FEAT_ARABIC
5523 "arabic",
5524#endif
5525#ifdef FEAT_AUTOCMD
5526 "autocmd",
5527#endif
5528#ifdef FEAT_BEVAL
5529 "balloon_eval",
5530# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
5531 "balloon_multiline",
5532# endif
5533#endif
5534#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
5535 "builtin_terms",
5536# ifdef ALL_BUILTIN_TCAPS
5537 "all_builtin_terms",
5538# endif
5539#endif
5540#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
5541 || defined(FEAT_GUI_W32) \
5542 || defined(FEAT_GUI_MOTIF))
5543 "browsefilter",
5544#endif
5545#ifdef FEAT_BYTEOFF
5546 "byte_offset",
5547#endif
5548#ifdef FEAT_JOB_CHANNEL
5549 "channel",
5550#endif
5551#ifdef FEAT_CINDENT
5552 "cindent",
5553#endif
5554#ifdef FEAT_CLIENTSERVER
5555 "clientserver",
5556#endif
5557#ifdef FEAT_CLIPBOARD
5558 "clipboard",
5559#endif
5560#ifdef FEAT_CMDL_COMPL
5561 "cmdline_compl",
5562#endif
5563#ifdef FEAT_CMDHIST
5564 "cmdline_hist",
5565#endif
5566#ifdef FEAT_COMMENTS
5567 "comments",
5568#endif
5569#ifdef FEAT_CONCEAL
5570 "conceal",
5571#endif
5572#ifdef FEAT_CRYPT
5573 "cryptv",
5574 "crypt-blowfish",
5575 "crypt-blowfish2",
5576#endif
5577#ifdef FEAT_CSCOPE
5578 "cscope",
5579#endif
5580#ifdef FEAT_CURSORBIND
5581 "cursorbind",
5582#endif
5583#ifdef CURSOR_SHAPE
5584 "cursorshape",
5585#endif
5586#ifdef DEBUG
5587 "debug",
5588#endif
5589#ifdef FEAT_CON_DIALOG
5590 "dialog_con",
5591#endif
5592#ifdef FEAT_GUI_DIALOG
5593 "dialog_gui",
5594#endif
5595#ifdef FEAT_DIFF
5596 "diff",
5597#endif
5598#ifdef FEAT_DIGRAPHS
5599 "digraphs",
5600#endif
5601#ifdef FEAT_DIRECTX
5602 "directx",
5603#endif
5604#ifdef FEAT_DND
5605 "dnd",
5606#endif
5607#ifdef FEAT_EMACS_TAGS
5608 "emacs_tags",
5609#endif
5610 "eval", /* always present, of course! */
5611 "ex_extra", /* graduated feature */
5612#ifdef FEAT_SEARCH_EXTRA
5613 "extra_search",
5614#endif
5615#ifdef FEAT_FKMAP
5616 "farsi",
5617#endif
5618#ifdef FEAT_SEARCHPATH
5619 "file_in_path",
5620#endif
5621#ifdef FEAT_FILTERPIPE
5622 "filterpipe",
5623#endif
5624#ifdef FEAT_FIND_ID
5625 "find_in_path",
5626#endif
5627#ifdef FEAT_FLOAT
5628 "float",
5629#endif
5630#ifdef FEAT_FOLDING
5631 "folding",
5632#endif
5633#ifdef FEAT_FOOTER
5634 "footer",
5635#endif
5636#if !defined(USE_SYSTEM) && defined(UNIX)
5637 "fork",
5638#endif
5639#ifdef FEAT_GETTEXT
5640 "gettext",
5641#endif
5642#ifdef FEAT_GUI
5643 "gui",
5644#endif
5645#ifdef FEAT_GUI_ATHENA
5646# ifdef FEAT_GUI_NEXTAW
5647 "gui_neXtaw",
5648# else
5649 "gui_athena",
5650# endif
5651#endif
5652#ifdef FEAT_GUI_GTK
5653 "gui_gtk",
5654# ifdef USE_GTK3
5655 "gui_gtk3",
5656# else
5657 "gui_gtk2",
5658# endif
5659#endif
5660#ifdef FEAT_GUI_GNOME
5661 "gui_gnome",
5662#endif
5663#ifdef FEAT_GUI_MAC
5664 "gui_mac",
5665#endif
5666#ifdef FEAT_GUI_MOTIF
5667 "gui_motif",
5668#endif
5669#ifdef FEAT_GUI_PHOTON
5670 "gui_photon",
5671#endif
5672#ifdef FEAT_GUI_W32
5673 "gui_win32",
5674#endif
5675#ifdef FEAT_HANGULIN
5676 "hangul_input",
5677#endif
5678#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
5679 "iconv",
5680#endif
5681#ifdef FEAT_INS_EXPAND
5682 "insert_expand",
5683#endif
5684#ifdef FEAT_JOB_CHANNEL
5685 "job",
5686#endif
5687#ifdef FEAT_JUMPLIST
5688 "jumplist",
5689#endif
5690#ifdef FEAT_KEYMAP
5691 "keymap",
5692#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02005693 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005694#ifdef FEAT_LANGMAP
5695 "langmap",
5696#endif
5697#ifdef FEAT_LIBCALL
5698 "libcall",
5699#endif
5700#ifdef FEAT_LINEBREAK
5701 "linebreak",
5702#endif
5703#ifdef FEAT_LISP
5704 "lispindent",
5705#endif
5706#ifdef FEAT_LISTCMDS
5707 "listcmds",
5708#endif
5709#ifdef FEAT_LOCALMAP
5710 "localmap",
5711#endif
5712#ifdef FEAT_LUA
5713# ifndef DYNAMIC_LUA
5714 "lua",
5715# endif
5716#endif
5717#ifdef FEAT_MENU
5718 "menu",
5719#endif
5720#ifdef FEAT_SESSION
5721 "mksession",
5722#endif
5723#ifdef FEAT_MODIFY_FNAME
5724 "modify_fname",
5725#endif
5726#ifdef FEAT_MOUSE
5727 "mouse",
5728#endif
5729#ifdef FEAT_MOUSESHAPE
5730 "mouseshape",
5731#endif
5732#if defined(UNIX) || defined(VMS)
5733# ifdef FEAT_MOUSE_DEC
5734 "mouse_dec",
5735# endif
5736# ifdef FEAT_MOUSE_GPM
5737 "mouse_gpm",
5738# endif
5739# ifdef FEAT_MOUSE_JSB
5740 "mouse_jsbterm",
5741# endif
5742# ifdef FEAT_MOUSE_NET
5743 "mouse_netterm",
5744# endif
5745# ifdef FEAT_MOUSE_PTERM
5746 "mouse_pterm",
5747# endif
5748# ifdef FEAT_MOUSE_SGR
5749 "mouse_sgr",
5750# endif
5751# ifdef FEAT_SYSMOUSE
5752 "mouse_sysmouse",
5753# endif
5754# ifdef FEAT_MOUSE_URXVT
5755 "mouse_urxvt",
5756# endif
5757# ifdef FEAT_MOUSE_XTERM
5758 "mouse_xterm",
5759# endif
5760#endif
5761#ifdef FEAT_MBYTE
5762 "multi_byte",
5763#endif
5764#ifdef FEAT_MBYTE_IME
5765 "multi_byte_ime",
5766#endif
5767#ifdef FEAT_MULTI_LANG
5768 "multi_lang",
5769#endif
5770#ifdef FEAT_MZSCHEME
5771#ifndef DYNAMIC_MZSCHEME
5772 "mzscheme",
5773#endif
5774#endif
5775#ifdef FEAT_NUM64
5776 "num64",
5777#endif
5778#ifdef FEAT_OLE
5779 "ole",
5780#endif
5781 "packages",
5782#ifdef FEAT_PATH_EXTRA
5783 "path_extra",
5784#endif
5785#ifdef FEAT_PERL
5786#ifndef DYNAMIC_PERL
5787 "perl",
5788#endif
5789#endif
5790#ifdef FEAT_PERSISTENT_UNDO
5791 "persistent_undo",
5792#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005793#if defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005794 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005795 "pythonx",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005796#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005797#if defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005798 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005799 "pythonx",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005800#endif
5801#ifdef FEAT_POSTSCRIPT
5802 "postscript",
5803#endif
5804#ifdef FEAT_PRINTER
5805 "printer",
5806#endif
5807#ifdef FEAT_PROFILE
5808 "profile",
5809#endif
5810#ifdef FEAT_RELTIME
5811 "reltime",
5812#endif
5813#ifdef FEAT_QUICKFIX
5814 "quickfix",
5815#endif
5816#ifdef FEAT_RIGHTLEFT
5817 "rightleft",
5818#endif
5819#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
5820 "ruby",
5821#endif
5822#ifdef FEAT_SCROLLBIND
5823 "scrollbind",
5824#endif
5825#ifdef FEAT_CMDL_INFO
5826 "showcmd",
5827 "cmdline_info",
5828#endif
5829#ifdef FEAT_SIGNS
5830 "signs",
5831#endif
5832#ifdef FEAT_SMARTINDENT
5833 "smartindent",
5834#endif
5835#ifdef STARTUPTIME
5836 "startuptime",
5837#endif
5838#ifdef FEAT_STL_OPT
5839 "statusline",
5840#endif
5841#ifdef FEAT_SUN_WORKSHOP
5842 "sun_workshop",
5843#endif
5844#ifdef FEAT_NETBEANS_INTG
5845 "netbeans_intg",
5846#endif
5847#ifdef FEAT_SPELL
5848 "spell",
5849#endif
5850#ifdef FEAT_SYN_HL
5851 "syntax",
5852#endif
5853#if defined(USE_SYSTEM) || !defined(UNIX)
5854 "system",
5855#endif
5856#ifdef FEAT_TAG_BINS
5857 "tag_binary",
5858#endif
5859#ifdef FEAT_TAG_OLDSTATIC
5860 "tag_old_static",
5861#endif
5862#ifdef FEAT_TAG_ANYWHITE
5863 "tag_any_white",
5864#endif
5865#ifdef FEAT_TCL
5866# ifndef DYNAMIC_TCL
5867 "tcl",
5868# endif
5869#endif
5870#ifdef FEAT_TERMGUICOLORS
5871 "termguicolors",
5872#endif
5873#ifdef TERMINFO
5874 "terminfo",
5875#endif
5876#ifdef FEAT_TERMRESPONSE
5877 "termresponse",
5878#endif
5879#ifdef FEAT_TEXTOBJ
5880 "textobjects",
5881#endif
5882#ifdef HAVE_TGETENT
5883 "tgetent",
5884#endif
5885#ifdef FEAT_TIMERS
5886 "timers",
5887#endif
5888#ifdef FEAT_TITLE
5889 "title",
5890#endif
5891#ifdef FEAT_TOOLBAR
5892 "toolbar",
5893#endif
5894#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
5895 "unnamedplus",
5896#endif
5897#ifdef FEAT_USR_CMDS
5898 "user-commands", /* was accidentally included in 5.4 */
5899 "user_commands",
5900#endif
5901#ifdef FEAT_VIMINFO
5902 "viminfo",
5903#endif
5904#ifdef FEAT_WINDOWS
5905 "vertsplit",
5906#endif
5907#ifdef FEAT_VIRTUALEDIT
5908 "virtualedit",
5909#endif
5910 "visual",
5911#ifdef FEAT_VISUALEXTRA
5912 "visualextra",
5913#endif
5914#ifdef FEAT_VREPLACE
5915 "vreplace",
5916#endif
5917#ifdef FEAT_WILDIGN
5918 "wildignore",
5919#endif
5920#ifdef FEAT_WILDMENU
5921 "wildmenu",
5922#endif
5923#ifdef FEAT_WINDOWS
5924 "windows",
5925#endif
5926#ifdef FEAT_WAK
5927 "winaltkeys",
5928#endif
5929#ifdef FEAT_WRITEBACKUP
5930 "writebackup",
5931#endif
5932#ifdef FEAT_XIM
5933 "xim",
5934#endif
5935#ifdef FEAT_XFONTSET
5936 "xfontset",
5937#endif
5938#ifdef FEAT_XPM_W32
5939 "xpm",
5940 "xpm_w32", /* for backward compatibility */
5941#else
5942# if defined(HAVE_XPM)
5943 "xpm",
5944# endif
5945#endif
5946#ifdef USE_XSMP
5947 "xsmp",
5948#endif
5949#ifdef USE_XSMP_INTERACT
5950 "xsmp_interact",
5951#endif
5952#ifdef FEAT_XCLIPBOARD
5953 "xterm_clipboard",
5954#endif
5955#ifdef FEAT_XTERM_SAVE
5956 "xterm_save",
5957#endif
5958#if defined(UNIX) && defined(FEAT_X11)
5959 "X11",
5960#endif
5961 NULL
5962 };
5963
5964 name = get_tv_string(&argvars[0]);
5965 for (i = 0; has_list[i] != NULL; ++i)
5966 if (STRICMP(name, has_list[i]) == 0)
5967 {
5968 n = TRUE;
5969 break;
5970 }
5971
5972 if (n == FALSE)
5973 {
5974 if (STRNICMP(name, "patch", 5) == 0)
5975 {
5976 if (name[5] == '-'
5977 && STRLEN(name) >= 11
5978 && vim_isdigit(name[6])
5979 && vim_isdigit(name[8])
5980 && vim_isdigit(name[10]))
5981 {
5982 int major = atoi((char *)name + 6);
5983 int minor = atoi((char *)name + 8);
5984
5985 /* Expect "patch-9.9.01234". */
5986 n = (major < VIM_VERSION_MAJOR
5987 || (major == VIM_VERSION_MAJOR
5988 && (minor < VIM_VERSION_MINOR
5989 || (minor == VIM_VERSION_MINOR
5990 && has_patch(atoi((char *)name + 10))))));
5991 }
5992 else
5993 n = has_patch(atoi((char *)name + 5));
5994 }
5995 else if (STRICMP(name, "vim_starting") == 0)
5996 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01005997 else if (STRICMP(name, "ttyin") == 0)
5998 n = mch_input_isatty();
5999 else if (STRICMP(name, "ttyout") == 0)
6000 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006001#ifdef FEAT_MBYTE
6002 else if (STRICMP(name, "multi_byte_encoding") == 0)
6003 n = has_mbyte;
6004#endif
6005#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
6006 else if (STRICMP(name, "balloon_multiline") == 0)
6007 n = multiline_balloon_available();
6008#endif
6009#ifdef DYNAMIC_TCL
6010 else if (STRICMP(name, "tcl") == 0)
6011 n = tcl_enabled(FALSE);
6012#endif
6013#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6014 else if (STRICMP(name, "iconv") == 0)
6015 n = iconv_enabled(FALSE);
6016#endif
6017#ifdef DYNAMIC_LUA
6018 else if (STRICMP(name, "lua") == 0)
6019 n = lua_enabled(FALSE);
6020#endif
6021#ifdef DYNAMIC_MZSCHEME
6022 else if (STRICMP(name, "mzscheme") == 0)
6023 n = mzscheme_enabled(FALSE);
6024#endif
6025#ifdef DYNAMIC_RUBY
6026 else if (STRICMP(name, "ruby") == 0)
6027 n = ruby_enabled(FALSE);
6028#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006029#ifdef DYNAMIC_PYTHON
6030 else if (STRICMP(name, "python") == 0)
6031 n = python_enabled(FALSE);
6032#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006033#ifdef DYNAMIC_PYTHON3
6034 else if (STRICMP(name, "python3") == 0)
6035 n = python3_enabled(FALSE);
6036#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006037#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6038 else if (STRICMP(name, "pythonx") == 0)
6039 {
6040# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6041 if (p_pyx == 0)
6042 n = python3_enabled(FALSE) || python_enabled(FALSE);
6043 else if (p_pyx == 3)
6044 n = python3_enabled(FALSE);
6045 else if (p_pyx == 2)
6046 n = python_enabled(FALSE);
6047# elif defined(DYNAMIC_PYTHON)
6048 n = python_enabled(FALSE);
6049# elif defined(DYNAMIC_PYTHON3)
6050 n = python3_enabled(FALSE);
6051# endif
6052 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006053#endif
6054#ifdef DYNAMIC_PERL
6055 else if (STRICMP(name, "perl") == 0)
6056 n = perl_enabled(FALSE);
6057#endif
6058#ifdef FEAT_GUI
6059 else if (STRICMP(name, "gui_running") == 0)
6060 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006061# ifdef FEAT_BROWSE
6062 else if (STRICMP(name, "browse") == 0)
6063 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6064# endif
6065#endif
6066#ifdef FEAT_SYN_HL
6067 else if (STRICMP(name, "syntax_items") == 0)
6068 n = syntax_present(curwin);
6069#endif
6070#if defined(WIN3264)
6071 else if (STRICMP(name, "win95") == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006072 n = FALSE; /* Win9x is no more supported. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006073#endif
6074#ifdef FEAT_NETBEANS_INTG
6075 else if (STRICMP(name, "netbeans_enabled") == 0)
6076 n = netbeans_active();
6077#endif
6078 }
6079
6080 rettv->vval.v_number = n;
6081}
6082
6083/*
6084 * "has_key()" function
6085 */
6086 static void
6087f_has_key(typval_T *argvars, typval_T *rettv)
6088{
6089 if (argvars[0].v_type != VAR_DICT)
6090 {
6091 EMSG(_(e_dictreq));
6092 return;
6093 }
6094 if (argvars[0].vval.v_dict == NULL)
6095 return;
6096
6097 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
6098 get_tv_string(&argvars[1]), -1) != NULL;
6099}
6100
6101/*
6102 * "haslocaldir()" function
6103 */
6104 static void
6105f_haslocaldir(typval_T *argvars, typval_T *rettv)
6106{
6107 win_T *wp = NULL;
6108
6109 wp = find_tabwin(&argvars[0], &argvars[1]);
6110 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6111}
6112
6113/*
6114 * "hasmapto()" function
6115 */
6116 static void
6117f_hasmapto(typval_T *argvars, typval_T *rettv)
6118{
6119 char_u *name;
6120 char_u *mode;
6121 char_u buf[NUMBUFLEN];
6122 int abbr = FALSE;
6123
6124 name = get_tv_string(&argvars[0]);
6125 if (argvars[1].v_type == VAR_UNKNOWN)
6126 mode = (char_u *)"nvo";
6127 else
6128 {
6129 mode = get_tv_string_buf(&argvars[1], buf);
6130 if (argvars[2].v_type != VAR_UNKNOWN)
6131 abbr = (int)get_tv_number(&argvars[2]);
6132 }
6133
6134 if (map_to_exists(name, mode, abbr))
6135 rettv->vval.v_number = TRUE;
6136 else
6137 rettv->vval.v_number = FALSE;
6138}
6139
6140/*
6141 * "histadd()" function
6142 */
6143 static void
6144f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6145{
6146#ifdef FEAT_CMDHIST
6147 int histype;
6148 char_u *str;
6149 char_u buf[NUMBUFLEN];
6150#endif
6151
6152 rettv->vval.v_number = FALSE;
6153 if (check_restricted() || check_secure())
6154 return;
6155#ifdef FEAT_CMDHIST
6156 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6157 histype = str != NULL ? get_histtype(str) : -1;
6158 if (histype >= 0)
6159 {
6160 str = get_tv_string_buf(&argvars[1], buf);
6161 if (*str != NUL)
6162 {
6163 init_history();
6164 add_to_history(histype, str, FALSE, NUL);
6165 rettv->vval.v_number = TRUE;
6166 return;
6167 }
6168 }
6169#endif
6170}
6171
6172/*
6173 * "histdel()" function
6174 */
6175 static void
6176f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6177{
6178#ifdef FEAT_CMDHIST
6179 int n;
6180 char_u buf[NUMBUFLEN];
6181 char_u *str;
6182
6183 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6184 if (str == NULL)
6185 n = 0;
6186 else if (argvars[1].v_type == VAR_UNKNOWN)
6187 /* only one argument: clear entire history */
6188 n = clr_history(get_histtype(str));
6189 else if (argvars[1].v_type == VAR_NUMBER)
6190 /* index given: remove that entry */
6191 n = del_history_idx(get_histtype(str),
6192 (int)get_tv_number(&argvars[1]));
6193 else
6194 /* string given: remove all matching entries */
6195 n = del_history_entry(get_histtype(str),
6196 get_tv_string_buf(&argvars[1], buf));
6197 rettv->vval.v_number = n;
6198#endif
6199}
6200
6201/*
6202 * "histget()" function
6203 */
6204 static void
6205f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6206{
6207#ifdef FEAT_CMDHIST
6208 int type;
6209 int idx;
6210 char_u *str;
6211
6212 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6213 if (str == NULL)
6214 rettv->vval.v_string = NULL;
6215 else
6216 {
6217 type = get_histtype(str);
6218 if (argvars[1].v_type == VAR_UNKNOWN)
6219 idx = get_history_idx(type);
6220 else
6221 idx = (int)get_tv_number_chk(&argvars[1], NULL);
6222 /* -1 on type error */
6223 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6224 }
6225#else
6226 rettv->vval.v_string = NULL;
6227#endif
6228 rettv->v_type = VAR_STRING;
6229}
6230
6231/*
6232 * "histnr()" function
6233 */
6234 static void
6235f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6236{
6237 int i;
6238
6239#ifdef FEAT_CMDHIST
6240 char_u *history = get_tv_string_chk(&argvars[0]);
6241
6242 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6243 if (i >= HIST_CMD && i < HIST_COUNT)
6244 i = get_history_idx(i);
6245 else
6246#endif
6247 i = -1;
6248 rettv->vval.v_number = i;
6249}
6250
6251/*
6252 * "highlightID(name)" function
6253 */
6254 static void
6255f_hlID(typval_T *argvars, typval_T *rettv)
6256{
6257 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
6258}
6259
6260/*
6261 * "highlight_exists()" function
6262 */
6263 static void
6264f_hlexists(typval_T *argvars, typval_T *rettv)
6265{
6266 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
6267}
6268
6269/*
6270 * "hostname()" function
6271 */
6272 static void
6273f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6274{
6275 char_u hostname[256];
6276
6277 mch_get_host_name(hostname, 256);
6278 rettv->v_type = VAR_STRING;
6279 rettv->vval.v_string = vim_strsave(hostname);
6280}
6281
6282/*
6283 * iconv() function
6284 */
6285 static void
6286f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6287{
6288#ifdef FEAT_MBYTE
6289 char_u buf1[NUMBUFLEN];
6290 char_u buf2[NUMBUFLEN];
6291 char_u *from, *to, *str;
6292 vimconv_T vimconv;
6293#endif
6294
6295 rettv->v_type = VAR_STRING;
6296 rettv->vval.v_string = NULL;
6297
6298#ifdef FEAT_MBYTE
6299 str = get_tv_string(&argvars[0]);
6300 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
6301 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
6302 vimconv.vc_type = CONV_NONE;
6303 convert_setup(&vimconv, from, to);
6304
6305 /* If the encodings are equal, no conversion needed. */
6306 if (vimconv.vc_type == CONV_NONE)
6307 rettv->vval.v_string = vim_strsave(str);
6308 else
6309 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6310
6311 convert_setup(&vimconv, NULL, NULL);
6312 vim_free(from);
6313 vim_free(to);
6314#endif
6315}
6316
6317/*
6318 * "indent()" function
6319 */
6320 static void
6321f_indent(typval_T *argvars, typval_T *rettv)
6322{
6323 linenr_T lnum;
6324
6325 lnum = get_tv_lnum(argvars);
6326 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6327 rettv->vval.v_number = get_indent_lnum(lnum);
6328 else
6329 rettv->vval.v_number = -1;
6330}
6331
6332/*
6333 * "index()" function
6334 */
6335 static void
6336f_index(typval_T *argvars, typval_T *rettv)
6337{
6338 list_T *l;
6339 listitem_T *item;
6340 long idx = 0;
6341 int ic = FALSE;
6342
6343 rettv->vval.v_number = -1;
6344 if (argvars[0].v_type != VAR_LIST)
6345 {
6346 EMSG(_(e_listreq));
6347 return;
6348 }
6349 l = argvars[0].vval.v_list;
6350 if (l != NULL)
6351 {
6352 item = l->lv_first;
6353 if (argvars[2].v_type != VAR_UNKNOWN)
6354 {
6355 int error = FALSE;
6356
6357 /* Start at specified item. Use the cached index that list_find()
6358 * sets, so that a negative number also works. */
6359 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
6360 idx = l->lv_idx;
6361 if (argvars[3].v_type != VAR_UNKNOWN)
6362 ic = (int)get_tv_number_chk(&argvars[3], &error);
6363 if (error)
6364 item = NULL;
6365 }
6366
6367 for ( ; item != NULL; item = item->li_next, ++idx)
6368 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6369 {
6370 rettv->vval.v_number = idx;
6371 break;
6372 }
6373 }
6374}
6375
6376static int inputsecret_flag = 0;
6377
6378/*
6379 * "input()" function
6380 * Also handles inputsecret() when inputsecret is set.
6381 */
6382 static void
6383f_input(typval_T *argvars, typval_T *rettv)
6384{
6385 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6386}
6387
6388/*
6389 * "inputdialog()" function
6390 */
6391 static void
6392f_inputdialog(typval_T *argvars, typval_T *rettv)
6393{
6394#if defined(FEAT_GUI_TEXTDIALOG)
6395 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
6396 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6397 {
6398 char_u *message;
6399 char_u buf[NUMBUFLEN];
6400 char_u *defstr = (char_u *)"";
6401
6402 message = get_tv_string_chk(&argvars[0]);
6403 if (argvars[1].v_type != VAR_UNKNOWN
6404 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
6405 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6406 else
6407 IObuff[0] = NUL;
6408 if (message != NULL && defstr != NULL
6409 && do_dialog(VIM_QUESTION, NULL, message,
6410 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6411 rettv->vval.v_string = vim_strsave(IObuff);
6412 else
6413 {
6414 if (message != NULL && defstr != NULL
6415 && argvars[1].v_type != VAR_UNKNOWN
6416 && argvars[2].v_type != VAR_UNKNOWN)
6417 rettv->vval.v_string = vim_strsave(
6418 get_tv_string_buf(&argvars[2], buf));
6419 else
6420 rettv->vval.v_string = NULL;
6421 }
6422 rettv->v_type = VAR_STRING;
6423 }
6424 else
6425#endif
6426 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6427}
6428
6429/*
6430 * "inputlist()" function
6431 */
6432 static void
6433f_inputlist(typval_T *argvars, typval_T *rettv)
6434{
6435 listitem_T *li;
6436 int selected;
6437 int mouse_used;
6438
6439#ifdef NO_CONSOLE_INPUT
6440 /* While starting up, there is no place to enter text. */
6441 if (no_console_input())
6442 return;
6443#endif
6444 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6445 {
6446 EMSG2(_(e_listarg), "inputlist()");
6447 return;
6448 }
6449
6450 msg_start();
6451 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
6452 lines_left = Rows; /* avoid more prompt */
6453 msg_scroll = TRUE;
6454 msg_clr_eos();
6455
6456 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
6457 {
6458 msg_puts(get_tv_string(&li->li_tv));
6459 msg_putchar('\n');
6460 }
6461
6462 /* Ask for choice. */
6463 selected = prompt_for_number(&mouse_used);
6464 if (mouse_used)
6465 selected -= lines_left;
6466
6467 rettv->vval.v_number = selected;
6468}
6469
6470
6471static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6472
6473/*
6474 * "inputrestore()" function
6475 */
6476 static void
6477f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6478{
6479 if (ga_userinput.ga_len > 0)
6480 {
6481 --ga_userinput.ga_len;
6482 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
6483 + ga_userinput.ga_len);
6484 /* default return is zero == OK */
6485 }
6486 else if (p_verbose > 1)
6487 {
6488 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
6489 rettv->vval.v_number = 1; /* Failed */
6490 }
6491}
6492
6493/*
6494 * "inputsave()" function
6495 */
6496 static void
6497f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
6498{
6499 /* Add an entry to the stack of typeahead storage. */
6500 if (ga_grow(&ga_userinput, 1) == OK)
6501 {
6502 save_typeahead((tasave_T *)(ga_userinput.ga_data)
6503 + ga_userinput.ga_len);
6504 ++ga_userinput.ga_len;
6505 /* default return is zero == OK */
6506 }
6507 else
6508 rettv->vval.v_number = 1; /* Failed */
6509}
6510
6511/*
6512 * "inputsecret()" function
6513 */
6514 static void
6515f_inputsecret(typval_T *argvars, typval_T *rettv)
6516{
6517 ++cmdline_star;
6518 ++inputsecret_flag;
6519 f_input(argvars, rettv);
6520 --cmdline_star;
6521 --inputsecret_flag;
6522}
6523
6524/*
6525 * "insert()" function
6526 */
6527 static void
6528f_insert(typval_T *argvars, typval_T *rettv)
6529{
6530 long before = 0;
6531 listitem_T *item;
6532 list_T *l;
6533 int error = FALSE;
6534
6535 if (argvars[0].v_type != VAR_LIST)
6536 EMSG2(_(e_listarg), "insert()");
6537 else if ((l = argvars[0].vval.v_list) != NULL
6538 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
6539 {
6540 if (argvars[2].v_type != VAR_UNKNOWN)
6541 before = (long)get_tv_number_chk(&argvars[2], &error);
6542 if (error)
6543 return; /* type error; errmsg already given */
6544
6545 if (before == l->lv_len)
6546 item = NULL;
6547 else
6548 {
6549 item = list_find(l, before);
6550 if (item == NULL)
6551 {
6552 EMSGN(_(e_listidx), before);
6553 l = NULL;
6554 }
6555 }
6556 if (l != NULL)
6557 {
6558 list_insert_tv(l, &argvars[1], item);
6559 copy_tv(&argvars[0], rettv);
6560 }
6561 }
6562}
6563
6564/*
6565 * "invert(expr)" function
6566 */
6567 static void
6568f_invert(typval_T *argvars, typval_T *rettv)
6569{
6570 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
6571}
6572
6573/*
6574 * "isdirectory()" function
6575 */
6576 static void
6577f_isdirectory(typval_T *argvars, typval_T *rettv)
6578{
6579 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
6580}
6581
6582/*
6583 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
6584 * or it refers to a List or Dictionary that is locked.
6585 */
6586 static int
6587tv_islocked(typval_T *tv)
6588{
6589 return (tv->v_lock & VAR_LOCKED)
6590 || (tv->v_type == VAR_LIST
6591 && tv->vval.v_list != NULL
6592 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
6593 || (tv->v_type == VAR_DICT
6594 && tv->vval.v_dict != NULL
6595 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
6596}
6597
6598/*
6599 * "islocked()" function
6600 */
6601 static void
6602f_islocked(typval_T *argvars, typval_T *rettv)
6603{
6604 lval_T lv;
6605 char_u *end;
6606 dictitem_T *di;
6607
6608 rettv->vval.v_number = -1;
6609 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01006610 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006611 if (end != NULL && lv.ll_name != NULL)
6612 {
6613 if (*end != NUL)
6614 EMSG(_(e_trailing));
6615 else
6616 {
6617 if (lv.ll_tv == NULL)
6618 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006619 di = find_var(lv.ll_name, NULL, TRUE);
6620 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006621 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006622 /* Consider a variable locked when:
6623 * 1. the variable itself is locked
6624 * 2. the value of the variable is locked.
6625 * 3. the List or Dict value is locked.
6626 */
6627 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
6628 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006629 }
6630 }
6631 else if (lv.ll_range)
6632 EMSG(_("E786: Range not allowed"));
6633 else if (lv.ll_newkey != NULL)
6634 EMSG2(_(e_dictkey), lv.ll_newkey);
6635 else if (lv.ll_list != NULL)
6636 /* List item. */
6637 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
6638 else
6639 /* Dictionary item. */
6640 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
6641 }
6642 }
6643
6644 clear_lval(&lv);
6645}
6646
6647#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
6648/*
6649 * "isnan()" function
6650 */
6651 static void
6652f_isnan(typval_T *argvars, typval_T *rettv)
6653{
6654 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
6655 && isnan(argvars[0].vval.v_float);
6656}
6657#endif
6658
6659/*
6660 * "items(dict)" function
6661 */
6662 static void
6663f_items(typval_T *argvars, typval_T *rettv)
6664{
6665 dict_list(argvars, rettv, 2);
6666}
6667
6668#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
6669/*
6670 * Get the job from the argument.
6671 * Returns NULL if the job is invalid.
6672 */
6673 static job_T *
6674get_job_arg(typval_T *tv)
6675{
6676 job_T *job;
6677
6678 if (tv->v_type != VAR_JOB)
6679 {
6680 EMSG2(_(e_invarg2), get_tv_string(tv));
6681 return NULL;
6682 }
6683 job = tv->vval.v_job;
6684
6685 if (job == NULL)
6686 EMSG(_("E916: not a valid job"));
6687 return job;
6688}
6689
6690/*
6691 * "job_getchannel()" function
6692 */
6693 static void
6694f_job_getchannel(typval_T *argvars, typval_T *rettv)
6695{
6696 job_T *job = get_job_arg(&argvars[0]);
6697
6698 if (job != NULL)
6699 {
6700 rettv->v_type = VAR_CHANNEL;
6701 rettv->vval.v_channel = job->jv_channel;
6702 if (job->jv_channel != NULL)
6703 ++job->jv_channel->ch_refcount;
6704 }
6705}
6706
6707/*
6708 * "job_info()" function
6709 */
6710 static void
6711f_job_info(typval_T *argvars, typval_T *rettv)
6712{
6713 job_T *job = get_job_arg(&argvars[0]);
6714
6715 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
6716 job_info(job, rettv->vval.v_dict);
6717}
6718
6719/*
6720 * "job_setoptions()" function
6721 */
6722 static void
6723f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
6724{
6725 job_T *job = get_job_arg(&argvars[0]);
6726 jobopt_T opt;
6727
6728 if (job == NULL)
6729 return;
6730 clear_job_options(&opt);
6731 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == OK)
6732 job_set_options(job, &opt);
6733 free_job_options(&opt);
6734}
6735
6736/*
6737 * "job_start()" function
6738 */
6739 static void
6740f_job_start(typval_T *argvars, typval_T *rettv)
6741{
6742 rettv->v_type = VAR_JOB;
6743 if (check_restricted() || check_secure())
6744 return;
6745 rettv->vval.v_job = job_start(argvars);
6746}
6747
6748/*
6749 * "job_status()" function
6750 */
6751 static void
6752f_job_status(typval_T *argvars, typval_T *rettv)
6753{
6754 job_T *job = get_job_arg(&argvars[0]);
6755
6756 if (job != NULL)
6757 {
6758 rettv->v_type = VAR_STRING;
6759 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
6760 }
6761}
6762
6763/*
6764 * "job_stop()" function
6765 */
6766 static void
6767f_job_stop(typval_T *argvars, typval_T *rettv)
6768{
6769 job_T *job = get_job_arg(&argvars[0]);
6770
6771 if (job != NULL)
6772 rettv->vval.v_number = job_stop(job, argvars);
6773}
6774#endif
6775
6776/*
6777 * "join()" function
6778 */
6779 static void
6780f_join(typval_T *argvars, typval_T *rettv)
6781{
6782 garray_T ga;
6783 char_u *sep;
6784
6785 if (argvars[0].v_type != VAR_LIST)
6786 {
6787 EMSG(_(e_listreq));
6788 return;
6789 }
6790 if (argvars[0].vval.v_list == NULL)
6791 return;
6792 if (argvars[1].v_type == VAR_UNKNOWN)
6793 sep = (char_u *)" ";
6794 else
6795 sep = get_tv_string_chk(&argvars[1]);
6796
6797 rettv->v_type = VAR_STRING;
6798
6799 if (sep != NULL)
6800 {
6801 ga_init2(&ga, (int)sizeof(char), 80);
6802 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
6803 ga_append(&ga, NUL);
6804 rettv->vval.v_string = (char_u *)ga.ga_data;
6805 }
6806 else
6807 rettv->vval.v_string = NULL;
6808}
6809
6810/*
6811 * "js_decode()" function
6812 */
6813 static void
6814f_js_decode(typval_T *argvars, typval_T *rettv)
6815{
6816 js_read_T reader;
6817
6818 reader.js_buf = get_tv_string(&argvars[0]);
6819 reader.js_fill = NULL;
6820 reader.js_used = 0;
6821 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
6822 EMSG(_(e_invarg));
6823}
6824
6825/*
6826 * "js_encode()" function
6827 */
6828 static void
6829f_js_encode(typval_T *argvars, typval_T *rettv)
6830{
6831 rettv->v_type = VAR_STRING;
6832 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
6833}
6834
6835/*
6836 * "json_decode()" function
6837 */
6838 static void
6839f_json_decode(typval_T *argvars, typval_T *rettv)
6840{
6841 js_read_T reader;
6842
6843 reader.js_buf = get_tv_string(&argvars[0]);
6844 reader.js_fill = NULL;
6845 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01006846 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006847}
6848
6849/*
6850 * "json_encode()" function
6851 */
6852 static void
6853f_json_encode(typval_T *argvars, typval_T *rettv)
6854{
6855 rettv->v_type = VAR_STRING;
6856 rettv->vval.v_string = json_encode(&argvars[0], 0);
6857}
6858
6859/*
6860 * "keys()" function
6861 */
6862 static void
6863f_keys(typval_T *argvars, typval_T *rettv)
6864{
6865 dict_list(argvars, rettv, 0);
6866}
6867
6868/*
6869 * "last_buffer_nr()" function.
6870 */
6871 static void
6872f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
6873{
6874 int n = 0;
6875 buf_T *buf;
6876
Bram Moolenaar29323592016-07-24 22:04:11 +02006877 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006878 if (n < buf->b_fnum)
6879 n = buf->b_fnum;
6880
6881 rettv->vval.v_number = n;
6882}
6883
6884/*
6885 * "len()" function
6886 */
6887 static void
6888f_len(typval_T *argvars, typval_T *rettv)
6889{
6890 switch (argvars[0].v_type)
6891 {
6892 case VAR_STRING:
6893 case VAR_NUMBER:
6894 rettv->vval.v_number = (varnumber_T)STRLEN(
6895 get_tv_string(&argvars[0]));
6896 break;
6897 case VAR_LIST:
6898 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
6899 break;
6900 case VAR_DICT:
6901 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
6902 break;
6903 case VAR_UNKNOWN:
6904 case VAR_SPECIAL:
6905 case VAR_FLOAT:
6906 case VAR_FUNC:
6907 case VAR_PARTIAL:
6908 case VAR_JOB:
6909 case VAR_CHANNEL:
6910 EMSG(_("E701: Invalid type for len()"));
6911 break;
6912 }
6913}
6914
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006915 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01006916libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006917{
6918#ifdef FEAT_LIBCALL
6919 char_u *string_in;
6920 char_u **string_result;
6921 int nr_result;
6922#endif
6923
6924 rettv->v_type = type;
6925 if (type != VAR_NUMBER)
6926 rettv->vval.v_string = NULL;
6927
6928 if (check_restricted() || check_secure())
6929 return;
6930
6931#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02006932 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006933 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
6934 {
6935 string_in = NULL;
6936 if (argvars[2].v_type == VAR_STRING)
6937 string_in = argvars[2].vval.v_string;
6938 if (type == VAR_NUMBER)
6939 string_result = NULL;
6940 else
6941 string_result = &rettv->vval.v_string;
6942 if (mch_libcall(argvars[0].vval.v_string,
6943 argvars[1].vval.v_string,
6944 string_in,
6945 argvars[2].vval.v_number,
6946 string_result,
6947 &nr_result) == OK
6948 && type == VAR_NUMBER)
6949 rettv->vval.v_number = nr_result;
6950 }
6951#endif
6952}
6953
6954/*
6955 * "libcall()" function
6956 */
6957 static void
6958f_libcall(typval_T *argvars, typval_T *rettv)
6959{
6960 libcall_common(argvars, rettv, VAR_STRING);
6961}
6962
6963/*
6964 * "libcallnr()" function
6965 */
6966 static void
6967f_libcallnr(typval_T *argvars, typval_T *rettv)
6968{
6969 libcall_common(argvars, rettv, VAR_NUMBER);
6970}
6971
6972/*
6973 * "line(string)" function
6974 */
6975 static void
6976f_line(typval_T *argvars, typval_T *rettv)
6977{
6978 linenr_T lnum = 0;
6979 pos_T *fp;
6980 int fnum;
6981
6982 fp = var2fpos(&argvars[0], TRUE, &fnum);
6983 if (fp != NULL)
6984 lnum = fp->lnum;
6985 rettv->vval.v_number = lnum;
6986}
6987
6988/*
6989 * "line2byte(lnum)" function
6990 */
6991 static void
6992f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
6993{
6994#ifndef FEAT_BYTEOFF
6995 rettv->vval.v_number = -1;
6996#else
6997 linenr_T lnum;
6998
6999 lnum = get_tv_lnum(argvars);
7000 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7001 rettv->vval.v_number = -1;
7002 else
7003 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7004 if (rettv->vval.v_number >= 0)
7005 ++rettv->vval.v_number;
7006#endif
7007}
7008
7009/*
7010 * "lispindent(lnum)" function
7011 */
7012 static void
7013f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7014{
7015#ifdef FEAT_LISP
7016 pos_T pos;
7017 linenr_T lnum;
7018
7019 pos = curwin->w_cursor;
7020 lnum = get_tv_lnum(argvars);
7021 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7022 {
7023 curwin->w_cursor.lnum = lnum;
7024 rettv->vval.v_number = get_lisp_indent();
7025 curwin->w_cursor = pos;
7026 }
7027 else
7028#endif
7029 rettv->vval.v_number = -1;
7030}
7031
7032/*
7033 * "localtime()" function
7034 */
7035 static void
7036f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7037{
7038 rettv->vval.v_number = (varnumber_T)time(NULL);
7039}
7040
7041static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
7042
7043 static void
7044get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7045{
7046 char_u *keys;
7047 char_u *which;
7048 char_u buf[NUMBUFLEN];
7049 char_u *keys_buf = NULL;
7050 char_u *rhs;
7051 int mode;
7052 int abbr = FALSE;
7053 int get_dict = FALSE;
7054 mapblock_T *mp;
7055 int buffer_local;
7056
7057 /* return empty string for failure */
7058 rettv->v_type = VAR_STRING;
7059 rettv->vval.v_string = NULL;
7060
7061 keys = get_tv_string(&argvars[0]);
7062 if (*keys == NUL)
7063 return;
7064
7065 if (argvars[1].v_type != VAR_UNKNOWN)
7066 {
7067 which = get_tv_string_buf_chk(&argvars[1], buf);
7068 if (argvars[2].v_type != VAR_UNKNOWN)
7069 {
7070 abbr = (int)get_tv_number(&argvars[2]);
7071 if (argvars[3].v_type != VAR_UNKNOWN)
7072 get_dict = (int)get_tv_number(&argvars[3]);
7073 }
7074 }
7075 else
7076 which = (char_u *)"";
7077 if (which == NULL)
7078 return;
7079
7080 mode = get_map_mode(&which, 0);
7081
7082 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7083 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7084 vim_free(keys_buf);
7085
7086 if (!get_dict)
7087 {
7088 /* Return a string. */
7089 if (rhs != NULL)
7090 rettv->vval.v_string = str2special_save(rhs, FALSE);
7091
7092 }
7093 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7094 {
7095 /* Return a dictionary. */
7096 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7097 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7098 dict_T *dict = rettv->vval.v_dict;
7099
7100 dict_add_nr_str(dict, "lhs", 0L, lhs);
7101 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
7102 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
7103 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
7104 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
7105 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
7106 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
7107 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
7108 dict_add_nr_str(dict, "mode", 0L, mapmode);
7109
7110 vim_free(lhs);
7111 vim_free(mapmode);
7112 }
7113}
7114
7115#ifdef FEAT_FLOAT
7116/*
7117 * "log()" function
7118 */
7119 static void
7120f_log(typval_T *argvars, typval_T *rettv)
7121{
7122 float_T f = 0.0;
7123
7124 rettv->v_type = VAR_FLOAT;
7125 if (get_float_arg(argvars, &f) == OK)
7126 rettv->vval.v_float = log(f);
7127 else
7128 rettv->vval.v_float = 0.0;
7129}
7130
7131/*
7132 * "log10()" function
7133 */
7134 static void
7135f_log10(typval_T *argvars, typval_T *rettv)
7136{
7137 float_T f = 0.0;
7138
7139 rettv->v_type = VAR_FLOAT;
7140 if (get_float_arg(argvars, &f) == OK)
7141 rettv->vval.v_float = log10(f);
7142 else
7143 rettv->vval.v_float = 0.0;
7144}
7145#endif
7146
7147#ifdef FEAT_LUA
7148/*
7149 * "luaeval()" function
7150 */
7151 static void
7152f_luaeval(typval_T *argvars, typval_T *rettv)
7153{
7154 char_u *str;
7155 char_u buf[NUMBUFLEN];
7156
7157 str = get_tv_string_buf(&argvars[0], buf);
7158 do_luaeval(str, argvars + 1, rettv);
7159}
7160#endif
7161
7162/*
7163 * "map()" function
7164 */
7165 static void
7166f_map(typval_T *argvars, typval_T *rettv)
7167{
7168 filter_map(argvars, rettv, TRUE);
7169}
7170
7171/*
7172 * "maparg()" function
7173 */
7174 static void
7175f_maparg(typval_T *argvars, typval_T *rettv)
7176{
7177 get_maparg(argvars, rettv, TRUE);
7178}
7179
7180/*
7181 * "mapcheck()" function
7182 */
7183 static void
7184f_mapcheck(typval_T *argvars, typval_T *rettv)
7185{
7186 get_maparg(argvars, rettv, FALSE);
7187}
7188
7189static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
7190
7191 static void
7192find_some_match(typval_T *argvars, typval_T *rettv, int type)
7193{
7194 char_u *str = NULL;
7195 long len = 0;
7196 char_u *expr = NULL;
7197 char_u *pat;
7198 regmatch_T regmatch;
7199 char_u patbuf[NUMBUFLEN];
7200 char_u strbuf[NUMBUFLEN];
7201 char_u *save_cpo;
7202 long start = 0;
7203 long nth = 1;
7204 colnr_T startcol = 0;
7205 int match = 0;
7206 list_T *l = NULL;
7207 listitem_T *li = NULL;
7208 long idx = 0;
7209 char_u *tofree = NULL;
7210
7211 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7212 save_cpo = p_cpo;
7213 p_cpo = (char_u *)"";
7214
7215 rettv->vval.v_number = -1;
7216 if (type == 3 || type == 4)
7217 {
7218 /* type 3: return empty list when there are no matches.
7219 * type 4: return ["", -1, -1, -1] */
7220 if (rettv_list_alloc(rettv) == FAIL)
7221 goto theend;
7222 if (type == 4
7223 && (list_append_string(rettv->vval.v_list,
7224 (char_u *)"", 0) == FAIL
7225 || list_append_number(rettv->vval.v_list,
7226 (varnumber_T)-1) == FAIL
7227 || list_append_number(rettv->vval.v_list,
7228 (varnumber_T)-1) == FAIL
7229 || list_append_number(rettv->vval.v_list,
7230 (varnumber_T)-1) == FAIL))
7231 {
7232 list_free(rettv->vval.v_list);
7233 rettv->vval.v_list = NULL;
7234 goto theend;
7235 }
7236 }
7237 else if (type == 2)
7238 {
7239 rettv->v_type = VAR_STRING;
7240 rettv->vval.v_string = NULL;
7241 }
7242
7243 if (argvars[0].v_type == VAR_LIST)
7244 {
7245 if ((l = argvars[0].vval.v_list) == NULL)
7246 goto theend;
7247 li = l->lv_first;
7248 }
7249 else
7250 {
7251 expr = str = get_tv_string(&argvars[0]);
7252 len = (long)STRLEN(str);
7253 }
7254
7255 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
7256 if (pat == NULL)
7257 goto theend;
7258
7259 if (argvars[2].v_type != VAR_UNKNOWN)
7260 {
7261 int error = FALSE;
7262
7263 start = (long)get_tv_number_chk(&argvars[2], &error);
7264 if (error)
7265 goto theend;
7266 if (l != NULL)
7267 {
7268 li = list_find(l, start);
7269 if (li == NULL)
7270 goto theend;
7271 idx = l->lv_idx; /* use the cached index */
7272 }
7273 else
7274 {
7275 if (start < 0)
7276 start = 0;
7277 if (start > len)
7278 goto theend;
7279 /* When "count" argument is there ignore matches before "start",
7280 * otherwise skip part of the string. Differs when pattern is "^"
7281 * or "\<". */
7282 if (argvars[3].v_type != VAR_UNKNOWN)
7283 startcol = start;
7284 else
7285 {
7286 str += start;
7287 len -= start;
7288 }
7289 }
7290
7291 if (argvars[3].v_type != VAR_UNKNOWN)
7292 nth = (long)get_tv_number_chk(&argvars[3], &error);
7293 if (error)
7294 goto theend;
7295 }
7296
7297 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7298 if (regmatch.regprog != NULL)
7299 {
7300 regmatch.rm_ic = p_ic;
7301
7302 for (;;)
7303 {
7304 if (l != NULL)
7305 {
7306 if (li == NULL)
7307 {
7308 match = FALSE;
7309 break;
7310 }
7311 vim_free(tofree);
7312 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7313 if (str == NULL)
7314 break;
7315 }
7316
7317 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7318
7319 if (match && --nth <= 0)
7320 break;
7321 if (l == NULL && !match)
7322 break;
7323
7324 /* Advance to just after the match. */
7325 if (l != NULL)
7326 {
7327 li = li->li_next;
7328 ++idx;
7329 }
7330 else
7331 {
7332#ifdef FEAT_MBYTE
7333 startcol = (colnr_T)(regmatch.startp[0]
7334 + (*mb_ptr2len)(regmatch.startp[0]) - str);
7335#else
7336 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
7337#endif
7338 if (startcol > (colnr_T)len
7339 || str + startcol <= regmatch.startp[0])
7340 {
7341 match = FALSE;
7342 break;
7343 }
7344 }
7345 }
7346
7347 if (match)
7348 {
7349 if (type == 4)
7350 {
7351 listitem_T *li1 = rettv->vval.v_list->lv_first;
7352 listitem_T *li2 = li1->li_next;
7353 listitem_T *li3 = li2->li_next;
7354 listitem_T *li4 = li3->li_next;
7355
7356 vim_free(li1->li_tv.vval.v_string);
7357 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7358 (int)(regmatch.endp[0] - regmatch.startp[0]));
7359 li3->li_tv.vval.v_number =
7360 (varnumber_T)(regmatch.startp[0] - expr);
7361 li4->li_tv.vval.v_number =
7362 (varnumber_T)(regmatch.endp[0] - expr);
7363 if (l != NULL)
7364 li2->li_tv.vval.v_number = (varnumber_T)idx;
7365 }
7366 else if (type == 3)
7367 {
7368 int i;
7369
7370 /* return list with matched string and submatches */
7371 for (i = 0; i < NSUBEXP; ++i)
7372 {
7373 if (regmatch.endp[i] == NULL)
7374 {
7375 if (list_append_string(rettv->vval.v_list,
7376 (char_u *)"", 0) == FAIL)
7377 break;
7378 }
7379 else if (list_append_string(rettv->vval.v_list,
7380 regmatch.startp[i],
7381 (int)(regmatch.endp[i] - regmatch.startp[i]))
7382 == FAIL)
7383 break;
7384 }
7385 }
7386 else if (type == 2)
7387 {
7388 /* return matched string */
7389 if (l != NULL)
7390 copy_tv(&li->li_tv, rettv);
7391 else
7392 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
7393 (int)(regmatch.endp[0] - regmatch.startp[0]));
7394 }
7395 else if (l != NULL)
7396 rettv->vval.v_number = idx;
7397 else
7398 {
7399 if (type != 0)
7400 rettv->vval.v_number =
7401 (varnumber_T)(regmatch.startp[0] - str);
7402 else
7403 rettv->vval.v_number =
7404 (varnumber_T)(regmatch.endp[0] - str);
7405 rettv->vval.v_number += (varnumber_T)(str - expr);
7406 }
7407 }
7408 vim_regfree(regmatch.regprog);
7409 }
7410
7411 if (type == 4 && l == NULL)
7412 /* matchstrpos() without a list: drop the second item. */
7413 listitem_remove(rettv->vval.v_list,
7414 rettv->vval.v_list->lv_first->li_next);
7415
7416theend:
7417 vim_free(tofree);
7418 p_cpo = save_cpo;
7419}
7420
7421/*
7422 * "match()" function
7423 */
7424 static void
7425f_match(typval_T *argvars, typval_T *rettv)
7426{
7427 find_some_match(argvars, rettv, 1);
7428}
7429
7430/*
7431 * "matchadd()" function
7432 */
7433 static void
7434f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7435{
7436#ifdef FEAT_SEARCH_EXTRA
7437 char_u buf[NUMBUFLEN];
7438 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
7439 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
7440 int prio = 10; /* default priority */
7441 int id = -1;
7442 int error = FALSE;
7443 char_u *conceal_char = NULL;
7444
7445 rettv->vval.v_number = -1;
7446
7447 if (grp == NULL || pat == NULL)
7448 return;
7449 if (argvars[2].v_type != VAR_UNKNOWN)
7450 {
7451 prio = (int)get_tv_number_chk(&argvars[2], &error);
7452 if (argvars[3].v_type != VAR_UNKNOWN)
7453 {
7454 id = (int)get_tv_number_chk(&argvars[3], &error);
7455 if (argvars[4].v_type != VAR_UNKNOWN)
7456 {
7457 if (argvars[4].v_type != VAR_DICT)
7458 {
7459 EMSG(_(e_dictreq));
7460 return;
7461 }
7462 if (dict_find(argvars[4].vval.v_dict,
7463 (char_u *)"conceal", -1) != NULL)
7464 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7465 (char_u *)"conceal", FALSE);
7466 }
7467 }
7468 }
7469 if (error == TRUE)
7470 return;
7471 if (id >= 1 && id <= 3)
7472 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007473 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007474 return;
7475 }
7476
7477 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
7478 conceal_char);
7479#endif
7480}
7481
7482/*
7483 * "matchaddpos()" function
7484 */
7485 static void
7486f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7487{
7488#ifdef FEAT_SEARCH_EXTRA
7489 char_u buf[NUMBUFLEN];
7490 char_u *group;
7491 int prio = 10;
7492 int id = -1;
7493 int error = FALSE;
7494 list_T *l;
7495 char_u *conceal_char = NULL;
7496
7497 rettv->vval.v_number = -1;
7498
7499 group = get_tv_string_buf_chk(&argvars[0], buf);
7500 if (group == NULL)
7501 return;
7502
7503 if (argvars[1].v_type != VAR_LIST)
7504 {
7505 EMSG2(_(e_listarg), "matchaddpos()");
7506 return;
7507 }
7508 l = argvars[1].vval.v_list;
7509 if (l == NULL)
7510 return;
7511
7512 if (argvars[2].v_type != VAR_UNKNOWN)
7513 {
7514 prio = (int)get_tv_number_chk(&argvars[2], &error);
7515 if (argvars[3].v_type != VAR_UNKNOWN)
7516 {
7517 id = (int)get_tv_number_chk(&argvars[3], &error);
7518 if (argvars[4].v_type != VAR_UNKNOWN)
7519 {
7520 if (argvars[4].v_type != VAR_DICT)
7521 {
7522 EMSG(_(e_dictreq));
7523 return;
7524 }
7525 if (dict_find(argvars[4].vval.v_dict,
7526 (char_u *)"conceal", -1) != NULL)
7527 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7528 (char_u *)"conceal", FALSE);
7529 }
7530 }
7531 }
7532 if (error == TRUE)
7533 return;
7534
7535 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
7536 if (id == 1 || id == 2)
7537 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007538 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007539 return;
7540 }
7541
7542 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
7543 conceal_char);
7544#endif
7545}
7546
7547/*
7548 * "matcharg()" function
7549 */
7550 static void
7551f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
7552{
7553 if (rettv_list_alloc(rettv) == OK)
7554 {
7555#ifdef FEAT_SEARCH_EXTRA
7556 int id = (int)get_tv_number(&argvars[0]);
7557 matchitem_T *m;
7558
7559 if (id >= 1 && id <= 3)
7560 {
7561 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
7562 {
7563 list_append_string(rettv->vval.v_list,
7564 syn_id2name(m->hlg_id), -1);
7565 list_append_string(rettv->vval.v_list, m->pattern, -1);
7566 }
7567 else
7568 {
7569 list_append_string(rettv->vval.v_list, NULL, -1);
7570 list_append_string(rettv->vval.v_list, NULL, -1);
7571 }
7572 }
7573#endif
7574 }
7575}
7576
7577/*
7578 * "matchdelete()" function
7579 */
7580 static void
7581f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7582{
7583#ifdef FEAT_SEARCH_EXTRA
7584 rettv->vval.v_number = match_delete(curwin,
7585 (int)get_tv_number(&argvars[0]), TRUE);
7586#endif
7587}
7588
7589/*
7590 * "matchend()" function
7591 */
7592 static void
7593f_matchend(typval_T *argvars, typval_T *rettv)
7594{
7595 find_some_match(argvars, rettv, 0);
7596}
7597
7598/*
7599 * "matchlist()" function
7600 */
7601 static void
7602f_matchlist(typval_T *argvars, typval_T *rettv)
7603{
7604 find_some_match(argvars, rettv, 3);
7605}
7606
7607/*
7608 * "matchstr()" function
7609 */
7610 static void
7611f_matchstr(typval_T *argvars, typval_T *rettv)
7612{
7613 find_some_match(argvars, rettv, 2);
7614}
7615
7616/*
7617 * "matchstrpos()" function
7618 */
7619 static void
7620f_matchstrpos(typval_T *argvars, typval_T *rettv)
7621{
7622 find_some_match(argvars, rettv, 4);
7623}
7624
7625static void max_min(typval_T *argvars, typval_T *rettv, int domax);
7626
7627 static void
7628max_min(typval_T *argvars, typval_T *rettv, int domax)
7629{
7630 varnumber_T n = 0;
7631 varnumber_T i;
7632 int error = FALSE;
7633
7634 if (argvars[0].v_type == VAR_LIST)
7635 {
7636 list_T *l;
7637 listitem_T *li;
7638
7639 l = argvars[0].vval.v_list;
7640 if (l != NULL)
7641 {
7642 li = l->lv_first;
7643 if (li != NULL)
7644 {
7645 n = get_tv_number_chk(&li->li_tv, &error);
7646 for (;;)
7647 {
7648 li = li->li_next;
7649 if (li == NULL)
7650 break;
7651 i = get_tv_number_chk(&li->li_tv, &error);
7652 if (domax ? i > n : i < n)
7653 n = i;
7654 }
7655 }
7656 }
7657 }
7658 else if (argvars[0].v_type == VAR_DICT)
7659 {
7660 dict_T *d;
7661 int first = TRUE;
7662 hashitem_T *hi;
7663 int todo;
7664
7665 d = argvars[0].vval.v_dict;
7666 if (d != NULL)
7667 {
7668 todo = (int)d->dv_hashtab.ht_used;
7669 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
7670 {
7671 if (!HASHITEM_EMPTY(hi))
7672 {
7673 --todo;
7674 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
7675 if (first)
7676 {
7677 n = i;
7678 first = FALSE;
7679 }
7680 else if (domax ? i > n : i < n)
7681 n = i;
7682 }
7683 }
7684 }
7685 }
7686 else
Bram Moolenaar26b84332016-09-04 21:42:36 +02007687 EMSG2(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007688 rettv->vval.v_number = error ? 0 : n;
7689}
7690
7691/*
7692 * "max()" function
7693 */
7694 static void
7695f_max(typval_T *argvars, typval_T *rettv)
7696{
7697 max_min(argvars, rettv, TRUE);
7698}
7699
7700/*
7701 * "min()" function
7702 */
7703 static void
7704f_min(typval_T *argvars, typval_T *rettv)
7705{
7706 max_min(argvars, rettv, FALSE);
7707}
7708
7709static int mkdir_recurse(char_u *dir, int prot);
7710
7711/*
7712 * Create the directory in which "dir" is located, and higher levels when
7713 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02007714 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007715 */
7716 static int
7717mkdir_recurse(char_u *dir, int prot)
7718{
7719 char_u *p;
7720 char_u *updir;
7721 int r = FAIL;
7722
7723 /* Get end of directory name in "dir".
7724 * We're done when it's "/" or "c:/". */
7725 p = gettail_sep(dir);
7726 if (p <= get_past_head(dir))
7727 return OK;
7728
7729 /* If the directory exists we're done. Otherwise: create it.*/
7730 updir = vim_strnsave(dir, (int)(p - dir));
7731 if (updir == NULL)
7732 return FAIL;
7733 if (mch_isdir(updir))
7734 r = OK;
7735 else if (mkdir_recurse(updir, prot) == OK)
7736 r = vim_mkdir_emsg(updir, prot);
7737 vim_free(updir);
7738 return r;
7739}
7740
7741#ifdef vim_mkdir
7742/*
7743 * "mkdir()" function
7744 */
7745 static void
7746f_mkdir(typval_T *argvars, typval_T *rettv)
7747{
7748 char_u *dir;
7749 char_u buf[NUMBUFLEN];
7750 int prot = 0755;
7751
7752 rettv->vval.v_number = FAIL;
7753 if (check_restricted() || check_secure())
7754 return;
7755
7756 dir = get_tv_string_buf(&argvars[0], buf);
7757 if (*dir == NUL)
7758 rettv->vval.v_number = FAIL;
7759 else
7760 {
7761 if (*gettail(dir) == NUL)
7762 /* remove trailing slashes */
7763 *gettail_sep(dir) = NUL;
7764
7765 if (argvars[1].v_type != VAR_UNKNOWN)
7766 {
7767 if (argvars[2].v_type != VAR_UNKNOWN)
7768 prot = (int)get_tv_number_chk(&argvars[2], NULL);
7769 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
7770 mkdir_recurse(dir, prot);
7771 }
7772 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
7773 }
7774}
7775#endif
7776
7777/*
7778 * "mode()" function
7779 */
7780 static void
7781f_mode(typval_T *argvars, typval_T *rettv)
7782{
7783 char_u buf[3];
7784
7785 buf[1] = NUL;
7786 buf[2] = NUL;
7787
7788 if (time_for_testing == 93784)
7789 {
7790 /* Testing the two-character code. */
7791 buf[0] = 'x';
7792 buf[1] = '!';
7793 }
7794 else if (VIsual_active)
7795 {
7796 if (VIsual_select)
7797 buf[0] = VIsual_mode + 's' - 'v';
7798 else
7799 buf[0] = VIsual_mode;
7800 }
7801 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
7802 || State == CONFIRM)
7803 {
7804 buf[0] = 'r';
7805 if (State == ASKMORE)
7806 buf[1] = 'm';
7807 else if (State == CONFIRM)
7808 buf[1] = '?';
7809 }
7810 else if (State == EXTERNCMD)
7811 buf[0] = '!';
7812 else if (State & INSERT)
7813 {
7814#ifdef FEAT_VREPLACE
7815 if (State & VREPLACE_FLAG)
7816 {
7817 buf[0] = 'R';
7818 buf[1] = 'v';
7819 }
7820 else
7821#endif
Bram Moolenaare90858d2017-02-01 17:24:34 +01007822 {
7823 if (State & REPLACE_FLAG)
7824 buf[0] = 'R';
7825 else
7826 buf[0] = 'i';
7827#ifdef FEAT_INS_EXPAND
7828 if (ins_compl_active())
7829 buf[1] = 'c';
7830 else if (ctrl_x_mode == 1)
7831 buf[1] = 'x';
7832#endif
7833 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007834 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01007835 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007836 {
7837 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01007838 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007839 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01007840 else if (exmode_active == EXMODE_NORMAL)
7841 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007842 }
7843 else
7844 {
7845 buf[0] = 'n';
7846 if (finish_op)
7847 buf[1] = 'o';
7848 }
7849
7850 /* Clear out the minor mode when the argument is not a non-zero number or
7851 * non-empty string. */
7852 if (!non_zero_arg(&argvars[0]))
7853 buf[1] = NUL;
7854
7855 rettv->vval.v_string = vim_strsave(buf);
7856 rettv->v_type = VAR_STRING;
7857}
7858
7859#if defined(FEAT_MZSCHEME) || defined(PROTO)
7860/*
7861 * "mzeval()" function
7862 */
7863 static void
7864f_mzeval(typval_T *argvars, typval_T *rettv)
7865{
7866 char_u *str;
7867 char_u buf[NUMBUFLEN];
7868
7869 str = get_tv_string_buf(&argvars[0], buf);
7870 do_mzeval(str, rettv);
7871}
7872
7873 void
7874mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
7875{
7876 typval_T argvars[3];
7877
7878 argvars[0].v_type = VAR_STRING;
7879 argvars[0].vval.v_string = name;
7880 copy_tv(args, &argvars[1]);
7881 argvars[2].v_type = VAR_UNKNOWN;
7882 f_call(argvars, rettv);
7883 clear_tv(&argvars[1]);
7884}
7885#endif
7886
7887/*
7888 * "nextnonblank()" function
7889 */
7890 static void
7891f_nextnonblank(typval_T *argvars, typval_T *rettv)
7892{
7893 linenr_T lnum;
7894
7895 for (lnum = get_tv_lnum(argvars); ; ++lnum)
7896 {
7897 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
7898 {
7899 lnum = 0;
7900 break;
7901 }
7902 if (*skipwhite(ml_get(lnum)) != NUL)
7903 break;
7904 }
7905 rettv->vval.v_number = lnum;
7906}
7907
7908/*
7909 * "nr2char()" function
7910 */
7911 static void
7912f_nr2char(typval_T *argvars, typval_T *rettv)
7913{
7914 char_u buf[NUMBUFLEN];
7915
7916#ifdef FEAT_MBYTE
7917 if (has_mbyte)
7918 {
7919 int utf8 = 0;
7920
7921 if (argvars[1].v_type != VAR_UNKNOWN)
7922 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
7923 if (utf8)
7924 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
7925 else
7926 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
7927 }
7928 else
7929#endif
7930 {
7931 buf[0] = (char_u)get_tv_number(&argvars[0]);
7932 buf[1] = NUL;
7933 }
7934 rettv->v_type = VAR_STRING;
7935 rettv->vval.v_string = vim_strsave(buf);
7936}
7937
7938/*
7939 * "or(expr, expr)" function
7940 */
7941 static void
7942f_or(typval_T *argvars, typval_T *rettv)
7943{
7944 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
7945 | get_tv_number_chk(&argvars[1], NULL);
7946}
7947
7948/*
7949 * "pathshorten()" function
7950 */
7951 static void
7952f_pathshorten(typval_T *argvars, typval_T *rettv)
7953{
7954 char_u *p;
7955
7956 rettv->v_type = VAR_STRING;
7957 p = get_tv_string_chk(&argvars[0]);
7958 if (p == NULL)
7959 rettv->vval.v_string = NULL;
7960 else
7961 {
7962 p = vim_strsave(p);
7963 rettv->vval.v_string = p;
7964 if (p != NULL)
7965 shorten_dir(p);
7966 }
7967}
7968
7969#ifdef FEAT_PERL
7970/*
7971 * "perleval()" function
7972 */
7973 static void
7974f_perleval(typval_T *argvars, typval_T *rettv)
7975{
7976 char_u *str;
7977 char_u buf[NUMBUFLEN];
7978
7979 str = get_tv_string_buf(&argvars[0], buf);
7980 do_perleval(str, rettv);
7981}
7982#endif
7983
7984#ifdef FEAT_FLOAT
7985/*
7986 * "pow()" function
7987 */
7988 static void
7989f_pow(typval_T *argvars, typval_T *rettv)
7990{
7991 float_T fx = 0.0, fy = 0.0;
7992
7993 rettv->v_type = VAR_FLOAT;
7994 if (get_float_arg(argvars, &fx) == OK
7995 && get_float_arg(&argvars[1], &fy) == OK)
7996 rettv->vval.v_float = pow(fx, fy);
7997 else
7998 rettv->vval.v_float = 0.0;
7999}
8000#endif
8001
8002/*
8003 * "prevnonblank()" function
8004 */
8005 static void
8006f_prevnonblank(typval_T *argvars, typval_T *rettv)
8007{
8008 linenr_T lnum;
8009
8010 lnum = get_tv_lnum(argvars);
8011 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8012 lnum = 0;
8013 else
8014 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8015 --lnum;
8016 rettv->vval.v_number = lnum;
8017}
8018
8019/* This dummy va_list is here because:
8020 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8021 * - locally in the function results in a "used before set" warning
8022 * - using va_start() to initialize it gives "function with fixed args" error */
8023static va_list ap;
8024
8025/*
8026 * "printf()" function
8027 */
8028 static void
8029f_printf(typval_T *argvars, typval_T *rettv)
8030{
8031 char_u buf[NUMBUFLEN];
8032 int len;
8033 char_u *s;
8034 int saved_did_emsg = did_emsg;
8035 char *fmt;
8036
8037 rettv->v_type = VAR_STRING;
8038 rettv->vval.v_string = NULL;
8039
8040 /* Get the required length, allocate the buffer and do it for real. */
8041 did_emsg = FALSE;
8042 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
8043 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
8044 if (!did_emsg)
8045 {
8046 s = alloc(len + 1);
8047 if (s != NULL)
8048 {
8049 rettv->vval.v_string = s;
8050 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
8051 }
8052 }
8053 did_emsg |= saved_did_emsg;
8054}
8055
8056/*
8057 * "pumvisible()" function
8058 */
8059 static void
8060f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8061{
8062#ifdef FEAT_INS_EXPAND
8063 if (pum_visible())
8064 rettv->vval.v_number = 1;
8065#endif
8066}
8067
8068#ifdef FEAT_PYTHON3
8069/*
8070 * "py3eval()" function
8071 */
8072 static void
8073f_py3eval(typval_T *argvars, typval_T *rettv)
8074{
8075 char_u *str;
8076 char_u buf[NUMBUFLEN];
8077
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008078 if (p_pyx == 0)
8079 p_pyx = 3;
8080
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008081 str = get_tv_string_buf(&argvars[0], buf);
8082 do_py3eval(str, rettv);
8083}
8084#endif
8085
8086#ifdef FEAT_PYTHON
8087/*
8088 * "pyeval()" function
8089 */
8090 static void
8091f_pyeval(typval_T *argvars, typval_T *rettv)
8092{
8093 char_u *str;
8094 char_u buf[NUMBUFLEN];
8095
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008096 if (p_pyx == 0)
8097 p_pyx = 2;
8098
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008099 str = get_tv_string_buf(&argvars[0], buf);
8100 do_pyeval(str, rettv);
8101}
8102#endif
8103
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008104#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8105/*
8106 * "pyxeval()" function
8107 */
8108 static void
8109f_pyxeval(typval_T *argvars, typval_T *rettv)
8110{
8111# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8112 init_pyxversion();
8113 if (p_pyx == 2)
8114 f_pyeval(argvars, rettv);
8115 else
8116 f_py3eval(argvars, rettv);
8117# elif defined(FEAT_PYTHON)
8118 f_pyeval(argvars, rettv);
8119# elif defined(FEAT_PYTHON3)
8120 f_py3eval(argvars, rettv);
8121# endif
8122}
8123#endif
8124
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008125/*
8126 * "range()" function
8127 */
8128 static void
8129f_range(typval_T *argvars, typval_T *rettv)
8130{
8131 varnumber_T start;
8132 varnumber_T end;
8133 varnumber_T stride = 1;
8134 varnumber_T i;
8135 int error = FALSE;
8136
8137 start = get_tv_number_chk(&argvars[0], &error);
8138 if (argvars[1].v_type == VAR_UNKNOWN)
8139 {
8140 end = start - 1;
8141 start = 0;
8142 }
8143 else
8144 {
8145 end = get_tv_number_chk(&argvars[1], &error);
8146 if (argvars[2].v_type != VAR_UNKNOWN)
8147 stride = get_tv_number_chk(&argvars[2], &error);
8148 }
8149
8150 if (error)
8151 return; /* type error; errmsg already given */
8152 if (stride == 0)
8153 EMSG(_("E726: Stride is zero"));
8154 else if (stride > 0 ? end + 1 < start : end - 1 > start)
8155 EMSG(_("E727: Start past end"));
8156 else
8157 {
8158 if (rettv_list_alloc(rettv) == OK)
8159 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8160 if (list_append_number(rettv->vval.v_list,
8161 (varnumber_T)i) == FAIL)
8162 break;
8163 }
8164}
8165
8166/*
8167 * "readfile()" function
8168 */
8169 static void
8170f_readfile(typval_T *argvars, typval_T *rettv)
8171{
8172 int binary = FALSE;
8173 int failed = FALSE;
8174 char_u *fname;
8175 FILE *fd;
8176 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8177 int io_size = sizeof(buf);
8178 int readlen; /* size of last fread() */
8179 char_u *prev = NULL; /* previously read bytes, if any */
8180 long prevlen = 0; /* length of data in prev */
8181 long prevsize = 0; /* size of prev buffer */
8182 long maxline = MAXLNUM;
8183 long cnt = 0;
8184 char_u *p; /* position in buf */
8185 char_u *start; /* start of current line */
8186
8187 if (argvars[1].v_type != VAR_UNKNOWN)
8188 {
8189 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
8190 binary = TRUE;
8191 if (argvars[2].v_type != VAR_UNKNOWN)
8192 maxline = (long)get_tv_number(&argvars[2]);
8193 }
8194
8195 if (rettv_list_alloc(rettv) == FAIL)
8196 return;
8197
8198 /* Always open the file in binary mode, library functions have a mind of
8199 * their own about CR-LF conversion. */
8200 fname = get_tv_string(&argvars[0]);
8201 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8202 {
8203 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
8204 return;
8205 }
8206
8207 while (cnt < maxline || maxline < 0)
8208 {
8209 readlen = (int)fread(buf, 1, io_size, fd);
8210
8211 /* This for loop processes what was read, but is also entered at end
8212 * of file so that either:
8213 * - an incomplete line gets written
8214 * - a "binary" file gets an empty line at the end if it ends in a
8215 * newline. */
8216 for (p = buf, start = buf;
8217 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8218 ++p)
8219 {
8220 if (*p == '\n' || readlen <= 0)
8221 {
8222 listitem_T *li;
8223 char_u *s = NULL;
8224 long_u len = p - start;
8225
8226 /* Finished a line. Remove CRs before NL. */
8227 if (readlen > 0 && !binary)
8228 {
8229 while (len > 0 && start[len - 1] == '\r')
8230 --len;
8231 /* removal may cross back to the "prev" string */
8232 if (len == 0)
8233 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8234 --prevlen;
8235 }
8236 if (prevlen == 0)
8237 s = vim_strnsave(start, (int)len);
8238 else
8239 {
8240 /* Change "prev" buffer to be the right size. This way
8241 * the bytes are only copied once, and very long lines are
8242 * allocated only once. */
8243 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8244 {
8245 mch_memmove(s + prevlen, start, len);
8246 s[prevlen + len] = NUL;
8247 prev = NULL; /* the list will own the string */
8248 prevlen = prevsize = 0;
8249 }
8250 }
8251 if (s == NULL)
8252 {
8253 do_outofmem_msg((long_u) prevlen + len + 1);
8254 failed = TRUE;
8255 break;
8256 }
8257
8258 if ((li = listitem_alloc()) == NULL)
8259 {
8260 vim_free(s);
8261 failed = TRUE;
8262 break;
8263 }
8264 li->li_tv.v_type = VAR_STRING;
8265 li->li_tv.v_lock = 0;
8266 li->li_tv.vval.v_string = s;
8267 list_append(rettv->vval.v_list, li);
8268
8269 start = p + 1; /* step over newline */
8270 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
8271 break;
8272 }
8273 else if (*p == NUL)
8274 *p = '\n';
8275#ifdef FEAT_MBYTE
8276 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
8277 * when finding the BF and check the previous two bytes. */
8278 else if (*p == 0xbf && enc_utf8 && !binary)
8279 {
8280 /* Find the two bytes before the 0xbf. If p is at buf, or buf
8281 * + 1, these may be in the "prev" string. */
8282 char_u back1 = p >= buf + 1 ? p[-1]
8283 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
8284 char_u back2 = p >= buf + 2 ? p[-2]
8285 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
8286 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
8287
8288 if (back2 == 0xef && back1 == 0xbb)
8289 {
8290 char_u *dest = p - 2;
8291
8292 /* Usually a BOM is at the beginning of a file, and so at
8293 * the beginning of a line; then we can just step over it.
8294 */
8295 if (start == dest)
8296 start = p + 1;
8297 else
8298 {
8299 /* have to shuffle buf to close gap */
8300 int adjust_prevlen = 0;
8301
8302 if (dest < buf)
8303 {
8304 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
8305 dest = buf;
8306 }
8307 if (readlen > p - buf + 1)
8308 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
8309 readlen -= 3 - adjust_prevlen;
8310 prevlen -= adjust_prevlen;
8311 p = dest - 1;
8312 }
8313 }
8314 }
8315#endif
8316 } /* for */
8317
8318 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
8319 break;
8320 if (start < p)
8321 {
8322 /* There's part of a line in buf, store it in "prev". */
8323 if (p - start + prevlen >= prevsize)
8324 {
8325 /* need bigger "prev" buffer */
8326 char_u *newprev;
8327
8328 /* A common use case is ordinary text files and "prev" gets a
8329 * fragment of a line, so the first allocation is made
8330 * small, to avoid repeatedly 'allocing' large and
8331 * 'reallocing' small. */
8332 if (prevsize == 0)
8333 prevsize = (long)(p - start);
8334 else
8335 {
8336 long grow50pc = (prevsize * 3) / 2;
8337 long growmin = (long)((p - start) * 2 + prevlen);
8338 prevsize = grow50pc > growmin ? grow50pc : growmin;
8339 }
8340 newprev = prev == NULL ? alloc(prevsize)
8341 : vim_realloc(prev, prevsize);
8342 if (newprev == NULL)
8343 {
8344 do_outofmem_msg((long_u)prevsize);
8345 failed = TRUE;
8346 break;
8347 }
8348 prev = newprev;
8349 }
8350 /* Add the line part to end of "prev". */
8351 mch_memmove(prev + prevlen, start, p - start);
8352 prevlen += (long)(p - start);
8353 }
8354 } /* while */
8355
8356 /*
8357 * For a negative line count use only the lines at the end of the file,
8358 * free the rest.
8359 */
8360 if (!failed && maxline < 0)
8361 while (cnt > -maxline)
8362 {
8363 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
8364 --cnt;
8365 }
8366
8367 if (failed)
8368 {
8369 list_free(rettv->vval.v_list);
8370 /* readfile doc says an empty list is returned on error */
8371 rettv->vval.v_list = list_alloc();
8372 }
8373
8374 vim_free(prev);
8375 fclose(fd);
8376}
8377
8378#if defined(FEAT_RELTIME)
8379static int list2proftime(typval_T *arg, proftime_T *tm);
8380
8381/*
8382 * Convert a List to proftime_T.
8383 * Return FAIL when there is something wrong.
8384 */
8385 static int
8386list2proftime(typval_T *arg, proftime_T *tm)
8387{
8388 long n1, n2;
8389 int error = FALSE;
8390
8391 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
8392 || arg->vval.v_list->lv_len != 2)
8393 return FAIL;
8394 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
8395 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
8396# ifdef WIN3264
8397 tm->HighPart = n1;
8398 tm->LowPart = n2;
8399# else
8400 tm->tv_sec = n1;
8401 tm->tv_usec = n2;
8402# endif
8403 return error ? FAIL : OK;
8404}
8405#endif /* FEAT_RELTIME */
8406
8407/*
8408 * "reltime()" function
8409 */
8410 static void
8411f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8412{
8413#ifdef FEAT_RELTIME
8414 proftime_T res;
8415 proftime_T start;
8416
8417 if (argvars[0].v_type == VAR_UNKNOWN)
8418 {
8419 /* No arguments: get current time. */
8420 profile_start(&res);
8421 }
8422 else if (argvars[1].v_type == VAR_UNKNOWN)
8423 {
8424 if (list2proftime(&argvars[0], &res) == FAIL)
8425 return;
8426 profile_end(&res);
8427 }
8428 else
8429 {
8430 /* Two arguments: compute the difference. */
8431 if (list2proftime(&argvars[0], &start) == FAIL
8432 || list2proftime(&argvars[1], &res) == FAIL)
8433 return;
8434 profile_sub(&res, &start);
8435 }
8436
8437 if (rettv_list_alloc(rettv) == OK)
8438 {
8439 long n1, n2;
8440
8441# ifdef WIN3264
8442 n1 = res.HighPart;
8443 n2 = res.LowPart;
8444# else
8445 n1 = res.tv_sec;
8446 n2 = res.tv_usec;
8447# endif
8448 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
8449 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
8450 }
8451#endif
8452}
8453
8454#ifdef FEAT_FLOAT
8455/*
8456 * "reltimefloat()" function
8457 */
8458 static void
8459f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
8460{
8461# ifdef FEAT_RELTIME
8462 proftime_T tm;
8463# endif
8464
8465 rettv->v_type = VAR_FLOAT;
8466 rettv->vval.v_float = 0;
8467# ifdef FEAT_RELTIME
8468 if (list2proftime(&argvars[0], &tm) == OK)
8469 rettv->vval.v_float = profile_float(&tm);
8470# endif
8471}
8472#endif
8473
8474/*
8475 * "reltimestr()" function
8476 */
8477 static void
8478f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
8479{
8480#ifdef FEAT_RELTIME
8481 proftime_T tm;
8482#endif
8483
8484 rettv->v_type = VAR_STRING;
8485 rettv->vval.v_string = NULL;
8486#ifdef FEAT_RELTIME
8487 if (list2proftime(&argvars[0], &tm) == OK)
8488 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
8489#endif
8490}
8491
8492#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
8493static void make_connection(void);
8494static int check_connection(void);
8495
8496 static void
8497make_connection(void)
8498{
8499 if (X_DISPLAY == NULL
8500# ifdef FEAT_GUI
8501 && !gui.in_use
8502# endif
8503 )
8504 {
8505 x_force_connect = TRUE;
8506 setup_term_clip();
8507 x_force_connect = FALSE;
8508 }
8509}
8510
8511 static int
8512check_connection(void)
8513{
8514 make_connection();
8515 if (X_DISPLAY == NULL)
8516 {
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01008517 EMSG(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008518 return FAIL;
8519 }
8520 return OK;
8521}
8522#endif
8523
8524#ifdef FEAT_CLIENTSERVER
8525 static void
8526remote_common(typval_T *argvars, typval_T *rettv, int expr)
8527{
8528 char_u *server_name;
8529 char_u *keys;
8530 char_u *r = NULL;
8531 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008532 int timeout = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008533# ifdef WIN32
8534 HWND w;
8535# else
8536 Window w;
8537# endif
8538
8539 if (check_restricted() || check_secure())
8540 return;
8541
8542# ifdef FEAT_X11
8543 if (check_connection() == FAIL)
8544 return;
8545# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008546 if (argvars[2].v_type != VAR_UNKNOWN
8547 && argvars[3].v_type != VAR_UNKNOWN)
8548 timeout = get_tv_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008549
8550 server_name = get_tv_string_chk(&argvars[0]);
8551 if (server_name == NULL)
8552 return; /* type error; errmsg already given */
8553 keys = get_tv_string_buf(&argvars[1], buf);
8554# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008555 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008556# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008557 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
8558 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008559# endif
8560 {
8561 if (r != NULL)
8562 EMSG(r); /* sending worked but evaluation failed */
8563 else
8564 EMSG2(_("E241: Unable to send to %s"), server_name);
8565 return;
8566 }
8567
8568 rettv->vval.v_string = r;
8569
8570 if (argvars[2].v_type != VAR_UNKNOWN)
8571 {
8572 dictitem_T v;
8573 char_u str[30];
8574 char_u *idvar;
8575
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008576 idvar = get_tv_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008577 if (idvar != NULL && *idvar != NUL)
8578 {
8579 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
8580 v.di_tv.v_type = VAR_STRING;
8581 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008582 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008583 vim_free(v.di_tv.vval.v_string);
8584 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008585 }
8586}
8587#endif
8588
8589/*
8590 * "remote_expr()" function
8591 */
8592 static void
8593f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
8594{
8595 rettv->v_type = VAR_STRING;
8596 rettv->vval.v_string = NULL;
8597#ifdef FEAT_CLIENTSERVER
8598 remote_common(argvars, rettv, TRUE);
8599#endif
8600}
8601
8602/*
8603 * "remote_foreground()" function
8604 */
8605 static void
8606f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8607{
8608#ifdef FEAT_CLIENTSERVER
8609# ifdef WIN32
8610 /* On Win32 it's done in this application. */
8611 {
8612 char_u *server_name = get_tv_string_chk(&argvars[0]);
8613
8614 if (server_name != NULL)
8615 serverForeground(server_name);
8616 }
8617# else
8618 /* Send a foreground() expression to the server. */
8619 argvars[1].v_type = VAR_STRING;
8620 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
8621 argvars[2].v_type = VAR_UNKNOWN;
8622 remote_common(argvars, rettv, TRUE);
8623 vim_free(argvars[1].vval.v_string);
8624# endif
8625#endif
8626}
8627
8628 static void
8629f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
8630{
8631#ifdef FEAT_CLIENTSERVER
8632 dictitem_T v;
8633 char_u *s = NULL;
8634# ifdef WIN32
8635 long_u n = 0;
8636# endif
8637 char_u *serverid;
8638
8639 if (check_restricted() || check_secure())
8640 {
8641 rettv->vval.v_number = -1;
8642 return;
8643 }
8644 serverid = get_tv_string_chk(&argvars[0]);
8645 if (serverid == NULL)
8646 {
8647 rettv->vval.v_number = -1;
8648 return; /* type error; errmsg already given */
8649 }
8650# ifdef WIN32
8651 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
8652 if (n == 0)
8653 rettv->vval.v_number = -1;
8654 else
8655 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008656 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008657 rettv->vval.v_number = (s != NULL);
8658 }
8659# else
8660 if (check_connection() == FAIL)
8661 return;
8662
8663 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
8664 serverStrToWin(serverid), &s);
8665# endif
8666
8667 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
8668 {
8669 char_u *retvar;
8670
8671 v.di_tv.v_type = VAR_STRING;
8672 v.di_tv.vval.v_string = vim_strsave(s);
8673 retvar = get_tv_string_chk(&argvars[1]);
8674 if (retvar != NULL)
8675 set_var(retvar, &v.di_tv, FALSE);
8676 vim_free(v.di_tv.vval.v_string);
8677 }
8678#else
8679 rettv->vval.v_number = -1;
8680#endif
8681}
8682
8683 static void
8684f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
8685{
8686 char_u *r = NULL;
8687
8688#ifdef FEAT_CLIENTSERVER
8689 char_u *serverid = get_tv_string_chk(&argvars[0]);
8690
8691 if (serverid != NULL && !check_restricted() && !check_secure())
8692 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008693 int timeout = 0;
Bram Moolenaar1662ce12017-03-19 21:47:50 +01008694# ifdef WIN32
8695 /* The server's HWND is encoded in the 'id' parameter */
8696 long_u n = 0;
8697# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008698
8699 if (argvars[1].v_type != VAR_UNKNOWN)
8700 timeout = get_tv_number(&argvars[1]);
8701
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008702# ifdef WIN32
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008703 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
8704 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008705 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008706 if (r == NULL)
8707# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008708 if (check_connection() == FAIL
8709 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
8710 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008711# endif
8712 EMSG(_("E277: Unable to read a server reply"));
8713 }
8714#endif
8715 rettv->v_type = VAR_STRING;
8716 rettv->vval.v_string = r;
8717}
8718
8719/*
8720 * "remote_send()" function
8721 */
8722 static void
8723f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
8724{
8725 rettv->v_type = VAR_STRING;
8726 rettv->vval.v_string = NULL;
8727#ifdef FEAT_CLIENTSERVER
8728 remote_common(argvars, rettv, FALSE);
8729#endif
8730}
8731
8732/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01008733 * "remote_startserver()" function
8734 */
8735 static void
8736f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8737{
8738#ifdef FEAT_CLIENTSERVER
8739 char_u *server = get_tv_string_chk(&argvars[0]);
8740
8741 if (server == NULL)
8742 return; /* type error; errmsg already given */
8743 if (serverName != NULL)
8744 EMSG(_("E941: already started a server"));
8745 else
8746 {
8747# ifdef FEAT_X11
8748 if (check_connection() == OK)
8749 serverRegisterName(X_DISPLAY, server);
8750# else
8751 serverSetName(server);
8752# endif
8753 }
8754#else
8755 EMSG(_("E942: +clientserver feature not available"));
8756#endif
8757}
8758
8759/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008760 * "remove()" function
8761 */
8762 static void
8763f_remove(typval_T *argvars, typval_T *rettv)
8764{
8765 list_T *l;
8766 listitem_T *item, *item2;
8767 listitem_T *li;
8768 long idx;
8769 long end;
8770 char_u *key;
8771 dict_T *d;
8772 dictitem_T *di;
8773 char_u *arg_errmsg = (char_u *)N_("remove() argument");
8774
8775 if (argvars[0].v_type == VAR_DICT)
8776 {
8777 if (argvars[2].v_type != VAR_UNKNOWN)
8778 EMSG2(_(e_toomanyarg), "remove()");
8779 else if ((d = argvars[0].vval.v_dict) != NULL
8780 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
8781 {
8782 key = get_tv_string_chk(&argvars[1]);
8783 if (key != NULL)
8784 {
8785 di = dict_find(d, key, -1);
8786 if (di == NULL)
8787 EMSG2(_(e_dictkey), key);
8788 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
8789 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
8790 {
8791 *rettv = di->di_tv;
8792 init_tv(&di->di_tv);
8793 dictitem_remove(d, di);
8794 }
8795 }
8796 }
8797 }
8798 else if (argvars[0].v_type != VAR_LIST)
8799 EMSG2(_(e_listdictarg), "remove()");
8800 else if ((l = argvars[0].vval.v_list) != NULL
8801 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
8802 {
8803 int error = FALSE;
8804
8805 idx = (long)get_tv_number_chk(&argvars[1], &error);
8806 if (error)
8807 ; /* type error: do nothing, errmsg already given */
8808 else if ((item = list_find(l, idx)) == NULL)
8809 EMSGN(_(e_listidx), idx);
8810 else
8811 {
8812 if (argvars[2].v_type == VAR_UNKNOWN)
8813 {
8814 /* Remove one item, return its value. */
8815 vimlist_remove(l, item, item);
8816 *rettv = item->li_tv;
8817 vim_free(item);
8818 }
8819 else
8820 {
8821 /* Remove range of items, return list with values. */
8822 end = (long)get_tv_number_chk(&argvars[2], &error);
8823 if (error)
8824 ; /* type error: do nothing */
8825 else if ((item2 = list_find(l, end)) == NULL)
8826 EMSGN(_(e_listidx), end);
8827 else
8828 {
8829 int cnt = 0;
8830
8831 for (li = item; li != NULL; li = li->li_next)
8832 {
8833 ++cnt;
8834 if (li == item2)
8835 break;
8836 }
8837 if (li == NULL) /* didn't find "item2" after "item" */
8838 EMSG(_(e_invrange));
8839 else
8840 {
8841 vimlist_remove(l, item, item2);
8842 if (rettv_list_alloc(rettv) == OK)
8843 {
8844 l = rettv->vval.v_list;
8845 l->lv_first = item;
8846 l->lv_last = item2;
8847 item->li_prev = NULL;
8848 item2->li_next = NULL;
8849 l->lv_len = cnt;
8850 }
8851 }
8852 }
8853 }
8854 }
8855 }
8856}
8857
8858/*
8859 * "rename({from}, {to})" function
8860 */
8861 static void
8862f_rename(typval_T *argvars, typval_T *rettv)
8863{
8864 char_u buf[NUMBUFLEN];
8865
8866 if (check_restricted() || check_secure())
8867 rettv->vval.v_number = -1;
8868 else
8869 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
8870 get_tv_string_buf(&argvars[1], buf));
8871}
8872
8873/*
8874 * "repeat()" function
8875 */
8876 static void
8877f_repeat(typval_T *argvars, typval_T *rettv)
8878{
8879 char_u *p;
8880 int n;
8881 int slen;
8882 int len;
8883 char_u *r;
8884 int i;
8885
8886 n = (int)get_tv_number(&argvars[1]);
8887 if (argvars[0].v_type == VAR_LIST)
8888 {
8889 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
8890 while (n-- > 0)
8891 if (list_extend(rettv->vval.v_list,
8892 argvars[0].vval.v_list, NULL) == FAIL)
8893 break;
8894 }
8895 else
8896 {
8897 p = get_tv_string(&argvars[0]);
8898 rettv->v_type = VAR_STRING;
8899 rettv->vval.v_string = NULL;
8900
8901 slen = (int)STRLEN(p);
8902 len = slen * n;
8903 if (len <= 0)
8904 return;
8905
8906 r = alloc(len + 1);
8907 if (r != NULL)
8908 {
8909 for (i = 0; i < n; i++)
8910 mch_memmove(r + i * slen, p, (size_t)slen);
8911 r[len] = NUL;
8912 }
8913
8914 rettv->vval.v_string = r;
8915 }
8916}
8917
8918/*
8919 * "resolve()" function
8920 */
8921 static void
8922f_resolve(typval_T *argvars, typval_T *rettv)
8923{
8924 char_u *p;
8925#ifdef HAVE_READLINK
8926 char_u *buf = NULL;
8927#endif
8928
8929 p = get_tv_string(&argvars[0]);
8930#ifdef FEAT_SHORTCUT
8931 {
8932 char_u *v = NULL;
8933
8934 v = mch_resolve_shortcut(p);
8935 if (v != NULL)
8936 rettv->vval.v_string = v;
8937 else
8938 rettv->vval.v_string = vim_strsave(p);
8939 }
8940#else
8941# ifdef HAVE_READLINK
8942 {
8943 char_u *cpy;
8944 int len;
8945 char_u *remain = NULL;
8946 char_u *q;
8947 int is_relative_to_current = FALSE;
8948 int has_trailing_pathsep = FALSE;
8949 int limit = 100;
8950
8951 p = vim_strsave(p);
8952
8953 if (p[0] == '.' && (vim_ispathsep(p[1])
8954 || (p[1] == '.' && (vim_ispathsep(p[2])))))
8955 is_relative_to_current = TRUE;
8956
8957 len = STRLEN(p);
8958 if (len > 0 && after_pathsep(p, p + len))
8959 {
8960 has_trailing_pathsep = TRUE;
8961 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
8962 }
8963
8964 q = getnextcomp(p);
8965 if (*q != NUL)
8966 {
8967 /* Separate the first path component in "p", and keep the
8968 * remainder (beginning with the path separator). */
8969 remain = vim_strsave(q - 1);
8970 q[-1] = NUL;
8971 }
8972
8973 buf = alloc(MAXPATHL + 1);
8974 if (buf == NULL)
8975 goto fail;
8976
8977 for (;;)
8978 {
8979 for (;;)
8980 {
8981 len = readlink((char *)p, (char *)buf, MAXPATHL);
8982 if (len <= 0)
8983 break;
8984 buf[len] = NUL;
8985
8986 if (limit-- == 0)
8987 {
8988 vim_free(p);
8989 vim_free(remain);
8990 EMSG(_("E655: Too many symbolic links (cycle?)"));
8991 rettv->vval.v_string = NULL;
8992 goto fail;
8993 }
8994
8995 /* Ensure that the result will have a trailing path separator
8996 * if the argument has one. */
8997 if (remain == NULL && has_trailing_pathsep)
8998 add_pathsep(buf);
8999
9000 /* Separate the first path component in the link value and
9001 * concatenate the remainders. */
9002 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9003 if (*q != NUL)
9004 {
9005 if (remain == NULL)
9006 remain = vim_strsave(q - 1);
9007 else
9008 {
9009 cpy = concat_str(q - 1, remain);
9010 if (cpy != NULL)
9011 {
9012 vim_free(remain);
9013 remain = cpy;
9014 }
9015 }
9016 q[-1] = NUL;
9017 }
9018
9019 q = gettail(p);
9020 if (q > p && *q == NUL)
9021 {
9022 /* Ignore trailing path separator. */
9023 q[-1] = NUL;
9024 q = gettail(p);
9025 }
9026 if (q > p && !mch_isFullName(buf))
9027 {
9028 /* symlink is relative to directory of argument */
9029 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
9030 if (cpy != NULL)
9031 {
9032 STRCPY(cpy, p);
9033 STRCPY(gettail(cpy), buf);
9034 vim_free(p);
9035 p = cpy;
9036 }
9037 }
9038 else
9039 {
9040 vim_free(p);
9041 p = vim_strsave(buf);
9042 }
9043 }
9044
9045 if (remain == NULL)
9046 break;
9047
9048 /* Append the first path component of "remain" to "p". */
9049 q = getnextcomp(remain + 1);
9050 len = q - remain - (*q != NUL);
9051 cpy = vim_strnsave(p, STRLEN(p) + len);
9052 if (cpy != NULL)
9053 {
9054 STRNCAT(cpy, remain, len);
9055 vim_free(p);
9056 p = cpy;
9057 }
9058 /* Shorten "remain". */
9059 if (*q != NUL)
9060 STRMOVE(remain, q - 1);
9061 else
9062 {
9063 vim_free(remain);
9064 remain = NULL;
9065 }
9066 }
9067
9068 /* If the result is a relative path name, make it explicitly relative to
9069 * the current directory if and only if the argument had this form. */
9070 if (!vim_ispathsep(*p))
9071 {
9072 if (is_relative_to_current
9073 && *p != NUL
9074 && !(p[0] == '.'
9075 && (p[1] == NUL
9076 || vim_ispathsep(p[1])
9077 || (p[1] == '.'
9078 && (p[2] == NUL
9079 || vim_ispathsep(p[2]))))))
9080 {
9081 /* Prepend "./". */
9082 cpy = concat_str((char_u *)"./", p);
9083 if (cpy != NULL)
9084 {
9085 vim_free(p);
9086 p = cpy;
9087 }
9088 }
9089 else if (!is_relative_to_current)
9090 {
9091 /* Strip leading "./". */
9092 q = p;
9093 while (q[0] == '.' && vim_ispathsep(q[1]))
9094 q += 2;
9095 if (q > p)
9096 STRMOVE(p, p + 2);
9097 }
9098 }
9099
9100 /* Ensure that the result will have no trailing path separator
9101 * if the argument had none. But keep "/" or "//". */
9102 if (!has_trailing_pathsep)
9103 {
9104 q = p + STRLEN(p);
9105 if (after_pathsep(p, q))
9106 *gettail_sep(p) = NUL;
9107 }
9108
9109 rettv->vval.v_string = p;
9110 }
9111# else
9112 rettv->vval.v_string = vim_strsave(p);
9113# endif
9114#endif
9115
9116 simplify_filename(rettv->vval.v_string);
9117
9118#ifdef HAVE_READLINK
9119fail:
9120 vim_free(buf);
9121#endif
9122 rettv->v_type = VAR_STRING;
9123}
9124
9125/*
9126 * "reverse({list})" function
9127 */
9128 static void
9129f_reverse(typval_T *argvars, typval_T *rettv)
9130{
9131 list_T *l;
9132 listitem_T *li, *ni;
9133
9134 if (argvars[0].v_type != VAR_LIST)
9135 EMSG2(_(e_listarg), "reverse()");
9136 else if ((l = argvars[0].vval.v_list) != NULL
9137 && !tv_check_lock(l->lv_lock,
9138 (char_u *)N_("reverse() argument"), TRUE))
9139 {
9140 li = l->lv_last;
9141 l->lv_first = l->lv_last = NULL;
9142 l->lv_len = 0;
9143 while (li != NULL)
9144 {
9145 ni = li->li_prev;
9146 list_append(l, li);
9147 li = ni;
9148 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009149 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009150 l->lv_idx = l->lv_len - l->lv_idx - 1;
9151 }
9152}
9153
9154#define SP_NOMOVE 0x01 /* don't move cursor */
9155#define SP_REPEAT 0x02 /* repeat to find outer pair */
9156#define SP_RETCOUNT 0x04 /* return matchcount */
9157#define SP_SETPCMARK 0x08 /* set previous context mark */
9158#define SP_START 0x10 /* accept match at start position */
9159#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9160#define SP_END 0x40 /* leave cursor at end of match */
9161#define SP_COLUMN 0x80 /* start at cursor column */
9162
9163static int get_search_arg(typval_T *varp, int *flagsp);
9164
9165/*
9166 * Get flags for a search function.
9167 * Possibly sets "p_ws".
9168 * Returns BACKWARD, FORWARD or zero (for an error).
9169 */
9170 static int
9171get_search_arg(typval_T *varp, int *flagsp)
9172{
9173 int dir = FORWARD;
9174 char_u *flags;
9175 char_u nbuf[NUMBUFLEN];
9176 int mask;
9177
9178 if (varp->v_type != VAR_UNKNOWN)
9179 {
9180 flags = get_tv_string_buf_chk(varp, nbuf);
9181 if (flags == NULL)
9182 return 0; /* type error; errmsg already given */
9183 while (*flags != NUL)
9184 {
9185 switch (*flags)
9186 {
9187 case 'b': dir = BACKWARD; break;
9188 case 'w': p_ws = TRUE; break;
9189 case 'W': p_ws = FALSE; break;
9190 default: mask = 0;
9191 if (flagsp != NULL)
9192 switch (*flags)
9193 {
9194 case 'c': mask = SP_START; break;
9195 case 'e': mask = SP_END; break;
9196 case 'm': mask = SP_RETCOUNT; break;
9197 case 'n': mask = SP_NOMOVE; break;
9198 case 'p': mask = SP_SUBPAT; break;
9199 case 'r': mask = SP_REPEAT; break;
9200 case 's': mask = SP_SETPCMARK; break;
9201 case 'z': mask = SP_COLUMN; break;
9202 }
9203 if (mask == 0)
9204 {
9205 EMSG2(_(e_invarg2), flags);
9206 dir = 0;
9207 }
9208 else
9209 *flagsp |= mask;
9210 }
9211 if (dir == 0)
9212 break;
9213 ++flags;
9214 }
9215 }
9216 return dir;
9217}
9218
9219/*
9220 * Shared by search() and searchpos() functions.
9221 */
9222 static int
9223search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9224{
9225 int flags;
9226 char_u *pat;
9227 pos_T pos;
9228 pos_T save_cursor;
9229 int save_p_ws = p_ws;
9230 int dir;
9231 int retval = 0; /* default: FAIL */
9232 long lnum_stop = 0;
9233 proftime_T tm;
9234#ifdef FEAT_RELTIME
9235 long time_limit = 0;
9236#endif
9237 int options = SEARCH_KEEP;
9238 int subpatnum;
9239
9240 pat = get_tv_string(&argvars[0]);
9241 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
9242 if (dir == 0)
9243 goto theend;
9244 flags = *flagsp;
9245 if (flags & SP_START)
9246 options |= SEARCH_START;
9247 if (flags & SP_END)
9248 options |= SEARCH_END;
9249 if (flags & SP_COLUMN)
9250 options |= SEARCH_COL;
9251
9252 /* Optional arguments: line number to stop searching and timeout. */
9253 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9254 {
9255 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
9256 if (lnum_stop < 0)
9257 goto theend;
9258#ifdef FEAT_RELTIME
9259 if (argvars[3].v_type != VAR_UNKNOWN)
9260 {
9261 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
9262 if (time_limit < 0)
9263 goto theend;
9264 }
9265#endif
9266 }
9267
9268#ifdef FEAT_RELTIME
9269 /* Set the time limit, if there is one. */
9270 profile_setlimit(time_limit, &tm);
9271#endif
9272
9273 /*
9274 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
9275 * Check to make sure only those flags are set.
9276 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
9277 * flags cannot be set. Check for that condition also.
9278 */
9279 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
9280 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9281 {
9282 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
9283 goto theend;
9284 }
9285
9286 pos = save_cursor = curwin->w_cursor;
9287 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009288 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009289 if (subpatnum != FAIL)
9290 {
9291 if (flags & SP_SUBPAT)
9292 retval = subpatnum;
9293 else
9294 retval = pos.lnum;
9295 if (flags & SP_SETPCMARK)
9296 setpcmark();
9297 curwin->w_cursor = pos;
9298 if (match_pos != NULL)
9299 {
9300 /* Store the match cursor position */
9301 match_pos->lnum = pos.lnum;
9302 match_pos->col = pos.col + 1;
9303 }
9304 /* "/$" will put the cursor after the end of the line, may need to
9305 * correct that here */
9306 check_cursor();
9307 }
9308
9309 /* If 'n' flag is used: restore cursor position. */
9310 if (flags & SP_NOMOVE)
9311 curwin->w_cursor = save_cursor;
9312 else
9313 curwin->w_set_curswant = TRUE;
9314theend:
9315 p_ws = save_p_ws;
9316
9317 return retval;
9318}
9319
9320#ifdef FEAT_FLOAT
9321
9322/*
9323 * round() is not in C90, use ceil() or floor() instead.
9324 */
9325 float_T
9326vim_round(float_T f)
9327{
9328 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
9329}
9330
9331/*
9332 * "round({float})" function
9333 */
9334 static void
9335f_round(typval_T *argvars, typval_T *rettv)
9336{
9337 float_T f = 0.0;
9338
9339 rettv->v_type = VAR_FLOAT;
9340 if (get_float_arg(argvars, &f) == OK)
9341 rettv->vval.v_float = vim_round(f);
9342 else
9343 rettv->vval.v_float = 0.0;
9344}
9345#endif
9346
9347/*
9348 * "screenattr()" function
9349 */
9350 static void
9351f_screenattr(typval_T *argvars, typval_T *rettv)
9352{
9353 int row;
9354 int col;
9355 int c;
9356
9357 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9358 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9359 if (row < 0 || row >= screen_Rows
9360 || col < 0 || col >= screen_Columns)
9361 c = -1;
9362 else
9363 c = ScreenAttrs[LineOffset[row] + col];
9364 rettv->vval.v_number = c;
9365}
9366
9367/*
9368 * "screenchar()" function
9369 */
9370 static void
9371f_screenchar(typval_T *argvars, typval_T *rettv)
9372{
9373 int row;
9374 int col;
9375 int off;
9376 int c;
9377
9378 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9379 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9380 if (row < 0 || row >= screen_Rows
9381 || col < 0 || col >= screen_Columns)
9382 c = -1;
9383 else
9384 {
9385 off = LineOffset[row] + col;
9386#ifdef FEAT_MBYTE
9387 if (enc_utf8 && ScreenLinesUC[off] != 0)
9388 c = ScreenLinesUC[off];
9389 else
9390#endif
9391 c = ScreenLines[off];
9392 }
9393 rettv->vval.v_number = c;
9394}
9395
9396/*
9397 * "screencol()" function
9398 *
9399 * First column is 1 to be consistent with virtcol().
9400 */
9401 static void
9402f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
9403{
9404 rettv->vval.v_number = screen_screencol() + 1;
9405}
9406
9407/*
9408 * "screenrow()" function
9409 */
9410 static void
9411f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
9412{
9413 rettv->vval.v_number = screen_screenrow() + 1;
9414}
9415
9416/*
9417 * "search()" function
9418 */
9419 static void
9420f_search(typval_T *argvars, typval_T *rettv)
9421{
9422 int flags = 0;
9423
9424 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
9425}
9426
9427/*
9428 * "searchdecl()" function
9429 */
9430 static void
9431f_searchdecl(typval_T *argvars, typval_T *rettv)
9432{
9433 int locally = 1;
9434 int thisblock = 0;
9435 int error = FALSE;
9436 char_u *name;
9437
9438 rettv->vval.v_number = 1; /* default: FAIL */
9439
9440 name = get_tv_string_chk(&argvars[0]);
9441 if (argvars[1].v_type != VAR_UNKNOWN)
9442 {
9443 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
9444 if (!error && argvars[2].v_type != VAR_UNKNOWN)
9445 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
9446 }
9447 if (!error && name != NULL)
9448 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
9449 locally, thisblock, SEARCH_KEEP) == FAIL;
9450}
9451
9452/*
9453 * Used by searchpair() and searchpairpos()
9454 */
9455 static int
9456searchpair_cmn(typval_T *argvars, pos_T *match_pos)
9457{
9458 char_u *spat, *mpat, *epat;
9459 char_u *skip;
9460 int save_p_ws = p_ws;
9461 int dir;
9462 int flags = 0;
9463 char_u nbuf1[NUMBUFLEN];
9464 char_u nbuf2[NUMBUFLEN];
9465 char_u nbuf3[NUMBUFLEN];
9466 int retval = 0; /* default: FAIL */
9467 long lnum_stop = 0;
9468 long time_limit = 0;
9469
9470 /* Get the three pattern arguments: start, middle, end. */
9471 spat = get_tv_string_chk(&argvars[0]);
9472 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
9473 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
9474 if (spat == NULL || mpat == NULL || epat == NULL)
9475 goto theend; /* type error */
9476
9477 /* Handle the optional fourth argument: flags */
9478 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
9479 if (dir == 0)
9480 goto theend;
9481
9482 /* Don't accept SP_END or SP_SUBPAT.
9483 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
9484 */
9485 if ((flags & (SP_END | SP_SUBPAT)) != 0
9486 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9487 {
9488 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
9489 goto theend;
9490 }
9491
9492 /* Using 'r' implies 'W', otherwise it doesn't work. */
9493 if (flags & SP_REPEAT)
9494 p_ws = FALSE;
9495
9496 /* Optional fifth argument: skip expression */
9497 if (argvars[3].v_type == VAR_UNKNOWN
9498 || argvars[4].v_type == VAR_UNKNOWN)
9499 skip = (char_u *)"";
9500 else
9501 {
9502 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
9503 if (argvars[5].v_type != VAR_UNKNOWN)
9504 {
9505 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
9506 if (lnum_stop < 0)
9507 goto theend;
9508#ifdef FEAT_RELTIME
9509 if (argvars[6].v_type != VAR_UNKNOWN)
9510 {
9511 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
9512 if (time_limit < 0)
9513 goto theend;
9514 }
9515#endif
9516 }
9517 }
9518 if (skip == NULL)
9519 goto theend; /* type error */
9520
9521 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
9522 match_pos, lnum_stop, time_limit);
9523
9524theend:
9525 p_ws = save_p_ws;
9526
9527 return retval;
9528}
9529
9530/*
9531 * "searchpair()" function
9532 */
9533 static void
9534f_searchpair(typval_T *argvars, typval_T *rettv)
9535{
9536 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
9537}
9538
9539/*
9540 * "searchpairpos()" function
9541 */
9542 static void
9543f_searchpairpos(typval_T *argvars, typval_T *rettv)
9544{
9545 pos_T match_pos;
9546 int lnum = 0;
9547 int col = 0;
9548
9549 if (rettv_list_alloc(rettv) == FAIL)
9550 return;
9551
9552 if (searchpair_cmn(argvars, &match_pos) > 0)
9553 {
9554 lnum = match_pos.lnum;
9555 col = match_pos.col;
9556 }
9557
9558 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9559 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9560}
9561
9562/*
9563 * Search for a start/middle/end thing.
9564 * Used by searchpair(), see its documentation for the details.
9565 * Returns 0 or -1 for no match,
9566 */
9567 long
9568do_searchpair(
9569 char_u *spat, /* start pattern */
9570 char_u *mpat, /* middle pattern */
9571 char_u *epat, /* end pattern */
9572 int dir, /* BACKWARD or FORWARD */
9573 char_u *skip, /* skip expression */
9574 int flags, /* SP_SETPCMARK and other SP_ values */
9575 pos_T *match_pos,
9576 linenr_T lnum_stop, /* stop at this line if not zero */
9577 long time_limit UNUSED) /* stop after this many msec */
9578{
9579 char_u *save_cpo;
9580 char_u *pat, *pat2 = NULL, *pat3 = NULL;
9581 long retval = 0;
9582 pos_T pos;
9583 pos_T firstpos;
9584 pos_T foundpos;
9585 pos_T save_cursor;
9586 pos_T save_pos;
9587 int n;
9588 int r;
9589 int nest = 1;
9590 int err;
9591 int options = SEARCH_KEEP;
9592 proftime_T tm;
9593
9594 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
9595 save_cpo = p_cpo;
9596 p_cpo = empty_option;
9597
9598#ifdef FEAT_RELTIME
9599 /* Set the time limit, if there is one. */
9600 profile_setlimit(time_limit, &tm);
9601#endif
9602
9603 /* Make two search patterns: start/end (pat2, for in nested pairs) and
9604 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009605 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
9606 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009607 if (pat2 == NULL || pat3 == NULL)
9608 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009609 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009610 if (*mpat == NUL)
9611 STRCPY(pat3, pat2);
9612 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009613 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009614 spat, epat, mpat);
9615 if (flags & SP_START)
9616 options |= SEARCH_START;
9617
9618 save_cursor = curwin->w_cursor;
9619 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009620 CLEAR_POS(&firstpos);
9621 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009622 pat = pat3;
9623 for (;;)
9624 {
9625 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009626 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009627 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009628 /* didn't find it or found the first match again: FAIL */
9629 break;
9630
9631 if (firstpos.lnum == 0)
9632 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009633 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009634 {
9635 /* Found the same position again. Can happen with a pattern that
9636 * has "\zs" at the end and searching backwards. Advance one
9637 * character and try again. */
9638 if (dir == BACKWARD)
9639 decl(&pos);
9640 else
9641 incl(&pos);
9642 }
9643 foundpos = pos;
9644
9645 /* clear the start flag to avoid getting stuck here */
9646 options &= ~SEARCH_START;
9647
9648 /* If the skip pattern matches, ignore this match. */
9649 if (*skip != NUL)
9650 {
9651 save_pos = curwin->w_cursor;
9652 curwin->w_cursor = pos;
9653 r = eval_to_bool(skip, &err, NULL, FALSE);
9654 curwin->w_cursor = save_pos;
9655 if (err)
9656 {
9657 /* Evaluating {skip} caused an error, break here. */
9658 curwin->w_cursor = save_cursor;
9659 retval = -1;
9660 break;
9661 }
9662 if (r)
9663 continue;
9664 }
9665
9666 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
9667 {
9668 /* Found end when searching backwards or start when searching
9669 * forward: nested pair. */
9670 ++nest;
9671 pat = pat2; /* nested, don't search for middle */
9672 }
9673 else
9674 {
9675 /* Found end when searching forward or start when searching
9676 * backward: end of (nested) pair; or found middle in outer pair. */
9677 if (--nest == 1)
9678 pat = pat3; /* outer level, search for middle */
9679 }
9680
9681 if (nest == 0)
9682 {
9683 /* Found the match: return matchcount or line number. */
9684 if (flags & SP_RETCOUNT)
9685 ++retval;
9686 else
9687 retval = pos.lnum;
9688 if (flags & SP_SETPCMARK)
9689 setpcmark();
9690 curwin->w_cursor = pos;
9691 if (!(flags & SP_REPEAT))
9692 break;
9693 nest = 1; /* search for next unmatched */
9694 }
9695 }
9696
9697 if (match_pos != NULL)
9698 {
9699 /* Store the match cursor position */
9700 match_pos->lnum = curwin->w_cursor.lnum;
9701 match_pos->col = curwin->w_cursor.col + 1;
9702 }
9703
9704 /* If 'n' flag is used or search failed: restore cursor position. */
9705 if ((flags & SP_NOMOVE) || retval == 0)
9706 curwin->w_cursor = save_cursor;
9707
9708theend:
9709 vim_free(pat2);
9710 vim_free(pat3);
9711 if (p_cpo == empty_option)
9712 p_cpo = save_cpo;
9713 else
9714 /* Darn, evaluating the {skip} expression changed the value. */
9715 free_string_option(save_cpo);
9716
9717 return retval;
9718}
9719
9720/*
9721 * "searchpos()" function
9722 */
9723 static void
9724f_searchpos(typval_T *argvars, typval_T *rettv)
9725{
9726 pos_T match_pos;
9727 int lnum = 0;
9728 int col = 0;
9729 int n;
9730 int flags = 0;
9731
9732 if (rettv_list_alloc(rettv) == FAIL)
9733 return;
9734
9735 n = search_cmn(argvars, &match_pos, &flags);
9736 if (n > 0)
9737 {
9738 lnum = match_pos.lnum;
9739 col = match_pos.col;
9740 }
9741
9742 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9743 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9744 if (flags & SP_SUBPAT)
9745 list_append_number(rettv->vval.v_list, (varnumber_T)n);
9746}
9747
9748 static void
9749f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
9750{
9751#ifdef FEAT_CLIENTSERVER
9752 char_u buf[NUMBUFLEN];
9753 char_u *server = get_tv_string_chk(&argvars[0]);
9754 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
9755
9756 rettv->vval.v_number = -1;
9757 if (server == NULL || reply == NULL)
9758 return;
9759 if (check_restricted() || check_secure())
9760 return;
9761# ifdef FEAT_X11
9762 if (check_connection() == FAIL)
9763 return;
9764# endif
9765
9766 if (serverSendReply(server, reply) < 0)
9767 {
9768 EMSG(_("E258: Unable to send to client"));
9769 return;
9770 }
9771 rettv->vval.v_number = 0;
9772#else
9773 rettv->vval.v_number = -1;
9774#endif
9775}
9776
9777 static void
9778f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
9779{
9780 char_u *r = NULL;
9781
9782#ifdef FEAT_CLIENTSERVER
9783# ifdef WIN32
9784 r = serverGetVimNames();
9785# else
9786 make_connection();
9787 if (X_DISPLAY != NULL)
9788 r = serverGetVimNames(X_DISPLAY);
9789# endif
9790#endif
9791 rettv->v_type = VAR_STRING;
9792 rettv->vval.v_string = r;
9793}
9794
9795/*
9796 * "setbufvar()" function
9797 */
9798 static void
9799f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
9800{
9801 buf_T *buf;
9802 char_u *varname, *bufvarname;
9803 typval_T *varp;
9804 char_u nbuf[NUMBUFLEN];
9805
9806 if (check_restricted() || check_secure())
9807 return;
9808 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9809 varname = get_tv_string_chk(&argvars[1]);
9810 buf = get_buf_tv(&argvars[0], FALSE);
9811 varp = &argvars[2];
9812
9813 if (buf != NULL && varname != NULL && varp != NULL)
9814 {
9815 if (*varname == '&')
9816 {
9817 long numval;
9818 char_u *strval;
9819 int error = FALSE;
9820 aco_save_T aco;
9821
9822 /* set curbuf to be our buf, temporarily */
9823 aucmd_prepbuf(&aco, buf);
9824
9825 ++varname;
9826 numval = (long)get_tv_number_chk(varp, &error);
9827 strval = get_tv_string_buf_chk(varp, nbuf);
9828 if (!error && strval != NULL)
9829 set_option_value(varname, numval, strval, OPT_LOCAL);
9830
9831 /* reset notion of buffer */
9832 aucmd_restbuf(&aco);
9833 }
9834 else
9835 {
9836 buf_T *save_curbuf = curbuf;
9837
9838 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
9839 if (bufvarname != NULL)
9840 {
9841 curbuf = buf;
9842 STRCPY(bufvarname, "b:");
9843 STRCPY(bufvarname + 2, varname);
9844 set_var(bufvarname, varp, TRUE);
9845 vim_free(bufvarname);
9846 curbuf = save_curbuf;
9847 }
9848 }
9849 }
9850}
9851
9852 static void
9853f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
9854{
9855 dict_T *d;
9856 dictitem_T *di;
9857 char_u *csearch;
9858
9859 if (argvars[0].v_type != VAR_DICT)
9860 {
9861 EMSG(_(e_dictreq));
9862 return;
9863 }
9864
9865 if ((d = argvars[0].vval.v_dict) != NULL)
9866 {
9867 csearch = get_dict_string(d, (char_u *)"char", FALSE);
9868 if (csearch != NULL)
9869 {
9870#ifdef FEAT_MBYTE
9871 if (enc_utf8)
9872 {
9873 int pcc[MAX_MCO];
9874 int c = utfc_ptr2char(csearch, pcc);
9875
9876 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
9877 }
9878 else
9879#endif
9880 set_last_csearch(PTR2CHAR(csearch),
9881 csearch, MB_PTR2LEN(csearch));
9882 }
9883
9884 di = dict_find(d, (char_u *)"forward", -1);
9885 if (di != NULL)
9886 set_csearch_direction((int)get_tv_number(&di->di_tv)
9887 ? FORWARD : BACKWARD);
9888
9889 di = dict_find(d, (char_u *)"until", -1);
9890 if (di != NULL)
9891 set_csearch_until(!!get_tv_number(&di->di_tv));
9892 }
9893}
9894
9895/*
9896 * "setcmdpos()" function
9897 */
9898 static void
9899f_setcmdpos(typval_T *argvars, typval_T *rettv)
9900{
9901 int pos = (int)get_tv_number(&argvars[0]) - 1;
9902
9903 if (pos >= 0)
9904 rettv->vval.v_number = set_cmdline_pos(pos);
9905}
9906
9907/*
9908 * "setfperm({fname}, {mode})" function
9909 */
9910 static void
9911f_setfperm(typval_T *argvars, typval_T *rettv)
9912{
9913 char_u *fname;
9914 char_u modebuf[NUMBUFLEN];
9915 char_u *mode_str;
9916 int i;
9917 int mask;
9918 int mode = 0;
9919
9920 rettv->vval.v_number = 0;
9921 fname = get_tv_string_chk(&argvars[0]);
9922 if (fname == NULL)
9923 return;
9924 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
9925 if (mode_str == NULL)
9926 return;
9927 if (STRLEN(mode_str) != 9)
9928 {
9929 EMSG2(_(e_invarg2), mode_str);
9930 return;
9931 }
9932
9933 mask = 1;
9934 for (i = 8; i >= 0; --i)
9935 {
9936 if (mode_str[i] != '-')
9937 mode |= mask;
9938 mask = mask << 1;
9939 }
9940 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
9941}
9942
9943/*
9944 * "setline()" function
9945 */
9946 static void
9947f_setline(typval_T *argvars, typval_T *rettv)
9948{
9949 linenr_T lnum;
9950 char_u *line = NULL;
9951 list_T *l = NULL;
9952 listitem_T *li = NULL;
9953 long added = 0;
9954 linenr_T lcount = curbuf->b_ml.ml_line_count;
9955
9956 lnum = get_tv_lnum(&argvars[0]);
9957 if (argvars[1].v_type == VAR_LIST)
9958 {
9959 l = argvars[1].vval.v_list;
9960 li = l->lv_first;
9961 }
9962 else
9963 line = get_tv_string_chk(&argvars[1]);
9964
9965 /* default result is zero == OK */
9966 for (;;)
9967 {
9968 if (l != NULL)
9969 {
9970 /* list argument, get next string */
9971 if (li == NULL)
9972 break;
9973 line = get_tv_string_chk(&li->li_tv);
9974 li = li->li_next;
9975 }
9976
9977 rettv->vval.v_number = 1; /* FAIL */
9978 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
9979 break;
9980
9981 /* When coming here from Insert mode, sync undo, so that this can be
9982 * undone separately from what was previously inserted. */
9983 if (u_sync_once == 2)
9984 {
9985 u_sync_once = 1; /* notify that u_sync() was called */
9986 u_sync(TRUE);
9987 }
9988
9989 if (lnum <= curbuf->b_ml.ml_line_count)
9990 {
9991 /* existing line, replace it */
9992 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
9993 {
9994 changed_bytes(lnum, 0);
9995 if (lnum == curwin->w_cursor.lnum)
9996 check_cursor_col();
9997 rettv->vval.v_number = 0; /* OK */
9998 }
9999 }
10000 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
10001 {
10002 /* lnum is one past the last line, append the line */
10003 ++added;
10004 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
10005 rettv->vval.v_number = 0; /* OK */
10006 }
10007
10008 if (l == NULL) /* only one string argument */
10009 break;
10010 ++lnum;
10011 }
10012
10013 if (added > 0)
10014 appended_lines_mark(lcount, added);
10015}
10016
Bram Moolenaard823fa92016-08-12 16:29:27 +020010017static 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 +020010018
10019/*
10020 * Used by "setqflist()" and "setloclist()" functions
10021 */
10022 static void
10023set_qf_ll_list(
10024 win_T *wp UNUSED,
10025 typval_T *list_arg UNUSED,
10026 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010027 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010028 typval_T *rettv)
10029{
10030#ifdef FEAT_QUICKFIX
10031 static char *e_invact = N_("E927: Invalid action: '%s'");
10032 char_u *act;
10033 int action = 0;
10034#endif
10035
10036 rettv->vval.v_number = -1;
10037
10038#ifdef FEAT_QUICKFIX
10039 if (list_arg->v_type != VAR_LIST)
10040 EMSG(_(e_listreq));
10041 else
10042 {
10043 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010044 dict_T *d = NULL;
10045 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010046
10047 if (action_arg->v_type == VAR_STRING)
10048 {
10049 act = get_tv_string_chk(action_arg);
10050 if (act == NULL)
10051 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020010052 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
10053 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010054 action = *act;
10055 else
10056 EMSG2(_(e_invact), act);
10057 }
10058 else if (action_arg->v_type == VAR_UNKNOWN)
10059 action = ' ';
10060 else
10061 EMSG(_(e_stringreq));
10062
Bram Moolenaard823fa92016-08-12 16:29:27 +020010063 if (action_arg->v_type != VAR_UNKNOWN
10064 && what_arg->v_type != VAR_UNKNOWN)
10065 {
10066 if (what_arg->v_type == VAR_DICT)
10067 d = what_arg->vval.v_dict;
10068 else
10069 {
10070 EMSG(_(e_dictreq));
10071 valid_dict = FALSE;
10072 }
10073 }
10074
10075 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
10076 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()"), d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010077 rettv->vval.v_number = 0;
10078 }
10079#endif
10080}
10081
10082/*
10083 * "setloclist()" function
10084 */
10085 static void
10086f_setloclist(typval_T *argvars, typval_T *rettv)
10087{
10088 win_T *win;
10089
10090 rettv->vval.v_number = -1;
10091
10092 win = find_win_by_nr(&argvars[0], NULL);
10093 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020010094 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010095}
10096
10097/*
10098 * "setmatches()" function
10099 */
10100 static void
10101f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10102{
10103#ifdef FEAT_SEARCH_EXTRA
10104 list_T *l;
10105 listitem_T *li;
10106 dict_T *d;
10107 list_T *s = NULL;
10108
10109 rettv->vval.v_number = -1;
10110 if (argvars[0].v_type != VAR_LIST)
10111 {
10112 EMSG(_(e_listreq));
10113 return;
10114 }
10115 if ((l = argvars[0].vval.v_list) != NULL)
10116 {
10117
10118 /* To some extent make sure that we are dealing with a list from
10119 * "getmatches()". */
10120 li = l->lv_first;
10121 while (li != NULL)
10122 {
10123 if (li->li_tv.v_type != VAR_DICT
10124 || (d = li->li_tv.vval.v_dict) == NULL)
10125 {
10126 EMSG(_(e_invarg));
10127 return;
10128 }
10129 if (!(dict_find(d, (char_u *)"group", -1) != NULL
10130 && (dict_find(d, (char_u *)"pattern", -1) != NULL
10131 || dict_find(d, (char_u *)"pos1", -1) != NULL)
10132 && dict_find(d, (char_u *)"priority", -1) != NULL
10133 && dict_find(d, (char_u *)"id", -1) != NULL))
10134 {
10135 EMSG(_(e_invarg));
10136 return;
10137 }
10138 li = li->li_next;
10139 }
10140
10141 clear_matches(curwin);
10142 li = l->lv_first;
10143 while (li != NULL)
10144 {
10145 int i = 0;
10146 char_u buf[5];
10147 dictitem_T *di;
10148 char_u *group;
10149 int priority;
10150 int id;
10151 char_u *conceal;
10152
10153 d = li->li_tv.vval.v_dict;
10154 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
10155 {
10156 if (s == NULL)
10157 {
10158 s = list_alloc();
10159 if (s == NULL)
10160 return;
10161 }
10162
10163 /* match from matchaddpos() */
10164 for (i = 1; i < 9; i++)
10165 {
10166 sprintf((char *)buf, (char *)"pos%d", i);
10167 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
10168 {
10169 if (di->di_tv.v_type != VAR_LIST)
10170 return;
10171
10172 list_append_tv(s, &di->di_tv);
10173 s->lv_refcount++;
10174 }
10175 else
10176 break;
10177 }
10178 }
10179
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010180 group = get_dict_string(d, (char_u *)"group", TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010181 priority = (int)get_dict_number(d, (char_u *)"priority");
10182 id = (int)get_dict_number(d, (char_u *)"id");
10183 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010184 ? get_dict_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010185 : NULL;
10186 if (i == 0)
10187 {
10188 match_add(curwin, group,
10189 get_dict_string(d, (char_u *)"pattern", FALSE),
10190 priority, id, NULL, conceal);
10191 }
10192 else
10193 {
10194 match_add(curwin, group, NULL, priority, id, s, conceal);
10195 list_unref(s);
10196 s = NULL;
10197 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010198 vim_free(group);
10199 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010200
10201 li = li->li_next;
10202 }
10203 rettv->vval.v_number = 0;
10204 }
10205#endif
10206}
10207
10208/*
10209 * "setpos()" function
10210 */
10211 static void
10212f_setpos(typval_T *argvars, typval_T *rettv)
10213{
10214 pos_T pos;
10215 int fnum;
10216 char_u *name;
10217 colnr_T curswant = -1;
10218
10219 rettv->vval.v_number = -1;
10220 name = get_tv_string_chk(argvars);
10221 if (name != NULL)
10222 {
10223 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10224 {
10225 if (--pos.col < 0)
10226 pos.col = 0;
10227 if (name[0] == '.' && name[1] == NUL)
10228 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010229 /* set cursor; "fnum" is ignored */
10230 curwin->w_cursor = pos;
10231 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010232 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010233 curwin->w_curswant = curswant - 1;
10234 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010235 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010236 check_cursor();
10237 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010238 }
10239 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10240 {
10241 /* set mark */
10242 if (setmark_pos(name[1], &pos, fnum) == OK)
10243 rettv->vval.v_number = 0;
10244 }
10245 else
10246 EMSG(_(e_invarg));
10247 }
10248 }
10249}
10250
10251/*
10252 * "setqflist()" function
10253 */
10254 static void
10255f_setqflist(typval_T *argvars, typval_T *rettv)
10256{
Bram Moolenaard823fa92016-08-12 16:29:27 +020010257 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010258}
10259
10260/*
10261 * "setreg()" function
10262 */
10263 static void
10264f_setreg(typval_T *argvars, typval_T *rettv)
10265{
10266 int regname;
10267 char_u *strregname;
10268 char_u *stropt;
10269 char_u *strval;
10270 int append;
10271 char_u yank_type;
10272 long block_len;
10273
10274 block_len = -1;
10275 yank_type = MAUTO;
10276 append = FALSE;
10277
10278 strregname = get_tv_string_chk(argvars);
10279 rettv->vval.v_number = 1; /* FAIL is default */
10280
10281 if (strregname == NULL)
10282 return; /* type error; errmsg already given */
10283 regname = *strregname;
10284 if (regname == 0 || regname == '@')
10285 regname = '"';
10286
10287 if (argvars[2].v_type != VAR_UNKNOWN)
10288 {
10289 stropt = get_tv_string_chk(&argvars[2]);
10290 if (stropt == NULL)
10291 return; /* type error */
10292 for (; *stropt != NUL; ++stropt)
10293 switch (*stropt)
10294 {
10295 case 'a': case 'A': /* append */
10296 append = TRUE;
10297 break;
10298 case 'v': case 'c': /* character-wise selection */
10299 yank_type = MCHAR;
10300 break;
10301 case 'V': case 'l': /* line-wise selection */
10302 yank_type = MLINE;
10303 break;
10304 case 'b': case Ctrl_V: /* block-wise selection */
10305 yank_type = MBLOCK;
10306 if (VIM_ISDIGIT(stropt[1]))
10307 {
10308 ++stropt;
10309 block_len = getdigits(&stropt) - 1;
10310 --stropt;
10311 }
10312 break;
10313 }
10314 }
10315
10316 if (argvars[1].v_type == VAR_LIST)
10317 {
10318 char_u **lstval;
10319 char_u **allocval;
10320 char_u buf[NUMBUFLEN];
10321 char_u **curval;
10322 char_u **curallocval;
10323 list_T *ll = argvars[1].vval.v_list;
10324 listitem_T *li;
10325 int len;
10326
10327 /* If the list is NULL handle like an empty list. */
10328 len = ll == NULL ? 0 : ll->lv_len;
10329
10330 /* First half: use for pointers to result lines; second half: use for
10331 * pointers to allocated copies. */
10332 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
10333 if (lstval == NULL)
10334 return;
10335 curval = lstval;
10336 allocval = lstval + len + 2;
10337 curallocval = allocval;
10338
10339 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
10340 li = li->li_next)
10341 {
10342 strval = get_tv_string_buf_chk(&li->li_tv, buf);
10343 if (strval == NULL)
10344 goto free_lstval;
10345 if (strval == buf)
10346 {
10347 /* Need to make a copy, next get_tv_string_buf_chk() will
10348 * overwrite the string. */
10349 strval = vim_strsave(buf);
10350 if (strval == NULL)
10351 goto free_lstval;
10352 *curallocval++ = strval;
10353 }
10354 *curval++ = strval;
10355 }
10356 *curval++ = NULL;
10357
10358 write_reg_contents_lst(regname, lstval, -1,
10359 append, yank_type, block_len);
10360free_lstval:
10361 while (curallocval > allocval)
10362 vim_free(*--curallocval);
10363 vim_free(lstval);
10364 }
10365 else
10366 {
10367 strval = get_tv_string_chk(&argvars[1]);
10368 if (strval == NULL)
10369 return;
10370 write_reg_contents_ex(regname, strval, -1,
10371 append, yank_type, block_len);
10372 }
10373 rettv->vval.v_number = 0;
10374}
10375
10376/*
10377 * "settabvar()" function
10378 */
10379 static void
10380f_settabvar(typval_T *argvars, typval_T *rettv)
10381{
10382#ifdef FEAT_WINDOWS
10383 tabpage_T *save_curtab;
10384 tabpage_T *tp;
10385#endif
10386 char_u *varname, *tabvarname;
10387 typval_T *varp;
10388
10389 rettv->vval.v_number = 0;
10390
10391 if (check_restricted() || check_secure())
10392 return;
10393
10394#ifdef FEAT_WINDOWS
10395 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
10396#endif
10397 varname = get_tv_string_chk(&argvars[1]);
10398 varp = &argvars[2];
10399
10400 if (varname != NULL && varp != NULL
10401#ifdef FEAT_WINDOWS
10402 && tp != NULL
10403#endif
10404 )
10405 {
10406#ifdef FEAT_WINDOWS
10407 save_curtab = curtab;
10408 goto_tabpage_tp(tp, FALSE, FALSE);
10409#endif
10410
10411 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
10412 if (tabvarname != NULL)
10413 {
10414 STRCPY(tabvarname, "t:");
10415 STRCPY(tabvarname + 2, varname);
10416 set_var(tabvarname, varp, TRUE);
10417 vim_free(tabvarname);
10418 }
10419
10420#ifdef FEAT_WINDOWS
10421 /* Restore current tabpage */
10422 if (valid_tabpage(save_curtab))
10423 goto_tabpage_tp(save_curtab, FALSE, FALSE);
10424#endif
10425 }
10426}
10427
10428/*
10429 * "settabwinvar()" function
10430 */
10431 static void
10432f_settabwinvar(typval_T *argvars, typval_T *rettv)
10433{
10434 setwinvar(argvars, rettv, 1);
10435}
10436
10437/*
10438 * "setwinvar()" function
10439 */
10440 static void
10441f_setwinvar(typval_T *argvars, typval_T *rettv)
10442{
10443 setwinvar(argvars, rettv, 0);
10444}
10445
10446#ifdef FEAT_CRYPT
10447/*
10448 * "sha256({string})" function
10449 */
10450 static void
10451f_sha256(typval_T *argvars, typval_T *rettv)
10452{
10453 char_u *p;
10454
10455 p = get_tv_string(&argvars[0]);
10456 rettv->vval.v_string = vim_strsave(
10457 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
10458 rettv->v_type = VAR_STRING;
10459}
10460#endif /* FEAT_CRYPT */
10461
10462/*
10463 * "shellescape({string})" function
10464 */
10465 static void
10466f_shellescape(typval_T *argvars, typval_T *rettv)
10467{
Bram Moolenaar20615522017-06-05 18:46:26 +020010468 int do_special = non_zero_arg(&argvars[1]);
10469
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010470 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar20615522017-06-05 18:46:26 +020010471 get_tv_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010472 rettv->v_type = VAR_STRING;
10473}
10474
10475/*
10476 * shiftwidth() function
10477 */
10478 static void
10479f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
10480{
10481 rettv->vval.v_number = get_sw_value(curbuf);
10482}
10483
10484/*
10485 * "simplify()" function
10486 */
10487 static void
10488f_simplify(typval_T *argvars, typval_T *rettv)
10489{
10490 char_u *p;
10491
10492 p = get_tv_string(&argvars[0]);
10493 rettv->vval.v_string = vim_strsave(p);
10494 simplify_filename(rettv->vval.v_string); /* simplify in place */
10495 rettv->v_type = VAR_STRING;
10496}
10497
10498#ifdef FEAT_FLOAT
10499/*
10500 * "sin()" function
10501 */
10502 static void
10503f_sin(typval_T *argvars, typval_T *rettv)
10504{
10505 float_T f = 0.0;
10506
10507 rettv->v_type = VAR_FLOAT;
10508 if (get_float_arg(argvars, &f) == OK)
10509 rettv->vval.v_float = sin(f);
10510 else
10511 rettv->vval.v_float = 0.0;
10512}
10513
10514/*
10515 * "sinh()" function
10516 */
10517 static void
10518f_sinh(typval_T *argvars, typval_T *rettv)
10519{
10520 float_T f = 0.0;
10521
10522 rettv->v_type = VAR_FLOAT;
10523 if (get_float_arg(argvars, &f) == OK)
10524 rettv->vval.v_float = sinh(f);
10525 else
10526 rettv->vval.v_float = 0.0;
10527}
10528#endif
10529
10530static int
10531#ifdef __BORLANDC__
10532 _RTLENTRYF
10533#endif
10534 item_compare(const void *s1, const void *s2);
10535static int
10536#ifdef __BORLANDC__
10537 _RTLENTRYF
10538#endif
10539 item_compare2(const void *s1, const void *s2);
10540
10541/* struct used in the array that's given to qsort() */
10542typedef struct
10543{
10544 listitem_T *item;
10545 int idx;
10546} sortItem_T;
10547
10548/* struct storing information about current sort */
10549typedef struct
10550{
10551 int item_compare_ic;
10552 int item_compare_numeric;
10553 int item_compare_numbers;
10554#ifdef FEAT_FLOAT
10555 int item_compare_float;
10556#endif
10557 char_u *item_compare_func;
10558 partial_T *item_compare_partial;
10559 dict_T *item_compare_selfdict;
10560 int item_compare_func_err;
10561 int item_compare_keep_zero;
10562} sortinfo_T;
10563static sortinfo_T *sortinfo = NULL;
10564static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
10565#define ITEM_COMPARE_FAIL 999
10566
10567/*
10568 * Compare functions for f_sort() and f_uniq() below.
10569 */
10570 static int
10571#ifdef __BORLANDC__
10572_RTLENTRYF
10573#endif
10574item_compare(const void *s1, const void *s2)
10575{
10576 sortItem_T *si1, *si2;
10577 typval_T *tv1, *tv2;
10578 char_u *p1, *p2;
10579 char_u *tofree1 = NULL, *tofree2 = NULL;
10580 int res;
10581 char_u numbuf1[NUMBUFLEN];
10582 char_u numbuf2[NUMBUFLEN];
10583
10584 si1 = (sortItem_T *)s1;
10585 si2 = (sortItem_T *)s2;
10586 tv1 = &si1->item->li_tv;
10587 tv2 = &si2->item->li_tv;
10588
10589 if (sortinfo->item_compare_numbers)
10590 {
10591 varnumber_T v1 = get_tv_number(tv1);
10592 varnumber_T v2 = get_tv_number(tv2);
10593
10594 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10595 }
10596
10597#ifdef FEAT_FLOAT
10598 if (sortinfo->item_compare_float)
10599 {
10600 float_T v1 = get_tv_float(tv1);
10601 float_T v2 = get_tv_float(tv2);
10602
10603 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10604 }
10605#endif
10606
10607 /* tv2string() puts quotes around a string and allocates memory. Don't do
10608 * that for string variables. Use a single quote when comparing with a
10609 * non-string to do what the docs promise. */
10610 if (tv1->v_type == VAR_STRING)
10611 {
10612 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
10613 p1 = (char_u *)"'";
10614 else
10615 p1 = tv1->vval.v_string;
10616 }
10617 else
10618 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
10619 if (tv2->v_type == VAR_STRING)
10620 {
10621 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
10622 p2 = (char_u *)"'";
10623 else
10624 p2 = tv2->vval.v_string;
10625 }
10626 else
10627 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
10628 if (p1 == NULL)
10629 p1 = (char_u *)"";
10630 if (p2 == NULL)
10631 p2 = (char_u *)"";
10632 if (!sortinfo->item_compare_numeric)
10633 {
10634 if (sortinfo->item_compare_ic)
10635 res = STRICMP(p1, p2);
10636 else
10637 res = STRCMP(p1, p2);
10638 }
10639 else
10640 {
10641 double n1, n2;
10642 n1 = strtod((char *)p1, (char **)&p1);
10643 n2 = strtod((char *)p2, (char **)&p2);
10644 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
10645 }
10646
10647 /* When the result would be zero, compare the item indexes. Makes the
10648 * sort stable. */
10649 if (res == 0 && !sortinfo->item_compare_keep_zero)
10650 res = si1->idx > si2->idx ? 1 : -1;
10651
10652 vim_free(tofree1);
10653 vim_free(tofree2);
10654 return res;
10655}
10656
10657 static int
10658#ifdef __BORLANDC__
10659_RTLENTRYF
10660#endif
10661item_compare2(const void *s1, const void *s2)
10662{
10663 sortItem_T *si1, *si2;
10664 int res;
10665 typval_T rettv;
10666 typval_T argv[3];
10667 int dummy;
10668 char_u *func_name;
10669 partial_T *partial = sortinfo->item_compare_partial;
10670
10671 /* shortcut after failure in previous call; compare all items equal */
10672 if (sortinfo->item_compare_func_err)
10673 return 0;
10674
10675 si1 = (sortItem_T *)s1;
10676 si2 = (sortItem_T *)s2;
10677
10678 if (partial == NULL)
10679 func_name = sortinfo->item_compare_func;
10680 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020010681 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010682
10683 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
10684 * in the copy without changing the original list items. */
10685 copy_tv(&si1->item->li_tv, &argv[0]);
10686 copy_tv(&si2->item->li_tv, &argv[1]);
10687
10688 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
10689 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020010690 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010691 partial, sortinfo->item_compare_selfdict);
10692 clear_tv(&argv[0]);
10693 clear_tv(&argv[1]);
10694
10695 if (res == FAIL)
10696 res = ITEM_COMPARE_FAIL;
10697 else
10698 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
10699 if (sortinfo->item_compare_func_err)
10700 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
10701 clear_tv(&rettv);
10702
10703 /* When the result would be zero, compare the pointers themselves. Makes
10704 * the sort stable. */
10705 if (res == 0 && !sortinfo->item_compare_keep_zero)
10706 res = si1->idx > si2->idx ? 1 : -1;
10707
10708 return res;
10709}
10710
10711/*
10712 * "sort({list})" function
10713 */
10714 static void
10715do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
10716{
10717 list_T *l;
10718 listitem_T *li;
10719 sortItem_T *ptrs;
10720 sortinfo_T *old_sortinfo;
10721 sortinfo_T info;
10722 long len;
10723 long i;
10724
10725 /* Pointer to current info struct used in compare function. Save and
10726 * restore the current one for nested calls. */
10727 old_sortinfo = sortinfo;
10728 sortinfo = &info;
10729
10730 if (argvars[0].v_type != VAR_LIST)
10731 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
10732 else
10733 {
10734 l = argvars[0].vval.v_list;
10735 if (l == NULL || tv_check_lock(l->lv_lock,
10736 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
10737 TRUE))
10738 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020010739 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010740
10741 len = list_len(l);
10742 if (len <= 1)
10743 goto theend; /* short list sorts pretty quickly */
10744
10745 info.item_compare_ic = FALSE;
10746 info.item_compare_numeric = FALSE;
10747 info.item_compare_numbers = FALSE;
10748#ifdef FEAT_FLOAT
10749 info.item_compare_float = FALSE;
10750#endif
10751 info.item_compare_func = NULL;
10752 info.item_compare_partial = NULL;
10753 info.item_compare_selfdict = NULL;
10754 if (argvars[1].v_type != VAR_UNKNOWN)
10755 {
10756 /* optional second argument: {func} */
10757 if (argvars[1].v_type == VAR_FUNC)
10758 info.item_compare_func = argvars[1].vval.v_string;
10759 else if (argvars[1].v_type == VAR_PARTIAL)
10760 info.item_compare_partial = argvars[1].vval.v_partial;
10761 else
10762 {
10763 int error = FALSE;
10764
10765 i = (long)get_tv_number_chk(&argvars[1], &error);
10766 if (error)
10767 goto theend; /* type error; errmsg already given */
10768 if (i == 1)
10769 info.item_compare_ic = TRUE;
10770 else if (argvars[1].v_type != VAR_NUMBER)
10771 info.item_compare_func = get_tv_string(&argvars[1]);
10772 else if (i != 0)
10773 {
10774 EMSG(_(e_invarg));
10775 goto theend;
10776 }
10777 if (info.item_compare_func != NULL)
10778 {
10779 if (*info.item_compare_func == NUL)
10780 {
10781 /* empty string means default sort */
10782 info.item_compare_func = NULL;
10783 }
10784 else if (STRCMP(info.item_compare_func, "n") == 0)
10785 {
10786 info.item_compare_func = NULL;
10787 info.item_compare_numeric = TRUE;
10788 }
10789 else if (STRCMP(info.item_compare_func, "N") == 0)
10790 {
10791 info.item_compare_func = NULL;
10792 info.item_compare_numbers = TRUE;
10793 }
10794#ifdef FEAT_FLOAT
10795 else if (STRCMP(info.item_compare_func, "f") == 0)
10796 {
10797 info.item_compare_func = NULL;
10798 info.item_compare_float = TRUE;
10799 }
10800#endif
10801 else if (STRCMP(info.item_compare_func, "i") == 0)
10802 {
10803 info.item_compare_func = NULL;
10804 info.item_compare_ic = TRUE;
10805 }
10806 }
10807 }
10808
10809 if (argvars[2].v_type != VAR_UNKNOWN)
10810 {
10811 /* optional third argument: {dict} */
10812 if (argvars[2].v_type != VAR_DICT)
10813 {
10814 EMSG(_(e_dictreq));
10815 goto theend;
10816 }
10817 info.item_compare_selfdict = argvars[2].vval.v_dict;
10818 }
10819 }
10820
10821 /* Make an array with each entry pointing to an item in the List. */
10822 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
10823 if (ptrs == NULL)
10824 goto theend;
10825
10826 i = 0;
10827 if (sort)
10828 {
10829 /* sort(): ptrs will be the list to sort */
10830 for (li = l->lv_first; li != NULL; li = li->li_next)
10831 {
10832 ptrs[i].item = li;
10833 ptrs[i].idx = i;
10834 ++i;
10835 }
10836
10837 info.item_compare_func_err = FALSE;
10838 info.item_compare_keep_zero = FALSE;
10839 /* test the compare function */
10840 if ((info.item_compare_func != NULL
10841 || info.item_compare_partial != NULL)
10842 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
10843 == ITEM_COMPARE_FAIL)
10844 EMSG(_("E702: Sort compare function failed"));
10845 else
10846 {
10847 /* Sort the array with item pointers. */
10848 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
10849 info.item_compare_func == NULL
10850 && info.item_compare_partial == NULL
10851 ? item_compare : item_compare2);
10852
10853 if (!info.item_compare_func_err)
10854 {
10855 /* Clear the List and append the items in sorted order. */
10856 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
10857 l->lv_len = 0;
10858 for (i = 0; i < len; ++i)
10859 list_append(l, ptrs[i].item);
10860 }
10861 }
10862 }
10863 else
10864 {
10865 int (*item_compare_func_ptr)(const void *, const void *);
10866
10867 /* f_uniq(): ptrs will be a stack of items to remove */
10868 info.item_compare_func_err = FALSE;
10869 info.item_compare_keep_zero = TRUE;
10870 item_compare_func_ptr = info.item_compare_func != NULL
10871 || info.item_compare_partial != NULL
10872 ? item_compare2 : item_compare;
10873
10874 for (li = l->lv_first; li != NULL && li->li_next != NULL;
10875 li = li->li_next)
10876 {
10877 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
10878 == 0)
10879 ptrs[i++].item = li;
10880 if (info.item_compare_func_err)
10881 {
10882 EMSG(_("E882: Uniq compare function failed"));
10883 break;
10884 }
10885 }
10886
10887 if (!info.item_compare_func_err)
10888 {
10889 while (--i >= 0)
10890 {
10891 li = ptrs[i].item->li_next;
10892 ptrs[i].item->li_next = li->li_next;
10893 if (li->li_next != NULL)
10894 li->li_next->li_prev = ptrs[i].item;
10895 else
10896 l->lv_last = ptrs[i].item;
10897 list_fix_watch(l, li);
10898 listitem_free(li);
10899 l->lv_len--;
10900 }
10901 }
10902 }
10903
10904 vim_free(ptrs);
10905 }
10906theend:
10907 sortinfo = old_sortinfo;
10908}
10909
10910/*
10911 * "sort({list})" function
10912 */
10913 static void
10914f_sort(typval_T *argvars, typval_T *rettv)
10915{
10916 do_sort_uniq(argvars, rettv, TRUE);
10917}
10918
10919/*
10920 * "uniq({list})" function
10921 */
10922 static void
10923f_uniq(typval_T *argvars, typval_T *rettv)
10924{
10925 do_sort_uniq(argvars, rettv, FALSE);
10926}
10927
10928/*
10929 * "soundfold({word})" function
10930 */
10931 static void
10932f_soundfold(typval_T *argvars, typval_T *rettv)
10933{
10934 char_u *s;
10935
10936 rettv->v_type = VAR_STRING;
10937 s = get_tv_string(&argvars[0]);
10938#ifdef FEAT_SPELL
10939 rettv->vval.v_string = eval_soundfold(s);
10940#else
10941 rettv->vval.v_string = vim_strsave(s);
10942#endif
10943}
10944
10945/*
10946 * "spellbadword()" function
10947 */
10948 static void
10949f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
10950{
10951 char_u *word = (char_u *)"";
10952 hlf_T attr = HLF_COUNT;
10953 int len = 0;
10954
10955 if (rettv_list_alloc(rettv) == FAIL)
10956 return;
10957
10958#ifdef FEAT_SPELL
10959 if (argvars[0].v_type == VAR_UNKNOWN)
10960 {
10961 /* Find the start and length of the badly spelled word. */
10962 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
10963 if (len != 0)
10964 word = ml_get_cursor();
10965 }
10966 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
10967 {
10968 char_u *str = get_tv_string_chk(&argvars[0]);
10969 int capcol = -1;
10970
10971 if (str != NULL)
10972 {
10973 /* Check the argument for spelling. */
10974 while (*str != NUL)
10975 {
10976 len = spell_check(curwin, str, &attr, &capcol, FALSE);
10977 if (attr != HLF_COUNT)
10978 {
10979 word = str;
10980 break;
10981 }
10982 str += len;
10983 }
10984 }
10985 }
10986#endif
10987
10988 list_append_string(rettv->vval.v_list, word, len);
10989 list_append_string(rettv->vval.v_list, (char_u *)(
10990 attr == HLF_SPB ? "bad" :
10991 attr == HLF_SPR ? "rare" :
10992 attr == HLF_SPL ? "local" :
10993 attr == HLF_SPC ? "caps" :
10994 ""), -1);
10995}
10996
10997/*
10998 * "spellsuggest()" function
10999 */
11000 static void
11001f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
11002{
11003#ifdef FEAT_SPELL
11004 char_u *str;
11005 int typeerr = FALSE;
11006 int maxcount;
11007 garray_T ga;
11008 int i;
11009 listitem_T *li;
11010 int need_capital = FALSE;
11011#endif
11012
11013 if (rettv_list_alloc(rettv) == FAIL)
11014 return;
11015
11016#ifdef FEAT_SPELL
11017 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
11018 {
11019 str = get_tv_string(&argvars[0]);
11020 if (argvars[1].v_type != VAR_UNKNOWN)
11021 {
11022 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
11023 if (maxcount <= 0)
11024 return;
11025 if (argvars[2].v_type != VAR_UNKNOWN)
11026 {
11027 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
11028 if (typeerr)
11029 return;
11030 }
11031 }
11032 else
11033 maxcount = 25;
11034
11035 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
11036
11037 for (i = 0; i < ga.ga_len; ++i)
11038 {
11039 str = ((char_u **)ga.ga_data)[i];
11040
11041 li = listitem_alloc();
11042 if (li == NULL)
11043 vim_free(str);
11044 else
11045 {
11046 li->li_tv.v_type = VAR_STRING;
11047 li->li_tv.v_lock = 0;
11048 li->li_tv.vval.v_string = str;
11049 list_append(rettv->vval.v_list, li);
11050 }
11051 }
11052 ga_clear(&ga);
11053 }
11054#endif
11055}
11056
11057 static void
11058f_split(typval_T *argvars, typval_T *rettv)
11059{
11060 char_u *str;
11061 char_u *end;
11062 char_u *pat = NULL;
11063 regmatch_T regmatch;
11064 char_u patbuf[NUMBUFLEN];
11065 char_u *save_cpo;
11066 int match;
11067 colnr_T col = 0;
11068 int keepempty = FALSE;
11069 int typeerr = FALSE;
11070
11071 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11072 save_cpo = p_cpo;
11073 p_cpo = (char_u *)"";
11074
11075 str = get_tv_string(&argvars[0]);
11076 if (argvars[1].v_type != VAR_UNKNOWN)
11077 {
11078 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11079 if (pat == NULL)
11080 typeerr = TRUE;
11081 if (argvars[2].v_type != VAR_UNKNOWN)
11082 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
11083 }
11084 if (pat == NULL || *pat == NUL)
11085 pat = (char_u *)"[\\x01- ]\\+";
11086
11087 if (rettv_list_alloc(rettv) == FAIL)
11088 return;
11089 if (typeerr)
11090 return;
11091
11092 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11093 if (regmatch.regprog != NULL)
11094 {
11095 regmatch.rm_ic = FALSE;
11096 while (*str != NUL || keepempty)
11097 {
11098 if (*str == NUL)
11099 match = FALSE; /* empty item at the end */
11100 else
11101 match = vim_regexec_nl(&regmatch, str, col);
11102 if (match)
11103 end = regmatch.startp[0];
11104 else
11105 end = str + STRLEN(str);
11106 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
11107 && *str != NUL && match && end < regmatch.endp[0]))
11108 {
11109 if (list_append_string(rettv->vval.v_list, str,
11110 (int)(end - str)) == FAIL)
11111 break;
11112 }
11113 if (!match)
11114 break;
11115 /* Advance to just after the match. */
11116 if (regmatch.endp[0] > str)
11117 col = 0;
11118 else
11119 {
11120 /* Don't get stuck at the same match. */
11121#ifdef FEAT_MBYTE
11122 col = (*mb_ptr2len)(regmatch.endp[0]);
11123#else
11124 col = 1;
11125#endif
11126 }
11127 str = regmatch.endp[0];
11128 }
11129
11130 vim_regfree(regmatch.regprog);
11131 }
11132
11133 p_cpo = save_cpo;
11134}
11135
11136#ifdef FEAT_FLOAT
11137/*
11138 * "sqrt()" function
11139 */
11140 static void
11141f_sqrt(typval_T *argvars, typval_T *rettv)
11142{
11143 float_T f = 0.0;
11144
11145 rettv->v_type = VAR_FLOAT;
11146 if (get_float_arg(argvars, &f) == OK)
11147 rettv->vval.v_float = sqrt(f);
11148 else
11149 rettv->vval.v_float = 0.0;
11150}
11151
11152/*
11153 * "str2float()" function
11154 */
11155 static void
11156f_str2float(typval_T *argvars, typval_T *rettv)
11157{
11158 char_u *p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011159 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011160
Bram Moolenaar08243d22017-01-10 16:12:29 +010011161 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011162 p = skipwhite(p + 1);
11163 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011164 if (isneg)
11165 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011166 rettv->v_type = VAR_FLOAT;
11167}
11168#endif
11169
11170/*
11171 * "str2nr()" function
11172 */
11173 static void
11174f_str2nr(typval_T *argvars, typval_T *rettv)
11175{
11176 int base = 10;
11177 char_u *p;
11178 varnumber_T n;
11179 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010011180 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011181
11182 if (argvars[1].v_type != VAR_UNKNOWN)
11183 {
11184 base = (int)get_tv_number(&argvars[1]);
11185 if (base != 2 && base != 8 && base != 10 && base != 16)
11186 {
11187 EMSG(_(e_invarg));
11188 return;
11189 }
11190 }
11191
11192 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011193 isneg = (*p == '-');
11194 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011195 p = skipwhite(p + 1);
11196 switch (base)
11197 {
11198 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
11199 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
11200 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
11201 default: what = 0;
11202 }
11203 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011204 if (isneg)
11205 rettv->vval.v_number = -n;
11206 else
11207 rettv->vval.v_number = n;
11208
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011209}
11210
11211#ifdef HAVE_STRFTIME
11212/*
11213 * "strftime({format}[, {time}])" function
11214 */
11215 static void
11216f_strftime(typval_T *argvars, typval_T *rettv)
11217{
11218 char_u result_buf[256];
11219 struct tm *curtime;
11220 time_t seconds;
11221 char_u *p;
11222
11223 rettv->v_type = VAR_STRING;
11224
11225 p = get_tv_string(&argvars[0]);
11226 if (argvars[1].v_type == VAR_UNKNOWN)
11227 seconds = time(NULL);
11228 else
11229 seconds = (time_t)get_tv_number(&argvars[1]);
11230 curtime = localtime(&seconds);
11231 /* MSVC returns NULL for an invalid value of seconds. */
11232 if (curtime == NULL)
11233 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
11234 else
11235 {
11236# ifdef FEAT_MBYTE
11237 vimconv_T conv;
11238 char_u *enc;
11239
11240 conv.vc_type = CONV_NONE;
11241 enc = enc_locale();
11242 convert_setup(&conv, p_enc, enc);
11243 if (conv.vc_type != CONV_NONE)
11244 p = string_convert(&conv, p, NULL);
11245# endif
11246 if (p != NULL)
11247 (void)strftime((char *)result_buf, sizeof(result_buf),
11248 (char *)p, curtime);
11249 else
11250 result_buf[0] = NUL;
11251
11252# ifdef FEAT_MBYTE
11253 if (conv.vc_type != CONV_NONE)
11254 vim_free(p);
11255 convert_setup(&conv, enc, p_enc);
11256 if (conv.vc_type != CONV_NONE)
11257 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
11258 else
11259# endif
11260 rettv->vval.v_string = vim_strsave(result_buf);
11261
11262# ifdef FEAT_MBYTE
11263 /* Release conversion descriptors */
11264 convert_setup(&conv, NULL, NULL);
11265 vim_free(enc);
11266# endif
11267 }
11268}
11269#endif
11270
11271/*
11272 * "strgetchar()" function
11273 */
11274 static void
11275f_strgetchar(typval_T *argvars, typval_T *rettv)
11276{
11277 char_u *str;
11278 int len;
11279 int error = FALSE;
11280 int charidx;
11281
11282 rettv->vval.v_number = -1;
11283 str = get_tv_string_chk(&argvars[0]);
11284 if (str == NULL)
11285 return;
11286 len = (int)STRLEN(str);
11287 charidx = (int)get_tv_number_chk(&argvars[1], &error);
11288 if (error)
11289 return;
11290#ifdef FEAT_MBYTE
11291 {
11292 int byteidx = 0;
11293
11294 while (charidx >= 0 && byteidx < len)
11295 {
11296 if (charidx == 0)
11297 {
11298 rettv->vval.v_number = mb_ptr2char(str + byteidx);
11299 break;
11300 }
11301 --charidx;
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011302 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011303 }
11304 }
11305#else
11306 if (charidx < len)
11307 rettv->vval.v_number = str[charidx];
11308#endif
11309}
11310
11311/*
11312 * "stridx()" function
11313 */
11314 static void
11315f_stridx(typval_T *argvars, typval_T *rettv)
11316{
11317 char_u buf[NUMBUFLEN];
11318 char_u *needle;
11319 char_u *haystack;
11320 char_u *save_haystack;
11321 char_u *pos;
11322 int start_idx;
11323
11324 needle = get_tv_string_chk(&argvars[1]);
11325 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
11326 rettv->vval.v_number = -1;
11327 if (needle == NULL || haystack == NULL)
11328 return; /* type error; errmsg already given */
11329
11330 if (argvars[2].v_type != VAR_UNKNOWN)
11331 {
11332 int error = FALSE;
11333
11334 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
11335 if (error || start_idx >= (int)STRLEN(haystack))
11336 return;
11337 if (start_idx >= 0)
11338 haystack += start_idx;
11339 }
11340
11341 pos = (char_u *)strstr((char *)haystack, (char *)needle);
11342 if (pos != NULL)
11343 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
11344}
11345
11346/*
11347 * "string()" function
11348 */
11349 static void
11350f_string(typval_T *argvars, typval_T *rettv)
11351{
11352 char_u *tofree;
11353 char_u numbuf[NUMBUFLEN];
11354
11355 rettv->v_type = VAR_STRING;
11356 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
11357 get_copyID());
11358 /* Make a copy if we have a value but it's not in allocated memory. */
11359 if (rettv->vval.v_string != NULL && tofree == NULL)
11360 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
11361}
11362
11363/*
11364 * "strlen()" function
11365 */
11366 static void
11367f_strlen(typval_T *argvars, typval_T *rettv)
11368{
11369 rettv->vval.v_number = (varnumber_T)(STRLEN(
11370 get_tv_string(&argvars[0])));
11371}
11372
11373/*
11374 * "strchars()" function
11375 */
11376 static void
11377f_strchars(typval_T *argvars, typval_T *rettv)
11378{
11379 char_u *s = get_tv_string(&argvars[0]);
11380 int skipcc = 0;
11381#ifdef FEAT_MBYTE
11382 varnumber_T len = 0;
11383 int (*func_mb_ptr2char_adv)(char_u **pp);
11384#endif
11385
11386 if (argvars[1].v_type != VAR_UNKNOWN)
11387 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
11388 if (skipcc < 0 || skipcc > 1)
11389 EMSG(_(e_invarg));
11390 else
11391 {
11392#ifdef FEAT_MBYTE
11393 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
11394 while (*s != NUL)
11395 {
11396 func_mb_ptr2char_adv(&s);
11397 ++len;
11398 }
11399 rettv->vval.v_number = len;
11400#else
11401 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
11402#endif
11403 }
11404}
11405
11406/*
11407 * "strdisplaywidth()" function
11408 */
11409 static void
11410f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
11411{
11412 char_u *s = get_tv_string(&argvars[0]);
11413 int col = 0;
11414
11415 if (argvars[1].v_type != VAR_UNKNOWN)
11416 col = (int)get_tv_number(&argvars[1]);
11417
11418 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
11419}
11420
11421/*
11422 * "strwidth()" function
11423 */
11424 static void
11425f_strwidth(typval_T *argvars, typval_T *rettv)
11426{
11427 char_u *s = get_tv_string(&argvars[0]);
11428
11429 rettv->vval.v_number = (varnumber_T)(
11430#ifdef FEAT_MBYTE
11431 mb_string2cells(s, -1)
11432#else
11433 STRLEN(s)
11434#endif
11435 );
11436}
11437
11438/*
11439 * "strcharpart()" function
11440 */
11441 static void
11442f_strcharpart(typval_T *argvars, typval_T *rettv)
11443{
11444#ifdef FEAT_MBYTE
11445 char_u *p;
11446 int nchar;
11447 int nbyte = 0;
11448 int charlen;
11449 int len = 0;
11450 int slen;
11451 int error = FALSE;
11452
11453 p = get_tv_string(&argvars[0]);
11454 slen = (int)STRLEN(p);
11455
11456 nchar = (int)get_tv_number_chk(&argvars[1], &error);
11457 if (!error)
11458 {
11459 if (nchar > 0)
11460 while (nchar > 0 && nbyte < slen)
11461 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011462 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011463 --nchar;
11464 }
11465 else
11466 nbyte = nchar;
11467 if (argvars[2].v_type != VAR_UNKNOWN)
11468 {
11469 charlen = (int)get_tv_number(&argvars[2]);
11470 while (charlen > 0 && nbyte + len < slen)
11471 {
11472 int off = nbyte + len;
11473
11474 if (off < 0)
11475 len += 1;
11476 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011477 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011478 --charlen;
11479 }
11480 }
11481 else
11482 len = slen - nbyte; /* default: all bytes that are available. */
11483 }
11484
11485 /*
11486 * Only return the overlap between the specified part and the actual
11487 * string.
11488 */
11489 if (nbyte < 0)
11490 {
11491 len += nbyte;
11492 nbyte = 0;
11493 }
11494 else if (nbyte > slen)
11495 nbyte = slen;
11496 if (len < 0)
11497 len = 0;
11498 else if (nbyte + len > slen)
11499 len = slen - nbyte;
11500
11501 rettv->v_type = VAR_STRING;
11502 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
11503#else
11504 f_strpart(argvars, rettv);
11505#endif
11506}
11507
11508/*
11509 * "strpart()" function
11510 */
11511 static void
11512f_strpart(typval_T *argvars, typval_T *rettv)
11513{
11514 char_u *p;
11515 int n;
11516 int len;
11517 int slen;
11518 int error = FALSE;
11519
11520 p = get_tv_string(&argvars[0]);
11521 slen = (int)STRLEN(p);
11522
11523 n = (int)get_tv_number_chk(&argvars[1], &error);
11524 if (error)
11525 len = 0;
11526 else if (argvars[2].v_type != VAR_UNKNOWN)
11527 len = (int)get_tv_number(&argvars[2]);
11528 else
11529 len = slen - n; /* default len: all bytes that are available. */
11530
11531 /*
11532 * Only return the overlap between the specified part and the actual
11533 * string.
11534 */
11535 if (n < 0)
11536 {
11537 len += n;
11538 n = 0;
11539 }
11540 else if (n > slen)
11541 n = slen;
11542 if (len < 0)
11543 len = 0;
11544 else if (n + len > slen)
11545 len = slen - n;
11546
11547 rettv->v_type = VAR_STRING;
11548 rettv->vval.v_string = vim_strnsave(p + n, len);
11549}
11550
11551/*
11552 * "strridx()" function
11553 */
11554 static void
11555f_strridx(typval_T *argvars, typval_T *rettv)
11556{
11557 char_u buf[NUMBUFLEN];
11558 char_u *needle;
11559 char_u *haystack;
11560 char_u *rest;
11561 char_u *lastmatch = NULL;
11562 int haystack_len, end_idx;
11563
11564 needle = get_tv_string_chk(&argvars[1]);
11565 haystack = get_tv_string_buf_chk(&argvars[0], buf);
11566
11567 rettv->vval.v_number = -1;
11568 if (needle == NULL || haystack == NULL)
11569 return; /* type error; errmsg already given */
11570
11571 haystack_len = (int)STRLEN(haystack);
11572 if (argvars[2].v_type != VAR_UNKNOWN)
11573 {
11574 /* Third argument: upper limit for index */
11575 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
11576 if (end_idx < 0)
11577 return; /* can never find a match */
11578 }
11579 else
11580 end_idx = haystack_len;
11581
11582 if (*needle == NUL)
11583 {
11584 /* Empty string matches past the end. */
11585 lastmatch = haystack + end_idx;
11586 }
11587 else
11588 {
11589 for (rest = haystack; *rest != '\0'; ++rest)
11590 {
11591 rest = (char_u *)strstr((char *)rest, (char *)needle);
11592 if (rest == NULL || rest > haystack + end_idx)
11593 break;
11594 lastmatch = rest;
11595 }
11596 }
11597
11598 if (lastmatch == NULL)
11599 rettv->vval.v_number = -1;
11600 else
11601 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
11602}
11603
11604/*
11605 * "strtrans()" function
11606 */
11607 static void
11608f_strtrans(typval_T *argvars, typval_T *rettv)
11609{
11610 rettv->v_type = VAR_STRING;
11611 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
11612}
11613
11614/*
11615 * "submatch()" function
11616 */
11617 static void
11618f_submatch(typval_T *argvars, typval_T *rettv)
11619{
11620 int error = FALSE;
11621 int no;
11622 int retList = 0;
11623
11624 no = (int)get_tv_number_chk(&argvars[0], &error);
11625 if (error)
11626 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020011627 if (no < 0 || no >= NSUBEXP)
11628 {
Bram Moolenaar79518e22017-02-17 16:31:35 +010011629 EMSGN(_("E935: invalid submatch number: %d"), no);
11630 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020011631 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011632 if (argvars[1].v_type != VAR_UNKNOWN)
11633 retList = (int)get_tv_number_chk(&argvars[1], &error);
11634 if (error)
11635 return;
11636
11637 if (retList == 0)
11638 {
11639 rettv->v_type = VAR_STRING;
11640 rettv->vval.v_string = reg_submatch(no);
11641 }
11642 else
11643 {
11644 rettv->v_type = VAR_LIST;
11645 rettv->vval.v_list = reg_submatch_list(no);
11646 }
11647}
11648
11649/*
11650 * "substitute()" function
11651 */
11652 static void
11653f_substitute(typval_T *argvars, typval_T *rettv)
11654{
11655 char_u patbuf[NUMBUFLEN];
11656 char_u subbuf[NUMBUFLEN];
11657 char_u flagsbuf[NUMBUFLEN];
11658
11659 char_u *str = get_tv_string_chk(&argvars[0]);
11660 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011661 char_u *sub = NULL;
11662 typval_T *expr = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011663 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
11664
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011665 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
11666 expr = &argvars[2];
11667 else
11668 sub = get_tv_string_buf_chk(&argvars[2], subbuf);
11669
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011670 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011671 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
11672 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011673 rettv->vval.v_string = NULL;
11674 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011675 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011676}
11677
11678/*
11679 * "synID(lnum, col, trans)" function
11680 */
11681 static void
11682f_synID(typval_T *argvars UNUSED, typval_T *rettv)
11683{
11684 int id = 0;
11685#ifdef FEAT_SYN_HL
11686 linenr_T lnum;
11687 colnr_T col;
11688 int trans;
11689 int transerr = FALSE;
11690
11691 lnum = get_tv_lnum(argvars); /* -1 on type error */
11692 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
11693 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
11694
11695 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11696 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
11697 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
11698#endif
11699
11700 rettv->vval.v_number = id;
11701}
11702
11703/*
11704 * "synIDattr(id, what [, mode])" function
11705 */
11706 static void
11707f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
11708{
11709 char_u *p = NULL;
11710#ifdef FEAT_SYN_HL
11711 int id;
11712 char_u *what;
11713 char_u *mode;
11714 char_u modebuf[NUMBUFLEN];
11715 int modec;
11716
11717 id = (int)get_tv_number(&argvars[0]);
11718 what = get_tv_string(&argvars[1]);
11719 if (argvars[2].v_type != VAR_UNKNOWN)
11720 {
11721 mode = get_tv_string_buf(&argvars[2], modebuf);
11722 modec = TOLOWER_ASC(mode[0]);
11723 if (modec != 't' && modec != 'c' && modec != 'g')
11724 modec = 0; /* replace invalid with current */
11725 }
11726 else
11727 {
11728#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
11729 if (USE_24BIT)
11730 modec = 'g';
11731 else
11732#endif
11733 if (t_colors > 1)
11734 modec = 'c';
11735 else
11736 modec = 't';
11737 }
11738
11739
11740 switch (TOLOWER_ASC(what[0]))
11741 {
11742 case 'b':
11743 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
11744 p = highlight_color(id, what, modec);
11745 else /* bold */
11746 p = highlight_has_attr(id, HL_BOLD, modec);
11747 break;
11748
11749 case 'f': /* fg[#] or font */
11750 p = highlight_color(id, what, modec);
11751 break;
11752
11753 case 'i':
11754 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
11755 p = highlight_has_attr(id, HL_INVERSE, modec);
11756 else /* italic */
11757 p = highlight_has_attr(id, HL_ITALIC, modec);
11758 break;
11759
11760 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020011761 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011762 break;
11763
11764 case 'r': /* reverse */
11765 p = highlight_has_attr(id, HL_INVERSE, modec);
11766 break;
11767
11768 case 's':
11769 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
11770 p = highlight_color(id, what, modec);
11771 else /* standout */
11772 p = highlight_has_attr(id, HL_STANDOUT, modec);
11773 break;
11774
11775 case 'u':
11776 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
11777 /* underline */
11778 p = highlight_has_attr(id, HL_UNDERLINE, modec);
11779 else
11780 /* undercurl */
11781 p = highlight_has_attr(id, HL_UNDERCURL, modec);
11782 break;
11783 }
11784
11785 if (p != NULL)
11786 p = vim_strsave(p);
11787#endif
11788 rettv->v_type = VAR_STRING;
11789 rettv->vval.v_string = p;
11790}
11791
11792/*
11793 * "synIDtrans(id)" function
11794 */
11795 static void
11796f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
11797{
11798 int id;
11799
11800#ifdef FEAT_SYN_HL
11801 id = (int)get_tv_number(&argvars[0]);
11802
11803 if (id > 0)
11804 id = syn_get_final_id(id);
11805 else
11806#endif
11807 id = 0;
11808
11809 rettv->vval.v_number = id;
11810}
11811
11812/*
11813 * "synconcealed(lnum, col)" function
11814 */
11815 static void
11816f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
11817{
11818#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
11819 linenr_T lnum;
11820 colnr_T col;
11821 int syntax_flags = 0;
11822 int cchar;
11823 int matchid = 0;
11824 char_u str[NUMBUFLEN];
11825#endif
11826
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011827 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011828
11829#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
11830 lnum = get_tv_lnum(argvars); /* -1 on type error */
11831 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
11832
11833 vim_memset(str, NUL, sizeof(str));
11834
11835 if (rettv_list_alloc(rettv) != FAIL)
11836 {
11837 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11838 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
11839 && curwin->w_p_cole > 0)
11840 {
11841 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
11842 syntax_flags = get_syntax_info(&matchid);
11843
11844 /* get the conceal character */
11845 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
11846 {
11847 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020011848 if (cchar == NUL && curwin->w_p_cole == 1)
11849 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011850 if (cchar != NUL)
11851 {
11852# ifdef FEAT_MBYTE
11853 if (has_mbyte)
11854 (*mb_char2bytes)(cchar, str);
11855 else
11856# endif
11857 str[0] = cchar;
11858 }
11859 }
11860 }
11861
11862 list_append_number(rettv->vval.v_list,
11863 (syntax_flags & HL_CONCEAL) != 0);
11864 /* -1 to auto-determine strlen */
11865 list_append_string(rettv->vval.v_list, str, -1);
11866 list_append_number(rettv->vval.v_list, matchid);
11867 }
11868#endif
11869}
11870
11871/*
11872 * "synstack(lnum, col)" function
11873 */
11874 static void
11875f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
11876{
11877#ifdef FEAT_SYN_HL
11878 linenr_T lnum;
11879 colnr_T col;
11880 int i;
11881 int id;
11882#endif
11883
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011884 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011885
11886#ifdef FEAT_SYN_HL
11887 lnum = get_tv_lnum(argvars); /* -1 on type error */
11888 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
11889
11890 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11891 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
11892 && rettv_list_alloc(rettv) != FAIL)
11893 {
11894 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
11895 for (i = 0; ; ++i)
11896 {
11897 id = syn_get_stack_item(i);
11898 if (id < 0)
11899 break;
11900 if (list_append_number(rettv->vval.v_list, id) == FAIL)
11901 break;
11902 }
11903 }
11904#endif
11905}
11906
11907 static void
11908get_cmd_output_as_rettv(
11909 typval_T *argvars,
11910 typval_T *rettv,
11911 int retlist)
11912{
11913 char_u *res = NULL;
11914 char_u *p;
11915 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011916 int err = FALSE;
11917 FILE *fd;
11918 list_T *list = NULL;
11919 int flags = SHELL_SILENT;
11920
11921 rettv->v_type = VAR_STRING;
11922 rettv->vval.v_string = NULL;
11923 if (check_restricted() || check_secure())
11924 goto errret;
11925
11926 if (argvars[1].v_type != VAR_UNKNOWN)
11927 {
11928 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010011929 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011930 * command.
11931 */
11932 if ((infile = vim_tempname('i', TRUE)) == NULL)
11933 {
11934 EMSG(_(e_notmp));
11935 goto errret;
11936 }
11937
11938 fd = mch_fopen((char *)infile, WRITEBIN);
11939 if (fd == NULL)
11940 {
11941 EMSG2(_(e_notopen), infile);
11942 goto errret;
11943 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010011944 if (argvars[1].v_type == VAR_NUMBER)
11945 {
11946 linenr_T lnum;
11947 buf_T *buf;
11948
11949 buf = buflist_findnr(argvars[1].vval.v_number);
11950 if (buf == NULL)
11951 {
11952 EMSGN(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010011953 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010011954 goto errret;
11955 }
11956
11957 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
11958 {
11959 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
11960 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
11961 {
11962 err = TRUE;
11963 break;
11964 }
11965 if (putc(NL, fd) == EOF)
11966 {
11967 err = TRUE;
11968 break;
11969 }
11970 }
11971 }
11972 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011973 {
11974 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
11975 err = TRUE;
11976 }
11977 else
11978 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010011979 size_t len;
11980 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011981
11982 p = get_tv_string_buf_chk(&argvars[1], buf);
11983 if (p == NULL)
11984 {
11985 fclose(fd);
11986 goto errret; /* type error; errmsg already given */
11987 }
11988 len = STRLEN(p);
11989 if (len > 0 && fwrite(p, len, 1, fd) != 1)
11990 err = TRUE;
11991 }
11992 if (fclose(fd) != 0)
11993 err = TRUE;
11994 if (err)
11995 {
11996 EMSG(_("E677: Error writing temp file"));
11997 goto errret;
11998 }
11999 }
12000
12001 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
12002 * echoes typeahead, that messes up the display. */
12003 if (!msg_silent)
12004 flags += SHELL_COOKED;
12005
12006 if (retlist)
12007 {
12008 int len;
12009 listitem_T *li;
12010 char_u *s = NULL;
12011 char_u *start;
12012 char_u *end;
12013 int i;
12014
12015 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
12016 if (res == NULL)
12017 goto errret;
12018
12019 list = list_alloc();
12020 if (list == NULL)
12021 goto errret;
12022
12023 for (i = 0; i < len; ++i)
12024 {
12025 start = res + i;
12026 while (i < len && res[i] != NL)
12027 ++i;
12028 end = res + i;
12029
12030 s = alloc((unsigned)(end - start + 1));
12031 if (s == NULL)
12032 goto errret;
12033
12034 for (p = s; start < end; ++p, ++start)
12035 *p = *start == NUL ? NL : *start;
12036 *p = NUL;
12037
12038 li = listitem_alloc();
12039 if (li == NULL)
12040 {
12041 vim_free(s);
12042 goto errret;
12043 }
12044 li->li_tv.v_type = VAR_STRING;
12045 li->li_tv.v_lock = 0;
12046 li->li_tv.vval.v_string = s;
12047 list_append(list, li);
12048 }
12049
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012050 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012051 list = NULL;
12052 }
12053 else
12054 {
12055 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
12056#ifdef USE_CR
12057 /* translate <CR> into <NL> */
12058 if (res != NULL)
12059 {
12060 char_u *s;
12061
12062 for (s = res; *s; ++s)
12063 {
12064 if (*s == CAR)
12065 *s = NL;
12066 }
12067 }
12068#else
12069# ifdef USE_CRNL
12070 /* translate <CR><NL> into <NL> */
12071 if (res != NULL)
12072 {
12073 char_u *s, *d;
12074
12075 d = res;
12076 for (s = res; *s; ++s)
12077 {
12078 if (s[0] == CAR && s[1] == NL)
12079 ++s;
12080 *d++ = *s;
12081 }
12082 *d = NUL;
12083 }
12084# endif
12085#endif
12086 rettv->vval.v_string = res;
12087 res = NULL;
12088 }
12089
12090errret:
12091 if (infile != NULL)
12092 {
12093 mch_remove(infile);
12094 vim_free(infile);
12095 }
12096 if (res != NULL)
12097 vim_free(res);
12098 if (list != NULL)
12099 list_free(list);
12100}
12101
12102/*
12103 * "system()" function
12104 */
12105 static void
12106f_system(typval_T *argvars, typval_T *rettv)
12107{
12108 get_cmd_output_as_rettv(argvars, rettv, FALSE);
12109}
12110
12111/*
12112 * "systemlist()" function
12113 */
12114 static void
12115f_systemlist(typval_T *argvars, typval_T *rettv)
12116{
12117 get_cmd_output_as_rettv(argvars, rettv, TRUE);
12118}
12119
12120/*
12121 * "tabpagebuflist()" function
12122 */
12123 static void
12124f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12125{
12126#ifdef FEAT_WINDOWS
12127 tabpage_T *tp;
12128 win_T *wp = NULL;
12129
12130 if (argvars[0].v_type == VAR_UNKNOWN)
12131 wp = firstwin;
12132 else
12133 {
12134 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12135 if (tp != NULL)
12136 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12137 }
12138 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
12139 {
12140 for (; wp != NULL; wp = wp->w_next)
12141 if (list_append_number(rettv->vval.v_list,
12142 wp->w_buffer->b_fnum) == FAIL)
12143 break;
12144 }
12145#endif
12146}
12147
12148
12149/*
12150 * "tabpagenr()" function
12151 */
12152 static void
12153f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
12154{
12155 int nr = 1;
12156#ifdef FEAT_WINDOWS
12157 char_u *arg;
12158
12159 if (argvars[0].v_type != VAR_UNKNOWN)
12160 {
12161 arg = get_tv_string_chk(&argvars[0]);
12162 nr = 0;
12163 if (arg != NULL)
12164 {
12165 if (STRCMP(arg, "$") == 0)
12166 nr = tabpage_index(NULL) - 1;
12167 else
12168 EMSG2(_(e_invexpr2), arg);
12169 }
12170 }
12171 else
12172 nr = tabpage_index(curtab);
12173#endif
12174 rettv->vval.v_number = nr;
12175}
12176
12177
12178#ifdef FEAT_WINDOWS
12179static int get_winnr(tabpage_T *tp, typval_T *argvar);
12180
12181/*
12182 * Common code for tabpagewinnr() and winnr().
12183 */
12184 static int
12185get_winnr(tabpage_T *tp, typval_T *argvar)
12186{
12187 win_T *twin;
12188 int nr = 1;
12189 win_T *wp;
12190 char_u *arg;
12191
12192 twin = (tp == curtab) ? curwin : tp->tp_curwin;
12193 if (argvar->v_type != VAR_UNKNOWN)
12194 {
12195 arg = get_tv_string_chk(argvar);
12196 if (arg == NULL)
12197 nr = 0; /* type error; errmsg already given */
12198 else if (STRCMP(arg, "$") == 0)
12199 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
12200 else if (STRCMP(arg, "#") == 0)
12201 {
12202 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
12203 if (twin == NULL)
12204 nr = 0;
12205 }
12206 else
12207 {
12208 EMSG2(_(e_invexpr2), arg);
12209 nr = 0;
12210 }
12211 }
12212
12213 if (nr > 0)
12214 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12215 wp != twin; wp = wp->w_next)
12216 {
12217 if (wp == NULL)
12218 {
12219 /* didn't find it in this tabpage */
12220 nr = 0;
12221 break;
12222 }
12223 ++nr;
12224 }
12225 return nr;
12226}
12227#endif
12228
12229/*
12230 * "tabpagewinnr()" function
12231 */
12232 static void
12233f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
12234{
12235 int nr = 1;
12236#ifdef FEAT_WINDOWS
12237 tabpage_T *tp;
12238
12239 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12240 if (tp == NULL)
12241 nr = 0;
12242 else
12243 nr = get_winnr(tp, &argvars[1]);
12244#endif
12245 rettv->vval.v_number = nr;
12246}
12247
12248
12249/*
12250 * "tagfiles()" function
12251 */
12252 static void
12253f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
12254{
12255 char_u *fname;
12256 tagname_T tn;
12257 int first;
12258
12259 if (rettv_list_alloc(rettv) == FAIL)
12260 return;
12261 fname = alloc(MAXPATHL);
12262 if (fname == NULL)
12263 return;
12264
12265 for (first = TRUE; ; first = FALSE)
12266 if (get_tagfname(&tn, first, fname) == FAIL
12267 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
12268 break;
12269 tagname_free(&tn);
12270 vim_free(fname);
12271}
12272
12273/*
12274 * "taglist()" function
12275 */
12276 static void
12277f_taglist(typval_T *argvars, typval_T *rettv)
12278{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012279 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012280 char_u *tag_pattern;
12281
12282 tag_pattern = get_tv_string(&argvars[0]);
12283
12284 rettv->vval.v_number = FALSE;
12285 if (*tag_pattern == NUL)
12286 return;
12287
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012288 if (argvars[1].v_type != VAR_UNKNOWN)
12289 fname = get_tv_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012290 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012291 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012292}
12293
12294/*
12295 * "tempname()" function
12296 */
12297 static void
12298f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
12299{
12300 static int x = 'A';
12301
12302 rettv->v_type = VAR_STRING;
12303 rettv->vval.v_string = vim_tempname(x, FALSE);
12304
12305 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
12306 * names. Skip 'I' and 'O', they are used for shell redirection. */
12307 do
12308 {
12309 if (x == 'Z')
12310 x = '0';
12311 else if (x == '9')
12312 x = 'A';
12313 else
12314 {
12315#ifdef EBCDIC
12316 if (x == 'I')
12317 x = 'J';
12318 else if (x == 'R')
12319 x = 'S';
12320 else
12321#endif
12322 ++x;
12323 }
12324 } while (x == 'I' || x == 'O');
12325}
12326
12327#ifdef FEAT_FLOAT
12328/*
12329 * "tan()" function
12330 */
12331 static void
12332f_tan(typval_T *argvars, typval_T *rettv)
12333{
12334 float_T f = 0.0;
12335
12336 rettv->v_type = VAR_FLOAT;
12337 if (get_float_arg(argvars, &f) == OK)
12338 rettv->vval.v_float = tan(f);
12339 else
12340 rettv->vval.v_float = 0.0;
12341}
12342
12343/*
12344 * "tanh()" function
12345 */
12346 static void
12347f_tanh(typval_T *argvars, typval_T *rettv)
12348{
12349 float_T f = 0.0;
12350
12351 rettv->v_type = VAR_FLOAT;
12352 if (get_float_arg(argvars, &f) == OK)
12353 rettv->vval.v_float = tanh(f);
12354 else
12355 rettv->vval.v_float = 0.0;
12356}
12357#endif
12358
12359/*
12360 * "test_alloc_fail(id, countdown, repeat)" function
12361 */
12362 static void
12363f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
12364{
12365 if (argvars[0].v_type != VAR_NUMBER
12366 || argvars[0].vval.v_number <= 0
12367 || argvars[1].v_type != VAR_NUMBER
12368 || argvars[1].vval.v_number < 0
12369 || argvars[2].v_type != VAR_NUMBER)
12370 EMSG(_(e_invarg));
12371 else
12372 {
12373 alloc_fail_id = argvars[0].vval.v_number;
12374 if (alloc_fail_id >= aid_last)
12375 EMSG(_(e_invarg));
12376 alloc_fail_countdown = argvars[1].vval.v_number;
12377 alloc_fail_repeat = argvars[2].vval.v_number;
12378 did_outofmem_msg = FALSE;
12379 }
12380}
12381
12382/*
12383 * "test_autochdir()"
12384 */
12385 static void
12386f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12387{
12388#if defined(FEAT_AUTOCHDIR)
12389 test_autochdir = TRUE;
12390#endif
12391}
12392
12393/*
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012394 * "test_disable({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012395 */
12396 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012397f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012398{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012399 char_u *name = (char_u *)"";
12400 int val;
12401
12402 if (argvars[0].v_type != VAR_STRING
12403 || (argvars[1].v_type) != VAR_NUMBER)
12404 EMSG(_(e_invarg));
12405 else
12406 {
12407 name = get_tv_string_chk(&argvars[0]);
12408 val = (int)get_tv_number(&argvars[1]);
12409
12410 if (STRCMP(name, (char_u *)"redraw") == 0)
12411 disable_redraw_for_testing = val;
12412 else if (STRCMP(name, (char_u *)"char_avail") == 0)
12413 disable_char_avail_for_testing = val;
12414 else if (STRCMP(name, (char_u *)"ALL") == 0)
12415 {
12416 disable_char_avail_for_testing = FALSE;
12417 disable_redraw_for_testing = FALSE;
12418 }
12419 else
12420 EMSG2(_(e_invarg2), name);
12421 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012422}
12423
12424/*
12425 * "test_garbagecollect_now()" function
12426 */
12427 static void
12428f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12429{
12430 /* This is dangerous, any Lists and Dicts used internally may be freed
12431 * while still in use. */
12432 garbage_collect(TRUE);
12433}
12434
Bram Moolenaare0c31f62017-03-01 15:07:05 +010012435/*
12436 * "test_ignore_error()" function
12437 */
12438 static void
12439f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
12440{
12441 ignore_error_for_testing(get_tv_string(&argvars[0]));
12442}
12443
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012444#ifdef FEAT_JOB_CHANNEL
12445 static void
12446f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
12447{
12448 rettv->v_type = VAR_CHANNEL;
12449 rettv->vval.v_channel = NULL;
12450}
12451#endif
12452
12453 static void
12454f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
12455{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012456 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012457}
12458
12459#ifdef FEAT_JOB_CHANNEL
12460 static void
12461f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
12462{
12463 rettv->v_type = VAR_JOB;
12464 rettv->vval.v_job = NULL;
12465}
12466#endif
12467
12468 static void
12469f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
12470{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012471 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012472}
12473
12474 static void
12475f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
12476{
12477 rettv->v_type = VAR_PARTIAL;
12478 rettv->vval.v_partial = NULL;
12479}
12480
12481 static void
12482f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
12483{
12484 rettv->v_type = VAR_STRING;
12485 rettv->vval.v_string = NULL;
12486}
12487
12488 static void
12489f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
12490{
12491 time_for_testing = (time_t)get_tv_number(&argvars[0]);
12492}
12493
12494#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
12495/*
12496 * Get a callback from "arg". It can be a Funcref or a function name.
12497 * When "arg" is zero return an empty string.
12498 * Return NULL for an invalid argument.
12499 */
12500 char_u *
12501get_callback(typval_T *arg, partial_T **pp)
12502{
12503 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
12504 {
12505 *pp = arg->vval.v_partial;
12506 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012507 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012508 }
12509 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012510 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012511 {
12512 func_ref(arg->vval.v_string);
12513 return arg->vval.v_string;
12514 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012515 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
12516 return (char_u *)"";
12517 EMSG(_("E921: Invalid callback argument"));
12518 return NULL;
12519}
12520
12521/*
12522 * Unref/free "callback" and "partial" retured by get_callback().
12523 */
12524 void
12525free_callback(char_u *callback, partial_T *partial)
12526{
12527 if (partial != NULL)
12528 partial_unref(partial);
12529 else if (callback != NULL)
12530 {
12531 func_unref(callback);
12532 vim_free(callback);
12533 }
12534}
12535#endif
12536
12537#ifdef FEAT_TIMERS
12538/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020012539 * "timer_info([timer])" function
12540 */
12541 static void
12542f_timer_info(typval_T *argvars, typval_T *rettv)
12543{
12544 timer_T *timer = NULL;
12545
12546 if (rettv_list_alloc(rettv) != OK)
12547 return;
12548 if (argvars[0].v_type != VAR_UNKNOWN)
12549 {
12550 if (argvars[0].v_type != VAR_NUMBER)
12551 EMSG(_(e_number_exp));
12552 else
12553 {
12554 timer = find_timer((int)get_tv_number(&argvars[0]));
12555 if (timer != NULL)
12556 add_timer_info(rettv, timer);
12557 }
12558 }
12559 else
12560 add_timer_info_all(rettv);
12561}
12562
12563/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012564 * "timer_pause(timer, paused)" function
12565 */
12566 static void
12567f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
12568{
12569 timer_T *timer = NULL;
12570 int paused = (int)get_tv_number(&argvars[1]);
12571
12572 if (argvars[0].v_type != VAR_NUMBER)
12573 EMSG(_(e_number_exp));
12574 else
12575 {
12576 timer = find_timer((int)get_tv_number(&argvars[0]));
12577 if (timer != NULL)
12578 timer->tr_paused = paused;
12579 }
12580}
12581
12582/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012583 * "timer_start(time, callback [, options])" function
12584 */
12585 static void
12586f_timer_start(typval_T *argvars, typval_T *rettv)
12587{
Bram Moolenaar75537a92016-09-05 22:45:28 +020012588 long msec = (long)get_tv_number(&argvars[0]);
12589 timer_T *timer;
12590 int repeat = 0;
12591 char_u *callback;
12592 dict_T *dict;
12593 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012594
Bram Moolenaar75537a92016-09-05 22:45:28 +020012595 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012596 if (check_secure())
12597 return;
12598 if (argvars[2].v_type != VAR_UNKNOWN)
12599 {
12600 if (argvars[2].v_type != VAR_DICT
12601 || (dict = argvars[2].vval.v_dict) == NULL)
12602 {
12603 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
12604 return;
12605 }
12606 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
12607 repeat = get_dict_number(dict, (char_u *)"repeat");
12608 }
12609
Bram Moolenaar75537a92016-09-05 22:45:28 +020012610 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012611 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020012612 return;
12613
12614 timer = create_timer(msec, repeat);
12615 if (timer == NULL)
12616 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012617 else
12618 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020012619 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020012620 timer->tr_callback = vim_strsave(callback);
12621 else
12622 /* pointer into the partial */
12623 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020012624 timer->tr_partial = partial;
12625 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012626 }
12627}
12628
12629/*
12630 * "timer_stop(timer)" function
12631 */
12632 static void
12633f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
12634{
12635 timer_T *timer;
12636
12637 if (argvars[0].v_type != VAR_NUMBER)
12638 {
12639 EMSG(_(e_number_exp));
12640 return;
12641 }
12642 timer = find_timer((int)get_tv_number(&argvars[0]));
12643 if (timer != NULL)
12644 stop_timer(timer);
12645}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012646
12647/*
12648 * "timer_stopall()" function
12649 */
12650 static void
12651f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12652{
12653 stop_all_timers();
12654}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012655#endif
12656
12657/*
12658 * "tolower(string)" function
12659 */
12660 static void
12661f_tolower(typval_T *argvars, typval_T *rettv)
12662{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012663 rettv->v_type = VAR_STRING;
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +010012664 rettv->vval.v_string = strlow_save(get_tv_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012665}
12666
12667/*
12668 * "toupper(string)" function
12669 */
12670 static void
12671f_toupper(typval_T *argvars, typval_T *rettv)
12672{
12673 rettv->v_type = VAR_STRING;
12674 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
12675}
12676
12677/*
12678 * "tr(string, fromstr, tostr)" function
12679 */
12680 static void
12681f_tr(typval_T *argvars, typval_T *rettv)
12682{
12683 char_u *in_str;
12684 char_u *fromstr;
12685 char_u *tostr;
12686 char_u *p;
12687#ifdef FEAT_MBYTE
12688 int inlen;
12689 int fromlen;
12690 int tolen;
12691 int idx;
12692 char_u *cpstr;
12693 int cplen;
12694 int first = TRUE;
12695#endif
12696 char_u buf[NUMBUFLEN];
12697 char_u buf2[NUMBUFLEN];
12698 garray_T ga;
12699
12700 in_str = get_tv_string(&argvars[0]);
12701 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
12702 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
12703
12704 /* Default return value: empty string. */
12705 rettv->v_type = VAR_STRING;
12706 rettv->vval.v_string = NULL;
12707 if (fromstr == NULL || tostr == NULL)
12708 return; /* type error; errmsg already given */
12709 ga_init2(&ga, (int)sizeof(char), 80);
12710
12711#ifdef FEAT_MBYTE
12712 if (!has_mbyte)
12713#endif
12714 /* not multi-byte: fromstr and tostr must be the same length */
12715 if (STRLEN(fromstr) != STRLEN(tostr))
12716 {
12717#ifdef FEAT_MBYTE
12718error:
12719#endif
12720 EMSG2(_(e_invarg2), fromstr);
12721 ga_clear(&ga);
12722 return;
12723 }
12724
12725 /* fromstr and tostr have to contain the same number of chars */
12726 while (*in_str != NUL)
12727 {
12728#ifdef FEAT_MBYTE
12729 if (has_mbyte)
12730 {
12731 inlen = (*mb_ptr2len)(in_str);
12732 cpstr = in_str;
12733 cplen = inlen;
12734 idx = 0;
12735 for (p = fromstr; *p != NUL; p += fromlen)
12736 {
12737 fromlen = (*mb_ptr2len)(p);
12738 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
12739 {
12740 for (p = tostr; *p != NUL; p += tolen)
12741 {
12742 tolen = (*mb_ptr2len)(p);
12743 if (idx-- == 0)
12744 {
12745 cplen = tolen;
12746 cpstr = p;
12747 break;
12748 }
12749 }
12750 if (*p == NUL) /* tostr is shorter than fromstr */
12751 goto error;
12752 break;
12753 }
12754 ++idx;
12755 }
12756
12757 if (first && cpstr == in_str)
12758 {
12759 /* Check that fromstr and tostr have the same number of
12760 * (multi-byte) characters. Done only once when a character
12761 * of in_str doesn't appear in fromstr. */
12762 first = FALSE;
12763 for (p = tostr; *p != NUL; p += tolen)
12764 {
12765 tolen = (*mb_ptr2len)(p);
12766 --idx;
12767 }
12768 if (idx != 0)
12769 goto error;
12770 }
12771
12772 (void)ga_grow(&ga, cplen);
12773 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
12774 ga.ga_len += cplen;
12775
12776 in_str += inlen;
12777 }
12778 else
12779#endif
12780 {
12781 /* When not using multi-byte chars we can do it faster. */
12782 p = vim_strchr(fromstr, *in_str);
12783 if (p != NULL)
12784 ga_append(&ga, tostr[p - fromstr]);
12785 else
12786 ga_append(&ga, *in_str);
12787 ++in_str;
12788 }
12789 }
12790
12791 /* add a terminating NUL */
12792 (void)ga_grow(&ga, 1);
12793 ga_append(&ga, NUL);
12794
12795 rettv->vval.v_string = ga.ga_data;
12796}
12797
12798#ifdef FEAT_FLOAT
12799/*
12800 * "trunc({float})" function
12801 */
12802 static void
12803f_trunc(typval_T *argvars, typval_T *rettv)
12804{
12805 float_T f = 0.0;
12806
12807 rettv->v_type = VAR_FLOAT;
12808 if (get_float_arg(argvars, &f) == OK)
12809 /* trunc() is not in C90, use floor() or ceil() instead. */
12810 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
12811 else
12812 rettv->vval.v_float = 0.0;
12813}
12814#endif
12815
12816/*
12817 * "type(expr)" function
12818 */
12819 static void
12820f_type(typval_T *argvars, typval_T *rettv)
12821{
12822 int n = -1;
12823
12824 switch (argvars[0].v_type)
12825 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020012826 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
12827 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012828 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020012829 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
12830 case VAR_LIST: n = VAR_TYPE_LIST; break;
12831 case VAR_DICT: n = VAR_TYPE_DICT; break;
12832 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012833 case VAR_SPECIAL:
12834 if (argvars[0].vval.v_number == VVAL_FALSE
12835 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020012836 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012837 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020012838 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012839 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020012840 case VAR_JOB: n = VAR_TYPE_JOB; break;
12841 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012842 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010012843 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012844 n = -1;
12845 break;
12846 }
12847 rettv->vval.v_number = n;
12848}
12849
12850/*
12851 * "undofile(name)" function
12852 */
12853 static void
12854f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
12855{
12856 rettv->v_type = VAR_STRING;
12857#ifdef FEAT_PERSISTENT_UNDO
12858 {
12859 char_u *fname = get_tv_string(&argvars[0]);
12860
12861 if (*fname == NUL)
12862 {
12863 /* If there is no file name there will be no undo file. */
12864 rettv->vval.v_string = NULL;
12865 }
12866 else
12867 {
12868 char_u *ffname = FullName_save(fname, FALSE);
12869
12870 if (ffname != NULL)
12871 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
12872 vim_free(ffname);
12873 }
12874 }
12875#else
12876 rettv->vval.v_string = NULL;
12877#endif
12878}
12879
12880/*
12881 * "undotree()" function
12882 */
12883 static void
12884f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
12885{
12886 if (rettv_dict_alloc(rettv) == OK)
12887 {
12888 dict_T *dict = rettv->vval.v_dict;
12889 list_T *list;
12890
12891 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
12892 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
12893 dict_add_nr_str(dict, "save_last",
12894 (long)curbuf->b_u_save_nr_last, NULL);
12895 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
12896 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
12897 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
12898
12899 list = list_alloc();
12900 if (list != NULL)
12901 {
12902 u_eval_tree(curbuf->b_u_oldhead, list);
12903 dict_add_list(dict, "entries", list);
12904 }
12905 }
12906}
12907
12908/*
12909 * "values(dict)" function
12910 */
12911 static void
12912f_values(typval_T *argvars, typval_T *rettv)
12913{
12914 dict_list(argvars, rettv, 1);
12915}
12916
12917/*
12918 * "virtcol(string)" function
12919 */
12920 static void
12921f_virtcol(typval_T *argvars, typval_T *rettv)
12922{
12923 colnr_T vcol = 0;
12924 pos_T *fp;
12925 int fnum = curbuf->b_fnum;
12926
12927 fp = var2fpos(&argvars[0], FALSE, &fnum);
12928 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
12929 && fnum == curbuf->b_fnum)
12930 {
12931 getvvcol(curwin, fp, NULL, NULL, &vcol);
12932 ++vcol;
12933 }
12934
12935 rettv->vval.v_number = vcol;
12936}
12937
12938/*
12939 * "visualmode()" function
12940 */
12941 static void
12942f_visualmode(typval_T *argvars, typval_T *rettv)
12943{
12944 char_u str[2];
12945
12946 rettv->v_type = VAR_STRING;
12947 str[0] = curbuf->b_visual_mode_eval;
12948 str[1] = NUL;
12949 rettv->vval.v_string = vim_strsave(str);
12950
12951 /* A non-zero number or non-empty string argument: reset mode. */
12952 if (non_zero_arg(&argvars[0]))
12953 curbuf->b_visual_mode_eval = NUL;
12954}
12955
12956/*
12957 * "wildmenumode()" function
12958 */
12959 static void
12960f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12961{
12962#ifdef FEAT_WILDMENU
12963 if (wild_menu_showing)
12964 rettv->vval.v_number = 1;
12965#endif
12966}
12967
12968/*
12969 * "winbufnr(nr)" function
12970 */
12971 static void
12972f_winbufnr(typval_T *argvars, typval_T *rettv)
12973{
12974 win_T *wp;
12975
12976 wp = find_win_by_nr(&argvars[0], NULL);
12977 if (wp == NULL)
12978 rettv->vval.v_number = -1;
12979 else
12980 rettv->vval.v_number = wp->w_buffer->b_fnum;
12981}
12982
12983/*
12984 * "wincol()" function
12985 */
12986 static void
12987f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
12988{
12989 validate_cursor();
12990 rettv->vval.v_number = curwin->w_wcol + 1;
12991}
12992
12993/*
12994 * "winheight(nr)" function
12995 */
12996 static void
12997f_winheight(typval_T *argvars, typval_T *rettv)
12998{
12999 win_T *wp;
13000
13001 wp = find_win_by_nr(&argvars[0], NULL);
13002 if (wp == NULL)
13003 rettv->vval.v_number = -1;
13004 else
13005 rettv->vval.v_number = wp->w_height;
13006}
13007
13008/*
13009 * "winline()" function
13010 */
13011 static void
13012f_winline(typval_T *argvars UNUSED, typval_T *rettv)
13013{
13014 validate_cursor();
13015 rettv->vval.v_number = curwin->w_wrow + 1;
13016}
13017
13018/*
13019 * "winnr()" function
13020 */
13021 static void
13022f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
13023{
13024 int nr = 1;
13025
13026#ifdef FEAT_WINDOWS
13027 nr = get_winnr(curtab, &argvars[0]);
13028#endif
13029 rettv->vval.v_number = nr;
13030}
13031
13032/*
13033 * "winrestcmd()" function
13034 */
13035 static void
13036f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
13037{
13038#ifdef FEAT_WINDOWS
13039 win_T *wp;
13040 int winnr = 1;
13041 garray_T ga;
13042 char_u buf[50];
13043
13044 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020013045 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013046 {
13047 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
13048 ga_concat(&ga, buf);
13049 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
13050 ga_concat(&ga, buf);
13051 ++winnr;
13052 }
13053 ga_append(&ga, NUL);
13054
13055 rettv->vval.v_string = ga.ga_data;
13056#else
13057 rettv->vval.v_string = NULL;
13058#endif
13059 rettv->v_type = VAR_STRING;
13060}
13061
13062/*
13063 * "winrestview()" function
13064 */
13065 static void
13066f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
13067{
13068 dict_T *dict;
13069
13070 if (argvars[0].v_type != VAR_DICT
13071 || (dict = argvars[0].vval.v_dict) == NULL)
13072 EMSG(_(e_invarg));
13073 else
13074 {
13075 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
13076 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
13077 if (dict_find(dict, (char_u *)"col", -1) != NULL)
13078 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
13079#ifdef FEAT_VIRTUALEDIT
13080 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
13081 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
13082#endif
13083 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
13084 {
13085 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
13086 curwin->w_set_curswant = FALSE;
13087 }
13088
13089 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
13090 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
13091#ifdef FEAT_DIFF
13092 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
13093 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
13094#endif
13095 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
13096 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
13097 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
13098 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
13099
13100 check_cursor();
13101 win_new_height(curwin, curwin->w_height);
13102# ifdef FEAT_WINDOWS
13103 win_new_width(curwin, W_WIDTH(curwin));
13104# endif
13105 changed_window_setting();
13106
13107 if (curwin->w_topline <= 0)
13108 curwin->w_topline = 1;
13109 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
13110 curwin->w_topline = curbuf->b_ml.ml_line_count;
13111#ifdef FEAT_DIFF
13112 check_topfill(curwin, TRUE);
13113#endif
13114 }
13115}
13116
13117/*
13118 * "winsaveview()" function
13119 */
13120 static void
13121f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
13122{
13123 dict_T *dict;
13124
13125 if (rettv_dict_alloc(rettv) == FAIL)
13126 return;
13127 dict = rettv->vval.v_dict;
13128
13129 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
13130 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
13131#ifdef FEAT_VIRTUALEDIT
13132 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
13133#endif
13134 update_curswant();
13135 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
13136
13137 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
13138#ifdef FEAT_DIFF
13139 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
13140#endif
13141 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
13142 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
13143}
13144
13145/*
13146 * "winwidth(nr)" function
13147 */
13148 static void
13149f_winwidth(typval_T *argvars, typval_T *rettv)
13150{
13151 win_T *wp;
13152
13153 wp = find_win_by_nr(&argvars[0], NULL);
13154 if (wp == NULL)
13155 rettv->vval.v_number = -1;
13156 else
13157#ifdef FEAT_WINDOWS
13158 rettv->vval.v_number = wp->w_width;
13159#else
13160 rettv->vval.v_number = Columns;
13161#endif
13162}
13163
13164/*
13165 * "wordcount()" function
13166 */
13167 static void
13168f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
13169{
13170 if (rettv_dict_alloc(rettv) == FAIL)
13171 return;
13172 cursor_pos_info(rettv->vval.v_dict);
13173}
13174
13175/*
13176 * "writefile()" function
13177 */
13178 static void
13179f_writefile(typval_T *argvars, typval_T *rettv)
13180{
13181 int binary = FALSE;
13182 int append = FALSE;
13183 char_u *fname;
13184 FILE *fd;
13185 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013186 listitem_T *li;
13187 list_T *list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013188
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013189 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013190 if (check_restricted() || check_secure())
13191 return;
13192
13193 if (argvars[0].v_type != VAR_LIST)
13194 {
13195 EMSG2(_(e_listarg), "writefile()");
13196 return;
13197 }
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013198 list = argvars[0].vval.v_list;
13199 if (list == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013200 return;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013201 for (li = list->lv_first; li != NULL; li = li->li_next)
13202 if (get_tv_string_chk(&li->li_tv) == NULL)
13203 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013204
13205 if (argvars[2].v_type != VAR_UNKNOWN)
13206 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013207 char_u *arg2 = get_tv_string_chk(&argvars[2]);
13208
13209 if (arg2 == NULL)
13210 return;
13211 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013212 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013213 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013214 append = TRUE;
13215 }
13216
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013217 fname = get_tv_string_chk(&argvars[1]);
13218 if (fname == NULL)
13219 return;
13220
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013221 /* Always open the file in binary mode, library functions have a mind of
13222 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013223 if (*fname == NUL || (fd = mch_fopen((char *)fname,
13224 append ? APPENDBIN : WRITEBIN)) == NULL)
13225 {
13226 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
13227 ret = -1;
13228 }
13229 else
13230 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013231 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013232 ret = -1;
13233 fclose(fd);
13234 }
13235
13236 rettv->vval.v_number = ret;
13237}
13238
13239/*
13240 * "xor(expr, expr)" function
13241 */
13242 static void
13243f_xor(typval_T *argvars, typval_T *rettv)
13244{
13245 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
13246 ^ get_tv_number_chk(&argvars[1], NULL);
13247}
13248
13249
13250#endif /* FEAT_EVAL */