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