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