blob: c85c334c82f13b5c929729d7fe46f201e608a7a8 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalfunc.c: Builtin functions
12 */
13#define USING_FLOAT_STUFF
14
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef VMS
24# include <float.h>
25#endif
26
27#ifdef MACOS
28# include <time.h> /* for time_t */
29#endif
30
31static char *e_listarg = N_("E686: Argument of %s must be a List");
32#ifdef FEAT_QUICKFIX
33static char *e_stringreq = N_("E928: String required");
34#endif
35
36#ifdef FEAT_FLOAT
37static void f_abs(typval_T *argvars, typval_T *rettv);
38static void f_acos(typval_T *argvars, typval_T *rettv);
39#endif
40static void f_add(typval_T *argvars, typval_T *rettv);
41static void f_and(typval_T *argvars, typval_T *rettv);
42static void f_append(typval_T *argvars, typval_T *rettv);
43static void f_argc(typval_T *argvars, typval_T *rettv);
44static void f_argidx(typval_T *argvars, typval_T *rettv);
45static void f_arglistid(typval_T *argvars, typval_T *rettv);
46static void f_argv(typval_T *argvars, typval_T *rettv);
47static void f_assert_equal(typval_T *argvars, typval_T *rettv);
48static void f_assert_exception(typval_T *argvars, typval_T *rettv);
49static void f_assert_fails(typval_T *argvars, typval_T *rettv);
50static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaar61c04492016-07-23 15:35:35 +020051static void f_assert_inrange(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020052static void f_assert_match(typval_T *argvars, typval_T *rettv);
53static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
54static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar42205552017-03-18 19:42:22 +010055static void f_assert_report(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020056static void f_assert_true(typval_T *argvars, typval_T *rettv);
57#ifdef FEAT_FLOAT
58static void f_asin(typval_T *argvars, typval_T *rettv);
59static void f_atan(typval_T *argvars, typval_T *rettv);
60static void f_atan2(typval_T *argvars, typval_T *rettv);
61#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010062#ifdef FEAT_BEVAL
63static void f_balloon_show(typval_T *argvars, typval_T *rettv);
64#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020065static void f_browse(typval_T *argvars, typval_T *rettv);
66static void f_browsedir(typval_T *argvars, typval_T *rettv);
67static void f_bufexists(typval_T *argvars, typval_T *rettv);
68static void f_buflisted(typval_T *argvars, typval_T *rettv);
69static void f_bufloaded(typval_T *argvars, typval_T *rettv);
70static void f_bufname(typval_T *argvars, typval_T *rettv);
71static void f_bufnr(typval_T *argvars, typval_T *rettv);
72static void f_bufwinid(typval_T *argvars, typval_T *rettv);
73static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
74static void f_byte2line(typval_T *argvars, typval_T *rettv);
75static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
76static void f_byteidx(typval_T *argvars, typval_T *rettv);
77static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
78static void f_call(typval_T *argvars, typval_T *rettv);
79#ifdef FEAT_FLOAT
80static void f_ceil(typval_T *argvars, typval_T *rettv);
81#endif
82#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +010083static void f_ch_canread(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020084static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0874a832016-09-01 15:11:51 +020085static void f_ch_close_in(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020086static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
87static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
88static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
89static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
90static void f_ch_info(typval_T *argvars, typval_T *rettv);
91static void f_ch_log(typval_T *argvars, typval_T *rettv);
92static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
93static void f_ch_open(typval_T *argvars, typval_T *rettv);
94static void f_ch_read(typval_T *argvars, typval_T *rettv);
95static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
96static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
97static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
98static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
99static void f_ch_status(typval_T *argvars, typval_T *rettv);
100#endif
101static void f_changenr(typval_T *argvars, typval_T *rettv);
102static void f_char2nr(typval_T *argvars, typval_T *rettv);
103static void f_cindent(typval_T *argvars, typval_T *rettv);
104static void f_clearmatches(typval_T *argvars, typval_T *rettv);
105static void f_col(typval_T *argvars, typval_T *rettv);
106#if defined(FEAT_INS_EXPAND)
107static void f_complete(typval_T *argvars, typval_T *rettv);
108static void f_complete_add(typval_T *argvars, typval_T *rettv);
109static void f_complete_check(typval_T *argvars, typval_T *rettv);
110#endif
111static void f_confirm(typval_T *argvars, typval_T *rettv);
112static void f_copy(typval_T *argvars, typval_T *rettv);
113#ifdef FEAT_FLOAT
114static void f_cos(typval_T *argvars, typval_T *rettv);
115static void f_cosh(typval_T *argvars, typval_T *rettv);
116#endif
117static void f_count(typval_T *argvars, typval_T *rettv);
118static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
119static void f_cursor(typval_T *argsvars, typval_T *rettv);
120static void f_deepcopy(typval_T *argvars, typval_T *rettv);
121static void f_delete(typval_T *argvars, typval_T *rettv);
122static void f_did_filetype(typval_T *argvars, typval_T *rettv);
123static void f_diff_filler(typval_T *argvars, typval_T *rettv);
124static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
125static void f_empty(typval_T *argvars, typval_T *rettv);
126static void f_escape(typval_T *argvars, typval_T *rettv);
127static void f_eval(typval_T *argvars, typval_T *rettv);
128static void f_eventhandler(typval_T *argvars, typval_T *rettv);
129static void f_executable(typval_T *argvars, typval_T *rettv);
130static void f_execute(typval_T *argvars, typval_T *rettv);
131static void f_exepath(typval_T *argvars, typval_T *rettv);
132static void f_exists(typval_T *argvars, typval_T *rettv);
133#ifdef FEAT_FLOAT
134static void f_exp(typval_T *argvars, typval_T *rettv);
135#endif
136static void f_expand(typval_T *argvars, typval_T *rettv);
137static void f_extend(typval_T *argvars, typval_T *rettv);
138static void f_feedkeys(typval_T *argvars, typval_T *rettv);
139static void f_filereadable(typval_T *argvars, typval_T *rettv);
140static void f_filewritable(typval_T *argvars, typval_T *rettv);
141static void f_filter(typval_T *argvars, typval_T *rettv);
142static void f_finddir(typval_T *argvars, typval_T *rettv);
143static void f_findfile(typval_T *argvars, typval_T *rettv);
144#ifdef FEAT_FLOAT
145static void f_float2nr(typval_T *argvars, typval_T *rettv);
146static void f_floor(typval_T *argvars, typval_T *rettv);
147static void f_fmod(typval_T *argvars, typval_T *rettv);
148#endif
149static void f_fnameescape(typval_T *argvars, typval_T *rettv);
150static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
151static void f_foldclosed(typval_T *argvars, typval_T *rettv);
152static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
153static void f_foldlevel(typval_T *argvars, typval_T *rettv);
154static void f_foldtext(typval_T *argvars, typval_T *rettv);
155static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
156static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200157static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200158static void f_function(typval_T *argvars, typval_T *rettv);
159static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
160static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200161static void f_getbufinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200162static void f_getbufline(typval_T *argvars, typval_T *rettv);
163static void f_getbufvar(typval_T *argvars, typval_T *rettv);
164static void f_getchar(typval_T *argvars, typval_T *rettv);
165static void f_getcharmod(typval_T *argvars, typval_T *rettv);
166static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
167static void f_getcmdline(typval_T *argvars, typval_T *rettv);
168#if defined(FEAT_CMDL_COMPL)
169static void f_getcompletion(typval_T *argvars, typval_T *rettv);
170#endif
171static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
172static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
173static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
174static void f_getcwd(typval_T *argvars, typval_T *rettv);
175static void f_getfontname(typval_T *argvars, typval_T *rettv);
176static void f_getfperm(typval_T *argvars, typval_T *rettv);
177static void f_getfsize(typval_T *argvars, typval_T *rettv);
178static void f_getftime(typval_T *argvars, typval_T *rettv);
179static void f_getftype(typval_T *argvars, typval_T *rettv);
180static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200181static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200182static void f_getmatches(typval_T *argvars, typval_T *rettv);
183static void f_getpid(typval_T *argvars, typval_T *rettv);
184static void f_getcurpos(typval_T *argvars, typval_T *rettv);
185static void f_getpos(typval_T *argvars, typval_T *rettv);
186static void f_getqflist(typval_T *argvars, typval_T *rettv);
187static void f_getreg(typval_T *argvars, typval_T *rettv);
188static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200189static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200190static void f_gettabvar(typval_T *argvars, typval_T *rettv);
191static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200192static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200193static void f_getwinposx(typval_T *argvars, typval_T *rettv);
194static void f_getwinposy(typval_T *argvars, typval_T *rettv);
195static void f_getwinvar(typval_T *argvars, typval_T *rettv);
196static void f_glob(typval_T *argvars, typval_T *rettv);
197static void f_globpath(typval_T *argvars, typval_T *rettv);
198static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
199static void f_has(typval_T *argvars, typval_T *rettv);
200static void f_has_key(typval_T *argvars, typval_T *rettv);
201static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
202static void f_hasmapto(typval_T *argvars, typval_T *rettv);
203static void f_histadd(typval_T *argvars, typval_T *rettv);
204static void f_histdel(typval_T *argvars, typval_T *rettv);
205static void f_histget(typval_T *argvars, typval_T *rettv);
206static void f_histnr(typval_T *argvars, typval_T *rettv);
207static void f_hlID(typval_T *argvars, typval_T *rettv);
208static void f_hlexists(typval_T *argvars, typval_T *rettv);
209static void f_hostname(typval_T *argvars, typval_T *rettv);
210static void f_iconv(typval_T *argvars, typval_T *rettv);
211static void f_indent(typval_T *argvars, typval_T *rettv);
212static void f_index(typval_T *argvars, typval_T *rettv);
213static void f_input(typval_T *argvars, typval_T *rettv);
214static void f_inputdialog(typval_T *argvars, typval_T *rettv);
215static void f_inputlist(typval_T *argvars, typval_T *rettv);
216static void f_inputrestore(typval_T *argvars, typval_T *rettv);
217static void f_inputsave(typval_T *argvars, typval_T *rettv);
218static void f_inputsecret(typval_T *argvars, typval_T *rettv);
219static void f_insert(typval_T *argvars, typval_T *rettv);
220static void f_invert(typval_T *argvars, typval_T *rettv);
221static void f_isdirectory(typval_T *argvars, typval_T *rettv);
222static void f_islocked(typval_T *argvars, typval_T *rettv);
223#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
224static void f_isnan(typval_T *argvars, typval_T *rettv);
225#endif
226static void f_items(typval_T *argvars, typval_T *rettv);
227#ifdef FEAT_JOB_CHANNEL
228static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
229static void f_job_info(typval_T *argvars, typval_T *rettv);
230static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
231static void f_job_start(typval_T *argvars, typval_T *rettv);
232static void f_job_stop(typval_T *argvars, typval_T *rettv);
233static void f_job_status(typval_T *argvars, typval_T *rettv);
234#endif
235static void f_join(typval_T *argvars, typval_T *rettv);
236static void f_js_decode(typval_T *argvars, typval_T *rettv);
237static void f_js_encode(typval_T *argvars, typval_T *rettv);
238static void f_json_decode(typval_T *argvars, typval_T *rettv);
239static void f_json_encode(typval_T *argvars, typval_T *rettv);
240static void f_keys(typval_T *argvars, typval_T *rettv);
241static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
242static void f_len(typval_T *argvars, typval_T *rettv);
243static void f_libcall(typval_T *argvars, typval_T *rettv);
244static void f_libcallnr(typval_T *argvars, typval_T *rettv);
245static void f_line(typval_T *argvars, typval_T *rettv);
246static void f_line2byte(typval_T *argvars, typval_T *rettv);
247static void f_lispindent(typval_T *argvars, typval_T *rettv);
248static void f_localtime(typval_T *argvars, typval_T *rettv);
249#ifdef FEAT_FLOAT
250static void f_log(typval_T *argvars, typval_T *rettv);
251static void f_log10(typval_T *argvars, typval_T *rettv);
252#endif
253#ifdef FEAT_LUA
254static void f_luaeval(typval_T *argvars, typval_T *rettv);
255#endif
256static void f_map(typval_T *argvars, typval_T *rettv);
257static void f_maparg(typval_T *argvars, typval_T *rettv);
258static void f_mapcheck(typval_T *argvars, typval_T *rettv);
259static void f_match(typval_T *argvars, typval_T *rettv);
260static void f_matchadd(typval_T *argvars, typval_T *rettv);
261static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
262static void f_matcharg(typval_T *argvars, typval_T *rettv);
263static void f_matchdelete(typval_T *argvars, typval_T *rettv);
264static void f_matchend(typval_T *argvars, typval_T *rettv);
265static void f_matchlist(typval_T *argvars, typval_T *rettv);
266static void f_matchstr(typval_T *argvars, typval_T *rettv);
267static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
268static void f_max(typval_T *argvars, typval_T *rettv);
269static void f_min(typval_T *argvars, typval_T *rettv);
270#ifdef vim_mkdir
271static void f_mkdir(typval_T *argvars, typval_T *rettv);
272#endif
273static void f_mode(typval_T *argvars, typval_T *rettv);
274#ifdef FEAT_MZSCHEME
275static void f_mzeval(typval_T *argvars, typval_T *rettv);
276#endif
277static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
278static void f_nr2char(typval_T *argvars, typval_T *rettv);
279static void f_or(typval_T *argvars, typval_T *rettv);
280static void f_pathshorten(typval_T *argvars, typval_T *rettv);
281#ifdef FEAT_PERL
282static void f_perleval(typval_T *argvars, typval_T *rettv);
283#endif
284#ifdef FEAT_FLOAT
285static void f_pow(typval_T *argvars, typval_T *rettv);
286#endif
287static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
288static void f_printf(typval_T *argvars, typval_T *rettv);
289static void f_pumvisible(typval_T *argvars, typval_T *rettv);
290#ifdef FEAT_PYTHON3
291static void f_py3eval(typval_T *argvars, typval_T *rettv);
292#endif
293#ifdef FEAT_PYTHON
294static void f_pyeval(typval_T *argvars, typval_T *rettv);
295#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100296#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
297static void f_pyxeval(typval_T *argvars, typval_T *rettv);
298#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200299static void f_range(typval_T *argvars, typval_T *rettv);
300static void f_readfile(typval_T *argvars, typval_T *rettv);
301static void f_reltime(typval_T *argvars, typval_T *rettv);
302#ifdef FEAT_FLOAT
303static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
304#endif
305static void f_reltimestr(typval_T *argvars, typval_T *rettv);
306static void f_remote_expr(typval_T *argvars, typval_T *rettv);
307static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
308static void f_remote_peek(typval_T *argvars, typval_T *rettv);
309static void f_remote_read(typval_T *argvars, typval_T *rettv);
310static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100311static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200312static void f_remove(typval_T *argvars, typval_T *rettv);
313static void f_rename(typval_T *argvars, typval_T *rettv);
314static void f_repeat(typval_T *argvars, typval_T *rettv);
315static void f_resolve(typval_T *argvars, typval_T *rettv);
316static void f_reverse(typval_T *argvars, typval_T *rettv);
317#ifdef FEAT_FLOAT
318static void f_round(typval_T *argvars, typval_T *rettv);
319#endif
320static void f_screenattr(typval_T *argvars, typval_T *rettv);
321static void f_screenchar(typval_T *argvars, typval_T *rettv);
322static void f_screencol(typval_T *argvars, typval_T *rettv);
323static void f_screenrow(typval_T *argvars, typval_T *rettv);
324static void f_search(typval_T *argvars, typval_T *rettv);
325static void f_searchdecl(typval_T *argvars, typval_T *rettv);
326static void f_searchpair(typval_T *argvars, typval_T *rettv);
327static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
328static void f_searchpos(typval_T *argvars, typval_T *rettv);
329static void f_server2client(typval_T *argvars, typval_T *rettv);
330static void f_serverlist(typval_T *argvars, typval_T *rettv);
331static void f_setbufvar(typval_T *argvars, typval_T *rettv);
332static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
333static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
334static void f_setfperm(typval_T *argvars, typval_T *rettv);
335static void f_setline(typval_T *argvars, typval_T *rettv);
336static void f_setloclist(typval_T *argvars, typval_T *rettv);
337static void f_setmatches(typval_T *argvars, typval_T *rettv);
338static void f_setpos(typval_T *argvars, typval_T *rettv);
339static void f_setqflist(typval_T *argvars, typval_T *rettv);
340static void f_setreg(typval_T *argvars, typval_T *rettv);
341static void f_settabvar(typval_T *argvars, typval_T *rettv);
342static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
343static void f_setwinvar(typval_T *argvars, typval_T *rettv);
344#ifdef FEAT_CRYPT
345static void f_sha256(typval_T *argvars, typval_T *rettv);
346#endif /* FEAT_CRYPT */
347static void f_shellescape(typval_T *argvars, typval_T *rettv);
348static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
349static void f_simplify(typval_T *argvars, typval_T *rettv);
350#ifdef FEAT_FLOAT
351static void f_sin(typval_T *argvars, typval_T *rettv);
352static void f_sinh(typval_T *argvars, typval_T *rettv);
353#endif
354static void f_sort(typval_T *argvars, typval_T *rettv);
355static void f_soundfold(typval_T *argvars, typval_T *rettv);
356static void f_spellbadword(typval_T *argvars, typval_T *rettv);
357static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
358static void f_split(typval_T *argvars, typval_T *rettv);
359#ifdef FEAT_FLOAT
360static void f_sqrt(typval_T *argvars, typval_T *rettv);
361static void f_str2float(typval_T *argvars, typval_T *rettv);
362#endif
363static void f_str2nr(typval_T *argvars, typval_T *rettv);
364static void f_strchars(typval_T *argvars, typval_T *rettv);
365#ifdef HAVE_STRFTIME
366static void f_strftime(typval_T *argvars, typval_T *rettv);
367#endif
368static void f_strgetchar(typval_T *argvars, typval_T *rettv);
369static void f_stridx(typval_T *argvars, typval_T *rettv);
370static void f_string(typval_T *argvars, typval_T *rettv);
371static void f_strlen(typval_T *argvars, typval_T *rettv);
372static void f_strcharpart(typval_T *argvars, typval_T *rettv);
373static void f_strpart(typval_T *argvars, typval_T *rettv);
374static void f_strridx(typval_T *argvars, typval_T *rettv);
375static void f_strtrans(typval_T *argvars, typval_T *rettv);
376static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
377static void f_strwidth(typval_T *argvars, typval_T *rettv);
378static void f_submatch(typval_T *argvars, typval_T *rettv);
379static void f_substitute(typval_T *argvars, typval_T *rettv);
380static void f_synID(typval_T *argvars, typval_T *rettv);
381static void f_synIDattr(typval_T *argvars, typval_T *rettv);
382static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
383static void f_synstack(typval_T *argvars, typval_T *rettv);
384static void f_synconcealed(typval_T *argvars, typval_T *rettv);
385static void f_system(typval_T *argvars, typval_T *rettv);
386static void f_systemlist(typval_T *argvars, typval_T *rettv);
387static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
388static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
389static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
390static void f_taglist(typval_T *argvars, typval_T *rettv);
391static void f_tagfiles(typval_T *argvars, typval_T *rettv);
392static void f_tempname(typval_T *argvars, typval_T *rettv);
393static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
394static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100395static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200396static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100397static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200398#ifdef FEAT_JOB_CHANNEL
399static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
400#endif
401static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
402#ifdef FEAT_JOB_CHANNEL
403static void f_test_null_job(typval_T *argvars, typval_T *rettv);
404#endif
405static void f_test_null_list(typval_T *argvars, typval_T *rettv);
406static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
407static void f_test_null_string(typval_T *argvars, typval_T *rettv);
408static void f_test_settime(typval_T *argvars, typval_T *rettv);
409#ifdef FEAT_FLOAT
410static void f_tan(typval_T *argvars, typval_T *rettv);
411static void f_tanh(typval_T *argvars, typval_T *rettv);
412#endif
413#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200414static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200415static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200416static void f_timer_start(typval_T *argvars, typval_T *rettv);
417static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200418static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200419#endif
420static void f_tolower(typval_T *argvars, typval_T *rettv);
421static void f_toupper(typval_T *argvars, typval_T *rettv);
422static void f_tr(typval_T *argvars, typval_T *rettv);
423#ifdef FEAT_FLOAT
424static void f_trunc(typval_T *argvars, typval_T *rettv);
425#endif
426static void f_type(typval_T *argvars, typval_T *rettv);
427static void f_undofile(typval_T *argvars, typval_T *rettv);
428static void f_undotree(typval_T *argvars, typval_T *rettv);
429static void f_uniq(typval_T *argvars, typval_T *rettv);
430static void f_values(typval_T *argvars, typval_T *rettv);
431static void f_virtcol(typval_T *argvars, typval_T *rettv);
432static void f_visualmode(typval_T *argvars, typval_T *rettv);
433static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
434static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
435static void f_win_getid(typval_T *argvars, typval_T *rettv);
436static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
437static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
438static void f_win_id2win(typval_T *argvars, typval_T *rettv);
439static void f_winbufnr(typval_T *argvars, typval_T *rettv);
440static void f_wincol(typval_T *argvars, typval_T *rettv);
441static void f_winheight(typval_T *argvars, typval_T *rettv);
442static void f_winline(typval_T *argvars, typval_T *rettv);
443static void f_winnr(typval_T *argvars, typval_T *rettv);
444static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
445static void f_winrestview(typval_T *argvars, typval_T *rettv);
446static void f_winsaveview(typval_T *argvars, typval_T *rettv);
447static void f_winwidth(typval_T *argvars, typval_T *rettv);
448static void f_writefile(typval_T *argvars, typval_T *rettv);
449static void f_wordcount(typval_T *argvars, typval_T *rettv);
450static void f_xor(typval_T *argvars, typval_T *rettv);
451
452/*
453 * Array with names and number of arguments of all internal functions
454 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
455 */
456static struct fst
457{
458 char *f_name; /* function name */
459 char f_min_argc; /* minimal number of arguments */
460 char f_max_argc; /* maximal number of arguments */
461 void (*f_func)(typval_T *args, typval_T *rvar);
462 /* implementation of function */
463} functions[] =
464{
465#ifdef FEAT_FLOAT
466 {"abs", 1, 1, f_abs},
467 {"acos", 1, 1, f_acos}, /* WJMc */
468#endif
469 {"add", 2, 2, f_add},
470 {"and", 2, 2, f_and},
471 {"append", 2, 2, f_append},
472 {"argc", 0, 0, f_argc},
473 {"argidx", 0, 0, f_argidx},
474 {"arglistid", 0, 2, f_arglistid},
475 {"argv", 0, 1, f_argv},
476#ifdef FEAT_FLOAT
477 {"asin", 1, 1, f_asin}, /* WJMc */
478#endif
479 {"assert_equal", 2, 3, f_assert_equal},
480 {"assert_exception", 1, 2, f_assert_exception},
481 {"assert_fails", 1, 2, f_assert_fails},
482 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100483 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200484 {"assert_match", 2, 3, f_assert_match},
485 {"assert_notequal", 2, 3, f_assert_notequal},
486 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100487 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200488 {"assert_true", 1, 2, f_assert_true},
489#ifdef FEAT_FLOAT
490 {"atan", 1, 1, f_atan},
491 {"atan2", 2, 2, f_atan2},
492#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100493#ifdef FEAT_BEVAL
494 {"balloon_show", 1, 1, f_balloon_show},
495#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200496 {"browse", 4, 4, f_browse},
497 {"browsedir", 2, 2, f_browsedir},
498 {"bufexists", 1, 1, f_bufexists},
499 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
500 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
501 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
502 {"buflisted", 1, 1, f_buflisted},
503 {"bufloaded", 1, 1, f_bufloaded},
504 {"bufname", 1, 1, f_bufname},
505 {"bufnr", 1, 2, f_bufnr},
506 {"bufwinid", 1, 1, f_bufwinid},
507 {"bufwinnr", 1, 1, f_bufwinnr},
508 {"byte2line", 1, 1, f_byte2line},
509 {"byteidx", 2, 2, f_byteidx},
510 {"byteidxcomp", 2, 2, f_byteidxcomp},
511 {"call", 2, 3, f_call},
512#ifdef FEAT_FLOAT
513 {"ceil", 1, 1, f_ceil},
514#endif
515#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100516 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200517 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200518 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200519 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
520 {"ch_evalraw", 2, 3, f_ch_evalraw},
521 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
522 {"ch_getjob", 1, 1, f_ch_getjob},
523 {"ch_info", 1, 1, f_ch_info},
524 {"ch_log", 1, 2, f_ch_log},
525 {"ch_logfile", 1, 2, f_ch_logfile},
526 {"ch_open", 1, 2, f_ch_open},
527 {"ch_read", 1, 2, f_ch_read},
528 {"ch_readraw", 1, 2, f_ch_readraw},
529 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
530 {"ch_sendraw", 2, 3, f_ch_sendraw},
531 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200532 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200533#endif
534 {"changenr", 0, 0, f_changenr},
535 {"char2nr", 1, 2, f_char2nr},
536 {"cindent", 1, 1, f_cindent},
537 {"clearmatches", 0, 0, f_clearmatches},
538 {"col", 1, 1, f_col},
539#if defined(FEAT_INS_EXPAND)
540 {"complete", 2, 2, f_complete},
541 {"complete_add", 1, 1, f_complete_add},
542 {"complete_check", 0, 0, f_complete_check},
543#endif
544 {"confirm", 1, 4, f_confirm},
545 {"copy", 1, 1, f_copy},
546#ifdef FEAT_FLOAT
547 {"cos", 1, 1, f_cos},
548 {"cosh", 1, 1, f_cosh},
549#endif
550 {"count", 2, 4, f_count},
551 {"cscope_connection",0,3, f_cscope_connection},
552 {"cursor", 1, 3, f_cursor},
553 {"deepcopy", 1, 2, f_deepcopy},
554 {"delete", 1, 2, f_delete},
555 {"did_filetype", 0, 0, f_did_filetype},
556 {"diff_filler", 1, 1, f_diff_filler},
557 {"diff_hlID", 2, 2, f_diff_hlID},
558 {"empty", 1, 1, f_empty},
559 {"escape", 2, 2, f_escape},
560 {"eval", 1, 1, f_eval},
561 {"eventhandler", 0, 0, f_eventhandler},
562 {"executable", 1, 1, f_executable},
563 {"execute", 1, 2, f_execute},
564 {"exepath", 1, 1, f_exepath},
565 {"exists", 1, 1, f_exists},
566#ifdef FEAT_FLOAT
567 {"exp", 1, 1, f_exp},
568#endif
569 {"expand", 1, 3, f_expand},
570 {"extend", 2, 3, f_extend},
571 {"feedkeys", 1, 2, f_feedkeys},
572 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
573 {"filereadable", 1, 1, f_filereadable},
574 {"filewritable", 1, 1, f_filewritable},
575 {"filter", 2, 2, f_filter},
576 {"finddir", 1, 3, f_finddir},
577 {"findfile", 1, 3, f_findfile},
578#ifdef FEAT_FLOAT
579 {"float2nr", 1, 1, f_float2nr},
580 {"floor", 1, 1, f_floor},
581 {"fmod", 2, 2, f_fmod},
582#endif
583 {"fnameescape", 1, 1, f_fnameescape},
584 {"fnamemodify", 2, 2, f_fnamemodify},
585 {"foldclosed", 1, 1, f_foldclosed},
586 {"foldclosedend", 1, 1, f_foldclosedend},
587 {"foldlevel", 1, 1, f_foldlevel},
588 {"foldtext", 0, 0, f_foldtext},
589 {"foldtextresult", 1, 1, f_foldtextresult},
590 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200591 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200592 {"function", 1, 3, f_function},
593 {"garbagecollect", 0, 1, f_garbagecollect},
594 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200595 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200596 {"getbufline", 2, 3, f_getbufline},
597 {"getbufvar", 2, 3, f_getbufvar},
598 {"getchar", 0, 1, f_getchar},
599 {"getcharmod", 0, 0, f_getcharmod},
600 {"getcharsearch", 0, 0, f_getcharsearch},
601 {"getcmdline", 0, 0, f_getcmdline},
602 {"getcmdpos", 0, 0, f_getcmdpos},
603 {"getcmdtype", 0, 0, f_getcmdtype},
604 {"getcmdwintype", 0, 0, f_getcmdwintype},
605#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200606 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200607#endif
608 {"getcurpos", 0, 0, f_getcurpos},
609 {"getcwd", 0, 2, f_getcwd},
610 {"getfontname", 0, 1, f_getfontname},
611 {"getfperm", 1, 1, f_getfperm},
612 {"getfsize", 1, 1, f_getfsize},
613 {"getftime", 1, 1, f_getftime},
614 {"getftype", 1, 1, f_getftype},
615 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200616 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200617 {"getmatches", 0, 0, f_getmatches},
618 {"getpid", 0, 0, f_getpid},
619 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200620 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200621 {"getreg", 0, 3, f_getreg},
622 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200623 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200624 {"gettabvar", 2, 3, f_gettabvar},
625 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200626 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200627 {"getwinposx", 0, 0, f_getwinposx},
628 {"getwinposy", 0, 0, f_getwinposy},
629 {"getwinvar", 2, 3, f_getwinvar},
630 {"glob", 1, 4, f_glob},
631 {"glob2regpat", 1, 1, f_glob2regpat},
632 {"globpath", 2, 5, f_globpath},
633 {"has", 1, 1, f_has},
634 {"has_key", 2, 2, f_has_key},
635 {"haslocaldir", 0, 2, f_haslocaldir},
636 {"hasmapto", 1, 3, f_hasmapto},
637 {"highlightID", 1, 1, f_hlID}, /* obsolete */
638 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
639 {"histadd", 2, 2, f_histadd},
640 {"histdel", 1, 2, f_histdel},
641 {"histget", 1, 2, f_histget},
642 {"histnr", 1, 1, f_histnr},
643 {"hlID", 1, 1, f_hlID},
644 {"hlexists", 1, 1, f_hlexists},
645 {"hostname", 0, 0, f_hostname},
646 {"iconv", 3, 3, f_iconv},
647 {"indent", 1, 1, f_indent},
648 {"index", 2, 4, f_index},
649 {"input", 1, 3, f_input},
650 {"inputdialog", 1, 3, f_inputdialog},
651 {"inputlist", 1, 1, f_inputlist},
652 {"inputrestore", 0, 0, f_inputrestore},
653 {"inputsave", 0, 0, f_inputsave},
654 {"inputsecret", 1, 2, f_inputsecret},
655 {"insert", 2, 3, f_insert},
656 {"invert", 1, 1, f_invert},
657 {"isdirectory", 1, 1, f_isdirectory},
658 {"islocked", 1, 1, f_islocked},
659#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
660 {"isnan", 1, 1, f_isnan},
661#endif
662 {"items", 1, 1, f_items},
663#ifdef FEAT_JOB_CHANNEL
664 {"job_getchannel", 1, 1, f_job_getchannel},
665 {"job_info", 1, 1, f_job_info},
666 {"job_setoptions", 2, 2, f_job_setoptions},
667 {"job_start", 1, 2, f_job_start},
668 {"job_status", 1, 1, f_job_status},
669 {"job_stop", 1, 2, f_job_stop},
670#endif
671 {"join", 1, 2, f_join},
672 {"js_decode", 1, 1, f_js_decode},
673 {"js_encode", 1, 1, f_js_encode},
674 {"json_decode", 1, 1, f_json_decode},
675 {"json_encode", 1, 1, f_json_encode},
676 {"keys", 1, 1, f_keys},
677 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
678 {"len", 1, 1, f_len},
679 {"libcall", 3, 3, f_libcall},
680 {"libcallnr", 3, 3, f_libcallnr},
681 {"line", 1, 1, f_line},
682 {"line2byte", 1, 1, f_line2byte},
683 {"lispindent", 1, 1, f_lispindent},
684 {"localtime", 0, 0, f_localtime},
685#ifdef FEAT_FLOAT
686 {"log", 1, 1, f_log},
687 {"log10", 1, 1, f_log10},
688#endif
689#ifdef FEAT_LUA
690 {"luaeval", 1, 2, f_luaeval},
691#endif
692 {"map", 2, 2, f_map},
693 {"maparg", 1, 4, f_maparg},
694 {"mapcheck", 1, 3, f_mapcheck},
695 {"match", 2, 4, f_match},
696 {"matchadd", 2, 5, f_matchadd},
697 {"matchaddpos", 2, 5, f_matchaddpos},
698 {"matcharg", 1, 1, f_matcharg},
699 {"matchdelete", 1, 1, f_matchdelete},
700 {"matchend", 2, 4, f_matchend},
701 {"matchlist", 2, 4, f_matchlist},
702 {"matchstr", 2, 4, f_matchstr},
703 {"matchstrpos", 2, 4, f_matchstrpos},
704 {"max", 1, 1, f_max},
705 {"min", 1, 1, f_min},
706#ifdef vim_mkdir
707 {"mkdir", 1, 3, f_mkdir},
708#endif
709 {"mode", 0, 1, f_mode},
710#ifdef FEAT_MZSCHEME
711 {"mzeval", 1, 1, f_mzeval},
712#endif
713 {"nextnonblank", 1, 1, f_nextnonblank},
714 {"nr2char", 1, 2, f_nr2char},
715 {"or", 2, 2, f_or},
716 {"pathshorten", 1, 1, f_pathshorten},
717#ifdef FEAT_PERL
718 {"perleval", 1, 1, f_perleval},
719#endif
720#ifdef FEAT_FLOAT
721 {"pow", 2, 2, f_pow},
722#endif
723 {"prevnonblank", 1, 1, f_prevnonblank},
724 {"printf", 2, 19, f_printf},
725 {"pumvisible", 0, 0, f_pumvisible},
726#ifdef FEAT_PYTHON3
727 {"py3eval", 1, 1, f_py3eval},
728#endif
729#ifdef FEAT_PYTHON
730 {"pyeval", 1, 1, f_pyeval},
731#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100732#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
733 {"pyxeval", 1, 1, f_pyxeval},
734#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200735 {"range", 1, 3, f_range},
736 {"readfile", 1, 3, f_readfile},
737 {"reltime", 0, 2, f_reltime},
738#ifdef FEAT_FLOAT
739 {"reltimefloat", 1, 1, f_reltimefloat},
740#endif
741 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100742 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200743 {"remote_foreground", 1, 1, f_remote_foreground},
744 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100745 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200746 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100747 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200748 {"remove", 2, 3, f_remove},
749 {"rename", 2, 2, f_rename},
750 {"repeat", 2, 2, f_repeat},
751 {"resolve", 1, 1, f_resolve},
752 {"reverse", 1, 1, f_reverse},
753#ifdef FEAT_FLOAT
754 {"round", 1, 1, f_round},
755#endif
756 {"screenattr", 2, 2, f_screenattr},
757 {"screenchar", 2, 2, f_screenchar},
758 {"screencol", 0, 0, f_screencol},
759 {"screenrow", 0, 0, f_screenrow},
760 {"search", 1, 4, f_search},
761 {"searchdecl", 1, 3, f_searchdecl},
762 {"searchpair", 3, 7, f_searchpair},
763 {"searchpairpos", 3, 7, f_searchpairpos},
764 {"searchpos", 1, 4, f_searchpos},
765 {"server2client", 2, 2, f_server2client},
766 {"serverlist", 0, 0, f_serverlist},
767 {"setbufvar", 3, 3, f_setbufvar},
768 {"setcharsearch", 1, 1, f_setcharsearch},
769 {"setcmdpos", 1, 1, f_setcmdpos},
770 {"setfperm", 2, 2, f_setfperm},
771 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200772 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200773 {"setmatches", 1, 1, f_setmatches},
774 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200775 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200776 {"setreg", 2, 3, f_setreg},
777 {"settabvar", 3, 3, f_settabvar},
778 {"settabwinvar", 4, 4, f_settabwinvar},
779 {"setwinvar", 3, 3, f_setwinvar},
780#ifdef FEAT_CRYPT
781 {"sha256", 1, 1, f_sha256},
782#endif
783 {"shellescape", 1, 2, f_shellescape},
784 {"shiftwidth", 0, 0, f_shiftwidth},
785 {"simplify", 1, 1, f_simplify},
786#ifdef FEAT_FLOAT
787 {"sin", 1, 1, f_sin},
788 {"sinh", 1, 1, f_sinh},
789#endif
790 {"sort", 1, 3, f_sort},
791 {"soundfold", 1, 1, f_soundfold},
792 {"spellbadword", 0, 1, f_spellbadword},
793 {"spellsuggest", 1, 3, f_spellsuggest},
794 {"split", 1, 3, f_split},
795#ifdef FEAT_FLOAT
796 {"sqrt", 1, 1, f_sqrt},
797 {"str2float", 1, 1, f_str2float},
798#endif
799 {"str2nr", 1, 2, f_str2nr},
800 {"strcharpart", 2, 3, f_strcharpart},
801 {"strchars", 1, 2, f_strchars},
802 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
803#ifdef HAVE_STRFTIME
804 {"strftime", 1, 2, f_strftime},
805#endif
806 {"strgetchar", 2, 2, f_strgetchar},
807 {"stridx", 2, 3, f_stridx},
808 {"string", 1, 1, f_string},
809 {"strlen", 1, 1, f_strlen},
810 {"strpart", 2, 3, f_strpart},
811 {"strridx", 2, 3, f_strridx},
812 {"strtrans", 1, 1, f_strtrans},
813 {"strwidth", 1, 1, f_strwidth},
814 {"submatch", 1, 2, f_submatch},
815 {"substitute", 4, 4, f_substitute},
816 {"synID", 3, 3, f_synID},
817 {"synIDattr", 2, 3, f_synIDattr},
818 {"synIDtrans", 1, 1, f_synIDtrans},
819 {"synconcealed", 2, 2, f_synconcealed},
820 {"synstack", 2, 2, f_synstack},
821 {"system", 1, 2, f_system},
822 {"systemlist", 1, 2, f_systemlist},
823 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
824 {"tabpagenr", 0, 1, f_tabpagenr},
825 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
826 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100827 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200828#ifdef FEAT_FLOAT
829 {"tan", 1, 1, f_tan},
830 {"tanh", 1, 1, f_tanh},
831#endif
832 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200833#ifdef FEAT_TERMINAL
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200834 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200835 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200836 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200837 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200838 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200839 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200840 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200841 {"term_getstatus", 1, 1, f_term_getstatus},
842 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar7c9aec42017-08-03 13:51:25 +0200843 {"term_gettty", 1, 1, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200844 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200845 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200846 {"term_sendkeys", 2, 2, f_term_sendkeys},
847 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200848 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200849#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200850 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
851 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200852 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100853 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200854#ifdef FEAT_JOB_CHANNEL
855 {"test_null_channel", 0, 0, f_test_null_channel},
856#endif
857 {"test_null_dict", 0, 0, f_test_null_dict},
858#ifdef FEAT_JOB_CHANNEL
859 {"test_null_job", 0, 0, f_test_null_job},
860#endif
861 {"test_null_list", 0, 0, f_test_null_list},
862 {"test_null_partial", 0, 0, f_test_null_partial},
863 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100864 {"test_override", 2, 2, f_test_override},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200865 {"test_settime", 1, 1, f_test_settime},
866#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200867 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200868 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200869 {"timer_start", 2, 3, f_timer_start},
870 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200871 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200872#endif
873 {"tolower", 1, 1, f_tolower},
874 {"toupper", 1, 1, f_toupper},
875 {"tr", 3, 3, f_tr},
876#ifdef FEAT_FLOAT
877 {"trunc", 1, 1, f_trunc},
878#endif
879 {"type", 1, 1, f_type},
880 {"undofile", 1, 1, f_undofile},
881 {"undotree", 0, 0, f_undotree},
882 {"uniq", 1, 3, f_uniq},
883 {"values", 1, 1, f_values},
884 {"virtcol", 1, 1, f_virtcol},
885 {"visualmode", 0, 1, f_visualmode},
886 {"wildmenumode", 0, 0, f_wildmenumode},
887 {"win_findbuf", 1, 1, f_win_findbuf},
888 {"win_getid", 0, 2, f_win_getid},
889 {"win_gotoid", 1, 1, f_win_gotoid},
890 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
891 {"win_id2win", 1, 1, f_win_id2win},
892 {"winbufnr", 1, 1, f_winbufnr},
893 {"wincol", 0, 0, f_wincol},
894 {"winheight", 1, 1, f_winheight},
895 {"winline", 0, 0, f_winline},
896 {"winnr", 0, 1, f_winnr},
897 {"winrestcmd", 0, 0, f_winrestcmd},
898 {"winrestview", 1, 1, f_winrestview},
899 {"winsaveview", 0, 0, f_winsaveview},
900 {"winwidth", 1, 1, f_winwidth},
901 {"wordcount", 0, 0, f_wordcount},
902 {"writefile", 2, 3, f_writefile},
903 {"xor", 2, 2, f_xor},
904};
905
906#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
907
908/*
909 * Function given to ExpandGeneric() to obtain the list of internal
910 * or user defined function names.
911 */
912 char_u *
913get_function_name(expand_T *xp, int idx)
914{
915 static int intidx = -1;
916 char_u *name;
917
918 if (idx == 0)
919 intidx = -1;
920 if (intidx < 0)
921 {
922 name = get_user_func_name(xp, idx);
923 if (name != NULL)
924 return name;
925 }
926 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
927 {
928 STRCPY(IObuff, functions[intidx].f_name);
929 STRCAT(IObuff, "(");
930 if (functions[intidx].f_max_argc == 0)
931 STRCAT(IObuff, ")");
932 return IObuff;
933 }
934
935 return NULL;
936}
937
938/*
939 * Function given to ExpandGeneric() to obtain the list of internal or
940 * user defined variable or function names.
941 */
942 char_u *
943get_expr_name(expand_T *xp, int idx)
944{
945 static int intidx = -1;
946 char_u *name;
947
948 if (idx == 0)
949 intidx = -1;
950 if (intidx < 0)
951 {
952 name = get_function_name(xp, idx);
953 if (name != NULL)
954 return name;
955 }
956 return get_user_var_name(xp, ++intidx);
957}
958
959#endif /* FEAT_CMDL_COMPL */
960
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200961/*
962 * Find internal function in table above.
963 * Return index, or -1 if not found
964 */
965 int
966find_internal_func(
967 char_u *name) /* name of the function */
968{
969 int first = 0;
970 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
971 int cmp;
972 int x;
973
974 /*
975 * Find the function name in the table. Binary search.
976 */
977 while (first <= last)
978 {
979 x = first + ((unsigned)(last - first) >> 1);
980 cmp = STRCMP(name, functions[x].f_name);
981 if (cmp < 0)
982 last = x - 1;
983 else if (cmp > 0)
984 first = x + 1;
985 else
986 return x;
987 }
988 return -1;
989}
990
991 int
992call_internal_func(
993 char_u *name,
994 int argcount,
995 typval_T *argvars,
996 typval_T *rettv)
997{
998 int i;
999
1000 i = find_internal_func(name);
1001 if (i < 0)
1002 return ERROR_UNKNOWN;
1003 if (argcount < functions[i].f_min_argc)
1004 return ERROR_TOOFEW;
1005 if (argcount > functions[i].f_max_argc)
1006 return ERROR_TOOMANY;
1007 argvars[argcount].v_type = VAR_UNKNOWN;
1008 functions[i].f_func(argvars, rettv);
1009 return ERROR_NONE;
1010}
1011
1012/*
1013 * Return TRUE for a non-zero Number and a non-empty String.
1014 */
1015 static int
1016non_zero_arg(typval_T *argvars)
1017{
1018 return ((argvars[0].v_type == VAR_NUMBER
1019 && argvars[0].vval.v_number != 0)
1020 || (argvars[0].v_type == VAR_SPECIAL
1021 && argvars[0].vval.v_number == VVAL_TRUE)
1022 || (argvars[0].v_type == VAR_STRING
1023 && argvars[0].vval.v_string != NULL
1024 && *argvars[0].vval.v_string != NUL));
1025}
1026
1027/*
1028 * Get the lnum from the first argument.
1029 * Also accepts ".", "$", etc., but that only works for the current buffer.
1030 * Returns -1 on error.
1031 */
1032 static linenr_T
1033get_tv_lnum(typval_T *argvars)
1034{
1035 typval_T rettv;
1036 linenr_T lnum;
1037
1038 lnum = (linenr_T)get_tv_number_chk(&argvars[0], NULL);
1039 if (lnum == 0) /* no valid number, try using line() */
1040 {
1041 rettv.v_type = VAR_NUMBER;
1042 f_line(argvars, &rettv);
1043 lnum = (linenr_T)rettv.vval.v_number;
1044 clear_tv(&rettv);
1045 }
1046 return lnum;
1047}
1048
1049#ifdef FEAT_FLOAT
1050static int get_float_arg(typval_T *argvars, float_T *f);
1051
1052/*
1053 * Get the float value of "argvars[0]" into "f".
1054 * Returns FAIL when the argument is not a Number or Float.
1055 */
1056 static int
1057get_float_arg(typval_T *argvars, float_T *f)
1058{
1059 if (argvars[0].v_type == VAR_FLOAT)
1060 {
1061 *f = argvars[0].vval.v_float;
1062 return OK;
1063 }
1064 if (argvars[0].v_type == VAR_NUMBER)
1065 {
1066 *f = (float_T)argvars[0].vval.v_number;
1067 return OK;
1068 }
1069 EMSG(_("E808: Number or Float required"));
1070 return FAIL;
1071}
1072
1073/*
1074 * "abs(expr)" function
1075 */
1076 static void
1077f_abs(typval_T *argvars, typval_T *rettv)
1078{
1079 if (argvars[0].v_type == VAR_FLOAT)
1080 {
1081 rettv->v_type = VAR_FLOAT;
1082 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1083 }
1084 else
1085 {
1086 varnumber_T n;
1087 int error = FALSE;
1088
1089 n = get_tv_number_chk(&argvars[0], &error);
1090 if (error)
1091 rettv->vval.v_number = -1;
1092 else if (n > 0)
1093 rettv->vval.v_number = n;
1094 else
1095 rettv->vval.v_number = -n;
1096 }
1097}
1098
1099/*
1100 * "acos()" function
1101 */
1102 static void
1103f_acos(typval_T *argvars, typval_T *rettv)
1104{
1105 float_T f = 0.0;
1106
1107 rettv->v_type = VAR_FLOAT;
1108 if (get_float_arg(argvars, &f) == OK)
1109 rettv->vval.v_float = acos(f);
1110 else
1111 rettv->vval.v_float = 0.0;
1112}
1113#endif
1114
1115/*
1116 * "add(list, item)" function
1117 */
1118 static void
1119f_add(typval_T *argvars, typval_T *rettv)
1120{
1121 list_T *l;
1122
1123 rettv->vval.v_number = 1; /* Default: Failed */
1124 if (argvars[0].v_type == VAR_LIST)
1125 {
1126 if ((l = argvars[0].vval.v_list) != NULL
1127 && !tv_check_lock(l->lv_lock,
1128 (char_u *)N_("add() argument"), TRUE)
1129 && list_append_tv(l, &argvars[1]) == OK)
1130 copy_tv(&argvars[0], rettv);
1131 }
1132 else
1133 EMSG(_(e_listreq));
1134}
1135
1136/*
1137 * "and(expr, expr)" function
1138 */
1139 static void
1140f_and(typval_T *argvars, typval_T *rettv)
1141{
1142 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
1143 & get_tv_number_chk(&argvars[1], NULL);
1144}
1145
1146/*
1147 * "append(lnum, string/list)" function
1148 */
1149 static void
1150f_append(typval_T *argvars, typval_T *rettv)
1151{
1152 long lnum;
1153 char_u *line;
1154 list_T *l = NULL;
1155 listitem_T *li = NULL;
1156 typval_T *tv;
1157 long added = 0;
1158
1159 /* When coming here from Insert mode, sync undo, so that this can be
1160 * undone separately from what was previously inserted. */
1161 if (u_sync_once == 2)
1162 {
1163 u_sync_once = 1; /* notify that u_sync() was called */
1164 u_sync(TRUE);
1165 }
1166
1167 lnum = get_tv_lnum(argvars);
1168 if (lnum >= 0
1169 && lnum <= curbuf->b_ml.ml_line_count
1170 && u_save(lnum, lnum + 1) == OK)
1171 {
1172 if (argvars[1].v_type == VAR_LIST)
1173 {
1174 l = argvars[1].vval.v_list;
1175 if (l == NULL)
1176 return;
1177 li = l->lv_first;
1178 }
1179 for (;;)
1180 {
1181 if (l == NULL)
1182 tv = &argvars[1]; /* append a string */
1183 else if (li == NULL)
1184 break; /* end of list */
1185 else
1186 tv = &li->li_tv; /* append item from list */
1187 line = get_tv_string_chk(tv);
1188 if (line == NULL) /* type error */
1189 {
1190 rettv->vval.v_number = 1; /* Failed */
1191 break;
1192 }
1193 ml_append(lnum + added, line, (colnr_T)0, FALSE);
1194 ++added;
1195 if (l == NULL)
1196 break;
1197 li = li->li_next;
1198 }
1199
1200 appended_lines_mark(lnum, added);
1201 if (curwin->w_cursor.lnum > lnum)
1202 curwin->w_cursor.lnum += added;
1203 }
1204 else
1205 rettv->vval.v_number = 1; /* Failed */
1206}
1207
1208/*
1209 * "argc()" function
1210 */
1211 static void
1212f_argc(typval_T *argvars UNUSED, typval_T *rettv)
1213{
1214 rettv->vval.v_number = ARGCOUNT;
1215}
1216
1217/*
1218 * "argidx()" function
1219 */
1220 static void
1221f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1222{
1223 rettv->vval.v_number = curwin->w_arg_idx;
1224}
1225
1226/*
1227 * "arglistid()" function
1228 */
1229 static void
1230f_arglistid(typval_T *argvars, typval_T *rettv)
1231{
1232 win_T *wp;
1233
1234 rettv->vval.v_number = -1;
1235 wp = find_tabwin(&argvars[0], &argvars[1]);
1236 if (wp != NULL)
1237 rettv->vval.v_number = wp->w_alist->id;
1238}
1239
1240/*
1241 * "argv(nr)" function
1242 */
1243 static void
1244f_argv(typval_T *argvars, typval_T *rettv)
1245{
1246 int idx;
1247
1248 if (argvars[0].v_type != VAR_UNKNOWN)
1249 {
1250 idx = (int)get_tv_number_chk(&argvars[0], NULL);
1251 if (idx >= 0 && idx < ARGCOUNT)
1252 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
1253 else
1254 rettv->vval.v_string = NULL;
1255 rettv->v_type = VAR_STRING;
1256 }
1257 else if (rettv_list_alloc(rettv) == OK)
1258 for (idx = 0; idx < ARGCOUNT; ++idx)
1259 list_append_string(rettv->vval.v_list,
1260 alist_name(&ARGLIST[idx]), -1);
1261}
1262
1263/*
1264 * "assert_equal(expected, actual[, msg])" function
1265 */
1266 static void
1267f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
1268{
1269 assert_equal_common(argvars, ASSERT_EQUAL);
1270}
1271
1272/*
1273 * "assert_notequal(expected, actual[, msg])" function
1274 */
1275 static void
1276f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED)
1277{
1278 assert_equal_common(argvars, ASSERT_NOTEQUAL);
1279}
1280
1281/*
1282 * "assert_exception(string[, msg])" function
1283 */
1284 static void
1285f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
1286{
1287 assert_exception(argvars);
1288}
1289
1290/*
1291 * "assert_fails(cmd [, error])" function
1292 */
1293 static void
1294f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
1295{
1296 assert_fails(argvars);
1297}
1298
1299/*
1300 * "assert_false(actual[, msg])" function
1301 */
1302 static void
1303f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
1304{
1305 assert_bool(argvars, FALSE);
1306}
1307
1308/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001309 * "assert_inrange(lower, upper[, msg])" function
1310 */
1311 static void
1312f_assert_inrange(typval_T *argvars, typval_T *rettv UNUSED)
1313{
1314 assert_inrange(argvars);
1315}
1316
1317/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001318 * "assert_match(pattern, actual[, msg])" function
1319 */
1320 static void
1321f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
1322{
1323 assert_match_common(argvars, ASSERT_MATCH);
1324}
1325
1326/*
1327 * "assert_notmatch(pattern, actual[, msg])" function
1328 */
1329 static void
1330f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED)
1331{
1332 assert_match_common(argvars, ASSERT_NOTMATCH);
1333}
1334
1335/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001336 * "assert_report(msg)" function
1337 */
1338 static void
1339f_assert_report(typval_T *argvars, typval_T *rettv UNUSED)
1340{
1341 assert_report(argvars);
1342}
1343
1344/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001345 * "assert_true(actual[, msg])" function
1346 */
1347 static void
1348f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
1349{
1350 assert_bool(argvars, TRUE);
1351}
1352
1353#ifdef FEAT_FLOAT
1354/*
1355 * "asin()" function
1356 */
1357 static void
1358f_asin(typval_T *argvars, typval_T *rettv)
1359{
1360 float_T f = 0.0;
1361
1362 rettv->v_type = VAR_FLOAT;
1363 if (get_float_arg(argvars, &f) == OK)
1364 rettv->vval.v_float = asin(f);
1365 else
1366 rettv->vval.v_float = 0.0;
1367}
1368
1369/*
1370 * "atan()" function
1371 */
1372 static void
1373f_atan(typval_T *argvars, typval_T *rettv)
1374{
1375 float_T f = 0.0;
1376
1377 rettv->v_type = VAR_FLOAT;
1378 if (get_float_arg(argvars, &f) == OK)
1379 rettv->vval.v_float = atan(f);
1380 else
1381 rettv->vval.v_float = 0.0;
1382}
1383
1384/*
1385 * "atan2()" function
1386 */
1387 static void
1388f_atan2(typval_T *argvars, typval_T *rettv)
1389{
1390 float_T fx = 0.0, fy = 0.0;
1391
1392 rettv->v_type = VAR_FLOAT;
1393 if (get_float_arg(argvars, &fx) == OK
1394 && get_float_arg(&argvars[1], &fy) == OK)
1395 rettv->vval.v_float = atan2(fx, fy);
1396 else
1397 rettv->vval.v_float = 0.0;
1398}
1399#endif
1400
1401/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001402 * "balloon_show()" function
1403 */
1404#ifdef FEAT_BEVAL
1405 static void
1406f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1407{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001408 if (balloonEval != NULL)
1409 gui_mch_post_balloon(balloonEval, get_tv_string_chk(&argvars[0]));
Bram Moolenaar59716a22017-03-01 20:32:44 +01001410}
1411#endif
1412
1413/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001414 * "browse(save, title, initdir, default)" function
1415 */
1416 static void
1417f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1418{
1419#ifdef FEAT_BROWSE
1420 int save;
1421 char_u *title;
1422 char_u *initdir;
1423 char_u *defname;
1424 char_u buf[NUMBUFLEN];
1425 char_u buf2[NUMBUFLEN];
1426 int error = FALSE;
1427
1428 save = (int)get_tv_number_chk(&argvars[0], &error);
1429 title = get_tv_string_chk(&argvars[1]);
1430 initdir = get_tv_string_buf_chk(&argvars[2], buf);
1431 defname = get_tv_string_buf_chk(&argvars[3], buf2);
1432
1433 if (error || title == NULL || initdir == NULL || defname == NULL)
1434 rettv->vval.v_string = NULL;
1435 else
1436 rettv->vval.v_string =
1437 do_browse(save ? BROWSE_SAVE : 0,
1438 title, defname, NULL, initdir, NULL, curbuf);
1439#else
1440 rettv->vval.v_string = NULL;
1441#endif
1442 rettv->v_type = VAR_STRING;
1443}
1444
1445/*
1446 * "browsedir(title, initdir)" function
1447 */
1448 static void
1449f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1450{
1451#ifdef FEAT_BROWSE
1452 char_u *title;
1453 char_u *initdir;
1454 char_u buf[NUMBUFLEN];
1455
1456 title = get_tv_string_chk(&argvars[0]);
1457 initdir = get_tv_string_buf_chk(&argvars[1], buf);
1458
1459 if (title == NULL || initdir == NULL)
1460 rettv->vval.v_string = NULL;
1461 else
1462 rettv->vval.v_string = do_browse(BROWSE_DIR,
1463 title, NULL, NULL, initdir, NULL, curbuf);
1464#else
1465 rettv->vval.v_string = NULL;
1466#endif
1467 rettv->v_type = VAR_STRING;
1468}
1469
1470static buf_T *find_buffer(typval_T *avar);
1471
1472/*
1473 * Find a buffer by number or exact name.
1474 */
1475 static buf_T *
1476find_buffer(typval_T *avar)
1477{
1478 buf_T *buf = NULL;
1479
1480 if (avar->v_type == VAR_NUMBER)
1481 buf = buflist_findnr((int)avar->vval.v_number);
1482 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1483 {
1484 buf = buflist_findname_exp(avar->vval.v_string);
1485 if (buf == NULL)
1486 {
1487 /* No full path name match, try a match with a URL or a "nofile"
1488 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001489 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001490 if (buf->b_fname != NULL
1491 && (path_with_url(buf->b_fname)
1492#ifdef FEAT_QUICKFIX
1493 || bt_nofile(buf)
1494#endif
1495 )
1496 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1497 break;
1498 }
1499 }
1500 return buf;
1501}
1502
1503/*
1504 * "bufexists(expr)" function
1505 */
1506 static void
1507f_bufexists(typval_T *argvars, typval_T *rettv)
1508{
1509 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1510}
1511
1512/*
1513 * "buflisted(expr)" function
1514 */
1515 static void
1516f_buflisted(typval_T *argvars, typval_T *rettv)
1517{
1518 buf_T *buf;
1519
1520 buf = find_buffer(&argvars[0]);
1521 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1522}
1523
1524/*
1525 * "bufloaded(expr)" function
1526 */
1527 static void
1528f_bufloaded(typval_T *argvars, typval_T *rettv)
1529{
1530 buf_T *buf;
1531
1532 buf = find_buffer(&argvars[0]);
1533 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1534}
1535
1536 buf_T *
1537buflist_find_by_name(char_u *name, int curtab_only)
1538{
1539 int save_magic;
1540 char_u *save_cpo;
1541 buf_T *buf;
1542
1543 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1544 save_magic = p_magic;
1545 p_magic = TRUE;
1546 save_cpo = p_cpo;
1547 p_cpo = (char_u *)"";
1548
1549 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1550 TRUE, FALSE, curtab_only));
1551
1552 p_magic = save_magic;
1553 p_cpo = save_cpo;
1554 return buf;
1555}
1556
1557/*
1558 * Get buffer by number or pattern.
1559 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001560 buf_T *
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001561get_buf_tv(typval_T *tv, int curtab_only)
1562{
1563 char_u *name = tv->vval.v_string;
1564 buf_T *buf;
1565
1566 if (tv->v_type == VAR_NUMBER)
1567 return buflist_findnr((int)tv->vval.v_number);
1568 if (tv->v_type != VAR_STRING)
1569 return NULL;
1570 if (name == NULL || *name == NUL)
1571 return curbuf;
1572 if (name[0] == '$' && name[1] == NUL)
1573 return lastbuf;
1574
1575 buf = buflist_find_by_name(name, curtab_only);
1576
1577 /* If not found, try expanding the name, like done for bufexists(). */
1578 if (buf == NULL)
1579 buf = find_buffer(tv);
1580
1581 return buf;
1582}
1583
1584/*
1585 * "bufname(expr)" function
1586 */
1587 static void
1588f_bufname(typval_T *argvars, typval_T *rettv)
1589{
1590 buf_T *buf;
1591
1592 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1593 ++emsg_off;
1594 buf = get_buf_tv(&argvars[0], FALSE);
1595 rettv->v_type = VAR_STRING;
1596 if (buf != NULL && buf->b_fname != NULL)
1597 rettv->vval.v_string = vim_strsave(buf->b_fname);
1598 else
1599 rettv->vval.v_string = NULL;
1600 --emsg_off;
1601}
1602
1603/*
1604 * "bufnr(expr)" function
1605 */
1606 static void
1607f_bufnr(typval_T *argvars, typval_T *rettv)
1608{
1609 buf_T *buf;
1610 int error = FALSE;
1611 char_u *name;
1612
1613 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1614 ++emsg_off;
1615 buf = get_buf_tv(&argvars[0], FALSE);
1616 --emsg_off;
1617
1618 /* If the buffer isn't found and the second argument is not zero create a
1619 * new buffer. */
1620 if (buf == NULL
1621 && argvars[1].v_type != VAR_UNKNOWN
1622 && get_tv_number_chk(&argvars[1], &error) != 0
1623 && !error
1624 && (name = get_tv_string_chk(&argvars[0])) != NULL
1625 && !error)
1626 buf = buflist_new(name, NULL, (linenr_T)1, 0);
1627
1628 if (buf != NULL)
1629 rettv->vval.v_number = buf->b_fnum;
1630 else
1631 rettv->vval.v_number = -1;
1632}
1633
1634 static void
1635buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
1636{
1637#ifdef FEAT_WINDOWS
1638 win_T *wp;
1639 int winnr = 0;
1640#endif
1641 buf_T *buf;
1642
1643 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1644 ++emsg_off;
1645 buf = get_buf_tv(&argvars[0], TRUE);
1646#ifdef FEAT_WINDOWS
Bram Moolenaar29323592016-07-24 22:04:11 +02001647 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001648 {
1649 ++winnr;
1650 if (wp->w_buffer == buf)
1651 break;
1652 }
1653 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
1654#else
1655 rettv->vval.v_number = (curwin->w_buffer == buf
1656 ? (get_nr ? 1 : curwin->w_id) : -1);
1657#endif
1658 --emsg_off;
1659}
1660
1661/*
1662 * "bufwinid(nr)" function
1663 */
1664 static void
1665f_bufwinid(typval_T *argvars, typval_T *rettv)
1666{
1667 buf_win_common(argvars, rettv, FALSE);
1668}
1669
1670/*
1671 * "bufwinnr(nr)" function
1672 */
1673 static void
1674f_bufwinnr(typval_T *argvars, typval_T *rettv)
1675{
1676 buf_win_common(argvars, rettv, TRUE);
1677}
1678
1679/*
1680 * "byte2line(byte)" function
1681 */
1682 static void
1683f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1684{
1685#ifndef FEAT_BYTEOFF
1686 rettv->vval.v_number = -1;
1687#else
1688 long boff = 0;
1689
1690 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
1691 if (boff < 0)
1692 rettv->vval.v_number = -1;
1693 else
1694 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
1695 (linenr_T)0, &boff);
1696#endif
1697}
1698
1699 static void
1700byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
1701{
1702#ifdef FEAT_MBYTE
1703 char_u *t;
1704#endif
1705 char_u *str;
1706 varnumber_T idx;
1707
1708 str = get_tv_string_chk(&argvars[0]);
1709 idx = get_tv_number_chk(&argvars[1], NULL);
1710 rettv->vval.v_number = -1;
1711 if (str == NULL || idx < 0)
1712 return;
1713
1714#ifdef FEAT_MBYTE
1715 t = str;
1716 for ( ; idx > 0; idx--)
1717 {
1718 if (*t == NUL) /* EOL reached */
1719 return;
1720 if (enc_utf8 && comp)
1721 t += utf_ptr2len(t);
1722 else
1723 t += (*mb_ptr2len)(t);
1724 }
1725 rettv->vval.v_number = (varnumber_T)(t - str);
1726#else
1727 if ((size_t)idx <= STRLEN(str))
1728 rettv->vval.v_number = idx;
1729#endif
1730}
1731
1732/*
1733 * "byteidx()" function
1734 */
1735 static void
1736f_byteidx(typval_T *argvars, typval_T *rettv)
1737{
1738 byteidx(argvars, rettv, FALSE);
1739}
1740
1741/*
1742 * "byteidxcomp()" function
1743 */
1744 static void
1745f_byteidxcomp(typval_T *argvars, typval_T *rettv)
1746{
1747 byteidx(argvars, rettv, TRUE);
1748}
1749
1750/*
1751 * "call(func, arglist [, dict])" function
1752 */
1753 static void
1754f_call(typval_T *argvars, typval_T *rettv)
1755{
1756 char_u *func;
1757 partial_T *partial = NULL;
1758 dict_T *selfdict = NULL;
1759
1760 if (argvars[1].v_type != VAR_LIST)
1761 {
1762 EMSG(_(e_listreq));
1763 return;
1764 }
1765 if (argvars[1].vval.v_list == NULL)
1766 return;
1767
1768 if (argvars[0].v_type == VAR_FUNC)
1769 func = argvars[0].vval.v_string;
1770 else if (argvars[0].v_type == VAR_PARTIAL)
1771 {
1772 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001773 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001774 }
1775 else
1776 func = get_tv_string(&argvars[0]);
1777 if (*func == NUL)
1778 return; /* type error or empty name */
1779
1780 if (argvars[2].v_type != VAR_UNKNOWN)
1781 {
1782 if (argvars[2].v_type != VAR_DICT)
1783 {
1784 EMSG(_(e_dictreq));
1785 return;
1786 }
1787 selfdict = argvars[2].vval.v_dict;
1788 }
1789
1790 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
1791}
1792
1793#ifdef FEAT_FLOAT
1794/*
1795 * "ceil({float})" function
1796 */
1797 static void
1798f_ceil(typval_T *argvars, typval_T *rettv)
1799{
1800 float_T f = 0.0;
1801
1802 rettv->v_type = VAR_FLOAT;
1803 if (get_float_arg(argvars, &f) == OK)
1804 rettv->vval.v_float = ceil(f);
1805 else
1806 rettv->vval.v_float = 0.0;
1807}
1808#endif
1809
1810#ifdef FEAT_JOB_CHANNEL
1811/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01001812 * "ch_canread()" function
1813 */
1814 static void
1815f_ch_canread(typval_T *argvars, typval_T *rettv)
1816{
Bram Moolenaar958dc692016-12-01 15:34:12 +01001817 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01001818
1819 rettv->vval.v_number = 0;
1820 if (channel != NULL)
1821 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
1822 || channel_has_readahead(channel, PART_OUT)
1823 || channel_has_readahead(channel, PART_ERR);
1824}
1825
1826/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001827 * "ch_close()" function
1828 */
1829 static void
1830f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
1831{
1832 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
1833
1834 if (channel != NULL)
1835 {
1836 channel_close(channel, FALSE);
1837 channel_clear(channel);
1838 }
1839}
1840
1841/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02001842 * "ch_close()" function
1843 */
1844 static void
1845f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
1846{
1847 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
1848
1849 if (channel != NULL)
1850 channel_close_in(channel);
1851}
1852
1853/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001854 * "ch_getbufnr()" function
1855 */
1856 static void
1857f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
1858{
1859 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1860
1861 rettv->vval.v_number = -1;
1862 if (channel != NULL)
1863 {
1864 char_u *what = get_tv_string(&argvars[1]);
1865 int part;
1866
1867 if (STRCMP(what, "err") == 0)
1868 part = PART_ERR;
1869 else if (STRCMP(what, "out") == 0)
1870 part = PART_OUT;
1871 else if (STRCMP(what, "in") == 0)
1872 part = PART_IN;
1873 else
1874 part = PART_SOCK;
1875 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
1876 rettv->vval.v_number =
1877 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
1878 }
1879}
1880
1881/*
1882 * "ch_getjob()" function
1883 */
1884 static void
1885f_ch_getjob(typval_T *argvars, typval_T *rettv)
1886{
1887 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1888
1889 if (channel != NULL)
1890 {
1891 rettv->v_type = VAR_JOB;
1892 rettv->vval.v_job = channel->ch_job;
1893 if (channel->ch_job != NULL)
1894 ++channel->ch_job->jv_refcount;
1895 }
1896}
1897
1898/*
1899 * "ch_info()" function
1900 */
1901 static void
1902f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
1903{
1904 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1905
1906 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
1907 channel_info(channel, rettv->vval.v_dict);
1908}
1909
1910/*
1911 * "ch_log()" function
1912 */
1913 static void
1914f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
1915{
1916 char_u *msg = get_tv_string(&argvars[0]);
1917 channel_T *channel = NULL;
1918
1919 if (argvars[1].v_type != VAR_UNKNOWN)
1920 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
1921
1922 ch_log(channel, (char *)msg);
1923}
1924
1925/*
1926 * "ch_logfile()" function
1927 */
1928 static void
1929f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
1930{
1931 char_u *fname;
1932 char_u *opt = (char_u *)"";
1933 char_u buf[NUMBUFLEN];
1934
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02001935 /* Don't open a file in restricted mode. */
1936 if (check_restricted() || check_secure())
1937 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001938 fname = get_tv_string(&argvars[0]);
1939 if (argvars[1].v_type == VAR_STRING)
1940 opt = get_tv_string_buf(&argvars[1], buf);
1941 ch_logfile(fname, opt);
1942}
1943
1944/*
1945 * "ch_open()" function
1946 */
1947 static void
1948f_ch_open(typval_T *argvars, typval_T *rettv)
1949{
1950 rettv->v_type = VAR_CHANNEL;
1951 if (check_restricted() || check_secure())
1952 return;
1953 rettv->vval.v_channel = channel_open_func(argvars);
1954}
1955
1956/*
1957 * "ch_read()" function
1958 */
1959 static void
1960f_ch_read(typval_T *argvars, typval_T *rettv)
1961{
1962 common_channel_read(argvars, rettv, FALSE);
1963}
1964
1965/*
1966 * "ch_readraw()" function
1967 */
1968 static void
1969f_ch_readraw(typval_T *argvars, typval_T *rettv)
1970{
1971 common_channel_read(argvars, rettv, TRUE);
1972}
1973
1974/*
1975 * "ch_evalexpr()" function
1976 */
1977 static void
1978f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
1979{
1980 ch_expr_common(argvars, rettv, TRUE);
1981}
1982
1983/*
1984 * "ch_sendexpr()" function
1985 */
1986 static void
1987f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
1988{
1989 ch_expr_common(argvars, rettv, FALSE);
1990}
1991
1992/*
1993 * "ch_evalraw()" function
1994 */
1995 static void
1996f_ch_evalraw(typval_T *argvars, typval_T *rettv)
1997{
1998 ch_raw_common(argvars, rettv, TRUE);
1999}
2000
2001/*
2002 * "ch_sendraw()" function
2003 */
2004 static void
2005f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2006{
2007 ch_raw_common(argvars, rettv, FALSE);
2008}
2009
2010/*
2011 * "ch_setoptions()" function
2012 */
2013 static void
2014f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2015{
2016 channel_T *channel;
2017 jobopt_T opt;
2018
2019 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2020 if (channel == NULL)
2021 return;
2022 clear_job_options(&opt);
2023 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002024 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002025 channel_set_options(channel, &opt);
2026 free_job_options(&opt);
2027}
2028
2029/*
2030 * "ch_status()" function
2031 */
2032 static void
2033f_ch_status(typval_T *argvars, typval_T *rettv)
2034{
2035 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002036 jobopt_T opt;
2037 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002038
2039 /* return an empty string by default */
2040 rettv->v_type = VAR_STRING;
2041 rettv->vval.v_string = NULL;
2042
2043 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002044
2045 if (argvars[1].v_type != VAR_UNKNOWN)
2046 {
2047 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002048 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002049 && (opt.jo_set & JO_PART))
2050 part = opt.jo_part;
2051 }
2052
2053 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002054}
2055#endif
2056
2057/*
2058 * "changenr()" function
2059 */
2060 static void
2061f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2062{
2063 rettv->vval.v_number = curbuf->b_u_seq_cur;
2064}
2065
2066/*
2067 * "char2nr(string)" function
2068 */
2069 static void
2070f_char2nr(typval_T *argvars, typval_T *rettv)
2071{
2072#ifdef FEAT_MBYTE
2073 if (has_mbyte)
2074 {
2075 int utf8 = 0;
2076
2077 if (argvars[1].v_type != VAR_UNKNOWN)
2078 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
2079
2080 if (utf8)
2081 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
2082 else
2083 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
2084 }
2085 else
2086#endif
2087 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
2088}
2089
2090/*
2091 * "cindent(lnum)" function
2092 */
2093 static void
2094f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2095{
2096#ifdef FEAT_CINDENT
2097 pos_T pos;
2098 linenr_T lnum;
2099
2100 pos = curwin->w_cursor;
2101 lnum = get_tv_lnum(argvars);
2102 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2103 {
2104 curwin->w_cursor.lnum = lnum;
2105 rettv->vval.v_number = get_c_indent();
2106 curwin->w_cursor = pos;
2107 }
2108 else
2109#endif
2110 rettv->vval.v_number = -1;
2111}
2112
2113/*
2114 * "clearmatches()" function
2115 */
2116 static void
2117f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2118{
2119#ifdef FEAT_SEARCH_EXTRA
2120 clear_matches(curwin);
2121#endif
2122}
2123
2124/*
2125 * "col(string)" function
2126 */
2127 static void
2128f_col(typval_T *argvars, typval_T *rettv)
2129{
2130 colnr_T col = 0;
2131 pos_T *fp;
2132 int fnum = curbuf->b_fnum;
2133
2134 fp = var2fpos(&argvars[0], FALSE, &fnum);
2135 if (fp != NULL && fnum == curbuf->b_fnum)
2136 {
2137 if (fp->col == MAXCOL)
2138 {
2139 /* '> can be MAXCOL, get the length of the line then */
2140 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2141 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2142 else
2143 col = MAXCOL;
2144 }
2145 else
2146 {
2147 col = fp->col + 1;
2148#ifdef FEAT_VIRTUALEDIT
2149 /* col(".") when the cursor is on the NUL at the end of the line
2150 * because of "coladd" can be seen as an extra column. */
2151 if (virtual_active() && fp == &curwin->w_cursor)
2152 {
2153 char_u *p = ml_get_cursor();
2154
2155 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2156 curwin->w_virtcol - curwin->w_cursor.coladd))
2157 {
2158# ifdef FEAT_MBYTE
2159 int l;
2160
2161 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2162 col += l;
2163# else
2164 if (*p != NUL && p[1] == NUL)
2165 ++col;
2166# endif
2167 }
2168 }
2169#endif
2170 }
2171 }
2172 rettv->vval.v_number = col;
2173}
2174
2175#if defined(FEAT_INS_EXPAND)
2176/*
2177 * "complete()" function
2178 */
2179 static void
2180f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2181{
2182 int startcol;
2183
2184 if ((State & INSERT) == 0)
2185 {
2186 EMSG(_("E785: complete() can only be used in Insert mode"));
2187 return;
2188 }
2189
2190 /* Check for undo allowed here, because if something was already inserted
2191 * the line was already saved for undo and this check isn't done. */
2192 if (!undo_allowed())
2193 return;
2194
2195 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2196 {
2197 EMSG(_(e_invarg));
2198 return;
2199 }
2200
2201 startcol = (int)get_tv_number_chk(&argvars[0], NULL);
2202 if (startcol <= 0)
2203 return;
2204
2205 set_completion(startcol - 1, argvars[1].vval.v_list);
2206}
2207
2208/*
2209 * "complete_add()" function
2210 */
2211 static void
2212f_complete_add(typval_T *argvars, typval_T *rettv)
2213{
2214 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2215}
2216
2217/*
2218 * "complete_check()" function
2219 */
2220 static void
2221f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2222{
2223 int saved = RedrawingDisabled;
2224
2225 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002226 ins_compl_check_keys(0, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002227 rettv->vval.v_number = compl_interrupted;
2228 RedrawingDisabled = saved;
2229}
2230#endif
2231
2232/*
2233 * "confirm(message, buttons[, default [, type]])" function
2234 */
2235 static void
2236f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2237{
2238#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2239 char_u *message;
2240 char_u *buttons = NULL;
2241 char_u buf[NUMBUFLEN];
2242 char_u buf2[NUMBUFLEN];
2243 int def = 1;
2244 int type = VIM_GENERIC;
2245 char_u *typestr;
2246 int error = FALSE;
2247
2248 message = get_tv_string_chk(&argvars[0]);
2249 if (message == NULL)
2250 error = TRUE;
2251 if (argvars[1].v_type != VAR_UNKNOWN)
2252 {
2253 buttons = get_tv_string_buf_chk(&argvars[1], buf);
2254 if (buttons == NULL)
2255 error = TRUE;
2256 if (argvars[2].v_type != VAR_UNKNOWN)
2257 {
2258 def = (int)get_tv_number_chk(&argvars[2], &error);
2259 if (argvars[3].v_type != VAR_UNKNOWN)
2260 {
2261 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
2262 if (typestr == NULL)
2263 error = TRUE;
2264 else
2265 {
2266 switch (TOUPPER_ASC(*typestr))
2267 {
2268 case 'E': type = VIM_ERROR; break;
2269 case 'Q': type = VIM_QUESTION; break;
2270 case 'I': type = VIM_INFO; break;
2271 case 'W': type = VIM_WARNING; break;
2272 case 'G': type = VIM_GENERIC; break;
2273 }
2274 }
2275 }
2276 }
2277 }
2278
2279 if (buttons == NULL || *buttons == NUL)
2280 buttons = (char_u *)_("&Ok");
2281
2282 if (!error)
2283 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2284 def, NULL, FALSE);
2285#endif
2286}
2287
2288/*
2289 * "copy()" function
2290 */
2291 static void
2292f_copy(typval_T *argvars, typval_T *rettv)
2293{
2294 item_copy(&argvars[0], rettv, FALSE, 0);
2295}
2296
2297#ifdef FEAT_FLOAT
2298/*
2299 * "cos()" function
2300 */
2301 static void
2302f_cos(typval_T *argvars, typval_T *rettv)
2303{
2304 float_T f = 0.0;
2305
2306 rettv->v_type = VAR_FLOAT;
2307 if (get_float_arg(argvars, &f) == OK)
2308 rettv->vval.v_float = cos(f);
2309 else
2310 rettv->vval.v_float = 0.0;
2311}
2312
2313/*
2314 * "cosh()" function
2315 */
2316 static void
2317f_cosh(typval_T *argvars, typval_T *rettv)
2318{
2319 float_T f = 0.0;
2320
2321 rettv->v_type = VAR_FLOAT;
2322 if (get_float_arg(argvars, &f) == OK)
2323 rettv->vval.v_float = cosh(f);
2324 else
2325 rettv->vval.v_float = 0.0;
2326}
2327#endif
2328
2329/*
2330 * "count()" function
2331 */
2332 static void
2333f_count(typval_T *argvars, typval_T *rettv)
2334{
2335 long n = 0;
2336 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002337 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002338
Bram Moolenaar9966b212017-07-28 16:46:57 +02002339 if (argvars[2].v_type != VAR_UNKNOWN)
2340 ic = (int)get_tv_number_chk(&argvars[2], &error);
2341
2342 if (argvars[0].v_type == VAR_STRING)
2343 {
2344 char_u *expr = get_tv_string_chk(&argvars[1]);
2345 char_u *p = argvars[0].vval.v_string;
2346 char_u *next;
2347
2348 if (!error && expr != NULL && p != NULL)
2349 {
2350 if (ic)
2351 {
2352 size_t len = STRLEN(expr);
2353
2354 while (*p != NUL)
2355 {
2356 if (MB_STRNICMP(p, expr, len) == 0)
2357 {
2358 ++n;
2359 p += len;
2360 }
2361 else
2362 MB_PTR_ADV(p);
2363 }
2364 }
2365 else
2366 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2367 != NULL)
2368 {
2369 ++n;
2370 p = next + STRLEN(expr);
2371 }
2372 }
2373
2374 }
2375 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002376 {
2377 listitem_T *li;
2378 list_T *l;
2379 long idx;
2380
2381 if ((l = argvars[0].vval.v_list) != NULL)
2382 {
2383 li = l->lv_first;
2384 if (argvars[2].v_type != VAR_UNKNOWN)
2385 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002386 if (argvars[3].v_type != VAR_UNKNOWN)
2387 {
2388 idx = (long)get_tv_number_chk(&argvars[3], &error);
2389 if (!error)
2390 {
2391 li = list_find(l, idx);
2392 if (li == NULL)
2393 EMSGN(_(e_listidx), idx);
2394 }
2395 }
2396 if (error)
2397 li = NULL;
2398 }
2399
2400 for ( ; li != NULL; li = li->li_next)
2401 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2402 ++n;
2403 }
2404 }
2405 else if (argvars[0].v_type == VAR_DICT)
2406 {
2407 int todo;
2408 dict_T *d;
2409 hashitem_T *hi;
2410
2411 if ((d = argvars[0].vval.v_dict) != NULL)
2412 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002413 if (argvars[2].v_type != VAR_UNKNOWN)
2414 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002415 if (argvars[3].v_type != VAR_UNKNOWN)
2416 EMSG(_(e_invarg));
2417 }
2418
2419 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2420 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2421 {
2422 if (!HASHITEM_EMPTY(hi))
2423 {
2424 --todo;
2425 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2426 ++n;
2427 }
2428 }
2429 }
2430 }
2431 else
2432 EMSG2(_(e_listdictarg), "count()");
2433 rettv->vval.v_number = n;
2434}
2435
2436/*
2437 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2438 *
2439 * Checks the existence of a cscope connection.
2440 */
2441 static void
2442f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2443{
2444#ifdef FEAT_CSCOPE
2445 int num = 0;
2446 char_u *dbpath = NULL;
2447 char_u *prepend = NULL;
2448 char_u buf[NUMBUFLEN];
2449
2450 if (argvars[0].v_type != VAR_UNKNOWN
2451 && argvars[1].v_type != VAR_UNKNOWN)
2452 {
2453 num = (int)get_tv_number(&argvars[0]);
2454 dbpath = get_tv_string(&argvars[1]);
2455 if (argvars[2].v_type != VAR_UNKNOWN)
2456 prepend = get_tv_string_buf(&argvars[2], buf);
2457 }
2458
2459 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2460#endif
2461}
2462
2463/*
2464 * "cursor(lnum, col)" function, or
2465 * "cursor(list)"
2466 *
2467 * Moves the cursor to the specified line and column.
2468 * Returns 0 when the position could be set, -1 otherwise.
2469 */
2470 static void
2471f_cursor(typval_T *argvars, typval_T *rettv)
2472{
2473 long line, col;
2474#ifdef FEAT_VIRTUALEDIT
2475 long coladd = 0;
2476#endif
2477 int set_curswant = TRUE;
2478
2479 rettv->vval.v_number = -1;
2480 if (argvars[1].v_type == VAR_UNKNOWN)
2481 {
2482 pos_T pos;
2483 colnr_T curswant = -1;
2484
2485 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2486 {
2487 EMSG(_(e_invarg));
2488 return;
2489 }
2490 line = pos.lnum;
2491 col = pos.col;
2492#ifdef FEAT_VIRTUALEDIT
2493 coladd = pos.coladd;
2494#endif
2495 if (curswant >= 0)
2496 {
2497 curwin->w_curswant = curswant - 1;
2498 set_curswant = FALSE;
2499 }
2500 }
2501 else
2502 {
2503 line = get_tv_lnum(argvars);
2504 col = (long)get_tv_number_chk(&argvars[1], NULL);
2505#ifdef FEAT_VIRTUALEDIT
2506 if (argvars[2].v_type != VAR_UNKNOWN)
2507 coladd = (long)get_tv_number_chk(&argvars[2], NULL);
2508#endif
2509 }
2510 if (line < 0 || col < 0
2511#ifdef FEAT_VIRTUALEDIT
2512 || coladd < 0
2513#endif
2514 )
2515 return; /* type error; errmsg already given */
2516 if (line > 0)
2517 curwin->w_cursor.lnum = line;
2518 if (col > 0)
2519 curwin->w_cursor.col = col - 1;
2520#ifdef FEAT_VIRTUALEDIT
2521 curwin->w_cursor.coladd = coladd;
2522#endif
2523
2524 /* Make sure the cursor is in a valid position. */
2525 check_cursor();
2526#ifdef FEAT_MBYTE
2527 /* Correct cursor for multi-byte character. */
2528 if (has_mbyte)
2529 mb_adjust_cursor();
2530#endif
2531
2532 curwin->w_set_curswant = set_curswant;
2533 rettv->vval.v_number = 0;
2534}
2535
2536/*
2537 * "deepcopy()" function
2538 */
2539 static void
2540f_deepcopy(typval_T *argvars, typval_T *rettv)
2541{
2542 int noref = 0;
2543 int copyID;
2544
2545 if (argvars[1].v_type != VAR_UNKNOWN)
2546 noref = (int)get_tv_number_chk(&argvars[1], NULL);
2547 if (noref < 0 || noref > 1)
2548 EMSG(_(e_invarg));
2549 else
2550 {
2551 copyID = get_copyID();
2552 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2553 }
2554}
2555
2556/*
2557 * "delete()" function
2558 */
2559 static void
2560f_delete(typval_T *argvars, typval_T *rettv)
2561{
2562 char_u nbuf[NUMBUFLEN];
2563 char_u *name;
2564 char_u *flags;
2565
2566 rettv->vval.v_number = -1;
2567 if (check_restricted() || check_secure())
2568 return;
2569
2570 name = get_tv_string(&argvars[0]);
2571 if (name == NULL || *name == NUL)
2572 {
2573 EMSG(_(e_invarg));
2574 return;
2575 }
2576
2577 if (argvars[1].v_type != VAR_UNKNOWN)
2578 flags = get_tv_string_buf(&argvars[1], nbuf);
2579 else
2580 flags = (char_u *)"";
2581
2582 if (*flags == NUL)
2583 /* delete a file */
2584 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2585 else if (STRCMP(flags, "d") == 0)
2586 /* delete an empty directory */
2587 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2588 else if (STRCMP(flags, "rf") == 0)
2589 /* delete a directory recursively */
2590 rettv->vval.v_number = delete_recursive(name);
2591 else
2592 EMSG2(_(e_invexpr2), flags);
2593}
2594
2595/*
2596 * "did_filetype()" function
2597 */
2598 static void
2599f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2600{
2601#ifdef FEAT_AUTOCMD
2602 rettv->vval.v_number = did_filetype;
2603#endif
2604}
2605
2606/*
2607 * "diff_filler()" function
2608 */
2609 static void
2610f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2611{
2612#ifdef FEAT_DIFF
2613 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
2614#endif
2615}
2616
2617/*
2618 * "diff_hlID()" function
2619 */
2620 static void
2621f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2622{
2623#ifdef FEAT_DIFF
2624 linenr_T lnum = get_tv_lnum(argvars);
2625 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01002626 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002627 static int fnum = 0;
2628 static int change_start = 0;
2629 static int change_end = 0;
2630 static hlf_T hlID = (hlf_T)0;
2631 int filler_lines;
2632 int col;
2633
2634 if (lnum < 0) /* ignore type error in {lnum} arg */
2635 lnum = 0;
2636 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002637 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002638 || fnum != curbuf->b_fnum)
2639 {
2640 /* New line, buffer, change: need to get the values. */
2641 filler_lines = diff_check(curwin, lnum);
2642 if (filler_lines < 0)
2643 {
2644 if (filler_lines == -1)
2645 {
2646 change_start = MAXCOL;
2647 change_end = -1;
2648 if (diff_find_change(curwin, lnum, &change_start, &change_end))
2649 hlID = HLF_ADD; /* added line */
2650 else
2651 hlID = HLF_CHD; /* changed line */
2652 }
2653 else
2654 hlID = HLF_ADD; /* added line */
2655 }
2656 else
2657 hlID = (hlf_T)0;
2658 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002659 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002660 fnum = curbuf->b_fnum;
2661 }
2662
2663 if (hlID == HLF_CHD || hlID == HLF_TXD)
2664 {
2665 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
2666 if (col >= change_start && col <= change_end)
2667 hlID = HLF_TXD; /* changed text */
2668 else
2669 hlID = HLF_CHD; /* changed line */
2670 }
2671 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
2672#endif
2673}
2674
2675/*
2676 * "empty({expr})" function
2677 */
2678 static void
2679f_empty(typval_T *argvars, typval_T *rettv)
2680{
2681 int n = FALSE;
2682
2683 switch (argvars[0].v_type)
2684 {
2685 case VAR_STRING:
2686 case VAR_FUNC:
2687 n = argvars[0].vval.v_string == NULL
2688 || *argvars[0].vval.v_string == NUL;
2689 break;
2690 case VAR_PARTIAL:
2691 n = FALSE;
2692 break;
2693 case VAR_NUMBER:
2694 n = argvars[0].vval.v_number == 0;
2695 break;
2696 case VAR_FLOAT:
2697#ifdef FEAT_FLOAT
2698 n = argvars[0].vval.v_float == 0.0;
2699 break;
2700#endif
2701 case VAR_LIST:
2702 n = argvars[0].vval.v_list == NULL
2703 || argvars[0].vval.v_list->lv_first == NULL;
2704 break;
2705 case VAR_DICT:
2706 n = argvars[0].vval.v_dict == NULL
2707 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2708 break;
2709 case VAR_SPECIAL:
2710 n = argvars[0].vval.v_number != VVAL_TRUE;
2711 break;
2712
2713 case VAR_JOB:
2714#ifdef FEAT_JOB_CHANNEL
2715 n = argvars[0].vval.v_job == NULL
2716 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2717 break;
2718#endif
2719 case VAR_CHANNEL:
2720#ifdef FEAT_JOB_CHANNEL
2721 n = argvars[0].vval.v_channel == NULL
2722 || !channel_is_open(argvars[0].vval.v_channel);
2723 break;
2724#endif
2725 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01002726 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002727 n = TRUE;
2728 break;
2729 }
2730
2731 rettv->vval.v_number = n;
2732}
2733
2734/*
2735 * "escape({string}, {chars})" function
2736 */
2737 static void
2738f_escape(typval_T *argvars, typval_T *rettv)
2739{
2740 char_u buf[NUMBUFLEN];
2741
2742 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
2743 get_tv_string_buf(&argvars[1], buf));
2744 rettv->v_type = VAR_STRING;
2745}
2746
2747/*
2748 * "eval()" function
2749 */
2750 static void
2751f_eval(typval_T *argvars, typval_T *rettv)
2752{
2753 char_u *s, *p;
2754
2755 s = get_tv_string_chk(&argvars[0]);
2756 if (s != NULL)
2757 s = skipwhite(s);
2758
2759 p = s;
2760 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
2761 {
2762 if (p != NULL && !aborting())
2763 EMSG2(_(e_invexpr2), p);
2764 need_clr_eos = FALSE;
2765 rettv->v_type = VAR_NUMBER;
2766 rettv->vval.v_number = 0;
2767 }
2768 else if (*s != NUL)
2769 EMSG(_(e_trailing));
2770}
2771
2772/*
2773 * "eventhandler()" function
2774 */
2775 static void
2776f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
2777{
2778 rettv->vval.v_number = vgetc_busy;
2779}
2780
2781/*
2782 * "executable()" function
2783 */
2784 static void
2785f_executable(typval_T *argvars, typval_T *rettv)
2786{
2787 char_u *name = get_tv_string(&argvars[0]);
2788
2789 /* Check in $PATH and also check directly if there is a directory name. */
2790 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
2791 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
2792}
2793
2794static garray_T redir_execute_ga;
2795
2796/*
2797 * Append "value[value_len]" to the execute() output.
2798 */
2799 void
2800execute_redir_str(char_u *value, int value_len)
2801{
2802 int len;
2803
2804 if (value_len == -1)
2805 len = (int)STRLEN(value); /* Append the entire string */
2806 else
2807 len = value_len; /* Append only "value_len" characters */
2808 if (ga_grow(&redir_execute_ga, len) == OK)
2809 {
2810 mch_memmove((char *)redir_execute_ga.ga_data
2811 + redir_execute_ga.ga_len, value, len);
2812 redir_execute_ga.ga_len += len;
2813 }
2814}
2815
2816/*
2817 * Get next line from a list.
2818 * Called by do_cmdline() to get the next line.
2819 * Returns allocated string, or NULL for end of function.
2820 */
2821
2822 static char_u *
2823get_list_line(
2824 int c UNUSED,
2825 void *cookie,
2826 int indent UNUSED)
2827{
2828 listitem_T **p = (listitem_T **)cookie;
2829 listitem_T *item = *p;
2830 char_u buf[NUMBUFLEN];
2831 char_u *s;
2832
2833 if (item == NULL)
2834 return NULL;
2835 s = get_tv_string_buf_chk(&item->li_tv, buf);
2836 *p = item->li_next;
2837 return s == NULL ? NULL : vim_strsave(s);
2838}
2839
2840/*
2841 * "execute()" function
2842 */
2843 static void
2844f_execute(typval_T *argvars, typval_T *rettv)
2845{
2846 char_u *cmd = NULL;
2847 list_T *list = NULL;
2848 int save_msg_silent = msg_silent;
2849 int save_emsg_silent = emsg_silent;
2850 int save_emsg_noredir = emsg_noredir;
2851 int save_redir_execute = redir_execute;
2852 garray_T save_ga;
2853
2854 rettv->vval.v_string = NULL;
2855 rettv->v_type = VAR_STRING;
2856
2857 if (argvars[0].v_type == VAR_LIST)
2858 {
2859 list = argvars[0].vval.v_list;
2860 if (list == NULL || list->lv_first == NULL)
2861 /* empty list, no commands, empty output */
2862 return;
2863 ++list->lv_refcount;
2864 }
2865 else
2866 {
2867 cmd = get_tv_string_chk(&argvars[0]);
2868 if (cmd == NULL)
2869 return;
2870 }
2871
2872 if (argvars[1].v_type != VAR_UNKNOWN)
2873 {
2874 char_u buf[NUMBUFLEN];
2875 char_u *s = get_tv_string_buf_chk(&argvars[1], buf);
2876
2877 if (s == NULL)
2878 return;
2879 if (STRNCMP(s, "silent", 6) == 0)
2880 ++msg_silent;
2881 if (STRCMP(s, "silent!") == 0)
2882 {
2883 emsg_silent = TRUE;
2884 emsg_noredir = TRUE;
2885 }
2886 }
2887 else
2888 ++msg_silent;
2889
2890 if (redir_execute)
2891 save_ga = redir_execute_ga;
2892 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
2893 redir_execute = TRUE;
2894
2895 if (cmd != NULL)
2896 do_cmdline_cmd(cmd);
2897 else
2898 {
2899 listitem_T *item = list->lv_first;
2900
2901 do_cmdline(NULL, get_list_line, (void *)&item,
2902 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
2903 --list->lv_refcount;
2904 }
2905
Bram Moolenaard297f352017-01-29 20:31:21 +01002906 /* Need to append a NUL to the result. */
2907 if (ga_grow(&redir_execute_ga, 1) == OK)
2908 {
2909 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
2910 rettv->vval.v_string = redir_execute_ga.ga_data;
2911 }
2912 else
2913 {
2914 ga_clear(&redir_execute_ga);
2915 rettv->vval.v_string = NULL;
2916 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002917 msg_silent = save_msg_silent;
2918 emsg_silent = save_emsg_silent;
2919 emsg_noredir = save_emsg_noredir;
2920
2921 redir_execute = save_redir_execute;
2922 if (redir_execute)
2923 redir_execute_ga = save_ga;
2924
2925 /* "silent reg" or "silent echo x" leaves msg_col somewhere in the
2926 * line. Put it back in the first column. */
2927 msg_col = 0;
2928}
2929
2930/*
2931 * "exepath()" function
2932 */
2933 static void
2934f_exepath(typval_T *argvars, typval_T *rettv)
2935{
2936 char_u *p = NULL;
2937
2938 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
2939 rettv->v_type = VAR_STRING;
2940 rettv->vval.v_string = p;
2941}
2942
2943/*
2944 * "exists()" function
2945 */
2946 static void
2947f_exists(typval_T *argvars, typval_T *rettv)
2948{
2949 char_u *p;
2950 char_u *name;
2951 int n = FALSE;
2952 int len = 0;
2953
2954 p = get_tv_string(&argvars[0]);
2955 if (*p == '$') /* environment variable */
2956 {
2957 /* first try "normal" environment variables (fast) */
2958 if (mch_getenv(p + 1) != NULL)
2959 n = TRUE;
2960 else
2961 {
2962 /* try expanding things like $VIM and ${HOME} */
2963 p = expand_env_save(p);
2964 if (p != NULL && *p != '$')
2965 n = TRUE;
2966 vim_free(p);
2967 }
2968 }
2969 else if (*p == '&' || *p == '+') /* option */
2970 {
2971 n = (get_option_tv(&p, NULL, TRUE) == OK);
2972 if (*skipwhite(p) != NUL)
2973 n = FALSE; /* trailing garbage */
2974 }
2975 else if (*p == '*') /* internal or user defined function */
2976 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002977 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002978 }
2979 else if (*p == ':')
2980 {
2981 n = cmd_exists(p + 1);
2982 }
2983 else if (*p == '#')
2984 {
2985#ifdef FEAT_AUTOCMD
2986 if (p[1] == '#')
2987 n = autocmd_supported(p + 2);
2988 else
2989 n = au_exists(p + 1);
2990#endif
2991 }
2992 else /* internal variable */
2993 {
2994 char_u *tofree;
2995 typval_T tv;
2996
2997 /* get_name_len() takes care of expanding curly braces */
2998 name = p;
2999 len = get_name_len(&p, &tofree, TRUE, FALSE);
3000 if (len > 0)
3001 {
3002 if (tofree != NULL)
3003 name = tofree;
3004 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
3005 if (n)
3006 {
3007 /* handle d.key, l[idx], f(expr) */
3008 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
3009 if (n)
3010 clear_tv(&tv);
3011 }
3012 }
3013 if (*p != NUL)
3014 n = FALSE;
3015
3016 vim_free(tofree);
3017 }
3018
3019 rettv->vval.v_number = n;
3020}
3021
3022#ifdef FEAT_FLOAT
3023/*
3024 * "exp()" function
3025 */
3026 static void
3027f_exp(typval_T *argvars, typval_T *rettv)
3028{
3029 float_T f = 0.0;
3030
3031 rettv->v_type = VAR_FLOAT;
3032 if (get_float_arg(argvars, &f) == OK)
3033 rettv->vval.v_float = exp(f);
3034 else
3035 rettv->vval.v_float = 0.0;
3036}
3037#endif
3038
3039/*
3040 * "expand()" function
3041 */
3042 static void
3043f_expand(typval_T *argvars, typval_T *rettv)
3044{
3045 char_u *s;
3046 int len;
3047 char_u *errormsg;
3048 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3049 expand_T xpc;
3050 int error = FALSE;
3051 char_u *result;
3052
3053 rettv->v_type = VAR_STRING;
3054 if (argvars[1].v_type != VAR_UNKNOWN
3055 && argvars[2].v_type != VAR_UNKNOWN
3056 && get_tv_number_chk(&argvars[2], &error)
3057 && !error)
3058 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003059 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003060 }
3061
3062 s = get_tv_string(&argvars[0]);
3063 if (*s == '%' || *s == '#' || *s == '<')
3064 {
3065 ++emsg_off;
3066 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3067 --emsg_off;
3068 if (rettv->v_type == VAR_LIST)
3069 {
3070 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3071 list_append_string(rettv->vval.v_list, result, -1);
3072 else
3073 vim_free(result);
3074 }
3075 else
3076 rettv->vval.v_string = result;
3077 }
3078 else
3079 {
3080 /* When the optional second argument is non-zero, don't remove matches
3081 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3082 if (argvars[1].v_type != VAR_UNKNOWN
3083 && get_tv_number_chk(&argvars[1], &error))
3084 options |= WILD_KEEP_ALL;
3085 if (!error)
3086 {
3087 ExpandInit(&xpc);
3088 xpc.xp_context = EXPAND_FILES;
3089 if (p_wic)
3090 options += WILD_ICASE;
3091 if (rettv->v_type == VAR_STRING)
3092 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3093 options, WILD_ALL);
3094 else if (rettv_list_alloc(rettv) != FAIL)
3095 {
3096 int i;
3097
3098 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3099 for (i = 0; i < xpc.xp_numfiles; i++)
3100 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3101 ExpandCleanup(&xpc);
3102 }
3103 }
3104 else
3105 rettv->vval.v_string = NULL;
3106 }
3107}
3108
3109/*
3110 * "extend(list, list [, idx])" function
3111 * "extend(dict, dict [, action])" function
3112 */
3113 static void
3114f_extend(typval_T *argvars, typval_T *rettv)
3115{
3116 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3117
3118 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3119 {
3120 list_T *l1, *l2;
3121 listitem_T *item;
3122 long before;
3123 int error = FALSE;
3124
3125 l1 = argvars[0].vval.v_list;
3126 l2 = argvars[1].vval.v_list;
3127 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
3128 && l2 != NULL)
3129 {
3130 if (argvars[2].v_type != VAR_UNKNOWN)
3131 {
3132 before = (long)get_tv_number_chk(&argvars[2], &error);
3133 if (error)
3134 return; /* type error; errmsg already given */
3135
3136 if (before == l1->lv_len)
3137 item = NULL;
3138 else
3139 {
3140 item = list_find(l1, before);
3141 if (item == NULL)
3142 {
3143 EMSGN(_(e_listidx), before);
3144 return;
3145 }
3146 }
3147 }
3148 else
3149 item = NULL;
3150 list_extend(l1, l2, item);
3151
3152 copy_tv(&argvars[0], rettv);
3153 }
3154 }
3155 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3156 {
3157 dict_T *d1, *d2;
3158 char_u *action;
3159 int i;
3160
3161 d1 = argvars[0].vval.v_dict;
3162 d2 = argvars[1].vval.v_dict;
3163 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
3164 && d2 != NULL)
3165 {
3166 /* Check the third argument. */
3167 if (argvars[2].v_type != VAR_UNKNOWN)
3168 {
3169 static char *(av[]) = {"keep", "force", "error"};
3170
3171 action = get_tv_string_chk(&argvars[2]);
3172 if (action == NULL)
3173 return; /* type error; errmsg already given */
3174 for (i = 0; i < 3; ++i)
3175 if (STRCMP(action, av[i]) == 0)
3176 break;
3177 if (i == 3)
3178 {
3179 EMSG2(_(e_invarg2), action);
3180 return;
3181 }
3182 }
3183 else
3184 action = (char_u *)"force";
3185
3186 dict_extend(d1, d2, action);
3187
3188 copy_tv(&argvars[0], rettv);
3189 }
3190 }
3191 else
3192 EMSG2(_(e_listdictarg), "extend()");
3193}
3194
3195/*
3196 * "feedkeys()" function
3197 */
3198 static void
3199f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3200{
3201 int remap = TRUE;
3202 int insert = FALSE;
3203 char_u *keys, *flags;
3204 char_u nbuf[NUMBUFLEN];
3205 int typed = FALSE;
3206 int execute = FALSE;
3207 int dangerous = FALSE;
3208 char_u *keys_esc;
3209
3210 /* This is not allowed in the sandbox. If the commands would still be
3211 * executed in the sandbox it would be OK, but it probably happens later,
3212 * when "sandbox" is no longer set. */
3213 if (check_secure())
3214 return;
3215
3216 keys = get_tv_string(&argvars[0]);
3217
3218 if (argvars[1].v_type != VAR_UNKNOWN)
3219 {
3220 flags = get_tv_string_buf(&argvars[1], nbuf);
3221 for ( ; *flags != NUL; ++flags)
3222 {
3223 switch (*flags)
3224 {
3225 case 'n': remap = FALSE; break;
3226 case 'm': remap = TRUE; break;
3227 case 't': typed = TRUE; break;
3228 case 'i': insert = TRUE; break;
3229 case 'x': execute = TRUE; break;
3230 case '!': dangerous = TRUE; break;
3231 }
3232 }
3233 }
3234
3235 if (*keys != NUL || execute)
3236 {
3237 /* Need to escape K_SPECIAL and CSI before putting the string in the
3238 * typeahead buffer. */
3239 keys_esc = vim_strsave_escape_csi(keys);
3240 if (keys_esc != NULL)
3241 {
3242 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
3243 insert ? 0 : typebuf.tb_len, !typed, FALSE);
3244 vim_free(keys_esc);
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003245 if (vgetc_busy
3246#ifdef FEAT_TIMERS
3247 || timer_busy
3248#endif
3249 )
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003250 typebuf_was_filled = TRUE;
3251 if (execute)
3252 {
3253 int save_msg_scroll = msg_scroll;
3254
3255 /* Avoid a 1 second delay when the keys start Insert mode. */
3256 msg_scroll = FALSE;
3257
3258 if (!dangerous)
3259 ++ex_normal_busy;
3260 exec_normal(TRUE);
3261 if (!dangerous)
3262 --ex_normal_busy;
3263 msg_scroll |= save_msg_scroll;
3264 }
3265 }
3266 }
3267}
3268
3269/*
3270 * "filereadable()" function
3271 */
3272 static void
3273f_filereadable(typval_T *argvars, typval_T *rettv)
3274{
3275 int fd;
3276 char_u *p;
3277 int n;
3278
3279#ifndef O_NONBLOCK
3280# define O_NONBLOCK 0
3281#endif
3282 p = get_tv_string(&argvars[0]);
3283 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3284 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3285 {
3286 n = TRUE;
3287 close(fd);
3288 }
3289 else
3290 n = FALSE;
3291
3292 rettv->vval.v_number = n;
3293}
3294
3295/*
3296 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3297 * rights to write into.
3298 */
3299 static void
3300f_filewritable(typval_T *argvars, typval_T *rettv)
3301{
3302 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
3303}
3304
3305 static void
3306findfilendir(
3307 typval_T *argvars UNUSED,
3308 typval_T *rettv,
3309 int find_what UNUSED)
3310{
3311#ifdef FEAT_SEARCHPATH
3312 char_u *fname;
3313 char_u *fresult = NULL;
3314 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3315 char_u *p;
3316 char_u pathbuf[NUMBUFLEN];
3317 int count = 1;
3318 int first = TRUE;
3319 int error = FALSE;
3320#endif
3321
3322 rettv->vval.v_string = NULL;
3323 rettv->v_type = VAR_STRING;
3324
3325#ifdef FEAT_SEARCHPATH
3326 fname = get_tv_string(&argvars[0]);
3327
3328 if (argvars[1].v_type != VAR_UNKNOWN)
3329 {
3330 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
3331 if (p == NULL)
3332 error = TRUE;
3333 else
3334 {
3335 if (*p != NUL)
3336 path = p;
3337
3338 if (argvars[2].v_type != VAR_UNKNOWN)
3339 count = (int)get_tv_number_chk(&argvars[2], &error);
3340 }
3341 }
3342
3343 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3344 error = TRUE;
3345
3346 if (*fname != NUL && !error)
3347 {
3348 do
3349 {
3350 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3351 vim_free(fresult);
3352 fresult = find_file_in_path_option(first ? fname : NULL,
3353 first ? (int)STRLEN(fname) : 0,
3354 0, first, path,
3355 find_what,
3356 curbuf->b_ffname,
3357 find_what == FINDFILE_DIR
3358 ? (char_u *)"" : curbuf->b_p_sua);
3359 first = FALSE;
3360
3361 if (fresult != NULL && rettv->v_type == VAR_LIST)
3362 list_append_string(rettv->vval.v_list, fresult, -1);
3363
3364 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3365 }
3366
3367 if (rettv->v_type == VAR_STRING)
3368 rettv->vval.v_string = fresult;
3369#endif
3370}
3371
3372/*
3373 * "filter()" function
3374 */
3375 static void
3376f_filter(typval_T *argvars, typval_T *rettv)
3377{
3378 filter_map(argvars, rettv, FALSE);
3379}
3380
3381/*
3382 * "finddir({fname}[, {path}[, {count}]])" function
3383 */
3384 static void
3385f_finddir(typval_T *argvars, typval_T *rettv)
3386{
3387 findfilendir(argvars, rettv, FINDFILE_DIR);
3388}
3389
3390/*
3391 * "findfile({fname}[, {path}[, {count}]])" function
3392 */
3393 static void
3394f_findfile(typval_T *argvars, typval_T *rettv)
3395{
3396 findfilendir(argvars, rettv, FINDFILE_FILE);
3397}
3398
3399#ifdef FEAT_FLOAT
3400/*
3401 * "float2nr({float})" function
3402 */
3403 static void
3404f_float2nr(typval_T *argvars, typval_T *rettv)
3405{
3406 float_T f = 0.0;
3407
3408 if (get_float_arg(argvars, &f) == OK)
3409 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003410 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003411 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003412 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003413 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003414 else
3415 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003416 }
3417}
3418
3419/*
3420 * "floor({float})" function
3421 */
3422 static void
3423f_floor(typval_T *argvars, typval_T *rettv)
3424{
3425 float_T f = 0.0;
3426
3427 rettv->v_type = VAR_FLOAT;
3428 if (get_float_arg(argvars, &f) == OK)
3429 rettv->vval.v_float = floor(f);
3430 else
3431 rettv->vval.v_float = 0.0;
3432}
3433
3434/*
3435 * "fmod()" function
3436 */
3437 static void
3438f_fmod(typval_T *argvars, typval_T *rettv)
3439{
3440 float_T fx = 0.0, fy = 0.0;
3441
3442 rettv->v_type = VAR_FLOAT;
3443 if (get_float_arg(argvars, &fx) == OK
3444 && get_float_arg(&argvars[1], &fy) == OK)
3445 rettv->vval.v_float = fmod(fx, fy);
3446 else
3447 rettv->vval.v_float = 0.0;
3448}
3449#endif
3450
3451/*
3452 * "fnameescape({string})" function
3453 */
3454 static void
3455f_fnameescape(typval_T *argvars, typval_T *rettv)
3456{
3457 rettv->vval.v_string = vim_strsave_fnameescape(
3458 get_tv_string(&argvars[0]), FALSE);
3459 rettv->v_type = VAR_STRING;
3460}
3461
3462/*
3463 * "fnamemodify({fname}, {mods})" function
3464 */
3465 static void
3466f_fnamemodify(typval_T *argvars, typval_T *rettv)
3467{
3468 char_u *fname;
3469 char_u *mods;
3470 int usedlen = 0;
3471 int len;
3472 char_u *fbuf = NULL;
3473 char_u buf[NUMBUFLEN];
3474
3475 fname = get_tv_string_chk(&argvars[0]);
3476 mods = get_tv_string_buf_chk(&argvars[1], buf);
3477 if (fname == NULL || mods == NULL)
3478 fname = NULL;
3479 else
3480 {
3481 len = (int)STRLEN(fname);
3482 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
3483 }
3484
3485 rettv->v_type = VAR_STRING;
3486 if (fname == NULL)
3487 rettv->vval.v_string = NULL;
3488 else
3489 rettv->vval.v_string = vim_strnsave(fname, len);
3490 vim_free(fbuf);
3491}
3492
3493static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
3494
3495/*
3496 * "foldclosed()" function
3497 */
3498 static void
3499foldclosed_both(
3500 typval_T *argvars UNUSED,
3501 typval_T *rettv,
3502 int end UNUSED)
3503{
3504#ifdef FEAT_FOLDING
3505 linenr_T lnum;
3506 linenr_T first, last;
3507
3508 lnum = get_tv_lnum(argvars);
3509 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3510 {
3511 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3512 {
3513 if (end)
3514 rettv->vval.v_number = (varnumber_T)last;
3515 else
3516 rettv->vval.v_number = (varnumber_T)first;
3517 return;
3518 }
3519 }
3520#endif
3521 rettv->vval.v_number = -1;
3522}
3523
3524/*
3525 * "foldclosed()" function
3526 */
3527 static void
3528f_foldclosed(typval_T *argvars, typval_T *rettv)
3529{
3530 foldclosed_both(argvars, rettv, FALSE);
3531}
3532
3533/*
3534 * "foldclosedend()" function
3535 */
3536 static void
3537f_foldclosedend(typval_T *argvars, typval_T *rettv)
3538{
3539 foldclosed_both(argvars, rettv, TRUE);
3540}
3541
3542/*
3543 * "foldlevel()" function
3544 */
3545 static void
3546f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3547{
3548#ifdef FEAT_FOLDING
3549 linenr_T lnum;
3550
3551 lnum = get_tv_lnum(argvars);
3552 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3553 rettv->vval.v_number = foldLevel(lnum);
3554#endif
3555}
3556
3557/*
3558 * "foldtext()" function
3559 */
3560 static void
3561f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
3562{
3563#ifdef FEAT_FOLDING
3564 linenr_T foldstart;
3565 linenr_T foldend;
3566 char_u *dashes;
3567 linenr_T lnum;
3568 char_u *s;
3569 char_u *r;
3570 int len;
3571 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003572 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003573#endif
3574
3575 rettv->v_type = VAR_STRING;
3576 rettv->vval.v_string = NULL;
3577#ifdef FEAT_FOLDING
3578 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
3579 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
3580 dashes = get_vim_var_str(VV_FOLDDASHES);
3581 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
3582 && dashes != NULL)
3583 {
3584 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02003585 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003586 if (!linewhite(lnum))
3587 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003588
3589 /* Find interesting text in this line. */
3590 s = skipwhite(ml_get(lnum));
3591 /* skip C comment-start */
3592 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
3593 {
3594 s = skipwhite(s + 2);
3595 if (*skipwhite(s) == NUL
3596 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
3597 {
3598 s = skipwhite(ml_get(lnum + 1));
3599 if (*s == '*')
3600 s = skipwhite(s + 1);
3601 }
3602 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02003603 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003604 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003605 r = alloc((unsigned)(STRLEN(txt)
3606 + STRLEN(dashes) /* for %s */
3607 + 20 /* for %3ld */
3608 + STRLEN(s))); /* concatenated */
3609 if (r != NULL)
3610 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02003611 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003612 len = (int)STRLEN(r);
3613 STRCAT(r, s);
3614 /* remove 'foldmarker' and 'commentstring' */
3615 foldtext_cleanup(r + len);
3616 rettv->vval.v_string = r;
3617 }
3618 }
3619#endif
3620}
3621
3622/*
3623 * "foldtextresult(lnum)" function
3624 */
3625 static void
3626f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
3627{
3628#ifdef FEAT_FOLDING
3629 linenr_T lnum;
3630 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003631 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003632 foldinfo_T foldinfo;
3633 int fold_count;
3634#endif
3635
3636 rettv->v_type = VAR_STRING;
3637 rettv->vval.v_string = NULL;
3638#ifdef FEAT_FOLDING
3639 lnum = get_tv_lnum(argvars);
3640 /* treat illegal types and illegal string values for {lnum} the same */
3641 if (lnum < 0)
3642 lnum = 0;
3643 fold_count = foldedCount(curwin, lnum, &foldinfo);
3644 if (fold_count > 0)
3645 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01003646 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
3647 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003648 if (text == buf)
3649 text = vim_strsave(text);
3650 rettv->vval.v_string = text;
3651 }
3652#endif
3653}
3654
3655/*
3656 * "foreground()" function
3657 */
3658 static void
3659f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3660{
3661#ifdef FEAT_GUI
3662 if (gui.in_use)
3663 gui_mch_set_foreground();
3664#else
3665# ifdef WIN32
3666 win32_set_foreground();
3667# endif
3668#endif
3669}
3670
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003671 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003672common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003673{
3674 char_u *s;
3675 char_u *name;
3676 int use_string = FALSE;
3677 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003678 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003679
3680 if (argvars[0].v_type == VAR_FUNC)
3681 {
3682 /* function(MyFunc, [arg], dict) */
3683 s = argvars[0].vval.v_string;
3684 }
3685 else if (argvars[0].v_type == VAR_PARTIAL
3686 && argvars[0].vval.v_partial != NULL)
3687 {
3688 /* function(dict.MyFunc, [arg]) */
3689 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003690 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003691 }
3692 else
3693 {
3694 /* function('MyFunc', [arg], dict) */
3695 s = get_tv_string(&argvars[0]);
3696 use_string = TRUE;
3697 }
3698
Bram Moolenaar843b8842016-08-21 14:36:15 +02003699 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003700 {
3701 name = s;
3702 trans_name = trans_function_name(&name, FALSE,
3703 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
3704 if (*name != NUL)
3705 s = NULL;
3706 }
3707
Bram Moolenaar843b8842016-08-21 14:36:15 +02003708 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
3709 || (is_funcref && trans_name == NULL))
Bram Moolenaar5582ef12016-09-14 22:16:13 +02003710 EMSG2(_(e_invarg2), use_string ? get_tv_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003711 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003712 else if (trans_name != NULL && (is_funcref
3713 ? find_func(trans_name) == NULL
3714 : !translated_function_exists(trans_name)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003715 EMSG2(_("E700: Unknown function: %s"), s);
3716 else
3717 {
3718 int dict_idx = 0;
3719 int arg_idx = 0;
3720 list_T *list = NULL;
3721
3722 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
3723 {
3724 char sid_buf[25];
3725 int off = *s == 's' ? 2 : 5;
3726
3727 /* Expand s: and <SID> into <SNR>nr_, so that the function can
3728 * also be called from another script. Using trans_function_name()
3729 * would also work, but some plugins depend on the name being
3730 * printable text. */
3731 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
3732 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
3733 if (name != NULL)
3734 {
3735 STRCPY(name, sid_buf);
3736 STRCAT(name, s + off);
3737 }
3738 }
3739 else
3740 name = vim_strsave(s);
3741
3742 if (argvars[1].v_type != VAR_UNKNOWN)
3743 {
3744 if (argvars[2].v_type != VAR_UNKNOWN)
3745 {
3746 /* function(name, [args], dict) */
3747 arg_idx = 1;
3748 dict_idx = 2;
3749 }
3750 else if (argvars[1].v_type == VAR_DICT)
3751 /* function(name, dict) */
3752 dict_idx = 1;
3753 else
3754 /* function(name, [args]) */
3755 arg_idx = 1;
3756 if (dict_idx > 0)
3757 {
3758 if (argvars[dict_idx].v_type != VAR_DICT)
3759 {
3760 EMSG(_("E922: expected a dict"));
3761 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003762 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003763 }
3764 if (argvars[dict_idx].vval.v_dict == NULL)
3765 dict_idx = 0;
3766 }
3767 if (arg_idx > 0)
3768 {
3769 if (argvars[arg_idx].v_type != VAR_LIST)
3770 {
3771 EMSG(_("E923: Second argument of function() must be a list or 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 list = argvars[arg_idx].vval.v_list;
3776 if (list == NULL || list->lv_len == 0)
3777 arg_idx = 0;
3778 }
3779 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003780 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003781 {
3782 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
3783
3784 /* result is a VAR_PARTIAL */
3785 if (pt == NULL)
3786 vim_free(name);
3787 else
3788 {
3789 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
3790 {
3791 listitem_T *li;
3792 int i = 0;
3793 int arg_len = 0;
3794 int lv_len = 0;
3795
3796 if (arg_pt != NULL)
3797 arg_len = arg_pt->pt_argc;
3798 if (list != NULL)
3799 lv_len = list->lv_len;
3800 pt->pt_argc = arg_len + lv_len;
3801 pt->pt_argv = (typval_T *)alloc(
3802 sizeof(typval_T) * pt->pt_argc);
3803 if (pt->pt_argv == NULL)
3804 {
3805 vim_free(pt);
3806 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003807 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003808 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003809 for (i = 0; i < arg_len; i++)
3810 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
3811 if (lv_len > 0)
3812 for (li = list->lv_first; li != NULL;
3813 li = li->li_next)
3814 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003815 }
3816
3817 /* For "function(dict.func, [], dict)" and "func" is a partial
3818 * use "dict". That is backwards compatible. */
3819 if (dict_idx > 0)
3820 {
3821 /* The dict is bound explicitly, pt_auto is FALSE. */
3822 pt->pt_dict = argvars[dict_idx].vval.v_dict;
3823 ++pt->pt_dict->dv_refcount;
3824 }
3825 else if (arg_pt != NULL)
3826 {
3827 /* If the dict was bound automatically the result is also
3828 * bound automatically. */
3829 pt->pt_dict = arg_pt->pt_dict;
3830 pt->pt_auto = arg_pt->pt_auto;
3831 if (pt->pt_dict != NULL)
3832 ++pt->pt_dict->dv_refcount;
3833 }
3834
3835 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003836 if (arg_pt != NULL && arg_pt->pt_func != NULL)
3837 {
3838 pt->pt_func = arg_pt->pt_func;
3839 func_ptr_ref(pt->pt_func);
3840 vim_free(name);
3841 }
3842 else if (is_funcref)
3843 {
3844 pt->pt_func = find_func(trans_name);
3845 func_ptr_ref(pt->pt_func);
3846 vim_free(name);
3847 }
3848 else
3849 {
3850 pt->pt_name = name;
3851 func_ref(name);
3852 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003853 }
3854 rettv->v_type = VAR_PARTIAL;
3855 rettv->vval.v_partial = pt;
3856 }
3857 else
3858 {
3859 /* result is a VAR_FUNC */
3860 rettv->v_type = VAR_FUNC;
3861 rettv->vval.v_string = name;
3862 func_ref(name);
3863 }
3864 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003865theend:
3866 vim_free(trans_name);
3867}
3868
3869/*
3870 * "funcref()" function
3871 */
3872 static void
3873f_funcref(typval_T *argvars, typval_T *rettv)
3874{
3875 common_function(argvars, rettv, TRUE);
3876}
3877
3878/*
3879 * "function()" function
3880 */
3881 static void
3882f_function(typval_T *argvars, typval_T *rettv)
3883{
3884 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003885}
3886
3887/*
3888 * "garbagecollect()" function
3889 */
3890 static void
3891f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
3892{
3893 /* This is postponed until we are back at the toplevel, because we may be
3894 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
3895 want_garbage_collect = TRUE;
3896
3897 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
3898 garbage_collect_at_exit = TRUE;
3899}
3900
3901/*
3902 * "get()" function
3903 */
3904 static void
3905f_get(typval_T *argvars, typval_T *rettv)
3906{
3907 listitem_T *li;
3908 list_T *l;
3909 dictitem_T *di;
3910 dict_T *d;
3911 typval_T *tv = NULL;
3912
3913 if (argvars[0].v_type == VAR_LIST)
3914 {
3915 if ((l = argvars[0].vval.v_list) != NULL)
3916 {
3917 int error = FALSE;
3918
3919 li = list_find(l, (long)get_tv_number_chk(&argvars[1], &error));
3920 if (!error && li != NULL)
3921 tv = &li->li_tv;
3922 }
3923 }
3924 else if (argvars[0].v_type == VAR_DICT)
3925 {
3926 if ((d = argvars[0].vval.v_dict) != NULL)
3927 {
3928 di = dict_find(d, get_tv_string(&argvars[1]), -1);
3929 if (di != NULL)
3930 tv = &di->di_tv;
3931 }
3932 }
3933 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
3934 {
3935 partial_T *pt;
3936 partial_T fref_pt;
3937
3938 if (argvars[0].v_type == VAR_PARTIAL)
3939 pt = argvars[0].vval.v_partial;
3940 else
3941 {
3942 vim_memset(&fref_pt, 0, sizeof(fref_pt));
3943 fref_pt.pt_name = argvars[0].vval.v_string;
3944 pt = &fref_pt;
3945 }
3946
3947 if (pt != NULL)
3948 {
3949 char_u *what = get_tv_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003950 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003951
3952 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
3953 {
3954 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003955 n = partial_name(pt);
3956 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003957 rettv->vval.v_string = NULL;
3958 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003959 {
3960 rettv->vval.v_string = vim_strsave(n);
3961 if (rettv->v_type == VAR_FUNC)
3962 func_ref(rettv->vval.v_string);
3963 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003964 }
3965 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003966 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003967 else if (STRCMP(what, "args") == 0)
3968 {
3969 rettv->v_type = VAR_LIST;
3970 if (rettv_list_alloc(rettv) == OK)
3971 {
3972 int i;
3973
3974 for (i = 0; i < pt->pt_argc; ++i)
3975 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
3976 }
3977 }
3978 else
3979 EMSG2(_(e_invarg2), what);
3980 return;
3981 }
3982 }
3983 else
3984 EMSG2(_(e_listdictarg), "get()");
3985
3986 if (tv == NULL)
3987 {
3988 if (argvars[2].v_type != VAR_UNKNOWN)
3989 copy_tv(&argvars[2], rettv);
3990 }
3991 else
3992 copy_tv(tv, rettv);
3993}
3994
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003995#ifdef FEAT_SIGNS
3996/*
3997 * Returns information about signs placed in a buffer as list of dicts.
3998 */
3999 static void
4000get_buffer_signs(buf_T *buf, list_T *l)
4001{
4002 signlist_T *sign;
4003
4004 for (sign = buf->b_signlist; sign; sign = sign->next)
4005 {
4006 dict_T *d = dict_alloc();
4007
4008 if (d != NULL)
4009 {
4010 dict_add_nr_str(d, "id", sign->id, NULL);
4011 dict_add_nr_str(d, "lnum", sign->lnum, NULL);
Bram Moolenaar6a402ed2016-08-28 14:11:24 +02004012 dict_add_nr_str(d, "name", 0L, sign_typenr2name(sign->typenr));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004013
4014 list_append_dict(l, d);
4015 }
4016 }
4017}
4018#endif
4019
4020/*
4021 * Returns buffer options, variables and other attributes in a dictionary.
4022 */
4023 static dict_T *
4024get_buffer_info(buf_T *buf)
4025{
4026 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004027 tabpage_T *tp;
4028 win_T *wp;
4029 list_T *windows;
4030
4031 dict = dict_alloc();
4032 if (dict == NULL)
4033 return NULL;
4034
Bram Moolenaar33928832016-08-18 21:22:04 +02004035 dict_add_nr_str(dict, "bufnr", buf->b_fnum, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004036 dict_add_nr_str(dict, "name", 0L,
4037 buf->b_ffname != NULL ? buf->b_ffname : (char_u *)"");
Bram Moolenaarf845b872017-01-06 14:04:54 +01004038 dict_add_nr_str(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4039 : buflist_findlnum(buf), NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004040 dict_add_nr_str(dict, "loaded", buf->b_ml.ml_mfp != NULL, NULL);
4041 dict_add_nr_str(dict, "listed", buf->b_p_bl, NULL);
4042 dict_add_nr_str(dict, "changed", bufIsChanged(buf), NULL);
Bram Moolenaar95c526e2017-02-25 14:59:34 +01004043 dict_add_nr_str(dict, "changedtick", CHANGEDTICK(buf), NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004044 dict_add_nr_str(dict, "hidden",
4045 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0,
4046 NULL);
4047
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004048 /* Get a reference to buffer variables */
4049 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004050
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004051 /* List of windows displaying this buffer */
4052 windows = list_alloc();
4053 if (windows != NULL)
4054 {
4055 FOR_ALL_TAB_WINDOWS(tp, wp)
4056 if (wp->w_buffer == buf)
4057 list_append_number(windows, (varnumber_T)wp->w_id);
4058 dict_add_list(dict, "windows", windows);
4059 }
4060
4061#ifdef FEAT_SIGNS
4062 if (buf->b_signlist != NULL)
4063 {
4064 /* List of signs placed in this buffer */
4065 list_T *signs = list_alloc();
4066 if (signs != NULL)
4067 {
4068 get_buffer_signs(buf, signs);
4069 dict_add_list(dict, "signs", signs);
4070 }
4071 }
4072#endif
4073
4074 return dict;
4075}
4076
4077/*
4078 * "getbufinfo()" function
4079 */
4080 static void
4081f_getbufinfo(typval_T *argvars, typval_T *rettv)
4082{
4083 buf_T *buf = NULL;
4084 buf_T *argbuf = NULL;
4085 dict_T *d;
4086 int filtered = FALSE;
4087 int sel_buflisted = FALSE;
4088 int sel_bufloaded = FALSE;
4089
4090 if (rettv_list_alloc(rettv) != OK)
4091 return;
4092
4093 /* List of all the buffers or selected buffers */
4094 if (argvars[0].v_type == VAR_DICT)
4095 {
4096 dict_T *sel_d = argvars[0].vval.v_dict;
4097
4098 if (sel_d != NULL)
4099 {
4100 dictitem_T *di;
4101
4102 filtered = TRUE;
4103
4104 di = dict_find(sel_d, (char_u *)"buflisted", -1);
4105 if (di != NULL && get_tv_number(&di->di_tv))
4106 sel_buflisted = TRUE;
4107
4108 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
4109 if (di != NULL && get_tv_number(&di->di_tv))
4110 sel_bufloaded = TRUE;
4111 }
4112 }
4113 else if (argvars[0].v_type != VAR_UNKNOWN)
4114 {
4115 /* Information about one buffer. Argument specifies the buffer */
4116 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4117 ++emsg_off;
4118 argbuf = get_buf_tv(&argvars[0], FALSE);
4119 --emsg_off;
4120 if (argbuf == NULL)
4121 return;
4122 }
4123
4124 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004125 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004126 {
4127 if (argbuf != NULL && argbuf != buf)
4128 continue;
4129 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
4130 || (sel_buflisted && !buf->b_p_bl)))
4131 continue;
4132
4133 d = get_buffer_info(buf);
4134 if (d != NULL)
4135 list_append_dict(rettv->vval.v_list, d);
4136 if (argbuf != NULL)
4137 return;
4138 }
4139}
4140
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004141static void get_buffer_lines(buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv);
4142
4143/*
4144 * Get line or list of lines from buffer "buf" into "rettv".
4145 * Return a range (from start to end) of lines in rettv from the specified
4146 * buffer.
4147 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4148 */
4149 static void
4150get_buffer_lines(
4151 buf_T *buf,
4152 linenr_T start,
4153 linenr_T end,
4154 int retlist,
4155 typval_T *rettv)
4156{
4157 char_u *p;
4158
4159 rettv->v_type = VAR_STRING;
4160 rettv->vval.v_string = NULL;
4161 if (retlist && rettv_list_alloc(rettv) == FAIL)
4162 return;
4163
4164 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4165 return;
4166
4167 if (!retlist)
4168 {
4169 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4170 p = ml_get_buf(buf, start, FALSE);
4171 else
4172 p = (char_u *)"";
4173 rettv->vval.v_string = vim_strsave(p);
4174 }
4175 else
4176 {
4177 if (end < start)
4178 return;
4179
4180 if (start < 1)
4181 start = 1;
4182 if (end > buf->b_ml.ml_line_count)
4183 end = buf->b_ml.ml_line_count;
4184 while (start <= end)
4185 if (list_append_string(rettv->vval.v_list,
4186 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4187 break;
4188 }
4189}
4190
4191/*
4192 * Get the lnum from the first argument.
4193 * Also accepts "$", then "buf" is used.
4194 * Returns 0 on error.
4195 */
4196 static linenr_T
4197get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
4198{
4199 if (argvars[0].v_type == VAR_STRING
4200 && argvars[0].vval.v_string != NULL
4201 && argvars[0].vval.v_string[0] == '$'
4202 && buf != NULL)
4203 return buf->b_ml.ml_line_count;
4204 return (linenr_T)get_tv_number_chk(&argvars[0], NULL);
4205}
4206
4207/*
4208 * "getbufline()" function
4209 */
4210 static void
4211f_getbufline(typval_T *argvars, typval_T *rettv)
4212{
4213 linenr_T lnum;
4214 linenr_T end;
4215 buf_T *buf;
4216
4217 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4218 ++emsg_off;
4219 buf = get_buf_tv(&argvars[0], FALSE);
4220 --emsg_off;
4221
4222 lnum = get_tv_lnum_buf(&argvars[1], buf);
4223 if (argvars[2].v_type == VAR_UNKNOWN)
4224 end = lnum;
4225 else
4226 end = get_tv_lnum_buf(&argvars[2], buf);
4227
4228 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4229}
4230
4231/*
4232 * "getbufvar()" function
4233 */
4234 static void
4235f_getbufvar(typval_T *argvars, typval_T *rettv)
4236{
4237 buf_T *buf;
4238 buf_T *save_curbuf;
4239 char_u *varname;
4240 dictitem_T *v;
4241 int done = FALSE;
4242
4243 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4244 varname = get_tv_string_chk(&argvars[1]);
4245 ++emsg_off;
4246 buf = get_buf_tv(&argvars[0], FALSE);
4247
4248 rettv->v_type = VAR_STRING;
4249 rettv->vval.v_string = NULL;
4250
4251 if (buf != NULL && varname != NULL)
4252 {
4253 /* set curbuf to be our buf, temporarily */
4254 save_curbuf = curbuf;
4255 curbuf = buf;
4256
Bram Moolenaar30567352016-08-27 21:25:44 +02004257 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004258 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004259 if (varname[1] == NUL)
4260 {
4261 /* get all buffer-local options in a dict */
4262 dict_T *opts = get_winbuf_options(TRUE);
4263
4264 if (opts != NULL)
4265 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004266 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004267 done = TRUE;
4268 }
4269 }
4270 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4271 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004272 done = TRUE;
4273 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004274 else
4275 {
4276 /* Look up the variable. */
4277 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4278 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4279 'b', varname, FALSE);
4280 if (v != NULL)
4281 {
4282 copy_tv(&v->di_tv, rettv);
4283 done = TRUE;
4284 }
4285 }
4286
4287 /* restore previous notion of curbuf */
4288 curbuf = save_curbuf;
4289 }
4290
4291 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4292 /* use the default value */
4293 copy_tv(&argvars[2], rettv);
4294
4295 --emsg_off;
4296}
4297
4298/*
4299 * "getchar()" function
4300 */
4301 static void
4302f_getchar(typval_T *argvars, typval_T *rettv)
4303{
4304 varnumber_T n;
4305 int error = FALSE;
4306
4307 /* Position the cursor. Needed after a message that ends in a space. */
4308 windgoto(msg_row, msg_col);
4309
4310 ++no_mapping;
4311 ++allow_keys;
4312 for (;;)
4313 {
4314 if (argvars[0].v_type == VAR_UNKNOWN)
4315 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004316 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004317 else if (get_tv_number_chk(&argvars[0], &error) == 1)
4318 /* getchar(1): only check if char avail */
4319 n = vpeekc_any();
4320 else if (error || vpeekc_any() == NUL)
4321 /* illegal argument or getchar(0) and no char avail: return zero */
4322 n = 0;
4323 else
4324 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004325 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004326
4327 if (n == K_IGNORE)
4328 continue;
4329 break;
4330 }
4331 --no_mapping;
4332 --allow_keys;
4333
4334 set_vim_var_nr(VV_MOUSE_WIN, 0);
4335 set_vim_var_nr(VV_MOUSE_WINID, 0);
4336 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4337 set_vim_var_nr(VV_MOUSE_COL, 0);
4338
4339 rettv->vval.v_number = n;
4340 if (IS_SPECIAL(n) || mod_mask != 0)
4341 {
4342 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4343 int i = 0;
4344
4345 /* Turn a special key into three bytes, plus modifier. */
4346 if (mod_mask != 0)
4347 {
4348 temp[i++] = K_SPECIAL;
4349 temp[i++] = KS_MODIFIER;
4350 temp[i++] = mod_mask;
4351 }
4352 if (IS_SPECIAL(n))
4353 {
4354 temp[i++] = K_SPECIAL;
4355 temp[i++] = K_SECOND(n);
4356 temp[i++] = K_THIRD(n);
4357 }
4358#ifdef FEAT_MBYTE
4359 else if (has_mbyte)
4360 i += (*mb_char2bytes)(n, temp + i);
4361#endif
4362 else
4363 temp[i++] = n;
4364 temp[i++] = NUL;
4365 rettv->v_type = VAR_STRING;
4366 rettv->vval.v_string = vim_strsave(temp);
4367
4368#ifdef FEAT_MOUSE
4369 if (is_mouse_key(n))
4370 {
4371 int row = mouse_row;
4372 int col = mouse_col;
4373 win_T *win;
4374 linenr_T lnum;
4375# ifdef FEAT_WINDOWS
4376 win_T *wp;
4377# endif
4378 int winnr = 1;
4379
4380 if (row >= 0 && col >= 0)
4381 {
4382 /* Find the window at the mouse coordinates and compute the
4383 * text position. */
4384 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004385 if (win == NULL)
4386 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004387 (void)mouse_comp_pos(win, &row, &col, &lnum);
4388# ifdef FEAT_WINDOWS
4389 for (wp = firstwin; wp != win; wp = wp->w_next)
4390 ++winnr;
4391# endif
4392 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4393 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4394 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4395 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4396 }
4397 }
4398#endif
4399 }
4400}
4401
4402/*
4403 * "getcharmod()" function
4404 */
4405 static void
4406f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4407{
4408 rettv->vval.v_number = mod_mask;
4409}
4410
4411/*
4412 * "getcharsearch()" function
4413 */
4414 static void
4415f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4416{
4417 if (rettv_dict_alloc(rettv) != FAIL)
4418 {
4419 dict_T *dict = rettv->vval.v_dict;
4420
4421 dict_add_nr_str(dict, "char", 0L, last_csearch());
4422 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
4423 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
4424 }
4425}
4426
4427/*
4428 * "getcmdline()" function
4429 */
4430 static void
4431f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4432{
4433 rettv->v_type = VAR_STRING;
4434 rettv->vval.v_string = get_cmdline_str();
4435}
4436
4437/*
4438 * "getcmdpos()" function
4439 */
4440 static void
4441f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4442{
4443 rettv->vval.v_number = get_cmdline_pos() + 1;
4444}
4445
4446/*
4447 * "getcmdtype()" function
4448 */
4449 static void
4450f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4451{
4452 rettv->v_type = VAR_STRING;
4453 rettv->vval.v_string = alloc(2);
4454 if (rettv->vval.v_string != NULL)
4455 {
4456 rettv->vval.v_string[0] = get_cmdline_type();
4457 rettv->vval.v_string[1] = NUL;
4458 }
4459}
4460
4461/*
4462 * "getcmdwintype()" function
4463 */
4464 static void
4465f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4466{
4467 rettv->v_type = VAR_STRING;
4468 rettv->vval.v_string = NULL;
4469#ifdef FEAT_CMDWIN
4470 rettv->vval.v_string = alloc(2);
4471 if (rettv->vval.v_string != NULL)
4472 {
4473 rettv->vval.v_string[0] = cmdwin_type;
4474 rettv->vval.v_string[1] = NUL;
4475 }
4476#endif
4477}
4478
4479#if defined(FEAT_CMDL_COMPL)
4480/*
4481 * "getcompletion()" function
4482 */
4483 static void
4484f_getcompletion(typval_T *argvars, typval_T *rettv)
4485{
4486 char_u *pat;
4487 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004488 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004489 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
4490 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004491
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004492 if (argvars[2].v_type != VAR_UNKNOWN)
4493 filtered = get_tv_number_chk(&argvars[2], NULL);
4494
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004495 if (p_wic)
4496 options |= WILD_ICASE;
4497
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004498 /* For filtered results, 'wildignore' is used */
4499 if (!filtered)
4500 options |= WILD_KEEP_ALL;
4501
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004502 ExpandInit(&xpc);
4503 xpc.xp_pattern = get_tv_string(&argvars[0]);
4504 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4505 xpc.xp_context = cmdcomplete_str_to_type(get_tv_string(&argvars[1]));
4506 if (xpc.xp_context == EXPAND_NOTHING)
4507 {
4508 if (argvars[1].v_type == VAR_STRING)
4509 EMSG2(_(e_invarg2), argvars[1].vval.v_string);
4510 else
4511 EMSG(_(e_invarg));
4512 return;
4513 }
4514
4515# if defined(FEAT_MENU)
4516 if (xpc.xp_context == EXPAND_MENUS)
4517 {
4518 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4519 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4520 }
4521# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02004522#ifdef FEAT_CSCOPE
4523 if (xpc.xp_context == EXPAND_CSCOPE)
4524 {
4525 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4526 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4527 }
4528#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02004529#ifdef FEAT_SIGNS
4530 if (xpc.xp_context == EXPAND_SIGN)
4531 {
4532 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4533 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4534 }
4535#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004536
4537 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4538 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
4539 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004540 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004541
4542 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4543
4544 for (i = 0; i < xpc.xp_numfiles; i++)
4545 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4546 }
4547 vim_free(pat);
4548 ExpandCleanup(&xpc);
4549}
4550#endif
4551
4552/*
4553 * "getcwd()" function
4554 */
4555 static void
4556f_getcwd(typval_T *argvars, typval_T *rettv)
4557{
4558 win_T *wp = NULL;
4559 char_u *cwd;
4560
4561 rettv->v_type = VAR_STRING;
4562 rettv->vval.v_string = NULL;
4563
4564 wp = find_tabwin(&argvars[0], &argvars[1]);
4565 if (wp != NULL)
4566 {
4567 if (wp->w_localdir != NULL)
4568 rettv->vval.v_string = vim_strsave(wp->w_localdir);
4569 else if (globaldir != NULL)
4570 rettv->vval.v_string = vim_strsave(globaldir);
4571 else
4572 {
4573 cwd = alloc(MAXPATHL);
4574 if (cwd != NULL)
4575 {
4576 if (mch_dirname(cwd, MAXPATHL) != FAIL)
4577 rettv->vval.v_string = vim_strsave(cwd);
4578 vim_free(cwd);
4579 }
4580 }
4581#ifdef BACKSLASH_IN_FILENAME
4582 if (rettv->vval.v_string != NULL)
4583 slash_adjust(rettv->vval.v_string);
4584#endif
4585 }
4586}
4587
4588/*
4589 * "getfontname()" function
4590 */
4591 static void
4592f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4593{
4594 rettv->v_type = VAR_STRING;
4595 rettv->vval.v_string = NULL;
4596#ifdef FEAT_GUI
4597 if (gui.in_use)
4598 {
4599 GuiFont font;
4600 char_u *name = NULL;
4601
4602 if (argvars[0].v_type == VAR_UNKNOWN)
4603 {
4604 /* Get the "Normal" font. Either the name saved by
4605 * hl_set_font_name() or from the font ID. */
4606 font = gui.norm_font;
4607 name = hl_get_font_name();
4608 }
4609 else
4610 {
4611 name = get_tv_string(&argvars[0]);
4612 if (STRCMP(name, "*") == 0) /* don't use font dialog */
4613 return;
4614 font = gui_mch_get_font(name, FALSE);
4615 if (font == NOFONT)
4616 return; /* Invalid font name, return empty string. */
4617 }
4618 rettv->vval.v_string = gui_mch_get_fontname(font, name);
4619 if (argvars[0].v_type != VAR_UNKNOWN)
4620 gui_mch_free_font(font);
4621 }
4622#endif
4623}
4624
4625/*
4626 * "getfperm({fname})" function
4627 */
4628 static void
4629f_getfperm(typval_T *argvars, typval_T *rettv)
4630{
4631 char_u *fname;
4632 stat_T st;
4633 char_u *perm = NULL;
4634 char_u flags[] = "rwx";
4635 int i;
4636
4637 fname = get_tv_string(&argvars[0]);
4638
4639 rettv->v_type = VAR_STRING;
4640 if (mch_stat((char *)fname, &st) >= 0)
4641 {
4642 perm = vim_strsave((char_u *)"---------");
4643 if (perm != NULL)
4644 {
4645 for (i = 0; i < 9; i++)
4646 {
4647 if (st.st_mode & (1 << (8 - i)))
4648 perm[i] = flags[i % 3];
4649 }
4650 }
4651 }
4652 rettv->vval.v_string = perm;
4653}
4654
4655/*
4656 * "getfsize({fname})" function
4657 */
4658 static void
4659f_getfsize(typval_T *argvars, typval_T *rettv)
4660{
4661 char_u *fname;
4662 stat_T st;
4663
4664 fname = get_tv_string(&argvars[0]);
4665
4666 rettv->v_type = VAR_NUMBER;
4667
4668 if (mch_stat((char *)fname, &st) >= 0)
4669 {
4670 if (mch_isdir(fname))
4671 rettv->vval.v_number = 0;
4672 else
4673 {
4674 rettv->vval.v_number = (varnumber_T)st.st_size;
4675
4676 /* non-perfect check for overflow */
4677 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
4678 rettv->vval.v_number = -2;
4679 }
4680 }
4681 else
4682 rettv->vval.v_number = -1;
4683}
4684
4685/*
4686 * "getftime({fname})" function
4687 */
4688 static void
4689f_getftime(typval_T *argvars, typval_T *rettv)
4690{
4691 char_u *fname;
4692 stat_T st;
4693
4694 fname = get_tv_string(&argvars[0]);
4695
4696 if (mch_stat((char *)fname, &st) >= 0)
4697 rettv->vval.v_number = (varnumber_T)st.st_mtime;
4698 else
4699 rettv->vval.v_number = -1;
4700}
4701
4702/*
4703 * "getftype({fname})" function
4704 */
4705 static void
4706f_getftype(typval_T *argvars, typval_T *rettv)
4707{
4708 char_u *fname;
4709 stat_T st;
4710 char_u *type = NULL;
4711 char *t;
4712
4713 fname = get_tv_string(&argvars[0]);
4714
4715 rettv->v_type = VAR_STRING;
4716 if (mch_lstat((char *)fname, &st) >= 0)
4717 {
4718#ifdef S_ISREG
4719 if (S_ISREG(st.st_mode))
4720 t = "file";
4721 else if (S_ISDIR(st.st_mode))
4722 t = "dir";
4723# ifdef S_ISLNK
4724 else if (S_ISLNK(st.st_mode))
4725 t = "link";
4726# endif
4727# ifdef S_ISBLK
4728 else if (S_ISBLK(st.st_mode))
4729 t = "bdev";
4730# endif
4731# ifdef S_ISCHR
4732 else if (S_ISCHR(st.st_mode))
4733 t = "cdev";
4734# endif
4735# ifdef S_ISFIFO
4736 else if (S_ISFIFO(st.st_mode))
4737 t = "fifo";
4738# endif
4739# ifdef S_ISSOCK
4740 else if (S_ISSOCK(st.st_mode))
4741 t = "fifo";
4742# endif
4743 else
4744 t = "other";
4745#else
4746# ifdef S_IFMT
4747 switch (st.st_mode & S_IFMT)
4748 {
4749 case S_IFREG: t = "file"; break;
4750 case S_IFDIR: t = "dir"; break;
4751# ifdef S_IFLNK
4752 case S_IFLNK: t = "link"; break;
4753# endif
4754# ifdef S_IFBLK
4755 case S_IFBLK: t = "bdev"; break;
4756# endif
4757# ifdef S_IFCHR
4758 case S_IFCHR: t = "cdev"; break;
4759# endif
4760# ifdef S_IFIFO
4761 case S_IFIFO: t = "fifo"; break;
4762# endif
4763# ifdef S_IFSOCK
4764 case S_IFSOCK: t = "socket"; break;
4765# endif
4766 default: t = "other";
4767 }
4768# else
4769 if (mch_isdir(fname))
4770 t = "dir";
4771 else
4772 t = "file";
4773# endif
4774#endif
4775 type = vim_strsave((char_u *)t);
4776 }
4777 rettv->vval.v_string = type;
4778}
4779
4780/*
4781 * "getline(lnum, [end])" function
4782 */
4783 static void
4784f_getline(typval_T *argvars, typval_T *rettv)
4785{
4786 linenr_T lnum;
4787 linenr_T end;
4788 int retlist;
4789
4790 lnum = get_tv_lnum(argvars);
4791 if (argvars[1].v_type == VAR_UNKNOWN)
4792 {
4793 end = 0;
4794 retlist = FALSE;
4795 }
4796 else
4797 {
4798 end = get_tv_lnum(&argvars[1]);
4799 retlist = TRUE;
4800 }
4801
4802 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
4803}
4804
Bram Moolenaar4ae20952016-08-13 15:29:14 +02004805#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02004806 static void
4807get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
4808{
Bram Moolenaard823fa92016-08-12 16:29:27 +02004809 if (what_arg->v_type == VAR_UNKNOWN)
4810 {
4811 if (rettv_list_alloc(rettv) == OK)
4812 if (is_qf || wp != NULL)
4813 (void)get_errorlist(wp, -1, rettv->vval.v_list);
4814 }
4815 else
4816 {
4817 if (rettv_dict_alloc(rettv) == OK)
4818 if (is_qf || (wp != NULL))
4819 {
4820 if (what_arg->v_type == VAR_DICT)
4821 {
4822 dict_T *d = what_arg->vval.v_dict;
4823
4824 if (d != NULL)
4825 get_errorlist_properties(wp, d, rettv->vval.v_dict);
4826 }
4827 else
4828 EMSG(_(e_dictreq));
4829 }
4830 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004831}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02004832#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02004833
4834/*
4835 * "getloclist()" function
4836 */
4837 static void
4838f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4839{
4840#ifdef FEAT_QUICKFIX
4841 win_T *wp;
4842
4843 wp = find_win_by_nr(&argvars[0], NULL);
4844 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
4845#endif
4846}
4847
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004848/*
4849 * "getmatches()" function
4850 */
4851 static void
4852f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4853{
4854#ifdef FEAT_SEARCH_EXTRA
4855 dict_T *dict;
4856 matchitem_T *cur = curwin->w_match_head;
4857 int i;
4858
4859 if (rettv_list_alloc(rettv) == OK)
4860 {
4861 while (cur != NULL)
4862 {
4863 dict = dict_alloc();
4864 if (dict == NULL)
4865 return;
4866 if (cur->match.regprog == NULL)
4867 {
4868 /* match added with matchaddpos() */
4869 for (i = 0; i < MAXPOSMATCH; ++i)
4870 {
4871 llpos_T *llpos;
4872 char buf[6];
4873 list_T *l;
4874
4875 llpos = &cur->pos.pos[i];
4876 if (llpos->lnum == 0)
4877 break;
4878 l = list_alloc();
4879 if (l == NULL)
4880 break;
4881 list_append_number(l, (varnumber_T)llpos->lnum);
4882 if (llpos->col > 0)
4883 {
4884 list_append_number(l, (varnumber_T)llpos->col);
4885 list_append_number(l, (varnumber_T)llpos->len);
4886 }
4887 sprintf(buf, "pos%d", i + 1);
4888 dict_add_list(dict, buf, l);
4889 }
4890 }
4891 else
4892 {
4893 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
4894 }
4895 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
4896 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
4897 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
4898# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
4899 if (cur->conceal_char)
4900 {
4901 char_u buf[MB_MAXBYTES + 1];
4902
4903 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
4904 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
4905 }
4906# endif
4907 list_append_dict(rettv->vval.v_list, dict);
4908 cur = cur->next;
4909 }
4910 }
4911#endif
4912}
4913
4914/*
4915 * "getpid()" function
4916 */
4917 static void
4918f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
4919{
4920 rettv->vval.v_number = mch_get_pid();
4921}
4922
4923 static void
4924getpos_both(
4925 typval_T *argvars,
4926 typval_T *rettv,
4927 int getcurpos)
4928{
4929 pos_T *fp;
4930 list_T *l;
4931 int fnum = -1;
4932
4933 if (rettv_list_alloc(rettv) == OK)
4934 {
4935 l = rettv->vval.v_list;
4936 if (getcurpos)
4937 fp = &curwin->w_cursor;
4938 else
4939 fp = var2fpos(&argvars[0], TRUE, &fnum);
4940 if (fnum != -1)
4941 list_append_number(l, (varnumber_T)fnum);
4942 else
4943 list_append_number(l, (varnumber_T)0);
4944 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
4945 : (varnumber_T)0);
4946 list_append_number(l, (fp != NULL)
4947 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
4948 : (varnumber_T)0);
4949 list_append_number(l,
4950#ifdef FEAT_VIRTUALEDIT
4951 (fp != NULL) ? (varnumber_T)fp->coladd :
4952#endif
4953 (varnumber_T)0);
4954 if (getcurpos)
4955 {
4956 update_curswant();
4957 list_append_number(l, curwin->w_curswant == MAXCOL ?
4958 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
4959 }
4960 }
4961 else
4962 rettv->vval.v_number = FALSE;
4963}
4964
4965
4966/*
4967 * "getcurpos()" function
4968 */
4969 static void
4970f_getcurpos(typval_T *argvars, typval_T *rettv)
4971{
4972 getpos_both(argvars, rettv, TRUE);
4973}
4974
4975/*
4976 * "getpos(string)" function
4977 */
4978 static void
4979f_getpos(typval_T *argvars, typval_T *rettv)
4980{
4981 getpos_both(argvars, rettv, FALSE);
4982}
4983
4984/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004985 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004986 */
4987 static void
4988f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4989{
4990#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02004991 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004992#endif
4993}
4994
4995/*
4996 * "getreg()" function
4997 */
4998 static void
4999f_getreg(typval_T *argvars, typval_T *rettv)
5000{
5001 char_u *strregname;
5002 int regname;
5003 int arg2 = FALSE;
5004 int return_list = FALSE;
5005 int error = FALSE;
5006
5007 if (argvars[0].v_type != VAR_UNKNOWN)
5008 {
5009 strregname = get_tv_string_chk(&argvars[0]);
5010 error = strregname == NULL;
5011 if (argvars[1].v_type != VAR_UNKNOWN)
5012 {
5013 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
5014 if (!error && argvars[2].v_type != VAR_UNKNOWN)
5015 return_list = (int)get_tv_number_chk(&argvars[2], &error);
5016 }
5017 }
5018 else
5019 strregname = get_vim_var_str(VV_REG);
5020
5021 if (error)
5022 return;
5023
5024 regname = (strregname == NULL ? '"' : *strregname);
5025 if (regname == 0)
5026 regname = '"';
5027
5028 if (return_list)
5029 {
5030 rettv->v_type = VAR_LIST;
5031 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5032 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5033 if (rettv->vval.v_list == NULL)
5034 (void)rettv_list_alloc(rettv);
5035 else
5036 ++rettv->vval.v_list->lv_refcount;
5037 }
5038 else
5039 {
5040 rettv->v_type = VAR_STRING;
5041 rettv->vval.v_string = get_reg_contents(regname,
5042 arg2 ? GREG_EXPR_SRC : 0);
5043 }
5044}
5045
5046/*
5047 * "getregtype()" function
5048 */
5049 static void
5050f_getregtype(typval_T *argvars, typval_T *rettv)
5051{
5052 char_u *strregname;
5053 int regname;
5054 char_u buf[NUMBUFLEN + 2];
5055 long reglen = 0;
5056
5057 if (argvars[0].v_type != VAR_UNKNOWN)
5058 {
5059 strregname = get_tv_string_chk(&argvars[0]);
5060 if (strregname == NULL) /* type error; errmsg already given */
5061 {
5062 rettv->v_type = VAR_STRING;
5063 rettv->vval.v_string = NULL;
5064 return;
5065 }
5066 }
5067 else
5068 /* Default to v:register */
5069 strregname = get_vim_var_str(VV_REG);
5070
5071 regname = (strregname == NULL ? '"' : *strregname);
5072 if (regname == 0)
5073 regname = '"';
5074
5075 buf[0] = NUL;
5076 buf[1] = NUL;
5077 switch (get_reg_type(regname, &reglen))
5078 {
5079 case MLINE: buf[0] = 'V'; break;
5080 case MCHAR: buf[0] = 'v'; break;
5081 case MBLOCK:
5082 buf[0] = Ctrl_V;
5083 sprintf((char *)buf + 1, "%ld", reglen + 1);
5084 break;
5085 }
5086 rettv->v_type = VAR_STRING;
5087 rettv->vval.v_string = vim_strsave(buf);
5088}
5089
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005090#ifdef FEAT_WINDOWS
5091/*
5092 * Returns information (variables, options, etc.) about a tab page
5093 * as a dictionary.
5094 */
5095 static dict_T *
5096get_tabpage_info(tabpage_T *tp, int tp_idx)
5097{
5098 win_T *wp;
5099 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005100 list_T *l;
5101
5102 dict = dict_alloc();
5103 if (dict == NULL)
5104 return NULL;
5105
Bram Moolenaar33928832016-08-18 21:22:04 +02005106 dict_add_nr_str(dict, "tabnr", tp_idx, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005107
5108 l = list_alloc();
5109 if (l != NULL)
5110 {
5111 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5112 wp; wp = wp->w_next)
5113 list_append_number(l, (varnumber_T)wp->w_id);
5114 dict_add_list(dict, "windows", l);
5115 }
5116
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005117 /* Make a reference to tabpage variables */
5118 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005119
5120 return dict;
5121}
5122#endif
5123
5124/*
5125 * "gettabinfo()" function
5126 */
5127 static void
5128f_gettabinfo(typval_T *argvars, typval_T *rettv)
5129{
5130#ifdef FEAT_WINDOWS
5131 tabpage_T *tp, *tparg = NULL;
5132 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005133 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005134
5135 if (rettv_list_alloc(rettv) != OK)
5136 return;
5137
5138 if (argvars[0].v_type != VAR_UNKNOWN)
5139 {
5140 /* Information about one tab page */
5141 tparg = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5142 if (tparg == NULL)
5143 return;
5144 }
5145
5146 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005147 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005148 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005149 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005150 if (tparg != NULL && tp != tparg)
5151 continue;
5152 d = get_tabpage_info(tp, tpnr);
5153 if (d != NULL)
5154 list_append_dict(rettv->vval.v_list, d);
5155 if (tparg != NULL)
5156 return;
5157 }
5158#endif
5159}
5160
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005161/*
5162 * "gettabvar()" function
5163 */
5164 static void
5165f_gettabvar(typval_T *argvars, typval_T *rettv)
5166{
5167 win_T *oldcurwin;
5168 tabpage_T *tp, *oldtabpage;
5169 dictitem_T *v;
5170 char_u *varname;
5171 int done = FALSE;
5172
5173 rettv->v_type = VAR_STRING;
5174 rettv->vval.v_string = NULL;
5175
5176 varname = get_tv_string_chk(&argvars[1]);
5177 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5178 if (tp != NULL && varname != NULL)
5179 {
5180 /* Set tp to be our tabpage, temporarily. Also set the window to the
5181 * first window in the tabpage, otherwise the window is not valid. */
5182 if (switch_win(&oldcurwin, &oldtabpage,
5183 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
5184 == OK)
5185 {
5186 /* look up the variable */
5187 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5188 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5189 if (v != NULL)
5190 {
5191 copy_tv(&v->di_tv, rettv);
5192 done = TRUE;
5193 }
5194 }
5195
5196 /* restore previous notion of curwin */
5197 restore_win(oldcurwin, oldtabpage, TRUE);
5198 }
5199
5200 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5201 copy_tv(&argvars[2], rettv);
5202}
5203
5204/*
5205 * "gettabwinvar()" function
5206 */
5207 static void
5208f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5209{
5210 getwinvar(argvars, rettv, 1);
5211}
5212
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005213#ifdef FEAT_WINDOWS
5214/*
5215 * Returns information about a window as a dictionary.
5216 */
5217 static dict_T *
5218get_win_info(win_T *wp, short tpnr, short winnr)
5219{
5220 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005221
5222 dict = dict_alloc();
5223 if (dict == NULL)
5224 return NULL;
5225
Bram Moolenaar33928832016-08-18 21:22:04 +02005226 dict_add_nr_str(dict, "tabnr", tpnr, NULL);
5227 dict_add_nr_str(dict, "winnr", winnr, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005228 dict_add_nr_str(dict, "winid", wp->w_id, NULL);
5229 dict_add_nr_str(dict, "height", wp->w_height, NULL);
5230 dict_add_nr_str(dict, "width", wp->w_width, NULL);
Bram Moolenaar33928832016-08-18 21:22:04 +02005231 dict_add_nr_str(dict, "bufnr", wp->w_buffer->b_fnum, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005232
Bram Moolenaar69905d12017-08-13 18:14:47 +02005233#ifdef FEAT_TERMINAL
5234 dict_add_nr_str(dict, "terminal", bt_terminal(wp->w_buffer), NULL);
5235#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005236#ifdef FEAT_QUICKFIX
5237 dict_add_nr_str(dict, "quickfix", bt_quickfix(wp->w_buffer), NULL);
5238 dict_add_nr_str(dict, "loclist",
5239 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL), NULL);
5240#endif
5241
Bram Moolenaar30567352016-08-27 21:25:44 +02005242 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005243 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005244
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005245 return dict;
5246}
5247#endif
5248
5249/*
5250 * "getwininfo()" function
5251 */
5252 static void
5253f_getwininfo(typval_T *argvars, typval_T *rettv)
5254{
5255#ifdef FEAT_WINDOWS
5256 tabpage_T *tp;
5257 win_T *wp = NULL, *wparg = NULL;
5258 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005259 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005260#endif
5261
5262 if (rettv_list_alloc(rettv) != OK)
5263 return;
5264
5265#ifdef FEAT_WINDOWS
5266 if (argvars[0].v_type != VAR_UNKNOWN)
5267 {
5268 wparg = win_id2wp(argvars);
5269 if (wparg == NULL)
5270 return;
5271 }
5272
5273 /* Collect information about either all the windows across all the tab
5274 * pages or one particular window.
5275 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005276 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005277 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005278 tabnr++;
5279 winnr = 0;
5280 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005281 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005282 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005283 if (wparg != NULL && wp != wparg)
5284 continue;
5285 d = get_win_info(wp, tabnr, winnr);
5286 if (d != NULL)
5287 list_append_dict(rettv->vval.v_list, d);
5288 if (wparg != NULL)
5289 /* found information about a specific window */
5290 return;
5291 }
5292 }
5293#endif
5294}
5295
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005296/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005297 * "win_findbuf()" function
5298 */
5299 static void
5300f_win_findbuf(typval_T *argvars, typval_T *rettv)
5301{
5302 if (rettv_list_alloc(rettv) != FAIL)
5303 win_findbuf(argvars, rettv->vval.v_list);
5304}
5305
5306/*
5307 * "win_getid()" function
5308 */
5309 static void
5310f_win_getid(typval_T *argvars, typval_T *rettv)
5311{
5312 rettv->vval.v_number = win_getid(argvars);
5313}
5314
5315/*
5316 * "win_gotoid()" function
5317 */
5318 static void
5319f_win_gotoid(typval_T *argvars, typval_T *rettv)
5320{
5321 rettv->vval.v_number = win_gotoid(argvars);
5322}
5323
5324/*
5325 * "win_id2tabwin()" function
5326 */
5327 static void
5328f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5329{
5330 if (rettv_list_alloc(rettv) != FAIL)
5331 win_id2tabwin(argvars, rettv->vval.v_list);
5332}
5333
5334/*
5335 * "win_id2win()" function
5336 */
5337 static void
5338f_win_id2win(typval_T *argvars, typval_T *rettv)
5339{
5340 rettv->vval.v_number = win_id2win(argvars);
5341}
5342
5343/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005344 * "getwinposx()" function
5345 */
5346 static void
5347f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5348{
5349 rettv->vval.v_number = -1;
5350#ifdef FEAT_GUI
5351 if (gui.in_use)
5352 {
5353 int x, y;
5354
5355 if (gui_mch_get_winpos(&x, &y) == OK)
5356 rettv->vval.v_number = x;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005357 return;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005358 }
5359#endif
5360#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5361 {
5362 int x, y;
5363
5364 if (term_get_winpos(&x, &y) == OK)
5365 rettv->vval.v_number = x;
5366 }
5367#endif
5368}
5369
5370/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005371 * "getwinposy()" function
5372 */
5373 static void
5374f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5375{
5376 rettv->vval.v_number = -1;
5377#ifdef FEAT_GUI
5378 if (gui.in_use)
5379 {
5380 int x, y;
5381
5382 if (gui_mch_get_winpos(&x, &y) == OK)
5383 rettv->vval.v_number = y;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005384 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005385 }
5386#endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005387#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5388 {
5389 int x, y;
5390
5391 if (term_get_winpos(&x, &y) == OK)
5392 rettv->vval.v_number = y;
5393 }
5394#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005395}
5396
5397/*
5398 * "getwinvar()" function
5399 */
5400 static void
5401f_getwinvar(typval_T *argvars, typval_T *rettv)
5402{
5403 getwinvar(argvars, rettv, 0);
5404}
5405
5406/*
5407 * "glob()" function
5408 */
5409 static void
5410f_glob(typval_T *argvars, typval_T *rettv)
5411{
5412 int options = WILD_SILENT|WILD_USE_NL;
5413 expand_T xpc;
5414 int error = FALSE;
5415
5416 /* When the optional second argument is non-zero, don't remove matches
5417 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5418 rettv->v_type = VAR_STRING;
5419 if (argvars[1].v_type != VAR_UNKNOWN)
5420 {
5421 if (get_tv_number_chk(&argvars[1], &error))
5422 options |= WILD_KEEP_ALL;
5423 if (argvars[2].v_type != VAR_UNKNOWN)
5424 {
5425 if (get_tv_number_chk(&argvars[2], &error))
5426 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005427 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005428 }
5429 if (argvars[3].v_type != VAR_UNKNOWN
5430 && get_tv_number_chk(&argvars[3], &error))
5431 options |= WILD_ALLLINKS;
5432 }
5433 }
5434 if (!error)
5435 {
5436 ExpandInit(&xpc);
5437 xpc.xp_context = EXPAND_FILES;
5438 if (p_wic)
5439 options += WILD_ICASE;
5440 if (rettv->v_type == VAR_STRING)
5441 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
5442 NULL, options, WILD_ALL);
5443 else if (rettv_list_alloc(rettv) != FAIL)
5444 {
5445 int i;
5446
5447 ExpandOne(&xpc, get_tv_string(&argvars[0]),
5448 NULL, options, WILD_ALL_KEEP);
5449 for (i = 0; i < xpc.xp_numfiles; i++)
5450 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5451
5452 ExpandCleanup(&xpc);
5453 }
5454 }
5455 else
5456 rettv->vval.v_string = NULL;
5457}
5458
5459/*
5460 * "globpath()" function
5461 */
5462 static void
5463f_globpath(typval_T *argvars, typval_T *rettv)
5464{
5465 int flags = 0;
5466 char_u buf1[NUMBUFLEN];
5467 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
5468 int error = FALSE;
5469 garray_T ga;
5470 int i;
5471
5472 /* When the optional second argument is non-zero, don't remove matches
5473 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5474 rettv->v_type = VAR_STRING;
5475 if (argvars[2].v_type != VAR_UNKNOWN)
5476 {
5477 if (get_tv_number_chk(&argvars[2], &error))
5478 flags |= WILD_KEEP_ALL;
5479 if (argvars[3].v_type != VAR_UNKNOWN)
5480 {
5481 if (get_tv_number_chk(&argvars[3], &error))
5482 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005483 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005484 }
5485 if (argvars[4].v_type != VAR_UNKNOWN
5486 && get_tv_number_chk(&argvars[4], &error))
5487 flags |= WILD_ALLLINKS;
5488 }
5489 }
5490 if (file != NULL && !error)
5491 {
5492 ga_init2(&ga, (int)sizeof(char_u *), 10);
5493 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
5494 if (rettv->v_type == VAR_STRING)
5495 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
5496 else if (rettv_list_alloc(rettv) != FAIL)
5497 for (i = 0; i < ga.ga_len; ++i)
5498 list_append_string(rettv->vval.v_list,
5499 ((char_u **)(ga.ga_data))[i], -1);
5500 ga_clear_strings(&ga);
5501 }
5502 else
5503 rettv->vval.v_string = NULL;
5504}
5505
5506/*
5507 * "glob2regpat()" function
5508 */
5509 static void
5510f_glob2regpat(typval_T *argvars, typval_T *rettv)
5511{
5512 char_u *pat = get_tv_string_chk(&argvars[0]);
5513
5514 rettv->v_type = VAR_STRING;
5515 rettv->vval.v_string = (pat == NULL)
5516 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
5517}
5518
5519/* for VIM_VERSION_ defines */
5520#include "version.h"
5521
5522/*
5523 * "has()" function
5524 */
5525 static void
5526f_has(typval_T *argvars, typval_T *rettv)
5527{
5528 int i;
5529 char_u *name;
5530 int n = FALSE;
5531 static char *(has_list[]) =
5532 {
5533#ifdef AMIGA
5534 "amiga",
5535# ifdef FEAT_ARP
5536 "arp",
5537# endif
5538#endif
5539#ifdef __BEOS__
5540 "beos",
5541#endif
5542#ifdef MACOS
5543 "mac",
5544#endif
5545#if defined(MACOS_X_UNIX)
5546 "macunix", /* built with 'darwin' enabled */
5547#endif
5548#if defined(__APPLE__) && __APPLE__ == 1
5549 "osx", /* built with or without 'darwin' enabled */
5550#endif
5551#ifdef __QNX__
5552 "qnx",
5553#endif
5554#ifdef UNIX
5555 "unix",
5556#endif
5557#ifdef VMS
5558 "vms",
5559#endif
5560#ifdef WIN32
5561 "win32",
5562#endif
5563#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
5564 "win32unix",
5565#endif
5566#if defined(WIN64) || defined(_WIN64)
5567 "win64",
5568#endif
5569#ifdef EBCDIC
5570 "ebcdic",
5571#endif
5572#ifndef CASE_INSENSITIVE_FILENAME
5573 "fname_case",
5574#endif
5575#ifdef HAVE_ACL
5576 "acl",
5577#endif
5578#ifdef FEAT_ARABIC
5579 "arabic",
5580#endif
5581#ifdef FEAT_AUTOCMD
5582 "autocmd",
5583#endif
5584#ifdef FEAT_BEVAL
5585 "balloon_eval",
5586# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
5587 "balloon_multiline",
5588# endif
5589#endif
5590#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
5591 "builtin_terms",
5592# ifdef ALL_BUILTIN_TCAPS
5593 "all_builtin_terms",
5594# endif
5595#endif
5596#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
5597 || defined(FEAT_GUI_W32) \
5598 || defined(FEAT_GUI_MOTIF))
5599 "browsefilter",
5600#endif
5601#ifdef FEAT_BYTEOFF
5602 "byte_offset",
5603#endif
5604#ifdef FEAT_JOB_CHANNEL
5605 "channel",
5606#endif
5607#ifdef FEAT_CINDENT
5608 "cindent",
5609#endif
5610#ifdef FEAT_CLIENTSERVER
5611 "clientserver",
5612#endif
5613#ifdef FEAT_CLIPBOARD
5614 "clipboard",
5615#endif
5616#ifdef FEAT_CMDL_COMPL
5617 "cmdline_compl",
5618#endif
5619#ifdef FEAT_CMDHIST
5620 "cmdline_hist",
5621#endif
5622#ifdef FEAT_COMMENTS
5623 "comments",
5624#endif
5625#ifdef FEAT_CONCEAL
5626 "conceal",
5627#endif
5628#ifdef FEAT_CRYPT
5629 "cryptv",
5630 "crypt-blowfish",
5631 "crypt-blowfish2",
5632#endif
5633#ifdef FEAT_CSCOPE
5634 "cscope",
5635#endif
5636#ifdef FEAT_CURSORBIND
5637 "cursorbind",
5638#endif
5639#ifdef CURSOR_SHAPE
5640 "cursorshape",
5641#endif
5642#ifdef DEBUG
5643 "debug",
5644#endif
5645#ifdef FEAT_CON_DIALOG
5646 "dialog_con",
5647#endif
5648#ifdef FEAT_GUI_DIALOG
5649 "dialog_gui",
5650#endif
5651#ifdef FEAT_DIFF
5652 "diff",
5653#endif
5654#ifdef FEAT_DIGRAPHS
5655 "digraphs",
5656#endif
5657#ifdef FEAT_DIRECTX
5658 "directx",
5659#endif
5660#ifdef FEAT_DND
5661 "dnd",
5662#endif
5663#ifdef FEAT_EMACS_TAGS
5664 "emacs_tags",
5665#endif
5666 "eval", /* always present, of course! */
5667 "ex_extra", /* graduated feature */
5668#ifdef FEAT_SEARCH_EXTRA
5669 "extra_search",
5670#endif
5671#ifdef FEAT_FKMAP
5672 "farsi",
5673#endif
5674#ifdef FEAT_SEARCHPATH
5675 "file_in_path",
5676#endif
5677#ifdef FEAT_FILTERPIPE
5678 "filterpipe",
5679#endif
5680#ifdef FEAT_FIND_ID
5681 "find_in_path",
5682#endif
5683#ifdef FEAT_FLOAT
5684 "float",
5685#endif
5686#ifdef FEAT_FOLDING
5687 "folding",
5688#endif
5689#ifdef FEAT_FOOTER
5690 "footer",
5691#endif
5692#if !defined(USE_SYSTEM) && defined(UNIX)
5693 "fork",
5694#endif
5695#ifdef FEAT_GETTEXT
5696 "gettext",
5697#endif
5698#ifdef FEAT_GUI
5699 "gui",
5700#endif
5701#ifdef FEAT_GUI_ATHENA
5702# ifdef FEAT_GUI_NEXTAW
5703 "gui_neXtaw",
5704# else
5705 "gui_athena",
5706# endif
5707#endif
5708#ifdef FEAT_GUI_GTK
5709 "gui_gtk",
5710# ifdef USE_GTK3
5711 "gui_gtk3",
5712# else
5713 "gui_gtk2",
5714# endif
5715#endif
5716#ifdef FEAT_GUI_GNOME
5717 "gui_gnome",
5718#endif
5719#ifdef FEAT_GUI_MAC
5720 "gui_mac",
5721#endif
5722#ifdef FEAT_GUI_MOTIF
5723 "gui_motif",
5724#endif
5725#ifdef FEAT_GUI_PHOTON
5726 "gui_photon",
5727#endif
5728#ifdef FEAT_GUI_W32
5729 "gui_win32",
5730#endif
5731#ifdef FEAT_HANGULIN
5732 "hangul_input",
5733#endif
5734#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
5735 "iconv",
5736#endif
5737#ifdef FEAT_INS_EXPAND
5738 "insert_expand",
5739#endif
5740#ifdef FEAT_JOB_CHANNEL
5741 "job",
5742#endif
5743#ifdef FEAT_JUMPLIST
5744 "jumplist",
5745#endif
5746#ifdef FEAT_KEYMAP
5747 "keymap",
5748#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02005749 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005750#ifdef FEAT_LANGMAP
5751 "langmap",
5752#endif
5753#ifdef FEAT_LIBCALL
5754 "libcall",
5755#endif
5756#ifdef FEAT_LINEBREAK
5757 "linebreak",
5758#endif
5759#ifdef FEAT_LISP
5760 "lispindent",
5761#endif
5762#ifdef FEAT_LISTCMDS
5763 "listcmds",
5764#endif
5765#ifdef FEAT_LOCALMAP
5766 "localmap",
5767#endif
5768#ifdef FEAT_LUA
5769# ifndef DYNAMIC_LUA
5770 "lua",
5771# endif
5772#endif
5773#ifdef FEAT_MENU
5774 "menu",
5775#endif
5776#ifdef FEAT_SESSION
5777 "mksession",
5778#endif
5779#ifdef FEAT_MODIFY_FNAME
5780 "modify_fname",
5781#endif
5782#ifdef FEAT_MOUSE
5783 "mouse",
5784#endif
5785#ifdef FEAT_MOUSESHAPE
5786 "mouseshape",
5787#endif
5788#if defined(UNIX) || defined(VMS)
5789# ifdef FEAT_MOUSE_DEC
5790 "mouse_dec",
5791# endif
5792# ifdef FEAT_MOUSE_GPM
5793 "mouse_gpm",
5794# endif
5795# ifdef FEAT_MOUSE_JSB
5796 "mouse_jsbterm",
5797# endif
5798# ifdef FEAT_MOUSE_NET
5799 "mouse_netterm",
5800# endif
5801# ifdef FEAT_MOUSE_PTERM
5802 "mouse_pterm",
5803# endif
5804# ifdef FEAT_MOUSE_SGR
5805 "mouse_sgr",
5806# endif
5807# ifdef FEAT_SYSMOUSE
5808 "mouse_sysmouse",
5809# endif
5810# ifdef FEAT_MOUSE_URXVT
5811 "mouse_urxvt",
5812# endif
5813# ifdef FEAT_MOUSE_XTERM
5814 "mouse_xterm",
5815# endif
5816#endif
5817#ifdef FEAT_MBYTE
5818 "multi_byte",
5819#endif
5820#ifdef FEAT_MBYTE_IME
5821 "multi_byte_ime",
5822#endif
5823#ifdef FEAT_MULTI_LANG
5824 "multi_lang",
5825#endif
5826#ifdef FEAT_MZSCHEME
5827#ifndef DYNAMIC_MZSCHEME
5828 "mzscheme",
5829#endif
5830#endif
5831#ifdef FEAT_NUM64
5832 "num64",
5833#endif
5834#ifdef FEAT_OLE
5835 "ole",
5836#endif
5837 "packages",
5838#ifdef FEAT_PATH_EXTRA
5839 "path_extra",
5840#endif
5841#ifdef FEAT_PERL
5842#ifndef DYNAMIC_PERL
5843 "perl",
5844#endif
5845#endif
5846#ifdef FEAT_PERSISTENT_UNDO
5847 "persistent_undo",
5848#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005849#if defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005850 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005851 "pythonx",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005852#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005853#if defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005854 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005855 "pythonx",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005856#endif
5857#ifdef FEAT_POSTSCRIPT
5858 "postscript",
5859#endif
5860#ifdef FEAT_PRINTER
5861 "printer",
5862#endif
5863#ifdef FEAT_PROFILE
5864 "profile",
5865#endif
5866#ifdef FEAT_RELTIME
5867 "reltime",
5868#endif
5869#ifdef FEAT_QUICKFIX
5870 "quickfix",
5871#endif
5872#ifdef FEAT_RIGHTLEFT
5873 "rightleft",
5874#endif
5875#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
5876 "ruby",
5877#endif
5878#ifdef FEAT_SCROLLBIND
5879 "scrollbind",
5880#endif
5881#ifdef FEAT_CMDL_INFO
5882 "showcmd",
5883 "cmdline_info",
5884#endif
5885#ifdef FEAT_SIGNS
5886 "signs",
5887#endif
5888#ifdef FEAT_SMARTINDENT
5889 "smartindent",
5890#endif
5891#ifdef STARTUPTIME
5892 "startuptime",
5893#endif
5894#ifdef FEAT_STL_OPT
5895 "statusline",
5896#endif
5897#ifdef FEAT_SUN_WORKSHOP
5898 "sun_workshop",
5899#endif
5900#ifdef FEAT_NETBEANS_INTG
5901 "netbeans_intg",
5902#endif
5903#ifdef FEAT_SPELL
5904 "spell",
5905#endif
5906#ifdef FEAT_SYN_HL
5907 "syntax",
5908#endif
5909#if defined(USE_SYSTEM) || !defined(UNIX)
5910 "system",
5911#endif
5912#ifdef FEAT_TAG_BINS
5913 "tag_binary",
5914#endif
5915#ifdef FEAT_TAG_OLDSTATIC
5916 "tag_old_static",
5917#endif
5918#ifdef FEAT_TAG_ANYWHITE
5919 "tag_any_white",
5920#endif
5921#ifdef FEAT_TCL
5922# ifndef DYNAMIC_TCL
5923 "tcl",
5924# endif
5925#endif
5926#ifdef FEAT_TERMGUICOLORS
5927 "termguicolors",
5928#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02005929#if defined(FEAT_TERMINAL) && !defined(WIN3264)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005930 "terminal",
5931#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005932#ifdef TERMINFO
5933 "terminfo",
5934#endif
5935#ifdef FEAT_TERMRESPONSE
5936 "termresponse",
5937#endif
5938#ifdef FEAT_TEXTOBJ
5939 "textobjects",
5940#endif
5941#ifdef HAVE_TGETENT
5942 "tgetent",
5943#endif
5944#ifdef FEAT_TIMERS
5945 "timers",
5946#endif
5947#ifdef FEAT_TITLE
5948 "title",
5949#endif
5950#ifdef FEAT_TOOLBAR
5951 "toolbar",
5952#endif
5953#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
5954 "unnamedplus",
5955#endif
5956#ifdef FEAT_USR_CMDS
5957 "user-commands", /* was accidentally included in 5.4 */
5958 "user_commands",
5959#endif
5960#ifdef FEAT_VIMINFO
5961 "viminfo",
5962#endif
5963#ifdef FEAT_WINDOWS
5964 "vertsplit",
5965#endif
5966#ifdef FEAT_VIRTUALEDIT
5967 "virtualedit",
5968#endif
5969 "visual",
5970#ifdef FEAT_VISUALEXTRA
5971 "visualextra",
5972#endif
5973#ifdef FEAT_VREPLACE
5974 "vreplace",
5975#endif
5976#ifdef FEAT_WILDIGN
5977 "wildignore",
5978#endif
5979#ifdef FEAT_WILDMENU
5980 "wildmenu",
5981#endif
5982#ifdef FEAT_WINDOWS
5983 "windows",
5984#endif
5985#ifdef FEAT_WAK
5986 "winaltkeys",
5987#endif
5988#ifdef FEAT_WRITEBACKUP
5989 "writebackup",
5990#endif
5991#ifdef FEAT_XIM
5992 "xim",
5993#endif
5994#ifdef FEAT_XFONTSET
5995 "xfontset",
5996#endif
5997#ifdef FEAT_XPM_W32
5998 "xpm",
5999 "xpm_w32", /* for backward compatibility */
6000#else
6001# if defined(HAVE_XPM)
6002 "xpm",
6003# endif
6004#endif
6005#ifdef USE_XSMP
6006 "xsmp",
6007#endif
6008#ifdef USE_XSMP_INTERACT
6009 "xsmp_interact",
6010#endif
6011#ifdef FEAT_XCLIPBOARD
6012 "xterm_clipboard",
6013#endif
6014#ifdef FEAT_XTERM_SAVE
6015 "xterm_save",
6016#endif
6017#if defined(UNIX) && defined(FEAT_X11)
6018 "X11",
6019#endif
6020 NULL
6021 };
6022
6023 name = get_tv_string(&argvars[0]);
6024 for (i = 0; has_list[i] != NULL; ++i)
6025 if (STRICMP(name, has_list[i]) == 0)
6026 {
6027 n = TRUE;
6028 break;
6029 }
6030
6031 if (n == FALSE)
6032 {
6033 if (STRNICMP(name, "patch", 5) == 0)
6034 {
6035 if (name[5] == '-'
6036 && STRLEN(name) >= 11
6037 && vim_isdigit(name[6])
6038 && vim_isdigit(name[8])
6039 && vim_isdigit(name[10]))
6040 {
6041 int major = atoi((char *)name + 6);
6042 int minor = atoi((char *)name + 8);
6043
6044 /* Expect "patch-9.9.01234". */
6045 n = (major < VIM_VERSION_MAJOR
6046 || (major == VIM_VERSION_MAJOR
6047 && (minor < VIM_VERSION_MINOR
6048 || (minor == VIM_VERSION_MINOR
6049 && has_patch(atoi((char *)name + 10))))));
6050 }
6051 else
6052 n = has_patch(atoi((char *)name + 5));
6053 }
6054 else if (STRICMP(name, "vim_starting") == 0)
6055 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006056 else if (STRICMP(name, "ttyin") == 0)
6057 n = mch_input_isatty();
6058 else if (STRICMP(name, "ttyout") == 0)
6059 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006060#ifdef FEAT_MBYTE
6061 else if (STRICMP(name, "multi_byte_encoding") == 0)
6062 n = has_mbyte;
6063#endif
6064#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
6065 else if (STRICMP(name, "balloon_multiline") == 0)
6066 n = multiline_balloon_available();
6067#endif
6068#ifdef DYNAMIC_TCL
6069 else if (STRICMP(name, "tcl") == 0)
6070 n = tcl_enabled(FALSE);
6071#endif
6072#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6073 else if (STRICMP(name, "iconv") == 0)
6074 n = iconv_enabled(FALSE);
6075#endif
6076#ifdef DYNAMIC_LUA
6077 else if (STRICMP(name, "lua") == 0)
6078 n = lua_enabled(FALSE);
6079#endif
6080#ifdef DYNAMIC_MZSCHEME
6081 else if (STRICMP(name, "mzscheme") == 0)
6082 n = mzscheme_enabled(FALSE);
6083#endif
6084#ifdef DYNAMIC_RUBY
6085 else if (STRICMP(name, "ruby") == 0)
6086 n = ruby_enabled(FALSE);
6087#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006088#ifdef DYNAMIC_PYTHON
6089 else if (STRICMP(name, "python") == 0)
6090 n = python_enabled(FALSE);
6091#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006092#ifdef DYNAMIC_PYTHON3
6093 else if (STRICMP(name, "python3") == 0)
6094 n = python3_enabled(FALSE);
6095#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006096#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6097 else if (STRICMP(name, "pythonx") == 0)
6098 {
6099# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6100 if (p_pyx == 0)
6101 n = python3_enabled(FALSE) || python_enabled(FALSE);
6102 else if (p_pyx == 3)
6103 n = python3_enabled(FALSE);
6104 else if (p_pyx == 2)
6105 n = python_enabled(FALSE);
6106# elif defined(DYNAMIC_PYTHON)
6107 n = python_enabled(FALSE);
6108# elif defined(DYNAMIC_PYTHON3)
6109 n = python3_enabled(FALSE);
6110# endif
6111 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006112#endif
6113#ifdef DYNAMIC_PERL
6114 else if (STRICMP(name, "perl") == 0)
6115 n = perl_enabled(FALSE);
6116#endif
6117#ifdef FEAT_GUI
6118 else if (STRICMP(name, "gui_running") == 0)
6119 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006120# ifdef FEAT_BROWSE
6121 else if (STRICMP(name, "browse") == 0)
6122 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6123# endif
6124#endif
6125#ifdef FEAT_SYN_HL
6126 else if (STRICMP(name, "syntax_items") == 0)
6127 n = syntax_present(curwin);
6128#endif
6129#if defined(WIN3264)
6130 else if (STRICMP(name, "win95") == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006131 n = FALSE; /* Win9x is no more supported. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006132#endif
6133#ifdef FEAT_NETBEANS_INTG
6134 else if (STRICMP(name, "netbeans_enabled") == 0)
6135 n = netbeans_active();
6136#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006137#if defined(FEAT_TERMINAL) && defined(WIN3264)
6138 else if (STRICMP(name, "terminal") == 0)
6139 n = terminal_enabled();
6140#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006141 }
6142
6143 rettv->vval.v_number = n;
6144}
6145
6146/*
6147 * "has_key()" function
6148 */
6149 static void
6150f_has_key(typval_T *argvars, typval_T *rettv)
6151{
6152 if (argvars[0].v_type != VAR_DICT)
6153 {
6154 EMSG(_(e_dictreq));
6155 return;
6156 }
6157 if (argvars[0].vval.v_dict == NULL)
6158 return;
6159
6160 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
6161 get_tv_string(&argvars[1]), -1) != NULL;
6162}
6163
6164/*
6165 * "haslocaldir()" function
6166 */
6167 static void
6168f_haslocaldir(typval_T *argvars, typval_T *rettv)
6169{
6170 win_T *wp = NULL;
6171
6172 wp = find_tabwin(&argvars[0], &argvars[1]);
6173 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6174}
6175
6176/*
6177 * "hasmapto()" function
6178 */
6179 static void
6180f_hasmapto(typval_T *argvars, typval_T *rettv)
6181{
6182 char_u *name;
6183 char_u *mode;
6184 char_u buf[NUMBUFLEN];
6185 int abbr = FALSE;
6186
6187 name = get_tv_string(&argvars[0]);
6188 if (argvars[1].v_type == VAR_UNKNOWN)
6189 mode = (char_u *)"nvo";
6190 else
6191 {
6192 mode = get_tv_string_buf(&argvars[1], buf);
6193 if (argvars[2].v_type != VAR_UNKNOWN)
6194 abbr = (int)get_tv_number(&argvars[2]);
6195 }
6196
6197 if (map_to_exists(name, mode, abbr))
6198 rettv->vval.v_number = TRUE;
6199 else
6200 rettv->vval.v_number = FALSE;
6201}
6202
6203/*
6204 * "histadd()" function
6205 */
6206 static void
6207f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6208{
6209#ifdef FEAT_CMDHIST
6210 int histype;
6211 char_u *str;
6212 char_u buf[NUMBUFLEN];
6213#endif
6214
6215 rettv->vval.v_number = FALSE;
6216 if (check_restricted() || check_secure())
6217 return;
6218#ifdef FEAT_CMDHIST
6219 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6220 histype = str != NULL ? get_histtype(str) : -1;
6221 if (histype >= 0)
6222 {
6223 str = get_tv_string_buf(&argvars[1], buf);
6224 if (*str != NUL)
6225 {
6226 init_history();
6227 add_to_history(histype, str, FALSE, NUL);
6228 rettv->vval.v_number = TRUE;
6229 return;
6230 }
6231 }
6232#endif
6233}
6234
6235/*
6236 * "histdel()" function
6237 */
6238 static void
6239f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6240{
6241#ifdef FEAT_CMDHIST
6242 int n;
6243 char_u buf[NUMBUFLEN];
6244 char_u *str;
6245
6246 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6247 if (str == NULL)
6248 n = 0;
6249 else if (argvars[1].v_type == VAR_UNKNOWN)
6250 /* only one argument: clear entire history */
6251 n = clr_history(get_histtype(str));
6252 else if (argvars[1].v_type == VAR_NUMBER)
6253 /* index given: remove that entry */
6254 n = del_history_idx(get_histtype(str),
6255 (int)get_tv_number(&argvars[1]));
6256 else
6257 /* string given: remove all matching entries */
6258 n = del_history_entry(get_histtype(str),
6259 get_tv_string_buf(&argvars[1], buf));
6260 rettv->vval.v_number = n;
6261#endif
6262}
6263
6264/*
6265 * "histget()" function
6266 */
6267 static void
6268f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6269{
6270#ifdef FEAT_CMDHIST
6271 int type;
6272 int idx;
6273 char_u *str;
6274
6275 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6276 if (str == NULL)
6277 rettv->vval.v_string = NULL;
6278 else
6279 {
6280 type = get_histtype(str);
6281 if (argvars[1].v_type == VAR_UNKNOWN)
6282 idx = get_history_idx(type);
6283 else
6284 idx = (int)get_tv_number_chk(&argvars[1], NULL);
6285 /* -1 on type error */
6286 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6287 }
6288#else
6289 rettv->vval.v_string = NULL;
6290#endif
6291 rettv->v_type = VAR_STRING;
6292}
6293
6294/*
6295 * "histnr()" function
6296 */
6297 static void
6298f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6299{
6300 int i;
6301
6302#ifdef FEAT_CMDHIST
6303 char_u *history = get_tv_string_chk(&argvars[0]);
6304
6305 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6306 if (i >= HIST_CMD && i < HIST_COUNT)
6307 i = get_history_idx(i);
6308 else
6309#endif
6310 i = -1;
6311 rettv->vval.v_number = i;
6312}
6313
6314/*
6315 * "highlightID(name)" function
6316 */
6317 static void
6318f_hlID(typval_T *argvars, typval_T *rettv)
6319{
6320 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
6321}
6322
6323/*
6324 * "highlight_exists()" function
6325 */
6326 static void
6327f_hlexists(typval_T *argvars, typval_T *rettv)
6328{
6329 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
6330}
6331
6332/*
6333 * "hostname()" function
6334 */
6335 static void
6336f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6337{
6338 char_u hostname[256];
6339
6340 mch_get_host_name(hostname, 256);
6341 rettv->v_type = VAR_STRING;
6342 rettv->vval.v_string = vim_strsave(hostname);
6343}
6344
6345/*
6346 * iconv() function
6347 */
6348 static void
6349f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6350{
6351#ifdef FEAT_MBYTE
6352 char_u buf1[NUMBUFLEN];
6353 char_u buf2[NUMBUFLEN];
6354 char_u *from, *to, *str;
6355 vimconv_T vimconv;
6356#endif
6357
6358 rettv->v_type = VAR_STRING;
6359 rettv->vval.v_string = NULL;
6360
6361#ifdef FEAT_MBYTE
6362 str = get_tv_string(&argvars[0]);
6363 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
6364 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
6365 vimconv.vc_type = CONV_NONE;
6366 convert_setup(&vimconv, from, to);
6367
6368 /* If the encodings are equal, no conversion needed. */
6369 if (vimconv.vc_type == CONV_NONE)
6370 rettv->vval.v_string = vim_strsave(str);
6371 else
6372 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6373
6374 convert_setup(&vimconv, NULL, NULL);
6375 vim_free(from);
6376 vim_free(to);
6377#endif
6378}
6379
6380/*
6381 * "indent()" function
6382 */
6383 static void
6384f_indent(typval_T *argvars, typval_T *rettv)
6385{
6386 linenr_T lnum;
6387
6388 lnum = get_tv_lnum(argvars);
6389 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6390 rettv->vval.v_number = get_indent_lnum(lnum);
6391 else
6392 rettv->vval.v_number = -1;
6393}
6394
6395/*
6396 * "index()" function
6397 */
6398 static void
6399f_index(typval_T *argvars, typval_T *rettv)
6400{
6401 list_T *l;
6402 listitem_T *item;
6403 long idx = 0;
6404 int ic = FALSE;
6405
6406 rettv->vval.v_number = -1;
6407 if (argvars[0].v_type != VAR_LIST)
6408 {
6409 EMSG(_(e_listreq));
6410 return;
6411 }
6412 l = argvars[0].vval.v_list;
6413 if (l != NULL)
6414 {
6415 item = l->lv_first;
6416 if (argvars[2].v_type != VAR_UNKNOWN)
6417 {
6418 int error = FALSE;
6419
6420 /* Start at specified item. Use the cached index that list_find()
6421 * sets, so that a negative number also works. */
6422 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
6423 idx = l->lv_idx;
6424 if (argvars[3].v_type != VAR_UNKNOWN)
6425 ic = (int)get_tv_number_chk(&argvars[3], &error);
6426 if (error)
6427 item = NULL;
6428 }
6429
6430 for ( ; item != NULL; item = item->li_next, ++idx)
6431 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6432 {
6433 rettv->vval.v_number = idx;
6434 break;
6435 }
6436 }
6437}
6438
6439static int inputsecret_flag = 0;
6440
6441/*
6442 * "input()" function
6443 * Also handles inputsecret() when inputsecret is set.
6444 */
6445 static void
6446f_input(typval_T *argvars, typval_T *rettv)
6447{
6448 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6449}
6450
6451/*
6452 * "inputdialog()" function
6453 */
6454 static void
6455f_inputdialog(typval_T *argvars, typval_T *rettv)
6456{
6457#if defined(FEAT_GUI_TEXTDIALOG)
6458 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
6459 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6460 {
6461 char_u *message;
6462 char_u buf[NUMBUFLEN];
6463 char_u *defstr = (char_u *)"";
6464
6465 message = get_tv_string_chk(&argvars[0]);
6466 if (argvars[1].v_type != VAR_UNKNOWN
6467 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
6468 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6469 else
6470 IObuff[0] = NUL;
6471 if (message != NULL && defstr != NULL
6472 && do_dialog(VIM_QUESTION, NULL, message,
6473 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6474 rettv->vval.v_string = vim_strsave(IObuff);
6475 else
6476 {
6477 if (message != NULL && defstr != NULL
6478 && argvars[1].v_type != VAR_UNKNOWN
6479 && argvars[2].v_type != VAR_UNKNOWN)
6480 rettv->vval.v_string = vim_strsave(
6481 get_tv_string_buf(&argvars[2], buf));
6482 else
6483 rettv->vval.v_string = NULL;
6484 }
6485 rettv->v_type = VAR_STRING;
6486 }
6487 else
6488#endif
6489 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6490}
6491
6492/*
6493 * "inputlist()" function
6494 */
6495 static void
6496f_inputlist(typval_T *argvars, typval_T *rettv)
6497{
6498 listitem_T *li;
6499 int selected;
6500 int mouse_used;
6501
6502#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02006503 /* While starting up, there is no place to enter text. When running tests
6504 * with --not-a-term we assume feedkeys() will be used. */
6505 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006506 return;
6507#endif
6508 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6509 {
6510 EMSG2(_(e_listarg), "inputlist()");
6511 return;
6512 }
6513
6514 msg_start();
6515 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
6516 lines_left = Rows; /* avoid more prompt */
6517 msg_scroll = TRUE;
6518 msg_clr_eos();
6519
6520 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
6521 {
6522 msg_puts(get_tv_string(&li->li_tv));
6523 msg_putchar('\n');
6524 }
6525
6526 /* Ask for choice. */
6527 selected = prompt_for_number(&mouse_used);
6528 if (mouse_used)
6529 selected -= lines_left;
6530
6531 rettv->vval.v_number = selected;
6532}
6533
6534
6535static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6536
6537/*
6538 * "inputrestore()" function
6539 */
6540 static void
6541f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6542{
6543 if (ga_userinput.ga_len > 0)
6544 {
6545 --ga_userinput.ga_len;
6546 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
6547 + ga_userinput.ga_len);
6548 /* default return is zero == OK */
6549 }
6550 else if (p_verbose > 1)
6551 {
6552 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
6553 rettv->vval.v_number = 1; /* Failed */
6554 }
6555}
6556
6557/*
6558 * "inputsave()" function
6559 */
6560 static void
6561f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
6562{
6563 /* Add an entry to the stack of typeahead storage. */
6564 if (ga_grow(&ga_userinput, 1) == OK)
6565 {
6566 save_typeahead((tasave_T *)(ga_userinput.ga_data)
6567 + ga_userinput.ga_len);
6568 ++ga_userinput.ga_len;
6569 /* default return is zero == OK */
6570 }
6571 else
6572 rettv->vval.v_number = 1; /* Failed */
6573}
6574
6575/*
6576 * "inputsecret()" function
6577 */
6578 static void
6579f_inputsecret(typval_T *argvars, typval_T *rettv)
6580{
6581 ++cmdline_star;
6582 ++inputsecret_flag;
6583 f_input(argvars, rettv);
6584 --cmdline_star;
6585 --inputsecret_flag;
6586}
6587
6588/*
6589 * "insert()" function
6590 */
6591 static void
6592f_insert(typval_T *argvars, typval_T *rettv)
6593{
6594 long before = 0;
6595 listitem_T *item;
6596 list_T *l;
6597 int error = FALSE;
6598
6599 if (argvars[0].v_type != VAR_LIST)
6600 EMSG2(_(e_listarg), "insert()");
6601 else if ((l = argvars[0].vval.v_list) != NULL
6602 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
6603 {
6604 if (argvars[2].v_type != VAR_UNKNOWN)
6605 before = (long)get_tv_number_chk(&argvars[2], &error);
6606 if (error)
6607 return; /* type error; errmsg already given */
6608
6609 if (before == l->lv_len)
6610 item = NULL;
6611 else
6612 {
6613 item = list_find(l, before);
6614 if (item == NULL)
6615 {
6616 EMSGN(_(e_listidx), before);
6617 l = NULL;
6618 }
6619 }
6620 if (l != NULL)
6621 {
6622 list_insert_tv(l, &argvars[1], item);
6623 copy_tv(&argvars[0], rettv);
6624 }
6625 }
6626}
6627
6628/*
6629 * "invert(expr)" function
6630 */
6631 static void
6632f_invert(typval_T *argvars, typval_T *rettv)
6633{
6634 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
6635}
6636
6637/*
6638 * "isdirectory()" function
6639 */
6640 static void
6641f_isdirectory(typval_T *argvars, typval_T *rettv)
6642{
6643 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
6644}
6645
6646/*
6647 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
6648 * or it refers to a List or Dictionary that is locked.
6649 */
6650 static int
6651tv_islocked(typval_T *tv)
6652{
6653 return (tv->v_lock & VAR_LOCKED)
6654 || (tv->v_type == VAR_LIST
6655 && tv->vval.v_list != NULL
6656 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
6657 || (tv->v_type == VAR_DICT
6658 && tv->vval.v_dict != NULL
6659 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
6660}
6661
6662/*
6663 * "islocked()" function
6664 */
6665 static void
6666f_islocked(typval_T *argvars, typval_T *rettv)
6667{
6668 lval_T lv;
6669 char_u *end;
6670 dictitem_T *di;
6671
6672 rettv->vval.v_number = -1;
6673 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01006674 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006675 if (end != NULL && lv.ll_name != NULL)
6676 {
6677 if (*end != NUL)
6678 EMSG(_(e_trailing));
6679 else
6680 {
6681 if (lv.ll_tv == NULL)
6682 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006683 di = find_var(lv.ll_name, NULL, TRUE);
6684 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006685 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006686 /* Consider a variable locked when:
6687 * 1. the variable itself is locked
6688 * 2. the value of the variable is locked.
6689 * 3. the List or Dict value is locked.
6690 */
6691 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
6692 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006693 }
6694 }
6695 else if (lv.ll_range)
6696 EMSG(_("E786: Range not allowed"));
6697 else if (lv.ll_newkey != NULL)
6698 EMSG2(_(e_dictkey), lv.ll_newkey);
6699 else if (lv.ll_list != NULL)
6700 /* List item. */
6701 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
6702 else
6703 /* Dictionary item. */
6704 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
6705 }
6706 }
6707
6708 clear_lval(&lv);
6709}
6710
6711#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
6712/*
6713 * "isnan()" function
6714 */
6715 static void
6716f_isnan(typval_T *argvars, typval_T *rettv)
6717{
6718 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
6719 && isnan(argvars[0].vval.v_float);
6720}
6721#endif
6722
6723/*
6724 * "items(dict)" function
6725 */
6726 static void
6727f_items(typval_T *argvars, typval_T *rettv)
6728{
6729 dict_list(argvars, rettv, 2);
6730}
6731
6732#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
6733/*
6734 * Get the job from the argument.
6735 * Returns NULL if the job is invalid.
6736 */
6737 static job_T *
6738get_job_arg(typval_T *tv)
6739{
6740 job_T *job;
6741
6742 if (tv->v_type != VAR_JOB)
6743 {
6744 EMSG2(_(e_invarg2), get_tv_string(tv));
6745 return NULL;
6746 }
6747 job = tv->vval.v_job;
6748
6749 if (job == NULL)
6750 EMSG(_("E916: not a valid job"));
6751 return job;
6752}
6753
6754/*
6755 * "job_getchannel()" function
6756 */
6757 static void
6758f_job_getchannel(typval_T *argvars, typval_T *rettv)
6759{
6760 job_T *job = get_job_arg(&argvars[0]);
6761
6762 if (job != NULL)
6763 {
6764 rettv->v_type = VAR_CHANNEL;
6765 rettv->vval.v_channel = job->jv_channel;
6766 if (job->jv_channel != NULL)
6767 ++job->jv_channel->ch_refcount;
6768 }
6769}
6770
6771/*
6772 * "job_info()" function
6773 */
6774 static void
6775f_job_info(typval_T *argvars, typval_T *rettv)
6776{
6777 job_T *job = get_job_arg(&argvars[0]);
6778
6779 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
6780 job_info(job, rettv->vval.v_dict);
6781}
6782
6783/*
6784 * "job_setoptions()" function
6785 */
6786 static void
6787f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
6788{
6789 job_T *job = get_job_arg(&argvars[0]);
6790 jobopt_T opt;
6791
6792 if (job == NULL)
6793 return;
6794 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02006795 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006796 job_set_options(job, &opt);
6797 free_job_options(&opt);
6798}
6799
6800/*
6801 * "job_start()" function
6802 */
6803 static void
6804f_job_start(typval_T *argvars, typval_T *rettv)
6805{
6806 rettv->v_type = VAR_JOB;
6807 if (check_restricted() || check_secure())
6808 return;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006809 rettv->vval.v_job = job_start(argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006810}
6811
6812/*
6813 * "job_status()" function
6814 */
6815 static void
6816f_job_status(typval_T *argvars, typval_T *rettv)
6817{
6818 job_T *job = get_job_arg(&argvars[0]);
6819
6820 if (job != NULL)
6821 {
6822 rettv->v_type = VAR_STRING;
6823 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
6824 }
6825}
6826
6827/*
6828 * "job_stop()" function
6829 */
6830 static void
6831f_job_stop(typval_T *argvars, typval_T *rettv)
6832{
6833 job_T *job = get_job_arg(&argvars[0]);
6834
6835 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02006836 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006837}
6838#endif
6839
6840/*
6841 * "join()" function
6842 */
6843 static void
6844f_join(typval_T *argvars, typval_T *rettv)
6845{
6846 garray_T ga;
6847 char_u *sep;
6848
6849 if (argvars[0].v_type != VAR_LIST)
6850 {
6851 EMSG(_(e_listreq));
6852 return;
6853 }
6854 if (argvars[0].vval.v_list == NULL)
6855 return;
6856 if (argvars[1].v_type == VAR_UNKNOWN)
6857 sep = (char_u *)" ";
6858 else
6859 sep = get_tv_string_chk(&argvars[1]);
6860
6861 rettv->v_type = VAR_STRING;
6862
6863 if (sep != NULL)
6864 {
6865 ga_init2(&ga, (int)sizeof(char), 80);
6866 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
6867 ga_append(&ga, NUL);
6868 rettv->vval.v_string = (char_u *)ga.ga_data;
6869 }
6870 else
6871 rettv->vval.v_string = NULL;
6872}
6873
6874/*
6875 * "js_decode()" function
6876 */
6877 static void
6878f_js_decode(typval_T *argvars, typval_T *rettv)
6879{
6880 js_read_T reader;
6881
6882 reader.js_buf = get_tv_string(&argvars[0]);
6883 reader.js_fill = NULL;
6884 reader.js_used = 0;
6885 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
6886 EMSG(_(e_invarg));
6887}
6888
6889/*
6890 * "js_encode()" function
6891 */
6892 static void
6893f_js_encode(typval_T *argvars, typval_T *rettv)
6894{
6895 rettv->v_type = VAR_STRING;
6896 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
6897}
6898
6899/*
6900 * "json_decode()" function
6901 */
6902 static void
6903f_json_decode(typval_T *argvars, typval_T *rettv)
6904{
6905 js_read_T reader;
6906
6907 reader.js_buf = get_tv_string(&argvars[0]);
6908 reader.js_fill = NULL;
6909 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01006910 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006911}
6912
6913/*
6914 * "json_encode()" function
6915 */
6916 static void
6917f_json_encode(typval_T *argvars, typval_T *rettv)
6918{
6919 rettv->v_type = VAR_STRING;
6920 rettv->vval.v_string = json_encode(&argvars[0], 0);
6921}
6922
6923/*
6924 * "keys()" function
6925 */
6926 static void
6927f_keys(typval_T *argvars, typval_T *rettv)
6928{
6929 dict_list(argvars, rettv, 0);
6930}
6931
6932/*
6933 * "last_buffer_nr()" function.
6934 */
6935 static void
6936f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
6937{
6938 int n = 0;
6939 buf_T *buf;
6940
Bram Moolenaar29323592016-07-24 22:04:11 +02006941 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006942 if (n < buf->b_fnum)
6943 n = buf->b_fnum;
6944
6945 rettv->vval.v_number = n;
6946}
6947
6948/*
6949 * "len()" function
6950 */
6951 static void
6952f_len(typval_T *argvars, typval_T *rettv)
6953{
6954 switch (argvars[0].v_type)
6955 {
6956 case VAR_STRING:
6957 case VAR_NUMBER:
6958 rettv->vval.v_number = (varnumber_T)STRLEN(
6959 get_tv_string(&argvars[0]));
6960 break;
6961 case VAR_LIST:
6962 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
6963 break;
6964 case VAR_DICT:
6965 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
6966 break;
6967 case VAR_UNKNOWN:
6968 case VAR_SPECIAL:
6969 case VAR_FLOAT:
6970 case VAR_FUNC:
6971 case VAR_PARTIAL:
6972 case VAR_JOB:
6973 case VAR_CHANNEL:
6974 EMSG(_("E701: Invalid type for len()"));
6975 break;
6976 }
6977}
6978
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006979 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01006980libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006981{
6982#ifdef FEAT_LIBCALL
6983 char_u *string_in;
6984 char_u **string_result;
6985 int nr_result;
6986#endif
6987
6988 rettv->v_type = type;
6989 if (type != VAR_NUMBER)
6990 rettv->vval.v_string = NULL;
6991
6992 if (check_restricted() || check_secure())
6993 return;
6994
6995#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02006996 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006997 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
6998 {
6999 string_in = NULL;
7000 if (argvars[2].v_type == VAR_STRING)
7001 string_in = argvars[2].vval.v_string;
7002 if (type == VAR_NUMBER)
7003 string_result = NULL;
7004 else
7005 string_result = &rettv->vval.v_string;
7006 if (mch_libcall(argvars[0].vval.v_string,
7007 argvars[1].vval.v_string,
7008 string_in,
7009 argvars[2].vval.v_number,
7010 string_result,
7011 &nr_result) == OK
7012 && type == VAR_NUMBER)
7013 rettv->vval.v_number = nr_result;
7014 }
7015#endif
7016}
7017
7018/*
7019 * "libcall()" function
7020 */
7021 static void
7022f_libcall(typval_T *argvars, typval_T *rettv)
7023{
7024 libcall_common(argvars, rettv, VAR_STRING);
7025}
7026
7027/*
7028 * "libcallnr()" function
7029 */
7030 static void
7031f_libcallnr(typval_T *argvars, typval_T *rettv)
7032{
7033 libcall_common(argvars, rettv, VAR_NUMBER);
7034}
7035
7036/*
7037 * "line(string)" function
7038 */
7039 static void
7040f_line(typval_T *argvars, typval_T *rettv)
7041{
7042 linenr_T lnum = 0;
7043 pos_T *fp;
7044 int fnum;
7045
7046 fp = var2fpos(&argvars[0], TRUE, &fnum);
7047 if (fp != NULL)
7048 lnum = fp->lnum;
7049 rettv->vval.v_number = lnum;
7050}
7051
7052/*
7053 * "line2byte(lnum)" function
7054 */
7055 static void
7056f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7057{
7058#ifndef FEAT_BYTEOFF
7059 rettv->vval.v_number = -1;
7060#else
7061 linenr_T lnum;
7062
7063 lnum = get_tv_lnum(argvars);
7064 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7065 rettv->vval.v_number = -1;
7066 else
7067 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7068 if (rettv->vval.v_number >= 0)
7069 ++rettv->vval.v_number;
7070#endif
7071}
7072
7073/*
7074 * "lispindent(lnum)" function
7075 */
7076 static void
7077f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7078{
7079#ifdef FEAT_LISP
7080 pos_T pos;
7081 linenr_T lnum;
7082
7083 pos = curwin->w_cursor;
7084 lnum = get_tv_lnum(argvars);
7085 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7086 {
7087 curwin->w_cursor.lnum = lnum;
7088 rettv->vval.v_number = get_lisp_indent();
7089 curwin->w_cursor = pos;
7090 }
7091 else
7092#endif
7093 rettv->vval.v_number = -1;
7094}
7095
7096/*
7097 * "localtime()" function
7098 */
7099 static void
7100f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7101{
7102 rettv->vval.v_number = (varnumber_T)time(NULL);
7103}
7104
7105static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
7106
7107 static void
7108get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7109{
7110 char_u *keys;
7111 char_u *which;
7112 char_u buf[NUMBUFLEN];
7113 char_u *keys_buf = NULL;
7114 char_u *rhs;
7115 int mode;
7116 int abbr = FALSE;
7117 int get_dict = FALSE;
7118 mapblock_T *mp;
7119 int buffer_local;
7120
7121 /* return empty string for failure */
7122 rettv->v_type = VAR_STRING;
7123 rettv->vval.v_string = NULL;
7124
7125 keys = get_tv_string(&argvars[0]);
7126 if (*keys == NUL)
7127 return;
7128
7129 if (argvars[1].v_type != VAR_UNKNOWN)
7130 {
7131 which = get_tv_string_buf_chk(&argvars[1], buf);
7132 if (argvars[2].v_type != VAR_UNKNOWN)
7133 {
7134 abbr = (int)get_tv_number(&argvars[2]);
7135 if (argvars[3].v_type != VAR_UNKNOWN)
7136 get_dict = (int)get_tv_number(&argvars[3]);
7137 }
7138 }
7139 else
7140 which = (char_u *)"";
7141 if (which == NULL)
7142 return;
7143
7144 mode = get_map_mode(&which, 0);
7145
7146 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7147 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7148 vim_free(keys_buf);
7149
7150 if (!get_dict)
7151 {
7152 /* Return a string. */
7153 if (rhs != NULL)
7154 rettv->vval.v_string = str2special_save(rhs, FALSE);
7155
7156 }
7157 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7158 {
7159 /* Return a dictionary. */
7160 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7161 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7162 dict_T *dict = rettv->vval.v_dict;
7163
7164 dict_add_nr_str(dict, "lhs", 0L, lhs);
7165 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
7166 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
7167 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
7168 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
7169 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
7170 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
7171 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
7172 dict_add_nr_str(dict, "mode", 0L, mapmode);
7173
7174 vim_free(lhs);
7175 vim_free(mapmode);
7176 }
7177}
7178
7179#ifdef FEAT_FLOAT
7180/*
7181 * "log()" function
7182 */
7183 static void
7184f_log(typval_T *argvars, typval_T *rettv)
7185{
7186 float_T f = 0.0;
7187
7188 rettv->v_type = VAR_FLOAT;
7189 if (get_float_arg(argvars, &f) == OK)
7190 rettv->vval.v_float = log(f);
7191 else
7192 rettv->vval.v_float = 0.0;
7193}
7194
7195/*
7196 * "log10()" function
7197 */
7198 static void
7199f_log10(typval_T *argvars, typval_T *rettv)
7200{
7201 float_T f = 0.0;
7202
7203 rettv->v_type = VAR_FLOAT;
7204 if (get_float_arg(argvars, &f) == OK)
7205 rettv->vval.v_float = log10(f);
7206 else
7207 rettv->vval.v_float = 0.0;
7208}
7209#endif
7210
7211#ifdef FEAT_LUA
7212/*
7213 * "luaeval()" function
7214 */
7215 static void
7216f_luaeval(typval_T *argvars, typval_T *rettv)
7217{
7218 char_u *str;
7219 char_u buf[NUMBUFLEN];
7220
7221 str = get_tv_string_buf(&argvars[0], buf);
7222 do_luaeval(str, argvars + 1, rettv);
7223}
7224#endif
7225
7226/*
7227 * "map()" function
7228 */
7229 static void
7230f_map(typval_T *argvars, typval_T *rettv)
7231{
7232 filter_map(argvars, rettv, TRUE);
7233}
7234
7235/*
7236 * "maparg()" function
7237 */
7238 static void
7239f_maparg(typval_T *argvars, typval_T *rettv)
7240{
7241 get_maparg(argvars, rettv, TRUE);
7242}
7243
7244/*
7245 * "mapcheck()" function
7246 */
7247 static void
7248f_mapcheck(typval_T *argvars, typval_T *rettv)
7249{
7250 get_maparg(argvars, rettv, FALSE);
7251}
7252
7253static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
7254
7255 static void
7256find_some_match(typval_T *argvars, typval_T *rettv, int type)
7257{
7258 char_u *str = NULL;
7259 long len = 0;
7260 char_u *expr = NULL;
7261 char_u *pat;
7262 regmatch_T regmatch;
7263 char_u patbuf[NUMBUFLEN];
7264 char_u strbuf[NUMBUFLEN];
7265 char_u *save_cpo;
7266 long start = 0;
7267 long nth = 1;
7268 colnr_T startcol = 0;
7269 int match = 0;
7270 list_T *l = NULL;
7271 listitem_T *li = NULL;
7272 long idx = 0;
7273 char_u *tofree = NULL;
7274
7275 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7276 save_cpo = p_cpo;
7277 p_cpo = (char_u *)"";
7278
7279 rettv->vval.v_number = -1;
7280 if (type == 3 || type == 4)
7281 {
7282 /* type 3: return empty list when there are no matches.
7283 * type 4: return ["", -1, -1, -1] */
7284 if (rettv_list_alloc(rettv) == FAIL)
7285 goto theend;
7286 if (type == 4
7287 && (list_append_string(rettv->vval.v_list,
7288 (char_u *)"", 0) == FAIL
7289 || list_append_number(rettv->vval.v_list,
7290 (varnumber_T)-1) == FAIL
7291 || list_append_number(rettv->vval.v_list,
7292 (varnumber_T)-1) == FAIL
7293 || list_append_number(rettv->vval.v_list,
7294 (varnumber_T)-1) == FAIL))
7295 {
7296 list_free(rettv->vval.v_list);
7297 rettv->vval.v_list = NULL;
7298 goto theend;
7299 }
7300 }
7301 else if (type == 2)
7302 {
7303 rettv->v_type = VAR_STRING;
7304 rettv->vval.v_string = NULL;
7305 }
7306
7307 if (argvars[0].v_type == VAR_LIST)
7308 {
7309 if ((l = argvars[0].vval.v_list) == NULL)
7310 goto theend;
7311 li = l->lv_first;
7312 }
7313 else
7314 {
7315 expr = str = get_tv_string(&argvars[0]);
7316 len = (long)STRLEN(str);
7317 }
7318
7319 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
7320 if (pat == NULL)
7321 goto theend;
7322
7323 if (argvars[2].v_type != VAR_UNKNOWN)
7324 {
7325 int error = FALSE;
7326
7327 start = (long)get_tv_number_chk(&argvars[2], &error);
7328 if (error)
7329 goto theend;
7330 if (l != NULL)
7331 {
7332 li = list_find(l, start);
7333 if (li == NULL)
7334 goto theend;
7335 idx = l->lv_idx; /* use the cached index */
7336 }
7337 else
7338 {
7339 if (start < 0)
7340 start = 0;
7341 if (start > len)
7342 goto theend;
7343 /* When "count" argument is there ignore matches before "start",
7344 * otherwise skip part of the string. Differs when pattern is "^"
7345 * or "\<". */
7346 if (argvars[3].v_type != VAR_UNKNOWN)
7347 startcol = start;
7348 else
7349 {
7350 str += start;
7351 len -= start;
7352 }
7353 }
7354
7355 if (argvars[3].v_type != VAR_UNKNOWN)
7356 nth = (long)get_tv_number_chk(&argvars[3], &error);
7357 if (error)
7358 goto theend;
7359 }
7360
7361 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7362 if (regmatch.regprog != NULL)
7363 {
7364 regmatch.rm_ic = p_ic;
7365
7366 for (;;)
7367 {
7368 if (l != NULL)
7369 {
7370 if (li == NULL)
7371 {
7372 match = FALSE;
7373 break;
7374 }
7375 vim_free(tofree);
7376 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7377 if (str == NULL)
7378 break;
7379 }
7380
7381 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7382
7383 if (match && --nth <= 0)
7384 break;
7385 if (l == NULL && !match)
7386 break;
7387
7388 /* Advance to just after the match. */
7389 if (l != NULL)
7390 {
7391 li = li->li_next;
7392 ++idx;
7393 }
7394 else
7395 {
7396#ifdef FEAT_MBYTE
7397 startcol = (colnr_T)(regmatch.startp[0]
7398 + (*mb_ptr2len)(regmatch.startp[0]) - str);
7399#else
7400 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
7401#endif
7402 if (startcol > (colnr_T)len
7403 || str + startcol <= regmatch.startp[0])
7404 {
7405 match = FALSE;
7406 break;
7407 }
7408 }
7409 }
7410
7411 if (match)
7412 {
7413 if (type == 4)
7414 {
7415 listitem_T *li1 = rettv->vval.v_list->lv_first;
7416 listitem_T *li2 = li1->li_next;
7417 listitem_T *li3 = li2->li_next;
7418 listitem_T *li4 = li3->li_next;
7419
7420 vim_free(li1->li_tv.vval.v_string);
7421 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7422 (int)(regmatch.endp[0] - regmatch.startp[0]));
7423 li3->li_tv.vval.v_number =
7424 (varnumber_T)(regmatch.startp[0] - expr);
7425 li4->li_tv.vval.v_number =
7426 (varnumber_T)(regmatch.endp[0] - expr);
7427 if (l != NULL)
7428 li2->li_tv.vval.v_number = (varnumber_T)idx;
7429 }
7430 else if (type == 3)
7431 {
7432 int i;
7433
7434 /* return list with matched string and submatches */
7435 for (i = 0; i < NSUBEXP; ++i)
7436 {
7437 if (regmatch.endp[i] == NULL)
7438 {
7439 if (list_append_string(rettv->vval.v_list,
7440 (char_u *)"", 0) == FAIL)
7441 break;
7442 }
7443 else if (list_append_string(rettv->vval.v_list,
7444 regmatch.startp[i],
7445 (int)(regmatch.endp[i] - regmatch.startp[i]))
7446 == FAIL)
7447 break;
7448 }
7449 }
7450 else if (type == 2)
7451 {
7452 /* return matched string */
7453 if (l != NULL)
7454 copy_tv(&li->li_tv, rettv);
7455 else
7456 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
7457 (int)(regmatch.endp[0] - regmatch.startp[0]));
7458 }
7459 else if (l != NULL)
7460 rettv->vval.v_number = idx;
7461 else
7462 {
7463 if (type != 0)
7464 rettv->vval.v_number =
7465 (varnumber_T)(regmatch.startp[0] - str);
7466 else
7467 rettv->vval.v_number =
7468 (varnumber_T)(regmatch.endp[0] - str);
7469 rettv->vval.v_number += (varnumber_T)(str - expr);
7470 }
7471 }
7472 vim_regfree(regmatch.regprog);
7473 }
7474
7475 if (type == 4 && l == NULL)
7476 /* matchstrpos() without a list: drop the second item. */
7477 listitem_remove(rettv->vval.v_list,
7478 rettv->vval.v_list->lv_first->li_next);
7479
7480theend:
7481 vim_free(tofree);
7482 p_cpo = save_cpo;
7483}
7484
7485/*
7486 * "match()" function
7487 */
7488 static void
7489f_match(typval_T *argvars, typval_T *rettv)
7490{
7491 find_some_match(argvars, rettv, 1);
7492}
7493
7494/*
7495 * "matchadd()" function
7496 */
7497 static void
7498f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7499{
7500#ifdef FEAT_SEARCH_EXTRA
7501 char_u buf[NUMBUFLEN];
7502 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
7503 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
7504 int prio = 10; /* default priority */
7505 int id = -1;
7506 int error = FALSE;
7507 char_u *conceal_char = NULL;
7508
7509 rettv->vval.v_number = -1;
7510
7511 if (grp == NULL || pat == NULL)
7512 return;
7513 if (argvars[2].v_type != VAR_UNKNOWN)
7514 {
7515 prio = (int)get_tv_number_chk(&argvars[2], &error);
7516 if (argvars[3].v_type != VAR_UNKNOWN)
7517 {
7518 id = (int)get_tv_number_chk(&argvars[3], &error);
7519 if (argvars[4].v_type != VAR_UNKNOWN)
7520 {
7521 if (argvars[4].v_type != VAR_DICT)
7522 {
7523 EMSG(_(e_dictreq));
7524 return;
7525 }
7526 if (dict_find(argvars[4].vval.v_dict,
7527 (char_u *)"conceal", -1) != NULL)
7528 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7529 (char_u *)"conceal", FALSE);
7530 }
7531 }
7532 }
7533 if (error == TRUE)
7534 return;
7535 if (id >= 1 && id <= 3)
7536 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007537 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007538 return;
7539 }
7540
7541 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
7542 conceal_char);
7543#endif
7544}
7545
7546/*
7547 * "matchaddpos()" function
7548 */
7549 static void
7550f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7551{
7552#ifdef FEAT_SEARCH_EXTRA
7553 char_u buf[NUMBUFLEN];
7554 char_u *group;
7555 int prio = 10;
7556 int id = -1;
7557 int error = FALSE;
7558 list_T *l;
7559 char_u *conceal_char = NULL;
7560
7561 rettv->vval.v_number = -1;
7562
7563 group = get_tv_string_buf_chk(&argvars[0], buf);
7564 if (group == NULL)
7565 return;
7566
7567 if (argvars[1].v_type != VAR_LIST)
7568 {
7569 EMSG2(_(e_listarg), "matchaddpos()");
7570 return;
7571 }
7572 l = argvars[1].vval.v_list;
7573 if (l == NULL)
7574 return;
7575
7576 if (argvars[2].v_type != VAR_UNKNOWN)
7577 {
7578 prio = (int)get_tv_number_chk(&argvars[2], &error);
7579 if (argvars[3].v_type != VAR_UNKNOWN)
7580 {
7581 id = (int)get_tv_number_chk(&argvars[3], &error);
7582 if (argvars[4].v_type != VAR_UNKNOWN)
7583 {
7584 if (argvars[4].v_type != VAR_DICT)
7585 {
7586 EMSG(_(e_dictreq));
7587 return;
7588 }
7589 if (dict_find(argvars[4].vval.v_dict,
7590 (char_u *)"conceal", -1) != NULL)
7591 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7592 (char_u *)"conceal", FALSE);
7593 }
7594 }
7595 }
7596 if (error == TRUE)
7597 return;
7598
7599 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
7600 if (id == 1 || id == 2)
7601 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007602 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007603 return;
7604 }
7605
7606 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
7607 conceal_char);
7608#endif
7609}
7610
7611/*
7612 * "matcharg()" function
7613 */
7614 static void
7615f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
7616{
7617 if (rettv_list_alloc(rettv) == OK)
7618 {
7619#ifdef FEAT_SEARCH_EXTRA
7620 int id = (int)get_tv_number(&argvars[0]);
7621 matchitem_T *m;
7622
7623 if (id >= 1 && id <= 3)
7624 {
7625 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
7626 {
7627 list_append_string(rettv->vval.v_list,
7628 syn_id2name(m->hlg_id), -1);
7629 list_append_string(rettv->vval.v_list, m->pattern, -1);
7630 }
7631 else
7632 {
7633 list_append_string(rettv->vval.v_list, NULL, -1);
7634 list_append_string(rettv->vval.v_list, NULL, -1);
7635 }
7636 }
7637#endif
7638 }
7639}
7640
7641/*
7642 * "matchdelete()" function
7643 */
7644 static void
7645f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7646{
7647#ifdef FEAT_SEARCH_EXTRA
7648 rettv->vval.v_number = match_delete(curwin,
7649 (int)get_tv_number(&argvars[0]), TRUE);
7650#endif
7651}
7652
7653/*
7654 * "matchend()" function
7655 */
7656 static void
7657f_matchend(typval_T *argvars, typval_T *rettv)
7658{
7659 find_some_match(argvars, rettv, 0);
7660}
7661
7662/*
7663 * "matchlist()" function
7664 */
7665 static void
7666f_matchlist(typval_T *argvars, typval_T *rettv)
7667{
7668 find_some_match(argvars, rettv, 3);
7669}
7670
7671/*
7672 * "matchstr()" function
7673 */
7674 static void
7675f_matchstr(typval_T *argvars, typval_T *rettv)
7676{
7677 find_some_match(argvars, rettv, 2);
7678}
7679
7680/*
7681 * "matchstrpos()" function
7682 */
7683 static void
7684f_matchstrpos(typval_T *argvars, typval_T *rettv)
7685{
7686 find_some_match(argvars, rettv, 4);
7687}
7688
7689static void max_min(typval_T *argvars, typval_T *rettv, int domax);
7690
7691 static void
7692max_min(typval_T *argvars, typval_T *rettv, int domax)
7693{
7694 varnumber_T n = 0;
7695 varnumber_T i;
7696 int error = FALSE;
7697
7698 if (argvars[0].v_type == VAR_LIST)
7699 {
7700 list_T *l;
7701 listitem_T *li;
7702
7703 l = argvars[0].vval.v_list;
7704 if (l != NULL)
7705 {
7706 li = l->lv_first;
7707 if (li != NULL)
7708 {
7709 n = get_tv_number_chk(&li->li_tv, &error);
7710 for (;;)
7711 {
7712 li = li->li_next;
7713 if (li == NULL)
7714 break;
7715 i = get_tv_number_chk(&li->li_tv, &error);
7716 if (domax ? i > n : i < n)
7717 n = i;
7718 }
7719 }
7720 }
7721 }
7722 else if (argvars[0].v_type == VAR_DICT)
7723 {
7724 dict_T *d;
7725 int first = TRUE;
7726 hashitem_T *hi;
7727 int todo;
7728
7729 d = argvars[0].vval.v_dict;
7730 if (d != NULL)
7731 {
7732 todo = (int)d->dv_hashtab.ht_used;
7733 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
7734 {
7735 if (!HASHITEM_EMPTY(hi))
7736 {
7737 --todo;
7738 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
7739 if (first)
7740 {
7741 n = i;
7742 first = FALSE;
7743 }
7744 else if (domax ? i > n : i < n)
7745 n = i;
7746 }
7747 }
7748 }
7749 }
7750 else
Bram Moolenaar26b84332016-09-04 21:42:36 +02007751 EMSG2(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007752 rettv->vval.v_number = error ? 0 : n;
7753}
7754
7755/*
7756 * "max()" function
7757 */
7758 static void
7759f_max(typval_T *argvars, typval_T *rettv)
7760{
7761 max_min(argvars, rettv, TRUE);
7762}
7763
7764/*
7765 * "min()" function
7766 */
7767 static void
7768f_min(typval_T *argvars, typval_T *rettv)
7769{
7770 max_min(argvars, rettv, FALSE);
7771}
7772
7773static int mkdir_recurse(char_u *dir, int prot);
7774
7775/*
7776 * Create the directory in which "dir" is located, and higher levels when
7777 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02007778 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007779 */
7780 static int
7781mkdir_recurse(char_u *dir, int prot)
7782{
7783 char_u *p;
7784 char_u *updir;
7785 int r = FAIL;
7786
7787 /* Get end of directory name in "dir".
7788 * We're done when it's "/" or "c:/". */
7789 p = gettail_sep(dir);
7790 if (p <= get_past_head(dir))
7791 return OK;
7792
7793 /* If the directory exists we're done. Otherwise: create it.*/
7794 updir = vim_strnsave(dir, (int)(p - dir));
7795 if (updir == NULL)
7796 return FAIL;
7797 if (mch_isdir(updir))
7798 r = OK;
7799 else if (mkdir_recurse(updir, prot) == OK)
7800 r = vim_mkdir_emsg(updir, prot);
7801 vim_free(updir);
7802 return r;
7803}
7804
7805#ifdef vim_mkdir
7806/*
7807 * "mkdir()" function
7808 */
7809 static void
7810f_mkdir(typval_T *argvars, typval_T *rettv)
7811{
7812 char_u *dir;
7813 char_u buf[NUMBUFLEN];
7814 int prot = 0755;
7815
7816 rettv->vval.v_number = FAIL;
7817 if (check_restricted() || check_secure())
7818 return;
7819
7820 dir = get_tv_string_buf(&argvars[0], buf);
7821 if (*dir == NUL)
7822 rettv->vval.v_number = FAIL;
7823 else
7824 {
7825 if (*gettail(dir) == NUL)
7826 /* remove trailing slashes */
7827 *gettail_sep(dir) = NUL;
7828
7829 if (argvars[1].v_type != VAR_UNKNOWN)
7830 {
7831 if (argvars[2].v_type != VAR_UNKNOWN)
7832 prot = (int)get_tv_number_chk(&argvars[2], NULL);
7833 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
7834 mkdir_recurse(dir, prot);
7835 }
7836 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
7837 }
7838}
7839#endif
7840
7841/*
7842 * "mode()" function
7843 */
7844 static void
7845f_mode(typval_T *argvars, typval_T *rettv)
7846{
7847 char_u buf[3];
7848
7849 buf[1] = NUL;
7850 buf[2] = NUL;
7851
7852 if (time_for_testing == 93784)
7853 {
7854 /* Testing the two-character code. */
7855 buf[0] = 'x';
7856 buf[1] = '!';
7857 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02007858#ifdef FEAT_TERMINAL
7859 else if (term_use_loop())
7860 buf[0] = 't';
7861#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007862 else if (VIsual_active)
7863 {
7864 if (VIsual_select)
7865 buf[0] = VIsual_mode + 's' - 'v';
7866 else
7867 buf[0] = VIsual_mode;
7868 }
7869 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
7870 || State == CONFIRM)
7871 {
7872 buf[0] = 'r';
7873 if (State == ASKMORE)
7874 buf[1] = 'm';
7875 else if (State == CONFIRM)
7876 buf[1] = '?';
7877 }
7878 else if (State == EXTERNCMD)
7879 buf[0] = '!';
7880 else if (State & INSERT)
7881 {
7882#ifdef FEAT_VREPLACE
7883 if (State & VREPLACE_FLAG)
7884 {
7885 buf[0] = 'R';
7886 buf[1] = 'v';
7887 }
7888 else
7889#endif
Bram Moolenaare90858d2017-02-01 17:24:34 +01007890 {
7891 if (State & REPLACE_FLAG)
7892 buf[0] = 'R';
7893 else
7894 buf[0] = 'i';
7895#ifdef FEAT_INS_EXPAND
7896 if (ins_compl_active())
7897 buf[1] = 'c';
7898 else if (ctrl_x_mode == 1)
7899 buf[1] = 'x';
7900#endif
7901 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007902 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01007903 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007904 {
7905 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01007906 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007907 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01007908 else if (exmode_active == EXMODE_NORMAL)
7909 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007910 }
7911 else
7912 {
7913 buf[0] = 'n';
7914 if (finish_op)
7915 buf[1] = 'o';
7916 }
7917
7918 /* Clear out the minor mode when the argument is not a non-zero number or
7919 * non-empty string. */
7920 if (!non_zero_arg(&argvars[0]))
7921 buf[1] = NUL;
7922
7923 rettv->vval.v_string = vim_strsave(buf);
7924 rettv->v_type = VAR_STRING;
7925}
7926
7927#if defined(FEAT_MZSCHEME) || defined(PROTO)
7928/*
7929 * "mzeval()" function
7930 */
7931 static void
7932f_mzeval(typval_T *argvars, typval_T *rettv)
7933{
7934 char_u *str;
7935 char_u buf[NUMBUFLEN];
7936
7937 str = get_tv_string_buf(&argvars[0], buf);
7938 do_mzeval(str, rettv);
7939}
7940
7941 void
7942mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
7943{
7944 typval_T argvars[3];
7945
7946 argvars[0].v_type = VAR_STRING;
7947 argvars[0].vval.v_string = name;
7948 copy_tv(args, &argvars[1]);
7949 argvars[2].v_type = VAR_UNKNOWN;
7950 f_call(argvars, rettv);
7951 clear_tv(&argvars[1]);
7952}
7953#endif
7954
7955/*
7956 * "nextnonblank()" function
7957 */
7958 static void
7959f_nextnonblank(typval_T *argvars, typval_T *rettv)
7960{
7961 linenr_T lnum;
7962
7963 for (lnum = get_tv_lnum(argvars); ; ++lnum)
7964 {
7965 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
7966 {
7967 lnum = 0;
7968 break;
7969 }
7970 if (*skipwhite(ml_get(lnum)) != NUL)
7971 break;
7972 }
7973 rettv->vval.v_number = lnum;
7974}
7975
7976/*
7977 * "nr2char()" function
7978 */
7979 static void
7980f_nr2char(typval_T *argvars, typval_T *rettv)
7981{
7982 char_u buf[NUMBUFLEN];
7983
7984#ifdef FEAT_MBYTE
7985 if (has_mbyte)
7986 {
7987 int utf8 = 0;
7988
7989 if (argvars[1].v_type != VAR_UNKNOWN)
7990 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
7991 if (utf8)
7992 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
7993 else
7994 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
7995 }
7996 else
7997#endif
7998 {
7999 buf[0] = (char_u)get_tv_number(&argvars[0]);
8000 buf[1] = NUL;
8001 }
8002 rettv->v_type = VAR_STRING;
8003 rettv->vval.v_string = vim_strsave(buf);
8004}
8005
8006/*
8007 * "or(expr, expr)" function
8008 */
8009 static void
8010f_or(typval_T *argvars, typval_T *rettv)
8011{
8012 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8013 | get_tv_number_chk(&argvars[1], NULL);
8014}
8015
8016/*
8017 * "pathshorten()" function
8018 */
8019 static void
8020f_pathshorten(typval_T *argvars, typval_T *rettv)
8021{
8022 char_u *p;
8023
8024 rettv->v_type = VAR_STRING;
8025 p = get_tv_string_chk(&argvars[0]);
8026 if (p == NULL)
8027 rettv->vval.v_string = NULL;
8028 else
8029 {
8030 p = vim_strsave(p);
8031 rettv->vval.v_string = p;
8032 if (p != NULL)
8033 shorten_dir(p);
8034 }
8035}
8036
8037#ifdef FEAT_PERL
8038/*
8039 * "perleval()" function
8040 */
8041 static void
8042f_perleval(typval_T *argvars, typval_T *rettv)
8043{
8044 char_u *str;
8045 char_u buf[NUMBUFLEN];
8046
8047 str = get_tv_string_buf(&argvars[0], buf);
8048 do_perleval(str, rettv);
8049}
8050#endif
8051
8052#ifdef FEAT_FLOAT
8053/*
8054 * "pow()" function
8055 */
8056 static void
8057f_pow(typval_T *argvars, typval_T *rettv)
8058{
8059 float_T fx = 0.0, fy = 0.0;
8060
8061 rettv->v_type = VAR_FLOAT;
8062 if (get_float_arg(argvars, &fx) == OK
8063 && get_float_arg(&argvars[1], &fy) == OK)
8064 rettv->vval.v_float = pow(fx, fy);
8065 else
8066 rettv->vval.v_float = 0.0;
8067}
8068#endif
8069
8070/*
8071 * "prevnonblank()" function
8072 */
8073 static void
8074f_prevnonblank(typval_T *argvars, typval_T *rettv)
8075{
8076 linenr_T lnum;
8077
8078 lnum = get_tv_lnum(argvars);
8079 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8080 lnum = 0;
8081 else
8082 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8083 --lnum;
8084 rettv->vval.v_number = lnum;
8085}
8086
8087/* This dummy va_list is here because:
8088 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8089 * - locally in the function results in a "used before set" warning
8090 * - using va_start() to initialize it gives "function with fixed args" error */
8091static va_list ap;
8092
8093/*
8094 * "printf()" function
8095 */
8096 static void
8097f_printf(typval_T *argvars, typval_T *rettv)
8098{
8099 char_u buf[NUMBUFLEN];
8100 int len;
8101 char_u *s;
8102 int saved_did_emsg = did_emsg;
8103 char *fmt;
8104
8105 rettv->v_type = VAR_STRING;
8106 rettv->vval.v_string = NULL;
8107
8108 /* Get the required length, allocate the buffer and do it for real. */
8109 did_emsg = FALSE;
8110 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008111 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008112 if (!did_emsg)
8113 {
8114 s = alloc(len + 1);
8115 if (s != NULL)
8116 {
8117 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008118 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8119 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008120 }
8121 }
8122 did_emsg |= saved_did_emsg;
8123}
8124
8125/*
8126 * "pumvisible()" function
8127 */
8128 static void
8129f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8130{
8131#ifdef FEAT_INS_EXPAND
8132 if (pum_visible())
8133 rettv->vval.v_number = 1;
8134#endif
8135}
8136
8137#ifdef FEAT_PYTHON3
8138/*
8139 * "py3eval()" function
8140 */
8141 static void
8142f_py3eval(typval_T *argvars, typval_T *rettv)
8143{
8144 char_u *str;
8145 char_u buf[NUMBUFLEN];
8146
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008147 if (p_pyx == 0)
8148 p_pyx = 3;
8149
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008150 str = get_tv_string_buf(&argvars[0], buf);
8151 do_py3eval(str, rettv);
8152}
8153#endif
8154
8155#ifdef FEAT_PYTHON
8156/*
8157 * "pyeval()" function
8158 */
8159 static void
8160f_pyeval(typval_T *argvars, typval_T *rettv)
8161{
8162 char_u *str;
8163 char_u buf[NUMBUFLEN];
8164
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008165 if (p_pyx == 0)
8166 p_pyx = 2;
8167
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008168 str = get_tv_string_buf(&argvars[0], buf);
8169 do_pyeval(str, rettv);
8170}
8171#endif
8172
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008173#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8174/*
8175 * "pyxeval()" function
8176 */
8177 static void
8178f_pyxeval(typval_T *argvars, typval_T *rettv)
8179{
8180# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8181 init_pyxversion();
8182 if (p_pyx == 2)
8183 f_pyeval(argvars, rettv);
8184 else
8185 f_py3eval(argvars, rettv);
8186# elif defined(FEAT_PYTHON)
8187 f_pyeval(argvars, rettv);
8188# elif defined(FEAT_PYTHON3)
8189 f_py3eval(argvars, rettv);
8190# endif
8191}
8192#endif
8193
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008194/*
8195 * "range()" function
8196 */
8197 static void
8198f_range(typval_T *argvars, typval_T *rettv)
8199{
8200 varnumber_T start;
8201 varnumber_T end;
8202 varnumber_T stride = 1;
8203 varnumber_T i;
8204 int error = FALSE;
8205
8206 start = get_tv_number_chk(&argvars[0], &error);
8207 if (argvars[1].v_type == VAR_UNKNOWN)
8208 {
8209 end = start - 1;
8210 start = 0;
8211 }
8212 else
8213 {
8214 end = get_tv_number_chk(&argvars[1], &error);
8215 if (argvars[2].v_type != VAR_UNKNOWN)
8216 stride = get_tv_number_chk(&argvars[2], &error);
8217 }
8218
8219 if (error)
8220 return; /* type error; errmsg already given */
8221 if (stride == 0)
8222 EMSG(_("E726: Stride is zero"));
8223 else if (stride > 0 ? end + 1 < start : end - 1 > start)
8224 EMSG(_("E727: Start past end"));
8225 else
8226 {
8227 if (rettv_list_alloc(rettv) == OK)
8228 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8229 if (list_append_number(rettv->vval.v_list,
8230 (varnumber_T)i) == FAIL)
8231 break;
8232 }
8233}
8234
8235/*
8236 * "readfile()" function
8237 */
8238 static void
8239f_readfile(typval_T *argvars, typval_T *rettv)
8240{
8241 int binary = FALSE;
8242 int failed = FALSE;
8243 char_u *fname;
8244 FILE *fd;
8245 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8246 int io_size = sizeof(buf);
8247 int readlen; /* size of last fread() */
8248 char_u *prev = NULL; /* previously read bytes, if any */
8249 long prevlen = 0; /* length of data in prev */
8250 long prevsize = 0; /* size of prev buffer */
8251 long maxline = MAXLNUM;
8252 long cnt = 0;
8253 char_u *p; /* position in buf */
8254 char_u *start; /* start of current line */
8255
8256 if (argvars[1].v_type != VAR_UNKNOWN)
8257 {
8258 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
8259 binary = TRUE;
8260 if (argvars[2].v_type != VAR_UNKNOWN)
8261 maxline = (long)get_tv_number(&argvars[2]);
8262 }
8263
8264 if (rettv_list_alloc(rettv) == FAIL)
8265 return;
8266
8267 /* Always open the file in binary mode, library functions have a mind of
8268 * their own about CR-LF conversion. */
8269 fname = get_tv_string(&argvars[0]);
8270 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8271 {
8272 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
8273 return;
8274 }
8275
8276 while (cnt < maxline || maxline < 0)
8277 {
8278 readlen = (int)fread(buf, 1, io_size, fd);
8279
8280 /* This for loop processes what was read, but is also entered at end
8281 * of file so that either:
8282 * - an incomplete line gets written
8283 * - a "binary" file gets an empty line at the end if it ends in a
8284 * newline. */
8285 for (p = buf, start = buf;
8286 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8287 ++p)
8288 {
8289 if (*p == '\n' || readlen <= 0)
8290 {
8291 listitem_T *li;
8292 char_u *s = NULL;
8293 long_u len = p - start;
8294
8295 /* Finished a line. Remove CRs before NL. */
8296 if (readlen > 0 && !binary)
8297 {
8298 while (len > 0 && start[len - 1] == '\r')
8299 --len;
8300 /* removal may cross back to the "prev" string */
8301 if (len == 0)
8302 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8303 --prevlen;
8304 }
8305 if (prevlen == 0)
8306 s = vim_strnsave(start, (int)len);
8307 else
8308 {
8309 /* Change "prev" buffer to be the right size. This way
8310 * the bytes are only copied once, and very long lines are
8311 * allocated only once. */
8312 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8313 {
8314 mch_memmove(s + prevlen, start, len);
8315 s[prevlen + len] = NUL;
8316 prev = NULL; /* the list will own the string */
8317 prevlen = prevsize = 0;
8318 }
8319 }
8320 if (s == NULL)
8321 {
8322 do_outofmem_msg((long_u) prevlen + len + 1);
8323 failed = TRUE;
8324 break;
8325 }
8326
8327 if ((li = listitem_alloc()) == NULL)
8328 {
8329 vim_free(s);
8330 failed = TRUE;
8331 break;
8332 }
8333 li->li_tv.v_type = VAR_STRING;
8334 li->li_tv.v_lock = 0;
8335 li->li_tv.vval.v_string = s;
8336 list_append(rettv->vval.v_list, li);
8337
8338 start = p + 1; /* step over newline */
8339 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
8340 break;
8341 }
8342 else if (*p == NUL)
8343 *p = '\n';
8344#ifdef FEAT_MBYTE
8345 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
8346 * when finding the BF and check the previous two bytes. */
8347 else if (*p == 0xbf && enc_utf8 && !binary)
8348 {
8349 /* Find the two bytes before the 0xbf. If p is at buf, or buf
8350 * + 1, these may be in the "prev" string. */
8351 char_u back1 = p >= buf + 1 ? p[-1]
8352 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
8353 char_u back2 = p >= buf + 2 ? p[-2]
8354 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
8355 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
8356
8357 if (back2 == 0xef && back1 == 0xbb)
8358 {
8359 char_u *dest = p - 2;
8360
8361 /* Usually a BOM is at the beginning of a file, and so at
8362 * the beginning of a line; then we can just step over it.
8363 */
8364 if (start == dest)
8365 start = p + 1;
8366 else
8367 {
8368 /* have to shuffle buf to close gap */
8369 int adjust_prevlen = 0;
8370
8371 if (dest < buf)
8372 {
8373 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
8374 dest = buf;
8375 }
8376 if (readlen > p - buf + 1)
8377 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
8378 readlen -= 3 - adjust_prevlen;
8379 prevlen -= adjust_prevlen;
8380 p = dest - 1;
8381 }
8382 }
8383 }
8384#endif
8385 } /* for */
8386
8387 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
8388 break;
8389 if (start < p)
8390 {
8391 /* There's part of a line in buf, store it in "prev". */
8392 if (p - start + prevlen >= prevsize)
8393 {
8394 /* need bigger "prev" buffer */
8395 char_u *newprev;
8396
8397 /* A common use case is ordinary text files and "prev" gets a
8398 * fragment of a line, so the first allocation is made
8399 * small, to avoid repeatedly 'allocing' large and
8400 * 'reallocing' small. */
8401 if (prevsize == 0)
8402 prevsize = (long)(p - start);
8403 else
8404 {
8405 long grow50pc = (prevsize * 3) / 2;
8406 long growmin = (long)((p - start) * 2 + prevlen);
8407 prevsize = grow50pc > growmin ? grow50pc : growmin;
8408 }
8409 newprev = prev == NULL ? alloc(prevsize)
8410 : vim_realloc(prev, prevsize);
8411 if (newprev == NULL)
8412 {
8413 do_outofmem_msg((long_u)prevsize);
8414 failed = TRUE;
8415 break;
8416 }
8417 prev = newprev;
8418 }
8419 /* Add the line part to end of "prev". */
8420 mch_memmove(prev + prevlen, start, p - start);
8421 prevlen += (long)(p - start);
8422 }
8423 } /* while */
8424
8425 /*
8426 * For a negative line count use only the lines at the end of the file,
8427 * free the rest.
8428 */
8429 if (!failed && maxline < 0)
8430 while (cnt > -maxline)
8431 {
8432 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
8433 --cnt;
8434 }
8435
8436 if (failed)
8437 {
8438 list_free(rettv->vval.v_list);
8439 /* readfile doc says an empty list is returned on error */
8440 rettv->vval.v_list = list_alloc();
8441 }
8442
8443 vim_free(prev);
8444 fclose(fd);
8445}
8446
8447#if defined(FEAT_RELTIME)
8448static int list2proftime(typval_T *arg, proftime_T *tm);
8449
8450/*
8451 * Convert a List to proftime_T.
8452 * Return FAIL when there is something wrong.
8453 */
8454 static int
8455list2proftime(typval_T *arg, proftime_T *tm)
8456{
8457 long n1, n2;
8458 int error = FALSE;
8459
8460 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
8461 || arg->vval.v_list->lv_len != 2)
8462 return FAIL;
8463 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
8464 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
8465# ifdef WIN3264
8466 tm->HighPart = n1;
8467 tm->LowPart = n2;
8468# else
8469 tm->tv_sec = n1;
8470 tm->tv_usec = n2;
8471# endif
8472 return error ? FAIL : OK;
8473}
8474#endif /* FEAT_RELTIME */
8475
8476/*
8477 * "reltime()" function
8478 */
8479 static void
8480f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8481{
8482#ifdef FEAT_RELTIME
8483 proftime_T res;
8484 proftime_T start;
8485
8486 if (argvars[0].v_type == VAR_UNKNOWN)
8487 {
8488 /* No arguments: get current time. */
8489 profile_start(&res);
8490 }
8491 else if (argvars[1].v_type == VAR_UNKNOWN)
8492 {
8493 if (list2proftime(&argvars[0], &res) == FAIL)
8494 return;
8495 profile_end(&res);
8496 }
8497 else
8498 {
8499 /* Two arguments: compute the difference. */
8500 if (list2proftime(&argvars[0], &start) == FAIL
8501 || list2proftime(&argvars[1], &res) == FAIL)
8502 return;
8503 profile_sub(&res, &start);
8504 }
8505
8506 if (rettv_list_alloc(rettv) == OK)
8507 {
8508 long n1, n2;
8509
8510# ifdef WIN3264
8511 n1 = res.HighPart;
8512 n2 = res.LowPart;
8513# else
8514 n1 = res.tv_sec;
8515 n2 = res.tv_usec;
8516# endif
8517 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
8518 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
8519 }
8520#endif
8521}
8522
8523#ifdef FEAT_FLOAT
8524/*
8525 * "reltimefloat()" function
8526 */
8527 static void
8528f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
8529{
8530# ifdef FEAT_RELTIME
8531 proftime_T tm;
8532# endif
8533
8534 rettv->v_type = VAR_FLOAT;
8535 rettv->vval.v_float = 0;
8536# ifdef FEAT_RELTIME
8537 if (list2proftime(&argvars[0], &tm) == OK)
8538 rettv->vval.v_float = profile_float(&tm);
8539# endif
8540}
8541#endif
8542
8543/*
8544 * "reltimestr()" function
8545 */
8546 static void
8547f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
8548{
8549#ifdef FEAT_RELTIME
8550 proftime_T tm;
8551#endif
8552
8553 rettv->v_type = VAR_STRING;
8554 rettv->vval.v_string = NULL;
8555#ifdef FEAT_RELTIME
8556 if (list2proftime(&argvars[0], &tm) == OK)
8557 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
8558#endif
8559}
8560
8561#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
8562static void make_connection(void);
8563static int check_connection(void);
8564
8565 static void
8566make_connection(void)
8567{
8568 if (X_DISPLAY == NULL
8569# ifdef FEAT_GUI
8570 && !gui.in_use
8571# endif
8572 )
8573 {
8574 x_force_connect = TRUE;
8575 setup_term_clip();
8576 x_force_connect = FALSE;
8577 }
8578}
8579
8580 static int
8581check_connection(void)
8582{
8583 make_connection();
8584 if (X_DISPLAY == NULL)
8585 {
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01008586 EMSG(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008587 return FAIL;
8588 }
8589 return OK;
8590}
8591#endif
8592
8593#ifdef FEAT_CLIENTSERVER
8594 static void
8595remote_common(typval_T *argvars, typval_T *rettv, int expr)
8596{
8597 char_u *server_name;
8598 char_u *keys;
8599 char_u *r = NULL;
8600 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008601 int timeout = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008602# ifdef WIN32
8603 HWND w;
8604# else
8605 Window w;
8606# endif
8607
8608 if (check_restricted() || check_secure())
8609 return;
8610
8611# ifdef FEAT_X11
8612 if (check_connection() == FAIL)
8613 return;
8614# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008615 if (argvars[2].v_type != VAR_UNKNOWN
8616 && argvars[3].v_type != VAR_UNKNOWN)
8617 timeout = get_tv_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008618
8619 server_name = get_tv_string_chk(&argvars[0]);
8620 if (server_name == NULL)
8621 return; /* type error; errmsg already given */
8622 keys = get_tv_string_buf(&argvars[1], buf);
8623# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008624 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008625# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008626 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
8627 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008628# endif
8629 {
8630 if (r != NULL)
8631 EMSG(r); /* sending worked but evaluation failed */
8632 else
8633 EMSG2(_("E241: Unable to send to %s"), server_name);
8634 return;
8635 }
8636
8637 rettv->vval.v_string = r;
8638
8639 if (argvars[2].v_type != VAR_UNKNOWN)
8640 {
8641 dictitem_T v;
8642 char_u str[30];
8643 char_u *idvar;
8644
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008645 idvar = get_tv_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008646 if (idvar != NULL && *idvar != NUL)
8647 {
8648 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
8649 v.di_tv.v_type = VAR_STRING;
8650 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008651 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008652 vim_free(v.di_tv.vval.v_string);
8653 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008654 }
8655}
8656#endif
8657
8658/*
8659 * "remote_expr()" function
8660 */
8661 static void
8662f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
8663{
8664 rettv->v_type = VAR_STRING;
8665 rettv->vval.v_string = NULL;
8666#ifdef FEAT_CLIENTSERVER
8667 remote_common(argvars, rettv, TRUE);
8668#endif
8669}
8670
8671/*
8672 * "remote_foreground()" function
8673 */
8674 static void
8675f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8676{
8677#ifdef FEAT_CLIENTSERVER
8678# ifdef WIN32
8679 /* On Win32 it's done in this application. */
8680 {
8681 char_u *server_name = get_tv_string_chk(&argvars[0]);
8682
8683 if (server_name != NULL)
8684 serverForeground(server_name);
8685 }
8686# else
8687 /* Send a foreground() expression to the server. */
8688 argvars[1].v_type = VAR_STRING;
8689 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
8690 argvars[2].v_type = VAR_UNKNOWN;
8691 remote_common(argvars, rettv, TRUE);
8692 vim_free(argvars[1].vval.v_string);
8693# endif
8694#endif
8695}
8696
8697 static void
8698f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
8699{
8700#ifdef FEAT_CLIENTSERVER
8701 dictitem_T v;
8702 char_u *s = NULL;
8703# ifdef WIN32
8704 long_u n = 0;
8705# endif
8706 char_u *serverid;
8707
8708 if (check_restricted() || check_secure())
8709 {
8710 rettv->vval.v_number = -1;
8711 return;
8712 }
8713 serverid = get_tv_string_chk(&argvars[0]);
8714 if (serverid == NULL)
8715 {
8716 rettv->vval.v_number = -1;
8717 return; /* type error; errmsg already given */
8718 }
8719# ifdef WIN32
8720 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
8721 if (n == 0)
8722 rettv->vval.v_number = -1;
8723 else
8724 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008725 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008726 rettv->vval.v_number = (s != NULL);
8727 }
8728# else
8729 if (check_connection() == FAIL)
8730 return;
8731
8732 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
8733 serverStrToWin(serverid), &s);
8734# endif
8735
8736 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
8737 {
8738 char_u *retvar;
8739
8740 v.di_tv.v_type = VAR_STRING;
8741 v.di_tv.vval.v_string = vim_strsave(s);
8742 retvar = get_tv_string_chk(&argvars[1]);
8743 if (retvar != NULL)
8744 set_var(retvar, &v.di_tv, FALSE);
8745 vim_free(v.di_tv.vval.v_string);
8746 }
8747#else
8748 rettv->vval.v_number = -1;
8749#endif
8750}
8751
8752 static void
8753f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
8754{
8755 char_u *r = NULL;
8756
8757#ifdef FEAT_CLIENTSERVER
8758 char_u *serverid = get_tv_string_chk(&argvars[0]);
8759
8760 if (serverid != NULL && !check_restricted() && !check_secure())
8761 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008762 int timeout = 0;
Bram Moolenaar1662ce12017-03-19 21:47:50 +01008763# ifdef WIN32
8764 /* The server's HWND is encoded in the 'id' parameter */
8765 long_u n = 0;
8766# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008767
8768 if (argvars[1].v_type != VAR_UNKNOWN)
8769 timeout = get_tv_number(&argvars[1]);
8770
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008771# ifdef WIN32
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008772 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
8773 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008774 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008775 if (r == NULL)
8776# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008777 if (check_connection() == FAIL
8778 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
8779 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008780# endif
8781 EMSG(_("E277: Unable to read a server reply"));
8782 }
8783#endif
8784 rettv->v_type = VAR_STRING;
8785 rettv->vval.v_string = r;
8786}
8787
8788/*
8789 * "remote_send()" function
8790 */
8791 static void
8792f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
8793{
8794 rettv->v_type = VAR_STRING;
8795 rettv->vval.v_string = NULL;
8796#ifdef FEAT_CLIENTSERVER
8797 remote_common(argvars, rettv, FALSE);
8798#endif
8799}
8800
8801/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01008802 * "remote_startserver()" function
8803 */
8804 static void
8805f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8806{
8807#ifdef FEAT_CLIENTSERVER
8808 char_u *server = get_tv_string_chk(&argvars[0]);
8809
8810 if (server == NULL)
8811 return; /* type error; errmsg already given */
8812 if (serverName != NULL)
8813 EMSG(_("E941: already started a server"));
8814 else
8815 {
8816# ifdef FEAT_X11
8817 if (check_connection() == OK)
8818 serverRegisterName(X_DISPLAY, server);
8819# else
8820 serverSetName(server);
8821# endif
8822 }
8823#else
8824 EMSG(_("E942: +clientserver feature not available"));
8825#endif
8826}
8827
8828/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008829 * "remove()" function
8830 */
8831 static void
8832f_remove(typval_T *argvars, typval_T *rettv)
8833{
8834 list_T *l;
8835 listitem_T *item, *item2;
8836 listitem_T *li;
8837 long idx;
8838 long end;
8839 char_u *key;
8840 dict_T *d;
8841 dictitem_T *di;
8842 char_u *arg_errmsg = (char_u *)N_("remove() argument");
8843
8844 if (argvars[0].v_type == VAR_DICT)
8845 {
8846 if (argvars[2].v_type != VAR_UNKNOWN)
8847 EMSG2(_(e_toomanyarg), "remove()");
8848 else if ((d = argvars[0].vval.v_dict) != NULL
8849 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
8850 {
8851 key = get_tv_string_chk(&argvars[1]);
8852 if (key != NULL)
8853 {
8854 di = dict_find(d, key, -1);
8855 if (di == NULL)
8856 EMSG2(_(e_dictkey), key);
8857 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
8858 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
8859 {
8860 *rettv = di->di_tv;
8861 init_tv(&di->di_tv);
8862 dictitem_remove(d, di);
8863 }
8864 }
8865 }
8866 }
8867 else if (argvars[0].v_type != VAR_LIST)
8868 EMSG2(_(e_listdictarg), "remove()");
8869 else if ((l = argvars[0].vval.v_list) != NULL
8870 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
8871 {
8872 int error = FALSE;
8873
8874 idx = (long)get_tv_number_chk(&argvars[1], &error);
8875 if (error)
8876 ; /* type error: do nothing, errmsg already given */
8877 else if ((item = list_find(l, idx)) == NULL)
8878 EMSGN(_(e_listidx), idx);
8879 else
8880 {
8881 if (argvars[2].v_type == VAR_UNKNOWN)
8882 {
8883 /* Remove one item, return its value. */
8884 vimlist_remove(l, item, item);
8885 *rettv = item->li_tv;
8886 vim_free(item);
8887 }
8888 else
8889 {
8890 /* Remove range of items, return list with values. */
8891 end = (long)get_tv_number_chk(&argvars[2], &error);
8892 if (error)
8893 ; /* type error: do nothing */
8894 else if ((item2 = list_find(l, end)) == NULL)
8895 EMSGN(_(e_listidx), end);
8896 else
8897 {
8898 int cnt = 0;
8899
8900 for (li = item; li != NULL; li = li->li_next)
8901 {
8902 ++cnt;
8903 if (li == item2)
8904 break;
8905 }
8906 if (li == NULL) /* didn't find "item2" after "item" */
8907 EMSG(_(e_invrange));
8908 else
8909 {
8910 vimlist_remove(l, item, item2);
8911 if (rettv_list_alloc(rettv) == OK)
8912 {
8913 l = rettv->vval.v_list;
8914 l->lv_first = item;
8915 l->lv_last = item2;
8916 item->li_prev = NULL;
8917 item2->li_next = NULL;
8918 l->lv_len = cnt;
8919 }
8920 }
8921 }
8922 }
8923 }
8924 }
8925}
8926
8927/*
8928 * "rename({from}, {to})" function
8929 */
8930 static void
8931f_rename(typval_T *argvars, typval_T *rettv)
8932{
8933 char_u buf[NUMBUFLEN];
8934
8935 if (check_restricted() || check_secure())
8936 rettv->vval.v_number = -1;
8937 else
8938 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
8939 get_tv_string_buf(&argvars[1], buf));
8940}
8941
8942/*
8943 * "repeat()" function
8944 */
8945 static void
8946f_repeat(typval_T *argvars, typval_T *rettv)
8947{
8948 char_u *p;
8949 int n;
8950 int slen;
8951 int len;
8952 char_u *r;
8953 int i;
8954
8955 n = (int)get_tv_number(&argvars[1]);
8956 if (argvars[0].v_type == VAR_LIST)
8957 {
8958 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
8959 while (n-- > 0)
8960 if (list_extend(rettv->vval.v_list,
8961 argvars[0].vval.v_list, NULL) == FAIL)
8962 break;
8963 }
8964 else
8965 {
8966 p = get_tv_string(&argvars[0]);
8967 rettv->v_type = VAR_STRING;
8968 rettv->vval.v_string = NULL;
8969
8970 slen = (int)STRLEN(p);
8971 len = slen * n;
8972 if (len <= 0)
8973 return;
8974
8975 r = alloc(len + 1);
8976 if (r != NULL)
8977 {
8978 for (i = 0; i < n; i++)
8979 mch_memmove(r + i * slen, p, (size_t)slen);
8980 r[len] = NUL;
8981 }
8982
8983 rettv->vval.v_string = r;
8984 }
8985}
8986
8987/*
8988 * "resolve()" function
8989 */
8990 static void
8991f_resolve(typval_T *argvars, typval_T *rettv)
8992{
8993 char_u *p;
8994#ifdef HAVE_READLINK
8995 char_u *buf = NULL;
8996#endif
8997
8998 p = get_tv_string(&argvars[0]);
8999#ifdef FEAT_SHORTCUT
9000 {
9001 char_u *v = NULL;
9002
9003 v = mch_resolve_shortcut(p);
9004 if (v != NULL)
9005 rettv->vval.v_string = v;
9006 else
9007 rettv->vval.v_string = vim_strsave(p);
9008 }
9009#else
9010# ifdef HAVE_READLINK
9011 {
9012 char_u *cpy;
9013 int len;
9014 char_u *remain = NULL;
9015 char_u *q;
9016 int is_relative_to_current = FALSE;
9017 int has_trailing_pathsep = FALSE;
9018 int limit = 100;
9019
9020 p = vim_strsave(p);
9021
9022 if (p[0] == '.' && (vim_ispathsep(p[1])
9023 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9024 is_relative_to_current = TRUE;
9025
9026 len = STRLEN(p);
9027 if (len > 0 && after_pathsep(p, p + len))
9028 {
9029 has_trailing_pathsep = TRUE;
9030 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9031 }
9032
9033 q = getnextcomp(p);
9034 if (*q != NUL)
9035 {
9036 /* Separate the first path component in "p", and keep the
9037 * remainder (beginning with the path separator). */
9038 remain = vim_strsave(q - 1);
9039 q[-1] = NUL;
9040 }
9041
9042 buf = alloc(MAXPATHL + 1);
9043 if (buf == NULL)
9044 goto fail;
9045
9046 for (;;)
9047 {
9048 for (;;)
9049 {
9050 len = readlink((char *)p, (char *)buf, MAXPATHL);
9051 if (len <= 0)
9052 break;
9053 buf[len] = NUL;
9054
9055 if (limit-- == 0)
9056 {
9057 vim_free(p);
9058 vim_free(remain);
9059 EMSG(_("E655: Too many symbolic links (cycle?)"));
9060 rettv->vval.v_string = NULL;
9061 goto fail;
9062 }
9063
9064 /* Ensure that the result will have a trailing path separator
9065 * if the argument has one. */
9066 if (remain == NULL && has_trailing_pathsep)
9067 add_pathsep(buf);
9068
9069 /* Separate the first path component in the link value and
9070 * concatenate the remainders. */
9071 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9072 if (*q != NUL)
9073 {
9074 if (remain == NULL)
9075 remain = vim_strsave(q - 1);
9076 else
9077 {
9078 cpy = concat_str(q - 1, remain);
9079 if (cpy != NULL)
9080 {
9081 vim_free(remain);
9082 remain = cpy;
9083 }
9084 }
9085 q[-1] = NUL;
9086 }
9087
9088 q = gettail(p);
9089 if (q > p && *q == NUL)
9090 {
9091 /* Ignore trailing path separator. */
9092 q[-1] = NUL;
9093 q = gettail(p);
9094 }
9095 if (q > p && !mch_isFullName(buf))
9096 {
9097 /* symlink is relative to directory of argument */
9098 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
9099 if (cpy != NULL)
9100 {
9101 STRCPY(cpy, p);
9102 STRCPY(gettail(cpy), buf);
9103 vim_free(p);
9104 p = cpy;
9105 }
9106 }
9107 else
9108 {
9109 vim_free(p);
9110 p = vim_strsave(buf);
9111 }
9112 }
9113
9114 if (remain == NULL)
9115 break;
9116
9117 /* Append the first path component of "remain" to "p". */
9118 q = getnextcomp(remain + 1);
9119 len = q - remain - (*q != NUL);
9120 cpy = vim_strnsave(p, STRLEN(p) + len);
9121 if (cpy != NULL)
9122 {
9123 STRNCAT(cpy, remain, len);
9124 vim_free(p);
9125 p = cpy;
9126 }
9127 /* Shorten "remain". */
9128 if (*q != NUL)
9129 STRMOVE(remain, q - 1);
9130 else
9131 {
9132 vim_free(remain);
9133 remain = NULL;
9134 }
9135 }
9136
9137 /* If the result is a relative path name, make it explicitly relative to
9138 * the current directory if and only if the argument had this form. */
9139 if (!vim_ispathsep(*p))
9140 {
9141 if (is_relative_to_current
9142 && *p != NUL
9143 && !(p[0] == '.'
9144 && (p[1] == NUL
9145 || vim_ispathsep(p[1])
9146 || (p[1] == '.'
9147 && (p[2] == NUL
9148 || vim_ispathsep(p[2]))))))
9149 {
9150 /* Prepend "./". */
9151 cpy = concat_str((char_u *)"./", p);
9152 if (cpy != NULL)
9153 {
9154 vim_free(p);
9155 p = cpy;
9156 }
9157 }
9158 else if (!is_relative_to_current)
9159 {
9160 /* Strip leading "./". */
9161 q = p;
9162 while (q[0] == '.' && vim_ispathsep(q[1]))
9163 q += 2;
9164 if (q > p)
9165 STRMOVE(p, p + 2);
9166 }
9167 }
9168
9169 /* Ensure that the result will have no trailing path separator
9170 * if the argument had none. But keep "/" or "//". */
9171 if (!has_trailing_pathsep)
9172 {
9173 q = p + STRLEN(p);
9174 if (after_pathsep(p, q))
9175 *gettail_sep(p) = NUL;
9176 }
9177
9178 rettv->vval.v_string = p;
9179 }
9180# else
9181 rettv->vval.v_string = vim_strsave(p);
9182# endif
9183#endif
9184
9185 simplify_filename(rettv->vval.v_string);
9186
9187#ifdef HAVE_READLINK
9188fail:
9189 vim_free(buf);
9190#endif
9191 rettv->v_type = VAR_STRING;
9192}
9193
9194/*
9195 * "reverse({list})" function
9196 */
9197 static void
9198f_reverse(typval_T *argvars, typval_T *rettv)
9199{
9200 list_T *l;
9201 listitem_T *li, *ni;
9202
9203 if (argvars[0].v_type != VAR_LIST)
9204 EMSG2(_(e_listarg), "reverse()");
9205 else if ((l = argvars[0].vval.v_list) != NULL
9206 && !tv_check_lock(l->lv_lock,
9207 (char_u *)N_("reverse() argument"), TRUE))
9208 {
9209 li = l->lv_last;
9210 l->lv_first = l->lv_last = NULL;
9211 l->lv_len = 0;
9212 while (li != NULL)
9213 {
9214 ni = li->li_prev;
9215 list_append(l, li);
9216 li = ni;
9217 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009218 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009219 l->lv_idx = l->lv_len - l->lv_idx - 1;
9220 }
9221}
9222
9223#define SP_NOMOVE 0x01 /* don't move cursor */
9224#define SP_REPEAT 0x02 /* repeat to find outer pair */
9225#define SP_RETCOUNT 0x04 /* return matchcount */
9226#define SP_SETPCMARK 0x08 /* set previous context mark */
9227#define SP_START 0x10 /* accept match at start position */
9228#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9229#define SP_END 0x40 /* leave cursor at end of match */
9230#define SP_COLUMN 0x80 /* start at cursor column */
9231
9232static int get_search_arg(typval_T *varp, int *flagsp);
9233
9234/*
9235 * Get flags for a search function.
9236 * Possibly sets "p_ws".
9237 * Returns BACKWARD, FORWARD or zero (for an error).
9238 */
9239 static int
9240get_search_arg(typval_T *varp, int *flagsp)
9241{
9242 int dir = FORWARD;
9243 char_u *flags;
9244 char_u nbuf[NUMBUFLEN];
9245 int mask;
9246
9247 if (varp->v_type != VAR_UNKNOWN)
9248 {
9249 flags = get_tv_string_buf_chk(varp, nbuf);
9250 if (flags == NULL)
9251 return 0; /* type error; errmsg already given */
9252 while (*flags != NUL)
9253 {
9254 switch (*flags)
9255 {
9256 case 'b': dir = BACKWARD; break;
9257 case 'w': p_ws = TRUE; break;
9258 case 'W': p_ws = FALSE; break;
9259 default: mask = 0;
9260 if (flagsp != NULL)
9261 switch (*flags)
9262 {
9263 case 'c': mask = SP_START; break;
9264 case 'e': mask = SP_END; break;
9265 case 'm': mask = SP_RETCOUNT; break;
9266 case 'n': mask = SP_NOMOVE; break;
9267 case 'p': mask = SP_SUBPAT; break;
9268 case 'r': mask = SP_REPEAT; break;
9269 case 's': mask = SP_SETPCMARK; break;
9270 case 'z': mask = SP_COLUMN; break;
9271 }
9272 if (mask == 0)
9273 {
9274 EMSG2(_(e_invarg2), flags);
9275 dir = 0;
9276 }
9277 else
9278 *flagsp |= mask;
9279 }
9280 if (dir == 0)
9281 break;
9282 ++flags;
9283 }
9284 }
9285 return dir;
9286}
9287
9288/*
9289 * Shared by search() and searchpos() functions.
9290 */
9291 static int
9292search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9293{
9294 int flags;
9295 char_u *pat;
9296 pos_T pos;
9297 pos_T save_cursor;
9298 int save_p_ws = p_ws;
9299 int dir;
9300 int retval = 0; /* default: FAIL */
9301 long lnum_stop = 0;
9302 proftime_T tm;
9303#ifdef FEAT_RELTIME
9304 long time_limit = 0;
9305#endif
9306 int options = SEARCH_KEEP;
9307 int subpatnum;
9308
9309 pat = get_tv_string(&argvars[0]);
9310 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
9311 if (dir == 0)
9312 goto theend;
9313 flags = *flagsp;
9314 if (flags & SP_START)
9315 options |= SEARCH_START;
9316 if (flags & SP_END)
9317 options |= SEARCH_END;
9318 if (flags & SP_COLUMN)
9319 options |= SEARCH_COL;
9320
9321 /* Optional arguments: line number to stop searching and timeout. */
9322 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9323 {
9324 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
9325 if (lnum_stop < 0)
9326 goto theend;
9327#ifdef FEAT_RELTIME
9328 if (argvars[3].v_type != VAR_UNKNOWN)
9329 {
9330 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
9331 if (time_limit < 0)
9332 goto theend;
9333 }
9334#endif
9335 }
9336
9337#ifdef FEAT_RELTIME
9338 /* Set the time limit, if there is one. */
9339 profile_setlimit(time_limit, &tm);
9340#endif
9341
9342 /*
9343 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
9344 * Check to make sure only those flags are set.
9345 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
9346 * flags cannot be set. Check for that condition also.
9347 */
9348 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
9349 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9350 {
9351 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
9352 goto theend;
9353 }
9354
9355 pos = save_cursor = curwin->w_cursor;
9356 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009357 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009358 if (subpatnum != FAIL)
9359 {
9360 if (flags & SP_SUBPAT)
9361 retval = subpatnum;
9362 else
9363 retval = pos.lnum;
9364 if (flags & SP_SETPCMARK)
9365 setpcmark();
9366 curwin->w_cursor = pos;
9367 if (match_pos != NULL)
9368 {
9369 /* Store the match cursor position */
9370 match_pos->lnum = pos.lnum;
9371 match_pos->col = pos.col + 1;
9372 }
9373 /* "/$" will put the cursor after the end of the line, may need to
9374 * correct that here */
9375 check_cursor();
9376 }
9377
9378 /* If 'n' flag is used: restore cursor position. */
9379 if (flags & SP_NOMOVE)
9380 curwin->w_cursor = save_cursor;
9381 else
9382 curwin->w_set_curswant = TRUE;
9383theend:
9384 p_ws = save_p_ws;
9385
9386 return retval;
9387}
9388
9389#ifdef FEAT_FLOAT
9390
9391/*
9392 * round() is not in C90, use ceil() or floor() instead.
9393 */
9394 float_T
9395vim_round(float_T f)
9396{
9397 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
9398}
9399
9400/*
9401 * "round({float})" function
9402 */
9403 static void
9404f_round(typval_T *argvars, typval_T *rettv)
9405{
9406 float_T f = 0.0;
9407
9408 rettv->v_type = VAR_FLOAT;
9409 if (get_float_arg(argvars, &f) == OK)
9410 rettv->vval.v_float = vim_round(f);
9411 else
9412 rettv->vval.v_float = 0.0;
9413}
9414#endif
9415
9416/*
9417 * "screenattr()" function
9418 */
9419 static void
9420f_screenattr(typval_T *argvars, typval_T *rettv)
9421{
9422 int row;
9423 int col;
9424 int c;
9425
9426 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9427 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9428 if (row < 0 || row >= screen_Rows
9429 || col < 0 || col >= screen_Columns)
9430 c = -1;
9431 else
9432 c = ScreenAttrs[LineOffset[row] + col];
9433 rettv->vval.v_number = c;
9434}
9435
9436/*
9437 * "screenchar()" function
9438 */
9439 static void
9440f_screenchar(typval_T *argvars, typval_T *rettv)
9441{
9442 int row;
9443 int col;
9444 int off;
9445 int c;
9446
9447 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9448 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9449 if (row < 0 || row >= screen_Rows
9450 || col < 0 || col >= screen_Columns)
9451 c = -1;
9452 else
9453 {
9454 off = LineOffset[row] + col;
9455#ifdef FEAT_MBYTE
9456 if (enc_utf8 && ScreenLinesUC[off] != 0)
9457 c = ScreenLinesUC[off];
9458 else
9459#endif
9460 c = ScreenLines[off];
9461 }
9462 rettv->vval.v_number = c;
9463}
9464
9465/*
9466 * "screencol()" function
9467 *
9468 * First column is 1 to be consistent with virtcol().
9469 */
9470 static void
9471f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
9472{
9473 rettv->vval.v_number = screen_screencol() + 1;
9474}
9475
9476/*
9477 * "screenrow()" function
9478 */
9479 static void
9480f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
9481{
9482 rettv->vval.v_number = screen_screenrow() + 1;
9483}
9484
9485/*
9486 * "search()" function
9487 */
9488 static void
9489f_search(typval_T *argvars, typval_T *rettv)
9490{
9491 int flags = 0;
9492
9493 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
9494}
9495
9496/*
9497 * "searchdecl()" function
9498 */
9499 static void
9500f_searchdecl(typval_T *argvars, typval_T *rettv)
9501{
9502 int locally = 1;
9503 int thisblock = 0;
9504 int error = FALSE;
9505 char_u *name;
9506
9507 rettv->vval.v_number = 1; /* default: FAIL */
9508
9509 name = get_tv_string_chk(&argvars[0]);
9510 if (argvars[1].v_type != VAR_UNKNOWN)
9511 {
9512 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
9513 if (!error && argvars[2].v_type != VAR_UNKNOWN)
9514 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
9515 }
9516 if (!error && name != NULL)
9517 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
9518 locally, thisblock, SEARCH_KEEP) == FAIL;
9519}
9520
9521/*
9522 * Used by searchpair() and searchpairpos()
9523 */
9524 static int
9525searchpair_cmn(typval_T *argvars, pos_T *match_pos)
9526{
9527 char_u *spat, *mpat, *epat;
9528 char_u *skip;
9529 int save_p_ws = p_ws;
9530 int dir;
9531 int flags = 0;
9532 char_u nbuf1[NUMBUFLEN];
9533 char_u nbuf2[NUMBUFLEN];
9534 char_u nbuf3[NUMBUFLEN];
9535 int retval = 0; /* default: FAIL */
9536 long lnum_stop = 0;
9537 long time_limit = 0;
9538
9539 /* Get the three pattern arguments: start, middle, end. */
9540 spat = get_tv_string_chk(&argvars[0]);
9541 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
9542 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
9543 if (spat == NULL || mpat == NULL || epat == NULL)
9544 goto theend; /* type error */
9545
9546 /* Handle the optional fourth argument: flags */
9547 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
9548 if (dir == 0)
9549 goto theend;
9550
9551 /* Don't accept SP_END or SP_SUBPAT.
9552 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
9553 */
9554 if ((flags & (SP_END | SP_SUBPAT)) != 0
9555 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9556 {
9557 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
9558 goto theend;
9559 }
9560
9561 /* Using 'r' implies 'W', otherwise it doesn't work. */
9562 if (flags & SP_REPEAT)
9563 p_ws = FALSE;
9564
9565 /* Optional fifth argument: skip expression */
9566 if (argvars[3].v_type == VAR_UNKNOWN
9567 || argvars[4].v_type == VAR_UNKNOWN)
9568 skip = (char_u *)"";
9569 else
9570 {
9571 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
9572 if (argvars[5].v_type != VAR_UNKNOWN)
9573 {
9574 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
9575 if (lnum_stop < 0)
9576 goto theend;
9577#ifdef FEAT_RELTIME
9578 if (argvars[6].v_type != VAR_UNKNOWN)
9579 {
9580 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
9581 if (time_limit < 0)
9582 goto theend;
9583 }
9584#endif
9585 }
9586 }
9587 if (skip == NULL)
9588 goto theend; /* type error */
9589
9590 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
9591 match_pos, lnum_stop, time_limit);
9592
9593theend:
9594 p_ws = save_p_ws;
9595
9596 return retval;
9597}
9598
9599/*
9600 * "searchpair()" function
9601 */
9602 static void
9603f_searchpair(typval_T *argvars, typval_T *rettv)
9604{
9605 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
9606}
9607
9608/*
9609 * "searchpairpos()" function
9610 */
9611 static void
9612f_searchpairpos(typval_T *argvars, typval_T *rettv)
9613{
9614 pos_T match_pos;
9615 int lnum = 0;
9616 int col = 0;
9617
9618 if (rettv_list_alloc(rettv) == FAIL)
9619 return;
9620
9621 if (searchpair_cmn(argvars, &match_pos) > 0)
9622 {
9623 lnum = match_pos.lnum;
9624 col = match_pos.col;
9625 }
9626
9627 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9628 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9629}
9630
9631/*
9632 * Search for a start/middle/end thing.
9633 * Used by searchpair(), see its documentation for the details.
9634 * Returns 0 or -1 for no match,
9635 */
9636 long
9637do_searchpair(
9638 char_u *spat, /* start pattern */
9639 char_u *mpat, /* middle pattern */
9640 char_u *epat, /* end pattern */
9641 int dir, /* BACKWARD or FORWARD */
9642 char_u *skip, /* skip expression */
9643 int flags, /* SP_SETPCMARK and other SP_ values */
9644 pos_T *match_pos,
9645 linenr_T lnum_stop, /* stop at this line if not zero */
9646 long time_limit UNUSED) /* stop after this many msec */
9647{
9648 char_u *save_cpo;
9649 char_u *pat, *pat2 = NULL, *pat3 = NULL;
9650 long retval = 0;
9651 pos_T pos;
9652 pos_T firstpos;
9653 pos_T foundpos;
9654 pos_T save_cursor;
9655 pos_T save_pos;
9656 int n;
9657 int r;
9658 int nest = 1;
9659 int err;
9660 int options = SEARCH_KEEP;
9661 proftime_T tm;
9662
9663 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
9664 save_cpo = p_cpo;
9665 p_cpo = empty_option;
9666
9667#ifdef FEAT_RELTIME
9668 /* Set the time limit, if there is one. */
9669 profile_setlimit(time_limit, &tm);
9670#endif
9671
9672 /* Make two search patterns: start/end (pat2, for in nested pairs) and
9673 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009674 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
9675 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009676 if (pat2 == NULL || pat3 == NULL)
9677 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009678 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009679 if (*mpat == NUL)
9680 STRCPY(pat3, pat2);
9681 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009682 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009683 spat, epat, mpat);
9684 if (flags & SP_START)
9685 options |= SEARCH_START;
9686
9687 save_cursor = curwin->w_cursor;
9688 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009689 CLEAR_POS(&firstpos);
9690 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009691 pat = pat3;
9692 for (;;)
9693 {
9694 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009695 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009696 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009697 /* didn't find it or found the first match again: FAIL */
9698 break;
9699
9700 if (firstpos.lnum == 0)
9701 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009702 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009703 {
9704 /* Found the same position again. Can happen with a pattern that
9705 * has "\zs" at the end and searching backwards. Advance one
9706 * character and try again. */
9707 if (dir == BACKWARD)
9708 decl(&pos);
9709 else
9710 incl(&pos);
9711 }
9712 foundpos = pos;
9713
9714 /* clear the start flag to avoid getting stuck here */
9715 options &= ~SEARCH_START;
9716
9717 /* If the skip pattern matches, ignore this match. */
9718 if (*skip != NUL)
9719 {
9720 save_pos = curwin->w_cursor;
9721 curwin->w_cursor = pos;
9722 r = eval_to_bool(skip, &err, NULL, FALSE);
9723 curwin->w_cursor = save_pos;
9724 if (err)
9725 {
9726 /* Evaluating {skip} caused an error, break here. */
9727 curwin->w_cursor = save_cursor;
9728 retval = -1;
9729 break;
9730 }
9731 if (r)
9732 continue;
9733 }
9734
9735 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
9736 {
9737 /* Found end when searching backwards or start when searching
9738 * forward: nested pair. */
9739 ++nest;
9740 pat = pat2; /* nested, don't search for middle */
9741 }
9742 else
9743 {
9744 /* Found end when searching forward or start when searching
9745 * backward: end of (nested) pair; or found middle in outer pair. */
9746 if (--nest == 1)
9747 pat = pat3; /* outer level, search for middle */
9748 }
9749
9750 if (nest == 0)
9751 {
9752 /* Found the match: return matchcount or line number. */
9753 if (flags & SP_RETCOUNT)
9754 ++retval;
9755 else
9756 retval = pos.lnum;
9757 if (flags & SP_SETPCMARK)
9758 setpcmark();
9759 curwin->w_cursor = pos;
9760 if (!(flags & SP_REPEAT))
9761 break;
9762 nest = 1; /* search for next unmatched */
9763 }
9764 }
9765
9766 if (match_pos != NULL)
9767 {
9768 /* Store the match cursor position */
9769 match_pos->lnum = curwin->w_cursor.lnum;
9770 match_pos->col = curwin->w_cursor.col + 1;
9771 }
9772
9773 /* If 'n' flag is used or search failed: restore cursor position. */
9774 if ((flags & SP_NOMOVE) || retval == 0)
9775 curwin->w_cursor = save_cursor;
9776
9777theend:
9778 vim_free(pat2);
9779 vim_free(pat3);
9780 if (p_cpo == empty_option)
9781 p_cpo = save_cpo;
9782 else
9783 /* Darn, evaluating the {skip} expression changed the value. */
9784 free_string_option(save_cpo);
9785
9786 return retval;
9787}
9788
9789/*
9790 * "searchpos()" function
9791 */
9792 static void
9793f_searchpos(typval_T *argvars, typval_T *rettv)
9794{
9795 pos_T match_pos;
9796 int lnum = 0;
9797 int col = 0;
9798 int n;
9799 int flags = 0;
9800
9801 if (rettv_list_alloc(rettv) == FAIL)
9802 return;
9803
9804 n = search_cmn(argvars, &match_pos, &flags);
9805 if (n > 0)
9806 {
9807 lnum = match_pos.lnum;
9808 col = match_pos.col;
9809 }
9810
9811 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9812 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9813 if (flags & SP_SUBPAT)
9814 list_append_number(rettv->vval.v_list, (varnumber_T)n);
9815}
9816
9817 static void
9818f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
9819{
9820#ifdef FEAT_CLIENTSERVER
9821 char_u buf[NUMBUFLEN];
9822 char_u *server = get_tv_string_chk(&argvars[0]);
9823 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
9824
9825 rettv->vval.v_number = -1;
9826 if (server == NULL || reply == NULL)
9827 return;
9828 if (check_restricted() || check_secure())
9829 return;
9830# ifdef FEAT_X11
9831 if (check_connection() == FAIL)
9832 return;
9833# endif
9834
9835 if (serverSendReply(server, reply) < 0)
9836 {
9837 EMSG(_("E258: Unable to send to client"));
9838 return;
9839 }
9840 rettv->vval.v_number = 0;
9841#else
9842 rettv->vval.v_number = -1;
9843#endif
9844}
9845
9846 static void
9847f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
9848{
9849 char_u *r = NULL;
9850
9851#ifdef FEAT_CLIENTSERVER
9852# ifdef WIN32
9853 r = serverGetVimNames();
9854# else
9855 make_connection();
9856 if (X_DISPLAY != NULL)
9857 r = serverGetVimNames(X_DISPLAY);
9858# endif
9859#endif
9860 rettv->v_type = VAR_STRING;
9861 rettv->vval.v_string = r;
9862}
9863
9864/*
9865 * "setbufvar()" function
9866 */
9867 static void
9868f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
9869{
9870 buf_T *buf;
9871 char_u *varname, *bufvarname;
9872 typval_T *varp;
9873 char_u nbuf[NUMBUFLEN];
9874
9875 if (check_restricted() || check_secure())
9876 return;
9877 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9878 varname = get_tv_string_chk(&argvars[1]);
9879 buf = get_buf_tv(&argvars[0], FALSE);
9880 varp = &argvars[2];
9881
9882 if (buf != NULL && varname != NULL && varp != NULL)
9883 {
9884 if (*varname == '&')
9885 {
9886 long numval;
9887 char_u *strval;
9888 int error = FALSE;
9889 aco_save_T aco;
9890
9891 /* set curbuf to be our buf, temporarily */
9892 aucmd_prepbuf(&aco, buf);
9893
9894 ++varname;
9895 numval = (long)get_tv_number_chk(varp, &error);
9896 strval = get_tv_string_buf_chk(varp, nbuf);
9897 if (!error && strval != NULL)
9898 set_option_value(varname, numval, strval, OPT_LOCAL);
9899
9900 /* reset notion of buffer */
9901 aucmd_restbuf(&aco);
9902 }
9903 else
9904 {
9905 buf_T *save_curbuf = curbuf;
9906
9907 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
9908 if (bufvarname != NULL)
9909 {
9910 curbuf = buf;
9911 STRCPY(bufvarname, "b:");
9912 STRCPY(bufvarname + 2, varname);
9913 set_var(bufvarname, varp, TRUE);
9914 vim_free(bufvarname);
9915 curbuf = save_curbuf;
9916 }
9917 }
9918 }
9919}
9920
9921 static void
9922f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
9923{
9924 dict_T *d;
9925 dictitem_T *di;
9926 char_u *csearch;
9927
9928 if (argvars[0].v_type != VAR_DICT)
9929 {
9930 EMSG(_(e_dictreq));
9931 return;
9932 }
9933
9934 if ((d = argvars[0].vval.v_dict) != NULL)
9935 {
9936 csearch = get_dict_string(d, (char_u *)"char", FALSE);
9937 if (csearch != NULL)
9938 {
9939#ifdef FEAT_MBYTE
9940 if (enc_utf8)
9941 {
9942 int pcc[MAX_MCO];
9943 int c = utfc_ptr2char(csearch, pcc);
9944
9945 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
9946 }
9947 else
9948#endif
9949 set_last_csearch(PTR2CHAR(csearch),
9950 csearch, MB_PTR2LEN(csearch));
9951 }
9952
9953 di = dict_find(d, (char_u *)"forward", -1);
9954 if (di != NULL)
9955 set_csearch_direction((int)get_tv_number(&di->di_tv)
9956 ? FORWARD : BACKWARD);
9957
9958 di = dict_find(d, (char_u *)"until", -1);
9959 if (di != NULL)
9960 set_csearch_until(!!get_tv_number(&di->di_tv));
9961 }
9962}
9963
9964/*
9965 * "setcmdpos()" function
9966 */
9967 static void
9968f_setcmdpos(typval_T *argvars, typval_T *rettv)
9969{
9970 int pos = (int)get_tv_number(&argvars[0]) - 1;
9971
9972 if (pos >= 0)
9973 rettv->vval.v_number = set_cmdline_pos(pos);
9974}
9975
9976/*
9977 * "setfperm({fname}, {mode})" function
9978 */
9979 static void
9980f_setfperm(typval_T *argvars, typval_T *rettv)
9981{
9982 char_u *fname;
9983 char_u modebuf[NUMBUFLEN];
9984 char_u *mode_str;
9985 int i;
9986 int mask;
9987 int mode = 0;
9988
9989 rettv->vval.v_number = 0;
9990 fname = get_tv_string_chk(&argvars[0]);
9991 if (fname == NULL)
9992 return;
9993 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
9994 if (mode_str == NULL)
9995 return;
9996 if (STRLEN(mode_str) != 9)
9997 {
9998 EMSG2(_(e_invarg2), mode_str);
9999 return;
10000 }
10001
10002 mask = 1;
10003 for (i = 8; i >= 0; --i)
10004 {
10005 if (mode_str[i] != '-')
10006 mode |= mask;
10007 mask = mask << 1;
10008 }
10009 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10010}
10011
10012/*
10013 * "setline()" function
10014 */
10015 static void
10016f_setline(typval_T *argvars, typval_T *rettv)
10017{
10018 linenr_T lnum;
10019 char_u *line = NULL;
10020 list_T *l = NULL;
10021 listitem_T *li = NULL;
10022 long added = 0;
10023 linenr_T lcount = curbuf->b_ml.ml_line_count;
10024
10025 lnum = get_tv_lnum(&argvars[0]);
10026 if (argvars[1].v_type == VAR_LIST)
10027 {
10028 l = argvars[1].vval.v_list;
10029 li = l->lv_first;
10030 }
10031 else
10032 line = get_tv_string_chk(&argvars[1]);
10033
10034 /* default result is zero == OK */
10035 for (;;)
10036 {
10037 if (l != NULL)
10038 {
10039 /* list argument, get next string */
10040 if (li == NULL)
10041 break;
10042 line = get_tv_string_chk(&li->li_tv);
10043 li = li->li_next;
10044 }
10045
10046 rettv->vval.v_number = 1; /* FAIL */
10047 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
10048 break;
10049
10050 /* When coming here from Insert mode, sync undo, so that this can be
10051 * undone separately from what was previously inserted. */
10052 if (u_sync_once == 2)
10053 {
10054 u_sync_once = 1; /* notify that u_sync() was called */
10055 u_sync(TRUE);
10056 }
10057
10058 if (lnum <= curbuf->b_ml.ml_line_count)
10059 {
10060 /* existing line, replace it */
10061 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
10062 {
10063 changed_bytes(lnum, 0);
10064 if (lnum == curwin->w_cursor.lnum)
10065 check_cursor_col();
10066 rettv->vval.v_number = 0; /* OK */
10067 }
10068 }
10069 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
10070 {
10071 /* lnum is one past the last line, append the line */
10072 ++added;
10073 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
10074 rettv->vval.v_number = 0; /* OK */
10075 }
10076
10077 if (l == NULL) /* only one string argument */
10078 break;
10079 ++lnum;
10080 }
10081
10082 if (added > 0)
10083 appended_lines_mark(lcount, added);
10084}
10085
Bram Moolenaard823fa92016-08-12 16:29:27 +020010086static 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 +020010087
10088/*
10089 * Used by "setqflist()" and "setloclist()" functions
10090 */
10091 static void
10092set_qf_ll_list(
10093 win_T *wp UNUSED,
10094 typval_T *list_arg UNUSED,
10095 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010096 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010097 typval_T *rettv)
10098{
10099#ifdef FEAT_QUICKFIX
10100 static char *e_invact = N_("E927: Invalid action: '%s'");
10101 char_u *act;
10102 int action = 0;
10103#endif
10104
10105 rettv->vval.v_number = -1;
10106
10107#ifdef FEAT_QUICKFIX
10108 if (list_arg->v_type != VAR_LIST)
10109 EMSG(_(e_listreq));
10110 else
10111 {
10112 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010113 dict_T *d = NULL;
10114 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010115
10116 if (action_arg->v_type == VAR_STRING)
10117 {
10118 act = get_tv_string_chk(action_arg);
10119 if (act == NULL)
10120 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020010121 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
10122 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010123 action = *act;
10124 else
10125 EMSG2(_(e_invact), act);
10126 }
10127 else if (action_arg->v_type == VAR_UNKNOWN)
10128 action = ' ';
10129 else
10130 EMSG(_(e_stringreq));
10131
Bram Moolenaard823fa92016-08-12 16:29:27 +020010132 if (action_arg->v_type != VAR_UNKNOWN
10133 && what_arg->v_type != VAR_UNKNOWN)
10134 {
10135 if (what_arg->v_type == VAR_DICT)
10136 d = what_arg->vval.v_dict;
10137 else
10138 {
10139 EMSG(_(e_dictreq));
10140 valid_dict = FALSE;
10141 }
10142 }
10143
10144 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
10145 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()"), d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010146 rettv->vval.v_number = 0;
10147 }
10148#endif
10149}
10150
10151/*
10152 * "setloclist()" function
10153 */
10154 static void
10155f_setloclist(typval_T *argvars, typval_T *rettv)
10156{
10157 win_T *win;
10158
10159 rettv->vval.v_number = -1;
10160
10161 win = find_win_by_nr(&argvars[0], NULL);
10162 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020010163 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010164}
10165
10166/*
10167 * "setmatches()" function
10168 */
10169 static void
10170f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10171{
10172#ifdef FEAT_SEARCH_EXTRA
10173 list_T *l;
10174 listitem_T *li;
10175 dict_T *d;
10176 list_T *s = NULL;
10177
10178 rettv->vval.v_number = -1;
10179 if (argvars[0].v_type != VAR_LIST)
10180 {
10181 EMSG(_(e_listreq));
10182 return;
10183 }
10184 if ((l = argvars[0].vval.v_list) != NULL)
10185 {
10186
10187 /* To some extent make sure that we are dealing with a list from
10188 * "getmatches()". */
10189 li = l->lv_first;
10190 while (li != NULL)
10191 {
10192 if (li->li_tv.v_type != VAR_DICT
10193 || (d = li->li_tv.vval.v_dict) == NULL)
10194 {
10195 EMSG(_(e_invarg));
10196 return;
10197 }
10198 if (!(dict_find(d, (char_u *)"group", -1) != NULL
10199 && (dict_find(d, (char_u *)"pattern", -1) != NULL
10200 || dict_find(d, (char_u *)"pos1", -1) != NULL)
10201 && dict_find(d, (char_u *)"priority", -1) != NULL
10202 && dict_find(d, (char_u *)"id", -1) != NULL))
10203 {
10204 EMSG(_(e_invarg));
10205 return;
10206 }
10207 li = li->li_next;
10208 }
10209
10210 clear_matches(curwin);
10211 li = l->lv_first;
10212 while (li != NULL)
10213 {
10214 int i = 0;
10215 char_u buf[5];
10216 dictitem_T *di;
10217 char_u *group;
10218 int priority;
10219 int id;
10220 char_u *conceal;
10221
10222 d = li->li_tv.vval.v_dict;
10223 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
10224 {
10225 if (s == NULL)
10226 {
10227 s = list_alloc();
10228 if (s == NULL)
10229 return;
10230 }
10231
10232 /* match from matchaddpos() */
10233 for (i = 1; i < 9; i++)
10234 {
10235 sprintf((char *)buf, (char *)"pos%d", i);
10236 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
10237 {
10238 if (di->di_tv.v_type != VAR_LIST)
10239 return;
10240
10241 list_append_tv(s, &di->di_tv);
10242 s->lv_refcount++;
10243 }
10244 else
10245 break;
10246 }
10247 }
10248
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010249 group = get_dict_string(d, (char_u *)"group", TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010250 priority = (int)get_dict_number(d, (char_u *)"priority");
10251 id = (int)get_dict_number(d, (char_u *)"id");
10252 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010253 ? get_dict_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010254 : NULL;
10255 if (i == 0)
10256 {
10257 match_add(curwin, group,
10258 get_dict_string(d, (char_u *)"pattern", FALSE),
10259 priority, id, NULL, conceal);
10260 }
10261 else
10262 {
10263 match_add(curwin, group, NULL, priority, id, s, conceal);
10264 list_unref(s);
10265 s = NULL;
10266 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010267 vim_free(group);
10268 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010269
10270 li = li->li_next;
10271 }
10272 rettv->vval.v_number = 0;
10273 }
10274#endif
10275}
10276
10277/*
10278 * "setpos()" function
10279 */
10280 static void
10281f_setpos(typval_T *argvars, typval_T *rettv)
10282{
10283 pos_T pos;
10284 int fnum;
10285 char_u *name;
10286 colnr_T curswant = -1;
10287
10288 rettv->vval.v_number = -1;
10289 name = get_tv_string_chk(argvars);
10290 if (name != NULL)
10291 {
10292 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10293 {
10294 if (--pos.col < 0)
10295 pos.col = 0;
10296 if (name[0] == '.' && name[1] == NUL)
10297 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010298 /* set cursor; "fnum" is ignored */
10299 curwin->w_cursor = pos;
10300 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010301 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010302 curwin->w_curswant = curswant - 1;
10303 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010304 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010305 check_cursor();
10306 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010307 }
10308 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10309 {
10310 /* set mark */
10311 if (setmark_pos(name[1], &pos, fnum) == OK)
10312 rettv->vval.v_number = 0;
10313 }
10314 else
10315 EMSG(_(e_invarg));
10316 }
10317 }
10318}
10319
10320/*
10321 * "setqflist()" function
10322 */
10323 static void
10324f_setqflist(typval_T *argvars, typval_T *rettv)
10325{
Bram Moolenaard823fa92016-08-12 16:29:27 +020010326 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010327}
10328
10329/*
10330 * "setreg()" function
10331 */
10332 static void
10333f_setreg(typval_T *argvars, typval_T *rettv)
10334{
10335 int regname;
10336 char_u *strregname;
10337 char_u *stropt;
10338 char_u *strval;
10339 int append;
10340 char_u yank_type;
10341 long block_len;
10342
10343 block_len = -1;
10344 yank_type = MAUTO;
10345 append = FALSE;
10346
10347 strregname = get_tv_string_chk(argvars);
10348 rettv->vval.v_number = 1; /* FAIL is default */
10349
10350 if (strregname == NULL)
10351 return; /* type error; errmsg already given */
10352 regname = *strregname;
10353 if (regname == 0 || regname == '@')
10354 regname = '"';
10355
10356 if (argvars[2].v_type != VAR_UNKNOWN)
10357 {
10358 stropt = get_tv_string_chk(&argvars[2]);
10359 if (stropt == NULL)
10360 return; /* type error */
10361 for (; *stropt != NUL; ++stropt)
10362 switch (*stropt)
10363 {
10364 case 'a': case 'A': /* append */
10365 append = TRUE;
10366 break;
10367 case 'v': case 'c': /* character-wise selection */
10368 yank_type = MCHAR;
10369 break;
10370 case 'V': case 'l': /* line-wise selection */
10371 yank_type = MLINE;
10372 break;
10373 case 'b': case Ctrl_V: /* block-wise selection */
10374 yank_type = MBLOCK;
10375 if (VIM_ISDIGIT(stropt[1]))
10376 {
10377 ++stropt;
10378 block_len = getdigits(&stropt) - 1;
10379 --stropt;
10380 }
10381 break;
10382 }
10383 }
10384
10385 if (argvars[1].v_type == VAR_LIST)
10386 {
10387 char_u **lstval;
10388 char_u **allocval;
10389 char_u buf[NUMBUFLEN];
10390 char_u **curval;
10391 char_u **curallocval;
10392 list_T *ll = argvars[1].vval.v_list;
10393 listitem_T *li;
10394 int len;
10395
10396 /* If the list is NULL handle like an empty list. */
10397 len = ll == NULL ? 0 : ll->lv_len;
10398
10399 /* First half: use for pointers to result lines; second half: use for
10400 * pointers to allocated copies. */
10401 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
10402 if (lstval == NULL)
10403 return;
10404 curval = lstval;
10405 allocval = lstval + len + 2;
10406 curallocval = allocval;
10407
10408 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
10409 li = li->li_next)
10410 {
10411 strval = get_tv_string_buf_chk(&li->li_tv, buf);
10412 if (strval == NULL)
10413 goto free_lstval;
10414 if (strval == buf)
10415 {
10416 /* Need to make a copy, next get_tv_string_buf_chk() will
10417 * overwrite the string. */
10418 strval = vim_strsave(buf);
10419 if (strval == NULL)
10420 goto free_lstval;
10421 *curallocval++ = strval;
10422 }
10423 *curval++ = strval;
10424 }
10425 *curval++ = NULL;
10426
10427 write_reg_contents_lst(regname, lstval, -1,
10428 append, yank_type, block_len);
10429free_lstval:
10430 while (curallocval > allocval)
10431 vim_free(*--curallocval);
10432 vim_free(lstval);
10433 }
10434 else
10435 {
10436 strval = get_tv_string_chk(&argvars[1]);
10437 if (strval == NULL)
10438 return;
10439 write_reg_contents_ex(regname, strval, -1,
10440 append, yank_type, block_len);
10441 }
10442 rettv->vval.v_number = 0;
10443}
10444
10445/*
10446 * "settabvar()" function
10447 */
10448 static void
10449f_settabvar(typval_T *argvars, typval_T *rettv)
10450{
10451#ifdef FEAT_WINDOWS
10452 tabpage_T *save_curtab;
10453 tabpage_T *tp;
10454#endif
10455 char_u *varname, *tabvarname;
10456 typval_T *varp;
10457
10458 rettv->vval.v_number = 0;
10459
10460 if (check_restricted() || check_secure())
10461 return;
10462
10463#ifdef FEAT_WINDOWS
10464 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
10465#endif
10466 varname = get_tv_string_chk(&argvars[1]);
10467 varp = &argvars[2];
10468
10469 if (varname != NULL && varp != NULL
10470#ifdef FEAT_WINDOWS
10471 && tp != NULL
10472#endif
10473 )
10474 {
10475#ifdef FEAT_WINDOWS
10476 save_curtab = curtab;
10477 goto_tabpage_tp(tp, FALSE, FALSE);
10478#endif
10479
10480 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
10481 if (tabvarname != NULL)
10482 {
10483 STRCPY(tabvarname, "t:");
10484 STRCPY(tabvarname + 2, varname);
10485 set_var(tabvarname, varp, TRUE);
10486 vim_free(tabvarname);
10487 }
10488
10489#ifdef FEAT_WINDOWS
10490 /* Restore current tabpage */
10491 if (valid_tabpage(save_curtab))
10492 goto_tabpage_tp(save_curtab, FALSE, FALSE);
10493#endif
10494 }
10495}
10496
10497/*
10498 * "settabwinvar()" function
10499 */
10500 static void
10501f_settabwinvar(typval_T *argvars, typval_T *rettv)
10502{
10503 setwinvar(argvars, rettv, 1);
10504}
10505
10506/*
10507 * "setwinvar()" function
10508 */
10509 static void
10510f_setwinvar(typval_T *argvars, typval_T *rettv)
10511{
10512 setwinvar(argvars, rettv, 0);
10513}
10514
10515#ifdef FEAT_CRYPT
10516/*
10517 * "sha256({string})" function
10518 */
10519 static void
10520f_sha256(typval_T *argvars, typval_T *rettv)
10521{
10522 char_u *p;
10523
10524 p = get_tv_string(&argvars[0]);
10525 rettv->vval.v_string = vim_strsave(
10526 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
10527 rettv->v_type = VAR_STRING;
10528}
10529#endif /* FEAT_CRYPT */
10530
10531/*
10532 * "shellescape({string})" function
10533 */
10534 static void
10535f_shellescape(typval_T *argvars, typval_T *rettv)
10536{
Bram Moolenaar20615522017-06-05 18:46:26 +020010537 int do_special = non_zero_arg(&argvars[1]);
10538
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010539 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar20615522017-06-05 18:46:26 +020010540 get_tv_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010541 rettv->v_type = VAR_STRING;
10542}
10543
10544/*
10545 * shiftwidth() function
10546 */
10547 static void
10548f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
10549{
10550 rettv->vval.v_number = get_sw_value(curbuf);
10551}
10552
10553/*
10554 * "simplify()" function
10555 */
10556 static void
10557f_simplify(typval_T *argvars, typval_T *rettv)
10558{
10559 char_u *p;
10560
10561 p = get_tv_string(&argvars[0]);
10562 rettv->vval.v_string = vim_strsave(p);
10563 simplify_filename(rettv->vval.v_string); /* simplify in place */
10564 rettv->v_type = VAR_STRING;
10565}
10566
10567#ifdef FEAT_FLOAT
10568/*
10569 * "sin()" function
10570 */
10571 static void
10572f_sin(typval_T *argvars, typval_T *rettv)
10573{
10574 float_T f = 0.0;
10575
10576 rettv->v_type = VAR_FLOAT;
10577 if (get_float_arg(argvars, &f) == OK)
10578 rettv->vval.v_float = sin(f);
10579 else
10580 rettv->vval.v_float = 0.0;
10581}
10582
10583/*
10584 * "sinh()" function
10585 */
10586 static void
10587f_sinh(typval_T *argvars, typval_T *rettv)
10588{
10589 float_T f = 0.0;
10590
10591 rettv->v_type = VAR_FLOAT;
10592 if (get_float_arg(argvars, &f) == OK)
10593 rettv->vval.v_float = sinh(f);
10594 else
10595 rettv->vval.v_float = 0.0;
10596}
10597#endif
10598
10599static int
10600#ifdef __BORLANDC__
10601 _RTLENTRYF
10602#endif
10603 item_compare(const void *s1, const void *s2);
10604static int
10605#ifdef __BORLANDC__
10606 _RTLENTRYF
10607#endif
10608 item_compare2(const void *s1, const void *s2);
10609
10610/* struct used in the array that's given to qsort() */
10611typedef struct
10612{
10613 listitem_T *item;
10614 int idx;
10615} sortItem_T;
10616
10617/* struct storing information about current sort */
10618typedef struct
10619{
10620 int item_compare_ic;
10621 int item_compare_numeric;
10622 int item_compare_numbers;
10623#ifdef FEAT_FLOAT
10624 int item_compare_float;
10625#endif
10626 char_u *item_compare_func;
10627 partial_T *item_compare_partial;
10628 dict_T *item_compare_selfdict;
10629 int item_compare_func_err;
10630 int item_compare_keep_zero;
10631} sortinfo_T;
10632static sortinfo_T *sortinfo = NULL;
10633static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
10634#define ITEM_COMPARE_FAIL 999
10635
10636/*
10637 * Compare functions for f_sort() and f_uniq() below.
10638 */
10639 static int
10640#ifdef __BORLANDC__
10641_RTLENTRYF
10642#endif
10643item_compare(const void *s1, const void *s2)
10644{
10645 sortItem_T *si1, *si2;
10646 typval_T *tv1, *tv2;
10647 char_u *p1, *p2;
10648 char_u *tofree1 = NULL, *tofree2 = NULL;
10649 int res;
10650 char_u numbuf1[NUMBUFLEN];
10651 char_u numbuf2[NUMBUFLEN];
10652
10653 si1 = (sortItem_T *)s1;
10654 si2 = (sortItem_T *)s2;
10655 tv1 = &si1->item->li_tv;
10656 tv2 = &si2->item->li_tv;
10657
10658 if (sortinfo->item_compare_numbers)
10659 {
10660 varnumber_T v1 = get_tv_number(tv1);
10661 varnumber_T v2 = get_tv_number(tv2);
10662
10663 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10664 }
10665
10666#ifdef FEAT_FLOAT
10667 if (sortinfo->item_compare_float)
10668 {
10669 float_T v1 = get_tv_float(tv1);
10670 float_T v2 = get_tv_float(tv2);
10671
10672 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10673 }
10674#endif
10675
10676 /* tv2string() puts quotes around a string and allocates memory. Don't do
10677 * that for string variables. Use a single quote when comparing with a
10678 * non-string to do what the docs promise. */
10679 if (tv1->v_type == VAR_STRING)
10680 {
10681 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
10682 p1 = (char_u *)"'";
10683 else
10684 p1 = tv1->vval.v_string;
10685 }
10686 else
10687 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
10688 if (tv2->v_type == VAR_STRING)
10689 {
10690 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
10691 p2 = (char_u *)"'";
10692 else
10693 p2 = tv2->vval.v_string;
10694 }
10695 else
10696 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
10697 if (p1 == NULL)
10698 p1 = (char_u *)"";
10699 if (p2 == NULL)
10700 p2 = (char_u *)"";
10701 if (!sortinfo->item_compare_numeric)
10702 {
10703 if (sortinfo->item_compare_ic)
10704 res = STRICMP(p1, p2);
10705 else
10706 res = STRCMP(p1, p2);
10707 }
10708 else
10709 {
10710 double n1, n2;
10711 n1 = strtod((char *)p1, (char **)&p1);
10712 n2 = strtod((char *)p2, (char **)&p2);
10713 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
10714 }
10715
10716 /* When the result would be zero, compare the item indexes. Makes the
10717 * sort stable. */
10718 if (res == 0 && !sortinfo->item_compare_keep_zero)
10719 res = si1->idx > si2->idx ? 1 : -1;
10720
10721 vim_free(tofree1);
10722 vim_free(tofree2);
10723 return res;
10724}
10725
10726 static int
10727#ifdef __BORLANDC__
10728_RTLENTRYF
10729#endif
10730item_compare2(const void *s1, const void *s2)
10731{
10732 sortItem_T *si1, *si2;
10733 int res;
10734 typval_T rettv;
10735 typval_T argv[3];
10736 int dummy;
10737 char_u *func_name;
10738 partial_T *partial = sortinfo->item_compare_partial;
10739
10740 /* shortcut after failure in previous call; compare all items equal */
10741 if (sortinfo->item_compare_func_err)
10742 return 0;
10743
10744 si1 = (sortItem_T *)s1;
10745 si2 = (sortItem_T *)s2;
10746
10747 if (partial == NULL)
10748 func_name = sortinfo->item_compare_func;
10749 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020010750 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010751
10752 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
10753 * in the copy without changing the original list items. */
10754 copy_tv(&si1->item->li_tv, &argv[0]);
10755 copy_tv(&si2->item->li_tv, &argv[1]);
10756
10757 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
10758 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020010759 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010760 partial, sortinfo->item_compare_selfdict);
10761 clear_tv(&argv[0]);
10762 clear_tv(&argv[1]);
10763
10764 if (res == FAIL)
10765 res = ITEM_COMPARE_FAIL;
10766 else
10767 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
10768 if (sortinfo->item_compare_func_err)
10769 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
10770 clear_tv(&rettv);
10771
10772 /* When the result would be zero, compare the pointers themselves. Makes
10773 * the sort stable. */
10774 if (res == 0 && !sortinfo->item_compare_keep_zero)
10775 res = si1->idx > si2->idx ? 1 : -1;
10776
10777 return res;
10778}
10779
10780/*
10781 * "sort({list})" function
10782 */
10783 static void
10784do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
10785{
10786 list_T *l;
10787 listitem_T *li;
10788 sortItem_T *ptrs;
10789 sortinfo_T *old_sortinfo;
10790 sortinfo_T info;
10791 long len;
10792 long i;
10793
10794 /* Pointer to current info struct used in compare function. Save and
10795 * restore the current one for nested calls. */
10796 old_sortinfo = sortinfo;
10797 sortinfo = &info;
10798
10799 if (argvars[0].v_type != VAR_LIST)
10800 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
10801 else
10802 {
10803 l = argvars[0].vval.v_list;
10804 if (l == NULL || tv_check_lock(l->lv_lock,
10805 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
10806 TRUE))
10807 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020010808 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010809
10810 len = list_len(l);
10811 if (len <= 1)
10812 goto theend; /* short list sorts pretty quickly */
10813
10814 info.item_compare_ic = FALSE;
10815 info.item_compare_numeric = FALSE;
10816 info.item_compare_numbers = FALSE;
10817#ifdef FEAT_FLOAT
10818 info.item_compare_float = FALSE;
10819#endif
10820 info.item_compare_func = NULL;
10821 info.item_compare_partial = NULL;
10822 info.item_compare_selfdict = NULL;
10823 if (argvars[1].v_type != VAR_UNKNOWN)
10824 {
10825 /* optional second argument: {func} */
10826 if (argvars[1].v_type == VAR_FUNC)
10827 info.item_compare_func = argvars[1].vval.v_string;
10828 else if (argvars[1].v_type == VAR_PARTIAL)
10829 info.item_compare_partial = argvars[1].vval.v_partial;
10830 else
10831 {
10832 int error = FALSE;
10833
10834 i = (long)get_tv_number_chk(&argvars[1], &error);
10835 if (error)
10836 goto theend; /* type error; errmsg already given */
10837 if (i == 1)
10838 info.item_compare_ic = TRUE;
10839 else if (argvars[1].v_type != VAR_NUMBER)
10840 info.item_compare_func = get_tv_string(&argvars[1]);
10841 else if (i != 0)
10842 {
10843 EMSG(_(e_invarg));
10844 goto theend;
10845 }
10846 if (info.item_compare_func != NULL)
10847 {
10848 if (*info.item_compare_func == NUL)
10849 {
10850 /* empty string means default sort */
10851 info.item_compare_func = NULL;
10852 }
10853 else if (STRCMP(info.item_compare_func, "n") == 0)
10854 {
10855 info.item_compare_func = NULL;
10856 info.item_compare_numeric = TRUE;
10857 }
10858 else if (STRCMP(info.item_compare_func, "N") == 0)
10859 {
10860 info.item_compare_func = NULL;
10861 info.item_compare_numbers = TRUE;
10862 }
10863#ifdef FEAT_FLOAT
10864 else if (STRCMP(info.item_compare_func, "f") == 0)
10865 {
10866 info.item_compare_func = NULL;
10867 info.item_compare_float = TRUE;
10868 }
10869#endif
10870 else if (STRCMP(info.item_compare_func, "i") == 0)
10871 {
10872 info.item_compare_func = NULL;
10873 info.item_compare_ic = TRUE;
10874 }
10875 }
10876 }
10877
10878 if (argvars[2].v_type != VAR_UNKNOWN)
10879 {
10880 /* optional third argument: {dict} */
10881 if (argvars[2].v_type != VAR_DICT)
10882 {
10883 EMSG(_(e_dictreq));
10884 goto theend;
10885 }
10886 info.item_compare_selfdict = argvars[2].vval.v_dict;
10887 }
10888 }
10889
10890 /* Make an array with each entry pointing to an item in the List. */
10891 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
10892 if (ptrs == NULL)
10893 goto theend;
10894
10895 i = 0;
10896 if (sort)
10897 {
10898 /* sort(): ptrs will be the list to sort */
10899 for (li = l->lv_first; li != NULL; li = li->li_next)
10900 {
10901 ptrs[i].item = li;
10902 ptrs[i].idx = i;
10903 ++i;
10904 }
10905
10906 info.item_compare_func_err = FALSE;
10907 info.item_compare_keep_zero = FALSE;
10908 /* test the compare function */
10909 if ((info.item_compare_func != NULL
10910 || info.item_compare_partial != NULL)
10911 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
10912 == ITEM_COMPARE_FAIL)
10913 EMSG(_("E702: Sort compare function failed"));
10914 else
10915 {
10916 /* Sort the array with item pointers. */
10917 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
10918 info.item_compare_func == NULL
10919 && info.item_compare_partial == NULL
10920 ? item_compare : item_compare2);
10921
10922 if (!info.item_compare_func_err)
10923 {
10924 /* Clear the List and append the items in sorted order. */
10925 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
10926 l->lv_len = 0;
10927 for (i = 0; i < len; ++i)
10928 list_append(l, ptrs[i].item);
10929 }
10930 }
10931 }
10932 else
10933 {
10934 int (*item_compare_func_ptr)(const void *, const void *);
10935
10936 /* f_uniq(): ptrs will be a stack of items to remove */
10937 info.item_compare_func_err = FALSE;
10938 info.item_compare_keep_zero = TRUE;
10939 item_compare_func_ptr = info.item_compare_func != NULL
10940 || info.item_compare_partial != NULL
10941 ? item_compare2 : item_compare;
10942
10943 for (li = l->lv_first; li != NULL && li->li_next != NULL;
10944 li = li->li_next)
10945 {
10946 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
10947 == 0)
10948 ptrs[i++].item = li;
10949 if (info.item_compare_func_err)
10950 {
10951 EMSG(_("E882: Uniq compare function failed"));
10952 break;
10953 }
10954 }
10955
10956 if (!info.item_compare_func_err)
10957 {
10958 while (--i >= 0)
10959 {
10960 li = ptrs[i].item->li_next;
10961 ptrs[i].item->li_next = li->li_next;
10962 if (li->li_next != NULL)
10963 li->li_next->li_prev = ptrs[i].item;
10964 else
10965 l->lv_last = ptrs[i].item;
10966 list_fix_watch(l, li);
10967 listitem_free(li);
10968 l->lv_len--;
10969 }
10970 }
10971 }
10972
10973 vim_free(ptrs);
10974 }
10975theend:
10976 sortinfo = old_sortinfo;
10977}
10978
10979/*
10980 * "sort({list})" function
10981 */
10982 static void
10983f_sort(typval_T *argvars, typval_T *rettv)
10984{
10985 do_sort_uniq(argvars, rettv, TRUE);
10986}
10987
10988/*
10989 * "uniq({list})" function
10990 */
10991 static void
10992f_uniq(typval_T *argvars, typval_T *rettv)
10993{
10994 do_sort_uniq(argvars, rettv, FALSE);
10995}
10996
10997/*
10998 * "soundfold({word})" function
10999 */
11000 static void
11001f_soundfold(typval_T *argvars, typval_T *rettv)
11002{
11003 char_u *s;
11004
11005 rettv->v_type = VAR_STRING;
11006 s = get_tv_string(&argvars[0]);
11007#ifdef FEAT_SPELL
11008 rettv->vval.v_string = eval_soundfold(s);
11009#else
11010 rettv->vval.v_string = vim_strsave(s);
11011#endif
11012}
11013
11014/*
11015 * "spellbadword()" function
11016 */
11017 static void
11018f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
11019{
11020 char_u *word = (char_u *)"";
11021 hlf_T attr = HLF_COUNT;
11022 int len = 0;
11023
11024 if (rettv_list_alloc(rettv) == FAIL)
11025 return;
11026
11027#ifdef FEAT_SPELL
11028 if (argvars[0].v_type == VAR_UNKNOWN)
11029 {
11030 /* Find the start and length of the badly spelled word. */
11031 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
11032 if (len != 0)
11033 word = ml_get_cursor();
11034 }
11035 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
11036 {
11037 char_u *str = get_tv_string_chk(&argvars[0]);
11038 int capcol = -1;
11039
11040 if (str != NULL)
11041 {
11042 /* Check the argument for spelling. */
11043 while (*str != NUL)
11044 {
11045 len = spell_check(curwin, str, &attr, &capcol, FALSE);
11046 if (attr != HLF_COUNT)
11047 {
11048 word = str;
11049 break;
11050 }
11051 str += len;
11052 }
11053 }
11054 }
11055#endif
11056
11057 list_append_string(rettv->vval.v_list, word, len);
11058 list_append_string(rettv->vval.v_list, (char_u *)(
11059 attr == HLF_SPB ? "bad" :
11060 attr == HLF_SPR ? "rare" :
11061 attr == HLF_SPL ? "local" :
11062 attr == HLF_SPC ? "caps" :
11063 ""), -1);
11064}
11065
11066/*
11067 * "spellsuggest()" function
11068 */
11069 static void
11070f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
11071{
11072#ifdef FEAT_SPELL
11073 char_u *str;
11074 int typeerr = FALSE;
11075 int maxcount;
11076 garray_T ga;
11077 int i;
11078 listitem_T *li;
11079 int need_capital = FALSE;
11080#endif
11081
11082 if (rettv_list_alloc(rettv) == FAIL)
11083 return;
11084
11085#ifdef FEAT_SPELL
11086 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
11087 {
11088 str = get_tv_string(&argvars[0]);
11089 if (argvars[1].v_type != VAR_UNKNOWN)
11090 {
11091 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
11092 if (maxcount <= 0)
11093 return;
11094 if (argvars[2].v_type != VAR_UNKNOWN)
11095 {
11096 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
11097 if (typeerr)
11098 return;
11099 }
11100 }
11101 else
11102 maxcount = 25;
11103
11104 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
11105
11106 for (i = 0; i < ga.ga_len; ++i)
11107 {
11108 str = ((char_u **)ga.ga_data)[i];
11109
11110 li = listitem_alloc();
11111 if (li == NULL)
11112 vim_free(str);
11113 else
11114 {
11115 li->li_tv.v_type = VAR_STRING;
11116 li->li_tv.v_lock = 0;
11117 li->li_tv.vval.v_string = str;
11118 list_append(rettv->vval.v_list, li);
11119 }
11120 }
11121 ga_clear(&ga);
11122 }
11123#endif
11124}
11125
11126 static void
11127f_split(typval_T *argvars, typval_T *rettv)
11128{
11129 char_u *str;
11130 char_u *end;
11131 char_u *pat = NULL;
11132 regmatch_T regmatch;
11133 char_u patbuf[NUMBUFLEN];
11134 char_u *save_cpo;
11135 int match;
11136 colnr_T col = 0;
11137 int keepempty = FALSE;
11138 int typeerr = FALSE;
11139
11140 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11141 save_cpo = p_cpo;
11142 p_cpo = (char_u *)"";
11143
11144 str = get_tv_string(&argvars[0]);
11145 if (argvars[1].v_type != VAR_UNKNOWN)
11146 {
11147 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11148 if (pat == NULL)
11149 typeerr = TRUE;
11150 if (argvars[2].v_type != VAR_UNKNOWN)
11151 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
11152 }
11153 if (pat == NULL || *pat == NUL)
11154 pat = (char_u *)"[\\x01- ]\\+";
11155
11156 if (rettv_list_alloc(rettv) == FAIL)
11157 return;
11158 if (typeerr)
11159 return;
11160
11161 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11162 if (regmatch.regprog != NULL)
11163 {
11164 regmatch.rm_ic = FALSE;
11165 while (*str != NUL || keepempty)
11166 {
11167 if (*str == NUL)
11168 match = FALSE; /* empty item at the end */
11169 else
11170 match = vim_regexec_nl(&regmatch, str, col);
11171 if (match)
11172 end = regmatch.startp[0];
11173 else
11174 end = str + STRLEN(str);
11175 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
11176 && *str != NUL && match && end < regmatch.endp[0]))
11177 {
11178 if (list_append_string(rettv->vval.v_list, str,
11179 (int)(end - str)) == FAIL)
11180 break;
11181 }
11182 if (!match)
11183 break;
11184 /* Advance to just after the match. */
11185 if (regmatch.endp[0] > str)
11186 col = 0;
11187 else
11188 {
11189 /* Don't get stuck at the same match. */
11190#ifdef FEAT_MBYTE
11191 col = (*mb_ptr2len)(regmatch.endp[0]);
11192#else
11193 col = 1;
11194#endif
11195 }
11196 str = regmatch.endp[0];
11197 }
11198
11199 vim_regfree(regmatch.regprog);
11200 }
11201
11202 p_cpo = save_cpo;
11203}
11204
11205#ifdef FEAT_FLOAT
11206/*
11207 * "sqrt()" function
11208 */
11209 static void
11210f_sqrt(typval_T *argvars, typval_T *rettv)
11211{
11212 float_T f = 0.0;
11213
11214 rettv->v_type = VAR_FLOAT;
11215 if (get_float_arg(argvars, &f) == OK)
11216 rettv->vval.v_float = sqrt(f);
11217 else
11218 rettv->vval.v_float = 0.0;
11219}
11220
11221/*
11222 * "str2float()" function
11223 */
11224 static void
11225f_str2float(typval_T *argvars, typval_T *rettv)
11226{
11227 char_u *p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011228 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011229
Bram Moolenaar08243d22017-01-10 16:12:29 +010011230 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011231 p = skipwhite(p + 1);
11232 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011233 if (isneg)
11234 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011235 rettv->v_type = VAR_FLOAT;
11236}
11237#endif
11238
11239/*
11240 * "str2nr()" function
11241 */
11242 static void
11243f_str2nr(typval_T *argvars, typval_T *rettv)
11244{
11245 int base = 10;
11246 char_u *p;
11247 varnumber_T n;
11248 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010011249 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011250
11251 if (argvars[1].v_type != VAR_UNKNOWN)
11252 {
11253 base = (int)get_tv_number(&argvars[1]);
11254 if (base != 2 && base != 8 && base != 10 && base != 16)
11255 {
11256 EMSG(_(e_invarg));
11257 return;
11258 }
11259 }
11260
11261 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011262 isneg = (*p == '-');
11263 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011264 p = skipwhite(p + 1);
11265 switch (base)
11266 {
11267 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
11268 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
11269 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
11270 default: what = 0;
11271 }
11272 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011273 if (isneg)
11274 rettv->vval.v_number = -n;
11275 else
11276 rettv->vval.v_number = n;
11277
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011278}
11279
11280#ifdef HAVE_STRFTIME
11281/*
11282 * "strftime({format}[, {time}])" function
11283 */
11284 static void
11285f_strftime(typval_T *argvars, typval_T *rettv)
11286{
11287 char_u result_buf[256];
11288 struct tm *curtime;
11289 time_t seconds;
11290 char_u *p;
11291
11292 rettv->v_type = VAR_STRING;
11293
11294 p = get_tv_string(&argvars[0]);
11295 if (argvars[1].v_type == VAR_UNKNOWN)
11296 seconds = time(NULL);
11297 else
11298 seconds = (time_t)get_tv_number(&argvars[1]);
11299 curtime = localtime(&seconds);
11300 /* MSVC returns NULL for an invalid value of seconds. */
11301 if (curtime == NULL)
11302 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
11303 else
11304 {
11305# ifdef FEAT_MBYTE
11306 vimconv_T conv;
11307 char_u *enc;
11308
11309 conv.vc_type = CONV_NONE;
11310 enc = enc_locale();
11311 convert_setup(&conv, p_enc, enc);
11312 if (conv.vc_type != CONV_NONE)
11313 p = string_convert(&conv, p, NULL);
11314# endif
11315 if (p != NULL)
11316 (void)strftime((char *)result_buf, sizeof(result_buf),
11317 (char *)p, curtime);
11318 else
11319 result_buf[0] = NUL;
11320
11321# ifdef FEAT_MBYTE
11322 if (conv.vc_type != CONV_NONE)
11323 vim_free(p);
11324 convert_setup(&conv, enc, p_enc);
11325 if (conv.vc_type != CONV_NONE)
11326 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
11327 else
11328# endif
11329 rettv->vval.v_string = vim_strsave(result_buf);
11330
11331# ifdef FEAT_MBYTE
11332 /* Release conversion descriptors */
11333 convert_setup(&conv, NULL, NULL);
11334 vim_free(enc);
11335# endif
11336 }
11337}
11338#endif
11339
11340/*
11341 * "strgetchar()" function
11342 */
11343 static void
11344f_strgetchar(typval_T *argvars, typval_T *rettv)
11345{
11346 char_u *str;
11347 int len;
11348 int error = FALSE;
11349 int charidx;
11350
11351 rettv->vval.v_number = -1;
11352 str = get_tv_string_chk(&argvars[0]);
11353 if (str == NULL)
11354 return;
11355 len = (int)STRLEN(str);
11356 charidx = (int)get_tv_number_chk(&argvars[1], &error);
11357 if (error)
11358 return;
11359#ifdef FEAT_MBYTE
11360 {
11361 int byteidx = 0;
11362
11363 while (charidx >= 0 && byteidx < len)
11364 {
11365 if (charidx == 0)
11366 {
11367 rettv->vval.v_number = mb_ptr2char(str + byteidx);
11368 break;
11369 }
11370 --charidx;
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011371 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011372 }
11373 }
11374#else
11375 if (charidx < len)
11376 rettv->vval.v_number = str[charidx];
11377#endif
11378}
11379
11380/*
11381 * "stridx()" function
11382 */
11383 static void
11384f_stridx(typval_T *argvars, typval_T *rettv)
11385{
11386 char_u buf[NUMBUFLEN];
11387 char_u *needle;
11388 char_u *haystack;
11389 char_u *save_haystack;
11390 char_u *pos;
11391 int start_idx;
11392
11393 needle = get_tv_string_chk(&argvars[1]);
11394 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
11395 rettv->vval.v_number = -1;
11396 if (needle == NULL || haystack == NULL)
11397 return; /* type error; errmsg already given */
11398
11399 if (argvars[2].v_type != VAR_UNKNOWN)
11400 {
11401 int error = FALSE;
11402
11403 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
11404 if (error || start_idx >= (int)STRLEN(haystack))
11405 return;
11406 if (start_idx >= 0)
11407 haystack += start_idx;
11408 }
11409
11410 pos = (char_u *)strstr((char *)haystack, (char *)needle);
11411 if (pos != NULL)
11412 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
11413}
11414
11415/*
11416 * "string()" function
11417 */
11418 static void
11419f_string(typval_T *argvars, typval_T *rettv)
11420{
11421 char_u *tofree;
11422 char_u numbuf[NUMBUFLEN];
11423
11424 rettv->v_type = VAR_STRING;
11425 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
11426 get_copyID());
11427 /* Make a copy if we have a value but it's not in allocated memory. */
11428 if (rettv->vval.v_string != NULL && tofree == NULL)
11429 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
11430}
11431
11432/*
11433 * "strlen()" function
11434 */
11435 static void
11436f_strlen(typval_T *argvars, typval_T *rettv)
11437{
11438 rettv->vval.v_number = (varnumber_T)(STRLEN(
11439 get_tv_string(&argvars[0])));
11440}
11441
11442/*
11443 * "strchars()" function
11444 */
11445 static void
11446f_strchars(typval_T *argvars, typval_T *rettv)
11447{
11448 char_u *s = get_tv_string(&argvars[0]);
11449 int skipcc = 0;
11450#ifdef FEAT_MBYTE
11451 varnumber_T len = 0;
11452 int (*func_mb_ptr2char_adv)(char_u **pp);
11453#endif
11454
11455 if (argvars[1].v_type != VAR_UNKNOWN)
11456 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
11457 if (skipcc < 0 || skipcc > 1)
11458 EMSG(_(e_invarg));
11459 else
11460 {
11461#ifdef FEAT_MBYTE
11462 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
11463 while (*s != NUL)
11464 {
11465 func_mb_ptr2char_adv(&s);
11466 ++len;
11467 }
11468 rettv->vval.v_number = len;
11469#else
11470 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
11471#endif
11472 }
11473}
11474
11475/*
11476 * "strdisplaywidth()" function
11477 */
11478 static void
11479f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
11480{
11481 char_u *s = get_tv_string(&argvars[0]);
11482 int col = 0;
11483
11484 if (argvars[1].v_type != VAR_UNKNOWN)
11485 col = (int)get_tv_number(&argvars[1]);
11486
11487 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
11488}
11489
11490/*
11491 * "strwidth()" function
11492 */
11493 static void
11494f_strwidth(typval_T *argvars, typval_T *rettv)
11495{
11496 char_u *s = get_tv_string(&argvars[0]);
11497
11498 rettv->vval.v_number = (varnumber_T)(
11499#ifdef FEAT_MBYTE
11500 mb_string2cells(s, -1)
11501#else
11502 STRLEN(s)
11503#endif
11504 );
11505}
11506
11507/*
11508 * "strcharpart()" function
11509 */
11510 static void
11511f_strcharpart(typval_T *argvars, typval_T *rettv)
11512{
11513#ifdef FEAT_MBYTE
11514 char_u *p;
11515 int nchar;
11516 int nbyte = 0;
11517 int charlen;
11518 int len = 0;
11519 int slen;
11520 int error = FALSE;
11521
11522 p = get_tv_string(&argvars[0]);
11523 slen = (int)STRLEN(p);
11524
11525 nchar = (int)get_tv_number_chk(&argvars[1], &error);
11526 if (!error)
11527 {
11528 if (nchar > 0)
11529 while (nchar > 0 && nbyte < slen)
11530 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011531 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011532 --nchar;
11533 }
11534 else
11535 nbyte = nchar;
11536 if (argvars[2].v_type != VAR_UNKNOWN)
11537 {
11538 charlen = (int)get_tv_number(&argvars[2]);
11539 while (charlen > 0 && nbyte + len < slen)
11540 {
11541 int off = nbyte + len;
11542
11543 if (off < 0)
11544 len += 1;
11545 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011546 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011547 --charlen;
11548 }
11549 }
11550 else
11551 len = slen - nbyte; /* default: all bytes that are available. */
11552 }
11553
11554 /*
11555 * Only return the overlap between the specified part and the actual
11556 * string.
11557 */
11558 if (nbyte < 0)
11559 {
11560 len += nbyte;
11561 nbyte = 0;
11562 }
11563 else if (nbyte > slen)
11564 nbyte = slen;
11565 if (len < 0)
11566 len = 0;
11567 else if (nbyte + len > slen)
11568 len = slen - nbyte;
11569
11570 rettv->v_type = VAR_STRING;
11571 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
11572#else
11573 f_strpart(argvars, rettv);
11574#endif
11575}
11576
11577/*
11578 * "strpart()" function
11579 */
11580 static void
11581f_strpart(typval_T *argvars, typval_T *rettv)
11582{
11583 char_u *p;
11584 int n;
11585 int len;
11586 int slen;
11587 int error = FALSE;
11588
11589 p = get_tv_string(&argvars[0]);
11590 slen = (int)STRLEN(p);
11591
11592 n = (int)get_tv_number_chk(&argvars[1], &error);
11593 if (error)
11594 len = 0;
11595 else if (argvars[2].v_type != VAR_UNKNOWN)
11596 len = (int)get_tv_number(&argvars[2]);
11597 else
11598 len = slen - n; /* default len: all bytes that are available. */
11599
11600 /*
11601 * Only return the overlap between the specified part and the actual
11602 * string.
11603 */
11604 if (n < 0)
11605 {
11606 len += n;
11607 n = 0;
11608 }
11609 else if (n > slen)
11610 n = slen;
11611 if (len < 0)
11612 len = 0;
11613 else if (n + len > slen)
11614 len = slen - n;
11615
11616 rettv->v_type = VAR_STRING;
11617 rettv->vval.v_string = vim_strnsave(p + n, len);
11618}
11619
11620/*
11621 * "strridx()" function
11622 */
11623 static void
11624f_strridx(typval_T *argvars, typval_T *rettv)
11625{
11626 char_u buf[NUMBUFLEN];
11627 char_u *needle;
11628 char_u *haystack;
11629 char_u *rest;
11630 char_u *lastmatch = NULL;
11631 int haystack_len, end_idx;
11632
11633 needle = get_tv_string_chk(&argvars[1]);
11634 haystack = get_tv_string_buf_chk(&argvars[0], buf);
11635
11636 rettv->vval.v_number = -1;
11637 if (needle == NULL || haystack == NULL)
11638 return; /* type error; errmsg already given */
11639
11640 haystack_len = (int)STRLEN(haystack);
11641 if (argvars[2].v_type != VAR_UNKNOWN)
11642 {
11643 /* Third argument: upper limit for index */
11644 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
11645 if (end_idx < 0)
11646 return; /* can never find a match */
11647 }
11648 else
11649 end_idx = haystack_len;
11650
11651 if (*needle == NUL)
11652 {
11653 /* Empty string matches past the end. */
11654 lastmatch = haystack + end_idx;
11655 }
11656 else
11657 {
11658 for (rest = haystack; *rest != '\0'; ++rest)
11659 {
11660 rest = (char_u *)strstr((char *)rest, (char *)needle);
11661 if (rest == NULL || rest > haystack + end_idx)
11662 break;
11663 lastmatch = rest;
11664 }
11665 }
11666
11667 if (lastmatch == NULL)
11668 rettv->vval.v_number = -1;
11669 else
11670 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
11671}
11672
11673/*
11674 * "strtrans()" function
11675 */
11676 static void
11677f_strtrans(typval_T *argvars, typval_T *rettv)
11678{
11679 rettv->v_type = VAR_STRING;
11680 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
11681}
11682
11683/*
11684 * "submatch()" function
11685 */
11686 static void
11687f_submatch(typval_T *argvars, typval_T *rettv)
11688{
11689 int error = FALSE;
11690 int no;
11691 int retList = 0;
11692
11693 no = (int)get_tv_number_chk(&argvars[0], &error);
11694 if (error)
11695 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020011696 if (no < 0 || no >= NSUBEXP)
11697 {
Bram Moolenaar79518e22017-02-17 16:31:35 +010011698 EMSGN(_("E935: invalid submatch number: %d"), no);
11699 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020011700 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011701 if (argvars[1].v_type != VAR_UNKNOWN)
11702 retList = (int)get_tv_number_chk(&argvars[1], &error);
11703 if (error)
11704 return;
11705
11706 if (retList == 0)
11707 {
11708 rettv->v_type = VAR_STRING;
11709 rettv->vval.v_string = reg_submatch(no);
11710 }
11711 else
11712 {
11713 rettv->v_type = VAR_LIST;
11714 rettv->vval.v_list = reg_submatch_list(no);
11715 }
11716}
11717
11718/*
11719 * "substitute()" function
11720 */
11721 static void
11722f_substitute(typval_T *argvars, typval_T *rettv)
11723{
11724 char_u patbuf[NUMBUFLEN];
11725 char_u subbuf[NUMBUFLEN];
11726 char_u flagsbuf[NUMBUFLEN];
11727
11728 char_u *str = get_tv_string_chk(&argvars[0]);
11729 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011730 char_u *sub = NULL;
11731 typval_T *expr = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011732 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
11733
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011734 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
11735 expr = &argvars[2];
11736 else
11737 sub = get_tv_string_buf_chk(&argvars[2], subbuf);
11738
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011739 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011740 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
11741 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011742 rettv->vval.v_string = NULL;
11743 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011744 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011745}
11746
11747/*
11748 * "synID(lnum, col, trans)" function
11749 */
11750 static void
11751f_synID(typval_T *argvars UNUSED, typval_T *rettv)
11752{
11753 int id = 0;
11754#ifdef FEAT_SYN_HL
11755 linenr_T lnum;
11756 colnr_T col;
11757 int trans;
11758 int transerr = FALSE;
11759
11760 lnum = get_tv_lnum(argvars); /* -1 on type error */
11761 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
11762 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
11763
11764 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11765 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
11766 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
11767#endif
11768
11769 rettv->vval.v_number = id;
11770}
11771
11772/*
11773 * "synIDattr(id, what [, mode])" function
11774 */
11775 static void
11776f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
11777{
11778 char_u *p = NULL;
11779#ifdef FEAT_SYN_HL
11780 int id;
11781 char_u *what;
11782 char_u *mode;
11783 char_u modebuf[NUMBUFLEN];
11784 int modec;
11785
11786 id = (int)get_tv_number(&argvars[0]);
11787 what = get_tv_string(&argvars[1]);
11788 if (argvars[2].v_type != VAR_UNKNOWN)
11789 {
11790 mode = get_tv_string_buf(&argvars[2], modebuf);
11791 modec = TOLOWER_ASC(mode[0]);
11792 if (modec != 't' && modec != 'c' && modec != 'g')
11793 modec = 0; /* replace invalid with current */
11794 }
11795 else
11796 {
11797#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
11798 if (USE_24BIT)
11799 modec = 'g';
11800 else
11801#endif
11802 if (t_colors > 1)
11803 modec = 'c';
11804 else
11805 modec = 't';
11806 }
11807
11808
11809 switch (TOLOWER_ASC(what[0]))
11810 {
11811 case 'b':
11812 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
11813 p = highlight_color(id, what, modec);
11814 else /* bold */
11815 p = highlight_has_attr(id, HL_BOLD, modec);
11816 break;
11817
11818 case 'f': /* fg[#] or font */
11819 p = highlight_color(id, what, modec);
11820 break;
11821
11822 case 'i':
11823 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
11824 p = highlight_has_attr(id, HL_INVERSE, modec);
11825 else /* italic */
11826 p = highlight_has_attr(id, HL_ITALIC, modec);
11827 break;
11828
11829 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020011830 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011831 break;
11832
11833 case 'r': /* reverse */
11834 p = highlight_has_attr(id, HL_INVERSE, modec);
11835 break;
11836
11837 case 's':
11838 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
11839 p = highlight_color(id, what, modec);
11840 else /* standout */
11841 p = highlight_has_attr(id, HL_STANDOUT, modec);
11842 break;
11843
11844 case 'u':
11845 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
11846 /* underline */
11847 p = highlight_has_attr(id, HL_UNDERLINE, modec);
11848 else
11849 /* undercurl */
11850 p = highlight_has_attr(id, HL_UNDERCURL, modec);
11851 break;
11852 }
11853
11854 if (p != NULL)
11855 p = vim_strsave(p);
11856#endif
11857 rettv->v_type = VAR_STRING;
11858 rettv->vval.v_string = p;
11859}
11860
11861/*
11862 * "synIDtrans(id)" function
11863 */
11864 static void
11865f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
11866{
11867 int id;
11868
11869#ifdef FEAT_SYN_HL
11870 id = (int)get_tv_number(&argvars[0]);
11871
11872 if (id > 0)
11873 id = syn_get_final_id(id);
11874 else
11875#endif
11876 id = 0;
11877
11878 rettv->vval.v_number = id;
11879}
11880
11881/*
11882 * "synconcealed(lnum, col)" function
11883 */
11884 static void
11885f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
11886{
11887#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
11888 linenr_T lnum;
11889 colnr_T col;
11890 int syntax_flags = 0;
11891 int cchar;
11892 int matchid = 0;
11893 char_u str[NUMBUFLEN];
11894#endif
11895
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011896 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011897
11898#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
11899 lnum = get_tv_lnum(argvars); /* -1 on type error */
11900 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
11901
11902 vim_memset(str, NUL, sizeof(str));
11903
11904 if (rettv_list_alloc(rettv) != FAIL)
11905 {
11906 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11907 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
11908 && curwin->w_p_cole > 0)
11909 {
11910 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
11911 syntax_flags = get_syntax_info(&matchid);
11912
11913 /* get the conceal character */
11914 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
11915 {
11916 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020011917 if (cchar == NUL && curwin->w_p_cole == 1)
11918 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011919 if (cchar != NUL)
11920 {
11921# ifdef FEAT_MBYTE
11922 if (has_mbyte)
11923 (*mb_char2bytes)(cchar, str);
11924 else
11925# endif
11926 str[0] = cchar;
11927 }
11928 }
11929 }
11930
11931 list_append_number(rettv->vval.v_list,
11932 (syntax_flags & HL_CONCEAL) != 0);
11933 /* -1 to auto-determine strlen */
11934 list_append_string(rettv->vval.v_list, str, -1);
11935 list_append_number(rettv->vval.v_list, matchid);
11936 }
11937#endif
11938}
11939
11940/*
11941 * "synstack(lnum, col)" function
11942 */
11943 static void
11944f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
11945{
11946#ifdef FEAT_SYN_HL
11947 linenr_T lnum;
11948 colnr_T col;
11949 int i;
11950 int id;
11951#endif
11952
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011953 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011954
11955#ifdef FEAT_SYN_HL
11956 lnum = get_tv_lnum(argvars); /* -1 on type error */
11957 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
11958
11959 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11960 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
11961 && rettv_list_alloc(rettv) != FAIL)
11962 {
11963 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
11964 for (i = 0; ; ++i)
11965 {
11966 id = syn_get_stack_item(i);
11967 if (id < 0)
11968 break;
11969 if (list_append_number(rettv->vval.v_list, id) == FAIL)
11970 break;
11971 }
11972 }
11973#endif
11974}
11975
11976 static void
11977get_cmd_output_as_rettv(
11978 typval_T *argvars,
11979 typval_T *rettv,
11980 int retlist)
11981{
11982 char_u *res = NULL;
11983 char_u *p;
11984 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011985 int err = FALSE;
11986 FILE *fd;
11987 list_T *list = NULL;
11988 int flags = SHELL_SILENT;
11989
11990 rettv->v_type = VAR_STRING;
11991 rettv->vval.v_string = NULL;
11992 if (check_restricted() || check_secure())
11993 goto errret;
11994
11995 if (argvars[1].v_type != VAR_UNKNOWN)
11996 {
11997 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010011998 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011999 * command.
12000 */
12001 if ((infile = vim_tempname('i', TRUE)) == NULL)
12002 {
12003 EMSG(_(e_notmp));
12004 goto errret;
12005 }
12006
12007 fd = mch_fopen((char *)infile, WRITEBIN);
12008 if (fd == NULL)
12009 {
12010 EMSG2(_(e_notopen), infile);
12011 goto errret;
12012 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010012013 if (argvars[1].v_type == VAR_NUMBER)
12014 {
12015 linenr_T lnum;
12016 buf_T *buf;
12017
12018 buf = buflist_findnr(argvars[1].vval.v_number);
12019 if (buf == NULL)
12020 {
12021 EMSGN(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010012022 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010012023 goto errret;
12024 }
12025
12026 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
12027 {
12028 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
12029 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
12030 {
12031 err = TRUE;
12032 break;
12033 }
12034 if (putc(NL, fd) == EOF)
12035 {
12036 err = TRUE;
12037 break;
12038 }
12039 }
12040 }
12041 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012042 {
12043 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
12044 err = TRUE;
12045 }
12046 else
12047 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010012048 size_t len;
12049 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012050
12051 p = get_tv_string_buf_chk(&argvars[1], buf);
12052 if (p == NULL)
12053 {
12054 fclose(fd);
12055 goto errret; /* type error; errmsg already given */
12056 }
12057 len = STRLEN(p);
12058 if (len > 0 && fwrite(p, len, 1, fd) != 1)
12059 err = TRUE;
12060 }
12061 if (fclose(fd) != 0)
12062 err = TRUE;
12063 if (err)
12064 {
12065 EMSG(_("E677: Error writing temp file"));
12066 goto errret;
12067 }
12068 }
12069
12070 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
12071 * echoes typeahead, that messes up the display. */
12072 if (!msg_silent)
12073 flags += SHELL_COOKED;
12074
12075 if (retlist)
12076 {
12077 int len;
12078 listitem_T *li;
12079 char_u *s = NULL;
12080 char_u *start;
12081 char_u *end;
12082 int i;
12083
12084 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
12085 if (res == NULL)
12086 goto errret;
12087
12088 list = list_alloc();
12089 if (list == NULL)
12090 goto errret;
12091
12092 for (i = 0; i < len; ++i)
12093 {
12094 start = res + i;
12095 while (i < len && res[i] != NL)
12096 ++i;
12097 end = res + i;
12098
12099 s = alloc((unsigned)(end - start + 1));
12100 if (s == NULL)
12101 goto errret;
12102
12103 for (p = s; start < end; ++p, ++start)
12104 *p = *start == NUL ? NL : *start;
12105 *p = NUL;
12106
12107 li = listitem_alloc();
12108 if (li == NULL)
12109 {
12110 vim_free(s);
12111 goto errret;
12112 }
12113 li->li_tv.v_type = VAR_STRING;
12114 li->li_tv.v_lock = 0;
12115 li->li_tv.vval.v_string = s;
12116 list_append(list, li);
12117 }
12118
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012119 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012120 list = NULL;
12121 }
12122 else
12123 {
12124 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
12125#ifdef USE_CR
12126 /* translate <CR> into <NL> */
12127 if (res != NULL)
12128 {
12129 char_u *s;
12130
12131 for (s = res; *s; ++s)
12132 {
12133 if (*s == CAR)
12134 *s = NL;
12135 }
12136 }
12137#else
12138# ifdef USE_CRNL
12139 /* translate <CR><NL> into <NL> */
12140 if (res != NULL)
12141 {
12142 char_u *s, *d;
12143
12144 d = res;
12145 for (s = res; *s; ++s)
12146 {
12147 if (s[0] == CAR && s[1] == NL)
12148 ++s;
12149 *d++ = *s;
12150 }
12151 *d = NUL;
12152 }
12153# endif
12154#endif
12155 rettv->vval.v_string = res;
12156 res = NULL;
12157 }
12158
12159errret:
12160 if (infile != NULL)
12161 {
12162 mch_remove(infile);
12163 vim_free(infile);
12164 }
12165 if (res != NULL)
12166 vim_free(res);
12167 if (list != NULL)
12168 list_free(list);
12169}
12170
12171/*
12172 * "system()" function
12173 */
12174 static void
12175f_system(typval_T *argvars, typval_T *rettv)
12176{
12177 get_cmd_output_as_rettv(argvars, rettv, FALSE);
12178}
12179
12180/*
12181 * "systemlist()" function
12182 */
12183 static void
12184f_systemlist(typval_T *argvars, typval_T *rettv)
12185{
12186 get_cmd_output_as_rettv(argvars, rettv, TRUE);
12187}
12188
12189/*
12190 * "tabpagebuflist()" function
12191 */
12192 static void
12193f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12194{
12195#ifdef FEAT_WINDOWS
12196 tabpage_T *tp;
12197 win_T *wp = NULL;
12198
12199 if (argvars[0].v_type == VAR_UNKNOWN)
12200 wp = firstwin;
12201 else
12202 {
12203 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12204 if (tp != NULL)
12205 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12206 }
12207 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
12208 {
12209 for (; wp != NULL; wp = wp->w_next)
12210 if (list_append_number(rettv->vval.v_list,
12211 wp->w_buffer->b_fnum) == FAIL)
12212 break;
12213 }
12214#endif
12215}
12216
12217
12218/*
12219 * "tabpagenr()" function
12220 */
12221 static void
12222f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
12223{
12224 int nr = 1;
12225#ifdef FEAT_WINDOWS
12226 char_u *arg;
12227
12228 if (argvars[0].v_type != VAR_UNKNOWN)
12229 {
12230 arg = get_tv_string_chk(&argvars[0]);
12231 nr = 0;
12232 if (arg != NULL)
12233 {
12234 if (STRCMP(arg, "$") == 0)
12235 nr = tabpage_index(NULL) - 1;
12236 else
12237 EMSG2(_(e_invexpr2), arg);
12238 }
12239 }
12240 else
12241 nr = tabpage_index(curtab);
12242#endif
12243 rettv->vval.v_number = nr;
12244}
12245
12246
12247#ifdef FEAT_WINDOWS
12248static int get_winnr(tabpage_T *tp, typval_T *argvar);
12249
12250/*
12251 * Common code for tabpagewinnr() and winnr().
12252 */
12253 static int
12254get_winnr(tabpage_T *tp, typval_T *argvar)
12255{
12256 win_T *twin;
12257 int nr = 1;
12258 win_T *wp;
12259 char_u *arg;
12260
12261 twin = (tp == curtab) ? curwin : tp->tp_curwin;
12262 if (argvar->v_type != VAR_UNKNOWN)
12263 {
12264 arg = get_tv_string_chk(argvar);
12265 if (arg == NULL)
12266 nr = 0; /* type error; errmsg already given */
12267 else if (STRCMP(arg, "$") == 0)
12268 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
12269 else if (STRCMP(arg, "#") == 0)
12270 {
12271 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
12272 if (twin == NULL)
12273 nr = 0;
12274 }
12275 else
12276 {
12277 EMSG2(_(e_invexpr2), arg);
12278 nr = 0;
12279 }
12280 }
12281
12282 if (nr > 0)
12283 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12284 wp != twin; wp = wp->w_next)
12285 {
12286 if (wp == NULL)
12287 {
12288 /* didn't find it in this tabpage */
12289 nr = 0;
12290 break;
12291 }
12292 ++nr;
12293 }
12294 return nr;
12295}
12296#endif
12297
12298/*
12299 * "tabpagewinnr()" function
12300 */
12301 static void
12302f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
12303{
12304 int nr = 1;
12305#ifdef FEAT_WINDOWS
12306 tabpage_T *tp;
12307
12308 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12309 if (tp == NULL)
12310 nr = 0;
12311 else
12312 nr = get_winnr(tp, &argvars[1]);
12313#endif
12314 rettv->vval.v_number = nr;
12315}
12316
12317
12318/*
12319 * "tagfiles()" function
12320 */
12321 static void
12322f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
12323{
12324 char_u *fname;
12325 tagname_T tn;
12326 int first;
12327
12328 if (rettv_list_alloc(rettv) == FAIL)
12329 return;
12330 fname = alloc(MAXPATHL);
12331 if (fname == NULL)
12332 return;
12333
12334 for (first = TRUE; ; first = FALSE)
12335 if (get_tagfname(&tn, first, fname) == FAIL
12336 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
12337 break;
12338 tagname_free(&tn);
12339 vim_free(fname);
12340}
12341
12342/*
12343 * "taglist()" function
12344 */
12345 static void
12346f_taglist(typval_T *argvars, typval_T *rettv)
12347{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012348 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012349 char_u *tag_pattern;
12350
12351 tag_pattern = get_tv_string(&argvars[0]);
12352
12353 rettv->vval.v_number = FALSE;
12354 if (*tag_pattern == NUL)
12355 return;
12356
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012357 if (argvars[1].v_type != VAR_UNKNOWN)
12358 fname = get_tv_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012359 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012360 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012361}
12362
12363/*
12364 * "tempname()" function
12365 */
12366 static void
12367f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
12368{
12369 static int x = 'A';
12370
12371 rettv->v_type = VAR_STRING;
12372 rettv->vval.v_string = vim_tempname(x, FALSE);
12373
12374 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
12375 * names. Skip 'I' and 'O', they are used for shell redirection. */
12376 do
12377 {
12378 if (x == 'Z')
12379 x = '0';
12380 else if (x == '9')
12381 x = 'A';
12382 else
12383 {
12384#ifdef EBCDIC
12385 if (x == 'I')
12386 x = 'J';
12387 else if (x == 'R')
12388 x = 'S';
12389 else
12390#endif
12391 ++x;
12392 }
12393 } while (x == 'I' || x == 'O');
12394}
12395
12396#ifdef FEAT_FLOAT
12397/*
12398 * "tan()" function
12399 */
12400 static void
12401f_tan(typval_T *argvars, typval_T *rettv)
12402{
12403 float_T f = 0.0;
12404
12405 rettv->v_type = VAR_FLOAT;
12406 if (get_float_arg(argvars, &f) == OK)
12407 rettv->vval.v_float = tan(f);
12408 else
12409 rettv->vval.v_float = 0.0;
12410}
12411
12412/*
12413 * "tanh()" function
12414 */
12415 static void
12416f_tanh(typval_T *argvars, typval_T *rettv)
12417{
12418 float_T f = 0.0;
12419
12420 rettv->v_type = VAR_FLOAT;
12421 if (get_float_arg(argvars, &f) == OK)
12422 rettv->vval.v_float = tanh(f);
12423 else
12424 rettv->vval.v_float = 0.0;
12425}
12426#endif
12427
12428/*
12429 * "test_alloc_fail(id, countdown, repeat)" function
12430 */
12431 static void
12432f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
12433{
12434 if (argvars[0].v_type != VAR_NUMBER
12435 || argvars[0].vval.v_number <= 0
12436 || argvars[1].v_type != VAR_NUMBER
12437 || argvars[1].vval.v_number < 0
12438 || argvars[2].v_type != VAR_NUMBER)
12439 EMSG(_(e_invarg));
12440 else
12441 {
12442 alloc_fail_id = argvars[0].vval.v_number;
12443 if (alloc_fail_id >= aid_last)
12444 EMSG(_(e_invarg));
12445 alloc_fail_countdown = argvars[1].vval.v_number;
12446 alloc_fail_repeat = argvars[2].vval.v_number;
12447 did_outofmem_msg = FALSE;
12448 }
12449}
12450
12451/*
12452 * "test_autochdir()"
12453 */
12454 static void
12455f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12456{
12457#if defined(FEAT_AUTOCHDIR)
12458 test_autochdir = TRUE;
12459#endif
12460}
12461
12462/*
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012463 * "test_disable({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012464 */
12465 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012466f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012467{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012468 char_u *name = (char_u *)"";
12469 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012470 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012471
12472 if (argvars[0].v_type != VAR_STRING
12473 || (argvars[1].v_type) != VAR_NUMBER)
12474 EMSG(_(e_invarg));
12475 else
12476 {
12477 name = get_tv_string_chk(&argvars[0]);
12478 val = (int)get_tv_number(&argvars[1]);
12479
12480 if (STRCMP(name, (char_u *)"redraw") == 0)
12481 disable_redraw_for_testing = val;
12482 else if (STRCMP(name, (char_u *)"char_avail") == 0)
12483 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012484 else if (STRCMP(name, (char_u *)"starting") == 0)
12485 {
12486 if (val)
12487 {
12488 if (save_starting < 0)
12489 save_starting = starting;
12490 starting = 0;
12491 }
12492 else
12493 {
12494 starting = save_starting;
12495 save_starting = -1;
12496 }
12497 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012498 else if (STRCMP(name, (char_u *)"ALL") == 0)
12499 {
12500 disable_char_avail_for_testing = FALSE;
12501 disable_redraw_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012502 if (save_starting >= 0)
12503 {
12504 starting = save_starting;
12505 save_starting = -1;
12506 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012507 }
12508 else
12509 EMSG2(_(e_invarg2), name);
12510 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012511}
12512
12513/*
12514 * "test_garbagecollect_now()" function
12515 */
12516 static void
12517f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12518{
12519 /* This is dangerous, any Lists and Dicts used internally may be freed
12520 * while still in use. */
12521 garbage_collect(TRUE);
12522}
12523
Bram Moolenaare0c31f62017-03-01 15:07:05 +010012524/*
12525 * "test_ignore_error()" function
12526 */
12527 static void
12528f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
12529{
12530 ignore_error_for_testing(get_tv_string(&argvars[0]));
12531}
12532
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012533#ifdef FEAT_JOB_CHANNEL
12534 static void
12535f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
12536{
12537 rettv->v_type = VAR_CHANNEL;
12538 rettv->vval.v_channel = NULL;
12539}
12540#endif
12541
12542 static void
12543f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
12544{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012545 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012546}
12547
12548#ifdef FEAT_JOB_CHANNEL
12549 static void
12550f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
12551{
12552 rettv->v_type = VAR_JOB;
12553 rettv->vval.v_job = NULL;
12554}
12555#endif
12556
12557 static void
12558f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
12559{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012560 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012561}
12562
12563 static void
12564f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
12565{
12566 rettv->v_type = VAR_PARTIAL;
12567 rettv->vval.v_partial = NULL;
12568}
12569
12570 static void
12571f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
12572{
12573 rettv->v_type = VAR_STRING;
12574 rettv->vval.v_string = NULL;
12575}
12576
12577 static void
12578f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
12579{
12580 time_for_testing = (time_t)get_tv_number(&argvars[0]);
12581}
12582
12583#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
12584/*
12585 * Get a callback from "arg". It can be a Funcref or a function name.
12586 * When "arg" is zero return an empty string.
12587 * Return NULL for an invalid argument.
12588 */
12589 char_u *
12590get_callback(typval_T *arg, partial_T **pp)
12591{
12592 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
12593 {
12594 *pp = arg->vval.v_partial;
12595 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012596 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012597 }
12598 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012599 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012600 {
12601 func_ref(arg->vval.v_string);
12602 return arg->vval.v_string;
12603 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012604 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
12605 return (char_u *)"";
12606 EMSG(_("E921: Invalid callback argument"));
12607 return NULL;
12608}
12609
12610/*
12611 * Unref/free "callback" and "partial" retured by get_callback().
12612 */
12613 void
12614free_callback(char_u *callback, partial_T *partial)
12615{
12616 if (partial != NULL)
12617 partial_unref(partial);
12618 else if (callback != NULL)
12619 {
12620 func_unref(callback);
12621 vim_free(callback);
12622 }
12623}
12624#endif
12625
12626#ifdef FEAT_TIMERS
12627/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020012628 * "timer_info([timer])" function
12629 */
12630 static void
12631f_timer_info(typval_T *argvars, typval_T *rettv)
12632{
12633 timer_T *timer = NULL;
12634
12635 if (rettv_list_alloc(rettv) != OK)
12636 return;
12637 if (argvars[0].v_type != VAR_UNKNOWN)
12638 {
12639 if (argvars[0].v_type != VAR_NUMBER)
12640 EMSG(_(e_number_exp));
12641 else
12642 {
12643 timer = find_timer((int)get_tv_number(&argvars[0]));
12644 if (timer != NULL)
12645 add_timer_info(rettv, timer);
12646 }
12647 }
12648 else
12649 add_timer_info_all(rettv);
12650}
12651
12652/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012653 * "timer_pause(timer, paused)" function
12654 */
12655 static void
12656f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
12657{
12658 timer_T *timer = NULL;
12659 int paused = (int)get_tv_number(&argvars[1]);
12660
12661 if (argvars[0].v_type != VAR_NUMBER)
12662 EMSG(_(e_number_exp));
12663 else
12664 {
12665 timer = find_timer((int)get_tv_number(&argvars[0]));
12666 if (timer != NULL)
12667 timer->tr_paused = paused;
12668 }
12669}
12670
12671/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012672 * "timer_start(time, callback [, options])" function
12673 */
12674 static void
12675f_timer_start(typval_T *argvars, typval_T *rettv)
12676{
Bram Moolenaar75537a92016-09-05 22:45:28 +020012677 long msec = (long)get_tv_number(&argvars[0]);
12678 timer_T *timer;
12679 int repeat = 0;
12680 char_u *callback;
12681 dict_T *dict;
12682 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012683
Bram Moolenaar75537a92016-09-05 22:45:28 +020012684 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012685 if (check_secure())
12686 return;
12687 if (argvars[2].v_type != VAR_UNKNOWN)
12688 {
12689 if (argvars[2].v_type != VAR_DICT
12690 || (dict = argvars[2].vval.v_dict) == NULL)
12691 {
12692 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
12693 return;
12694 }
12695 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
12696 repeat = get_dict_number(dict, (char_u *)"repeat");
12697 }
12698
Bram Moolenaar75537a92016-09-05 22:45:28 +020012699 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012700 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020012701 return;
12702
12703 timer = create_timer(msec, repeat);
12704 if (timer == NULL)
12705 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012706 else
12707 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020012708 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020012709 timer->tr_callback = vim_strsave(callback);
12710 else
12711 /* pointer into the partial */
12712 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020012713 timer->tr_partial = partial;
12714 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012715 }
12716}
12717
12718/*
12719 * "timer_stop(timer)" function
12720 */
12721 static void
12722f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
12723{
12724 timer_T *timer;
12725
12726 if (argvars[0].v_type != VAR_NUMBER)
12727 {
12728 EMSG(_(e_number_exp));
12729 return;
12730 }
12731 timer = find_timer((int)get_tv_number(&argvars[0]));
12732 if (timer != NULL)
12733 stop_timer(timer);
12734}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012735
12736/*
12737 * "timer_stopall()" function
12738 */
12739 static void
12740f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12741{
12742 stop_all_timers();
12743}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012744#endif
12745
12746/*
12747 * "tolower(string)" function
12748 */
12749 static void
12750f_tolower(typval_T *argvars, typval_T *rettv)
12751{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012752 rettv->v_type = VAR_STRING;
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +010012753 rettv->vval.v_string = strlow_save(get_tv_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012754}
12755
12756/*
12757 * "toupper(string)" function
12758 */
12759 static void
12760f_toupper(typval_T *argvars, typval_T *rettv)
12761{
12762 rettv->v_type = VAR_STRING;
12763 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
12764}
12765
12766/*
12767 * "tr(string, fromstr, tostr)" function
12768 */
12769 static void
12770f_tr(typval_T *argvars, typval_T *rettv)
12771{
12772 char_u *in_str;
12773 char_u *fromstr;
12774 char_u *tostr;
12775 char_u *p;
12776#ifdef FEAT_MBYTE
12777 int inlen;
12778 int fromlen;
12779 int tolen;
12780 int idx;
12781 char_u *cpstr;
12782 int cplen;
12783 int first = TRUE;
12784#endif
12785 char_u buf[NUMBUFLEN];
12786 char_u buf2[NUMBUFLEN];
12787 garray_T ga;
12788
12789 in_str = get_tv_string(&argvars[0]);
12790 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
12791 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
12792
12793 /* Default return value: empty string. */
12794 rettv->v_type = VAR_STRING;
12795 rettv->vval.v_string = NULL;
12796 if (fromstr == NULL || tostr == NULL)
12797 return; /* type error; errmsg already given */
12798 ga_init2(&ga, (int)sizeof(char), 80);
12799
12800#ifdef FEAT_MBYTE
12801 if (!has_mbyte)
12802#endif
12803 /* not multi-byte: fromstr and tostr must be the same length */
12804 if (STRLEN(fromstr) != STRLEN(tostr))
12805 {
12806#ifdef FEAT_MBYTE
12807error:
12808#endif
12809 EMSG2(_(e_invarg2), fromstr);
12810 ga_clear(&ga);
12811 return;
12812 }
12813
12814 /* fromstr and tostr have to contain the same number of chars */
12815 while (*in_str != NUL)
12816 {
12817#ifdef FEAT_MBYTE
12818 if (has_mbyte)
12819 {
12820 inlen = (*mb_ptr2len)(in_str);
12821 cpstr = in_str;
12822 cplen = inlen;
12823 idx = 0;
12824 for (p = fromstr; *p != NUL; p += fromlen)
12825 {
12826 fromlen = (*mb_ptr2len)(p);
12827 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
12828 {
12829 for (p = tostr; *p != NUL; p += tolen)
12830 {
12831 tolen = (*mb_ptr2len)(p);
12832 if (idx-- == 0)
12833 {
12834 cplen = tolen;
12835 cpstr = p;
12836 break;
12837 }
12838 }
12839 if (*p == NUL) /* tostr is shorter than fromstr */
12840 goto error;
12841 break;
12842 }
12843 ++idx;
12844 }
12845
12846 if (first && cpstr == in_str)
12847 {
12848 /* Check that fromstr and tostr have the same number of
12849 * (multi-byte) characters. Done only once when a character
12850 * of in_str doesn't appear in fromstr. */
12851 first = FALSE;
12852 for (p = tostr; *p != NUL; p += tolen)
12853 {
12854 tolen = (*mb_ptr2len)(p);
12855 --idx;
12856 }
12857 if (idx != 0)
12858 goto error;
12859 }
12860
12861 (void)ga_grow(&ga, cplen);
12862 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
12863 ga.ga_len += cplen;
12864
12865 in_str += inlen;
12866 }
12867 else
12868#endif
12869 {
12870 /* When not using multi-byte chars we can do it faster. */
12871 p = vim_strchr(fromstr, *in_str);
12872 if (p != NULL)
12873 ga_append(&ga, tostr[p - fromstr]);
12874 else
12875 ga_append(&ga, *in_str);
12876 ++in_str;
12877 }
12878 }
12879
12880 /* add a terminating NUL */
12881 (void)ga_grow(&ga, 1);
12882 ga_append(&ga, NUL);
12883
12884 rettv->vval.v_string = ga.ga_data;
12885}
12886
12887#ifdef FEAT_FLOAT
12888/*
12889 * "trunc({float})" function
12890 */
12891 static void
12892f_trunc(typval_T *argvars, typval_T *rettv)
12893{
12894 float_T f = 0.0;
12895
12896 rettv->v_type = VAR_FLOAT;
12897 if (get_float_arg(argvars, &f) == OK)
12898 /* trunc() is not in C90, use floor() or ceil() instead. */
12899 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
12900 else
12901 rettv->vval.v_float = 0.0;
12902}
12903#endif
12904
12905/*
12906 * "type(expr)" function
12907 */
12908 static void
12909f_type(typval_T *argvars, typval_T *rettv)
12910{
12911 int n = -1;
12912
12913 switch (argvars[0].v_type)
12914 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020012915 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
12916 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012917 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020012918 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
12919 case VAR_LIST: n = VAR_TYPE_LIST; break;
12920 case VAR_DICT: n = VAR_TYPE_DICT; break;
12921 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012922 case VAR_SPECIAL:
12923 if (argvars[0].vval.v_number == VVAL_FALSE
12924 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020012925 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012926 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020012927 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012928 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020012929 case VAR_JOB: n = VAR_TYPE_JOB; break;
12930 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012931 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010012932 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012933 n = -1;
12934 break;
12935 }
12936 rettv->vval.v_number = n;
12937}
12938
12939/*
12940 * "undofile(name)" function
12941 */
12942 static void
12943f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
12944{
12945 rettv->v_type = VAR_STRING;
12946#ifdef FEAT_PERSISTENT_UNDO
12947 {
12948 char_u *fname = get_tv_string(&argvars[0]);
12949
12950 if (*fname == NUL)
12951 {
12952 /* If there is no file name there will be no undo file. */
12953 rettv->vval.v_string = NULL;
12954 }
12955 else
12956 {
12957 char_u *ffname = FullName_save(fname, FALSE);
12958
12959 if (ffname != NULL)
12960 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
12961 vim_free(ffname);
12962 }
12963 }
12964#else
12965 rettv->vval.v_string = NULL;
12966#endif
12967}
12968
12969/*
12970 * "undotree()" function
12971 */
12972 static void
12973f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
12974{
12975 if (rettv_dict_alloc(rettv) == OK)
12976 {
12977 dict_T *dict = rettv->vval.v_dict;
12978 list_T *list;
12979
12980 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
12981 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
12982 dict_add_nr_str(dict, "save_last",
12983 (long)curbuf->b_u_save_nr_last, NULL);
12984 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
12985 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
12986 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
12987
12988 list = list_alloc();
12989 if (list != NULL)
12990 {
12991 u_eval_tree(curbuf->b_u_oldhead, list);
12992 dict_add_list(dict, "entries", list);
12993 }
12994 }
12995}
12996
12997/*
12998 * "values(dict)" function
12999 */
13000 static void
13001f_values(typval_T *argvars, typval_T *rettv)
13002{
13003 dict_list(argvars, rettv, 1);
13004}
13005
13006/*
13007 * "virtcol(string)" function
13008 */
13009 static void
13010f_virtcol(typval_T *argvars, typval_T *rettv)
13011{
13012 colnr_T vcol = 0;
13013 pos_T *fp;
13014 int fnum = curbuf->b_fnum;
13015
13016 fp = var2fpos(&argvars[0], FALSE, &fnum);
13017 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
13018 && fnum == curbuf->b_fnum)
13019 {
13020 getvvcol(curwin, fp, NULL, NULL, &vcol);
13021 ++vcol;
13022 }
13023
13024 rettv->vval.v_number = vcol;
13025}
13026
13027/*
13028 * "visualmode()" function
13029 */
13030 static void
13031f_visualmode(typval_T *argvars, typval_T *rettv)
13032{
13033 char_u str[2];
13034
13035 rettv->v_type = VAR_STRING;
13036 str[0] = curbuf->b_visual_mode_eval;
13037 str[1] = NUL;
13038 rettv->vval.v_string = vim_strsave(str);
13039
13040 /* A non-zero number or non-empty string argument: reset mode. */
13041 if (non_zero_arg(&argvars[0]))
13042 curbuf->b_visual_mode_eval = NUL;
13043}
13044
13045/*
13046 * "wildmenumode()" function
13047 */
13048 static void
13049f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13050{
13051#ifdef FEAT_WILDMENU
13052 if (wild_menu_showing)
13053 rettv->vval.v_number = 1;
13054#endif
13055}
13056
13057/*
13058 * "winbufnr(nr)" function
13059 */
13060 static void
13061f_winbufnr(typval_T *argvars, typval_T *rettv)
13062{
13063 win_T *wp;
13064
13065 wp = find_win_by_nr(&argvars[0], NULL);
13066 if (wp == NULL)
13067 rettv->vval.v_number = -1;
13068 else
13069 rettv->vval.v_number = wp->w_buffer->b_fnum;
13070}
13071
13072/*
13073 * "wincol()" function
13074 */
13075 static void
13076f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
13077{
13078 validate_cursor();
13079 rettv->vval.v_number = curwin->w_wcol + 1;
13080}
13081
13082/*
13083 * "winheight(nr)" function
13084 */
13085 static void
13086f_winheight(typval_T *argvars, typval_T *rettv)
13087{
13088 win_T *wp;
13089
13090 wp = find_win_by_nr(&argvars[0], NULL);
13091 if (wp == NULL)
13092 rettv->vval.v_number = -1;
13093 else
13094 rettv->vval.v_number = wp->w_height;
13095}
13096
13097/*
13098 * "winline()" function
13099 */
13100 static void
13101f_winline(typval_T *argvars UNUSED, typval_T *rettv)
13102{
13103 validate_cursor();
13104 rettv->vval.v_number = curwin->w_wrow + 1;
13105}
13106
13107/*
13108 * "winnr()" function
13109 */
13110 static void
13111f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
13112{
13113 int nr = 1;
13114
13115#ifdef FEAT_WINDOWS
13116 nr = get_winnr(curtab, &argvars[0]);
13117#endif
13118 rettv->vval.v_number = nr;
13119}
13120
13121/*
13122 * "winrestcmd()" function
13123 */
13124 static void
13125f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
13126{
13127#ifdef FEAT_WINDOWS
13128 win_T *wp;
13129 int winnr = 1;
13130 garray_T ga;
13131 char_u buf[50];
13132
13133 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020013134 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013135 {
13136 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
13137 ga_concat(&ga, buf);
13138 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
13139 ga_concat(&ga, buf);
13140 ++winnr;
13141 }
13142 ga_append(&ga, NUL);
13143
13144 rettv->vval.v_string = ga.ga_data;
13145#else
13146 rettv->vval.v_string = NULL;
13147#endif
13148 rettv->v_type = VAR_STRING;
13149}
13150
13151/*
13152 * "winrestview()" function
13153 */
13154 static void
13155f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
13156{
13157 dict_T *dict;
13158
13159 if (argvars[0].v_type != VAR_DICT
13160 || (dict = argvars[0].vval.v_dict) == NULL)
13161 EMSG(_(e_invarg));
13162 else
13163 {
13164 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
13165 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
13166 if (dict_find(dict, (char_u *)"col", -1) != NULL)
13167 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
13168#ifdef FEAT_VIRTUALEDIT
13169 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
13170 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
13171#endif
13172 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
13173 {
13174 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
13175 curwin->w_set_curswant = FALSE;
13176 }
13177
13178 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
13179 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
13180#ifdef FEAT_DIFF
13181 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
13182 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
13183#endif
13184 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
13185 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
13186 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
13187 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
13188
13189 check_cursor();
13190 win_new_height(curwin, curwin->w_height);
13191# ifdef FEAT_WINDOWS
13192 win_new_width(curwin, W_WIDTH(curwin));
13193# endif
13194 changed_window_setting();
13195
13196 if (curwin->w_topline <= 0)
13197 curwin->w_topline = 1;
13198 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
13199 curwin->w_topline = curbuf->b_ml.ml_line_count;
13200#ifdef FEAT_DIFF
13201 check_topfill(curwin, TRUE);
13202#endif
13203 }
13204}
13205
13206/*
13207 * "winsaveview()" function
13208 */
13209 static void
13210f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
13211{
13212 dict_T *dict;
13213
13214 if (rettv_dict_alloc(rettv) == FAIL)
13215 return;
13216 dict = rettv->vval.v_dict;
13217
13218 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
13219 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
13220#ifdef FEAT_VIRTUALEDIT
13221 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
13222#endif
13223 update_curswant();
13224 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
13225
13226 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
13227#ifdef FEAT_DIFF
13228 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
13229#endif
13230 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
13231 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
13232}
13233
13234/*
13235 * "winwidth(nr)" function
13236 */
13237 static void
13238f_winwidth(typval_T *argvars, typval_T *rettv)
13239{
13240 win_T *wp;
13241
13242 wp = find_win_by_nr(&argvars[0], NULL);
13243 if (wp == NULL)
13244 rettv->vval.v_number = -1;
13245 else
13246#ifdef FEAT_WINDOWS
13247 rettv->vval.v_number = wp->w_width;
13248#else
13249 rettv->vval.v_number = Columns;
13250#endif
13251}
13252
13253/*
13254 * "wordcount()" function
13255 */
13256 static void
13257f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
13258{
13259 if (rettv_dict_alloc(rettv) == FAIL)
13260 return;
13261 cursor_pos_info(rettv->vval.v_dict);
13262}
13263
13264/*
13265 * "writefile()" function
13266 */
13267 static void
13268f_writefile(typval_T *argvars, typval_T *rettv)
13269{
13270 int binary = FALSE;
13271 int append = FALSE;
13272 char_u *fname;
13273 FILE *fd;
13274 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013275 listitem_T *li;
13276 list_T *list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013277
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013278 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013279 if (check_restricted() || check_secure())
13280 return;
13281
13282 if (argvars[0].v_type != VAR_LIST)
13283 {
13284 EMSG2(_(e_listarg), "writefile()");
13285 return;
13286 }
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013287 list = argvars[0].vval.v_list;
13288 if (list == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013289 return;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013290 for (li = list->lv_first; li != NULL; li = li->li_next)
13291 if (get_tv_string_chk(&li->li_tv) == NULL)
13292 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013293
13294 if (argvars[2].v_type != VAR_UNKNOWN)
13295 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013296 char_u *arg2 = get_tv_string_chk(&argvars[2]);
13297
13298 if (arg2 == NULL)
13299 return;
13300 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013301 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013302 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013303 append = TRUE;
13304 }
13305
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013306 fname = get_tv_string_chk(&argvars[1]);
13307 if (fname == NULL)
13308 return;
13309
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013310 /* Always open the file in binary mode, library functions have a mind of
13311 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013312 if (*fname == NUL || (fd = mch_fopen((char *)fname,
13313 append ? APPENDBIN : WRITEBIN)) == NULL)
13314 {
13315 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
13316 ret = -1;
13317 }
13318 else
13319 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013320 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013321 ret = -1;
13322 fclose(fd);
13323 }
13324
13325 rettv->vval.v_number = ret;
13326}
13327
13328/*
13329 * "xor(expr, expr)" function
13330 */
13331 static void
13332f_xor(typval_T *argvars, typval_T *rettv)
13333{
13334 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
13335 ^ get_tv_number_chk(&argvars[1], NULL);
13336}
13337
13338
13339#endif /* FEAT_EVAL */