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