blob: 8167abd60f9b855108ca813b61f2bd6fac6a6ed7 [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);
3194 if (vgetc_busy)
3195 typebuf_was_filled = TRUE;
3196 if (execute)
3197 {
3198 int save_msg_scroll = msg_scroll;
3199
3200 /* Avoid a 1 second delay when the keys start Insert mode. */
3201 msg_scroll = FALSE;
3202
3203 if (!dangerous)
3204 ++ex_normal_busy;
3205 exec_normal(TRUE);
3206 if (!dangerous)
3207 --ex_normal_busy;
3208 msg_scroll |= save_msg_scroll;
3209 }
3210 }
3211 }
3212}
3213
3214/*
3215 * "filereadable()" function
3216 */
3217 static void
3218f_filereadable(typval_T *argvars, typval_T *rettv)
3219{
3220 int fd;
3221 char_u *p;
3222 int n;
3223
3224#ifndef O_NONBLOCK
3225# define O_NONBLOCK 0
3226#endif
3227 p = get_tv_string(&argvars[0]);
3228 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3229 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3230 {
3231 n = TRUE;
3232 close(fd);
3233 }
3234 else
3235 n = FALSE;
3236
3237 rettv->vval.v_number = n;
3238}
3239
3240/*
3241 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3242 * rights to write into.
3243 */
3244 static void
3245f_filewritable(typval_T *argvars, typval_T *rettv)
3246{
3247 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
3248}
3249
3250 static void
3251findfilendir(
3252 typval_T *argvars UNUSED,
3253 typval_T *rettv,
3254 int find_what UNUSED)
3255{
3256#ifdef FEAT_SEARCHPATH
3257 char_u *fname;
3258 char_u *fresult = NULL;
3259 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3260 char_u *p;
3261 char_u pathbuf[NUMBUFLEN];
3262 int count = 1;
3263 int first = TRUE;
3264 int error = FALSE;
3265#endif
3266
3267 rettv->vval.v_string = NULL;
3268 rettv->v_type = VAR_STRING;
3269
3270#ifdef FEAT_SEARCHPATH
3271 fname = get_tv_string(&argvars[0]);
3272
3273 if (argvars[1].v_type != VAR_UNKNOWN)
3274 {
3275 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
3276 if (p == NULL)
3277 error = TRUE;
3278 else
3279 {
3280 if (*p != NUL)
3281 path = p;
3282
3283 if (argvars[2].v_type != VAR_UNKNOWN)
3284 count = (int)get_tv_number_chk(&argvars[2], &error);
3285 }
3286 }
3287
3288 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3289 error = TRUE;
3290
3291 if (*fname != NUL && !error)
3292 {
3293 do
3294 {
3295 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3296 vim_free(fresult);
3297 fresult = find_file_in_path_option(first ? fname : NULL,
3298 first ? (int)STRLEN(fname) : 0,
3299 0, first, path,
3300 find_what,
3301 curbuf->b_ffname,
3302 find_what == FINDFILE_DIR
3303 ? (char_u *)"" : curbuf->b_p_sua);
3304 first = FALSE;
3305
3306 if (fresult != NULL && rettv->v_type == VAR_LIST)
3307 list_append_string(rettv->vval.v_list, fresult, -1);
3308
3309 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3310 }
3311
3312 if (rettv->v_type == VAR_STRING)
3313 rettv->vval.v_string = fresult;
3314#endif
3315}
3316
3317/*
3318 * "filter()" function
3319 */
3320 static void
3321f_filter(typval_T *argvars, typval_T *rettv)
3322{
3323 filter_map(argvars, rettv, FALSE);
3324}
3325
3326/*
3327 * "finddir({fname}[, {path}[, {count}]])" function
3328 */
3329 static void
3330f_finddir(typval_T *argvars, typval_T *rettv)
3331{
3332 findfilendir(argvars, rettv, FINDFILE_DIR);
3333}
3334
3335/*
3336 * "findfile({fname}[, {path}[, {count}]])" function
3337 */
3338 static void
3339f_findfile(typval_T *argvars, typval_T *rettv)
3340{
3341 findfilendir(argvars, rettv, FINDFILE_FILE);
3342}
3343
3344#ifdef FEAT_FLOAT
3345/*
3346 * "float2nr({float})" function
3347 */
3348 static void
3349f_float2nr(typval_T *argvars, typval_T *rettv)
3350{
3351 float_T f = 0.0;
3352
3353 if (get_float_arg(argvars, &f) == OK)
3354 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003355 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003356 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003357 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003358 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003359 else
3360 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003361 }
3362}
3363
3364/*
3365 * "floor({float})" function
3366 */
3367 static void
3368f_floor(typval_T *argvars, typval_T *rettv)
3369{
3370 float_T f = 0.0;
3371
3372 rettv->v_type = VAR_FLOAT;
3373 if (get_float_arg(argvars, &f) == OK)
3374 rettv->vval.v_float = floor(f);
3375 else
3376 rettv->vval.v_float = 0.0;
3377}
3378
3379/*
3380 * "fmod()" function
3381 */
3382 static void
3383f_fmod(typval_T *argvars, typval_T *rettv)
3384{
3385 float_T fx = 0.0, fy = 0.0;
3386
3387 rettv->v_type = VAR_FLOAT;
3388 if (get_float_arg(argvars, &fx) == OK
3389 && get_float_arg(&argvars[1], &fy) == OK)
3390 rettv->vval.v_float = fmod(fx, fy);
3391 else
3392 rettv->vval.v_float = 0.0;
3393}
3394#endif
3395
3396/*
3397 * "fnameescape({string})" function
3398 */
3399 static void
3400f_fnameescape(typval_T *argvars, typval_T *rettv)
3401{
3402 rettv->vval.v_string = vim_strsave_fnameescape(
3403 get_tv_string(&argvars[0]), FALSE);
3404 rettv->v_type = VAR_STRING;
3405}
3406
3407/*
3408 * "fnamemodify({fname}, {mods})" function
3409 */
3410 static void
3411f_fnamemodify(typval_T *argvars, typval_T *rettv)
3412{
3413 char_u *fname;
3414 char_u *mods;
3415 int usedlen = 0;
3416 int len;
3417 char_u *fbuf = NULL;
3418 char_u buf[NUMBUFLEN];
3419
3420 fname = get_tv_string_chk(&argvars[0]);
3421 mods = get_tv_string_buf_chk(&argvars[1], buf);
3422 if (fname == NULL || mods == NULL)
3423 fname = NULL;
3424 else
3425 {
3426 len = (int)STRLEN(fname);
3427 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
3428 }
3429
3430 rettv->v_type = VAR_STRING;
3431 if (fname == NULL)
3432 rettv->vval.v_string = NULL;
3433 else
3434 rettv->vval.v_string = vim_strnsave(fname, len);
3435 vim_free(fbuf);
3436}
3437
3438static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
3439
3440/*
3441 * "foldclosed()" function
3442 */
3443 static void
3444foldclosed_both(
3445 typval_T *argvars UNUSED,
3446 typval_T *rettv,
3447 int end UNUSED)
3448{
3449#ifdef FEAT_FOLDING
3450 linenr_T lnum;
3451 linenr_T first, last;
3452
3453 lnum = get_tv_lnum(argvars);
3454 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3455 {
3456 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3457 {
3458 if (end)
3459 rettv->vval.v_number = (varnumber_T)last;
3460 else
3461 rettv->vval.v_number = (varnumber_T)first;
3462 return;
3463 }
3464 }
3465#endif
3466 rettv->vval.v_number = -1;
3467}
3468
3469/*
3470 * "foldclosed()" function
3471 */
3472 static void
3473f_foldclosed(typval_T *argvars, typval_T *rettv)
3474{
3475 foldclosed_both(argvars, rettv, FALSE);
3476}
3477
3478/*
3479 * "foldclosedend()" function
3480 */
3481 static void
3482f_foldclosedend(typval_T *argvars, typval_T *rettv)
3483{
3484 foldclosed_both(argvars, rettv, TRUE);
3485}
3486
3487/*
3488 * "foldlevel()" function
3489 */
3490 static void
3491f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3492{
3493#ifdef FEAT_FOLDING
3494 linenr_T lnum;
3495
3496 lnum = get_tv_lnum(argvars);
3497 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3498 rettv->vval.v_number = foldLevel(lnum);
3499#endif
3500}
3501
3502/*
3503 * "foldtext()" function
3504 */
3505 static void
3506f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
3507{
3508#ifdef FEAT_FOLDING
3509 linenr_T foldstart;
3510 linenr_T foldend;
3511 char_u *dashes;
3512 linenr_T lnum;
3513 char_u *s;
3514 char_u *r;
3515 int len;
3516 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003517 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003518#endif
3519
3520 rettv->v_type = VAR_STRING;
3521 rettv->vval.v_string = NULL;
3522#ifdef FEAT_FOLDING
3523 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
3524 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
3525 dashes = get_vim_var_str(VV_FOLDDASHES);
3526 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
3527 && dashes != NULL)
3528 {
3529 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02003530 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003531 if (!linewhite(lnum))
3532 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003533
3534 /* Find interesting text in this line. */
3535 s = skipwhite(ml_get(lnum));
3536 /* skip C comment-start */
3537 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
3538 {
3539 s = skipwhite(s + 2);
3540 if (*skipwhite(s) == NUL
3541 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
3542 {
3543 s = skipwhite(ml_get(lnum + 1));
3544 if (*s == '*')
3545 s = skipwhite(s + 1);
3546 }
3547 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02003548 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003549 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003550 r = alloc((unsigned)(STRLEN(txt)
3551 + STRLEN(dashes) /* for %s */
3552 + 20 /* for %3ld */
3553 + STRLEN(s))); /* concatenated */
3554 if (r != NULL)
3555 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02003556 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003557 len = (int)STRLEN(r);
3558 STRCAT(r, s);
3559 /* remove 'foldmarker' and 'commentstring' */
3560 foldtext_cleanup(r + len);
3561 rettv->vval.v_string = r;
3562 }
3563 }
3564#endif
3565}
3566
3567/*
3568 * "foldtextresult(lnum)" function
3569 */
3570 static void
3571f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
3572{
3573#ifdef FEAT_FOLDING
3574 linenr_T lnum;
3575 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003576 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003577 foldinfo_T foldinfo;
3578 int fold_count;
3579#endif
3580
3581 rettv->v_type = VAR_STRING;
3582 rettv->vval.v_string = NULL;
3583#ifdef FEAT_FOLDING
3584 lnum = get_tv_lnum(argvars);
3585 /* treat illegal types and illegal string values for {lnum} the same */
3586 if (lnum < 0)
3587 lnum = 0;
3588 fold_count = foldedCount(curwin, lnum, &foldinfo);
3589 if (fold_count > 0)
3590 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01003591 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
3592 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003593 if (text == buf)
3594 text = vim_strsave(text);
3595 rettv->vval.v_string = text;
3596 }
3597#endif
3598}
3599
3600/*
3601 * "foreground()" function
3602 */
3603 static void
3604f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3605{
3606#ifdef FEAT_GUI
3607 if (gui.in_use)
3608 gui_mch_set_foreground();
3609#else
3610# ifdef WIN32
3611 win32_set_foreground();
3612# endif
3613#endif
3614}
3615
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003616 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003617common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003618{
3619 char_u *s;
3620 char_u *name;
3621 int use_string = FALSE;
3622 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003623 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003624
3625 if (argvars[0].v_type == VAR_FUNC)
3626 {
3627 /* function(MyFunc, [arg], dict) */
3628 s = argvars[0].vval.v_string;
3629 }
3630 else if (argvars[0].v_type == VAR_PARTIAL
3631 && argvars[0].vval.v_partial != NULL)
3632 {
3633 /* function(dict.MyFunc, [arg]) */
3634 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003635 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003636 }
3637 else
3638 {
3639 /* function('MyFunc', [arg], dict) */
3640 s = get_tv_string(&argvars[0]);
3641 use_string = TRUE;
3642 }
3643
Bram Moolenaar843b8842016-08-21 14:36:15 +02003644 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003645 {
3646 name = s;
3647 trans_name = trans_function_name(&name, FALSE,
3648 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
3649 if (*name != NUL)
3650 s = NULL;
3651 }
3652
Bram Moolenaar843b8842016-08-21 14:36:15 +02003653 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
3654 || (is_funcref && trans_name == NULL))
Bram Moolenaar5582ef12016-09-14 22:16:13 +02003655 EMSG2(_(e_invarg2), use_string ? get_tv_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003656 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003657 else if (trans_name != NULL && (is_funcref
3658 ? find_func(trans_name) == NULL
3659 : !translated_function_exists(trans_name)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003660 EMSG2(_("E700: Unknown function: %s"), s);
3661 else
3662 {
3663 int dict_idx = 0;
3664 int arg_idx = 0;
3665 list_T *list = NULL;
3666
3667 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
3668 {
3669 char sid_buf[25];
3670 int off = *s == 's' ? 2 : 5;
3671
3672 /* Expand s: and <SID> into <SNR>nr_, so that the function can
3673 * also be called from another script. Using trans_function_name()
3674 * would also work, but some plugins depend on the name being
3675 * printable text. */
3676 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
3677 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
3678 if (name != NULL)
3679 {
3680 STRCPY(name, sid_buf);
3681 STRCAT(name, s + off);
3682 }
3683 }
3684 else
3685 name = vim_strsave(s);
3686
3687 if (argvars[1].v_type != VAR_UNKNOWN)
3688 {
3689 if (argvars[2].v_type != VAR_UNKNOWN)
3690 {
3691 /* function(name, [args], dict) */
3692 arg_idx = 1;
3693 dict_idx = 2;
3694 }
3695 else if (argvars[1].v_type == VAR_DICT)
3696 /* function(name, dict) */
3697 dict_idx = 1;
3698 else
3699 /* function(name, [args]) */
3700 arg_idx = 1;
3701 if (dict_idx > 0)
3702 {
3703 if (argvars[dict_idx].v_type != VAR_DICT)
3704 {
3705 EMSG(_("E922: expected a dict"));
3706 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003707 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003708 }
3709 if (argvars[dict_idx].vval.v_dict == NULL)
3710 dict_idx = 0;
3711 }
3712 if (arg_idx > 0)
3713 {
3714 if (argvars[arg_idx].v_type != VAR_LIST)
3715 {
3716 EMSG(_("E923: Second argument of function() must be a list or a dict"));
3717 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003718 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003719 }
3720 list = argvars[arg_idx].vval.v_list;
3721 if (list == NULL || list->lv_len == 0)
3722 arg_idx = 0;
3723 }
3724 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003725 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003726 {
3727 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
3728
3729 /* result is a VAR_PARTIAL */
3730 if (pt == NULL)
3731 vim_free(name);
3732 else
3733 {
3734 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
3735 {
3736 listitem_T *li;
3737 int i = 0;
3738 int arg_len = 0;
3739 int lv_len = 0;
3740
3741 if (arg_pt != NULL)
3742 arg_len = arg_pt->pt_argc;
3743 if (list != NULL)
3744 lv_len = list->lv_len;
3745 pt->pt_argc = arg_len + lv_len;
3746 pt->pt_argv = (typval_T *)alloc(
3747 sizeof(typval_T) * pt->pt_argc);
3748 if (pt->pt_argv == NULL)
3749 {
3750 vim_free(pt);
3751 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003752 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003753 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003754 for (i = 0; i < arg_len; i++)
3755 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
3756 if (lv_len > 0)
3757 for (li = list->lv_first; li != NULL;
3758 li = li->li_next)
3759 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003760 }
3761
3762 /* For "function(dict.func, [], dict)" and "func" is a partial
3763 * use "dict". That is backwards compatible. */
3764 if (dict_idx > 0)
3765 {
3766 /* The dict is bound explicitly, pt_auto is FALSE. */
3767 pt->pt_dict = argvars[dict_idx].vval.v_dict;
3768 ++pt->pt_dict->dv_refcount;
3769 }
3770 else if (arg_pt != NULL)
3771 {
3772 /* If the dict was bound automatically the result is also
3773 * bound automatically. */
3774 pt->pt_dict = arg_pt->pt_dict;
3775 pt->pt_auto = arg_pt->pt_auto;
3776 if (pt->pt_dict != NULL)
3777 ++pt->pt_dict->dv_refcount;
3778 }
3779
3780 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003781 if (arg_pt != NULL && arg_pt->pt_func != NULL)
3782 {
3783 pt->pt_func = arg_pt->pt_func;
3784 func_ptr_ref(pt->pt_func);
3785 vim_free(name);
3786 }
3787 else if (is_funcref)
3788 {
3789 pt->pt_func = find_func(trans_name);
3790 func_ptr_ref(pt->pt_func);
3791 vim_free(name);
3792 }
3793 else
3794 {
3795 pt->pt_name = name;
3796 func_ref(name);
3797 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003798 }
3799 rettv->v_type = VAR_PARTIAL;
3800 rettv->vval.v_partial = pt;
3801 }
3802 else
3803 {
3804 /* result is a VAR_FUNC */
3805 rettv->v_type = VAR_FUNC;
3806 rettv->vval.v_string = name;
3807 func_ref(name);
3808 }
3809 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003810theend:
3811 vim_free(trans_name);
3812}
3813
3814/*
3815 * "funcref()" function
3816 */
3817 static void
3818f_funcref(typval_T *argvars, typval_T *rettv)
3819{
3820 common_function(argvars, rettv, TRUE);
3821}
3822
3823/*
3824 * "function()" function
3825 */
3826 static void
3827f_function(typval_T *argvars, typval_T *rettv)
3828{
3829 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003830}
3831
3832/*
3833 * "garbagecollect()" function
3834 */
3835 static void
3836f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
3837{
3838 /* This is postponed until we are back at the toplevel, because we may be
3839 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
3840 want_garbage_collect = TRUE;
3841
3842 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
3843 garbage_collect_at_exit = TRUE;
3844}
3845
3846/*
3847 * "get()" function
3848 */
3849 static void
3850f_get(typval_T *argvars, typval_T *rettv)
3851{
3852 listitem_T *li;
3853 list_T *l;
3854 dictitem_T *di;
3855 dict_T *d;
3856 typval_T *tv = NULL;
3857
3858 if (argvars[0].v_type == VAR_LIST)
3859 {
3860 if ((l = argvars[0].vval.v_list) != NULL)
3861 {
3862 int error = FALSE;
3863
3864 li = list_find(l, (long)get_tv_number_chk(&argvars[1], &error));
3865 if (!error && li != NULL)
3866 tv = &li->li_tv;
3867 }
3868 }
3869 else if (argvars[0].v_type == VAR_DICT)
3870 {
3871 if ((d = argvars[0].vval.v_dict) != NULL)
3872 {
3873 di = dict_find(d, get_tv_string(&argvars[1]), -1);
3874 if (di != NULL)
3875 tv = &di->di_tv;
3876 }
3877 }
3878 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
3879 {
3880 partial_T *pt;
3881 partial_T fref_pt;
3882
3883 if (argvars[0].v_type == VAR_PARTIAL)
3884 pt = argvars[0].vval.v_partial;
3885 else
3886 {
3887 vim_memset(&fref_pt, 0, sizeof(fref_pt));
3888 fref_pt.pt_name = argvars[0].vval.v_string;
3889 pt = &fref_pt;
3890 }
3891
3892 if (pt != NULL)
3893 {
3894 char_u *what = get_tv_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003895 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003896
3897 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
3898 {
3899 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003900 n = partial_name(pt);
3901 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003902 rettv->vval.v_string = NULL;
3903 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003904 {
3905 rettv->vval.v_string = vim_strsave(n);
3906 if (rettv->v_type == VAR_FUNC)
3907 func_ref(rettv->vval.v_string);
3908 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003909 }
3910 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003911 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003912 else if (STRCMP(what, "args") == 0)
3913 {
3914 rettv->v_type = VAR_LIST;
3915 if (rettv_list_alloc(rettv) == OK)
3916 {
3917 int i;
3918
3919 for (i = 0; i < pt->pt_argc; ++i)
3920 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
3921 }
3922 }
3923 else
3924 EMSG2(_(e_invarg2), what);
3925 return;
3926 }
3927 }
3928 else
3929 EMSG2(_(e_listdictarg), "get()");
3930
3931 if (tv == NULL)
3932 {
3933 if (argvars[2].v_type != VAR_UNKNOWN)
3934 copy_tv(&argvars[2], rettv);
3935 }
3936 else
3937 copy_tv(tv, rettv);
3938}
3939
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003940#ifdef FEAT_SIGNS
3941/*
3942 * Returns information about signs placed in a buffer as list of dicts.
3943 */
3944 static void
3945get_buffer_signs(buf_T *buf, list_T *l)
3946{
3947 signlist_T *sign;
3948
3949 for (sign = buf->b_signlist; sign; sign = sign->next)
3950 {
3951 dict_T *d = dict_alloc();
3952
3953 if (d != NULL)
3954 {
3955 dict_add_nr_str(d, "id", sign->id, NULL);
3956 dict_add_nr_str(d, "lnum", sign->lnum, NULL);
Bram Moolenaar6a402ed2016-08-28 14:11:24 +02003957 dict_add_nr_str(d, "name", 0L, sign_typenr2name(sign->typenr));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003958
3959 list_append_dict(l, d);
3960 }
3961 }
3962}
3963#endif
3964
3965/*
3966 * Returns buffer options, variables and other attributes in a dictionary.
3967 */
3968 static dict_T *
3969get_buffer_info(buf_T *buf)
3970{
3971 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003972 tabpage_T *tp;
3973 win_T *wp;
3974 list_T *windows;
3975
3976 dict = dict_alloc();
3977 if (dict == NULL)
3978 return NULL;
3979
Bram Moolenaar33928832016-08-18 21:22:04 +02003980 dict_add_nr_str(dict, "bufnr", buf->b_fnum, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003981 dict_add_nr_str(dict, "name", 0L,
3982 buf->b_ffname != NULL ? buf->b_ffname : (char_u *)"");
Bram Moolenaarf845b872017-01-06 14:04:54 +01003983 dict_add_nr_str(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
3984 : buflist_findlnum(buf), NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003985 dict_add_nr_str(dict, "loaded", buf->b_ml.ml_mfp != NULL, NULL);
3986 dict_add_nr_str(dict, "listed", buf->b_p_bl, NULL);
3987 dict_add_nr_str(dict, "changed", bufIsChanged(buf), NULL);
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003988 dict_add_nr_str(dict, "changedtick", CHANGEDTICK(buf), NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003989 dict_add_nr_str(dict, "hidden",
3990 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0,
3991 NULL);
3992
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02003993 /* Get a reference to buffer variables */
3994 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003995
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003996 /* List of windows displaying this buffer */
3997 windows = list_alloc();
3998 if (windows != NULL)
3999 {
4000 FOR_ALL_TAB_WINDOWS(tp, wp)
4001 if (wp->w_buffer == buf)
4002 list_append_number(windows, (varnumber_T)wp->w_id);
4003 dict_add_list(dict, "windows", windows);
4004 }
4005
4006#ifdef FEAT_SIGNS
4007 if (buf->b_signlist != NULL)
4008 {
4009 /* List of signs placed in this buffer */
4010 list_T *signs = list_alloc();
4011 if (signs != NULL)
4012 {
4013 get_buffer_signs(buf, signs);
4014 dict_add_list(dict, "signs", signs);
4015 }
4016 }
4017#endif
4018
4019 return dict;
4020}
4021
4022/*
4023 * "getbufinfo()" function
4024 */
4025 static void
4026f_getbufinfo(typval_T *argvars, typval_T *rettv)
4027{
4028 buf_T *buf = NULL;
4029 buf_T *argbuf = NULL;
4030 dict_T *d;
4031 int filtered = FALSE;
4032 int sel_buflisted = FALSE;
4033 int sel_bufloaded = FALSE;
4034
4035 if (rettv_list_alloc(rettv) != OK)
4036 return;
4037
4038 /* List of all the buffers or selected buffers */
4039 if (argvars[0].v_type == VAR_DICT)
4040 {
4041 dict_T *sel_d = argvars[0].vval.v_dict;
4042
4043 if (sel_d != NULL)
4044 {
4045 dictitem_T *di;
4046
4047 filtered = TRUE;
4048
4049 di = dict_find(sel_d, (char_u *)"buflisted", -1);
4050 if (di != NULL && get_tv_number(&di->di_tv))
4051 sel_buflisted = TRUE;
4052
4053 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
4054 if (di != NULL && get_tv_number(&di->di_tv))
4055 sel_bufloaded = TRUE;
4056 }
4057 }
4058 else if (argvars[0].v_type != VAR_UNKNOWN)
4059 {
4060 /* Information about one buffer. Argument specifies the buffer */
4061 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4062 ++emsg_off;
4063 argbuf = get_buf_tv(&argvars[0], FALSE);
4064 --emsg_off;
4065 if (argbuf == NULL)
4066 return;
4067 }
4068
4069 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004070 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004071 {
4072 if (argbuf != NULL && argbuf != buf)
4073 continue;
4074 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
4075 || (sel_buflisted && !buf->b_p_bl)))
4076 continue;
4077
4078 d = get_buffer_info(buf);
4079 if (d != NULL)
4080 list_append_dict(rettv->vval.v_list, d);
4081 if (argbuf != NULL)
4082 return;
4083 }
4084}
4085
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004086static void get_buffer_lines(buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv);
4087
4088/*
4089 * Get line or list of lines from buffer "buf" into "rettv".
4090 * Return a range (from start to end) of lines in rettv from the specified
4091 * buffer.
4092 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4093 */
4094 static void
4095get_buffer_lines(
4096 buf_T *buf,
4097 linenr_T start,
4098 linenr_T end,
4099 int retlist,
4100 typval_T *rettv)
4101{
4102 char_u *p;
4103
4104 rettv->v_type = VAR_STRING;
4105 rettv->vval.v_string = NULL;
4106 if (retlist && rettv_list_alloc(rettv) == FAIL)
4107 return;
4108
4109 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4110 return;
4111
4112 if (!retlist)
4113 {
4114 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4115 p = ml_get_buf(buf, start, FALSE);
4116 else
4117 p = (char_u *)"";
4118 rettv->vval.v_string = vim_strsave(p);
4119 }
4120 else
4121 {
4122 if (end < start)
4123 return;
4124
4125 if (start < 1)
4126 start = 1;
4127 if (end > buf->b_ml.ml_line_count)
4128 end = buf->b_ml.ml_line_count;
4129 while (start <= end)
4130 if (list_append_string(rettv->vval.v_list,
4131 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4132 break;
4133 }
4134}
4135
4136/*
4137 * Get the lnum from the first argument.
4138 * Also accepts "$", then "buf" is used.
4139 * Returns 0 on error.
4140 */
4141 static linenr_T
4142get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
4143{
4144 if (argvars[0].v_type == VAR_STRING
4145 && argvars[0].vval.v_string != NULL
4146 && argvars[0].vval.v_string[0] == '$'
4147 && buf != NULL)
4148 return buf->b_ml.ml_line_count;
4149 return (linenr_T)get_tv_number_chk(&argvars[0], NULL);
4150}
4151
4152/*
4153 * "getbufline()" function
4154 */
4155 static void
4156f_getbufline(typval_T *argvars, typval_T *rettv)
4157{
4158 linenr_T lnum;
4159 linenr_T end;
4160 buf_T *buf;
4161
4162 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4163 ++emsg_off;
4164 buf = get_buf_tv(&argvars[0], FALSE);
4165 --emsg_off;
4166
4167 lnum = get_tv_lnum_buf(&argvars[1], buf);
4168 if (argvars[2].v_type == VAR_UNKNOWN)
4169 end = lnum;
4170 else
4171 end = get_tv_lnum_buf(&argvars[2], buf);
4172
4173 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4174}
4175
4176/*
4177 * "getbufvar()" function
4178 */
4179 static void
4180f_getbufvar(typval_T *argvars, typval_T *rettv)
4181{
4182 buf_T *buf;
4183 buf_T *save_curbuf;
4184 char_u *varname;
4185 dictitem_T *v;
4186 int done = FALSE;
4187
4188 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4189 varname = get_tv_string_chk(&argvars[1]);
4190 ++emsg_off;
4191 buf = get_buf_tv(&argvars[0], FALSE);
4192
4193 rettv->v_type = VAR_STRING;
4194 rettv->vval.v_string = NULL;
4195
4196 if (buf != NULL && varname != NULL)
4197 {
4198 /* set curbuf to be our buf, temporarily */
4199 save_curbuf = curbuf;
4200 curbuf = buf;
4201
Bram Moolenaar30567352016-08-27 21:25:44 +02004202 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004203 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004204 if (varname[1] == NUL)
4205 {
4206 /* get all buffer-local options in a dict */
4207 dict_T *opts = get_winbuf_options(TRUE);
4208
4209 if (opts != NULL)
4210 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004211 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004212 done = TRUE;
4213 }
4214 }
4215 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4216 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004217 done = TRUE;
4218 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004219 else
4220 {
4221 /* Look up the variable. */
4222 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4223 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4224 'b', varname, FALSE);
4225 if (v != NULL)
4226 {
4227 copy_tv(&v->di_tv, rettv);
4228 done = TRUE;
4229 }
4230 }
4231
4232 /* restore previous notion of curbuf */
4233 curbuf = save_curbuf;
4234 }
4235
4236 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4237 /* use the default value */
4238 copy_tv(&argvars[2], rettv);
4239
4240 --emsg_off;
4241}
4242
4243/*
4244 * "getchar()" function
4245 */
4246 static void
4247f_getchar(typval_T *argvars, typval_T *rettv)
4248{
4249 varnumber_T n;
4250 int error = FALSE;
4251
4252 /* Position the cursor. Needed after a message that ends in a space. */
4253 windgoto(msg_row, msg_col);
4254
4255 ++no_mapping;
4256 ++allow_keys;
4257 for (;;)
4258 {
4259 if (argvars[0].v_type == VAR_UNKNOWN)
4260 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004261 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004262 else if (get_tv_number_chk(&argvars[0], &error) == 1)
4263 /* getchar(1): only check if char avail */
4264 n = vpeekc_any();
4265 else if (error || vpeekc_any() == NUL)
4266 /* illegal argument or getchar(0) and no char avail: return zero */
4267 n = 0;
4268 else
4269 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004270 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004271
4272 if (n == K_IGNORE)
4273 continue;
4274 break;
4275 }
4276 --no_mapping;
4277 --allow_keys;
4278
4279 set_vim_var_nr(VV_MOUSE_WIN, 0);
4280 set_vim_var_nr(VV_MOUSE_WINID, 0);
4281 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4282 set_vim_var_nr(VV_MOUSE_COL, 0);
4283
4284 rettv->vval.v_number = n;
4285 if (IS_SPECIAL(n) || mod_mask != 0)
4286 {
4287 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4288 int i = 0;
4289
4290 /* Turn a special key into three bytes, plus modifier. */
4291 if (mod_mask != 0)
4292 {
4293 temp[i++] = K_SPECIAL;
4294 temp[i++] = KS_MODIFIER;
4295 temp[i++] = mod_mask;
4296 }
4297 if (IS_SPECIAL(n))
4298 {
4299 temp[i++] = K_SPECIAL;
4300 temp[i++] = K_SECOND(n);
4301 temp[i++] = K_THIRD(n);
4302 }
4303#ifdef FEAT_MBYTE
4304 else if (has_mbyte)
4305 i += (*mb_char2bytes)(n, temp + i);
4306#endif
4307 else
4308 temp[i++] = n;
4309 temp[i++] = NUL;
4310 rettv->v_type = VAR_STRING;
4311 rettv->vval.v_string = vim_strsave(temp);
4312
4313#ifdef FEAT_MOUSE
4314 if (is_mouse_key(n))
4315 {
4316 int row = mouse_row;
4317 int col = mouse_col;
4318 win_T *win;
4319 linenr_T lnum;
4320# ifdef FEAT_WINDOWS
4321 win_T *wp;
4322# endif
4323 int winnr = 1;
4324
4325 if (row >= 0 && col >= 0)
4326 {
4327 /* Find the window at the mouse coordinates and compute the
4328 * text position. */
4329 win = mouse_find_win(&row, &col);
4330 (void)mouse_comp_pos(win, &row, &col, &lnum);
4331# ifdef FEAT_WINDOWS
4332 for (wp = firstwin; wp != win; wp = wp->w_next)
4333 ++winnr;
4334# endif
4335 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4336 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4337 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4338 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4339 }
4340 }
4341#endif
4342 }
4343}
4344
4345/*
4346 * "getcharmod()" function
4347 */
4348 static void
4349f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4350{
4351 rettv->vval.v_number = mod_mask;
4352}
4353
4354/*
4355 * "getcharsearch()" function
4356 */
4357 static void
4358f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4359{
4360 if (rettv_dict_alloc(rettv) != FAIL)
4361 {
4362 dict_T *dict = rettv->vval.v_dict;
4363
4364 dict_add_nr_str(dict, "char", 0L, last_csearch());
4365 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
4366 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
4367 }
4368}
4369
4370/*
4371 * "getcmdline()" function
4372 */
4373 static void
4374f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4375{
4376 rettv->v_type = VAR_STRING;
4377 rettv->vval.v_string = get_cmdline_str();
4378}
4379
4380/*
4381 * "getcmdpos()" function
4382 */
4383 static void
4384f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4385{
4386 rettv->vval.v_number = get_cmdline_pos() + 1;
4387}
4388
4389/*
4390 * "getcmdtype()" function
4391 */
4392 static void
4393f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4394{
4395 rettv->v_type = VAR_STRING;
4396 rettv->vval.v_string = alloc(2);
4397 if (rettv->vval.v_string != NULL)
4398 {
4399 rettv->vval.v_string[0] = get_cmdline_type();
4400 rettv->vval.v_string[1] = NUL;
4401 }
4402}
4403
4404/*
4405 * "getcmdwintype()" function
4406 */
4407 static void
4408f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4409{
4410 rettv->v_type = VAR_STRING;
4411 rettv->vval.v_string = NULL;
4412#ifdef FEAT_CMDWIN
4413 rettv->vval.v_string = alloc(2);
4414 if (rettv->vval.v_string != NULL)
4415 {
4416 rettv->vval.v_string[0] = cmdwin_type;
4417 rettv->vval.v_string[1] = NUL;
4418 }
4419#endif
4420}
4421
4422#if defined(FEAT_CMDL_COMPL)
4423/*
4424 * "getcompletion()" function
4425 */
4426 static void
4427f_getcompletion(typval_T *argvars, typval_T *rettv)
4428{
4429 char_u *pat;
4430 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004431 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004432 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
4433 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004434
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004435 if (argvars[2].v_type != VAR_UNKNOWN)
4436 filtered = get_tv_number_chk(&argvars[2], NULL);
4437
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004438 if (p_wic)
4439 options |= WILD_ICASE;
4440
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004441 /* For filtered results, 'wildignore' is used */
4442 if (!filtered)
4443 options |= WILD_KEEP_ALL;
4444
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004445 ExpandInit(&xpc);
4446 xpc.xp_pattern = get_tv_string(&argvars[0]);
4447 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4448 xpc.xp_context = cmdcomplete_str_to_type(get_tv_string(&argvars[1]));
4449 if (xpc.xp_context == EXPAND_NOTHING)
4450 {
4451 if (argvars[1].v_type == VAR_STRING)
4452 EMSG2(_(e_invarg2), argvars[1].vval.v_string);
4453 else
4454 EMSG(_(e_invarg));
4455 return;
4456 }
4457
4458# if defined(FEAT_MENU)
4459 if (xpc.xp_context == EXPAND_MENUS)
4460 {
4461 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4462 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4463 }
4464# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02004465#ifdef FEAT_CSCOPE
4466 if (xpc.xp_context == EXPAND_CSCOPE)
4467 {
4468 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4469 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4470 }
4471#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02004472#ifdef FEAT_SIGNS
4473 if (xpc.xp_context == EXPAND_SIGN)
4474 {
4475 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4476 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4477 }
4478#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004479
4480 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4481 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
4482 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004483 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004484
4485 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4486
4487 for (i = 0; i < xpc.xp_numfiles; i++)
4488 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4489 }
4490 vim_free(pat);
4491 ExpandCleanup(&xpc);
4492}
4493#endif
4494
4495/*
4496 * "getcwd()" function
4497 */
4498 static void
4499f_getcwd(typval_T *argvars, typval_T *rettv)
4500{
4501 win_T *wp = NULL;
4502 char_u *cwd;
4503
4504 rettv->v_type = VAR_STRING;
4505 rettv->vval.v_string = NULL;
4506
4507 wp = find_tabwin(&argvars[0], &argvars[1]);
4508 if (wp != NULL)
4509 {
4510 if (wp->w_localdir != NULL)
4511 rettv->vval.v_string = vim_strsave(wp->w_localdir);
4512 else if (globaldir != NULL)
4513 rettv->vval.v_string = vim_strsave(globaldir);
4514 else
4515 {
4516 cwd = alloc(MAXPATHL);
4517 if (cwd != NULL)
4518 {
4519 if (mch_dirname(cwd, MAXPATHL) != FAIL)
4520 rettv->vval.v_string = vim_strsave(cwd);
4521 vim_free(cwd);
4522 }
4523 }
4524#ifdef BACKSLASH_IN_FILENAME
4525 if (rettv->vval.v_string != NULL)
4526 slash_adjust(rettv->vval.v_string);
4527#endif
4528 }
4529}
4530
4531/*
4532 * "getfontname()" function
4533 */
4534 static void
4535f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4536{
4537 rettv->v_type = VAR_STRING;
4538 rettv->vval.v_string = NULL;
4539#ifdef FEAT_GUI
4540 if (gui.in_use)
4541 {
4542 GuiFont font;
4543 char_u *name = NULL;
4544
4545 if (argvars[0].v_type == VAR_UNKNOWN)
4546 {
4547 /* Get the "Normal" font. Either the name saved by
4548 * hl_set_font_name() or from the font ID. */
4549 font = gui.norm_font;
4550 name = hl_get_font_name();
4551 }
4552 else
4553 {
4554 name = get_tv_string(&argvars[0]);
4555 if (STRCMP(name, "*") == 0) /* don't use font dialog */
4556 return;
4557 font = gui_mch_get_font(name, FALSE);
4558 if (font == NOFONT)
4559 return; /* Invalid font name, return empty string. */
4560 }
4561 rettv->vval.v_string = gui_mch_get_fontname(font, name);
4562 if (argvars[0].v_type != VAR_UNKNOWN)
4563 gui_mch_free_font(font);
4564 }
4565#endif
4566}
4567
4568/*
4569 * "getfperm({fname})" function
4570 */
4571 static void
4572f_getfperm(typval_T *argvars, typval_T *rettv)
4573{
4574 char_u *fname;
4575 stat_T st;
4576 char_u *perm = NULL;
4577 char_u flags[] = "rwx";
4578 int i;
4579
4580 fname = get_tv_string(&argvars[0]);
4581
4582 rettv->v_type = VAR_STRING;
4583 if (mch_stat((char *)fname, &st) >= 0)
4584 {
4585 perm = vim_strsave((char_u *)"---------");
4586 if (perm != NULL)
4587 {
4588 for (i = 0; i < 9; i++)
4589 {
4590 if (st.st_mode & (1 << (8 - i)))
4591 perm[i] = flags[i % 3];
4592 }
4593 }
4594 }
4595 rettv->vval.v_string = perm;
4596}
4597
4598/*
4599 * "getfsize({fname})" function
4600 */
4601 static void
4602f_getfsize(typval_T *argvars, typval_T *rettv)
4603{
4604 char_u *fname;
4605 stat_T st;
4606
4607 fname = get_tv_string(&argvars[0]);
4608
4609 rettv->v_type = VAR_NUMBER;
4610
4611 if (mch_stat((char *)fname, &st) >= 0)
4612 {
4613 if (mch_isdir(fname))
4614 rettv->vval.v_number = 0;
4615 else
4616 {
4617 rettv->vval.v_number = (varnumber_T)st.st_size;
4618
4619 /* non-perfect check for overflow */
4620 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
4621 rettv->vval.v_number = -2;
4622 }
4623 }
4624 else
4625 rettv->vval.v_number = -1;
4626}
4627
4628/*
4629 * "getftime({fname})" function
4630 */
4631 static void
4632f_getftime(typval_T *argvars, typval_T *rettv)
4633{
4634 char_u *fname;
4635 stat_T st;
4636
4637 fname = get_tv_string(&argvars[0]);
4638
4639 if (mch_stat((char *)fname, &st) >= 0)
4640 rettv->vval.v_number = (varnumber_T)st.st_mtime;
4641 else
4642 rettv->vval.v_number = -1;
4643}
4644
4645/*
4646 * "getftype({fname})" function
4647 */
4648 static void
4649f_getftype(typval_T *argvars, typval_T *rettv)
4650{
4651 char_u *fname;
4652 stat_T st;
4653 char_u *type = NULL;
4654 char *t;
4655
4656 fname = get_tv_string(&argvars[0]);
4657
4658 rettv->v_type = VAR_STRING;
4659 if (mch_lstat((char *)fname, &st) >= 0)
4660 {
4661#ifdef S_ISREG
4662 if (S_ISREG(st.st_mode))
4663 t = "file";
4664 else if (S_ISDIR(st.st_mode))
4665 t = "dir";
4666# ifdef S_ISLNK
4667 else if (S_ISLNK(st.st_mode))
4668 t = "link";
4669# endif
4670# ifdef S_ISBLK
4671 else if (S_ISBLK(st.st_mode))
4672 t = "bdev";
4673# endif
4674# ifdef S_ISCHR
4675 else if (S_ISCHR(st.st_mode))
4676 t = "cdev";
4677# endif
4678# ifdef S_ISFIFO
4679 else if (S_ISFIFO(st.st_mode))
4680 t = "fifo";
4681# endif
4682# ifdef S_ISSOCK
4683 else if (S_ISSOCK(st.st_mode))
4684 t = "fifo";
4685# endif
4686 else
4687 t = "other";
4688#else
4689# ifdef S_IFMT
4690 switch (st.st_mode & S_IFMT)
4691 {
4692 case S_IFREG: t = "file"; break;
4693 case S_IFDIR: t = "dir"; break;
4694# ifdef S_IFLNK
4695 case S_IFLNK: t = "link"; break;
4696# endif
4697# ifdef S_IFBLK
4698 case S_IFBLK: t = "bdev"; break;
4699# endif
4700# ifdef S_IFCHR
4701 case S_IFCHR: t = "cdev"; break;
4702# endif
4703# ifdef S_IFIFO
4704 case S_IFIFO: t = "fifo"; break;
4705# endif
4706# ifdef S_IFSOCK
4707 case S_IFSOCK: t = "socket"; break;
4708# endif
4709 default: t = "other";
4710 }
4711# else
4712 if (mch_isdir(fname))
4713 t = "dir";
4714 else
4715 t = "file";
4716# endif
4717#endif
4718 type = vim_strsave((char_u *)t);
4719 }
4720 rettv->vval.v_string = type;
4721}
4722
4723/*
4724 * "getline(lnum, [end])" function
4725 */
4726 static void
4727f_getline(typval_T *argvars, typval_T *rettv)
4728{
4729 linenr_T lnum;
4730 linenr_T end;
4731 int retlist;
4732
4733 lnum = get_tv_lnum(argvars);
4734 if (argvars[1].v_type == VAR_UNKNOWN)
4735 {
4736 end = 0;
4737 retlist = FALSE;
4738 }
4739 else
4740 {
4741 end = get_tv_lnum(&argvars[1]);
4742 retlist = TRUE;
4743 }
4744
4745 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
4746}
4747
Bram Moolenaar4ae20952016-08-13 15:29:14 +02004748#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02004749 static void
4750get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
4751{
Bram Moolenaard823fa92016-08-12 16:29:27 +02004752 if (what_arg->v_type == VAR_UNKNOWN)
4753 {
4754 if (rettv_list_alloc(rettv) == OK)
4755 if (is_qf || wp != NULL)
4756 (void)get_errorlist(wp, -1, rettv->vval.v_list);
4757 }
4758 else
4759 {
4760 if (rettv_dict_alloc(rettv) == OK)
4761 if (is_qf || (wp != NULL))
4762 {
4763 if (what_arg->v_type == VAR_DICT)
4764 {
4765 dict_T *d = what_arg->vval.v_dict;
4766
4767 if (d != NULL)
4768 get_errorlist_properties(wp, d, rettv->vval.v_dict);
4769 }
4770 else
4771 EMSG(_(e_dictreq));
4772 }
4773 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004774}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02004775#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02004776
4777/*
4778 * "getloclist()" function
4779 */
4780 static void
4781f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4782{
4783#ifdef FEAT_QUICKFIX
4784 win_T *wp;
4785
4786 wp = find_win_by_nr(&argvars[0], NULL);
4787 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
4788#endif
4789}
4790
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004791/*
4792 * "getmatches()" function
4793 */
4794 static void
4795f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4796{
4797#ifdef FEAT_SEARCH_EXTRA
4798 dict_T *dict;
4799 matchitem_T *cur = curwin->w_match_head;
4800 int i;
4801
4802 if (rettv_list_alloc(rettv) == OK)
4803 {
4804 while (cur != NULL)
4805 {
4806 dict = dict_alloc();
4807 if (dict == NULL)
4808 return;
4809 if (cur->match.regprog == NULL)
4810 {
4811 /* match added with matchaddpos() */
4812 for (i = 0; i < MAXPOSMATCH; ++i)
4813 {
4814 llpos_T *llpos;
4815 char buf[6];
4816 list_T *l;
4817
4818 llpos = &cur->pos.pos[i];
4819 if (llpos->lnum == 0)
4820 break;
4821 l = list_alloc();
4822 if (l == NULL)
4823 break;
4824 list_append_number(l, (varnumber_T)llpos->lnum);
4825 if (llpos->col > 0)
4826 {
4827 list_append_number(l, (varnumber_T)llpos->col);
4828 list_append_number(l, (varnumber_T)llpos->len);
4829 }
4830 sprintf(buf, "pos%d", i + 1);
4831 dict_add_list(dict, buf, l);
4832 }
4833 }
4834 else
4835 {
4836 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
4837 }
4838 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
4839 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
4840 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
4841# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
4842 if (cur->conceal_char)
4843 {
4844 char_u buf[MB_MAXBYTES + 1];
4845
4846 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
4847 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
4848 }
4849# endif
4850 list_append_dict(rettv->vval.v_list, dict);
4851 cur = cur->next;
4852 }
4853 }
4854#endif
4855}
4856
4857/*
4858 * "getpid()" function
4859 */
4860 static void
4861f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
4862{
4863 rettv->vval.v_number = mch_get_pid();
4864}
4865
4866 static void
4867getpos_both(
4868 typval_T *argvars,
4869 typval_T *rettv,
4870 int getcurpos)
4871{
4872 pos_T *fp;
4873 list_T *l;
4874 int fnum = -1;
4875
4876 if (rettv_list_alloc(rettv) == OK)
4877 {
4878 l = rettv->vval.v_list;
4879 if (getcurpos)
4880 fp = &curwin->w_cursor;
4881 else
4882 fp = var2fpos(&argvars[0], TRUE, &fnum);
4883 if (fnum != -1)
4884 list_append_number(l, (varnumber_T)fnum);
4885 else
4886 list_append_number(l, (varnumber_T)0);
4887 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
4888 : (varnumber_T)0);
4889 list_append_number(l, (fp != NULL)
4890 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
4891 : (varnumber_T)0);
4892 list_append_number(l,
4893#ifdef FEAT_VIRTUALEDIT
4894 (fp != NULL) ? (varnumber_T)fp->coladd :
4895#endif
4896 (varnumber_T)0);
4897 if (getcurpos)
4898 {
4899 update_curswant();
4900 list_append_number(l, curwin->w_curswant == MAXCOL ?
4901 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
4902 }
4903 }
4904 else
4905 rettv->vval.v_number = FALSE;
4906}
4907
4908
4909/*
4910 * "getcurpos()" function
4911 */
4912 static void
4913f_getcurpos(typval_T *argvars, typval_T *rettv)
4914{
4915 getpos_both(argvars, rettv, TRUE);
4916}
4917
4918/*
4919 * "getpos(string)" function
4920 */
4921 static void
4922f_getpos(typval_T *argvars, typval_T *rettv)
4923{
4924 getpos_both(argvars, rettv, FALSE);
4925}
4926
4927/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004928 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004929 */
4930 static void
4931f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4932{
4933#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02004934 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004935#endif
4936}
4937
4938/*
4939 * "getreg()" function
4940 */
4941 static void
4942f_getreg(typval_T *argvars, typval_T *rettv)
4943{
4944 char_u *strregname;
4945 int regname;
4946 int arg2 = FALSE;
4947 int return_list = FALSE;
4948 int error = FALSE;
4949
4950 if (argvars[0].v_type != VAR_UNKNOWN)
4951 {
4952 strregname = get_tv_string_chk(&argvars[0]);
4953 error = strregname == NULL;
4954 if (argvars[1].v_type != VAR_UNKNOWN)
4955 {
4956 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
4957 if (!error && argvars[2].v_type != VAR_UNKNOWN)
4958 return_list = (int)get_tv_number_chk(&argvars[2], &error);
4959 }
4960 }
4961 else
4962 strregname = get_vim_var_str(VV_REG);
4963
4964 if (error)
4965 return;
4966
4967 regname = (strregname == NULL ? '"' : *strregname);
4968 if (regname == 0)
4969 regname = '"';
4970
4971 if (return_list)
4972 {
4973 rettv->v_type = VAR_LIST;
4974 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
4975 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
4976 if (rettv->vval.v_list == NULL)
4977 (void)rettv_list_alloc(rettv);
4978 else
4979 ++rettv->vval.v_list->lv_refcount;
4980 }
4981 else
4982 {
4983 rettv->v_type = VAR_STRING;
4984 rettv->vval.v_string = get_reg_contents(regname,
4985 arg2 ? GREG_EXPR_SRC : 0);
4986 }
4987}
4988
4989/*
4990 * "getregtype()" function
4991 */
4992 static void
4993f_getregtype(typval_T *argvars, typval_T *rettv)
4994{
4995 char_u *strregname;
4996 int regname;
4997 char_u buf[NUMBUFLEN + 2];
4998 long reglen = 0;
4999
5000 if (argvars[0].v_type != VAR_UNKNOWN)
5001 {
5002 strregname = get_tv_string_chk(&argvars[0]);
5003 if (strregname == NULL) /* type error; errmsg already given */
5004 {
5005 rettv->v_type = VAR_STRING;
5006 rettv->vval.v_string = NULL;
5007 return;
5008 }
5009 }
5010 else
5011 /* Default to v:register */
5012 strregname = get_vim_var_str(VV_REG);
5013
5014 regname = (strregname == NULL ? '"' : *strregname);
5015 if (regname == 0)
5016 regname = '"';
5017
5018 buf[0] = NUL;
5019 buf[1] = NUL;
5020 switch (get_reg_type(regname, &reglen))
5021 {
5022 case MLINE: buf[0] = 'V'; break;
5023 case MCHAR: buf[0] = 'v'; break;
5024 case MBLOCK:
5025 buf[0] = Ctrl_V;
5026 sprintf((char *)buf + 1, "%ld", reglen + 1);
5027 break;
5028 }
5029 rettv->v_type = VAR_STRING;
5030 rettv->vval.v_string = vim_strsave(buf);
5031}
5032
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005033#ifdef FEAT_WINDOWS
5034/*
5035 * Returns information (variables, options, etc.) about a tab page
5036 * as a dictionary.
5037 */
5038 static dict_T *
5039get_tabpage_info(tabpage_T *tp, int tp_idx)
5040{
5041 win_T *wp;
5042 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005043 list_T *l;
5044
5045 dict = dict_alloc();
5046 if (dict == NULL)
5047 return NULL;
5048
Bram Moolenaar33928832016-08-18 21:22:04 +02005049 dict_add_nr_str(dict, "tabnr", tp_idx, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005050
5051 l = list_alloc();
5052 if (l != NULL)
5053 {
5054 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5055 wp; wp = wp->w_next)
5056 list_append_number(l, (varnumber_T)wp->w_id);
5057 dict_add_list(dict, "windows", l);
5058 }
5059
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005060 /* Make a reference to tabpage variables */
5061 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005062
5063 return dict;
5064}
5065#endif
5066
5067/*
5068 * "gettabinfo()" function
5069 */
5070 static void
5071f_gettabinfo(typval_T *argvars, typval_T *rettv)
5072{
5073#ifdef FEAT_WINDOWS
5074 tabpage_T *tp, *tparg = NULL;
5075 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005076 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005077
5078 if (rettv_list_alloc(rettv) != OK)
5079 return;
5080
5081 if (argvars[0].v_type != VAR_UNKNOWN)
5082 {
5083 /* Information about one tab page */
5084 tparg = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5085 if (tparg == NULL)
5086 return;
5087 }
5088
5089 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005090 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005091 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005092 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005093 if (tparg != NULL && tp != tparg)
5094 continue;
5095 d = get_tabpage_info(tp, tpnr);
5096 if (d != NULL)
5097 list_append_dict(rettv->vval.v_list, d);
5098 if (tparg != NULL)
5099 return;
5100 }
5101#endif
5102}
5103
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005104/*
5105 * "gettabvar()" function
5106 */
5107 static void
5108f_gettabvar(typval_T *argvars, typval_T *rettv)
5109{
5110 win_T *oldcurwin;
5111 tabpage_T *tp, *oldtabpage;
5112 dictitem_T *v;
5113 char_u *varname;
5114 int done = FALSE;
5115
5116 rettv->v_type = VAR_STRING;
5117 rettv->vval.v_string = NULL;
5118
5119 varname = get_tv_string_chk(&argvars[1]);
5120 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5121 if (tp != NULL && varname != NULL)
5122 {
5123 /* Set tp to be our tabpage, temporarily. Also set the window to the
5124 * first window in the tabpage, otherwise the window is not valid. */
5125 if (switch_win(&oldcurwin, &oldtabpage,
5126 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
5127 == OK)
5128 {
5129 /* look up the variable */
5130 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5131 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5132 if (v != NULL)
5133 {
5134 copy_tv(&v->di_tv, rettv);
5135 done = TRUE;
5136 }
5137 }
5138
5139 /* restore previous notion of curwin */
5140 restore_win(oldcurwin, oldtabpage, TRUE);
5141 }
5142
5143 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5144 copy_tv(&argvars[2], rettv);
5145}
5146
5147/*
5148 * "gettabwinvar()" function
5149 */
5150 static void
5151f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5152{
5153 getwinvar(argvars, rettv, 1);
5154}
5155
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005156#ifdef FEAT_WINDOWS
5157/*
5158 * Returns information about a window as a dictionary.
5159 */
5160 static dict_T *
5161get_win_info(win_T *wp, short tpnr, short winnr)
5162{
5163 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005164
5165 dict = dict_alloc();
5166 if (dict == NULL)
5167 return NULL;
5168
Bram Moolenaar33928832016-08-18 21:22:04 +02005169 dict_add_nr_str(dict, "tabnr", tpnr, NULL);
5170 dict_add_nr_str(dict, "winnr", winnr, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005171 dict_add_nr_str(dict, "winid", wp->w_id, NULL);
5172 dict_add_nr_str(dict, "height", wp->w_height, NULL);
5173 dict_add_nr_str(dict, "width", wp->w_width, NULL);
Bram Moolenaar33928832016-08-18 21:22:04 +02005174 dict_add_nr_str(dict, "bufnr", wp->w_buffer->b_fnum, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005175
Bram Moolenaar386600f2016-08-15 22:16:25 +02005176#ifdef FEAT_QUICKFIX
5177 dict_add_nr_str(dict, "quickfix", bt_quickfix(wp->w_buffer), NULL);
5178 dict_add_nr_str(dict, "loclist",
5179 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL), NULL);
5180#endif
5181
Bram Moolenaar30567352016-08-27 21:25:44 +02005182 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005183 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005184
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005185 return dict;
5186}
5187#endif
5188
5189/*
5190 * "getwininfo()" function
5191 */
5192 static void
5193f_getwininfo(typval_T *argvars, typval_T *rettv)
5194{
5195#ifdef FEAT_WINDOWS
5196 tabpage_T *tp;
5197 win_T *wp = NULL, *wparg = NULL;
5198 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005199 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005200#endif
5201
5202 if (rettv_list_alloc(rettv) != OK)
5203 return;
5204
5205#ifdef FEAT_WINDOWS
5206 if (argvars[0].v_type != VAR_UNKNOWN)
5207 {
5208 wparg = win_id2wp(argvars);
5209 if (wparg == NULL)
5210 return;
5211 }
5212
5213 /* Collect information about either all the windows across all the tab
5214 * pages or one particular window.
5215 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005216 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005217 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005218 tabnr++;
5219 winnr = 0;
5220 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005221 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005222 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005223 if (wparg != NULL && wp != wparg)
5224 continue;
5225 d = get_win_info(wp, tabnr, winnr);
5226 if (d != NULL)
5227 list_append_dict(rettv->vval.v_list, d);
5228 if (wparg != NULL)
5229 /* found information about a specific window */
5230 return;
5231 }
5232 }
5233#endif
5234}
5235
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005236/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005237 * "win_findbuf()" function
5238 */
5239 static void
5240f_win_findbuf(typval_T *argvars, typval_T *rettv)
5241{
5242 if (rettv_list_alloc(rettv) != FAIL)
5243 win_findbuf(argvars, rettv->vval.v_list);
5244}
5245
5246/*
5247 * "win_getid()" function
5248 */
5249 static void
5250f_win_getid(typval_T *argvars, typval_T *rettv)
5251{
5252 rettv->vval.v_number = win_getid(argvars);
5253}
5254
5255/*
5256 * "win_gotoid()" function
5257 */
5258 static void
5259f_win_gotoid(typval_T *argvars, typval_T *rettv)
5260{
5261 rettv->vval.v_number = win_gotoid(argvars);
5262}
5263
5264/*
5265 * "win_id2tabwin()" function
5266 */
5267 static void
5268f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5269{
5270 if (rettv_list_alloc(rettv) != FAIL)
5271 win_id2tabwin(argvars, rettv->vval.v_list);
5272}
5273
5274/*
5275 * "win_id2win()" function
5276 */
5277 static void
5278f_win_id2win(typval_T *argvars, typval_T *rettv)
5279{
5280 rettv->vval.v_number = win_id2win(argvars);
5281}
5282
5283/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005284 * "getwinposx()" function
5285 */
5286 static void
5287f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5288{
5289 rettv->vval.v_number = -1;
5290#ifdef FEAT_GUI
5291 if (gui.in_use)
5292 {
5293 int x, y;
5294
5295 if (gui_mch_get_winpos(&x, &y) == OK)
5296 rettv->vval.v_number = x;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005297 return;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005298 }
5299#endif
5300#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5301 {
5302 int x, y;
5303
5304 if (term_get_winpos(&x, &y) == OK)
5305 rettv->vval.v_number = x;
5306 }
5307#endif
5308}
5309
5310/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005311 * "getwinposy()" function
5312 */
5313 static void
5314f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5315{
5316 rettv->vval.v_number = -1;
5317#ifdef FEAT_GUI
5318 if (gui.in_use)
5319 {
5320 int x, y;
5321
5322 if (gui_mch_get_winpos(&x, &y) == OK)
5323 rettv->vval.v_number = y;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005324 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005325 }
5326#endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005327#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5328 {
5329 int x, y;
5330
5331 if (term_get_winpos(&x, &y) == OK)
5332 rettv->vval.v_number = y;
5333 }
5334#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005335}
5336
5337/*
5338 * "getwinvar()" function
5339 */
5340 static void
5341f_getwinvar(typval_T *argvars, typval_T *rettv)
5342{
5343 getwinvar(argvars, rettv, 0);
5344}
5345
5346/*
5347 * "glob()" function
5348 */
5349 static void
5350f_glob(typval_T *argvars, typval_T *rettv)
5351{
5352 int options = WILD_SILENT|WILD_USE_NL;
5353 expand_T xpc;
5354 int error = FALSE;
5355
5356 /* When the optional second argument is non-zero, don't remove matches
5357 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5358 rettv->v_type = VAR_STRING;
5359 if (argvars[1].v_type != VAR_UNKNOWN)
5360 {
5361 if (get_tv_number_chk(&argvars[1], &error))
5362 options |= WILD_KEEP_ALL;
5363 if (argvars[2].v_type != VAR_UNKNOWN)
5364 {
5365 if (get_tv_number_chk(&argvars[2], &error))
5366 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005367 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005368 }
5369 if (argvars[3].v_type != VAR_UNKNOWN
5370 && get_tv_number_chk(&argvars[3], &error))
5371 options |= WILD_ALLLINKS;
5372 }
5373 }
5374 if (!error)
5375 {
5376 ExpandInit(&xpc);
5377 xpc.xp_context = EXPAND_FILES;
5378 if (p_wic)
5379 options += WILD_ICASE;
5380 if (rettv->v_type == VAR_STRING)
5381 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
5382 NULL, options, WILD_ALL);
5383 else if (rettv_list_alloc(rettv) != FAIL)
5384 {
5385 int i;
5386
5387 ExpandOne(&xpc, get_tv_string(&argvars[0]),
5388 NULL, options, WILD_ALL_KEEP);
5389 for (i = 0; i < xpc.xp_numfiles; i++)
5390 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5391
5392 ExpandCleanup(&xpc);
5393 }
5394 }
5395 else
5396 rettv->vval.v_string = NULL;
5397}
5398
5399/*
5400 * "globpath()" function
5401 */
5402 static void
5403f_globpath(typval_T *argvars, typval_T *rettv)
5404{
5405 int flags = 0;
5406 char_u buf1[NUMBUFLEN];
5407 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
5408 int error = FALSE;
5409 garray_T ga;
5410 int i;
5411
5412 /* When the optional second argument is non-zero, don't remove matches
5413 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5414 rettv->v_type = VAR_STRING;
5415 if (argvars[2].v_type != VAR_UNKNOWN)
5416 {
5417 if (get_tv_number_chk(&argvars[2], &error))
5418 flags |= WILD_KEEP_ALL;
5419 if (argvars[3].v_type != VAR_UNKNOWN)
5420 {
5421 if (get_tv_number_chk(&argvars[3], &error))
5422 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005423 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005424 }
5425 if (argvars[4].v_type != VAR_UNKNOWN
5426 && get_tv_number_chk(&argvars[4], &error))
5427 flags |= WILD_ALLLINKS;
5428 }
5429 }
5430 if (file != NULL && !error)
5431 {
5432 ga_init2(&ga, (int)sizeof(char_u *), 10);
5433 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
5434 if (rettv->v_type == VAR_STRING)
5435 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
5436 else if (rettv_list_alloc(rettv) != FAIL)
5437 for (i = 0; i < ga.ga_len; ++i)
5438 list_append_string(rettv->vval.v_list,
5439 ((char_u **)(ga.ga_data))[i], -1);
5440 ga_clear_strings(&ga);
5441 }
5442 else
5443 rettv->vval.v_string = NULL;
5444}
5445
5446/*
5447 * "glob2regpat()" function
5448 */
5449 static void
5450f_glob2regpat(typval_T *argvars, typval_T *rettv)
5451{
5452 char_u *pat = get_tv_string_chk(&argvars[0]);
5453
5454 rettv->v_type = VAR_STRING;
5455 rettv->vval.v_string = (pat == NULL)
5456 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
5457}
5458
5459/* for VIM_VERSION_ defines */
5460#include "version.h"
5461
5462/*
5463 * "has()" function
5464 */
5465 static void
5466f_has(typval_T *argvars, typval_T *rettv)
5467{
5468 int i;
5469 char_u *name;
5470 int n = FALSE;
5471 static char *(has_list[]) =
5472 {
5473#ifdef AMIGA
5474 "amiga",
5475# ifdef FEAT_ARP
5476 "arp",
5477# endif
5478#endif
5479#ifdef __BEOS__
5480 "beos",
5481#endif
5482#ifdef MACOS
5483 "mac",
5484#endif
5485#if defined(MACOS_X_UNIX)
5486 "macunix", /* built with 'darwin' enabled */
5487#endif
5488#if defined(__APPLE__) && __APPLE__ == 1
5489 "osx", /* built with or without 'darwin' enabled */
5490#endif
5491#ifdef __QNX__
5492 "qnx",
5493#endif
5494#ifdef UNIX
5495 "unix",
5496#endif
5497#ifdef VMS
5498 "vms",
5499#endif
5500#ifdef WIN32
5501 "win32",
5502#endif
5503#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
5504 "win32unix",
5505#endif
5506#if defined(WIN64) || defined(_WIN64)
5507 "win64",
5508#endif
5509#ifdef EBCDIC
5510 "ebcdic",
5511#endif
5512#ifndef CASE_INSENSITIVE_FILENAME
5513 "fname_case",
5514#endif
5515#ifdef HAVE_ACL
5516 "acl",
5517#endif
5518#ifdef FEAT_ARABIC
5519 "arabic",
5520#endif
5521#ifdef FEAT_AUTOCMD
5522 "autocmd",
5523#endif
5524#ifdef FEAT_BEVAL
5525 "balloon_eval",
5526# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
5527 "balloon_multiline",
5528# endif
5529#endif
5530#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
5531 "builtin_terms",
5532# ifdef ALL_BUILTIN_TCAPS
5533 "all_builtin_terms",
5534# endif
5535#endif
5536#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
5537 || defined(FEAT_GUI_W32) \
5538 || defined(FEAT_GUI_MOTIF))
5539 "browsefilter",
5540#endif
5541#ifdef FEAT_BYTEOFF
5542 "byte_offset",
5543#endif
5544#ifdef FEAT_JOB_CHANNEL
5545 "channel",
5546#endif
5547#ifdef FEAT_CINDENT
5548 "cindent",
5549#endif
5550#ifdef FEAT_CLIENTSERVER
5551 "clientserver",
5552#endif
5553#ifdef FEAT_CLIPBOARD
5554 "clipboard",
5555#endif
5556#ifdef FEAT_CMDL_COMPL
5557 "cmdline_compl",
5558#endif
5559#ifdef FEAT_CMDHIST
5560 "cmdline_hist",
5561#endif
5562#ifdef FEAT_COMMENTS
5563 "comments",
5564#endif
5565#ifdef FEAT_CONCEAL
5566 "conceal",
5567#endif
5568#ifdef FEAT_CRYPT
5569 "cryptv",
5570 "crypt-blowfish",
5571 "crypt-blowfish2",
5572#endif
5573#ifdef FEAT_CSCOPE
5574 "cscope",
5575#endif
5576#ifdef FEAT_CURSORBIND
5577 "cursorbind",
5578#endif
5579#ifdef CURSOR_SHAPE
5580 "cursorshape",
5581#endif
5582#ifdef DEBUG
5583 "debug",
5584#endif
5585#ifdef FEAT_CON_DIALOG
5586 "dialog_con",
5587#endif
5588#ifdef FEAT_GUI_DIALOG
5589 "dialog_gui",
5590#endif
5591#ifdef FEAT_DIFF
5592 "diff",
5593#endif
5594#ifdef FEAT_DIGRAPHS
5595 "digraphs",
5596#endif
5597#ifdef FEAT_DIRECTX
5598 "directx",
5599#endif
5600#ifdef FEAT_DND
5601 "dnd",
5602#endif
5603#ifdef FEAT_EMACS_TAGS
5604 "emacs_tags",
5605#endif
5606 "eval", /* always present, of course! */
5607 "ex_extra", /* graduated feature */
5608#ifdef FEAT_SEARCH_EXTRA
5609 "extra_search",
5610#endif
5611#ifdef FEAT_FKMAP
5612 "farsi",
5613#endif
5614#ifdef FEAT_SEARCHPATH
5615 "file_in_path",
5616#endif
5617#ifdef FEAT_FILTERPIPE
5618 "filterpipe",
5619#endif
5620#ifdef FEAT_FIND_ID
5621 "find_in_path",
5622#endif
5623#ifdef FEAT_FLOAT
5624 "float",
5625#endif
5626#ifdef FEAT_FOLDING
5627 "folding",
5628#endif
5629#ifdef FEAT_FOOTER
5630 "footer",
5631#endif
5632#if !defined(USE_SYSTEM) && defined(UNIX)
5633 "fork",
5634#endif
5635#ifdef FEAT_GETTEXT
5636 "gettext",
5637#endif
5638#ifdef FEAT_GUI
5639 "gui",
5640#endif
5641#ifdef FEAT_GUI_ATHENA
5642# ifdef FEAT_GUI_NEXTAW
5643 "gui_neXtaw",
5644# else
5645 "gui_athena",
5646# endif
5647#endif
5648#ifdef FEAT_GUI_GTK
5649 "gui_gtk",
5650# ifdef USE_GTK3
5651 "gui_gtk3",
5652# else
5653 "gui_gtk2",
5654# endif
5655#endif
5656#ifdef FEAT_GUI_GNOME
5657 "gui_gnome",
5658#endif
5659#ifdef FEAT_GUI_MAC
5660 "gui_mac",
5661#endif
5662#ifdef FEAT_GUI_MOTIF
5663 "gui_motif",
5664#endif
5665#ifdef FEAT_GUI_PHOTON
5666 "gui_photon",
5667#endif
5668#ifdef FEAT_GUI_W32
5669 "gui_win32",
5670#endif
5671#ifdef FEAT_HANGULIN
5672 "hangul_input",
5673#endif
5674#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
5675 "iconv",
5676#endif
5677#ifdef FEAT_INS_EXPAND
5678 "insert_expand",
5679#endif
5680#ifdef FEAT_JOB_CHANNEL
5681 "job",
5682#endif
5683#ifdef FEAT_JUMPLIST
5684 "jumplist",
5685#endif
5686#ifdef FEAT_KEYMAP
5687 "keymap",
5688#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02005689 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005690#ifdef FEAT_LANGMAP
5691 "langmap",
5692#endif
5693#ifdef FEAT_LIBCALL
5694 "libcall",
5695#endif
5696#ifdef FEAT_LINEBREAK
5697 "linebreak",
5698#endif
5699#ifdef FEAT_LISP
5700 "lispindent",
5701#endif
5702#ifdef FEAT_LISTCMDS
5703 "listcmds",
5704#endif
5705#ifdef FEAT_LOCALMAP
5706 "localmap",
5707#endif
5708#ifdef FEAT_LUA
5709# ifndef DYNAMIC_LUA
5710 "lua",
5711# endif
5712#endif
5713#ifdef FEAT_MENU
5714 "menu",
5715#endif
5716#ifdef FEAT_SESSION
5717 "mksession",
5718#endif
5719#ifdef FEAT_MODIFY_FNAME
5720 "modify_fname",
5721#endif
5722#ifdef FEAT_MOUSE
5723 "mouse",
5724#endif
5725#ifdef FEAT_MOUSESHAPE
5726 "mouseshape",
5727#endif
5728#if defined(UNIX) || defined(VMS)
5729# ifdef FEAT_MOUSE_DEC
5730 "mouse_dec",
5731# endif
5732# ifdef FEAT_MOUSE_GPM
5733 "mouse_gpm",
5734# endif
5735# ifdef FEAT_MOUSE_JSB
5736 "mouse_jsbterm",
5737# endif
5738# ifdef FEAT_MOUSE_NET
5739 "mouse_netterm",
5740# endif
5741# ifdef FEAT_MOUSE_PTERM
5742 "mouse_pterm",
5743# endif
5744# ifdef FEAT_MOUSE_SGR
5745 "mouse_sgr",
5746# endif
5747# ifdef FEAT_SYSMOUSE
5748 "mouse_sysmouse",
5749# endif
5750# ifdef FEAT_MOUSE_URXVT
5751 "mouse_urxvt",
5752# endif
5753# ifdef FEAT_MOUSE_XTERM
5754 "mouse_xterm",
5755# endif
5756#endif
5757#ifdef FEAT_MBYTE
5758 "multi_byte",
5759#endif
5760#ifdef FEAT_MBYTE_IME
5761 "multi_byte_ime",
5762#endif
5763#ifdef FEAT_MULTI_LANG
5764 "multi_lang",
5765#endif
5766#ifdef FEAT_MZSCHEME
5767#ifndef DYNAMIC_MZSCHEME
5768 "mzscheme",
5769#endif
5770#endif
5771#ifdef FEAT_NUM64
5772 "num64",
5773#endif
5774#ifdef FEAT_OLE
5775 "ole",
5776#endif
5777 "packages",
5778#ifdef FEAT_PATH_EXTRA
5779 "path_extra",
5780#endif
5781#ifdef FEAT_PERL
5782#ifndef DYNAMIC_PERL
5783 "perl",
5784#endif
5785#endif
5786#ifdef FEAT_PERSISTENT_UNDO
5787 "persistent_undo",
5788#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005789#if defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005790 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005791 "pythonx",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005792#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005793#if defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005794 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005795 "pythonx",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005796#endif
5797#ifdef FEAT_POSTSCRIPT
5798 "postscript",
5799#endif
5800#ifdef FEAT_PRINTER
5801 "printer",
5802#endif
5803#ifdef FEAT_PROFILE
5804 "profile",
5805#endif
5806#ifdef FEAT_RELTIME
5807 "reltime",
5808#endif
5809#ifdef FEAT_QUICKFIX
5810 "quickfix",
5811#endif
5812#ifdef FEAT_RIGHTLEFT
5813 "rightleft",
5814#endif
5815#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
5816 "ruby",
5817#endif
5818#ifdef FEAT_SCROLLBIND
5819 "scrollbind",
5820#endif
5821#ifdef FEAT_CMDL_INFO
5822 "showcmd",
5823 "cmdline_info",
5824#endif
5825#ifdef FEAT_SIGNS
5826 "signs",
5827#endif
5828#ifdef FEAT_SMARTINDENT
5829 "smartindent",
5830#endif
5831#ifdef STARTUPTIME
5832 "startuptime",
5833#endif
5834#ifdef FEAT_STL_OPT
5835 "statusline",
5836#endif
5837#ifdef FEAT_SUN_WORKSHOP
5838 "sun_workshop",
5839#endif
5840#ifdef FEAT_NETBEANS_INTG
5841 "netbeans_intg",
5842#endif
5843#ifdef FEAT_SPELL
5844 "spell",
5845#endif
5846#ifdef FEAT_SYN_HL
5847 "syntax",
5848#endif
5849#if defined(USE_SYSTEM) || !defined(UNIX)
5850 "system",
5851#endif
5852#ifdef FEAT_TAG_BINS
5853 "tag_binary",
5854#endif
5855#ifdef FEAT_TAG_OLDSTATIC
5856 "tag_old_static",
5857#endif
5858#ifdef FEAT_TAG_ANYWHITE
5859 "tag_any_white",
5860#endif
5861#ifdef FEAT_TCL
5862# ifndef DYNAMIC_TCL
5863 "tcl",
5864# endif
5865#endif
5866#ifdef FEAT_TERMGUICOLORS
5867 "termguicolors",
5868#endif
5869#ifdef TERMINFO
5870 "terminfo",
5871#endif
5872#ifdef FEAT_TERMRESPONSE
5873 "termresponse",
5874#endif
5875#ifdef FEAT_TEXTOBJ
5876 "textobjects",
5877#endif
5878#ifdef HAVE_TGETENT
5879 "tgetent",
5880#endif
5881#ifdef FEAT_TIMERS
5882 "timers",
5883#endif
5884#ifdef FEAT_TITLE
5885 "title",
5886#endif
5887#ifdef FEAT_TOOLBAR
5888 "toolbar",
5889#endif
5890#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
5891 "unnamedplus",
5892#endif
5893#ifdef FEAT_USR_CMDS
5894 "user-commands", /* was accidentally included in 5.4 */
5895 "user_commands",
5896#endif
5897#ifdef FEAT_VIMINFO
5898 "viminfo",
5899#endif
5900#ifdef FEAT_WINDOWS
5901 "vertsplit",
5902#endif
5903#ifdef FEAT_VIRTUALEDIT
5904 "virtualedit",
5905#endif
5906 "visual",
5907#ifdef FEAT_VISUALEXTRA
5908 "visualextra",
5909#endif
5910#ifdef FEAT_VREPLACE
5911 "vreplace",
5912#endif
5913#ifdef FEAT_WILDIGN
5914 "wildignore",
5915#endif
5916#ifdef FEAT_WILDMENU
5917 "wildmenu",
5918#endif
5919#ifdef FEAT_WINDOWS
5920 "windows",
5921#endif
5922#ifdef FEAT_WAK
5923 "winaltkeys",
5924#endif
5925#ifdef FEAT_WRITEBACKUP
5926 "writebackup",
5927#endif
5928#ifdef FEAT_XIM
5929 "xim",
5930#endif
5931#ifdef FEAT_XFONTSET
5932 "xfontset",
5933#endif
5934#ifdef FEAT_XPM_W32
5935 "xpm",
5936 "xpm_w32", /* for backward compatibility */
5937#else
5938# if defined(HAVE_XPM)
5939 "xpm",
5940# endif
5941#endif
5942#ifdef USE_XSMP
5943 "xsmp",
5944#endif
5945#ifdef USE_XSMP_INTERACT
5946 "xsmp_interact",
5947#endif
5948#ifdef FEAT_XCLIPBOARD
5949 "xterm_clipboard",
5950#endif
5951#ifdef FEAT_XTERM_SAVE
5952 "xterm_save",
5953#endif
5954#if defined(UNIX) && defined(FEAT_X11)
5955 "X11",
5956#endif
5957 NULL
5958 };
5959
5960 name = get_tv_string(&argvars[0]);
5961 for (i = 0; has_list[i] != NULL; ++i)
5962 if (STRICMP(name, has_list[i]) == 0)
5963 {
5964 n = TRUE;
5965 break;
5966 }
5967
5968 if (n == FALSE)
5969 {
5970 if (STRNICMP(name, "patch", 5) == 0)
5971 {
5972 if (name[5] == '-'
5973 && STRLEN(name) >= 11
5974 && vim_isdigit(name[6])
5975 && vim_isdigit(name[8])
5976 && vim_isdigit(name[10]))
5977 {
5978 int major = atoi((char *)name + 6);
5979 int minor = atoi((char *)name + 8);
5980
5981 /* Expect "patch-9.9.01234". */
5982 n = (major < VIM_VERSION_MAJOR
5983 || (major == VIM_VERSION_MAJOR
5984 && (minor < VIM_VERSION_MINOR
5985 || (minor == VIM_VERSION_MINOR
5986 && has_patch(atoi((char *)name + 10))))));
5987 }
5988 else
5989 n = has_patch(atoi((char *)name + 5));
5990 }
5991 else if (STRICMP(name, "vim_starting") == 0)
5992 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01005993 else if (STRICMP(name, "ttyin") == 0)
5994 n = mch_input_isatty();
5995 else if (STRICMP(name, "ttyout") == 0)
5996 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005997#ifdef FEAT_MBYTE
5998 else if (STRICMP(name, "multi_byte_encoding") == 0)
5999 n = has_mbyte;
6000#endif
6001#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
6002 else if (STRICMP(name, "balloon_multiline") == 0)
6003 n = multiline_balloon_available();
6004#endif
6005#ifdef DYNAMIC_TCL
6006 else if (STRICMP(name, "tcl") == 0)
6007 n = tcl_enabled(FALSE);
6008#endif
6009#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6010 else if (STRICMP(name, "iconv") == 0)
6011 n = iconv_enabled(FALSE);
6012#endif
6013#ifdef DYNAMIC_LUA
6014 else if (STRICMP(name, "lua") == 0)
6015 n = lua_enabled(FALSE);
6016#endif
6017#ifdef DYNAMIC_MZSCHEME
6018 else if (STRICMP(name, "mzscheme") == 0)
6019 n = mzscheme_enabled(FALSE);
6020#endif
6021#ifdef DYNAMIC_RUBY
6022 else if (STRICMP(name, "ruby") == 0)
6023 n = ruby_enabled(FALSE);
6024#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006025#ifdef DYNAMIC_PYTHON
6026 else if (STRICMP(name, "python") == 0)
6027 n = python_enabled(FALSE);
6028#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006029#ifdef DYNAMIC_PYTHON3
6030 else if (STRICMP(name, "python3") == 0)
6031 n = python3_enabled(FALSE);
6032#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006033#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6034 else if (STRICMP(name, "pythonx") == 0)
6035 {
6036# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6037 if (p_pyx == 0)
6038 n = python3_enabled(FALSE) || python_enabled(FALSE);
6039 else if (p_pyx == 3)
6040 n = python3_enabled(FALSE);
6041 else if (p_pyx == 2)
6042 n = python_enabled(FALSE);
6043# elif defined(DYNAMIC_PYTHON)
6044 n = python_enabled(FALSE);
6045# elif defined(DYNAMIC_PYTHON3)
6046 n = python3_enabled(FALSE);
6047# endif
6048 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006049#endif
6050#ifdef DYNAMIC_PERL
6051 else if (STRICMP(name, "perl") == 0)
6052 n = perl_enabled(FALSE);
6053#endif
6054#ifdef FEAT_GUI
6055 else if (STRICMP(name, "gui_running") == 0)
6056 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006057# ifdef FEAT_BROWSE
6058 else if (STRICMP(name, "browse") == 0)
6059 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6060# endif
6061#endif
6062#ifdef FEAT_SYN_HL
6063 else if (STRICMP(name, "syntax_items") == 0)
6064 n = syntax_present(curwin);
6065#endif
6066#if defined(WIN3264)
6067 else if (STRICMP(name, "win95") == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006068 n = FALSE; /* Win9x is no more supported. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006069#endif
6070#ifdef FEAT_NETBEANS_INTG
6071 else if (STRICMP(name, "netbeans_enabled") == 0)
6072 n = netbeans_active();
6073#endif
6074 }
6075
6076 rettv->vval.v_number = n;
6077}
6078
6079/*
6080 * "has_key()" function
6081 */
6082 static void
6083f_has_key(typval_T *argvars, typval_T *rettv)
6084{
6085 if (argvars[0].v_type != VAR_DICT)
6086 {
6087 EMSG(_(e_dictreq));
6088 return;
6089 }
6090 if (argvars[0].vval.v_dict == NULL)
6091 return;
6092
6093 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
6094 get_tv_string(&argvars[1]), -1) != NULL;
6095}
6096
6097/*
6098 * "haslocaldir()" function
6099 */
6100 static void
6101f_haslocaldir(typval_T *argvars, typval_T *rettv)
6102{
6103 win_T *wp = NULL;
6104
6105 wp = find_tabwin(&argvars[0], &argvars[1]);
6106 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6107}
6108
6109/*
6110 * "hasmapto()" function
6111 */
6112 static void
6113f_hasmapto(typval_T *argvars, typval_T *rettv)
6114{
6115 char_u *name;
6116 char_u *mode;
6117 char_u buf[NUMBUFLEN];
6118 int abbr = FALSE;
6119
6120 name = get_tv_string(&argvars[0]);
6121 if (argvars[1].v_type == VAR_UNKNOWN)
6122 mode = (char_u *)"nvo";
6123 else
6124 {
6125 mode = get_tv_string_buf(&argvars[1], buf);
6126 if (argvars[2].v_type != VAR_UNKNOWN)
6127 abbr = (int)get_tv_number(&argvars[2]);
6128 }
6129
6130 if (map_to_exists(name, mode, abbr))
6131 rettv->vval.v_number = TRUE;
6132 else
6133 rettv->vval.v_number = FALSE;
6134}
6135
6136/*
6137 * "histadd()" function
6138 */
6139 static void
6140f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6141{
6142#ifdef FEAT_CMDHIST
6143 int histype;
6144 char_u *str;
6145 char_u buf[NUMBUFLEN];
6146#endif
6147
6148 rettv->vval.v_number = FALSE;
6149 if (check_restricted() || check_secure())
6150 return;
6151#ifdef FEAT_CMDHIST
6152 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6153 histype = str != NULL ? get_histtype(str) : -1;
6154 if (histype >= 0)
6155 {
6156 str = get_tv_string_buf(&argvars[1], buf);
6157 if (*str != NUL)
6158 {
6159 init_history();
6160 add_to_history(histype, str, FALSE, NUL);
6161 rettv->vval.v_number = TRUE;
6162 return;
6163 }
6164 }
6165#endif
6166}
6167
6168/*
6169 * "histdel()" function
6170 */
6171 static void
6172f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6173{
6174#ifdef FEAT_CMDHIST
6175 int n;
6176 char_u buf[NUMBUFLEN];
6177 char_u *str;
6178
6179 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6180 if (str == NULL)
6181 n = 0;
6182 else if (argvars[1].v_type == VAR_UNKNOWN)
6183 /* only one argument: clear entire history */
6184 n = clr_history(get_histtype(str));
6185 else if (argvars[1].v_type == VAR_NUMBER)
6186 /* index given: remove that entry */
6187 n = del_history_idx(get_histtype(str),
6188 (int)get_tv_number(&argvars[1]));
6189 else
6190 /* string given: remove all matching entries */
6191 n = del_history_entry(get_histtype(str),
6192 get_tv_string_buf(&argvars[1], buf));
6193 rettv->vval.v_number = n;
6194#endif
6195}
6196
6197/*
6198 * "histget()" function
6199 */
6200 static void
6201f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6202{
6203#ifdef FEAT_CMDHIST
6204 int type;
6205 int idx;
6206 char_u *str;
6207
6208 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6209 if (str == NULL)
6210 rettv->vval.v_string = NULL;
6211 else
6212 {
6213 type = get_histtype(str);
6214 if (argvars[1].v_type == VAR_UNKNOWN)
6215 idx = get_history_idx(type);
6216 else
6217 idx = (int)get_tv_number_chk(&argvars[1], NULL);
6218 /* -1 on type error */
6219 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6220 }
6221#else
6222 rettv->vval.v_string = NULL;
6223#endif
6224 rettv->v_type = VAR_STRING;
6225}
6226
6227/*
6228 * "histnr()" function
6229 */
6230 static void
6231f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6232{
6233 int i;
6234
6235#ifdef FEAT_CMDHIST
6236 char_u *history = get_tv_string_chk(&argvars[0]);
6237
6238 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6239 if (i >= HIST_CMD && i < HIST_COUNT)
6240 i = get_history_idx(i);
6241 else
6242#endif
6243 i = -1;
6244 rettv->vval.v_number = i;
6245}
6246
6247/*
6248 * "highlightID(name)" function
6249 */
6250 static void
6251f_hlID(typval_T *argvars, typval_T *rettv)
6252{
6253 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
6254}
6255
6256/*
6257 * "highlight_exists()" function
6258 */
6259 static void
6260f_hlexists(typval_T *argvars, typval_T *rettv)
6261{
6262 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
6263}
6264
6265/*
6266 * "hostname()" function
6267 */
6268 static void
6269f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6270{
6271 char_u hostname[256];
6272
6273 mch_get_host_name(hostname, 256);
6274 rettv->v_type = VAR_STRING;
6275 rettv->vval.v_string = vim_strsave(hostname);
6276}
6277
6278/*
6279 * iconv() function
6280 */
6281 static void
6282f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6283{
6284#ifdef FEAT_MBYTE
6285 char_u buf1[NUMBUFLEN];
6286 char_u buf2[NUMBUFLEN];
6287 char_u *from, *to, *str;
6288 vimconv_T vimconv;
6289#endif
6290
6291 rettv->v_type = VAR_STRING;
6292 rettv->vval.v_string = NULL;
6293
6294#ifdef FEAT_MBYTE
6295 str = get_tv_string(&argvars[0]);
6296 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
6297 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
6298 vimconv.vc_type = CONV_NONE;
6299 convert_setup(&vimconv, from, to);
6300
6301 /* If the encodings are equal, no conversion needed. */
6302 if (vimconv.vc_type == CONV_NONE)
6303 rettv->vval.v_string = vim_strsave(str);
6304 else
6305 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6306
6307 convert_setup(&vimconv, NULL, NULL);
6308 vim_free(from);
6309 vim_free(to);
6310#endif
6311}
6312
6313/*
6314 * "indent()" function
6315 */
6316 static void
6317f_indent(typval_T *argvars, typval_T *rettv)
6318{
6319 linenr_T lnum;
6320
6321 lnum = get_tv_lnum(argvars);
6322 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6323 rettv->vval.v_number = get_indent_lnum(lnum);
6324 else
6325 rettv->vval.v_number = -1;
6326}
6327
6328/*
6329 * "index()" function
6330 */
6331 static void
6332f_index(typval_T *argvars, typval_T *rettv)
6333{
6334 list_T *l;
6335 listitem_T *item;
6336 long idx = 0;
6337 int ic = FALSE;
6338
6339 rettv->vval.v_number = -1;
6340 if (argvars[0].v_type != VAR_LIST)
6341 {
6342 EMSG(_(e_listreq));
6343 return;
6344 }
6345 l = argvars[0].vval.v_list;
6346 if (l != NULL)
6347 {
6348 item = l->lv_first;
6349 if (argvars[2].v_type != VAR_UNKNOWN)
6350 {
6351 int error = FALSE;
6352
6353 /* Start at specified item. Use the cached index that list_find()
6354 * sets, so that a negative number also works. */
6355 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
6356 idx = l->lv_idx;
6357 if (argvars[3].v_type != VAR_UNKNOWN)
6358 ic = (int)get_tv_number_chk(&argvars[3], &error);
6359 if (error)
6360 item = NULL;
6361 }
6362
6363 for ( ; item != NULL; item = item->li_next, ++idx)
6364 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6365 {
6366 rettv->vval.v_number = idx;
6367 break;
6368 }
6369 }
6370}
6371
6372static int inputsecret_flag = 0;
6373
6374/*
6375 * "input()" function
6376 * Also handles inputsecret() when inputsecret is set.
6377 */
6378 static void
6379f_input(typval_T *argvars, typval_T *rettv)
6380{
6381 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6382}
6383
6384/*
6385 * "inputdialog()" function
6386 */
6387 static void
6388f_inputdialog(typval_T *argvars, typval_T *rettv)
6389{
6390#if defined(FEAT_GUI_TEXTDIALOG)
6391 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
6392 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6393 {
6394 char_u *message;
6395 char_u buf[NUMBUFLEN];
6396 char_u *defstr = (char_u *)"";
6397
6398 message = get_tv_string_chk(&argvars[0]);
6399 if (argvars[1].v_type != VAR_UNKNOWN
6400 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
6401 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6402 else
6403 IObuff[0] = NUL;
6404 if (message != NULL && defstr != NULL
6405 && do_dialog(VIM_QUESTION, NULL, message,
6406 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6407 rettv->vval.v_string = vim_strsave(IObuff);
6408 else
6409 {
6410 if (message != NULL && defstr != NULL
6411 && argvars[1].v_type != VAR_UNKNOWN
6412 && argvars[2].v_type != VAR_UNKNOWN)
6413 rettv->vval.v_string = vim_strsave(
6414 get_tv_string_buf(&argvars[2], buf));
6415 else
6416 rettv->vval.v_string = NULL;
6417 }
6418 rettv->v_type = VAR_STRING;
6419 }
6420 else
6421#endif
6422 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6423}
6424
6425/*
6426 * "inputlist()" function
6427 */
6428 static void
6429f_inputlist(typval_T *argvars, typval_T *rettv)
6430{
6431 listitem_T *li;
6432 int selected;
6433 int mouse_used;
6434
6435#ifdef NO_CONSOLE_INPUT
6436 /* While starting up, there is no place to enter text. */
6437 if (no_console_input())
6438 return;
6439#endif
6440 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6441 {
6442 EMSG2(_(e_listarg), "inputlist()");
6443 return;
6444 }
6445
6446 msg_start();
6447 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
6448 lines_left = Rows; /* avoid more prompt */
6449 msg_scroll = TRUE;
6450 msg_clr_eos();
6451
6452 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
6453 {
6454 msg_puts(get_tv_string(&li->li_tv));
6455 msg_putchar('\n');
6456 }
6457
6458 /* Ask for choice. */
6459 selected = prompt_for_number(&mouse_used);
6460 if (mouse_used)
6461 selected -= lines_left;
6462
6463 rettv->vval.v_number = selected;
6464}
6465
6466
6467static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6468
6469/*
6470 * "inputrestore()" function
6471 */
6472 static void
6473f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6474{
6475 if (ga_userinput.ga_len > 0)
6476 {
6477 --ga_userinput.ga_len;
6478 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
6479 + ga_userinput.ga_len);
6480 /* default return is zero == OK */
6481 }
6482 else if (p_verbose > 1)
6483 {
6484 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
6485 rettv->vval.v_number = 1; /* Failed */
6486 }
6487}
6488
6489/*
6490 * "inputsave()" function
6491 */
6492 static void
6493f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
6494{
6495 /* Add an entry to the stack of typeahead storage. */
6496 if (ga_grow(&ga_userinput, 1) == OK)
6497 {
6498 save_typeahead((tasave_T *)(ga_userinput.ga_data)
6499 + ga_userinput.ga_len);
6500 ++ga_userinput.ga_len;
6501 /* default return is zero == OK */
6502 }
6503 else
6504 rettv->vval.v_number = 1; /* Failed */
6505}
6506
6507/*
6508 * "inputsecret()" function
6509 */
6510 static void
6511f_inputsecret(typval_T *argvars, typval_T *rettv)
6512{
6513 ++cmdline_star;
6514 ++inputsecret_flag;
6515 f_input(argvars, rettv);
6516 --cmdline_star;
6517 --inputsecret_flag;
6518}
6519
6520/*
6521 * "insert()" function
6522 */
6523 static void
6524f_insert(typval_T *argvars, typval_T *rettv)
6525{
6526 long before = 0;
6527 listitem_T *item;
6528 list_T *l;
6529 int error = FALSE;
6530
6531 if (argvars[0].v_type != VAR_LIST)
6532 EMSG2(_(e_listarg), "insert()");
6533 else if ((l = argvars[0].vval.v_list) != NULL
6534 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
6535 {
6536 if (argvars[2].v_type != VAR_UNKNOWN)
6537 before = (long)get_tv_number_chk(&argvars[2], &error);
6538 if (error)
6539 return; /* type error; errmsg already given */
6540
6541 if (before == l->lv_len)
6542 item = NULL;
6543 else
6544 {
6545 item = list_find(l, before);
6546 if (item == NULL)
6547 {
6548 EMSGN(_(e_listidx), before);
6549 l = NULL;
6550 }
6551 }
6552 if (l != NULL)
6553 {
6554 list_insert_tv(l, &argvars[1], item);
6555 copy_tv(&argvars[0], rettv);
6556 }
6557 }
6558}
6559
6560/*
6561 * "invert(expr)" function
6562 */
6563 static void
6564f_invert(typval_T *argvars, typval_T *rettv)
6565{
6566 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
6567}
6568
6569/*
6570 * "isdirectory()" function
6571 */
6572 static void
6573f_isdirectory(typval_T *argvars, typval_T *rettv)
6574{
6575 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
6576}
6577
6578/*
6579 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
6580 * or it refers to a List or Dictionary that is locked.
6581 */
6582 static int
6583tv_islocked(typval_T *tv)
6584{
6585 return (tv->v_lock & VAR_LOCKED)
6586 || (tv->v_type == VAR_LIST
6587 && tv->vval.v_list != NULL
6588 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
6589 || (tv->v_type == VAR_DICT
6590 && tv->vval.v_dict != NULL
6591 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
6592}
6593
6594/*
6595 * "islocked()" function
6596 */
6597 static void
6598f_islocked(typval_T *argvars, typval_T *rettv)
6599{
6600 lval_T lv;
6601 char_u *end;
6602 dictitem_T *di;
6603
6604 rettv->vval.v_number = -1;
6605 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01006606 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006607 if (end != NULL && lv.ll_name != NULL)
6608 {
6609 if (*end != NUL)
6610 EMSG(_(e_trailing));
6611 else
6612 {
6613 if (lv.ll_tv == NULL)
6614 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006615 di = find_var(lv.ll_name, NULL, TRUE);
6616 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006617 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006618 /* Consider a variable locked when:
6619 * 1. the variable itself is locked
6620 * 2. the value of the variable is locked.
6621 * 3. the List or Dict value is locked.
6622 */
6623 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
6624 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006625 }
6626 }
6627 else if (lv.ll_range)
6628 EMSG(_("E786: Range not allowed"));
6629 else if (lv.ll_newkey != NULL)
6630 EMSG2(_(e_dictkey), lv.ll_newkey);
6631 else if (lv.ll_list != NULL)
6632 /* List item. */
6633 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
6634 else
6635 /* Dictionary item. */
6636 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
6637 }
6638 }
6639
6640 clear_lval(&lv);
6641}
6642
6643#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
6644/*
6645 * "isnan()" function
6646 */
6647 static void
6648f_isnan(typval_T *argvars, typval_T *rettv)
6649{
6650 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
6651 && isnan(argvars[0].vval.v_float);
6652}
6653#endif
6654
6655/*
6656 * "items(dict)" function
6657 */
6658 static void
6659f_items(typval_T *argvars, typval_T *rettv)
6660{
6661 dict_list(argvars, rettv, 2);
6662}
6663
6664#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
6665/*
6666 * Get the job from the argument.
6667 * Returns NULL if the job is invalid.
6668 */
6669 static job_T *
6670get_job_arg(typval_T *tv)
6671{
6672 job_T *job;
6673
6674 if (tv->v_type != VAR_JOB)
6675 {
6676 EMSG2(_(e_invarg2), get_tv_string(tv));
6677 return NULL;
6678 }
6679 job = tv->vval.v_job;
6680
6681 if (job == NULL)
6682 EMSG(_("E916: not a valid job"));
6683 return job;
6684}
6685
6686/*
6687 * "job_getchannel()" function
6688 */
6689 static void
6690f_job_getchannel(typval_T *argvars, typval_T *rettv)
6691{
6692 job_T *job = get_job_arg(&argvars[0]);
6693
6694 if (job != NULL)
6695 {
6696 rettv->v_type = VAR_CHANNEL;
6697 rettv->vval.v_channel = job->jv_channel;
6698 if (job->jv_channel != NULL)
6699 ++job->jv_channel->ch_refcount;
6700 }
6701}
6702
6703/*
6704 * "job_info()" function
6705 */
6706 static void
6707f_job_info(typval_T *argvars, typval_T *rettv)
6708{
6709 job_T *job = get_job_arg(&argvars[0]);
6710
6711 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
6712 job_info(job, rettv->vval.v_dict);
6713}
6714
6715/*
6716 * "job_setoptions()" function
6717 */
6718 static void
6719f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
6720{
6721 job_T *job = get_job_arg(&argvars[0]);
6722 jobopt_T opt;
6723
6724 if (job == NULL)
6725 return;
6726 clear_job_options(&opt);
6727 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == OK)
6728 job_set_options(job, &opt);
6729 free_job_options(&opt);
6730}
6731
6732/*
6733 * "job_start()" function
6734 */
6735 static void
6736f_job_start(typval_T *argvars, typval_T *rettv)
6737{
6738 rettv->v_type = VAR_JOB;
6739 if (check_restricted() || check_secure())
6740 return;
6741 rettv->vval.v_job = job_start(argvars);
6742}
6743
6744/*
6745 * "job_status()" function
6746 */
6747 static void
6748f_job_status(typval_T *argvars, typval_T *rettv)
6749{
6750 job_T *job = get_job_arg(&argvars[0]);
6751
6752 if (job != NULL)
6753 {
6754 rettv->v_type = VAR_STRING;
6755 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
6756 }
6757}
6758
6759/*
6760 * "job_stop()" function
6761 */
6762 static void
6763f_job_stop(typval_T *argvars, typval_T *rettv)
6764{
6765 job_T *job = get_job_arg(&argvars[0]);
6766
6767 if (job != NULL)
6768 rettv->vval.v_number = job_stop(job, argvars);
6769}
6770#endif
6771
6772/*
6773 * "join()" function
6774 */
6775 static void
6776f_join(typval_T *argvars, typval_T *rettv)
6777{
6778 garray_T ga;
6779 char_u *sep;
6780
6781 if (argvars[0].v_type != VAR_LIST)
6782 {
6783 EMSG(_(e_listreq));
6784 return;
6785 }
6786 if (argvars[0].vval.v_list == NULL)
6787 return;
6788 if (argvars[1].v_type == VAR_UNKNOWN)
6789 sep = (char_u *)" ";
6790 else
6791 sep = get_tv_string_chk(&argvars[1]);
6792
6793 rettv->v_type = VAR_STRING;
6794
6795 if (sep != NULL)
6796 {
6797 ga_init2(&ga, (int)sizeof(char), 80);
6798 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
6799 ga_append(&ga, NUL);
6800 rettv->vval.v_string = (char_u *)ga.ga_data;
6801 }
6802 else
6803 rettv->vval.v_string = NULL;
6804}
6805
6806/*
6807 * "js_decode()" function
6808 */
6809 static void
6810f_js_decode(typval_T *argvars, typval_T *rettv)
6811{
6812 js_read_T reader;
6813
6814 reader.js_buf = get_tv_string(&argvars[0]);
6815 reader.js_fill = NULL;
6816 reader.js_used = 0;
6817 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
6818 EMSG(_(e_invarg));
6819}
6820
6821/*
6822 * "js_encode()" function
6823 */
6824 static void
6825f_js_encode(typval_T *argvars, typval_T *rettv)
6826{
6827 rettv->v_type = VAR_STRING;
6828 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
6829}
6830
6831/*
6832 * "json_decode()" function
6833 */
6834 static void
6835f_json_decode(typval_T *argvars, typval_T *rettv)
6836{
6837 js_read_T reader;
6838
6839 reader.js_buf = get_tv_string(&argvars[0]);
6840 reader.js_fill = NULL;
6841 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01006842 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006843}
6844
6845/*
6846 * "json_encode()" function
6847 */
6848 static void
6849f_json_encode(typval_T *argvars, typval_T *rettv)
6850{
6851 rettv->v_type = VAR_STRING;
6852 rettv->vval.v_string = json_encode(&argvars[0], 0);
6853}
6854
6855/*
6856 * "keys()" function
6857 */
6858 static void
6859f_keys(typval_T *argvars, typval_T *rettv)
6860{
6861 dict_list(argvars, rettv, 0);
6862}
6863
6864/*
6865 * "last_buffer_nr()" function.
6866 */
6867 static void
6868f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
6869{
6870 int n = 0;
6871 buf_T *buf;
6872
Bram Moolenaar29323592016-07-24 22:04:11 +02006873 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006874 if (n < buf->b_fnum)
6875 n = buf->b_fnum;
6876
6877 rettv->vval.v_number = n;
6878}
6879
6880/*
6881 * "len()" function
6882 */
6883 static void
6884f_len(typval_T *argvars, typval_T *rettv)
6885{
6886 switch (argvars[0].v_type)
6887 {
6888 case VAR_STRING:
6889 case VAR_NUMBER:
6890 rettv->vval.v_number = (varnumber_T)STRLEN(
6891 get_tv_string(&argvars[0]));
6892 break;
6893 case VAR_LIST:
6894 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
6895 break;
6896 case VAR_DICT:
6897 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
6898 break;
6899 case VAR_UNKNOWN:
6900 case VAR_SPECIAL:
6901 case VAR_FLOAT:
6902 case VAR_FUNC:
6903 case VAR_PARTIAL:
6904 case VAR_JOB:
6905 case VAR_CHANNEL:
6906 EMSG(_("E701: Invalid type for len()"));
6907 break;
6908 }
6909}
6910
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006911 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01006912libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006913{
6914#ifdef FEAT_LIBCALL
6915 char_u *string_in;
6916 char_u **string_result;
6917 int nr_result;
6918#endif
6919
6920 rettv->v_type = type;
6921 if (type != VAR_NUMBER)
6922 rettv->vval.v_string = NULL;
6923
6924 if (check_restricted() || check_secure())
6925 return;
6926
6927#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02006928 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006929 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
6930 {
6931 string_in = NULL;
6932 if (argvars[2].v_type == VAR_STRING)
6933 string_in = argvars[2].vval.v_string;
6934 if (type == VAR_NUMBER)
6935 string_result = NULL;
6936 else
6937 string_result = &rettv->vval.v_string;
6938 if (mch_libcall(argvars[0].vval.v_string,
6939 argvars[1].vval.v_string,
6940 string_in,
6941 argvars[2].vval.v_number,
6942 string_result,
6943 &nr_result) == OK
6944 && type == VAR_NUMBER)
6945 rettv->vval.v_number = nr_result;
6946 }
6947#endif
6948}
6949
6950/*
6951 * "libcall()" function
6952 */
6953 static void
6954f_libcall(typval_T *argvars, typval_T *rettv)
6955{
6956 libcall_common(argvars, rettv, VAR_STRING);
6957}
6958
6959/*
6960 * "libcallnr()" function
6961 */
6962 static void
6963f_libcallnr(typval_T *argvars, typval_T *rettv)
6964{
6965 libcall_common(argvars, rettv, VAR_NUMBER);
6966}
6967
6968/*
6969 * "line(string)" function
6970 */
6971 static void
6972f_line(typval_T *argvars, typval_T *rettv)
6973{
6974 linenr_T lnum = 0;
6975 pos_T *fp;
6976 int fnum;
6977
6978 fp = var2fpos(&argvars[0], TRUE, &fnum);
6979 if (fp != NULL)
6980 lnum = fp->lnum;
6981 rettv->vval.v_number = lnum;
6982}
6983
6984/*
6985 * "line2byte(lnum)" function
6986 */
6987 static void
6988f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
6989{
6990#ifndef FEAT_BYTEOFF
6991 rettv->vval.v_number = -1;
6992#else
6993 linenr_T lnum;
6994
6995 lnum = get_tv_lnum(argvars);
6996 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
6997 rettv->vval.v_number = -1;
6998 else
6999 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7000 if (rettv->vval.v_number >= 0)
7001 ++rettv->vval.v_number;
7002#endif
7003}
7004
7005/*
7006 * "lispindent(lnum)" function
7007 */
7008 static void
7009f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7010{
7011#ifdef FEAT_LISP
7012 pos_T pos;
7013 linenr_T lnum;
7014
7015 pos = curwin->w_cursor;
7016 lnum = get_tv_lnum(argvars);
7017 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7018 {
7019 curwin->w_cursor.lnum = lnum;
7020 rettv->vval.v_number = get_lisp_indent();
7021 curwin->w_cursor = pos;
7022 }
7023 else
7024#endif
7025 rettv->vval.v_number = -1;
7026}
7027
7028/*
7029 * "localtime()" function
7030 */
7031 static void
7032f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7033{
7034 rettv->vval.v_number = (varnumber_T)time(NULL);
7035}
7036
7037static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
7038
7039 static void
7040get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7041{
7042 char_u *keys;
7043 char_u *which;
7044 char_u buf[NUMBUFLEN];
7045 char_u *keys_buf = NULL;
7046 char_u *rhs;
7047 int mode;
7048 int abbr = FALSE;
7049 int get_dict = FALSE;
7050 mapblock_T *mp;
7051 int buffer_local;
7052
7053 /* return empty string for failure */
7054 rettv->v_type = VAR_STRING;
7055 rettv->vval.v_string = NULL;
7056
7057 keys = get_tv_string(&argvars[0]);
7058 if (*keys == NUL)
7059 return;
7060
7061 if (argvars[1].v_type != VAR_UNKNOWN)
7062 {
7063 which = get_tv_string_buf_chk(&argvars[1], buf);
7064 if (argvars[2].v_type != VAR_UNKNOWN)
7065 {
7066 abbr = (int)get_tv_number(&argvars[2]);
7067 if (argvars[3].v_type != VAR_UNKNOWN)
7068 get_dict = (int)get_tv_number(&argvars[3]);
7069 }
7070 }
7071 else
7072 which = (char_u *)"";
7073 if (which == NULL)
7074 return;
7075
7076 mode = get_map_mode(&which, 0);
7077
7078 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7079 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7080 vim_free(keys_buf);
7081
7082 if (!get_dict)
7083 {
7084 /* Return a string. */
7085 if (rhs != NULL)
7086 rettv->vval.v_string = str2special_save(rhs, FALSE);
7087
7088 }
7089 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7090 {
7091 /* Return a dictionary. */
7092 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7093 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7094 dict_T *dict = rettv->vval.v_dict;
7095
7096 dict_add_nr_str(dict, "lhs", 0L, lhs);
7097 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
7098 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
7099 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
7100 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
7101 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
7102 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
7103 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
7104 dict_add_nr_str(dict, "mode", 0L, mapmode);
7105
7106 vim_free(lhs);
7107 vim_free(mapmode);
7108 }
7109}
7110
7111#ifdef FEAT_FLOAT
7112/*
7113 * "log()" function
7114 */
7115 static void
7116f_log(typval_T *argvars, typval_T *rettv)
7117{
7118 float_T f = 0.0;
7119
7120 rettv->v_type = VAR_FLOAT;
7121 if (get_float_arg(argvars, &f) == OK)
7122 rettv->vval.v_float = log(f);
7123 else
7124 rettv->vval.v_float = 0.0;
7125}
7126
7127/*
7128 * "log10()" function
7129 */
7130 static void
7131f_log10(typval_T *argvars, typval_T *rettv)
7132{
7133 float_T f = 0.0;
7134
7135 rettv->v_type = VAR_FLOAT;
7136 if (get_float_arg(argvars, &f) == OK)
7137 rettv->vval.v_float = log10(f);
7138 else
7139 rettv->vval.v_float = 0.0;
7140}
7141#endif
7142
7143#ifdef FEAT_LUA
7144/*
7145 * "luaeval()" function
7146 */
7147 static void
7148f_luaeval(typval_T *argvars, typval_T *rettv)
7149{
7150 char_u *str;
7151 char_u buf[NUMBUFLEN];
7152
7153 str = get_tv_string_buf(&argvars[0], buf);
7154 do_luaeval(str, argvars + 1, rettv);
7155}
7156#endif
7157
7158/*
7159 * "map()" function
7160 */
7161 static void
7162f_map(typval_T *argvars, typval_T *rettv)
7163{
7164 filter_map(argvars, rettv, TRUE);
7165}
7166
7167/*
7168 * "maparg()" function
7169 */
7170 static void
7171f_maparg(typval_T *argvars, typval_T *rettv)
7172{
7173 get_maparg(argvars, rettv, TRUE);
7174}
7175
7176/*
7177 * "mapcheck()" function
7178 */
7179 static void
7180f_mapcheck(typval_T *argvars, typval_T *rettv)
7181{
7182 get_maparg(argvars, rettv, FALSE);
7183}
7184
7185static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
7186
7187 static void
7188find_some_match(typval_T *argvars, typval_T *rettv, int type)
7189{
7190 char_u *str = NULL;
7191 long len = 0;
7192 char_u *expr = NULL;
7193 char_u *pat;
7194 regmatch_T regmatch;
7195 char_u patbuf[NUMBUFLEN];
7196 char_u strbuf[NUMBUFLEN];
7197 char_u *save_cpo;
7198 long start = 0;
7199 long nth = 1;
7200 colnr_T startcol = 0;
7201 int match = 0;
7202 list_T *l = NULL;
7203 listitem_T *li = NULL;
7204 long idx = 0;
7205 char_u *tofree = NULL;
7206
7207 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7208 save_cpo = p_cpo;
7209 p_cpo = (char_u *)"";
7210
7211 rettv->vval.v_number = -1;
7212 if (type == 3 || type == 4)
7213 {
7214 /* type 3: return empty list when there are no matches.
7215 * type 4: return ["", -1, -1, -1] */
7216 if (rettv_list_alloc(rettv) == FAIL)
7217 goto theend;
7218 if (type == 4
7219 && (list_append_string(rettv->vval.v_list,
7220 (char_u *)"", 0) == FAIL
7221 || list_append_number(rettv->vval.v_list,
7222 (varnumber_T)-1) == FAIL
7223 || list_append_number(rettv->vval.v_list,
7224 (varnumber_T)-1) == FAIL
7225 || list_append_number(rettv->vval.v_list,
7226 (varnumber_T)-1) == FAIL))
7227 {
7228 list_free(rettv->vval.v_list);
7229 rettv->vval.v_list = NULL;
7230 goto theend;
7231 }
7232 }
7233 else if (type == 2)
7234 {
7235 rettv->v_type = VAR_STRING;
7236 rettv->vval.v_string = NULL;
7237 }
7238
7239 if (argvars[0].v_type == VAR_LIST)
7240 {
7241 if ((l = argvars[0].vval.v_list) == NULL)
7242 goto theend;
7243 li = l->lv_first;
7244 }
7245 else
7246 {
7247 expr = str = get_tv_string(&argvars[0]);
7248 len = (long)STRLEN(str);
7249 }
7250
7251 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
7252 if (pat == NULL)
7253 goto theend;
7254
7255 if (argvars[2].v_type != VAR_UNKNOWN)
7256 {
7257 int error = FALSE;
7258
7259 start = (long)get_tv_number_chk(&argvars[2], &error);
7260 if (error)
7261 goto theend;
7262 if (l != NULL)
7263 {
7264 li = list_find(l, start);
7265 if (li == NULL)
7266 goto theend;
7267 idx = l->lv_idx; /* use the cached index */
7268 }
7269 else
7270 {
7271 if (start < 0)
7272 start = 0;
7273 if (start > len)
7274 goto theend;
7275 /* When "count" argument is there ignore matches before "start",
7276 * otherwise skip part of the string. Differs when pattern is "^"
7277 * or "\<". */
7278 if (argvars[3].v_type != VAR_UNKNOWN)
7279 startcol = start;
7280 else
7281 {
7282 str += start;
7283 len -= start;
7284 }
7285 }
7286
7287 if (argvars[3].v_type != VAR_UNKNOWN)
7288 nth = (long)get_tv_number_chk(&argvars[3], &error);
7289 if (error)
7290 goto theend;
7291 }
7292
7293 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7294 if (regmatch.regprog != NULL)
7295 {
7296 regmatch.rm_ic = p_ic;
7297
7298 for (;;)
7299 {
7300 if (l != NULL)
7301 {
7302 if (li == NULL)
7303 {
7304 match = FALSE;
7305 break;
7306 }
7307 vim_free(tofree);
7308 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7309 if (str == NULL)
7310 break;
7311 }
7312
7313 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7314
7315 if (match && --nth <= 0)
7316 break;
7317 if (l == NULL && !match)
7318 break;
7319
7320 /* Advance to just after the match. */
7321 if (l != NULL)
7322 {
7323 li = li->li_next;
7324 ++idx;
7325 }
7326 else
7327 {
7328#ifdef FEAT_MBYTE
7329 startcol = (colnr_T)(regmatch.startp[0]
7330 + (*mb_ptr2len)(regmatch.startp[0]) - str);
7331#else
7332 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
7333#endif
7334 if (startcol > (colnr_T)len
7335 || str + startcol <= regmatch.startp[0])
7336 {
7337 match = FALSE;
7338 break;
7339 }
7340 }
7341 }
7342
7343 if (match)
7344 {
7345 if (type == 4)
7346 {
7347 listitem_T *li1 = rettv->vval.v_list->lv_first;
7348 listitem_T *li2 = li1->li_next;
7349 listitem_T *li3 = li2->li_next;
7350 listitem_T *li4 = li3->li_next;
7351
7352 vim_free(li1->li_tv.vval.v_string);
7353 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7354 (int)(regmatch.endp[0] - regmatch.startp[0]));
7355 li3->li_tv.vval.v_number =
7356 (varnumber_T)(regmatch.startp[0] - expr);
7357 li4->li_tv.vval.v_number =
7358 (varnumber_T)(regmatch.endp[0] - expr);
7359 if (l != NULL)
7360 li2->li_tv.vval.v_number = (varnumber_T)idx;
7361 }
7362 else if (type == 3)
7363 {
7364 int i;
7365
7366 /* return list with matched string and submatches */
7367 for (i = 0; i < NSUBEXP; ++i)
7368 {
7369 if (regmatch.endp[i] == NULL)
7370 {
7371 if (list_append_string(rettv->vval.v_list,
7372 (char_u *)"", 0) == FAIL)
7373 break;
7374 }
7375 else if (list_append_string(rettv->vval.v_list,
7376 regmatch.startp[i],
7377 (int)(regmatch.endp[i] - regmatch.startp[i]))
7378 == FAIL)
7379 break;
7380 }
7381 }
7382 else if (type == 2)
7383 {
7384 /* return matched string */
7385 if (l != NULL)
7386 copy_tv(&li->li_tv, rettv);
7387 else
7388 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
7389 (int)(regmatch.endp[0] - regmatch.startp[0]));
7390 }
7391 else if (l != NULL)
7392 rettv->vval.v_number = idx;
7393 else
7394 {
7395 if (type != 0)
7396 rettv->vval.v_number =
7397 (varnumber_T)(regmatch.startp[0] - str);
7398 else
7399 rettv->vval.v_number =
7400 (varnumber_T)(regmatch.endp[0] - str);
7401 rettv->vval.v_number += (varnumber_T)(str - expr);
7402 }
7403 }
7404 vim_regfree(regmatch.regprog);
7405 }
7406
7407 if (type == 4 && l == NULL)
7408 /* matchstrpos() without a list: drop the second item. */
7409 listitem_remove(rettv->vval.v_list,
7410 rettv->vval.v_list->lv_first->li_next);
7411
7412theend:
7413 vim_free(tofree);
7414 p_cpo = save_cpo;
7415}
7416
7417/*
7418 * "match()" function
7419 */
7420 static void
7421f_match(typval_T *argvars, typval_T *rettv)
7422{
7423 find_some_match(argvars, rettv, 1);
7424}
7425
7426/*
7427 * "matchadd()" function
7428 */
7429 static void
7430f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7431{
7432#ifdef FEAT_SEARCH_EXTRA
7433 char_u buf[NUMBUFLEN];
7434 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
7435 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
7436 int prio = 10; /* default priority */
7437 int id = -1;
7438 int error = FALSE;
7439 char_u *conceal_char = NULL;
7440
7441 rettv->vval.v_number = -1;
7442
7443 if (grp == NULL || pat == NULL)
7444 return;
7445 if (argvars[2].v_type != VAR_UNKNOWN)
7446 {
7447 prio = (int)get_tv_number_chk(&argvars[2], &error);
7448 if (argvars[3].v_type != VAR_UNKNOWN)
7449 {
7450 id = (int)get_tv_number_chk(&argvars[3], &error);
7451 if (argvars[4].v_type != VAR_UNKNOWN)
7452 {
7453 if (argvars[4].v_type != VAR_DICT)
7454 {
7455 EMSG(_(e_dictreq));
7456 return;
7457 }
7458 if (dict_find(argvars[4].vval.v_dict,
7459 (char_u *)"conceal", -1) != NULL)
7460 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7461 (char_u *)"conceal", FALSE);
7462 }
7463 }
7464 }
7465 if (error == TRUE)
7466 return;
7467 if (id >= 1 && id <= 3)
7468 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007469 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007470 return;
7471 }
7472
7473 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
7474 conceal_char);
7475#endif
7476}
7477
7478/*
7479 * "matchaddpos()" function
7480 */
7481 static void
7482f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7483{
7484#ifdef FEAT_SEARCH_EXTRA
7485 char_u buf[NUMBUFLEN];
7486 char_u *group;
7487 int prio = 10;
7488 int id = -1;
7489 int error = FALSE;
7490 list_T *l;
7491 char_u *conceal_char = NULL;
7492
7493 rettv->vval.v_number = -1;
7494
7495 group = get_tv_string_buf_chk(&argvars[0], buf);
7496 if (group == NULL)
7497 return;
7498
7499 if (argvars[1].v_type != VAR_LIST)
7500 {
7501 EMSG2(_(e_listarg), "matchaddpos()");
7502 return;
7503 }
7504 l = argvars[1].vval.v_list;
7505 if (l == NULL)
7506 return;
7507
7508 if (argvars[2].v_type != VAR_UNKNOWN)
7509 {
7510 prio = (int)get_tv_number_chk(&argvars[2], &error);
7511 if (argvars[3].v_type != VAR_UNKNOWN)
7512 {
7513 id = (int)get_tv_number_chk(&argvars[3], &error);
7514 if (argvars[4].v_type != VAR_UNKNOWN)
7515 {
7516 if (argvars[4].v_type != VAR_DICT)
7517 {
7518 EMSG(_(e_dictreq));
7519 return;
7520 }
7521 if (dict_find(argvars[4].vval.v_dict,
7522 (char_u *)"conceal", -1) != NULL)
7523 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7524 (char_u *)"conceal", FALSE);
7525 }
7526 }
7527 }
7528 if (error == TRUE)
7529 return;
7530
7531 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
7532 if (id == 1 || id == 2)
7533 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007534 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007535 return;
7536 }
7537
7538 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
7539 conceal_char);
7540#endif
7541}
7542
7543/*
7544 * "matcharg()" function
7545 */
7546 static void
7547f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
7548{
7549 if (rettv_list_alloc(rettv) == OK)
7550 {
7551#ifdef FEAT_SEARCH_EXTRA
7552 int id = (int)get_tv_number(&argvars[0]);
7553 matchitem_T *m;
7554
7555 if (id >= 1 && id <= 3)
7556 {
7557 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
7558 {
7559 list_append_string(rettv->vval.v_list,
7560 syn_id2name(m->hlg_id), -1);
7561 list_append_string(rettv->vval.v_list, m->pattern, -1);
7562 }
7563 else
7564 {
7565 list_append_string(rettv->vval.v_list, NULL, -1);
7566 list_append_string(rettv->vval.v_list, NULL, -1);
7567 }
7568 }
7569#endif
7570 }
7571}
7572
7573/*
7574 * "matchdelete()" function
7575 */
7576 static void
7577f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7578{
7579#ifdef FEAT_SEARCH_EXTRA
7580 rettv->vval.v_number = match_delete(curwin,
7581 (int)get_tv_number(&argvars[0]), TRUE);
7582#endif
7583}
7584
7585/*
7586 * "matchend()" function
7587 */
7588 static void
7589f_matchend(typval_T *argvars, typval_T *rettv)
7590{
7591 find_some_match(argvars, rettv, 0);
7592}
7593
7594/*
7595 * "matchlist()" function
7596 */
7597 static void
7598f_matchlist(typval_T *argvars, typval_T *rettv)
7599{
7600 find_some_match(argvars, rettv, 3);
7601}
7602
7603/*
7604 * "matchstr()" function
7605 */
7606 static void
7607f_matchstr(typval_T *argvars, typval_T *rettv)
7608{
7609 find_some_match(argvars, rettv, 2);
7610}
7611
7612/*
7613 * "matchstrpos()" function
7614 */
7615 static void
7616f_matchstrpos(typval_T *argvars, typval_T *rettv)
7617{
7618 find_some_match(argvars, rettv, 4);
7619}
7620
7621static void max_min(typval_T *argvars, typval_T *rettv, int domax);
7622
7623 static void
7624max_min(typval_T *argvars, typval_T *rettv, int domax)
7625{
7626 varnumber_T n = 0;
7627 varnumber_T i;
7628 int error = FALSE;
7629
7630 if (argvars[0].v_type == VAR_LIST)
7631 {
7632 list_T *l;
7633 listitem_T *li;
7634
7635 l = argvars[0].vval.v_list;
7636 if (l != NULL)
7637 {
7638 li = l->lv_first;
7639 if (li != NULL)
7640 {
7641 n = get_tv_number_chk(&li->li_tv, &error);
7642 for (;;)
7643 {
7644 li = li->li_next;
7645 if (li == NULL)
7646 break;
7647 i = get_tv_number_chk(&li->li_tv, &error);
7648 if (domax ? i > n : i < n)
7649 n = i;
7650 }
7651 }
7652 }
7653 }
7654 else if (argvars[0].v_type == VAR_DICT)
7655 {
7656 dict_T *d;
7657 int first = TRUE;
7658 hashitem_T *hi;
7659 int todo;
7660
7661 d = argvars[0].vval.v_dict;
7662 if (d != NULL)
7663 {
7664 todo = (int)d->dv_hashtab.ht_used;
7665 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
7666 {
7667 if (!HASHITEM_EMPTY(hi))
7668 {
7669 --todo;
7670 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
7671 if (first)
7672 {
7673 n = i;
7674 first = FALSE;
7675 }
7676 else if (domax ? i > n : i < n)
7677 n = i;
7678 }
7679 }
7680 }
7681 }
7682 else
Bram Moolenaar26b84332016-09-04 21:42:36 +02007683 EMSG2(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007684 rettv->vval.v_number = error ? 0 : n;
7685}
7686
7687/*
7688 * "max()" function
7689 */
7690 static void
7691f_max(typval_T *argvars, typval_T *rettv)
7692{
7693 max_min(argvars, rettv, TRUE);
7694}
7695
7696/*
7697 * "min()" function
7698 */
7699 static void
7700f_min(typval_T *argvars, typval_T *rettv)
7701{
7702 max_min(argvars, rettv, FALSE);
7703}
7704
7705static int mkdir_recurse(char_u *dir, int prot);
7706
7707/*
7708 * Create the directory in which "dir" is located, and higher levels when
7709 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02007710 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007711 */
7712 static int
7713mkdir_recurse(char_u *dir, int prot)
7714{
7715 char_u *p;
7716 char_u *updir;
7717 int r = FAIL;
7718
7719 /* Get end of directory name in "dir".
7720 * We're done when it's "/" or "c:/". */
7721 p = gettail_sep(dir);
7722 if (p <= get_past_head(dir))
7723 return OK;
7724
7725 /* If the directory exists we're done. Otherwise: create it.*/
7726 updir = vim_strnsave(dir, (int)(p - dir));
7727 if (updir == NULL)
7728 return FAIL;
7729 if (mch_isdir(updir))
7730 r = OK;
7731 else if (mkdir_recurse(updir, prot) == OK)
7732 r = vim_mkdir_emsg(updir, prot);
7733 vim_free(updir);
7734 return r;
7735}
7736
7737#ifdef vim_mkdir
7738/*
7739 * "mkdir()" function
7740 */
7741 static void
7742f_mkdir(typval_T *argvars, typval_T *rettv)
7743{
7744 char_u *dir;
7745 char_u buf[NUMBUFLEN];
7746 int prot = 0755;
7747
7748 rettv->vval.v_number = FAIL;
7749 if (check_restricted() || check_secure())
7750 return;
7751
7752 dir = get_tv_string_buf(&argvars[0], buf);
7753 if (*dir == NUL)
7754 rettv->vval.v_number = FAIL;
7755 else
7756 {
7757 if (*gettail(dir) == NUL)
7758 /* remove trailing slashes */
7759 *gettail_sep(dir) = NUL;
7760
7761 if (argvars[1].v_type != VAR_UNKNOWN)
7762 {
7763 if (argvars[2].v_type != VAR_UNKNOWN)
7764 prot = (int)get_tv_number_chk(&argvars[2], NULL);
7765 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
7766 mkdir_recurse(dir, prot);
7767 }
7768 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
7769 }
7770}
7771#endif
7772
7773/*
7774 * "mode()" function
7775 */
7776 static void
7777f_mode(typval_T *argvars, typval_T *rettv)
7778{
7779 char_u buf[3];
7780
7781 buf[1] = NUL;
7782 buf[2] = NUL;
7783
7784 if (time_for_testing == 93784)
7785 {
7786 /* Testing the two-character code. */
7787 buf[0] = 'x';
7788 buf[1] = '!';
7789 }
7790 else if (VIsual_active)
7791 {
7792 if (VIsual_select)
7793 buf[0] = VIsual_mode + 's' - 'v';
7794 else
7795 buf[0] = VIsual_mode;
7796 }
7797 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
7798 || State == CONFIRM)
7799 {
7800 buf[0] = 'r';
7801 if (State == ASKMORE)
7802 buf[1] = 'm';
7803 else if (State == CONFIRM)
7804 buf[1] = '?';
7805 }
7806 else if (State == EXTERNCMD)
7807 buf[0] = '!';
7808 else if (State & INSERT)
7809 {
7810#ifdef FEAT_VREPLACE
7811 if (State & VREPLACE_FLAG)
7812 {
7813 buf[0] = 'R';
7814 buf[1] = 'v';
7815 }
7816 else
7817#endif
Bram Moolenaare90858d2017-02-01 17:24:34 +01007818 {
7819 if (State & REPLACE_FLAG)
7820 buf[0] = 'R';
7821 else
7822 buf[0] = 'i';
7823#ifdef FEAT_INS_EXPAND
7824 if (ins_compl_active())
7825 buf[1] = 'c';
7826 else if (ctrl_x_mode == 1)
7827 buf[1] = 'x';
7828#endif
7829 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007830 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01007831 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007832 {
7833 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01007834 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007835 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01007836 else if (exmode_active == EXMODE_NORMAL)
7837 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007838 }
7839 else
7840 {
7841 buf[0] = 'n';
7842 if (finish_op)
7843 buf[1] = 'o';
7844 }
7845
7846 /* Clear out the minor mode when the argument is not a non-zero number or
7847 * non-empty string. */
7848 if (!non_zero_arg(&argvars[0]))
7849 buf[1] = NUL;
7850
7851 rettv->vval.v_string = vim_strsave(buf);
7852 rettv->v_type = VAR_STRING;
7853}
7854
7855#if defined(FEAT_MZSCHEME) || defined(PROTO)
7856/*
7857 * "mzeval()" function
7858 */
7859 static void
7860f_mzeval(typval_T *argvars, typval_T *rettv)
7861{
7862 char_u *str;
7863 char_u buf[NUMBUFLEN];
7864
7865 str = get_tv_string_buf(&argvars[0], buf);
7866 do_mzeval(str, rettv);
7867}
7868
7869 void
7870mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
7871{
7872 typval_T argvars[3];
7873
7874 argvars[0].v_type = VAR_STRING;
7875 argvars[0].vval.v_string = name;
7876 copy_tv(args, &argvars[1]);
7877 argvars[2].v_type = VAR_UNKNOWN;
7878 f_call(argvars, rettv);
7879 clear_tv(&argvars[1]);
7880}
7881#endif
7882
7883/*
7884 * "nextnonblank()" function
7885 */
7886 static void
7887f_nextnonblank(typval_T *argvars, typval_T *rettv)
7888{
7889 linenr_T lnum;
7890
7891 for (lnum = get_tv_lnum(argvars); ; ++lnum)
7892 {
7893 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
7894 {
7895 lnum = 0;
7896 break;
7897 }
7898 if (*skipwhite(ml_get(lnum)) != NUL)
7899 break;
7900 }
7901 rettv->vval.v_number = lnum;
7902}
7903
7904/*
7905 * "nr2char()" function
7906 */
7907 static void
7908f_nr2char(typval_T *argvars, typval_T *rettv)
7909{
7910 char_u buf[NUMBUFLEN];
7911
7912#ifdef FEAT_MBYTE
7913 if (has_mbyte)
7914 {
7915 int utf8 = 0;
7916
7917 if (argvars[1].v_type != VAR_UNKNOWN)
7918 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
7919 if (utf8)
7920 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
7921 else
7922 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
7923 }
7924 else
7925#endif
7926 {
7927 buf[0] = (char_u)get_tv_number(&argvars[0]);
7928 buf[1] = NUL;
7929 }
7930 rettv->v_type = VAR_STRING;
7931 rettv->vval.v_string = vim_strsave(buf);
7932}
7933
7934/*
7935 * "or(expr, expr)" function
7936 */
7937 static void
7938f_or(typval_T *argvars, typval_T *rettv)
7939{
7940 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
7941 | get_tv_number_chk(&argvars[1], NULL);
7942}
7943
7944/*
7945 * "pathshorten()" function
7946 */
7947 static void
7948f_pathshorten(typval_T *argvars, typval_T *rettv)
7949{
7950 char_u *p;
7951
7952 rettv->v_type = VAR_STRING;
7953 p = get_tv_string_chk(&argvars[0]);
7954 if (p == NULL)
7955 rettv->vval.v_string = NULL;
7956 else
7957 {
7958 p = vim_strsave(p);
7959 rettv->vval.v_string = p;
7960 if (p != NULL)
7961 shorten_dir(p);
7962 }
7963}
7964
7965#ifdef FEAT_PERL
7966/*
7967 * "perleval()" function
7968 */
7969 static void
7970f_perleval(typval_T *argvars, typval_T *rettv)
7971{
7972 char_u *str;
7973 char_u buf[NUMBUFLEN];
7974
7975 str = get_tv_string_buf(&argvars[0], buf);
7976 do_perleval(str, rettv);
7977}
7978#endif
7979
7980#ifdef FEAT_FLOAT
7981/*
7982 * "pow()" function
7983 */
7984 static void
7985f_pow(typval_T *argvars, typval_T *rettv)
7986{
7987 float_T fx = 0.0, fy = 0.0;
7988
7989 rettv->v_type = VAR_FLOAT;
7990 if (get_float_arg(argvars, &fx) == OK
7991 && get_float_arg(&argvars[1], &fy) == OK)
7992 rettv->vval.v_float = pow(fx, fy);
7993 else
7994 rettv->vval.v_float = 0.0;
7995}
7996#endif
7997
7998/*
7999 * "prevnonblank()" function
8000 */
8001 static void
8002f_prevnonblank(typval_T *argvars, typval_T *rettv)
8003{
8004 linenr_T lnum;
8005
8006 lnum = get_tv_lnum(argvars);
8007 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8008 lnum = 0;
8009 else
8010 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8011 --lnum;
8012 rettv->vval.v_number = lnum;
8013}
8014
8015/* This dummy va_list is here because:
8016 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8017 * - locally in the function results in a "used before set" warning
8018 * - using va_start() to initialize it gives "function with fixed args" error */
8019static va_list ap;
8020
8021/*
8022 * "printf()" function
8023 */
8024 static void
8025f_printf(typval_T *argvars, typval_T *rettv)
8026{
8027 char_u buf[NUMBUFLEN];
8028 int len;
8029 char_u *s;
8030 int saved_did_emsg = did_emsg;
8031 char *fmt;
8032
8033 rettv->v_type = VAR_STRING;
8034 rettv->vval.v_string = NULL;
8035
8036 /* Get the required length, allocate the buffer and do it for real. */
8037 did_emsg = FALSE;
8038 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
8039 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
8040 if (!did_emsg)
8041 {
8042 s = alloc(len + 1);
8043 if (s != NULL)
8044 {
8045 rettv->vval.v_string = s;
8046 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
8047 }
8048 }
8049 did_emsg |= saved_did_emsg;
8050}
8051
8052/*
8053 * "pumvisible()" function
8054 */
8055 static void
8056f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8057{
8058#ifdef FEAT_INS_EXPAND
8059 if (pum_visible())
8060 rettv->vval.v_number = 1;
8061#endif
8062}
8063
8064#ifdef FEAT_PYTHON3
8065/*
8066 * "py3eval()" function
8067 */
8068 static void
8069f_py3eval(typval_T *argvars, typval_T *rettv)
8070{
8071 char_u *str;
8072 char_u buf[NUMBUFLEN];
8073
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008074 if (p_pyx == 0)
8075 p_pyx = 3;
8076
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008077 str = get_tv_string_buf(&argvars[0], buf);
8078 do_py3eval(str, rettv);
8079}
8080#endif
8081
8082#ifdef FEAT_PYTHON
8083/*
8084 * "pyeval()" function
8085 */
8086 static void
8087f_pyeval(typval_T *argvars, typval_T *rettv)
8088{
8089 char_u *str;
8090 char_u buf[NUMBUFLEN];
8091
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008092 if (p_pyx == 0)
8093 p_pyx = 2;
8094
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008095 str = get_tv_string_buf(&argvars[0], buf);
8096 do_pyeval(str, rettv);
8097}
8098#endif
8099
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008100#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8101/*
8102 * "pyxeval()" function
8103 */
8104 static void
8105f_pyxeval(typval_T *argvars, typval_T *rettv)
8106{
8107# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8108 init_pyxversion();
8109 if (p_pyx == 2)
8110 f_pyeval(argvars, rettv);
8111 else
8112 f_py3eval(argvars, rettv);
8113# elif defined(FEAT_PYTHON)
8114 f_pyeval(argvars, rettv);
8115# elif defined(FEAT_PYTHON3)
8116 f_py3eval(argvars, rettv);
8117# endif
8118}
8119#endif
8120
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008121/*
8122 * "range()" function
8123 */
8124 static void
8125f_range(typval_T *argvars, typval_T *rettv)
8126{
8127 varnumber_T start;
8128 varnumber_T end;
8129 varnumber_T stride = 1;
8130 varnumber_T i;
8131 int error = FALSE;
8132
8133 start = get_tv_number_chk(&argvars[0], &error);
8134 if (argvars[1].v_type == VAR_UNKNOWN)
8135 {
8136 end = start - 1;
8137 start = 0;
8138 }
8139 else
8140 {
8141 end = get_tv_number_chk(&argvars[1], &error);
8142 if (argvars[2].v_type != VAR_UNKNOWN)
8143 stride = get_tv_number_chk(&argvars[2], &error);
8144 }
8145
8146 if (error)
8147 return; /* type error; errmsg already given */
8148 if (stride == 0)
8149 EMSG(_("E726: Stride is zero"));
8150 else if (stride > 0 ? end + 1 < start : end - 1 > start)
8151 EMSG(_("E727: Start past end"));
8152 else
8153 {
8154 if (rettv_list_alloc(rettv) == OK)
8155 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8156 if (list_append_number(rettv->vval.v_list,
8157 (varnumber_T)i) == FAIL)
8158 break;
8159 }
8160}
8161
8162/*
8163 * "readfile()" function
8164 */
8165 static void
8166f_readfile(typval_T *argvars, typval_T *rettv)
8167{
8168 int binary = FALSE;
8169 int failed = FALSE;
8170 char_u *fname;
8171 FILE *fd;
8172 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8173 int io_size = sizeof(buf);
8174 int readlen; /* size of last fread() */
8175 char_u *prev = NULL; /* previously read bytes, if any */
8176 long prevlen = 0; /* length of data in prev */
8177 long prevsize = 0; /* size of prev buffer */
8178 long maxline = MAXLNUM;
8179 long cnt = 0;
8180 char_u *p; /* position in buf */
8181 char_u *start; /* start of current line */
8182
8183 if (argvars[1].v_type != VAR_UNKNOWN)
8184 {
8185 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
8186 binary = TRUE;
8187 if (argvars[2].v_type != VAR_UNKNOWN)
8188 maxline = (long)get_tv_number(&argvars[2]);
8189 }
8190
8191 if (rettv_list_alloc(rettv) == FAIL)
8192 return;
8193
8194 /* Always open the file in binary mode, library functions have a mind of
8195 * their own about CR-LF conversion. */
8196 fname = get_tv_string(&argvars[0]);
8197 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8198 {
8199 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
8200 return;
8201 }
8202
8203 while (cnt < maxline || maxline < 0)
8204 {
8205 readlen = (int)fread(buf, 1, io_size, fd);
8206
8207 /* This for loop processes what was read, but is also entered at end
8208 * of file so that either:
8209 * - an incomplete line gets written
8210 * - a "binary" file gets an empty line at the end if it ends in a
8211 * newline. */
8212 for (p = buf, start = buf;
8213 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8214 ++p)
8215 {
8216 if (*p == '\n' || readlen <= 0)
8217 {
8218 listitem_T *li;
8219 char_u *s = NULL;
8220 long_u len = p - start;
8221
8222 /* Finished a line. Remove CRs before NL. */
8223 if (readlen > 0 && !binary)
8224 {
8225 while (len > 0 && start[len - 1] == '\r')
8226 --len;
8227 /* removal may cross back to the "prev" string */
8228 if (len == 0)
8229 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8230 --prevlen;
8231 }
8232 if (prevlen == 0)
8233 s = vim_strnsave(start, (int)len);
8234 else
8235 {
8236 /* Change "prev" buffer to be the right size. This way
8237 * the bytes are only copied once, and very long lines are
8238 * allocated only once. */
8239 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8240 {
8241 mch_memmove(s + prevlen, start, len);
8242 s[prevlen + len] = NUL;
8243 prev = NULL; /* the list will own the string */
8244 prevlen = prevsize = 0;
8245 }
8246 }
8247 if (s == NULL)
8248 {
8249 do_outofmem_msg((long_u) prevlen + len + 1);
8250 failed = TRUE;
8251 break;
8252 }
8253
8254 if ((li = listitem_alloc()) == NULL)
8255 {
8256 vim_free(s);
8257 failed = TRUE;
8258 break;
8259 }
8260 li->li_tv.v_type = VAR_STRING;
8261 li->li_tv.v_lock = 0;
8262 li->li_tv.vval.v_string = s;
8263 list_append(rettv->vval.v_list, li);
8264
8265 start = p + 1; /* step over newline */
8266 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
8267 break;
8268 }
8269 else if (*p == NUL)
8270 *p = '\n';
8271#ifdef FEAT_MBYTE
8272 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
8273 * when finding the BF and check the previous two bytes. */
8274 else if (*p == 0xbf && enc_utf8 && !binary)
8275 {
8276 /* Find the two bytes before the 0xbf. If p is at buf, or buf
8277 * + 1, these may be in the "prev" string. */
8278 char_u back1 = p >= buf + 1 ? p[-1]
8279 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
8280 char_u back2 = p >= buf + 2 ? p[-2]
8281 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
8282 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
8283
8284 if (back2 == 0xef && back1 == 0xbb)
8285 {
8286 char_u *dest = p - 2;
8287
8288 /* Usually a BOM is at the beginning of a file, and so at
8289 * the beginning of a line; then we can just step over it.
8290 */
8291 if (start == dest)
8292 start = p + 1;
8293 else
8294 {
8295 /* have to shuffle buf to close gap */
8296 int adjust_prevlen = 0;
8297
8298 if (dest < buf)
8299 {
8300 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
8301 dest = buf;
8302 }
8303 if (readlen > p - buf + 1)
8304 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
8305 readlen -= 3 - adjust_prevlen;
8306 prevlen -= adjust_prevlen;
8307 p = dest - 1;
8308 }
8309 }
8310 }
8311#endif
8312 } /* for */
8313
8314 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
8315 break;
8316 if (start < p)
8317 {
8318 /* There's part of a line in buf, store it in "prev". */
8319 if (p - start + prevlen >= prevsize)
8320 {
8321 /* need bigger "prev" buffer */
8322 char_u *newprev;
8323
8324 /* A common use case is ordinary text files and "prev" gets a
8325 * fragment of a line, so the first allocation is made
8326 * small, to avoid repeatedly 'allocing' large and
8327 * 'reallocing' small. */
8328 if (prevsize == 0)
8329 prevsize = (long)(p - start);
8330 else
8331 {
8332 long grow50pc = (prevsize * 3) / 2;
8333 long growmin = (long)((p - start) * 2 + prevlen);
8334 prevsize = grow50pc > growmin ? grow50pc : growmin;
8335 }
8336 newprev = prev == NULL ? alloc(prevsize)
8337 : vim_realloc(prev, prevsize);
8338 if (newprev == NULL)
8339 {
8340 do_outofmem_msg((long_u)prevsize);
8341 failed = TRUE;
8342 break;
8343 }
8344 prev = newprev;
8345 }
8346 /* Add the line part to end of "prev". */
8347 mch_memmove(prev + prevlen, start, p - start);
8348 prevlen += (long)(p - start);
8349 }
8350 } /* while */
8351
8352 /*
8353 * For a negative line count use only the lines at the end of the file,
8354 * free the rest.
8355 */
8356 if (!failed && maxline < 0)
8357 while (cnt > -maxline)
8358 {
8359 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
8360 --cnt;
8361 }
8362
8363 if (failed)
8364 {
8365 list_free(rettv->vval.v_list);
8366 /* readfile doc says an empty list is returned on error */
8367 rettv->vval.v_list = list_alloc();
8368 }
8369
8370 vim_free(prev);
8371 fclose(fd);
8372}
8373
8374#if defined(FEAT_RELTIME)
8375static int list2proftime(typval_T *arg, proftime_T *tm);
8376
8377/*
8378 * Convert a List to proftime_T.
8379 * Return FAIL when there is something wrong.
8380 */
8381 static int
8382list2proftime(typval_T *arg, proftime_T *tm)
8383{
8384 long n1, n2;
8385 int error = FALSE;
8386
8387 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
8388 || arg->vval.v_list->lv_len != 2)
8389 return FAIL;
8390 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
8391 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
8392# ifdef WIN3264
8393 tm->HighPart = n1;
8394 tm->LowPart = n2;
8395# else
8396 tm->tv_sec = n1;
8397 tm->tv_usec = n2;
8398# endif
8399 return error ? FAIL : OK;
8400}
8401#endif /* FEAT_RELTIME */
8402
8403/*
8404 * "reltime()" function
8405 */
8406 static void
8407f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8408{
8409#ifdef FEAT_RELTIME
8410 proftime_T res;
8411 proftime_T start;
8412
8413 if (argvars[0].v_type == VAR_UNKNOWN)
8414 {
8415 /* No arguments: get current time. */
8416 profile_start(&res);
8417 }
8418 else if (argvars[1].v_type == VAR_UNKNOWN)
8419 {
8420 if (list2proftime(&argvars[0], &res) == FAIL)
8421 return;
8422 profile_end(&res);
8423 }
8424 else
8425 {
8426 /* Two arguments: compute the difference. */
8427 if (list2proftime(&argvars[0], &start) == FAIL
8428 || list2proftime(&argvars[1], &res) == FAIL)
8429 return;
8430 profile_sub(&res, &start);
8431 }
8432
8433 if (rettv_list_alloc(rettv) == OK)
8434 {
8435 long n1, n2;
8436
8437# ifdef WIN3264
8438 n1 = res.HighPart;
8439 n2 = res.LowPart;
8440# else
8441 n1 = res.tv_sec;
8442 n2 = res.tv_usec;
8443# endif
8444 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
8445 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
8446 }
8447#endif
8448}
8449
8450#ifdef FEAT_FLOAT
8451/*
8452 * "reltimefloat()" function
8453 */
8454 static void
8455f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
8456{
8457# ifdef FEAT_RELTIME
8458 proftime_T tm;
8459# endif
8460
8461 rettv->v_type = VAR_FLOAT;
8462 rettv->vval.v_float = 0;
8463# ifdef FEAT_RELTIME
8464 if (list2proftime(&argvars[0], &tm) == OK)
8465 rettv->vval.v_float = profile_float(&tm);
8466# endif
8467}
8468#endif
8469
8470/*
8471 * "reltimestr()" function
8472 */
8473 static void
8474f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
8475{
8476#ifdef FEAT_RELTIME
8477 proftime_T tm;
8478#endif
8479
8480 rettv->v_type = VAR_STRING;
8481 rettv->vval.v_string = NULL;
8482#ifdef FEAT_RELTIME
8483 if (list2proftime(&argvars[0], &tm) == OK)
8484 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
8485#endif
8486}
8487
8488#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
8489static void make_connection(void);
8490static int check_connection(void);
8491
8492 static void
8493make_connection(void)
8494{
8495 if (X_DISPLAY == NULL
8496# ifdef FEAT_GUI
8497 && !gui.in_use
8498# endif
8499 )
8500 {
8501 x_force_connect = TRUE;
8502 setup_term_clip();
8503 x_force_connect = FALSE;
8504 }
8505}
8506
8507 static int
8508check_connection(void)
8509{
8510 make_connection();
8511 if (X_DISPLAY == NULL)
8512 {
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01008513 EMSG(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008514 return FAIL;
8515 }
8516 return OK;
8517}
8518#endif
8519
8520#ifdef FEAT_CLIENTSERVER
8521 static void
8522remote_common(typval_T *argvars, typval_T *rettv, int expr)
8523{
8524 char_u *server_name;
8525 char_u *keys;
8526 char_u *r = NULL;
8527 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008528 int timeout = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008529# ifdef WIN32
8530 HWND w;
8531# else
8532 Window w;
8533# endif
8534
8535 if (check_restricted() || check_secure())
8536 return;
8537
8538# ifdef FEAT_X11
8539 if (check_connection() == FAIL)
8540 return;
8541# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008542 if (argvars[2].v_type != VAR_UNKNOWN
8543 && argvars[3].v_type != VAR_UNKNOWN)
8544 timeout = get_tv_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008545
8546 server_name = get_tv_string_chk(&argvars[0]);
8547 if (server_name == NULL)
8548 return; /* type error; errmsg already given */
8549 keys = get_tv_string_buf(&argvars[1], buf);
8550# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008551 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008552# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008553 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
8554 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008555# endif
8556 {
8557 if (r != NULL)
8558 EMSG(r); /* sending worked but evaluation failed */
8559 else
8560 EMSG2(_("E241: Unable to send to %s"), server_name);
8561 return;
8562 }
8563
8564 rettv->vval.v_string = r;
8565
8566 if (argvars[2].v_type != VAR_UNKNOWN)
8567 {
8568 dictitem_T v;
8569 char_u str[30];
8570 char_u *idvar;
8571
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008572 idvar = get_tv_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008573 if (idvar != NULL && *idvar != NUL)
8574 {
8575 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
8576 v.di_tv.v_type = VAR_STRING;
8577 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008578 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008579 vim_free(v.di_tv.vval.v_string);
8580 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008581 }
8582}
8583#endif
8584
8585/*
8586 * "remote_expr()" function
8587 */
8588 static void
8589f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
8590{
8591 rettv->v_type = VAR_STRING;
8592 rettv->vval.v_string = NULL;
8593#ifdef FEAT_CLIENTSERVER
8594 remote_common(argvars, rettv, TRUE);
8595#endif
8596}
8597
8598/*
8599 * "remote_foreground()" function
8600 */
8601 static void
8602f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8603{
8604#ifdef FEAT_CLIENTSERVER
8605# ifdef WIN32
8606 /* On Win32 it's done in this application. */
8607 {
8608 char_u *server_name = get_tv_string_chk(&argvars[0]);
8609
8610 if (server_name != NULL)
8611 serverForeground(server_name);
8612 }
8613# else
8614 /* Send a foreground() expression to the server. */
8615 argvars[1].v_type = VAR_STRING;
8616 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
8617 argvars[2].v_type = VAR_UNKNOWN;
8618 remote_common(argvars, rettv, TRUE);
8619 vim_free(argvars[1].vval.v_string);
8620# endif
8621#endif
8622}
8623
8624 static void
8625f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
8626{
8627#ifdef FEAT_CLIENTSERVER
8628 dictitem_T v;
8629 char_u *s = NULL;
8630# ifdef WIN32
8631 long_u n = 0;
8632# endif
8633 char_u *serverid;
8634
8635 if (check_restricted() || check_secure())
8636 {
8637 rettv->vval.v_number = -1;
8638 return;
8639 }
8640 serverid = get_tv_string_chk(&argvars[0]);
8641 if (serverid == NULL)
8642 {
8643 rettv->vval.v_number = -1;
8644 return; /* type error; errmsg already given */
8645 }
8646# ifdef WIN32
8647 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
8648 if (n == 0)
8649 rettv->vval.v_number = -1;
8650 else
8651 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008652 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008653 rettv->vval.v_number = (s != NULL);
8654 }
8655# else
8656 if (check_connection() == FAIL)
8657 return;
8658
8659 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
8660 serverStrToWin(serverid), &s);
8661# endif
8662
8663 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
8664 {
8665 char_u *retvar;
8666
8667 v.di_tv.v_type = VAR_STRING;
8668 v.di_tv.vval.v_string = vim_strsave(s);
8669 retvar = get_tv_string_chk(&argvars[1]);
8670 if (retvar != NULL)
8671 set_var(retvar, &v.di_tv, FALSE);
8672 vim_free(v.di_tv.vval.v_string);
8673 }
8674#else
8675 rettv->vval.v_number = -1;
8676#endif
8677}
8678
8679 static void
8680f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
8681{
8682 char_u *r = NULL;
8683
8684#ifdef FEAT_CLIENTSERVER
8685 char_u *serverid = get_tv_string_chk(&argvars[0]);
8686
8687 if (serverid != NULL && !check_restricted() && !check_secure())
8688 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008689 int timeout = 0;
Bram Moolenaar1662ce12017-03-19 21:47:50 +01008690# ifdef WIN32
8691 /* The server's HWND is encoded in the 'id' parameter */
8692 long_u n = 0;
8693# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008694
8695 if (argvars[1].v_type != VAR_UNKNOWN)
8696 timeout = get_tv_number(&argvars[1]);
8697
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008698# ifdef WIN32
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008699 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
8700 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008701 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008702 if (r == NULL)
8703# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008704 if (check_connection() == FAIL
8705 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
8706 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008707# endif
8708 EMSG(_("E277: Unable to read a server reply"));
8709 }
8710#endif
8711 rettv->v_type = VAR_STRING;
8712 rettv->vval.v_string = r;
8713}
8714
8715/*
8716 * "remote_send()" function
8717 */
8718 static void
8719f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
8720{
8721 rettv->v_type = VAR_STRING;
8722 rettv->vval.v_string = NULL;
8723#ifdef FEAT_CLIENTSERVER
8724 remote_common(argvars, rettv, FALSE);
8725#endif
8726}
8727
8728/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01008729 * "remote_startserver()" function
8730 */
8731 static void
8732f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8733{
8734#ifdef FEAT_CLIENTSERVER
8735 char_u *server = get_tv_string_chk(&argvars[0]);
8736
8737 if (server == NULL)
8738 return; /* type error; errmsg already given */
8739 if (serverName != NULL)
8740 EMSG(_("E941: already started a server"));
8741 else
8742 {
8743# ifdef FEAT_X11
8744 if (check_connection() == OK)
8745 serverRegisterName(X_DISPLAY, server);
8746# else
8747 serverSetName(server);
8748# endif
8749 }
8750#else
8751 EMSG(_("E942: +clientserver feature not available"));
8752#endif
8753}
8754
8755/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008756 * "remove()" function
8757 */
8758 static void
8759f_remove(typval_T *argvars, typval_T *rettv)
8760{
8761 list_T *l;
8762 listitem_T *item, *item2;
8763 listitem_T *li;
8764 long idx;
8765 long end;
8766 char_u *key;
8767 dict_T *d;
8768 dictitem_T *di;
8769 char_u *arg_errmsg = (char_u *)N_("remove() argument");
8770
8771 if (argvars[0].v_type == VAR_DICT)
8772 {
8773 if (argvars[2].v_type != VAR_UNKNOWN)
8774 EMSG2(_(e_toomanyarg), "remove()");
8775 else if ((d = argvars[0].vval.v_dict) != NULL
8776 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
8777 {
8778 key = get_tv_string_chk(&argvars[1]);
8779 if (key != NULL)
8780 {
8781 di = dict_find(d, key, -1);
8782 if (di == NULL)
8783 EMSG2(_(e_dictkey), key);
8784 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
8785 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
8786 {
8787 *rettv = di->di_tv;
8788 init_tv(&di->di_tv);
8789 dictitem_remove(d, di);
8790 }
8791 }
8792 }
8793 }
8794 else if (argvars[0].v_type != VAR_LIST)
8795 EMSG2(_(e_listdictarg), "remove()");
8796 else if ((l = argvars[0].vval.v_list) != NULL
8797 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
8798 {
8799 int error = FALSE;
8800
8801 idx = (long)get_tv_number_chk(&argvars[1], &error);
8802 if (error)
8803 ; /* type error: do nothing, errmsg already given */
8804 else if ((item = list_find(l, idx)) == NULL)
8805 EMSGN(_(e_listidx), idx);
8806 else
8807 {
8808 if (argvars[2].v_type == VAR_UNKNOWN)
8809 {
8810 /* Remove one item, return its value. */
8811 vimlist_remove(l, item, item);
8812 *rettv = item->li_tv;
8813 vim_free(item);
8814 }
8815 else
8816 {
8817 /* Remove range of items, return list with values. */
8818 end = (long)get_tv_number_chk(&argvars[2], &error);
8819 if (error)
8820 ; /* type error: do nothing */
8821 else if ((item2 = list_find(l, end)) == NULL)
8822 EMSGN(_(e_listidx), end);
8823 else
8824 {
8825 int cnt = 0;
8826
8827 for (li = item; li != NULL; li = li->li_next)
8828 {
8829 ++cnt;
8830 if (li == item2)
8831 break;
8832 }
8833 if (li == NULL) /* didn't find "item2" after "item" */
8834 EMSG(_(e_invrange));
8835 else
8836 {
8837 vimlist_remove(l, item, item2);
8838 if (rettv_list_alloc(rettv) == OK)
8839 {
8840 l = rettv->vval.v_list;
8841 l->lv_first = item;
8842 l->lv_last = item2;
8843 item->li_prev = NULL;
8844 item2->li_next = NULL;
8845 l->lv_len = cnt;
8846 }
8847 }
8848 }
8849 }
8850 }
8851 }
8852}
8853
8854/*
8855 * "rename({from}, {to})" function
8856 */
8857 static void
8858f_rename(typval_T *argvars, typval_T *rettv)
8859{
8860 char_u buf[NUMBUFLEN];
8861
8862 if (check_restricted() || check_secure())
8863 rettv->vval.v_number = -1;
8864 else
8865 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
8866 get_tv_string_buf(&argvars[1], buf));
8867}
8868
8869/*
8870 * "repeat()" function
8871 */
8872 static void
8873f_repeat(typval_T *argvars, typval_T *rettv)
8874{
8875 char_u *p;
8876 int n;
8877 int slen;
8878 int len;
8879 char_u *r;
8880 int i;
8881
8882 n = (int)get_tv_number(&argvars[1]);
8883 if (argvars[0].v_type == VAR_LIST)
8884 {
8885 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
8886 while (n-- > 0)
8887 if (list_extend(rettv->vval.v_list,
8888 argvars[0].vval.v_list, NULL) == FAIL)
8889 break;
8890 }
8891 else
8892 {
8893 p = get_tv_string(&argvars[0]);
8894 rettv->v_type = VAR_STRING;
8895 rettv->vval.v_string = NULL;
8896
8897 slen = (int)STRLEN(p);
8898 len = slen * n;
8899 if (len <= 0)
8900 return;
8901
8902 r = alloc(len + 1);
8903 if (r != NULL)
8904 {
8905 for (i = 0; i < n; i++)
8906 mch_memmove(r + i * slen, p, (size_t)slen);
8907 r[len] = NUL;
8908 }
8909
8910 rettv->vval.v_string = r;
8911 }
8912}
8913
8914/*
8915 * "resolve()" function
8916 */
8917 static void
8918f_resolve(typval_T *argvars, typval_T *rettv)
8919{
8920 char_u *p;
8921#ifdef HAVE_READLINK
8922 char_u *buf = NULL;
8923#endif
8924
8925 p = get_tv_string(&argvars[0]);
8926#ifdef FEAT_SHORTCUT
8927 {
8928 char_u *v = NULL;
8929
8930 v = mch_resolve_shortcut(p);
8931 if (v != NULL)
8932 rettv->vval.v_string = v;
8933 else
8934 rettv->vval.v_string = vim_strsave(p);
8935 }
8936#else
8937# ifdef HAVE_READLINK
8938 {
8939 char_u *cpy;
8940 int len;
8941 char_u *remain = NULL;
8942 char_u *q;
8943 int is_relative_to_current = FALSE;
8944 int has_trailing_pathsep = FALSE;
8945 int limit = 100;
8946
8947 p = vim_strsave(p);
8948
8949 if (p[0] == '.' && (vim_ispathsep(p[1])
8950 || (p[1] == '.' && (vim_ispathsep(p[2])))))
8951 is_relative_to_current = TRUE;
8952
8953 len = STRLEN(p);
8954 if (len > 0 && after_pathsep(p, p + len))
8955 {
8956 has_trailing_pathsep = TRUE;
8957 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
8958 }
8959
8960 q = getnextcomp(p);
8961 if (*q != NUL)
8962 {
8963 /* Separate the first path component in "p", and keep the
8964 * remainder (beginning with the path separator). */
8965 remain = vim_strsave(q - 1);
8966 q[-1] = NUL;
8967 }
8968
8969 buf = alloc(MAXPATHL + 1);
8970 if (buf == NULL)
8971 goto fail;
8972
8973 for (;;)
8974 {
8975 for (;;)
8976 {
8977 len = readlink((char *)p, (char *)buf, MAXPATHL);
8978 if (len <= 0)
8979 break;
8980 buf[len] = NUL;
8981
8982 if (limit-- == 0)
8983 {
8984 vim_free(p);
8985 vim_free(remain);
8986 EMSG(_("E655: Too many symbolic links (cycle?)"));
8987 rettv->vval.v_string = NULL;
8988 goto fail;
8989 }
8990
8991 /* Ensure that the result will have a trailing path separator
8992 * if the argument has one. */
8993 if (remain == NULL && has_trailing_pathsep)
8994 add_pathsep(buf);
8995
8996 /* Separate the first path component in the link value and
8997 * concatenate the remainders. */
8998 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
8999 if (*q != NUL)
9000 {
9001 if (remain == NULL)
9002 remain = vim_strsave(q - 1);
9003 else
9004 {
9005 cpy = concat_str(q - 1, remain);
9006 if (cpy != NULL)
9007 {
9008 vim_free(remain);
9009 remain = cpy;
9010 }
9011 }
9012 q[-1] = NUL;
9013 }
9014
9015 q = gettail(p);
9016 if (q > p && *q == NUL)
9017 {
9018 /* Ignore trailing path separator. */
9019 q[-1] = NUL;
9020 q = gettail(p);
9021 }
9022 if (q > p && !mch_isFullName(buf))
9023 {
9024 /* symlink is relative to directory of argument */
9025 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
9026 if (cpy != NULL)
9027 {
9028 STRCPY(cpy, p);
9029 STRCPY(gettail(cpy), buf);
9030 vim_free(p);
9031 p = cpy;
9032 }
9033 }
9034 else
9035 {
9036 vim_free(p);
9037 p = vim_strsave(buf);
9038 }
9039 }
9040
9041 if (remain == NULL)
9042 break;
9043
9044 /* Append the first path component of "remain" to "p". */
9045 q = getnextcomp(remain + 1);
9046 len = q - remain - (*q != NUL);
9047 cpy = vim_strnsave(p, STRLEN(p) + len);
9048 if (cpy != NULL)
9049 {
9050 STRNCAT(cpy, remain, len);
9051 vim_free(p);
9052 p = cpy;
9053 }
9054 /* Shorten "remain". */
9055 if (*q != NUL)
9056 STRMOVE(remain, q - 1);
9057 else
9058 {
9059 vim_free(remain);
9060 remain = NULL;
9061 }
9062 }
9063
9064 /* If the result is a relative path name, make it explicitly relative to
9065 * the current directory if and only if the argument had this form. */
9066 if (!vim_ispathsep(*p))
9067 {
9068 if (is_relative_to_current
9069 && *p != NUL
9070 && !(p[0] == '.'
9071 && (p[1] == NUL
9072 || vim_ispathsep(p[1])
9073 || (p[1] == '.'
9074 && (p[2] == NUL
9075 || vim_ispathsep(p[2]))))))
9076 {
9077 /* Prepend "./". */
9078 cpy = concat_str((char_u *)"./", p);
9079 if (cpy != NULL)
9080 {
9081 vim_free(p);
9082 p = cpy;
9083 }
9084 }
9085 else if (!is_relative_to_current)
9086 {
9087 /* Strip leading "./". */
9088 q = p;
9089 while (q[0] == '.' && vim_ispathsep(q[1]))
9090 q += 2;
9091 if (q > p)
9092 STRMOVE(p, p + 2);
9093 }
9094 }
9095
9096 /* Ensure that the result will have no trailing path separator
9097 * if the argument had none. But keep "/" or "//". */
9098 if (!has_trailing_pathsep)
9099 {
9100 q = p + STRLEN(p);
9101 if (after_pathsep(p, q))
9102 *gettail_sep(p) = NUL;
9103 }
9104
9105 rettv->vval.v_string = p;
9106 }
9107# else
9108 rettv->vval.v_string = vim_strsave(p);
9109# endif
9110#endif
9111
9112 simplify_filename(rettv->vval.v_string);
9113
9114#ifdef HAVE_READLINK
9115fail:
9116 vim_free(buf);
9117#endif
9118 rettv->v_type = VAR_STRING;
9119}
9120
9121/*
9122 * "reverse({list})" function
9123 */
9124 static void
9125f_reverse(typval_T *argvars, typval_T *rettv)
9126{
9127 list_T *l;
9128 listitem_T *li, *ni;
9129
9130 if (argvars[0].v_type != VAR_LIST)
9131 EMSG2(_(e_listarg), "reverse()");
9132 else if ((l = argvars[0].vval.v_list) != NULL
9133 && !tv_check_lock(l->lv_lock,
9134 (char_u *)N_("reverse() argument"), TRUE))
9135 {
9136 li = l->lv_last;
9137 l->lv_first = l->lv_last = NULL;
9138 l->lv_len = 0;
9139 while (li != NULL)
9140 {
9141 ni = li->li_prev;
9142 list_append(l, li);
9143 li = ni;
9144 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009145 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009146 l->lv_idx = l->lv_len - l->lv_idx - 1;
9147 }
9148}
9149
9150#define SP_NOMOVE 0x01 /* don't move cursor */
9151#define SP_REPEAT 0x02 /* repeat to find outer pair */
9152#define SP_RETCOUNT 0x04 /* return matchcount */
9153#define SP_SETPCMARK 0x08 /* set previous context mark */
9154#define SP_START 0x10 /* accept match at start position */
9155#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9156#define SP_END 0x40 /* leave cursor at end of match */
9157#define SP_COLUMN 0x80 /* start at cursor column */
9158
9159static int get_search_arg(typval_T *varp, int *flagsp);
9160
9161/*
9162 * Get flags for a search function.
9163 * Possibly sets "p_ws".
9164 * Returns BACKWARD, FORWARD or zero (for an error).
9165 */
9166 static int
9167get_search_arg(typval_T *varp, int *flagsp)
9168{
9169 int dir = FORWARD;
9170 char_u *flags;
9171 char_u nbuf[NUMBUFLEN];
9172 int mask;
9173
9174 if (varp->v_type != VAR_UNKNOWN)
9175 {
9176 flags = get_tv_string_buf_chk(varp, nbuf);
9177 if (flags == NULL)
9178 return 0; /* type error; errmsg already given */
9179 while (*flags != NUL)
9180 {
9181 switch (*flags)
9182 {
9183 case 'b': dir = BACKWARD; break;
9184 case 'w': p_ws = TRUE; break;
9185 case 'W': p_ws = FALSE; break;
9186 default: mask = 0;
9187 if (flagsp != NULL)
9188 switch (*flags)
9189 {
9190 case 'c': mask = SP_START; break;
9191 case 'e': mask = SP_END; break;
9192 case 'm': mask = SP_RETCOUNT; break;
9193 case 'n': mask = SP_NOMOVE; break;
9194 case 'p': mask = SP_SUBPAT; break;
9195 case 'r': mask = SP_REPEAT; break;
9196 case 's': mask = SP_SETPCMARK; break;
9197 case 'z': mask = SP_COLUMN; break;
9198 }
9199 if (mask == 0)
9200 {
9201 EMSG2(_(e_invarg2), flags);
9202 dir = 0;
9203 }
9204 else
9205 *flagsp |= mask;
9206 }
9207 if (dir == 0)
9208 break;
9209 ++flags;
9210 }
9211 }
9212 return dir;
9213}
9214
9215/*
9216 * Shared by search() and searchpos() functions.
9217 */
9218 static int
9219search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9220{
9221 int flags;
9222 char_u *pat;
9223 pos_T pos;
9224 pos_T save_cursor;
9225 int save_p_ws = p_ws;
9226 int dir;
9227 int retval = 0; /* default: FAIL */
9228 long lnum_stop = 0;
9229 proftime_T tm;
9230#ifdef FEAT_RELTIME
9231 long time_limit = 0;
9232#endif
9233 int options = SEARCH_KEEP;
9234 int subpatnum;
9235
9236 pat = get_tv_string(&argvars[0]);
9237 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
9238 if (dir == 0)
9239 goto theend;
9240 flags = *flagsp;
9241 if (flags & SP_START)
9242 options |= SEARCH_START;
9243 if (flags & SP_END)
9244 options |= SEARCH_END;
9245 if (flags & SP_COLUMN)
9246 options |= SEARCH_COL;
9247
9248 /* Optional arguments: line number to stop searching and timeout. */
9249 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9250 {
9251 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
9252 if (lnum_stop < 0)
9253 goto theend;
9254#ifdef FEAT_RELTIME
9255 if (argvars[3].v_type != VAR_UNKNOWN)
9256 {
9257 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
9258 if (time_limit < 0)
9259 goto theend;
9260 }
9261#endif
9262 }
9263
9264#ifdef FEAT_RELTIME
9265 /* Set the time limit, if there is one. */
9266 profile_setlimit(time_limit, &tm);
9267#endif
9268
9269 /*
9270 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
9271 * Check to make sure only those flags are set.
9272 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
9273 * flags cannot be set. Check for that condition also.
9274 */
9275 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
9276 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9277 {
9278 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
9279 goto theend;
9280 }
9281
9282 pos = save_cursor = curwin->w_cursor;
9283 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
9284 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
9285 if (subpatnum != FAIL)
9286 {
9287 if (flags & SP_SUBPAT)
9288 retval = subpatnum;
9289 else
9290 retval = pos.lnum;
9291 if (flags & SP_SETPCMARK)
9292 setpcmark();
9293 curwin->w_cursor = pos;
9294 if (match_pos != NULL)
9295 {
9296 /* Store the match cursor position */
9297 match_pos->lnum = pos.lnum;
9298 match_pos->col = pos.col + 1;
9299 }
9300 /* "/$" will put the cursor after the end of the line, may need to
9301 * correct that here */
9302 check_cursor();
9303 }
9304
9305 /* If 'n' flag is used: restore cursor position. */
9306 if (flags & SP_NOMOVE)
9307 curwin->w_cursor = save_cursor;
9308 else
9309 curwin->w_set_curswant = TRUE;
9310theend:
9311 p_ws = save_p_ws;
9312
9313 return retval;
9314}
9315
9316#ifdef FEAT_FLOAT
9317
9318/*
9319 * round() is not in C90, use ceil() or floor() instead.
9320 */
9321 float_T
9322vim_round(float_T f)
9323{
9324 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
9325}
9326
9327/*
9328 * "round({float})" function
9329 */
9330 static void
9331f_round(typval_T *argvars, typval_T *rettv)
9332{
9333 float_T f = 0.0;
9334
9335 rettv->v_type = VAR_FLOAT;
9336 if (get_float_arg(argvars, &f) == OK)
9337 rettv->vval.v_float = vim_round(f);
9338 else
9339 rettv->vval.v_float = 0.0;
9340}
9341#endif
9342
9343/*
9344 * "screenattr()" function
9345 */
9346 static void
9347f_screenattr(typval_T *argvars, typval_T *rettv)
9348{
9349 int row;
9350 int col;
9351 int c;
9352
9353 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9354 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9355 if (row < 0 || row >= screen_Rows
9356 || col < 0 || col >= screen_Columns)
9357 c = -1;
9358 else
9359 c = ScreenAttrs[LineOffset[row] + col];
9360 rettv->vval.v_number = c;
9361}
9362
9363/*
9364 * "screenchar()" function
9365 */
9366 static void
9367f_screenchar(typval_T *argvars, typval_T *rettv)
9368{
9369 int row;
9370 int col;
9371 int off;
9372 int c;
9373
9374 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9375 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9376 if (row < 0 || row >= screen_Rows
9377 || col < 0 || col >= screen_Columns)
9378 c = -1;
9379 else
9380 {
9381 off = LineOffset[row] + col;
9382#ifdef FEAT_MBYTE
9383 if (enc_utf8 && ScreenLinesUC[off] != 0)
9384 c = ScreenLinesUC[off];
9385 else
9386#endif
9387 c = ScreenLines[off];
9388 }
9389 rettv->vval.v_number = c;
9390}
9391
9392/*
9393 * "screencol()" function
9394 *
9395 * First column is 1 to be consistent with virtcol().
9396 */
9397 static void
9398f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
9399{
9400 rettv->vval.v_number = screen_screencol() + 1;
9401}
9402
9403/*
9404 * "screenrow()" function
9405 */
9406 static void
9407f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
9408{
9409 rettv->vval.v_number = screen_screenrow() + 1;
9410}
9411
9412/*
9413 * "search()" function
9414 */
9415 static void
9416f_search(typval_T *argvars, typval_T *rettv)
9417{
9418 int flags = 0;
9419
9420 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
9421}
9422
9423/*
9424 * "searchdecl()" function
9425 */
9426 static void
9427f_searchdecl(typval_T *argvars, typval_T *rettv)
9428{
9429 int locally = 1;
9430 int thisblock = 0;
9431 int error = FALSE;
9432 char_u *name;
9433
9434 rettv->vval.v_number = 1; /* default: FAIL */
9435
9436 name = get_tv_string_chk(&argvars[0]);
9437 if (argvars[1].v_type != VAR_UNKNOWN)
9438 {
9439 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
9440 if (!error && argvars[2].v_type != VAR_UNKNOWN)
9441 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
9442 }
9443 if (!error && name != NULL)
9444 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
9445 locally, thisblock, SEARCH_KEEP) == FAIL;
9446}
9447
9448/*
9449 * Used by searchpair() and searchpairpos()
9450 */
9451 static int
9452searchpair_cmn(typval_T *argvars, pos_T *match_pos)
9453{
9454 char_u *spat, *mpat, *epat;
9455 char_u *skip;
9456 int save_p_ws = p_ws;
9457 int dir;
9458 int flags = 0;
9459 char_u nbuf1[NUMBUFLEN];
9460 char_u nbuf2[NUMBUFLEN];
9461 char_u nbuf3[NUMBUFLEN];
9462 int retval = 0; /* default: FAIL */
9463 long lnum_stop = 0;
9464 long time_limit = 0;
9465
9466 /* Get the three pattern arguments: start, middle, end. */
9467 spat = get_tv_string_chk(&argvars[0]);
9468 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
9469 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
9470 if (spat == NULL || mpat == NULL || epat == NULL)
9471 goto theend; /* type error */
9472
9473 /* Handle the optional fourth argument: flags */
9474 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
9475 if (dir == 0)
9476 goto theend;
9477
9478 /* Don't accept SP_END or SP_SUBPAT.
9479 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
9480 */
9481 if ((flags & (SP_END | SP_SUBPAT)) != 0
9482 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9483 {
9484 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
9485 goto theend;
9486 }
9487
9488 /* Using 'r' implies 'W', otherwise it doesn't work. */
9489 if (flags & SP_REPEAT)
9490 p_ws = FALSE;
9491
9492 /* Optional fifth argument: skip expression */
9493 if (argvars[3].v_type == VAR_UNKNOWN
9494 || argvars[4].v_type == VAR_UNKNOWN)
9495 skip = (char_u *)"";
9496 else
9497 {
9498 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
9499 if (argvars[5].v_type != VAR_UNKNOWN)
9500 {
9501 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
9502 if (lnum_stop < 0)
9503 goto theend;
9504#ifdef FEAT_RELTIME
9505 if (argvars[6].v_type != VAR_UNKNOWN)
9506 {
9507 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
9508 if (time_limit < 0)
9509 goto theend;
9510 }
9511#endif
9512 }
9513 }
9514 if (skip == NULL)
9515 goto theend; /* type error */
9516
9517 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
9518 match_pos, lnum_stop, time_limit);
9519
9520theend:
9521 p_ws = save_p_ws;
9522
9523 return retval;
9524}
9525
9526/*
9527 * "searchpair()" function
9528 */
9529 static void
9530f_searchpair(typval_T *argvars, typval_T *rettv)
9531{
9532 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
9533}
9534
9535/*
9536 * "searchpairpos()" function
9537 */
9538 static void
9539f_searchpairpos(typval_T *argvars, typval_T *rettv)
9540{
9541 pos_T match_pos;
9542 int lnum = 0;
9543 int col = 0;
9544
9545 if (rettv_list_alloc(rettv) == FAIL)
9546 return;
9547
9548 if (searchpair_cmn(argvars, &match_pos) > 0)
9549 {
9550 lnum = match_pos.lnum;
9551 col = match_pos.col;
9552 }
9553
9554 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9555 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9556}
9557
9558/*
9559 * Search for a start/middle/end thing.
9560 * Used by searchpair(), see its documentation for the details.
9561 * Returns 0 or -1 for no match,
9562 */
9563 long
9564do_searchpair(
9565 char_u *spat, /* start pattern */
9566 char_u *mpat, /* middle pattern */
9567 char_u *epat, /* end pattern */
9568 int dir, /* BACKWARD or FORWARD */
9569 char_u *skip, /* skip expression */
9570 int flags, /* SP_SETPCMARK and other SP_ values */
9571 pos_T *match_pos,
9572 linenr_T lnum_stop, /* stop at this line if not zero */
9573 long time_limit UNUSED) /* stop after this many msec */
9574{
9575 char_u *save_cpo;
9576 char_u *pat, *pat2 = NULL, *pat3 = NULL;
9577 long retval = 0;
9578 pos_T pos;
9579 pos_T firstpos;
9580 pos_T foundpos;
9581 pos_T save_cursor;
9582 pos_T save_pos;
9583 int n;
9584 int r;
9585 int nest = 1;
9586 int err;
9587 int options = SEARCH_KEEP;
9588 proftime_T tm;
9589
9590 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
9591 save_cpo = p_cpo;
9592 p_cpo = empty_option;
9593
9594#ifdef FEAT_RELTIME
9595 /* Set the time limit, if there is one. */
9596 profile_setlimit(time_limit, &tm);
9597#endif
9598
9599 /* Make two search patterns: start/end (pat2, for in nested pairs) and
9600 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009601 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
9602 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009603 if (pat2 == NULL || pat3 == NULL)
9604 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009605 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009606 if (*mpat == NUL)
9607 STRCPY(pat3, pat2);
9608 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009609 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009610 spat, epat, mpat);
9611 if (flags & SP_START)
9612 options |= SEARCH_START;
9613
9614 save_cursor = curwin->w_cursor;
9615 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009616 CLEAR_POS(&firstpos);
9617 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009618 pat = pat3;
9619 for (;;)
9620 {
9621 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
9622 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009623 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009624 /* didn't find it or found the first match again: FAIL */
9625 break;
9626
9627 if (firstpos.lnum == 0)
9628 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009629 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009630 {
9631 /* Found the same position again. Can happen with a pattern that
9632 * has "\zs" at the end and searching backwards. Advance one
9633 * character and try again. */
9634 if (dir == BACKWARD)
9635 decl(&pos);
9636 else
9637 incl(&pos);
9638 }
9639 foundpos = pos;
9640
9641 /* clear the start flag to avoid getting stuck here */
9642 options &= ~SEARCH_START;
9643
9644 /* If the skip pattern matches, ignore this match. */
9645 if (*skip != NUL)
9646 {
9647 save_pos = curwin->w_cursor;
9648 curwin->w_cursor = pos;
9649 r = eval_to_bool(skip, &err, NULL, FALSE);
9650 curwin->w_cursor = save_pos;
9651 if (err)
9652 {
9653 /* Evaluating {skip} caused an error, break here. */
9654 curwin->w_cursor = save_cursor;
9655 retval = -1;
9656 break;
9657 }
9658 if (r)
9659 continue;
9660 }
9661
9662 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
9663 {
9664 /* Found end when searching backwards or start when searching
9665 * forward: nested pair. */
9666 ++nest;
9667 pat = pat2; /* nested, don't search for middle */
9668 }
9669 else
9670 {
9671 /* Found end when searching forward or start when searching
9672 * backward: end of (nested) pair; or found middle in outer pair. */
9673 if (--nest == 1)
9674 pat = pat3; /* outer level, search for middle */
9675 }
9676
9677 if (nest == 0)
9678 {
9679 /* Found the match: return matchcount or line number. */
9680 if (flags & SP_RETCOUNT)
9681 ++retval;
9682 else
9683 retval = pos.lnum;
9684 if (flags & SP_SETPCMARK)
9685 setpcmark();
9686 curwin->w_cursor = pos;
9687 if (!(flags & SP_REPEAT))
9688 break;
9689 nest = 1; /* search for next unmatched */
9690 }
9691 }
9692
9693 if (match_pos != NULL)
9694 {
9695 /* Store the match cursor position */
9696 match_pos->lnum = curwin->w_cursor.lnum;
9697 match_pos->col = curwin->w_cursor.col + 1;
9698 }
9699
9700 /* If 'n' flag is used or search failed: restore cursor position. */
9701 if ((flags & SP_NOMOVE) || retval == 0)
9702 curwin->w_cursor = save_cursor;
9703
9704theend:
9705 vim_free(pat2);
9706 vim_free(pat3);
9707 if (p_cpo == empty_option)
9708 p_cpo = save_cpo;
9709 else
9710 /* Darn, evaluating the {skip} expression changed the value. */
9711 free_string_option(save_cpo);
9712
9713 return retval;
9714}
9715
9716/*
9717 * "searchpos()" function
9718 */
9719 static void
9720f_searchpos(typval_T *argvars, typval_T *rettv)
9721{
9722 pos_T match_pos;
9723 int lnum = 0;
9724 int col = 0;
9725 int n;
9726 int flags = 0;
9727
9728 if (rettv_list_alloc(rettv) == FAIL)
9729 return;
9730
9731 n = search_cmn(argvars, &match_pos, &flags);
9732 if (n > 0)
9733 {
9734 lnum = match_pos.lnum;
9735 col = match_pos.col;
9736 }
9737
9738 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9739 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9740 if (flags & SP_SUBPAT)
9741 list_append_number(rettv->vval.v_list, (varnumber_T)n);
9742}
9743
9744 static void
9745f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
9746{
9747#ifdef FEAT_CLIENTSERVER
9748 char_u buf[NUMBUFLEN];
9749 char_u *server = get_tv_string_chk(&argvars[0]);
9750 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
9751
9752 rettv->vval.v_number = -1;
9753 if (server == NULL || reply == NULL)
9754 return;
9755 if (check_restricted() || check_secure())
9756 return;
9757# ifdef FEAT_X11
9758 if (check_connection() == FAIL)
9759 return;
9760# endif
9761
9762 if (serverSendReply(server, reply) < 0)
9763 {
9764 EMSG(_("E258: Unable to send to client"));
9765 return;
9766 }
9767 rettv->vval.v_number = 0;
9768#else
9769 rettv->vval.v_number = -1;
9770#endif
9771}
9772
9773 static void
9774f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
9775{
9776 char_u *r = NULL;
9777
9778#ifdef FEAT_CLIENTSERVER
9779# ifdef WIN32
9780 r = serverGetVimNames();
9781# else
9782 make_connection();
9783 if (X_DISPLAY != NULL)
9784 r = serverGetVimNames(X_DISPLAY);
9785# endif
9786#endif
9787 rettv->v_type = VAR_STRING;
9788 rettv->vval.v_string = r;
9789}
9790
9791/*
9792 * "setbufvar()" function
9793 */
9794 static void
9795f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
9796{
9797 buf_T *buf;
9798 char_u *varname, *bufvarname;
9799 typval_T *varp;
9800 char_u nbuf[NUMBUFLEN];
9801
9802 if (check_restricted() || check_secure())
9803 return;
9804 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9805 varname = get_tv_string_chk(&argvars[1]);
9806 buf = get_buf_tv(&argvars[0], FALSE);
9807 varp = &argvars[2];
9808
9809 if (buf != NULL && varname != NULL && varp != NULL)
9810 {
9811 if (*varname == '&')
9812 {
9813 long numval;
9814 char_u *strval;
9815 int error = FALSE;
9816 aco_save_T aco;
9817
9818 /* set curbuf to be our buf, temporarily */
9819 aucmd_prepbuf(&aco, buf);
9820
9821 ++varname;
9822 numval = (long)get_tv_number_chk(varp, &error);
9823 strval = get_tv_string_buf_chk(varp, nbuf);
9824 if (!error && strval != NULL)
9825 set_option_value(varname, numval, strval, OPT_LOCAL);
9826
9827 /* reset notion of buffer */
9828 aucmd_restbuf(&aco);
9829 }
9830 else
9831 {
9832 buf_T *save_curbuf = curbuf;
9833
9834 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
9835 if (bufvarname != NULL)
9836 {
9837 curbuf = buf;
9838 STRCPY(bufvarname, "b:");
9839 STRCPY(bufvarname + 2, varname);
9840 set_var(bufvarname, varp, TRUE);
9841 vim_free(bufvarname);
9842 curbuf = save_curbuf;
9843 }
9844 }
9845 }
9846}
9847
9848 static void
9849f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
9850{
9851 dict_T *d;
9852 dictitem_T *di;
9853 char_u *csearch;
9854
9855 if (argvars[0].v_type != VAR_DICT)
9856 {
9857 EMSG(_(e_dictreq));
9858 return;
9859 }
9860
9861 if ((d = argvars[0].vval.v_dict) != NULL)
9862 {
9863 csearch = get_dict_string(d, (char_u *)"char", FALSE);
9864 if (csearch != NULL)
9865 {
9866#ifdef FEAT_MBYTE
9867 if (enc_utf8)
9868 {
9869 int pcc[MAX_MCO];
9870 int c = utfc_ptr2char(csearch, pcc);
9871
9872 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
9873 }
9874 else
9875#endif
9876 set_last_csearch(PTR2CHAR(csearch),
9877 csearch, MB_PTR2LEN(csearch));
9878 }
9879
9880 di = dict_find(d, (char_u *)"forward", -1);
9881 if (di != NULL)
9882 set_csearch_direction((int)get_tv_number(&di->di_tv)
9883 ? FORWARD : BACKWARD);
9884
9885 di = dict_find(d, (char_u *)"until", -1);
9886 if (di != NULL)
9887 set_csearch_until(!!get_tv_number(&di->di_tv));
9888 }
9889}
9890
9891/*
9892 * "setcmdpos()" function
9893 */
9894 static void
9895f_setcmdpos(typval_T *argvars, typval_T *rettv)
9896{
9897 int pos = (int)get_tv_number(&argvars[0]) - 1;
9898
9899 if (pos >= 0)
9900 rettv->vval.v_number = set_cmdline_pos(pos);
9901}
9902
9903/*
9904 * "setfperm({fname}, {mode})" function
9905 */
9906 static void
9907f_setfperm(typval_T *argvars, typval_T *rettv)
9908{
9909 char_u *fname;
9910 char_u modebuf[NUMBUFLEN];
9911 char_u *mode_str;
9912 int i;
9913 int mask;
9914 int mode = 0;
9915
9916 rettv->vval.v_number = 0;
9917 fname = get_tv_string_chk(&argvars[0]);
9918 if (fname == NULL)
9919 return;
9920 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
9921 if (mode_str == NULL)
9922 return;
9923 if (STRLEN(mode_str) != 9)
9924 {
9925 EMSG2(_(e_invarg2), mode_str);
9926 return;
9927 }
9928
9929 mask = 1;
9930 for (i = 8; i >= 0; --i)
9931 {
9932 if (mode_str[i] != '-')
9933 mode |= mask;
9934 mask = mask << 1;
9935 }
9936 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
9937}
9938
9939/*
9940 * "setline()" function
9941 */
9942 static void
9943f_setline(typval_T *argvars, typval_T *rettv)
9944{
9945 linenr_T lnum;
9946 char_u *line = NULL;
9947 list_T *l = NULL;
9948 listitem_T *li = NULL;
9949 long added = 0;
9950 linenr_T lcount = curbuf->b_ml.ml_line_count;
9951
9952 lnum = get_tv_lnum(&argvars[0]);
9953 if (argvars[1].v_type == VAR_LIST)
9954 {
9955 l = argvars[1].vval.v_list;
9956 li = l->lv_first;
9957 }
9958 else
9959 line = get_tv_string_chk(&argvars[1]);
9960
9961 /* default result is zero == OK */
9962 for (;;)
9963 {
9964 if (l != NULL)
9965 {
9966 /* list argument, get next string */
9967 if (li == NULL)
9968 break;
9969 line = get_tv_string_chk(&li->li_tv);
9970 li = li->li_next;
9971 }
9972
9973 rettv->vval.v_number = 1; /* FAIL */
9974 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
9975 break;
9976
9977 /* When coming here from Insert mode, sync undo, so that this can be
9978 * undone separately from what was previously inserted. */
9979 if (u_sync_once == 2)
9980 {
9981 u_sync_once = 1; /* notify that u_sync() was called */
9982 u_sync(TRUE);
9983 }
9984
9985 if (lnum <= curbuf->b_ml.ml_line_count)
9986 {
9987 /* existing line, replace it */
9988 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
9989 {
9990 changed_bytes(lnum, 0);
9991 if (lnum == curwin->w_cursor.lnum)
9992 check_cursor_col();
9993 rettv->vval.v_number = 0; /* OK */
9994 }
9995 }
9996 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
9997 {
9998 /* lnum is one past the last line, append the line */
9999 ++added;
10000 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
10001 rettv->vval.v_number = 0; /* OK */
10002 }
10003
10004 if (l == NULL) /* only one string argument */
10005 break;
10006 ++lnum;
10007 }
10008
10009 if (added > 0)
10010 appended_lines_mark(lcount, added);
10011}
10012
Bram Moolenaard823fa92016-08-12 16:29:27 +020010013static 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 +020010014
10015/*
10016 * Used by "setqflist()" and "setloclist()" functions
10017 */
10018 static void
10019set_qf_ll_list(
10020 win_T *wp UNUSED,
10021 typval_T *list_arg UNUSED,
10022 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010023 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010024 typval_T *rettv)
10025{
10026#ifdef FEAT_QUICKFIX
10027 static char *e_invact = N_("E927: Invalid action: '%s'");
10028 char_u *act;
10029 int action = 0;
10030#endif
10031
10032 rettv->vval.v_number = -1;
10033
10034#ifdef FEAT_QUICKFIX
10035 if (list_arg->v_type != VAR_LIST)
10036 EMSG(_(e_listreq));
10037 else
10038 {
10039 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010040 dict_T *d = NULL;
10041 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010042
10043 if (action_arg->v_type == VAR_STRING)
10044 {
10045 act = get_tv_string_chk(action_arg);
10046 if (act == NULL)
10047 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020010048 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
10049 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010050 action = *act;
10051 else
10052 EMSG2(_(e_invact), act);
10053 }
10054 else if (action_arg->v_type == VAR_UNKNOWN)
10055 action = ' ';
10056 else
10057 EMSG(_(e_stringreq));
10058
Bram Moolenaard823fa92016-08-12 16:29:27 +020010059 if (action_arg->v_type != VAR_UNKNOWN
10060 && what_arg->v_type != VAR_UNKNOWN)
10061 {
10062 if (what_arg->v_type == VAR_DICT)
10063 d = what_arg->vval.v_dict;
10064 else
10065 {
10066 EMSG(_(e_dictreq));
10067 valid_dict = FALSE;
10068 }
10069 }
10070
10071 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
10072 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()"), d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010073 rettv->vval.v_number = 0;
10074 }
10075#endif
10076}
10077
10078/*
10079 * "setloclist()" function
10080 */
10081 static void
10082f_setloclist(typval_T *argvars, typval_T *rettv)
10083{
10084 win_T *win;
10085
10086 rettv->vval.v_number = -1;
10087
10088 win = find_win_by_nr(&argvars[0], NULL);
10089 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020010090 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010091}
10092
10093/*
10094 * "setmatches()" function
10095 */
10096 static void
10097f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10098{
10099#ifdef FEAT_SEARCH_EXTRA
10100 list_T *l;
10101 listitem_T *li;
10102 dict_T *d;
10103 list_T *s = NULL;
10104
10105 rettv->vval.v_number = -1;
10106 if (argvars[0].v_type != VAR_LIST)
10107 {
10108 EMSG(_(e_listreq));
10109 return;
10110 }
10111 if ((l = argvars[0].vval.v_list) != NULL)
10112 {
10113
10114 /* To some extent make sure that we are dealing with a list from
10115 * "getmatches()". */
10116 li = l->lv_first;
10117 while (li != NULL)
10118 {
10119 if (li->li_tv.v_type != VAR_DICT
10120 || (d = li->li_tv.vval.v_dict) == NULL)
10121 {
10122 EMSG(_(e_invarg));
10123 return;
10124 }
10125 if (!(dict_find(d, (char_u *)"group", -1) != NULL
10126 && (dict_find(d, (char_u *)"pattern", -1) != NULL
10127 || dict_find(d, (char_u *)"pos1", -1) != NULL)
10128 && dict_find(d, (char_u *)"priority", -1) != NULL
10129 && dict_find(d, (char_u *)"id", -1) != NULL))
10130 {
10131 EMSG(_(e_invarg));
10132 return;
10133 }
10134 li = li->li_next;
10135 }
10136
10137 clear_matches(curwin);
10138 li = l->lv_first;
10139 while (li != NULL)
10140 {
10141 int i = 0;
10142 char_u buf[5];
10143 dictitem_T *di;
10144 char_u *group;
10145 int priority;
10146 int id;
10147 char_u *conceal;
10148
10149 d = li->li_tv.vval.v_dict;
10150 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
10151 {
10152 if (s == NULL)
10153 {
10154 s = list_alloc();
10155 if (s == NULL)
10156 return;
10157 }
10158
10159 /* match from matchaddpos() */
10160 for (i = 1; i < 9; i++)
10161 {
10162 sprintf((char *)buf, (char *)"pos%d", i);
10163 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
10164 {
10165 if (di->di_tv.v_type != VAR_LIST)
10166 return;
10167
10168 list_append_tv(s, &di->di_tv);
10169 s->lv_refcount++;
10170 }
10171 else
10172 break;
10173 }
10174 }
10175
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010176 group = get_dict_string(d, (char_u *)"group", TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010177 priority = (int)get_dict_number(d, (char_u *)"priority");
10178 id = (int)get_dict_number(d, (char_u *)"id");
10179 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010180 ? get_dict_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010181 : NULL;
10182 if (i == 0)
10183 {
10184 match_add(curwin, group,
10185 get_dict_string(d, (char_u *)"pattern", FALSE),
10186 priority, id, NULL, conceal);
10187 }
10188 else
10189 {
10190 match_add(curwin, group, NULL, priority, id, s, conceal);
10191 list_unref(s);
10192 s = NULL;
10193 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010194 vim_free(group);
10195 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010196
10197 li = li->li_next;
10198 }
10199 rettv->vval.v_number = 0;
10200 }
10201#endif
10202}
10203
10204/*
10205 * "setpos()" function
10206 */
10207 static void
10208f_setpos(typval_T *argvars, typval_T *rettv)
10209{
10210 pos_T pos;
10211 int fnum;
10212 char_u *name;
10213 colnr_T curswant = -1;
10214
10215 rettv->vval.v_number = -1;
10216 name = get_tv_string_chk(argvars);
10217 if (name != NULL)
10218 {
10219 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10220 {
10221 if (--pos.col < 0)
10222 pos.col = 0;
10223 if (name[0] == '.' && name[1] == NUL)
10224 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010225 /* set cursor; "fnum" is ignored */
10226 curwin->w_cursor = pos;
10227 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010228 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010229 curwin->w_curswant = curswant - 1;
10230 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010231 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010232 check_cursor();
10233 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010234 }
10235 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10236 {
10237 /* set mark */
10238 if (setmark_pos(name[1], &pos, fnum) == OK)
10239 rettv->vval.v_number = 0;
10240 }
10241 else
10242 EMSG(_(e_invarg));
10243 }
10244 }
10245}
10246
10247/*
10248 * "setqflist()" function
10249 */
10250 static void
10251f_setqflist(typval_T *argvars, typval_T *rettv)
10252{
Bram Moolenaard823fa92016-08-12 16:29:27 +020010253 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010254}
10255
10256/*
10257 * "setreg()" function
10258 */
10259 static void
10260f_setreg(typval_T *argvars, typval_T *rettv)
10261{
10262 int regname;
10263 char_u *strregname;
10264 char_u *stropt;
10265 char_u *strval;
10266 int append;
10267 char_u yank_type;
10268 long block_len;
10269
10270 block_len = -1;
10271 yank_type = MAUTO;
10272 append = FALSE;
10273
10274 strregname = get_tv_string_chk(argvars);
10275 rettv->vval.v_number = 1; /* FAIL is default */
10276
10277 if (strregname == NULL)
10278 return; /* type error; errmsg already given */
10279 regname = *strregname;
10280 if (regname == 0 || regname == '@')
10281 regname = '"';
10282
10283 if (argvars[2].v_type != VAR_UNKNOWN)
10284 {
10285 stropt = get_tv_string_chk(&argvars[2]);
10286 if (stropt == NULL)
10287 return; /* type error */
10288 for (; *stropt != NUL; ++stropt)
10289 switch (*stropt)
10290 {
10291 case 'a': case 'A': /* append */
10292 append = TRUE;
10293 break;
10294 case 'v': case 'c': /* character-wise selection */
10295 yank_type = MCHAR;
10296 break;
10297 case 'V': case 'l': /* line-wise selection */
10298 yank_type = MLINE;
10299 break;
10300 case 'b': case Ctrl_V: /* block-wise selection */
10301 yank_type = MBLOCK;
10302 if (VIM_ISDIGIT(stropt[1]))
10303 {
10304 ++stropt;
10305 block_len = getdigits(&stropt) - 1;
10306 --stropt;
10307 }
10308 break;
10309 }
10310 }
10311
10312 if (argvars[1].v_type == VAR_LIST)
10313 {
10314 char_u **lstval;
10315 char_u **allocval;
10316 char_u buf[NUMBUFLEN];
10317 char_u **curval;
10318 char_u **curallocval;
10319 list_T *ll = argvars[1].vval.v_list;
10320 listitem_T *li;
10321 int len;
10322
10323 /* If the list is NULL handle like an empty list. */
10324 len = ll == NULL ? 0 : ll->lv_len;
10325
10326 /* First half: use for pointers to result lines; second half: use for
10327 * pointers to allocated copies. */
10328 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
10329 if (lstval == NULL)
10330 return;
10331 curval = lstval;
10332 allocval = lstval + len + 2;
10333 curallocval = allocval;
10334
10335 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
10336 li = li->li_next)
10337 {
10338 strval = get_tv_string_buf_chk(&li->li_tv, buf);
10339 if (strval == NULL)
10340 goto free_lstval;
10341 if (strval == buf)
10342 {
10343 /* Need to make a copy, next get_tv_string_buf_chk() will
10344 * overwrite the string. */
10345 strval = vim_strsave(buf);
10346 if (strval == NULL)
10347 goto free_lstval;
10348 *curallocval++ = strval;
10349 }
10350 *curval++ = strval;
10351 }
10352 *curval++ = NULL;
10353
10354 write_reg_contents_lst(regname, lstval, -1,
10355 append, yank_type, block_len);
10356free_lstval:
10357 while (curallocval > allocval)
10358 vim_free(*--curallocval);
10359 vim_free(lstval);
10360 }
10361 else
10362 {
10363 strval = get_tv_string_chk(&argvars[1]);
10364 if (strval == NULL)
10365 return;
10366 write_reg_contents_ex(regname, strval, -1,
10367 append, yank_type, block_len);
10368 }
10369 rettv->vval.v_number = 0;
10370}
10371
10372/*
10373 * "settabvar()" function
10374 */
10375 static void
10376f_settabvar(typval_T *argvars, typval_T *rettv)
10377{
10378#ifdef FEAT_WINDOWS
10379 tabpage_T *save_curtab;
10380 tabpage_T *tp;
10381#endif
10382 char_u *varname, *tabvarname;
10383 typval_T *varp;
10384
10385 rettv->vval.v_number = 0;
10386
10387 if (check_restricted() || check_secure())
10388 return;
10389
10390#ifdef FEAT_WINDOWS
10391 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
10392#endif
10393 varname = get_tv_string_chk(&argvars[1]);
10394 varp = &argvars[2];
10395
10396 if (varname != NULL && varp != NULL
10397#ifdef FEAT_WINDOWS
10398 && tp != NULL
10399#endif
10400 )
10401 {
10402#ifdef FEAT_WINDOWS
10403 save_curtab = curtab;
10404 goto_tabpage_tp(tp, FALSE, FALSE);
10405#endif
10406
10407 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
10408 if (tabvarname != NULL)
10409 {
10410 STRCPY(tabvarname, "t:");
10411 STRCPY(tabvarname + 2, varname);
10412 set_var(tabvarname, varp, TRUE);
10413 vim_free(tabvarname);
10414 }
10415
10416#ifdef FEAT_WINDOWS
10417 /* Restore current tabpage */
10418 if (valid_tabpage(save_curtab))
10419 goto_tabpage_tp(save_curtab, FALSE, FALSE);
10420#endif
10421 }
10422}
10423
10424/*
10425 * "settabwinvar()" function
10426 */
10427 static void
10428f_settabwinvar(typval_T *argvars, typval_T *rettv)
10429{
10430 setwinvar(argvars, rettv, 1);
10431}
10432
10433/*
10434 * "setwinvar()" function
10435 */
10436 static void
10437f_setwinvar(typval_T *argvars, typval_T *rettv)
10438{
10439 setwinvar(argvars, rettv, 0);
10440}
10441
10442#ifdef FEAT_CRYPT
10443/*
10444 * "sha256({string})" function
10445 */
10446 static void
10447f_sha256(typval_T *argvars, typval_T *rettv)
10448{
10449 char_u *p;
10450
10451 p = get_tv_string(&argvars[0]);
10452 rettv->vval.v_string = vim_strsave(
10453 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
10454 rettv->v_type = VAR_STRING;
10455}
10456#endif /* FEAT_CRYPT */
10457
10458/*
10459 * "shellescape({string})" function
10460 */
10461 static void
10462f_shellescape(typval_T *argvars, typval_T *rettv)
10463{
Bram Moolenaar20615522017-06-05 18:46:26 +020010464 int do_special = non_zero_arg(&argvars[1]);
10465
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010466 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar20615522017-06-05 18:46:26 +020010467 get_tv_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010468 rettv->v_type = VAR_STRING;
10469}
10470
10471/*
10472 * shiftwidth() function
10473 */
10474 static void
10475f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
10476{
10477 rettv->vval.v_number = get_sw_value(curbuf);
10478}
10479
10480/*
10481 * "simplify()" function
10482 */
10483 static void
10484f_simplify(typval_T *argvars, typval_T *rettv)
10485{
10486 char_u *p;
10487
10488 p = get_tv_string(&argvars[0]);
10489 rettv->vval.v_string = vim_strsave(p);
10490 simplify_filename(rettv->vval.v_string); /* simplify in place */
10491 rettv->v_type = VAR_STRING;
10492}
10493
10494#ifdef FEAT_FLOAT
10495/*
10496 * "sin()" function
10497 */
10498 static void
10499f_sin(typval_T *argvars, typval_T *rettv)
10500{
10501 float_T f = 0.0;
10502
10503 rettv->v_type = VAR_FLOAT;
10504 if (get_float_arg(argvars, &f) == OK)
10505 rettv->vval.v_float = sin(f);
10506 else
10507 rettv->vval.v_float = 0.0;
10508}
10509
10510/*
10511 * "sinh()" function
10512 */
10513 static void
10514f_sinh(typval_T *argvars, typval_T *rettv)
10515{
10516 float_T f = 0.0;
10517
10518 rettv->v_type = VAR_FLOAT;
10519 if (get_float_arg(argvars, &f) == OK)
10520 rettv->vval.v_float = sinh(f);
10521 else
10522 rettv->vval.v_float = 0.0;
10523}
10524#endif
10525
10526static int
10527#ifdef __BORLANDC__
10528 _RTLENTRYF
10529#endif
10530 item_compare(const void *s1, const void *s2);
10531static int
10532#ifdef __BORLANDC__
10533 _RTLENTRYF
10534#endif
10535 item_compare2(const void *s1, const void *s2);
10536
10537/* struct used in the array that's given to qsort() */
10538typedef struct
10539{
10540 listitem_T *item;
10541 int idx;
10542} sortItem_T;
10543
10544/* struct storing information about current sort */
10545typedef struct
10546{
10547 int item_compare_ic;
10548 int item_compare_numeric;
10549 int item_compare_numbers;
10550#ifdef FEAT_FLOAT
10551 int item_compare_float;
10552#endif
10553 char_u *item_compare_func;
10554 partial_T *item_compare_partial;
10555 dict_T *item_compare_selfdict;
10556 int item_compare_func_err;
10557 int item_compare_keep_zero;
10558} sortinfo_T;
10559static sortinfo_T *sortinfo = NULL;
10560static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
10561#define ITEM_COMPARE_FAIL 999
10562
10563/*
10564 * Compare functions for f_sort() and f_uniq() below.
10565 */
10566 static int
10567#ifdef __BORLANDC__
10568_RTLENTRYF
10569#endif
10570item_compare(const void *s1, const void *s2)
10571{
10572 sortItem_T *si1, *si2;
10573 typval_T *tv1, *tv2;
10574 char_u *p1, *p2;
10575 char_u *tofree1 = NULL, *tofree2 = NULL;
10576 int res;
10577 char_u numbuf1[NUMBUFLEN];
10578 char_u numbuf2[NUMBUFLEN];
10579
10580 si1 = (sortItem_T *)s1;
10581 si2 = (sortItem_T *)s2;
10582 tv1 = &si1->item->li_tv;
10583 tv2 = &si2->item->li_tv;
10584
10585 if (sortinfo->item_compare_numbers)
10586 {
10587 varnumber_T v1 = get_tv_number(tv1);
10588 varnumber_T v2 = get_tv_number(tv2);
10589
10590 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10591 }
10592
10593#ifdef FEAT_FLOAT
10594 if (sortinfo->item_compare_float)
10595 {
10596 float_T v1 = get_tv_float(tv1);
10597 float_T v2 = get_tv_float(tv2);
10598
10599 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10600 }
10601#endif
10602
10603 /* tv2string() puts quotes around a string and allocates memory. Don't do
10604 * that for string variables. Use a single quote when comparing with a
10605 * non-string to do what the docs promise. */
10606 if (tv1->v_type == VAR_STRING)
10607 {
10608 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
10609 p1 = (char_u *)"'";
10610 else
10611 p1 = tv1->vval.v_string;
10612 }
10613 else
10614 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
10615 if (tv2->v_type == VAR_STRING)
10616 {
10617 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
10618 p2 = (char_u *)"'";
10619 else
10620 p2 = tv2->vval.v_string;
10621 }
10622 else
10623 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
10624 if (p1 == NULL)
10625 p1 = (char_u *)"";
10626 if (p2 == NULL)
10627 p2 = (char_u *)"";
10628 if (!sortinfo->item_compare_numeric)
10629 {
10630 if (sortinfo->item_compare_ic)
10631 res = STRICMP(p1, p2);
10632 else
10633 res = STRCMP(p1, p2);
10634 }
10635 else
10636 {
10637 double n1, n2;
10638 n1 = strtod((char *)p1, (char **)&p1);
10639 n2 = strtod((char *)p2, (char **)&p2);
10640 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
10641 }
10642
10643 /* When the result would be zero, compare the item indexes. Makes the
10644 * sort stable. */
10645 if (res == 0 && !sortinfo->item_compare_keep_zero)
10646 res = si1->idx > si2->idx ? 1 : -1;
10647
10648 vim_free(tofree1);
10649 vim_free(tofree2);
10650 return res;
10651}
10652
10653 static int
10654#ifdef __BORLANDC__
10655_RTLENTRYF
10656#endif
10657item_compare2(const void *s1, const void *s2)
10658{
10659 sortItem_T *si1, *si2;
10660 int res;
10661 typval_T rettv;
10662 typval_T argv[3];
10663 int dummy;
10664 char_u *func_name;
10665 partial_T *partial = sortinfo->item_compare_partial;
10666
10667 /* shortcut after failure in previous call; compare all items equal */
10668 if (sortinfo->item_compare_func_err)
10669 return 0;
10670
10671 si1 = (sortItem_T *)s1;
10672 si2 = (sortItem_T *)s2;
10673
10674 if (partial == NULL)
10675 func_name = sortinfo->item_compare_func;
10676 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020010677 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010678
10679 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
10680 * in the copy without changing the original list items. */
10681 copy_tv(&si1->item->li_tv, &argv[0]);
10682 copy_tv(&si2->item->li_tv, &argv[1]);
10683
10684 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
10685 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020010686 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010687 partial, sortinfo->item_compare_selfdict);
10688 clear_tv(&argv[0]);
10689 clear_tv(&argv[1]);
10690
10691 if (res == FAIL)
10692 res = ITEM_COMPARE_FAIL;
10693 else
10694 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
10695 if (sortinfo->item_compare_func_err)
10696 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
10697 clear_tv(&rettv);
10698
10699 /* When the result would be zero, compare the pointers themselves. Makes
10700 * the sort stable. */
10701 if (res == 0 && !sortinfo->item_compare_keep_zero)
10702 res = si1->idx > si2->idx ? 1 : -1;
10703
10704 return res;
10705}
10706
10707/*
10708 * "sort({list})" function
10709 */
10710 static void
10711do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
10712{
10713 list_T *l;
10714 listitem_T *li;
10715 sortItem_T *ptrs;
10716 sortinfo_T *old_sortinfo;
10717 sortinfo_T info;
10718 long len;
10719 long i;
10720
10721 /* Pointer to current info struct used in compare function. Save and
10722 * restore the current one for nested calls. */
10723 old_sortinfo = sortinfo;
10724 sortinfo = &info;
10725
10726 if (argvars[0].v_type != VAR_LIST)
10727 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
10728 else
10729 {
10730 l = argvars[0].vval.v_list;
10731 if (l == NULL || tv_check_lock(l->lv_lock,
10732 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
10733 TRUE))
10734 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020010735 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010736
10737 len = list_len(l);
10738 if (len <= 1)
10739 goto theend; /* short list sorts pretty quickly */
10740
10741 info.item_compare_ic = FALSE;
10742 info.item_compare_numeric = FALSE;
10743 info.item_compare_numbers = FALSE;
10744#ifdef FEAT_FLOAT
10745 info.item_compare_float = FALSE;
10746#endif
10747 info.item_compare_func = NULL;
10748 info.item_compare_partial = NULL;
10749 info.item_compare_selfdict = NULL;
10750 if (argvars[1].v_type != VAR_UNKNOWN)
10751 {
10752 /* optional second argument: {func} */
10753 if (argvars[1].v_type == VAR_FUNC)
10754 info.item_compare_func = argvars[1].vval.v_string;
10755 else if (argvars[1].v_type == VAR_PARTIAL)
10756 info.item_compare_partial = argvars[1].vval.v_partial;
10757 else
10758 {
10759 int error = FALSE;
10760
10761 i = (long)get_tv_number_chk(&argvars[1], &error);
10762 if (error)
10763 goto theend; /* type error; errmsg already given */
10764 if (i == 1)
10765 info.item_compare_ic = TRUE;
10766 else if (argvars[1].v_type != VAR_NUMBER)
10767 info.item_compare_func = get_tv_string(&argvars[1]);
10768 else if (i != 0)
10769 {
10770 EMSG(_(e_invarg));
10771 goto theend;
10772 }
10773 if (info.item_compare_func != NULL)
10774 {
10775 if (*info.item_compare_func == NUL)
10776 {
10777 /* empty string means default sort */
10778 info.item_compare_func = NULL;
10779 }
10780 else if (STRCMP(info.item_compare_func, "n") == 0)
10781 {
10782 info.item_compare_func = NULL;
10783 info.item_compare_numeric = TRUE;
10784 }
10785 else if (STRCMP(info.item_compare_func, "N") == 0)
10786 {
10787 info.item_compare_func = NULL;
10788 info.item_compare_numbers = TRUE;
10789 }
10790#ifdef FEAT_FLOAT
10791 else if (STRCMP(info.item_compare_func, "f") == 0)
10792 {
10793 info.item_compare_func = NULL;
10794 info.item_compare_float = TRUE;
10795 }
10796#endif
10797 else if (STRCMP(info.item_compare_func, "i") == 0)
10798 {
10799 info.item_compare_func = NULL;
10800 info.item_compare_ic = TRUE;
10801 }
10802 }
10803 }
10804
10805 if (argvars[2].v_type != VAR_UNKNOWN)
10806 {
10807 /* optional third argument: {dict} */
10808 if (argvars[2].v_type != VAR_DICT)
10809 {
10810 EMSG(_(e_dictreq));
10811 goto theend;
10812 }
10813 info.item_compare_selfdict = argvars[2].vval.v_dict;
10814 }
10815 }
10816
10817 /* Make an array with each entry pointing to an item in the List. */
10818 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
10819 if (ptrs == NULL)
10820 goto theend;
10821
10822 i = 0;
10823 if (sort)
10824 {
10825 /* sort(): ptrs will be the list to sort */
10826 for (li = l->lv_first; li != NULL; li = li->li_next)
10827 {
10828 ptrs[i].item = li;
10829 ptrs[i].idx = i;
10830 ++i;
10831 }
10832
10833 info.item_compare_func_err = FALSE;
10834 info.item_compare_keep_zero = FALSE;
10835 /* test the compare function */
10836 if ((info.item_compare_func != NULL
10837 || info.item_compare_partial != NULL)
10838 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
10839 == ITEM_COMPARE_FAIL)
10840 EMSG(_("E702: Sort compare function failed"));
10841 else
10842 {
10843 /* Sort the array with item pointers. */
10844 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
10845 info.item_compare_func == NULL
10846 && info.item_compare_partial == NULL
10847 ? item_compare : item_compare2);
10848
10849 if (!info.item_compare_func_err)
10850 {
10851 /* Clear the List and append the items in sorted order. */
10852 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
10853 l->lv_len = 0;
10854 for (i = 0; i < len; ++i)
10855 list_append(l, ptrs[i].item);
10856 }
10857 }
10858 }
10859 else
10860 {
10861 int (*item_compare_func_ptr)(const void *, const void *);
10862
10863 /* f_uniq(): ptrs will be a stack of items to remove */
10864 info.item_compare_func_err = FALSE;
10865 info.item_compare_keep_zero = TRUE;
10866 item_compare_func_ptr = info.item_compare_func != NULL
10867 || info.item_compare_partial != NULL
10868 ? item_compare2 : item_compare;
10869
10870 for (li = l->lv_first; li != NULL && li->li_next != NULL;
10871 li = li->li_next)
10872 {
10873 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
10874 == 0)
10875 ptrs[i++].item = li;
10876 if (info.item_compare_func_err)
10877 {
10878 EMSG(_("E882: Uniq compare function failed"));
10879 break;
10880 }
10881 }
10882
10883 if (!info.item_compare_func_err)
10884 {
10885 while (--i >= 0)
10886 {
10887 li = ptrs[i].item->li_next;
10888 ptrs[i].item->li_next = li->li_next;
10889 if (li->li_next != NULL)
10890 li->li_next->li_prev = ptrs[i].item;
10891 else
10892 l->lv_last = ptrs[i].item;
10893 list_fix_watch(l, li);
10894 listitem_free(li);
10895 l->lv_len--;
10896 }
10897 }
10898 }
10899
10900 vim_free(ptrs);
10901 }
10902theend:
10903 sortinfo = old_sortinfo;
10904}
10905
10906/*
10907 * "sort({list})" function
10908 */
10909 static void
10910f_sort(typval_T *argvars, typval_T *rettv)
10911{
10912 do_sort_uniq(argvars, rettv, TRUE);
10913}
10914
10915/*
10916 * "uniq({list})" function
10917 */
10918 static void
10919f_uniq(typval_T *argvars, typval_T *rettv)
10920{
10921 do_sort_uniq(argvars, rettv, FALSE);
10922}
10923
10924/*
10925 * "soundfold({word})" function
10926 */
10927 static void
10928f_soundfold(typval_T *argvars, typval_T *rettv)
10929{
10930 char_u *s;
10931
10932 rettv->v_type = VAR_STRING;
10933 s = get_tv_string(&argvars[0]);
10934#ifdef FEAT_SPELL
10935 rettv->vval.v_string = eval_soundfold(s);
10936#else
10937 rettv->vval.v_string = vim_strsave(s);
10938#endif
10939}
10940
10941/*
10942 * "spellbadword()" function
10943 */
10944 static void
10945f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
10946{
10947 char_u *word = (char_u *)"";
10948 hlf_T attr = HLF_COUNT;
10949 int len = 0;
10950
10951 if (rettv_list_alloc(rettv) == FAIL)
10952 return;
10953
10954#ifdef FEAT_SPELL
10955 if (argvars[0].v_type == VAR_UNKNOWN)
10956 {
10957 /* Find the start and length of the badly spelled word. */
10958 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
10959 if (len != 0)
10960 word = ml_get_cursor();
10961 }
10962 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
10963 {
10964 char_u *str = get_tv_string_chk(&argvars[0]);
10965 int capcol = -1;
10966
10967 if (str != NULL)
10968 {
10969 /* Check the argument for spelling. */
10970 while (*str != NUL)
10971 {
10972 len = spell_check(curwin, str, &attr, &capcol, FALSE);
10973 if (attr != HLF_COUNT)
10974 {
10975 word = str;
10976 break;
10977 }
10978 str += len;
10979 }
10980 }
10981 }
10982#endif
10983
10984 list_append_string(rettv->vval.v_list, word, len);
10985 list_append_string(rettv->vval.v_list, (char_u *)(
10986 attr == HLF_SPB ? "bad" :
10987 attr == HLF_SPR ? "rare" :
10988 attr == HLF_SPL ? "local" :
10989 attr == HLF_SPC ? "caps" :
10990 ""), -1);
10991}
10992
10993/*
10994 * "spellsuggest()" function
10995 */
10996 static void
10997f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
10998{
10999#ifdef FEAT_SPELL
11000 char_u *str;
11001 int typeerr = FALSE;
11002 int maxcount;
11003 garray_T ga;
11004 int i;
11005 listitem_T *li;
11006 int need_capital = FALSE;
11007#endif
11008
11009 if (rettv_list_alloc(rettv) == FAIL)
11010 return;
11011
11012#ifdef FEAT_SPELL
11013 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
11014 {
11015 str = get_tv_string(&argvars[0]);
11016 if (argvars[1].v_type != VAR_UNKNOWN)
11017 {
11018 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
11019 if (maxcount <= 0)
11020 return;
11021 if (argvars[2].v_type != VAR_UNKNOWN)
11022 {
11023 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
11024 if (typeerr)
11025 return;
11026 }
11027 }
11028 else
11029 maxcount = 25;
11030
11031 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
11032
11033 for (i = 0; i < ga.ga_len; ++i)
11034 {
11035 str = ((char_u **)ga.ga_data)[i];
11036
11037 li = listitem_alloc();
11038 if (li == NULL)
11039 vim_free(str);
11040 else
11041 {
11042 li->li_tv.v_type = VAR_STRING;
11043 li->li_tv.v_lock = 0;
11044 li->li_tv.vval.v_string = str;
11045 list_append(rettv->vval.v_list, li);
11046 }
11047 }
11048 ga_clear(&ga);
11049 }
11050#endif
11051}
11052
11053 static void
11054f_split(typval_T *argvars, typval_T *rettv)
11055{
11056 char_u *str;
11057 char_u *end;
11058 char_u *pat = NULL;
11059 regmatch_T regmatch;
11060 char_u patbuf[NUMBUFLEN];
11061 char_u *save_cpo;
11062 int match;
11063 colnr_T col = 0;
11064 int keepempty = FALSE;
11065 int typeerr = FALSE;
11066
11067 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11068 save_cpo = p_cpo;
11069 p_cpo = (char_u *)"";
11070
11071 str = get_tv_string(&argvars[0]);
11072 if (argvars[1].v_type != VAR_UNKNOWN)
11073 {
11074 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11075 if (pat == NULL)
11076 typeerr = TRUE;
11077 if (argvars[2].v_type != VAR_UNKNOWN)
11078 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
11079 }
11080 if (pat == NULL || *pat == NUL)
11081 pat = (char_u *)"[\\x01- ]\\+";
11082
11083 if (rettv_list_alloc(rettv) == FAIL)
11084 return;
11085 if (typeerr)
11086 return;
11087
11088 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11089 if (regmatch.regprog != NULL)
11090 {
11091 regmatch.rm_ic = FALSE;
11092 while (*str != NUL || keepempty)
11093 {
11094 if (*str == NUL)
11095 match = FALSE; /* empty item at the end */
11096 else
11097 match = vim_regexec_nl(&regmatch, str, col);
11098 if (match)
11099 end = regmatch.startp[0];
11100 else
11101 end = str + STRLEN(str);
11102 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
11103 && *str != NUL && match && end < regmatch.endp[0]))
11104 {
11105 if (list_append_string(rettv->vval.v_list, str,
11106 (int)(end - str)) == FAIL)
11107 break;
11108 }
11109 if (!match)
11110 break;
11111 /* Advance to just after the match. */
11112 if (regmatch.endp[0] > str)
11113 col = 0;
11114 else
11115 {
11116 /* Don't get stuck at the same match. */
11117#ifdef FEAT_MBYTE
11118 col = (*mb_ptr2len)(regmatch.endp[0]);
11119#else
11120 col = 1;
11121#endif
11122 }
11123 str = regmatch.endp[0];
11124 }
11125
11126 vim_regfree(regmatch.regprog);
11127 }
11128
11129 p_cpo = save_cpo;
11130}
11131
11132#ifdef FEAT_FLOAT
11133/*
11134 * "sqrt()" function
11135 */
11136 static void
11137f_sqrt(typval_T *argvars, typval_T *rettv)
11138{
11139 float_T f = 0.0;
11140
11141 rettv->v_type = VAR_FLOAT;
11142 if (get_float_arg(argvars, &f) == OK)
11143 rettv->vval.v_float = sqrt(f);
11144 else
11145 rettv->vval.v_float = 0.0;
11146}
11147
11148/*
11149 * "str2float()" function
11150 */
11151 static void
11152f_str2float(typval_T *argvars, typval_T *rettv)
11153{
11154 char_u *p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011155 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011156
Bram Moolenaar08243d22017-01-10 16:12:29 +010011157 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011158 p = skipwhite(p + 1);
11159 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011160 if (isneg)
11161 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011162 rettv->v_type = VAR_FLOAT;
11163}
11164#endif
11165
11166/*
11167 * "str2nr()" function
11168 */
11169 static void
11170f_str2nr(typval_T *argvars, typval_T *rettv)
11171{
11172 int base = 10;
11173 char_u *p;
11174 varnumber_T n;
11175 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010011176 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011177
11178 if (argvars[1].v_type != VAR_UNKNOWN)
11179 {
11180 base = (int)get_tv_number(&argvars[1]);
11181 if (base != 2 && base != 8 && base != 10 && base != 16)
11182 {
11183 EMSG(_(e_invarg));
11184 return;
11185 }
11186 }
11187
11188 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011189 isneg = (*p == '-');
11190 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011191 p = skipwhite(p + 1);
11192 switch (base)
11193 {
11194 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
11195 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
11196 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
11197 default: what = 0;
11198 }
11199 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011200 if (isneg)
11201 rettv->vval.v_number = -n;
11202 else
11203 rettv->vval.v_number = n;
11204
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011205}
11206
11207#ifdef HAVE_STRFTIME
11208/*
11209 * "strftime({format}[, {time}])" function
11210 */
11211 static void
11212f_strftime(typval_T *argvars, typval_T *rettv)
11213{
11214 char_u result_buf[256];
11215 struct tm *curtime;
11216 time_t seconds;
11217 char_u *p;
11218
11219 rettv->v_type = VAR_STRING;
11220
11221 p = get_tv_string(&argvars[0]);
11222 if (argvars[1].v_type == VAR_UNKNOWN)
11223 seconds = time(NULL);
11224 else
11225 seconds = (time_t)get_tv_number(&argvars[1]);
11226 curtime = localtime(&seconds);
11227 /* MSVC returns NULL for an invalid value of seconds. */
11228 if (curtime == NULL)
11229 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
11230 else
11231 {
11232# ifdef FEAT_MBYTE
11233 vimconv_T conv;
11234 char_u *enc;
11235
11236 conv.vc_type = CONV_NONE;
11237 enc = enc_locale();
11238 convert_setup(&conv, p_enc, enc);
11239 if (conv.vc_type != CONV_NONE)
11240 p = string_convert(&conv, p, NULL);
11241# endif
11242 if (p != NULL)
11243 (void)strftime((char *)result_buf, sizeof(result_buf),
11244 (char *)p, curtime);
11245 else
11246 result_buf[0] = NUL;
11247
11248# ifdef FEAT_MBYTE
11249 if (conv.vc_type != CONV_NONE)
11250 vim_free(p);
11251 convert_setup(&conv, enc, p_enc);
11252 if (conv.vc_type != CONV_NONE)
11253 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
11254 else
11255# endif
11256 rettv->vval.v_string = vim_strsave(result_buf);
11257
11258# ifdef FEAT_MBYTE
11259 /* Release conversion descriptors */
11260 convert_setup(&conv, NULL, NULL);
11261 vim_free(enc);
11262# endif
11263 }
11264}
11265#endif
11266
11267/*
11268 * "strgetchar()" function
11269 */
11270 static void
11271f_strgetchar(typval_T *argvars, typval_T *rettv)
11272{
11273 char_u *str;
11274 int len;
11275 int error = FALSE;
11276 int charidx;
11277
11278 rettv->vval.v_number = -1;
11279 str = get_tv_string_chk(&argvars[0]);
11280 if (str == NULL)
11281 return;
11282 len = (int)STRLEN(str);
11283 charidx = (int)get_tv_number_chk(&argvars[1], &error);
11284 if (error)
11285 return;
11286#ifdef FEAT_MBYTE
11287 {
11288 int byteidx = 0;
11289
11290 while (charidx >= 0 && byteidx < len)
11291 {
11292 if (charidx == 0)
11293 {
11294 rettv->vval.v_number = mb_ptr2char(str + byteidx);
11295 break;
11296 }
11297 --charidx;
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011298 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011299 }
11300 }
11301#else
11302 if (charidx < len)
11303 rettv->vval.v_number = str[charidx];
11304#endif
11305}
11306
11307/*
11308 * "stridx()" function
11309 */
11310 static void
11311f_stridx(typval_T *argvars, typval_T *rettv)
11312{
11313 char_u buf[NUMBUFLEN];
11314 char_u *needle;
11315 char_u *haystack;
11316 char_u *save_haystack;
11317 char_u *pos;
11318 int start_idx;
11319
11320 needle = get_tv_string_chk(&argvars[1]);
11321 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
11322 rettv->vval.v_number = -1;
11323 if (needle == NULL || haystack == NULL)
11324 return; /* type error; errmsg already given */
11325
11326 if (argvars[2].v_type != VAR_UNKNOWN)
11327 {
11328 int error = FALSE;
11329
11330 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
11331 if (error || start_idx >= (int)STRLEN(haystack))
11332 return;
11333 if (start_idx >= 0)
11334 haystack += start_idx;
11335 }
11336
11337 pos = (char_u *)strstr((char *)haystack, (char *)needle);
11338 if (pos != NULL)
11339 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
11340}
11341
11342/*
11343 * "string()" function
11344 */
11345 static void
11346f_string(typval_T *argvars, typval_T *rettv)
11347{
11348 char_u *tofree;
11349 char_u numbuf[NUMBUFLEN];
11350
11351 rettv->v_type = VAR_STRING;
11352 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
11353 get_copyID());
11354 /* Make a copy if we have a value but it's not in allocated memory. */
11355 if (rettv->vval.v_string != NULL && tofree == NULL)
11356 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
11357}
11358
11359/*
11360 * "strlen()" function
11361 */
11362 static void
11363f_strlen(typval_T *argvars, typval_T *rettv)
11364{
11365 rettv->vval.v_number = (varnumber_T)(STRLEN(
11366 get_tv_string(&argvars[0])));
11367}
11368
11369/*
11370 * "strchars()" function
11371 */
11372 static void
11373f_strchars(typval_T *argvars, typval_T *rettv)
11374{
11375 char_u *s = get_tv_string(&argvars[0]);
11376 int skipcc = 0;
11377#ifdef FEAT_MBYTE
11378 varnumber_T len = 0;
11379 int (*func_mb_ptr2char_adv)(char_u **pp);
11380#endif
11381
11382 if (argvars[1].v_type != VAR_UNKNOWN)
11383 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
11384 if (skipcc < 0 || skipcc > 1)
11385 EMSG(_(e_invarg));
11386 else
11387 {
11388#ifdef FEAT_MBYTE
11389 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
11390 while (*s != NUL)
11391 {
11392 func_mb_ptr2char_adv(&s);
11393 ++len;
11394 }
11395 rettv->vval.v_number = len;
11396#else
11397 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
11398#endif
11399 }
11400}
11401
11402/*
11403 * "strdisplaywidth()" function
11404 */
11405 static void
11406f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
11407{
11408 char_u *s = get_tv_string(&argvars[0]);
11409 int col = 0;
11410
11411 if (argvars[1].v_type != VAR_UNKNOWN)
11412 col = (int)get_tv_number(&argvars[1]);
11413
11414 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
11415}
11416
11417/*
11418 * "strwidth()" function
11419 */
11420 static void
11421f_strwidth(typval_T *argvars, typval_T *rettv)
11422{
11423 char_u *s = get_tv_string(&argvars[0]);
11424
11425 rettv->vval.v_number = (varnumber_T)(
11426#ifdef FEAT_MBYTE
11427 mb_string2cells(s, -1)
11428#else
11429 STRLEN(s)
11430#endif
11431 );
11432}
11433
11434/*
11435 * "strcharpart()" function
11436 */
11437 static void
11438f_strcharpart(typval_T *argvars, typval_T *rettv)
11439{
11440#ifdef FEAT_MBYTE
11441 char_u *p;
11442 int nchar;
11443 int nbyte = 0;
11444 int charlen;
11445 int len = 0;
11446 int slen;
11447 int error = FALSE;
11448
11449 p = get_tv_string(&argvars[0]);
11450 slen = (int)STRLEN(p);
11451
11452 nchar = (int)get_tv_number_chk(&argvars[1], &error);
11453 if (!error)
11454 {
11455 if (nchar > 0)
11456 while (nchar > 0 && nbyte < slen)
11457 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011458 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011459 --nchar;
11460 }
11461 else
11462 nbyte = nchar;
11463 if (argvars[2].v_type != VAR_UNKNOWN)
11464 {
11465 charlen = (int)get_tv_number(&argvars[2]);
11466 while (charlen > 0 && nbyte + len < slen)
11467 {
11468 int off = nbyte + len;
11469
11470 if (off < 0)
11471 len += 1;
11472 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011473 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011474 --charlen;
11475 }
11476 }
11477 else
11478 len = slen - nbyte; /* default: all bytes that are available. */
11479 }
11480
11481 /*
11482 * Only return the overlap between the specified part and the actual
11483 * string.
11484 */
11485 if (nbyte < 0)
11486 {
11487 len += nbyte;
11488 nbyte = 0;
11489 }
11490 else if (nbyte > slen)
11491 nbyte = slen;
11492 if (len < 0)
11493 len = 0;
11494 else if (nbyte + len > slen)
11495 len = slen - nbyte;
11496
11497 rettv->v_type = VAR_STRING;
11498 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
11499#else
11500 f_strpart(argvars, rettv);
11501#endif
11502}
11503
11504/*
11505 * "strpart()" function
11506 */
11507 static void
11508f_strpart(typval_T *argvars, typval_T *rettv)
11509{
11510 char_u *p;
11511 int n;
11512 int len;
11513 int slen;
11514 int error = FALSE;
11515
11516 p = get_tv_string(&argvars[0]);
11517 slen = (int)STRLEN(p);
11518
11519 n = (int)get_tv_number_chk(&argvars[1], &error);
11520 if (error)
11521 len = 0;
11522 else if (argvars[2].v_type != VAR_UNKNOWN)
11523 len = (int)get_tv_number(&argvars[2]);
11524 else
11525 len = slen - n; /* default len: all bytes that are available. */
11526
11527 /*
11528 * Only return the overlap between the specified part and the actual
11529 * string.
11530 */
11531 if (n < 0)
11532 {
11533 len += n;
11534 n = 0;
11535 }
11536 else if (n > slen)
11537 n = slen;
11538 if (len < 0)
11539 len = 0;
11540 else if (n + len > slen)
11541 len = slen - n;
11542
11543 rettv->v_type = VAR_STRING;
11544 rettv->vval.v_string = vim_strnsave(p + n, len);
11545}
11546
11547/*
11548 * "strridx()" function
11549 */
11550 static void
11551f_strridx(typval_T *argvars, typval_T *rettv)
11552{
11553 char_u buf[NUMBUFLEN];
11554 char_u *needle;
11555 char_u *haystack;
11556 char_u *rest;
11557 char_u *lastmatch = NULL;
11558 int haystack_len, end_idx;
11559
11560 needle = get_tv_string_chk(&argvars[1]);
11561 haystack = get_tv_string_buf_chk(&argvars[0], buf);
11562
11563 rettv->vval.v_number = -1;
11564 if (needle == NULL || haystack == NULL)
11565 return; /* type error; errmsg already given */
11566
11567 haystack_len = (int)STRLEN(haystack);
11568 if (argvars[2].v_type != VAR_UNKNOWN)
11569 {
11570 /* Third argument: upper limit for index */
11571 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
11572 if (end_idx < 0)
11573 return; /* can never find a match */
11574 }
11575 else
11576 end_idx = haystack_len;
11577
11578 if (*needle == NUL)
11579 {
11580 /* Empty string matches past the end. */
11581 lastmatch = haystack + end_idx;
11582 }
11583 else
11584 {
11585 for (rest = haystack; *rest != '\0'; ++rest)
11586 {
11587 rest = (char_u *)strstr((char *)rest, (char *)needle);
11588 if (rest == NULL || rest > haystack + end_idx)
11589 break;
11590 lastmatch = rest;
11591 }
11592 }
11593
11594 if (lastmatch == NULL)
11595 rettv->vval.v_number = -1;
11596 else
11597 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
11598}
11599
11600/*
11601 * "strtrans()" function
11602 */
11603 static void
11604f_strtrans(typval_T *argvars, typval_T *rettv)
11605{
11606 rettv->v_type = VAR_STRING;
11607 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
11608}
11609
11610/*
11611 * "submatch()" function
11612 */
11613 static void
11614f_submatch(typval_T *argvars, typval_T *rettv)
11615{
11616 int error = FALSE;
11617 int no;
11618 int retList = 0;
11619
11620 no = (int)get_tv_number_chk(&argvars[0], &error);
11621 if (error)
11622 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020011623 if (no < 0 || no >= NSUBEXP)
11624 {
Bram Moolenaar79518e22017-02-17 16:31:35 +010011625 EMSGN(_("E935: invalid submatch number: %d"), no);
11626 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020011627 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011628 if (argvars[1].v_type != VAR_UNKNOWN)
11629 retList = (int)get_tv_number_chk(&argvars[1], &error);
11630 if (error)
11631 return;
11632
11633 if (retList == 0)
11634 {
11635 rettv->v_type = VAR_STRING;
11636 rettv->vval.v_string = reg_submatch(no);
11637 }
11638 else
11639 {
11640 rettv->v_type = VAR_LIST;
11641 rettv->vval.v_list = reg_submatch_list(no);
11642 }
11643}
11644
11645/*
11646 * "substitute()" function
11647 */
11648 static void
11649f_substitute(typval_T *argvars, typval_T *rettv)
11650{
11651 char_u patbuf[NUMBUFLEN];
11652 char_u subbuf[NUMBUFLEN];
11653 char_u flagsbuf[NUMBUFLEN];
11654
11655 char_u *str = get_tv_string_chk(&argvars[0]);
11656 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011657 char_u *sub = NULL;
11658 typval_T *expr = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011659 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
11660
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011661 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
11662 expr = &argvars[2];
11663 else
11664 sub = get_tv_string_buf_chk(&argvars[2], subbuf);
11665
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011666 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011667 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
11668 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011669 rettv->vval.v_string = NULL;
11670 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011671 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011672}
11673
11674/*
11675 * "synID(lnum, col, trans)" function
11676 */
11677 static void
11678f_synID(typval_T *argvars UNUSED, typval_T *rettv)
11679{
11680 int id = 0;
11681#ifdef FEAT_SYN_HL
11682 linenr_T lnum;
11683 colnr_T col;
11684 int trans;
11685 int transerr = FALSE;
11686
11687 lnum = get_tv_lnum(argvars); /* -1 on type error */
11688 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
11689 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
11690
11691 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11692 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
11693 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
11694#endif
11695
11696 rettv->vval.v_number = id;
11697}
11698
11699/*
11700 * "synIDattr(id, what [, mode])" function
11701 */
11702 static void
11703f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
11704{
11705 char_u *p = NULL;
11706#ifdef FEAT_SYN_HL
11707 int id;
11708 char_u *what;
11709 char_u *mode;
11710 char_u modebuf[NUMBUFLEN];
11711 int modec;
11712
11713 id = (int)get_tv_number(&argvars[0]);
11714 what = get_tv_string(&argvars[1]);
11715 if (argvars[2].v_type != VAR_UNKNOWN)
11716 {
11717 mode = get_tv_string_buf(&argvars[2], modebuf);
11718 modec = TOLOWER_ASC(mode[0]);
11719 if (modec != 't' && modec != 'c' && modec != 'g')
11720 modec = 0; /* replace invalid with current */
11721 }
11722 else
11723 {
11724#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
11725 if (USE_24BIT)
11726 modec = 'g';
11727 else
11728#endif
11729 if (t_colors > 1)
11730 modec = 'c';
11731 else
11732 modec = 't';
11733 }
11734
11735
11736 switch (TOLOWER_ASC(what[0]))
11737 {
11738 case 'b':
11739 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
11740 p = highlight_color(id, what, modec);
11741 else /* bold */
11742 p = highlight_has_attr(id, HL_BOLD, modec);
11743 break;
11744
11745 case 'f': /* fg[#] or font */
11746 p = highlight_color(id, what, modec);
11747 break;
11748
11749 case 'i':
11750 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
11751 p = highlight_has_attr(id, HL_INVERSE, modec);
11752 else /* italic */
11753 p = highlight_has_attr(id, HL_ITALIC, modec);
11754 break;
11755
11756 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020011757 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011758 break;
11759
11760 case 'r': /* reverse */
11761 p = highlight_has_attr(id, HL_INVERSE, modec);
11762 break;
11763
11764 case 's':
11765 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
11766 p = highlight_color(id, what, modec);
11767 else /* standout */
11768 p = highlight_has_attr(id, HL_STANDOUT, modec);
11769 break;
11770
11771 case 'u':
11772 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
11773 /* underline */
11774 p = highlight_has_attr(id, HL_UNDERLINE, modec);
11775 else
11776 /* undercurl */
11777 p = highlight_has_attr(id, HL_UNDERCURL, modec);
11778 break;
11779 }
11780
11781 if (p != NULL)
11782 p = vim_strsave(p);
11783#endif
11784 rettv->v_type = VAR_STRING;
11785 rettv->vval.v_string = p;
11786}
11787
11788/*
11789 * "synIDtrans(id)" function
11790 */
11791 static void
11792f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
11793{
11794 int id;
11795
11796#ifdef FEAT_SYN_HL
11797 id = (int)get_tv_number(&argvars[0]);
11798
11799 if (id > 0)
11800 id = syn_get_final_id(id);
11801 else
11802#endif
11803 id = 0;
11804
11805 rettv->vval.v_number = id;
11806}
11807
11808/*
11809 * "synconcealed(lnum, col)" function
11810 */
11811 static void
11812f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
11813{
11814#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
11815 linenr_T lnum;
11816 colnr_T col;
11817 int syntax_flags = 0;
11818 int cchar;
11819 int matchid = 0;
11820 char_u str[NUMBUFLEN];
11821#endif
11822
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011823 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011824
11825#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
11826 lnum = get_tv_lnum(argvars); /* -1 on type error */
11827 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
11828
11829 vim_memset(str, NUL, sizeof(str));
11830
11831 if (rettv_list_alloc(rettv) != FAIL)
11832 {
11833 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11834 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
11835 && curwin->w_p_cole > 0)
11836 {
11837 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
11838 syntax_flags = get_syntax_info(&matchid);
11839
11840 /* get the conceal character */
11841 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
11842 {
11843 cchar = syn_get_sub_char();
11844 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
11845 cchar = lcs_conceal;
11846 if (cchar != NUL)
11847 {
11848# ifdef FEAT_MBYTE
11849 if (has_mbyte)
11850 (*mb_char2bytes)(cchar, str);
11851 else
11852# endif
11853 str[0] = cchar;
11854 }
11855 }
11856 }
11857
11858 list_append_number(rettv->vval.v_list,
11859 (syntax_flags & HL_CONCEAL) != 0);
11860 /* -1 to auto-determine strlen */
11861 list_append_string(rettv->vval.v_list, str, -1);
11862 list_append_number(rettv->vval.v_list, matchid);
11863 }
11864#endif
11865}
11866
11867/*
11868 * "synstack(lnum, col)" function
11869 */
11870 static void
11871f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
11872{
11873#ifdef FEAT_SYN_HL
11874 linenr_T lnum;
11875 colnr_T col;
11876 int i;
11877 int id;
11878#endif
11879
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011880 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011881
11882#ifdef FEAT_SYN_HL
11883 lnum = get_tv_lnum(argvars); /* -1 on type error */
11884 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
11885
11886 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11887 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
11888 && rettv_list_alloc(rettv) != FAIL)
11889 {
11890 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
11891 for (i = 0; ; ++i)
11892 {
11893 id = syn_get_stack_item(i);
11894 if (id < 0)
11895 break;
11896 if (list_append_number(rettv->vval.v_list, id) == FAIL)
11897 break;
11898 }
11899 }
11900#endif
11901}
11902
11903 static void
11904get_cmd_output_as_rettv(
11905 typval_T *argvars,
11906 typval_T *rettv,
11907 int retlist)
11908{
11909 char_u *res = NULL;
11910 char_u *p;
11911 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011912 int err = FALSE;
11913 FILE *fd;
11914 list_T *list = NULL;
11915 int flags = SHELL_SILENT;
11916
11917 rettv->v_type = VAR_STRING;
11918 rettv->vval.v_string = NULL;
11919 if (check_restricted() || check_secure())
11920 goto errret;
11921
11922 if (argvars[1].v_type != VAR_UNKNOWN)
11923 {
11924 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010011925 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011926 * command.
11927 */
11928 if ((infile = vim_tempname('i', TRUE)) == NULL)
11929 {
11930 EMSG(_(e_notmp));
11931 goto errret;
11932 }
11933
11934 fd = mch_fopen((char *)infile, WRITEBIN);
11935 if (fd == NULL)
11936 {
11937 EMSG2(_(e_notopen), infile);
11938 goto errret;
11939 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010011940 if (argvars[1].v_type == VAR_NUMBER)
11941 {
11942 linenr_T lnum;
11943 buf_T *buf;
11944
11945 buf = buflist_findnr(argvars[1].vval.v_number);
11946 if (buf == NULL)
11947 {
11948 EMSGN(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010011949 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010011950 goto errret;
11951 }
11952
11953 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
11954 {
11955 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
11956 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
11957 {
11958 err = TRUE;
11959 break;
11960 }
11961 if (putc(NL, fd) == EOF)
11962 {
11963 err = TRUE;
11964 break;
11965 }
11966 }
11967 }
11968 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011969 {
11970 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
11971 err = TRUE;
11972 }
11973 else
11974 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010011975 size_t len;
11976 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011977
11978 p = get_tv_string_buf_chk(&argvars[1], buf);
11979 if (p == NULL)
11980 {
11981 fclose(fd);
11982 goto errret; /* type error; errmsg already given */
11983 }
11984 len = STRLEN(p);
11985 if (len > 0 && fwrite(p, len, 1, fd) != 1)
11986 err = TRUE;
11987 }
11988 if (fclose(fd) != 0)
11989 err = TRUE;
11990 if (err)
11991 {
11992 EMSG(_("E677: Error writing temp file"));
11993 goto errret;
11994 }
11995 }
11996
11997 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
11998 * echoes typeahead, that messes up the display. */
11999 if (!msg_silent)
12000 flags += SHELL_COOKED;
12001
12002 if (retlist)
12003 {
12004 int len;
12005 listitem_T *li;
12006 char_u *s = NULL;
12007 char_u *start;
12008 char_u *end;
12009 int i;
12010
12011 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
12012 if (res == NULL)
12013 goto errret;
12014
12015 list = list_alloc();
12016 if (list == NULL)
12017 goto errret;
12018
12019 for (i = 0; i < len; ++i)
12020 {
12021 start = res + i;
12022 while (i < len && res[i] != NL)
12023 ++i;
12024 end = res + i;
12025
12026 s = alloc((unsigned)(end - start + 1));
12027 if (s == NULL)
12028 goto errret;
12029
12030 for (p = s; start < end; ++p, ++start)
12031 *p = *start == NUL ? NL : *start;
12032 *p = NUL;
12033
12034 li = listitem_alloc();
12035 if (li == NULL)
12036 {
12037 vim_free(s);
12038 goto errret;
12039 }
12040 li->li_tv.v_type = VAR_STRING;
12041 li->li_tv.v_lock = 0;
12042 li->li_tv.vval.v_string = s;
12043 list_append(list, li);
12044 }
12045
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012046 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012047 list = NULL;
12048 }
12049 else
12050 {
12051 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
12052#ifdef USE_CR
12053 /* translate <CR> into <NL> */
12054 if (res != NULL)
12055 {
12056 char_u *s;
12057
12058 for (s = res; *s; ++s)
12059 {
12060 if (*s == CAR)
12061 *s = NL;
12062 }
12063 }
12064#else
12065# ifdef USE_CRNL
12066 /* translate <CR><NL> into <NL> */
12067 if (res != NULL)
12068 {
12069 char_u *s, *d;
12070
12071 d = res;
12072 for (s = res; *s; ++s)
12073 {
12074 if (s[0] == CAR && s[1] == NL)
12075 ++s;
12076 *d++ = *s;
12077 }
12078 *d = NUL;
12079 }
12080# endif
12081#endif
12082 rettv->vval.v_string = res;
12083 res = NULL;
12084 }
12085
12086errret:
12087 if (infile != NULL)
12088 {
12089 mch_remove(infile);
12090 vim_free(infile);
12091 }
12092 if (res != NULL)
12093 vim_free(res);
12094 if (list != NULL)
12095 list_free(list);
12096}
12097
12098/*
12099 * "system()" function
12100 */
12101 static void
12102f_system(typval_T *argvars, typval_T *rettv)
12103{
12104 get_cmd_output_as_rettv(argvars, rettv, FALSE);
12105}
12106
12107/*
12108 * "systemlist()" function
12109 */
12110 static void
12111f_systemlist(typval_T *argvars, typval_T *rettv)
12112{
12113 get_cmd_output_as_rettv(argvars, rettv, TRUE);
12114}
12115
12116/*
12117 * "tabpagebuflist()" function
12118 */
12119 static void
12120f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12121{
12122#ifdef FEAT_WINDOWS
12123 tabpage_T *tp;
12124 win_T *wp = NULL;
12125
12126 if (argvars[0].v_type == VAR_UNKNOWN)
12127 wp = firstwin;
12128 else
12129 {
12130 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12131 if (tp != NULL)
12132 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12133 }
12134 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
12135 {
12136 for (; wp != NULL; wp = wp->w_next)
12137 if (list_append_number(rettv->vval.v_list,
12138 wp->w_buffer->b_fnum) == FAIL)
12139 break;
12140 }
12141#endif
12142}
12143
12144
12145/*
12146 * "tabpagenr()" function
12147 */
12148 static void
12149f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
12150{
12151 int nr = 1;
12152#ifdef FEAT_WINDOWS
12153 char_u *arg;
12154
12155 if (argvars[0].v_type != VAR_UNKNOWN)
12156 {
12157 arg = get_tv_string_chk(&argvars[0]);
12158 nr = 0;
12159 if (arg != NULL)
12160 {
12161 if (STRCMP(arg, "$") == 0)
12162 nr = tabpage_index(NULL) - 1;
12163 else
12164 EMSG2(_(e_invexpr2), arg);
12165 }
12166 }
12167 else
12168 nr = tabpage_index(curtab);
12169#endif
12170 rettv->vval.v_number = nr;
12171}
12172
12173
12174#ifdef FEAT_WINDOWS
12175static int get_winnr(tabpage_T *tp, typval_T *argvar);
12176
12177/*
12178 * Common code for tabpagewinnr() and winnr().
12179 */
12180 static int
12181get_winnr(tabpage_T *tp, typval_T *argvar)
12182{
12183 win_T *twin;
12184 int nr = 1;
12185 win_T *wp;
12186 char_u *arg;
12187
12188 twin = (tp == curtab) ? curwin : tp->tp_curwin;
12189 if (argvar->v_type != VAR_UNKNOWN)
12190 {
12191 arg = get_tv_string_chk(argvar);
12192 if (arg == NULL)
12193 nr = 0; /* type error; errmsg already given */
12194 else if (STRCMP(arg, "$") == 0)
12195 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
12196 else if (STRCMP(arg, "#") == 0)
12197 {
12198 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
12199 if (twin == NULL)
12200 nr = 0;
12201 }
12202 else
12203 {
12204 EMSG2(_(e_invexpr2), arg);
12205 nr = 0;
12206 }
12207 }
12208
12209 if (nr > 0)
12210 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12211 wp != twin; wp = wp->w_next)
12212 {
12213 if (wp == NULL)
12214 {
12215 /* didn't find it in this tabpage */
12216 nr = 0;
12217 break;
12218 }
12219 ++nr;
12220 }
12221 return nr;
12222}
12223#endif
12224
12225/*
12226 * "tabpagewinnr()" function
12227 */
12228 static void
12229f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
12230{
12231 int nr = 1;
12232#ifdef FEAT_WINDOWS
12233 tabpage_T *tp;
12234
12235 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12236 if (tp == NULL)
12237 nr = 0;
12238 else
12239 nr = get_winnr(tp, &argvars[1]);
12240#endif
12241 rettv->vval.v_number = nr;
12242}
12243
12244
12245/*
12246 * "tagfiles()" function
12247 */
12248 static void
12249f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
12250{
12251 char_u *fname;
12252 tagname_T tn;
12253 int first;
12254
12255 if (rettv_list_alloc(rettv) == FAIL)
12256 return;
12257 fname = alloc(MAXPATHL);
12258 if (fname == NULL)
12259 return;
12260
12261 for (first = TRUE; ; first = FALSE)
12262 if (get_tagfname(&tn, first, fname) == FAIL
12263 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
12264 break;
12265 tagname_free(&tn);
12266 vim_free(fname);
12267}
12268
12269/*
12270 * "taglist()" function
12271 */
12272 static void
12273f_taglist(typval_T *argvars, typval_T *rettv)
12274{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012275 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012276 char_u *tag_pattern;
12277
12278 tag_pattern = get_tv_string(&argvars[0]);
12279
12280 rettv->vval.v_number = FALSE;
12281 if (*tag_pattern == NUL)
12282 return;
12283
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012284 if (argvars[1].v_type != VAR_UNKNOWN)
12285 fname = get_tv_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012286 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012287 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012288}
12289
12290/*
12291 * "tempname()" function
12292 */
12293 static void
12294f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
12295{
12296 static int x = 'A';
12297
12298 rettv->v_type = VAR_STRING;
12299 rettv->vval.v_string = vim_tempname(x, FALSE);
12300
12301 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
12302 * names. Skip 'I' and 'O', they are used for shell redirection. */
12303 do
12304 {
12305 if (x == 'Z')
12306 x = '0';
12307 else if (x == '9')
12308 x = 'A';
12309 else
12310 {
12311#ifdef EBCDIC
12312 if (x == 'I')
12313 x = 'J';
12314 else if (x == 'R')
12315 x = 'S';
12316 else
12317#endif
12318 ++x;
12319 }
12320 } while (x == 'I' || x == 'O');
12321}
12322
12323#ifdef FEAT_FLOAT
12324/*
12325 * "tan()" function
12326 */
12327 static void
12328f_tan(typval_T *argvars, typval_T *rettv)
12329{
12330 float_T f = 0.0;
12331
12332 rettv->v_type = VAR_FLOAT;
12333 if (get_float_arg(argvars, &f) == OK)
12334 rettv->vval.v_float = tan(f);
12335 else
12336 rettv->vval.v_float = 0.0;
12337}
12338
12339/*
12340 * "tanh()" function
12341 */
12342 static void
12343f_tanh(typval_T *argvars, typval_T *rettv)
12344{
12345 float_T f = 0.0;
12346
12347 rettv->v_type = VAR_FLOAT;
12348 if (get_float_arg(argvars, &f) == OK)
12349 rettv->vval.v_float = tanh(f);
12350 else
12351 rettv->vval.v_float = 0.0;
12352}
12353#endif
12354
12355/*
12356 * "test_alloc_fail(id, countdown, repeat)" function
12357 */
12358 static void
12359f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
12360{
12361 if (argvars[0].v_type != VAR_NUMBER
12362 || argvars[0].vval.v_number <= 0
12363 || argvars[1].v_type != VAR_NUMBER
12364 || argvars[1].vval.v_number < 0
12365 || argvars[2].v_type != VAR_NUMBER)
12366 EMSG(_(e_invarg));
12367 else
12368 {
12369 alloc_fail_id = argvars[0].vval.v_number;
12370 if (alloc_fail_id >= aid_last)
12371 EMSG(_(e_invarg));
12372 alloc_fail_countdown = argvars[1].vval.v_number;
12373 alloc_fail_repeat = argvars[2].vval.v_number;
12374 did_outofmem_msg = FALSE;
12375 }
12376}
12377
12378/*
12379 * "test_autochdir()"
12380 */
12381 static void
12382f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12383{
12384#if defined(FEAT_AUTOCHDIR)
12385 test_autochdir = TRUE;
12386#endif
12387}
12388
12389/*
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012390 * "test_disable({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012391 */
12392 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012393f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012394{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012395 char_u *name = (char_u *)"";
12396 int val;
12397
12398 if (argvars[0].v_type != VAR_STRING
12399 || (argvars[1].v_type) != VAR_NUMBER)
12400 EMSG(_(e_invarg));
12401 else
12402 {
12403 name = get_tv_string_chk(&argvars[0]);
12404 val = (int)get_tv_number(&argvars[1]);
12405
12406 if (STRCMP(name, (char_u *)"redraw") == 0)
12407 disable_redraw_for_testing = val;
12408 else if (STRCMP(name, (char_u *)"char_avail") == 0)
12409 disable_char_avail_for_testing = val;
12410 else if (STRCMP(name, (char_u *)"ALL") == 0)
12411 {
12412 disable_char_avail_for_testing = FALSE;
12413 disable_redraw_for_testing = FALSE;
12414 }
12415 else
12416 EMSG2(_(e_invarg2), name);
12417 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012418}
12419
12420/*
12421 * "test_garbagecollect_now()" function
12422 */
12423 static void
12424f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12425{
12426 /* This is dangerous, any Lists and Dicts used internally may be freed
12427 * while still in use. */
12428 garbage_collect(TRUE);
12429}
12430
Bram Moolenaare0c31f62017-03-01 15:07:05 +010012431/*
12432 * "test_ignore_error()" function
12433 */
12434 static void
12435f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
12436{
12437 ignore_error_for_testing(get_tv_string(&argvars[0]));
12438}
12439
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012440#ifdef FEAT_JOB_CHANNEL
12441 static void
12442f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
12443{
12444 rettv->v_type = VAR_CHANNEL;
12445 rettv->vval.v_channel = NULL;
12446}
12447#endif
12448
12449 static void
12450f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
12451{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012452 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012453}
12454
12455#ifdef FEAT_JOB_CHANNEL
12456 static void
12457f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
12458{
12459 rettv->v_type = VAR_JOB;
12460 rettv->vval.v_job = NULL;
12461}
12462#endif
12463
12464 static void
12465f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
12466{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012467 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012468}
12469
12470 static void
12471f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
12472{
12473 rettv->v_type = VAR_PARTIAL;
12474 rettv->vval.v_partial = NULL;
12475}
12476
12477 static void
12478f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
12479{
12480 rettv->v_type = VAR_STRING;
12481 rettv->vval.v_string = NULL;
12482}
12483
12484 static void
12485f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
12486{
12487 time_for_testing = (time_t)get_tv_number(&argvars[0]);
12488}
12489
12490#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
12491/*
12492 * Get a callback from "arg". It can be a Funcref or a function name.
12493 * When "arg" is zero return an empty string.
12494 * Return NULL for an invalid argument.
12495 */
12496 char_u *
12497get_callback(typval_T *arg, partial_T **pp)
12498{
12499 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
12500 {
12501 *pp = arg->vval.v_partial;
12502 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012503 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012504 }
12505 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012506 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012507 {
12508 func_ref(arg->vval.v_string);
12509 return arg->vval.v_string;
12510 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012511 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
12512 return (char_u *)"";
12513 EMSG(_("E921: Invalid callback argument"));
12514 return NULL;
12515}
12516
12517/*
12518 * Unref/free "callback" and "partial" retured by get_callback().
12519 */
12520 void
12521free_callback(char_u *callback, partial_T *partial)
12522{
12523 if (partial != NULL)
12524 partial_unref(partial);
12525 else if (callback != NULL)
12526 {
12527 func_unref(callback);
12528 vim_free(callback);
12529 }
12530}
12531#endif
12532
12533#ifdef FEAT_TIMERS
12534/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020012535 * "timer_info([timer])" function
12536 */
12537 static void
12538f_timer_info(typval_T *argvars, typval_T *rettv)
12539{
12540 timer_T *timer = NULL;
12541
12542 if (rettv_list_alloc(rettv) != OK)
12543 return;
12544 if (argvars[0].v_type != VAR_UNKNOWN)
12545 {
12546 if (argvars[0].v_type != VAR_NUMBER)
12547 EMSG(_(e_number_exp));
12548 else
12549 {
12550 timer = find_timer((int)get_tv_number(&argvars[0]));
12551 if (timer != NULL)
12552 add_timer_info(rettv, timer);
12553 }
12554 }
12555 else
12556 add_timer_info_all(rettv);
12557}
12558
12559/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012560 * "timer_pause(timer, paused)" function
12561 */
12562 static void
12563f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
12564{
12565 timer_T *timer = NULL;
12566 int paused = (int)get_tv_number(&argvars[1]);
12567
12568 if (argvars[0].v_type != VAR_NUMBER)
12569 EMSG(_(e_number_exp));
12570 else
12571 {
12572 timer = find_timer((int)get_tv_number(&argvars[0]));
12573 if (timer != NULL)
12574 timer->tr_paused = paused;
12575 }
12576}
12577
12578/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012579 * "timer_start(time, callback [, options])" function
12580 */
12581 static void
12582f_timer_start(typval_T *argvars, typval_T *rettv)
12583{
Bram Moolenaar75537a92016-09-05 22:45:28 +020012584 long msec = (long)get_tv_number(&argvars[0]);
12585 timer_T *timer;
12586 int repeat = 0;
12587 char_u *callback;
12588 dict_T *dict;
12589 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012590
Bram Moolenaar75537a92016-09-05 22:45:28 +020012591 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012592 if (check_secure())
12593 return;
12594 if (argvars[2].v_type != VAR_UNKNOWN)
12595 {
12596 if (argvars[2].v_type != VAR_DICT
12597 || (dict = argvars[2].vval.v_dict) == NULL)
12598 {
12599 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
12600 return;
12601 }
12602 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
12603 repeat = get_dict_number(dict, (char_u *)"repeat");
12604 }
12605
Bram Moolenaar75537a92016-09-05 22:45:28 +020012606 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012607 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020012608 return;
12609
12610 timer = create_timer(msec, repeat);
12611 if (timer == NULL)
12612 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012613 else
12614 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020012615 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020012616 timer->tr_callback = vim_strsave(callback);
12617 else
12618 /* pointer into the partial */
12619 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020012620 timer->tr_partial = partial;
12621 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012622 }
12623}
12624
12625/*
12626 * "timer_stop(timer)" function
12627 */
12628 static void
12629f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
12630{
12631 timer_T *timer;
12632
12633 if (argvars[0].v_type != VAR_NUMBER)
12634 {
12635 EMSG(_(e_number_exp));
12636 return;
12637 }
12638 timer = find_timer((int)get_tv_number(&argvars[0]));
12639 if (timer != NULL)
12640 stop_timer(timer);
12641}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012642
12643/*
12644 * "timer_stopall()" function
12645 */
12646 static void
12647f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12648{
12649 stop_all_timers();
12650}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012651#endif
12652
12653/*
12654 * "tolower(string)" function
12655 */
12656 static void
12657f_tolower(typval_T *argvars, typval_T *rettv)
12658{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012659 rettv->v_type = VAR_STRING;
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +010012660 rettv->vval.v_string = strlow_save(get_tv_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012661}
12662
12663/*
12664 * "toupper(string)" function
12665 */
12666 static void
12667f_toupper(typval_T *argvars, typval_T *rettv)
12668{
12669 rettv->v_type = VAR_STRING;
12670 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
12671}
12672
12673/*
12674 * "tr(string, fromstr, tostr)" function
12675 */
12676 static void
12677f_tr(typval_T *argvars, typval_T *rettv)
12678{
12679 char_u *in_str;
12680 char_u *fromstr;
12681 char_u *tostr;
12682 char_u *p;
12683#ifdef FEAT_MBYTE
12684 int inlen;
12685 int fromlen;
12686 int tolen;
12687 int idx;
12688 char_u *cpstr;
12689 int cplen;
12690 int first = TRUE;
12691#endif
12692 char_u buf[NUMBUFLEN];
12693 char_u buf2[NUMBUFLEN];
12694 garray_T ga;
12695
12696 in_str = get_tv_string(&argvars[0]);
12697 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
12698 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
12699
12700 /* Default return value: empty string. */
12701 rettv->v_type = VAR_STRING;
12702 rettv->vval.v_string = NULL;
12703 if (fromstr == NULL || tostr == NULL)
12704 return; /* type error; errmsg already given */
12705 ga_init2(&ga, (int)sizeof(char), 80);
12706
12707#ifdef FEAT_MBYTE
12708 if (!has_mbyte)
12709#endif
12710 /* not multi-byte: fromstr and tostr must be the same length */
12711 if (STRLEN(fromstr) != STRLEN(tostr))
12712 {
12713#ifdef FEAT_MBYTE
12714error:
12715#endif
12716 EMSG2(_(e_invarg2), fromstr);
12717 ga_clear(&ga);
12718 return;
12719 }
12720
12721 /* fromstr and tostr have to contain the same number of chars */
12722 while (*in_str != NUL)
12723 {
12724#ifdef FEAT_MBYTE
12725 if (has_mbyte)
12726 {
12727 inlen = (*mb_ptr2len)(in_str);
12728 cpstr = in_str;
12729 cplen = inlen;
12730 idx = 0;
12731 for (p = fromstr; *p != NUL; p += fromlen)
12732 {
12733 fromlen = (*mb_ptr2len)(p);
12734 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
12735 {
12736 for (p = tostr; *p != NUL; p += tolen)
12737 {
12738 tolen = (*mb_ptr2len)(p);
12739 if (idx-- == 0)
12740 {
12741 cplen = tolen;
12742 cpstr = p;
12743 break;
12744 }
12745 }
12746 if (*p == NUL) /* tostr is shorter than fromstr */
12747 goto error;
12748 break;
12749 }
12750 ++idx;
12751 }
12752
12753 if (first && cpstr == in_str)
12754 {
12755 /* Check that fromstr and tostr have the same number of
12756 * (multi-byte) characters. Done only once when a character
12757 * of in_str doesn't appear in fromstr. */
12758 first = FALSE;
12759 for (p = tostr; *p != NUL; p += tolen)
12760 {
12761 tolen = (*mb_ptr2len)(p);
12762 --idx;
12763 }
12764 if (idx != 0)
12765 goto error;
12766 }
12767
12768 (void)ga_grow(&ga, cplen);
12769 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
12770 ga.ga_len += cplen;
12771
12772 in_str += inlen;
12773 }
12774 else
12775#endif
12776 {
12777 /* When not using multi-byte chars we can do it faster. */
12778 p = vim_strchr(fromstr, *in_str);
12779 if (p != NULL)
12780 ga_append(&ga, tostr[p - fromstr]);
12781 else
12782 ga_append(&ga, *in_str);
12783 ++in_str;
12784 }
12785 }
12786
12787 /* add a terminating NUL */
12788 (void)ga_grow(&ga, 1);
12789 ga_append(&ga, NUL);
12790
12791 rettv->vval.v_string = ga.ga_data;
12792}
12793
12794#ifdef FEAT_FLOAT
12795/*
12796 * "trunc({float})" function
12797 */
12798 static void
12799f_trunc(typval_T *argvars, typval_T *rettv)
12800{
12801 float_T f = 0.0;
12802
12803 rettv->v_type = VAR_FLOAT;
12804 if (get_float_arg(argvars, &f) == OK)
12805 /* trunc() is not in C90, use floor() or ceil() instead. */
12806 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
12807 else
12808 rettv->vval.v_float = 0.0;
12809}
12810#endif
12811
12812/*
12813 * "type(expr)" function
12814 */
12815 static void
12816f_type(typval_T *argvars, typval_T *rettv)
12817{
12818 int n = -1;
12819
12820 switch (argvars[0].v_type)
12821 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020012822 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
12823 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012824 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020012825 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
12826 case VAR_LIST: n = VAR_TYPE_LIST; break;
12827 case VAR_DICT: n = VAR_TYPE_DICT; break;
12828 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012829 case VAR_SPECIAL:
12830 if (argvars[0].vval.v_number == VVAL_FALSE
12831 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020012832 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012833 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020012834 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012835 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020012836 case VAR_JOB: n = VAR_TYPE_JOB; break;
12837 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012838 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010012839 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012840 n = -1;
12841 break;
12842 }
12843 rettv->vval.v_number = n;
12844}
12845
12846/*
12847 * "undofile(name)" function
12848 */
12849 static void
12850f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
12851{
12852 rettv->v_type = VAR_STRING;
12853#ifdef FEAT_PERSISTENT_UNDO
12854 {
12855 char_u *fname = get_tv_string(&argvars[0]);
12856
12857 if (*fname == NUL)
12858 {
12859 /* If there is no file name there will be no undo file. */
12860 rettv->vval.v_string = NULL;
12861 }
12862 else
12863 {
12864 char_u *ffname = FullName_save(fname, FALSE);
12865
12866 if (ffname != NULL)
12867 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
12868 vim_free(ffname);
12869 }
12870 }
12871#else
12872 rettv->vval.v_string = NULL;
12873#endif
12874}
12875
12876/*
12877 * "undotree()" function
12878 */
12879 static void
12880f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
12881{
12882 if (rettv_dict_alloc(rettv) == OK)
12883 {
12884 dict_T *dict = rettv->vval.v_dict;
12885 list_T *list;
12886
12887 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
12888 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
12889 dict_add_nr_str(dict, "save_last",
12890 (long)curbuf->b_u_save_nr_last, NULL);
12891 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
12892 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
12893 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
12894
12895 list = list_alloc();
12896 if (list != NULL)
12897 {
12898 u_eval_tree(curbuf->b_u_oldhead, list);
12899 dict_add_list(dict, "entries", list);
12900 }
12901 }
12902}
12903
12904/*
12905 * "values(dict)" function
12906 */
12907 static void
12908f_values(typval_T *argvars, typval_T *rettv)
12909{
12910 dict_list(argvars, rettv, 1);
12911}
12912
12913/*
12914 * "virtcol(string)" function
12915 */
12916 static void
12917f_virtcol(typval_T *argvars, typval_T *rettv)
12918{
12919 colnr_T vcol = 0;
12920 pos_T *fp;
12921 int fnum = curbuf->b_fnum;
12922
12923 fp = var2fpos(&argvars[0], FALSE, &fnum);
12924 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
12925 && fnum == curbuf->b_fnum)
12926 {
12927 getvvcol(curwin, fp, NULL, NULL, &vcol);
12928 ++vcol;
12929 }
12930
12931 rettv->vval.v_number = vcol;
12932}
12933
12934/*
12935 * "visualmode()" function
12936 */
12937 static void
12938f_visualmode(typval_T *argvars, typval_T *rettv)
12939{
12940 char_u str[2];
12941
12942 rettv->v_type = VAR_STRING;
12943 str[0] = curbuf->b_visual_mode_eval;
12944 str[1] = NUL;
12945 rettv->vval.v_string = vim_strsave(str);
12946
12947 /* A non-zero number or non-empty string argument: reset mode. */
12948 if (non_zero_arg(&argvars[0]))
12949 curbuf->b_visual_mode_eval = NUL;
12950}
12951
12952/*
12953 * "wildmenumode()" function
12954 */
12955 static void
12956f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12957{
12958#ifdef FEAT_WILDMENU
12959 if (wild_menu_showing)
12960 rettv->vval.v_number = 1;
12961#endif
12962}
12963
12964/*
12965 * "winbufnr(nr)" function
12966 */
12967 static void
12968f_winbufnr(typval_T *argvars, typval_T *rettv)
12969{
12970 win_T *wp;
12971
12972 wp = find_win_by_nr(&argvars[0], NULL);
12973 if (wp == NULL)
12974 rettv->vval.v_number = -1;
12975 else
12976 rettv->vval.v_number = wp->w_buffer->b_fnum;
12977}
12978
12979/*
12980 * "wincol()" function
12981 */
12982 static void
12983f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
12984{
12985 validate_cursor();
12986 rettv->vval.v_number = curwin->w_wcol + 1;
12987}
12988
12989/*
12990 * "winheight(nr)" function
12991 */
12992 static void
12993f_winheight(typval_T *argvars, typval_T *rettv)
12994{
12995 win_T *wp;
12996
12997 wp = find_win_by_nr(&argvars[0], NULL);
12998 if (wp == NULL)
12999 rettv->vval.v_number = -1;
13000 else
13001 rettv->vval.v_number = wp->w_height;
13002}
13003
13004/*
13005 * "winline()" function
13006 */
13007 static void
13008f_winline(typval_T *argvars UNUSED, typval_T *rettv)
13009{
13010 validate_cursor();
13011 rettv->vval.v_number = curwin->w_wrow + 1;
13012}
13013
13014/*
13015 * "winnr()" function
13016 */
13017 static void
13018f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
13019{
13020 int nr = 1;
13021
13022#ifdef FEAT_WINDOWS
13023 nr = get_winnr(curtab, &argvars[0]);
13024#endif
13025 rettv->vval.v_number = nr;
13026}
13027
13028/*
13029 * "winrestcmd()" function
13030 */
13031 static void
13032f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
13033{
13034#ifdef FEAT_WINDOWS
13035 win_T *wp;
13036 int winnr = 1;
13037 garray_T ga;
13038 char_u buf[50];
13039
13040 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020013041 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013042 {
13043 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
13044 ga_concat(&ga, buf);
13045 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
13046 ga_concat(&ga, buf);
13047 ++winnr;
13048 }
13049 ga_append(&ga, NUL);
13050
13051 rettv->vval.v_string = ga.ga_data;
13052#else
13053 rettv->vval.v_string = NULL;
13054#endif
13055 rettv->v_type = VAR_STRING;
13056}
13057
13058/*
13059 * "winrestview()" function
13060 */
13061 static void
13062f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
13063{
13064 dict_T *dict;
13065
13066 if (argvars[0].v_type != VAR_DICT
13067 || (dict = argvars[0].vval.v_dict) == NULL)
13068 EMSG(_(e_invarg));
13069 else
13070 {
13071 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
13072 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
13073 if (dict_find(dict, (char_u *)"col", -1) != NULL)
13074 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
13075#ifdef FEAT_VIRTUALEDIT
13076 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
13077 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
13078#endif
13079 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
13080 {
13081 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
13082 curwin->w_set_curswant = FALSE;
13083 }
13084
13085 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
13086 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
13087#ifdef FEAT_DIFF
13088 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
13089 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
13090#endif
13091 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
13092 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
13093 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
13094 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
13095
13096 check_cursor();
13097 win_new_height(curwin, curwin->w_height);
13098# ifdef FEAT_WINDOWS
13099 win_new_width(curwin, W_WIDTH(curwin));
13100# endif
13101 changed_window_setting();
13102
13103 if (curwin->w_topline <= 0)
13104 curwin->w_topline = 1;
13105 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
13106 curwin->w_topline = curbuf->b_ml.ml_line_count;
13107#ifdef FEAT_DIFF
13108 check_topfill(curwin, TRUE);
13109#endif
13110 }
13111}
13112
13113/*
13114 * "winsaveview()" function
13115 */
13116 static void
13117f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
13118{
13119 dict_T *dict;
13120
13121 if (rettv_dict_alloc(rettv) == FAIL)
13122 return;
13123 dict = rettv->vval.v_dict;
13124
13125 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
13126 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
13127#ifdef FEAT_VIRTUALEDIT
13128 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
13129#endif
13130 update_curswant();
13131 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
13132
13133 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
13134#ifdef FEAT_DIFF
13135 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
13136#endif
13137 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
13138 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
13139}
13140
13141/*
13142 * "winwidth(nr)" function
13143 */
13144 static void
13145f_winwidth(typval_T *argvars, typval_T *rettv)
13146{
13147 win_T *wp;
13148
13149 wp = find_win_by_nr(&argvars[0], NULL);
13150 if (wp == NULL)
13151 rettv->vval.v_number = -1;
13152 else
13153#ifdef FEAT_WINDOWS
13154 rettv->vval.v_number = wp->w_width;
13155#else
13156 rettv->vval.v_number = Columns;
13157#endif
13158}
13159
13160/*
13161 * "wordcount()" function
13162 */
13163 static void
13164f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
13165{
13166 if (rettv_dict_alloc(rettv) == FAIL)
13167 return;
13168 cursor_pos_info(rettv->vval.v_dict);
13169}
13170
13171/*
13172 * "writefile()" function
13173 */
13174 static void
13175f_writefile(typval_T *argvars, typval_T *rettv)
13176{
13177 int binary = FALSE;
13178 int append = FALSE;
13179 char_u *fname;
13180 FILE *fd;
13181 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013182 listitem_T *li;
13183 list_T *list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013184
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013185 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013186 if (check_restricted() || check_secure())
13187 return;
13188
13189 if (argvars[0].v_type != VAR_LIST)
13190 {
13191 EMSG2(_(e_listarg), "writefile()");
13192 return;
13193 }
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013194 list = argvars[0].vval.v_list;
13195 if (list == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013196 return;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013197 for (li = list->lv_first; li != NULL; li = li->li_next)
13198 if (get_tv_string_chk(&li->li_tv) == NULL)
13199 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013200
13201 if (argvars[2].v_type != VAR_UNKNOWN)
13202 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013203 char_u *arg2 = get_tv_string_chk(&argvars[2]);
13204
13205 if (arg2 == NULL)
13206 return;
13207 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013208 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013209 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013210 append = TRUE;
13211 }
13212
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013213 fname = get_tv_string_chk(&argvars[1]);
13214 if (fname == NULL)
13215 return;
13216
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013217 /* Always open the file in binary mode, library functions have a mind of
13218 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013219 if (*fname == NUL || (fd = mch_fopen((char *)fname,
13220 append ? APPENDBIN : WRITEBIN)) == NULL)
13221 {
13222 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
13223 ret = -1;
13224 }
13225 else
13226 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013227 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013228 ret = -1;
13229 fclose(fd);
13230 }
13231
13232 rettv->vval.v_number = ret;
13233}
13234
13235/*
13236 * "xor(expr, expr)" function
13237 */
13238 static void
13239f_xor(typval_T *argvars, typval_T *rettv)
13240{
13241 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
13242 ^ get_tv_number_chk(&argvars[1], NULL);
13243}
13244
13245
13246#endif /* FEAT_EVAL */