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