blob: da870c67bb49067960335cd8951cedd898cdb1b5 [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
Bram Moolenaard0573012017-10-28 21:11:06 +020027#ifdef MACOS_X
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020028# 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);
Bram Moolenaar669a8282017-11-19 20:13:05 +010064# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010065static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010066# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010067#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020068static void f_browse(typval_T *argvars, typval_T *rettv);
69static void f_browsedir(typval_T *argvars, typval_T *rettv);
70static void f_bufexists(typval_T *argvars, typval_T *rettv);
71static void f_buflisted(typval_T *argvars, typval_T *rettv);
72static void f_bufloaded(typval_T *argvars, typval_T *rettv);
73static void f_bufname(typval_T *argvars, typval_T *rettv);
74static void f_bufnr(typval_T *argvars, typval_T *rettv);
75static void f_bufwinid(typval_T *argvars, typval_T *rettv);
76static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
77static void f_byte2line(typval_T *argvars, typval_T *rettv);
78static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
79static void f_byteidx(typval_T *argvars, typval_T *rettv);
80static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
81static void f_call(typval_T *argvars, typval_T *rettv);
82#ifdef FEAT_FLOAT
83static void f_ceil(typval_T *argvars, typval_T *rettv);
84#endif
85#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +010086static void f_ch_canread(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020087static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0874a832016-09-01 15:11:51 +020088static void f_ch_close_in(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020089static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
90static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
91static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
92static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
93static void f_ch_info(typval_T *argvars, typval_T *rettv);
94static void f_ch_log(typval_T *argvars, typval_T *rettv);
95static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
96static void f_ch_open(typval_T *argvars, typval_T *rettv);
97static void f_ch_read(typval_T *argvars, typval_T *rettv);
98static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
99static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
100static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
101static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
102static void f_ch_status(typval_T *argvars, typval_T *rettv);
103#endif
104static void f_changenr(typval_T *argvars, typval_T *rettv);
105static void f_char2nr(typval_T *argvars, typval_T *rettv);
106static void f_cindent(typval_T *argvars, typval_T *rettv);
107static void f_clearmatches(typval_T *argvars, typval_T *rettv);
108static void f_col(typval_T *argvars, typval_T *rettv);
109#if defined(FEAT_INS_EXPAND)
110static void f_complete(typval_T *argvars, typval_T *rettv);
111static void f_complete_add(typval_T *argvars, typval_T *rettv);
112static void f_complete_check(typval_T *argvars, typval_T *rettv);
113#endif
114static void f_confirm(typval_T *argvars, typval_T *rettv);
115static void f_copy(typval_T *argvars, typval_T *rettv);
116#ifdef FEAT_FLOAT
117static void f_cos(typval_T *argvars, typval_T *rettv);
118static void f_cosh(typval_T *argvars, typval_T *rettv);
119#endif
120static void f_count(typval_T *argvars, typval_T *rettv);
121static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
122static void f_cursor(typval_T *argsvars, typval_T *rettv);
123static void f_deepcopy(typval_T *argvars, typval_T *rettv);
124static void f_delete(typval_T *argvars, typval_T *rettv);
125static void f_did_filetype(typval_T *argvars, typval_T *rettv);
126static void f_diff_filler(typval_T *argvars, typval_T *rettv);
127static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
128static void f_empty(typval_T *argvars, typval_T *rettv);
129static void f_escape(typval_T *argvars, typval_T *rettv);
130static void f_eval(typval_T *argvars, typval_T *rettv);
131static void f_eventhandler(typval_T *argvars, typval_T *rettv);
132static void f_executable(typval_T *argvars, typval_T *rettv);
133static void f_execute(typval_T *argvars, typval_T *rettv);
134static void f_exepath(typval_T *argvars, typval_T *rettv);
135static void f_exists(typval_T *argvars, typval_T *rettv);
136#ifdef FEAT_FLOAT
137static void f_exp(typval_T *argvars, typval_T *rettv);
138#endif
139static void f_expand(typval_T *argvars, typval_T *rettv);
140static void f_extend(typval_T *argvars, typval_T *rettv);
141static void f_feedkeys(typval_T *argvars, typval_T *rettv);
142static void f_filereadable(typval_T *argvars, typval_T *rettv);
143static void f_filewritable(typval_T *argvars, typval_T *rettv);
144static void f_filter(typval_T *argvars, typval_T *rettv);
145static void f_finddir(typval_T *argvars, typval_T *rettv);
146static void f_findfile(typval_T *argvars, typval_T *rettv);
147#ifdef FEAT_FLOAT
148static void f_float2nr(typval_T *argvars, typval_T *rettv);
149static void f_floor(typval_T *argvars, typval_T *rettv);
150static void f_fmod(typval_T *argvars, typval_T *rettv);
151#endif
152static void f_fnameescape(typval_T *argvars, typval_T *rettv);
153static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
154static void f_foldclosed(typval_T *argvars, typval_T *rettv);
155static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
156static void f_foldlevel(typval_T *argvars, typval_T *rettv);
157static void f_foldtext(typval_T *argvars, typval_T *rettv);
158static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
159static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200160static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200161static void f_function(typval_T *argvars, typval_T *rettv);
162static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
163static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200164static void f_getbufinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200165static void f_getbufline(typval_T *argvars, typval_T *rettv);
166static void f_getbufvar(typval_T *argvars, typval_T *rettv);
167static void f_getchar(typval_T *argvars, typval_T *rettv);
168static void f_getcharmod(typval_T *argvars, typval_T *rettv);
169static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
170static void f_getcmdline(typval_T *argvars, typval_T *rettv);
171#if defined(FEAT_CMDL_COMPL)
172static void f_getcompletion(typval_T *argvars, typval_T *rettv);
173#endif
174static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
175static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
176static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
177static void f_getcwd(typval_T *argvars, typval_T *rettv);
178static void f_getfontname(typval_T *argvars, typval_T *rettv);
179static void f_getfperm(typval_T *argvars, typval_T *rettv);
180static void f_getfsize(typval_T *argvars, typval_T *rettv);
181static void f_getftime(typval_T *argvars, typval_T *rettv);
182static void f_getftype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +0100183static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200184static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200185static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200186static void f_getmatches(typval_T *argvars, typval_T *rettv);
187static void f_getpid(typval_T *argvars, typval_T *rettv);
188static void f_getcurpos(typval_T *argvars, typval_T *rettv);
189static void f_getpos(typval_T *argvars, typval_T *rettv);
190static void f_getqflist(typval_T *argvars, typval_T *rettv);
191static void f_getreg(typval_T *argvars, typval_T *rettv);
192static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200193static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200194static void f_gettabvar(typval_T *argvars, typval_T *rettv);
195static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200196static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200197static void f_getwinposx(typval_T *argvars, typval_T *rettv);
198static void f_getwinposy(typval_T *argvars, typval_T *rettv);
199static void f_getwinvar(typval_T *argvars, typval_T *rettv);
200static void f_glob(typval_T *argvars, typval_T *rettv);
201static void f_globpath(typval_T *argvars, typval_T *rettv);
202static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
203static void f_has(typval_T *argvars, typval_T *rettv);
204static void f_has_key(typval_T *argvars, typval_T *rettv);
205static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
206static void f_hasmapto(typval_T *argvars, typval_T *rettv);
207static void f_histadd(typval_T *argvars, typval_T *rettv);
208static void f_histdel(typval_T *argvars, typval_T *rettv);
209static void f_histget(typval_T *argvars, typval_T *rettv);
210static void f_histnr(typval_T *argvars, typval_T *rettv);
211static void f_hlID(typval_T *argvars, typval_T *rettv);
212static void f_hlexists(typval_T *argvars, typval_T *rettv);
213static void f_hostname(typval_T *argvars, typval_T *rettv);
214static void f_iconv(typval_T *argvars, typval_T *rettv);
215static void f_indent(typval_T *argvars, typval_T *rettv);
216static void f_index(typval_T *argvars, typval_T *rettv);
217static void f_input(typval_T *argvars, typval_T *rettv);
218static void f_inputdialog(typval_T *argvars, typval_T *rettv);
219static void f_inputlist(typval_T *argvars, typval_T *rettv);
220static void f_inputrestore(typval_T *argvars, typval_T *rettv);
221static void f_inputsave(typval_T *argvars, typval_T *rettv);
222static void f_inputsecret(typval_T *argvars, typval_T *rettv);
223static void f_insert(typval_T *argvars, typval_T *rettv);
224static void f_invert(typval_T *argvars, typval_T *rettv);
225static void f_isdirectory(typval_T *argvars, typval_T *rettv);
226static void f_islocked(typval_T *argvars, typval_T *rettv);
227#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
228static void f_isnan(typval_T *argvars, typval_T *rettv);
229#endif
230static void f_items(typval_T *argvars, typval_T *rettv);
231#ifdef FEAT_JOB_CHANNEL
232static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
233static void f_job_info(typval_T *argvars, typval_T *rettv);
234static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
235static void f_job_start(typval_T *argvars, typval_T *rettv);
236static void f_job_stop(typval_T *argvars, typval_T *rettv);
237static void f_job_status(typval_T *argvars, typval_T *rettv);
238#endif
239static void f_join(typval_T *argvars, typval_T *rettv);
240static void f_js_decode(typval_T *argvars, typval_T *rettv);
241static void f_js_encode(typval_T *argvars, typval_T *rettv);
242static void f_json_decode(typval_T *argvars, typval_T *rettv);
243static void f_json_encode(typval_T *argvars, typval_T *rettv);
244static void f_keys(typval_T *argvars, typval_T *rettv);
245static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
246static void f_len(typval_T *argvars, typval_T *rettv);
247static void f_libcall(typval_T *argvars, typval_T *rettv);
248static void f_libcallnr(typval_T *argvars, typval_T *rettv);
249static void f_line(typval_T *argvars, typval_T *rettv);
250static void f_line2byte(typval_T *argvars, typval_T *rettv);
251static void f_lispindent(typval_T *argvars, typval_T *rettv);
252static void f_localtime(typval_T *argvars, typval_T *rettv);
253#ifdef FEAT_FLOAT
254static void f_log(typval_T *argvars, typval_T *rettv);
255static void f_log10(typval_T *argvars, typval_T *rettv);
256#endif
257#ifdef FEAT_LUA
258static void f_luaeval(typval_T *argvars, typval_T *rettv);
259#endif
260static void f_map(typval_T *argvars, typval_T *rettv);
261static void f_maparg(typval_T *argvars, typval_T *rettv);
262static void f_mapcheck(typval_T *argvars, typval_T *rettv);
263static void f_match(typval_T *argvars, typval_T *rettv);
264static void f_matchadd(typval_T *argvars, typval_T *rettv);
265static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
266static void f_matcharg(typval_T *argvars, typval_T *rettv);
267static void f_matchdelete(typval_T *argvars, typval_T *rettv);
268static void f_matchend(typval_T *argvars, typval_T *rettv);
269static void f_matchlist(typval_T *argvars, typval_T *rettv);
270static void f_matchstr(typval_T *argvars, typval_T *rettv);
271static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
272static void f_max(typval_T *argvars, typval_T *rettv);
273static void f_min(typval_T *argvars, typval_T *rettv);
274#ifdef vim_mkdir
275static void f_mkdir(typval_T *argvars, typval_T *rettv);
276#endif
277static void f_mode(typval_T *argvars, typval_T *rettv);
278#ifdef FEAT_MZSCHEME
279static void f_mzeval(typval_T *argvars, typval_T *rettv);
280#endif
281static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
282static void f_nr2char(typval_T *argvars, typval_T *rettv);
283static void f_or(typval_T *argvars, typval_T *rettv);
284static void f_pathshorten(typval_T *argvars, typval_T *rettv);
285#ifdef FEAT_PERL
286static void f_perleval(typval_T *argvars, typval_T *rettv);
287#endif
288#ifdef FEAT_FLOAT
289static void f_pow(typval_T *argvars, typval_T *rettv);
290#endif
291static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
292static void f_printf(typval_T *argvars, typval_T *rettv);
293static void f_pumvisible(typval_T *argvars, typval_T *rettv);
294#ifdef FEAT_PYTHON3
295static void f_py3eval(typval_T *argvars, typval_T *rettv);
296#endif
297#ifdef FEAT_PYTHON
298static void f_pyeval(typval_T *argvars, typval_T *rettv);
299#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100300#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
301static void f_pyxeval(typval_T *argvars, typval_T *rettv);
302#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200303static void f_range(typval_T *argvars, typval_T *rettv);
304static void f_readfile(typval_T *argvars, typval_T *rettv);
305static void f_reltime(typval_T *argvars, typval_T *rettv);
306#ifdef FEAT_FLOAT
307static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
308#endif
309static void f_reltimestr(typval_T *argvars, typval_T *rettv);
310static void f_remote_expr(typval_T *argvars, typval_T *rettv);
311static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
312static void f_remote_peek(typval_T *argvars, typval_T *rettv);
313static void f_remote_read(typval_T *argvars, typval_T *rettv);
314static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100315static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200316static void f_remove(typval_T *argvars, typval_T *rettv);
317static void f_rename(typval_T *argvars, typval_T *rettv);
318static void f_repeat(typval_T *argvars, typval_T *rettv);
319static void f_resolve(typval_T *argvars, typval_T *rettv);
320static void f_reverse(typval_T *argvars, typval_T *rettv);
321#ifdef FEAT_FLOAT
322static void f_round(typval_T *argvars, typval_T *rettv);
323#endif
324static void f_screenattr(typval_T *argvars, typval_T *rettv);
325static void f_screenchar(typval_T *argvars, typval_T *rettv);
326static void f_screencol(typval_T *argvars, typval_T *rettv);
327static void f_screenrow(typval_T *argvars, typval_T *rettv);
328static void f_search(typval_T *argvars, typval_T *rettv);
329static void f_searchdecl(typval_T *argvars, typval_T *rettv);
330static void f_searchpair(typval_T *argvars, typval_T *rettv);
331static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
332static void f_searchpos(typval_T *argvars, typval_T *rettv);
333static void f_server2client(typval_T *argvars, typval_T *rettv);
334static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200335static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200336static void f_setbufvar(typval_T *argvars, typval_T *rettv);
337static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
338static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
339static void f_setfperm(typval_T *argvars, typval_T *rettv);
340static void f_setline(typval_T *argvars, typval_T *rettv);
341static void f_setloclist(typval_T *argvars, typval_T *rettv);
342static void f_setmatches(typval_T *argvars, typval_T *rettv);
343static void f_setpos(typval_T *argvars, typval_T *rettv);
344static void f_setqflist(typval_T *argvars, typval_T *rettv);
345static void f_setreg(typval_T *argvars, typval_T *rettv);
346static void f_settabvar(typval_T *argvars, typval_T *rettv);
347static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
348static void f_setwinvar(typval_T *argvars, typval_T *rettv);
349#ifdef FEAT_CRYPT
350static void f_sha256(typval_T *argvars, typval_T *rettv);
351#endif /* FEAT_CRYPT */
352static void f_shellescape(typval_T *argvars, typval_T *rettv);
353static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
354static void f_simplify(typval_T *argvars, typval_T *rettv);
355#ifdef FEAT_FLOAT
356static void f_sin(typval_T *argvars, typval_T *rettv);
357static void f_sinh(typval_T *argvars, typval_T *rettv);
358#endif
359static void f_sort(typval_T *argvars, typval_T *rettv);
360static void f_soundfold(typval_T *argvars, typval_T *rettv);
361static void f_spellbadword(typval_T *argvars, typval_T *rettv);
362static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
363static void f_split(typval_T *argvars, typval_T *rettv);
364#ifdef FEAT_FLOAT
365static void f_sqrt(typval_T *argvars, typval_T *rettv);
366static void f_str2float(typval_T *argvars, typval_T *rettv);
367#endif
368static void f_str2nr(typval_T *argvars, typval_T *rettv);
369static void f_strchars(typval_T *argvars, typval_T *rettv);
370#ifdef HAVE_STRFTIME
371static void f_strftime(typval_T *argvars, typval_T *rettv);
372#endif
373static void f_strgetchar(typval_T *argvars, typval_T *rettv);
374static void f_stridx(typval_T *argvars, typval_T *rettv);
375static void f_string(typval_T *argvars, typval_T *rettv);
376static void f_strlen(typval_T *argvars, typval_T *rettv);
377static void f_strcharpart(typval_T *argvars, typval_T *rettv);
378static void f_strpart(typval_T *argvars, typval_T *rettv);
379static void f_strridx(typval_T *argvars, typval_T *rettv);
380static void f_strtrans(typval_T *argvars, typval_T *rettv);
381static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
382static void f_strwidth(typval_T *argvars, typval_T *rettv);
383static void f_submatch(typval_T *argvars, typval_T *rettv);
384static void f_substitute(typval_T *argvars, typval_T *rettv);
385static void f_synID(typval_T *argvars, typval_T *rettv);
386static void f_synIDattr(typval_T *argvars, typval_T *rettv);
387static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
388static void f_synstack(typval_T *argvars, typval_T *rettv);
389static void f_synconcealed(typval_T *argvars, typval_T *rettv);
390static void f_system(typval_T *argvars, typval_T *rettv);
391static void f_systemlist(typval_T *argvars, typval_T *rettv);
392static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
393static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
394static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
395static void f_taglist(typval_T *argvars, typval_T *rettv);
396static void f_tagfiles(typval_T *argvars, typval_T *rettv);
397static void f_tempname(typval_T *argvars, typval_T *rettv);
398static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
399static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200400static void f_test_feedinput(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100401static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200402static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100403static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200404#ifdef FEAT_JOB_CHANNEL
405static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
406#endif
407static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
408#ifdef FEAT_JOB_CHANNEL
409static void f_test_null_job(typval_T *argvars, typval_T *rettv);
410#endif
411static void f_test_null_list(typval_T *argvars, typval_T *rettv);
412static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
413static void f_test_null_string(typval_T *argvars, typval_T *rettv);
414static void f_test_settime(typval_T *argvars, typval_T *rettv);
415#ifdef FEAT_FLOAT
416static void f_tan(typval_T *argvars, typval_T *rettv);
417static void f_tanh(typval_T *argvars, typval_T *rettv);
418#endif
419#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200420static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200421static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200422static void f_timer_start(typval_T *argvars, typval_T *rettv);
423static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200424static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200425#endif
426static void f_tolower(typval_T *argvars, typval_T *rettv);
427static void f_toupper(typval_T *argvars, typval_T *rettv);
428static void f_tr(typval_T *argvars, typval_T *rettv);
429#ifdef FEAT_FLOAT
430static void f_trunc(typval_T *argvars, typval_T *rettv);
431#endif
432static void f_type(typval_T *argvars, typval_T *rettv);
433static void f_undofile(typval_T *argvars, typval_T *rettv);
434static void f_undotree(typval_T *argvars, typval_T *rettv);
435static void f_uniq(typval_T *argvars, typval_T *rettv);
436static void f_values(typval_T *argvars, typval_T *rettv);
437static void f_virtcol(typval_T *argvars, typval_T *rettv);
438static void f_visualmode(typval_T *argvars, typval_T *rettv);
439static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
440static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
441static void f_win_getid(typval_T *argvars, typval_T *rettv);
442static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
443static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
444static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100445static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200446static void f_winbufnr(typval_T *argvars, typval_T *rettv);
447static void f_wincol(typval_T *argvars, typval_T *rettv);
448static void f_winheight(typval_T *argvars, typval_T *rettv);
449static void f_winline(typval_T *argvars, typval_T *rettv);
450static void f_winnr(typval_T *argvars, typval_T *rettv);
451static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
452static void f_winrestview(typval_T *argvars, typval_T *rettv);
453static void f_winsaveview(typval_T *argvars, typval_T *rettv);
454static void f_winwidth(typval_T *argvars, typval_T *rettv);
455static void f_writefile(typval_T *argvars, typval_T *rettv);
456static void f_wordcount(typval_T *argvars, typval_T *rettv);
457static void f_xor(typval_T *argvars, typval_T *rettv);
458
459/*
460 * Array with names and number of arguments of all internal functions
461 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
462 */
463static struct fst
464{
465 char *f_name; /* function name */
466 char f_min_argc; /* minimal number of arguments */
467 char f_max_argc; /* maximal number of arguments */
468 void (*f_func)(typval_T *args, typval_T *rvar);
469 /* implementation of function */
470} functions[] =
471{
472#ifdef FEAT_FLOAT
473 {"abs", 1, 1, f_abs},
474 {"acos", 1, 1, f_acos}, /* WJMc */
475#endif
476 {"add", 2, 2, f_add},
477 {"and", 2, 2, f_and},
478 {"append", 2, 2, f_append},
479 {"argc", 0, 0, f_argc},
480 {"argidx", 0, 0, f_argidx},
481 {"arglistid", 0, 2, f_arglistid},
482 {"argv", 0, 1, f_argv},
483#ifdef FEAT_FLOAT
484 {"asin", 1, 1, f_asin}, /* WJMc */
485#endif
486 {"assert_equal", 2, 3, f_assert_equal},
487 {"assert_exception", 1, 2, f_assert_exception},
488 {"assert_fails", 1, 2, f_assert_fails},
489 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100490 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200491 {"assert_match", 2, 3, f_assert_match},
492 {"assert_notequal", 2, 3, f_assert_notequal},
493 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100494 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200495 {"assert_true", 1, 2, f_assert_true},
496#ifdef FEAT_FLOAT
497 {"atan", 1, 1, f_atan},
498 {"atan2", 2, 2, f_atan2},
499#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100500#ifdef FEAT_BEVAL
501 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100502# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100503 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100504# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100505#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200506 {"browse", 4, 4, f_browse},
507 {"browsedir", 2, 2, f_browsedir},
508 {"bufexists", 1, 1, f_bufexists},
509 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
510 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
511 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
512 {"buflisted", 1, 1, f_buflisted},
513 {"bufloaded", 1, 1, f_bufloaded},
514 {"bufname", 1, 1, f_bufname},
515 {"bufnr", 1, 2, f_bufnr},
516 {"bufwinid", 1, 1, f_bufwinid},
517 {"bufwinnr", 1, 1, f_bufwinnr},
518 {"byte2line", 1, 1, f_byte2line},
519 {"byteidx", 2, 2, f_byteidx},
520 {"byteidxcomp", 2, 2, f_byteidxcomp},
521 {"call", 2, 3, f_call},
522#ifdef FEAT_FLOAT
523 {"ceil", 1, 1, f_ceil},
524#endif
525#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100526 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200527 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200528 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200529 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
530 {"ch_evalraw", 2, 3, f_ch_evalraw},
531 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
532 {"ch_getjob", 1, 1, f_ch_getjob},
533 {"ch_info", 1, 1, f_ch_info},
534 {"ch_log", 1, 2, f_ch_log},
535 {"ch_logfile", 1, 2, f_ch_logfile},
536 {"ch_open", 1, 2, f_ch_open},
537 {"ch_read", 1, 2, f_ch_read},
538 {"ch_readraw", 1, 2, f_ch_readraw},
539 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
540 {"ch_sendraw", 2, 3, f_ch_sendraw},
541 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200542 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200543#endif
544 {"changenr", 0, 0, f_changenr},
545 {"char2nr", 1, 2, f_char2nr},
546 {"cindent", 1, 1, f_cindent},
547 {"clearmatches", 0, 0, f_clearmatches},
548 {"col", 1, 1, f_col},
549#if defined(FEAT_INS_EXPAND)
550 {"complete", 2, 2, f_complete},
551 {"complete_add", 1, 1, f_complete_add},
552 {"complete_check", 0, 0, f_complete_check},
553#endif
554 {"confirm", 1, 4, f_confirm},
555 {"copy", 1, 1, f_copy},
556#ifdef FEAT_FLOAT
557 {"cos", 1, 1, f_cos},
558 {"cosh", 1, 1, f_cosh},
559#endif
560 {"count", 2, 4, f_count},
561 {"cscope_connection",0,3, f_cscope_connection},
562 {"cursor", 1, 3, f_cursor},
563 {"deepcopy", 1, 2, f_deepcopy},
564 {"delete", 1, 2, f_delete},
565 {"did_filetype", 0, 0, f_did_filetype},
566 {"diff_filler", 1, 1, f_diff_filler},
567 {"diff_hlID", 2, 2, f_diff_hlID},
568 {"empty", 1, 1, f_empty},
569 {"escape", 2, 2, f_escape},
570 {"eval", 1, 1, f_eval},
571 {"eventhandler", 0, 0, f_eventhandler},
572 {"executable", 1, 1, f_executable},
573 {"execute", 1, 2, f_execute},
574 {"exepath", 1, 1, f_exepath},
575 {"exists", 1, 1, f_exists},
576#ifdef FEAT_FLOAT
577 {"exp", 1, 1, f_exp},
578#endif
579 {"expand", 1, 3, f_expand},
580 {"extend", 2, 3, f_extend},
581 {"feedkeys", 1, 2, f_feedkeys},
582 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
583 {"filereadable", 1, 1, f_filereadable},
584 {"filewritable", 1, 1, f_filewritable},
585 {"filter", 2, 2, f_filter},
586 {"finddir", 1, 3, f_finddir},
587 {"findfile", 1, 3, f_findfile},
588#ifdef FEAT_FLOAT
589 {"float2nr", 1, 1, f_float2nr},
590 {"floor", 1, 1, f_floor},
591 {"fmod", 2, 2, f_fmod},
592#endif
593 {"fnameescape", 1, 1, f_fnameescape},
594 {"fnamemodify", 2, 2, f_fnamemodify},
595 {"foldclosed", 1, 1, f_foldclosed},
596 {"foldclosedend", 1, 1, f_foldclosedend},
597 {"foldlevel", 1, 1, f_foldlevel},
598 {"foldtext", 0, 0, f_foldtext},
599 {"foldtextresult", 1, 1, f_foldtextresult},
600 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200601 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200602 {"function", 1, 3, f_function},
603 {"garbagecollect", 0, 1, f_garbagecollect},
604 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200605 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200606 {"getbufline", 2, 3, f_getbufline},
607 {"getbufvar", 2, 3, f_getbufvar},
608 {"getchar", 0, 1, f_getchar},
609 {"getcharmod", 0, 0, f_getcharmod},
610 {"getcharsearch", 0, 0, f_getcharsearch},
611 {"getcmdline", 0, 0, f_getcmdline},
612 {"getcmdpos", 0, 0, f_getcmdpos},
613 {"getcmdtype", 0, 0, f_getcmdtype},
614 {"getcmdwintype", 0, 0, f_getcmdwintype},
615#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200616 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200617#endif
618 {"getcurpos", 0, 0, f_getcurpos},
619 {"getcwd", 0, 2, f_getcwd},
620 {"getfontname", 0, 1, f_getfontname},
621 {"getfperm", 1, 1, f_getfperm},
622 {"getfsize", 1, 1, f_getfsize},
623 {"getftime", 1, 1, f_getftime},
624 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100625 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200626 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200627 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200628 {"getmatches", 0, 0, f_getmatches},
629 {"getpid", 0, 0, f_getpid},
630 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200631 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200632 {"getreg", 0, 3, f_getreg},
633 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200634 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200635 {"gettabvar", 2, 3, f_gettabvar},
636 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200637 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200638 {"getwinposx", 0, 0, f_getwinposx},
639 {"getwinposy", 0, 0, f_getwinposy},
640 {"getwinvar", 2, 3, f_getwinvar},
641 {"glob", 1, 4, f_glob},
642 {"glob2regpat", 1, 1, f_glob2regpat},
643 {"globpath", 2, 5, f_globpath},
644 {"has", 1, 1, f_has},
645 {"has_key", 2, 2, f_has_key},
646 {"haslocaldir", 0, 2, f_haslocaldir},
647 {"hasmapto", 1, 3, f_hasmapto},
648 {"highlightID", 1, 1, f_hlID}, /* obsolete */
649 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
650 {"histadd", 2, 2, f_histadd},
651 {"histdel", 1, 2, f_histdel},
652 {"histget", 1, 2, f_histget},
653 {"histnr", 1, 1, f_histnr},
654 {"hlID", 1, 1, f_hlID},
655 {"hlexists", 1, 1, f_hlexists},
656 {"hostname", 0, 0, f_hostname},
657 {"iconv", 3, 3, f_iconv},
658 {"indent", 1, 1, f_indent},
659 {"index", 2, 4, f_index},
660 {"input", 1, 3, f_input},
661 {"inputdialog", 1, 3, f_inputdialog},
662 {"inputlist", 1, 1, f_inputlist},
663 {"inputrestore", 0, 0, f_inputrestore},
664 {"inputsave", 0, 0, f_inputsave},
665 {"inputsecret", 1, 2, f_inputsecret},
666 {"insert", 2, 3, f_insert},
667 {"invert", 1, 1, f_invert},
668 {"isdirectory", 1, 1, f_isdirectory},
669 {"islocked", 1, 1, f_islocked},
670#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
671 {"isnan", 1, 1, f_isnan},
672#endif
673 {"items", 1, 1, f_items},
674#ifdef FEAT_JOB_CHANNEL
675 {"job_getchannel", 1, 1, f_job_getchannel},
676 {"job_info", 1, 1, f_job_info},
677 {"job_setoptions", 2, 2, f_job_setoptions},
678 {"job_start", 1, 2, f_job_start},
679 {"job_status", 1, 1, f_job_status},
680 {"job_stop", 1, 2, f_job_stop},
681#endif
682 {"join", 1, 2, f_join},
683 {"js_decode", 1, 1, f_js_decode},
684 {"js_encode", 1, 1, f_js_encode},
685 {"json_decode", 1, 1, f_json_decode},
686 {"json_encode", 1, 1, f_json_encode},
687 {"keys", 1, 1, f_keys},
688 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
689 {"len", 1, 1, f_len},
690 {"libcall", 3, 3, f_libcall},
691 {"libcallnr", 3, 3, f_libcallnr},
692 {"line", 1, 1, f_line},
693 {"line2byte", 1, 1, f_line2byte},
694 {"lispindent", 1, 1, f_lispindent},
695 {"localtime", 0, 0, f_localtime},
696#ifdef FEAT_FLOAT
697 {"log", 1, 1, f_log},
698 {"log10", 1, 1, f_log10},
699#endif
700#ifdef FEAT_LUA
701 {"luaeval", 1, 2, f_luaeval},
702#endif
703 {"map", 2, 2, f_map},
704 {"maparg", 1, 4, f_maparg},
705 {"mapcheck", 1, 3, f_mapcheck},
706 {"match", 2, 4, f_match},
707 {"matchadd", 2, 5, f_matchadd},
708 {"matchaddpos", 2, 5, f_matchaddpos},
709 {"matcharg", 1, 1, f_matcharg},
710 {"matchdelete", 1, 1, f_matchdelete},
711 {"matchend", 2, 4, f_matchend},
712 {"matchlist", 2, 4, f_matchlist},
713 {"matchstr", 2, 4, f_matchstr},
714 {"matchstrpos", 2, 4, f_matchstrpos},
715 {"max", 1, 1, f_max},
716 {"min", 1, 1, f_min},
717#ifdef vim_mkdir
718 {"mkdir", 1, 3, f_mkdir},
719#endif
720 {"mode", 0, 1, f_mode},
721#ifdef FEAT_MZSCHEME
722 {"mzeval", 1, 1, f_mzeval},
723#endif
724 {"nextnonblank", 1, 1, f_nextnonblank},
725 {"nr2char", 1, 2, f_nr2char},
726 {"or", 2, 2, f_or},
727 {"pathshorten", 1, 1, f_pathshorten},
728#ifdef FEAT_PERL
729 {"perleval", 1, 1, f_perleval},
730#endif
731#ifdef FEAT_FLOAT
732 {"pow", 2, 2, f_pow},
733#endif
734 {"prevnonblank", 1, 1, f_prevnonblank},
735 {"printf", 2, 19, f_printf},
736 {"pumvisible", 0, 0, f_pumvisible},
737#ifdef FEAT_PYTHON3
738 {"py3eval", 1, 1, f_py3eval},
739#endif
740#ifdef FEAT_PYTHON
741 {"pyeval", 1, 1, f_pyeval},
742#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100743#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
744 {"pyxeval", 1, 1, f_pyxeval},
745#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200746 {"range", 1, 3, f_range},
747 {"readfile", 1, 3, f_readfile},
748 {"reltime", 0, 2, f_reltime},
749#ifdef FEAT_FLOAT
750 {"reltimefloat", 1, 1, f_reltimefloat},
751#endif
752 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100753 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200754 {"remote_foreground", 1, 1, f_remote_foreground},
755 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100756 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200757 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100758 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200759 {"remove", 2, 3, f_remove},
760 {"rename", 2, 2, f_rename},
761 {"repeat", 2, 2, f_repeat},
762 {"resolve", 1, 1, f_resolve},
763 {"reverse", 1, 1, f_reverse},
764#ifdef FEAT_FLOAT
765 {"round", 1, 1, f_round},
766#endif
767 {"screenattr", 2, 2, f_screenattr},
768 {"screenchar", 2, 2, f_screenchar},
769 {"screencol", 0, 0, f_screencol},
770 {"screenrow", 0, 0, f_screenrow},
771 {"search", 1, 4, f_search},
772 {"searchdecl", 1, 3, f_searchdecl},
773 {"searchpair", 3, 7, f_searchpair},
774 {"searchpairpos", 3, 7, f_searchpairpos},
775 {"searchpos", 1, 4, f_searchpos},
776 {"server2client", 2, 2, f_server2client},
777 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200778 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200779 {"setbufvar", 3, 3, f_setbufvar},
780 {"setcharsearch", 1, 1, f_setcharsearch},
781 {"setcmdpos", 1, 1, f_setcmdpos},
782 {"setfperm", 2, 2, f_setfperm},
783 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200784 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200785 {"setmatches", 1, 1, f_setmatches},
786 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200787 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200788 {"setreg", 2, 3, f_setreg},
789 {"settabvar", 3, 3, f_settabvar},
790 {"settabwinvar", 4, 4, f_settabwinvar},
791 {"setwinvar", 3, 3, f_setwinvar},
792#ifdef FEAT_CRYPT
793 {"sha256", 1, 1, f_sha256},
794#endif
795 {"shellescape", 1, 2, f_shellescape},
796 {"shiftwidth", 0, 0, f_shiftwidth},
797 {"simplify", 1, 1, f_simplify},
798#ifdef FEAT_FLOAT
799 {"sin", 1, 1, f_sin},
800 {"sinh", 1, 1, f_sinh},
801#endif
802 {"sort", 1, 3, f_sort},
803 {"soundfold", 1, 1, f_soundfold},
804 {"spellbadword", 0, 1, f_spellbadword},
805 {"spellsuggest", 1, 3, f_spellsuggest},
806 {"split", 1, 3, f_split},
807#ifdef FEAT_FLOAT
808 {"sqrt", 1, 1, f_sqrt},
809 {"str2float", 1, 1, f_str2float},
810#endif
811 {"str2nr", 1, 2, f_str2nr},
812 {"strcharpart", 2, 3, f_strcharpart},
813 {"strchars", 1, 2, f_strchars},
814 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
815#ifdef HAVE_STRFTIME
816 {"strftime", 1, 2, f_strftime},
817#endif
818 {"strgetchar", 2, 2, f_strgetchar},
819 {"stridx", 2, 3, f_stridx},
820 {"string", 1, 1, f_string},
821 {"strlen", 1, 1, f_strlen},
822 {"strpart", 2, 3, f_strpart},
823 {"strridx", 2, 3, f_strridx},
824 {"strtrans", 1, 1, f_strtrans},
825 {"strwidth", 1, 1, f_strwidth},
826 {"submatch", 1, 2, f_submatch},
827 {"substitute", 4, 4, f_substitute},
828 {"synID", 3, 3, f_synID},
829 {"synIDattr", 2, 3, f_synIDattr},
830 {"synIDtrans", 1, 1, f_synIDtrans},
831 {"synconcealed", 2, 2, f_synconcealed},
832 {"synstack", 2, 2, f_synstack},
833 {"system", 1, 2, f_system},
834 {"systemlist", 1, 2, f_systemlist},
835 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
836 {"tabpagenr", 0, 1, f_tabpagenr},
837 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
838 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100839 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200840#ifdef FEAT_FLOAT
841 {"tan", 1, 1, f_tan},
842 {"tanh", 1, 1, f_tanh},
843#endif
844 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200845#ifdef FEAT_TERMINAL
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200846 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200847 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200848 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200849 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200850 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200851 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200852 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200853 {"term_getstatus", 1, 1, f_term_getstatus},
854 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200855 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200856 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200857 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200858 {"term_sendkeys", 2, 2, f_term_sendkeys},
859 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200860 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200861#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200862 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
863 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200864 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200865 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100866 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200867#ifdef FEAT_JOB_CHANNEL
868 {"test_null_channel", 0, 0, f_test_null_channel},
869#endif
870 {"test_null_dict", 0, 0, f_test_null_dict},
871#ifdef FEAT_JOB_CHANNEL
872 {"test_null_job", 0, 0, f_test_null_job},
873#endif
874 {"test_null_list", 0, 0, f_test_null_list},
875 {"test_null_partial", 0, 0, f_test_null_partial},
876 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100877 {"test_override", 2, 2, f_test_override},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200878 {"test_settime", 1, 1, f_test_settime},
879#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200880 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200881 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200882 {"timer_start", 2, 3, f_timer_start},
883 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200884 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200885#endif
886 {"tolower", 1, 1, f_tolower},
887 {"toupper", 1, 1, f_toupper},
888 {"tr", 3, 3, f_tr},
889#ifdef FEAT_FLOAT
890 {"trunc", 1, 1, f_trunc},
891#endif
892 {"type", 1, 1, f_type},
893 {"undofile", 1, 1, f_undofile},
894 {"undotree", 0, 0, f_undotree},
895 {"uniq", 1, 3, f_uniq},
896 {"values", 1, 1, f_values},
897 {"virtcol", 1, 1, f_virtcol},
898 {"visualmode", 0, 1, f_visualmode},
899 {"wildmenumode", 0, 0, f_wildmenumode},
900 {"win_findbuf", 1, 1, f_win_findbuf},
901 {"win_getid", 0, 2, f_win_getid},
902 {"win_gotoid", 1, 1, f_win_gotoid},
903 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
904 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100905 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200906 {"winbufnr", 1, 1, f_winbufnr},
907 {"wincol", 0, 0, f_wincol},
908 {"winheight", 1, 1, f_winheight},
909 {"winline", 0, 0, f_winline},
910 {"winnr", 0, 1, f_winnr},
911 {"winrestcmd", 0, 0, f_winrestcmd},
912 {"winrestview", 1, 1, f_winrestview},
913 {"winsaveview", 0, 0, f_winsaveview},
914 {"winwidth", 1, 1, f_winwidth},
915 {"wordcount", 0, 0, f_wordcount},
916 {"writefile", 2, 3, f_writefile},
917 {"xor", 2, 2, f_xor},
918};
919
920#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
921
922/*
923 * Function given to ExpandGeneric() to obtain the list of internal
924 * or user defined function names.
925 */
926 char_u *
927get_function_name(expand_T *xp, int idx)
928{
929 static int intidx = -1;
930 char_u *name;
931
932 if (idx == 0)
933 intidx = -1;
934 if (intidx < 0)
935 {
936 name = get_user_func_name(xp, idx);
937 if (name != NULL)
938 return name;
939 }
940 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
941 {
942 STRCPY(IObuff, functions[intidx].f_name);
943 STRCAT(IObuff, "(");
944 if (functions[intidx].f_max_argc == 0)
945 STRCAT(IObuff, ")");
946 return IObuff;
947 }
948
949 return NULL;
950}
951
952/*
953 * Function given to ExpandGeneric() to obtain the list of internal or
954 * user defined variable or function names.
955 */
956 char_u *
957get_expr_name(expand_T *xp, int idx)
958{
959 static int intidx = -1;
960 char_u *name;
961
962 if (idx == 0)
963 intidx = -1;
964 if (intidx < 0)
965 {
966 name = get_function_name(xp, idx);
967 if (name != NULL)
968 return name;
969 }
970 return get_user_var_name(xp, ++intidx);
971}
972
973#endif /* FEAT_CMDL_COMPL */
974
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200975/*
976 * Find internal function in table above.
977 * Return index, or -1 if not found
978 */
979 int
980find_internal_func(
981 char_u *name) /* name of the function */
982{
983 int first = 0;
984 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
985 int cmp;
986 int x;
987
988 /*
989 * Find the function name in the table. Binary search.
990 */
991 while (first <= last)
992 {
993 x = first + ((unsigned)(last - first) >> 1);
994 cmp = STRCMP(name, functions[x].f_name);
995 if (cmp < 0)
996 last = x - 1;
997 else if (cmp > 0)
998 first = x + 1;
999 else
1000 return x;
1001 }
1002 return -1;
1003}
1004
1005 int
1006call_internal_func(
1007 char_u *name,
1008 int argcount,
1009 typval_T *argvars,
1010 typval_T *rettv)
1011{
1012 int i;
1013
1014 i = find_internal_func(name);
1015 if (i < 0)
1016 return ERROR_UNKNOWN;
1017 if (argcount < functions[i].f_min_argc)
1018 return ERROR_TOOFEW;
1019 if (argcount > functions[i].f_max_argc)
1020 return ERROR_TOOMANY;
1021 argvars[argcount].v_type = VAR_UNKNOWN;
1022 functions[i].f_func(argvars, rettv);
1023 return ERROR_NONE;
1024}
1025
1026/*
1027 * Return TRUE for a non-zero Number and a non-empty String.
1028 */
1029 static int
1030non_zero_arg(typval_T *argvars)
1031{
1032 return ((argvars[0].v_type == VAR_NUMBER
1033 && argvars[0].vval.v_number != 0)
1034 || (argvars[0].v_type == VAR_SPECIAL
1035 && argvars[0].vval.v_number == VVAL_TRUE)
1036 || (argvars[0].v_type == VAR_STRING
1037 && argvars[0].vval.v_string != NULL
1038 && *argvars[0].vval.v_string != NUL));
1039}
1040
1041/*
1042 * Get the lnum from the first argument.
1043 * Also accepts ".", "$", etc., but that only works for the current buffer.
1044 * Returns -1 on error.
1045 */
1046 static linenr_T
1047get_tv_lnum(typval_T *argvars)
1048{
1049 typval_T rettv;
1050 linenr_T lnum;
1051
1052 lnum = (linenr_T)get_tv_number_chk(&argvars[0], NULL);
1053 if (lnum == 0) /* no valid number, try using line() */
1054 {
1055 rettv.v_type = VAR_NUMBER;
1056 f_line(argvars, &rettv);
1057 lnum = (linenr_T)rettv.vval.v_number;
1058 clear_tv(&rettv);
1059 }
1060 return lnum;
1061}
1062
1063#ifdef FEAT_FLOAT
1064static int get_float_arg(typval_T *argvars, float_T *f);
1065
1066/*
1067 * Get the float value of "argvars[0]" into "f".
1068 * Returns FAIL when the argument is not a Number or Float.
1069 */
1070 static int
1071get_float_arg(typval_T *argvars, float_T *f)
1072{
1073 if (argvars[0].v_type == VAR_FLOAT)
1074 {
1075 *f = argvars[0].vval.v_float;
1076 return OK;
1077 }
1078 if (argvars[0].v_type == VAR_NUMBER)
1079 {
1080 *f = (float_T)argvars[0].vval.v_number;
1081 return OK;
1082 }
1083 EMSG(_("E808: Number or Float required"));
1084 return FAIL;
1085}
1086
1087/*
1088 * "abs(expr)" function
1089 */
1090 static void
1091f_abs(typval_T *argvars, typval_T *rettv)
1092{
1093 if (argvars[0].v_type == VAR_FLOAT)
1094 {
1095 rettv->v_type = VAR_FLOAT;
1096 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1097 }
1098 else
1099 {
1100 varnumber_T n;
1101 int error = FALSE;
1102
1103 n = get_tv_number_chk(&argvars[0], &error);
1104 if (error)
1105 rettv->vval.v_number = -1;
1106 else if (n > 0)
1107 rettv->vval.v_number = n;
1108 else
1109 rettv->vval.v_number = -n;
1110 }
1111}
1112
1113/*
1114 * "acos()" function
1115 */
1116 static void
1117f_acos(typval_T *argvars, typval_T *rettv)
1118{
1119 float_T f = 0.0;
1120
1121 rettv->v_type = VAR_FLOAT;
1122 if (get_float_arg(argvars, &f) == OK)
1123 rettv->vval.v_float = acos(f);
1124 else
1125 rettv->vval.v_float = 0.0;
1126}
1127#endif
1128
1129/*
1130 * "add(list, item)" function
1131 */
1132 static void
1133f_add(typval_T *argvars, typval_T *rettv)
1134{
1135 list_T *l;
1136
1137 rettv->vval.v_number = 1; /* Default: Failed */
1138 if (argvars[0].v_type == VAR_LIST)
1139 {
1140 if ((l = argvars[0].vval.v_list) != NULL
1141 && !tv_check_lock(l->lv_lock,
1142 (char_u *)N_("add() argument"), TRUE)
1143 && list_append_tv(l, &argvars[1]) == OK)
1144 copy_tv(&argvars[0], rettv);
1145 }
1146 else
1147 EMSG(_(e_listreq));
1148}
1149
1150/*
1151 * "and(expr, expr)" function
1152 */
1153 static void
1154f_and(typval_T *argvars, typval_T *rettv)
1155{
1156 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
1157 & get_tv_number_chk(&argvars[1], NULL);
1158}
1159
1160/*
1161 * "append(lnum, string/list)" function
1162 */
1163 static void
1164f_append(typval_T *argvars, typval_T *rettv)
1165{
1166 long lnum;
1167 char_u *line;
1168 list_T *l = NULL;
1169 listitem_T *li = NULL;
1170 typval_T *tv;
1171 long added = 0;
1172
1173 /* When coming here from Insert mode, sync undo, so that this can be
1174 * undone separately from what was previously inserted. */
1175 if (u_sync_once == 2)
1176 {
1177 u_sync_once = 1; /* notify that u_sync() was called */
1178 u_sync(TRUE);
1179 }
1180
1181 lnum = get_tv_lnum(argvars);
1182 if (lnum >= 0
1183 && lnum <= curbuf->b_ml.ml_line_count
1184 && u_save(lnum, lnum + 1) == OK)
1185 {
1186 if (argvars[1].v_type == VAR_LIST)
1187 {
1188 l = argvars[1].vval.v_list;
1189 if (l == NULL)
1190 return;
1191 li = l->lv_first;
1192 }
1193 for (;;)
1194 {
1195 if (l == NULL)
1196 tv = &argvars[1]; /* append a string */
1197 else if (li == NULL)
1198 break; /* end of list */
1199 else
1200 tv = &li->li_tv; /* append item from list */
1201 line = get_tv_string_chk(tv);
1202 if (line == NULL) /* type error */
1203 {
1204 rettv->vval.v_number = 1; /* Failed */
1205 break;
1206 }
1207 ml_append(lnum + added, line, (colnr_T)0, FALSE);
1208 ++added;
1209 if (l == NULL)
1210 break;
1211 li = li->li_next;
1212 }
1213
1214 appended_lines_mark(lnum, added);
1215 if (curwin->w_cursor.lnum > lnum)
1216 curwin->w_cursor.lnum += added;
1217 }
1218 else
1219 rettv->vval.v_number = 1; /* Failed */
1220}
1221
1222/*
1223 * "argc()" function
1224 */
1225 static void
1226f_argc(typval_T *argvars UNUSED, typval_T *rettv)
1227{
1228 rettv->vval.v_number = ARGCOUNT;
1229}
1230
1231/*
1232 * "argidx()" function
1233 */
1234 static void
1235f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1236{
1237 rettv->vval.v_number = curwin->w_arg_idx;
1238}
1239
1240/*
1241 * "arglistid()" function
1242 */
1243 static void
1244f_arglistid(typval_T *argvars, typval_T *rettv)
1245{
1246 win_T *wp;
1247
1248 rettv->vval.v_number = -1;
1249 wp = find_tabwin(&argvars[0], &argvars[1]);
1250 if (wp != NULL)
1251 rettv->vval.v_number = wp->w_alist->id;
1252}
1253
1254/*
1255 * "argv(nr)" function
1256 */
1257 static void
1258f_argv(typval_T *argvars, typval_T *rettv)
1259{
1260 int idx;
1261
1262 if (argvars[0].v_type != VAR_UNKNOWN)
1263 {
1264 idx = (int)get_tv_number_chk(&argvars[0], NULL);
1265 if (idx >= 0 && idx < ARGCOUNT)
1266 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
1267 else
1268 rettv->vval.v_string = NULL;
1269 rettv->v_type = VAR_STRING;
1270 }
1271 else if (rettv_list_alloc(rettv) == OK)
1272 for (idx = 0; idx < ARGCOUNT; ++idx)
1273 list_append_string(rettv->vval.v_list,
1274 alist_name(&ARGLIST[idx]), -1);
1275}
1276
1277/*
1278 * "assert_equal(expected, actual[, msg])" function
1279 */
1280 static void
1281f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
1282{
1283 assert_equal_common(argvars, ASSERT_EQUAL);
1284}
1285
1286/*
1287 * "assert_notequal(expected, actual[, msg])" function
1288 */
1289 static void
1290f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED)
1291{
1292 assert_equal_common(argvars, ASSERT_NOTEQUAL);
1293}
1294
1295/*
1296 * "assert_exception(string[, msg])" function
1297 */
1298 static void
1299f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
1300{
1301 assert_exception(argvars);
1302}
1303
1304/*
1305 * "assert_fails(cmd [, error])" function
1306 */
1307 static void
1308f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
1309{
1310 assert_fails(argvars);
1311}
1312
1313/*
1314 * "assert_false(actual[, msg])" function
1315 */
1316 static void
1317f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
1318{
1319 assert_bool(argvars, FALSE);
1320}
1321
1322/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001323 * "assert_inrange(lower, upper[, msg])" function
1324 */
1325 static void
1326f_assert_inrange(typval_T *argvars, typval_T *rettv UNUSED)
1327{
1328 assert_inrange(argvars);
1329}
1330
1331/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001332 * "assert_match(pattern, actual[, msg])" function
1333 */
1334 static void
1335f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
1336{
1337 assert_match_common(argvars, ASSERT_MATCH);
1338}
1339
1340/*
1341 * "assert_notmatch(pattern, actual[, msg])" function
1342 */
1343 static void
1344f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED)
1345{
1346 assert_match_common(argvars, ASSERT_NOTMATCH);
1347}
1348
1349/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001350 * "assert_report(msg)" function
1351 */
1352 static void
1353f_assert_report(typval_T *argvars, typval_T *rettv UNUSED)
1354{
1355 assert_report(argvars);
1356}
1357
1358/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001359 * "assert_true(actual[, msg])" function
1360 */
1361 static void
1362f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
1363{
1364 assert_bool(argvars, TRUE);
1365}
1366
1367#ifdef FEAT_FLOAT
1368/*
1369 * "asin()" function
1370 */
1371 static void
1372f_asin(typval_T *argvars, typval_T *rettv)
1373{
1374 float_T f = 0.0;
1375
1376 rettv->v_type = VAR_FLOAT;
1377 if (get_float_arg(argvars, &f) == OK)
1378 rettv->vval.v_float = asin(f);
1379 else
1380 rettv->vval.v_float = 0.0;
1381}
1382
1383/*
1384 * "atan()" function
1385 */
1386 static void
1387f_atan(typval_T *argvars, typval_T *rettv)
1388{
1389 float_T f = 0.0;
1390
1391 rettv->v_type = VAR_FLOAT;
1392 if (get_float_arg(argvars, &f) == OK)
1393 rettv->vval.v_float = atan(f);
1394 else
1395 rettv->vval.v_float = 0.0;
1396}
1397
1398/*
1399 * "atan2()" function
1400 */
1401 static void
1402f_atan2(typval_T *argvars, typval_T *rettv)
1403{
1404 float_T fx = 0.0, fy = 0.0;
1405
1406 rettv->v_type = VAR_FLOAT;
1407 if (get_float_arg(argvars, &fx) == OK
1408 && get_float_arg(&argvars[1], &fy) == OK)
1409 rettv->vval.v_float = atan2(fx, fy);
1410 else
1411 rettv->vval.v_float = 0.0;
1412}
1413#endif
1414
1415/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001416 * "balloon_show()" function
1417 */
1418#ifdef FEAT_BEVAL
1419 static void
1420f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1421{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001422 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001423 {
1424 if (argvars[0].v_type == VAR_LIST
1425# ifdef FEAT_GUI
1426 && !gui.in_use
1427# endif
1428 )
1429 post_balloon(balloonEval, NULL, argvars[0].vval.v_list);
1430 else
1431 post_balloon(balloonEval, get_tv_string_chk(&argvars[0]), NULL);
1432 }
1433}
1434
Bram Moolenaar669a8282017-11-19 20:13:05 +01001435# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001436 static void
1437f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1438{
1439 if (rettv_list_alloc(rettv) == OK)
1440 {
1441 char_u *msg = get_tv_string_chk(&argvars[0]);
1442
1443 if (msg != NULL)
1444 {
1445 pumitem_T *array;
1446 int size = split_message(msg, &array);
1447 int i;
1448
1449 /* Skip the first and last item, they are always empty. */
1450 for (i = 1; i < size - 1; ++i)
1451 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001452 while (size > 0)
1453 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001454 vim_free(array);
1455 }
1456 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001457}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001458# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001459#endif
1460
1461/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001462 * "browse(save, title, initdir, default)" function
1463 */
1464 static void
1465f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1466{
1467#ifdef FEAT_BROWSE
1468 int save;
1469 char_u *title;
1470 char_u *initdir;
1471 char_u *defname;
1472 char_u buf[NUMBUFLEN];
1473 char_u buf2[NUMBUFLEN];
1474 int error = FALSE;
1475
1476 save = (int)get_tv_number_chk(&argvars[0], &error);
1477 title = get_tv_string_chk(&argvars[1]);
1478 initdir = get_tv_string_buf_chk(&argvars[2], buf);
1479 defname = get_tv_string_buf_chk(&argvars[3], buf2);
1480
1481 if (error || title == NULL || initdir == NULL || defname == NULL)
1482 rettv->vval.v_string = NULL;
1483 else
1484 rettv->vval.v_string =
1485 do_browse(save ? BROWSE_SAVE : 0,
1486 title, defname, NULL, initdir, NULL, curbuf);
1487#else
1488 rettv->vval.v_string = NULL;
1489#endif
1490 rettv->v_type = VAR_STRING;
1491}
1492
1493/*
1494 * "browsedir(title, initdir)" function
1495 */
1496 static void
1497f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1498{
1499#ifdef FEAT_BROWSE
1500 char_u *title;
1501 char_u *initdir;
1502 char_u buf[NUMBUFLEN];
1503
1504 title = get_tv_string_chk(&argvars[0]);
1505 initdir = get_tv_string_buf_chk(&argvars[1], buf);
1506
1507 if (title == NULL || initdir == NULL)
1508 rettv->vval.v_string = NULL;
1509 else
1510 rettv->vval.v_string = do_browse(BROWSE_DIR,
1511 title, NULL, NULL, initdir, NULL, curbuf);
1512#else
1513 rettv->vval.v_string = NULL;
1514#endif
1515 rettv->v_type = VAR_STRING;
1516}
1517
1518static buf_T *find_buffer(typval_T *avar);
1519
1520/*
1521 * Find a buffer by number or exact name.
1522 */
1523 static buf_T *
1524find_buffer(typval_T *avar)
1525{
1526 buf_T *buf = NULL;
1527
1528 if (avar->v_type == VAR_NUMBER)
1529 buf = buflist_findnr((int)avar->vval.v_number);
1530 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1531 {
1532 buf = buflist_findname_exp(avar->vval.v_string);
1533 if (buf == NULL)
1534 {
1535 /* No full path name match, try a match with a URL or a "nofile"
1536 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001537 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001538 if (buf->b_fname != NULL
1539 && (path_with_url(buf->b_fname)
1540#ifdef FEAT_QUICKFIX
1541 || bt_nofile(buf)
1542#endif
1543 )
1544 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1545 break;
1546 }
1547 }
1548 return buf;
1549}
1550
1551/*
1552 * "bufexists(expr)" function
1553 */
1554 static void
1555f_bufexists(typval_T *argvars, typval_T *rettv)
1556{
1557 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1558}
1559
1560/*
1561 * "buflisted(expr)" function
1562 */
1563 static void
1564f_buflisted(typval_T *argvars, typval_T *rettv)
1565{
1566 buf_T *buf;
1567
1568 buf = find_buffer(&argvars[0]);
1569 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1570}
1571
1572/*
1573 * "bufloaded(expr)" function
1574 */
1575 static void
1576f_bufloaded(typval_T *argvars, typval_T *rettv)
1577{
1578 buf_T *buf;
1579
1580 buf = find_buffer(&argvars[0]);
1581 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1582}
1583
1584 buf_T *
1585buflist_find_by_name(char_u *name, int curtab_only)
1586{
1587 int save_magic;
1588 char_u *save_cpo;
1589 buf_T *buf;
1590
1591 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1592 save_magic = p_magic;
1593 p_magic = TRUE;
1594 save_cpo = p_cpo;
1595 p_cpo = (char_u *)"";
1596
1597 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1598 TRUE, FALSE, curtab_only));
1599
1600 p_magic = save_magic;
1601 p_cpo = save_cpo;
1602 return buf;
1603}
1604
1605/*
1606 * Get buffer by number or pattern.
1607 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001608 buf_T *
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001609get_buf_tv(typval_T *tv, int curtab_only)
1610{
1611 char_u *name = tv->vval.v_string;
1612 buf_T *buf;
1613
1614 if (tv->v_type == VAR_NUMBER)
1615 return buflist_findnr((int)tv->vval.v_number);
1616 if (tv->v_type != VAR_STRING)
1617 return NULL;
1618 if (name == NULL || *name == NUL)
1619 return curbuf;
1620 if (name[0] == '$' && name[1] == NUL)
1621 return lastbuf;
1622
1623 buf = buflist_find_by_name(name, curtab_only);
1624
1625 /* If not found, try expanding the name, like done for bufexists(). */
1626 if (buf == NULL)
1627 buf = find_buffer(tv);
1628
1629 return buf;
1630}
1631
1632/*
1633 * "bufname(expr)" function
1634 */
1635 static void
1636f_bufname(typval_T *argvars, typval_T *rettv)
1637{
1638 buf_T *buf;
1639
1640 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1641 ++emsg_off;
1642 buf = get_buf_tv(&argvars[0], FALSE);
1643 rettv->v_type = VAR_STRING;
1644 if (buf != NULL && buf->b_fname != NULL)
1645 rettv->vval.v_string = vim_strsave(buf->b_fname);
1646 else
1647 rettv->vval.v_string = NULL;
1648 --emsg_off;
1649}
1650
1651/*
1652 * "bufnr(expr)" function
1653 */
1654 static void
1655f_bufnr(typval_T *argvars, typval_T *rettv)
1656{
1657 buf_T *buf;
1658 int error = FALSE;
1659 char_u *name;
1660
1661 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1662 ++emsg_off;
1663 buf = get_buf_tv(&argvars[0], FALSE);
1664 --emsg_off;
1665
1666 /* If the buffer isn't found and the second argument is not zero create a
1667 * new buffer. */
1668 if (buf == NULL
1669 && argvars[1].v_type != VAR_UNKNOWN
1670 && get_tv_number_chk(&argvars[1], &error) != 0
1671 && !error
1672 && (name = get_tv_string_chk(&argvars[0])) != NULL
1673 && !error)
1674 buf = buflist_new(name, NULL, (linenr_T)1, 0);
1675
1676 if (buf != NULL)
1677 rettv->vval.v_number = buf->b_fnum;
1678 else
1679 rettv->vval.v_number = -1;
1680}
1681
1682 static void
1683buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
1684{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001685 win_T *wp;
1686 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001687 buf_T *buf;
1688
1689 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1690 ++emsg_off;
1691 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02001692 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001693 {
1694 ++winnr;
1695 if (wp->w_buffer == buf)
1696 break;
1697 }
1698 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001699 --emsg_off;
1700}
1701
1702/*
1703 * "bufwinid(nr)" function
1704 */
1705 static void
1706f_bufwinid(typval_T *argvars, typval_T *rettv)
1707{
1708 buf_win_common(argvars, rettv, FALSE);
1709}
1710
1711/*
1712 * "bufwinnr(nr)" function
1713 */
1714 static void
1715f_bufwinnr(typval_T *argvars, typval_T *rettv)
1716{
1717 buf_win_common(argvars, rettv, TRUE);
1718}
1719
1720/*
1721 * "byte2line(byte)" function
1722 */
1723 static void
1724f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1725{
1726#ifndef FEAT_BYTEOFF
1727 rettv->vval.v_number = -1;
1728#else
1729 long boff = 0;
1730
1731 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
1732 if (boff < 0)
1733 rettv->vval.v_number = -1;
1734 else
1735 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
1736 (linenr_T)0, &boff);
1737#endif
1738}
1739
1740 static void
1741byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
1742{
1743#ifdef FEAT_MBYTE
1744 char_u *t;
1745#endif
1746 char_u *str;
1747 varnumber_T idx;
1748
1749 str = get_tv_string_chk(&argvars[0]);
1750 idx = get_tv_number_chk(&argvars[1], NULL);
1751 rettv->vval.v_number = -1;
1752 if (str == NULL || idx < 0)
1753 return;
1754
1755#ifdef FEAT_MBYTE
1756 t = str;
1757 for ( ; idx > 0; idx--)
1758 {
1759 if (*t == NUL) /* EOL reached */
1760 return;
1761 if (enc_utf8 && comp)
1762 t += utf_ptr2len(t);
1763 else
1764 t += (*mb_ptr2len)(t);
1765 }
1766 rettv->vval.v_number = (varnumber_T)(t - str);
1767#else
1768 if ((size_t)idx <= STRLEN(str))
1769 rettv->vval.v_number = idx;
1770#endif
1771}
1772
1773/*
1774 * "byteidx()" function
1775 */
1776 static void
1777f_byteidx(typval_T *argvars, typval_T *rettv)
1778{
1779 byteidx(argvars, rettv, FALSE);
1780}
1781
1782/*
1783 * "byteidxcomp()" function
1784 */
1785 static void
1786f_byteidxcomp(typval_T *argvars, typval_T *rettv)
1787{
1788 byteidx(argvars, rettv, TRUE);
1789}
1790
1791/*
1792 * "call(func, arglist [, dict])" function
1793 */
1794 static void
1795f_call(typval_T *argvars, typval_T *rettv)
1796{
1797 char_u *func;
1798 partial_T *partial = NULL;
1799 dict_T *selfdict = NULL;
1800
1801 if (argvars[1].v_type != VAR_LIST)
1802 {
1803 EMSG(_(e_listreq));
1804 return;
1805 }
1806 if (argvars[1].vval.v_list == NULL)
1807 return;
1808
1809 if (argvars[0].v_type == VAR_FUNC)
1810 func = argvars[0].vval.v_string;
1811 else if (argvars[0].v_type == VAR_PARTIAL)
1812 {
1813 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001814 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001815 }
1816 else
1817 func = get_tv_string(&argvars[0]);
1818 if (*func == NUL)
1819 return; /* type error or empty name */
1820
1821 if (argvars[2].v_type != VAR_UNKNOWN)
1822 {
1823 if (argvars[2].v_type != VAR_DICT)
1824 {
1825 EMSG(_(e_dictreq));
1826 return;
1827 }
1828 selfdict = argvars[2].vval.v_dict;
1829 }
1830
1831 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
1832}
1833
1834#ifdef FEAT_FLOAT
1835/*
1836 * "ceil({float})" function
1837 */
1838 static void
1839f_ceil(typval_T *argvars, typval_T *rettv)
1840{
1841 float_T f = 0.0;
1842
1843 rettv->v_type = VAR_FLOAT;
1844 if (get_float_arg(argvars, &f) == OK)
1845 rettv->vval.v_float = ceil(f);
1846 else
1847 rettv->vval.v_float = 0.0;
1848}
1849#endif
1850
1851#ifdef FEAT_JOB_CHANNEL
1852/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01001853 * "ch_canread()" function
1854 */
1855 static void
1856f_ch_canread(typval_T *argvars, typval_T *rettv)
1857{
Bram Moolenaar958dc692016-12-01 15:34:12 +01001858 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01001859
1860 rettv->vval.v_number = 0;
1861 if (channel != NULL)
1862 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
1863 || channel_has_readahead(channel, PART_OUT)
1864 || channel_has_readahead(channel, PART_ERR);
1865}
1866
1867/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001868 * "ch_close()" function
1869 */
1870 static void
1871f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
1872{
1873 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
1874
1875 if (channel != NULL)
1876 {
1877 channel_close(channel, FALSE);
1878 channel_clear(channel);
1879 }
1880}
1881
1882/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02001883 * "ch_close()" function
1884 */
1885 static void
1886f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
1887{
1888 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
1889
1890 if (channel != NULL)
1891 channel_close_in(channel);
1892}
1893
1894/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001895 * "ch_getbufnr()" function
1896 */
1897 static void
1898f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
1899{
1900 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1901
1902 rettv->vval.v_number = -1;
1903 if (channel != NULL)
1904 {
1905 char_u *what = get_tv_string(&argvars[1]);
1906 int part;
1907
1908 if (STRCMP(what, "err") == 0)
1909 part = PART_ERR;
1910 else if (STRCMP(what, "out") == 0)
1911 part = PART_OUT;
1912 else if (STRCMP(what, "in") == 0)
1913 part = PART_IN;
1914 else
1915 part = PART_SOCK;
1916 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
1917 rettv->vval.v_number =
1918 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
1919 }
1920}
1921
1922/*
1923 * "ch_getjob()" function
1924 */
1925 static void
1926f_ch_getjob(typval_T *argvars, typval_T *rettv)
1927{
1928 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1929
1930 if (channel != NULL)
1931 {
1932 rettv->v_type = VAR_JOB;
1933 rettv->vval.v_job = channel->ch_job;
1934 if (channel->ch_job != NULL)
1935 ++channel->ch_job->jv_refcount;
1936 }
1937}
1938
1939/*
1940 * "ch_info()" function
1941 */
1942 static void
1943f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
1944{
1945 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1946
1947 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
1948 channel_info(channel, rettv->vval.v_dict);
1949}
1950
1951/*
1952 * "ch_log()" function
1953 */
1954 static void
1955f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
1956{
1957 char_u *msg = get_tv_string(&argvars[0]);
1958 channel_T *channel = NULL;
1959
1960 if (argvars[1].v_type != VAR_UNKNOWN)
1961 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
1962
1963 ch_log(channel, (char *)msg);
1964}
1965
1966/*
1967 * "ch_logfile()" function
1968 */
1969 static void
1970f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
1971{
1972 char_u *fname;
1973 char_u *opt = (char_u *)"";
1974 char_u buf[NUMBUFLEN];
1975
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02001976 /* Don't open a file in restricted mode. */
1977 if (check_restricted() || check_secure())
1978 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001979 fname = get_tv_string(&argvars[0]);
1980 if (argvars[1].v_type == VAR_STRING)
1981 opt = get_tv_string_buf(&argvars[1], buf);
1982 ch_logfile(fname, opt);
1983}
1984
1985/*
1986 * "ch_open()" function
1987 */
1988 static void
1989f_ch_open(typval_T *argvars, typval_T *rettv)
1990{
1991 rettv->v_type = VAR_CHANNEL;
1992 if (check_restricted() || check_secure())
1993 return;
1994 rettv->vval.v_channel = channel_open_func(argvars);
1995}
1996
1997/*
1998 * "ch_read()" function
1999 */
2000 static void
2001f_ch_read(typval_T *argvars, typval_T *rettv)
2002{
2003 common_channel_read(argvars, rettv, FALSE);
2004}
2005
2006/*
2007 * "ch_readraw()" function
2008 */
2009 static void
2010f_ch_readraw(typval_T *argvars, typval_T *rettv)
2011{
2012 common_channel_read(argvars, rettv, TRUE);
2013}
2014
2015/*
2016 * "ch_evalexpr()" function
2017 */
2018 static void
2019f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2020{
2021 ch_expr_common(argvars, rettv, TRUE);
2022}
2023
2024/*
2025 * "ch_sendexpr()" function
2026 */
2027 static void
2028f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2029{
2030 ch_expr_common(argvars, rettv, FALSE);
2031}
2032
2033/*
2034 * "ch_evalraw()" function
2035 */
2036 static void
2037f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2038{
2039 ch_raw_common(argvars, rettv, TRUE);
2040}
2041
2042/*
2043 * "ch_sendraw()" function
2044 */
2045 static void
2046f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2047{
2048 ch_raw_common(argvars, rettv, FALSE);
2049}
2050
2051/*
2052 * "ch_setoptions()" function
2053 */
2054 static void
2055f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2056{
2057 channel_T *channel;
2058 jobopt_T opt;
2059
2060 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2061 if (channel == NULL)
2062 return;
2063 clear_job_options(&opt);
2064 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002065 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002066 channel_set_options(channel, &opt);
2067 free_job_options(&opt);
2068}
2069
2070/*
2071 * "ch_status()" function
2072 */
2073 static void
2074f_ch_status(typval_T *argvars, typval_T *rettv)
2075{
2076 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002077 jobopt_T opt;
2078 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002079
2080 /* return an empty string by default */
2081 rettv->v_type = VAR_STRING;
2082 rettv->vval.v_string = NULL;
2083
2084 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002085
2086 if (argvars[1].v_type != VAR_UNKNOWN)
2087 {
2088 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002089 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002090 && (opt.jo_set & JO_PART))
2091 part = opt.jo_part;
2092 }
2093
2094 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002095}
2096#endif
2097
2098/*
2099 * "changenr()" function
2100 */
2101 static void
2102f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2103{
2104 rettv->vval.v_number = curbuf->b_u_seq_cur;
2105}
2106
2107/*
2108 * "char2nr(string)" function
2109 */
2110 static void
2111f_char2nr(typval_T *argvars, typval_T *rettv)
2112{
2113#ifdef FEAT_MBYTE
2114 if (has_mbyte)
2115 {
2116 int utf8 = 0;
2117
2118 if (argvars[1].v_type != VAR_UNKNOWN)
2119 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
2120
2121 if (utf8)
2122 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
2123 else
2124 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
2125 }
2126 else
2127#endif
2128 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
2129}
2130
2131/*
2132 * "cindent(lnum)" function
2133 */
2134 static void
2135f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2136{
2137#ifdef FEAT_CINDENT
2138 pos_T pos;
2139 linenr_T lnum;
2140
2141 pos = curwin->w_cursor;
2142 lnum = get_tv_lnum(argvars);
2143 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2144 {
2145 curwin->w_cursor.lnum = lnum;
2146 rettv->vval.v_number = get_c_indent();
2147 curwin->w_cursor = pos;
2148 }
2149 else
2150#endif
2151 rettv->vval.v_number = -1;
2152}
2153
2154/*
2155 * "clearmatches()" function
2156 */
2157 static void
2158f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2159{
2160#ifdef FEAT_SEARCH_EXTRA
2161 clear_matches(curwin);
2162#endif
2163}
2164
2165/*
2166 * "col(string)" function
2167 */
2168 static void
2169f_col(typval_T *argvars, typval_T *rettv)
2170{
2171 colnr_T col = 0;
2172 pos_T *fp;
2173 int fnum = curbuf->b_fnum;
2174
2175 fp = var2fpos(&argvars[0], FALSE, &fnum);
2176 if (fp != NULL && fnum == curbuf->b_fnum)
2177 {
2178 if (fp->col == MAXCOL)
2179 {
2180 /* '> can be MAXCOL, get the length of the line then */
2181 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2182 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2183 else
2184 col = MAXCOL;
2185 }
2186 else
2187 {
2188 col = fp->col + 1;
2189#ifdef FEAT_VIRTUALEDIT
2190 /* col(".") when the cursor is on the NUL at the end of the line
2191 * because of "coladd" can be seen as an extra column. */
2192 if (virtual_active() && fp == &curwin->w_cursor)
2193 {
2194 char_u *p = ml_get_cursor();
2195
2196 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2197 curwin->w_virtcol - curwin->w_cursor.coladd))
2198 {
2199# ifdef FEAT_MBYTE
2200 int l;
2201
2202 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2203 col += l;
2204# else
2205 if (*p != NUL && p[1] == NUL)
2206 ++col;
2207# endif
2208 }
2209 }
2210#endif
2211 }
2212 }
2213 rettv->vval.v_number = col;
2214}
2215
2216#if defined(FEAT_INS_EXPAND)
2217/*
2218 * "complete()" function
2219 */
2220 static void
2221f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2222{
2223 int startcol;
2224
2225 if ((State & INSERT) == 0)
2226 {
2227 EMSG(_("E785: complete() can only be used in Insert mode"));
2228 return;
2229 }
2230
2231 /* Check for undo allowed here, because if something was already inserted
2232 * the line was already saved for undo and this check isn't done. */
2233 if (!undo_allowed())
2234 return;
2235
2236 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2237 {
2238 EMSG(_(e_invarg));
2239 return;
2240 }
2241
2242 startcol = (int)get_tv_number_chk(&argvars[0], NULL);
2243 if (startcol <= 0)
2244 return;
2245
2246 set_completion(startcol - 1, argvars[1].vval.v_list);
2247}
2248
2249/*
2250 * "complete_add()" function
2251 */
2252 static void
2253f_complete_add(typval_T *argvars, typval_T *rettv)
2254{
2255 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2256}
2257
2258/*
2259 * "complete_check()" function
2260 */
2261 static void
2262f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2263{
2264 int saved = RedrawingDisabled;
2265
2266 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002267 ins_compl_check_keys(0, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002268 rettv->vval.v_number = compl_interrupted;
2269 RedrawingDisabled = saved;
2270}
2271#endif
2272
2273/*
2274 * "confirm(message, buttons[, default [, type]])" function
2275 */
2276 static void
2277f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2278{
2279#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2280 char_u *message;
2281 char_u *buttons = NULL;
2282 char_u buf[NUMBUFLEN];
2283 char_u buf2[NUMBUFLEN];
2284 int def = 1;
2285 int type = VIM_GENERIC;
2286 char_u *typestr;
2287 int error = FALSE;
2288
2289 message = get_tv_string_chk(&argvars[0]);
2290 if (message == NULL)
2291 error = TRUE;
2292 if (argvars[1].v_type != VAR_UNKNOWN)
2293 {
2294 buttons = get_tv_string_buf_chk(&argvars[1], buf);
2295 if (buttons == NULL)
2296 error = TRUE;
2297 if (argvars[2].v_type != VAR_UNKNOWN)
2298 {
2299 def = (int)get_tv_number_chk(&argvars[2], &error);
2300 if (argvars[3].v_type != VAR_UNKNOWN)
2301 {
2302 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
2303 if (typestr == NULL)
2304 error = TRUE;
2305 else
2306 {
2307 switch (TOUPPER_ASC(*typestr))
2308 {
2309 case 'E': type = VIM_ERROR; break;
2310 case 'Q': type = VIM_QUESTION; break;
2311 case 'I': type = VIM_INFO; break;
2312 case 'W': type = VIM_WARNING; break;
2313 case 'G': type = VIM_GENERIC; break;
2314 }
2315 }
2316 }
2317 }
2318 }
2319
2320 if (buttons == NULL || *buttons == NUL)
2321 buttons = (char_u *)_("&Ok");
2322
2323 if (!error)
2324 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2325 def, NULL, FALSE);
2326#endif
2327}
2328
2329/*
2330 * "copy()" function
2331 */
2332 static void
2333f_copy(typval_T *argvars, typval_T *rettv)
2334{
2335 item_copy(&argvars[0], rettv, FALSE, 0);
2336}
2337
2338#ifdef FEAT_FLOAT
2339/*
2340 * "cos()" function
2341 */
2342 static void
2343f_cos(typval_T *argvars, typval_T *rettv)
2344{
2345 float_T f = 0.0;
2346
2347 rettv->v_type = VAR_FLOAT;
2348 if (get_float_arg(argvars, &f) == OK)
2349 rettv->vval.v_float = cos(f);
2350 else
2351 rettv->vval.v_float = 0.0;
2352}
2353
2354/*
2355 * "cosh()" function
2356 */
2357 static void
2358f_cosh(typval_T *argvars, typval_T *rettv)
2359{
2360 float_T f = 0.0;
2361
2362 rettv->v_type = VAR_FLOAT;
2363 if (get_float_arg(argvars, &f) == OK)
2364 rettv->vval.v_float = cosh(f);
2365 else
2366 rettv->vval.v_float = 0.0;
2367}
2368#endif
2369
2370/*
2371 * "count()" function
2372 */
2373 static void
2374f_count(typval_T *argvars, typval_T *rettv)
2375{
2376 long n = 0;
2377 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002378 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002379
Bram Moolenaar9966b212017-07-28 16:46:57 +02002380 if (argvars[2].v_type != VAR_UNKNOWN)
2381 ic = (int)get_tv_number_chk(&argvars[2], &error);
2382
2383 if (argvars[0].v_type == VAR_STRING)
2384 {
2385 char_u *expr = get_tv_string_chk(&argvars[1]);
2386 char_u *p = argvars[0].vval.v_string;
2387 char_u *next;
2388
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002389 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002390 {
2391 if (ic)
2392 {
2393 size_t len = STRLEN(expr);
2394
2395 while (*p != NUL)
2396 {
2397 if (MB_STRNICMP(p, expr, len) == 0)
2398 {
2399 ++n;
2400 p += len;
2401 }
2402 else
2403 MB_PTR_ADV(p);
2404 }
2405 }
2406 else
2407 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2408 != NULL)
2409 {
2410 ++n;
2411 p = next + STRLEN(expr);
2412 }
2413 }
2414
2415 }
2416 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002417 {
2418 listitem_T *li;
2419 list_T *l;
2420 long idx;
2421
2422 if ((l = argvars[0].vval.v_list) != NULL)
2423 {
2424 li = l->lv_first;
2425 if (argvars[2].v_type != VAR_UNKNOWN)
2426 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002427 if (argvars[3].v_type != VAR_UNKNOWN)
2428 {
2429 idx = (long)get_tv_number_chk(&argvars[3], &error);
2430 if (!error)
2431 {
2432 li = list_find(l, idx);
2433 if (li == NULL)
2434 EMSGN(_(e_listidx), idx);
2435 }
2436 }
2437 if (error)
2438 li = NULL;
2439 }
2440
2441 for ( ; li != NULL; li = li->li_next)
2442 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2443 ++n;
2444 }
2445 }
2446 else if (argvars[0].v_type == VAR_DICT)
2447 {
2448 int todo;
2449 dict_T *d;
2450 hashitem_T *hi;
2451
2452 if ((d = argvars[0].vval.v_dict) != NULL)
2453 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002454 if (argvars[2].v_type != VAR_UNKNOWN)
2455 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002456 if (argvars[3].v_type != VAR_UNKNOWN)
2457 EMSG(_(e_invarg));
2458 }
2459
2460 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2461 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2462 {
2463 if (!HASHITEM_EMPTY(hi))
2464 {
2465 --todo;
2466 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2467 ++n;
2468 }
2469 }
2470 }
2471 }
2472 else
2473 EMSG2(_(e_listdictarg), "count()");
2474 rettv->vval.v_number = n;
2475}
2476
2477/*
2478 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2479 *
2480 * Checks the existence of a cscope connection.
2481 */
2482 static void
2483f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2484{
2485#ifdef FEAT_CSCOPE
2486 int num = 0;
2487 char_u *dbpath = NULL;
2488 char_u *prepend = NULL;
2489 char_u buf[NUMBUFLEN];
2490
2491 if (argvars[0].v_type != VAR_UNKNOWN
2492 && argvars[1].v_type != VAR_UNKNOWN)
2493 {
2494 num = (int)get_tv_number(&argvars[0]);
2495 dbpath = get_tv_string(&argvars[1]);
2496 if (argvars[2].v_type != VAR_UNKNOWN)
2497 prepend = get_tv_string_buf(&argvars[2], buf);
2498 }
2499
2500 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2501#endif
2502}
2503
2504/*
2505 * "cursor(lnum, col)" function, or
2506 * "cursor(list)"
2507 *
2508 * Moves the cursor to the specified line and column.
2509 * Returns 0 when the position could be set, -1 otherwise.
2510 */
2511 static void
2512f_cursor(typval_T *argvars, typval_T *rettv)
2513{
2514 long line, col;
2515#ifdef FEAT_VIRTUALEDIT
2516 long coladd = 0;
2517#endif
2518 int set_curswant = TRUE;
2519
2520 rettv->vval.v_number = -1;
2521 if (argvars[1].v_type == VAR_UNKNOWN)
2522 {
2523 pos_T pos;
2524 colnr_T curswant = -1;
2525
2526 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2527 {
2528 EMSG(_(e_invarg));
2529 return;
2530 }
2531 line = pos.lnum;
2532 col = pos.col;
2533#ifdef FEAT_VIRTUALEDIT
2534 coladd = pos.coladd;
2535#endif
2536 if (curswant >= 0)
2537 {
2538 curwin->w_curswant = curswant - 1;
2539 set_curswant = FALSE;
2540 }
2541 }
2542 else
2543 {
2544 line = get_tv_lnum(argvars);
2545 col = (long)get_tv_number_chk(&argvars[1], NULL);
2546#ifdef FEAT_VIRTUALEDIT
2547 if (argvars[2].v_type != VAR_UNKNOWN)
2548 coladd = (long)get_tv_number_chk(&argvars[2], NULL);
2549#endif
2550 }
2551 if (line < 0 || col < 0
2552#ifdef FEAT_VIRTUALEDIT
2553 || coladd < 0
2554#endif
2555 )
2556 return; /* type error; errmsg already given */
2557 if (line > 0)
2558 curwin->w_cursor.lnum = line;
2559 if (col > 0)
2560 curwin->w_cursor.col = col - 1;
2561#ifdef FEAT_VIRTUALEDIT
2562 curwin->w_cursor.coladd = coladd;
2563#endif
2564
2565 /* Make sure the cursor is in a valid position. */
2566 check_cursor();
2567#ifdef FEAT_MBYTE
2568 /* Correct cursor for multi-byte character. */
2569 if (has_mbyte)
2570 mb_adjust_cursor();
2571#endif
2572
2573 curwin->w_set_curswant = set_curswant;
2574 rettv->vval.v_number = 0;
2575}
2576
2577/*
2578 * "deepcopy()" function
2579 */
2580 static void
2581f_deepcopy(typval_T *argvars, typval_T *rettv)
2582{
2583 int noref = 0;
2584 int copyID;
2585
2586 if (argvars[1].v_type != VAR_UNKNOWN)
2587 noref = (int)get_tv_number_chk(&argvars[1], NULL);
2588 if (noref < 0 || noref > 1)
2589 EMSG(_(e_invarg));
2590 else
2591 {
2592 copyID = get_copyID();
2593 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2594 }
2595}
2596
2597/*
2598 * "delete()" function
2599 */
2600 static void
2601f_delete(typval_T *argvars, typval_T *rettv)
2602{
2603 char_u nbuf[NUMBUFLEN];
2604 char_u *name;
2605 char_u *flags;
2606
2607 rettv->vval.v_number = -1;
2608 if (check_restricted() || check_secure())
2609 return;
2610
2611 name = get_tv_string(&argvars[0]);
2612 if (name == NULL || *name == NUL)
2613 {
2614 EMSG(_(e_invarg));
2615 return;
2616 }
2617
2618 if (argvars[1].v_type != VAR_UNKNOWN)
2619 flags = get_tv_string_buf(&argvars[1], nbuf);
2620 else
2621 flags = (char_u *)"";
2622
2623 if (*flags == NUL)
2624 /* delete a file */
2625 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2626 else if (STRCMP(flags, "d") == 0)
2627 /* delete an empty directory */
2628 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2629 else if (STRCMP(flags, "rf") == 0)
2630 /* delete a directory recursively */
2631 rettv->vval.v_number = delete_recursive(name);
2632 else
2633 EMSG2(_(e_invexpr2), flags);
2634}
2635
2636/*
2637 * "did_filetype()" function
2638 */
2639 static void
2640f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2641{
2642#ifdef FEAT_AUTOCMD
2643 rettv->vval.v_number = did_filetype;
2644#endif
2645}
2646
2647/*
2648 * "diff_filler()" function
2649 */
2650 static void
2651f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2652{
2653#ifdef FEAT_DIFF
2654 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
2655#endif
2656}
2657
2658/*
2659 * "diff_hlID()" function
2660 */
2661 static void
2662f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2663{
2664#ifdef FEAT_DIFF
2665 linenr_T lnum = get_tv_lnum(argvars);
2666 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01002667 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002668 static int fnum = 0;
2669 static int change_start = 0;
2670 static int change_end = 0;
2671 static hlf_T hlID = (hlf_T)0;
2672 int filler_lines;
2673 int col;
2674
2675 if (lnum < 0) /* ignore type error in {lnum} arg */
2676 lnum = 0;
2677 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002678 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002679 || fnum != curbuf->b_fnum)
2680 {
2681 /* New line, buffer, change: need to get the values. */
2682 filler_lines = diff_check(curwin, lnum);
2683 if (filler_lines < 0)
2684 {
2685 if (filler_lines == -1)
2686 {
2687 change_start = MAXCOL;
2688 change_end = -1;
2689 if (diff_find_change(curwin, lnum, &change_start, &change_end))
2690 hlID = HLF_ADD; /* added line */
2691 else
2692 hlID = HLF_CHD; /* changed line */
2693 }
2694 else
2695 hlID = HLF_ADD; /* added line */
2696 }
2697 else
2698 hlID = (hlf_T)0;
2699 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002700 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002701 fnum = curbuf->b_fnum;
2702 }
2703
2704 if (hlID == HLF_CHD || hlID == HLF_TXD)
2705 {
2706 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
2707 if (col >= change_start && col <= change_end)
2708 hlID = HLF_TXD; /* changed text */
2709 else
2710 hlID = HLF_CHD; /* changed line */
2711 }
2712 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
2713#endif
2714}
2715
2716/*
2717 * "empty({expr})" function
2718 */
2719 static void
2720f_empty(typval_T *argvars, typval_T *rettv)
2721{
2722 int n = FALSE;
2723
2724 switch (argvars[0].v_type)
2725 {
2726 case VAR_STRING:
2727 case VAR_FUNC:
2728 n = argvars[0].vval.v_string == NULL
2729 || *argvars[0].vval.v_string == NUL;
2730 break;
2731 case VAR_PARTIAL:
2732 n = FALSE;
2733 break;
2734 case VAR_NUMBER:
2735 n = argvars[0].vval.v_number == 0;
2736 break;
2737 case VAR_FLOAT:
2738#ifdef FEAT_FLOAT
2739 n = argvars[0].vval.v_float == 0.0;
2740 break;
2741#endif
2742 case VAR_LIST:
2743 n = argvars[0].vval.v_list == NULL
2744 || argvars[0].vval.v_list->lv_first == NULL;
2745 break;
2746 case VAR_DICT:
2747 n = argvars[0].vval.v_dict == NULL
2748 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2749 break;
2750 case VAR_SPECIAL:
2751 n = argvars[0].vval.v_number != VVAL_TRUE;
2752 break;
2753
2754 case VAR_JOB:
2755#ifdef FEAT_JOB_CHANNEL
2756 n = argvars[0].vval.v_job == NULL
2757 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2758 break;
2759#endif
2760 case VAR_CHANNEL:
2761#ifdef FEAT_JOB_CHANNEL
2762 n = argvars[0].vval.v_channel == NULL
2763 || !channel_is_open(argvars[0].vval.v_channel);
2764 break;
2765#endif
2766 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01002767 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002768 n = TRUE;
2769 break;
2770 }
2771
2772 rettv->vval.v_number = n;
2773}
2774
2775/*
2776 * "escape({string}, {chars})" function
2777 */
2778 static void
2779f_escape(typval_T *argvars, typval_T *rettv)
2780{
2781 char_u buf[NUMBUFLEN];
2782
2783 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
2784 get_tv_string_buf(&argvars[1], buf));
2785 rettv->v_type = VAR_STRING;
2786}
2787
2788/*
2789 * "eval()" function
2790 */
2791 static void
2792f_eval(typval_T *argvars, typval_T *rettv)
2793{
2794 char_u *s, *p;
2795
2796 s = get_tv_string_chk(&argvars[0]);
2797 if (s != NULL)
2798 s = skipwhite(s);
2799
2800 p = s;
2801 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
2802 {
2803 if (p != NULL && !aborting())
2804 EMSG2(_(e_invexpr2), p);
2805 need_clr_eos = FALSE;
2806 rettv->v_type = VAR_NUMBER;
2807 rettv->vval.v_number = 0;
2808 }
2809 else if (*s != NUL)
2810 EMSG(_(e_trailing));
2811}
2812
2813/*
2814 * "eventhandler()" function
2815 */
2816 static void
2817f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
2818{
2819 rettv->vval.v_number = vgetc_busy;
2820}
2821
2822/*
2823 * "executable()" function
2824 */
2825 static void
2826f_executable(typval_T *argvars, typval_T *rettv)
2827{
2828 char_u *name = get_tv_string(&argvars[0]);
2829
2830 /* Check in $PATH and also check directly if there is a directory name. */
2831 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
2832 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
2833}
2834
2835static garray_T redir_execute_ga;
2836
2837/*
2838 * Append "value[value_len]" to the execute() output.
2839 */
2840 void
2841execute_redir_str(char_u *value, int value_len)
2842{
2843 int len;
2844
2845 if (value_len == -1)
2846 len = (int)STRLEN(value); /* Append the entire string */
2847 else
2848 len = value_len; /* Append only "value_len" characters */
2849 if (ga_grow(&redir_execute_ga, len) == OK)
2850 {
2851 mch_memmove((char *)redir_execute_ga.ga_data
2852 + redir_execute_ga.ga_len, value, len);
2853 redir_execute_ga.ga_len += len;
2854 }
2855}
2856
2857/*
2858 * Get next line from a list.
2859 * Called by do_cmdline() to get the next line.
2860 * Returns allocated string, or NULL for end of function.
2861 */
2862
2863 static char_u *
2864get_list_line(
2865 int c UNUSED,
2866 void *cookie,
2867 int indent UNUSED)
2868{
2869 listitem_T **p = (listitem_T **)cookie;
2870 listitem_T *item = *p;
2871 char_u buf[NUMBUFLEN];
2872 char_u *s;
2873
2874 if (item == NULL)
2875 return NULL;
2876 s = get_tv_string_buf_chk(&item->li_tv, buf);
2877 *p = item->li_next;
2878 return s == NULL ? NULL : vim_strsave(s);
2879}
2880
2881/*
2882 * "execute()" function
2883 */
2884 static void
2885f_execute(typval_T *argvars, typval_T *rettv)
2886{
2887 char_u *cmd = NULL;
2888 list_T *list = NULL;
2889 int save_msg_silent = msg_silent;
2890 int save_emsg_silent = emsg_silent;
2891 int save_emsg_noredir = emsg_noredir;
2892 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01002893 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002894 garray_T save_ga;
2895
2896 rettv->vval.v_string = NULL;
2897 rettv->v_type = VAR_STRING;
2898
2899 if (argvars[0].v_type == VAR_LIST)
2900 {
2901 list = argvars[0].vval.v_list;
2902 if (list == NULL || list->lv_first == NULL)
2903 /* empty list, no commands, empty output */
2904 return;
2905 ++list->lv_refcount;
2906 }
2907 else
2908 {
2909 cmd = get_tv_string_chk(&argvars[0]);
2910 if (cmd == NULL)
2911 return;
2912 }
2913
2914 if (argvars[1].v_type != VAR_UNKNOWN)
2915 {
2916 char_u buf[NUMBUFLEN];
2917 char_u *s = get_tv_string_buf_chk(&argvars[1], buf);
2918
2919 if (s == NULL)
2920 return;
2921 if (STRNCMP(s, "silent", 6) == 0)
2922 ++msg_silent;
2923 if (STRCMP(s, "silent!") == 0)
2924 {
2925 emsg_silent = TRUE;
2926 emsg_noredir = TRUE;
2927 }
2928 }
2929 else
2930 ++msg_silent;
2931
2932 if (redir_execute)
2933 save_ga = redir_execute_ga;
2934 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
2935 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01002936 redir_off = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002937
2938 if (cmd != NULL)
2939 do_cmdline_cmd(cmd);
2940 else
2941 {
2942 listitem_T *item = list->lv_first;
2943
2944 do_cmdline(NULL, get_list_line, (void *)&item,
2945 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
2946 --list->lv_refcount;
2947 }
2948
Bram Moolenaard297f352017-01-29 20:31:21 +01002949 /* Need to append a NUL to the result. */
2950 if (ga_grow(&redir_execute_ga, 1) == OK)
2951 {
2952 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
2953 rettv->vval.v_string = redir_execute_ga.ga_data;
2954 }
2955 else
2956 {
2957 ga_clear(&redir_execute_ga);
2958 rettv->vval.v_string = NULL;
2959 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002960 msg_silent = save_msg_silent;
2961 emsg_silent = save_emsg_silent;
2962 emsg_noredir = save_emsg_noredir;
2963
2964 redir_execute = save_redir_execute;
2965 if (redir_execute)
2966 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01002967 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002968
2969 /* "silent reg" or "silent echo x" leaves msg_col somewhere in the
2970 * line. Put it back in the first column. */
2971 msg_col = 0;
2972}
2973
2974/*
2975 * "exepath()" function
2976 */
2977 static void
2978f_exepath(typval_T *argvars, typval_T *rettv)
2979{
2980 char_u *p = NULL;
2981
2982 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
2983 rettv->v_type = VAR_STRING;
2984 rettv->vval.v_string = p;
2985}
2986
2987/*
2988 * "exists()" function
2989 */
2990 static void
2991f_exists(typval_T *argvars, typval_T *rettv)
2992{
2993 char_u *p;
2994 char_u *name;
2995 int n = FALSE;
2996 int len = 0;
2997
2998 p = get_tv_string(&argvars[0]);
2999 if (*p == '$') /* environment variable */
3000 {
3001 /* first try "normal" environment variables (fast) */
3002 if (mch_getenv(p + 1) != NULL)
3003 n = TRUE;
3004 else
3005 {
3006 /* try expanding things like $VIM and ${HOME} */
3007 p = expand_env_save(p);
3008 if (p != NULL && *p != '$')
3009 n = TRUE;
3010 vim_free(p);
3011 }
3012 }
3013 else if (*p == '&' || *p == '+') /* option */
3014 {
3015 n = (get_option_tv(&p, NULL, TRUE) == OK);
3016 if (*skipwhite(p) != NUL)
3017 n = FALSE; /* trailing garbage */
3018 }
3019 else if (*p == '*') /* internal or user defined function */
3020 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003021 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003022 }
3023 else if (*p == ':')
3024 {
3025 n = cmd_exists(p + 1);
3026 }
3027 else if (*p == '#')
3028 {
3029#ifdef FEAT_AUTOCMD
3030 if (p[1] == '#')
3031 n = autocmd_supported(p + 2);
3032 else
3033 n = au_exists(p + 1);
3034#endif
3035 }
3036 else /* internal variable */
3037 {
3038 char_u *tofree;
3039 typval_T tv;
3040
3041 /* get_name_len() takes care of expanding curly braces */
3042 name = p;
3043 len = get_name_len(&p, &tofree, TRUE, FALSE);
3044 if (len > 0)
3045 {
3046 if (tofree != NULL)
3047 name = tofree;
3048 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
3049 if (n)
3050 {
3051 /* handle d.key, l[idx], f(expr) */
3052 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
3053 if (n)
3054 clear_tv(&tv);
3055 }
3056 }
3057 if (*p != NUL)
3058 n = FALSE;
3059
3060 vim_free(tofree);
3061 }
3062
3063 rettv->vval.v_number = n;
3064}
3065
3066#ifdef FEAT_FLOAT
3067/*
3068 * "exp()" function
3069 */
3070 static void
3071f_exp(typval_T *argvars, typval_T *rettv)
3072{
3073 float_T f = 0.0;
3074
3075 rettv->v_type = VAR_FLOAT;
3076 if (get_float_arg(argvars, &f) == OK)
3077 rettv->vval.v_float = exp(f);
3078 else
3079 rettv->vval.v_float = 0.0;
3080}
3081#endif
3082
3083/*
3084 * "expand()" function
3085 */
3086 static void
3087f_expand(typval_T *argvars, typval_T *rettv)
3088{
3089 char_u *s;
3090 int len;
3091 char_u *errormsg;
3092 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3093 expand_T xpc;
3094 int error = FALSE;
3095 char_u *result;
3096
3097 rettv->v_type = VAR_STRING;
3098 if (argvars[1].v_type != VAR_UNKNOWN
3099 && argvars[2].v_type != VAR_UNKNOWN
3100 && get_tv_number_chk(&argvars[2], &error)
3101 && !error)
3102 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003103 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003104 }
3105
3106 s = get_tv_string(&argvars[0]);
3107 if (*s == '%' || *s == '#' || *s == '<')
3108 {
3109 ++emsg_off;
3110 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3111 --emsg_off;
3112 if (rettv->v_type == VAR_LIST)
3113 {
3114 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3115 list_append_string(rettv->vval.v_list, result, -1);
3116 else
3117 vim_free(result);
3118 }
3119 else
3120 rettv->vval.v_string = result;
3121 }
3122 else
3123 {
3124 /* When the optional second argument is non-zero, don't remove matches
3125 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3126 if (argvars[1].v_type != VAR_UNKNOWN
3127 && get_tv_number_chk(&argvars[1], &error))
3128 options |= WILD_KEEP_ALL;
3129 if (!error)
3130 {
3131 ExpandInit(&xpc);
3132 xpc.xp_context = EXPAND_FILES;
3133 if (p_wic)
3134 options += WILD_ICASE;
3135 if (rettv->v_type == VAR_STRING)
3136 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3137 options, WILD_ALL);
3138 else if (rettv_list_alloc(rettv) != FAIL)
3139 {
3140 int i;
3141
3142 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3143 for (i = 0; i < xpc.xp_numfiles; i++)
3144 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3145 ExpandCleanup(&xpc);
3146 }
3147 }
3148 else
3149 rettv->vval.v_string = NULL;
3150 }
3151}
3152
3153/*
3154 * "extend(list, list [, idx])" function
3155 * "extend(dict, dict [, action])" function
3156 */
3157 static void
3158f_extend(typval_T *argvars, typval_T *rettv)
3159{
3160 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3161
3162 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3163 {
3164 list_T *l1, *l2;
3165 listitem_T *item;
3166 long before;
3167 int error = FALSE;
3168
3169 l1 = argvars[0].vval.v_list;
3170 l2 = argvars[1].vval.v_list;
3171 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
3172 && l2 != NULL)
3173 {
3174 if (argvars[2].v_type != VAR_UNKNOWN)
3175 {
3176 before = (long)get_tv_number_chk(&argvars[2], &error);
3177 if (error)
3178 return; /* type error; errmsg already given */
3179
3180 if (before == l1->lv_len)
3181 item = NULL;
3182 else
3183 {
3184 item = list_find(l1, before);
3185 if (item == NULL)
3186 {
3187 EMSGN(_(e_listidx), before);
3188 return;
3189 }
3190 }
3191 }
3192 else
3193 item = NULL;
3194 list_extend(l1, l2, item);
3195
3196 copy_tv(&argvars[0], rettv);
3197 }
3198 }
3199 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3200 {
3201 dict_T *d1, *d2;
3202 char_u *action;
3203 int i;
3204
3205 d1 = argvars[0].vval.v_dict;
3206 d2 = argvars[1].vval.v_dict;
3207 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
3208 && d2 != NULL)
3209 {
3210 /* Check the third argument. */
3211 if (argvars[2].v_type != VAR_UNKNOWN)
3212 {
3213 static char *(av[]) = {"keep", "force", "error"};
3214
3215 action = get_tv_string_chk(&argvars[2]);
3216 if (action == NULL)
3217 return; /* type error; errmsg already given */
3218 for (i = 0; i < 3; ++i)
3219 if (STRCMP(action, av[i]) == 0)
3220 break;
3221 if (i == 3)
3222 {
3223 EMSG2(_(e_invarg2), action);
3224 return;
3225 }
3226 }
3227 else
3228 action = (char_u *)"force";
3229
3230 dict_extend(d1, d2, action);
3231
3232 copy_tv(&argvars[0], rettv);
3233 }
3234 }
3235 else
3236 EMSG2(_(e_listdictarg), "extend()");
3237}
3238
3239/*
3240 * "feedkeys()" function
3241 */
3242 static void
3243f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3244{
3245 int remap = TRUE;
3246 int insert = FALSE;
3247 char_u *keys, *flags;
3248 char_u nbuf[NUMBUFLEN];
3249 int typed = FALSE;
3250 int execute = FALSE;
3251 int dangerous = FALSE;
3252 char_u *keys_esc;
3253
3254 /* This is not allowed in the sandbox. If the commands would still be
3255 * executed in the sandbox it would be OK, but it probably happens later,
3256 * when "sandbox" is no longer set. */
3257 if (check_secure())
3258 return;
3259
3260 keys = get_tv_string(&argvars[0]);
3261
3262 if (argvars[1].v_type != VAR_UNKNOWN)
3263 {
3264 flags = get_tv_string_buf(&argvars[1], nbuf);
3265 for ( ; *flags != NUL; ++flags)
3266 {
3267 switch (*flags)
3268 {
3269 case 'n': remap = FALSE; break;
3270 case 'm': remap = TRUE; break;
3271 case 't': typed = TRUE; break;
3272 case 'i': insert = TRUE; break;
3273 case 'x': execute = TRUE; break;
3274 case '!': dangerous = TRUE; break;
3275 }
3276 }
3277 }
3278
3279 if (*keys != NUL || execute)
3280 {
3281 /* Need to escape K_SPECIAL and CSI before putting the string in the
3282 * typeahead buffer. */
3283 keys_esc = vim_strsave_escape_csi(keys);
3284 if (keys_esc != NULL)
3285 {
3286 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
3287 insert ? 0 : typebuf.tb_len, !typed, FALSE);
3288 vim_free(keys_esc);
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003289 if (vgetc_busy
3290#ifdef FEAT_TIMERS
3291 || timer_busy
3292#endif
3293 )
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003294 typebuf_was_filled = TRUE;
3295 if (execute)
3296 {
3297 int save_msg_scroll = msg_scroll;
3298
3299 /* Avoid a 1 second delay when the keys start Insert mode. */
3300 msg_scroll = FALSE;
3301
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02003302#ifdef FEAT_TERMINAL
3303 if (term_use_loop())
3304 terminal_loop(FALSE);
3305 else
3306#endif
3307 {
3308 if (!dangerous)
3309 ++ex_normal_busy;
3310 exec_normal(TRUE);
3311 if (!dangerous)
3312 --ex_normal_busy;
3313 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003314 msg_scroll |= save_msg_scroll;
3315 }
3316 }
3317 }
3318}
3319
3320/*
3321 * "filereadable()" function
3322 */
3323 static void
3324f_filereadable(typval_T *argvars, typval_T *rettv)
3325{
3326 int fd;
3327 char_u *p;
3328 int n;
3329
3330#ifndef O_NONBLOCK
3331# define O_NONBLOCK 0
3332#endif
3333 p = get_tv_string(&argvars[0]);
3334 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3335 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3336 {
3337 n = TRUE;
3338 close(fd);
3339 }
3340 else
3341 n = FALSE;
3342
3343 rettv->vval.v_number = n;
3344}
3345
3346/*
3347 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3348 * rights to write into.
3349 */
3350 static void
3351f_filewritable(typval_T *argvars, typval_T *rettv)
3352{
3353 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
3354}
3355
3356 static void
3357findfilendir(
3358 typval_T *argvars UNUSED,
3359 typval_T *rettv,
3360 int find_what UNUSED)
3361{
3362#ifdef FEAT_SEARCHPATH
3363 char_u *fname;
3364 char_u *fresult = NULL;
3365 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3366 char_u *p;
3367 char_u pathbuf[NUMBUFLEN];
3368 int count = 1;
3369 int first = TRUE;
3370 int error = FALSE;
3371#endif
3372
3373 rettv->vval.v_string = NULL;
3374 rettv->v_type = VAR_STRING;
3375
3376#ifdef FEAT_SEARCHPATH
3377 fname = get_tv_string(&argvars[0]);
3378
3379 if (argvars[1].v_type != VAR_UNKNOWN)
3380 {
3381 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
3382 if (p == NULL)
3383 error = TRUE;
3384 else
3385 {
3386 if (*p != NUL)
3387 path = p;
3388
3389 if (argvars[2].v_type != VAR_UNKNOWN)
3390 count = (int)get_tv_number_chk(&argvars[2], &error);
3391 }
3392 }
3393
3394 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3395 error = TRUE;
3396
3397 if (*fname != NUL && !error)
3398 {
3399 do
3400 {
3401 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3402 vim_free(fresult);
3403 fresult = find_file_in_path_option(first ? fname : NULL,
3404 first ? (int)STRLEN(fname) : 0,
3405 0, first, path,
3406 find_what,
3407 curbuf->b_ffname,
3408 find_what == FINDFILE_DIR
3409 ? (char_u *)"" : curbuf->b_p_sua);
3410 first = FALSE;
3411
3412 if (fresult != NULL && rettv->v_type == VAR_LIST)
3413 list_append_string(rettv->vval.v_list, fresult, -1);
3414
3415 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3416 }
3417
3418 if (rettv->v_type == VAR_STRING)
3419 rettv->vval.v_string = fresult;
3420#endif
3421}
3422
3423/*
3424 * "filter()" function
3425 */
3426 static void
3427f_filter(typval_T *argvars, typval_T *rettv)
3428{
3429 filter_map(argvars, rettv, FALSE);
3430}
3431
3432/*
3433 * "finddir({fname}[, {path}[, {count}]])" function
3434 */
3435 static void
3436f_finddir(typval_T *argvars, typval_T *rettv)
3437{
3438 findfilendir(argvars, rettv, FINDFILE_DIR);
3439}
3440
3441/*
3442 * "findfile({fname}[, {path}[, {count}]])" function
3443 */
3444 static void
3445f_findfile(typval_T *argvars, typval_T *rettv)
3446{
3447 findfilendir(argvars, rettv, FINDFILE_FILE);
3448}
3449
3450#ifdef FEAT_FLOAT
3451/*
3452 * "float2nr({float})" function
3453 */
3454 static void
3455f_float2nr(typval_T *argvars, typval_T *rettv)
3456{
3457 float_T f = 0.0;
3458
3459 if (get_float_arg(argvars, &f) == OK)
3460 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003461 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003462 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003463 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003464 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003465 else
3466 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003467 }
3468}
3469
3470/*
3471 * "floor({float})" function
3472 */
3473 static void
3474f_floor(typval_T *argvars, typval_T *rettv)
3475{
3476 float_T f = 0.0;
3477
3478 rettv->v_type = VAR_FLOAT;
3479 if (get_float_arg(argvars, &f) == OK)
3480 rettv->vval.v_float = floor(f);
3481 else
3482 rettv->vval.v_float = 0.0;
3483}
3484
3485/*
3486 * "fmod()" function
3487 */
3488 static void
3489f_fmod(typval_T *argvars, typval_T *rettv)
3490{
3491 float_T fx = 0.0, fy = 0.0;
3492
3493 rettv->v_type = VAR_FLOAT;
3494 if (get_float_arg(argvars, &fx) == OK
3495 && get_float_arg(&argvars[1], &fy) == OK)
3496 rettv->vval.v_float = fmod(fx, fy);
3497 else
3498 rettv->vval.v_float = 0.0;
3499}
3500#endif
3501
3502/*
3503 * "fnameescape({string})" function
3504 */
3505 static void
3506f_fnameescape(typval_T *argvars, typval_T *rettv)
3507{
3508 rettv->vval.v_string = vim_strsave_fnameescape(
3509 get_tv_string(&argvars[0]), FALSE);
3510 rettv->v_type = VAR_STRING;
3511}
3512
3513/*
3514 * "fnamemodify({fname}, {mods})" function
3515 */
3516 static void
3517f_fnamemodify(typval_T *argvars, typval_T *rettv)
3518{
3519 char_u *fname;
3520 char_u *mods;
3521 int usedlen = 0;
3522 int len;
3523 char_u *fbuf = NULL;
3524 char_u buf[NUMBUFLEN];
3525
3526 fname = get_tv_string_chk(&argvars[0]);
3527 mods = get_tv_string_buf_chk(&argvars[1], buf);
3528 if (fname == NULL || mods == NULL)
3529 fname = NULL;
3530 else
3531 {
3532 len = (int)STRLEN(fname);
3533 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
3534 }
3535
3536 rettv->v_type = VAR_STRING;
3537 if (fname == NULL)
3538 rettv->vval.v_string = NULL;
3539 else
3540 rettv->vval.v_string = vim_strnsave(fname, len);
3541 vim_free(fbuf);
3542}
3543
3544static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
3545
3546/*
3547 * "foldclosed()" function
3548 */
3549 static void
3550foldclosed_both(
3551 typval_T *argvars UNUSED,
3552 typval_T *rettv,
3553 int end UNUSED)
3554{
3555#ifdef FEAT_FOLDING
3556 linenr_T lnum;
3557 linenr_T first, last;
3558
3559 lnum = get_tv_lnum(argvars);
3560 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3561 {
3562 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3563 {
3564 if (end)
3565 rettv->vval.v_number = (varnumber_T)last;
3566 else
3567 rettv->vval.v_number = (varnumber_T)first;
3568 return;
3569 }
3570 }
3571#endif
3572 rettv->vval.v_number = -1;
3573}
3574
3575/*
3576 * "foldclosed()" function
3577 */
3578 static void
3579f_foldclosed(typval_T *argvars, typval_T *rettv)
3580{
3581 foldclosed_both(argvars, rettv, FALSE);
3582}
3583
3584/*
3585 * "foldclosedend()" function
3586 */
3587 static void
3588f_foldclosedend(typval_T *argvars, typval_T *rettv)
3589{
3590 foldclosed_both(argvars, rettv, TRUE);
3591}
3592
3593/*
3594 * "foldlevel()" function
3595 */
3596 static void
3597f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3598{
3599#ifdef FEAT_FOLDING
3600 linenr_T lnum;
3601
3602 lnum = get_tv_lnum(argvars);
3603 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3604 rettv->vval.v_number = foldLevel(lnum);
3605#endif
3606}
3607
3608/*
3609 * "foldtext()" function
3610 */
3611 static void
3612f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
3613{
3614#ifdef FEAT_FOLDING
3615 linenr_T foldstart;
3616 linenr_T foldend;
3617 char_u *dashes;
3618 linenr_T lnum;
3619 char_u *s;
3620 char_u *r;
3621 int len;
3622 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003623 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003624#endif
3625
3626 rettv->v_type = VAR_STRING;
3627 rettv->vval.v_string = NULL;
3628#ifdef FEAT_FOLDING
3629 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
3630 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
3631 dashes = get_vim_var_str(VV_FOLDDASHES);
3632 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
3633 && dashes != NULL)
3634 {
3635 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02003636 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003637 if (!linewhite(lnum))
3638 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003639
3640 /* Find interesting text in this line. */
3641 s = skipwhite(ml_get(lnum));
3642 /* skip C comment-start */
3643 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
3644 {
3645 s = skipwhite(s + 2);
3646 if (*skipwhite(s) == NUL
3647 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
3648 {
3649 s = skipwhite(ml_get(lnum + 1));
3650 if (*s == '*')
3651 s = skipwhite(s + 1);
3652 }
3653 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02003654 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003655 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003656 r = alloc((unsigned)(STRLEN(txt)
3657 + STRLEN(dashes) /* for %s */
3658 + 20 /* for %3ld */
3659 + STRLEN(s))); /* concatenated */
3660 if (r != NULL)
3661 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02003662 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003663 len = (int)STRLEN(r);
3664 STRCAT(r, s);
3665 /* remove 'foldmarker' and 'commentstring' */
3666 foldtext_cleanup(r + len);
3667 rettv->vval.v_string = r;
3668 }
3669 }
3670#endif
3671}
3672
3673/*
3674 * "foldtextresult(lnum)" function
3675 */
3676 static void
3677f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
3678{
3679#ifdef FEAT_FOLDING
3680 linenr_T lnum;
3681 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003682 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003683 foldinfo_T foldinfo;
3684 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003685 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003686#endif
3687
3688 rettv->v_type = VAR_STRING;
3689 rettv->vval.v_string = NULL;
3690#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003691 if (entered)
3692 return; /* reject recursive use */
3693 entered = TRUE;
3694
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003695 lnum = get_tv_lnum(argvars);
3696 /* treat illegal types and illegal string values for {lnum} the same */
3697 if (lnum < 0)
3698 lnum = 0;
3699 fold_count = foldedCount(curwin, lnum, &foldinfo);
3700 if (fold_count > 0)
3701 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01003702 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
3703 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003704 if (text == buf)
3705 text = vim_strsave(text);
3706 rettv->vval.v_string = text;
3707 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003708
3709 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003710#endif
3711}
3712
3713/*
3714 * "foreground()" function
3715 */
3716 static void
3717f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3718{
3719#ifdef FEAT_GUI
3720 if (gui.in_use)
3721 gui_mch_set_foreground();
3722#else
3723# ifdef WIN32
3724 win32_set_foreground();
3725# endif
3726#endif
3727}
3728
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003729 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003730common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003731{
3732 char_u *s;
3733 char_u *name;
3734 int use_string = FALSE;
3735 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003736 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003737
3738 if (argvars[0].v_type == VAR_FUNC)
3739 {
3740 /* function(MyFunc, [arg], dict) */
3741 s = argvars[0].vval.v_string;
3742 }
3743 else if (argvars[0].v_type == VAR_PARTIAL
3744 && argvars[0].vval.v_partial != NULL)
3745 {
3746 /* function(dict.MyFunc, [arg]) */
3747 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003748 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003749 }
3750 else
3751 {
3752 /* function('MyFunc', [arg], dict) */
3753 s = get_tv_string(&argvars[0]);
3754 use_string = TRUE;
3755 }
3756
Bram Moolenaar843b8842016-08-21 14:36:15 +02003757 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003758 {
3759 name = s;
3760 trans_name = trans_function_name(&name, FALSE,
3761 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
3762 if (*name != NUL)
3763 s = NULL;
3764 }
3765
Bram Moolenaar843b8842016-08-21 14:36:15 +02003766 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
3767 || (is_funcref && trans_name == NULL))
Bram Moolenaar5582ef12016-09-14 22:16:13 +02003768 EMSG2(_(e_invarg2), use_string ? get_tv_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003769 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003770 else if (trans_name != NULL && (is_funcref
3771 ? find_func(trans_name) == NULL
3772 : !translated_function_exists(trans_name)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003773 EMSG2(_("E700: Unknown function: %s"), s);
3774 else
3775 {
3776 int dict_idx = 0;
3777 int arg_idx = 0;
3778 list_T *list = NULL;
3779
3780 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
3781 {
3782 char sid_buf[25];
3783 int off = *s == 's' ? 2 : 5;
3784
3785 /* Expand s: and <SID> into <SNR>nr_, so that the function can
3786 * also be called from another script. Using trans_function_name()
3787 * would also work, but some plugins depend on the name being
3788 * printable text. */
3789 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
3790 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
3791 if (name != NULL)
3792 {
3793 STRCPY(name, sid_buf);
3794 STRCAT(name, s + off);
3795 }
3796 }
3797 else
3798 name = vim_strsave(s);
3799
3800 if (argvars[1].v_type != VAR_UNKNOWN)
3801 {
3802 if (argvars[2].v_type != VAR_UNKNOWN)
3803 {
3804 /* function(name, [args], dict) */
3805 arg_idx = 1;
3806 dict_idx = 2;
3807 }
3808 else if (argvars[1].v_type == VAR_DICT)
3809 /* function(name, dict) */
3810 dict_idx = 1;
3811 else
3812 /* function(name, [args]) */
3813 arg_idx = 1;
3814 if (dict_idx > 0)
3815 {
3816 if (argvars[dict_idx].v_type != VAR_DICT)
3817 {
3818 EMSG(_("E922: expected a dict"));
3819 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003820 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003821 }
3822 if (argvars[dict_idx].vval.v_dict == NULL)
3823 dict_idx = 0;
3824 }
3825 if (arg_idx > 0)
3826 {
3827 if (argvars[arg_idx].v_type != VAR_LIST)
3828 {
3829 EMSG(_("E923: Second argument of function() must be a list or a dict"));
3830 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003831 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003832 }
3833 list = argvars[arg_idx].vval.v_list;
3834 if (list == NULL || list->lv_len == 0)
3835 arg_idx = 0;
3836 }
3837 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003838 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003839 {
3840 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
3841
3842 /* result is a VAR_PARTIAL */
3843 if (pt == NULL)
3844 vim_free(name);
3845 else
3846 {
3847 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
3848 {
3849 listitem_T *li;
3850 int i = 0;
3851 int arg_len = 0;
3852 int lv_len = 0;
3853
3854 if (arg_pt != NULL)
3855 arg_len = arg_pt->pt_argc;
3856 if (list != NULL)
3857 lv_len = list->lv_len;
3858 pt->pt_argc = arg_len + lv_len;
3859 pt->pt_argv = (typval_T *)alloc(
3860 sizeof(typval_T) * pt->pt_argc);
3861 if (pt->pt_argv == NULL)
3862 {
3863 vim_free(pt);
3864 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003865 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003866 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003867 for (i = 0; i < arg_len; i++)
3868 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
3869 if (lv_len > 0)
3870 for (li = list->lv_first; li != NULL;
3871 li = li->li_next)
3872 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003873 }
3874
3875 /* For "function(dict.func, [], dict)" and "func" is a partial
3876 * use "dict". That is backwards compatible. */
3877 if (dict_idx > 0)
3878 {
3879 /* The dict is bound explicitly, pt_auto is FALSE. */
3880 pt->pt_dict = argvars[dict_idx].vval.v_dict;
3881 ++pt->pt_dict->dv_refcount;
3882 }
3883 else if (arg_pt != NULL)
3884 {
3885 /* If the dict was bound automatically the result is also
3886 * bound automatically. */
3887 pt->pt_dict = arg_pt->pt_dict;
3888 pt->pt_auto = arg_pt->pt_auto;
3889 if (pt->pt_dict != NULL)
3890 ++pt->pt_dict->dv_refcount;
3891 }
3892
3893 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003894 if (arg_pt != NULL && arg_pt->pt_func != NULL)
3895 {
3896 pt->pt_func = arg_pt->pt_func;
3897 func_ptr_ref(pt->pt_func);
3898 vim_free(name);
3899 }
3900 else if (is_funcref)
3901 {
3902 pt->pt_func = find_func(trans_name);
3903 func_ptr_ref(pt->pt_func);
3904 vim_free(name);
3905 }
3906 else
3907 {
3908 pt->pt_name = name;
3909 func_ref(name);
3910 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003911 }
3912 rettv->v_type = VAR_PARTIAL;
3913 rettv->vval.v_partial = pt;
3914 }
3915 else
3916 {
3917 /* result is a VAR_FUNC */
3918 rettv->v_type = VAR_FUNC;
3919 rettv->vval.v_string = name;
3920 func_ref(name);
3921 }
3922 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003923theend:
3924 vim_free(trans_name);
3925}
3926
3927/*
3928 * "funcref()" function
3929 */
3930 static void
3931f_funcref(typval_T *argvars, typval_T *rettv)
3932{
3933 common_function(argvars, rettv, TRUE);
3934}
3935
3936/*
3937 * "function()" function
3938 */
3939 static void
3940f_function(typval_T *argvars, typval_T *rettv)
3941{
3942 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003943}
3944
3945/*
3946 * "garbagecollect()" function
3947 */
3948 static void
3949f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
3950{
3951 /* This is postponed until we are back at the toplevel, because we may be
3952 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
3953 want_garbage_collect = TRUE;
3954
3955 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
3956 garbage_collect_at_exit = TRUE;
3957}
3958
3959/*
3960 * "get()" function
3961 */
3962 static void
3963f_get(typval_T *argvars, typval_T *rettv)
3964{
3965 listitem_T *li;
3966 list_T *l;
3967 dictitem_T *di;
3968 dict_T *d;
3969 typval_T *tv = NULL;
3970
3971 if (argvars[0].v_type == VAR_LIST)
3972 {
3973 if ((l = argvars[0].vval.v_list) != NULL)
3974 {
3975 int error = FALSE;
3976
3977 li = list_find(l, (long)get_tv_number_chk(&argvars[1], &error));
3978 if (!error && li != NULL)
3979 tv = &li->li_tv;
3980 }
3981 }
3982 else if (argvars[0].v_type == VAR_DICT)
3983 {
3984 if ((d = argvars[0].vval.v_dict) != NULL)
3985 {
3986 di = dict_find(d, get_tv_string(&argvars[1]), -1);
3987 if (di != NULL)
3988 tv = &di->di_tv;
3989 }
3990 }
3991 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
3992 {
3993 partial_T *pt;
3994 partial_T fref_pt;
3995
3996 if (argvars[0].v_type == VAR_PARTIAL)
3997 pt = argvars[0].vval.v_partial;
3998 else
3999 {
4000 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4001 fref_pt.pt_name = argvars[0].vval.v_string;
4002 pt = &fref_pt;
4003 }
4004
4005 if (pt != NULL)
4006 {
4007 char_u *what = get_tv_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004008 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004009
4010 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4011 {
4012 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004013 n = partial_name(pt);
4014 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004015 rettv->vval.v_string = NULL;
4016 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004017 {
4018 rettv->vval.v_string = vim_strsave(n);
4019 if (rettv->v_type == VAR_FUNC)
4020 func_ref(rettv->vval.v_string);
4021 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004022 }
4023 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004024 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004025 else if (STRCMP(what, "args") == 0)
4026 {
4027 rettv->v_type = VAR_LIST;
4028 if (rettv_list_alloc(rettv) == OK)
4029 {
4030 int i;
4031
4032 for (i = 0; i < pt->pt_argc; ++i)
4033 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4034 }
4035 }
4036 else
4037 EMSG2(_(e_invarg2), what);
4038 return;
4039 }
4040 }
4041 else
4042 EMSG2(_(e_listdictarg), "get()");
4043
4044 if (tv == NULL)
4045 {
4046 if (argvars[2].v_type != VAR_UNKNOWN)
4047 copy_tv(&argvars[2], rettv);
4048 }
4049 else
4050 copy_tv(tv, rettv);
4051}
4052
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004053#ifdef FEAT_SIGNS
4054/*
4055 * Returns information about signs placed in a buffer as list of dicts.
4056 */
4057 static void
4058get_buffer_signs(buf_T *buf, list_T *l)
4059{
4060 signlist_T *sign;
4061
4062 for (sign = buf->b_signlist; sign; sign = sign->next)
4063 {
4064 dict_T *d = dict_alloc();
4065
4066 if (d != NULL)
4067 {
4068 dict_add_nr_str(d, "id", sign->id, NULL);
4069 dict_add_nr_str(d, "lnum", sign->lnum, NULL);
Bram Moolenaar6a402ed2016-08-28 14:11:24 +02004070 dict_add_nr_str(d, "name", 0L, sign_typenr2name(sign->typenr));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004071
4072 list_append_dict(l, d);
4073 }
4074 }
4075}
4076#endif
4077
4078/*
4079 * Returns buffer options, variables and other attributes in a dictionary.
4080 */
4081 static dict_T *
4082get_buffer_info(buf_T *buf)
4083{
4084 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004085 tabpage_T *tp;
4086 win_T *wp;
4087 list_T *windows;
4088
4089 dict = dict_alloc();
4090 if (dict == NULL)
4091 return NULL;
4092
Bram Moolenaar33928832016-08-18 21:22:04 +02004093 dict_add_nr_str(dict, "bufnr", buf->b_fnum, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004094 dict_add_nr_str(dict, "name", 0L,
4095 buf->b_ffname != NULL ? buf->b_ffname : (char_u *)"");
Bram Moolenaarf845b872017-01-06 14:04:54 +01004096 dict_add_nr_str(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4097 : buflist_findlnum(buf), NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004098 dict_add_nr_str(dict, "loaded", buf->b_ml.ml_mfp != NULL, NULL);
4099 dict_add_nr_str(dict, "listed", buf->b_p_bl, NULL);
4100 dict_add_nr_str(dict, "changed", bufIsChanged(buf), NULL);
Bram Moolenaar95c526e2017-02-25 14:59:34 +01004101 dict_add_nr_str(dict, "changedtick", CHANGEDTICK(buf), NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004102 dict_add_nr_str(dict, "hidden",
4103 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0,
4104 NULL);
4105
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004106 /* Get a reference to buffer variables */
4107 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004108
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004109 /* List of windows displaying this buffer */
4110 windows = list_alloc();
4111 if (windows != NULL)
4112 {
4113 FOR_ALL_TAB_WINDOWS(tp, wp)
4114 if (wp->w_buffer == buf)
4115 list_append_number(windows, (varnumber_T)wp->w_id);
4116 dict_add_list(dict, "windows", windows);
4117 }
4118
4119#ifdef FEAT_SIGNS
4120 if (buf->b_signlist != NULL)
4121 {
4122 /* List of signs placed in this buffer */
4123 list_T *signs = list_alloc();
4124 if (signs != NULL)
4125 {
4126 get_buffer_signs(buf, signs);
4127 dict_add_list(dict, "signs", signs);
4128 }
4129 }
4130#endif
4131
4132 return dict;
4133}
4134
4135/*
4136 * "getbufinfo()" function
4137 */
4138 static void
4139f_getbufinfo(typval_T *argvars, typval_T *rettv)
4140{
4141 buf_T *buf = NULL;
4142 buf_T *argbuf = NULL;
4143 dict_T *d;
4144 int filtered = FALSE;
4145 int sel_buflisted = FALSE;
4146 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004147 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004148
4149 if (rettv_list_alloc(rettv) != OK)
4150 return;
4151
4152 /* List of all the buffers or selected buffers */
4153 if (argvars[0].v_type == VAR_DICT)
4154 {
4155 dict_T *sel_d = argvars[0].vval.v_dict;
4156
4157 if (sel_d != NULL)
4158 {
4159 dictitem_T *di;
4160
4161 filtered = TRUE;
4162
4163 di = dict_find(sel_d, (char_u *)"buflisted", -1);
4164 if (di != NULL && get_tv_number(&di->di_tv))
4165 sel_buflisted = TRUE;
4166
4167 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
4168 if (di != NULL && get_tv_number(&di->di_tv))
4169 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004170
4171 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
4172 if (di != NULL && get_tv_number(&di->di_tv))
4173 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004174 }
4175 }
4176 else if (argvars[0].v_type != VAR_UNKNOWN)
4177 {
4178 /* Information about one buffer. Argument specifies the buffer */
4179 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4180 ++emsg_off;
4181 argbuf = get_buf_tv(&argvars[0], FALSE);
4182 --emsg_off;
4183 if (argbuf == NULL)
4184 return;
4185 }
4186
4187 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004188 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004189 {
4190 if (argbuf != NULL && argbuf != buf)
4191 continue;
4192 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004193 || (sel_buflisted && !buf->b_p_bl)
4194 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004195 continue;
4196
4197 d = get_buffer_info(buf);
4198 if (d != NULL)
4199 list_append_dict(rettv->vval.v_list, d);
4200 if (argbuf != NULL)
4201 return;
4202 }
4203}
4204
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004205static void get_buffer_lines(buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv);
4206
4207/*
4208 * Get line or list of lines from buffer "buf" into "rettv".
4209 * Return a range (from start to end) of lines in rettv from the specified
4210 * buffer.
4211 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4212 */
4213 static void
4214get_buffer_lines(
4215 buf_T *buf,
4216 linenr_T start,
4217 linenr_T end,
4218 int retlist,
4219 typval_T *rettv)
4220{
4221 char_u *p;
4222
4223 rettv->v_type = VAR_STRING;
4224 rettv->vval.v_string = NULL;
4225 if (retlist && rettv_list_alloc(rettv) == FAIL)
4226 return;
4227
4228 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4229 return;
4230
4231 if (!retlist)
4232 {
4233 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4234 p = ml_get_buf(buf, start, FALSE);
4235 else
4236 p = (char_u *)"";
4237 rettv->vval.v_string = vim_strsave(p);
4238 }
4239 else
4240 {
4241 if (end < start)
4242 return;
4243
4244 if (start < 1)
4245 start = 1;
4246 if (end > buf->b_ml.ml_line_count)
4247 end = buf->b_ml.ml_line_count;
4248 while (start <= end)
4249 if (list_append_string(rettv->vval.v_list,
4250 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4251 break;
4252 }
4253}
4254
4255/*
4256 * Get the lnum from the first argument.
4257 * Also accepts "$", then "buf" is used.
4258 * Returns 0 on error.
4259 */
4260 static linenr_T
4261get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
4262{
4263 if (argvars[0].v_type == VAR_STRING
4264 && argvars[0].vval.v_string != NULL
4265 && argvars[0].vval.v_string[0] == '$'
4266 && buf != NULL)
4267 return buf->b_ml.ml_line_count;
4268 return (linenr_T)get_tv_number_chk(&argvars[0], NULL);
4269}
4270
4271/*
4272 * "getbufline()" function
4273 */
4274 static void
4275f_getbufline(typval_T *argvars, typval_T *rettv)
4276{
4277 linenr_T lnum;
4278 linenr_T end;
4279 buf_T *buf;
4280
4281 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4282 ++emsg_off;
4283 buf = get_buf_tv(&argvars[0], FALSE);
4284 --emsg_off;
4285
4286 lnum = get_tv_lnum_buf(&argvars[1], buf);
4287 if (argvars[2].v_type == VAR_UNKNOWN)
4288 end = lnum;
4289 else
4290 end = get_tv_lnum_buf(&argvars[2], buf);
4291
4292 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4293}
4294
4295/*
4296 * "getbufvar()" function
4297 */
4298 static void
4299f_getbufvar(typval_T *argvars, typval_T *rettv)
4300{
4301 buf_T *buf;
4302 buf_T *save_curbuf;
4303 char_u *varname;
4304 dictitem_T *v;
4305 int done = FALSE;
4306
4307 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4308 varname = get_tv_string_chk(&argvars[1]);
4309 ++emsg_off;
4310 buf = get_buf_tv(&argvars[0], FALSE);
4311
4312 rettv->v_type = VAR_STRING;
4313 rettv->vval.v_string = NULL;
4314
4315 if (buf != NULL && varname != NULL)
4316 {
4317 /* set curbuf to be our buf, temporarily */
4318 save_curbuf = curbuf;
4319 curbuf = buf;
4320
Bram Moolenaar30567352016-08-27 21:25:44 +02004321 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004322 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004323 if (varname[1] == NUL)
4324 {
4325 /* get all buffer-local options in a dict */
4326 dict_T *opts = get_winbuf_options(TRUE);
4327
4328 if (opts != NULL)
4329 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004330 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004331 done = TRUE;
4332 }
4333 }
4334 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4335 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004336 done = TRUE;
4337 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004338 else
4339 {
4340 /* Look up the variable. */
4341 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4342 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4343 'b', varname, FALSE);
4344 if (v != NULL)
4345 {
4346 copy_tv(&v->di_tv, rettv);
4347 done = TRUE;
4348 }
4349 }
4350
4351 /* restore previous notion of curbuf */
4352 curbuf = save_curbuf;
4353 }
4354
4355 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4356 /* use the default value */
4357 copy_tv(&argvars[2], rettv);
4358
4359 --emsg_off;
4360}
4361
4362/*
4363 * "getchar()" function
4364 */
4365 static void
4366f_getchar(typval_T *argvars, typval_T *rettv)
4367{
4368 varnumber_T n;
4369 int error = FALSE;
4370
4371 /* Position the cursor. Needed after a message that ends in a space. */
4372 windgoto(msg_row, msg_col);
4373
4374 ++no_mapping;
4375 ++allow_keys;
4376 for (;;)
4377 {
4378 if (argvars[0].v_type == VAR_UNKNOWN)
4379 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004380 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004381 else if (get_tv_number_chk(&argvars[0], &error) == 1)
4382 /* getchar(1): only check if char avail */
4383 n = vpeekc_any();
4384 else if (error || vpeekc_any() == NUL)
4385 /* illegal argument or getchar(0) and no char avail: return zero */
4386 n = 0;
4387 else
4388 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004389 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004390
4391 if (n == K_IGNORE)
4392 continue;
4393 break;
4394 }
4395 --no_mapping;
4396 --allow_keys;
4397
4398 set_vim_var_nr(VV_MOUSE_WIN, 0);
4399 set_vim_var_nr(VV_MOUSE_WINID, 0);
4400 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4401 set_vim_var_nr(VV_MOUSE_COL, 0);
4402
4403 rettv->vval.v_number = n;
4404 if (IS_SPECIAL(n) || mod_mask != 0)
4405 {
4406 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4407 int i = 0;
4408
4409 /* Turn a special key into three bytes, plus modifier. */
4410 if (mod_mask != 0)
4411 {
4412 temp[i++] = K_SPECIAL;
4413 temp[i++] = KS_MODIFIER;
4414 temp[i++] = mod_mask;
4415 }
4416 if (IS_SPECIAL(n))
4417 {
4418 temp[i++] = K_SPECIAL;
4419 temp[i++] = K_SECOND(n);
4420 temp[i++] = K_THIRD(n);
4421 }
4422#ifdef FEAT_MBYTE
4423 else if (has_mbyte)
4424 i += (*mb_char2bytes)(n, temp + i);
4425#endif
4426 else
4427 temp[i++] = n;
4428 temp[i++] = NUL;
4429 rettv->v_type = VAR_STRING;
4430 rettv->vval.v_string = vim_strsave(temp);
4431
4432#ifdef FEAT_MOUSE
4433 if (is_mouse_key(n))
4434 {
4435 int row = mouse_row;
4436 int col = mouse_col;
4437 win_T *win;
4438 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004439 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004440 int winnr = 1;
4441
4442 if (row >= 0 && col >= 0)
4443 {
4444 /* Find the window at the mouse coordinates and compute the
4445 * text position. */
4446 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004447 if (win == NULL)
4448 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004449 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004450 for (wp = firstwin; wp != win; wp = wp->w_next)
4451 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004452 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4453 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4454 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4455 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4456 }
4457 }
4458#endif
4459 }
4460}
4461
4462/*
4463 * "getcharmod()" function
4464 */
4465 static void
4466f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4467{
4468 rettv->vval.v_number = mod_mask;
4469}
4470
4471/*
4472 * "getcharsearch()" function
4473 */
4474 static void
4475f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4476{
4477 if (rettv_dict_alloc(rettv) != FAIL)
4478 {
4479 dict_T *dict = rettv->vval.v_dict;
4480
4481 dict_add_nr_str(dict, "char", 0L, last_csearch());
4482 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
4483 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
4484 }
4485}
4486
4487/*
4488 * "getcmdline()" function
4489 */
4490 static void
4491f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4492{
4493 rettv->v_type = VAR_STRING;
4494 rettv->vval.v_string = get_cmdline_str();
4495}
4496
4497/*
4498 * "getcmdpos()" function
4499 */
4500 static void
4501f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4502{
4503 rettv->vval.v_number = get_cmdline_pos() + 1;
4504}
4505
4506/*
4507 * "getcmdtype()" function
4508 */
4509 static void
4510f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4511{
4512 rettv->v_type = VAR_STRING;
4513 rettv->vval.v_string = alloc(2);
4514 if (rettv->vval.v_string != NULL)
4515 {
4516 rettv->vval.v_string[0] = get_cmdline_type();
4517 rettv->vval.v_string[1] = NUL;
4518 }
4519}
4520
4521/*
4522 * "getcmdwintype()" function
4523 */
4524 static void
4525f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4526{
4527 rettv->v_type = VAR_STRING;
4528 rettv->vval.v_string = NULL;
4529#ifdef FEAT_CMDWIN
4530 rettv->vval.v_string = alloc(2);
4531 if (rettv->vval.v_string != NULL)
4532 {
4533 rettv->vval.v_string[0] = cmdwin_type;
4534 rettv->vval.v_string[1] = NUL;
4535 }
4536#endif
4537}
4538
4539#if defined(FEAT_CMDL_COMPL)
4540/*
4541 * "getcompletion()" function
4542 */
4543 static void
4544f_getcompletion(typval_T *argvars, typval_T *rettv)
4545{
4546 char_u *pat;
4547 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004548 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004549 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
4550 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004551
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004552 if (argvars[2].v_type != VAR_UNKNOWN)
4553 filtered = get_tv_number_chk(&argvars[2], NULL);
4554
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004555 if (p_wic)
4556 options |= WILD_ICASE;
4557
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004558 /* For filtered results, 'wildignore' is used */
4559 if (!filtered)
4560 options |= WILD_KEEP_ALL;
4561
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004562 ExpandInit(&xpc);
4563 xpc.xp_pattern = get_tv_string(&argvars[0]);
4564 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4565 xpc.xp_context = cmdcomplete_str_to_type(get_tv_string(&argvars[1]));
4566 if (xpc.xp_context == EXPAND_NOTHING)
4567 {
4568 if (argvars[1].v_type == VAR_STRING)
4569 EMSG2(_(e_invarg2), argvars[1].vval.v_string);
4570 else
4571 EMSG(_(e_invarg));
4572 return;
4573 }
4574
4575# if defined(FEAT_MENU)
4576 if (xpc.xp_context == EXPAND_MENUS)
4577 {
4578 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4579 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4580 }
4581# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02004582#ifdef FEAT_CSCOPE
4583 if (xpc.xp_context == EXPAND_CSCOPE)
4584 {
4585 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4586 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4587 }
4588#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02004589#ifdef FEAT_SIGNS
4590 if (xpc.xp_context == EXPAND_SIGN)
4591 {
4592 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4593 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4594 }
4595#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004596
4597 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4598 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
4599 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004600 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004601
4602 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4603
4604 for (i = 0; i < xpc.xp_numfiles; i++)
4605 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4606 }
4607 vim_free(pat);
4608 ExpandCleanup(&xpc);
4609}
4610#endif
4611
4612/*
4613 * "getcwd()" function
4614 */
4615 static void
4616f_getcwd(typval_T *argvars, typval_T *rettv)
4617{
4618 win_T *wp = NULL;
4619 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01004620 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004621
4622 rettv->v_type = VAR_STRING;
4623 rettv->vval.v_string = NULL;
4624
Bram Moolenaar54591292018-02-09 20:53:59 +01004625 if (argvars[0].v_type == VAR_NUMBER && argvars[0].vval.v_number == -1)
4626 global = TRUE;
4627 else
4628 wp = find_tabwin(&argvars[0], &argvars[1]);
4629
4630 if (wp != NULL && wp->w_localdir != NULL)
4631 rettv->vval.v_string = vim_strsave(wp->w_localdir);
4632 else if (wp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004633 {
Bram Moolenaar54591292018-02-09 20:53:59 +01004634 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004635 rettv->vval.v_string = vim_strsave(globaldir);
4636 else
4637 {
4638 cwd = alloc(MAXPATHL);
4639 if (cwd != NULL)
4640 {
4641 if (mch_dirname(cwd, MAXPATHL) != FAIL)
4642 rettv->vval.v_string = vim_strsave(cwd);
4643 vim_free(cwd);
4644 }
4645 }
4646#ifdef BACKSLASH_IN_FILENAME
4647 if (rettv->vval.v_string != NULL)
4648 slash_adjust(rettv->vval.v_string);
4649#endif
4650 }
4651}
4652
4653/*
4654 * "getfontname()" function
4655 */
4656 static void
4657f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4658{
4659 rettv->v_type = VAR_STRING;
4660 rettv->vval.v_string = NULL;
4661#ifdef FEAT_GUI
4662 if (gui.in_use)
4663 {
4664 GuiFont font;
4665 char_u *name = NULL;
4666
4667 if (argvars[0].v_type == VAR_UNKNOWN)
4668 {
4669 /* Get the "Normal" font. Either the name saved by
4670 * hl_set_font_name() or from the font ID. */
4671 font = gui.norm_font;
4672 name = hl_get_font_name();
4673 }
4674 else
4675 {
4676 name = get_tv_string(&argvars[0]);
4677 if (STRCMP(name, "*") == 0) /* don't use font dialog */
4678 return;
4679 font = gui_mch_get_font(name, FALSE);
4680 if (font == NOFONT)
4681 return; /* Invalid font name, return empty string. */
4682 }
4683 rettv->vval.v_string = gui_mch_get_fontname(font, name);
4684 if (argvars[0].v_type != VAR_UNKNOWN)
4685 gui_mch_free_font(font);
4686 }
4687#endif
4688}
4689
4690/*
4691 * "getfperm({fname})" function
4692 */
4693 static void
4694f_getfperm(typval_T *argvars, typval_T *rettv)
4695{
4696 char_u *fname;
4697 stat_T st;
4698 char_u *perm = NULL;
4699 char_u flags[] = "rwx";
4700 int i;
4701
4702 fname = get_tv_string(&argvars[0]);
4703
4704 rettv->v_type = VAR_STRING;
4705 if (mch_stat((char *)fname, &st) >= 0)
4706 {
4707 perm = vim_strsave((char_u *)"---------");
4708 if (perm != NULL)
4709 {
4710 for (i = 0; i < 9; i++)
4711 {
4712 if (st.st_mode & (1 << (8 - i)))
4713 perm[i] = flags[i % 3];
4714 }
4715 }
4716 }
4717 rettv->vval.v_string = perm;
4718}
4719
4720/*
4721 * "getfsize({fname})" function
4722 */
4723 static void
4724f_getfsize(typval_T *argvars, typval_T *rettv)
4725{
4726 char_u *fname;
4727 stat_T st;
4728
4729 fname = get_tv_string(&argvars[0]);
4730
4731 rettv->v_type = VAR_NUMBER;
4732
4733 if (mch_stat((char *)fname, &st) >= 0)
4734 {
4735 if (mch_isdir(fname))
4736 rettv->vval.v_number = 0;
4737 else
4738 {
4739 rettv->vval.v_number = (varnumber_T)st.st_size;
4740
4741 /* non-perfect check for overflow */
4742 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
4743 rettv->vval.v_number = -2;
4744 }
4745 }
4746 else
4747 rettv->vval.v_number = -1;
4748}
4749
4750/*
4751 * "getftime({fname})" function
4752 */
4753 static void
4754f_getftime(typval_T *argvars, typval_T *rettv)
4755{
4756 char_u *fname;
4757 stat_T st;
4758
4759 fname = get_tv_string(&argvars[0]);
4760
4761 if (mch_stat((char *)fname, &st) >= 0)
4762 rettv->vval.v_number = (varnumber_T)st.st_mtime;
4763 else
4764 rettv->vval.v_number = -1;
4765}
4766
4767/*
4768 * "getftype({fname})" function
4769 */
4770 static void
4771f_getftype(typval_T *argvars, typval_T *rettv)
4772{
4773 char_u *fname;
4774 stat_T st;
4775 char_u *type = NULL;
4776 char *t;
4777
4778 fname = get_tv_string(&argvars[0]);
4779
4780 rettv->v_type = VAR_STRING;
4781 if (mch_lstat((char *)fname, &st) >= 0)
4782 {
4783#ifdef S_ISREG
4784 if (S_ISREG(st.st_mode))
4785 t = "file";
4786 else if (S_ISDIR(st.st_mode))
4787 t = "dir";
4788# ifdef S_ISLNK
4789 else if (S_ISLNK(st.st_mode))
4790 t = "link";
4791# endif
4792# ifdef S_ISBLK
4793 else if (S_ISBLK(st.st_mode))
4794 t = "bdev";
4795# endif
4796# ifdef S_ISCHR
4797 else if (S_ISCHR(st.st_mode))
4798 t = "cdev";
4799# endif
4800# ifdef S_ISFIFO
4801 else if (S_ISFIFO(st.st_mode))
4802 t = "fifo";
4803# endif
4804# ifdef S_ISSOCK
4805 else if (S_ISSOCK(st.st_mode))
4806 t = "fifo";
4807# endif
4808 else
4809 t = "other";
4810#else
4811# ifdef S_IFMT
4812 switch (st.st_mode & S_IFMT)
4813 {
4814 case S_IFREG: t = "file"; break;
4815 case S_IFDIR: t = "dir"; break;
4816# ifdef S_IFLNK
4817 case S_IFLNK: t = "link"; break;
4818# endif
4819# ifdef S_IFBLK
4820 case S_IFBLK: t = "bdev"; break;
4821# endif
4822# ifdef S_IFCHR
4823 case S_IFCHR: t = "cdev"; break;
4824# endif
4825# ifdef S_IFIFO
4826 case S_IFIFO: t = "fifo"; break;
4827# endif
4828# ifdef S_IFSOCK
4829 case S_IFSOCK: t = "socket"; break;
4830# endif
4831 default: t = "other";
4832 }
4833# else
4834 if (mch_isdir(fname))
4835 t = "dir";
4836 else
4837 t = "file";
4838# endif
4839#endif
4840 type = vim_strsave((char_u *)t);
4841 }
4842 rettv->vval.v_string = type;
4843}
4844
4845/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01004846 * "getjumplist()" function
4847 */
4848 static void
4849f_getjumplist(typval_T *argvars, typval_T *rettv)
4850{
4851#ifdef FEAT_JUMPLIST
4852 win_T *wp;
4853 int i;
4854 list_T *l;
4855 dict_T *d;
4856#endif
4857
4858 if (rettv_list_alloc(rettv) != OK)
4859 return;
4860
4861#ifdef FEAT_JUMPLIST
4862 wp = find_tabwin(&argvars[0], &argvars[1]);
4863 if (wp == NULL)
4864 return;
4865
4866 l = list_alloc();
4867 if (l == NULL)
4868 return;
4869
4870 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4871 return;
4872 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
4873
4874 for (i = 0; i < wp->w_jumplistlen; ++i)
4875 {
4876 if ((d = dict_alloc()) == NULL)
4877 return;
4878 if (list_append_dict(l, d) == FAIL)
4879 return;
4880 dict_add_nr_str(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum,
4881 NULL);
4882 dict_add_nr_str(d, "col", (long)wp->w_jumplist[i].fmark.mark.col,
4883 NULL);
4884# ifdef FEAT_VIRTUALEDIT
4885 dict_add_nr_str(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd,
4886 NULL);
4887# endif
4888 dict_add_nr_str(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum, NULL);
4889 if (wp->w_jumplist[i].fmark.fnum == 0)
4890 dict_add_nr_str(d, "filename", 0L, wp->w_jumplist[i].fname);
4891 }
4892#endif
4893}
4894
4895/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004896 * "getline(lnum, [end])" function
4897 */
4898 static void
4899f_getline(typval_T *argvars, typval_T *rettv)
4900{
4901 linenr_T lnum;
4902 linenr_T end;
4903 int retlist;
4904
4905 lnum = get_tv_lnum(argvars);
4906 if (argvars[1].v_type == VAR_UNKNOWN)
4907 {
4908 end = 0;
4909 retlist = FALSE;
4910 }
4911 else
4912 {
4913 end = get_tv_lnum(&argvars[1]);
4914 retlist = TRUE;
4915 }
4916
4917 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
4918}
4919
Bram Moolenaar4ae20952016-08-13 15:29:14 +02004920#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02004921 static void
4922get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
4923{
Bram Moolenaard823fa92016-08-12 16:29:27 +02004924 if (what_arg->v_type == VAR_UNKNOWN)
4925 {
4926 if (rettv_list_alloc(rettv) == OK)
4927 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004928 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004929 }
4930 else
4931 {
4932 if (rettv_dict_alloc(rettv) == OK)
4933 if (is_qf || (wp != NULL))
4934 {
4935 if (what_arg->v_type == VAR_DICT)
4936 {
4937 dict_T *d = what_arg->vval.v_dict;
4938
4939 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02004940 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004941 }
4942 else
4943 EMSG(_(e_dictreq));
4944 }
4945 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004946}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02004947#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02004948
4949/*
4950 * "getloclist()" function
4951 */
4952 static void
4953f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4954{
4955#ifdef FEAT_QUICKFIX
4956 win_T *wp;
4957
4958 wp = find_win_by_nr(&argvars[0], NULL);
4959 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
4960#endif
4961}
4962
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004963/*
4964 * "getmatches()" function
4965 */
4966 static void
4967f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4968{
4969#ifdef FEAT_SEARCH_EXTRA
4970 dict_T *dict;
4971 matchitem_T *cur = curwin->w_match_head;
4972 int i;
4973
4974 if (rettv_list_alloc(rettv) == OK)
4975 {
4976 while (cur != NULL)
4977 {
4978 dict = dict_alloc();
4979 if (dict == NULL)
4980 return;
4981 if (cur->match.regprog == NULL)
4982 {
4983 /* match added with matchaddpos() */
4984 for (i = 0; i < MAXPOSMATCH; ++i)
4985 {
4986 llpos_T *llpos;
4987 char buf[6];
4988 list_T *l;
4989
4990 llpos = &cur->pos.pos[i];
4991 if (llpos->lnum == 0)
4992 break;
4993 l = list_alloc();
4994 if (l == NULL)
4995 break;
4996 list_append_number(l, (varnumber_T)llpos->lnum);
4997 if (llpos->col > 0)
4998 {
4999 list_append_number(l, (varnumber_T)llpos->col);
5000 list_append_number(l, (varnumber_T)llpos->len);
5001 }
5002 sprintf(buf, "pos%d", i + 1);
5003 dict_add_list(dict, buf, l);
5004 }
5005 }
5006 else
5007 {
5008 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
5009 }
5010 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
5011 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
5012 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
5013# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
5014 if (cur->conceal_char)
5015 {
5016 char_u buf[MB_MAXBYTES + 1];
5017
5018 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
5019 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
5020 }
5021# endif
5022 list_append_dict(rettv->vval.v_list, dict);
5023 cur = cur->next;
5024 }
5025 }
5026#endif
5027}
5028
5029/*
5030 * "getpid()" function
5031 */
5032 static void
5033f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5034{
5035 rettv->vval.v_number = mch_get_pid();
5036}
5037
5038 static void
5039getpos_both(
5040 typval_T *argvars,
5041 typval_T *rettv,
5042 int getcurpos)
5043{
5044 pos_T *fp;
5045 list_T *l;
5046 int fnum = -1;
5047
5048 if (rettv_list_alloc(rettv) == OK)
5049 {
5050 l = rettv->vval.v_list;
5051 if (getcurpos)
5052 fp = &curwin->w_cursor;
5053 else
5054 fp = var2fpos(&argvars[0], TRUE, &fnum);
5055 if (fnum != -1)
5056 list_append_number(l, (varnumber_T)fnum);
5057 else
5058 list_append_number(l, (varnumber_T)0);
5059 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5060 : (varnumber_T)0);
5061 list_append_number(l, (fp != NULL)
5062 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5063 : (varnumber_T)0);
5064 list_append_number(l,
5065#ifdef FEAT_VIRTUALEDIT
5066 (fp != NULL) ? (varnumber_T)fp->coladd :
5067#endif
5068 (varnumber_T)0);
5069 if (getcurpos)
5070 {
5071 update_curswant();
5072 list_append_number(l, curwin->w_curswant == MAXCOL ?
5073 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
5074 }
5075 }
5076 else
5077 rettv->vval.v_number = FALSE;
5078}
5079
5080
5081/*
5082 * "getcurpos()" function
5083 */
5084 static void
5085f_getcurpos(typval_T *argvars, typval_T *rettv)
5086{
5087 getpos_both(argvars, rettv, TRUE);
5088}
5089
5090/*
5091 * "getpos(string)" function
5092 */
5093 static void
5094f_getpos(typval_T *argvars, typval_T *rettv)
5095{
5096 getpos_both(argvars, rettv, FALSE);
5097}
5098
5099/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005100 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005101 */
5102 static void
5103f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5104{
5105#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005106 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005107#endif
5108}
5109
5110/*
5111 * "getreg()" function
5112 */
5113 static void
5114f_getreg(typval_T *argvars, typval_T *rettv)
5115{
5116 char_u *strregname;
5117 int regname;
5118 int arg2 = FALSE;
5119 int return_list = FALSE;
5120 int error = FALSE;
5121
5122 if (argvars[0].v_type != VAR_UNKNOWN)
5123 {
5124 strregname = get_tv_string_chk(&argvars[0]);
5125 error = strregname == NULL;
5126 if (argvars[1].v_type != VAR_UNKNOWN)
5127 {
5128 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
5129 if (!error && argvars[2].v_type != VAR_UNKNOWN)
5130 return_list = (int)get_tv_number_chk(&argvars[2], &error);
5131 }
5132 }
5133 else
5134 strregname = get_vim_var_str(VV_REG);
5135
5136 if (error)
5137 return;
5138
5139 regname = (strregname == NULL ? '"' : *strregname);
5140 if (regname == 0)
5141 regname = '"';
5142
5143 if (return_list)
5144 {
5145 rettv->v_type = VAR_LIST;
5146 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5147 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5148 if (rettv->vval.v_list == NULL)
5149 (void)rettv_list_alloc(rettv);
5150 else
5151 ++rettv->vval.v_list->lv_refcount;
5152 }
5153 else
5154 {
5155 rettv->v_type = VAR_STRING;
5156 rettv->vval.v_string = get_reg_contents(regname,
5157 arg2 ? GREG_EXPR_SRC : 0);
5158 }
5159}
5160
5161/*
5162 * "getregtype()" function
5163 */
5164 static void
5165f_getregtype(typval_T *argvars, typval_T *rettv)
5166{
5167 char_u *strregname;
5168 int regname;
5169 char_u buf[NUMBUFLEN + 2];
5170 long reglen = 0;
5171
5172 if (argvars[0].v_type != VAR_UNKNOWN)
5173 {
5174 strregname = get_tv_string_chk(&argvars[0]);
5175 if (strregname == NULL) /* type error; errmsg already given */
5176 {
5177 rettv->v_type = VAR_STRING;
5178 rettv->vval.v_string = NULL;
5179 return;
5180 }
5181 }
5182 else
5183 /* Default to v:register */
5184 strregname = get_vim_var_str(VV_REG);
5185
5186 regname = (strregname == NULL ? '"' : *strregname);
5187 if (regname == 0)
5188 regname = '"';
5189
5190 buf[0] = NUL;
5191 buf[1] = NUL;
5192 switch (get_reg_type(regname, &reglen))
5193 {
5194 case MLINE: buf[0] = 'V'; break;
5195 case MCHAR: buf[0] = 'v'; break;
5196 case MBLOCK:
5197 buf[0] = Ctrl_V;
5198 sprintf((char *)buf + 1, "%ld", reglen + 1);
5199 break;
5200 }
5201 rettv->v_type = VAR_STRING;
5202 rettv->vval.v_string = vim_strsave(buf);
5203}
5204
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005205/*
5206 * Returns information (variables, options, etc.) about a tab page
5207 * as a dictionary.
5208 */
5209 static dict_T *
5210get_tabpage_info(tabpage_T *tp, int tp_idx)
5211{
5212 win_T *wp;
5213 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005214 list_T *l;
5215
5216 dict = dict_alloc();
5217 if (dict == NULL)
5218 return NULL;
5219
Bram Moolenaar33928832016-08-18 21:22:04 +02005220 dict_add_nr_str(dict, "tabnr", tp_idx, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005221
5222 l = list_alloc();
5223 if (l != NULL)
5224 {
5225 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5226 wp; wp = wp->w_next)
5227 list_append_number(l, (varnumber_T)wp->w_id);
5228 dict_add_list(dict, "windows", l);
5229 }
5230
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005231 /* Make a reference to tabpage variables */
5232 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005233
5234 return dict;
5235}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005236
5237/*
5238 * "gettabinfo()" function
5239 */
5240 static void
5241f_gettabinfo(typval_T *argvars, typval_T *rettv)
5242{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005243 tabpage_T *tp, *tparg = NULL;
5244 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005245 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005246
5247 if (rettv_list_alloc(rettv) != OK)
5248 return;
5249
5250 if (argvars[0].v_type != VAR_UNKNOWN)
5251 {
5252 /* Information about one tab page */
5253 tparg = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5254 if (tparg == NULL)
5255 return;
5256 }
5257
5258 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005259 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005260 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005261 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005262 if (tparg != NULL && tp != tparg)
5263 continue;
5264 d = get_tabpage_info(tp, tpnr);
5265 if (d != NULL)
5266 list_append_dict(rettv->vval.v_list, d);
5267 if (tparg != NULL)
5268 return;
5269 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005270}
5271
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005272/*
5273 * "gettabvar()" function
5274 */
5275 static void
5276f_gettabvar(typval_T *argvars, typval_T *rettv)
5277{
5278 win_T *oldcurwin;
5279 tabpage_T *tp, *oldtabpage;
5280 dictitem_T *v;
5281 char_u *varname;
5282 int done = FALSE;
5283
5284 rettv->v_type = VAR_STRING;
5285 rettv->vval.v_string = NULL;
5286
5287 varname = get_tv_string_chk(&argvars[1]);
5288 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5289 if (tp != NULL && varname != NULL)
5290 {
5291 /* Set tp to be our tabpage, temporarily. Also set the window to the
5292 * first window in the tabpage, otherwise the window is not valid. */
5293 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005294 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5295 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005296 {
5297 /* look up the variable */
5298 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5299 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5300 if (v != NULL)
5301 {
5302 copy_tv(&v->di_tv, rettv);
5303 done = TRUE;
5304 }
5305 }
5306
5307 /* restore previous notion of curwin */
5308 restore_win(oldcurwin, oldtabpage, TRUE);
5309 }
5310
5311 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5312 copy_tv(&argvars[2], rettv);
5313}
5314
5315/*
5316 * "gettabwinvar()" function
5317 */
5318 static void
5319f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5320{
5321 getwinvar(argvars, rettv, 1);
5322}
5323
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005324/*
5325 * Returns information about a window as a dictionary.
5326 */
5327 static dict_T *
5328get_win_info(win_T *wp, short tpnr, short winnr)
5329{
5330 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005331
5332 dict = dict_alloc();
5333 if (dict == NULL)
5334 return NULL;
5335
Bram Moolenaar33928832016-08-18 21:22:04 +02005336 dict_add_nr_str(dict, "tabnr", tpnr, NULL);
5337 dict_add_nr_str(dict, "winnr", winnr, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005338 dict_add_nr_str(dict, "winid", wp->w_id, NULL);
5339 dict_add_nr_str(dict, "height", wp->w_height, NULL);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005340#ifdef FEAT_MENU
5341 dict_add_nr_str(dict, "winbar", wp->w_winbar_height, NULL);
5342#endif
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005343 dict_add_nr_str(dict, "width", wp->w_width, NULL);
Bram Moolenaar33928832016-08-18 21:22:04 +02005344 dict_add_nr_str(dict, "bufnr", wp->w_buffer->b_fnum, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005345
Bram Moolenaar69905d12017-08-13 18:14:47 +02005346#ifdef FEAT_TERMINAL
5347 dict_add_nr_str(dict, "terminal", bt_terminal(wp->w_buffer), NULL);
5348#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005349#ifdef FEAT_QUICKFIX
5350 dict_add_nr_str(dict, "quickfix", bt_quickfix(wp->w_buffer), NULL);
5351 dict_add_nr_str(dict, "loclist",
5352 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL), NULL);
5353#endif
5354
Bram Moolenaar30567352016-08-27 21:25:44 +02005355 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005356 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005357
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005358 return dict;
5359}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005360
5361/*
5362 * "getwininfo()" function
5363 */
5364 static void
5365f_getwininfo(typval_T *argvars, typval_T *rettv)
5366{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005367 tabpage_T *tp;
5368 win_T *wp = NULL, *wparg = NULL;
5369 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005370 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005371
5372 if (rettv_list_alloc(rettv) != OK)
5373 return;
5374
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005375 if (argvars[0].v_type != VAR_UNKNOWN)
5376 {
5377 wparg = win_id2wp(argvars);
5378 if (wparg == NULL)
5379 return;
5380 }
5381
5382 /* Collect information about either all the windows across all the tab
5383 * pages or one particular window.
5384 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005385 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005386 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005387 tabnr++;
5388 winnr = 0;
5389 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005390 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005391 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005392 if (wparg != NULL && wp != wparg)
5393 continue;
5394 d = get_win_info(wp, tabnr, winnr);
5395 if (d != NULL)
5396 list_append_dict(rettv->vval.v_list, d);
5397 if (wparg != NULL)
5398 /* found information about a specific window */
5399 return;
5400 }
5401 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005402}
5403
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005404/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005405 * "win_findbuf()" function
5406 */
5407 static void
5408f_win_findbuf(typval_T *argvars, typval_T *rettv)
5409{
5410 if (rettv_list_alloc(rettv) != FAIL)
5411 win_findbuf(argvars, rettv->vval.v_list);
5412}
5413
5414/*
5415 * "win_getid()" function
5416 */
5417 static void
5418f_win_getid(typval_T *argvars, typval_T *rettv)
5419{
5420 rettv->vval.v_number = win_getid(argvars);
5421}
5422
5423/*
5424 * "win_gotoid()" function
5425 */
5426 static void
5427f_win_gotoid(typval_T *argvars, typval_T *rettv)
5428{
5429 rettv->vval.v_number = win_gotoid(argvars);
5430}
5431
5432/*
5433 * "win_id2tabwin()" function
5434 */
5435 static void
5436f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5437{
5438 if (rettv_list_alloc(rettv) != FAIL)
5439 win_id2tabwin(argvars, rettv->vval.v_list);
5440}
5441
5442/*
5443 * "win_id2win()" function
5444 */
5445 static void
5446f_win_id2win(typval_T *argvars, typval_T *rettv)
5447{
5448 rettv->vval.v_number = win_id2win(argvars);
5449}
5450
5451/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005452 * "win_screenpos()" function
5453 */
5454 static void
5455f_win_screenpos(typval_T *argvars, typval_T *rettv)
5456{
5457 win_T *wp;
5458
5459 if (rettv_list_alloc(rettv) == FAIL)
5460 return;
5461
5462 wp = find_win_by_nr(&argvars[0], NULL);
5463 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5464 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5465}
5466
5467/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005468 * "getwinposx()" function
5469 */
5470 static void
5471f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5472{
5473 rettv->vval.v_number = -1;
5474#ifdef FEAT_GUI
5475 if (gui.in_use)
5476 {
5477 int x, y;
5478
5479 if (gui_mch_get_winpos(&x, &y) == OK)
5480 rettv->vval.v_number = x;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005481 return;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005482 }
5483#endif
5484#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5485 {
5486 int x, y;
5487
5488 if (term_get_winpos(&x, &y) == OK)
5489 rettv->vval.v_number = x;
5490 }
5491#endif
5492}
5493
5494/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005495 * "getwinposy()" function
5496 */
5497 static void
5498f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5499{
5500 rettv->vval.v_number = -1;
5501#ifdef FEAT_GUI
5502 if (gui.in_use)
5503 {
5504 int x, y;
5505
5506 if (gui_mch_get_winpos(&x, &y) == OK)
5507 rettv->vval.v_number = y;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005508 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005509 }
5510#endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005511#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5512 {
5513 int x, y;
5514
5515 if (term_get_winpos(&x, &y) == OK)
5516 rettv->vval.v_number = y;
5517 }
5518#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005519}
5520
5521/*
5522 * "getwinvar()" function
5523 */
5524 static void
5525f_getwinvar(typval_T *argvars, typval_T *rettv)
5526{
5527 getwinvar(argvars, rettv, 0);
5528}
5529
5530/*
5531 * "glob()" function
5532 */
5533 static void
5534f_glob(typval_T *argvars, typval_T *rettv)
5535{
5536 int options = WILD_SILENT|WILD_USE_NL;
5537 expand_T xpc;
5538 int error = FALSE;
5539
5540 /* When the optional second argument is non-zero, don't remove matches
5541 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5542 rettv->v_type = VAR_STRING;
5543 if (argvars[1].v_type != VAR_UNKNOWN)
5544 {
5545 if (get_tv_number_chk(&argvars[1], &error))
5546 options |= WILD_KEEP_ALL;
5547 if (argvars[2].v_type != VAR_UNKNOWN)
5548 {
5549 if (get_tv_number_chk(&argvars[2], &error))
5550 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005551 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005552 }
5553 if (argvars[3].v_type != VAR_UNKNOWN
5554 && get_tv_number_chk(&argvars[3], &error))
5555 options |= WILD_ALLLINKS;
5556 }
5557 }
5558 if (!error)
5559 {
5560 ExpandInit(&xpc);
5561 xpc.xp_context = EXPAND_FILES;
5562 if (p_wic)
5563 options += WILD_ICASE;
5564 if (rettv->v_type == VAR_STRING)
5565 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
5566 NULL, options, WILD_ALL);
5567 else if (rettv_list_alloc(rettv) != FAIL)
5568 {
5569 int i;
5570
5571 ExpandOne(&xpc, get_tv_string(&argvars[0]),
5572 NULL, options, WILD_ALL_KEEP);
5573 for (i = 0; i < xpc.xp_numfiles; i++)
5574 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5575
5576 ExpandCleanup(&xpc);
5577 }
5578 }
5579 else
5580 rettv->vval.v_string = NULL;
5581}
5582
5583/*
5584 * "globpath()" function
5585 */
5586 static void
5587f_globpath(typval_T *argvars, typval_T *rettv)
5588{
5589 int flags = 0;
5590 char_u buf1[NUMBUFLEN];
5591 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
5592 int error = FALSE;
5593 garray_T ga;
5594 int i;
5595
5596 /* When the optional second argument is non-zero, don't remove matches
5597 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5598 rettv->v_type = VAR_STRING;
5599 if (argvars[2].v_type != VAR_UNKNOWN)
5600 {
5601 if (get_tv_number_chk(&argvars[2], &error))
5602 flags |= WILD_KEEP_ALL;
5603 if (argvars[3].v_type != VAR_UNKNOWN)
5604 {
5605 if (get_tv_number_chk(&argvars[3], &error))
5606 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005607 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005608 }
5609 if (argvars[4].v_type != VAR_UNKNOWN
5610 && get_tv_number_chk(&argvars[4], &error))
5611 flags |= WILD_ALLLINKS;
5612 }
5613 }
5614 if (file != NULL && !error)
5615 {
5616 ga_init2(&ga, (int)sizeof(char_u *), 10);
5617 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
5618 if (rettv->v_type == VAR_STRING)
5619 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
5620 else if (rettv_list_alloc(rettv) != FAIL)
5621 for (i = 0; i < ga.ga_len; ++i)
5622 list_append_string(rettv->vval.v_list,
5623 ((char_u **)(ga.ga_data))[i], -1);
5624 ga_clear_strings(&ga);
5625 }
5626 else
5627 rettv->vval.v_string = NULL;
5628}
5629
5630/*
5631 * "glob2regpat()" function
5632 */
5633 static void
5634f_glob2regpat(typval_T *argvars, typval_T *rettv)
5635{
5636 char_u *pat = get_tv_string_chk(&argvars[0]);
5637
5638 rettv->v_type = VAR_STRING;
5639 rettv->vval.v_string = (pat == NULL)
5640 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
5641}
5642
5643/* for VIM_VERSION_ defines */
5644#include "version.h"
5645
5646/*
5647 * "has()" function
5648 */
5649 static void
5650f_has(typval_T *argvars, typval_T *rettv)
5651{
5652 int i;
5653 char_u *name;
5654 int n = FALSE;
5655 static char *(has_list[]) =
5656 {
5657#ifdef AMIGA
5658 "amiga",
5659# ifdef FEAT_ARP
5660 "arp",
5661# endif
5662#endif
5663#ifdef __BEOS__
5664 "beos",
5665#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02005666#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01005667 "mac", /* Mac OS X (and, once, Mac OS Classic) */
5668 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02005669# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01005670 "macunix", /* Mac OS X, with the darwin feature */
5671 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02005672# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005673#endif
5674#ifdef __QNX__
5675 "qnx",
5676#endif
5677#ifdef UNIX
5678 "unix",
5679#endif
5680#ifdef VMS
5681 "vms",
5682#endif
5683#ifdef WIN32
5684 "win32",
5685#endif
5686#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
5687 "win32unix",
5688#endif
5689#if defined(WIN64) || defined(_WIN64)
5690 "win64",
5691#endif
5692#ifdef EBCDIC
5693 "ebcdic",
5694#endif
5695#ifndef CASE_INSENSITIVE_FILENAME
5696 "fname_case",
5697#endif
5698#ifdef HAVE_ACL
5699 "acl",
5700#endif
5701#ifdef FEAT_ARABIC
5702 "arabic",
5703#endif
5704#ifdef FEAT_AUTOCMD
5705 "autocmd",
5706#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01005707#ifdef FEAT_AUTOSERVERNAME
5708 "autoservername",
5709#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01005710#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005711 "balloon_eval",
5712# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
5713 "balloon_multiline",
5714# endif
5715#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01005716#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01005717 "balloon_eval_term",
5718#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005719#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
5720 "builtin_terms",
5721# ifdef ALL_BUILTIN_TCAPS
5722 "all_builtin_terms",
5723# endif
5724#endif
5725#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
5726 || defined(FEAT_GUI_W32) \
5727 || defined(FEAT_GUI_MOTIF))
5728 "browsefilter",
5729#endif
5730#ifdef FEAT_BYTEOFF
5731 "byte_offset",
5732#endif
5733#ifdef FEAT_JOB_CHANNEL
5734 "channel",
5735#endif
5736#ifdef FEAT_CINDENT
5737 "cindent",
5738#endif
5739#ifdef FEAT_CLIENTSERVER
5740 "clientserver",
5741#endif
5742#ifdef FEAT_CLIPBOARD
5743 "clipboard",
5744#endif
5745#ifdef FEAT_CMDL_COMPL
5746 "cmdline_compl",
5747#endif
5748#ifdef FEAT_CMDHIST
5749 "cmdline_hist",
5750#endif
5751#ifdef FEAT_COMMENTS
5752 "comments",
5753#endif
5754#ifdef FEAT_CONCEAL
5755 "conceal",
5756#endif
5757#ifdef FEAT_CRYPT
5758 "cryptv",
5759 "crypt-blowfish",
5760 "crypt-blowfish2",
5761#endif
5762#ifdef FEAT_CSCOPE
5763 "cscope",
5764#endif
5765#ifdef FEAT_CURSORBIND
5766 "cursorbind",
5767#endif
5768#ifdef CURSOR_SHAPE
5769 "cursorshape",
5770#endif
5771#ifdef DEBUG
5772 "debug",
5773#endif
5774#ifdef FEAT_CON_DIALOG
5775 "dialog_con",
5776#endif
5777#ifdef FEAT_GUI_DIALOG
5778 "dialog_gui",
5779#endif
5780#ifdef FEAT_DIFF
5781 "diff",
5782#endif
5783#ifdef FEAT_DIGRAPHS
5784 "digraphs",
5785#endif
5786#ifdef FEAT_DIRECTX
5787 "directx",
5788#endif
5789#ifdef FEAT_DND
5790 "dnd",
5791#endif
5792#ifdef FEAT_EMACS_TAGS
5793 "emacs_tags",
5794#endif
5795 "eval", /* always present, of course! */
5796 "ex_extra", /* graduated feature */
5797#ifdef FEAT_SEARCH_EXTRA
5798 "extra_search",
5799#endif
5800#ifdef FEAT_FKMAP
5801 "farsi",
5802#endif
5803#ifdef FEAT_SEARCHPATH
5804 "file_in_path",
5805#endif
5806#ifdef FEAT_FILTERPIPE
5807 "filterpipe",
5808#endif
5809#ifdef FEAT_FIND_ID
5810 "find_in_path",
5811#endif
5812#ifdef FEAT_FLOAT
5813 "float",
5814#endif
5815#ifdef FEAT_FOLDING
5816 "folding",
5817#endif
5818#ifdef FEAT_FOOTER
5819 "footer",
5820#endif
5821#if !defined(USE_SYSTEM) && defined(UNIX)
5822 "fork",
5823#endif
5824#ifdef FEAT_GETTEXT
5825 "gettext",
5826#endif
5827#ifdef FEAT_GUI
5828 "gui",
5829#endif
5830#ifdef FEAT_GUI_ATHENA
5831# ifdef FEAT_GUI_NEXTAW
5832 "gui_neXtaw",
5833# else
5834 "gui_athena",
5835# endif
5836#endif
5837#ifdef FEAT_GUI_GTK
5838 "gui_gtk",
5839# ifdef USE_GTK3
5840 "gui_gtk3",
5841# else
5842 "gui_gtk2",
5843# endif
5844#endif
5845#ifdef FEAT_GUI_GNOME
5846 "gui_gnome",
5847#endif
5848#ifdef FEAT_GUI_MAC
5849 "gui_mac",
5850#endif
5851#ifdef FEAT_GUI_MOTIF
5852 "gui_motif",
5853#endif
5854#ifdef FEAT_GUI_PHOTON
5855 "gui_photon",
5856#endif
5857#ifdef FEAT_GUI_W32
5858 "gui_win32",
5859#endif
5860#ifdef FEAT_HANGULIN
5861 "hangul_input",
5862#endif
5863#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
5864 "iconv",
5865#endif
5866#ifdef FEAT_INS_EXPAND
5867 "insert_expand",
5868#endif
5869#ifdef FEAT_JOB_CHANNEL
5870 "job",
5871#endif
5872#ifdef FEAT_JUMPLIST
5873 "jumplist",
5874#endif
5875#ifdef FEAT_KEYMAP
5876 "keymap",
5877#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02005878 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005879#ifdef FEAT_LANGMAP
5880 "langmap",
5881#endif
5882#ifdef FEAT_LIBCALL
5883 "libcall",
5884#endif
5885#ifdef FEAT_LINEBREAK
5886 "linebreak",
5887#endif
5888#ifdef FEAT_LISP
5889 "lispindent",
5890#endif
5891#ifdef FEAT_LISTCMDS
5892 "listcmds",
5893#endif
5894#ifdef FEAT_LOCALMAP
5895 "localmap",
5896#endif
5897#ifdef FEAT_LUA
5898# ifndef DYNAMIC_LUA
5899 "lua",
5900# endif
5901#endif
5902#ifdef FEAT_MENU
5903 "menu",
5904#endif
5905#ifdef FEAT_SESSION
5906 "mksession",
5907#endif
5908#ifdef FEAT_MODIFY_FNAME
5909 "modify_fname",
5910#endif
5911#ifdef FEAT_MOUSE
5912 "mouse",
5913#endif
5914#ifdef FEAT_MOUSESHAPE
5915 "mouseshape",
5916#endif
5917#if defined(UNIX) || defined(VMS)
5918# ifdef FEAT_MOUSE_DEC
5919 "mouse_dec",
5920# endif
5921# ifdef FEAT_MOUSE_GPM
5922 "mouse_gpm",
5923# endif
5924# ifdef FEAT_MOUSE_JSB
5925 "mouse_jsbterm",
5926# endif
5927# ifdef FEAT_MOUSE_NET
5928 "mouse_netterm",
5929# endif
5930# ifdef FEAT_MOUSE_PTERM
5931 "mouse_pterm",
5932# endif
5933# ifdef FEAT_MOUSE_SGR
5934 "mouse_sgr",
5935# endif
5936# ifdef FEAT_SYSMOUSE
5937 "mouse_sysmouse",
5938# endif
5939# ifdef FEAT_MOUSE_URXVT
5940 "mouse_urxvt",
5941# endif
5942# ifdef FEAT_MOUSE_XTERM
5943 "mouse_xterm",
5944# endif
5945#endif
5946#ifdef FEAT_MBYTE
5947 "multi_byte",
5948#endif
5949#ifdef FEAT_MBYTE_IME
5950 "multi_byte_ime",
5951#endif
5952#ifdef FEAT_MULTI_LANG
5953 "multi_lang",
5954#endif
5955#ifdef FEAT_MZSCHEME
5956#ifndef DYNAMIC_MZSCHEME
5957 "mzscheme",
5958#endif
5959#endif
5960#ifdef FEAT_NUM64
5961 "num64",
5962#endif
5963#ifdef FEAT_OLE
5964 "ole",
5965#endif
5966 "packages",
5967#ifdef FEAT_PATH_EXTRA
5968 "path_extra",
5969#endif
5970#ifdef FEAT_PERL
5971#ifndef DYNAMIC_PERL
5972 "perl",
5973#endif
5974#endif
5975#ifdef FEAT_PERSISTENT_UNDO
5976 "persistent_undo",
5977#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01005978#if defined(FEAT_PYTHON)
5979 "python_compiled",
5980# if defined(DYNAMIC_PYTHON)
5981 "python_dynamic",
5982# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005983 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005984 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01005985# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005986#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01005987#if defined(FEAT_PYTHON3)
5988 "python3_compiled",
5989# if defined(DYNAMIC_PYTHON3)
5990 "python3_dynamic",
5991# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005992 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005993 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01005994# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005995#endif
5996#ifdef FEAT_POSTSCRIPT
5997 "postscript",
5998#endif
5999#ifdef FEAT_PRINTER
6000 "printer",
6001#endif
6002#ifdef FEAT_PROFILE
6003 "profile",
6004#endif
6005#ifdef FEAT_RELTIME
6006 "reltime",
6007#endif
6008#ifdef FEAT_QUICKFIX
6009 "quickfix",
6010#endif
6011#ifdef FEAT_RIGHTLEFT
6012 "rightleft",
6013#endif
6014#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6015 "ruby",
6016#endif
6017#ifdef FEAT_SCROLLBIND
6018 "scrollbind",
6019#endif
6020#ifdef FEAT_CMDL_INFO
6021 "showcmd",
6022 "cmdline_info",
6023#endif
6024#ifdef FEAT_SIGNS
6025 "signs",
6026#endif
6027#ifdef FEAT_SMARTINDENT
6028 "smartindent",
6029#endif
6030#ifdef STARTUPTIME
6031 "startuptime",
6032#endif
6033#ifdef FEAT_STL_OPT
6034 "statusline",
6035#endif
6036#ifdef FEAT_SUN_WORKSHOP
6037 "sun_workshop",
6038#endif
6039#ifdef FEAT_NETBEANS_INTG
6040 "netbeans_intg",
6041#endif
6042#ifdef FEAT_SPELL
6043 "spell",
6044#endif
6045#ifdef FEAT_SYN_HL
6046 "syntax",
6047#endif
6048#if defined(USE_SYSTEM) || !defined(UNIX)
6049 "system",
6050#endif
6051#ifdef FEAT_TAG_BINS
6052 "tag_binary",
6053#endif
6054#ifdef FEAT_TAG_OLDSTATIC
6055 "tag_old_static",
6056#endif
6057#ifdef FEAT_TAG_ANYWHITE
6058 "tag_any_white",
6059#endif
6060#ifdef FEAT_TCL
6061# ifndef DYNAMIC_TCL
6062 "tcl",
6063# endif
6064#endif
6065#ifdef FEAT_TERMGUICOLORS
6066 "termguicolors",
6067#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006068#if defined(FEAT_TERMINAL) && !defined(WIN3264)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006069 "terminal",
6070#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006071#ifdef TERMINFO
6072 "terminfo",
6073#endif
6074#ifdef FEAT_TERMRESPONSE
6075 "termresponse",
6076#endif
6077#ifdef FEAT_TEXTOBJ
6078 "textobjects",
6079#endif
6080#ifdef HAVE_TGETENT
6081 "tgetent",
6082#endif
6083#ifdef FEAT_TIMERS
6084 "timers",
6085#endif
6086#ifdef FEAT_TITLE
6087 "title",
6088#endif
6089#ifdef FEAT_TOOLBAR
6090 "toolbar",
6091#endif
6092#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6093 "unnamedplus",
6094#endif
6095#ifdef FEAT_USR_CMDS
6096 "user-commands", /* was accidentally included in 5.4 */
6097 "user_commands",
6098#endif
6099#ifdef FEAT_VIMINFO
6100 "viminfo",
6101#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006102 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006103#ifdef FEAT_VIRTUALEDIT
6104 "virtualedit",
6105#endif
6106 "visual",
6107#ifdef FEAT_VISUALEXTRA
6108 "visualextra",
6109#endif
6110#ifdef FEAT_VREPLACE
6111 "vreplace",
6112#endif
6113#ifdef FEAT_WILDIGN
6114 "wildignore",
6115#endif
6116#ifdef FEAT_WILDMENU
6117 "wildmenu",
6118#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006119 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006120#ifdef FEAT_WAK
6121 "winaltkeys",
6122#endif
6123#ifdef FEAT_WRITEBACKUP
6124 "writebackup",
6125#endif
6126#ifdef FEAT_XIM
6127 "xim",
6128#endif
6129#ifdef FEAT_XFONTSET
6130 "xfontset",
6131#endif
6132#ifdef FEAT_XPM_W32
6133 "xpm",
6134 "xpm_w32", /* for backward compatibility */
6135#else
6136# if defined(HAVE_XPM)
6137 "xpm",
6138# endif
6139#endif
6140#ifdef USE_XSMP
6141 "xsmp",
6142#endif
6143#ifdef USE_XSMP_INTERACT
6144 "xsmp_interact",
6145#endif
6146#ifdef FEAT_XCLIPBOARD
6147 "xterm_clipboard",
6148#endif
6149#ifdef FEAT_XTERM_SAVE
6150 "xterm_save",
6151#endif
6152#if defined(UNIX) && defined(FEAT_X11)
6153 "X11",
6154#endif
6155 NULL
6156 };
6157
6158 name = get_tv_string(&argvars[0]);
6159 for (i = 0; has_list[i] != NULL; ++i)
6160 if (STRICMP(name, has_list[i]) == 0)
6161 {
6162 n = TRUE;
6163 break;
6164 }
6165
6166 if (n == FALSE)
6167 {
6168 if (STRNICMP(name, "patch", 5) == 0)
6169 {
6170 if (name[5] == '-'
6171 && STRLEN(name) >= 11
6172 && vim_isdigit(name[6])
6173 && vim_isdigit(name[8])
6174 && vim_isdigit(name[10]))
6175 {
6176 int major = atoi((char *)name + 6);
6177 int minor = atoi((char *)name + 8);
6178
6179 /* Expect "patch-9.9.01234". */
6180 n = (major < VIM_VERSION_MAJOR
6181 || (major == VIM_VERSION_MAJOR
6182 && (minor < VIM_VERSION_MINOR
6183 || (minor == VIM_VERSION_MINOR
6184 && has_patch(atoi((char *)name + 10))))));
6185 }
6186 else
6187 n = has_patch(atoi((char *)name + 5));
6188 }
6189 else if (STRICMP(name, "vim_starting") == 0)
6190 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006191 else if (STRICMP(name, "ttyin") == 0)
6192 n = mch_input_isatty();
6193 else if (STRICMP(name, "ttyout") == 0)
6194 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006195#ifdef FEAT_MBYTE
6196 else if (STRICMP(name, "multi_byte_encoding") == 0)
6197 n = has_mbyte;
6198#endif
6199#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
6200 else if (STRICMP(name, "balloon_multiline") == 0)
6201 n = multiline_balloon_available();
6202#endif
6203#ifdef DYNAMIC_TCL
6204 else if (STRICMP(name, "tcl") == 0)
6205 n = tcl_enabled(FALSE);
6206#endif
6207#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6208 else if (STRICMP(name, "iconv") == 0)
6209 n = iconv_enabled(FALSE);
6210#endif
6211#ifdef DYNAMIC_LUA
6212 else if (STRICMP(name, "lua") == 0)
6213 n = lua_enabled(FALSE);
6214#endif
6215#ifdef DYNAMIC_MZSCHEME
6216 else if (STRICMP(name, "mzscheme") == 0)
6217 n = mzscheme_enabled(FALSE);
6218#endif
6219#ifdef DYNAMIC_RUBY
6220 else if (STRICMP(name, "ruby") == 0)
6221 n = ruby_enabled(FALSE);
6222#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006223#ifdef DYNAMIC_PYTHON
6224 else if (STRICMP(name, "python") == 0)
6225 n = python_enabled(FALSE);
6226#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006227#ifdef DYNAMIC_PYTHON3
6228 else if (STRICMP(name, "python3") == 0)
6229 n = python3_enabled(FALSE);
6230#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006231#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6232 else if (STRICMP(name, "pythonx") == 0)
6233 {
6234# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6235 if (p_pyx == 0)
6236 n = python3_enabled(FALSE) || python_enabled(FALSE);
6237 else if (p_pyx == 3)
6238 n = python3_enabled(FALSE);
6239 else if (p_pyx == 2)
6240 n = python_enabled(FALSE);
6241# elif defined(DYNAMIC_PYTHON)
6242 n = python_enabled(FALSE);
6243# elif defined(DYNAMIC_PYTHON3)
6244 n = python3_enabled(FALSE);
6245# endif
6246 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006247#endif
6248#ifdef DYNAMIC_PERL
6249 else if (STRICMP(name, "perl") == 0)
6250 n = perl_enabled(FALSE);
6251#endif
6252#ifdef FEAT_GUI
6253 else if (STRICMP(name, "gui_running") == 0)
6254 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006255# ifdef FEAT_BROWSE
6256 else if (STRICMP(name, "browse") == 0)
6257 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6258# endif
6259#endif
6260#ifdef FEAT_SYN_HL
6261 else if (STRICMP(name, "syntax_items") == 0)
6262 n = syntax_present(curwin);
6263#endif
6264#if defined(WIN3264)
6265 else if (STRICMP(name, "win95") == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006266 n = FALSE; /* Win9x is no more supported. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006267#endif
6268#ifdef FEAT_NETBEANS_INTG
6269 else if (STRICMP(name, "netbeans_enabled") == 0)
6270 n = netbeans_active();
6271#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006272#if defined(FEAT_TERMINAL) && defined(WIN3264)
6273 else if (STRICMP(name, "terminal") == 0)
6274 n = terminal_enabled();
6275#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006276 }
6277
6278 rettv->vval.v_number = n;
6279}
6280
6281/*
6282 * "has_key()" function
6283 */
6284 static void
6285f_has_key(typval_T *argvars, typval_T *rettv)
6286{
6287 if (argvars[0].v_type != VAR_DICT)
6288 {
6289 EMSG(_(e_dictreq));
6290 return;
6291 }
6292 if (argvars[0].vval.v_dict == NULL)
6293 return;
6294
6295 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
6296 get_tv_string(&argvars[1]), -1) != NULL;
6297}
6298
6299/*
6300 * "haslocaldir()" function
6301 */
6302 static void
6303f_haslocaldir(typval_T *argvars, typval_T *rettv)
6304{
6305 win_T *wp = NULL;
6306
6307 wp = find_tabwin(&argvars[0], &argvars[1]);
6308 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6309}
6310
6311/*
6312 * "hasmapto()" function
6313 */
6314 static void
6315f_hasmapto(typval_T *argvars, typval_T *rettv)
6316{
6317 char_u *name;
6318 char_u *mode;
6319 char_u buf[NUMBUFLEN];
6320 int abbr = FALSE;
6321
6322 name = get_tv_string(&argvars[0]);
6323 if (argvars[1].v_type == VAR_UNKNOWN)
6324 mode = (char_u *)"nvo";
6325 else
6326 {
6327 mode = get_tv_string_buf(&argvars[1], buf);
6328 if (argvars[2].v_type != VAR_UNKNOWN)
6329 abbr = (int)get_tv_number(&argvars[2]);
6330 }
6331
6332 if (map_to_exists(name, mode, abbr))
6333 rettv->vval.v_number = TRUE;
6334 else
6335 rettv->vval.v_number = FALSE;
6336}
6337
6338/*
6339 * "histadd()" function
6340 */
6341 static void
6342f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6343{
6344#ifdef FEAT_CMDHIST
6345 int histype;
6346 char_u *str;
6347 char_u buf[NUMBUFLEN];
6348#endif
6349
6350 rettv->vval.v_number = FALSE;
6351 if (check_restricted() || check_secure())
6352 return;
6353#ifdef FEAT_CMDHIST
6354 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6355 histype = str != NULL ? get_histtype(str) : -1;
6356 if (histype >= 0)
6357 {
6358 str = get_tv_string_buf(&argvars[1], buf);
6359 if (*str != NUL)
6360 {
6361 init_history();
6362 add_to_history(histype, str, FALSE, NUL);
6363 rettv->vval.v_number = TRUE;
6364 return;
6365 }
6366 }
6367#endif
6368}
6369
6370/*
6371 * "histdel()" function
6372 */
6373 static void
6374f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6375{
6376#ifdef FEAT_CMDHIST
6377 int n;
6378 char_u buf[NUMBUFLEN];
6379 char_u *str;
6380
6381 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6382 if (str == NULL)
6383 n = 0;
6384 else if (argvars[1].v_type == VAR_UNKNOWN)
6385 /* only one argument: clear entire history */
6386 n = clr_history(get_histtype(str));
6387 else if (argvars[1].v_type == VAR_NUMBER)
6388 /* index given: remove that entry */
6389 n = del_history_idx(get_histtype(str),
6390 (int)get_tv_number(&argvars[1]));
6391 else
6392 /* string given: remove all matching entries */
6393 n = del_history_entry(get_histtype(str),
6394 get_tv_string_buf(&argvars[1], buf));
6395 rettv->vval.v_number = n;
6396#endif
6397}
6398
6399/*
6400 * "histget()" function
6401 */
6402 static void
6403f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6404{
6405#ifdef FEAT_CMDHIST
6406 int type;
6407 int idx;
6408 char_u *str;
6409
6410 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6411 if (str == NULL)
6412 rettv->vval.v_string = NULL;
6413 else
6414 {
6415 type = get_histtype(str);
6416 if (argvars[1].v_type == VAR_UNKNOWN)
6417 idx = get_history_idx(type);
6418 else
6419 idx = (int)get_tv_number_chk(&argvars[1], NULL);
6420 /* -1 on type error */
6421 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6422 }
6423#else
6424 rettv->vval.v_string = NULL;
6425#endif
6426 rettv->v_type = VAR_STRING;
6427}
6428
6429/*
6430 * "histnr()" function
6431 */
6432 static void
6433f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6434{
6435 int i;
6436
6437#ifdef FEAT_CMDHIST
6438 char_u *history = get_tv_string_chk(&argvars[0]);
6439
6440 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6441 if (i >= HIST_CMD && i < HIST_COUNT)
6442 i = get_history_idx(i);
6443 else
6444#endif
6445 i = -1;
6446 rettv->vval.v_number = i;
6447}
6448
6449/*
6450 * "highlightID(name)" function
6451 */
6452 static void
6453f_hlID(typval_T *argvars, typval_T *rettv)
6454{
6455 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
6456}
6457
6458/*
6459 * "highlight_exists()" function
6460 */
6461 static void
6462f_hlexists(typval_T *argvars, typval_T *rettv)
6463{
6464 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
6465}
6466
6467/*
6468 * "hostname()" function
6469 */
6470 static void
6471f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6472{
6473 char_u hostname[256];
6474
6475 mch_get_host_name(hostname, 256);
6476 rettv->v_type = VAR_STRING;
6477 rettv->vval.v_string = vim_strsave(hostname);
6478}
6479
6480/*
6481 * iconv() function
6482 */
6483 static void
6484f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6485{
6486#ifdef FEAT_MBYTE
6487 char_u buf1[NUMBUFLEN];
6488 char_u buf2[NUMBUFLEN];
6489 char_u *from, *to, *str;
6490 vimconv_T vimconv;
6491#endif
6492
6493 rettv->v_type = VAR_STRING;
6494 rettv->vval.v_string = NULL;
6495
6496#ifdef FEAT_MBYTE
6497 str = get_tv_string(&argvars[0]);
6498 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
6499 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
6500 vimconv.vc_type = CONV_NONE;
6501 convert_setup(&vimconv, from, to);
6502
6503 /* If the encodings are equal, no conversion needed. */
6504 if (vimconv.vc_type == CONV_NONE)
6505 rettv->vval.v_string = vim_strsave(str);
6506 else
6507 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6508
6509 convert_setup(&vimconv, NULL, NULL);
6510 vim_free(from);
6511 vim_free(to);
6512#endif
6513}
6514
6515/*
6516 * "indent()" function
6517 */
6518 static void
6519f_indent(typval_T *argvars, typval_T *rettv)
6520{
6521 linenr_T lnum;
6522
6523 lnum = get_tv_lnum(argvars);
6524 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6525 rettv->vval.v_number = get_indent_lnum(lnum);
6526 else
6527 rettv->vval.v_number = -1;
6528}
6529
6530/*
6531 * "index()" function
6532 */
6533 static void
6534f_index(typval_T *argvars, typval_T *rettv)
6535{
6536 list_T *l;
6537 listitem_T *item;
6538 long idx = 0;
6539 int ic = FALSE;
6540
6541 rettv->vval.v_number = -1;
6542 if (argvars[0].v_type != VAR_LIST)
6543 {
6544 EMSG(_(e_listreq));
6545 return;
6546 }
6547 l = argvars[0].vval.v_list;
6548 if (l != NULL)
6549 {
6550 item = l->lv_first;
6551 if (argvars[2].v_type != VAR_UNKNOWN)
6552 {
6553 int error = FALSE;
6554
6555 /* Start at specified item. Use the cached index that list_find()
6556 * sets, so that a negative number also works. */
6557 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
6558 idx = l->lv_idx;
6559 if (argvars[3].v_type != VAR_UNKNOWN)
6560 ic = (int)get_tv_number_chk(&argvars[3], &error);
6561 if (error)
6562 item = NULL;
6563 }
6564
6565 for ( ; item != NULL; item = item->li_next, ++idx)
6566 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6567 {
6568 rettv->vval.v_number = idx;
6569 break;
6570 }
6571 }
6572}
6573
6574static int inputsecret_flag = 0;
6575
6576/*
6577 * "input()" function
6578 * Also handles inputsecret() when inputsecret is set.
6579 */
6580 static void
6581f_input(typval_T *argvars, typval_T *rettv)
6582{
6583 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6584}
6585
6586/*
6587 * "inputdialog()" function
6588 */
6589 static void
6590f_inputdialog(typval_T *argvars, typval_T *rettv)
6591{
6592#if defined(FEAT_GUI_TEXTDIALOG)
6593 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
6594 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6595 {
6596 char_u *message;
6597 char_u buf[NUMBUFLEN];
6598 char_u *defstr = (char_u *)"";
6599
6600 message = get_tv_string_chk(&argvars[0]);
6601 if (argvars[1].v_type != VAR_UNKNOWN
6602 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
6603 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6604 else
6605 IObuff[0] = NUL;
6606 if (message != NULL && defstr != NULL
6607 && do_dialog(VIM_QUESTION, NULL, message,
6608 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6609 rettv->vval.v_string = vim_strsave(IObuff);
6610 else
6611 {
6612 if (message != NULL && defstr != NULL
6613 && argvars[1].v_type != VAR_UNKNOWN
6614 && argvars[2].v_type != VAR_UNKNOWN)
6615 rettv->vval.v_string = vim_strsave(
6616 get_tv_string_buf(&argvars[2], buf));
6617 else
6618 rettv->vval.v_string = NULL;
6619 }
6620 rettv->v_type = VAR_STRING;
6621 }
6622 else
6623#endif
6624 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6625}
6626
6627/*
6628 * "inputlist()" function
6629 */
6630 static void
6631f_inputlist(typval_T *argvars, typval_T *rettv)
6632{
6633 listitem_T *li;
6634 int selected;
6635 int mouse_used;
6636
6637#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02006638 /* While starting up, there is no place to enter text. When running tests
6639 * with --not-a-term we assume feedkeys() will be used. */
6640 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006641 return;
6642#endif
6643 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6644 {
6645 EMSG2(_(e_listarg), "inputlist()");
6646 return;
6647 }
6648
6649 msg_start();
6650 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
6651 lines_left = Rows; /* avoid more prompt */
6652 msg_scroll = TRUE;
6653 msg_clr_eos();
6654
6655 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
6656 {
6657 msg_puts(get_tv_string(&li->li_tv));
6658 msg_putchar('\n');
6659 }
6660
6661 /* Ask for choice. */
6662 selected = prompt_for_number(&mouse_used);
6663 if (mouse_used)
6664 selected -= lines_left;
6665
6666 rettv->vval.v_number = selected;
6667}
6668
6669
6670static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6671
6672/*
6673 * "inputrestore()" function
6674 */
6675 static void
6676f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6677{
6678 if (ga_userinput.ga_len > 0)
6679 {
6680 --ga_userinput.ga_len;
6681 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
6682 + ga_userinput.ga_len);
6683 /* default return is zero == OK */
6684 }
6685 else if (p_verbose > 1)
6686 {
6687 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
6688 rettv->vval.v_number = 1; /* Failed */
6689 }
6690}
6691
6692/*
6693 * "inputsave()" function
6694 */
6695 static void
6696f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
6697{
6698 /* Add an entry to the stack of typeahead storage. */
6699 if (ga_grow(&ga_userinput, 1) == OK)
6700 {
6701 save_typeahead((tasave_T *)(ga_userinput.ga_data)
6702 + ga_userinput.ga_len);
6703 ++ga_userinput.ga_len;
6704 /* default return is zero == OK */
6705 }
6706 else
6707 rettv->vval.v_number = 1; /* Failed */
6708}
6709
6710/*
6711 * "inputsecret()" function
6712 */
6713 static void
6714f_inputsecret(typval_T *argvars, typval_T *rettv)
6715{
6716 ++cmdline_star;
6717 ++inputsecret_flag;
6718 f_input(argvars, rettv);
6719 --cmdline_star;
6720 --inputsecret_flag;
6721}
6722
6723/*
6724 * "insert()" function
6725 */
6726 static void
6727f_insert(typval_T *argvars, typval_T *rettv)
6728{
6729 long before = 0;
6730 listitem_T *item;
6731 list_T *l;
6732 int error = FALSE;
6733
6734 if (argvars[0].v_type != VAR_LIST)
6735 EMSG2(_(e_listarg), "insert()");
6736 else if ((l = argvars[0].vval.v_list) != NULL
6737 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
6738 {
6739 if (argvars[2].v_type != VAR_UNKNOWN)
6740 before = (long)get_tv_number_chk(&argvars[2], &error);
6741 if (error)
6742 return; /* type error; errmsg already given */
6743
6744 if (before == l->lv_len)
6745 item = NULL;
6746 else
6747 {
6748 item = list_find(l, before);
6749 if (item == NULL)
6750 {
6751 EMSGN(_(e_listidx), before);
6752 l = NULL;
6753 }
6754 }
6755 if (l != NULL)
6756 {
6757 list_insert_tv(l, &argvars[1], item);
6758 copy_tv(&argvars[0], rettv);
6759 }
6760 }
6761}
6762
6763/*
6764 * "invert(expr)" function
6765 */
6766 static void
6767f_invert(typval_T *argvars, typval_T *rettv)
6768{
6769 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
6770}
6771
6772/*
6773 * "isdirectory()" function
6774 */
6775 static void
6776f_isdirectory(typval_T *argvars, typval_T *rettv)
6777{
6778 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
6779}
6780
6781/*
6782 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
6783 * or it refers to a List or Dictionary that is locked.
6784 */
6785 static int
6786tv_islocked(typval_T *tv)
6787{
6788 return (tv->v_lock & VAR_LOCKED)
6789 || (tv->v_type == VAR_LIST
6790 && tv->vval.v_list != NULL
6791 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
6792 || (tv->v_type == VAR_DICT
6793 && tv->vval.v_dict != NULL
6794 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
6795}
6796
6797/*
6798 * "islocked()" function
6799 */
6800 static void
6801f_islocked(typval_T *argvars, typval_T *rettv)
6802{
6803 lval_T lv;
6804 char_u *end;
6805 dictitem_T *di;
6806
6807 rettv->vval.v_number = -1;
6808 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01006809 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006810 if (end != NULL && lv.ll_name != NULL)
6811 {
6812 if (*end != NUL)
6813 EMSG(_(e_trailing));
6814 else
6815 {
6816 if (lv.ll_tv == NULL)
6817 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006818 di = find_var(lv.ll_name, NULL, TRUE);
6819 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006820 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006821 /* Consider a variable locked when:
6822 * 1. the variable itself is locked
6823 * 2. the value of the variable is locked.
6824 * 3. the List or Dict value is locked.
6825 */
6826 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
6827 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006828 }
6829 }
6830 else if (lv.ll_range)
6831 EMSG(_("E786: Range not allowed"));
6832 else if (lv.ll_newkey != NULL)
6833 EMSG2(_(e_dictkey), lv.ll_newkey);
6834 else if (lv.ll_list != NULL)
6835 /* List item. */
6836 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
6837 else
6838 /* Dictionary item. */
6839 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
6840 }
6841 }
6842
6843 clear_lval(&lv);
6844}
6845
6846#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
6847/*
6848 * "isnan()" function
6849 */
6850 static void
6851f_isnan(typval_T *argvars, typval_T *rettv)
6852{
6853 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
6854 && isnan(argvars[0].vval.v_float);
6855}
6856#endif
6857
6858/*
6859 * "items(dict)" function
6860 */
6861 static void
6862f_items(typval_T *argvars, typval_T *rettv)
6863{
6864 dict_list(argvars, rettv, 2);
6865}
6866
6867#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
6868/*
6869 * Get the job from the argument.
6870 * Returns NULL if the job is invalid.
6871 */
6872 static job_T *
6873get_job_arg(typval_T *tv)
6874{
6875 job_T *job;
6876
6877 if (tv->v_type != VAR_JOB)
6878 {
6879 EMSG2(_(e_invarg2), get_tv_string(tv));
6880 return NULL;
6881 }
6882 job = tv->vval.v_job;
6883
6884 if (job == NULL)
6885 EMSG(_("E916: not a valid job"));
6886 return job;
6887}
6888
6889/*
6890 * "job_getchannel()" function
6891 */
6892 static void
6893f_job_getchannel(typval_T *argvars, typval_T *rettv)
6894{
6895 job_T *job = get_job_arg(&argvars[0]);
6896
6897 if (job != NULL)
6898 {
6899 rettv->v_type = VAR_CHANNEL;
6900 rettv->vval.v_channel = job->jv_channel;
6901 if (job->jv_channel != NULL)
6902 ++job->jv_channel->ch_refcount;
6903 }
6904}
6905
6906/*
6907 * "job_info()" function
6908 */
6909 static void
6910f_job_info(typval_T *argvars, typval_T *rettv)
6911{
6912 job_T *job = get_job_arg(&argvars[0]);
6913
6914 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
6915 job_info(job, rettv->vval.v_dict);
6916}
6917
6918/*
6919 * "job_setoptions()" function
6920 */
6921 static void
6922f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
6923{
6924 job_T *job = get_job_arg(&argvars[0]);
6925 jobopt_T opt;
6926
6927 if (job == NULL)
6928 return;
6929 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02006930 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006931 job_set_options(job, &opt);
6932 free_job_options(&opt);
6933}
6934
6935/*
6936 * "job_start()" function
6937 */
6938 static void
6939f_job_start(typval_T *argvars, typval_T *rettv)
6940{
6941 rettv->v_type = VAR_JOB;
6942 if (check_restricted() || check_secure())
6943 return;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006944 rettv->vval.v_job = job_start(argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006945}
6946
6947/*
6948 * "job_status()" function
6949 */
6950 static void
6951f_job_status(typval_T *argvars, typval_T *rettv)
6952{
6953 job_T *job = get_job_arg(&argvars[0]);
6954
6955 if (job != NULL)
6956 {
6957 rettv->v_type = VAR_STRING;
6958 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
6959 }
6960}
6961
6962/*
6963 * "job_stop()" function
6964 */
6965 static void
6966f_job_stop(typval_T *argvars, typval_T *rettv)
6967{
6968 job_T *job = get_job_arg(&argvars[0]);
6969
6970 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02006971 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006972}
6973#endif
6974
6975/*
6976 * "join()" function
6977 */
6978 static void
6979f_join(typval_T *argvars, typval_T *rettv)
6980{
6981 garray_T ga;
6982 char_u *sep;
6983
6984 if (argvars[0].v_type != VAR_LIST)
6985 {
6986 EMSG(_(e_listreq));
6987 return;
6988 }
6989 if (argvars[0].vval.v_list == NULL)
6990 return;
6991 if (argvars[1].v_type == VAR_UNKNOWN)
6992 sep = (char_u *)" ";
6993 else
6994 sep = get_tv_string_chk(&argvars[1]);
6995
6996 rettv->v_type = VAR_STRING;
6997
6998 if (sep != NULL)
6999 {
7000 ga_init2(&ga, (int)sizeof(char), 80);
7001 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7002 ga_append(&ga, NUL);
7003 rettv->vval.v_string = (char_u *)ga.ga_data;
7004 }
7005 else
7006 rettv->vval.v_string = NULL;
7007}
7008
7009/*
7010 * "js_decode()" function
7011 */
7012 static void
7013f_js_decode(typval_T *argvars, typval_T *rettv)
7014{
7015 js_read_T reader;
7016
7017 reader.js_buf = get_tv_string(&argvars[0]);
7018 reader.js_fill = NULL;
7019 reader.js_used = 0;
7020 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
7021 EMSG(_(e_invarg));
7022}
7023
7024/*
7025 * "js_encode()" function
7026 */
7027 static void
7028f_js_encode(typval_T *argvars, typval_T *rettv)
7029{
7030 rettv->v_type = VAR_STRING;
7031 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7032}
7033
7034/*
7035 * "json_decode()" function
7036 */
7037 static void
7038f_json_decode(typval_T *argvars, typval_T *rettv)
7039{
7040 js_read_T reader;
7041
7042 reader.js_buf = get_tv_string(&argvars[0]);
7043 reader.js_fill = NULL;
7044 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007045 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007046}
7047
7048/*
7049 * "json_encode()" function
7050 */
7051 static void
7052f_json_encode(typval_T *argvars, typval_T *rettv)
7053{
7054 rettv->v_type = VAR_STRING;
7055 rettv->vval.v_string = json_encode(&argvars[0], 0);
7056}
7057
7058/*
7059 * "keys()" function
7060 */
7061 static void
7062f_keys(typval_T *argvars, typval_T *rettv)
7063{
7064 dict_list(argvars, rettv, 0);
7065}
7066
7067/*
7068 * "last_buffer_nr()" function.
7069 */
7070 static void
7071f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7072{
7073 int n = 0;
7074 buf_T *buf;
7075
Bram Moolenaar29323592016-07-24 22:04:11 +02007076 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007077 if (n < buf->b_fnum)
7078 n = buf->b_fnum;
7079
7080 rettv->vval.v_number = n;
7081}
7082
7083/*
7084 * "len()" function
7085 */
7086 static void
7087f_len(typval_T *argvars, typval_T *rettv)
7088{
7089 switch (argvars[0].v_type)
7090 {
7091 case VAR_STRING:
7092 case VAR_NUMBER:
7093 rettv->vval.v_number = (varnumber_T)STRLEN(
7094 get_tv_string(&argvars[0]));
7095 break;
7096 case VAR_LIST:
7097 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7098 break;
7099 case VAR_DICT:
7100 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7101 break;
7102 case VAR_UNKNOWN:
7103 case VAR_SPECIAL:
7104 case VAR_FLOAT:
7105 case VAR_FUNC:
7106 case VAR_PARTIAL:
7107 case VAR_JOB:
7108 case VAR_CHANNEL:
7109 EMSG(_("E701: Invalid type for len()"));
7110 break;
7111 }
7112}
7113
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007114 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007115libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007116{
7117#ifdef FEAT_LIBCALL
7118 char_u *string_in;
7119 char_u **string_result;
7120 int nr_result;
7121#endif
7122
7123 rettv->v_type = type;
7124 if (type != VAR_NUMBER)
7125 rettv->vval.v_string = NULL;
7126
7127 if (check_restricted() || check_secure())
7128 return;
7129
7130#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007131 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007132 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7133 {
7134 string_in = NULL;
7135 if (argvars[2].v_type == VAR_STRING)
7136 string_in = argvars[2].vval.v_string;
7137 if (type == VAR_NUMBER)
7138 string_result = NULL;
7139 else
7140 string_result = &rettv->vval.v_string;
7141 if (mch_libcall(argvars[0].vval.v_string,
7142 argvars[1].vval.v_string,
7143 string_in,
7144 argvars[2].vval.v_number,
7145 string_result,
7146 &nr_result) == OK
7147 && type == VAR_NUMBER)
7148 rettv->vval.v_number = nr_result;
7149 }
7150#endif
7151}
7152
7153/*
7154 * "libcall()" function
7155 */
7156 static void
7157f_libcall(typval_T *argvars, typval_T *rettv)
7158{
7159 libcall_common(argvars, rettv, VAR_STRING);
7160}
7161
7162/*
7163 * "libcallnr()" function
7164 */
7165 static void
7166f_libcallnr(typval_T *argvars, typval_T *rettv)
7167{
7168 libcall_common(argvars, rettv, VAR_NUMBER);
7169}
7170
7171/*
7172 * "line(string)" function
7173 */
7174 static void
7175f_line(typval_T *argvars, typval_T *rettv)
7176{
7177 linenr_T lnum = 0;
7178 pos_T *fp;
7179 int fnum;
7180
7181 fp = var2fpos(&argvars[0], TRUE, &fnum);
7182 if (fp != NULL)
7183 lnum = fp->lnum;
7184 rettv->vval.v_number = lnum;
7185}
7186
7187/*
7188 * "line2byte(lnum)" function
7189 */
7190 static void
7191f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7192{
7193#ifndef FEAT_BYTEOFF
7194 rettv->vval.v_number = -1;
7195#else
7196 linenr_T lnum;
7197
7198 lnum = get_tv_lnum(argvars);
7199 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7200 rettv->vval.v_number = -1;
7201 else
7202 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7203 if (rettv->vval.v_number >= 0)
7204 ++rettv->vval.v_number;
7205#endif
7206}
7207
7208/*
7209 * "lispindent(lnum)" function
7210 */
7211 static void
7212f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7213{
7214#ifdef FEAT_LISP
7215 pos_T pos;
7216 linenr_T lnum;
7217
7218 pos = curwin->w_cursor;
7219 lnum = get_tv_lnum(argvars);
7220 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7221 {
7222 curwin->w_cursor.lnum = lnum;
7223 rettv->vval.v_number = get_lisp_indent();
7224 curwin->w_cursor = pos;
7225 }
7226 else
7227#endif
7228 rettv->vval.v_number = -1;
7229}
7230
7231/*
7232 * "localtime()" function
7233 */
7234 static void
7235f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7236{
7237 rettv->vval.v_number = (varnumber_T)time(NULL);
7238}
7239
7240static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
7241
7242 static void
7243get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7244{
7245 char_u *keys;
7246 char_u *which;
7247 char_u buf[NUMBUFLEN];
7248 char_u *keys_buf = NULL;
7249 char_u *rhs;
7250 int mode;
7251 int abbr = FALSE;
7252 int get_dict = FALSE;
7253 mapblock_T *mp;
7254 int buffer_local;
7255
7256 /* return empty string for failure */
7257 rettv->v_type = VAR_STRING;
7258 rettv->vval.v_string = NULL;
7259
7260 keys = get_tv_string(&argvars[0]);
7261 if (*keys == NUL)
7262 return;
7263
7264 if (argvars[1].v_type != VAR_UNKNOWN)
7265 {
7266 which = get_tv_string_buf_chk(&argvars[1], buf);
7267 if (argvars[2].v_type != VAR_UNKNOWN)
7268 {
7269 abbr = (int)get_tv_number(&argvars[2]);
7270 if (argvars[3].v_type != VAR_UNKNOWN)
7271 get_dict = (int)get_tv_number(&argvars[3]);
7272 }
7273 }
7274 else
7275 which = (char_u *)"";
7276 if (which == NULL)
7277 return;
7278
7279 mode = get_map_mode(&which, 0);
7280
7281 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7282 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7283 vim_free(keys_buf);
7284
7285 if (!get_dict)
7286 {
7287 /* Return a string. */
7288 if (rhs != NULL)
7289 rettv->vval.v_string = str2special_save(rhs, FALSE);
7290
7291 }
7292 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7293 {
7294 /* Return a dictionary. */
7295 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7296 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7297 dict_T *dict = rettv->vval.v_dict;
7298
7299 dict_add_nr_str(dict, "lhs", 0L, lhs);
7300 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
7301 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
7302 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
7303 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
7304 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
7305 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
7306 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
7307 dict_add_nr_str(dict, "mode", 0L, mapmode);
7308
7309 vim_free(lhs);
7310 vim_free(mapmode);
7311 }
7312}
7313
7314#ifdef FEAT_FLOAT
7315/*
7316 * "log()" function
7317 */
7318 static void
7319f_log(typval_T *argvars, typval_T *rettv)
7320{
7321 float_T f = 0.0;
7322
7323 rettv->v_type = VAR_FLOAT;
7324 if (get_float_arg(argvars, &f) == OK)
7325 rettv->vval.v_float = log(f);
7326 else
7327 rettv->vval.v_float = 0.0;
7328}
7329
7330/*
7331 * "log10()" function
7332 */
7333 static void
7334f_log10(typval_T *argvars, typval_T *rettv)
7335{
7336 float_T f = 0.0;
7337
7338 rettv->v_type = VAR_FLOAT;
7339 if (get_float_arg(argvars, &f) == OK)
7340 rettv->vval.v_float = log10(f);
7341 else
7342 rettv->vval.v_float = 0.0;
7343}
7344#endif
7345
7346#ifdef FEAT_LUA
7347/*
7348 * "luaeval()" function
7349 */
7350 static void
7351f_luaeval(typval_T *argvars, typval_T *rettv)
7352{
7353 char_u *str;
7354 char_u buf[NUMBUFLEN];
7355
7356 str = get_tv_string_buf(&argvars[0], buf);
7357 do_luaeval(str, argvars + 1, rettv);
7358}
7359#endif
7360
7361/*
7362 * "map()" function
7363 */
7364 static void
7365f_map(typval_T *argvars, typval_T *rettv)
7366{
7367 filter_map(argvars, rettv, TRUE);
7368}
7369
7370/*
7371 * "maparg()" function
7372 */
7373 static void
7374f_maparg(typval_T *argvars, typval_T *rettv)
7375{
7376 get_maparg(argvars, rettv, TRUE);
7377}
7378
7379/*
7380 * "mapcheck()" function
7381 */
7382 static void
7383f_mapcheck(typval_T *argvars, typval_T *rettv)
7384{
7385 get_maparg(argvars, rettv, FALSE);
7386}
7387
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007388typedef enum
7389{
7390 MATCH_END, /* matchend() */
7391 MATCH_MATCH, /* match() */
7392 MATCH_STR, /* matchstr() */
7393 MATCH_LIST, /* matchlist() */
7394 MATCH_POS /* matchstrpos() */
7395} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007396
7397 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007398find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007399{
7400 char_u *str = NULL;
7401 long len = 0;
7402 char_u *expr = NULL;
7403 char_u *pat;
7404 regmatch_T regmatch;
7405 char_u patbuf[NUMBUFLEN];
7406 char_u strbuf[NUMBUFLEN];
7407 char_u *save_cpo;
7408 long start = 0;
7409 long nth = 1;
7410 colnr_T startcol = 0;
7411 int match = 0;
7412 list_T *l = NULL;
7413 listitem_T *li = NULL;
7414 long idx = 0;
7415 char_u *tofree = NULL;
7416
7417 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7418 save_cpo = p_cpo;
7419 p_cpo = (char_u *)"";
7420
7421 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007422 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007423 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007424 /* type MATCH_LIST: return empty list when there are no matches.
7425 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007426 if (rettv_list_alloc(rettv) == FAIL)
7427 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007428 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007429 && (list_append_string(rettv->vval.v_list,
7430 (char_u *)"", 0) == FAIL
7431 || list_append_number(rettv->vval.v_list,
7432 (varnumber_T)-1) == FAIL
7433 || list_append_number(rettv->vval.v_list,
7434 (varnumber_T)-1) == FAIL
7435 || list_append_number(rettv->vval.v_list,
7436 (varnumber_T)-1) == FAIL))
7437 {
7438 list_free(rettv->vval.v_list);
7439 rettv->vval.v_list = NULL;
7440 goto theend;
7441 }
7442 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007443 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007444 {
7445 rettv->v_type = VAR_STRING;
7446 rettv->vval.v_string = NULL;
7447 }
7448
7449 if (argvars[0].v_type == VAR_LIST)
7450 {
7451 if ((l = argvars[0].vval.v_list) == NULL)
7452 goto theend;
7453 li = l->lv_first;
7454 }
7455 else
7456 {
7457 expr = str = get_tv_string(&argvars[0]);
7458 len = (long)STRLEN(str);
7459 }
7460
7461 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
7462 if (pat == NULL)
7463 goto theend;
7464
7465 if (argvars[2].v_type != VAR_UNKNOWN)
7466 {
7467 int error = FALSE;
7468
7469 start = (long)get_tv_number_chk(&argvars[2], &error);
7470 if (error)
7471 goto theend;
7472 if (l != NULL)
7473 {
7474 li = list_find(l, start);
7475 if (li == NULL)
7476 goto theend;
7477 idx = l->lv_idx; /* use the cached index */
7478 }
7479 else
7480 {
7481 if (start < 0)
7482 start = 0;
7483 if (start > len)
7484 goto theend;
7485 /* When "count" argument is there ignore matches before "start",
7486 * otherwise skip part of the string. Differs when pattern is "^"
7487 * or "\<". */
7488 if (argvars[3].v_type != VAR_UNKNOWN)
7489 startcol = start;
7490 else
7491 {
7492 str += start;
7493 len -= start;
7494 }
7495 }
7496
7497 if (argvars[3].v_type != VAR_UNKNOWN)
7498 nth = (long)get_tv_number_chk(&argvars[3], &error);
7499 if (error)
7500 goto theend;
7501 }
7502
7503 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7504 if (regmatch.regprog != NULL)
7505 {
7506 regmatch.rm_ic = p_ic;
7507
7508 for (;;)
7509 {
7510 if (l != NULL)
7511 {
7512 if (li == NULL)
7513 {
7514 match = FALSE;
7515 break;
7516 }
7517 vim_free(tofree);
7518 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7519 if (str == NULL)
7520 break;
7521 }
7522
7523 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7524
7525 if (match && --nth <= 0)
7526 break;
7527 if (l == NULL && !match)
7528 break;
7529
7530 /* Advance to just after the match. */
7531 if (l != NULL)
7532 {
7533 li = li->li_next;
7534 ++idx;
7535 }
7536 else
7537 {
7538#ifdef FEAT_MBYTE
7539 startcol = (colnr_T)(regmatch.startp[0]
7540 + (*mb_ptr2len)(regmatch.startp[0]) - str);
7541#else
7542 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
7543#endif
7544 if (startcol > (colnr_T)len
7545 || str + startcol <= regmatch.startp[0])
7546 {
7547 match = FALSE;
7548 break;
7549 }
7550 }
7551 }
7552
7553 if (match)
7554 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007555 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007556 {
7557 listitem_T *li1 = rettv->vval.v_list->lv_first;
7558 listitem_T *li2 = li1->li_next;
7559 listitem_T *li3 = li2->li_next;
7560 listitem_T *li4 = li3->li_next;
7561
7562 vim_free(li1->li_tv.vval.v_string);
7563 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7564 (int)(regmatch.endp[0] - regmatch.startp[0]));
7565 li3->li_tv.vval.v_number =
7566 (varnumber_T)(regmatch.startp[0] - expr);
7567 li4->li_tv.vval.v_number =
7568 (varnumber_T)(regmatch.endp[0] - expr);
7569 if (l != NULL)
7570 li2->li_tv.vval.v_number = (varnumber_T)idx;
7571 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007572 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007573 {
7574 int i;
7575
7576 /* return list with matched string and submatches */
7577 for (i = 0; i < NSUBEXP; ++i)
7578 {
7579 if (regmatch.endp[i] == NULL)
7580 {
7581 if (list_append_string(rettv->vval.v_list,
7582 (char_u *)"", 0) == FAIL)
7583 break;
7584 }
7585 else if (list_append_string(rettv->vval.v_list,
7586 regmatch.startp[i],
7587 (int)(regmatch.endp[i] - regmatch.startp[i]))
7588 == FAIL)
7589 break;
7590 }
7591 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007592 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007593 {
7594 /* return matched string */
7595 if (l != NULL)
7596 copy_tv(&li->li_tv, rettv);
7597 else
7598 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
7599 (int)(regmatch.endp[0] - regmatch.startp[0]));
7600 }
7601 else if (l != NULL)
7602 rettv->vval.v_number = idx;
7603 else
7604 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007605 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007606 rettv->vval.v_number =
7607 (varnumber_T)(regmatch.startp[0] - str);
7608 else
7609 rettv->vval.v_number =
7610 (varnumber_T)(regmatch.endp[0] - str);
7611 rettv->vval.v_number += (varnumber_T)(str - expr);
7612 }
7613 }
7614 vim_regfree(regmatch.regprog);
7615 }
7616
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007617theend:
7618 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007619 /* matchstrpos() without a list: drop the second item. */
7620 listitem_remove(rettv->vval.v_list,
7621 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007622 vim_free(tofree);
7623 p_cpo = save_cpo;
7624}
7625
7626/*
7627 * "match()" function
7628 */
7629 static void
7630f_match(typval_T *argvars, typval_T *rettv)
7631{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007632 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007633}
7634
7635/*
7636 * "matchadd()" function
7637 */
7638 static void
7639f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7640{
7641#ifdef FEAT_SEARCH_EXTRA
7642 char_u buf[NUMBUFLEN];
7643 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
7644 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
7645 int prio = 10; /* default priority */
7646 int id = -1;
7647 int error = FALSE;
7648 char_u *conceal_char = NULL;
7649
7650 rettv->vval.v_number = -1;
7651
7652 if (grp == NULL || pat == NULL)
7653 return;
7654 if (argvars[2].v_type != VAR_UNKNOWN)
7655 {
7656 prio = (int)get_tv_number_chk(&argvars[2], &error);
7657 if (argvars[3].v_type != VAR_UNKNOWN)
7658 {
7659 id = (int)get_tv_number_chk(&argvars[3], &error);
7660 if (argvars[4].v_type != VAR_UNKNOWN)
7661 {
7662 if (argvars[4].v_type != VAR_DICT)
7663 {
7664 EMSG(_(e_dictreq));
7665 return;
7666 }
7667 if (dict_find(argvars[4].vval.v_dict,
7668 (char_u *)"conceal", -1) != NULL)
7669 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7670 (char_u *)"conceal", FALSE);
7671 }
7672 }
7673 }
7674 if (error == TRUE)
7675 return;
7676 if (id >= 1 && id <= 3)
7677 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007678 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007679 return;
7680 }
7681
7682 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
7683 conceal_char);
7684#endif
7685}
7686
7687/*
7688 * "matchaddpos()" function
7689 */
7690 static void
7691f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7692{
7693#ifdef FEAT_SEARCH_EXTRA
7694 char_u buf[NUMBUFLEN];
7695 char_u *group;
7696 int prio = 10;
7697 int id = -1;
7698 int error = FALSE;
7699 list_T *l;
7700 char_u *conceal_char = NULL;
7701
7702 rettv->vval.v_number = -1;
7703
7704 group = get_tv_string_buf_chk(&argvars[0], buf);
7705 if (group == NULL)
7706 return;
7707
7708 if (argvars[1].v_type != VAR_LIST)
7709 {
7710 EMSG2(_(e_listarg), "matchaddpos()");
7711 return;
7712 }
7713 l = argvars[1].vval.v_list;
7714 if (l == NULL)
7715 return;
7716
7717 if (argvars[2].v_type != VAR_UNKNOWN)
7718 {
7719 prio = (int)get_tv_number_chk(&argvars[2], &error);
7720 if (argvars[3].v_type != VAR_UNKNOWN)
7721 {
7722 id = (int)get_tv_number_chk(&argvars[3], &error);
7723 if (argvars[4].v_type != VAR_UNKNOWN)
7724 {
7725 if (argvars[4].v_type != VAR_DICT)
7726 {
7727 EMSG(_(e_dictreq));
7728 return;
7729 }
7730 if (dict_find(argvars[4].vval.v_dict,
7731 (char_u *)"conceal", -1) != NULL)
7732 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7733 (char_u *)"conceal", FALSE);
7734 }
7735 }
7736 }
7737 if (error == TRUE)
7738 return;
7739
7740 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
7741 if (id == 1 || id == 2)
7742 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007743 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007744 return;
7745 }
7746
7747 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
7748 conceal_char);
7749#endif
7750}
7751
7752/*
7753 * "matcharg()" function
7754 */
7755 static void
7756f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
7757{
7758 if (rettv_list_alloc(rettv) == OK)
7759 {
7760#ifdef FEAT_SEARCH_EXTRA
7761 int id = (int)get_tv_number(&argvars[0]);
7762 matchitem_T *m;
7763
7764 if (id >= 1 && id <= 3)
7765 {
7766 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
7767 {
7768 list_append_string(rettv->vval.v_list,
7769 syn_id2name(m->hlg_id), -1);
7770 list_append_string(rettv->vval.v_list, m->pattern, -1);
7771 }
7772 else
7773 {
7774 list_append_string(rettv->vval.v_list, NULL, -1);
7775 list_append_string(rettv->vval.v_list, NULL, -1);
7776 }
7777 }
7778#endif
7779 }
7780}
7781
7782/*
7783 * "matchdelete()" function
7784 */
7785 static void
7786f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7787{
7788#ifdef FEAT_SEARCH_EXTRA
7789 rettv->vval.v_number = match_delete(curwin,
7790 (int)get_tv_number(&argvars[0]), TRUE);
7791#endif
7792}
7793
7794/*
7795 * "matchend()" function
7796 */
7797 static void
7798f_matchend(typval_T *argvars, typval_T *rettv)
7799{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007800 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007801}
7802
7803/*
7804 * "matchlist()" function
7805 */
7806 static void
7807f_matchlist(typval_T *argvars, typval_T *rettv)
7808{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007809 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007810}
7811
7812/*
7813 * "matchstr()" function
7814 */
7815 static void
7816f_matchstr(typval_T *argvars, typval_T *rettv)
7817{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007818 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007819}
7820
7821/*
7822 * "matchstrpos()" function
7823 */
7824 static void
7825f_matchstrpos(typval_T *argvars, typval_T *rettv)
7826{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007827 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007828}
7829
7830static void max_min(typval_T *argvars, typval_T *rettv, int domax);
7831
7832 static void
7833max_min(typval_T *argvars, typval_T *rettv, int domax)
7834{
7835 varnumber_T n = 0;
7836 varnumber_T i;
7837 int error = FALSE;
7838
7839 if (argvars[0].v_type == VAR_LIST)
7840 {
7841 list_T *l;
7842 listitem_T *li;
7843
7844 l = argvars[0].vval.v_list;
7845 if (l != NULL)
7846 {
7847 li = l->lv_first;
7848 if (li != NULL)
7849 {
7850 n = get_tv_number_chk(&li->li_tv, &error);
7851 for (;;)
7852 {
7853 li = li->li_next;
7854 if (li == NULL)
7855 break;
7856 i = get_tv_number_chk(&li->li_tv, &error);
7857 if (domax ? i > n : i < n)
7858 n = i;
7859 }
7860 }
7861 }
7862 }
7863 else if (argvars[0].v_type == VAR_DICT)
7864 {
7865 dict_T *d;
7866 int first = TRUE;
7867 hashitem_T *hi;
7868 int todo;
7869
7870 d = argvars[0].vval.v_dict;
7871 if (d != NULL)
7872 {
7873 todo = (int)d->dv_hashtab.ht_used;
7874 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
7875 {
7876 if (!HASHITEM_EMPTY(hi))
7877 {
7878 --todo;
7879 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
7880 if (first)
7881 {
7882 n = i;
7883 first = FALSE;
7884 }
7885 else if (domax ? i > n : i < n)
7886 n = i;
7887 }
7888 }
7889 }
7890 }
7891 else
Bram Moolenaar26b84332016-09-04 21:42:36 +02007892 EMSG2(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007893 rettv->vval.v_number = error ? 0 : n;
7894}
7895
7896/*
7897 * "max()" function
7898 */
7899 static void
7900f_max(typval_T *argvars, typval_T *rettv)
7901{
7902 max_min(argvars, rettv, TRUE);
7903}
7904
7905/*
7906 * "min()" function
7907 */
7908 static void
7909f_min(typval_T *argvars, typval_T *rettv)
7910{
7911 max_min(argvars, rettv, FALSE);
7912}
7913
7914static int mkdir_recurse(char_u *dir, int prot);
7915
7916/*
7917 * Create the directory in which "dir" is located, and higher levels when
7918 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02007919 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007920 */
7921 static int
7922mkdir_recurse(char_u *dir, int prot)
7923{
7924 char_u *p;
7925 char_u *updir;
7926 int r = FAIL;
7927
7928 /* Get end of directory name in "dir".
7929 * We're done when it's "/" or "c:/". */
7930 p = gettail_sep(dir);
7931 if (p <= get_past_head(dir))
7932 return OK;
7933
7934 /* If the directory exists we're done. Otherwise: create it.*/
7935 updir = vim_strnsave(dir, (int)(p - dir));
7936 if (updir == NULL)
7937 return FAIL;
7938 if (mch_isdir(updir))
7939 r = OK;
7940 else if (mkdir_recurse(updir, prot) == OK)
7941 r = vim_mkdir_emsg(updir, prot);
7942 vim_free(updir);
7943 return r;
7944}
7945
7946#ifdef vim_mkdir
7947/*
7948 * "mkdir()" function
7949 */
7950 static void
7951f_mkdir(typval_T *argvars, typval_T *rettv)
7952{
7953 char_u *dir;
7954 char_u buf[NUMBUFLEN];
7955 int prot = 0755;
7956
7957 rettv->vval.v_number = FAIL;
7958 if (check_restricted() || check_secure())
7959 return;
7960
7961 dir = get_tv_string_buf(&argvars[0], buf);
7962 if (*dir == NUL)
7963 rettv->vval.v_number = FAIL;
7964 else
7965 {
7966 if (*gettail(dir) == NUL)
7967 /* remove trailing slashes */
7968 *gettail_sep(dir) = NUL;
7969
7970 if (argvars[1].v_type != VAR_UNKNOWN)
7971 {
7972 if (argvars[2].v_type != VAR_UNKNOWN)
7973 prot = (int)get_tv_number_chk(&argvars[2], NULL);
7974 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
7975 mkdir_recurse(dir, prot);
7976 }
7977 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
7978 }
7979}
7980#endif
7981
7982/*
7983 * "mode()" function
7984 */
7985 static void
7986f_mode(typval_T *argvars, typval_T *rettv)
7987{
7988 char_u buf[3];
7989
7990 buf[1] = NUL;
7991 buf[2] = NUL;
7992
7993 if (time_for_testing == 93784)
7994 {
7995 /* Testing the two-character code. */
7996 buf[0] = 'x';
7997 buf[1] = '!';
7998 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02007999#ifdef FEAT_TERMINAL
8000 else if (term_use_loop())
8001 buf[0] = 't';
8002#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008003 else if (VIsual_active)
8004 {
8005 if (VIsual_select)
8006 buf[0] = VIsual_mode + 's' - 'v';
8007 else
8008 buf[0] = VIsual_mode;
8009 }
8010 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8011 || State == CONFIRM)
8012 {
8013 buf[0] = 'r';
8014 if (State == ASKMORE)
8015 buf[1] = 'm';
8016 else if (State == CONFIRM)
8017 buf[1] = '?';
8018 }
8019 else if (State == EXTERNCMD)
8020 buf[0] = '!';
8021 else if (State & INSERT)
8022 {
8023#ifdef FEAT_VREPLACE
8024 if (State & VREPLACE_FLAG)
8025 {
8026 buf[0] = 'R';
8027 buf[1] = 'v';
8028 }
8029 else
8030#endif
Bram Moolenaare90858d2017-02-01 17:24:34 +01008031 {
8032 if (State & REPLACE_FLAG)
8033 buf[0] = 'R';
8034 else
8035 buf[0] = 'i';
8036#ifdef FEAT_INS_EXPAND
8037 if (ins_compl_active())
8038 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008039 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008040 buf[1] = 'x';
8041#endif
8042 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008043 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008044 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008045 {
8046 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008047 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008048 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008049 else if (exmode_active == EXMODE_NORMAL)
8050 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008051 }
8052 else
8053 {
8054 buf[0] = 'n';
8055 if (finish_op)
8056 buf[1] = 'o';
8057 }
8058
8059 /* Clear out the minor mode when the argument is not a non-zero number or
8060 * non-empty string. */
8061 if (!non_zero_arg(&argvars[0]))
8062 buf[1] = NUL;
8063
8064 rettv->vval.v_string = vim_strsave(buf);
8065 rettv->v_type = VAR_STRING;
8066}
8067
8068#if defined(FEAT_MZSCHEME) || defined(PROTO)
8069/*
8070 * "mzeval()" function
8071 */
8072 static void
8073f_mzeval(typval_T *argvars, typval_T *rettv)
8074{
8075 char_u *str;
8076 char_u buf[NUMBUFLEN];
8077
8078 str = get_tv_string_buf(&argvars[0], buf);
8079 do_mzeval(str, rettv);
8080}
8081
8082 void
8083mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8084{
8085 typval_T argvars[3];
8086
8087 argvars[0].v_type = VAR_STRING;
8088 argvars[0].vval.v_string = name;
8089 copy_tv(args, &argvars[1]);
8090 argvars[2].v_type = VAR_UNKNOWN;
8091 f_call(argvars, rettv);
8092 clear_tv(&argvars[1]);
8093}
8094#endif
8095
8096/*
8097 * "nextnonblank()" function
8098 */
8099 static void
8100f_nextnonblank(typval_T *argvars, typval_T *rettv)
8101{
8102 linenr_T lnum;
8103
8104 for (lnum = get_tv_lnum(argvars); ; ++lnum)
8105 {
8106 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8107 {
8108 lnum = 0;
8109 break;
8110 }
8111 if (*skipwhite(ml_get(lnum)) != NUL)
8112 break;
8113 }
8114 rettv->vval.v_number = lnum;
8115}
8116
8117/*
8118 * "nr2char()" function
8119 */
8120 static void
8121f_nr2char(typval_T *argvars, typval_T *rettv)
8122{
8123 char_u buf[NUMBUFLEN];
8124
8125#ifdef FEAT_MBYTE
8126 if (has_mbyte)
8127 {
8128 int utf8 = 0;
8129
8130 if (argvars[1].v_type != VAR_UNKNOWN)
8131 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
8132 if (utf8)
8133 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8134 else
8135 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8136 }
8137 else
8138#endif
8139 {
8140 buf[0] = (char_u)get_tv_number(&argvars[0]);
8141 buf[1] = NUL;
8142 }
8143 rettv->v_type = VAR_STRING;
8144 rettv->vval.v_string = vim_strsave(buf);
8145}
8146
8147/*
8148 * "or(expr, expr)" function
8149 */
8150 static void
8151f_or(typval_T *argvars, typval_T *rettv)
8152{
8153 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8154 | get_tv_number_chk(&argvars[1], NULL);
8155}
8156
8157/*
8158 * "pathshorten()" function
8159 */
8160 static void
8161f_pathshorten(typval_T *argvars, typval_T *rettv)
8162{
8163 char_u *p;
8164
8165 rettv->v_type = VAR_STRING;
8166 p = get_tv_string_chk(&argvars[0]);
8167 if (p == NULL)
8168 rettv->vval.v_string = NULL;
8169 else
8170 {
8171 p = vim_strsave(p);
8172 rettv->vval.v_string = p;
8173 if (p != NULL)
8174 shorten_dir(p);
8175 }
8176}
8177
8178#ifdef FEAT_PERL
8179/*
8180 * "perleval()" function
8181 */
8182 static void
8183f_perleval(typval_T *argvars, typval_T *rettv)
8184{
8185 char_u *str;
8186 char_u buf[NUMBUFLEN];
8187
8188 str = get_tv_string_buf(&argvars[0], buf);
8189 do_perleval(str, rettv);
8190}
8191#endif
8192
8193#ifdef FEAT_FLOAT
8194/*
8195 * "pow()" function
8196 */
8197 static void
8198f_pow(typval_T *argvars, typval_T *rettv)
8199{
8200 float_T fx = 0.0, fy = 0.0;
8201
8202 rettv->v_type = VAR_FLOAT;
8203 if (get_float_arg(argvars, &fx) == OK
8204 && get_float_arg(&argvars[1], &fy) == OK)
8205 rettv->vval.v_float = pow(fx, fy);
8206 else
8207 rettv->vval.v_float = 0.0;
8208}
8209#endif
8210
8211/*
8212 * "prevnonblank()" function
8213 */
8214 static void
8215f_prevnonblank(typval_T *argvars, typval_T *rettv)
8216{
8217 linenr_T lnum;
8218
8219 lnum = get_tv_lnum(argvars);
8220 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8221 lnum = 0;
8222 else
8223 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8224 --lnum;
8225 rettv->vval.v_number = lnum;
8226}
8227
8228/* This dummy va_list is here because:
8229 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8230 * - locally in the function results in a "used before set" warning
8231 * - using va_start() to initialize it gives "function with fixed args" error */
8232static va_list ap;
8233
8234/*
8235 * "printf()" function
8236 */
8237 static void
8238f_printf(typval_T *argvars, typval_T *rettv)
8239{
8240 char_u buf[NUMBUFLEN];
8241 int len;
8242 char_u *s;
8243 int saved_did_emsg = did_emsg;
8244 char *fmt;
8245
8246 rettv->v_type = VAR_STRING;
8247 rettv->vval.v_string = NULL;
8248
8249 /* Get the required length, allocate the buffer and do it for real. */
8250 did_emsg = FALSE;
8251 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008252 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008253 if (!did_emsg)
8254 {
8255 s = alloc(len + 1);
8256 if (s != NULL)
8257 {
8258 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008259 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8260 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008261 }
8262 }
8263 did_emsg |= saved_did_emsg;
8264}
8265
8266/*
8267 * "pumvisible()" function
8268 */
8269 static void
8270f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8271{
8272#ifdef FEAT_INS_EXPAND
8273 if (pum_visible())
8274 rettv->vval.v_number = 1;
8275#endif
8276}
8277
8278#ifdef FEAT_PYTHON3
8279/*
8280 * "py3eval()" function
8281 */
8282 static void
8283f_py3eval(typval_T *argvars, typval_T *rettv)
8284{
8285 char_u *str;
8286 char_u buf[NUMBUFLEN];
8287
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008288 if (p_pyx == 0)
8289 p_pyx = 3;
8290
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008291 str = get_tv_string_buf(&argvars[0], buf);
8292 do_py3eval(str, rettv);
8293}
8294#endif
8295
8296#ifdef FEAT_PYTHON
8297/*
8298 * "pyeval()" function
8299 */
8300 static void
8301f_pyeval(typval_T *argvars, typval_T *rettv)
8302{
8303 char_u *str;
8304 char_u buf[NUMBUFLEN];
8305
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008306 if (p_pyx == 0)
8307 p_pyx = 2;
8308
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008309 str = get_tv_string_buf(&argvars[0], buf);
8310 do_pyeval(str, rettv);
8311}
8312#endif
8313
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008314#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8315/*
8316 * "pyxeval()" function
8317 */
8318 static void
8319f_pyxeval(typval_T *argvars, typval_T *rettv)
8320{
8321# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8322 init_pyxversion();
8323 if (p_pyx == 2)
8324 f_pyeval(argvars, rettv);
8325 else
8326 f_py3eval(argvars, rettv);
8327# elif defined(FEAT_PYTHON)
8328 f_pyeval(argvars, rettv);
8329# elif defined(FEAT_PYTHON3)
8330 f_py3eval(argvars, rettv);
8331# endif
8332}
8333#endif
8334
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008335/*
8336 * "range()" function
8337 */
8338 static void
8339f_range(typval_T *argvars, typval_T *rettv)
8340{
8341 varnumber_T start;
8342 varnumber_T end;
8343 varnumber_T stride = 1;
8344 varnumber_T i;
8345 int error = FALSE;
8346
8347 start = get_tv_number_chk(&argvars[0], &error);
8348 if (argvars[1].v_type == VAR_UNKNOWN)
8349 {
8350 end = start - 1;
8351 start = 0;
8352 }
8353 else
8354 {
8355 end = get_tv_number_chk(&argvars[1], &error);
8356 if (argvars[2].v_type != VAR_UNKNOWN)
8357 stride = get_tv_number_chk(&argvars[2], &error);
8358 }
8359
8360 if (error)
8361 return; /* type error; errmsg already given */
8362 if (stride == 0)
8363 EMSG(_("E726: Stride is zero"));
8364 else if (stride > 0 ? end + 1 < start : end - 1 > start)
8365 EMSG(_("E727: Start past end"));
8366 else
8367 {
8368 if (rettv_list_alloc(rettv) == OK)
8369 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8370 if (list_append_number(rettv->vval.v_list,
8371 (varnumber_T)i) == FAIL)
8372 break;
8373 }
8374}
8375
8376/*
8377 * "readfile()" function
8378 */
8379 static void
8380f_readfile(typval_T *argvars, typval_T *rettv)
8381{
8382 int binary = FALSE;
8383 int failed = FALSE;
8384 char_u *fname;
8385 FILE *fd;
8386 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8387 int io_size = sizeof(buf);
8388 int readlen; /* size of last fread() */
8389 char_u *prev = NULL; /* previously read bytes, if any */
8390 long prevlen = 0; /* length of data in prev */
8391 long prevsize = 0; /* size of prev buffer */
8392 long maxline = MAXLNUM;
8393 long cnt = 0;
8394 char_u *p; /* position in buf */
8395 char_u *start; /* start of current line */
8396
8397 if (argvars[1].v_type != VAR_UNKNOWN)
8398 {
8399 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
8400 binary = TRUE;
8401 if (argvars[2].v_type != VAR_UNKNOWN)
8402 maxline = (long)get_tv_number(&argvars[2]);
8403 }
8404
8405 if (rettv_list_alloc(rettv) == FAIL)
8406 return;
8407
8408 /* Always open the file in binary mode, library functions have a mind of
8409 * their own about CR-LF conversion. */
8410 fname = get_tv_string(&argvars[0]);
8411 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8412 {
8413 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
8414 return;
8415 }
8416
8417 while (cnt < maxline || maxline < 0)
8418 {
8419 readlen = (int)fread(buf, 1, io_size, fd);
8420
8421 /* This for loop processes what was read, but is also entered at end
8422 * of file so that either:
8423 * - an incomplete line gets written
8424 * - a "binary" file gets an empty line at the end if it ends in a
8425 * newline. */
8426 for (p = buf, start = buf;
8427 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8428 ++p)
8429 {
8430 if (*p == '\n' || readlen <= 0)
8431 {
8432 listitem_T *li;
8433 char_u *s = NULL;
8434 long_u len = p - start;
8435
8436 /* Finished a line. Remove CRs before NL. */
8437 if (readlen > 0 && !binary)
8438 {
8439 while (len > 0 && start[len - 1] == '\r')
8440 --len;
8441 /* removal may cross back to the "prev" string */
8442 if (len == 0)
8443 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8444 --prevlen;
8445 }
8446 if (prevlen == 0)
8447 s = vim_strnsave(start, (int)len);
8448 else
8449 {
8450 /* Change "prev" buffer to be the right size. This way
8451 * the bytes are only copied once, and very long lines are
8452 * allocated only once. */
8453 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8454 {
8455 mch_memmove(s + prevlen, start, len);
8456 s[prevlen + len] = NUL;
8457 prev = NULL; /* the list will own the string */
8458 prevlen = prevsize = 0;
8459 }
8460 }
8461 if (s == NULL)
8462 {
8463 do_outofmem_msg((long_u) prevlen + len + 1);
8464 failed = TRUE;
8465 break;
8466 }
8467
8468 if ((li = listitem_alloc()) == NULL)
8469 {
8470 vim_free(s);
8471 failed = TRUE;
8472 break;
8473 }
8474 li->li_tv.v_type = VAR_STRING;
8475 li->li_tv.v_lock = 0;
8476 li->li_tv.vval.v_string = s;
8477 list_append(rettv->vval.v_list, li);
8478
8479 start = p + 1; /* step over newline */
8480 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
8481 break;
8482 }
8483 else if (*p == NUL)
8484 *p = '\n';
8485#ifdef FEAT_MBYTE
8486 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
8487 * when finding the BF and check the previous two bytes. */
8488 else if (*p == 0xbf && enc_utf8 && !binary)
8489 {
8490 /* Find the two bytes before the 0xbf. If p is at buf, or buf
8491 * + 1, these may be in the "prev" string. */
8492 char_u back1 = p >= buf + 1 ? p[-1]
8493 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
8494 char_u back2 = p >= buf + 2 ? p[-2]
8495 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
8496 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
8497
8498 if (back2 == 0xef && back1 == 0xbb)
8499 {
8500 char_u *dest = p - 2;
8501
8502 /* Usually a BOM is at the beginning of a file, and so at
8503 * the beginning of a line; then we can just step over it.
8504 */
8505 if (start == dest)
8506 start = p + 1;
8507 else
8508 {
8509 /* have to shuffle buf to close gap */
8510 int adjust_prevlen = 0;
8511
8512 if (dest < buf)
8513 {
8514 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
8515 dest = buf;
8516 }
8517 if (readlen > p - buf + 1)
8518 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
8519 readlen -= 3 - adjust_prevlen;
8520 prevlen -= adjust_prevlen;
8521 p = dest - 1;
8522 }
8523 }
8524 }
8525#endif
8526 } /* for */
8527
8528 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
8529 break;
8530 if (start < p)
8531 {
8532 /* There's part of a line in buf, store it in "prev". */
8533 if (p - start + prevlen >= prevsize)
8534 {
8535 /* need bigger "prev" buffer */
8536 char_u *newprev;
8537
8538 /* A common use case is ordinary text files and "prev" gets a
8539 * fragment of a line, so the first allocation is made
8540 * small, to avoid repeatedly 'allocing' large and
8541 * 'reallocing' small. */
8542 if (prevsize == 0)
8543 prevsize = (long)(p - start);
8544 else
8545 {
8546 long grow50pc = (prevsize * 3) / 2;
8547 long growmin = (long)((p - start) * 2 + prevlen);
8548 prevsize = grow50pc > growmin ? grow50pc : growmin;
8549 }
8550 newprev = prev == NULL ? alloc(prevsize)
8551 : vim_realloc(prev, prevsize);
8552 if (newprev == NULL)
8553 {
8554 do_outofmem_msg((long_u)prevsize);
8555 failed = TRUE;
8556 break;
8557 }
8558 prev = newprev;
8559 }
8560 /* Add the line part to end of "prev". */
8561 mch_memmove(prev + prevlen, start, p - start);
8562 prevlen += (long)(p - start);
8563 }
8564 } /* while */
8565
8566 /*
8567 * For a negative line count use only the lines at the end of the file,
8568 * free the rest.
8569 */
8570 if (!failed && maxline < 0)
8571 while (cnt > -maxline)
8572 {
8573 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
8574 --cnt;
8575 }
8576
8577 if (failed)
8578 {
8579 list_free(rettv->vval.v_list);
8580 /* readfile doc says an empty list is returned on error */
8581 rettv->vval.v_list = list_alloc();
8582 }
8583
8584 vim_free(prev);
8585 fclose(fd);
8586}
8587
8588#if defined(FEAT_RELTIME)
8589static int list2proftime(typval_T *arg, proftime_T *tm);
8590
8591/*
8592 * Convert a List to proftime_T.
8593 * Return FAIL when there is something wrong.
8594 */
8595 static int
8596list2proftime(typval_T *arg, proftime_T *tm)
8597{
8598 long n1, n2;
8599 int error = FALSE;
8600
8601 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
8602 || arg->vval.v_list->lv_len != 2)
8603 return FAIL;
8604 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
8605 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
8606# ifdef WIN3264
8607 tm->HighPart = n1;
8608 tm->LowPart = n2;
8609# else
8610 tm->tv_sec = n1;
8611 tm->tv_usec = n2;
8612# endif
8613 return error ? FAIL : OK;
8614}
8615#endif /* FEAT_RELTIME */
8616
8617/*
8618 * "reltime()" function
8619 */
8620 static void
8621f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8622{
8623#ifdef FEAT_RELTIME
8624 proftime_T res;
8625 proftime_T start;
8626
8627 if (argvars[0].v_type == VAR_UNKNOWN)
8628 {
8629 /* No arguments: get current time. */
8630 profile_start(&res);
8631 }
8632 else if (argvars[1].v_type == VAR_UNKNOWN)
8633 {
8634 if (list2proftime(&argvars[0], &res) == FAIL)
8635 return;
8636 profile_end(&res);
8637 }
8638 else
8639 {
8640 /* Two arguments: compute the difference. */
8641 if (list2proftime(&argvars[0], &start) == FAIL
8642 || list2proftime(&argvars[1], &res) == FAIL)
8643 return;
8644 profile_sub(&res, &start);
8645 }
8646
8647 if (rettv_list_alloc(rettv) == OK)
8648 {
8649 long n1, n2;
8650
8651# ifdef WIN3264
8652 n1 = res.HighPart;
8653 n2 = res.LowPart;
8654# else
8655 n1 = res.tv_sec;
8656 n2 = res.tv_usec;
8657# endif
8658 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
8659 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
8660 }
8661#endif
8662}
8663
8664#ifdef FEAT_FLOAT
8665/*
8666 * "reltimefloat()" function
8667 */
8668 static void
8669f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
8670{
8671# ifdef FEAT_RELTIME
8672 proftime_T tm;
8673# endif
8674
8675 rettv->v_type = VAR_FLOAT;
8676 rettv->vval.v_float = 0;
8677# ifdef FEAT_RELTIME
8678 if (list2proftime(&argvars[0], &tm) == OK)
8679 rettv->vval.v_float = profile_float(&tm);
8680# endif
8681}
8682#endif
8683
8684/*
8685 * "reltimestr()" function
8686 */
8687 static void
8688f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
8689{
8690#ifdef FEAT_RELTIME
8691 proftime_T tm;
8692#endif
8693
8694 rettv->v_type = VAR_STRING;
8695 rettv->vval.v_string = NULL;
8696#ifdef FEAT_RELTIME
8697 if (list2proftime(&argvars[0], &tm) == OK)
8698 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
8699#endif
8700}
8701
8702#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
8703static void make_connection(void);
8704static int check_connection(void);
8705
8706 static void
8707make_connection(void)
8708{
8709 if (X_DISPLAY == NULL
8710# ifdef FEAT_GUI
8711 && !gui.in_use
8712# endif
8713 )
8714 {
8715 x_force_connect = TRUE;
8716 setup_term_clip();
8717 x_force_connect = FALSE;
8718 }
8719}
8720
8721 static int
8722check_connection(void)
8723{
8724 make_connection();
8725 if (X_DISPLAY == NULL)
8726 {
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01008727 EMSG(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008728 return FAIL;
8729 }
8730 return OK;
8731}
8732#endif
8733
8734#ifdef FEAT_CLIENTSERVER
8735 static void
8736remote_common(typval_T *argvars, typval_T *rettv, int expr)
8737{
8738 char_u *server_name;
8739 char_u *keys;
8740 char_u *r = NULL;
8741 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008742 int timeout = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008743# ifdef WIN32
8744 HWND w;
8745# else
8746 Window w;
8747# endif
8748
8749 if (check_restricted() || check_secure())
8750 return;
8751
8752# ifdef FEAT_X11
8753 if (check_connection() == FAIL)
8754 return;
8755# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008756 if (argvars[2].v_type != VAR_UNKNOWN
8757 && argvars[3].v_type != VAR_UNKNOWN)
8758 timeout = get_tv_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008759
8760 server_name = get_tv_string_chk(&argvars[0]);
8761 if (server_name == NULL)
8762 return; /* type error; errmsg already given */
8763 keys = get_tv_string_buf(&argvars[1], buf);
8764# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008765 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008766# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008767 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
8768 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008769# endif
8770 {
8771 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008772 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008773 EMSG(r); /* sending worked but evaluation failed */
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008774 vim_free(r);
8775 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008776 else
8777 EMSG2(_("E241: Unable to send to %s"), server_name);
8778 return;
8779 }
8780
8781 rettv->vval.v_string = r;
8782
8783 if (argvars[2].v_type != VAR_UNKNOWN)
8784 {
8785 dictitem_T v;
8786 char_u str[30];
8787 char_u *idvar;
8788
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008789 idvar = get_tv_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008790 if (idvar != NULL && *idvar != NUL)
8791 {
8792 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
8793 v.di_tv.v_type = VAR_STRING;
8794 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008795 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008796 vim_free(v.di_tv.vval.v_string);
8797 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008798 }
8799}
8800#endif
8801
8802/*
8803 * "remote_expr()" function
8804 */
8805 static void
8806f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
8807{
8808 rettv->v_type = VAR_STRING;
8809 rettv->vval.v_string = NULL;
8810#ifdef FEAT_CLIENTSERVER
8811 remote_common(argvars, rettv, TRUE);
8812#endif
8813}
8814
8815/*
8816 * "remote_foreground()" function
8817 */
8818 static void
8819f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8820{
8821#ifdef FEAT_CLIENTSERVER
8822# ifdef WIN32
8823 /* On Win32 it's done in this application. */
8824 {
8825 char_u *server_name = get_tv_string_chk(&argvars[0]);
8826
8827 if (server_name != NULL)
8828 serverForeground(server_name);
8829 }
8830# else
8831 /* Send a foreground() expression to the server. */
8832 argvars[1].v_type = VAR_STRING;
8833 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
8834 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008835 rettv->v_type = VAR_STRING;
8836 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008837 remote_common(argvars, rettv, TRUE);
8838 vim_free(argvars[1].vval.v_string);
8839# endif
8840#endif
8841}
8842
8843 static void
8844f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
8845{
8846#ifdef FEAT_CLIENTSERVER
8847 dictitem_T v;
8848 char_u *s = NULL;
8849# ifdef WIN32
8850 long_u n = 0;
8851# endif
8852 char_u *serverid;
8853
8854 if (check_restricted() || check_secure())
8855 {
8856 rettv->vval.v_number = -1;
8857 return;
8858 }
8859 serverid = get_tv_string_chk(&argvars[0]);
8860 if (serverid == NULL)
8861 {
8862 rettv->vval.v_number = -1;
8863 return; /* type error; errmsg already given */
8864 }
8865# ifdef WIN32
8866 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
8867 if (n == 0)
8868 rettv->vval.v_number = -1;
8869 else
8870 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008871 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008872 rettv->vval.v_number = (s != NULL);
8873 }
8874# else
8875 if (check_connection() == FAIL)
8876 return;
8877
8878 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
8879 serverStrToWin(serverid), &s);
8880# endif
8881
8882 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
8883 {
8884 char_u *retvar;
8885
8886 v.di_tv.v_type = VAR_STRING;
8887 v.di_tv.vval.v_string = vim_strsave(s);
8888 retvar = get_tv_string_chk(&argvars[1]);
8889 if (retvar != NULL)
8890 set_var(retvar, &v.di_tv, FALSE);
8891 vim_free(v.di_tv.vval.v_string);
8892 }
8893#else
8894 rettv->vval.v_number = -1;
8895#endif
8896}
8897
8898 static void
8899f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
8900{
8901 char_u *r = NULL;
8902
8903#ifdef FEAT_CLIENTSERVER
8904 char_u *serverid = get_tv_string_chk(&argvars[0]);
8905
8906 if (serverid != NULL && !check_restricted() && !check_secure())
8907 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008908 int timeout = 0;
Bram Moolenaar1662ce12017-03-19 21:47:50 +01008909# ifdef WIN32
8910 /* The server's HWND is encoded in the 'id' parameter */
8911 long_u n = 0;
8912# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008913
8914 if (argvars[1].v_type != VAR_UNKNOWN)
8915 timeout = get_tv_number(&argvars[1]);
8916
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008917# ifdef WIN32
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008918 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
8919 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008920 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008921 if (r == NULL)
8922# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008923 if (check_connection() == FAIL
8924 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
8925 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008926# endif
8927 EMSG(_("E277: Unable to read a server reply"));
8928 }
8929#endif
8930 rettv->v_type = VAR_STRING;
8931 rettv->vval.v_string = r;
8932}
8933
8934/*
8935 * "remote_send()" function
8936 */
8937 static void
8938f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
8939{
8940 rettv->v_type = VAR_STRING;
8941 rettv->vval.v_string = NULL;
8942#ifdef FEAT_CLIENTSERVER
8943 remote_common(argvars, rettv, FALSE);
8944#endif
8945}
8946
8947/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01008948 * "remote_startserver()" function
8949 */
8950 static void
8951f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8952{
8953#ifdef FEAT_CLIENTSERVER
8954 char_u *server = get_tv_string_chk(&argvars[0]);
8955
8956 if (server == NULL)
8957 return; /* type error; errmsg already given */
8958 if (serverName != NULL)
8959 EMSG(_("E941: already started a server"));
8960 else
8961 {
8962# ifdef FEAT_X11
8963 if (check_connection() == OK)
8964 serverRegisterName(X_DISPLAY, server);
8965# else
8966 serverSetName(server);
8967# endif
8968 }
8969#else
8970 EMSG(_("E942: +clientserver feature not available"));
8971#endif
8972}
8973
8974/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008975 * "remove()" function
8976 */
8977 static void
8978f_remove(typval_T *argvars, typval_T *rettv)
8979{
8980 list_T *l;
8981 listitem_T *item, *item2;
8982 listitem_T *li;
8983 long idx;
8984 long end;
8985 char_u *key;
8986 dict_T *d;
8987 dictitem_T *di;
8988 char_u *arg_errmsg = (char_u *)N_("remove() argument");
8989
8990 if (argvars[0].v_type == VAR_DICT)
8991 {
8992 if (argvars[2].v_type != VAR_UNKNOWN)
8993 EMSG2(_(e_toomanyarg), "remove()");
8994 else if ((d = argvars[0].vval.v_dict) != NULL
8995 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
8996 {
8997 key = get_tv_string_chk(&argvars[1]);
8998 if (key != NULL)
8999 {
9000 di = dict_find(d, key, -1);
9001 if (di == NULL)
9002 EMSG2(_(e_dictkey), key);
9003 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
9004 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
9005 {
9006 *rettv = di->di_tv;
9007 init_tv(&di->di_tv);
9008 dictitem_remove(d, di);
9009 }
9010 }
9011 }
9012 }
9013 else if (argvars[0].v_type != VAR_LIST)
9014 EMSG2(_(e_listdictarg), "remove()");
9015 else if ((l = argvars[0].vval.v_list) != NULL
9016 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
9017 {
9018 int error = FALSE;
9019
9020 idx = (long)get_tv_number_chk(&argvars[1], &error);
9021 if (error)
9022 ; /* type error: do nothing, errmsg already given */
9023 else if ((item = list_find(l, idx)) == NULL)
9024 EMSGN(_(e_listidx), idx);
9025 else
9026 {
9027 if (argvars[2].v_type == VAR_UNKNOWN)
9028 {
9029 /* Remove one item, return its value. */
9030 vimlist_remove(l, item, item);
9031 *rettv = item->li_tv;
9032 vim_free(item);
9033 }
9034 else
9035 {
9036 /* Remove range of items, return list with values. */
9037 end = (long)get_tv_number_chk(&argvars[2], &error);
9038 if (error)
9039 ; /* type error: do nothing */
9040 else if ((item2 = list_find(l, end)) == NULL)
9041 EMSGN(_(e_listidx), end);
9042 else
9043 {
9044 int cnt = 0;
9045
9046 for (li = item; li != NULL; li = li->li_next)
9047 {
9048 ++cnt;
9049 if (li == item2)
9050 break;
9051 }
9052 if (li == NULL) /* didn't find "item2" after "item" */
9053 EMSG(_(e_invrange));
9054 else
9055 {
9056 vimlist_remove(l, item, item2);
9057 if (rettv_list_alloc(rettv) == OK)
9058 {
9059 l = rettv->vval.v_list;
9060 l->lv_first = item;
9061 l->lv_last = item2;
9062 item->li_prev = NULL;
9063 item2->li_next = NULL;
9064 l->lv_len = cnt;
9065 }
9066 }
9067 }
9068 }
9069 }
9070 }
9071}
9072
9073/*
9074 * "rename({from}, {to})" function
9075 */
9076 static void
9077f_rename(typval_T *argvars, typval_T *rettv)
9078{
9079 char_u buf[NUMBUFLEN];
9080
9081 if (check_restricted() || check_secure())
9082 rettv->vval.v_number = -1;
9083 else
9084 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
9085 get_tv_string_buf(&argvars[1], buf));
9086}
9087
9088/*
9089 * "repeat()" function
9090 */
9091 static void
9092f_repeat(typval_T *argvars, typval_T *rettv)
9093{
9094 char_u *p;
9095 int n;
9096 int slen;
9097 int len;
9098 char_u *r;
9099 int i;
9100
9101 n = (int)get_tv_number(&argvars[1]);
9102 if (argvars[0].v_type == VAR_LIST)
9103 {
9104 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9105 while (n-- > 0)
9106 if (list_extend(rettv->vval.v_list,
9107 argvars[0].vval.v_list, NULL) == FAIL)
9108 break;
9109 }
9110 else
9111 {
9112 p = get_tv_string(&argvars[0]);
9113 rettv->v_type = VAR_STRING;
9114 rettv->vval.v_string = NULL;
9115
9116 slen = (int)STRLEN(p);
9117 len = slen * n;
9118 if (len <= 0)
9119 return;
9120
9121 r = alloc(len + 1);
9122 if (r != NULL)
9123 {
9124 for (i = 0; i < n; i++)
9125 mch_memmove(r + i * slen, p, (size_t)slen);
9126 r[len] = NUL;
9127 }
9128
9129 rettv->vval.v_string = r;
9130 }
9131}
9132
9133/*
9134 * "resolve()" function
9135 */
9136 static void
9137f_resolve(typval_T *argvars, typval_T *rettv)
9138{
9139 char_u *p;
9140#ifdef HAVE_READLINK
9141 char_u *buf = NULL;
9142#endif
9143
9144 p = get_tv_string(&argvars[0]);
9145#ifdef FEAT_SHORTCUT
9146 {
9147 char_u *v = NULL;
9148
9149 v = mch_resolve_shortcut(p);
9150 if (v != NULL)
9151 rettv->vval.v_string = v;
9152 else
9153 rettv->vval.v_string = vim_strsave(p);
9154 }
9155#else
9156# ifdef HAVE_READLINK
9157 {
9158 char_u *cpy;
9159 int len;
9160 char_u *remain = NULL;
9161 char_u *q;
9162 int is_relative_to_current = FALSE;
9163 int has_trailing_pathsep = FALSE;
9164 int limit = 100;
9165
9166 p = vim_strsave(p);
9167
9168 if (p[0] == '.' && (vim_ispathsep(p[1])
9169 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9170 is_relative_to_current = TRUE;
9171
9172 len = STRLEN(p);
9173 if (len > 0 && after_pathsep(p, p + len))
9174 {
9175 has_trailing_pathsep = TRUE;
9176 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9177 }
9178
9179 q = getnextcomp(p);
9180 if (*q != NUL)
9181 {
9182 /* Separate the first path component in "p", and keep the
9183 * remainder (beginning with the path separator). */
9184 remain = vim_strsave(q - 1);
9185 q[-1] = NUL;
9186 }
9187
9188 buf = alloc(MAXPATHL + 1);
9189 if (buf == NULL)
9190 goto fail;
9191
9192 for (;;)
9193 {
9194 for (;;)
9195 {
9196 len = readlink((char *)p, (char *)buf, MAXPATHL);
9197 if (len <= 0)
9198 break;
9199 buf[len] = NUL;
9200
9201 if (limit-- == 0)
9202 {
9203 vim_free(p);
9204 vim_free(remain);
9205 EMSG(_("E655: Too many symbolic links (cycle?)"));
9206 rettv->vval.v_string = NULL;
9207 goto fail;
9208 }
9209
9210 /* Ensure that the result will have a trailing path separator
9211 * if the argument has one. */
9212 if (remain == NULL && has_trailing_pathsep)
9213 add_pathsep(buf);
9214
9215 /* Separate the first path component in the link value and
9216 * concatenate the remainders. */
9217 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9218 if (*q != NUL)
9219 {
9220 if (remain == NULL)
9221 remain = vim_strsave(q - 1);
9222 else
9223 {
9224 cpy = concat_str(q - 1, remain);
9225 if (cpy != NULL)
9226 {
9227 vim_free(remain);
9228 remain = cpy;
9229 }
9230 }
9231 q[-1] = NUL;
9232 }
9233
9234 q = gettail(p);
9235 if (q > p && *q == NUL)
9236 {
9237 /* Ignore trailing path separator. */
9238 q[-1] = NUL;
9239 q = gettail(p);
9240 }
9241 if (q > p && !mch_isFullName(buf))
9242 {
9243 /* symlink is relative to directory of argument */
9244 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
9245 if (cpy != NULL)
9246 {
9247 STRCPY(cpy, p);
9248 STRCPY(gettail(cpy), buf);
9249 vim_free(p);
9250 p = cpy;
9251 }
9252 }
9253 else
9254 {
9255 vim_free(p);
9256 p = vim_strsave(buf);
9257 }
9258 }
9259
9260 if (remain == NULL)
9261 break;
9262
9263 /* Append the first path component of "remain" to "p". */
9264 q = getnextcomp(remain + 1);
9265 len = q - remain - (*q != NUL);
9266 cpy = vim_strnsave(p, STRLEN(p) + len);
9267 if (cpy != NULL)
9268 {
9269 STRNCAT(cpy, remain, len);
9270 vim_free(p);
9271 p = cpy;
9272 }
9273 /* Shorten "remain". */
9274 if (*q != NUL)
9275 STRMOVE(remain, q - 1);
9276 else
Bram Moolenaard23a8232018-02-10 18:45:26 +01009277 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009278 }
9279
9280 /* If the result is a relative path name, make it explicitly relative to
9281 * the current directory if and only if the argument had this form. */
9282 if (!vim_ispathsep(*p))
9283 {
9284 if (is_relative_to_current
9285 && *p != NUL
9286 && !(p[0] == '.'
9287 && (p[1] == NUL
9288 || vim_ispathsep(p[1])
9289 || (p[1] == '.'
9290 && (p[2] == NUL
9291 || vim_ispathsep(p[2]))))))
9292 {
9293 /* Prepend "./". */
9294 cpy = concat_str((char_u *)"./", p);
9295 if (cpy != NULL)
9296 {
9297 vim_free(p);
9298 p = cpy;
9299 }
9300 }
9301 else if (!is_relative_to_current)
9302 {
9303 /* Strip leading "./". */
9304 q = p;
9305 while (q[0] == '.' && vim_ispathsep(q[1]))
9306 q += 2;
9307 if (q > p)
9308 STRMOVE(p, p + 2);
9309 }
9310 }
9311
9312 /* Ensure that the result will have no trailing path separator
9313 * if the argument had none. But keep "/" or "//". */
9314 if (!has_trailing_pathsep)
9315 {
9316 q = p + STRLEN(p);
9317 if (after_pathsep(p, q))
9318 *gettail_sep(p) = NUL;
9319 }
9320
9321 rettv->vval.v_string = p;
9322 }
9323# else
9324 rettv->vval.v_string = vim_strsave(p);
9325# endif
9326#endif
9327
9328 simplify_filename(rettv->vval.v_string);
9329
9330#ifdef HAVE_READLINK
9331fail:
9332 vim_free(buf);
9333#endif
9334 rettv->v_type = VAR_STRING;
9335}
9336
9337/*
9338 * "reverse({list})" function
9339 */
9340 static void
9341f_reverse(typval_T *argvars, typval_T *rettv)
9342{
9343 list_T *l;
9344 listitem_T *li, *ni;
9345
9346 if (argvars[0].v_type != VAR_LIST)
9347 EMSG2(_(e_listarg), "reverse()");
9348 else if ((l = argvars[0].vval.v_list) != NULL
9349 && !tv_check_lock(l->lv_lock,
9350 (char_u *)N_("reverse() argument"), TRUE))
9351 {
9352 li = l->lv_last;
9353 l->lv_first = l->lv_last = NULL;
9354 l->lv_len = 0;
9355 while (li != NULL)
9356 {
9357 ni = li->li_prev;
9358 list_append(l, li);
9359 li = ni;
9360 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009361 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009362 l->lv_idx = l->lv_len - l->lv_idx - 1;
9363 }
9364}
9365
9366#define SP_NOMOVE 0x01 /* don't move cursor */
9367#define SP_REPEAT 0x02 /* repeat to find outer pair */
9368#define SP_RETCOUNT 0x04 /* return matchcount */
9369#define SP_SETPCMARK 0x08 /* set previous context mark */
9370#define SP_START 0x10 /* accept match at start position */
9371#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9372#define SP_END 0x40 /* leave cursor at end of match */
9373#define SP_COLUMN 0x80 /* start at cursor column */
9374
9375static int get_search_arg(typval_T *varp, int *flagsp);
9376
9377/*
9378 * Get flags for a search function.
9379 * Possibly sets "p_ws".
9380 * Returns BACKWARD, FORWARD or zero (for an error).
9381 */
9382 static int
9383get_search_arg(typval_T *varp, int *flagsp)
9384{
9385 int dir = FORWARD;
9386 char_u *flags;
9387 char_u nbuf[NUMBUFLEN];
9388 int mask;
9389
9390 if (varp->v_type != VAR_UNKNOWN)
9391 {
9392 flags = get_tv_string_buf_chk(varp, nbuf);
9393 if (flags == NULL)
9394 return 0; /* type error; errmsg already given */
9395 while (*flags != NUL)
9396 {
9397 switch (*flags)
9398 {
9399 case 'b': dir = BACKWARD; break;
9400 case 'w': p_ws = TRUE; break;
9401 case 'W': p_ws = FALSE; break;
9402 default: mask = 0;
9403 if (flagsp != NULL)
9404 switch (*flags)
9405 {
9406 case 'c': mask = SP_START; break;
9407 case 'e': mask = SP_END; break;
9408 case 'm': mask = SP_RETCOUNT; break;
9409 case 'n': mask = SP_NOMOVE; break;
9410 case 'p': mask = SP_SUBPAT; break;
9411 case 'r': mask = SP_REPEAT; break;
9412 case 's': mask = SP_SETPCMARK; break;
9413 case 'z': mask = SP_COLUMN; break;
9414 }
9415 if (mask == 0)
9416 {
9417 EMSG2(_(e_invarg2), flags);
9418 dir = 0;
9419 }
9420 else
9421 *flagsp |= mask;
9422 }
9423 if (dir == 0)
9424 break;
9425 ++flags;
9426 }
9427 }
9428 return dir;
9429}
9430
9431/*
9432 * Shared by search() and searchpos() functions.
9433 */
9434 static int
9435search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9436{
9437 int flags;
9438 char_u *pat;
9439 pos_T pos;
9440 pos_T save_cursor;
9441 int save_p_ws = p_ws;
9442 int dir;
9443 int retval = 0; /* default: FAIL */
9444 long lnum_stop = 0;
9445 proftime_T tm;
9446#ifdef FEAT_RELTIME
9447 long time_limit = 0;
9448#endif
9449 int options = SEARCH_KEEP;
9450 int subpatnum;
9451
9452 pat = get_tv_string(&argvars[0]);
9453 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
9454 if (dir == 0)
9455 goto theend;
9456 flags = *flagsp;
9457 if (flags & SP_START)
9458 options |= SEARCH_START;
9459 if (flags & SP_END)
9460 options |= SEARCH_END;
9461 if (flags & SP_COLUMN)
9462 options |= SEARCH_COL;
9463
9464 /* Optional arguments: line number to stop searching and timeout. */
9465 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9466 {
9467 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
9468 if (lnum_stop < 0)
9469 goto theend;
9470#ifdef FEAT_RELTIME
9471 if (argvars[3].v_type != VAR_UNKNOWN)
9472 {
9473 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
9474 if (time_limit < 0)
9475 goto theend;
9476 }
9477#endif
9478 }
9479
9480#ifdef FEAT_RELTIME
9481 /* Set the time limit, if there is one. */
9482 profile_setlimit(time_limit, &tm);
9483#endif
9484
9485 /*
9486 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
9487 * Check to make sure only those flags are set.
9488 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
9489 * flags cannot be set. Check for that condition also.
9490 */
9491 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
9492 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9493 {
9494 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
9495 goto theend;
9496 }
9497
9498 pos = save_cursor = curwin->w_cursor;
9499 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009500 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009501 if (subpatnum != FAIL)
9502 {
9503 if (flags & SP_SUBPAT)
9504 retval = subpatnum;
9505 else
9506 retval = pos.lnum;
9507 if (flags & SP_SETPCMARK)
9508 setpcmark();
9509 curwin->w_cursor = pos;
9510 if (match_pos != NULL)
9511 {
9512 /* Store the match cursor position */
9513 match_pos->lnum = pos.lnum;
9514 match_pos->col = pos.col + 1;
9515 }
9516 /* "/$" will put the cursor after the end of the line, may need to
9517 * correct that here */
9518 check_cursor();
9519 }
9520
9521 /* If 'n' flag is used: restore cursor position. */
9522 if (flags & SP_NOMOVE)
9523 curwin->w_cursor = save_cursor;
9524 else
9525 curwin->w_set_curswant = TRUE;
9526theend:
9527 p_ws = save_p_ws;
9528
9529 return retval;
9530}
9531
9532#ifdef FEAT_FLOAT
9533
9534/*
9535 * round() is not in C90, use ceil() or floor() instead.
9536 */
9537 float_T
9538vim_round(float_T f)
9539{
9540 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
9541}
9542
9543/*
9544 * "round({float})" function
9545 */
9546 static void
9547f_round(typval_T *argvars, typval_T *rettv)
9548{
9549 float_T f = 0.0;
9550
9551 rettv->v_type = VAR_FLOAT;
9552 if (get_float_arg(argvars, &f) == OK)
9553 rettv->vval.v_float = vim_round(f);
9554 else
9555 rettv->vval.v_float = 0.0;
9556}
9557#endif
9558
9559/*
9560 * "screenattr()" function
9561 */
9562 static void
9563f_screenattr(typval_T *argvars, typval_T *rettv)
9564{
9565 int row;
9566 int col;
9567 int c;
9568
9569 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9570 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9571 if (row < 0 || row >= screen_Rows
9572 || col < 0 || col >= screen_Columns)
9573 c = -1;
9574 else
9575 c = ScreenAttrs[LineOffset[row] + col];
9576 rettv->vval.v_number = c;
9577}
9578
9579/*
9580 * "screenchar()" function
9581 */
9582 static void
9583f_screenchar(typval_T *argvars, typval_T *rettv)
9584{
9585 int row;
9586 int col;
9587 int off;
9588 int c;
9589
9590 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9591 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9592 if (row < 0 || row >= screen_Rows
9593 || col < 0 || col >= screen_Columns)
9594 c = -1;
9595 else
9596 {
9597 off = LineOffset[row] + col;
9598#ifdef FEAT_MBYTE
9599 if (enc_utf8 && ScreenLinesUC[off] != 0)
9600 c = ScreenLinesUC[off];
9601 else
9602#endif
9603 c = ScreenLines[off];
9604 }
9605 rettv->vval.v_number = c;
9606}
9607
9608/*
9609 * "screencol()" function
9610 *
9611 * First column is 1 to be consistent with virtcol().
9612 */
9613 static void
9614f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
9615{
9616 rettv->vval.v_number = screen_screencol() + 1;
9617}
9618
9619/*
9620 * "screenrow()" function
9621 */
9622 static void
9623f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
9624{
9625 rettv->vval.v_number = screen_screenrow() + 1;
9626}
9627
9628/*
9629 * "search()" function
9630 */
9631 static void
9632f_search(typval_T *argvars, typval_T *rettv)
9633{
9634 int flags = 0;
9635
9636 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
9637}
9638
9639/*
9640 * "searchdecl()" function
9641 */
9642 static void
9643f_searchdecl(typval_T *argvars, typval_T *rettv)
9644{
9645 int locally = 1;
9646 int thisblock = 0;
9647 int error = FALSE;
9648 char_u *name;
9649
9650 rettv->vval.v_number = 1; /* default: FAIL */
9651
9652 name = get_tv_string_chk(&argvars[0]);
9653 if (argvars[1].v_type != VAR_UNKNOWN)
9654 {
9655 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
9656 if (!error && argvars[2].v_type != VAR_UNKNOWN)
9657 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
9658 }
9659 if (!error && name != NULL)
9660 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
9661 locally, thisblock, SEARCH_KEEP) == FAIL;
9662}
9663
9664/*
9665 * Used by searchpair() and searchpairpos()
9666 */
9667 static int
9668searchpair_cmn(typval_T *argvars, pos_T *match_pos)
9669{
9670 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01009671 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009672 int save_p_ws = p_ws;
9673 int dir;
9674 int flags = 0;
9675 char_u nbuf1[NUMBUFLEN];
9676 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009677 int retval = 0; /* default: FAIL */
9678 long lnum_stop = 0;
9679 long time_limit = 0;
9680
9681 /* Get the three pattern arguments: start, middle, end. */
9682 spat = get_tv_string_chk(&argvars[0]);
9683 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
9684 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
9685 if (spat == NULL || mpat == NULL || epat == NULL)
9686 goto theend; /* type error */
9687
9688 /* Handle the optional fourth argument: flags */
9689 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
9690 if (dir == 0)
9691 goto theend;
9692
9693 /* Don't accept SP_END or SP_SUBPAT.
9694 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
9695 */
9696 if ((flags & (SP_END | SP_SUBPAT)) != 0
9697 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9698 {
9699 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
9700 goto theend;
9701 }
9702
9703 /* Using 'r' implies 'W', otherwise it doesn't work. */
9704 if (flags & SP_REPEAT)
9705 p_ws = FALSE;
9706
9707 /* Optional fifth argument: skip expression */
9708 if (argvars[3].v_type == VAR_UNKNOWN
9709 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01009710 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009711 else
9712 {
Bram Moolenaar48570482017-10-30 21:48:41 +01009713 skip = &argvars[4];
9714 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
9715 && skip->v_type != VAR_STRING)
9716 {
9717 /* Type error */
9718 goto theend;
9719 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009720 if (argvars[5].v_type != VAR_UNKNOWN)
9721 {
9722 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
9723 if (lnum_stop < 0)
9724 goto theend;
9725#ifdef FEAT_RELTIME
9726 if (argvars[6].v_type != VAR_UNKNOWN)
9727 {
9728 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
9729 if (time_limit < 0)
9730 goto theend;
9731 }
9732#endif
9733 }
9734 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009735
9736 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
9737 match_pos, lnum_stop, time_limit);
9738
9739theend:
9740 p_ws = save_p_ws;
9741
9742 return retval;
9743}
9744
9745/*
9746 * "searchpair()" function
9747 */
9748 static void
9749f_searchpair(typval_T *argvars, typval_T *rettv)
9750{
9751 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
9752}
9753
9754/*
9755 * "searchpairpos()" function
9756 */
9757 static void
9758f_searchpairpos(typval_T *argvars, typval_T *rettv)
9759{
9760 pos_T match_pos;
9761 int lnum = 0;
9762 int col = 0;
9763
9764 if (rettv_list_alloc(rettv) == FAIL)
9765 return;
9766
9767 if (searchpair_cmn(argvars, &match_pos) > 0)
9768 {
9769 lnum = match_pos.lnum;
9770 col = match_pos.col;
9771 }
9772
9773 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9774 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9775}
9776
9777/*
9778 * Search for a start/middle/end thing.
9779 * Used by searchpair(), see its documentation for the details.
9780 * Returns 0 or -1 for no match,
9781 */
9782 long
9783do_searchpair(
9784 char_u *spat, /* start pattern */
9785 char_u *mpat, /* middle pattern */
9786 char_u *epat, /* end pattern */
9787 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +01009788 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009789 int flags, /* SP_SETPCMARK and other SP_ values */
9790 pos_T *match_pos,
9791 linenr_T lnum_stop, /* stop at this line if not zero */
9792 long time_limit UNUSED) /* stop after this many msec */
9793{
9794 char_u *save_cpo;
9795 char_u *pat, *pat2 = NULL, *pat3 = NULL;
9796 long retval = 0;
9797 pos_T pos;
9798 pos_T firstpos;
9799 pos_T foundpos;
9800 pos_T save_cursor;
9801 pos_T save_pos;
9802 int n;
9803 int r;
9804 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01009805 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009806 int err;
9807 int options = SEARCH_KEEP;
9808 proftime_T tm;
9809
9810 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
9811 save_cpo = p_cpo;
9812 p_cpo = empty_option;
9813
9814#ifdef FEAT_RELTIME
9815 /* Set the time limit, if there is one. */
9816 profile_setlimit(time_limit, &tm);
9817#endif
9818
9819 /* Make two search patterns: start/end (pat2, for in nested pairs) and
9820 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009821 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
9822 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009823 if (pat2 == NULL || pat3 == NULL)
9824 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009825 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009826 if (*mpat == NUL)
9827 STRCPY(pat3, pat2);
9828 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009829 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009830 spat, epat, mpat);
9831 if (flags & SP_START)
9832 options |= SEARCH_START;
9833
Bram Moolenaar48570482017-10-30 21:48:41 +01009834 if (skip != NULL)
9835 {
9836 /* Empty string means to not use the skip expression. */
9837 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
9838 use_skip = skip->vval.v_string != NULL
9839 && *skip->vval.v_string != NUL;
9840 }
9841
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009842 save_cursor = curwin->w_cursor;
9843 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009844 CLEAR_POS(&firstpos);
9845 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009846 pat = pat3;
9847 for (;;)
9848 {
9849 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009850 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009851 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009852 /* didn't find it or found the first match again: FAIL */
9853 break;
9854
9855 if (firstpos.lnum == 0)
9856 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009857 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009858 {
9859 /* Found the same position again. Can happen with a pattern that
9860 * has "\zs" at the end and searching backwards. Advance one
9861 * character and try again. */
9862 if (dir == BACKWARD)
9863 decl(&pos);
9864 else
9865 incl(&pos);
9866 }
9867 foundpos = pos;
9868
9869 /* clear the start flag to avoid getting stuck here */
9870 options &= ~SEARCH_START;
9871
9872 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +01009873 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009874 {
9875 save_pos = curwin->w_cursor;
9876 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01009877 err = FALSE;
9878 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009879 curwin->w_cursor = save_pos;
9880 if (err)
9881 {
9882 /* Evaluating {skip} caused an error, break here. */
9883 curwin->w_cursor = save_cursor;
9884 retval = -1;
9885 break;
9886 }
9887 if (r)
9888 continue;
9889 }
9890
9891 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
9892 {
9893 /* Found end when searching backwards or start when searching
9894 * forward: nested pair. */
9895 ++nest;
9896 pat = pat2; /* nested, don't search for middle */
9897 }
9898 else
9899 {
9900 /* Found end when searching forward or start when searching
9901 * backward: end of (nested) pair; or found middle in outer pair. */
9902 if (--nest == 1)
9903 pat = pat3; /* outer level, search for middle */
9904 }
9905
9906 if (nest == 0)
9907 {
9908 /* Found the match: return matchcount or line number. */
9909 if (flags & SP_RETCOUNT)
9910 ++retval;
9911 else
9912 retval = pos.lnum;
9913 if (flags & SP_SETPCMARK)
9914 setpcmark();
9915 curwin->w_cursor = pos;
9916 if (!(flags & SP_REPEAT))
9917 break;
9918 nest = 1; /* search for next unmatched */
9919 }
9920 }
9921
9922 if (match_pos != NULL)
9923 {
9924 /* Store the match cursor position */
9925 match_pos->lnum = curwin->w_cursor.lnum;
9926 match_pos->col = curwin->w_cursor.col + 1;
9927 }
9928
9929 /* If 'n' flag is used or search failed: restore cursor position. */
9930 if ((flags & SP_NOMOVE) || retval == 0)
9931 curwin->w_cursor = save_cursor;
9932
9933theend:
9934 vim_free(pat2);
9935 vim_free(pat3);
9936 if (p_cpo == empty_option)
9937 p_cpo = save_cpo;
9938 else
9939 /* Darn, evaluating the {skip} expression changed the value. */
9940 free_string_option(save_cpo);
9941
9942 return retval;
9943}
9944
9945/*
9946 * "searchpos()" function
9947 */
9948 static void
9949f_searchpos(typval_T *argvars, typval_T *rettv)
9950{
9951 pos_T match_pos;
9952 int lnum = 0;
9953 int col = 0;
9954 int n;
9955 int flags = 0;
9956
9957 if (rettv_list_alloc(rettv) == FAIL)
9958 return;
9959
9960 n = search_cmn(argvars, &match_pos, &flags);
9961 if (n > 0)
9962 {
9963 lnum = match_pos.lnum;
9964 col = match_pos.col;
9965 }
9966
9967 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9968 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9969 if (flags & SP_SUBPAT)
9970 list_append_number(rettv->vval.v_list, (varnumber_T)n);
9971}
9972
9973 static void
9974f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
9975{
9976#ifdef FEAT_CLIENTSERVER
9977 char_u buf[NUMBUFLEN];
9978 char_u *server = get_tv_string_chk(&argvars[0]);
9979 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
9980
9981 rettv->vval.v_number = -1;
9982 if (server == NULL || reply == NULL)
9983 return;
9984 if (check_restricted() || check_secure())
9985 return;
9986# ifdef FEAT_X11
9987 if (check_connection() == FAIL)
9988 return;
9989# endif
9990
9991 if (serverSendReply(server, reply) < 0)
9992 {
9993 EMSG(_("E258: Unable to send to client"));
9994 return;
9995 }
9996 rettv->vval.v_number = 0;
9997#else
9998 rettv->vval.v_number = -1;
9999#endif
10000}
10001
10002 static void
10003f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10004{
10005 char_u *r = NULL;
10006
10007#ifdef FEAT_CLIENTSERVER
10008# ifdef WIN32
10009 r = serverGetVimNames();
10010# else
10011 make_connection();
10012 if (X_DISPLAY != NULL)
10013 r = serverGetVimNames(X_DISPLAY);
10014# endif
10015#endif
10016 rettv->v_type = VAR_STRING;
10017 rettv->vval.v_string = r;
10018}
10019
10020/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010021 * Set line or list of lines in buffer "buf".
10022 */
10023 static void
10024set_buffer_lines(buf_T *buf, linenr_T lnum, typval_T *lines, typval_T *rettv)
10025{
10026 char_u *line = NULL;
10027 list_T *l = NULL;
10028 listitem_T *li = NULL;
10029 long added = 0;
10030 linenr_T lcount;
Bram Moolenaar0c4dc882017-11-06 21:32:54 +010010031 buf_T *curbuf_save = NULL;
10032 win_T *curwin_save = NULL;
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010033 int is_curbuf = buf == curbuf;
10034
Bram Moolenaar9d954202017-09-04 20:34:19 +020010035 /* When using the current buffer ml_mfp will be set if needed. Useful when
10036 * setline() is used on startup. For other buffers the buffer must be
10037 * loaded. */
10038 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010039 {
10040 rettv->vval.v_number = 1; /* FAIL */
10041 return;
10042 }
10043
Bram Moolenaar0c4dc882017-11-06 21:32:54 +010010044 if (!is_curbuf)
10045 {
10046 wininfo_T *wip;
10047
10048 curbuf_save = curbuf;
10049 curwin_save = curwin;
10050 curbuf = buf;
10051 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
10052 {
10053 if (wip->wi_win != NULL)
10054 {
10055 curwin = wip->wi_win;
10056 break;
10057 }
10058 }
10059 }
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010060
10061 lcount = curbuf->b_ml.ml_line_count;
10062
10063 if (lines->v_type == VAR_LIST)
10064 {
10065 l = lines->vval.v_list;
10066 li = l->lv_first;
10067 }
10068 else
10069 line = get_tv_string_chk(lines);
10070
10071 /* default result is zero == OK */
10072 for (;;)
10073 {
10074 if (l != NULL)
10075 {
10076 /* list argument, get next string */
10077 if (li == NULL)
10078 break;
10079 line = get_tv_string_chk(&li->li_tv);
10080 li = li->li_next;
10081 }
10082
10083 rettv->vval.v_number = 1; /* FAIL */
10084 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
10085 break;
10086
10087 /* When coming here from Insert mode, sync undo, so that this can be
10088 * undone separately from what was previously inserted. */
10089 if (u_sync_once == 2)
10090 {
10091 u_sync_once = 1; /* notify that u_sync() was called */
10092 u_sync(TRUE);
10093 }
10094
10095 if (lnum <= curbuf->b_ml.ml_line_count)
10096 {
10097 /* existing line, replace it */
10098 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
10099 {
10100 changed_bytes(lnum, 0);
10101 if (is_curbuf && lnum == curwin->w_cursor.lnum)
10102 check_cursor_col();
10103 rettv->vval.v_number = 0; /* OK */
10104 }
10105 }
10106 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
10107 {
10108 /* lnum is one past the last line, append the line */
10109 ++added;
10110 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
10111 rettv->vval.v_number = 0; /* OK */
10112 }
10113
10114 if (l == NULL) /* only one string argument */
10115 break;
10116 ++lnum;
10117 }
10118
10119 if (added > 0)
10120 appended_lines_mark(lcount, added);
10121
Bram Moolenaar0c4dc882017-11-06 21:32:54 +010010122 if (!is_curbuf)
10123 {
10124 curbuf = curbuf_save;
10125 curwin = curwin_save;
10126 }
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010127}
10128
10129/*
10130 * "setbufline()" function
10131 */
10132 static void
10133f_setbufline(argvars, rettv)
10134 typval_T *argvars;
10135 typval_T *rettv;
10136{
10137 linenr_T lnum;
10138 buf_T *buf;
10139
10140 buf = get_buf_tv(&argvars[0], FALSE);
10141 if (buf == NULL)
10142 rettv->vval.v_number = 1; /* FAIL */
10143 else
10144 {
10145 lnum = get_tv_lnum_buf(&argvars[1], buf);
10146
10147 set_buffer_lines(buf, lnum, &argvars[2], rettv);
10148 }
10149}
10150
10151/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010152 * "setbufvar()" function
10153 */
10154 static void
10155f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10156{
10157 buf_T *buf;
10158 char_u *varname, *bufvarname;
10159 typval_T *varp;
10160 char_u nbuf[NUMBUFLEN];
10161
10162 if (check_restricted() || check_secure())
10163 return;
10164 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10165 varname = get_tv_string_chk(&argvars[1]);
10166 buf = get_buf_tv(&argvars[0], FALSE);
10167 varp = &argvars[2];
10168
10169 if (buf != NULL && varname != NULL && varp != NULL)
10170 {
10171 if (*varname == '&')
10172 {
10173 long numval;
10174 char_u *strval;
10175 int error = FALSE;
10176 aco_save_T aco;
10177
10178 /* set curbuf to be our buf, temporarily */
10179 aucmd_prepbuf(&aco, buf);
10180
10181 ++varname;
10182 numval = (long)get_tv_number_chk(varp, &error);
10183 strval = get_tv_string_buf_chk(varp, nbuf);
10184 if (!error && strval != NULL)
10185 set_option_value(varname, numval, strval, OPT_LOCAL);
10186
10187 /* reset notion of buffer */
10188 aucmd_restbuf(&aco);
10189 }
10190 else
10191 {
10192 buf_T *save_curbuf = curbuf;
10193
10194 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
10195 if (bufvarname != NULL)
10196 {
10197 curbuf = buf;
10198 STRCPY(bufvarname, "b:");
10199 STRCPY(bufvarname + 2, varname);
10200 set_var(bufvarname, varp, TRUE);
10201 vim_free(bufvarname);
10202 curbuf = save_curbuf;
10203 }
10204 }
10205 }
10206}
10207
10208 static void
10209f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10210{
10211 dict_T *d;
10212 dictitem_T *di;
10213 char_u *csearch;
10214
10215 if (argvars[0].v_type != VAR_DICT)
10216 {
10217 EMSG(_(e_dictreq));
10218 return;
10219 }
10220
10221 if ((d = argvars[0].vval.v_dict) != NULL)
10222 {
10223 csearch = get_dict_string(d, (char_u *)"char", FALSE);
10224 if (csearch != NULL)
10225 {
10226#ifdef FEAT_MBYTE
10227 if (enc_utf8)
10228 {
10229 int pcc[MAX_MCO];
10230 int c = utfc_ptr2char(csearch, pcc);
10231
10232 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10233 }
10234 else
10235#endif
10236 set_last_csearch(PTR2CHAR(csearch),
10237 csearch, MB_PTR2LEN(csearch));
10238 }
10239
10240 di = dict_find(d, (char_u *)"forward", -1);
10241 if (di != NULL)
10242 set_csearch_direction((int)get_tv_number(&di->di_tv)
10243 ? FORWARD : BACKWARD);
10244
10245 di = dict_find(d, (char_u *)"until", -1);
10246 if (di != NULL)
10247 set_csearch_until(!!get_tv_number(&di->di_tv));
10248 }
10249}
10250
10251/*
10252 * "setcmdpos()" function
10253 */
10254 static void
10255f_setcmdpos(typval_T *argvars, typval_T *rettv)
10256{
10257 int pos = (int)get_tv_number(&argvars[0]) - 1;
10258
10259 if (pos >= 0)
10260 rettv->vval.v_number = set_cmdline_pos(pos);
10261}
10262
10263/*
10264 * "setfperm({fname}, {mode})" function
10265 */
10266 static void
10267f_setfperm(typval_T *argvars, typval_T *rettv)
10268{
10269 char_u *fname;
10270 char_u modebuf[NUMBUFLEN];
10271 char_u *mode_str;
10272 int i;
10273 int mask;
10274 int mode = 0;
10275
10276 rettv->vval.v_number = 0;
10277 fname = get_tv_string_chk(&argvars[0]);
10278 if (fname == NULL)
10279 return;
10280 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
10281 if (mode_str == NULL)
10282 return;
10283 if (STRLEN(mode_str) != 9)
10284 {
10285 EMSG2(_(e_invarg2), mode_str);
10286 return;
10287 }
10288
10289 mask = 1;
10290 for (i = 8; i >= 0; --i)
10291 {
10292 if (mode_str[i] != '-')
10293 mode |= mask;
10294 mask = mask << 1;
10295 }
10296 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10297}
10298
10299/*
10300 * "setline()" function
10301 */
10302 static void
10303f_setline(typval_T *argvars, typval_T *rettv)
10304{
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010305 linenr_T lnum = get_tv_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010306
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010307 set_buffer_lines(curbuf, lnum, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010308}
10309
Bram Moolenaard823fa92016-08-12 16:29:27 +020010310static 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 +020010311
10312/*
10313 * Used by "setqflist()" and "setloclist()" functions
10314 */
10315 static void
10316set_qf_ll_list(
10317 win_T *wp UNUSED,
10318 typval_T *list_arg UNUSED,
10319 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010320 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010321 typval_T *rettv)
10322{
10323#ifdef FEAT_QUICKFIX
10324 static char *e_invact = N_("E927: Invalid action: '%s'");
10325 char_u *act;
10326 int action = 0;
10327#endif
10328
10329 rettv->vval.v_number = -1;
10330
10331#ifdef FEAT_QUICKFIX
10332 if (list_arg->v_type != VAR_LIST)
10333 EMSG(_(e_listreq));
10334 else
10335 {
10336 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010337 dict_T *d = NULL;
10338 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010339
10340 if (action_arg->v_type == VAR_STRING)
10341 {
10342 act = get_tv_string_chk(action_arg);
10343 if (act == NULL)
10344 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020010345 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
10346 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010347 action = *act;
10348 else
10349 EMSG2(_(e_invact), act);
10350 }
10351 else if (action_arg->v_type == VAR_UNKNOWN)
10352 action = ' ';
10353 else
10354 EMSG(_(e_stringreq));
10355
Bram Moolenaard823fa92016-08-12 16:29:27 +020010356 if (action_arg->v_type != VAR_UNKNOWN
10357 && what_arg->v_type != VAR_UNKNOWN)
10358 {
10359 if (what_arg->v_type == VAR_DICT)
10360 d = what_arg->vval.v_dict;
10361 else
10362 {
10363 EMSG(_(e_dictreq));
10364 valid_dict = FALSE;
10365 }
10366 }
10367
10368 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
10369 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()"), d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010370 rettv->vval.v_number = 0;
10371 }
10372#endif
10373}
10374
10375/*
10376 * "setloclist()" function
10377 */
10378 static void
10379f_setloclist(typval_T *argvars, typval_T *rettv)
10380{
10381 win_T *win;
10382
10383 rettv->vval.v_number = -1;
10384
10385 win = find_win_by_nr(&argvars[0], NULL);
10386 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020010387 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010388}
10389
10390/*
10391 * "setmatches()" function
10392 */
10393 static void
10394f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10395{
10396#ifdef FEAT_SEARCH_EXTRA
10397 list_T *l;
10398 listitem_T *li;
10399 dict_T *d;
10400 list_T *s = NULL;
10401
10402 rettv->vval.v_number = -1;
10403 if (argvars[0].v_type != VAR_LIST)
10404 {
10405 EMSG(_(e_listreq));
10406 return;
10407 }
10408 if ((l = argvars[0].vval.v_list) != NULL)
10409 {
10410
10411 /* To some extent make sure that we are dealing with a list from
10412 * "getmatches()". */
10413 li = l->lv_first;
10414 while (li != NULL)
10415 {
10416 if (li->li_tv.v_type != VAR_DICT
10417 || (d = li->li_tv.vval.v_dict) == NULL)
10418 {
10419 EMSG(_(e_invarg));
10420 return;
10421 }
10422 if (!(dict_find(d, (char_u *)"group", -1) != NULL
10423 && (dict_find(d, (char_u *)"pattern", -1) != NULL
10424 || dict_find(d, (char_u *)"pos1", -1) != NULL)
10425 && dict_find(d, (char_u *)"priority", -1) != NULL
10426 && dict_find(d, (char_u *)"id", -1) != NULL))
10427 {
10428 EMSG(_(e_invarg));
10429 return;
10430 }
10431 li = li->li_next;
10432 }
10433
10434 clear_matches(curwin);
10435 li = l->lv_first;
10436 while (li != NULL)
10437 {
10438 int i = 0;
10439 char_u buf[5];
10440 dictitem_T *di;
10441 char_u *group;
10442 int priority;
10443 int id;
10444 char_u *conceal;
10445
10446 d = li->li_tv.vval.v_dict;
10447 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
10448 {
10449 if (s == NULL)
10450 {
10451 s = list_alloc();
10452 if (s == NULL)
10453 return;
10454 }
10455
10456 /* match from matchaddpos() */
10457 for (i = 1; i < 9; i++)
10458 {
10459 sprintf((char *)buf, (char *)"pos%d", i);
10460 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
10461 {
10462 if (di->di_tv.v_type != VAR_LIST)
10463 return;
10464
10465 list_append_tv(s, &di->di_tv);
10466 s->lv_refcount++;
10467 }
10468 else
10469 break;
10470 }
10471 }
10472
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010473 group = get_dict_string(d, (char_u *)"group", TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010474 priority = (int)get_dict_number(d, (char_u *)"priority");
10475 id = (int)get_dict_number(d, (char_u *)"id");
10476 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010477 ? get_dict_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010478 : NULL;
10479 if (i == 0)
10480 {
10481 match_add(curwin, group,
10482 get_dict_string(d, (char_u *)"pattern", FALSE),
10483 priority, id, NULL, conceal);
10484 }
10485 else
10486 {
10487 match_add(curwin, group, NULL, priority, id, s, conceal);
10488 list_unref(s);
10489 s = NULL;
10490 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010491 vim_free(group);
10492 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010493
10494 li = li->li_next;
10495 }
10496 rettv->vval.v_number = 0;
10497 }
10498#endif
10499}
10500
10501/*
10502 * "setpos()" function
10503 */
10504 static void
10505f_setpos(typval_T *argvars, typval_T *rettv)
10506{
10507 pos_T pos;
10508 int fnum;
10509 char_u *name;
10510 colnr_T curswant = -1;
10511
10512 rettv->vval.v_number = -1;
10513 name = get_tv_string_chk(argvars);
10514 if (name != NULL)
10515 {
10516 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10517 {
10518 if (--pos.col < 0)
10519 pos.col = 0;
10520 if (name[0] == '.' && name[1] == NUL)
10521 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010522 /* set cursor; "fnum" is ignored */
10523 curwin->w_cursor = pos;
10524 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010525 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010526 curwin->w_curswant = curswant - 1;
10527 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010528 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010529 check_cursor();
10530 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010531 }
10532 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10533 {
10534 /* set mark */
10535 if (setmark_pos(name[1], &pos, fnum) == OK)
10536 rettv->vval.v_number = 0;
10537 }
10538 else
10539 EMSG(_(e_invarg));
10540 }
10541 }
10542}
10543
10544/*
10545 * "setqflist()" function
10546 */
10547 static void
10548f_setqflist(typval_T *argvars, typval_T *rettv)
10549{
Bram Moolenaard823fa92016-08-12 16:29:27 +020010550 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010551}
10552
10553/*
10554 * "setreg()" function
10555 */
10556 static void
10557f_setreg(typval_T *argvars, typval_T *rettv)
10558{
10559 int regname;
10560 char_u *strregname;
10561 char_u *stropt;
10562 char_u *strval;
10563 int append;
10564 char_u yank_type;
10565 long block_len;
10566
10567 block_len = -1;
10568 yank_type = MAUTO;
10569 append = FALSE;
10570
10571 strregname = get_tv_string_chk(argvars);
10572 rettv->vval.v_number = 1; /* FAIL is default */
10573
10574 if (strregname == NULL)
10575 return; /* type error; errmsg already given */
10576 regname = *strregname;
10577 if (regname == 0 || regname == '@')
10578 regname = '"';
10579
10580 if (argvars[2].v_type != VAR_UNKNOWN)
10581 {
10582 stropt = get_tv_string_chk(&argvars[2]);
10583 if (stropt == NULL)
10584 return; /* type error */
10585 for (; *stropt != NUL; ++stropt)
10586 switch (*stropt)
10587 {
10588 case 'a': case 'A': /* append */
10589 append = TRUE;
10590 break;
10591 case 'v': case 'c': /* character-wise selection */
10592 yank_type = MCHAR;
10593 break;
10594 case 'V': case 'l': /* line-wise selection */
10595 yank_type = MLINE;
10596 break;
10597 case 'b': case Ctrl_V: /* block-wise selection */
10598 yank_type = MBLOCK;
10599 if (VIM_ISDIGIT(stropt[1]))
10600 {
10601 ++stropt;
10602 block_len = getdigits(&stropt) - 1;
10603 --stropt;
10604 }
10605 break;
10606 }
10607 }
10608
10609 if (argvars[1].v_type == VAR_LIST)
10610 {
10611 char_u **lstval;
10612 char_u **allocval;
10613 char_u buf[NUMBUFLEN];
10614 char_u **curval;
10615 char_u **curallocval;
10616 list_T *ll = argvars[1].vval.v_list;
10617 listitem_T *li;
10618 int len;
10619
10620 /* If the list is NULL handle like an empty list. */
10621 len = ll == NULL ? 0 : ll->lv_len;
10622
10623 /* First half: use for pointers to result lines; second half: use for
10624 * pointers to allocated copies. */
10625 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
10626 if (lstval == NULL)
10627 return;
10628 curval = lstval;
10629 allocval = lstval + len + 2;
10630 curallocval = allocval;
10631
10632 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
10633 li = li->li_next)
10634 {
10635 strval = get_tv_string_buf_chk(&li->li_tv, buf);
10636 if (strval == NULL)
10637 goto free_lstval;
10638 if (strval == buf)
10639 {
10640 /* Need to make a copy, next get_tv_string_buf_chk() will
10641 * overwrite the string. */
10642 strval = vim_strsave(buf);
10643 if (strval == NULL)
10644 goto free_lstval;
10645 *curallocval++ = strval;
10646 }
10647 *curval++ = strval;
10648 }
10649 *curval++ = NULL;
10650
10651 write_reg_contents_lst(regname, lstval, -1,
10652 append, yank_type, block_len);
10653free_lstval:
10654 while (curallocval > allocval)
10655 vim_free(*--curallocval);
10656 vim_free(lstval);
10657 }
10658 else
10659 {
10660 strval = get_tv_string_chk(&argvars[1]);
10661 if (strval == NULL)
10662 return;
10663 write_reg_contents_ex(regname, strval, -1,
10664 append, yank_type, block_len);
10665 }
10666 rettv->vval.v_number = 0;
10667}
10668
10669/*
10670 * "settabvar()" function
10671 */
10672 static void
10673f_settabvar(typval_T *argvars, typval_T *rettv)
10674{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010675 tabpage_T *save_curtab;
10676 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010677 char_u *varname, *tabvarname;
10678 typval_T *varp;
10679
10680 rettv->vval.v_number = 0;
10681
10682 if (check_restricted() || check_secure())
10683 return;
10684
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010685 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010686 varname = get_tv_string_chk(&argvars[1]);
10687 varp = &argvars[2];
10688
Bram Moolenaar4033c552017-09-16 20:54:51 +020010689 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010690 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010691 save_curtab = curtab;
10692 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010693
10694 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
10695 if (tabvarname != NULL)
10696 {
10697 STRCPY(tabvarname, "t:");
10698 STRCPY(tabvarname + 2, varname);
10699 set_var(tabvarname, varp, TRUE);
10700 vim_free(tabvarname);
10701 }
10702
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010703 /* Restore current tabpage */
10704 if (valid_tabpage(save_curtab))
10705 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010706 }
10707}
10708
10709/*
10710 * "settabwinvar()" function
10711 */
10712 static void
10713f_settabwinvar(typval_T *argvars, typval_T *rettv)
10714{
10715 setwinvar(argvars, rettv, 1);
10716}
10717
10718/*
10719 * "setwinvar()" function
10720 */
10721 static void
10722f_setwinvar(typval_T *argvars, typval_T *rettv)
10723{
10724 setwinvar(argvars, rettv, 0);
10725}
10726
10727#ifdef FEAT_CRYPT
10728/*
10729 * "sha256({string})" function
10730 */
10731 static void
10732f_sha256(typval_T *argvars, typval_T *rettv)
10733{
10734 char_u *p;
10735
10736 p = get_tv_string(&argvars[0]);
10737 rettv->vval.v_string = vim_strsave(
10738 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
10739 rettv->v_type = VAR_STRING;
10740}
10741#endif /* FEAT_CRYPT */
10742
10743/*
10744 * "shellescape({string})" function
10745 */
10746 static void
10747f_shellescape(typval_T *argvars, typval_T *rettv)
10748{
Bram Moolenaar20615522017-06-05 18:46:26 +020010749 int do_special = non_zero_arg(&argvars[1]);
10750
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010751 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar20615522017-06-05 18:46:26 +020010752 get_tv_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010753 rettv->v_type = VAR_STRING;
10754}
10755
10756/*
10757 * shiftwidth() function
10758 */
10759 static void
10760f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
10761{
10762 rettv->vval.v_number = get_sw_value(curbuf);
10763}
10764
10765/*
10766 * "simplify()" function
10767 */
10768 static void
10769f_simplify(typval_T *argvars, typval_T *rettv)
10770{
10771 char_u *p;
10772
10773 p = get_tv_string(&argvars[0]);
10774 rettv->vval.v_string = vim_strsave(p);
10775 simplify_filename(rettv->vval.v_string); /* simplify in place */
10776 rettv->v_type = VAR_STRING;
10777}
10778
10779#ifdef FEAT_FLOAT
10780/*
10781 * "sin()" function
10782 */
10783 static void
10784f_sin(typval_T *argvars, typval_T *rettv)
10785{
10786 float_T f = 0.0;
10787
10788 rettv->v_type = VAR_FLOAT;
10789 if (get_float_arg(argvars, &f) == OK)
10790 rettv->vval.v_float = sin(f);
10791 else
10792 rettv->vval.v_float = 0.0;
10793}
10794
10795/*
10796 * "sinh()" function
10797 */
10798 static void
10799f_sinh(typval_T *argvars, typval_T *rettv)
10800{
10801 float_T f = 0.0;
10802
10803 rettv->v_type = VAR_FLOAT;
10804 if (get_float_arg(argvars, &f) == OK)
10805 rettv->vval.v_float = sinh(f);
10806 else
10807 rettv->vval.v_float = 0.0;
10808}
10809#endif
10810
10811static int
10812#ifdef __BORLANDC__
10813 _RTLENTRYF
10814#endif
10815 item_compare(const void *s1, const void *s2);
10816static int
10817#ifdef __BORLANDC__
10818 _RTLENTRYF
10819#endif
10820 item_compare2(const void *s1, const void *s2);
10821
10822/* struct used in the array that's given to qsort() */
10823typedef struct
10824{
10825 listitem_T *item;
10826 int idx;
10827} sortItem_T;
10828
10829/* struct storing information about current sort */
10830typedef struct
10831{
10832 int item_compare_ic;
10833 int item_compare_numeric;
10834 int item_compare_numbers;
10835#ifdef FEAT_FLOAT
10836 int item_compare_float;
10837#endif
10838 char_u *item_compare_func;
10839 partial_T *item_compare_partial;
10840 dict_T *item_compare_selfdict;
10841 int item_compare_func_err;
10842 int item_compare_keep_zero;
10843} sortinfo_T;
10844static sortinfo_T *sortinfo = NULL;
10845static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
10846#define ITEM_COMPARE_FAIL 999
10847
10848/*
10849 * Compare functions for f_sort() and f_uniq() below.
10850 */
10851 static int
10852#ifdef __BORLANDC__
10853_RTLENTRYF
10854#endif
10855item_compare(const void *s1, const void *s2)
10856{
10857 sortItem_T *si1, *si2;
10858 typval_T *tv1, *tv2;
10859 char_u *p1, *p2;
10860 char_u *tofree1 = NULL, *tofree2 = NULL;
10861 int res;
10862 char_u numbuf1[NUMBUFLEN];
10863 char_u numbuf2[NUMBUFLEN];
10864
10865 si1 = (sortItem_T *)s1;
10866 si2 = (sortItem_T *)s2;
10867 tv1 = &si1->item->li_tv;
10868 tv2 = &si2->item->li_tv;
10869
10870 if (sortinfo->item_compare_numbers)
10871 {
10872 varnumber_T v1 = get_tv_number(tv1);
10873 varnumber_T v2 = get_tv_number(tv2);
10874
10875 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10876 }
10877
10878#ifdef FEAT_FLOAT
10879 if (sortinfo->item_compare_float)
10880 {
10881 float_T v1 = get_tv_float(tv1);
10882 float_T v2 = get_tv_float(tv2);
10883
10884 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10885 }
10886#endif
10887
10888 /* tv2string() puts quotes around a string and allocates memory. Don't do
10889 * that for string variables. Use a single quote when comparing with a
10890 * non-string to do what the docs promise. */
10891 if (tv1->v_type == VAR_STRING)
10892 {
10893 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
10894 p1 = (char_u *)"'";
10895 else
10896 p1 = tv1->vval.v_string;
10897 }
10898 else
10899 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
10900 if (tv2->v_type == VAR_STRING)
10901 {
10902 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
10903 p2 = (char_u *)"'";
10904 else
10905 p2 = tv2->vval.v_string;
10906 }
10907 else
10908 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
10909 if (p1 == NULL)
10910 p1 = (char_u *)"";
10911 if (p2 == NULL)
10912 p2 = (char_u *)"";
10913 if (!sortinfo->item_compare_numeric)
10914 {
10915 if (sortinfo->item_compare_ic)
10916 res = STRICMP(p1, p2);
10917 else
10918 res = STRCMP(p1, p2);
10919 }
10920 else
10921 {
10922 double n1, n2;
10923 n1 = strtod((char *)p1, (char **)&p1);
10924 n2 = strtod((char *)p2, (char **)&p2);
10925 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
10926 }
10927
10928 /* When the result would be zero, compare the item indexes. Makes the
10929 * sort stable. */
10930 if (res == 0 && !sortinfo->item_compare_keep_zero)
10931 res = si1->idx > si2->idx ? 1 : -1;
10932
10933 vim_free(tofree1);
10934 vim_free(tofree2);
10935 return res;
10936}
10937
10938 static int
10939#ifdef __BORLANDC__
10940_RTLENTRYF
10941#endif
10942item_compare2(const void *s1, const void *s2)
10943{
10944 sortItem_T *si1, *si2;
10945 int res;
10946 typval_T rettv;
10947 typval_T argv[3];
10948 int dummy;
10949 char_u *func_name;
10950 partial_T *partial = sortinfo->item_compare_partial;
10951
10952 /* shortcut after failure in previous call; compare all items equal */
10953 if (sortinfo->item_compare_func_err)
10954 return 0;
10955
10956 si1 = (sortItem_T *)s1;
10957 si2 = (sortItem_T *)s2;
10958
10959 if (partial == NULL)
10960 func_name = sortinfo->item_compare_func;
10961 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020010962 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010963
10964 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
10965 * in the copy without changing the original list items. */
10966 copy_tv(&si1->item->li_tv, &argv[0]);
10967 copy_tv(&si2->item->li_tv, &argv[1]);
10968
10969 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
10970 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020010971 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010972 partial, sortinfo->item_compare_selfdict);
10973 clear_tv(&argv[0]);
10974 clear_tv(&argv[1]);
10975
10976 if (res == FAIL)
10977 res = ITEM_COMPARE_FAIL;
10978 else
10979 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
10980 if (sortinfo->item_compare_func_err)
10981 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
10982 clear_tv(&rettv);
10983
10984 /* When the result would be zero, compare the pointers themselves. Makes
10985 * the sort stable. */
10986 if (res == 0 && !sortinfo->item_compare_keep_zero)
10987 res = si1->idx > si2->idx ? 1 : -1;
10988
10989 return res;
10990}
10991
10992/*
10993 * "sort({list})" function
10994 */
10995 static void
10996do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
10997{
10998 list_T *l;
10999 listitem_T *li;
11000 sortItem_T *ptrs;
11001 sortinfo_T *old_sortinfo;
11002 sortinfo_T info;
11003 long len;
11004 long i;
11005
11006 /* Pointer to current info struct used in compare function. Save and
11007 * restore the current one for nested calls. */
11008 old_sortinfo = sortinfo;
11009 sortinfo = &info;
11010
11011 if (argvars[0].v_type != VAR_LIST)
11012 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
11013 else
11014 {
11015 l = argvars[0].vval.v_list;
11016 if (l == NULL || tv_check_lock(l->lv_lock,
11017 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
11018 TRUE))
11019 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011020 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011021
11022 len = list_len(l);
11023 if (len <= 1)
11024 goto theend; /* short list sorts pretty quickly */
11025
11026 info.item_compare_ic = FALSE;
11027 info.item_compare_numeric = FALSE;
11028 info.item_compare_numbers = FALSE;
11029#ifdef FEAT_FLOAT
11030 info.item_compare_float = FALSE;
11031#endif
11032 info.item_compare_func = NULL;
11033 info.item_compare_partial = NULL;
11034 info.item_compare_selfdict = NULL;
11035 if (argvars[1].v_type != VAR_UNKNOWN)
11036 {
11037 /* optional second argument: {func} */
11038 if (argvars[1].v_type == VAR_FUNC)
11039 info.item_compare_func = argvars[1].vval.v_string;
11040 else if (argvars[1].v_type == VAR_PARTIAL)
11041 info.item_compare_partial = argvars[1].vval.v_partial;
11042 else
11043 {
11044 int error = FALSE;
11045
11046 i = (long)get_tv_number_chk(&argvars[1], &error);
11047 if (error)
11048 goto theend; /* type error; errmsg already given */
11049 if (i == 1)
11050 info.item_compare_ic = TRUE;
11051 else if (argvars[1].v_type != VAR_NUMBER)
11052 info.item_compare_func = get_tv_string(&argvars[1]);
11053 else if (i != 0)
11054 {
11055 EMSG(_(e_invarg));
11056 goto theend;
11057 }
11058 if (info.item_compare_func != NULL)
11059 {
11060 if (*info.item_compare_func == NUL)
11061 {
11062 /* empty string means default sort */
11063 info.item_compare_func = NULL;
11064 }
11065 else if (STRCMP(info.item_compare_func, "n") == 0)
11066 {
11067 info.item_compare_func = NULL;
11068 info.item_compare_numeric = TRUE;
11069 }
11070 else if (STRCMP(info.item_compare_func, "N") == 0)
11071 {
11072 info.item_compare_func = NULL;
11073 info.item_compare_numbers = TRUE;
11074 }
11075#ifdef FEAT_FLOAT
11076 else if (STRCMP(info.item_compare_func, "f") == 0)
11077 {
11078 info.item_compare_func = NULL;
11079 info.item_compare_float = TRUE;
11080 }
11081#endif
11082 else if (STRCMP(info.item_compare_func, "i") == 0)
11083 {
11084 info.item_compare_func = NULL;
11085 info.item_compare_ic = TRUE;
11086 }
11087 }
11088 }
11089
11090 if (argvars[2].v_type != VAR_UNKNOWN)
11091 {
11092 /* optional third argument: {dict} */
11093 if (argvars[2].v_type != VAR_DICT)
11094 {
11095 EMSG(_(e_dictreq));
11096 goto theend;
11097 }
11098 info.item_compare_selfdict = argvars[2].vval.v_dict;
11099 }
11100 }
11101
11102 /* Make an array with each entry pointing to an item in the List. */
11103 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
11104 if (ptrs == NULL)
11105 goto theend;
11106
11107 i = 0;
11108 if (sort)
11109 {
11110 /* sort(): ptrs will be the list to sort */
11111 for (li = l->lv_first; li != NULL; li = li->li_next)
11112 {
11113 ptrs[i].item = li;
11114 ptrs[i].idx = i;
11115 ++i;
11116 }
11117
11118 info.item_compare_func_err = FALSE;
11119 info.item_compare_keep_zero = FALSE;
11120 /* test the compare function */
11121 if ((info.item_compare_func != NULL
11122 || info.item_compare_partial != NULL)
11123 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11124 == ITEM_COMPARE_FAIL)
11125 EMSG(_("E702: Sort compare function failed"));
11126 else
11127 {
11128 /* Sort the array with item pointers. */
11129 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
11130 info.item_compare_func == NULL
11131 && info.item_compare_partial == NULL
11132 ? item_compare : item_compare2);
11133
11134 if (!info.item_compare_func_err)
11135 {
11136 /* Clear the List and append the items in sorted order. */
11137 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
11138 l->lv_len = 0;
11139 for (i = 0; i < len; ++i)
11140 list_append(l, ptrs[i].item);
11141 }
11142 }
11143 }
11144 else
11145 {
11146 int (*item_compare_func_ptr)(const void *, const void *);
11147
11148 /* f_uniq(): ptrs will be a stack of items to remove */
11149 info.item_compare_func_err = FALSE;
11150 info.item_compare_keep_zero = TRUE;
11151 item_compare_func_ptr = info.item_compare_func != NULL
11152 || info.item_compare_partial != NULL
11153 ? item_compare2 : item_compare;
11154
11155 for (li = l->lv_first; li != NULL && li->li_next != NULL;
11156 li = li->li_next)
11157 {
11158 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
11159 == 0)
11160 ptrs[i++].item = li;
11161 if (info.item_compare_func_err)
11162 {
11163 EMSG(_("E882: Uniq compare function failed"));
11164 break;
11165 }
11166 }
11167
11168 if (!info.item_compare_func_err)
11169 {
11170 while (--i >= 0)
11171 {
11172 li = ptrs[i].item->li_next;
11173 ptrs[i].item->li_next = li->li_next;
11174 if (li->li_next != NULL)
11175 li->li_next->li_prev = ptrs[i].item;
11176 else
11177 l->lv_last = ptrs[i].item;
11178 list_fix_watch(l, li);
11179 listitem_free(li);
11180 l->lv_len--;
11181 }
11182 }
11183 }
11184
11185 vim_free(ptrs);
11186 }
11187theend:
11188 sortinfo = old_sortinfo;
11189}
11190
11191/*
11192 * "sort({list})" function
11193 */
11194 static void
11195f_sort(typval_T *argvars, typval_T *rettv)
11196{
11197 do_sort_uniq(argvars, rettv, TRUE);
11198}
11199
11200/*
11201 * "uniq({list})" function
11202 */
11203 static void
11204f_uniq(typval_T *argvars, typval_T *rettv)
11205{
11206 do_sort_uniq(argvars, rettv, FALSE);
11207}
11208
11209/*
11210 * "soundfold({word})" function
11211 */
11212 static void
11213f_soundfold(typval_T *argvars, typval_T *rettv)
11214{
11215 char_u *s;
11216
11217 rettv->v_type = VAR_STRING;
11218 s = get_tv_string(&argvars[0]);
11219#ifdef FEAT_SPELL
11220 rettv->vval.v_string = eval_soundfold(s);
11221#else
11222 rettv->vval.v_string = vim_strsave(s);
11223#endif
11224}
11225
11226/*
11227 * "spellbadword()" function
11228 */
11229 static void
11230f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
11231{
11232 char_u *word = (char_u *)"";
11233 hlf_T attr = HLF_COUNT;
11234 int len = 0;
11235
11236 if (rettv_list_alloc(rettv) == FAIL)
11237 return;
11238
11239#ifdef FEAT_SPELL
11240 if (argvars[0].v_type == VAR_UNKNOWN)
11241 {
11242 /* Find the start and length of the badly spelled word. */
11243 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
11244 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011245 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011246 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011247 curwin->w_set_curswant = TRUE;
11248 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011249 }
11250 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
11251 {
11252 char_u *str = get_tv_string_chk(&argvars[0]);
11253 int capcol = -1;
11254
11255 if (str != NULL)
11256 {
11257 /* Check the argument for spelling. */
11258 while (*str != NUL)
11259 {
11260 len = spell_check(curwin, str, &attr, &capcol, FALSE);
11261 if (attr != HLF_COUNT)
11262 {
11263 word = str;
11264 break;
11265 }
11266 str += len;
11267 }
11268 }
11269 }
11270#endif
11271
11272 list_append_string(rettv->vval.v_list, word, len);
11273 list_append_string(rettv->vval.v_list, (char_u *)(
11274 attr == HLF_SPB ? "bad" :
11275 attr == HLF_SPR ? "rare" :
11276 attr == HLF_SPL ? "local" :
11277 attr == HLF_SPC ? "caps" :
11278 ""), -1);
11279}
11280
11281/*
11282 * "spellsuggest()" function
11283 */
11284 static void
11285f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
11286{
11287#ifdef FEAT_SPELL
11288 char_u *str;
11289 int typeerr = FALSE;
11290 int maxcount;
11291 garray_T ga;
11292 int i;
11293 listitem_T *li;
11294 int need_capital = FALSE;
11295#endif
11296
11297 if (rettv_list_alloc(rettv) == FAIL)
11298 return;
11299
11300#ifdef FEAT_SPELL
11301 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
11302 {
11303 str = get_tv_string(&argvars[0]);
11304 if (argvars[1].v_type != VAR_UNKNOWN)
11305 {
11306 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
11307 if (maxcount <= 0)
11308 return;
11309 if (argvars[2].v_type != VAR_UNKNOWN)
11310 {
11311 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
11312 if (typeerr)
11313 return;
11314 }
11315 }
11316 else
11317 maxcount = 25;
11318
11319 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
11320
11321 for (i = 0; i < ga.ga_len; ++i)
11322 {
11323 str = ((char_u **)ga.ga_data)[i];
11324
11325 li = listitem_alloc();
11326 if (li == NULL)
11327 vim_free(str);
11328 else
11329 {
11330 li->li_tv.v_type = VAR_STRING;
11331 li->li_tv.v_lock = 0;
11332 li->li_tv.vval.v_string = str;
11333 list_append(rettv->vval.v_list, li);
11334 }
11335 }
11336 ga_clear(&ga);
11337 }
11338#endif
11339}
11340
11341 static void
11342f_split(typval_T *argvars, typval_T *rettv)
11343{
11344 char_u *str;
11345 char_u *end;
11346 char_u *pat = NULL;
11347 regmatch_T regmatch;
11348 char_u patbuf[NUMBUFLEN];
11349 char_u *save_cpo;
11350 int match;
11351 colnr_T col = 0;
11352 int keepempty = FALSE;
11353 int typeerr = FALSE;
11354
11355 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11356 save_cpo = p_cpo;
11357 p_cpo = (char_u *)"";
11358
11359 str = get_tv_string(&argvars[0]);
11360 if (argvars[1].v_type != VAR_UNKNOWN)
11361 {
11362 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11363 if (pat == NULL)
11364 typeerr = TRUE;
11365 if (argvars[2].v_type != VAR_UNKNOWN)
11366 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
11367 }
11368 if (pat == NULL || *pat == NUL)
11369 pat = (char_u *)"[\\x01- ]\\+";
11370
11371 if (rettv_list_alloc(rettv) == FAIL)
11372 return;
11373 if (typeerr)
11374 return;
11375
11376 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11377 if (regmatch.regprog != NULL)
11378 {
11379 regmatch.rm_ic = FALSE;
11380 while (*str != NUL || keepempty)
11381 {
11382 if (*str == NUL)
11383 match = FALSE; /* empty item at the end */
11384 else
11385 match = vim_regexec_nl(&regmatch, str, col);
11386 if (match)
11387 end = regmatch.startp[0];
11388 else
11389 end = str + STRLEN(str);
11390 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
11391 && *str != NUL && match && end < regmatch.endp[0]))
11392 {
11393 if (list_append_string(rettv->vval.v_list, str,
11394 (int)(end - str)) == FAIL)
11395 break;
11396 }
11397 if (!match)
11398 break;
11399 /* Advance to just after the match. */
11400 if (regmatch.endp[0] > str)
11401 col = 0;
11402 else
11403 {
11404 /* Don't get stuck at the same match. */
11405#ifdef FEAT_MBYTE
11406 col = (*mb_ptr2len)(regmatch.endp[0]);
11407#else
11408 col = 1;
11409#endif
11410 }
11411 str = regmatch.endp[0];
11412 }
11413
11414 vim_regfree(regmatch.regprog);
11415 }
11416
11417 p_cpo = save_cpo;
11418}
11419
11420#ifdef FEAT_FLOAT
11421/*
11422 * "sqrt()" function
11423 */
11424 static void
11425f_sqrt(typval_T *argvars, typval_T *rettv)
11426{
11427 float_T f = 0.0;
11428
11429 rettv->v_type = VAR_FLOAT;
11430 if (get_float_arg(argvars, &f) == OK)
11431 rettv->vval.v_float = sqrt(f);
11432 else
11433 rettv->vval.v_float = 0.0;
11434}
11435
11436/*
11437 * "str2float()" function
11438 */
11439 static void
11440f_str2float(typval_T *argvars, typval_T *rettv)
11441{
11442 char_u *p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011443 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011444
Bram Moolenaar08243d22017-01-10 16:12:29 +010011445 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011446 p = skipwhite(p + 1);
11447 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011448 if (isneg)
11449 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011450 rettv->v_type = VAR_FLOAT;
11451}
11452#endif
11453
11454/*
11455 * "str2nr()" function
11456 */
11457 static void
11458f_str2nr(typval_T *argvars, typval_T *rettv)
11459{
11460 int base = 10;
11461 char_u *p;
11462 varnumber_T n;
11463 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010011464 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011465
11466 if (argvars[1].v_type != VAR_UNKNOWN)
11467 {
11468 base = (int)get_tv_number(&argvars[1]);
11469 if (base != 2 && base != 8 && base != 10 && base != 16)
11470 {
11471 EMSG(_(e_invarg));
11472 return;
11473 }
11474 }
11475
11476 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011477 isneg = (*p == '-');
11478 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011479 p = skipwhite(p + 1);
11480 switch (base)
11481 {
11482 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
11483 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
11484 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
11485 default: what = 0;
11486 }
11487 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011488 if (isneg)
11489 rettv->vval.v_number = -n;
11490 else
11491 rettv->vval.v_number = n;
11492
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011493}
11494
11495#ifdef HAVE_STRFTIME
11496/*
11497 * "strftime({format}[, {time}])" function
11498 */
11499 static void
11500f_strftime(typval_T *argvars, typval_T *rettv)
11501{
11502 char_u result_buf[256];
11503 struct tm *curtime;
11504 time_t seconds;
11505 char_u *p;
11506
11507 rettv->v_type = VAR_STRING;
11508
11509 p = get_tv_string(&argvars[0]);
11510 if (argvars[1].v_type == VAR_UNKNOWN)
11511 seconds = time(NULL);
11512 else
11513 seconds = (time_t)get_tv_number(&argvars[1]);
11514 curtime = localtime(&seconds);
11515 /* MSVC returns NULL for an invalid value of seconds. */
11516 if (curtime == NULL)
11517 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
11518 else
11519 {
11520# ifdef FEAT_MBYTE
11521 vimconv_T conv;
11522 char_u *enc;
11523
11524 conv.vc_type = CONV_NONE;
11525 enc = enc_locale();
11526 convert_setup(&conv, p_enc, enc);
11527 if (conv.vc_type != CONV_NONE)
11528 p = string_convert(&conv, p, NULL);
11529# endif
11530 if (p != NULL)
11531 (void)strftime((char *)result_buf, sizeof(result_buf),
11532 (char *)p, curtime);
11533 else
11534 result_buf[0] = NUL;
11535
11536# ifdef FEAT_MBYTE
11537 if (conv.vc_type != CONV_NONE)
11538 vim_free(p);
11539 convert_setup(&conv, enc, p_enc);
11540 if (conv.vc_type != CONV_NONE)
11541 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
11542 else
11543# endif
11544 rettv->vval.v_string = vim_strsave(result_buf);
11545
11546# ifdef FEAT_MBYTE
11547 /* Release conversion descriptors */
11548 convert_setup(&conv, NULL, NULL);
11549 vim_free(enc);
11550# endif
11551 }
11552}
11553#endif
11554
11555/*
11556 * "strgetchar()" function
11557 */
11558 static void
11559f_strgetchar(typval_T *argvars, typval_T *rettv)
11560{
11561 char_u *str;
11562 int len;
11563 int error = FALSE;
11564 int charidx;
11565
11566 rettv->vval.v_number = -1;
11567 str = get_tv_string_chk(&argvars[0]);
11568 if (str == NULL)
11569 return;
11570 len = (int)STRLEN(str);
11571 charidx = (int)get_tv_number_chk(&argvars[1], &error);
11572 if (error)
11573 return;
11574#ifdef FEAT_MBYTE
11575 {
11576 int byteidx = 0;
11577
11578 while (charidx >= 0 && byteidx < len)
11579 {
11580 if (charidx == 0)
11581 {
11582 rettv->vval.v_number = mb_ptr2char(str + byteidx);
11583 break;
11584 }
11585 --charidx;
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011586 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011587 }
11588 }
11589#else
11590 if (charidx < len)
11591 rettv->vval.v_number = str[charidx];
11592#endif
11593}
11594
11595/*
11596 * "stridx()" function
11597 */
11598 static void
11599f_stridx(typval_T *argvars, typval_T *rettv)
11600{
11601 char_u buf[NUMBUFLEN];
11602 char_u *needle;
11603 char_u *haystack;
11604 char_u *save_haystack;
11605 char_u *pos;
11606 int start_idx;
11607
11608 needle = get_tv_string_chk(&argvars[1]);
11609 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
11610 rettv->vval.v_number = -1;
11611 if (needle == NULL || haystack == NULL)
11612 return; /* type error; errmsg already given */
11613
11614 if (argvars[2].v_type != VAR_UNKNOWN)
11615 {
11616 int error = FALSE;
11617
11618 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
11619 if (error || start_idx >= (int)STRLEN(haystack))
11620 return;
11621 if (start_idx >= 0)
11622 haystack += start_idx;
11623 }
11624
11625 pos = (char_u *)strstr((char *)haystack, (char *)needle);
11626 if (pos != NULL)
11627 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
11628}
11629
11630/*
11631 * "string()" function
11632 */
11633 static void
11634f_string(typval_T *argvars, typval_T *rettv)
11635{
11636 char_u *tofree;
11637 char_u numbuf[NUMBUFLEN];
11638
11639 rettv->v_type = VAR_STRING;
11640 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
11641 get_copyID());
11642 /* Make a copy if we have a value but it's not in allocated memory. */
11643 if (rettv->vval.v_string != NULL && tofree == NULL)
11644 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
11645}
11646
11647/*
11648 * "strlen()" function
11649 */
11650 static void
11651f_strlen(typval_T *argvars, typval_T *rettv)
11652{
11653 rettv->vval.v_number = (varnumber_T)(STRLEN(
11654 get_tv_string(&argvars[0])));
11655}
11656
11657/*
11658 * "strchars()" function
11659 */
11660 static void
11661f_strchars(typval_T *argvars, typval_T *rettv)
11662{
11663 char_u *s = get_tv_string(&argvars[0]);
11664 int skipcc = 0;
11665#ifdef FEAT_MBYTE
11666 varnumber_T len = 0;
11667 int (*func_mb_ptr2char_adv)(char_u **pp);
11668#endif
11669
11670 if (argvars[1].v_type != VAR_UNKNOWN)
11671 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
11672 if (skipcc < 0 || skipcc > 1)
11673 EMSG(_(e_invarg));
11674 else
11675 {
11676#ifdef FEAT_MBYTE
11677 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
11678 while (*s != NUL)
11679 {
11680 func_mb_ptr2char_adv(&s);
11681 ++len;
11682 }
11683 rettv->vval.v_number = len;
11684#else
11685 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
11686#endif
11687 }
11688}
11689
11690/*
11691 * "strdisplaywidth()" function
11692 */
11693 static void
11694f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
11695{
11696 char_u *s = get_tv_string(&argvars[0]);
11697 int col = 0;
11698
11699 if (argvars[1].v_type != VAR_UNKNOWN)
11700 col = (int)get_tv_number(&argvars[1]);
11701
11702 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
11703}
11704
11705/*
11706 * "strwidth()" function
11707 */
11708 static void
11709f_strwidth(typval_T *argvars, typval_T *rettv)
11710{
11711 char_u *s = get_tv_string(&argvars[0]);
11712
11713 rettv->vval.v_number = (varnumber_T)(
11714#ifdef FEAT_MBYTE
11715 mb_string2cells(s, -1)
11716#else
11717 STRLEN(s)
11718#endif
11719 );
11720}
11721
11722/*
11723 * "strcharpart()" function
11724 */
11725 static void
11726f_strcharpart(typval_T *argvars, typval_T *rettv)
11727{
11728#ifdef FEAT_MBYTE
11729 char_u *p;
11730 int nchar;
11731 int nbyte = 0;
11732 int charlen;
11733 int len = 0;
11734 int slen;
11735 int error = FALSE;
11736
11737 p = get_tv_string(&argvars[0]);
11738 slen = (int)STRLEN(p);
11739
11740 nchar = (int)get_tv_number_chk(&argvars[1], &error);
11741 if (!error)
11742 {
11743 if (nchar > 0)
11744 while (nchar > 0 && nbyte < slen)
11745 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011746 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011747 --nchar;
11748 }
11749 else
11750 nbyte = nchar;
11751 if (argvars[2].v_type != VAR_UNKNOWN)
11752 {
11753 charlen = (int)get_tv_number(&argvars[2]);
11754 while (charlen > 0 && nbyte + len < slen)
11755 {
11756 int off = nbyte + len;
11757
11758 if (off < 0)
11759 len += 1;
11760 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011761 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011762 --charlen;
11763 }
11764 }
11765 else
11766 len = slen - nbyte; /* default: all bytes that are available. */
11767 }
11768
11769 /*
11770 * Only return the overlap between the specified part and the actual
11771 * string.
11772 */
11773 if (nbyte < 0)
11774 {
11775 len += nbyte;
11776 nbyte = 0;
11777 }
11778 else if (nbyte > slen)
11779 nbyte = slen;
11780 if (len < 0)
11781 len = 0;
11782 else if (nbyte + len > slen)
11783 len = slen - nbyte;
11784
11785 rettv->v_type = VAR_STRING;
11786 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
11787#else
11788 f_strpart(argvars, rettv);
11789#endif
11790}
11791
11792/*
11793 * "strpart()" function
11794 */
11795 static void
11796f_strpart(typval_T *argvars, typval_T *rettv)
11797{
11798 char_u *p;
11799 int n;
11800 int len;
11801 int slen;
11802 int error = FALSE;
11803
11804 p = get_tv_string(&argvars[0]);
11805 slen = (int)STRLEN(p);
11806
11807 n = (int)get_tv_number_chk(&argvars[1], &error);
11808 if (error)
11809 len = 0;
11810 else if (argvars[2].v_type != VAR_UNKNOWN)
11811 len = (int)get_tv_number(&argvars[2]);
11812 else
11813 len = slen - n; /* default len: all bytes that are available. */
11814
11815 /*
11816 * Only return the overlap between the specified part and the actual
11817 * string.
11818 */
11819 if (n < 0)
11820 {
11821 len += n;
11822 n = 0;
11823 }
11824 else if (n > slen)
11825 n = slen;
11826 if (len < 0)
11827 len = 0;
11828 else if (n + len > slen)
11829 len = slen - n;
11830
11831 rettv->v_type = VAR_STRING;
11832 rettv->vval.v_string = vim_strnsave(p + n, len);
11833}
11834
11835/*
11836 * "strridx()" function
11837 */
11838 static void
11839f_strridx(typval_T *argvars, typval_T *rettv)
11840{
11841 char_u buf[NUMBUFLEN];
11842 char_u *needle;
11843 char_u *haystack;
11844 char_u *rest;
11845 char_u *lastmatch = NULL;
11846 int haystack_len, end_idx;
11847
11848 needle = get_tv_string_chk(&argvars[1]);
11849 haystack = get_tv_string_buf_chk(&argvars[0], buf);
11850
11851 rettv->vval.v_number = -1;
11852 if (needle == NULL || haystack == NULL)
11853 return; /* type error; errmsg already given */
11854
11855 haystack_len = (int)STRLEN(haystack);
11856 if (argvars[2].v_type != VAR_UNKNOWN)
11857 {
11858 /* Third argument: upper limit for index */
11859 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
11860 if (end_idx < 0)
11861 return; /* can never find a match */
11862 }
11863 else
11864 end_idx = haystack_len;
11865
11866 if (*needle == NUL)
11867 {
11868 /* Empty string matches past the end. */
11869 lastmatch = haystack + end_idx;
11870 }
11871 else
11872 {
11873 for (rest = haystack; *rest != '\0'; ++rest)
11874 {
11875 rest = (char_u *)strstr((char *)rest, (char *)needle);
11876 if (rest == NULL || rest > haystack + end_idx)
11877 break;
11878 lastmatch = rest;
11879 }
11880 }
11881
11882 if (lastmatch == NULL)
11883 rettv->vval.v_number = -1;
11884 else
11885 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
11886}
11887
11888/*
11889 * "strtrans()" function
11890 */
11891 static void
11892f_strtrans(typval_T *argvars, typval_T *rettv)
11893{
11894 rettv->v_type = VAR_STRING;
11895 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
11896}
11897
11898/*
11899 * "submatch()" function
11900 */
11901 static void
11902f_submatch(typval_T *argvars, typval_T *rettv)
11903{
11904 int error = FALSE;
11905 int no;
11906 int retList = 0;
11907
11908 no = (int)get_tv_number_chk(&argvars[0], &error);
11909 if (error)
11910 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020011911 if (no < 0 || no >= NSUBEXP)
11912 {
Bram Moolenaar79518e22017-02-17 16:31:35 +010011913 EMSGN(_("E935: invalid submatch number: %d"), no);
11914 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020011915 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011916 if (argvars[1].v_type != VAR_UNKNOWN)
11917 retList = (int)get_tv_number_chk(&argvars[1], &error);
11918 if (error)
11919 return;
11920
11921 if (retList == 0)
11922 {
11923 rettv->v_type = VAR_STRING;
11924 rettv->vval.v_string = reg_submatch(no);
11925 }
11926 else
11927 {
11928 rettv->v_type = VAR_LIST;
11929 rettv->vval.v_list = reg_submatch_list(no);
11930 }
11931}
11932
11933/*
11934 * "substitute()" function
11935 */
11936 static void
11937f_substitute(typval_T *argvars, typval_T *rettv)
11938{
11939 char_u patbuf[NUMBUFLEN];
11940 char_u subbuf[NUMBUFLEN];
11941 char_u flagsbuf[NUMBUFLEN];
11942
11943 char_u *str = get_tv_string_chk(&argvars[0]);
11944 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011945 char_u *sub = NULL;
11946 typval_T *expr = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011947 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
11948
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011949 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
11950 expr = &argvars[2];
11951 else
11952 sub = get_tv_string_buf_chk(&argvars[2], subbuf);
11953
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011954 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011955 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
11956 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011957 rettv->vval.v_string = NULL;
11958 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011959 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011960}
11961
11962/*
11963 * "synID(lnum, col, trans)" function
11964 */
11965 static void
11966f_synID(typval_T *argvars UNUSED, typval_T *rettv)
11967{
11968 int id = 0;
11969#ifdef FEAT_SYN_HL
11970 linenr_T lnum;
11971 colnr_T col;
11972 int trans;
11973 int transerr = FALSE;
11974
11975 lnum = get_tv_lnum(argvars); /* -1 on type error */
11976 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
11977 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
11978
11979 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11980 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
11981 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
11982#endif
11983
11984 rettv->vval.v_number = id;
11985}
11986
11987/*
11988 * "synIDattr(id, what [, mode])" function
11989 */
11990 static void
11991f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
11992{
11993 char_u *p = NULL;
11994#ifdef FEAT_SYN_HL
11995 int id;
11996 char_u *what;
11997 char_u *mode;
11998 char_u modebuf[NUMBUFLEN];
11999 int modec;
12000
12001 id = (int)get_tv_number(&argvars[0]);
12002 what = get_tv_string(&argvars[1]);
12003 if (argvars[2].v_type != VAR_UNKNOWN)
12004 {
12005 mode = get_tv_string_buf(&argvars[2], modebuf);
12006 modec = TOLOWER_ASC(mode[0]);
12007 if (modec != 't' && modec != 'c' && modec != 'g')
12008 modec = 0; /* replace invalid with current */
12009 }
12010 else
12011 {
12012#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
12013 if (USE_24BIT)
12014 modec = 'g';
12015 else
12016#endif
12017 if (t_colors > 1)
12018 modec = 'c';
12019 else
12020 modec = 't';
12021 }
12022
12023
12024 switch (TOLOWER_ASC(what[0]))
12025 {
12026 case 'b':
12027 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12028 p = highlight_color(id, what, modec);
12029 else /* bold */
12030 p = highlight_has_attr(id, HL_BOLD, modec);
12031 break;
12032
12033 case 'f': /* fg[#] or font */
12034 p = highlight_color(id, what, modec);
12035 break;
12036
12037 case 'i':
12038 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
12039 p = highlight_has_attr(id, HL_INVERSE, modec);
12040 else /* italic */
12041 p = highlight_has_attr(id, HL_ITALIC, modec);
12042 break;
12043
12044 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020012045 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012046 break;
12047
12048 case 'r': /* reverse */
12049 p = highlight_has_attr(id, HL_INVERSE, modec);
12050 break;
12051
12052 case 's':
12053 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
12054 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020012055 /* strikeout */
12056 else if (TOLOWER_ASC(what[1]) == 't' &&
12057 TOLOWER_ASC(what[2]) == 'r')
12058 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012059 else /* standout */
12060 p = highlight_has_attr(id, HL_STANDOUT, modec);
12061 break;
12062
12063 case 'u':
12064 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
12065 /* underline */
12066 p = highlight_has_attr(id, HL_UNDERLINE, modec);
12067 else
12068 /* undercurl */
12069 p = highlight_has_attr(id, HL_UNDERCURL, modec);
12070 break;
12071 }
12072
12073 if (p != NULL)
12074 p = vim_strsave(p);
12075#endif
12076 rettv->v_type = VAR_STRING;
12077 rettv->vval.v_string = p;
12078}
12079
12080/*
12081 * "synIDtrans(id)" function
12082 */
12083 static void
12084f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
12085{
12086 int id;
12087
12088#ifdef FEAT_SYN_HL
12089 id = (int)get_tv_number(&argvars[0]);
12090
12091 if (id > 0)
12092 id = syn_get_final_id(id);
12093 else
12094#endif
12095 id = 0;
12096
12097 rettv->vval.v_number = id;
12098}
12099
12100/*
12101 * "synconcealed(lnum, col)" function
12102 */
12103 static void
12104f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
12105{
12106#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12107 linenr_T lnum;
12108 colnr_T col;
12109 int syntax_flags = 0;
12110 int cchar;
12111 int matchid = 0;
12112 char_u str[NUMBUFLEN];
12113#endif
12114
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012115 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012116
12117#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12118 lnum = get_tv_lnum(argvars); /* -1 on type error */
12119 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12120
12121 vim_memset(str, NUL, sizeof(str));
12122
12123 if (rettv_list_alloc(rettv) != FAIL)
12124 {
12125 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12126 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12127 && curwin->w_p_cole > 0)
12128 {
12129 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
12130 syntax_flags = get_syntax_info(&matchid);
12131
12132 /* get the conceal character */
12133 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
12134 {
12135 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020012136 if (cchar == NUL && curwin->w_p_cole == 1)
12137 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012138 if (cchar != NUL)
12139 {
12140# ifdef FEAT_MBYTE
12141 if (has_mbyte)
12142 (*mb_char2bytes)(cchar, str);
12143 else
12144# endif
12145 str[0] = cchar;
12146 }
12147 }
12148 }
12149
12150 list_append_number(rettv->vval.v_list,
12151 (syntax_flags & HL_CONCEAL) != 0);
12152 /* -1 to auto-determine strlen */
12153 list_append_string(rettv->vval.v_list, str, -1);
12154 list_append_number(rettv->vval.v_list, matchid);
12155 }
12156#endif
12157}
12158
12159/*
12160 * "synstack(lnum, col)" function
12161 */
12162 static void
12163f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
12164{
12165#ifdef FEAT_SYN_HL
12166 linenr_T lnum;
12167 colnr_T col;
12168 int i;
12169 int id;
12170#endif
12171
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012172 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012173
12174#ifdef FEAT_SYN_HL
12175 lnum = get_tv_lnum(argvars); /* -1 on type error */
12176 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12177
12178 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12179 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12180 && rettv_list_alloc(rettv) != FAIL)
12181 {
12182 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
12183 for (i = 0; ; ++i)
12184 {
12185 id = syn_get_stack_item(i);
12186 if (id < 0)
12187 break;
12188 if (list_append_number(rettv->vval.v_list, id) == FAIL)
12189 break;
12190 }
12191 }
12192#endif
12193}
12194
12195 static void
12196get_cmd_output_as_rettv(
12197 typval_T *argvars,
12198 typval_T *rettv,
12199 int retlist)
12200{
12201 char_u *res = NULL;
12202 char_u *p;
12203 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012204 int err = FALSE;
12205 FILE *fd;
12206 list_T *list = NULL;
12207 int flags = SHELL_SILENT;
12208
12209 rettv->v_type = VAR_STRING;
12210 rettv->vval.v_string = NULL;
12211 if (check_restricted() || check_secure())
12212 goto errret;
12213
12214 if (argvars[1].v_type != VAR_UNKNOWN)
12215 {
12216 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010012217 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012218 * command.
12219 */
12220 if ((infile = vim_tempname('i', TRUE)) == NULL)
12221 {
12222 EMSG(_(e_notmp));
12223 goto errret;
12224 }
12225
12226 fd = mch_fopen((char *)infile, WRITEBIN);
12227 if (fd == NULL)
12228 {
12229 EMSG2(_(e_notopen), infile);
12230 goto errret;
12231 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010012232 if (argvars[1].v_type == VAR_NUMBER)
12233 {
12234 linenr_T lnum;
12235 buf_T *buf;
12236
12237 buf = buflist_findnr(argvars[1].vval.v_number);
12238 if (buf == NULL)
12239 {
12240 EMSGN(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010012241 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010012242 goto errret;
12243 }
12244
12245 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
12246 {
12247 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
12248 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
12249 {
12250 err = TRUE;
12251 break;
12252 }
12253 if (putc(NL, fd) == EOF)
12254 {
12255 err = TRUE;
12256 break;
12257 }
12258 }
12259 }
12260 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012261 {
12262 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
12263 err = TRUE;
12264 }
12265 else
12266 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010012267 size_t len;
12268 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012269
12270 p = get_tv_string_buf_chk(&argvars[1], buf);
12271 if (p == NULL)
12272 {
12273 fclose(fd);
12274 goto errret; /* type error; errmsg already given */
12275 }
12276 len = STRLEN(p);
12277 if (len > 0 && fwrite(p, len, 1, fd) != 1)
12278 err = TRUE;
12279 }
12280 if (fclose(fd) != 0)
12281 err = TRUE;
12282 if (err)
12283 {
12284 EMSG(_("E677: Error writing temp file"));
12285 goto errret;
12286 }
12287 }
12288
12289 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
12290 * echoes typeahead, that messes up the display. */
12291 if (!msg_silent)
12292 flags += SHELL_COOKED;
12293
12294 if (retlist)
12295 {
12296 int len;
12297 listitem_T *li;
12298 char_u *s = NULL;
12299 char_u *start;
12300 char_u *end;
12301 int i;
12302
12303 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
12304 if (res == NULL)
12305 goto errret;
12306
12307 list = list_alloc();
12308 if (list == NULL)
12309 goto errret;
12310
12311 for (i = 0; i < len; ++i)
12312 {
12313 start = res + i;
12314 while (i < len && res[i] != NL)
12315 ++i;
12316 end = res + i;
12317
12318 s = alloc((unsigned)(end - start + 1));
12319 if (s == NULL)
12320 goto errret;
12321
12322 for (p = s; start < end; ++p, ++start)
12323 *p = *start == NUL ? NL : *start;
12324 *p = NUL;
12325
12326 li = listitem_alloc();
12327 if (li == NULL)
12328 {
12329 vim_free(s);
12330 goto errret;
12331 }
12332 li->li_tv.v_type = VAR_STRING;
12333 li->li_tv.v_lock = 0;
12334 li->li_tv.vval.v_string = s;
12335 list_append(list, li);
12336 }
12337
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012338 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012339 list = NULL;
12340 }
12341 else
12342 {
12343 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
12344#ifdef USE_CR
12345 /* translate <CR> into <NL> */
12346 if (res != NULL)
12347 {
12348 char_u *s;
12349
12350 for (s = res; *s; ++s)
12351 {
12352 if (*s == CAR)
12353 *s = NL;
12354 }
12355 }
12356#else
12357# ifdef USE_CRNL
12358 /* translate <CR><NL> into <NL> */
12359 if (res != NULL)
12360 {
12361 char_u *s, *d;
12362
12363 d = res;
12364 for (s = res; *s; ++s)
12365 {
12366 if (s[0] == CAR && s[1] == NL)
12367 ++s;
12368 *d++ = *s;
12369 }
12370 *d = NUL;
12371 }
12372# endif
12373#endif
12374 rettv->vval.v_string = res;
12375 res = NULL;
12376 }
12377
12378errret:
12379 if (infile != NULL)
12380 {
12381 mch_remove(infile);
12382 vim_free(infile);
12383 }
12384 if (res != NULL)
12385 vim_free(res);
12386 if (list != NULL)
12387 list_free(list);
12388}
12389
12390/*
12391 * "system()" function
12392 */
12393 static void
12394f_system(typval_T *argvars, typval_T *rettv)
12395{
12396 get_cmd_output_as_rettv(argvars, rettv, FALSE);
12397}
12398
12399/*
12400 * "systemlist()" function
12401 */
12402 static void
12403f_systemlist(typval_T *argvars, typval_T *rettv)
12404{
12405 get_cmd_output_as_rettv(argvars, rettv, TRUE);
12406}
12407
12408/*
12409 * "tabpagebuflist()" function
12410 */
12411 static void
12412f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12413{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012414 tabpage_T *tp;
12415 win_T *wp = NULL;
12416
12417 if (argvars[0].v_type == VAR_UNKNOWN)
12418 wp = firstwin;
12419 else
12420 {
12421 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12422 if (tp != NULL)
12423 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12424 }
12425 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
12426 {
12427 for (; wp != NULL; wp = wp->w_next)
12428 if (list_append_number(rettv->vval.v_list,
12429 wp->w_buffer->b_fnum) == FAIL)
12430 break;
12431 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012432}
12433
12434
12435/*
12436 * "tabpagenr()" function
12437 */
12438 static void
12439f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
12440{
12441 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012442 char_u *arg;
12443
12444 if (argvars[0].v_type != VAR_UNKNOWN)
12445 {
12446 arg = get_tv_string_chk(&argvars[0]);
12447 nr = 0;
12448 if (arg != NULL)
12449 {
12450 if (STRCMP(arg, "$") == 0)
12451 nr = tabpage_index(NULL) - 1;
12452 else
12453 EMSG2(_(e_invexpr2), arg);
12454 }
12455 }
12456 else
12457 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012458 rettv->vval.v_number = nr;
12459}
12460
12461
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012462static int get_winnr(tabpage_T *tp, typval_T *argvar);
12463
12464/*
12465 * Common code for tabpagewinnr() and winnr().
12466 */
12467 static int
12468get_winnr(tabpage_T *tp, typval_T *argvar)
12469{
12470 win_T *twin;
12471 int nr = 1;
12472 win_T *wp;
12473 char_u *arg;
12474
12475 twin = (tp == curtab) ? curwin : tp->tp_curwin;
12476 if (argvar->v_type != VAR_UNKNOWN)
12477 {
12478 arg = get_tv_string_chk(argvar);
12479 if (arg == NULL)
12480 nr = 0; /* type error; errmsg already given */
12481 else if (STRCMP(arg, "$") == 0)
12482 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
12483 else if (STRCMP(arg, "#") == 0)
12484 {
12485 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
12486 if (twin == NULL)
12487 nr = 0;
12488 }
12489 else
12490 {
12491 EMSG2(_(e_invexpr2), arg);
12492 nr = 0;
12493 }
12494 }
12495
12496 if (nr > 0)
12497 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12498 wp != twin; wp = wp->w_next)
12499 {
12500 if (wp == NULL)
12501 {
12502 /* didn't find it in this tabpage */
12503 nr = 0;
12504 break;
12505 }
12506 ++nr;
12507 }
12508 return nr;
12509}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012510
12511/*
12512 * "tabpagewinnr()" function
12513 */
12514 static void
12515f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
12516{
12517 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012518 tabpage_T *tp;
12519
12520 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12521 if (tp == NULL)
12522 nr = 0;
12523 else
12524 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012525 rettv->vval.v_number = nr;
12526}
12527
12528
12529/*
12530 * "tagfiles()" function
12531 */
12532 static void
12533f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
12534{
12535 char_u *fname;
12536 tagname_T tn;
12537 int first;
12538
12539 if (rettv_list_alloc(rettv) == FAIL)
12540 return;
12541 fname = alloc(MAXPATHL);
12542 if (fname == NULL)
12543 return;
12544
12545 for (first = TRUE; ; first = FALSE)
12546 if (get_tagfname(&tn, first, fname) == FAIL
12547 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
12548 break;
12549 tagname_free(&tn);
12550 vim_free(fname);
12551}
12552
12553/*
12554 * "taglist()" function
12555 */
12556 static void
12557f_taglist(typval_T *argvars, typval_T *rettv)
12558{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012559 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012560 char_u *tag_pattern;
12561
12562 tag_pattern = get_tv_string(&argvars[0]);
12563
12564 rettv->vval.v_number = FALSE;
12565 if (*tag_pattern == NUL)
12566 return;
12567
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012568 if (argvars[1].v_type != VAR_UNKNOWN)
12569 fname = get_tv_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012570 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012571 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012572}
12573
12574/*
12575 * "tempname()" function
12576 */
12577 static void
12578f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
12579{
12580 static int x = 'A';
12581
12582 rettv->v_type = VAR_STRING;
12583 rettv->vval.v_string = vim_tempname(x, FALSE);
12584
12585 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
12586 * names. Skip 'I' and 'O', they are used for shell redirection. */
12587 do
12588 {
12589 if (x == 'Z')
12590 x = '0';
12591 else if (x == '9')
12592 x = 'A';
12593 else
12594 {
12595#ifdef EBCDIC
12596 if (x == 'I')
12597 x = 'J';
12598 else if (x == 'R')
12599 x = 'S';
12600 else
12601#endif
12602 ++x;
12603 }
12604 } while (x == 'I' || x == 'O');
12605}
12606
12607#ifdef FEAT_FLOAT
12608/*
12609 * "tan()" function
12610 */
12611 static void
12612f_tan(typval_T *argvars, typval_T *rettv)
12613{
12614 float_T f = 0.0;
12615
12616 rettv->v_type = VAR_FLOAT;
12617 if (get_float_arg(argvars, &f) == OK)
12618 rettv->vval.v_float = tan(f);
12619 else
12620 rettv->vval.v_float = 0.0;
12621}
12622
12623/*
12624 * "tanh()" function
12625 */
12626 static void
12627f_tanh(typval_T *argvars, typval_T *rettv)
12628{
12629 float_T f = 0.0;
12630
12631 rettv->v_type = VAR_FLOAT;
12632 if (get_float_arg(argvars, &f) == OK)
12633 rettv->vval.v_float = tanh(f);
12634 else
12635 rettv->vval.v_float = 0.0;
12636}
12637#endif
12638
12639/*
12640 * "test_alloc_fail(id, countdown, repeat)" function
12641 */
12642 static void
12643f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
12644{
12645 if (argvars[0].v_type != VAR_NUMBER
12646 || argvars[0].vval.v_number <= 0
12647 || argvars[1].v_type != VAR_NUMBER
12648 || argvars[1].vval.v_number < 0
12649 || argvars[2].v_type != VAR_NUMBER)
12650 EMSG(_(e_invarg));
12651 else
12652 {
12653 alloc_fail_id = argvars[0].vval.v_number;
12654 if (alloc_fail_id >= aid_last)
12655 EMSG(_(e_invarg));
12656 alloc_fail_countdown = argvars[1].vval.v_number;
12657 alloc_fail_repeat = argvars[2].vval.v_number;
12658 did_outofmem_msg = FALSE;
12659 }
12660}
12661
12662/*
12663 * "test_autochdir()"
12664 */
12665 static void
12666f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12667{
12668#if defined(FEAT_AUTOCHDIR)
12669 test_autochdir = TRUE;
12670#endif
12671}
12672
12673/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020012674 * "test_feedinput()"
12675 */
12676 static void
12677f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
12678{
12679#ifdef USE_INPUT_BUF
12680 char_u *val = get_tv_string_chk(&argvars[0]);
12681
12682 if (val != NULL)
12683 {
12684 trash_input_buf();
12685 add_to_input_buf_csi(val, (int)STRLEN(val));
12686 }
12687#endif
12688}
12689
12690/*
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012691 * "test_disable({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012692 */
12693 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012694f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012695{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012696 char_u *name = (char_u *)"";
12697 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012698 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012699
12700 if (argvars[0].v_type != VAR_STRING
12701 || (argvars[1].v_type) != VAR_NUMBER)
12702 EMSG(_(e_invarg));
12703 else
12704 {
12705 name = get_tv_string_chk(&argvars[0]);
12706 val = (int)get_tv_number(&argvars[1]);
12707
12708 if (STRCMP(name, (char_u *)"redraw") == 0)
12709 disable_redraw_for_testing = val;
12710 else if (STRCMP(name, (char_u *)"char_avail") == 0)
12711 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012712 else if (STRCMP(name, (char_u *)"starting") == 0)
12713 {
12714 if (val)
12715 {
12716 if (save_starting < 0)
12717 save_starting = starting;
12718 starting = 0;
12719 }
12720 else
12721 {
12722 starting = save_starting;
12723 save_starting = -1;
12724 }
12725 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012726 else if (STRCMP(name, (char_u *)"ALL") == 0)
12727 {
12728 disable_char_avail_for_testing = FALSE;
12729 disable_redraw_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012730 if (save_starting >= 0)
12731 {
12732 starting = save_starting;
12733 save_starting = -1;
12734 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012735 }
12736 else
12737 EMSG2(_(e_invarg2), name);
12738 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012739}
12740
12741/*
12742 * "test_garbagecollect_now()" function
12743 */
12744 static void
12745f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12746{
12747 /* This is dangerous, any Lists and Dicts used internally may be freed
12748 * while still in use. */
12749 garbage_collect(TRUE);
12750}
12751
Bram Moolenaare0c31f62017-03-01 15:07:05 +010012752/*
12753 * "test_ignore_error()" function
12754 */
12755 static void
12756f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
12757{
12758 ignore_error_for_testing(get_tv_string(&argvars[0]));
12759}
12760
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012761#ifdef FEAT_JOB_CHANNEL
12762 static void
12763f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
12764{
12765 rettv->v_type = VAR_CHANNEL;
12766 rettv->vval.v_channel = NULL;
12767}
12768#endif
12769
12770 static void
12771f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
12772{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012773 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012774}
12775
12776#ifdef FEAT_JOB_CHANNEL
12777 static void
12778f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
12779{
12780 rettv->v_type = VAR_JOB;
12781 rettv->vval.v_job = NULL;
12782}
12783#endif
12784
12785 static void
12786f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
12787{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012788 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012789}
12790
12791 static void
12792f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
12793{
12794 rettv->v_type = VAR_PARTIAL;
12795 rettv->vval.v_partial = NULL;
12796}
12797
12798 static void
12799f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
12800{
12801 rettv->v_type = VAR_STRING;
12802 rettv->vval.v_string = NULL;
12803}
12804
12805 static void
12806f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
12807{
12808 time_for_testing = (time_t)get_tv_number(&argvars[0]);
12809}
12810
12811#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
12812/*
12813 * Get a callback from "arg". It can be a Funcref or a function name.
12814 * When "arg" is zero return an empty string.
12815 * Return NULL for an invalid argument.
12816 */
12817 char_u *
12818get_callback(typval_T *arg, partial_T **pp)
12819{
12820 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
12821 {
12822 *pp = arg->vval.v_partial;
12823 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012824 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012825 }
12826 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012827 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012828 {
12829 func_ref(arg->vval.v_string);
12830 return arg->vval.v_string;
12831 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012832 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
12833 return (char_u *)"";
12834 EMSG(_("E921: Invalid callback argument"));
12835 return NULL;
12836}
12837
12838/*
12839 * Unref/free "callback" and "partial" retured by get_callback().
12840 */
12841 void
12842free_callback(char_u *callback, partial_T *partial)
12843{
12844 if (partial != NULL)
12845 partial_unref(partial);
12846 else if (callback != NULL)
12847 {
12848 func_unref(callback);
12849 vim_free(callback);
12850 }
12851}
12852#endif
12853
12854#ifdef FEAT_TIMERS
12855/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020012856 * "timer_info([timer])" function
12857 */
12858 static void
12859f_timer_info(typval_T *argvars, typval_T *rettv)
12860{
12861 timer_T *timer = NULL;
12862
12863 if (rettv_list_alloc(rettv) != OK)
12864 return;
12865 if (argvars[0].v_type != VAR_UNKNOWN)
12866 {
12867 if (argvars[0].v_type != VAR_NUMBER)
12868 EMSG(_(e_number_exp));
12869 else
12870 {
12871 timer = find_timer((int)get_tv_number(&argvars[0]));
12872 if (timer != NULL)
12873 add_timer_info(rettv, timer);
12874 }
12875 }
12876 else
12877 add_timer_info_all(rettv);
12878}
12879
12880/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012881 * "timer_pause(timer, paused)" function
12882 */
12883 static void
12884f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
12885{
12886 timer_T *timer = NULL;
12887 int paused = (int)get_tv_number(&argvars[1]);
12888
12889 if (argvars[0].v_type != VAR_NUMBER)
12890 EMSG(_(e_number_exp));
12891 else
12892 {
12893 timer = find_timer((int)get_tv_number(&argvars[0]));
12894 if (timer != NULL)
12895 timer->tr_paused = paused;
12896 }
12897}
12898
12899/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012900 * "timer_start(time, callback [, options])" function
12901 */
12902 static void
12903f_timer_start(typval_T *argvars, typval_T *rettv)
12904{
Bram Moolenaar75537a92016-09-05 22:45:28 +020012905 long msec = (long)get_tv_number(&argvars[0]);
12906 timer_T *timer;
12907 int repeat = 0;
12908 char_u *callback;
12909 dict_T *dict;
12910 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012911
Bram Moolenaar75537a92016-09-05 22:45:28 +020012912 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012913 if (check_secure())
12914 return;
12915 if (argvars[2].v_type != VAR_UNKNOWN)
12916 {
12917 if (argvars[2].v_type != VAR_DICT
12918 || (dict = argvars[2].vval.v_dict) == NULL)
12919 {
12920 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
12921 return;
12922 }
12923 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
12924 repeat = get_dict_number(dict, (char_u *)"repeat");
12925 }
12926
Bram Moolenaar75537a92016-09-05 22:45:28 +020012927 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012928 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020012929 return;
12930
12931 timer = create_timer(msec, repeat);
12932 if (timer == NULL)
12933 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012934 else
12935 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020012936 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020012937 timer->tr_callback = vim_strsave(callback);
12938 else
12939 /* pointer into the partial */
12940 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020012941 timer->tr_partial = partial;
12942 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012943 }
12944}
12945
12946/*
12947 * "timer_stop(timer)" function
12948 */
12949 static void
12950f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
12951{
12952 timer_T *timer;
12953
12954 if (argvars[0].v_type != VAR_NUMBER)
12955 {
12956 EMSG(_(e_number_exp));
12957 return;
12958 }
12959 timer = find_timer((int)get_tv_number(&argvars[0]));
12960 if (timer != NULL)
12961 stop_timer(timer);
12962}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012963
12964/*
12965 * "timer_stopall()" function
12966 */
12967 static void
12968f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12969{
12970 stop_all_timers();
12971}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012972#endif
12973
12974/*
12975 * "tolower(string)" function
12976 */
12977 static void
12978f_tolower(typval_T *argvars, typval_T *rettv)
12979{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012980 rettv->v_type = VAR_STRING;
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +010012981 rettv->vval.v_string = strlow_save(get_tv_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012982}
12983
12984/*
12985 * "toupper(string)" function
12986 */
12987 static void
12988f_toupper(typval_T *argvars, typval_T *rettv)
12989{
12990 rettv->v_type = VAR_STRING;
12991 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
12992}
12993
12994/*
12995 * "tr(string, fromstr, tostr)" function
12996 */
12997 static void
12998f_tr(typval_T *argvars, typval_T *rettv)
12999{
13000 char_u *in_str;
13001 char_u *fromstr;
13002 char_u *tostr;
13003 char_u *p;
13004#ifdef FEAT_MBYTE
13005 int inlen;
13006 int fromlen;
13007 int tolen;
13008 int idx;
13009 char_u *cpstr;
13010 int cplen;
13011 int first = TRUE;
13012#endif
13013 char_u buf[NUMBUFLEN];
13014 char_u buf2[NUMBUFLEN];
13015 garray_T ga;
13016
13017 in_str = get_tv_string(&argvars[0]);
13018 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
13019 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
13020
13021 /* Default return value: empty string. */
13022 rettv->v_type = VAR_STRING;
13023 rettv->vval.v_string = NULL;
13024 if (fromstr == NULL || tostr == NULL)
13025 return; /* type error; errmsg already given */
13026 ga_init2(&ga, (int)sizeof(char), 80);
13027
13028#ifdef FEAT_MBYTE
13029 if (!has_mbyte)
13030#endif
13031 /* not multi-byte: fromstr and tostr must be the same length */
13032 if (STRLEN(fromstr) != STRLEN(tostr))
13033 {
13034#ifdef FEAT_MBYTE
13035error:
13036#endif
13037 EMSG2(_(e_invarg2), fromstr);
13038 ga_clear(&ga);
13039 return;
13040 }
13041
13042 /* fromstr and tostr have to contain the same number of chars */
13043 while (*in_str != NUL)
13044 {
13045#ifdef FEAT_MBYTE
13046 if (has_mbyte)
13047 {
13048 inlen = (*mb_ptr2len)(in_str);
13049 cpstr = in_str;
13050 cplen = inlen;
13051 idx = 0;
13052 for (p = fromstr; *p != NUL; p += fromlen)
13053 {
13054 fromlen = (*mb_ptr2len)(p);
13055 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
13056 {
13057 for (p = tostr; *p != NUL; p += tolen)
13058 {
13059 tolen = (*mb_ptr2len)(p);
13060 if (idx-- == 0)
13061 {
13062 cplen = tolen;
13063 cpstr = p;
13064 break;
13065 }
13066 }
13067 if (*p == NUL) /* tostr is shorter than fromstr */
13068 goto error;
13069 break;
13070 }
13071 ++idx;
13072 }
13073
13074 if (first && cpstr == in_str)
13075 {
13076 /* Check that fromstr and tostr have the same number of
13077 * (multi-byte) characters. Done only once when a character
13078 * of in_str doesn't appear in fromstr. */
13079 first = FALSE;
13080 for (p = tostr; *p != NUL; p += tolen)
13081 {
13082 tolen = (*mb_ptr2len)(p);
13083 --idx;
13084 }
13085 if (idx != 0)
13086 goto error;
13087 }
13088
13089 (void)ga_grow(&ga, cplen);
13090 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
13091 ga.ga_len += cplen;
13092
13093 in_str += inlen;
13094 }
13095 else
13096#endif
13097 {
13098 /* When not using multi-byte chars we can do it faster. */
13099 p = vim_strchr(fromstr, *in_str);
13100 if (p != NULL)
13101 ga_append(&ga, tostr[p - fromstr]);
13102 else
13103 ga_append(&ga, *in_str);
13104 ++in_str;
13105 }
13106 }
13107
13108 /* add a terminating NUL */
13109 (void)ga_grow(&ga, 1);
13110 ga_append(&ga, NUL);
13111
13112 rettv->vval.v_string = ga.ga_data;
13113}
13114
13115#ifdef FEAT_FLOAT
13116/*
13117 * "trunc({float})" function
13118 */
13119 static void
13120f_trunc(typval_T *argvars, typval_T *rettv)
13121{
13122 float_T f = 0.0;
13123
13124 rettv->v_type = VAR_FLOAT;
13125 if (get_float_arg(argvars, &f) == OK)
13126 /* trunc() is not in C90, use floor() or ceil() instead. */
13127 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
13128 else
13129 rettv->vval.v_float = 0.0;
13130}
13131#endif
13132
13133/*
13134 * "type(expr)" function
13135 */
13136 static void
13137f_type(typval_T *argvars, typval_T *rettv)
13138{
13139 int n = -1;
13140
13141 switch (argvars[0].v_type)
13142 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020013143 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
13144 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013145 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020013146 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
13147 case VAR_LIST: n = VAR_TYPE_LIST; break;
13148 case VAR_DICT: n = VAR_TYPE_DICT; break;
13149 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013150 case VAR_SPECIAL:
13151 if (argvars[0].vval.v_number == VVAL_FALSE
13152 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020013153 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013154 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020013155 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013156 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020013157 case VAR_JOB: n = VAR_TYPE_JOB; break;
13158 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013159 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010013160 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013161 n = -1;
13162 break;
13163 }
13164 rettv->vval.v_number = n;
13165}
13166
13167/*
13168 * "undofile(name)" function
13169 */
13170 static void
13171f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
13172{
13173 rettv->v_type = VAR_STRING;
13174#ifdef FEAT_PERSISTENT_UNDO
13175 {
13176 char_u *fname = get_tv_string(&argvars[0]);
13177
13178 if (*fname == NUL)
13179 {
13180 /* If there is no file name there will be no undo file. */
13181 rettv->vval.v_string = NULL;
13182 }
13183 else
13184 {
13185 char_u *ffname = FullName_save(fname, FALSE);
13186
13187 if (ffname != NULL)
13188 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
13189 vim_free(ffname);
13190 }
13191 }
13192#else
13193 rettv->vval.v_string = NULL;
13194#endif
13195}
13196
13197/*
13198 * "undotree()" function
13199 */
13200 static void
13201f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
13202{
13203 if (rettv_dict_alloc(rettv) == OK)
13204 {
13205 dict_T *dict = rettv->vval.v_dict;
13206 list_T *list;
13207
13208 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
13209 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
13210 dict_add_nr_str(dict, "save_last",
13211 (long)curbuf->b_u_save_nr_last, NULL);
13212 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
13213 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
13214 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
13215
13216 list = list_alloc();
13217 if (list != NULL)
13218 {
13219 u_eval_tree(curbuf->b_u_oldhead, list);
13220 dict_add_list(dict, "entries", list);
13221 }
13222 }
13223}
13224
13225/*
13226 * "values(dict)" function
13227 */
13228 static void
13229f_values(typval_T *argvars, typval_T *rettv)
13230{
13231 dict_list(argvars, rettv, 1);
13232}
13233
13234/*
13235 * "virtcol(string)" function
13236 */
13237 static void
13238f_virtcol(typval_T *argvars, typval_T *rettv)
13239{
13240 colnr_T vcol = 0;
13241 pos_T *fp;
13242 int fnum = curbuf->b_fnum;
13243
13244 fp = var2fpos(&argvars[0], FALSE, &fnum);
13245 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
13246 && fnum == curbuf->b_fnum)
13247 {
13248 getvvcol(curwin, fp, NULL, NULL, &vcol);
13249 ++vcol;
13250 }
13251
13252 rettv->vval.v_number = vcol;
13253}
13254
13255/*
13256 * "visualmode()" function
13257 */
13258 static void
13259f_visualmode(typval_T *argvars, typval_T *rettv)
13260{
13261 char_u str[2];
13262
13263 rettv->v_type = VAR_STRING;
13264 str[0] = curbuf->b_visual_mode_eval;
13265 str[1] = NUL;
13266 rettv->vval.v_string = vim_strsave(str);
13267
13268 /* A non-zero number or non-empty string argument: reset mode. */
13269 if (non_zero_arg(&argvars[0]))
13270 curbuf->b_visual_mode_eval = NUL;
13271}
13272
13273/*
13274 * "wildmenumode()" function
13275 */
13276 static void
13277f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13278{
13279#ifdef FEAT_WILDMENU
13280 if (wild_menu_showing)
13281 rettv->vval.v_number = 1;
13282#endif
13283}
13284
13285/*
13286 * "winbufnr(nr)" function
13287 */
13288 static void
13289f_winbufnr(typval_T *argvars, typval_T *rettv)
13290{
13291 win_T *wp;
13292
13293 wp = find_win_by_nr(&argvars[0], NULL);
13294 if (wp == NULL)
13295 rettv->vval.v_number = -1;
13296 else
13297 rettv->vval.v_number = wp->w_buffer->b_fnum;
13298}
13299
13300/*
13301 * "wincol()" function
13302 */
13303 static void
13304f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
13305{
13306 validate_cursor();
13307 rettv->vval.v_number = curwin->w_wcol + 1;
13308}
13309
13310/*
13311 * "winheight(nr)" function
13312 */
13313 static void
13314f_winheight(typval_T *argvars, typval_T *rettv)
13315{
13316 win_T *wp;
13317
13318 wp = find_win_by_nr(&argvars[0], NULL);
13319 if (wp == NULL)
13320 rettv->vval.v_number = -1;
13321 else
13322 rettv->vval.v_number = wp->w_height;
13323}
13324
13325/*
13326 * "winline()" function
13327 */
13328 static void
13329f_winline(typval_T *argvars UNUSED, typval_T *rettv)
13330{
13331 validate_cursor();
13332 rettv->vval.v_number = curwin->w_wrow + 1;
13333}
13334
13335/*
13336 * "winnr()" function
13337 */
13338 static void
13339f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
13340{
13341 int nr = 1;
13342
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013343 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013344 rettv->vval.v_number = nr;
13345}
13346
13347/*
13348 * "winrestcmd()" function
13349 */
13350 static void
13351f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
13352{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013353 win_T *wp;
13354 int winnr = 1;
13355 garray_T ga;
13356 char_u buf[50];
13357
13358 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020013359 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013360 {
13361 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
13362 ga_concat(&ga, buf);
13363 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
13364 ga_concat(&ga, buf);
13365 ++winnr;
13366 }
13367 ga_append(&ga, NUL);
13368
13369 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013370 rettv->v_type = VAR_STRING;
13371}
13372
13373/*
13374 * "winrestview()" function
13375 */
13376 static void
13377f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
13378{
13379 dict_T *dict;
13380
13381 if (argvars[0].v_type != VAR_DICT
13382 || (dict = argvars[0].vval.v_dict) == NULL)
13383 EMSG(_(e_invarg));
13384 else
13385 {
13386 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
13387 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
13388 if (dict_find(dict, (char_u *)"col", -1) != NULL)
13389 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
13390#ifdef FEAT_VIRTUALEDIT
13391 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
13392 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
13393#endif
13394 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
13395 {
13396 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
13397 curwin->w_set_curswant = FALSE;
13398 }
13399
13400 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
13401 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
13402#ifdef FEAT_DIFF
13403 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
13404 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
13405#endif
13406 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
13407 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
13408 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
13409 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
13410
13411 check_cursor();
13412 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020013413 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013414 changed_window_setting();
13415
13416 if (curwin->w_topline <= 0)
13417 curwin->w_topline = 1;
13418 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
13419 curwin->w_topline = curbuf->b_ml.ml_line_count;
13420#ifdef FEAT_DIFF
13421 check_topfill(curwin, TRUE);
13422#endif
13423 }
13424}
13425
13426/*
13427 * "winsaveview()" function
13428 */
13429 static void
13430f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
13431{
13432 dict_T *dict;
13433
13434 if (rettv_dict_alloc(rettv) == FAIL)
13435 return;
13436 dict = rettv->vval.v_dict;
13437
13438 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
13439 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
13440#ifdef FEAT_VIRTUALEDIT
13441 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
13442#endif
13443 update_curswant();
13444 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
13445
13446 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
13447#ifdef FEAT_DIFF
13448 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
13449#endif
13450 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
13451 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
13452}
13453
13454/*
13455 * "winwidth(nr)" function
13456 */
13457 static void
13458f_winwidth(typval_T *argvars, typval_T *rettv)
13459{
13460 win_T *wp;
13461
13462 wp = find_win_by_nr(&argvars[0], NULL);
13463 if (wp == NULL)
13464 rettv->vval.v_number = -1;
13465 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013466 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013467}
13468
13469/*
13470 * "wordcount()" function
13471 */
13472 static void
13473f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
13474{
13475 if (rettv_dict_alloc(rettv) == FAIL)
13476 return;
13477 cursor_pos_info(rettv->vval.v_dict);
13478}
13479
13480/*
13481 * "writefile()" function
13482 */
13483 static void
13484f_writefile(typval_T *argvars, typval_T *rettv)
13485{
13486 int binary = FALSE;
13487 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013488#ifdef HAVE_FSYNC
13489 int do_fsync = p_fs;
13490#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013491 char_u *fname;
13492 FILE *fd;
13493 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013494 listitem_T *li;
13495 list_T *list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013496
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013497 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013498 if (check_restricted() || check_secure())
13499 return;
13500
13501 if (argvars[0].v_type != VAR_LIST)
13502 {
13503 EMSG2(_(e_listarg), "writefile()");
13504 return;
13505 }
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013506 list = argvars[0].vval.v_list;
13507 if (list == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013508 return;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013509 for (li = list->lv_first; li != NULL; li = li->li_next)
13510 if (get_tv_string_chk(&li->li_tv) == NULL)
13511 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013512
13513 if (argvars[2].v_type != VAR_UNKNOWN)
13514 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013515 char_u *arg2 = get_tv_string_chk(&argvars[2]);
13516
13517 if (arg2 == NULL)
13518 return;
13519 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013520 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013521 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013522 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013523#ifdef HAVE_FSYNC
13524 if (vim_strchr(arg2, 's') != NULL)
13525 do_fsync = TRUE;
13526 else if (vim_strchr(arg2, 'S') != NULL)
13527 do_fsync = FALSE;
13528#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013529 }
13530
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013531 fname = get_tv_string_chk(&argvars[1]);
13532 if (fname == NULL)
13533 return;
13534
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013535 /* Always open the file in binary mode, library functions have a mind of
13536 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013537 if (*fname == NUL || (fd = mch_fopen((char *)fname,
13538 append ? APPENDBIN : WRITEBIN)) == NULL)
13539 {
13540 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
13541 ret = -1;
13542 }
13543 else
13544 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013545 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013546 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013547#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010013548 else if (do_fsync)
13549 /* Ignore the error, the user wouldn't know what to do about it.
13550 * May happen for a device. */
13551 ignored = fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013552#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013553 fclose(fd);
13554 }
13555
13556 rettv->vval.v_number = ret;
13557}
13558
13559/*
13560 * "xor(expr, expr)" function
13561 */
13562 static void
13563f_xor(typval_T *argvars, typval_T *rettv)
13564{
13565 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
13566 ^ get_tv_number_chk(&argvars[1], NULL);
13567}
13568
13569
13570#endif /* FEAT_EVAL */