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