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