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