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