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