blob: 72690847276185481fc54c81eae62936d294dbda [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);
183static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200184static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200185static void f_getmatches(typval_T *argvars, typval_T *rettv);
186static void f_getpid(typval_T *argvars, typval_T *rettv);
187static void f_getcurpos(typval_T *argvars, typval_T *rettv);
188static void f_getpos(typval_T *argvars, typval_T *rettv);
189static void f_getqflist(typval_T *argvars, typval_T *rettv);
190static void f_getreg(typval_T *argvars, typval_T *rettv);
191static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200192static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200193static void f_gettabvar(typval_T *argvars, typval_T *rettv);
194static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200195static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200196static void f_getwinposx(typval_T *argvars, typval_T *rettv);
197static void f_getwinposy(typval_T *argvars, typval_T *rettv);
198static void f_getwinvar(typval_T *argvars, typval_T *rettv);
199static void f_glob(typval_T *argvars, typval_T *rettv);
200static void f_globpath(typval_T *argvars, typval_T *rettv);
201static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
202static void f_has(typval_T *argvars, typval_T *rettv);
203static void f_has_key(typval_T *argvars, typval_T *rettv);
204static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
205static void f_hasmapto(typval_T *argvars, typval_T *rettv);
206static void f_histadd(typval_T *argvars, typval_T *rettv);
207static void f_histdel(typval_T *argvars, typval_T *rettv);
208static void f_histget(typval_T *argvars, typval_T *rettv);
209static void f_histnr(typval_T *argvars, typval_T *rettv);
210static void f_hlID(typval_T *argvars, typval_T *rettv);
211static void f_hlexists(typval_T *argvars, typval_T *rettv);
212static void f_hostname(typval_T *argvars, typval_T *rettv);
213static void f_iconv(typval_T *argvars, typval_T *rettv);
214static void f_indent(typval_T *argvars, typval_T *rettv);
215static void f_index(typval_T *argvars, typval_T *rettv);
216static void f_input(typval_T *argvars, typval_T *rettv);
217static void f_inputdialog(typval_T *argvars, typval_T *rettv);
218static void f_inputlist(typval_T *argvars, typval_T *rettv);
219static void f_inputrestore(typval_T *argvars, typval_T *rettv);
220static void f_inputsave(typval_T *argvars, typval_T *rettv);
221static void f_inputsecret(typval_T *argvars, typval_T *rettv);
222static void f_insert(typval_T *argvars, typval_T *rettv);
223static void f_invert(typval_T *argvars, typval_T *rettv);
224static void f_isdirectory(typval_T *argvars, typval_T *rettv);
225static void f_islocked(typval_T *argvars, typval_T *rettv);
226#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
227static void f_isnan(typval_T *argvars, typval_T *rettv);
228#endif
229static void f_items(typval_T *argvars, typval_T *rettv);
230#ifdef FEAT_JOB_CHANNEL
231static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
232static void f_job_info(typval_T *argvars, typval_T *rettv);
233static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
234static void f_job_start(typval_T *argvars, typval_T *rettv);
235static void f_job_stop(typval_T *argvars, typval_T *rettv);
236static void f_job_status(typval_T *argvars, typval_T *rettv);
237#endif
238static void f_join(typval_T *argvars, typval_T *rettv);
239static void f_js_decode(typval_T *argvars, typval_T *rettv);
240static void f_js_encode(typval_T *argvars, typval_T *rettv);
241static void f_json_decode(typval_T *argvars, typval_T *rettv);
242static void f_json_encode(typval_T *argvars, typval_T *rettv);
243static void f_keys(typval_T *argvars, typval_T *rettv);
244static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
245static void f_len(typval_T *argvars, typval_T *rettv);
246static void f_libcall(typval_T *argvars, typval_T *rettv);
247static void f_libcallnr(typval_T *argvars, typval_T *rettv);
248static void f_line(typval_T *argvars, typval_T *rettv);
249static void f_line2byte(typval_T *argvars, typval_T *rettv);
250static void f_lispindent(typval_T *argvars, typval_T *rettv);
251static void f_localtime(typval_T *argvars, typval_T *rettv);
252#ifdef FEAT_FLOAT
253static void f_log(typval_T *argvars, typval_T *rettv);
254static void f_log10(typval_T *argvars, typval_T *rettv);
255#endif
256#ifdef FEAT_LUA
257static void f_luaeval(typval_T *argvars, typval_T *rettv);
258#endif
259static void f_map(typval_T *argvars, typval_T *rettv);
260static void f_maparg(typval_T *argvars, typval_T *rettv);
261static void f_mapcheck(typval_T *argvars, typval_T *rettv);
262static void f_match(typval_T *argvars, typval_T *rettv);
263static void f_matchadd(typval_T *argvars, typval_T *rettv);
264static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
265static void f_matcharg(typval_T *argvars, typval_T *rettv);
266static void f_matchdelete(typval_T *argvars, typval_T *rettv);
267static void f_matchend(typval_T *argvars, typval_T *rettv);
268static void f_matchlist(typval_T *argvars, typval_T *rettv);
269static void f_matchstr(typval_T *argvars, typval_T *rettv);
270static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
271static void f_max(typval_T *argvars, typval_T *rettv);
272static void f_min(typval_T *argvars, typval_T *rettv);
273#ifdef vim_mkdir
274static void f_mkdir(typval_T *argvars, typval_T *rettv);
275#endif
276static void f_mode(typval_T *argvars, typval_T *rettv);
277#ifdef FEAT_MZSCHEME
278static void f_mzeval(typval_T *argvars, typval_T *rettv);
279#endif
280static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
281static void f_nr2char(typval_T *argvars, typval_T *rettv);
282static void f_or(typval_T *argvars, typval_T *rettv);
283static void f_pathshorten(typval_T *argvars, typval_T *rettv);
284#ifdef FEAT_PERL
285static void f_perleval(typval_T *argvars, typval_T *rettv);
286#endif
287#ifdef FEAT_FLOAT
288static void f_pow(typval_T *argvars, typval_T *rettv);
289#endif
290static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
291static void f_printf(typval_T *argvars, typval_T *rettv);
292static void f_pumvisible(typval_T *argvars, typval_T *rettv);
293#ifdef FEAT_PYTHON3
294static void f_py3eval(typval_T *argvars, typval_T *rettv);
295#endif
296#ifdef FEAT_PYTHON
297static void f_pyeval(typval_T *argvars, typval_T *rettv);
298#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100299#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
300static void f_pyxeval(typval_T *argvars, typval_T *rettv);
301#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200302static void f_range(typval_T *argvars, typval_T *rettv);
303static void f_readfile(typval_T *argvars, typval_T *rettv);
304static void f_reltime(typval_T *argvars, typval_T *rettv);
305#ifdef FEAT_FLOAT
306static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
307#endif
308static void f_reltimestr(typval_T *argvars, typval_T *rettv);
309static void f_remote_expr(typval_T *argvars, typval_T *rettv);
310static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
311static void f_remote_peek(typval_T *argvars, typval_T *rettv);
312static void f_remote_read(typval_T *argvars, typval_T *rettv);
313static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100314static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200315static void f_remove(typval_T *argvars, typval_T *rettv);
316static void f_rename(typval_T *argvars, typval_T *rettv);
317static void f_repeat(typval_T *argvars, typval_T *rettv);
318static void f_resolve(typval_T *argvars, typval_T *rettv);
319static void f_reverse(typval_T *argvars, typval_T *rettv);
320#ifdef FEAT_FLOAT
321static void f_round(typval_T *argvars, typval_T *rettv);
322#endif
323static void f_screenattr(typval_T *argvars, typval_T *rettv);
324static void f_screenchar(typval_T *argvars, typval_T *rettv);
325static void f_screencol(typval_T *argvars, typval_T *rettv);
326static void f_screenrow(typval_T *argvars, typval_T *rettv);
327static void f_search(typval_T *argvars, typval_T *rettv);
328static void f_searchdecl(typval_T *argvars, typval_T *rettv);
329static void f_searchpair(typval_T *argvars, typval_T *rettv);
330static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
331static void f_searchpos(typval_T *argvars, typval_T *rettv);
332static void f_server2client(typval_T *argvars, typval_T *rettv);
333static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200334static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200335static void f_setbufvar(typval_T *argvars, typval_T *rettv);
336static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
337static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
338static void f_setfperm(typval_T *argvars, typval_T *rettv);
339static void f_setline(typval_T *argvars, typval_T *rettv);
340static void f_setloclist(typval_T *argvars, typval_T *rettv);
341static void f_setmatches(typval_T *argvars, typval_T *rettv);
342static void f_setpos(typval_T *argvars, typval_T *rettv);
343static void f_setqflist(typval_T *argvars, typval_T *rettv);
344static void f_setreg(typval_T *argvars, typval_T *rettv);
345static void f_settabvar(typval_T *argvars, typval_T *rettv);
346static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
347static void f_setwinvar(typval_T *argvars, typval_T *rettv);
348#ifdef FEAT_CRYPT
349static void f_sha256(typval_T *argvars, typval_T *rettv);
350#endif /* FEAT_CRYPT */
351static void f_shellescape(typval_T *argvars, typval_T *rettv);
352static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
353static void f_simplify(typval_T *argvars, typval_T *rettv);
354#ifdef FEAT_FLOAT
355static void f_sin(typval_T *argvars, typval_T *rettv);
356static void f_sinh(typval_T *argvars, typval_T *rettv);
357#endif
358static void f_sort(typval_T *argvars, typval_T *rettv);
359static void f_soundfold(typval_T *argvars, typval_T *rettv);
360static void f_spellbadword(typval_T *argvars, typval_T *rettv);
361static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
362static void f_split(typval_T *argvars, typval_T *rettv);
363#ifdef FEAT_FLOAT
364static void f_sqrt(typval_T *argvars, typval_T *rettv);
365static void f_str2float(typval_T *argvars, typval_T *rettv);
366#endif
367static void f_str2nr(typval_T *argvars, typval_T *rettv);
368static void f_strchars(typval_T *argvars, typval_T *rettv);
369#ifdef HAVE_STRFTIME
370static void f_strftime(typval_T *argvars, typval_T *rettv);
371#endif
372static void f_strgetchar(typval_T *argvars, typval_T *rettv);
373static void f_stridx(typval_T *argvars, typval_T *rettv);
374static void f_string(typval_T *argvars, typval_T *rettv);
375static void f_strlen(typval_T *argvars, typval_T *rettv);
376static void f_strcharpart(typval_T *argvars, typval_T *rettv);
377static void f_strpart(typval_T *argvars, typval_T *rettv);
378static void f_strridx(typval_T *argvars, typval_T *rettv);
379static void f_strtrans(typval_T *argvars, typval_T *rettv);
380static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
381static void f_strwidth(typval_T *argvars, typval_T *rettv);
382static void f_submatch(typval_T *argvars, typval_T *rettv);
383static void f_substitute(typval_T *argvars, typval_T *rettv);
384static void f_synID(typval_T *argvars, typval_T *rettv);
385static void f_synIDattr(typval_T *argvars, typval_T *rettv);
386static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
387static void f_synstack(typval_T *argvars, typval_T *rettv);
388static void f_synconcealed(typval_T *argvars, typval_T *rettv);
389static void f_system(typval_T *argvars, typval_T *rettv);
390static void f_systemlist(typval_T *argvars, typval_T *rettv);
391static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
392static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
393static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
394static void f_taglist(typval_T *argvars, typval_T *rettv);
395static void f_tagfiles(typval_T *argvars, typval_T *rettv);
396static void f_tempname(typval_T *argvars, typval_T *rettv);
397static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
398static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200399static void f_test_feedinput(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100400static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200401static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100402static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200403#ifdef FEAT_JOB_CHANNEL
404static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
405#endif
406static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
407#ifdef FEAT_JOB_CHANNEL
408static void f_test_null_job(typval_T *argvars, typval_T *rettv);
409#endif
410static void f_test_null_list(typval_T *argvars, typval_T *rettv);
411static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
412static void f_test_null_string(typval_T *argvars, typval_T *rettv);
413static void f_test_settime(typval_T *argvars, typval_T *rettv);
414#ifdef FEAT_FLOAT
415static void f_tan(typval_T *argvars, typval_T *rettv);
416static void f_tanh(typval_T *argvars, typval_T *rettv);
417#endif
418#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200419static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200420static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200421static void f_timer_start(typval_T *argvars, typval_T *rettv);
422static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200423static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200424#endif
425static void f_tolower(typval_T *argvars, typval_T *rettv);
426static void f_toupper(typval_T *argvars, typval_T *rettv);
427static void f_tr(typval_T *argvars, typval_T *rettv);
428#ifdef FEAT_FLOAT
429static void f_trunc(typval_T *argvars, typval_T *rettv);
430#endif
431static void f_type(typval_T *argvars, typval_T *rettv);
432static void f_undofile(typval_T *argvars, typval_T *rettv);
433static void f_undotree(typval_T *argvars, typval_T *rettv);
434static void f_uniq(typval_T *argvars, typval_T *rettv);
435static void f_values(typval_T *argvars, typval_T *rettv);
436static void f_virtcol(typval_T *argvars, typval_T *rettv);
437static void f_visualmode(typval_T *argvars, typval_T *rettv);
438static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
439static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
440static void f_win_getid(typval_T *argvars, typval_T *rettv);
441static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
442static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
443static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100444static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200445static void f_winbufnr(typval_T *argvars, typval_T *rettv);
446static void f_wincol(typval_T *argvars, typval_T *rettv);
447static void f_winheight(typval_T *argvars, typval_T *rettv);
448static void f_winline(typval_T *argvars, typval_T *rettv);
449static void f_winnr(typval_T *argvars, typval_T *rettv);
450static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
451static void f_winrestview(typval_T *argvars, typval_T *rettv);
452static void f_winsaveview(typval_T *argvars, typval_T *rettv);
453static void f_winwidth(typval_T *argvars, typval_T *rettv);
454static void f_writefile(typval_T *argvars, typval_T *rettv);
455static void f_wordcount(typval_T *argvars, typval_T *rettv);
456static void f_xor(typval_T *argvars, typval_T *rettv);
457
458/*
459 * Array with names and number of arguments of all internal functions
460 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
461 */
462static struct fst
463{
464 char *f_name; /* function name */
465 char f_min_argc; /* minimal number of arguments */
466 char f_max_argc; /* maximal number of arguments */
467 void (*f_func)(typval_T *args, typval_T *rvar);
468 /* implementation of function */
469} functions[] =
470{
471#ifdef FEAT_FLOAT
472 {"abs", 1, 1, f_abs},
473 {"acos", 1, 1, f_acos}, /* WJMc */
474#endif
475 {"add", 2, 2, f_add},
476 {"and", 2, 2, f_and},
477 {"append", 2, 2, f_append},
478 {"argc", 0, 0, f_argc},
479 {"argidx", 0, 0, f_argidx},
480 {"arglistid", 0, 2, f_arglistid},
481 {"argv", 0, 1, f_argv},
482#ifdef FEAT_FLOAT
483 {"asin", 1, 1, f_asin}, /* WJMc */
484#endif
485 {"assert_equal", 2, 3, f_assert_equal},
486 {"assert_exception", 1, 2, f_assert_exception},
487 {"assert_fails", 1, 2, f_assert_fails},
488 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100489 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200490 {"assert_match", 2, 3, f_assert_match},
491 {"assert_notequal", 2, 3, f_assert_notequal},
492 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100493 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200494 {"assert_true", 1, 2, f_assert_true},
495#ifdef FEAT_FLOAT
496 {"atan", 1, 1, f_atan},
497 {"atan2", 2, 2, f_atan2},
498#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100499#ifdef FEAT_BEVAL
500 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100501# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100502 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100503# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100504#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200505 {"browse", 4, 4, f_browse},
506 {"browsedir", 2, 2, f_browsedir},
507 {"bufexists", 1, 1, f_bufexists},
508 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
509 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
510 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
511 {"buflisted", 1, 1, f_buflisted},
512 {"bufloaded", 1, 1, f_bufloaded},
513 {"bufname", 1, 1, f_bufname},
514 {"bufnr", 1, 2, f_bufnr},
515 {"bufwinid", 1, 1, f_bufwinid},
516 {"bufwinnr", 1, 1, f_bufwinnr},
517 {"byte2line", 1, 1, f_byte2line},
518 {"byteidx", 2, 2, f_byteidx},
519 {"byteidxcomp", 2, 2, f_byteidxcomp},
520 {"call", 2, 3, f_call},
521#ifdef FEAT_FLOAT
522 {"ceil", 1, 1, f_ceil},
523#endif
524#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100525 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200526 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200527 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200528 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
529 {"ch_evalraw", 2, 3, f_ch_evalraw},
530 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
531 {"ch_getjob", 1, 1, f_ch_getjob},
532 {"ch_info", 1, 1, f_ch_info},
533 {"ch_log", 1, 2, f_ch_log},
534 {"ch_logfile", 1, 2, f_ch_logfile},
535 {"ch_open", 1, 2, f_ch_open},
536 {"ch_read", 1, 2, f_ch_read},
537 {"ch_readraw", 1, 2, f_ch_readraw},
538 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
539 {"ch_sendraw", 2, 3, f_ch_sendraw},
540 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200541 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200542#endif
543 {"changenr", 0, 0, f_changenr},
544 {"char2nr", 1, 2, f_char2nr},
545 {"cindent", 1, 1, f_cindent},
546 {"clearmatches", 0, 0, f_clearmatches},
547 {"col", 1, 1, f_col},
548#if defined(FEAT_INS_EXPAND)
549 {"complete", 2, 2, f_complete},
550 {"complete_add", 1, 1, f_complete_add},
551 {"complete_check", 0, 0, f_complete_check},
552#endif
553 {"confirm", 1, 4, f_confirm},
554 {"copy", 1, 1, f_copy},
555#ifdef FEAT_FLOAT
556 {"cos", 1, 1, f_cos},
557 {"cosh", 1, 1, f_cosh},
558#endif
559 {"count", 2, 4, f_count},
560 {"cscope_connection",0,3, f_cscope_connection},
561 {"cursor", 1, 3, f_cursor},
562 {"deepcopy", 1, 2, f_deepcopy},
563 {"delete", 1, 2, f_delete},
564 {"did_filetype", 0, 0, f_did_filetype},
565 {"diff_filler", 1, 1, f_diff_filler},
566 {"diff_hlID", 2, 2, f_diff_hlID},
567 {"empty", 1, 1, f_empty},
568 {"escape", 2, 2, f_escape},
569 {"eval", 1, 1, f_eval},
570 {"eventhandler", 0, 0, f_eventhandler},
571 {"executable", 1, 1, f_executable},
572 {"execute", 1, 2, f_execute},
573 {"exepath", 1, 1, f_exepath},
574 {"exists", 1, 1, f_exists},
575#ifdef FEAT_FLOAT
576 {"exp", 1, 1, f_exp},
577#endif
578 {"expand", 1, 3, f_expand},
579 {"extend", 2, 3, f_extend},
580 {"feedkeys", 1, 2, f_feedkeys},
581 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
582 {"filereadable", 1, 1, f_filereadable},
583 {"filewritable", 1, 1, f_filewritable},
584 {"filter", 2, 2, f_filter},
585 {"finddir", 1, 3, f_finddir},
586 {"findfile", 1, 3, f_findfile},
587#ifdef FEAT_FLOAT
588 {"float2nr", 1, 1, f_float2nr},
589 {"floor", 1, 1, f_floor},
590 {"fmod", 2, 2, f_fmod},
591#endif
592 {"fnameescape", 1, 1, f_fnameescape},
593 {"fnamemodify", 2, 2, f_fnamemodify},
594 {"foldclosed", 1, 1, f_foldclosed},
595 {"foldclosedend", 1, 1, f_foldclosedend},
596 {"foldlevel", 1, 1, f_foldlevel},
597 {"foldtext", 0, 0, f_foldtext},
598 {"foldtextresult", 1, 1, f_foldtextresult},
599 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200600 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200601 {"function", 1, 3, f_function},
602 {"garbagecollect", 0, 1, f_garbagecollect},
603 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200604 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200605 {"getbufline", 2, 3, f_getbufline},
606 {"getbufvar", 2, 3, f_getbufvar},
607 {"getchar", 0, 1, f_getchar},
608 {"getcharmod", 0, 0, f_getcharmod},
609 {"getcharsearch", 0, 0, f_getcharsearch},
610 {"getcmdline", 0, 0, f_getcmdline},
611 {"getcmdpos", 0, 0, f_getcmdpos},
612 {"getcmdtype", 0, 0, f_getcmdtype},
613 {"getcmdwintype", 0, 0, f_getcmdwintype},
614#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200615 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200616#endif
617 {"getcurpos", 0, 0, f_getcurpos},
618 {"getcwd", 0, 2, f_getcwd},
619 {"getfontname", 0, 1, f_getfontname},
620 {"getfperm", 1, 1, f_getfperm},
621 {"getfsize", 1, 1, f_getfsize},
622 {"getftime", 1, 1, f_getftime},
623 {"getftype", 1, 1, f_getftype},
624 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200625 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200626 {"getmatches", 0, 0, f_getmatches},
627 {"getpid", 0, 0, f_getpid},
628 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200629 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200630 {"getreg", 0, 3, f_getreg},
631 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200632 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200633 {"gettabvar", 2, 3, f_gettabvar},
634 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200635 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200636 {"getwinposx", 0, 0, f_getwinposx},
637 {"getwinposy", 0, 0, f_getwinposy},
638 {"getwinvar", 2, 3, f_getwinvar},
639 {"glob", 1, 4, f_glob},
640 {"glob2regpat", 1, 1, f_glob2regpat},
641 {"globpath", 2, 5, f_globpath},
642 {"has", 1, 1, f_has},
643 {"has_key", 2, 2, f_has_key},
644 {"haslocaldir", 0, 2, f_haslocaldir},
645 {"hasmapto", 1, 3, f_hasmapto},
646 {"highlightID", 1, 1, f_hlID}, /* obsolete */
647 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
648 {"histadd", 2, 2, f_histadd},
649 {"histdel", 1, 2, f_histdel},
650 {"histget", 1, 2, f_histget},
651 {"histnr", 1, 1, f_histnr},
652 {"hlID", 1, 1, f_hlID},
653 {"hlexists", 1, 1, f_hlexists},
654 {"hostname", 0, 0, f_hostname},
655 {"iconv", 3, 3, f_iconv},
656 {"indent", 1, 1, f_indent},
657 {"index", 2, 4, f_index},
658 {"input", 1, 3, f_input},
659 {"inputdialog", 1, 3, f_inputdialog},
660 {"inputlist", 1, 1, f_inputlist},
661 {"inputrestore", 0, 0, f_inputrestore},
662 {"inputsave", 0, 0, f_inputsave},
663 {"inputsecret", 1, 2, f_inputsecret},
664 {"insert", 2, 3, f_insert},
665 {"invert", 1, 1, f_invert},
666 {"isdirectory", 1, 1, f_isdirectory},
667 {"islocked", 1, 1, f_islocked},
668#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
669 {"isnan", 1, 1, f_isnan},
670#endif
671 {"items", 1, 1, f_items},
672#ifdef FEAT_JOB_CHANNEL
673 {"job_getchannel", 1, 1, f_job_getchannel},
674 {"job_info", 1, 1, f_job_info},
675 {"job_setoptions", 2, 2, f_job_setoptions},
676 {"job_start", 1, 2, f_job_start},
677 {"job_status", 1, 1, f_job_status},
678 {"job_stop", 1, 2, f_job_stop},
679#endif
680 {"join", 1, 2, f_join},
681 {"js_decode", 1, 1, f_js_decode},
682 {"js_encode", 1, 1, f_js_encode},
683 {"json_decode", 1, 1, f_json_decode},
684 {"json_encode", 1, 1, f_json_encode},
685 {"keys", 1, 1, f_keys},
686 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
687 {"len", 1, 1, f_len},
688 {"libcall", 3, 3, f_libcall},
689 {"libcallnr", 3, 3, f_libcallnr},
690 {"line", 1, 1, f_line},
691 {"line2byte", 1, 1, f_line2byte},
692 {"lispindent", 1, 1, f_lispindent},
693 {"localtime", 0, 0, f_localtime},
694#ifdef FEAT_FLOAT
695 {"log", 1, 1, f_log},
696 {"log10", 1, 1, f_log10},
697#endif
698#ifdef FEAT_LUA
699 {"luaeval", 1, 2, f_luaeval},
700#endif
701 {"map", 2, 2, f_map},
702 {"maparg", 1, 4, f_maparg},
703 {"mapcheck", 1, 3, f_mapcheck},
704 {"match", 2, 4, f_match},
705 {"matchadd", 2, 5, f_matchadd},
706 {"matchaddpos", 2, 5, f_matchaddpos},
707 {"matcharg", 1, 1, f_matcharg},
708 {"matchdelete", 1, 1, f_matchdelete},
709 {"matchend", 2, 4, f_matchend},
710 {"matchlist", 2, 4, f_matchlist},
711 {"matchstr", 2, 4, f_matchstr},
712 {"matchstrpos", 2, 4, f_matchstrpos},
713 {"max", 1, 1, f_max},
714 {"min", 1, 1, f_min},
715#ifdef vim_mkdir
716 {"mkdir", 1, 3, f_mkdir},
717#endif
718 {"mode", 0, 1, f_mode},
719#ifdef FEAT_MZSCHEME
720 {"mzeval", 1, 1, f_mzeval},
721#endif
722 {"nextnonblank", 1, 1, f_nextnonblank},
723 {"nr2char", 1, 2, f_nr2char},
724 {"or", 2, 2, f_or},
725 {"pathshorten", 1, 1, f_pathshorten},
726#ifdef FEAT_PERL
727 {"perleval", 1, 1, f_perleval},
728#endif
729#ifdef FEAT_FLOAT
730 {"pow", 2, 2, f_pow},
731#endif
732 {"prevnonblank", 1, 1, f_prevnonblank},
733 {"printf", 2, 19, f_printf},
734 {"pumvisible", 0, 0, f_pumvisible},
735#ifdef FEAT_PYTHON3
736 {"py3eval", 1, 1, f_py3eval},
737#endif
738#ifdef FEAT_PYTHON
739 {"pyeval", 1, 1, f_pyeval},
740#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100741#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
742 {"pyxeval", 1, 1, f_pyxeval},
743#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200744 {"range", 1, 3, f_range},
745 {"readfile", 1, 3, f_readfile},
746 {"reltime", 0, 2, f_reltime},
747#ifdef FEAT_FLOAT
748 {"reltimefloat", 1, 1, f_reltimefloat},
749#endif
750 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100751 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200752 {"remote_foreground", 1, 1, f_remote_foreground},
753 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100754 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200755 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100756 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200757 {"remove", 2, 3, f_remove},
758 {"rename", 2, 2, f_rename},
759 {"repeat", 2, 2, f_repeat},
760 {"resolve", 1, 1, f_resolve},
761 {"reverse", 1, 1, f_reverse},
762#ifdef FEAT_FLOAT
763 {"round", 1, 1, f_round},
764#endif
765 {"screenattr", 2, 2, f_screenattr},
766 {"screenchar", 2, 2, f_screenchar},
767 {"screencol", 0, 0, f_screencol},
768 {"screenrow", 0, 0, f_screenrow},
769 {"search", 1, 4, f_search},
770 {"searchdecl", 1, 3, f_searchdecl},
771 {"searchpair", 3, 7, f_searchpair},
772 {"searchpairpos", 3, 7, f_searchpairpos},
773 {"searchpos", 1, 4, f_searchpos},
774 {"server2client", 2, 2, f_server2client},
775 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200776 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200777 {"setbufvar", 3, 3, f_setbufvar},
778 {"setcharsearch", 1, 1, f_setcharsearch},
779 {"setcmdpos", 1, 1, f_setcmdpos},
780 {"setfperm", 2, 2, f_setfperm},
781 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200782 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200783 {"setmatches", 1, 1, f_setmatches},
784 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200785 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200786 {"setreg", 2, 3, f_setreg},
787 {"settabvar", 3, 3, f_settabvar},
788 {"settabwinvar", 4, 4, f_settabwinvar},
789 {"setwinvar", 3, 3, f_setwinvar},
790#ifdef FEAT_CRYPT
791 {"sha256", 1, 1, f_sha256},
792#endif
793 {"shellescape", 1, 2, f_shellescape},
794 {"shiftwidth", 0, 0, f_shiftwidth},
795 {"simplify", 1, 1, f_simplify},
796#ifdef FEAT_FLOAT
797 {"sin", 1, 1, f_sin},
798 {"sinh", 1, 1, f_sinh},
799#endif
800 {"sort", 1, 3, f_sort},
801 {"soundfold", 1, 1, f_soundfold},
802 {"spellbadword", 0, 1, f_spellbadword},
803 {"spellsuggest", 1, 3, f_spellsuggest},
804 {"split", 1, 3, f_split},
805#ifdef FEAT_FLOAT
806 {"sqrt", 1, 1, f_sqrt},
807 {"str2float", 1, 1, f_str2float},
808#endif
809 {"str2nr", 1, 2, f_str2nr},
810 {"strcharpart", 2, 3, f_strcharpart},
811 {"strchars", 1, 2, f_strchars},
812 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
813#ifdef HAVE_STRFTIME
814 {"strftime", 1, 2, f_strftime},
815#endif
816 {"strgetchar", 2, 2, f_strgetchar},
817 {"stridx", 2, 3, f_stridx},
818 {"string", 1, 1, f_string},
819 {"strlen", 1, 1, f_strlen},
820 {"strpart", 2, 3, f_strpart},
821 {"strridx", 2, 3, f_strridx},
822 {"strtrans", 1, 1, f_strtrans},
823 {"strwidth", 1, 1, f_strwidth},
824 {"submatch", 1, 2, f_submatch},
825 {"substitute", 4, 4, f_substitute},
826 {"synID", 3, 3, f_synID},
827 {"synIDattr", 2, 3, f_synIDattr},
828 {"synIDtrans", 1, 1, f_synIDtrans},
829 {"synconcealed", 2, 2, f_synconcealed},
830 {"synstack", 2, 2, f_synstack},
831 {"system", 1, 2, f_system},
832 {"systemlist", 1, 2, f_systemlist},
833 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
834 {"tabpagenr", 0, 1, f_tabpagenr},
835 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
836 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100837 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200838#ifdef FEAT_FLOAT
839 {"tan", 1, 1, f_tan},
840 {"tanh", 1, 1, f_tanh},
841#endif
842 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200843#ifdef FEAT_TERMINAL
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200844 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200845 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200846 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200847 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200848 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200849 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200850 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200851 {"term_getstatus", 1, 1, f_term_getstatus},
852 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200853 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200854 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200855 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200856 {"term_sendkeys", 2, 2, f_term_sendkeys},
857 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200858 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200859#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200860 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
861 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200862 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200863 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100864 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200865#ifdef FEAT_JOB_CHANNEL
866 {"test_null_channel", 0, 0, f_test_null_channel},
867#endif
868 {"test_null_dict", 0, 0, f_test_null_dict},
869#ifdef FEAT_JOB_CHANNEL
870 {"test_null_job", 0, 0, f_test_null_job},
871#endif
872 {"test_null_list", 0, 0, f_test_null_list},
873 {"test_null_partial", 0, 0, f_test_null_partial},
874 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100875 {"test_override", 2, 2, f_test_override},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200876 {"test_settime", 1, 1, f_test_settime},
877#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200878 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200879 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200880 {"timer_start", 2, 3, f_timer_start},
881 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200882 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200883#endif
884 {"tolower", 1, 1, f_tolower},
885 {"toupper", 1, 1, f_toupper},
886 {"tr", 3, 3, f_tr},
887#ifdef FEAT_FLOAT
888 {"trunc", 1, 1, f_trunc},
889#endif
890 {"type", 1, 1, f_type},
891 {"undofile", 1, 1, f_undofile},
892 {"undotree", 0, 0, f_undotree},
893 {"uniq", 1, 3, f_uniq},
894 {"values", 1, 1, f_values},
895 {"virtcol", 1, 1, f_virtcol},
896 {"visualmode", 0, 1, f_visualmode},
897 {"wildmenumode", 0, 0, f_wildmenumode},
898 {"win_findbuf", 1, 1, f_win_findbuf},
899 {"win_getid", 0, 2, f_win_getid},
900 {"win_gotoid", 1, 1, f_win_gotoid},
901 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
902 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100903 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200904 {"winbufnr", 1, 1, f_winbufnr},
905 {"wincol", 0, 0, f_wincol},
906 {"winheight", 1, 1, f_winheight},
907 {"winline", 0, 0, f_winline},
908 {"winnr", 0, 1, f_winnr},
909 {"winrestcmd", 0, 0, f_winrestcmd},
910 {"winrestview", 1, 1, f_winrestview},
911 {"winsaveview", 0, 0, f_winsaveview},
912 {"winwidth", 1, 1, f_winwidth},
913 {"wordcount", 0, 0, f_wordcount},
914 {"writefile", 2, 3, f_writefile},
915 {"xor", 2, 2, f_xor},
916};
917
918#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
919
920/*
921 * Function given to ExpandGeneric() to obtain the list of internal
922 * or user defined function names.
923 */
924 char_u *
925get_function_name(expand_T *xp, int idx)
926{
927 static int intidx = -1;
928 char_u *name;
929
930 if (idx == 0)
931 intidx = -1;
932 if (intidx < 0)
933 {
934 name = get_user_func_name(xp, idx);
935 if (name != NULL)
936 return name;
937 }
938 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
939 {
940 STRCPY(IObuff, functions[intidx].f_name);
941 STRCAT(IObuff, "(");
942 if (functions[intidx].f_max_argc == 0)
943 STRCAT(IObuff, ")");
944 return IObuff;
945 }
946
947 return NULL;
948}
949
950/*
951 * Function given to ExpandGeneric() to obtain the list of internal or
952 * user defined variable or function names.
953 */
954 char_u *
955get_expr_name(expand_T *xp, int idx)
956{
957 static int intidx = -1;
958 char_u *name;
959
960 if (idx == 0)
961 intidx = -1;
962 if (intidx < 0)
963 {
964 name = get_function_name(xp, idx);
965 if (name != NULL)
966 return name;
967 }
968 return get_user_var_name(xp, ++intidx);
969}
970
971#endif /* FEAT_CMDL_COMPL */
972
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200973/*
974 * Find internal function in table above.
975 * Return index, or -1 if not found
976 */
977 int
978find_internal_func(
979 char_u *name) /* name of the function */
980{
981 int first = 0;
982 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
983 int cmp;
984 int x;
985
986 /*
987 * Find the function name in the table. Binary search.
988 */
989 while (first <= last)
990 {
991 x = first + ((unsigned)(last - first) >> 1);
992 cmp = STRCMP(name, functions[x].f_name);
993 if (cmp < 0)
994 last = x - 1;
995 else if (cmp > 0)
996 first = x + 1;
997 else
998 return x;
999 }
1000 return -1;
1001}
1002
1003 int
1004call_internal_func(
1005 char_u *name,
1006 int argcount,
1007 typval_T *argvars,
1008 typval_T *rettv)
1009{
1010 int i;
1011
1012 i = find_internal_func(name);
1013 if (i < 0)
1014 return ERROR_UNKNOWN;
1015 if (argcount < functions[i].f_min_argc)
1016 return ERROR_TOOFEW;
1017 if (argcount > functions[i].f_max_argc)
1018 return ERROR_TOOMANY;
1019 argvars[argcount].v_type = VAR_UNKNOWN;
1020 functions[i].f_func(argvars, rettv);
1021 return ERROR_NONE;
1022}
1023
1024/*
1025 * Return TRUE for a non-zero Number and a non-empty String.
1026 */
1027 static int
1028non_zero_arg(typval_T *argvars)
1029{
1030 return ((argvars[0].v_type == VAR_NUMBER
1031 && argvars[0].vval.v_number != 0)
1032 || (argvars[0].v_type == VAR_SPECIAL
1033 && argvars[0].vval.v_number == VVAL_TRUE)
1034 || (argvars[0].v_type == VAR_STRING
1035 && argvars[0].vval.v_string != NULL
1036 && *argvars[0].vval.v_string != NUL));
1037}
1038
1039/*
1040 * Get the lnum from the first argument.
1041 * Also accepts ".", "$", etc., but that only works for the current buffer.
1042 * Returns -1 on error.
1043 */
1044 static linenr_T
1045get_tv_lnum(typval_T *argvars)
1046{
1047 typval_T rettv;
1048 linenr_T lnum;
1049
1050 lnum = (linenr_T)get_tv_number_chk(&argvars[0], NULL);
1051 if (lnum == 0) /* no valid number, try using line() */
1052 {
1053 rettv.v_type = VAR_NUMBER;
1054 f_line(argvars, &rettv);
1055 lnum = (linenr_T)rettv.vval.v_number;
1056 clear_tv(&rettv);
1057 }
1058 return lnum;
1059}
1060
1061#ifdef FEAT_FLOAT
1062static int get_float_arg(typval_T *argvars, float_T *f);
1063
1064/*
1065 * Get the float value of "argvars[0]" into "f".
1066 * Returns FAIL when the argument is not a Number or Float.
1067 */
1068 static int
1069get_float_arg(typval_T *argvars, float_T *f)
1070{
1071 if (argvars[0].v_type == VAR_FLOAT)
1072 {
1073 *f = argvars[0].vval.v_float;
1074 return OK;
1075 }
1076 if (argvars[0].v_type == VAR_NUMBER)
1077 {
1078 *f = (float_T)argvars[0].vval.v_number;
1079 return OK;
1080 }
1081 EMSG(_("E808: Number or Float required"));
1082 return FAIL;
1083}
1084
1085/*
1086 * "abs(expr)" function
1087 */
1088 static void
1089f_abs(typval_T *argvars, typval_T *rettv)
1090{
1091 if (argvars[0].v_type == VAR_FLOAT)
1092 {
1093 rettv->v_type = VAR_FLOAT;
1094 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1095 }
1096 else
1097 {
1098 varnumber_T n;
1099 int error = FALSE;
1100
1101 n = get_tv_number_chk(&argvars[0], &error);
1102 if (error)
1103 rettv->vval.v_number = -1;
1104 else if (n > 0)
1105 rettv->vval.v_number = n;
1106 else
1107 rettv->vval.v_number = -n;
1108 }
1109}
1110
1111/*
1112 * "acos()" function
1113 */
1114 static void
1115f_acos(typval_T *argvars, typval_T *rettv)
1116{
1117 float_T f = 0.0;
1118
1119 rettv->v_type = VAR_FLOAT;
1120 if (get_float_arg(argvars, &f) == OK)
1121 rettv->vval.v_float = acos(f);
1122 else
1123 rettv->vval.v_float = 0.0;
1124}
1125#endif
1126
1127/*
1128 * "add(list, item)" function
1129 */
1130 static void
1131f_add(typval_T *argvars, typval_T *rettv)
1132{
1133 list_T *l;
1134
1135 rettv->vval.v_number = 1; /* Default: Failed */
1136 if (argvars[0].v_type == VAR_LIST)
1137 {
1138 if ((l = argvars[0].vval.v_list) != NULL
1139 && !tv_check_lock(l->lv_lock,
1140 (char_u *)N_("add() argument"), TRUE)
1141 && list_append_tv(l, &argvars[1]) == OK)
1142 copy_tv(&argvars[0], rettv);
1143 }
1144 else
1145 EMSG(_(e_listreq));
1146}
1147
1148/*
1149 * "and(expr, expr)" function
1150 */
1151 static void
1152f_and(typval_T *argvars, typval_T *rettv)
1153{
1154 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
1155 & get_tv_number_chk(&argvars[1], NULL);
1156}
1157
1158/*
1159 * "append(lnum, string/list)" function
1160 */
1161 static void
1162f_append(typval_T *argvars, typval_T *rettv)
1163{
1164 long lnum;
1165 char_u *line;
1166 list_T *l = NULL;
1167 listitem_T *li = NULL;
1168 typval_T *tv;
1169 long added = 0;
1170
1171 /* When coming here from Insert mode, sync undo, so that this can be
1172 * undone separately from what was previously inserted. */
1173 if (u_sync_once == 2)
1174 {
1175 u_sync_once = 1; /* notify that u_sync() was called */
1176 u_sync(TRUE);
1177 }
1178
1179 lnum = get_tv_lnum(argvars);
1180 if (lnum >= 0
1181 && lnum <= curbuf->b_ml.ml_line_count
1182 && u_save(lnum, lnum + 1) == OK)
1183 {
1184 if (argvars[1].v_type == VAR_LIST)
1185 {
1186 l = argvars[1].vval.v_list;
1187 if (l == NULL)
1188 return;
1189 li = l->lv_first;
1190 }
1191 for (;;)
1192 {
1193 if (l == NULL)
1194 tv = &argvars[1]; /* append a string */
1195 else if (li == NULL)
1196 break; /* end of list */
1197 else
1198 tv = &li->li_tv; /* append item from list */
1199 line = get_tv_string_chk(tv);
1200 if (line == NULL) /* type error */
1201 {
1202 rettv->vval.v_number = 1; /* Failed */
1203 break;
1204 }
1205 ml_append(lnum + added, line, (colnr_T)0, FALSE);
1206 ++added;
1207 if (l == NULL)
1208 break;
1209 li = li->li_next;
1210 }
1211
1212 appended_lines_mark(lnum, added);
1213 if (curwin->w_cursor.lnum > lnum)
1214 curwin->w_cursor.lnum += added;
1215 }
1216 else
1217 rettv->vval.v_number = 1; /* Failed */
1218}
1219
1220/*
1221 * "argc()" function
1222 */
1223 static void
1224f_argc(typval_T *argvars UNUSED, typval_T *rettv)
1225{
1226 rettv->vval.v_number = ARGCOUNT;
1227}
1228
1229/*
1230 * "argidx()" function
1231 */
1232 static void
1233f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1234{
1235 rettv->vval.v_number = curwin->w_arg_idx;
1236}
1237
1238/*
1239 * "arglistid()" function
1240 */
1241 static void
1242f_arglistid(typval_T *argvars, typval_T *rettv)
1243{
1244 win_T *wp;
1245
1246 rettv->vval.v_number = -1;
1247 wp = find_tabwin(&argvars[0], &argvars[1]);
1248 if (wp != NULL)
1249 rettv->vval.v_number = wp->w_alist->id;
1250}
1251
1252/*
1253 * "argv(nr)" function
1254 */
1255 static void
1256f_argv(typval_T *argvars, typval_T *rettv)
1257{
1258 int idx;
1259
1260 if (argvars[0].v_type != VAR_UNKNOWN)
1261 {
1262 idx = (int)get_tv_number_chk(&argvars[0], NULL);
1263 if (idx >= 0 && idx < ARGCOUNT)
1264 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
1265 else
1266 rettv->vval.v_string = NULL;
1267 rettv->v_type = VAR_STRING;
1268 }
1269 else if (rettv_list_alloc(rettv) == OK)
1270 for (idx = 0; idx < ARGCOUNT; ++idx)
1271 list_append_string(rettv->vval.v_list,
1272 alist_name(&ARGLIST[idx]), -1);
1273}
1274
1275/*
1276 * "assert_equal(expected, actual[, msg])" function
1277 */
1278 static void
1279f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
1280{
1281 assert_equal_common(argvars, ASSERT_EQUAL);
1282}
1283
1284/*
1285 * "assert_notequal(expected, actual[, msg])" function
1286 */
1287 static void
1288f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED)
1289{
1290 assert_equal_common(argvars, ASSERT_NOTEQUAL);
1291}
1292
1293/*
1294 * "assert_exception(string[, msg])" function
1295 */
1296 static void
1297f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
1298{
1299 assert_exception(argvars);
1300}
1301
1302/*
1303 * "assert_fails(cmd [, error])" function
1304 */
1305 static void
1306f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
1307{
1308 assert_fails(argvars);
1309}
1310
1311/*
1312 * "assert_false(actual[, msg])" function
1313 */
1314 static void
1315f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
1316{
1317 assert_bool(argvars, FALSE);
1318}
1319
1320/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001321 * "assert_inrange(lower, upper[, msg])" function
1322 */
1323 static void
1324f_assert_inrange(typval_T *argvars, typval_T *rettv UNUSED)
1325{
1326 assert_inrange(argvars);
1327}
1328
1329/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001330 * "assert_match(pattern, actual[, msg])" function
1331 */
1332 static void
1333f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
1334{
1335 assert_match_common(argvars, ASSERT_MATCH);
1336}
1337
1338/*
1339 * "assert_notmatch(pattern, actual[, msg])" function
1340 */
1341 static void
1342f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED)
1343{
1344 assert_match_common(argvars, ASSERT_NOTMATCH);
1345}
1346
1347/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001348 * "assert_report(msg)" function
1349 */
1350 static void
1351f_assert_report(typval_T *argvars, typval_T *rettv UNUSED)
1352{
1353 assert_report(argvars);
1354}
1355
1356/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001357 * "assert_true(actual[, msg])" function
1358 */
1359 static void
1360f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
1361{
1362 assert_bool(argvars, TRUE);
1363}
1364
1365#ifdef FEAT_FLOAT
1366/*
1367 * "asin()" function
1368 */
1369 static void
1370f_asin(typval_T *argvars, typval_T *rettv)
1371{
1372 float_T f = 0.0;
1373
1374 rettv->v_type = VAR_FLOAT;
1375 if (get_float_arg(argvars, &f) == OK)
1376 rettv->vval.v_float = asin(f);
1377 else
1378 rettv->vval.v_float = 0.0;
1379}
1380
1381/*
1382 * "atan()" function
1383 */
1384 static void
1385f_atan(typval_T *argvars, typval_T *rettv)
1386{
1387 float_T f = 0.0;
1388
1389 rettv->v_type = VAR_FLOAT;
1390 if (get_float_arg(argvars, &f) == OK)
1391 rettv->vval.v_float = atan(f);
1392 else
1393 rettv->vval.v_float = 0.0;
1394}
1395
1396/*
1397 * "atan2()" function
1398 */
1399 static void
1400f_atan2(typval_T *argvars, typval_T *rettv)
1401{
1402 float_T fx = 0.0, fy = 0.0;
1403
1404 rettv->v_type = VAR_FLOAT;
1405 if (get_float_arg(argvars, &fx) == OK
1406 && get_float_arg(&argvars[1], &fy) == OK)
1407 rettv->vval.v_float = atan2(fx, fy);
1408 else
1409 rettv->vval.v_float = 0.0;
1410}
1411#endif
1412
1413/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001414 * "balloon_show()" function
1415 */
1416#ifdef FEAT_BEVAL
1417 static void
1418f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1419{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001420 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001421 {
1422 if (argvars[0].v_type == VAR_LIST
1423# ifdef FEAT_GUI
1424 && !gui.in_use
1425# endif
1426 )
1427 post_balloon(balloonEval, NULL, argvars[0].vval.v_list);
1428 else
1429 post_balloon(balloonEval, get_tv_string_chk(&argvars[0]), NULL);
1430 }
1431}
1432
Bram Moolenaar669a8282017-11-19 20:13:05 +01001433# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001434 static void
1435f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1436{
1437 if (rettv_list_alloc(rettv) == OK)
1438 {
1439 char_u *msg = get_tv_string_chk(&argvars[0]);
1440
1441 if (msg != NULL)
1442 {
1443 pumitem_T *array;
1444 int size = split_message(msg, &array);
1445 int i;
1446
1447 /* Skip the first and last item, they are always empty. */
1448 for (i = 1; i < size - 1; ++i)
1449 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
1450 vim_free(array);
1451 }
1452 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001453}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001454# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001455#endif
1456
1457/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001458 * "browse(save, title, initdir, default)" function
1459 */
1460 static void
1461f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1462{
1463#ifdef FEAT_BROWSE
1464 int save;
1465 char_u *title;
1466 char_u *initdir;
1467 char_u *defname;
1468 char_u buf[NUMBUFLEN];
1469 char_u buf2[NUMBUFLEN];
1470 int error = FALSE;
1471
1472 save = (int)get_tv_number_chk(&argvars[0], &error);
1473 title = get_tv_string_chk(&argvars[1]);
1474 initdir = get_tv_string_buf_chk(&argvars[2], buf);
1475 defname = get_tv_string_buf_chk(&argvars[3], buf2);
1476
1477 if (error || title == NULL || initdir == NULL || defname == NULL)
1478 rettv->vval.v_string = NULL;
1479 else
1480 rettv->vval.v_string =
1481 do_browse(save ? BROWSE_SAVE : 0,
1482 title, defname, NULL, initdir, NULL, curbuf);
1483#else
1484 rettv->vval.v_string = NULL;
1485#endif
1486 rettv->v_type = VAR_STRING;
1487}
1488
1489/*
1490 * "browsedir(title, initdir)" function
1491 */
1492 static void
1493f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1494{
1495#ifdef FEAT_BROWSE
1496 char_u *title;
1497 char_u *initdir;
1498 char_u buf[NUMBUFLEN];
1499
1500 title = get_tv_string_chk(&argvars[0]);
1501 initdir = get_tv_string_buf_chk(&argvars[1], buf);
1502
1503 if (title == NULL || initdir == NULL)
1504 rettv->vval.v_string = NULL;
1505 else
1506 rettv->vval.v_string = do_browse(BROWSE_DIR,
1507 title, NULL, NULL, initdir, NULL, curbuf);
1508#else
1509 rettv->vval.v_string = NULL;
1510#endif
1511 rettv->v_type = VAR_STRING;
1512}
1513
1514static buf_T *find_buffer(typval_T *avar);
1515
1516/*
1517 * Find a buffer by number or exact name.
1518 */
1519 static buf_T *
1520find_buffer(typval_T *avar)
1521{
1522 buf_T *buf = NULL;
1523
1524 if (avar->v_type == VAR_NUMBER)
1525 buf = buflist_findnr((int)avar->vval.v_number);
1526 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1527 {
1528 buf = buflist_findname_exp(avar->vval.v_string);
1529 if (buf == NULL)
1530 {
1531 /* No full path name match, try a match with a URL or a "nofile"
1532 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001533 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001534 if (buf->b_fname != NULL
1535 && (path_with_url(buf->b_fname)
1536#ifdef FEAT_QUICKFIX
1537 || bt_nofile(buf)
1538#endif
1539 )
1540 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1541 break;
1542 }
1543 }
1544 return buf;
1545}
1546
1547/*
1548 * "bufexists(expr)" function
1549 */
1550 static void
1551f_bufexists(typval_T *argvars, typval_T *rettv)
1552{
1553 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1554}
1555
1556/*
1557 * "buflisted(expr)" function
1558 */
1559 static void
1560f_buflisted(typval_T *argvars, typval_T *rettv)
1561{
1562 buf_T *buf;
1563
1564 buf = find_buffer(&argvars[0]);
1565 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1566}
1567
1568/*
1569 * "bufloaded(expr)" function
1570 */
1571 static void
1572f_bufloaded(typval_T *argvars, typval_T *rettv)
1573{
1574 buf_T *buf;
1575
1576 buf = find_buffer(&argvars[0]);
1577 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1578}
1579
1580 buf_T *
1581buflist_find_by_name(char_u *name, int curtab_only)
1582{
1583 int save_magic;
1584 char_u *save_cpo;
1585 buf_T *buf;
1586
1587 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1588 save_magic = p_magic;
1589 p_magic = TRUE;
1590 save_cpo = p_cpo;
1591 p_cpo = (char_u *)"";
1592
1593 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1594 TRUE, FALSE, curtab_only));
1595
1596 p_magic = save_magic;
1597 p_cpo = save_cpo;
1598 return buf;
1599}
1600
1601/*
1602 * Get buffer by number or pattern.
1603 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001604 buf_T *
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001605get_buf_tv(typval_T *tv, int curtab_only)
1606{
1607 char_u *name = tv->vval.v_string;
1608 buf_T *buf;
1609
1610 if (tv->v_type == VAR_NUMBER)
1611 return buflist_findnr((int)tv->vval.v_number);
1612 if (tv->v_type != VAR_STRING)
1613 return NULL;
1614 if (name == NULL || *name == NUL)
1615 return curbuf;
1616 if (name[0] == '$' && name[1] == NUL)
1617 return lastbuf;
1618
1619 buf = buflist_find_by_name(name, curtab_only);
1620
1621 /* If not found, try expanding the name, like done for bufexists(). */
1622 if (buf == NULL)
1623 buf = find_buffer(tv);
1624
1625 return buf;
1626}
1627
1628/*
1629 * "bufname(expr)" function
1630 */
1631 static void
1632f_bufname(typval_T *argvars, typval_T *rettv)
1633{
1634 buf_T *buf;
1635
1636 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1637 ++emsg_off;
1638 buf = get_buf_tv(&argvars[0], FALSE);
1639 rettv->v_type = VAR_STRING;
1640 if (buf != NULL && buf->b_fname != NULL)
1641 rettv->vval.v_string = vim_strsave(buf->b_fname);
1642 else
1643 rettv->vval.v_string = NULL;
1644 --emsg_off;
1645}
1646
1647/*
1648 * "bufnr(expr)" function
1649 */
1650 static void
1651f_bufnr(typval_T *argvars, typval_T *rettv)
1652{
1653 buf_T *buf;
1654 int error = FALSE;
1655 char_u *name;
1656
1657 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1658 ++emsg_off;
1659 buf = get_buf_tv(&argvars[0], FALSE);
1660 --emsg_off;
1661
1662 /* If the buffer isn't found and the second argument is not zero create a
1663 * new buffer. */
1664 if (buf == NULL
1665 && argvars[1].v_type != VAR_UNKNOWN
1666 && get_tv_number_chk(&argvars[1], &error) != 0
1667 && !error
1668 && (name = get_tv_string_chk(&argvars[0])) != NULL
1669 && !error)
1670 buf = buflist_new(name, NULL, (linenr_T)1, 0);
1671
1672 if (buf != NULL)
1673 rettv->vval.v_number = buf->b_fnum;
1674 else
1675 rettv->vval.v_number = -1;
1676}
1677
1678 static void
1679buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
1680{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001681 win_T *wp;
1682 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001683 buf_T *buf;
1684
1685 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1686 ++emsg_off;
1687 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02001688 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001689 {
1690 ++winnr;
1691 if (wp->w_buffer == buf)
1692 break;
1693 }
1694 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001695 --emsg_off;
1696}
1697
1698/*
1699 * "bufwinid(nr)" function
1700 */
1701 static void
1702f_bufwinid(typval_T *argvars, typval_T *rettv)
1703{
1704 buf_win_common(argvars, rettv, FALSE);
1705}
1706
1707/*
1708 * "bufwinnr(nr)" function
1709 */
1710 static void
1711f_bufwinnr(typval_T *argvars, typval_T *rettv)
1712{
1713 buf_win_common(argvars, rettv, TRUE);
1714}
1715
1716/*
1717 * "byte2line(byte)" function
1718 */
1719 static void
1720f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1721{
1722#ifndef FEAT_BYTEOFF
1723 rettv->vval.v_number = -1;
1724#else
1725 long boff = 0;
1726
1727 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
1728 if (boff < 0)
1729 rettv->vval.v_number = -1;
1730 else
1731 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
1732 (linenr_T)0, &boff);
1733#endif
1734}
1735
1736 static void
1737byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
1738{
1739#ifdef FEAT_MBYTE
1740 char_u *t;
1741#endif
1742 char_u *str;
1743 varnumber_T idx;
1744
1745 str = get_tv_string_chk(&argvars[0]);
1746 idx = get_tv_number_chk(&argvars[1], NULL);
1747 rettv->vval.v_number = -1;
1748 if (str == NULL || idx < 0)
1749 return;
1750
1751#ifdef FEAT_MBYTE
1752 t = str;
1753 for ( ; idx > 0; idx--)
1754 {
1755 if (*t == NUL) /* EOL reached */
1756 return;
1757 if (enc_utf8 && comp)
1758 t += utf_ptr2len(t);
1759 else
1760 t += (*mb_ptr2len)(t);
1761 }
1762 rettv->vval.v_number = (varnumber_T)(t - str);
1763#else
1764 if ((size_t)idx <= STRLEN(str))
1765 rettv->vval.v_number = idx;
1766#endif
1767}
1768
1769/*
1770 * "byteidx()" function
1771 */
1772 static void
1773f_byteidx(typval_T *argvars, typval_T *rettv)
1774{
1775 byteidx(argvars, rettv, FALSE);
1776}
1777
1778/*
1779 * "byteidxcomp()" function
1780 */
1781 static void
1782f_byteidxcomp(typval_T *argvars, typval_T *rettv)
1783{
1784 byteidx(argvars, rettv, TRUE);
1785}
1786
1787/*
1788 * "call(func, arglist [, dict])" function
1789 */
1790 static void
1791f_call(typval_T *argvars, typval_T *rettv)
1792{
1793 char_u *func;
1794 partial_T *partial = NULL;
1795 dict_T *selfdict = NULL;
1796
1797 if (argvars[1].v_type != VAR_LIST)
1798 {
1799 EMSG(_(e_listreq));
1800 return;
1801 }
1802 if (argvars[1].vval.v_list == NULL)
1803 return;
1804
1805 if (argvars[0].v_type == VAR_FUNC)
1806 func = argvars[0].vval.v_string;
1807 else if (argvars[0].v_type == VAR_PARTIAL)
1808 {
1809 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001810 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001811 }
1812 else
1813 func = get_tv_string(&argvars[0]);
1814 if (*func == NUL)
1815 return; /* type error or empty name */
1816
1817 if (argvars[2].v_type != VAR_UNKNOWN)
1818 {
1819 if (argvars[2].v_type != VAR_DICT)
1820 {
1821 EMSG(_(e_dictreq));
1822 return;
1823 }
1824 selfdict = argvars[2].vval.v_dict;
1825 }
1826
1827 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
1828}
1829
1830#ifdef FEAT_FLOAT
1831/*
1832 * "ceil({float})" function
1833 */
1834 static void
1835f_ceil(typval_T *argvars, typval_T *rettv)
1836{
1837 float_T f = 0.0;
1838
1839 rettv->v_type = VAR_FLOAT;
1840 if (get_float_arg(argvars, &f) == OK)
1841 rettv->vval.v_float = ceil(f);
1842 else
1843 rettv->vval.v_float = 0.0;
1844}
1845#endif
1846
1847#ifdef FEAT_JOB_CHANNEL
1848/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01001849 * "ch_canread()" function
1850 */
1851 static void
1852f_ch_canread(typval_T *argvars, typval_T *rettv)
1853{
Bram Moolenaar958dc692016-12-01 15:34:12 +01001854 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01001855
1856 rettv->vval.v_number = 0;
1857 if (channel != NULL)
1858 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
1859 || channel_has_readahead(channel, PART_OUT)
1860 || channel_has_readahead(channel, PART_ERR);
1861}
1862
1863/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001864 * "ch_close()" function
1865 */
1866 static void
1867f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
1868{
1869 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
1870
1871 if (channel != NULL)
1872 {
1873 channel_close(channel, FALSE);
1874 channel_clear(channel);
1875 }
1876}
1877
1878/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02001879 * "ch_close()" function
1880 */
1881 static void
1882f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
1883{
1884 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
1885
1886 if (channel != NULL)
1887 channel_close_in(channel);
1888}
1889
1890/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001891 * "ch_getbufnr()" function
1892 */
1893 static void
1894f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
1895{
1896 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1897
1898 rettv->vval.v_number = -1;
1899 if (channel != NULL)
1900 {
1901 char_u *what = get_tv_string(&argvars[1]);
1902 int part;
1903
1904 if (STRCMP(what, "err") == 0)
1905 part = PART_ERR;
1906 else if (STRCMP(what, "out") == 0)
1907 part = PART_OUT;
1908 else if (STRCMP(what, "in") == 0)
1909 part = PART_IN;
1910 else
1911 part = PART_SOCK;
1912 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
1913 rettv->vval.v_number =
1914 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
1915 }
1916}
1917
1918/*
1919 * "ch_getjob()" function
1920 */
1921 static void
1922f_ch_getjob(typval_T *argvars, typval_T *rettv)
1923{
1924 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1925
1926 if (channel != NULL)
1927 {
1928 rettv->v_type = VAR_JOB;
1929 rettv->vval.v_job = channel->ch_job;
1930 if (channel->ch_job != NULL)
1931 ++channel->ch_job->jv_refcount;
1932 }
1933}
1934
1935/*
1936 * "ch_info()" function
1937 */
1938 static void
1939f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
1940{
1941 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1942
1943 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
1944 channel_info(channel, rettv->vval.v_dict);
1945}
1946
1947/*
1948 * "ch_log()" function
1949 */
1950 static void
1951f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
1952{
1953 char_u *msg = get_tv_string(&argvars[0]);
1954 channel_T *channel = NULL;
1955
1956 if (argvars[1].v_type != VAR_UNKNOWN)
1957 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
1958
1959 ch_log(channel, (char *)msg);
1960}
1961
1962/*
1963 * "ch_logfile()" function
1964 */
1965 static void
1966f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
1967{
1968 char_u *fname;
1969 char_u *opt = (char_u *)"";
1970 char_u buf[NUMBUFLEN];
1971
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02001972 /* Don't open a file in restricted mode. */
1973 if (check_restricted() || check_secure())
1974 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001975 fname = get_tv_string(&argvars[0]);
1976 if (argvars[1].v_type == VAR_STRING)
1977 opt = get_tv_string_buf(&argvars[1], buf);
1978 ch_logfile(fname, opt);
1979}
1980
1981/*
1982 * "ch_open()" function
1983 */
1984 static void
1985f_ch_open(typval_T *argvars, typval_T *rettv)
1986{
1987 rettv->v_type = VAR_CHANNEL;
1988 if (check_restricted() || check_secure())
1989 return;
1990 rettv->vval.v_channel = channel_open_func(argvars);
1991}
1992
1993/*
1994 * "ch_read()" function
1995 */
1996 static void
1997f_ch_read(typval_T *argvars, typval_T *rettv)
1998{
1999 common_channel_read(argvars, rettv, FALSE);
2000}
2001
2002/*
2003 * "ch_readraw()" function
2004 */
2005 static void
2006f_ch_readraw(typval_T *argvars, typval_T *rettv)
2007{
2008 common_channel_read(argvars, rettv, TRUE);
2009}
2010
2011/*
2012 * "ch_evalexpr()" function
2013 */
2014 static void
2015f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2016{
2017 ch_expr_common(argvars, rettv, TRUE);
2018}
2019
2020/*
2021 * "ch_sendexpr()" function
2022 */
2023 static void
2024f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2025{
2026 ch_expr_common(argvars, rettv, FALSE);
2027}
2028
2029/*
2030 * "ch_evalraw()" function
2031 */
2032 static void
2033f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2034{
2035 ch_raw_common(argvars, rettv, TRUE);
2036}
2037
2038/*
2039 * "ch_sendraw()" function
2040 */
2041 static void
2042f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2043{
2044 ch_raw_common(argvars, rettv, FALSE);
2045}
2046
2047/*
2048 * "ch_setoptions()" function
2049 */
2050 static void
2051f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2052{
2053 channel_T *channel;
2054 jobopt_T opt;
2055
2056 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2057 if (channel == NULL)
2058 return;
2059 clear_job_options(&opt);
2060 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002061 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002062 channel_set_options(channel, &opt);
2063 free_job_options(&opt);
2064}
2065
2066/*
2067 * "ch_status()" function
2068 */
2069 static void
2070f_ch_status(typval_T *argvars, typval_T *rettv)
2071{
2072 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002073 jobopt_T opt;
2074 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002075
2076 /* return an empty string by default */
2077 rettv->v_type = VAR_STRING;
2078 rettv->vval.v_string = NULL;
2079
2080 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002081
2082 if (argvars[1].v_type != VAR_UNKNOWN)
2083 {
2084 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002085 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002086 && (opt.jo_set & JO_PART))
2087 part = opt.jo_part;
2088 }
2089
2090 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002091}
2092#endif
2093
2094/*
2095 * "changenr()" function
2096 */
2097 static void
2098f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2099{
2100 rettv->vval.v_number = curbuf->b_u_seq_cur;
2101}
2102
2103/*
2104 * "char2nr(string)" function
2105 */
2106 static void
2107f_char2nr(typval_T *argvars, typval_T *rettv)
2108{
2109#ifdef FEAT_MBYTE
2110 if (has_mbyte)
2111 {
2112 int utf8 = 0;
2113
2114 if (argvars[1].v_type != VAR_UNKNOWN)
2115 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
2116
2117 if (utf8)
2118 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
2119 else
2120 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
2121 }
2122 else
2123#endif
2124 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
2125}
2126
2127/*
2128 * "cindent(lnum)" function
2129 */
2130 static void
2131f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2132{
2133#ifdef FEAT_CINDENT
2134 pos_T pos;
2135 linenr_T lnum;
2136
2137 pos = curwin->w_cursor;
2138 lnum = get_tv_lnum(argvars);
2139 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2140 {
2141 curwin->w_cursor.lnum = lnum;
2142 rettv->vval.v_number = get_c_indent();
2143 curwin->w_cursor = pos;
2144 }
2145 else
2146#endif
2147 rettv->vval.v_number = -1;
2148}
2149
2150/*
2151 * "clearmatches()" function
2152 */
2153 static void
2154f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2155{
2156#ifdef FEAT_SEARCH_EXTRA
2157 clear_matches(curwin);
2158#endif
2159}
2160
2161/*
2162 * "col(string)" function
2163 */
2164 static void
2165f_col(typval_T *argvars, typval_T *rettv)
2166{
2167 colnr_T col = 0;
2168 pos_T *fp;
2169 int fnum = curbuf->b_fnum;
2170
2171 fp = var2fpos(&argvars[0], FALSE, &fnum);
2172 if (fp != NULL && fnum == curbuf->b_fnum)
2173 {
2174 if (fp->col == MAXCOL)
2175 {
2176 /* '> can be MAXCOL, get the length of the line then */
2177 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2178 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2179 else
2180 col = MAXCOL;
2181 }
2182 else
2183 {
2184 col = fp->col + 1;
2185#ifdef FEAT_VIRTUALEDIT
2186 /* col(".") when the cursor is on the NUL at the end of the line
2187 * because of "coladd" can be seen as an extra column. */
2188 if (virtual_active() && fp == &curwin->w_cursor)
2189 {
2190 char_u *p = ml_get_cursor();
2191
2192 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2193 curwin->w_virtcol - curwin->w_cursor.coladd))
2194 {
2195# ifdef FEAT_MBYTE
2196 int l;
2197
2198 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2199 col += l;
2200# else
2201 if (*p != NUL && p[1] == NUL)
2202 ++col;
2203# endif
2204 }
2205 }
2206#endif
2207 }
2208 }
2209 rettv->vval.v_number = col;
2210}
2211
2212#if defined(FEAT_INS_EXPAND)
2213/*
2214 * "complete()" function
2215 */
2216 static void
2217f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2218{
2219 int startcol;
2220
2221 if ((State & INSERT) == 0)
2222 {
2223 EMSG(_("E785: complete() can only be used in Insert mode"));
2224 return;
2225 }
2226
2227 /* Check for undo allowed here, because if something was already inserted
2228 * the line was already saved for undo and this check isn't done. */
2229 if (!undo_allowed())
2230 return;
2231
2232 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2233 {
2234 EMSG(_(e_invarg));
2235 return;
2236 }
2237
2238 startcol = (int)get_tv_number_chk(&argvars[0], NULL);
2239 if (startcol <= 0)
2240 return;
2241
2242 set_completion(startcol - 1, argvars[1].vval.v_list);
2243}
2244
2245/*
2246 * "complete_add()" function
2247 */
2248 static void
2249f_complete_add(typval_T *argvars, typval_T *rettv)
2250{
2251 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2252}
2253
2254/*
2255 * "complete_check()" function
2256 */
2257 static void
2258f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2259{
2260 int saved = RedrawingDisabled;
2261
2262 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002263 ins_compl_check_keys(0, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002264 rettv->vval.v_number = compl_interrupted;
2265 RedrawingDisabled = saved;
2266}
2267#endif
2268
2269/*
2270 * "confirm(message, buttons[, default [, type]])" function
2271 */
2272 static void
2273f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2274{
2275#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2276 char_u *message;
2277 char_u *buttons = NULL;
2278 char_u buf[NUMBUFLEN];
2279 char_u buf2[NUMBUFLEN];
2280 int def = 1;
2281 int type = VIM_GENERIC;
2282 char_u *typestr;
2283 int error = FALSE;
2284
2285 message = get_tv_string_chk(&argvars[0]);
2286 if (message == NULL)
2287 error = TRUE;
2288 if (argvars[1].v_type != VAR_UNKNOWN)
2289 {
2290 buttons = get_tv_string_buf_chk(&argvars[1], buf);
2291 if (buttons == NULL)
2292 error = TRUE;
2293 if (argvars[2].v_type != VAR_UNKNOWN)
2294 {
2295 def = (int)get_tv_number_chk(&argvars[2], &error);
2296 if (argvars[3].v_type != VAR_UNKNOWN)
2297 {
2298 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
2299 if (typestr == NULL)
2300 error = TRUE;
2301 else
2302 {
2303 switch (TOUPPER_ASC(*typestr))
2304 {
2305 case 'E': type = VIM_ERROR; break;
2306 case 'Q': type = VIM_QUESTION; break;
2307 case 'I': type = VIM_INFO; break;
2308 case 'W': type = VIM_WARNING; break;
2309 case 'G': type = VIM_GENERIC; break;
2310 }
2311 }
2312 }
2313 }
2314 }
2315
2316 if (buttons == NULL || *buttons == NUL)
2317 buttons = (char_u *)_("&Ok");
2318
2319 if (!error)
2320 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2321 def, NULL, FALSE);
2322#endif
2323}
2324
2325/*
2326 * "copy()" function
2327 */
2328 static void
2329f_copy(typval_T *argvars, typval_T *rettv)
2330{
2331 item_copy(&argvars[0], rettv, FALSE, 0);
2332}
2333
2334#ifdef FEAT_FLOAT
2335/*
2336 * "cos()" function
2337 */
2338 static void
2339f_cos(typval_T *argvars, typval_T *rettv)
2340{
2341 float_T f = 0.0;
2342
2343 rettv->v_type = VAR_FLOAT;
2344 if (get_float_arg(argvars, &f) == OK)
2345 rettv->vval.v_float = cos(f);
2346 else
2347 rettv->vval.v_float = 0.0;
2348}
2349
2350/*
2351 * "cosh()" function
2352 */
2353 static void
2354f_cosh(typval_T *argvars, typval_T *rettv)
2355{
2356 float_T f = 0.0;
2357
2358 rettv->v_type = VAR_FLOAT;
2359 if (get_float_arg(argvars, &f) == OK)
2360 rettv->vval.v_float = cosh(f);
2361 else
2362 rettv->vval.v_float = 0.0;
2363}
2364#endif
2365
2366/*
2367 * "count()" function
2368 */
2369 static void
2370f_count(typval_T *argvars, typval_T *rettv)
2371{
2372 long n = 0;
2373 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002374 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002375
Bram Moolenaar9966b212017-07-28 16:46:57 +02002376 if (argvars[2].v_type != VAR_UNKNOWN)
2377 ic = (int)get_tv_number_chk(&argvars[2], &error);
2378
2379 if (argvars[0].v_type == VAR_STRING)
2380 {
2381 char_u *expr = get_tv_string_chk(&argvars[1]);
2382 char_u *p = argvars[0].vval.v_string;
2383 char_u *next;
2384
2385 if (!error && expr != NULL && p != NULL)
2386 {
2387 if (ic)
2388 {
2389 size_t len = STRLEN(expr);
2390
2391 while (*p != NUL)
2392 {
2393 if (MB_STRNICMP(p, expr, len) == 0)
2394 {
2395 ++n;
2396 p += len;
2397 }
2398 else
2399 MB_PTR_ADV(p);
2400 }
2401 }
2402 else
2403 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2404 != NULL)
2405 {
2406 ++n;
2407 p = next + STRLEN(expr);
2408 }
2409 }
2410
2411 }
2412 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002413 {
2414 listitem_T *li;
2415 list_T *l;
2416 long idx;
2417
2418 if ((l = argvars[0].vval.v_list) != NULL)
2419 {
2420 li = l->lv_first;
2421 if (argvars[2].v_type != VAR_UNKNOWN)
2422 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002423 if (argvars[3].v_type != VAR_UNKNOWN)
2424 {
2425 idx = (long)get_tv_number_chk(&argvars[3], &error);
2426 if (!error)
2427 {
2428 li = list_find(l, idx);
2429 if (li == NULL)
2430 EMSGN(_(e_listidx), idx);
2431 }
2432 }
2433 if (error)
2434 li = NULL;
2435 }
2436
2437 for ( ; li != NULL; li = li->li_next)
2438 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2439 ++n;
2440 }
2441 }
2442 else if (argvars[0].v_type == VAR_DICT)
2443 {
2444 int todo;
2445 dict_T *d;
2446 hashitem_T *hi;
2447
2448 if ((d = argvars[0].vval.v_dict) != NULL)
2449 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002450 if (argvars[2].v_type != VAR_UNKNOWN)
2451 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002452 if (argvars[3].v_type != VAR_UNKNOWN)
2453 EMSG(_(e_invarg));
2454 }
2455
2456 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2457 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2458 {
2459 if (!HASHITEM_EMPTY(hi))
2460 {
2461 --todo;
2462 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2463 ++n;
2464 }
2465 }
2466 }
2467 }
2468 else
2469 EMSG2(_(e_listdictarg), "count()");
2470 rettv->vval.v_number = n;
2471}
2472
2473/*
2474 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2475 *
2476 * Checks the existence of a cscope connection.
2477 */
2478 static void
2479f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2480{
2481#ifdef FEAT_CSCOPE
2482 int num = 0;
2483 char_u *dbpath = NULL;
2484 char_u *prepend = NULL;
2485 char_u buf[NUMBUFLEN];
2486
2487 if (argvars[0].v_type != VAR_UNKNOWN
2488 && argvars[1].v_type != VAR_UNKNOWN)
2489 {
2490 num = (int)get_tv_number(&argvars[0]);
2491 dbpath = get_tv_string(&argvars[1]);
2492 if (argvars[2].v_type != VAR_UNKNOWN)
2493 prepend = get_tv_string_buf(&argvars[2], buf);
2494 }
2495
2496 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2497#endif
2498}
2499
2500/*
2501 * "cursor(lnum, col)" function, or
2502 * "cursor(list)"
2503 *
2504 * Moves the cursor to the specified line and column.
2505 * Returns 0 when the position could be set, -1 otherwise.
2506 */
2507 static void
2508f_cursor(typval_T *argvars, typval_T *rettv)
2509{
2510 long line, col;
2511#ifdef FEAT_VIRTUALEDIT
2512 long coladd = 0;
2513#endif
2514 int set_curswant = TRUE;
2515
2516 rettv->vval.v_number = -1;
2517 if (argvars[1].v_type == VAR_UNKNOWN)
2518 {
2519 pos_T pos;
2520 colnr_T curswant = -1;
2521
2522 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2523 {
2524 EMSG(_(e_invarg));
2525 return;
2526 }
2527 line = pos.lnum;
2528 col = pos.col;
2529#ifdef FEAT_VIRTUALEDIT
2530 coladd = pos.coladd;
2531#endif
2532 if (curswant >= 0)
2533 {
2534 curwin->w_curswant = curswant - 1;
2535 set_curswant = FALSE;
2536 }
2537 }
2538 else
2539 {
2540 line = get_tv_lnum(argvars);
2541 col = (long)get_tv_number_chk(&argvars[1], NULL);
2542#ifdef FEAT_VIRTUALEDIT
2543 if (argvars[2].v_type != VAR_UNKNOWN)
2544 coladd = (long)get_tv_number_chk(&argvars[2], NULL);
2545#endif
2546 }
2547 if (line < 0 || col < 0
2548#ifdef FEAT_VIRTUALEDIT
2549 || coladd < 0
2550#endif
2551 )
2552 return; /* type error; errmsg already given */
2553 if (line > 0)
2554 curwin->w_cursor.lnum = line;
2555 if (col > 0)
2556 curwin->w_cursor.col = col - 1;
2557#ifdef FEAT_VIRTUALEDIT
2558 curwin->w_cursor.coladd = coladd;
2559#endif
2560
2561 /* Make sure the cursor is in a valid position. */
2562 check_cursor();
2563#ifdef FEAT_MBYTE
2564 /* Correct cursor for multi-byte character. */
2565 if (has_mbyte)
2566 mb_adjust_cursor();
2567#endif
2568
2569 curwin->w_set_curswant = set_curswant;
2570 rettv->vval.v_number = 0;
2571}
2572
2573/*
2574 * "deepcopy()" function
2575 */
2576 static void
2577f_deepcopy(typval_T *argvars, typval_T *rettv)
2578{
2579 int noref = 0;
2580 int copyID;
2581
2582 if (argvars[1].v_type != VAR_UNKNOWN)
2583 noref = (int)get_tv_number_chk(&argvars[1], NULL);
2584 if (noref < 0 || noref > 1)
2585 EMSG(_(e_invarg));
2586 else
2587 {
2588 copyID = get_copyID();
2589 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2590 }
2591}
2592
2593/*
2594 * "delete()" function
2595 */
2596 static void
2597f_delete(typval_T *argvars, typval_T *rettv)
2598{
2599 char_u nbuf[NUMBUFLEN];
2600 char_u *name;
2601 char_u *flags;
2602
2603 rettv->vval.v_number = -1;
2604 if (check_restricted() || check_secure())
2605 return;
2606
2607 name = get_tv_string(&argvars[0]);
2608 if (name == NULL || *name == NUL)
2609 {
2610 EMSG(_(e_invarg));
2611 return;
2612 }
2613
2614 if (argvars[1].v_type != VAR_UNKNOWN)
2615 flags = get_tv_string_buf(&argvars[1], nbuf);
2616 else
2617 flags = (char_u *)"";
2618
2619 if (*flags == NUL)
2620 /* delete a file */
2621 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2622 else if (STRCMP(flags, "d") == 0)
2623 /* delete an empty directory */
2624 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2625 else if (STRCMP(flags, "rf") == 0)
2626 /* delete a directory recursively */
2627 rettv->vval.v_number = delete_recursive(name);
2628 else
2629 EMSG2(_(e_invexpr2), flags);
2630}
2631
2632/*
2633 * "did_filetype()" function
2634 */
2635 static void
2636f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2637{
2638#ifdef FEAT_AUTOCMD
2639 rettv->vval.v_number = did_filetype;
2640#endif
2641}
2642
2643/*
2644 * "diff_filler()" function
2645 */
2646 static void
2647f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2648{
2649#ifdef FEAT_DIFF
2650 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
2651#endif
2652}
2653
2654/*
2655 * "diff_hlID()" function
2656 */
2657 static void
2658f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2659{
2660#ifdef FEAT_DIFF
2661 linenr_T lnum = get_tv_lnum(argvars);
2662 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01002663 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002664 static int fnum = 0;
2665 static int change_start = 0;
2666 static int change_end = 0;
2667 static hlf_T hlID = (hlf_T)0;
2668 int filler_lines;
2669 int col;
2670
2671 if (lnum < 0) /* ignore type error in {lnum} arg */
2672 lnum = 0;
2673 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002674 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002675 || fnum != curbuf->b_fnum)
2676 {
2677 /* New line, buffer, change: need to get the values. */
2678 filler_lines = diff_check(curwin, lnum);
2679 if (filler_lines < 0)
2680 {
2681 if (filler_lines == -1)
2682 {
2683 change_start = MAXCOL;
2684 change_end = -1;
2685 if (diff_find_change(curwin, lnum, &change_start, &change_end))
2686 hlID = HLF_ADD; /* added line */
2687 else
2688 hlID = HLF_CHD; /* changed line */
2689 }
2690 else
2691 hlID = HLF_ADD; /* added line */
2692 }
2693 else
2694 hlID = (hlf_T)0;
2695 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002696 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002697 fnum = curbuf->b_fnum;
2698 }
2699
2700 if (hlID == HLF_CHD || hlID == HLF_TXD)
2701 {
2702 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
2703 if (col >= change_start && col <= change_end)
2704 hlID = HLF_TXD; /* changed text */
2705 else
2706 hlID = HLF_CHD; /* changed line */
2707 }
2708 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
2709#endif
2710}
2711
2712/*
2713 * "empty({expr})" function
2714 */
2715 static void
2716f_empty(typval_T *argvars, typval_T *rettv)
2717{
2718 int n = FALSE;
2719
2720 switch (argvars[0].v_type)
2721 {
2722 case VAR_STRING:
2723 case VAR_FUNC:
2724 n = argvars[0].vval.v_string == NULL
2725 || *argvars[0].vval.v_string == NUL;
2726 break;
2727 case VAR_PARTIAL:
2728 n = FALSE;
2729 break;
2730 case VAR_NUMBER:
2731 n = argvars[0].vval.v_number == 0;
2732 break;
2733 case VAR_FLOAT:
2734#ifdef FEAT_FLOAT
2735 n = argvars[0].vval.v_float == 0.0;
2736 break;
2737#endif
2738 case VAR_LIST:
2739 n = argvars[0].vval.v_list == NULL
2740 || argvars[0].vval.v_list->lv_first == NULL;
2741 break;
2742 case VAR_DICT:
2743 n = argvars[0].vval.v_dict == NULL
2744 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2745 break;
2746 case VAR_SPECIAL:
2747 n = argvars[0].vval.v_number != VVAL_TRUE;
2748 break;
2749
2750 case VAR_JOB:
2751#ifdef FEAT_JOB_CHANNEL
2752 n = argvars[0].vval.v_job == NULL
2753 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2754 break;
2755#endif
2756 case VAR_CHANNEL:
2757#ifdef FEAT_JOB_CHANNEL
2758 n = argvars[0].vval.v_channel == NULL
2759 || !channel_is_open(argvars[0].vval.v_channel);
2760 break;
2761#endif
2762 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01002763 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002764 n = TRUE;
2765 break;
2766 }
2767
2768 rettv->vval.v_number = n;
2769}
2770
2771/*
2772 * "escape({string}, {chars})" function
2773 */
2774 static void
2775f_escape(typval_T *argvars, typval_T *rettv)
2776{
2777 char_u buf[NUMBUFLEN];
2778
2779 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
2780 get_tv_string_buf(&argvars[1], buf));
2781 rettv->v_type = VAR_STRING;
2782}
2783
2784/*
2785 * "eval()" function
2786 */
2787 static void
2788f_eval(typval_T *argvars, typval_T *rettv)
2789{
2790 char_u *s, *p;
2791
2792 s = get_tv_string_chk(&argvars[0]);
2793 if (s != NULL)
2794 s = skipwhite(s);
2795
2796 p = s;
2797 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
2798 {
2799 if (p != NULL && !aborting())
2800 EMSG2(_(e_invexpr2), p);
2801 need_clr_eos = FALSE;
2802 rettv->v_type = VAR_NUMBER;
2803 rettv->vval.v_number = 0;
2804 }
2805 else if (*s != NUL)
2806 EMSG(_(e_trailing));
2807}
2808
2809/*
2810 * "eventhandler()" function
2811 */
2812 static void
2813f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
2814{
2815 rettv->vval.v_number = vgetc_busy;
2816}
2817
2818/*
2819 * "executable()" function
2820 */
2821 static void
2822f_executable(typval_T *argvars, typval_T *rettv)
2823{
2824 char_u *name = get_tv_string(&argvars[0]);
2825
2826 /* Check in $PATH and also check directly if there is a directory name. */
2827 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
2828 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
2829}
2830
2831static garray_T redir_execute_ga;
2832
2833/*
2834 * Append "value[value_len]" to the execute() output.
2835 */
2836 void
2837execute_redir_str(char_u *value, int value_len)
2838{
2839 int len;
2840
2841 if (value_len == -1)
2842 len = (int)STRLEN(value); /* Append the entire string */
2843 else
2844 len = value_len; /* Append only "value_len" characters */
2845 if (ga_grow(&redir_execute_ga, len) == OK)
2846 {
2847 mch_memmove((char *)redir_execute_ga.ga_data
2848 + redir_execute_ga.ga_len, value, len);
2849 redir_execute_ga.ga_len += len;
2850 }
2851}
2852
2853/*
2854 * Get next line from a list.
2855 * Called by do_cmdline() to get the next line.
2856 * Returns allocated string, or NULL for end of function.
2857 */
2858
2859 static char_u *
2860get_list_line(
2861 int c UNUSED,
2862 void *cookie,
2863 int indent UNUSED)
2864{
2865 listitem_T **p = (listitem_T **)cookie;
2866 listitem_T *item = *p;
2867 char_u buf[NUMBUFLEN];
2868 char_u *s;
2869
2870 if (item == NULL)
2871 return NULL;
2872 s = get_tv_string_buf_chk(&item->li_tv, buf);
2873 *p = item->li_next;
2874 return s == NULL ? NULL : vim_strsave(s);
2875}
2876
2877/*
2878 * "execute()" function
2879 */
2880 static void
2881f_execute(typval_T *argvars, typval_T *rettv)
2882{
2883 char_u *cmd = NULL;
2884 list_T *list = NULL;
2885 int save_msg_silent = msg_silent;
2886 int save_emsg_silent = emsg_silent;
2887 int save_emsg_noredir = emsg_noredir;
2888 int save_redir_execute = redir_execute;
2889 garray_T save_ga;
2890
2891 rettv->vval.v_string = NULL;
2892 rettv->v_type = VAR_STRING;
2893
2894 if (argvars[0].v_type == VAR_LIST)
2895 {
2896 list = argvars[0].vval.v_list;
2897 if (list == NULL || list->lv_first == NULL)
2898 /* empty list, no commands, empty output */
2899 return;
2900 ++list->lv_refcount;
2901 }
2902 else
2903 {
2904 cmd = get_tv_string_chk(&argvars[0]);
2905 if (cmd == NULL)
2906 return;
2907 }
2908
2909 if (argvars[1].v_type != VAR_UNKNOWN)
2910 {
2911 char_u buf[NUMBUFLEN];
2912 char_u *s = get_tv_string_buf_chk(&argvars[1], buf);
2913
2914 if (s == NULL)
2915 return;
2916 if (STRNCMP(s, "silent", 6) == 0)
2917 ++msg_silent;
2918 if (STRCMP(s, "silent!") == 0)
2919 {
2920 emsg_silent = TRUE;
2921 emsg_noredir = TRUE;
2922 }
2923 }
2924 else
2925 ++msg_silent;
2926
2927 if (redir_execute)
2928 save_ga = redir_execute_ga;
2929 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
2930 redir_execute = TRUE;
2931
2932 if (cmd != NULL)
2933 do_cmdline_cmd(cmd);
2934 else
2935 {
2936 listitem_T *item = list->lv_first;
2937
2938 do_cmdline(NULL, get_list_line, (void *)&item,
2939 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
2940 --list->lv_refcount;
2941 }
2942
Bram Moolenaard297f352017-01-29 20:31:21 +01002943 /* Need to append a NUL to the result. */
2944 if (ga_grow(&redir_execute_ga, 1) == OK)
2945 {
2946 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
2947 rettv->vval.v_string = redir_execute_ga.ga_data;
2948 }
2949 else
2950 {
2951 ga_clear(&redir_execute_ga);
2952 rettv->vval.v_string = NULL;
2953 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002954 msg_silent = save_msg_silent;
2955 emsg_silent = save_emsg_silent;
2956 emsg_noredir = save_emsg_noredir;
2957
2958 redir_execute = save_redir_execute;
2959 if (redir_execute)
2960 redir_execute_ga = save_ga;
2961
2962 /* "silent reg" or "silent echo x" leaves msg_col somewhere in the
2963 * line. Put it back in the first column. */
2964 msg_col = 0;
2965}
2966
2967/*
2968 * "exepath()" function
2969 */
2970 static void
2971f_exepath(typval_T *argvars, typval_T *rettv)
2972{
2973 char_u *p = NULL;
2974
2975 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
2976 rettv->v_type = VAR_STRING;
2977 rettv->vval.v_string = p;
2978}
2979
2980/*
2981 * "exists()" function
2982 */
2983 static void
2984f_exists(typval_T *argvars, typval_T *rettv)
2985{
2986 char_u *p;
2987 char_u *name;
2988 int n = FALSE;
2989 int len = 0;
2990
2991 p = get_tv_string(&argvars[0]);
2992 if (*p == '$') /* environment variable */
2993 {
2994 /* first try "normal" environment variables (fast) */
2995 if (mch_getenv(p + 1) != NULL)
2996 n = TRUE;
2997 else
2998 {
2999 /* try expanding things like $VIM and ${HOME} */
3000 p = expand_env_save(p);
3001 if (p != NULL && *p != '$')
3002 n = TRUE;
3003 vim_free(p);
3004 }
3005 }
3006 else if (*p == '&' || *p == '+') /* option */
3007 {
3008 n = (get_option_tv(&p, NULL, TRUE) == OK);
3009 if (*skipwhite(p) != NUL)
3010 n = FALSE; /* trailing garbage */
3011 }
3012 else if (*p == '*') /* internal or user defined function */
3013 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003014 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003015 }
3016 else if (*p == ':')
3017 {
3018 n = cmd_exists(p + 1);
3019 }
3020 else if (*p == '#')
3021 {
3022#ifdef FEAT_AUTOCMD
3023 if (p[1] == '#')
3024 n = autocmd_supported(p + 2);
3025 else
3026 n = au_exists(p + 1);
3027#endif
3028 }
3029 else /* internal variable */
3030 {
3031 char_u *tofree;
3032 typval_T tv;
3033
3034 /* get_name_len() takes care of expanding curly braces */
3035 name = p;
3036 len = get_name_len(&p, &tofree, TRUE, FALSE);
3037 if (len > 0)
3038 {
3039 if (tofree != NULL)
3040 name = tofree;
3041 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
3042 if (n)
3043 {
3044 /* handle d.key, l[idx], f(expr) */
3045 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
3046 if (n)
3047 clear_tv(&tv);
3048 }
3049 }
3050 if (*p != NUL)
3051 n = FALSE;
3052
3053 vim_free(tofree);
3054 }
3055
3056 rettv->vval.v_number = n;
3057}
3058
3059#ifdef FEAT_FLOAT
3060/*
3061 * "exp()" function
3062 */
3063 static void
3064f_exp(typval_T *argvars, typval_T *rettv)
3065{
3066 float_T f = 0.0;
3067
3068 rettv->v_type = VAR_FLOAT;
3069 if (get_float_arg(argvars, &f) == OK)
3070 rettv->vval.v_float = exp(f);
3071 else
3072 rettv->vval.v_float = 0.0;
3073}
3074#endif
3075
3076/*
3077 * "expand()" function
3078 */
3079 static void
3080f_expand(typval_T *argvars, typval_T *rettv)
3081{
3082 char_u *s;
3083 int len;
3084 char_u *errormsg;
3085 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3086 expand_T xpc;
3087 int error = FALSE;
3088 char_u *result;
3089
3090 rettv->v_type = VAR_STRING;
3091 if (argvars[1].v_type != VAR_UNKNOWN
3092 && argvars[2].v_type != VAR_UNKNOWN
3093 && get_tv_number_chk(&argvars[2], &error)
3094 && !error)
3095 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003096 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003097 }
3098
3099 s = get_tv_string(&argvars[0]);
3100 if (*s == '%' || *s == '#' || *s == '<')
3101 {
3102 ++emsg_off;
3103 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3104 --emsg_off;
3105 if (rettv->v_type == VAR_LIST)
3106 {
3107 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3108 list_append_string(rettv->vval.v_list, result, -1);
3109 else
3110 vim_free(result);
3111 }
3112 else
3113 rettv->vval.v_string = result;
3114 }
3115 else
3116 {
3117 /* When the optional second argument is non-zero, don't remove matches
3118 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3119 if (argvars[1].v_type != VAR_UNKNOWN
3120 && get_tv_number_chk(&argvars[1], &error))
3121 options |= WILD_KEEP_ALL;
3122 if (!error)
3123 {
3124 ExpandInit(&xpc);
3125 xpc.xp_context = EXPAND_FILES;
3126 if (p_wic)
3127 options += WILD_ICASE;
3128 if (rettv->v_type == VAR_STRING)
3129 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3130 options, WILD_ALL);
3131 else if (rettv_list_alloc(rettv) != FAIL)
3132 {
3133 int i;
3134
3135 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3136 for (i = 0; i < xpc.xp_numfiles; i++)
3137 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3138 ExpandCleanup(&xpc);
3139 }
3140 }
3141 else
3142 rettv->vval.v_string = NULL;
3143 }
3144}
3145
3146/*
3147 * "extend(list, list [, idx])" function
3148 * "extend(dict, dict [, action])" function
3149 */
3150 static void
3151f_extend(typval_T *argvars, typval_T *rettv)
3152{
3153 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3154
3155 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3156 {
3157 list_T *l1, *l2;
3158 listitem_T *item;
3159 long before;
3160 int error = FALSE;
3161
3162 l1 = argvars[0].vval.v_list;
3163 l2 = argvars[1].vval.v_list;
3164 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
3165 && l2 != NULL)
3166 {
3167 if (argvars[2].v_type != VAR_UNKNOWN)
3168 {
3169 before = (long)get_tv_number_chk(&argvars[2], &error);
3170 if (error)
3171 return; /* type error; errmsg already given */
3172
3173 if (before == l1->lv_len)
3174 item = NULL;
3175 else
3176 {
3177 item = list_find(l1, before);
3178 if (item == NULL)
3179 {
3180 EMSGN(_(e_listidx), before);
3181 return;
3182 }
3183 }
3184 }
3185 else
3186 item = NULL;
3187 list_extend(l1, l2, item);
3188
3189 copy_tv(&argvars[0], rettv);
3190 }
3191 }
3192 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3193 {
3194 dict_T *d1, *d2;
3195 char_u *action;
3196 int i;
3197
3198 d1 = argvars[0].vval.v_dict;
3199 d2 = argvars[1].vval.v_dict;
3200 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
3201 && d2 != NULL)
3202 {
3203 /* Check the third argument. */
3204 if (argvars[2].v_type != VAR_UNKNOWN)
3205 {
3206 static char *(av[]) = {"keep", "force", "error"};
3207
3208 action = get_tv_string_chk(&argvars[2]);
3209 if (action == NULL)
3210 return; /* type error; errmsg already given */
3211 for (i = 0; i < 3; ++i)
3212 if (STRCMP(action, av[i]) == 0)
3213 break;
3214 if (i == 3)
3215 {
3216 EMSG2(_(e_invarg2), action);
3217 return;
3218 }
3219 }
3220 else
3221 action = (char_u *)"force";
3222
3223 dict_extend(d1, d2, action);
3224
3225 copy_tv(&argvars[0], rettv);
3226 }
3227 }
3228 else
3229 EMSG2(_(e_listdictarg), "extend()");
3230}
3231
3232/*
3233 * "feedkeys()" function
3234 */
3235 static void
3236f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3237{
3238 int remap = TRUE;
3239 int insert = FALSE;
3240 char_u *keys, *flags;
3241 char_u nbuf[NUMBUFLEN];
3242 int typed = FALSE;
3243 int execute = FALSE;
3244 int dangerous = FALSE;
3245 char_u *keys_esc;
3246
3247 /* This is not allowed in the sandbox. If the commands would still be
3248 * executed in the sandbox it would be OK, but it probably happens later,
3249 * when "sandbox" is no longer set. */
3250 if (check_secure())
3251 return;
3252
3253 keys = get_tv_string(&argvars[0]);
3254
3255 if (argvars[1].v_type != VAR_UNKNOWN)
3256 {
3257 flags = get_tv_string_buf(&argvars[1], nbuf);
3258 for ( ; *flags != NUL; ++flags)
3259 {
3260 switch (*flags)
3261 {
3262 case 'n': remap = FALSE; break;
3263 case 'm': remap = TRUE; break;
3264 case 't': typed = TRUE; break;
3265 case 'i': insert = TRUE; break;
3266 case 'x': execute = TRUE; break;
3267 case '!': dangerous = TRUE; break;
3268 }
3269 }
3270 }
3271
3272 if (*keys != NUL || execute)
3273 {
3274 /* Need to escape K_SPECIAL and CSI before putting the string in the
3275 * typeahead buffer. */
3276 keys_esc = vim_strsave_escape_csi(keys);
3277 if (keys_esc != NULL)
3278 {
3279 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
3280 insert ? 0 : typebuf.tb_len, !typed, FALSE);
3281 vim_free(keys_esc);
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003282 if (vgetc_busy
3283#ifdef FEAT_TIMERS
3284 || timer_busy
3285#endif
3286 )
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003287 typebuf_was_filled = TRUE;
3288 if (execute)
3289 {
3290 int save_msg_scroll = msg_scroll;
3291
3292 /* Avoid a 1 second delay when the keys start Insert mode. */
3293 msg_scroll = FALSE;
3294
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02003295#ifdef FEAT_TERMINAL
3296 if (term_use_loop())
3297 terminal_loop(FALSE);
3298 else
3299#endif
3300 {
3301 if (!dangerous)
3302 ++ex_normal_busy;
3303 exec_normal(TRUE);
3304 if (!dangerous)
3305 --ex_normal_busy;
3306 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003307 msg_scroll |= save_msg_scroll;
3308 }
3309 }
3310 }
3311}
3312
3313/*
3314 * "filereadable()" function
3315 */
3316 static void
3317f_filereadable(typval_T *argvars, typval_T *rettv)
3318{
3319 int fd;
3320 char_u *p;
3321 int n;
3322
3323#ifndef O_NONBLOCK
3324# define O_NONBLOCK 0
3325#endif
3326 p = get_tv_string(&argvars[0]);
3327 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3328 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3329 {
3330 n = TRUE;
3331 close(fd);
3332 }
3333 else
3334 n = FALSE;
3335
3336 rettv->vval.v_number = n;
3337}
3338
3339/*
3340 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3341 * rights to write into.
3342 */
3343 static void
3344f_filewritable(typval_T *argvars, typval_T *rettv)
3345{
3346 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
3347}
3348
3349 static void
3350findfilendir(
3351 typval_T *argvars UNUSED,
3352 typval_T *rettv,
3353 int find_what UNUSED)
3354{
3355#ifdef FEAT_SEARCHPATH
3356 char_u *fname;
3357 char_u *fresult = NULL;
3358 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3359 char_u *p;
3360 char_u pathbuf[NUMBUFLEN];
3361 int count = 1;
3362 int first = TRUE;
3363 int error = FALSE;
3364#endif
3365
3366 rettv->vval.v_string = NULL;
3367 rettv->v_type = VAR_STRING;
3368
3369#ifdef FEAT_SEARCHPATH
3370 fname = get_tv_string(&argvars[0]);
3371
3372 if (argvars[1].v_type != VAR_UNKNOWN)
3373 {
3374 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
3375 if (p == NULL)
3376 error = TRUE;
3377 else
3378 {
3379 if (*p != NUL)
3380 path = p;
3381
3382 if (argvars[2].v_type != VAR_UNKNOWN)
3383 count = (int)get_tv_number_chk(&argvars[2], &error);
3384 }
3385 }
3386
3387 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3388 error = TRUE;
3389
3390 if (*fname != NUL && !error)
3391 {
3392 do
3393 {
3394 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3395 vim_free(fresult);
3396 fresult = find_file_in_path_option(first ? fname : NULL,
3397 first ? (int)STRLEN(fname) : 0,
3398 0, first, path,
3399 find_what,
3400 curbuf->b_ffname,
3401 find_what == FINDFILE_DIR
3402 ? (char_u *)"" : curbuf->b_p_sua);
3403 first = FALSE;
3404
3405 if (fresult != NULL && rettv->v_type == VAR_LIST)
3406 list_append_string(rettv->vval.v_list, fresult, -1);
3407
3408 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3409 }
3410
3411 if (rettv->v_type == VAR_STRING)
3412 rettv->vval.v_string = fresult;
3413#endif
3414}
3415
3416/*
3417 * "filter()" function
3418 */
3419 static void
3420f_filter(typval_T *argvars, typval_T *rettv)
3421{
3422 filter_map(argvars, rettv, FALSE);
3423}
3424
3425/*
3426 * "finddir({fname}[, {path}[, {count}]])" function
3427 */
3428 static void
3429f_finddir(typval_T *argvars, typval_T *rettv)
3430{
3431 findfilendir(argvars, rettv, FINDFILE_DIR);
3432}
3433
3434/*
3435 * "findfile({fname}[, {path}[, {count}]])" function
3436 */
3437 static void
3438f_findfile(typval_T *argvars, typval_T *rettv)
3439{
3440 findfilendir(argvars, rettv, FINDFILE_FILE);
3441}
3442
3443#ifdef FEAT_FLOAT
3444/*
3445 * "float2nr({float})" function
3446 */
3447 static void
3448f_float2nr(typval_T *argvars, typval_T *rettv)
3449{
3450 float_T f = 0.0;
3451
3452 if (get_float_arg(argvars, &f) == OK)
3453 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003454 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003455 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003456 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003457 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003458 else
3459 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003460 }
3461}
3462
3463/*
3464 * "floor({float})" function
3465 */
3466 static void
3467f_floor(typval_T *argvars, typval_T *rettv)
3468{
3469 float_T f = 0.0;
3470
3471 rettv->v_type = VAR_FLOAT;
3472 if (get_float_arg(argvars, &f) == OK)
3473 rettv->vval.v_float = floor(f);
3474 else
3475 rettv->vval.v_float = 0.0;
3476}
3477
3478/*
3479 * "fmod()" function
3480 */
3481 static void
3482f_fmod(typval_T *argvars, typval_T *rettv)
3483{
3484 float_T fx = 0.0, fy = 0.0;
3485
3486 rettv->v_type = VAR_FLOAT;
3487 if (get_float_arg(argvars, &fx) == OK
3488 && get_float_arg(&argvars[1], &fy) == OK)
3489 rettv->vval.v_float = fmod(fx, fy);
3490 else
3491 rettv->vval.v_float = 0.0;
3492}
3493#endif
3494
3495/*
3496 * "fnameescape({string})" function
3497 */
3498 static void
3499f_fnameescape(typval_T *argvars, typval_T *rettv)
3500{
3501 rettv->vval.v_string = vim_strsave_fnameescape(
3502 get_tv_string(&argvars[0]), FALSE);
3503 rettv->v_type = VAR_STRING;
3504}
3505
3506/*
3507 * "fnamemodify({fname}, {mods})" function
3508 */
3509 static void
3510f_fnamemodify(typval_T *argvars, typval_T *rettv)
3511{
3512 char_u *fname;
3513 char_u *mods;
3514 int usedlen = 0;
3515 int len;
3516 char_u *fbuf = NULL;
3517 char_u buf[NUMBUFLEN];
3518
3519 fname = get_tv_string_chk(&argvars[0]);
3520 mods = get_tv_string_buf_chk(&argvars[1], buf);
3521 if (fname == NULL || mods == NULL)
3522 fname = NULL;
3523 else
3524 {
3525 len = (int)STRLEN(fname);
3526 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
3527 }
3528
3529 rettv->v_type = VAR_STRING;
3530 if (fname == NULL)
3531 rettv->vval.v_string = NULL;
3532 else
3533 rettv->vval.v_string = vim_strnsave(fname, len);
3534 vim_free(fbuf);
3535}
3536
3537static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
3538
3539/*
3540 * "foldclosed()" function
3541 */
3542 static void
3543foldclosed_both(
3544 typval_T *argvars UNUSED,
3545 typval_T *rettv,
3546 int end UNUSED)
3547{
3548#ifdef FEAT_FOLDING
3549 linenr_T lnum;
3550 linenr_T first, last;
3551
3552 lnum = get_tv_lnum(argvars);
3553 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3554 {
3555 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3556 {
3557 if (end)
3558 rettv->vval.v_number = (varnumber_T)last;
3559 else
3560 rettv->vval.v_number = (varnumber_T)first;
3561 return;
3562 }
3563 }
3564#endif
3565 rettv->vval.v_number = -1;
3566}
3567
3568/*
3569 * "foldclosed()" function
3570 */
3571 static void
3572f_foldclosed(typval_T *argvars, typval_T *rettv)
3573{
3574 foldclosed_both(argvars, rettv, FALSE);
3575}
3576
3577/*
3578 * "foldclosedend()" function
3579 */
3580 static void
3581f_foldclosedend(typval_T *argvars, typval_T *rettv)
3582{
3583 foldclosed_both(argvars, rettv, TRUE);
3584}
3585
3586/*
3587 * "foldlevel()" function
3588 */
3589 static void
3590f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3591{
3592#ifdef FEAT_FOLDING
3593 linenr_T lnum;
3594
3595 lnum = get_tv_lnum(argvars);
3596 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3597 rettv->vval.v_number = foldLevel(lnum);
3598#endif
3599}
3600
3601/*
3602 * "foldtext()" function
3603 */
3604 static void
3605f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
3606{
3607#ifdef FEAT_FOLDING
3608 linenr_T foldstart;
3609 linenr_T foldend;
3610 char_u *dashes;
3611 linenr_T lnum;
3612 char_u *s;
3613 char_u *r;
3614 int len;
3615 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003616 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003617#endif
3618
3619 rettv->v_type = VAR_STRING;
3620 rettv->vval.v_string = NULL;
3621#ifdef FEAT_FOLDING
3622 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
3623 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
3624 dashes = get_vim_var_str(VV_FOLDDASHES);
3625 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
3626 && dashes != NULL)
3627 {
3628 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02003629 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003630 if (!linewhite(lnum))
3631 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003632
3633 /* Find interesting text in this line. */
3634 s = skipwhite(ml_get(lnum));
3635 /* skip C comment-start */
3636 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
3637 {
3638 s = skipwhite(s + 2);
3639 if (*skipwhite(s) == NUL
3640 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
3641 {
3642 s = skipwhite(ml_get(lnum + 1));
3643 if (*s == '*')
3644 s = skipwhite(s + 1);
3645 }
3646 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02003647 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003648 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003649 r = alloc((unsigned)(STRLEN(txt)
3650 + STRLEN(dashes) /* for %s */
3651 + 20 /* for %3ld */
3652 + STRLEN(s))); /* concatenated */
3653 if (r != NULL)
3654 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02003655 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003656 len = (int)STRLEN(r);
3657 STRCAT(r, s);
3658 /* remove 'foldmarker' and 'commentstring' */
3659 foldtext_cleanup(r + len);
3660 rettv->vval.v_string = r;
3661 }
3662 }
3663#endif
3664}
3665
3666/*
3667 * "foldtextresult(lnum)" function
3668 */
3669 static void
3670f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
3671{
3672#ifdef FEAT_FOLDING
3673 linenr_T lnum;
3674 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003675 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003676 foldinfo_T foldinfo;
3677 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003678 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003679#endif
3680
3681 rettv->v_type = VAR_STRING;
3682 rettv->vval.v_string = NULL;
3683#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003684 if (entered)
3685 return; /* reject recursive use */
3686 entered = TRUE;
3687
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003688 lnum = get_tv_lnum(argvars);
3689 /* treat illegal types and illegal string values for {lnum} the same */
3690 if (lnum < 0)
3691 lnum = 0;
3692 fold_count = foldedCount(curwin, lnum, &foldinfo);
3693 if (fold_count > 0)
3694 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01003695 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
3696 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003697 if (text == buf)
3698 text = vim_strsave(text);
3699 rettv->vval.v_string = text;
3700 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003701
3702 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003703#endif
3704}
3705
3706/*
3707 * "foreground()" function
3708 */
3709 static void
3710f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3711{
3712#ifdef FEAT_GUI
3713 if (gui.in_use)
3714 gui_mch_set_foreground();
3715#else
3716# ifdef WIN32
3717 win32_set_foreground();
3718# endif
3719#endif
3720}
3721
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003722 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003723common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003724{
3725 char_u *s;
3726 char_u *name;
3727 int use_string = FALSE;
3728 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003729 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003730
3731 if (argvars[0].v_type == VAR_FUNC)
3732 {
3733 /* function(MyFunc, [arg], dict) */
3734 s = argvars[0].vval.v_string;
3735 }
3736 else if (argvars[0].v_type == VAR_PARTIAL
3737 && argvars[0].vval.v_partial != NULL)
3738 {
3739 /* function(dict.MyFunc, [arg]) */
3740 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003741 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003742 }
3743 else
3744 {
3745 /* function('MyFunc', [arg], dict) */
3746 s = get_tv_string(&argvars[0]);
3747 use_string = TRUE;
3748 }
3749
Bram Moolenaar843b8842016-08-21 14:36:15 +02003750 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003751 {
3752 name = s;
3753 trans_name = trans_function_name(&name, FALSE,
3754 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
3755 if (*name != NUL)
3756 s = NULL;
3757 }
3758
Bram Moolenaar843b8842016-08-21 14:36:15 +02003759 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
3760 || (is_funcref && trans_name == NULL))
Bram Moolenaar5582ef12016-09-14 22:16:13 +02003761 EMSG2(_(e_invarg2), use_string ? get_tv_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003762 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003763 else if (trans_name != NULL && (is_funcref
3764 ? find_func(trans_name) == NULL
3765 : !translated_function_exists(trans_name)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003766 EMSG2(_("E700: Unknown function: %s"), s);
3767 else
3768 {
3769 int dict_idx = 0;
3770 int arg_idx = 0;
3771 list_T *list = NULL;
3772
3773 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
3774 {
3775 char sid_buf[25];
3776 int off = *s == 's' ? 2 : 5;
3777
3778 /* Expand s: and <SID> into <SNR>nr_, so that the function can
3779 * also be called from another script. Using trans_function_name()
3780 * would also work, but some plugins depend on the name being
3781 * printable text. */
3782 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
3783 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
3784 if (name != NULL)
3785 {
3786 STRCPY(name, sid_buf);
3787 STRCAT(name, s + off);
3788 }
3789 }
3790 else
3791 name = vim_strsave(s);
3792
3793 if (argvars[1].v_type != VAR_UNKNOWN)
3794 {
3795 if (argvars[2].v_type != VAR_UNKNOWN)
3796 {
3797 /* function(name, [args], dict) */
3798 arg_idx = 1;
3799 dict_idx = 2;
3800 }
3801 else if (argvars[1].v_type == VAR_DICT)
3802 /* function(name, dict) */
3803 dict_idx = 1;
3804 else
3805 /* function(name, [args]) */
3806 arg_idx = 1;
3807 if (dict_idx > 0)
3808 {
3809 if (argvars[dict_idx].v_type != VAR_DICT)
3810 {
3811 EMSG(_("E922: expected a dict"));
3812 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003813 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003814 }
3815 if (argvars[dict_idx].vval.v_dict == NULL)
3816 dict_idx = 0;
3817 }
3818 if (arg_idx > 0)
3819 {
3820 if (argvars[arg_idx].v_type != VAR_LIST)
3821 {
3822 EMSG(_("E923: Second argument of function() must be a list or a dict"));
3823 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003824 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003825 }
3826 list = argvars[arg_idx].vval.v_list;
3827 if (list == NULL || list->lv_len == 0)
3828 arg_idx = 0;
3829 }
3830 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003831 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003832 {
3833 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
3834
3835 /* result is a VAR_PARTIAL */
3836 if (pt == NULL)
3837 vim_free(name);
3838 else
3839 {
3840 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
3841 {
3842 listitem_T *li;
3843 int i = 0;
3844 int arg_len = 0;
3845 int lv_len = 0;
3846
3847 if (arg_pt != NULL)
3848 arg_len = arg_pt->pt_argc;
3849 if (list != NULL)
3850 lv_len = list->lv_len;
3851 pt->pt_argc = arg_len + lv_len;
3852 pt->pt_argv = (typval_T *)alloc(
3853 sizeof(typval_T) * pt->pt_argc);
3854 if (pt->pt_argv == NULL)
3855 {
3856 vim_free(pt);
3857 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003858 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003859 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003860 for (i = 0; i < arg_len; i++)
3861 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
3862 if (lv_len > 0)
3863 for (li = list->lv_first; li != NULL;
3864 li = li->li_next)
3865 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003866 }
3867
3868 /* For "function(dict.func, [], dict)" and "func" is a partial
3869 * use "dict". That is backwards compatible. */
3870 if (dict_idx > 0)
3871 {
3872 /* The dict is bound explicitly, pt_auto is FALSE. */
3873 pt->pt_dict = argvars[dict_idx].vval.v_dict;
3874 ++pt->pt_dict->dv_refcount;
3875 }
3876 else if (arg_pt != NULL)
3877 {
3878 /* If the dict was bound automatically the result is also
3879 * bound automatically. */
3880 pt->pt_dict = arg_pt->pt_dict;
3881 pt->pt_auto = arg_pt->pt_auto;
3882 if (pt->pt_dict != NULL)
3883 ++pt->pt_dict->dv_refcount;
3884 }
3885
3886 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003887 if (arg_pt != NULL && arg_pt->pt_func != NULL)
3888 {
3889 pt->pt_func = arg_pt->pt_func;
3890 func_ptr_ref(pt->pt_func);
3891 vim_free(name);
3892 }
3893 else if (is_funcref)
3894 {
3895 pt->pt_func = find_func(trans_name);
3896 func_ptr_ref(pt->pt_func);
3897 vim_free(name);
3898 }
3899 else
3900 {
3901 pt->pt_name = name;
3902 func_ref(name);
3903 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003904 }
3905 rettv->v_type = VAR_PARTIAL;
3906 rettv->vval.v_partial = pt;
3907 }
3908 else
3909 {
3910 /* result is a VAR_FUNC */
3911 rettv->v_type = VAR_FUNC;
3912 rettv->vval.v_string = name;
3913 func_ref(name);
3914 }
3915 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003916theend:
3917 vim_free(trans_name);
3918}
3919
3920/*
3921 * "funcref()" function
3922 */
3923 static void
3924f_funcref(typval_T *argvars, typval_T *rettv)
3925{
3926 common_function(argvars, rettv, TRUE);
3927}
3928
3929/*
3930 * "function()" function
3931 */
3932 static void
3933f_function(typval_T *argvars, typval_T *rettv)
3934{
3935 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003936}
3937
3938/*
3939 * "garbagecollect()" function
3940 */
3941 static void
3942f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
3943{
3944 /* This is postponed until we are back at the toplevel, because we may be
3945 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
3946 want_garbage_collect = TRUE;
3947
3948 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
3949 garbage_collect_at_exit = TRUE;
3950}
3951
3952/*
3953 * "get()" function
3954 */
3955 static void
3956f_get(typval_T *argvars, typval_T *rettv)
3957{
3958 listitem_T *li;
3959 list_T *l;
3960 dictitem_T *di;
3961 dict_T *d;
3962 typval_T *tv = NULL;
3963
3964 if (argvars[0].v_type == VAR_LIST)
3965 {
3966 if ((l = argvars[0].vval.v_list) != NULL)
3967 {
3968 int error = FALSE;
3969
3970 li = list_find(l, (long)get_tv_number_chk(&argvars[1], &error));
3971 if (!error && li != NULL)
3972 tv = &li->li_tv;
3973 }
3974 }
3975 else if (argvars[0].v_type == VAR_DICT)
3976 {
3977 if ((d = argvars[0].vval.v_dict) != NULL)
3978 {
3979 di = dict_find(d, get_tv_string(&argvars[1]), -1);
3980 if (di != NULL)
3981 tv = &di->di_tv;
3982 }
3983 }
3984 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
3985 {
3986 partial_T *pt;
3987 partial_T fref_pt;
3988
3989 if (argvars[0].v_type == VAR_PARTIAL)
3990 pt = argvars[0].vval.v_partial;
3991 else
3992 {
3993 vim_memset(&fref_pt, 0, sizeof(fref_pt));
3994 fref_pt.pt_name = argvars[0].vval.v_string;
3995 pt = &fref_pt;
3996 }
3997
3998 if (pt != NULL)
3999 {
4000 char_u *what = get_tv_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004001 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004002
4003 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4004 {
4005 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004006 n = partial_name(pt);
4007 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004008 rettv->vval.v_string = NULL;
4009 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004010 {
4011 rettv->vval.v_string = vim_strsave(n);
4012 if (rettv->v_type == VAR_FUNC)
4013 func_ref(rettv->vval.v_string);
4014 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004015 }
4016 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004017 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004018 else if (STRCMP(what, "args") == 0)
4019 {
4020 rettv->v_type = VAR_LIST;
4021 if (rettv_list_alloc(rettv) == OK)
4022 {
4023 int i;
4024
4025 for (i = 0; i < pt->pt_argc; ++i)
4026 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4027 }
4028 }
4029 else
4030 EMSG2(_(e_invarg2), what);
4031 return;
4032 }
4033 }
4034 else
4035 EMSG2(_(e_listdictarg), "get()");
4036
4037 if (tv == NULL)
4038 {
4039 if (argvars[2].v_type != VAR_UNKNOWN)
4040 copy_tv(&argvars[2], rettv);
4041 }
4042 else
4043 copy_tv(tv, rettv);
4044}
4045
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004046#ifdef FEAT_SIGNS
4047/*
4048 * Returns information about signs placed in a buffer as list of dicts.
4049 */
4050 static void
4051get_buffer_signs(buf_T *buf, list_T *l)
4052{
4053 signlist_T *sign;
4054
4055 for (sign = buf->b_signlist; sign; sign = sign->next)
4056 {
4057 dict_T *d = dict_alloc();
4058
4059 if (d != NULL)
4060 {
4061 dict_add_nr_str(d, "id", sign->id, NULL);
4062 dict_add_nr_str(d, "lnum", sign->lnum, NULL);
Bram Moolenaar6a402ed2016-08-28 14:11:24 +02004063 dict_add_nr_str(d, "name", 0L, sign_typenr2name(sign->typenr));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004064
4065 list_append_dict(l, d);
4066 }
4067 }
4068}
4069#endif
4070
4071/*
4072 * Returns buffer options, variables and other attributes in a dictionary.
4073 */
4074 static dict_T *
4075get_buffer_info(buf_T *buf)
4076{
4077 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004078 tabpage_T *tp;
4079 win_T *wp;
4080 list_T *windows;
4081
4082 dict = dict_alloc();
4083 if (dict == NULL)
4084 return NULL;
4085
Bram Moolenaar33928832016-08-18 21:22:04 +02004086 dict_add_nr_str(dict, "bufnr", buf->b_fnum, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004087 dict_add_nr_str(dict, "name", 0L,
4088 buf->b_ffname != NULL ? buf->b_ffname : (char_u *)"");
Bram Moolenaarf845b872017-01-06 14:04:54 +01004089 dict_add_nr_str(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4090 : buflist_findlnum(buf), NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004091 dict_add_nr_str(dict, "loaded", buf->b_ml.ml_mfp != NULL, NULL);
4092 dict_add_nr_str(dict, "listed", buf->b_p_bl, NULL);
4093 dict_add_nr_str(dict, "changed", bufIsChanged(buf), NULL);
Bram Moolenaar95c526e2017-02-25 14:59:34 +01004094 dict_add_nr_str(dict, "changedtick", CHANGEDTICK(buf), NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004095 dict_add_nr_str(dict, "hidden",
4096 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0,
4097 NULL);
4098
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004099 /* Get a reference to buffer variables */
4100 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004101
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004102 /* List of windows displaying this buffer */
4103 windows = list_alloc();
4104 if (windows != NULL)
4105 {
4106 FOR_ALL_TAB_WINDOWS(tp, wp)
4107 if (wp->w_buffer == buf)
4108 list_append_number(windows, (varnumber_T)wp->w_id);
4109 dict_add_list(dict, "windows", windows);
4110 }
4111
4112#ifdef FEAT_SIGNS
4113 if (buf->b_signlist != NULL)
4114 {
4115 /* List of signs placed in this buffer */
4116 list_T *signs = list_alloc();
4117 if (signs != NULL)
4118 {
4119 get_buffer_signs(buf, signs);
4120 dict_add_list(dict, "signs", signs);
4121 }
4122 }
4123#endif
4124
4125 return dict;
4126}
4127
4128/*
4129 * "getbufinfo()" function
4130 */
4131 static void
4132f_getbufinfo(typval_T *argvars, typval_T *rettv)
4133{
4134 buf_T *buf = NULL;
4135 buf_T *argbuf = NULL;
4136 dict_T *d;
4137 int filtered = FALSE;
4138 int sel_buflisted = FALSE;
4139 int sel_bufloaded = FALSE;
4140
4141 if (rettv_list_alloc(rettv) != OK)
4142 return;
4143
4144 /* List of all the buffers or selected buffers */
4145 if (argvars[0].v_type == VAR_DICT)
4146 {
4147 dict_T *sel_d = argvars[0].vval.v_dict;
4148
4149 if (sel_d != NULL)
4150 {
4151 dictitem_T *di;
4152
4153 filtered = TRUE;
4154
4155 di = dict_find(sel_d, (char_u *)"buflisted", -1);
4156 if (di != NULL && get_tv_number(&di->di_tv))
4157 sel_buflisted = TRUE;
4158
4159 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
4160 if (di != NULL && get_tv_number(&di->di_tv))
4161 sel_bufloaded = TRUE;
4162 }
4163 }
4164 else if (argvars[0].v_type != VAR_UNKNOWN)
4165 {
4166 /* Information about one buffer. Argument specifies the buffer */
4167 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4168 ++emsg_off;
4169 argbuf = get_buf_tv(&argvars[0], FALSE);
4170 --emsg_off;
4171 if (argbuf == NULL)
4172 return;
4173 }
4174
4175 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004176 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004177 {
4178 if (argbuf != NULL && argbuf != buf)
4179 continue;
4180 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
4181 || (sel_buflisted && !buf->b_p_bl)))
4182 continue;
4183
4184 d = get_buffer_info(buf);
4185 if (d != NULL)
4186 list_append_dict(rettv->vval.v_list, d);
4187 if (argbuf != NULL)
4188 return;
4189 }
4190}
4191
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004192static void get_buffer_lines(buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv);
4193
4194/*
4195 * Get line or list of lines from buffer "buf" into "rettv".
4196 * Return a range (from start to end) of lines in rettv from the specified
4197 * buffer.
4198 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4199 */
4200 static void
4201get_buffer_lines(
4202 buf_T *buf,
4203 linenr_T start,
4204 linenr_T end,
4205 int retlist,
4206 typval_T *rettv)
4207{
4208 char_u *p;
4209
4210 rettv->v_type = VAR_STRING;
4211 rettv->vval.v_string = NULL;
4212 if (retlist && rettv_list_alloc(rettv) == FAIL)
4213 return;
4214
4215 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4216 return;
4217
4218 if (!retlist)
4219 {
4220 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4221 p = ml_get_buf(buf, start, FALSE);
4222 else
4223 p = (char_u *)"";
4224 rettv->vval.v_string = vim_strsave(p);
4225 }
4226 else
4227 {
4228 if (end < start)
4229 return;
4230
4231 if (start < 1)
4232 start = 1;
4233 if (end > buf->b_ml.ml_line_count)
4234 end = buf->b_ml.ml_line_count;
4235 while (start <= end)
4236 if (list_append_string(rettv->vval.v_list,
4237 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4238 break;
4239 }
4240}
4241
4242/*
4243 * Get the lnum from the first argument.
4244 * Also accepts "$", then "buf" is used.
4245 * Returns 0 on error.
4246 */
4247 static linenr_T
4248get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
4249{
4250 if (argvars[0].v_type == VAR_STRING
4251 && argvars[0].vval.v_string != NULL
4252 && argvars[0].vval.v_string[0] == '$'
4253 && buf != NULL)
4254 return buf->b_ml.ml_line_count;
4255 return (linenr_T)get_tv_number_chk(&argvars[0], NULL);
4256}
4257
4258/*
4259 * "getbufline()" function
4260 */
4261 static void
4262f_getbufline(typval_T *argvars, typval_T *rettv)
4263{
4264 linenr_T lnum;
4265 linenr_T end;
4266 buf_T *buf;
4267
4268 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4269 ++emsg_off;
4270 buf = get_buf_tv(&argvars[0], FALSE);
4271 --emsg_off;
4272
4273 lnum = get_tv_lnum_buf(&argvars[1], buf);
4274 if (argvars[2].v_type == VAR_UNKNOWN)
4275 end = lnum;
4276 else
4277 end = get_tv_lnum_buf(&argvars[2], buf);
4278
4279 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4280}
4281
4282/*
4283 * "getbufvar()" function
4284 */
4285 static void
4286f_getbufvar(typval_T *argvars, typval_T *rettv)
4287{
4288 buf_T *buf;
4289 buf_T *save_curbuf;
4290 char_u *varname;
4291 dictitem_T *v;
4292 int done = FALSE;
4293
4294 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4295 varname = get_tv_string_chk(&argvars[1]);
4296 ++emsg_off;
4297 buf = get_buf_tv(&argvars[0], FALSE);
4298
4299 rettv->v_type = VAR_STRING;
4300 rettv->vval.v_string = NULL;
4301
4302 if (buf != NULL && varname != NULL)
4303 {
4304 /* set curbuf to be our buf, temporarily */
4305 save_curbuf = curbuf;
4306 curbuf = buf;
4307
Bram Moolenaar30567352016-08-27 21:25:44 +02004308 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004309 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004310 if (varname[1] == NUL)
4311 {
4312 /* get all buffer-local options in a dict */
4313 dict_T *opts = get_winbuf_options(TRUE);
4314
4315 if (opts != NULL)
4316 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004317 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004318 done = TRUE;
4319 }
4320 }
4321 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4322 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004323 done = TRUE;
4324 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004325 else
4326 {
4327 /* Look up the variable. */
4328 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4329 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4330 'b', varname, FALSE);
4331 if (v != NULL)
4332 {
4333 copy_tv(&v->di_tv, rettv);
4334 done = TRUE;
4335 }
4336 }
4337
4338 /* restore previous notion of curbuf */
4339 curbuf = save_curbuf;
4340 }
4341
4342 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4343 /* use the default value */
4344 copy_tv(&argvars[2], rettv);
4345
4346 --emsg_off;
4347}
4348
4349/*
4350 * "getchar()" function
4351 */
4352 static void
4353f_getchar(typval_T *argvars, typval_T *rettv)
4354{
4355 varnumber_T n;
4356 int error = FALSE;
4357
4358 /* Position the cursor. Needed after a message that ends in a space. */
4359 windgoto(msg_row, msg_col);
4360
4361 ++no_mapping;
4362 ++allow_keys;
4363 for (;;)
4364 {
4365 if (argvars[0].v_type == VAR_UNKNOWN)
4366 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004367 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004368 else if (get_tv_number_chk(&argvars[0], &error) == 1)
4369 /* getchar(1): only check if char avail */
4370 n = vpeekc_any();
4371 else if (error || vpeekc_any() == NUL)
4372 /* illegal argument or getchar(0) and no char avail: return zero */
4373 n = 0;
4374 else
4375 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004376 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004377
4378 if (n == K_IGNORE)
4379 continue;
4380 break;
4381 }
4382 --no_mapping;
4383 --allow_keys;
4384
4385 set_vim_var_nr(VV_MOUSE_WIN, 0);
4386 set_vim_var_nr(VV_MOUSE_WINID, 0);
4387 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4388 set_vim_var_nr(VV_MOUSE_COL, 0);
4389
4390 rettv->vval.v_number = n;
4391 if (IS_SPECIAL(n) || mod_mask != 0)
4392 {
4393 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4394 int i = 0;
4395
4396 /* Turn a special key into three bytes, plus modifier. */
4397 if (mod_mask != 0)
4398 {
4399 temp[i++] = K_SPECIAL;
4400 temp[i++] = KS_MODIFIER;
4401 temp[i++] = mod_mask;
4402 }
4403 if (IS_SPECIAL(n))
4404 {
4405 temp[i++] = K_SPECIAL;
4406 temp[i++] = K_SECOND(n);
4407 temp[i++] = K_THIRD(n);
4408 }
4409#ifdef FEAT_MBYTE
4410 else if (has_mbyte)
4411 i += (*mb_char2bytes)(n, temp + i);
4412#endif
4413 else
4414 temp[i++] = n;
4415 temp[i++] = NUL;
4416 rettv->v_type = VAR_STRING;
4417 rettv->vval.v_string = vim_strsave(temp);
4418
4419#ifdef FEAT_MOUSE
4420 if (is_mouse_key(n))
4421 {
4422 int row = mouse_row;
4423 int col = mouse_col;
4424 win_T *win;
4425 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004426 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004427 int winnr = 1;
4428
4429 if (row >= 0 && col >= 0)
4430 {
4431 /* Find the window at the mouse coordinates and compute the
4432 * text position. */
4433 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004434 if (win == NULL)
4435 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004436 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004437 for (wp = firstwin; wp != win; wp = wp->w_next)
4438 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004439 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4440 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4441 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4442 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4443 }
4444 }
4445#endif
4446 }
4447}
4448
4449/*
4450 * "getcharmod()" function
4451 */
4452 static void
4453f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4454{
4455 rettv->vval.v_number = mod_mask;
4456}
4457
4458/*
4459 * "getcharsearch()" function
4460 */
4461 static void
4462f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4463{
4464 if (rettv_dict_alloc(rettv) != FAIL)
4465 {
4466 dict_T *dict = rettv->vval.v_dict;
4467
4468 dict_add_nr_str(dict, "char", 0L, last_csearch());
4469 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
4470 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
4471 }
4472}
4473
4474/*
4475 * "getcmdline()" function
4476 */
4477 static void
4478f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4479{
4480 rettv->v_type = VAR_STRING;
4481 rettv->vval.v_string = get_cmdline_str();
4482}
4483
4484/*
4485 * "getcmdpos()" function
4486 */
4487 static void
4488f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4489{
4490 rettv->vval.v_number = get_cmdline_pos() + 1;
4491}
4492
4493/*
4494 * "getcmdtype()" function
4495 */
4496 static void
4497f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4498{
4499 rettv->v_type = VAR_STRING;
4500 rettv->vval.v_string = alloc(2);
4501 if (rettv->vval.v_string != NULL)
4502 {
4503 rettv->vval.v_string[0] = get_cmdline_type();
4504 rettv->vval.v_string[1] = NUL;
4505 }
4506}
4507
4508/*
4509 * "getcmdwintype()" function
4510 */
4511 static void
4512f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4513{
4514 rettv->v_type = VAR_STRING;
4515 rettv->vval.v_string = NULL;
4516#ifdef FEAT_CMDWIN
4517 rettv->vval.v_string = alloc(2);
4518 if (rettv->vval.v_string != NULL)
4519 {
4520 rettv->vval.v_string[0] = cmdwin_type;
4521 rettv->vval.v_string[1] = NUL;
4522 }
4523#endif
4524}
4525
4526#if defined(FEAT_CMDL_COMPL)
4527/*
4528 * "getcompletion()" function
4529 */
4530 static void
4531f_getcompletion(typval_T *argvars, typval_T *rettv)
4532{
4533 char_u *pat;
4534 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004535 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004536 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
4537 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004538
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004539 if (argvars[2].v_type != VAR_UNKNOWN)
4540 filtered = get_tv_number_chk(&argvars[2], NULL);
4541
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004542 if (p_wic)
4543 options |= WILD_ICASE;
4544
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004545 /* For filtered results, 'wildignore' is used */
4546 if (!filtered)
4547 options |= WILD_KEEP_ALL;
4548
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004549 ExpandInit(&xpc);
4550 xpc.xp_pattern = get_tv_string(&argvars[0]);
4551 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4552 xpc.xp_context = cmdcomplete_str_to_type(get_tv_string(&argvars[1]));
4553 if (xpc.xp_context == EXPAND_NOTHING)
4554 {
4555 if (argvars[1].v_type == VAR_STRING)
4556 EMSG2(_(e_invarg2), argvars[1].vval.v_string);
4557 else
4558 EMSG(_(e_invarg));
4559 return;
4560 }
4561
4562# if defined(FEAT_MENU)
4563 if (xpc.xp_context == EXPAND_MENUS)
4564 {
4565 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4566 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4567 }
4568# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02004569#ifdef FEAT_CSCOPE
4570 if (xpc.xp_context == EXPAND_CSCOPE)
4571 {
4572 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4573 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4574 }
4575#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02004576#ifdef FEAT_SIGNS
4577 if (xpc.xp_context == EXPAND_SIGN)
4578 {
4579 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4580 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4581 }
4582#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004583
4584 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4585 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
4586 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004587 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004588
4589 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4590
4591 for (i = 0; i < xpc.xp_numfiles; i++)
4592 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4593 }
4594 vim_free(pat);
4595 ExpandCleanup(&xpc);
4596}
4597#endif
4598
4599/*
4600 * "getcwd()" function
4601 */
4602 static void
4603f_getcwd(typval_T *argvars, typval_T *rettv)
4604{
4605 win_T *wp = NULL;
4606 char_u *cwd;
4607
4608 rettv->v_type = VAR_STRING;
4609 rettv->vval.v_string = NULL;
4610
4611 wp = find_tabwin(&argvars[0], &argvars[1]);
4612 if (wp != NULL)
4613 {
4614 if (wp->w_localdir != NULL)
4615 rettv->vval.v_string = vim_strsave(wp->w_localdir);
4616 else if (globaldir != NULL)
4617 rettv->vval.v_string = vim_strsave(globaldir);
4618 else
4619 {
4620 cwd = alloc(MAXPATHL);
4621 if (cwd != NULL)
4622 {
4623 if (mch_dirname(cwd, MAXPATHL) != FAIL)
4624 rettv->vval.v_string = vim_strsave(cwd);
4625 vim_free(cwd);
4626 }
4627 }
4628#ifdef BACKSLASH_IN_FILENAME
4629 if (rettv->vval.v_string != NULL)
4630 slash_adjust(rettv->vval.v_string);
4631#endif
4632 }
4633}
4634
4635/*
4636 * "getfontname()" function
4637 */
4638 static void
4639f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4640{
4641 rettv->v_type = VAR_STRING;
4642 rettv->vval.v_string = NULL;
4643#ifdef FEAT_GUI
4644 if (gui.in_use)
4645 {
4646 GuiFont font;
4647 char_u *name = NULL;
4648
4649 if (argvars[0].v_type == VAR_UNKNOWN)
4650 {
4651 /* Get the "Normal" font. Either the name saved by
4652 * hl_set_font_name() or from the font ID. */
4653 font = gui.norm_font;
4654 name = hl_get_font_name();
4655 }
4656 else
4657 {
4658 name = get_tv_string(&argvars[0]);
4659 if (STRCMP(name, "*") == 0) /* don't use font dialog */
4660 return;
4661 font = gui_mch_get_font(name, FALSE);
4662 if (font == NOFONT)
4663 return; /* Invalid font name, return empty string. */
4664 }
4665 rettv->vval.v_string = gui_mch_get_fontname(font, name);
4666 if (argvars[0].v_type != VAR_UNKNOWN)
4667 gui_mch_free_font(font);
4668 }
4669#endif
4670}
4671
4672/*
4673 * "getfperm({fname})" function
4674 */
4675 static void
4676f_getfperm(typval_T *argvars, typval_T *rettv)
4677{
4678 char_u *fname;
4679 stat_T st;
4680 char_u *perm = NULL;
4681 char_u flags[] = "rwx";
4682 int i;
4683
4684 fname = get_tv_string(&argvars[0]);
4685
4686 rettv->v_type = VAR_STRING;
4687 if (mch_stat((char *)fname, &st) >= 0)
4688 {
4689 perm = vim_strsave((char_u *)"---------");
4690 if (perm != NULL)
4691 {
4692 for (i = 0; i < 9; i++)
4693 {
4694 if (st.st_mode & (1 << (8 - i)))
4695 perm[i] = flags[i % 3];
4696 }
4697 }
4698 }
4699 rettv->vval.v_string = perm;
4700}
4701
4702/*
4703 * "getfsize({fname})" function
4704 */
4705 static void
4706f_getfsize(typval_T *argvars, typval_T *rettv)
4707{
4708 char_u *fname;
4709 stat_T st;
4710
4711 fname = get_tv_string(&argvars[0]);
4712
4713 rettv->v_type = VAR_NUMBER;
4714
4715 if (mch_stat((char *)fname, &st) >= 0)
4716 {
4717 if (mch_isdir(fname))
4718 rettv->vval.v_number = 0;
4719 else
4720 {
4721 rettv->vval.v_number = (varnumber_T)st.st_size;
4722
4723 /* non-perfect check for overflow */
4724 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
4725 rettv->vval.v_number = -2;
4726 }
4727 }
4728 else
4729 rettv->vval.v_number = -1;
4730}
4731
4732/*
4733 * "getftime({fname})" function
4734 */
4735 static void
4736f_getftime(typval_T *argvars, typval_T *rettv)
4737{
4738 char_u *fname;
4739 stat_T st;
4740
4741 fname = get_tv_string(&argvars[0]);
4742
4743 if (mch_stat((char *)fname, &st) >= 0)
4744 rettv->vval.v_number = (varnumber_T)st.st_mtime;
4745 else
4746 rettv->vval.v_number = -1;
4747}
4748
4749/*
4750 * "getftype({fname})" function
4751 */
4752 static void
4753f_getftype(typval_T *argvars, typval_T *rettv)
4754{
4755 char_u *fname;
4756 stat_T st;
4757 char_u *type = NULL;
4758 char *t;
4759
4760 fname = get_tv_string(&argvars[0]);
4761
4762 rettv->v_type = VAR_STRING;
4763 if (mch_lstat((char *)fname, &st) >= 0)
4764 {
4765#ifdef S_ISREG
4766 if (S_ISREG(st.st_mode))
4767 t = "file";
4768 else if (S_ISDIR(st.st_mode))
4769 t = "dir";
4770# ifdef S_ISLNK
4771 else if (S_ISLNK(st.st_mode))
4772 t = "link";
4773# endif
4774# ifdef S_ISBLK
4775 else if (S_ISBLK(st.st_mode))
4776 t = "bdev";
4777# endif
4778# ifdef S_ISCHR
4779 else if (S_ISCHR(st.st_mode))
4780 t = "cdev";
4781# endif
4782# ifdef S_ISFIFO
4783 else if (S_ISFIFO(st.st_mode))
4784 t = "fifo";
4785# endif
4786# ifdef S_ISSOCK
4787 else if (S_ISSOCK(st.st_mode))
4788 t = "fifo";
4789# endif
4790 else
4791 t = "other";
4792#else
4793# ifdef S_IFMT
4794 switch (st.st_mode & S_IFMT)
4795 {
4796 case S_IFREG: t = "file"; break;
4797 case S_IFDIR: t = "dir"; break;
4798# ifdef S_IFLNK
4799 case S_IFLNK: t = "link"; break;
4800# endif
4801# ifdef S_IFBLK
4802 case S_IFBLK: t = "bdev"; break;
4803# endif
4804# ifdef S_IFCHR
4805 case S_IFCHR: t = "cdev"; break;
4806# endif
4807# ifdef S_IFIFO
4808 case S_IFIFO: t = "fifo"; break;
4809# endif
4810# ifdef S_IFSOCK
4811 case S_IFSOCK: t = "socket"; break;
4812# endif
4813 default: t = "other";
4814 }
4815# else
4816 if (mch_isdir(fname))
4817 t = "dir";
4818 else
4819 t = "file";
4820# endif
4821#endif
4822 type = vim_strsave((char_u *)t);
4823 }
4824 rettv->vval.v_string = type;
4825}
4826
4827/*
4828 * "getline(lnum, [end])" function
4829 */
4830 static void
4831f_getline(typval_T *argvars, typval_T *rettv)
4832{
4833 linenr_T lnum;
4834 linenr_T end;
4835 int retlist;
4836
4837 lnum = get_tv_lnum(argvars);
4838 if (argvars[1].v_type == VAR_UNKNOWN)
4839 {
4840 end = 0;
4841 retlist = FALSE;
4842 }
4843 else
4844 {
4845 end = get_tv_lnum(&argvars[1]);
4846 retlist = TRUE;
4847 }
4848
4849 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
4850}
4851
Bram Moolenaar4ae20952016-08-13 15:29:14 +02004852#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02004853 static void
4854get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
4855{
Bram Moolenaard823fa92016-08-12 16:29:27 +02004856 if (what_arg->v_type == VAR_UNKNOWN)
4857 {
4858 if (rettv_list_alloc(rettv) == OK)
4859 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004860 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004861 }
4862 else
4863 {
4864 if (rettv_dict_alloc(rettv) == OK)
4865 if (is_qf || (wp != NULL))
4866 {
4867 if (what_arg->v_type == VAR_DICT)
4868 {
4869 dict_T *d = what_arg->vval.v_dict;
4870
4871 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02004872 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004873 }
4874 else
4875 EMSG(_(e_dictreq));
4876 }
4877 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004878}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02004879#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02004880
4881/*
4882 * "getloclist()" function
4883 */
4884 static void
4885f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4886{
4887#ifdef FEAT_QUICKFIX
4888 win_T *wp;
4889
4890 wp = find_win_by_nr(&argvars[0], NULL);
4891 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
4892#endif
4893}
4894
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004895/*
4896 * "getmatches()" function
4897 */
4898 static void
4899f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4900{
4901#ifdef FEAT_SEARCH_EXTRA
4902 dict_T *dict;
4903 matchitem_T *cur = curwin->w_match_head;
4904 int i;
4905
4906 if (rettv_list_alloc(rettv) == OK)
4907 {
4908 while (cur != NULL)
4909 {
4910 dict = dict_alloc();
4911 if (dict == NULL)
4912 return;
4913 if (cur->match.regprog == NULL)
4914 {
4915 /* match added with matchaddpos() */
4916 for (i = 0; i < MAXPOSMATCH; ++i)
4917 {
4918 llpos_T *llpos;
4919 char buf[6];
4920 list_T *l;
4921
4922 llpos = &cur->pos.pos[i];
4923 if (llpos->lnum == 0)
4924 break;
4925 l = list_alloc();
4926 if (l == NULL)
4927 break;
4928 list_append_number(l, (varnumber_T)llpos->lnum);
4929 if (llpos->col > 0)
4930 {
4931 list_append_number(l, (varnumber_T)llpos->col);
4932 list_append_number(l, (varnumber_T)llpos->len);
4933 }
4934 sprintf(buf, "pos%d", i + 1);
4935 dict_add_list(dict, buf, l);
4936 }
4937 }
4938 else
4939 {
4940 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
4941 }
4942 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
4943 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
4944 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
4945# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
4946 if (cur->conceal_char)
4947 {
4948 char_u buf[MB_MAXBYTES + 1];
4949
4950 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
4951 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
4952 }
4953# endif
4954 list_append_dict(rettv->vval.v_list, dict);
4955 cur = cur->next;
4956 }
4957 }
4958#endif
4959}
4960
4961/*
4962 * "getpid()" function
4963 */
4964 static void
4965f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
4966{
4967 rettv->vval.v_number = mch_get_pid();
4968}
4969
4970 static void
4971getpos_both(
4972 typval_T *argvars,
4973 typval_T *rettv,
4974 int getcurpos)
4975{
4976 pos_T *fp;
4977 list_T *l;
4978 int fnum = -1;
4979
4980 if (rettv_list_alloc(rettv) == OK)
4981 {
4982 l = rettv->vval.v_list;
4983 if (getcurpos)
4984 fp = &curwin->w_cursor;
4985 else
4986 fp = var2fpos(&argvars[0], TRUE, &fnum);
4987 if (fnum != -1)
4988 list_append_number(l, (varnumber_T)fnum);
4989 else
4990 list_append_number(l, (varnumber_T)0);
4991 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
4992 : (varnumber_T)0);
4993 list_append_number(l, (fp != NULL)
4994 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
4995 : (varnumber_T)0);
4996 list_append_number(l,
4997#ifdef FEAT_VIRTUALEDIT
4998 (fp != NULL) ? (varnumber_T)fp->coladd :
4999#endif
5000 (varnumber_T)0);
5001 if (getcurpos)
5002 {
5003 update_curswant();
5004 list_append_number(l, curwin->w_curswant == MAXCOL ?
5005 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
5006 }
5007 }
5008 else
5009 rettv->vval.v_number = FALSE;
5010}
5011
5012
5013/*
5014 * "getcurpos()" function
5015 */
5016 static void
5017f_getcurpos(typval_T *argvars, typval_T *rettv)
5018{
5019 getpos_both(argvars, rettv, TRUE);
5020}
5021
5022/*
5023 * "getpos(string)" function
5024 */
5025 static void
5026f_getpos(typval_T *argvars, typval_T *rettv)
5027{
5028 getpos_both(argvars, rettv, FALSE);
5029}
5030
5031/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005032 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005033 */
5034 static void
5035f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5036{
5037#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005038 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005039#endif
5040}
5041
5042/*
5043 * "getreg()" function
5044 */
5045 static void
5046f_getreg(typval_T *argvars, typval_T *rettv)
5047{
5048 char_u *strregname;
5049 int regname;
5050 int arg2 = FALSE;
5051 int return_list = FALSE;
5052 int error = FALSE;
5053
5054 if (argvars[0].v_type != VAR_UNKNOWN)
5055 {
5056 strregname = get_tv_string_chk(&argvars[0]);
5057 error = strregname == NULL;
5058 if (argvars[1].v_type != VAR_UNKNOWN)
5059 {
5060 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
5061 if (!error && argvars[2].v_type != VAR_UNKNOWN)
5062 return_list = (int)get_tv_number_chk(&argvars[2], &error);
5063 }
5064 }
5065 else
5066 strregname = get_vim_var_str(VV_REG);
5067
5068 if (error)
5069 return;
5070
5071 regname = (strregname == NULL ? '"' : *strregname);
5072 if (regname == 0)
5073 regname = '"';
5074
5075 if (return_list)
5076 {
5077 rettv->v_type = VAR_LIST;
5078 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5079 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5080 if (rettv->vval.v_list == NULL)
5081 (void)rettv_list_alloc(rettv);
5082 else
5083 ++rettv->vval.v_list->lv_refcount;
5084 }
5085 else
5086 {
5087 rettv->v_type = VAR_STRING;
5088 rettv->vval.v_string = get_reg_contents(regname,
5089 arg2 ? GREG_EXPR_SRC : 0);
5090 }
5091}
5092
5093/*
5094 * "getregtype()" function
5095 */
5096 static void
5097f_getregtype(typval_T *argvars, typval_T *rettv)
5098{
5099 char_u *strregname;
5100 int regname;
5101 char_u buf[NUMBUFLEN + 2];
5102 long reglen = 0;
5103
5104 if (argvars[0].v_type != VAR_UNKNOWN)
5105 {
5106 strregname = get_tv_string_chk(&argvars[0]);
5107 if (strregname == NULL) /* type error; errmsg already given */
5108 {
5109 rettv->v_type = VAR_STRING;
5110 rettv->vval.v_string = NULL;
5111 return;
5112 }
5113 }
5114 else
5115 /* Default to v:register */
5116 strregname = get_vim_var_str(VV_REG);
5117
5118 regname = (strregname == NULL ? '"' : *strregname);
5119 if (regname == 0)
5120 regname = '"';
5121
5122 buf[0] = NUL;
5123 buf[1] = NUL;
5124 switch (get_reg_type(regname, &reglen))
5125 {
5126 case MLINE: buf[0] = 'V'; break;
5127 case MCHAR: buf[0] = 'v'; break;
5128 case MBLOCK:
5129 buf[0] = Ctrl_V;
5130 sprintf((char *)buf + 1, "%ld", reglen + 1);
5131 break;
5132 }
5133 rettv->v_type = VAR_STRING;
5134 rettv->vval.v_string = vim_strsave(buf);
5135}
5136
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005137/*
5138 * Returns information (variables, options, etc.) about a tab page
5139 * as a dictionary.
5140 */
5141 static dict_T *
5142get_tabpage_info(tabpage_T *tp, int tp_idx)
5143{
5144 win_T *wp;
5145 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005146 list_T *l;
5147
5148 dict = dict_alloc();
5149 if (dict == NULL)
5150 return NULL;
5151
Bram Moolenaar33928832016-08-18 21:22:04 +02005152 dict_add_nr_str(dict, "tabnr", tp_idx, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005153
5154 l = list_alloc();
5155 if (l != NULL)
5156 {
5157 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5158 wp; wp = wp->w_next)
5159 list_append_number(l, (varnumber_T)wp->w_id);
5160 dict_add_list(dict, "windows", l);
5161 }
5162
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005163 /* Make a reference to tabpage variables */
5164 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005165
5166 return dict;
5167}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005168
5169/*
5170 * "gettabinfo()" function
5171 */
5172 static void
5173f_gettabinfo(typval_T *argvars, typval_T *rettv)
5174{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005175 tabpage_T *tp, *tparg = NULL;
5176 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005177 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005178
5179 if (rettv_list_alloc(rettv) != OK)
5180 return;
5181
5182 if (argvars[0].v_type != VAR_UNKNOWN)
5183 {
5184 /* Information about one tab page */
5185 tparg = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5186 if (tparg == NULL)
5187 return;
5188 }
5189
5190 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005191 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005192 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005193 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005194 if (tparg != NULL && tp != tparg)
5195 continue;
5196 d = get_tabpage_info(tp, tpnr);
5197 if (d != NULL)
5198 list_append_dict(rettv->vval.v_list, d);
5199 if (tparg != NULL)
5200 return;
5201 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005202}
5203
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005204/*
5205 * "gettabvar()" function
5206 */
5207 static void
5208f_gettabvar(typval_T *argvars, typval_T *rettv)
5209{
5210 win_T *oldcurwin;
5211 tabpage_T *tp, *oldtabpage;
5212 dictitem_T *v;
5213 char_u *varname;
5214 int done = FALSE;
5215
5216 rettv->v_type = VAR_STRING;
5217 rettv->vval.v_string = NULL;
5218
5219 varname = get_tv_string_chk(&argvars[1]);
5220 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5221 if (tp != NULL && varname != NULL)
5222 {
5223 /* Set tp to be our tabpage, temporarily. Also set the window to the
5224 * first window in the tabpage, otherwise the window is not valid. */
5225 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005226 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5227 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005228 {
5229 /* look up the variable */
5230 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5231 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5232 if (v != NULL)
5233 {
5234 copy_tv(&v->di_tv, rettv);
5235 done = TRUE;
5236 }
5237 }
5238
5239 /* restore previous notion of curwin */
5240 restore_win(oldcurwin, oldtabpage, TRUE);
5241 }
5242
5243 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5244 copy_tv(&argvars[2], rettv);
5245}
5246
5247/*
5248 * "gettabwinvar()" function
5249 */
5250 static void
5251f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5252{
5253 getwinvar(argvars, rettv, 1);
5254}
5255
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005256/*
5257 * Returns information about a window as a dictionary.
5258 */
5259 static dict_T *
5260get_win_info(win_T *wp, short tpnr, short winnr)
5261{
5262 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005263
5264 dict = dict_alloc();
5265 if (dict == NULL)
5266 return NULL;
5267
Bram Moolenaar33928832016-08-18 21:22:04 +02005268 dict_add_nr_str(dict, "tabnr", tpnr, NULL);
5269 dict_add_nr_str(dict, "winnr", winnr, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005270 dict_add_nr_str(dict, "winid", wp->w_id, NULL);
5271 dict_add_nr_str(dict, "height", wp->w_height, NULL);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005272#ifdef FEAT_MENU
5273 dict_add_nr_str(dict, "winbar", wp->w_winbar_height, NULL);
5274#endif
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005275 dict_add_nr_str(dict, "width", wp->w_width, NULL);
Bram Moolenaar33928832016-08-18 21:22:04 +02005276 dict_add_nr_str(dict, "bufnr", wp->w_buffer->b_fnum, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005277
Bram Moolenaar69905d12017-08-13 18:14:47 +02005278#ifdef FEAT_TERMINAL
5279 dict_add_nr_str(dict, "terminal", bt_terminal(wp->w_buffer), NULL);
5280#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005281#ifdef FEAT_QUICKFIX
5282 dict_add_nr_str(dict, "quickfix", bt_quickfix(wp->w_buffer), NULL);
5283 dict_add_nr_str(dict, "loclist",
5284 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL), NULL);
5285#endif
5286
Bram Moolenaar30567352016-08-27 21:25:44 +02005287 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005288 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005289
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005290 return dict;
5291}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005292
5293/*
5294 * "getwininfo()" function
5295 */
5296 static void
5297f_getwininfo(typval_T *argvars, typval_T *rettv)
5298{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005299 tabpage_T *tp;
5300 win_T *wp = NULL, *wparg = NULL;
5301 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005302 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005303
5304 if (rettv_list_alloc(rettv) != OK)
5305 return;
5306
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005307 if (argvars[0].v_type != VAR_UNKNOWN)
5308 {
5309 wparg = win_id2wp(argvars);
5310 if (wparg == NULL)
5311 return;
5312 }
5313
5314 /* Collect information about either all the windows across all the tab
5315 * pages or one particular window.
5316 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005317 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005318 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005319 tabnr++;
5320 winnr = 0;
5321 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005322 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005323 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005324 if (wparg != NULL && wp != wparg)
5325 continue;
5326 d = get_win_info(wp, tabnr, winnr);
5327 if (d != NULL)
5328 list_append_dict(rettv->vval.v_list, d);
5329 if (wparg != NULL)
5330 /* found information about a specific window */
5331 return;
5332 }
5333 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005334}
5335
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005336/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005337 * "win_findbuf()" function
5338 */
5339 static void
5340f_win_findbuf(typval_T *argvars, typval_T *rettv)
5341{
5342 if (rettv_list_alloc(rettv) != FAIL)
5343 win_findbuf(argvars, rettv->vval.v_list);
5344}
5345
5346/*
5347 * "win_getid()" function
5348 */
5349 static void
5350f_win_getid(typval_T *argvars, typval_T *rettv)
5351{
5352 rettv->vval.v_number = win_getid(argvars);
5353}
5354
5355/*
5356 * "win_gotoid()" function
5357 */
5358 static void
5359f_win_gotoid(typval_T *argvars, typval_T *rettv)
5360{
5361 rettv->vval.v_number = win_gotoid(argvars);
5362}
5363
5364/*
5365 * "win_id2tabwin()" function
5366 */
5367 static void
5368f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5369{
5370 if (rettv_list_alloc(rettv) != FAIL)
5371 win_id2tabwin(argvars, rettv->vval.v_list);
5372}
5373
5374/*
5375 * "win_id2win()" function
5376 */
5377 static void
5378f_win_id2win(typval_T *argvars, typval_T *rettv)
5379{
5380 rettv->vval.v_number = win_id2win(argvars);
5381}
5382
5383/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005384 * "win_screenpos()" function
5385 */
5386 static void
5387f_win_screenpos(typval_T *argvars, typval_T *rettv)
5388{
5389 win_T *wp;
5390
5391 if (rettv_list_alloc(rettv) == FAIL)
5392 return;
5393
5394 wp = find_win_by_nr(&argvars[0], NULL);
5395 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5396 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5397}
5398
5399/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005400 * "getwinposx()" function
5401 */
5402 static void
5403f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5404{
5405 rettv->vval.v_number = -1;
5406#ifdef FEAT_GUI
5407 if (gui.in_use)
5408 {
5409 int x, y;
5410
5411 if (gui_mch_get_winpos(&x, &y) == OK)
5412 rettv->vval.v_number = x;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005413 return;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005414 }
5415#endif
5416#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5417 {
5418 int x, y;
5419
5420 if (term_get_winpos(&x, &y) == OK)
5421 rettv->vval.v_number = x;
5422 }
5423#endif
5424}
5425
5426/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005427 * "getwinposy()" function
5428 */
5429 static void
5430f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5431{
5432 rettv->vval.v_number = -1;
5433#ifdef FEAT_GUI
5434 if (gui.in_use)
5435 {
5436 int x, y;
5437
5438 if (gui_mch_get_winpos(&x, &y) == OK)
5439 rettv->vval.v_number = y;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005440 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005441 }
5442#endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005443#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5444 {
5445 int x, y;
5446
5447 if (term_get_winpos(&x, &y) == OK)
5448 rettv->vval.v_number = y;
5449 }
5450#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005451}
5452
5453/*
5454 * "getwinvar()" function
5455 */
5456 static void
5457f_getwinvar(typval_T *argvars, typval_T *rettv)
5458{
5459 getwinvar(argvars, rettv, 0);
5460}
5461
5462/*
5463 * "glob()" function
5464 */
5465 static void
5466f_glob(typval_T *argvars, typval_T *rettv)
5467{
5468 int options = WILD_SILENT|WILD_USE_NL;
5469 expand_T xpc;
5470 int error = FALSE;
5471
5472 /* When the optional second argument is non-zero, don't remove matches
5473 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5474 rettv->v_type = VAR_STRING;
5475 if (argvars[1].v_type != VAR_UNKNOWN)
5476 {
5477 if (get_tv_number_chk(&argvars[1], &error))
5478 options |= WILD_KEEP_ALL;
5479 if (argvars[2].v_type != VAR_UNKNOWN)
5480 {
5481 if (get_tv_number_chk(&argvars[2], &error))
5482 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005483 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005484 }
5485 if (argvars[3].v_type != VAR_UNKNOWN
5486 && get_tv_number_chk(&argvars[3], &error))
5487 options |= WILD_ALLLINKS;
5488 }
5489 }
5490 if (!error)
5491 {
5492 ExpandInit(&xpc);
5493 xpc.xp_context = EXPAND_FILES;
5494 if (p_wic)
5495 options += WILD_ICASE;
5496 if (rettv->v_type == VAR_STRING)
5497 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
5498 NULL, options, WILD_ALL);
5499 else if (rettv_list_alloc(rettv) != FAIL)
5500 {
5501 int i;
5502
5503 ExpandOne(&xpc, get_tv_string(&argvars[0]),
5504 NULL, options, WILD_ALL_KEEP);
5505 for (i = 0; i < xpc.xp_numfiles; i++)
5506 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5507
5508 ExpandCleanup(&xpc);
5509 }
5510 }
5511 else
5512 rettv->vval.v_string = NULL;
5513}
5514
5515/*
5516 * "globpath()" function
5517 */
5518 static void
5519f_globpath(typval_T *argvars, typval_T *rettv)
5520{
5521 int flags = 0;
5522 char_u buf1[NUMBUFLEN];
5523 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
5524 int error = FALSE;
5525 garray_T ga;
5526 int i;
5527
5528 /* When the optional second argument is non-zero, don't remove matches
5529 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5530 rettv->v_type = VAR_STRING;
5531 if (argvars[2].v_type != VAR_UNKNOWN)
5532 {
5533 if (get_tv_number_chk(&argvars[2], &error))
5534 flags |= WILD_KEEP_ALL;
5535 if (argvars[3].v_type != VAR_UNKNOWN)
5536 {
5537 if (get_tv_number_chk(&argvars[3], &error))
5538 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005539 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005540 }
5541 if (argvars[4].v_type != VAR_UNKNOWN
5542 && get_tv_number_chk(&argvars[4], &error))
5543 flags |= WILD_ALLLINKS;
5544 }
5545 }
5546 if (file != NULL && !error)
5547 {
5548 ga_init2(&ga, (int)sizeof(char_u *), 10);
5549 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
5550 if (rettv->v_type == VAR_STRING)
5551 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
5552 else if (rettv_list_alloc(rettv) != FAIL)
5553 for (i = 0; i < ga.ga_len; ++i)
5554 list_append_string(rettv->vval.v_list,
5555 ((char_u **)(ga.ga_data))[i], -1);
5556 ga_clear_strings(&ga);
5557 }
5558 else
5559 rettv->vval.v_string = NULL;
5560}
5561
5562/*
5563 * "glob2regpat()" function
5564 */
5565 static void
5566f_glob2regpat(typval_T *argvars, typval_T *rettv)
5567{
5568 char_u *pat = get_tv_string_chk(&argvars[0]);
5569
5570 rettv->v_type = VAR_STRING;
5571 rettv->vval.v_string = (pat == NULL)
5572 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
5573}
5574
5575/* for VIM_VERSION_ defines */
5576#include "version.h"
5577
5578/*
5579 * "has()" function
5580 */
5581 static void
5582f_has(typval_T *argvars, typval_T *rettv)
5583{
5584 int i;
5585 char_u *name;
5586 int n = FALSE;
5587 static char *(has_list[]) =
5588 {
5589#ifdef AMIGA
5590 "amiga",
5591# ifdef FEAT_ARP
5592 "arp",
5593# endif
5594#endif
5595#ifdef __BEOS__
5596 "beos",
5597#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02005598#ifdef MACOS_X
5599 "mac", /* Mac OS X (and, once, Mac OS Classic) */
5600 "osx", /* Mac OS X */
5601# ifdef MACOS_X_DARWIN
5602 "macunix", /* Mac OS X, with the darwin feature */
5603 "osxdarwin", /* synonym for macunix */
5604# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005605#endif
5606#ifdef __QNX__
5607 "qnx",
5608#endif
5609#ifdef UNIX
5610 "unix",
5611#endif
5612#ifdef VMS
5613 "vms",
5614#endif
5615#ifdef WIN32
5616 "win32",
5617#endif
5618#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
5619 "win32unix",
5620#endif
5621#if defined(WIN64) || defined(_WIN64)
5622 "win64",
5623#endif
5624#ifdef EBCDIC
5625 "ebcdic",
5626#endif
5627#ifndef CASE_INSENSITIVE_FILENAME
5628 "fname_case",
5629#endif
5630#ifdef HAVE_ACL
5631 "acl",
5632#endif
5633#ifdef FEAT_ARABIC
5634 "arabic",
5635#endif
5636#ifdef FEAT_AUTOCMD
5637 "autocmd",
5638#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01005639#ifdef FEAT_AUTOSERVERNAME
5640 "autoservername",
5641#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01005642#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005643 "balloon_eval",
5644# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
5645 "balloon_multiline",
5646# endif
5647#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01005648#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01005649 "balloon_eval_term",
5650#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005651#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
5652 "builtin_terms",
5653# ifdef ALL_BUILTIN_TCAPS
5654 "all_builtin_terms",
5655# endif
5656#endif
5657#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
5658 || defined(FEAT_GUI_W32) \
5659 || defined(FEAT_GUI_MOTIF))
5660 "browsefilter",
5661#endif
5662#ifdef FEAT_BYTEOFF
5663 "byte_offset",
5664#endif
5665#ifdef FEAT_JOB_CHANNEL
5666 "channel",
5667#endif
5668#ifdef FEAT_CINDENT
5669 "cindent",
5670#endif
5671#ifdef FEAT_CLIENTSERVER
5672 "clientserver",
5673#endif
5674#ifdef FEAT_CLIPBOARD
5675 "clipboard",
5676#endif
5677#ifdef FEAT_CMDL_COMPL
5678 "cmdline_compl",
5679#endif
5680#ifdef FEAT_CMDHIST
5681 "cmdline_hist",
5682#endif
5683#ifdef FEAT_COMMENTS
5684 "comments",
5685#endif
5686#ifdef FEAT_CONCEAL
5687 "conceal",
5688#endif
5689#ifdef FEAT_CRYPT
5690 "cryptv",
5691 "crypt-blowfish",
5692 "crypt-blowfish2",
5693#endif
5694#ifdef FEAT_CSCOPE
5695 "cscope",
5696#endif
5697#ifdef FEAT_CURSORBIND
5698 "cursorbind",
5699#endif
5700#ifdef CURSOR_SHAPE
5701 "cursorshape",
5702#endif
5703#ifdef DEBUG
5704 "debug",
5705#endif
5706#ifdef FEAT_CON_DIALOG
5707 "dialog_con",
5708#endif
5709#ifdef FEAT_GUI_DIALOG
5710 "dialog_gui",
5711#endif
5712#ifdef FEAT_DIFF
5713 "diff",
5714#endif
5715#ifdef FEAT_DIGRAPHS
5716 "digraphs",
5717#endif
5718#ifdef FEAT_DIRECTX
5719 "directx",
5720#endif
5721#ifdef FEAT_DND
5722 "dnd",
5723#endif
5724#ifdef FEAT_EMACS_TAGS
5725 "emacs_tags",
5726#endif
5727 "eval", /* always present, of course! */
5728 "ex_extra", /* graduated feature */
5729#ifdef FEAT_SEARCH_EXTRA
5730 "extra_search",
5731#endif
5732#ifdef FEAT_FKMAP
5733 "farsi",
5734#endif
5735#ifdef FEAT_SEARCHPATH
5736 "file_in_path",
5737#endif
5738#ifdef FEAT_FILTERPIPE
5739 "filterpipe",
5740#endif
5741#ifdef FEAT_FIND_ID
5742 "find_in_path",
5743#endif
5744#ifdef FEAT_FLOAT
5745 "float",
5746#endif
5747#ifdef FEAT_FOLDING
5748 "folding",
5749#endif
5750#ifdef FEAT_FOOTER
5751 "footer",
5752#endif
5753#if !defined(USE_SYSTEM) && defined(UNIX)
5754 "fork",
5755#endif
5756#ifdef FEAT_GETTEXT
5757 "gettext",
5758#endif
5759#ifdef FEAT_GUI
5760 "gui",
5761#endif
5762#ifdef FEAT_GUI_ATHENA
5763# ifdef FEAT_GUI_NEXTAW
5764 "gui_neXtaw",
5765# else
5766 "gui_athena",
5767# endif
5768#endif
5769#ifdef FEAT_GUI_GTK
5770 "gui_gtk",
5771# ifdef USE_GTK3
5772 "gui_gtk3",
5773# else
5774 "gui_gtk2",
5775# endif
5776#endif
5777#ifdef FEAT_GUI_GNOME
5778 "gui_gnome",
5779#endif
5780#ifdef FEAT_GUI_MAC
5781 "gui_mac",
5782#endif
5783#ifdef FEAT_GUI_MOTIF
5784 "gui_motif",
5785#endif
5786#ifdef FEAT_GUI_PHOTON
5787 "gui_photon",
5788#endif
5789#ifdef FEAT_GUI_W32
5790 "gui_win32",
5791#endif
5792#ifdef FEAT_HANGULIN
5793 "hangul_input",
5794#endif
5795#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
5796 "iconv",
5797#endif
5798#ifdef FEAT_INS_EXPAND
5799 "insert_expand",
5800#endif
5801#ifdef FEAT_JOB_CHANNEL
5802 "job",
5803#endif
5804#ifdef FEAT_JUMPLIST
5805 "jumplist",
5806#endif
5807#ifdef FEAT_KEYMAP
5808 "keymap",
5809#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02005810 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005811#ifdef FEAT_LANGMAP
5812 "langmap",
5813#endif
5814#ifdef FEAT_LIBCALL
5815 "libcall",
5816#endif
5817#ifdef FEAT_LINEBREAK
5818 "linebreak",
5819#endif
5820#ifdef FEAT_LISP
5821 "lispindent",
5822#endif
5823#ifdef FEAT_LISTCMDS
5824 "listcmds",
5825#endif
5826#ifdef FEAT_LOCALMAP
5827 "localmap",
5828#endif
5829#ifdef FEAT_LUA
5830# ifndef DYNAMIC_LUA
5831 "lua",
5832# endif
5833#endif
5834#ifdef FEAT_MENU
5835 "menu",
5836#endif
5837#ifdef FEAT_SESSION
5838 "mksession",
5839#endif
5840#ifdef FEAT_MODIFY_FNAME
5841 "modify_fname",
5842#endif
5843#ifdef FEAT_MOUSE
5844 "mouse",
5845#endif
5846#ifdef FEAT_MOUSESHAPE
5847 "mouseshape",
5848#endif
5849#if defined(UNIX) || defined(VMS)
5850# ifdef FEAT_MOUSE_DEC
5851 "mouse_dec",
5852# endif
5853# ifdef FEAT_MOUSE_GPM
5854 "mouse_gpm",
5855# endif
5856# ifdef FEAT_MOUSE_JSB
5857 "mouse_jsbterm",
5858# endif
5859# ifdef FEAT_MOUSE_NET
5860 "mouse_netterm",
5861# endif
5862# ifdef FEAT_MOUSE_PTERM
5863 "mouse_pterm",
5864# endif
5865# ifdef FEAT_MOUSE_SGR
5866 "mouse_sgr",
5867# endif
5868# ifdef FEAT_SYSMOUSE
5869 "mouse_sysmouse",
5870# endif
5871# ifdef FEAT_MOUSE_URXVT
5872 "mouse_urxvt",
5873# endif
5874# ifdef FEAT_MOUSE_XTERM
5875 "mouse_xterm",
5876# endif
5877#endif
5878#ifdef FEAT_MBYTE
5879 "multi_byte",
5880#endif
5881#ifdef FEAT_MBYTE_IME
5882 "multi_byte_ime",
5883#endif
5884#ifdef FEAT_MULTI_LANG
5885 "multi_lang",
5886#endif
5887#ifdef FEAT_MZSCHEME
5888#ifndef DYNAMIC_MZSCHEME
5889 "mzscheme",
5890#endif
5891#endif
5892#ifdef FEAT_NUM64
5893 "num64",
5894#endif
5895#ifdef FEAT_OLE
5896 "ole",
5897#endif
5898 "packages",
5899#ifdef FEAT_PATH_EXTRA
5900 "path_extra",
5901#endif
5902#ifdef FEAT_PERL
5903#ifndef DYNAMIC_PERL
5904 "perl",
5905#endif
5906#endif
5907#ifdef FEAT_PERSISTENT_UNDO
5908 "persistent_undo",
5909#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005910#if defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005911 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005912 "pythonx",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005913#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005914#if defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005915 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005916 "pythonx",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005917#endif
5918#ifdef FEAT_POSTSCRIPT
5919 "postscript",
5920#endif
5921#ifdef FEAT_PRINTER
5922 "printer",
5923#endif
5924#ifdef FEAT_PROFILE
5925 "profile",
5926#endif
5927#ifdef FEAT_RELTIME
5928 "reltime",
5929#endif
5930#ifdef FEAT_QUICKFIX
5931 "quickfix",
5932#endif
5933#ifdef FEAT_RIGHTLEFT
5934 "rightleft",
5935#endif
5936#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
5937 "ruby",
5938#endif
5939#ifdef FEAT_SCROLLBIND
5940 "scrollbind",
5941#endif
5942#ifdef FEAT_CMDL_INFO
5943 "showcmd",
5944 "cmdline_info",
5945#endif
5946#ifdef FEAT_SIGNS
5947 "signs",
5948#endif
5949#ifdef FEAT_SMARTINDENT
5950 "smartindent",
5951#endif
5952#ifdef STARTUPTIME
5953 "startuptime",
5954#endif
5955#ifdef FEAT_STL_OPT
5956 "statusline",
5957#endif
5958#ifdef FEAT_SUN_WORKSHOP
5959 "sun_workshop",
5960#endif
5961#ifdef FEAT_NETBEANS_INTG
5962 "netbeans_intg",
5963#endif
5964#ifdef FEAT_SPELL
5965 "spell",
5966#endif
5967#ifdef FEAT_SYN_HL
5968 "syntax",
5969#endif
5970#if defined(USE_SYSTEM) || !defined(UNIX)
5971 "system",
5972#endif
5973#ifdef FEAT_TAG_BINS
5974 "tag_binary",
5975#endif
5976#ifdef FEAT_TAG_OLDSTATIC
5977 "tag_old_static",
5978#endif
5979#ifdef FEAT_TAG_ANYWHITE
5980 "tag_any_white",
5981#endif
5982#ifdef FEAT_TCL
5983# ifndef DYNAMIC_TCL
5984 "tcl",
5985# endif
5986#endif
5987#ifdef FEAT_TERMGUICOLORS
5988 "termguicolors",
5989#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02005990#if defined(FEAT_TERMINAL) && !defined(WIN3264)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005991 "terminal",
5992#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005993#ifdef TERMINFO
5994 "terminfo",
5995#endif
5996#ifdef FEAT_TERMRESPONSE
5997 "termresponse",
5998#endif
5999#ifdef FEAT_TEXTOBJ
6000 "textobjects",
6001#endif
6002#ifdef HAVE_TGETENT
6003 "tgetent",
6004#endif
6005#ifdef FEAT_TIMERS
6006 "timers",
6007#endif
6008#ifdef FEAT_TITLE
6009 "title",
6010#endif
6011#ifdef FEAT_TOOLBAR
6012 "toolbar",
6013#endif
6014#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6015 "unnamedplus",
6016#endif
6017#ifdef FEAT_USR_CMDS
6018 "user-commands", /* was accidentally included in 5.4 */
6019 "user_commands",
6020#endif
6021#ifdef FEAT_VIMINFO
6022 "viminfo",
6023#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006024 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006025#ifdef FEAT_VIRTUALEDIT
6026 "virtualedit",
6027#endif
6028 "visual",
6029#ifdef FEAT_VISUALEXTRA
6030 "visualextra",
6031#endif
6032#ifdef FEAT_VREPLACE
6033 "vreplace",
6034#endif
6035#ifdef FEAT_WILDIGN
6036 "wildignore",
6037#endif
6038#ifdef FEAT_WILDMENU
6039 "wildmenu",
6040#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006041 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006042#ifdef FEAT_WAK
6043 "winaltkeys",
6044#endif
6045#ifdef FEAT_WRITEBACKUP
6046 "writebackup",
6047#endif
6048#ifdef FEAT_XIM
6049 "xim",
6050#endif
6051#ifdef FEAT_XFONTSET
6052 "xfontset",
6053#endif
6054#ifdef FEAT_XPM_W32
6055 "xpm",
6056 "xpm_w32", /* for backward compatibility */
6057#else
6058# if defined(HAVE_XPM)
6059 "xpm",
6060# endif
6061#endif
6062#ifdef USE_XSMP
6063 "xsmp",
6064#endif
6065#ifdef USE_XSMP_INTERACT
6066 "xsmp_interact",
6067#endif
6068#ifdef FEAT_XCLIPBOARD
6069 "xterm_clipboard",
6070#endif
6071#ifdef FEAT_XTERM_SAVE
6072 "xterm_save",
6073#endif
6074#if defined(UNIX) && defined(FEAT_X11)
6075 "X11",
6076#endif
6077 NULL
6078 };
6079
6080 name = get_tv_string(&argvars[0]);
6081 for (i = 0; has_list[i] != NULL; ++i)
6082 if (STRICMP(name, has_list[i]) == 0)
6083 {
6084 n = TRUE;
6085 break;
6086 }
6087
6088 if (n == FALSE)
6089 {
6090 if (STRNICMP(name, "patch", 5) == 0)
6091 {
6092 if (name[5] == '-'
6093 && STRLEN(name) >= 11
6094 && vim_isdigit(name[6])
6095 && vim_isdigit(name[8])
6096 && vim_isdigit(name[10]))
6097 {
6098 int major = atoi((char *)name + 6);
6099 int minor = atoi((char *)name + 8);
6100
6101 /* Expect "patch-9.9.01234". */
6102 n = (major < VIM_VERSION_MAJOR
6103 || (major == VIM_VERSION_MAJOR
6104 && (minor < VIM_VERSION_MINOR
6105 || (minor == VIM_VERSION_MINOR
6106 && has_patch(atoi((char *)name + 10))))));
6107 }
6108 else
6109 n = has_patch(atoi((char *)name + 5));
6110 }
6111 else if (STRICMP(name, "vim_starting") == 0)
6112 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006113 else if (STRICMP(name, "ttyin") == 0)
6114 n = mch_input_isatty();
6115 else if (STRICMP(name, "ttyout") == 0)
6116 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006117#ifdef FEAT_MBYTE
6118 else if (STRICMP(name, "multi_byte_encoding") == 0)
6119 n = has_mbyte;
6120#endif
6121#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
6122 else if (STRICMP(name, "balloon_multiline") == 0)
6123 n = multiline_balloon_available();
6124#endif
6125#ifdef DYNAMIC_TCL
6126 else if (STRICMP(name, "tcl") == 0)
6127 n = tcl_enabled(FALSE);
6128#endif
6129#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6130 else if (STRICMP(name, "iconv") == 0)
6131 n = iconv_enabled(FALSE);
6132#endif
6133#ifdef DYNAMIC_LUA
6134 else if (STRICMP(name, "lua") == 0)
6135 n = lua_enabled(FALSE);
6136#endif
6137#ifdef DYNAMIC_MZSCHEME
6138 else if (STRICMP(name, "mzscheme") == 0)
6139 n = mzscheme_enabled(FALSE);
6140#endif
6141#ifdef DYNAMIC_RUBY
6142 else if (STRICMP(name, "ruby") == 0)
6143 n = ruby_enabled(FALSE);
6144#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006145#ifdef DYNAMIC_PYTHON
6146 else if (STRICMP(name, "python") == 0)
6147 n = python_enabled(FALSE);
6148#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006149#ifdef DYNAMIC_PYTHON3
6150 else if (STRICMP(name, "python3") == 0)
6151 n = python3_enabled(FALSE);
6152#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006153#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6154 else if (STRICMP(name, "pythonx") == 0)
6155 {
6156# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6157 if (p_pyx == 0)
6158 n = python3_enabled(FALSE) || python_enabled(FALSE);
6159 else if (p_pyx == 3)
6160 n = python3_enabled(FALSE);
6161 else if (p_pyx == 2)
6162 n = python_enabled(FALSE);
6163# elif defined(DYNAMIC_PYTHON)
6164 n = python_enabled(FALSE);
6165# elif defined(DYNAMIC_PYTHON3)
6166 n = python3_enabled(FALSE);
6167# endif
6168 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006169#endif
6170#ifdef DYNAMIC_PERL
6171 else if (STRICMP(name, "perl") == 0)
6172 n = perl_enabled(FALSE);
6173#endif
6174#ifdef FEAT_GUI
6175 else if (STRICMP(name, "gui_running") == 0)
6176 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006177# ifdef FEAT_BROWSE
6178 else if (STRICMP(name, "browse") == 0)
6179 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6180# endif
6181#endif
6182#ifdef FEAT_SYN_HL
6183 else if (STRICMP(name, "syntax_items") == 0)
6184 n = syntax_present(curwin);
6185#endif
6186#if defined(WIN3264)
6187 else if (STRICMP(name, "win95") == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006188 n = FALSE; /* Win9x is no more supported. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006189#endif
6190#ifdef FEAT_NETBEANS_INTG
6191 else if (STRICMP(name, "netbeans_enabled") == 0)
6192 n = netbeans_active();
6193#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006194#if defined(FEAT_TERMINAL) && defined(WIN3264)
6195 else if (STRICMP(name, "terminal") == 0)
6196 n = terminal_enabled();
6197#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006198 }
6199
6200 rettv->vval.v_number = n;
6201}
6202
6203/*
6204 * "has_key()" function
6205 */
6206 static void
6207f_has_key(typval_T *argvars, typval_T *rettv)
6208{
6209 if (argvars[0].v_type != VAR_DICT)
6210 {
6211 EMSG(_(e_dictreq));
6212 return;
6213 }
6214 if (argvars[0].vval.v_dict == NULL)
6215 return;
6216
6217 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
6218 get_tv_string(&argvars[1]), -1) != NULL;
6219}
6220
6221/*
6222 * "haslocaldir()" function
6223 */
6224 static void
6225f_haslocaldir(typval_T *argvars, typval_T *rettv)
6226{
6227 win_T *wp = NULL;
6228
6229 wp = find_tabwin(&argvars[0], &argvars[1]);
6230 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6231}
6232
6233/*
6234 * "hasmapto()" function
6235 */
6236 static void
6237f_hasmapto(typval_T *argvars, typval_T *rettv)
6238{
6239 char_u *name;
6240 char_u *mode;
6241 char_u buf[NUMBUFLEN];
6242 int abbr = FALSE;
6243
6244 name = get_tv_string(&argvars[0]);
6245 if (argvars[1].v_type == VAR_UNKNOWN)
6246 mode = (char_u *)"nvo";
6247 else
6248 {
6249 mode = get_tv_string_buf(&argvars[1], buf);
6250 if (argvars[2].v_type != VAR_UNKNOWN)
6251 abbr = (int)get_tv_number(&argvars[2]);
6252 }
6253
6254 if (map_to_exists(name, mode, abbr))
6255 rettv->vval.v_number = TRUE;
6256 else
6257 rettv->vval.v_number = FALSE;
6258}
6259
6260/*
6261 * "histadd()" function
6262 */
6263 static void
6264f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6265{
6266#ifdef FEAT_CMDHIST
6267 int histype;
6268 char_u *str;
6269 char_u buf[NUMBUFLEN];
6270#endif
6271
6272 rettv->vval.v_number = FALSE;
6273 if (check_restricted() || check_secure())
6274 return;
6275#ifdef FEAT_CMDHIST
6276 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6277 histype = str != NULL ? get_histtype(str) : -1;
6278 if (histype >= 0)
6279 {
6280 str = get_tv_string_buf(&argvars[1], buf);
6281 if (*str != NUL)
6282 {
6283 init_history();
6284 add_to_history(histype, str, FALSE, NUL);
6285 rettv->vval.v_number = TRUE;
6286 return;
6287 }
6288 }
6289#endif
6290}
6291
6292/*
6293 * "histdel()" function
6294 */
6295 static void
6296f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6297{
6298#ifdef FEAT_CMDHIST
6299 int n;
6300 char_u buf[NUMBUFLEN];
6301 char_u *str;
6302
6303 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6304 if (str == NULL)
6305 n = 0;
6306 else if (argvars[1].v_type == VAR_UNKNOWN)
6307 /* only one argument: clear entire history */
6308 n = clr_history(get_histtype(str));
6309 else if (argvars[1].v_type == VAR_NUMBER)
6310 /* index given: remove that entry */
6311 n = del_history_idx(get_histtype(str),
6312 (int)get_tv_number(&argvars[1]));
6313 else
6314 /* string given: remove all matching entries */
6315 n = del_history_entry(get_histtype(str),
6316 get_tv_string_buf(&argvars[1], buf));
6317 rettv->vval.v_number = n;
6318#endif
6319}
6320
6321/*
6322 * "histget()" function
6323 */
6324 static void
6325f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6326{
6327#ifdef FEAT_CMDHIST
6328 int type;
6329 int idx;
6330 char_u *str;
6331
6332 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6333 if (str == NULL)
6334 rettv->vval.v_string = NULL;
6335 else
6336 {
6337 type = get_histtype(str);
6338 if (argvars[1].v_type == VAR_UNKNOWN)
6339 idx = get_history_idx(type);
6340 else
6341 idx = (int)get_tv_number_chk(&argvars[1], NULL);
6342 /* -1 on type error */
6343 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6344 }
6345#else
6346 rettv->vval.v_string = NULL;
6347#endif
6348 rettv->v_type = VAR_STRING;
6349}
6350
6351/*
6352 * "histnr()" function
6353 */
6354 static void
6355f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6356{
6357 int i;
6358
6359#ifdef FEAT_CMDHIST
6360 char_u *history = get_tv_string_chk(&argvars[0]);
6361
6362 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6363 if (i >= HIST_CMD && i < HIST_COUNT)
6364 i = get_history_idx(i);
6365 else
6366#endif
6367 i = -1;
6368 rettv->vval.v_number = i;
6369}
6370
6371/*
6372 * "highlightID(name)" function
6373 */
6374 static void
6375f_hlID(typval_T *argvars, typval_T *rettv)
6376{
6377 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
6378}
6379
6380/*
6381 * "highlight_exists()" function
6382 */
6383 static void
6384f_hlexists(typval_T *argvars, typval_T *rettv)
6385{
6386 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
6387}
6388
6389/*
6390 * "hostname()" function
6391 */
6392 static void
6393f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6394{
6395 char_u hostname[256];
6396
6397 mch_get_host_name(hostname, 256);
6398 rettv->v_type = VAR_STRING;
6399 rettv->vval.v_string = vim_strsave(hostname);
6400}
6401
6402/*
6403 * iconv() function
6404 */
6405 static void
6406f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6407{
6408#ifdef FEAT_MBYTE
6409 char_u buf1[NUMBUFLEN];
6410 char_u buf2[NUMBUFLEN];
6411 char_u *from, *to, *str;
6412 vimconv_T vimconv;
6413#endif
6414
6415 rettv->v_type = VAR_STRING;
6416 rettv->vval.v_string = NULL;
6417
6418#ifdef FEAT_MBYTE
6419 str = get_tv_string(&argvars[0]);
6420 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
6421 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
6422 vimconv.vc_type = CONV_NONE;
6423 convert_setup(&vimconv, from, to);
6424
6425 /* If the encodings are equal, no conversion needed. */
6426 if (vimconv.vc_type == CONV_NONE)
6427 rettv->vval.v_string = vim_strsave(str);
6428 else
6429 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6430
6431 convert_setup(&vimconv, NULL, NULL);
6432 vim_free(from);
6433 vim_free(to);
6434#endif
6435}
6436
6437/*
6438 * "indent()" function
6439 */
6440 static void
6441f_indent(typval_T *argvars, typval_T *rettv)
6442{
6443 linenr_T lnum;
6444
6445 lnum = get_tv_lnum(argvars);
6446 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6447 rettv->vval.v_number = get_indent_lnum(lnum);
6448 else
6449 rettv->vval.v_number = -1;
6450}
6451
6452/*
6453 * "index()" function
6454 */
6455 static void
6456f_index(typval_T *argvars, typval_T *rettv)
6457{
6458 list_T *l;
6459 listitem_T *item;
6460 long idx = 0;
6461 int ic = FALSE;
6462
6463 rettv->vval.v_number = -1;
6464 if (argvars[0].v_type != VAR_LIST)
6465 {
6466 EMSG(_(e_listreq));
6467 return;
6468 }
6469 l = argvars[0].vval.v_list;
6470 if (l != NULL)
6471 {
6472 item = l->lv_first;
6473 if (argvars[2].v_type != VAR_UNKNOWN)
6474 {
6475 int error = FALSE;
6476
6477 /* Start at specified item. Use the cached index that list_find()
6478 * sets, so that a negative number also works. */
6479 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
6480 idx = l->lv_idx;
6481 if (argvars[3].v_type != VAR_UNKNOWN)
6482 ic = (int)get_tv_number_chk(&argvars[3], &error);
6483 if (error)
6484 item = NULL;
6485 }
6486
6487 for ( ; item != NULL; item = item->li_next, ++idx)
6488 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6489 {
6490 rettv->vval.v_number = idx;
6491 break;
6492 }
6493 }
6494}
6495
6496static int inputsecret_flag = 0;
6497
6498/*
6499 * "input()" function
6500 * Also handles inputsecret() when inputsecret is set.
6501 */
6502 static void
6503f_input(typval_T *argvars, typval_T *rettv)
6504{
6505 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6506}
6507
6508/*
6509 * "inputdialog()" function
6510 */
6511 static void
6512f_inputdialog(typval_T *argvars, typval_T *rettv)
6513{
6514#if defined(FEAT_GUI_TEXTDIALOG)
6515 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
6516 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6517 {
6518 char_u *message;
6519 char_u buf[NUMBUFLEN];
6520 char_u *defstr = (char_u *)"";
6521
6522 message = get_tv_string_chk(&argvars[0]);
6523 if (argvars[1].v_type != VAR_UNKNOWN
6524 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
6525 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6526 else
6527 IObuff[0] = NUL;
6528 if (message != NULL && defstr != NULL
6529 && do_dialog(VIM_QUESTION, NULL, message,
6530 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6531 rettv->vval.v_string = vim_strsave(IObuff);
6532 else
6533 {
6534 if (message != NULL && defstr != NULL
6535 && argvars[1].v_type != VAR_UNKNOWN
6536 && argvars[2].v_type != VAR_UNKNOWN)
6537 rettv->vval.v_string = vim_strsave(
6538 get_tv_string_buf(&argvars[2], buf));
6539 else
6540 rettv->vval.v_string = NULL;
6541 }
6542 rettv->v_type = VAR_STRING;
6543 }
6544 else
6545#endif
6546 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6547}
6548
6549/*
6550 * "inputlist()" function
6551 */
6552 static void
6553f_inputlist(typval_T *argvars, typval_T *rettv)
6554{
6555 listitem_T *li;
6556 int selected;
6557 int mouse_used;
6558
6559#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02006560 /* While starting up, there is no place to enter text. When running tests
6561 * with --not-a-term we assume feedkeys() will be used. */
6562 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006563 return;
6564#endif
6565 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6566 {
6567 EMSG2(_(e_listarg), "inputlist()");
6568 return;
6569 }
6570
6571 msg_start();
6572 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
6573 lines_left = Rows; /* avoid more prompt */
6574 msg_scroll = TRUE;
6575 msg_clr_eos();
6576
6577 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
6578 {
6579 msg_puts(get_tv_string(&li->li_tv));
6580 msg_putchar('\n');
6581 }
6582
6583 /* Ask for choice. */
6584 selected = prompt_for_number(&mouse_used);
6585 if (mouse_used)
6586 selected -= lines_left;
6587
6588 rettv->vval.v_number = selected;
6589}
6590
6591
6592static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6593
6594/*
6595 * "inputrestore()" function
6596 */
6597 static void
6598f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6599{
6600 if (ga_userinput.ga_len > 0)
6601 {
6602 --ga_userinput.ga_len;
6603 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
6604 + ga_userinput.ga_len);
6605 /* default return is zero == OK */
6606 }
6607 else if (p_verbose > 1)
6608 {
6609 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
6610 rettv->vval.v_number = 1; /* Failed */
6611 }
6612}
6613
6614/*
6615 * "inputsave()" function
6616 */
6617 static void
6618f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
6619{
6620 /* Add an entry to the stack of typeahead storage. */
6621 if (ga_grow(&ga_userinput, 1) == OK)
6622 {
6623 save_typeahead((tasave_T *)(ga_userinput.ga_data)
6624 + ga_userinput.ga_len);
6625 ++ga_userinput.ga_len;
6626 /* default return is zero == OK */
6627 }
6628 else
6629 rettv->vval.v_number = 1; /* Failed */
6630}
6631
6632/*
6633 * "inputsecret()" function
6634 */
6635 static void
6636f_inputsecret(typval_T *argvars, typval_T *rettv)
6637{
6638 ++cmdline_star;
6639 ++inputsecret_flag;
6640 f_input(argvars, rettv);
6641 --cmdline_star;
6642 --inputsecret_flag;
6643}
6644
6645/*
6646 * "insert()" function
6647 */
6648 static void
6649f_insert(typval_T *argvars, typval_T *rettv)
6650{
6651 long before = 0;
6652 listitem_T *item;
6653 list_T *l;
6654 int error = FALSE;
6655
6656 if (argvars[0].v_type != VAR_LIST)
6657 EMSG2(_(e_listarg), "insert()");
6658 else if ((l = argvars[0].vval.v_list) != NULL
6659 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
6660 {
6661 if (argvars[2].v_type != VAR_UNKNOWN)
6662 before = (long)get_tv_number_chk(&argvars[2], &error);
6663 if (error)
6664 return; /* type error; errmsg already given */
6665
6666 if (before == l->lv_len)
6667 item = NULL;
6668 else
6669 {
6670 item = list_find(l, before);
6671 if (item == NULL)
6672 {
6673 EMSGN(_(e_listidx), before);
6674 l = NULL;
6675 }
6676 }
6677 if (l != NULL)
6678 {
6679 list_insert_tv(l, &argvars[1], item);
6680 copy_tv(&argvars[0], rettv);
6681 }
6682 }
6683}
6684
6685/*
6686 * "invert(expr)" function
6687 */
6688 static void
6689f_invert(typval_T *argvars, typval_T *rettv)
6690{
6691 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
6692}
6693
6694/*
6695 * "isdirectory()" function
6696 */
6697 static void
6698f_isdirectory(typval_T *argvars, typval_T *rettv)
6699{
6700 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
6701}
6702
6703/*
6704 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
6705 * or it refers to a List or Dictionary that is locked.
6706 */
6707 static int
6708tv_islocked(typval_T *tv)
6709{
6710 return (tv->v_lock & VAR_LOCKED)
6711 || (tv->v_type == VAR_LIST
6712 && tv->vval.v_list != NULL
6713 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
6714 || (tv->v_type == VAR_DICT
6715 && tv->vval.v_dict != NULL
6716 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
6717}
6718
6719/*
6720 * "islocked()" function
6721 */
6722 static void
6723f_islocked(typval_T *argvars, typval_T *rettv)
6724{
6725 lval_T lv;
6726 char_u *end;
6727 dictitem_T *di;
6728
6729 rettv->vval.v_number = -1;
6730 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01006731 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006732 if (end != NULL && lv.ll_name != NULL)
6733 {
6734 if (*end != NUL)
6735 EMSG(_(e_trailing));
6736 else
6737 {
6738 if (lv.ll_tv == NULL)
6739 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006740 di = find_var(lv.ll_name, NULL, TRUE);
6741 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006742 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006743 /* Consider a variable locked when:
6744 * 1. the variable itself is locked
6745 * 2. the value of the variable is locked.
6746 * 3. the List or Dict value is locked.
6747 */
6748 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
6749 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006750 }
6751 }
6752 else if (lv.ll_range)
6753 EMSG(_("E786: Range not allowed"));
6754 else if (lv.ll_newkey != NULL)
6755 EMSG2(_(e_dictkey), lv.ll_newkey);
6756 else if (lv.ll_list != NULL)
6757 /* List item. */
6758 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
6759 else
6760 /* Dictionary item. */
6761 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
6762 }
6763 }
6764
6765 clear_lval(&lv);
6766}
6767
6768#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
6769/*
6770 * "isnan()" function
6771 */
6772 static void
6773f_isnan(typval_T *argvars, typval_T *rettv)
6774{
6775 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
6776 && isnan(argvars[0].vval.v_float);
6777}
6778#endif
6779
6780/*
6781 * "items(dict)" function
6782 */
6783 static void
6784f_items(typval_T *argvars, typval_T *rettv)
6785{
6786 dict_list(argvars, rettv, 2);
6787}
6788
6789#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
6790/*
6791 * Get the job from the argument.
6792 * Returns NULL if the job is invalid.
6793 */
6794 static job_T *
6795get_job_arg(typval_T *tv)
6796{
6797 job_T *job;
6798
6799 if (tv->v_type != VAR_JOB)
6800 {
6801 EMSG2(_(e_invarg2), get_tv_string(tv));
6802 return NULL;
6803 }
6804 job = tv->vval.v_job;
6805
6806 if (job == NULL)
6807 EMSG(_("E916: not a valid job"));
6808 return job;
6809}
6810
6811/*
6812 * "job_getchannel()" function
6813 */
6814 static void
6815f_job_getchannel(typval_T *argvars, typval_T *rettv)
6816{
6817 job_T *job = get_job_arg(&argvars[0]);
6818
6819 if (job != NULL)
6820 {
6821 rettv->v_type = VAR_CHANNEL;
6822 rettv->vval.v_channel = job->jv_channel;
6823 if (job->jv_channel != NULL)
6824 ++job->jv_channel->ch_refcount;
6825 }
6826}
6827
6828/*
6829 * "job_info()" function
6830 */
6831 static void
6832f_job_info(typval_T *argvars, typval_T *rettv)
6833{
6834 job_T *job = get_job_arg(&argvars[0]);
6835
6836 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
6837 job_info(job, rettv->vval.v_dict);
6838}
6839
6840/*
6841 * "job_setoptions()" function
6842 */
6843 static void
6844f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
6845{
6846 job_T *job = get_job_arg(&argvars[0]);
6847 jobopt_T opt;
6848
6849 if (job == NULL)
6850 return;
6851 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02006852 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006853 job_set_options(job, &opt);
6854 free_job_options(&opt);
6855}
6856
6857/*
6858 * "job_start()" function
6859 */
6860 static void
6861f_job_start(typval_T *argvars, typval_T *rettv)
6862{
6863 rettv->v_type = VAR_JOB;
6864 if (check_restricted() || check_secure())
6865 return;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006866 rettv->vval.v_job = job_start(argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006867}
6868
6869/*
6870 * "job_status()" function
6871 */
6872 static void
6873f_job_status(typval_T *argvars, typval_T *rettv)
6874{
6875 job_T *job = get_job_arg(&argvars[0]);
6876
6877 if (job != NULL)
6878 {
6879 rettv->v_type = VAR_STRING;
6880 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
6881 }
6882}
6883
6884/*
6885 * "job_stop()" function
6886 */
6887 static void
6888f_job_stop(typval_T *argvars, typval_T *rettv)
6889{
6890 job_T *job = get_job_arg(&argvars[0]);
6891
6892 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02006893 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006894}
6895#endif
6896
6897/*
6898 * "join()" function
6899 */
6900 static void
6901f_join(typval_T *argvars, typval_T *rettv)
6902{
6903 garray_T ga;
6904 char_u *sep;
6905
6906 if (argvars[0].v_type != VAR_LIST)
6907 {
6908 EMSG(_(e_listreq));
6909 return;
6910 }
6911 if (argvars[0].vval.v_list == NULL)
6912 return;
6913 if (argvars[1].v_type == VAR_UNKNOWN)
6914 sep = (char_u *)" ";
6915 else
6916 sep = get_tv_string_chk(&argvars[1]);
6917
6918 rettv->v_type = VAR_STRING;
6919
6920 if (sep != NULL)
6921 {
6922 ga_init2(&ga, (int)sizeof(char), 80);
6923 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
6924 ga_append(&ga, NUL);
6925 rettv->vval.v_string = (char_u *)ga.ga_data;
6926 }
6927 else
6928 rettv->vval.v_string = NULL;
6929}
6930
6931/*
6932 * "js_decode()" function
6933 */
6934 static void
6935f_js_decode(typval_T *argvars, typval_T *rettv)
6936{
6937 js_read_T reader;
6938
6939 reader.js_buf = get_tv_string(&argvars[0]);
6940 reader.js_fill = NULL;
6941 reader.js_used = 0;
6942 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
6943 EMSG(_(e_invarg));
6944}
6945
6946/*
6947 * "js_encode()" function
6948 */
6949 static void
6950f_js_encode(typval_T *argvars, typval_T *rettv)
6951{
6952 rettv->v_type = VAR_STRING;
6953 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
6954}
6955
6956/*
6957 * "json_decode()" function
6958 */
6959 static void
6960f_json_decode(typval_T *argvars, typval_T *rettv)
6961{
6962 js_read_T reader;
6963
6964 reader.js_buf = get_tv_string(&argvars[0]);
6965 reader.js_fill = NULL;
6966 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01006967 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006968}
6969
6970/*
6971 * "json_encode()" function
6972 */
6973 static void
6974f_json_encode(typval_T *argvars, typval_T *rettv)
6975{
6976 rettv->v_type = VAR_STRING;
6977 rettv->vval.v_string = json_encode(&argvars[0], 0);
6978}
6979
6980/*
6981 * "keys()" function
6982 */
6983 static void
6984f_keys(typval_T *argvars, typval_T *rettv)
6985{
6986 dict_list(argvars, rettv, 0);
6987}
6988
6989/*
6990 * "last_buffer_nr()" function.
6991 */
6992 static void
6993f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
6994{
6995 int n = 0;
6996 buf_T *buf;
6997
Bram Moolenaar29323592016-07-24 22:04:11 +02006998 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006999 if (n < buf->b_fnum)
7000 n = buf->b_fnum;
7001
7002 rettv->vval.v_number = n;
7003}
7004
7005/*
7006 * "len()" function
7007 */
7008 static void
7009f_len(typval_T *argvars, typval_T *rettv)
7010{
7011 switch (argvars[0].v_type)
7012 {
7013 case VAR_STRING:
7014 case VAR_NUMBER:
7015 rettv->vval.v_number = (varnumber_T)STRLEN(
7016 get_tv_string(&argvars[0]));
7017 break;
7018 case VAR_LIST:
7019 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7020 break;
7021 case VAR_DICT:
7022 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7023 break;
7024 case VAR_UNKNOWN:
7025 case VAR_SPECIAL:
7026 case VAR_FLOAT:
7027 case VAR_FUNC:
7028 case VAR_PARTIAL:
7029 case VAR_JOB:
7030 case VAR_CHANNEL:
7031 EMSG(_("E701: Invalid type for len()"));
7032 break;
7033 }
7034}
7035
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007036 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007037libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007038{
7039#ifdef FEAT_LIBCALL
7040 char_u *string_in;
7041 char_u **string_result;
7042 int nr_result;
7043#endif
7044
7045 rettv->v_type = type;
7046 if (type != VAR_NUMBER)
7047 rettv->vval.v_string = NULL;
7048
7049 if (check_restricted() || check_secure())
7050 return;
7051
7052#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007053 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007054 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7055 {
7056 string_in = NULL;
7057 if (argvars[2].v_type == VAR_STRING)
7058 string_in = argvars[2].vval.v_string;
7059 if (type == VAR_NUMBER)
7060 string_result = NULL;
7061 else
7062 string_result = &rettv->vval.v_string;
7063 if (mch_libcall(argvars[0].vval.v_string,
7064 argvars[1].vval.v_string,
7065 string_in,
7066 argvars[2].vval.v_number,
7067 string_result,
7068 &nr_result) == OK
7069 && type == VAR_NUMBER)
7070 rettv->vval.v_number = nr_result;
7071 }
7072#endif
7073}
7074
7075/*
7076 * "libcall()" function
7077 */
7078 static void
7079f_libcall(typval_T *argvars, typval_T *rettv)
7080{
7081 libcall_common(argvars, rettv, VAR_STRING);
7082}
7083
7084/*
7085 * "libcallnr()" function
7086 */
7087 static void
7088f_libcallnr(typval_T *argvars, typval_T *rettv)
7089{
7090 libcall_common(argvars, rettv, VAR_NUMBER);
7091}
7092
7093/*
7094 * "line(string)" function
7095 */
7096 static void
7097f_line(typval_T *argvars, typval_T *rettv)
7098{
7099 linenr_T lnum = 0;
7100 pos_T *fp;
7101 int fnum;
7102
7103 fp = var2fpos(&argvars[0], TRUE, &fnum);
7104 if (fp != NULL)
7105 lnum = fp->lnum;
7106 rettv->vval.v_number = lnum;
7107}
7108
7109/*
7110 * "line2byte(lnum)" function
7111 */
7112 static void
7113f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7114{
7115#ifndef FEAT_BYTEOFF
7116 rettv->vval.v_number = -1;
7117#else
7118 linenr_T lnum;
7119
7120 lnum = get_tv_lnum(argvars);
7121 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7122 rettv->vval.v_number = -1;
7123 else
7124 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7125 if (rettv->vval.v_number >= 0)
7126 ++rettv->vval.v_number;
7127#endif
7128}
7129
7130/*
7131 * "lispindent(lnum)" function
7132 */
7133 static void
7134f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7135{
7136#ifdef FEAT_LISP
7137 pos_T pos;
7138 linenr_T lnum;
7139
7140 pos = curwin->w_cursor;
7141 lnum = get_tv_lnum(argvars);
7142 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7143 {
7144 curwin->w_cursor.lnum = lnum;
7145 rettv->vval.v_number = get_lisp_indent();
7146 curwin->w_cursor = pos;
7147 }
7148 else
7149#endif
7150 rettv->vval.v_number = -1;
7151}
7152
7153/*
7154 * "localtime()" function
7155 */
7156 static void
7157f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7158{
7159 rettv->vval.v_number = (varnumber_T)time(NULL);
7160}
7161
7162static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
7163
7164 static void
7165get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7166{
7167 char_u *keys;
7168 char_u *which;
7169 char_u buf[NUMBUFLEN];
7170 char_u *keys_buf = NULL;
7171 char_u *rhs;
7172 int mode;
7173 int abbr = FALSE;
7174 int get_dict = FALSE;
7175 mapblock_T *mp;
7176 int buffer_local;
7177
7178 /* return empty string for failure */
7179 rettv->v_type = VAR_STRING;
7180 rettv->vval.v_string = NULL;
7181
7182 keys = get_tv_string(&argvars[0]);
7183 if (*keys == NUL)
7184 return;
7185
7186 if (argvars[1].v_type != VAR_UNKNOWN)
7187 {
7188 which = get_tv_string_buf_chk(&argvars[1], buf);
7189 if (argvars[2].v_type != VAR_UNKNOWN)
7190 {
7191 abbr = (int)get_tv_number(&argvars[2]);
7192 if (argvars[3].v_type != VAR_UNKNOWN)
7193 get_dict = (int)get_tv_number(&argvars[3]);
7194 }
7195 }
7196 else
7197 which = (char_u *)"";
7198 if (which == NULL)
7199 return;
7200
7201 mode = get_map_mode(&which, 0);
7202
7203 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7204 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7205 vim_free(keys_buf);
7206
7207 if (!get_dict)
7208 {
7209 /* Return a string. */
7210 if (rhs != NULL)
7211 rettv->vval.v_string = str2special_save(rhs, FALSE);
7212
7213 }
7214 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7215 {
7216 /* Return a dictionary. */
7217 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7218 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7219 dict_T *dict = rettv->vval.v_dict;
7220
7221 dict_add_nr_str(dict, "lhs", 0L, lhs);
7222 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
7223 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
7224 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
7225 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
7226 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
7227 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
7228 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
7229 dict_add_nr_str(dict, "mode", 0L, mapmode);
7230
7231 vim_free(lhs);
7232 vim_free(mapmode);
7233 }
7234}
7235
7236#ifdef FEAT_FLOAT
7237/*
7238 * "log()" function
7239 */
7240 static void
7241f_log(typval_T *argvars, typval_T *rettv)
7242{
7243 float_T f = 0.0;
7244
7245 rettv->v_type = VAR_FLOAT;
7246 if (get_float_arg(argvars, &f) == OK)
7247 rettv->vval.v_float = log(f);
7248 else
7249 rettv->vval.v_float = 0.0;
7250}
7251
7252/*
7253 * "log10()" function
7254 */
7255 static void
7256f_log10(typval_T *argvars, typval_T *rettv)
7257{
7258 float_T f = 0.0;
7259
7260 rettv->v_type = VAR_FLOAT;
7261 if (get_float_arg(argvars, &f) == OK)
7262 rettv->vval.v_float = log10(f);
7263 else
7264 rettv->vval.v_float = 0.0;
7265}
7266#endif
7267
7268#ifdef FEAT_LUA
7269/*
7270 * "luaeval()" function
7271 */
7272 static void
7273f_luaeval(typval_T *argvars, typval_T *rettv)
7274{
7275 char_u *str;
7276 char_u buf[NUMBUFLEN];
7277
7278 str = get_tv_string_buf(&argvars[0], buf);
7279 do_luaeval(str, argvars + 1, rettv);
7280}
7281#endif
7282
7283/*
7284 * "map()" function
7285 */
7286 static void
7287f_map(typval_T *argvars, typval_T *rettv)
7288{
7289 filter_map(argvars, rettv, TRUE);
7290}
7291
7292/*
7293 * "maparg()" function
7294 */
7295 static void
7296f_maparg(typval_T *argvars, typval_T *rettv)
7297{
7298 get_maparg(argvars, rettv, TRUE);
7299}
7300
7301/*
7302 * "mapcheck()" function
7303 */
7304 static void
7305f_mapcheck(typval_T *argvars, typval_T *rettv)
7306{
7307 get_maparg(argvars, rettv, FALSE);
7308}
7309
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007310typedef enum
7311{
7312 MATCH_END, /* matchend() */
7313 MATCH_MATCH, /* match() */
7314 MATCH_STR, /* matchstr() */
7315 MATCH_LIST, /* matchlist() */
7316 MATCH_POS /* matchstrpos() */
7317} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007318
7319 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007320find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007321{
7322 char_u *str = NULL;
7323 long len = 0;
7324 char_u *expr = NULL;
7325 char_u *pat;
7326 regmatch_T regmatch;
7327 char_u patbuf[NUMBUFLEN];
7328 char_u strbuf[NUMBUFLEN];
7329 char_u *save_cpo;
7330 long start = 0;
7331 long nth = 1;
7332 colnr_T startcol = 0;
7333 int match = 0;
7334 list_T *l = NULL;
7335 listitem_T *li = NULL;
7336 long idx = 0;
7337 char_u *tofree = NULL;
7338
7339 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7340 save_cpo = p_cpo;
7341 p_cpo = (char_u *)"";
7342
7343 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007344 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007345 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007346 /* type MATCH_LIST: return empty list when there are no matches.
7347 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007348 if (rettv_list_alloc(rettv) == FAIL)
7349 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007350 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007351 && (list_append_string(rettv->vval.v_list,
7352 (char_u *)"", 0) == FAIL
7353 || list_append_number(rettv->vval.v_list,
7354 (varnumber_T)-1) == FAIL
7355 || list_append_number(rettv->vval.v_list,
7356 (varnumber_T)-1) == FAIL
7357 || list_append_number(rettv->vval.v_list,
7358 (varnumber_T)-1) == FAIL))
7359 {
7360 list_free(rettv->vval.v_list);
7361 rettv->vval.v_list = NULL;
7362 goto theend;
7363 }
7364 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007365 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007366 {
7367 rettv->v_type = VAR_STRING;
7368 rettv->vval.v_string = NULL;
7369 }
7370
7371 if (argvars[0].v_type == VAR_LIST)
7372 {
7373 if ((l = argvars[0].vval.v_list) == NULL)
7374 goto theend;
7375 li = l->lv_first;
7376 }
7377 else
7378 {
7379 expr = str = get_tv_string(&argvars[0]);
7380 len = (long)STRLEN(str);
7381 }
7382
7383 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
7384 if (pat == NULL)
7385 goto theend;
7386
7387 if (argvars[2].v_type != VAR_UNKNOWN)
7388 {
7389 int error = FALSE;
7390
7391 start = (long)get_tv_number_chk(&argvars[2], &error);
7392 if (error)
7393 goto theend;
7394 if (l != NULL)
7395 {
7396 li = list_find(l, start);
7397 if (li == NULL)
7398 goto theend;
7399 idx = l->lv_idx; /* use the cached index */
7400 }
7401 else
7402 {
7403 if (start < 0)
7404 start = 0;
7405 if (start > len)
7406 goto theend;
7407 /* When "count" argument is there ignore matches before "start",
7408 * otherwise skip part of the string. Differs when pattern is "^"
7409 * or "\<". */
7410 if (argvars[3].v_type != VAR_UNKNOWN)
7411 startcol = start;
7412 else
7413 {
7414 str += start;
7415 len -= start;
7416 }
7417 }
7418
7419 if (argvars[3].v_type != VAR_UNKNOWN)
7420 nth = (long)get_tv_number_chk(&argvars[3], &error);
7421 if (error)
7422 goto theend;
7423 }
7424
7425 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7426 if (regmatch.regprog != NULL)
7427 {
7428 regmatch.rm_ic = p_ic;
7429
7430 for (;;)
7431 {
7432 if (l != NULL)
7433 {
7434 if (li == NULL)
7435 {
7436 match = FALSE;
7437 break;
7438 }
7439 vim_free(tofree);
7440 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7441 if (str == NULL)
7442 break;
7443 }
7444
7445 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7446
7447 if (match && --nth <= 0)
7448 break;
7449 if (l == NULL && !match)
7450 break;
7451
7452 /* Advance to just after the match. */
7453 if (l != NULL)
7454 {
7455 li = li->li_next;
7456 ++idx;
7457 }
7458 else
7459 {
7460#ifdef FEAT_MBYTE
7461 startcol = (colnr_T)(regmatch.startp[0]
7462 + (*mb_ptr2len)(regmatch.startp[0]) - str);
7463#else
7464 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
7465#endif
7466 if (startcol > (colnr_T)len
7467 || str + startcol <= regmatch.startp[0])
7468 {
7469 match = FALSE;
7470 break;
7471 }
7472 }
7473 }
7474
7475 if (match)
7476 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007477 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007478 {
7479 listitem_T *li1 = rettv->vval.v_list->lv_first;
7480 listitem_T *li2 = li1->li_next;
7481 listitem_T *li3 = li2->li_next;
7482 listitem_T *li4 = li3->li_next;
7483
7484 vim_free(li1->li_tv.vval.v_string);
7485 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7486 (int)(regmatch.endp[0] - regmatch.startp[0]));
7487 li3->li_tv.vval.v_number =
7488 (varnumber_T)(regmatch.startp[0] - expr);
7489 li4->li_tv.vval.v_number =
7490 (varnumber_T)(regmatch.endp[0] - expr);
7491 if (l != NULL)
7492 li2->li_tv.vval.v_number = (varnumber_T)idx;
7493 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007494 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007495 {
7496 int i;
7497
7498 /* return list with matched string and submatches */
7499 for (i = 0; i < NSUBEXP; ++i)
7500 {
7501 if (regmatch.endp[i] == NULL)
7502 {
7503 if (list_append_string(rettv->vval.v_list,
7504 (char_u *)"", 0) == FAIL)
7505 break;
7506 }
7507 else if (list_append_string(rettv->vval.v_list,
7508 regmatch.startp[i],
7509 (int)(regmatch.endp[i] - regmatch.startp[i]))
7510 == FAIL)
7511 break;
7512 }
7513 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007514 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007515 {
7516 /* return matched string */
7517 if (l != NULL)
7518 copy_tv(&li->li_tv, rettv);
7519 else
7520 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
7521 (int)(regmatch.endp[0] - regmatch.startp[0]));
7522 }
7523 else if (l != NULL)
7524 rettv->vval.v_number = idx;
7525 else
7526 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007527 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007528 rettv->vval.v_number =
7529 (varnumber_T)(regmatch.startp[0] - str);
7530 else
7531 rettv->vval.v_number =
7532 (varnumber_T)(regmatch.endp[0] - str);
7533 rettv->vval.v_number += (varnumber_T)(str - expr);
7534 }
7535 }
7536 vim_regfree(regmatch.regprog);
7537 }
7538
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007539theend:
7540 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007541 /* matchstrpos() without a list: drop the second item. */
7542 listitem_remove(rettv->vval.v_list,
7543 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007544 vim_free(tofree);
7545 p_cpo = save_cpo;
7546}
7547
7548/*
7549 * "match()" function
7550 */
7551 static void
7552f_match(typval_T *argvars, typval_T *rettv)
7553{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007554 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007555}
7556
7557/*
7558 * "matchadd()" function
7559 */
7560 static void
7561f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7562{
7563#ifdef FEAT_SEARCH_EXTRA
7564 char_u buf[NUMBUFLEN];
7565 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
7566 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
7567 int prio = 10; /* default priority */
7568 int id = -1;
7569 int error = FALSE;
7570 char_u *conceal_char = NULL;
7571
7572 rettv->vval.v_number = -1;
7573
7574 if (grp == NULL || pat == NULL)
7575 return;
7576 if (argvars[2].v_type != VAR_UNKNOWN)
7577 {
7578 prio = (int)get_tv_number_chk(&argvars[2], &error);
7579 if (argvars[3].v_type != VAR_UNKNOWN)
7580 {
7581 id = (int)get_tv_number_chk(&argvars[3], &error);
7582 if (argvars[4].v_type != VAR_UNKNOWN)
7583 {
7584 if (argvars[4].v_type != VAR_DICT)
7585 {
7586 EMSG(_(e_dictreq));
7587 return;
7588 }
7589 if (dict_find(argvars[4].vval.v_dict,
7590 (char_u *)"conceal", -1) != NULL)
7591 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7592 (char_u *)"conceal", FALSE);
7593 }
7594 }
7595 }
7596 if (error == TRUE)
7597 return;
7598 if (id >= 1 && id <= 3)
7599 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007600 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007601 return;
7602 }
7603
7604 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
7605 conceal_char);
7606#endif
7607}
7608
7609/*
7610 * "matchaddpos()" function
7611 */
7612 static void
7613f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7614{
7615#ifdef FEAT_SEARCH_EXTRA
7616 char_u buf[NUMBUFLEN];
7617 char_u *group;
7618 int prio = 10;
7619 int id = -1;
7620 int error = FALSE;
7621 list_T *l;
7622 char_u *conceal_char = NULL;
7623
7624 rettv->vval.v_number = -1;
7625
7626 group = get_tv_string_buf_chk(&argvars[0], buf);
7627 if (group == NULL)
7628 return;
7629
7630 if (argvars[1].v_type != VAR_LIST)
7631 {
7632 EMSG2(_(e_listarg), "matchaddpos()");
7633 return;
7634 }
7635 l = argvars[1].vval.v_list;
7636 if (l == NULL)
7637 return;
7638
7639 if (argvars[2].v_type != VAR_UNKNOWN)
7640 {
7641 prio = (int)get_tv_number_chk(&argvars[2], &error);
7642 if (argvars[3].v_type != VAR_UNKNOWN)
7643 {
7644 id = (int)get_tv_number_chk(&argvars[3], &error);
7645 if (argvars[4].v_type != VAR_UNKNOWN)
7646 {
7647 if (argvars[4].v_type != VAR_DICT)
7648 {
7649 EMSG(_(e_dictreq));
7650 return;
7651 }
7652 if (dict_find(argvars[4].vval.v_dict,
7653 (char_u *)"conceal", -1) != NULL)
7654 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7655 (char_u *)"conceal", FALSE);
7656 }
7657 }
7658 }
7659 if (error == TRUE)
7660 return;
7661
7662 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
7663 if (id == 1 || id == 2)
7664 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007665 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007666 return;
7667 }
7668
7669 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
7670 conceal_char);
7671#endif
7672}
7673
7674/*
7675 * "matcharg()" function
7676 */
7677 static void
7678f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
7679{
7680 if (rettv_list_alloc(rettv) == OK)
7681 {
7682#ifdef FEAT_SEARCH_EXTRA
7683 int id = (int)get_tv_number(&argvars[0]);
7684 matchitem_T *m;
7685
7686 if (id >= 1 && id <= 3)
7687 {
7688 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
7689 {
7690 list_append_string(rettv->vval.v_list,
7691 syn_id2name(m->hlg_id), -1);
7692 list_append_string(rettv->vval.v_list, m->pattern, -1);
7693 }
7694 else
7695 {
7696 list_append_string(rettv->vval.v_list, NULL, -1);
7697 list_append_string(rettv->vval.v_list, NULL, -1);
7698 }
7699 }
7700#endif
7701 }
7702}
7703
7704/*
7705 * "matchdelete()" function
7706 */
7707 static void
7708f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7709{
7710#ifdef FEAT_SEARCH_EXTRA
7711 rettv->vval.v_number = match_delete(curwin,
7712 (int)get_tv_number(&argvars[0]), TRUE);
7713#endif
7714}
7715
7716/*
7717 * "matchend()" function
7718 */
7719 static void
7720f_matchend(typval_T *argvars, typval_T *rettv)
7721{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007722 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007723}
7724
7725/*
7726 * "matchlist()" function
7727 */
7728 static void
7729f_matchlist(typval_T *argvars, typval_T *rettv)
7730{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007731 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007732}
7733
7734/*
7735 * "matchstr()" function
7736 */
7737 static void
7738f_matchstr(typval_T *argvars, typval_T *rettv)
7739{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007740 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007741}
7742
7743/*
7744 * "matchstrpos()" function
7745 */
7746 static void
7747f_matchstrpos(typval_T *argvars, typval_T *rettv)
7748{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007749 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007750}
7751
7752static void max_min(typval_T *argvars, typval_T *rettv, int domax);
7753
7754 static void
7755max_min(typval_T *argvars, typval_T *rettv, int domax)
7756{
7757 varnumber_T n = 0;
7758 varnumber_T i;
7759 int error = FALSE;
7760
7761 if (argvars[0].v_type == VAR_LIST)
7762 {
7763 list_T *l;
7764 listitem_T *li;
7765
7766 l = argvars[0].vval.v_list;
7767 if (l != NULL)
7768 {
7769 li = l->lv_first;
7770 if (li != NULL)
7771 {
7772 n = get_tv_number_chk(&li->li_tv, &error);
7773 for (;;)
7774 {
7775 li = li->li_next;
7776 if (li == NULL)
7777 break;
7778 i = get_tv_number_chk(&li->li_tv, &error);
7779 if (domax ? i > n : i < n)
7780 n = i;
7781 }
7782 }
7783 }
7784 }
7785 else if (argvars[0].v_type == VAR_DICT)
7786 {
7787 dict_T *d;
7788 int first = TRUE;
7789 hashitem_T *hi;
7790 int todo;
7791
7792 d = argvars[0].vval.v_dict;
7793 if (d != NULL)
7794 {
7795 todo = (int)d->dv_hashtab.ht_used;
7796 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
7797 {
7798 if (!HASHITEM_EMPTY(hi))
7799 {
7800 --todo;
7801 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
7802 if (first)
7803 {
7804 n = i;
7805 first = FALSE;
7806 }
7807 else if (domax ? i > n : i < n)
7808 n = i;
7809 }
7810 }
7811 }
7812 }
7813 else
Bram Moolenaar26b84332016-09-04 21:42:36 +02007814 EMSG2(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007815 rettv->vval.v_number = error ? 0 : n;
7816}
7817
7818/*
7819 * "max()" function
7820 */
7821 static void
7822f_max(typval_T *argvars, typval_T *rettv)
7823{
7824 max_min(argvars, rettv, TRUE);
7825}
7826
7827/*
7828 * "min()" function
7829 */
7830 static void
7831f_min(typval_T *argvars, typval_T *rettv)
7832{
7833 max_min(argvars, rettv, FALSE);
7834}
7835
7836static int mkdir_recurse(char_u *dir, int prot);
7837
7838/*
7839 * Create the directory in which "dir" is located, and higher levels when
7840 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02007841 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007842 */
7843 static int
7844mkdir_recurse(char_u *dir, int prot)
7845{
7846 char_u *p;
7847 char_u *updir;
7848 int r = FAIL;
7849
7850 /* Get end of directory name in "dir".
7851 * We're done when it's "/" or "c:/". */
7852 p = gettail_sep(dir);
7853 if (p <= get_past_head(dir))
7854 return OK;
7855
7856 /* If the directory exists we're done. Otherwise: create it.*/
7857 updir = vim_strnsave(dir, (int)(p - dir));
7858 if (updir == NULL)
7859 return FAIL;
7860 if (mch_isdir(updir))
7861 r = OK;
7862 else if (mkdir_recurse(updir, prot) == OK)
7863 r = vim_mkdir_emsg(updir, prot);
7864 vim_free(updir);
7865 return r;
7866}
7867
7868#ifdef vim_mkdir
7869/*
7870 * "mkdir()" function
7871 */
7872 static void
7873f_mkdir(typval_T *argvars, typval_T *rettv)
7874{
7875 char_u *dir;
7876 char_u buf[NUMBUFLEN];
7877 int prot = 0755;
7878
7879 rettv->vval.v_number = FAIL;
7880 if (check_restricted() || check_secure())
7881 return;
7882
7883 dir = get_tv_string_buf(&argvars[0], buf);
7884 if (*dir == NUL)
7885 rettv->vval.v_number = FAIL;
7886 else
7887 {
7888 if (*gettail(dir) == NUL)
7889 /* remove trailing slashes */
7890 *gettail_sep(dir) = NUL;
7891
7892 if (argvars[1].v_type != VAR_UNKNOWN)
7893 {
7894 if (argvars[2].v_type != VAR_UNKNOWN)
7895 prot = (int)get_tv_number_chk(&argvars[2], NULL);
7896 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
7897 mkdir_recurse(dir, prot);
7898 }
7899 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
7900 }
7901}
7902#endif
7903
7904/*
7905 * "mode()" function
7906 */
7907 static void
7908f_mode(typval_T *argvars, typval_T *rettv)
7909{
7910 char_u buf[3];
7911
7912 buf[1] = NUL;
7913 buf[2] = NUL;
7914
7915 if (time_for_testing == 93784)
7916 {
7917 /* Testing the two-character code. */
7918 buf[0] = 'x';
7919 buf[1] = '!';
7920 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02007921#ifdef FEAT_TERMINAL
7922 else if (term_use_loop())
7923 buf[0] = 't';
7924#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007925 else if (VIsual_active)
7926 {
7927 if (VIsual_select)
7928 buf[0] = VIsual_mode + 's' - 'v';
7929 else
7930 buf[0] = VIsual_mode;
7931 }
7932 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
7933 || State == CONFIRM)
7934 {
7935 buf[0] = 'r';
7936 if (State == ASKMORE)
7937 buf[1] = 'm';
7938 else if (State == CONFIRM)
7939 buf[1] = '?';
7940 }
7941 else if (State == EXTERNCMD)
7942 buf[0] = '!';
7943 else if (State & INSERT)
7944 {
7945#ifdef FEAT_VREPLACE
7946 if (State & VREPLACE_FLAG)
7947 {
7948 buf[0] = 'R';
7949 buf[1] = 'v';
7950 }
7951 else
7952#endif
Bram Moolenaare90858d2017-02-01 17:24:34 +01007953 {
7954 if (State & REPLACE_FLAG)
7955 buf[0] = 'R';
7956 else
7957 buf[0] = 'i';
7958#ifdef FEAT_INS_EXPAND
7959 if (ins_compl_active())
7960 buf[1] = 'c';
7961 else if (ctrl_x_mode == 1)
7962 buf[1] = 'x';
7963#endif
7964 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007965 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01007966 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007967 {
7968 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01007969 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007970 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01007971 else if (exmode_active == EXMODE_NORMAL)
7972 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007973 }
7974 else
7975 {
7976 buf[0] = 'n';
7977 if (finish_op)
7978 buf[1] = 'o';
7979 }
7980
7981 /* Clear out the minor mode when the argument is not a non-zero number or
7982 * non-empty string. */
7983 if (!non_zero_arg(&argvars[0]))
7984 buf[1] = NUL;
7985
7986 rettv->vval.v_string = vim_strsave(buf);
7987 rettv->v_type = VAR_STRING;
7988}
7989
7990#if defined(FEAT_MZSCHEME) || defined(PROTO)
7991/*
7992 * "mzeval()" function
7993 */
7994 static void
7995f_mzeval(typval_T *argvars, typval_T *rettv)
7996{
7997 char_u *str;
7998 char_u buf[NUMBUFLEN];
7999
8000 str = get_tv_string_buf(&argvars[0], buf);
8001 do_mzeval(str, rettv);
8002}
8003
8004 void
8005mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8006{
8007 typval_T argvars[3];
8008
8009 argvars[0].v_type = VAR_STRING;
8010 argvars[0].vval.v_string = name;
8011 copy_tv(args, &argvars[1]);
8012 argvars[2].v_type = VAR_UNKNOWN;
8013 f_call(argvars, rettv);
8014 clear_tv(&argvars[1]);
8015}
8016#endif
8017
8018/*
8019 * "nextnonblank()" function
8020 */
8021 static void
8022f_nextnonblank(typval_T *argvars, typval_T *rettv)
8023{
8024 linenr_T lnum;
8025
8026 for (lnum = get_tv_lnum(argvars); ; ++lnum)
8027 {
8028 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8029 {
8030 lnum = 0;
8031 break;
8032 }
8033 if (*skipwhite(ml_get(lnum)) != NUL)
8034 break;
8035 }
8036 rettv->vval.v_number = lnum;
8037}
8038
8039/*
8040 * "nr2char()" function
8041 */
8042 static void
8043f_nr2char(typval_T *argvars, typval_T *rettv)
8044{
8045 char_u buf[NUMBUFLEN];
8046
8047#ifdef FEAT_MBYTE
8048 if (has_mbyte)
8049 {
8050 int utf8 = 0;
8051
8052 if (argvars[1].v_type != VAR_UNKNOWN)
8053 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
8054 if (utf8)
8055 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8056 else
8057 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8058 }
8059 else
8060#endif
8061 {
8062 buf[0] = (char_u)get_tv_number(&argvars[0]);
8063 buf[1] = NUL;
8064 }
8065 rettv->v_type = VAR_STRING;
8066 rettv->vval.v_string = vim_strsave(buf);
8067}
8068
8069/*
8070 * "or(expr, expr)" function
8071 */
8072 static void
8073f_or(typval_T *argvars, typval_T *rettv)
8074{
8075 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8076 | get_tv_number_chk(&argvars[1], NULL);
8077}
8078
8079/*
8080 * "pathshorten()" function
8081 */
8082 static void
8083f_pathshorten(typval_T *argvars, typval_T *rettv)
8084{
8085 char_u *p;
8086
8087 rettv->v_type = VAR_STRING;
8088 p = get_tv_string_chk(&argvars[0]);
8089 if (p == NULL)
8090 rettv->vval.v_string = NULL;
8091 else
8092 {
8093 p = vim_strsave(p);
8094 rettv->vval.v_string = p;
8095 if (p != NULL)
8096 shorten_dir(p);
8097 }
8098}
8099
8100#ifdef FEAT_PERL
8101/*
8102 * "perleval()" function
8103 */
8104 static void
8105f_perleval(typval_T *argvars, typval_T *rettv)
8106{
8107 char_u *str;
8108 char_u buf[NUMBUFLEN];
8109
8110 str = get_tv_string_buf(&argvars[0], buf);
8111 do_perleval(str, rettv);
8112}
8113#endif
8114
8115#ifdef FEAT_FLOAT
8116/*
8117 * "pow()" function
8118 */
8119 static void
8120f_pow(typval_T *argvars, typval_T *rettv)
8121{
8122 float_T fx = 0.0, fy = 0.0;
8123
8124 rettv->v_type = VAR_FLOAT;
8125 if (get_float_arg(argvars, &fx) == OK
8126 && get_float_arg(&argvars[1], &fy) == OK)
8127 rettv->vval.v_float = pow(fx, fy);
8128 else
8129 rettv->vval.v_float = 0.0;
8130}
8131#endif
8132
8133/*
8134 * "prevnonblank()" function
8135 */
8136 static void
8137f_prevnonblank(typval_T *argvars, typval_T *rettv)
8138{
8139 linenr_T lnum;
8140
8141 lnum = get_tv_lnum(argvars);
8142 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8143 lnum = 0;
8144 else
8145 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8146 --lnum;
8147 rettv->vval.v_number = lnum;
8148}
8149
8150/* This dummy va_list is here because:
8151 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8152 * - locally in the function results in a "used before set" warning
8153 * - using va_start() to initialize it gives "function with fixed args" error */
8154static va_list ap;
8155
8156/*
8157 * "printf()" function
8158 */
8159 static void
8160f_printf(typval_T *argvars, typval_T *rettv)
8161{
8162 char_u buf[NUMBUFLEN];
8163 int len;
8164 char_u *s;
8165 int saved_did_emsg = did_emsg;
8166 char *fmt;
8167
8168 rettv->v_type = VAR_STRING;
8169 rettv->vval.v_string = NULL;
8170
8171 /* Get the required length, allocate the buffer and do it for real. */
8172 did_emsg = FALSE;
8173 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008174 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008175 if (!did_emsg)
8176 {
8177 s = alloc(len + 1);
8178 if (s != NULL)
8179 {
8180 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008181 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8182 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008183 }
8184 }
8185 did_emsg |= saved_did_emsg;
8186}
8187
8188/*
8189 * "pumvisible()" function
8190 */
8191 static void
8192f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8193{
8194#ifdef FEAT_INS_EXPAND
8195 if (pum_visible())
8196 rettv->vval.v_number = 1;
8197#endif
8198}
8199
8200#ifdef FEAT_PYTHON3
8201/*
8202 * "py3eval()" function
8203 */
8204 static void
8205f_py3eval(typval_T *argvars, typval_T *rettv)
8206{
8207 char_u *str;
8208 char_u buf[NUMBUFLEN];
8209
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008210 if (p_pyx == 0)
8211 p_pyx = 3;
8212
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008213 str = get_tv_string_buf(&argvars[0], buf);
8214 do_py3eval(str, rettv);
8215}
8216#endif
8217
8218#ifdef FEAT_PYTHON
8219/*
8220 * "pyeval()" function
8221 */
8222 static void
8223f_pyeval(typval_T *argvars, typval_T *rettv)
8224{
8225 char_u *str;
8226 char_u buf[NUMBUFLEN];
8227
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008228 if (p_pyx == 0)
8229 p_pyx = 2;
8230
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008231 str = get_tv_string_buf(&argvars[0], buf);
8232 do_pyeval(str, rettv);
8233}
8234#endif
8235
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008236#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8237/*
8238 * "pyxeval()" function
8239 */
8240 static void
8241f_pyxeval(typval_T *argvars, typval_T *rettv)
8242{
8243# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8244 init_pyxversion();
8245 if (p_pyx == 2)
8246 f_pyeval(argvars, rettv);
8247 else
8248 f_py3eval(argvars, rettv);
8249# elif defined(FEAT_PYTHON)
8250 f_pyeval(argvars, rettv);
8251# elif defined(FEAT_PYTHON3)
8252 f_py3eval(argvars, rettv);
8253# endif
8254}
8255#endif
8256
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008257/*
8258 * "range()" function
8259 */
8260 static void
8261f_range(typval_T *argvars, typval_T *rettv)
8262{
8263 varnumber_T start;
8264 varnumber_T end;
8265 varnumber_T stride = 1;
8266 varnumber_T i;
8267 int error = FALSE;
8268
8269 start = get_tv_number_chk(&argvars[0], &error);
8270 if (argvars[1].v_type == VAR_UNKNOWN)
8271 {
8272 end = start - 1;
8273 start = 0;
8274 }
8275 else
8276 {
8277 end = get_tv_number_chk(&argvars[1], &error);
8278 if (argvars[2].v_type != VAR_UNKNOWN)
8279 stride = get_tv_number_chk(&argvars[2], &error);
8280 }
8281
8282 if (error)
8283 return; /* type error; errmsg already given */
8284 if (stride == 0)
8285 EMSG(_("E726: Stride is zero"));
8286 else if (stride > 0 ? end + 1 < start : end - 1 > start)
8287 EMSG(_("E727: Start past end"));
8288 else
8289 {
8290 if (rettv_list_alloc(rettv) == OK)
8291 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8292 if (list_append_number(rettv->vval.v_list,
8293 (varnumber_T)i) == FAIL)
8294 break;
8295 }
8296}
8297
8298/*
8299 * "readfile()" function
8300 */
8301 static void
8302f_readfile(typval_T *argvars, typval_T *rettv)
8303{
8304 int binary = FALSE;
8305 int failed = FALSE;
8306 char_u *fname;
8307 FILE *fd;
8308 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8309 int io_size = sizeof(buf);
8310 int readlen; /* size of last fread() */
8311 char_u *prev = NULL; /* previously read bytes, if any */
8312 long prevlen = 0; /* length of data in prev */
8313 long prevsize = 0; /* size of prev buffer */
8314 long maxline = MAXLNUM;
8315 long cnt = 0;
8316 char_u *p; /* position in buf */
8317 char_u *start; /* start of current line */
8318
8319 if (argvars[1].v_type != VAR_UNKNOWN)
8320 {
8321 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
8322 binary = TRUE;
8323 if (argvars[2].v_type != VAR_UNKNOWN)
8324 maxline = (long)get_tv_number(&argvars[2]);
8325 }
8326
8327 if (rettv_list_alloc(rettv) == FAIL)
8328 return;
8329
8330 /* Always open the file in binary mode, library functions have a mind of
8331 * their own about CR-LF conversion. */
8332 fname = get_tv_string(&argvars[0]);
8333 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8334 {
8335 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
8336 return;
8337 }
8338
8339 while (cnt < maxline || maxline < 0)
8340 {
8341 readlen = (int)fread(buf, 1, io_size, fd);
8342
8343 /* This for loop processes what was read, but is also entered at end
8344 * of file so that either:
8345 * - an incomplete line gets written
8346 * - a "binary" file gets an empty line at the end if it ends in a
8347 * newline. */
8348 for (p = buf, start = buf;
8349 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8350 ++p)
8351 {
8352 if (*p == '\n' || readlen <= 0)
8353 {
8354 listitem_T *li;
8355 char_u *s = NULL;
8356 long_u len = p - start;
8357
8358 /* Finished a line. Remove CRs before NL. */
8359 if (readlen > 0 && !binary)
8360 {
8361 while (len > 0 && start[len - 1] == '\r')
8362 --len;
8363 /* removal may cross back to the "prev" string */
8364 if (len == 0)
8365 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8366 --prevlen;
8367 }
8368 if (prevlen == 0)
8369 s = vim_strnsave(start, (int)len);
8370 else
8371 {
8372 /* Change "prev" buffer to be the right size. This way
8373 * the bytes are only copied once, and very long lines are
8374 * allocated only once. */
8375 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8376 {
8377 mch_memmove(s + prevlen, start, len);
8378 s[prevlen + len] = NUL;
8379 prev = NULL; /* the list will own the string */
8380 prevlen = prevsize = 0;
8381 }
8382 }
8383 if (s == NULL)
8384 {
8385 do_outofmem_msg((long_u) prevlen + len + 1);
8386 failed = TRUE;
8387 break;
8388 }
8389
8390 if ((li = listitem_alloc()) == NULL)
8391 {
8392 vim_free(s);
8393 failed = TRUE;
8394 break;
8395 }
8396 li->li_tv.v_type = VAR_STRING;
8397 li->li_tv.v_lock = 0;
8398 li->li_tv.vval.v_string = s;
8399 list_append(rettv->vval.v_list, li);
8400
8401 start = p + 1; /* step over newline */
8402 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
8403 break;
8404 }
8405 else if (*p == NUL)
8406 *p = '\n';
8407#ifdef FEAT_MBYTE
8408 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
8409 * when finding the BF and check the previous two bytes. */
8410 else if (*p == 0xbf && enc_utf8 && !binary)
8411 {
8412 /* Find the two bytes before the 0xbf. If p is at buf, or buf
8413 * + 1, these may be in the "prev" string. */
8414 char_u back1 = p >= buf + 1 ? p[-1]
8415 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
8416 char_u back2 = p >= buf + 2 ? p[-2]
8417 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
8418 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
8419
8420 if (back2 == 0xef && back1 == 0xbb)
8421 {
8422 char_u *dest = p - 2;
8423
8424 /* Usually a BOM is at the beginning of a file, and so at
8425 * the beginning of a line; then we can just step over it.
8426 */
8427 if (start == dest)
8428 start = p + 1;
8429 else
8430 {
8431 /* have to shuffle buf to close gap */
8432 int adjust_prevlen = 0;
8433
8434 if (dest < buf)
8435 {
8436 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
8437 dest = buf;
8438 }
8439 if (readlen > p - buf + 1)
8440 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
8441 readlen -= 3 - adjust_prevlen;
8442 prevlen -= adjust_prevlen;
8443 p = dest - 1;
8444 }
8445 }
8446 }
8447#endif
8448 } /* for */
8449
8450 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
8451 break;
8452 if (start < p)
8453 {
8454 /* There's part of a line in buf, store it in "prev". */
8455 if (p - start + prevlen >= prevsize)
8456 {
8457 /* need bigger "prev" buffer */
8458 char_u *newprev;
8459
8460 /* A common use case is ordinary text files and "prev" gets a
8461 * fragment of a line, so the first allocation is made
8462 * small, to avoid repeatedly 'allocing' large and
8463 * 'reallocing' small. */
8464 if (prevsize == 0)
8465 prevsize = (long)(p - start);
8466 else
8467 {
8468 long grow50pc = (prevsize * 3) / 2;
8469 long growmin = (long)((p - start) * 2 + prevlen);
8470 prevsize = grow50pc > growmin ? grow50pc : growmin;
8471 }
8472 newprev = prev == NULL ? alloc(prevsize)
8473 : vim_realloc(prev, prevsize);
8474 if (newprev == NULL)
8475 {
8476 do_outofmem_msg((long_u)prevsize);
8477 failed = TRUE;
8478 break;
8479 }
8480 prev = newprev;
8481 }
8482 /* Add the line part to end of "prev". */
8483 mch_memmove(prev + prevlen, start, p - start);
8484 prevlen += (long)(p - start);
8485 }
8486 } /* while */
8487
8488 /*
8489 * For a negative line count use only the lines at the end of the file,
8490 * free the rest.
8491 */
8492 if (!failed && maxline < 0)
8493 while (cnt > -maxline)
8494 {
8495 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
8496 --cnt;
8497 }
8498
8499 if (failed)
8500 {
8501 list_free(rettv->vval.v_list);
8502 /* readfile doc says an empty list is returned on error */
8503 rettv->vval.v_list = list_alloc();
8504 }
8505
8506 vim_free(prev);
8507 fclose(fd);
8508}
8509
8510#if defined(FEAT_RELTIME)
8511static int list2proftime(typval_T *arg, proftime_T *tm);
8512
8513/*
8514 * Convert a List to proftime_T.
8515 * Return FAIL when there is something wrong.
8516 */
8517 static int
8518list2proftime(typval_T *arg, proftime_T *tm)
8519{
8520 long n1, n2;
8521 int error = FALSE;
8522
8523 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
8524 || arg->vval.v_list->lv_len != 2)
8525 return FAIL;
8526 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
8527 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
8528# ifdef WIN3264
8529 tm->HighPart = n1;
8530 tm->LowPart = n2;
8531# else
8532 tm->tv_sec = n1;
8533 tm->tv_usec = n2;
8534# endif
8535 return error ? FAIL : OK;
8536}
8537#endif /* FEAT_RELTIME */
8538
8539/*
8540 * "reltime()" function
8541 */
8542 static void
8543f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8544{
8545#ifdef FEAT_RELTIME
8546 proftime_T res;
8547 proftime_T start;
8548
8549 if (argvars[0].v_type == VAR_UNKNOWN)
8550 {
8551 /* No arguments: get current time. */
8552 profile_start(&res);
8553 }
8554 else if (argvars[1].v_type == VAR_UNKNOWN)
8555 {
8556 if (list2proftime(&argvars[0], &res) == FAIL)
8557 return;
8558 profile_end(&res);
8559 }
8560 else
8561 {
8562 /* Two arguments: compute the difference. */
8563 if (list2proftime(&argvars[0], &start) == FAIL
8564 || list2proftime(&argvars[1], &res) == FAIL)
8565 return;
8566 profile_sub(&res, &start);
8567 }
8568
8569 if (rettv_list_alloc(rettv) == OK)
8570 {
8571 long n1, n2;
8572
8573# ifdef WIN3264
8574 n1 = res.HighPart;
8575 n2 = res.LowPart;
8576# else
8577 n1 = res.tv_sec;
8578 n2 = res.tv_usec;
8579# endif
8580 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
8581 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
8582 }
8583#endif
8584}
8585
8586#ifdef FEAT_FLOAT
8587/*
8588 * "reltimefloat()" function
8589 */
8590 static void
8591f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
8592{
8593# ifdef FEAT_RELTIME
8594 proftime_T tm;
8595# endif
8596
8597 rettv->v_type = VAR_FLOAT;
8598 rettv->vval.v_float = 0;
8599# ifdef FEAT_RELTIME
8600 if (list2proftime(&argvars[0], &tm) == OK)
8601 rettv->vval.v_float = profile_float(&tm);
8602# endif
8603}
8604#endif
8605
8606/*
8607 * "reltimestr()" function
8608 */
8609 static void
8610f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
8611{
8612#ifdef FEAT_RELTIME
8613 proftime_T tm;
8614#endif
8615
8616 rettv->v_type = VAR_STRING;
8617 rettv->vval.v_string = NULL;
8618#ifdef FEAT_RELTIME
8619 if (list2proftime(&argvars[0], &tm) == OK)
8620 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
8621#endif
8622}
8623
8624#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
8625static void make_connection(void);
8626static int check_connection(void);
8627
8628 static void
8629make_connection(void)
8630{
8631 if (X_DISPLAY == NULL
8632# ifdef FEAT_GUI
8633 && !gui.in_use
8634# endif
8635 )
8636 {
8637 x_force_connect = TRUE;
8638 setup_term_clip();
8639 x_force_connect = FALSE;
8640 }
8641}
8642
8643 static int
8644check_connection(void)
8645{
8646 make_connection();
8647 if (X_DISPLAY == NULL)
8648 {
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01008649 EMSG(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008650 return FAIL;
8651 }
8652 return OK;
8653}
8654#endif
8655
8656#ifdef FEAT_CLIENTSERVER
8657 static void
8658remote_common(typval_T *argvars, typval_T *rettv, int expr)
8659{
8660 char_u *server_name;
8661 char_u *keys;
8662 char_u *r = NULL;
8663 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008664 int timeout = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008665# ifdef WIN32
8666 HWND w;
8667# else
8668 Window w;
8669# endif
8670
8671 if (check_restricted() || check_secure())
8672 return;
8673
8674# ifdef FEAT_X11
8675 if (check_connection() == FAIL)
8676 return;
8677# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008678 if (argvars[2].v_type != VAR_UNKNOWN
8679 && argvars[3].v_type != VAR_UNKNOWN)
8680 timeout = get_tv_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008681
8682 server_name = get_tv_string_chk(&argvars[0]);
8683 if (server_name == NULL)
8684 return; /* type error; errmsg already given */
8685 keys = get_tv_string_buf(&argvars[1], buf);
8686# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008687 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008688# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008689 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
8690 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008691# endif
8692 {
8693 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008694 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008695 EMSG(r); /* sending worked but evaluation failed */
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008696 vim_free(r);
8697 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008698 else
8699 EMSG2(_("E241: Unable to send to %s"), server_name);
8700 return;
8701 }
8702
8703 rettv->vval.v_string = r;
8704
8705 if (argvars[2].v_type != VAR_UNKNOWN)
8706 {
8707 dictitem_T v;
8708 char_u str[30];
8709 char_u *idvar;
8710
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008711 idvar = get_tv_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008712 if (idvar != NULL && *idvar != NUL)
8713 {
8714 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
8715 v.di_tv.v_type = VAR_STRING;
8716 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008717 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008718 vim_free(v.di_tv.vval.v_string);
8719 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008720 }
8721}
8722#endif
8723
8724/*
8725 * "remote_expr()" function
8726 */
8727 static void
8728f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
8729{
8730 rettv->v_type = VAR_STRING;
8731 rettv->vval.v_string = NULL;
8732#ifdef FEAT_CLIENTSERVER
8733 remote_common(argvars, rettv, TRUE);
8734#endif
8735}
8736
8737/*
8738 * "remote_foreground()" function
8739 */
8740 static void
8741f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8742{
8743#ifdef FEAT_CLIENTSERVER
8744# ifdef WIN32
8745 /* On Win32 it's done in this application. */
8746 {
8747 char_u *server_name = get_tv_string_chk(&argvars[0]);
8748
8749 if (server_name != NULL)
8750 serverForeground(server_name);
8751 }
8752# else
8753 /* Send a foreground() expression to the server. */
8754 argvars[1].v_type = VAR_STRING;
8755 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
8756 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008757 rettv->v_type = VAR_STRING;
8758 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008759 remote_common(argvars, rettv, TRUE);
8760 vim_free(argvars[1].vval.v_string);
8761# endif
8762#endif
8763}
8764
8765 static void
8766f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
8767{
8768#ifdef FEAT_CLIENTSERVER
8769 dictitem_T v;
8770 char_u *s = NULL;
8771# ifdef WIN32
8772 long_u n = 0;
8773# endif
8774 char_u *serverid;
8775
8776 if (check_restricted() || check_secure())
8777 {
8778 rettv->vval.v_number = -1;
8779 return;
8780 }
8781 serverid = get_tv_string_chk(&argvars[0]);
8782 if (serverid == NULL)
8783 {
8784 rettv->vval.v_number = -1;
8785 return; /* type error; errmsg already given */
8786 }
8787# ifdef WIN32
8788 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
8789 if (n == 0)
8790 rettv->vval.v_number = -1;
8791 else
8792 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008793 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008794 rettv->vval.v_number = (s != NULL);
8795 }
8796# else
8797 if (check_connection() == FAIL)
8798 return;
8799
8800 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
8801 serverStrToWin(serverid), &s);
8802# endif
8803
8804 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
8805 {
8806 char_u *retvar;
8807
8808 v.di_tv.v_type = VAR_STRING;
8809 v.di_tv.vval.v_string = vim_strsave(s);
8810 retvar = get_tv_string_chk(&argvars[1]);
8811 if (retvar != NULL)
8812 set_var(retvar, &v.di_tv, FALSE);
8813 vim_free(v.di_tv.vval.v_string);
8814 }
8815#else
8816 rettv->vval.v_number = -1;
8817#endif
8818}
8819
8820 static void
8821f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
8822{
8823 char_u *r = NULL;
8824
8825#ifdef FEAT_CLIENTSERVER
8826 char_u *serverid = get_tv_string_chk(&argvars[0]);
8827
8828 if (serverid != NULL && !check_restricted() && !check_secure())
8829 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008830 int timeout = 0;
Bram Moolenaar1662ce12017-03-19 21:47:50 +01008831# ifdef WIN32
8832 /* The server's HWND is encoded in the 'id' parameter */
8833 long_u n = 0;
8834# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008835
8836 if (argvars[1].v_type != VAR_UNKNOWN)
8837 timeout = get_tv_number(&argvars[1]);
8838
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008839# ifdef WIN32
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008840 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
8841 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008842 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008843 if (r == NULL)
8844# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008845 if (check_connection() == FAIL
8846 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
8847 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008848# endif
8849 EMSG(_("E277: Unable to read a server reply"));
8850 }
8851#endif
8852 rettv->v_type = VAR_STRING;
8853 rettv->vval.v_string = r;
8854}
8855
8856/*
8857 * "remote_send()" function
8858 */
8859 static void
8860f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
8861{
8862 rettv->v_type = VAR_STRING;
8863 rettv->vval.v_string = NULL;
8864#ifdef FEAT_CLIENTSERVER
8865 remote_common(argvars, rettv, FALSE);
8866#endif
8867}
8868
8869/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01008870 * "remote_startserver()" function
8871 */
8872 static void
8873f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8874{
8875#ifdef FEAT_CLIENTSERVER
8876 char_u *server = get_tv_string_chk(&argvars[0]);
8877
8878 if (server == NULL)
8879 return; /* type error; errmsg already given */
8880 if (serverName != NULL)
8881 EMSG(_("E941: already started a server"));
8882 else
8883 {
8884# ifdef FEAT_X11
8885 if (check_connection() == OK)
8886 serverRegisterName(X_DISPLAY, server);
8887# else
8888 serverSetName(server);
8889# endif
8890 }
8891#else
8892 EMSG(_("E942: +clientserver feature not available"));
8893#endif
8894}
8895
8896/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008897 * "remove()" function
8898 */
8899 static void
8900f_remove(typval_T *argvars, typval_T *rettv)
8901{
8902 list_T *l;
8903 listitem_T *item, *item2;
8904 listitem_T *li;
8905 long idx;
8906 long end;
8907 char_u *key;
8908 dict_T *d;
8909 dictitem_T *di;
8910 char_u *arg_errmsg = (char_u *)N_("remove() argument");
8911
8912 if (argvars[0].v_type == VAR_DICT)
8913 {
8914 if (argvars[2].v_type != VAR_UNKNOWN)
8915 EMSG2(_(e_toomanyarg), "remove()");
8916 else if ((d = argvars[0].vval.v_dict) != NULL
8917 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
8918 {
8919 key = get_tv_string_chk(&argvars[1]);
8920 if (key != NULL)
8921 {
8922 di = dict_find(d, key, -1);
8923 if (di == NULL)
8924 EMSG2(_(e_dictkey), key);
8925 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
8926 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
8927 {
8928 *rettv = di->di_tv;
8929 init_tv(&di->di_tv);
8930 dictitem_remove(d, di);
8931 }
8932 }
8933 }
8934 }
8935 else if (argvars[0].v_type != VAR_LIST)
8936 EMSG2(_(e_listdictarg), "remove()");
8937 else if ((l = argvars[0].vval.v_list) != NULL
8938 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
8939 {
8940 int error = FALSE;
8941
8942 idx = (long)get_tv_number_chk(&argvars[1], &error);
8943 if (error)
8944 ; /* type error: do nothing, errmsg already given */
8945 else if ((item = list_find(l, idx)) == NULL)
8946 EMSGN(_(e_listidx), idx);
8947 else
8948 {
8949 if (argvars[2].v_type == VAR_UNKNOWN)
8950 {
8951 /* Remove one item, return its value. */
8952 vimlist_remove(l, item, item);
8953 *rettv = item->li_tv;
8954 vim_free(item);
8955 }
8956 else
8957 {
8958 /* Remove range of items, return list with values. */
8959 end = (long)get_tv_number_chk(&argvars[2], &error);
8960 if (error)
8961 ; /* type error: do nothing */
8962 else if ((item2 = list_find(l, end)) == NULL)
8963 EMSGN(_(e_listidx), end);
8964 else
8965 {
8966 int cnt = 0;
8967
8968 for (li = item; li != NULL; li = li->li_next)
8969 {
8970 ++cnt;
8971 if (li == item2)
8972 break;
8973 }
8974 if (li == NULL) /* didn't find "item2" after "item" */
8975 EMSG(_(e_invrange));
8976 else
8977 {
8978 vimlist_remove(l, item, item2);
8979 if (rettv_list_alloc(rettv) == OK)
8980 {
8981 l = rettv->vval.v_list;
8982 l->lv_first = item;
8983 l->lv_last = item2;
8984 item->li_prev = NULL;
8985 item2->li_next = NULL;
8986 l->lv_len = cnt;
8987 }
8988 }
8989 }
8990 }
8991 }
8992 }
8993}
8994
8995/*
8996 * "rename({from}, {to})" function
8997 */
8998 static void
8999f_rename(typval_T *argvars, typval_T *rettv)
9000{
9001 char_u buf[NUMBUFLEN];
9002
9003 if (check_restricted() || check_secure())
9004 rettv->vval.v_number = -1;
9005 else
9006 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
9007 get_tv_string_buf(&argvars[1], buf));
9008}
9009
9010/*
9011 * "repeat()" function
9012 */
9013 static void
9014f_repeat(typval_T *argvars, typval_T *rettv)
9015{
9016 char_u *p;
9017 int n;
9018 int slen;
9019 int len;
9020 char_u *r;
9021 int i;
9022
9023 n = (int)get_tv_number(&argvars[1]);
9024 if (argvars[0].v_type == VAR_LIST)
9025 {
9026 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9027 while (n-- > 0)
9028 if (list_extend(rettv->vval.v_list,
9029 argvars[0].vval.v_list, NULL) == FAIL)
9030 break;
9031 }
9032 else
9033 {
9034 p = get_tv_string(&argvars[0]);
9035 rettv->v_type = VAR_STRING;
9036 rettv->vval.v_string = NULL;
9037
9038 slen = (int)STRLEN(p);
9039 len = slen * n;
9040 if (len <= 0)
9041 return;
9042
9043 r = alloc(len + 1);
9044 if (r != NULL)
9045 {
9046 for (i = 0; i < n; i++)
9047 mch_memmove(r + i * slen, p, (size_t)slen);
9048 r[len] = NUL;
9049 }
9050
9051 rettv->vval.v_string = r;
9052 }
9053}
9054
9055/*
9056 * "resolve()" function
9057 */
9058 static void
9059f_resolve(typval_T *argvars, typval_T *rettv)
9060{
9061 char_u *p;
9062#ifdef HAVE_READLINK
9063 char_u *buf = NULL;
9064#endif
9065
9066 p = get_tv_string(&argvars[0]);
9067#ifdef FEAT_SHORTCUT
9068 {
9069 char_u *v = NULL;
9070
9071 v = mch_resolve_shortcut(p);
9072 if (v != NULL)
9073 rettv->vval.v_string = v;
9074 else
9075 rettv->vval.v_string = vim_strsave(p);
9076 }
9077#else
9078# ifdef HAVE_READLINK
9079 {
9080 char_u *cpy;
9081 int len;
9082 char_u *remain = NULL;
9083 char_u *q;
9084 int is_relative_to_current = FALSE;
9085 int has_trailing_pathsep = FALSE;
9086 int limit = 100;
9087
9088 p = vim_strsave(p);
9089
9090 if (p[0] == '.' && (vim_ispathsep(p[1])
9091 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9092 is_relative_to_current = TRUE;
9093
9094 len = STRLEN(p);
9095 if (len > 0 && after_pathsep(p, p + len))
9096 {
9097 has_trailing_pathsep = TRUE;
9098 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9099 }
9100
9101 q = getnextcomp(p);
9102 if (*q != NUL)
9103 {
9104 /* Separate the first path component in "p", and keep the
9105 * remainder (beginning with the path separator). */
9106 remain = vim_strsave(q - 1);
9107 q[-1] = NUL;
9108 }
9109
9110 buf = alloc(MAXPATHL + 1);
9111 if (buf == NULL)
9112 goto fail;
9113
9114 for (;;)
9115 {
9116 for (;;)
9117 {
9118 len = readlink((char *)p, (char *)buf, MAXPATHL);
9119 if (len <= 0)
9120 break;
9121 buf[len] = NUL;
9122
9123 if (limit-- == 0)
9124 {
9125 vim_free(p);
9126 vim_free(remain);
9127 EMSG(_("E655: Too many symbolic links (cycle?)"));
9128 rettv->vval.v_string = NULL;
9129 goto fail;
9130 }
9131
9132 /* Ensure that the result will have a trailing path separator
9133 * if the argument has one. */
9134 if (remain == NULL && has_trailing_pathsep)
9135 add_pathsep(buf);
9136
9137 /* Separate the first path component in the link value and
9138 * concatenate the remainders. */
9139 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9140 if (*q != NUL)
9141 {
9142 if (remain == NULL)
9143 remain = vim_strsave(q - 1);
9144 else
9145 {
9146 cpy = concat_str(q - 1, remain);
9147 if (cpy != NULL)
9148 {
9149 vim_free(remain);
9150 remain = cpy;
9151 }
9152 }
9153 q[-1] = NUL;
9154 }
9155
9156 q = gettail(p);
9157 if (q > p && *q == NUL)
9158 {
9159 /* Ignore trailing path separator. */
9160 q[-1] = NUL;
9161 q = gettail(p);
9162 }
9163 if (q > p && !mch_isFullName(buf))
9164 {
9165 /* symlink is relative to directory of argument */
9166 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
9167 if (cpy != NULL)
9168 {
9169 STRCPY(cpy, p);
9170 STRCPY(gettail(cpy), buf);
9171 vim_free(p);
9172 p = cpy;
9173 }
9174 }
9175 else
9176 {
9177 vim_free(p);
9178 p = vim_strsave(buf);
9179 }
9180 }
9181
9182 if (remain == NULL)
9183 break;
9184
9185 /* Append the first path component of "remain" to "p". */
9186 q = getnextcomp(remain + 1);
9187 len = q - remain - (*q != NUL);
9188 cpy = vim_strnsave(p, STRLEN(p) + len);
9189 if (cpy != NULL)
9190 {
9191 STRNCAT(cpy, remain, len);
9192 vim_free(p);
9193 p = cpy;
9194 }
9195 /* Shorten "remain". */
9196 if (*q != NUL)
9197 STRMOVE(remain, q - 1);
9198 else
9199 {
9200 vim_free(remain);
9201 remain = NULL;
9202 }
9203 }
9204
9205 /* If the result is a relative path name, make it explicitly relative to
9206 * the current directory if and only if the argument had this form. */
9207 if (!vim_ispathsep(*p))
9208 {
9209 if (is_relative_to_current
9210 && *p != NUL
9211 && !(p[0] == '.'
9212 && (p[1] == NUL
9213 || vim_ispathsep(p[1])
9214 || (p[1] == '.'
9215 && (p[2] == NUL
9216 || vim_ispathsep(p[2]))))))
9217 {
9218 /* Prepend "./". */
9219 cpy = concat_str((char_u *)"./", p);
9220 if (cpy != NULL)
9221 {
9222 vim_free(p);
9223 p = cpy;
9224 }
9225 }
9226 else if (!is_relative_to_current)
9227 {
9228 /* Strip leading "./". */
9229 q = p;
9230 while (q[0] == '.' && vim_ispathsep(q[1]))
9231 q += 2;
9232 if (q > p)
9233 STRMOVE(p, p + 2);
9234 }
9235 }
9236
9237 /* Ensure that the result will have no trailing path separator
9238 * if the argument had none. But keep "/" or "//". */
9239 if (!has_trailing_pathsep)
9240 {
9241 q = p + STRLEN(p);
9242 if (after_pathsep(p, q))
9243 *gettail_sep(p) = NUL;
9244 }
9245
9246 rettv->vval.v_string = p;
9247 }
9248# else
9249 rettv->vval.v_string = vim_strsave(p);
9250# endif
9251#endif
9252
9253 simplify_filename(rettv->vval.v_string);
9254
9255#ifdef HAVE_READLINK
9256fail:
9257 vim_free(buf);
9258#endif
9259 rettv->v_type = VAR_STRING;
9260}
9261
9262/*
9263 * "reverse({list})" function
9264 */
9265 static void
9266f_reverse(typval_T *argvars, typval_T *rettv)
9267{
9268 list_T *l;
9269 listitem_T *li, *ni;
9270
9271 if (argvars[0].v_type != VAR_LIST)
9272 EMSG2(_(e_listarg), "reverse()");
9273 else if ((l = argvars[0].vval.v_list) != NULL
9274 && !tv_check_lock(l->lv_lock,
9275 (char_u *)N_("reverse() argument"), TRUE))
9276 {
9277 li = l->lv_last;
9278 l->lv_first = l->lv_last = NULL;
9279 l->lv_len = 0;
9280 while (li != NULL)
9281 {
9282 ni = li->li_prev;
9283 list_append(l, li);
9284 li = ni;
9285 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009286 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009287 l->lv_idx = l->lv_len - l->lv_idx - 1;
9288 }
9289}
9290
9291#define SP_NOMOVE 0x01 /* don't move cursor */
9292#define SP_REPEAT 0x02 /* repeat to find outer pair */
9293#define SP_RETCOUNT 0x04 /* return matchcount */
9294#define SP_SETPCMARK 0x08 /* set previous context mark */
9295#define SP_START 0x10 /* accept match at start position */
9296#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9297#define SP_END 0x40 /* leave cursor at end of match */
9298#define SP_COLUMN 0x80 /* start at cursor column */
9299
9300static int get_search_arg(typval_T *varp, int *flagsp);
9301
9302/*
9303 * Get flags for a search function.
9304 * Possibly sets "p_ws".
9305 * Returns BACKWARD, FORWARD or zero (for an error).
9306 */
9307 static int
9308get_search_arg(typval_T *varp, int *flagsp)
9309{
9310 int dir = FORWARD;
9311 char_u *flags;
9312 char_u nbuf[NUMBUFLEN];
9313 int mask;
9314
9315 if (varp->v_type != VAR_UNKNOWN)
9316 {
9317 flags = get_tv_string_buf_chk(varp, nbuf);
9318 if (flags == NULL)
9319 return 0; /* type error; errmsg already given */
9320 while (*flags != NUL)
9321 {
9322 switch (*flags)
9323 {
9324 case 'b': dir = BACKWARD; break;
9325 case 'w': p_ws = TRUE; break;
9326 case 'W': p_ws = FALSE; break;
9327 default: mask = 0;
9328 if (flagsp != NULL)
9329 switch (*flags)
9330 {
9331 case 'c': mask = SP_START; break;
9332 case 'e': mask = SP_END; break;
9333 case 'm': mask = SP_RETCOUNT; break;
9334 case 'n': mask = SP_NOMOVE; break;
9335 case 'p': mask = SP_SUBPAT; break;
9336 case 'r': mask = SP_REPEAT; break;
9337 case 's': mask = SP_SETPCMARK; break;
9338 case 'z': mask = SP_COLUMN; break;
9339 }
9340 if (mask == 0)
9341 {
9342 EMSG2(_(e_invarg2), flags);
9343 dir = 0;
9344 }
9345 else
9346 *flagsp |= mask;
9347 }
9348 if (dir == 0)
9349 break;
9350 ++flags;
9351 }
9352 }
9353 return dir;
9354}
9355
9356/*
9357 * Shared by search() and searchpos() functions.
9358 */
9359 static int
9360search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9361{
9362 int flags;
9363 char_u *pat;
9364 pos_T pos;
9365 pos_T save_cursor;
9366 int save_p_ws = p_ws;
9367 int dir;
9368 int retval = 0; /* default: FAIL */
9369 long lnum_stop = 0;
9370 proftime_T tm;
9371#ifdef FEAT_RELTIME
9372 long time_limit = 0;
9373#endif
9374 int options = SEARCH_KEEP;
9375 int subpatnum;
9376
9377 pat = get_tv_string(&argvars[0]);
9378 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
9379 if (dir == 0)
9380 goto theend;
9381 flags = *flagsp;
9382 if (flags & SP_START)
9383 options |= SEARCH_START;
9384 if (flags & SP_END)
9385 options |= SEARCH_END;
9386 if (flags & SP_COLUMN)
9387 options |= SEARCH_COL;
9388
9389 /* Optional arguments: line number to stop searching and timeout. */
9390 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9391 {
9392 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
9393 if (lnum_stop < 0)
9394 goto theend;
9395#ifdef FEAT_RELTIME
9396 if (argvars[3].v_type != VAR_UNKNOWN)
9397 {
9398 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
9399 if (time_limit < 0)
9400 goto theend;
9401 }
9402#endif
9403 }
9404
9405#ifdef FEAT_RELTIME
9406 /* Set the time limit, if there is one. */
9407 profile_setlimit(time_limit, &tm);
9408#endif
9409
9410 /*
9411 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
9412 * Check to make sure only those flags are set.
9413 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
9414 * flags cannot be set. Check for that condition also.
9415 */
9416 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
9417 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9418 {
9419 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
9420 goto theend;
9421 }
9422
9423 pos = save_cursor = curwin->w_cursor;
9424 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009425 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009426 if (subpatnum != FAIL)
9427 {
9428 if (flags & SP_SUBPAT)
9429 retval = subpatnum;
9430 else
9431 retval = pos.lnum;
9432 if (flags & SP_SETPCMARK)
9433 setpcmark();
9434 curwin->w_cursor = pos;
9435 if (match_pos != NULL)
9436 {
9437 /* Store the match cursor position */
9438 match_pos->lnum = pos.lnum;
9439 match_pos->col = pos.col + 1;
9440 }
9441 /* "/$" will put the cursor after the end of the line, may need to
9442 * correct that here */
9443 check_cursor();
9444 }
9445
9446 /* If 'n' flag is used: restore cursor position. */
9447 if (flags & SP_NOMOVE)
9448 curwin->w_cursor = save_cursor;
9449 else
9450 curwin->w_set_curswant = TRUE;
9451theend:
9452 p_ws = save_p_ws;
9453
9454 return retval;
9455}
9456
9457#ifdef FEAT_FLOAT
9458
9459/*
9460 * round() is not in C90, use ceil() or floor() instead.
9461 */
9462 float_T
9463vim_round(float_T f)
9464{
9465 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
9466}
9467
9468/*
9469 * "round({float})" function
9470 */
9471 static void
9472f_round(typval_T *argvars, typval_T *rettv)
9473{
9474 float_T f = 0.0;
9475
9476 rettv->v_type = VAR_FLOAT;
9477 if (get_float_arg(argvars, &f) == OK)
9478 rettv->vval.v_float = vim_round(f);
9479 else
9480 rettv->vval.v_float = 0.0;
9481}
9482#endif
9483
9484/*
9485 * "screenattr()" function
9486 */
9487 static void
9488f_screenattr(typval_T *argvars, typval_T *rettv)
9489{
9490 int row;
9491 int col;
9492 int c;
9493
9494 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9495 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9496 if (row < 0 || row >= screen_Rows
9497 || col < 0 || col >= screen_Columns)
9498 c = -1;
9499 else
9500 c = ScreenAttrs[LineOffset[row] + col];
9501 rettv->vval.v_number = c;
9502}
9503
9504/*
9505 * "screenchar()" function
9506 */
9507 static void
9508f_screenchar(typval_T *argvars, typval_T *rettv)
9509{
9510 int row;
9511 int col;
9512 int off;
9513 int c;
9514
9515 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9516 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9517 if (row < 0 || row >= screen_Rows
9518 || col < 0 || col >= screen_Columns)
9519 c = -1;
9520 else
9521 {
9522 off = LineOffset[row] + col;
9523#ifdef FEAT_MBYTE
9524 if (enc_utf8 && ScreenLinesUC[off] != 0)
9525 c = ScreenLinesUC[off];
9526 else
9527#endif
9528 c = ScreenLines[off];
9529 }
9530 rettv->vval.v_number = c;
9531}
9532
9533/*
9534 * "screencol()" function
9535 *
9536 * First column is 1 to be consistent with virtcol().
9537 */
9538 static void
9539f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
9540{
9541 rettv->vval.v_number = screen_screencol() + 1;
9542}
9543
9544/*
9545 * "screenrow()" function
9546 */
9547 static void
9548f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
9549{
9550 rettv->vval.v_number = screen_screenrow() + 1;
9551}
9552
9553/*
9554 * "search()" function
9555 */
9556 static void
9557f_search(typval_T *argvars, typval_T *rettv)
9558{
9559 int flags = 0;
9560
9561 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
9562}
9563
9564/*
9565 * "searchdecl()" function
9566 */
9567 static void
9568f_searchdecl(typval_T *argvars, typval_T *rettv)
9569{
9570 int locally = 1;
9571 int thisblock = 0;
9572 int error = FALSE;
9573 char_u *name;
9574
9575 rettv->vval.v_number = 1; /* default: FAIL */
9576
9577 name = get_tv_string_chk(&argvars[0]);
9578 if (argvars[1].v_type != VAR_UNKNOWN)
9579 {
9580 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
9581 if (!error && argvars[2].v_type != VAR_UNKNOWN)
9582 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
9583 }
9584 if (!error && name != NULL)
9585 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
9586 locally, thisblock, SEARCH_KEEP) == FAIL;
9587}
9588
9589/*
9590 * Used by searchpair() and searchpairpos()
9591 */
9592 static int
9593searchpair_cmn(typval_T *argvars, pos_T *match_pos)
9594{
9595 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01009596 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009597 int save_p_ws = p_ws;
9598 int dir;
9599 int flags = 0;
9600 char_u nbuf1[NUMBUFLEN];
9601 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009602 int retval = 0; /* default: FAIL */
9603 long lnum_stop = 0;
9604 long time_limit = 0;
9605
9606 /* Get the three pattern arguments: start, middle, end. */
9607 spat = get_tv_string_chk(&argvars[0]);
9608 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
9609 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
9610 if (spat == NULL || mpat == NULL || epat == NULL)
9611 goto theend; /* type error */
9612
9613 /* Handle the optional fourth argument: flags */
9614 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
9615 if (dir == 0)
9616 goto theend;
9617
9618 /* Don't accept SP_END or SP_SUBPAT.
9619 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
9620 */
9621 if ((flags & (SP_END | SP_SUBPAT)) != 0
9622 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9623 {
9624 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
9625 goto theend;
9626 }
9627
9628 /* Using 'r' implies 'W', otherwise it doesn't work. */
9629 if (flags & SP_REPEAT)
9630 p_ws = FALSE;
9631
9632 /* Optional fifth argument: skip expression */
9633 if (argvars[3].v_type == VAR_UNKNOWN
9634 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01009635 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009636 else
9637 {
Bram Moolenaar48570482017-10-30 21:48:41 +01009638 skip = &argvars[4];
9639 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
9640 && skip->v_type != VAR_STRING)
9641 {
9642 /* Type error */
9643 goto theend;
9644 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009645 if (argvars[5].v_type != VAR_UNKNOWN)
9646 {
9647 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
9648 if (lnum_stop < 0)
9649 goto theend;
9650#ifdef FEAT_RELTIME
9651 if (argvars[6].v_type != VAR_UNKNOWN)
9652 {
9653 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
9654 if (time_limit < 0)
9655 goto theend;
9656 }
9657#endif
9658 }
9659 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009660
9661 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
9662 match_pos, lnum_stop, time_limit);
9663
9664theend:
9665 p_ws = save_p_ws;
9666
9667 return retval;
9668}
9669
9670/*
9671 * "searchpair()" function
9672 */
9673 static void
9674f_searchpair(typval_T *argvars, typval_T *rettv)
9675{
9676 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
9677}
9678
9679/*
9680 * "searchpairpos()" function
9681 */
9682 static void
9683f_searchpairpos(typval_T *argvars, typval_T *rettv)
9684{
9685 pos_T match_pos;
9686 int lnum = 0;
9687 int col = 0;
9688
9689 if (rettv_list_alloc(rettv) == FAIL)
9690 return;
9691
9692 if (searchpair_cmn(argvars, &match_pos) > 0)
9693 {
9694 lnum = match_pos.lnum;
9695 col = match_pos.col;
9696 }
9697
9698 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9699 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9700}
9701
9702/*
9703 * Search for a start/middle/end thing.
9704 * Used by searchpair(), see its documentation for the details.
9705 * Returns 0 or -1 for no match,
9706 */
9707 long
9708do_searchpair(
9709 char_u *spat, /* start pattern */
9710 char_u *mpat, /* middle pattern */
9711 char_u *epat, /* end pattern */
9712 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +01009713 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009714 int flags, /* SP_SETPCMARK and other SP_ values */
9715 pos_T *match_pos,
9716 linenr_T lnum_stop, /* stop at this line if not zero */
9717 long time_limit UNUSED) /* stop after this many msec */
9718{
9719 char_u *save_cpo;
9720 char_u *pat, *pat2 = NULL, *pat3 = NULL;
9721 long retval = 0;
9722 pos_T pos;
9723 pos_T firstpos;
9724 pos_T foundpos;
9725 pos_T save_cursor;
9726 pos_T save_pos;
9727 int n;
9728 int r;
9729 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01009730 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009731 int err;
9732 int options = SEARCH_KEEP;
9733 proftime_T tm;
9734
9735 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
9736 save_cpo = p_cpo;
9737 p_cpo = empty_option;
9738
9739#ifdef FEAT_RELTIME
9740 /* Set the time limit, if there is one. */
9741 profile_setlimit(time_limit, &tm);
9742#endif
9743
9744 /* Make two search patterns: start/end (pat2, for in nested pairs) and
9745 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009746 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
9747 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009748 if (pat2 == NULL || pat3 == NULL)
9749 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009750 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009751 if (*mpat == NUL)
9752 STRCPY(pat3, pat2);
9753 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009754 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009755 spat, epat, mpat);
9756 if (flags & SP_START)
9757 options |= SEARCH_START;
9758
Bram Moolenaar48570482017-10-30 21:48:41 +01009759 if (skip != NULL)
9760 {
9761 /* Empty string means to not use the skip expression. */
9762 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
9763 use_skip = skip->vval.v_string != NULL
9764 && *skip->vval.v_string != NUL;
9765 }
9766
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009767 save_cursor = curwin->w_cursor;
9768 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009769 CLEAR_POS(&firstpos);
9770 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009771 pat = pat3;
9772 for (;;)
9773 {
9774 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009775 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009776 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009777 /* didn't find it or found the first match again: FAIL */
9778 break;
9779
9780 if (firstpos.lnum == 0)
9781 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009782 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009783 {
9784 /* Found the same position again. Can happen with a pattern that
9785 * has "\zs" at the end and searching backwards. Advance one
9786 * character and try again. */
9787 if (dir == BACKWARD)
9788 decl(&pos);
9789 else
9790 incl(&pos);
9791 }
9792 foundpos = pos;
9793
9794 /* clear the start flag to avoid getting stuck here */
9795 options &= ~SEARCH_START;
9796
9797 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +01009798 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009799 {
9800 save_pos = curwin->w_cursor;
9801 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01009802 err = FALSE;
9803 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009804 curwin->w_cursor = save_pos;
9805 if (err)
9806 {
9807 /* Evaluating {skip} caused an error, break here. */
9808 curwin->w_cursor = save_cursor;
9809 retval = -1;
9810 break;
9811 }
9812 if (r)
9813 continue;
9814 }
9815
9816 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
9817 {
9818 /* Found end when searching backwards or start when searching
9819 * forward: nested pair. */
9820 ++nest;
9821 pat = pat2; /* nested, don't search for middle */
9822 }
9823 else
9824 {
9825 /* Found end when searching forward or start when searching
9826 * backward: end of (nested) pair; or found middle in outer pair. */
9827 if (--nest == 1)
9828 pat = pat3; /* outer level, search for middle */
9829 }
9830
9831 if (nest == 0)
9832 {
9833 /* Found the match: return matchcount or line number. */
9834 if (flags & SP_RETCOUNT)
9835 ++retval;
9836 else
9837 retval = pos.lnum;
9838 if (flags & SP_SETPCMARK)
9839 setpcmark();
9840 curwin->w_cursor = pos;
9841 if (!(flags & SP_REPEAT))
9842 break;
9843 nest = 1; /* search for next unmatched */
9844 }
9845 }
9846
9847 if (match_pos != NULL)
9848 {
9849 /* Store the match cursor position */
9850 match_pos->lnum = curwin->w_cursor.lnum;
9851 match_pos->col = curwin->w_cursor.col + 1;
9852 }
9853
9854 /* If 'n' flag is used or search failed: restore cursor position. */
9855 if ((flags & SP_NOMOVE) || retval == 0)
9856 curwin->w_cursor = save_cursor;
9857
9858theend:
9859 vim_free(pat2);
9860 vim_free(pat3);
9861 if (p_cpo == empty_option)
9862 p_cpo = save_cpo;
9863 else
9864 /* Darn, evaluating the {skip} expression changed the value. */
9865 free_string_option(save_cpo);
9866
9867 return retval;
9868}
9869
9870/*
9871 * "searchpos()" function
9872 */
9873 static void
9874f_searchpos(typval_T *argvars, typval_T *rettv)
9875{
9876 pos_T match_pos;
9877 int lnum = 0;
9878 int col = 0;
9879 int n;
9880 int flags = 0;
9881
9882 if (rettv_list_alloc(rettv) == FAIL)
9883 return;
9884
9885 n = search_cmn(argvars, &match_pos, &flags);
9886 if (n > 0)
9887 {
9888 lnum = match_pos.lnum;
9889 col = match_pos.col;
9890 }
9891
9892 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9893 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9894 if (flags & SP_SUBPAT)
9895 list_append_number(rettv->vval.v_list, (varnumber_T)n);
9896}
9897
9898 static void
9899f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
9900{
9901#ifdef FEAT_CLIENTSERVER
9902 char_u buf[NUMBUFLEN];
9903 char_u *server = get_tv_string_chk(&argvars[0]);
9904 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
9905
9906 rettv->vval.v_number = -1;
9907 if (server == NULL || reply == NULL)
9908 return;
9909 if (check_restricted() || check_secure())
9910 return;
9911# ifdef FEAT_X11
9912 if (check_connection() == FAIL)
9913 return;
9914# endif
9915
9916 if (serverSendReply(server, reply) < 0)
9917 {
9918 EMSG(_("E258: Unable to send to client"));
9919 return;
9920 }
9921 rettv->vval.v_number = 0;
9922#else
9923 rettv->vval.v_number = -1;
9924#endif
9925}
9926
9927 static void
9928f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
9929{
9930 char_u *r = NULL;
9931
9932#ifdef FEAT_CLIENTSERVER
9933# ifdef WIN32
9934 r = serverGetVimNames();
9935# else
9936 make_connection();
9937 if (X_DISPLAY != NULL)
9938 r = serverGetVimNames(X_DISPLAY);
9939# endif
9940#endif
9941 rettv->v_type = VAR_STRING;
9942 rettv->vval.v_string = r;
9943}
9944
9945/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +02009946 * Set line or list of lines in buffer "buf".
9947 */
9948 static void
9949set_buffer_lines(buf_T *buf, linenr_T lnum, typval_T *lines, typval_T *rettv)
9950{
9951 char_u *line = NULL;
9952 list_T *l = NULL;
9953 listitem_T *li = NULL;
9954 long added = 0;
9955 linenr_T lcount;
Bram Moolenaar0c4dc882017-11-06 21:32:54 +01009956 buf_T *curbuf_save = NULL;
9957 win_T *curwin_save = NULL;
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +02009958 int is_curbuf = buf == curbuf;
9959
Bram Moolenaar9d954202017-09-04 20:34:19 +02009960 /* When using the current buffer ml_mfp will be set if needed. Useful when
9961 * setline() is used on startup. For other buffers the buffer must be
9962 * loaded. */
9963 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +02009964 {
9965 rettv->vval.v_number = 1; /* FAIL */
9966 return;
9967 }
9968
Bram Moolenaar0c4dc882017-11-06 21:32:54 +01009969 if (!is_curbuf)
9970 {
9971 wininfo_T *wip;
9972
9973 curbuf_save = curbuf;
9974 curwin_save = curwin;
9975 curbuf = buf;
9976 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
9977 {
9978 if (wip->wi_win != NULL)
9979 {
9980 curwin = wip->wi_win;
9981 break;
9982 }
9983 }
9984 }
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +02009985
9986 lcount = curbuf->b_ml.ml_line_count;
9987
9988 if (lines->v_type == VAR_LIST)
9989 {
9990 l = lines->vval.v_list;
9991 li = l->lv_first;
9992 }
9993 else
9994 line = get_tv_string_chk(lines);
9995
9996 /* default result is zero == OK */
9997 for (;;)
9998 {
9999 if (l != NULL)
10000 {
10001 /* list argument, get next string */
10002 if (li == NULL)
10003 break;
10004 line = get_tv_string_chk(&li->li_tv);
10005 li = li->li_next;
10006 }
10007
10008 rettv->vval.v_number = 1; /* FAIL */
10009 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
10010 break;
10011
10012 /* When coming here from Insert mode, sync undo, so that this can be
10013 * undone separately from what was previously inserted. */
10014 if (u_sync_once == 2)
10015 {
10016 u_sync_once = 1; /* notify that u_sync() was called */
10017 u_sync(TRUE);
10018 }
10019
10020 if (lnum <= curbuf->b_ml.ml_line_count)
10021 {
10022 /* existing line, replace it */
10023 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
10024 {
10025 changed_bytes(lnum, 0);
10026 if (is_curbuf && lnum == curwin->w_cursor.lnum)
10027 check_cursor_col();
10028 rettv->vval.v_number = 0; /* OK */
10029 }
10030 }
10031 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
10032 {
10033 /* lnum is one past the last line, append the line */
10034 ++added;
10035 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
10036 rettv->vval.v_number = 0; /* OK */
10037 }
10038
10039 if (l == NULL) /* only one string argument */
10040 break;
10041 ++lnum;
10042 }
10043
10044 if (added > 0)
10045 appended_lines_mark(lcount, added);
10046
Bram Moolenaar0c4dc882017-11-06 21:32:54 +010010047 if (!is_curbuf)
10048 {
10049 curbuf = curbuf_save;
10050 curwin = curwin_save;
10051 }
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010052}
10053
10054/*
10055 * "setbufline()" function
10056 */
10057 static void
10058f_setbufline(argvars, rettv)
10059 typval_T *argvars;
10060 typval_T *rettv;
10061{
10062 linenr_T lnum;
10063 buf_T *buf;
10064
10065 buf = get_buf_tv(&argvars[0], FALSE);
10066 if (buf == NULL)
10067 rettv->vval.v_number = 1; /* FAIL */
10068 else
10069 {
10070 lnum = get_tv_lnum_buf(&argvars[1], buf);
10071
10072 set_buffer_lines(buf, lnum, &argvars[2], rettv);
10073 }
10074}
10075
10076/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010077 * "setbufvar()" function
10078 */
10079 static void
10080f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10081{
10082 buf_T *buf;
10083 char_u *varname, *bufvarname;
10084 typval_T *varp;
10085 char_u nbuf[NUMBUFLEN];
10086
10087 if (check_restricted() || check_secure())
10088 return;
10089 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10090 varname = get_tv_string_chk(&argvars[1]);
10091 buf = get_buf_tv(&argvars[0], FALSE);
10092 varp = &argvars[2];
10093
10094 if (buf != NULL && varname != NULL && varp != NULL)
10095 {
10096 if (*varname == '&')
10097 {
10098 long numval;
10099 char_u *strval;
10100 int error = FALSE;
10101 aco_save_T aco;
10102
10103 /* set curbuf to be our buf, temporarily */
10104 aucmd_prepbuf(&aco, buf);
10105
10106 ++varname;
10107 numval = (long)get_tv_number_chk(varp, &error);
10108 strval = get_tv_string_buf_chk(varp, nbuf);
10109 if (!error && strval != NULL)
10110 set_option_value(varname, numval, strval, OPT_LOCAL);
10111
10112 /* reset notion of buffer */
10113 aucmd_restbuf(&aco);
10114 }
10115 else
10116 {
10117 buf_T *save_curbuf = curbuf;
10118
10119 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
10120 if (bufvarname != NULL)
10121 {
10122 curbuf = buf;
10123 STRCPY(bufvarname, "b:");
10124 STRCPY(bufvarname + 2, varname);
10125 set_var(bufvarname, varp, TRUE);
10126 vim_free(bufvarname);
10127 curbuf = save_curbuf;
10128 }
10129 }
10130 }
10131}
10132
10133 static void
10134f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10135{
10136 dict_T *d;
10137 dictitem_T *di;
10138 char_u *csearch;
10139
10140 if (argvars[0].v_type != VAR_DICT)
10141 {
10142 EMSG(_(e_dictreq));
10143 return;
10144 }
10145
10146 if ((d = argvars[0].vval.v_dict) != NULL)
10147 {
10148 csearch = get_dict_string(d, (char_u *)"char", FALSE);
10149 if (csearch != NULL)
10150 {
10151#ifdef FEAT_MBYTE
10152 if (enc_utf8)
10153 {
10154 int pcc[MAX_MCO];
10155 int c = utfc_ptr2char(csearch, pcc);
10156
10157 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10158 }
10159 else
10160#endif
10161 set_last_csearch(PTR2CHAR(csearch),
10162 csearch, MB_PTR2LEN(csearch));
10163 }
10164
10165 di = dict_find(d, (char_u *)"forward", -1);
10166 if (di != NULL)
10167 set_csearch_direction((int)get_tv_number(&di->di_tv)
10168 ? FORWARD : BACKWARD);
10169
10170 di = dict_find(d, (char_u *)"until", -1);
10171 if (di != NULL)
10172 set_csearch_until(!!get_tv_number(&di->di_tv));
10173 }
10174}
10175
10176/*
10177 * "setcmdpos()" function
10178 */
10179 static void
10180f_setcmdpos(typval_T *argvars, typval_T *rettv)
10181{
10182 int pos = (int)get_tv_number(&argvars[0]) - 1;
10183
10184 if (pos >= 0)
10185 rettv->vval.v_number = set_cmdline_pos(pos);
10186}
10187
10188/*
10189 * "setfperm({fname}, {mode})" function
10190 */
10191 static void
10192f_setfperm(typval_T *argvars, typval_T *rettv)
10193{
10194 char_u *fname;
10195 char_u modebuf[NUMBUFLEN];
10196 char_u *mode_str;
10197 int i;
10198 int mask;
10199 int mode = 0;
10200
10201 rettv->vval.v_number = 0;
10202 fname = get_tv_string_chk(&argvars[0]);
10203 if (fname == NULL)
10204 return;
10205 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
10206 if (mode_str == NULL)
10207 return;
10208 if (STRLEN(mode_str) != 9)
10209 {
10210 EMSG2(_(e_invarg2), mode_str);
10211 return;
10212 }
10213
10214 mask = 1;
10215 for (i = 8; i >= 0; --i)
10216 {
10217 if (mode_str[i] != '-')
10218 mode |= mask;
10219 mask = mask << 1;
10220 }
10221 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10222}
10223
10224/*
10225 * "setline()" function
10226 */
10227 static void
10228f_setline(typval_T *argvars, typval_T *rettv)
10229{
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010230 linenr_T lnum = get_tv_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010231
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010232 set_buffer_lines(curbuf, lnum, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010233}
10234
Bram Moolenaard823fa92016-08-12 16:29:27 +020010235static 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 +020010236
10237/*
10238 * Used by "setqflist()" and "setloclist()" functions
10239 */
10240 static void
10241set_qf_ll_list(
10242 win_T *wp UNUSED,
10243 typval_T *list_arg UNUSED,
10244 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010245 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010246 typval_T *rettv)
10247{
10248#ifdef FEAT_QUICKFIX
10249 static char *e_invact = N_("E927: Invalid action: '%s'");
10250 char_u *act;
10251 int action = 0;
10252#endif
10253
10254 rettv->vval.v_number = -1;
10255
10256#ifdef FEAT_QUICKFIX
10257 if (list_arg->v_type != VAR_LIST)
10258 EMSG(_(e_listreq));
10259 else
10260 {
10261 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010262 dict_T *d = NULL;
10263 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010264
10265 if (action_arg->v_type == VAR_STRING)
10266 {
10267 act = get_tv_string_chk(action_arg);
10268 if (act == NULL)
10269 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020010270 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
10271 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010272 action = *act;
10273 else
10274 EMSG2(_(e_invact), act);
10275 }
10276 else if (action_arg->v_type == VAR_UNKNOWN)
10277 action = ' ';
10278 else
10279 EMSG(_(e_stringreq));
10280
Bram Moolenaard823fa92016-08-12 16:29:27 +020010281 if (action_arg->v_type != VAR_UNKNOWN
10282 && what_arg->v_type != VAR_UNKNOWN)
10283 {
10284 if (what_arg->v_type == VAR_DICT)
10285 d = what_arg->vval.v_dict;
10286 else
10287 {
10288 EMSG(_(e_dictreq));
10289 valid_dict = FALSE;
10290 }
10291 }
10292
10293 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
10294 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()"), d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010295 rettv->vval.v_number = 0;
10296 }
10297#endif
10298}
10299
10300/*
10301 * "setloclist()" function
10302 */
10303 static void
10304f_setloclist(typval_T *argvars, typval_T *rettv)
10305{
10306 win_T *win;
10307
10308 rettv->vval.v_number = -1;
10309
10310 win = find_win_by_nr(&argvars[0], NULL);
10311 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020010312 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010313}
10314
10315/*
10316 * "setmatches()" function
10317 */
10318 static void
10319f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10320{
10321#ifdef FEAT_SEARCH_EXTRA
10322 list_T *l;
10323 listitem_T *li;
10324 dict_T *d;
10325 list_T *s = NULL;
10326
10327 rettv->vval.v_number = -1;
10328 if (argvars[0].v_type != VAR_LIST)
10329 {
10330 EMSG(_(e_listreq));
10331 return;
10332 }
10333 if ((l = argvars[0].vval.v_list) != NULL)
10334 {
10335
10336 /* To some extent make sure that we are dealing with a list from
10337 * "getmatches()". */
10338 li = l->lv_first;
10339 while (li != NULL)
10340 {
10341 if (li->li_tv.v_type != VAR_DICT
10342 || (d = li->li_tv.vval.v_dict) == NULL)
10343 {
10344 EMSG(_(e_invarg));
10345 return;
10346 }
10347 if (!(dict_find(d, (char_u *)"group", -1) != NULL
10348 && (dict_find(d, (char_u *)"pattern", -1) != NULL
10349 || dict_find(d, (char_u *)"pos1", -1) != NULL)
10350 && dict_find(d, (char_u *)"priority", -1) != NULL
10351 && dict_find(d, (char_u *)"id", -1) != NULL))
10352 {
10353 EMSG(_(e_invarg));
10354 return;
10355 }
10356 li = li->li_next;
10357 }
10358
10359 clear_matches(curwin);
10360 li = l->lv_first;
10361 while (li != NULL)
10362 {
10363 int i = 0;
10364 char_u buf[5];
10365 dictitem_T *di;
10366 char_u *group;
10367 int priority;
10368 int id;
10369 char_u *conceal;
10370
10371 d = li->li_tv.vval.v_dict;
10372 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
10373 {
10374 if (s == NULL)
10375 {
10376 s = list_alloc();
10377 if (s == NULL)
10378 return;
10379 }
10380
10381 /* match from matchaddpos() */
10382 for (i = 1; i < 9; i++)
10383 {
10384 sprintf((char *)buf, (char *)"pos%d", i);
10385 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
10386 {
10387 if (di->di_tv.v_type != VAR_LIST)
10388 return;
10389
10390 list_append_tv(s, &di->di_tv);
10391 s->lv_refcount++;
10392 }
10393 else
10394 break;
10395 }
10396 }
10397
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010398 group = get_dict_string(d, (char_u *)"group", TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010399 priority = (int)get_dict_number(d, (char_u *)"priority");
10400 id = (int)get_dict_number(d, (char_u *)"id");
10401 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010402 ? get_dict_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010403 : NULL;
10404 if (i == 0)
10405 {
10406 match_add(curwin, group,
10407 get_dict_string(d, (char_u *)"pattern", FALSE),
10408 priority, id, NULL, conceal);
10409 }
10410 else
10411 {
10412 match_add(curwin, group, NULL, priority, id, s, conceal);
10413 list_unref(s);
10414 s = NULL;
10415 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010416 vim_free(group);
10417 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010418
10419 li = li->li_next;
10420 }
10421 rettv->vval.v_number = 0;
10422 }
10423#endif
10424}
10425
10426/*
10427 * "setpos()" function
10428 */
10429 static void
10430f_setpos(typval_T *argvars, typval_T *rettv)
10431{
10432 pos_T pos;
10433 int fnum;
10434 char_u *name;
10435 colnr_T curswant = -1;
10436
10437 rettv->vval.v_number = -1;
10438 name = get_tv_string_chk(argvars);
10439 if (name != NULL)
10440 {
10441 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10442 {
10443 if (--pos.col < 0)
10444 pos.col = 0;
10445 if (name[0] == '.' && name[1] == NUL)
10446 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010447 /* set cursor; "fnum" is ignored */
10448 curwin->w_cursor = pos;
10449 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010450 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010451 curwin->w_curswant = curswant - 1;
10452 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010453 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010454 check_cursor();
10455 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010456 }
10457 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10458 {
10459 /* set mark */
10460 if (setmark_pos(name[1], &pos, fnum) == OK)
10461 rettv->vval.v_number = 0;
10462 }
10463 else
10464 EMSG(_(e_invarg));
10465 }
10466 }
10467}
10468
10469/*
10470 * "setqflist()" function
10471 */
10472 static void
10473f_setqflist(typval_T *argvars, typval_T *rettv)
10474{
Bram Moolenaard823fa92016-08-12 16:29:27 +020010475 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010476}
10477
10478/*
10479 * "setreg()" function
10480 */
10481 static void
10482f_setreg(typval_T *argvars, typval_T *rettv)
10483{
10484 int regname;
10485 char_u *strregname;
10486 char_u *stropt;
10487 char_u *strval;
10488 int append;
10489 char_u yank_type;
10490 long block_len;
10491
10492 block_len = -1;
10493 yank_type = MAUTO;
10494 append = FALSE;
10495
10496 strregname = get_tv_string_chk(argvars);
10497 rettv->vval.v_number = 1; /* FAIL is default */
10498
10499 if (strregname == NULL)
10500 return; /* type error; errmsg already given */
10501 regname = *strregname;
10502 if (regname == 0 || regname == '@')
10503 regname = '"';
10504
10505 if (argvars[2].v_type != VAR_UNKNOWN)
10506 {
10507 stropt = get_tv_string_chk(&argvars[2]);
10508 if (stropt == NULL)
10509 return; /* type error */
10510 for (; *stropt != NUL; ++stropt)
10511 switch (*stropt)
10512 {
10513 case 'a': case 'A': /* append */
10514 append = TRUE;
10515 break;
10516 case 'v': case 'c': /* character-wise selection */
10517 yank_type = MCHAR;
10518 break;
10519 case 'V': case 'l': /* line-wise selection */
10520 yank_type = MLINE;
10521 break;
10522 case 'b': case Ctrl_V: /* block-wise selection */
10523 yank_type = MBLOCK;
10524 if (VIM_ISDIGIT(stropt[1]))
10525 {
10526 ++stropt;
10527 block_len = getdigits(&stropt) - 1;
10528 --stropt;
10529 }
10530 break;
10531 }
10532 }
10533
10534 if (argvars[1].v_type == VAR_LIST)
10535 {
10536 char_u **lstval;
10537 char_u **allocval;
10538 char_u buf[NUMBUFLEN];
10539 char_u **curval;
10540 char_u **curallocval;
10541 list_T *ll = argvars[1].vval.v_list;
10542 listitem_T *li;
10543 int len;
10544
10545 /* If the list is NULL handle like an empty list. */
10546 len = ll == NULL ? 0 : ll->lv_len;
10547
10548 /* First half: use for pointers to result lines; second half: use for
10549 * pointers to allocated copies. */
10550 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
10551 if (lstval == NULL)
10552 return;
10553 curval = lstval;
10554 allocval = lstval + len + 2;
10555 curallocval = allocval;
10556
10557 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
10558 li = li->li_next)
10559 {
10560 strval = get_tv_string_buf_chk(&li->li_tv, buf);
10561 if (strval == NULL)
10562 goto free_lstval;
10563 if (strval == buf)
10564 {
10565 /* Need to make a copy, next get_tv_string_buf_chk() will
10566 * overwrite the string. */
10567 strval = vim_strsave(buf);
10568 if (strval == NULL)
10569 goto free_lstval;
10570 *curallocval++ = strval;
10571 }
10572 *curval++ = strval;
10573 }
10574 *curval++ = NULL;
10575
10576 write_reg_contents_lst(regname, lstval, -1,
10577 append, yank_type, block_len);
10578free_lstval:
10579 while (curallocval > allocval)
10580 vim_free(*--curallocval);
10581 vim_free(lstval);
10582 }
10583 else
10584 {
10585 strval = get_tv_string_chk(&argvars[1]);
10586 if (strval == NULL)
10587 return;
10588 write_reg_contents_ex(regname, strval, -1,
10589 append, yank_type, block_len);
10590 }
10591 rettv->vval.v_number = 0;
10592}
10593
10594/*
10595 * "settabvar()" function
10596 */
10597 static void
10598f_settabvar(typval_T *argvars, typval_T *rettv)
10599{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010600 tabpage_T *save_curtab;
10601 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010602 char_u *varname, *tabvarname;
10603 typval_T *varp;
10604
10605 rettv->vval.v_number = 0;
10606
10607 if (check_restricted() || check_secure())
10608 return;
10609
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010610 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010611 varname = get_tv_string_chk(&argvars[1]);
10612 varp = &argvars[2];
10613
Bram Moolenaar4033c552017-09-16 20:54:51 +020010614 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010615 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010616 save_curtab = curtab;
10617 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010618
10619 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
10620 if (tabvarname != NULL)
10621 {
10622 STRCPY(tabvarname, "t:");
10623 STRCPY(tabvarname + 2, varname);
10624 set_var(tabvarname, varp, TRUE);
10625 vim_free(tabvarname);
10626 }
10627
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010628 /* Restore current tabpage */
10629 if (valid_tabpage(save_curtab))
10630 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010631 }
10632}
10633
10634/*
10635 * "settabwinvar()" function
10636 */
10637 static void
10638f_settabwinvar(typval_T *argvars, typval_T *rettv)
10639{
10640 setwinvar(argvars, rettv, 1);
10641}
10642
10643/*
10644 * "setwinvar()" function
10645 */
10646 static void
10647f_setwinvar(typval_T *argvars, typval_T *rettv)
10648{
10649 setwinvar(argvars, rettv, 0);
10650}
10651
10652#ifdef FEAT_CRYPT
10653/*
10654 * "sha256({string})" function
10655 */
10656 static void
10657f_sha256(typval_T *argvars, typval_T *rettv)
10658{
10659 char_u *p;
10660
10661 p = get_tv_string(&argvars[0]);
10662 rettv->vval.v_string = vim_strsave(
10663 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
10664 rettv->v_type = VAR_STRING;
10665}
10666#endif /* FEAT_CRYPT */
10667
10668/*
10669 * "shellescape({string})" function
10670 */
10671 static void
10672f_shellescape(typval_T *argvars, typval_T *rettv)
10673{
Bram Moolenaar20615522017-06-05 18:46:26 +020010674 int do_special = non_zero_arg(&argvars[1]);
10675
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010676 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar20615522017-06-05 18:46:26 +020010677 get_tv_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010678 rettv->v_type = VAR_STRING;
10679}
10680
10681/*
10682 * shiftwidth() function
10683 */
10684 static void
10685f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
10686{
10687 rettv->vval.v_number = get_sw_value(curbuf);
10688}
10689
10690/*
10691 * "simplify()" function
10692 */
10693 static void
10694f_simplify(typval_T *argvars, typval_T *rettv)
10695{
10696 char_u *p;
10697
10698 p = get_tv_string(&argvars[0]);
10699 rettv->vval.v_string = vim_strsave(p);
10700 simplify_filename(rettv->vval.v_string); /* simplify in place */
10701 rettv->v_type = VAR_STRING;
10702}
10703
10704#ifdef FEAT_FLOAT
10705/*
10706 * "sin()" function
10707 */
10708 static void
10709f_sin(typval_T *argvars, typval_T *rettv)
10710{
10711 float_T f = 0.0;
10712
10713 rettv->v_type = VAR_FLOAT;
10714 if (get_float_arg(argvars, &f) == OK)
10715 rettv->vval.v_float = sin(f);
10716 else
10717 rettv->vval.v_float = 0.0;
10718}
10719
10720/*
10721 * "sinh()" function
10722 */
10723 static void
10724f_sinh(typval_T *argvars, typval_T *rettv)
10725{
10726 float_T f = 0.0;
10727
10728 rettv->v_type = VAR_FLOAT;
10729 if (get_float_arg(argvars, &f) == OK)
10730 rettv->vval.v_float = sinh(f);
10731 else
10732 rettv->vval.v_float = 0.0;
10733}
10734#endif
10735
10736static int
10737#ifdef __BORLANDC__
10738 _RTLENTRYF
10739#endif
10740 item_compare(const void *s1, const void *s2);
10741static int
10742#ifdef __BORLANDC__
10743 _RTLENTRYF
10744#endif
10745 item_compare2(const void *s1, const void *s2);
10746
10747/* struct used in the array that's given to qsort() */
10748typedef struct
10749{
10750 listitem_T *item;
10751 int idx;
10752} sortItem_T;
10753
10754/* struct storing information about current sort */
10755typedef struct
10756{
10757 int item_compare_ic;
10758 int item_compare_numeric;
10759 int item_compare_numbers;
10760#ifdef FEAT_FLOAT
10761 int item_compare_float;
10762#endif
10763 char_u *item_compare_func;
10764 partial_T *item_compare_partial;
10765 dict_T *item_compare_selfdict;
10766 int item_compare_func_err;
10767 int item_compare_keep_zero;
10768} sortinfo_T;
10769static sortinfo_T *sortinfo = NULL;
10770static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
10771#define ITEM_COMPARE_FAIL 999
10772
10773/*
10774 * Compare functions for f_sort() and f_uniq() below.
10775 */
10776 static int
10777#ifdef __BORLANDC__
10778_RTLENTRYF
10779#endif
10780item_compare(const void *s1, const void *s2)
10781{
10782 sortItem_T *si1, *si2;
10783 typval_T *tv1, *tv2;
10784 char_u *p1, *p2;
10785 char_u *tofree1 = NULL, *tofree2 = NULL;
10786 int res;
10787 char_u numbuf1[NUMBUFLEN];
10788 char_u numbuf2[NUMBUFLEN];
10789
10790 si1 = (sortItem_T *)s1;
10791 si2 = (sortItem_T *)s2;
10792 tv1 = &si1->item->li_tv;
10793 tv2 = &si2->item->li_tv;
10794
10795 if (sortinfo->item_compare_numbers)
10796 {
10797 varnumber_T v1 = get_tv_number(tv1);
10798 varnumber_T v2 = get_tv_number(tv2);
10799
10800 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10801 }
10802
10803#ifdef FEAT_FLOAT
10804 if (sortinfo->item_compare_float)
10805 {
10806 float_T v1 = get_tv_float(tv1);
10807 float_T v2 = get_tv_float(tv2);
10808
10809 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10810 }
10811#endif
10812
10813 /* tv2string() puts quotes around a string and allocates memory. Don't do
10814 * that for string variables. Use a single quote when comparing with a
10815 * non-string to do what the docs promise. */
10816 if (tv1->v_type == VAR_STRING)
10817 {
10818 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
10819 p1 = (char_u *)"'";
10820 else
10821 p1 = tv1->vval.v_string;
10822 }
10823 else
10824 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
10825 if (tv2->v_type == VAR_STRING)
10826 {
10827 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
10828 p2 = (char_u *)"'";
10829 else
10830 p2 = tv2->vval.v_string;
10831 }
10832 else
10833 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
10834 if (p1 == NULL)
10835 p1 = (char_u *)"";
10836 if (p2 == NULL)
10837 p2 = (char_u *)"";
10838 if (!sortinfo->item_compare_numeric)
10839 {
10840 if (sortinfo->item_compare_ic)
10841 res = STRICMP(p1, p2);
10842 else
10843 res = STRCMP(p1, p2);
10844 }
10845 else
10846 {
10847 double n1, n2;
10848 n1 = strtod((char *)p1, (char **)&p1);
10849 n2 = strtod((char *)p2, (char **)&p2);
10850 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
10851 }
10852
10853 /* When the result would be zero, compare the item indexes. Makes the
10854 * sort stable. */
10855 if (res == 0 && !sortinfo->item_compare_keep_zero)
10856 res = si1->idx > si2->idx ? 1 : -1;
10857
10858 vim_free(tofree1);
10859 vim_free(tofree2);
10860 return res;
10861}
10862
10863 static int
10864#ifdef __BORLANDC__
10865_RTLENTRYF
10866#endif
10867item_compare2(const void *s1, const void *s2)
10868{
10869 sortItem_T *si1, *si2;
10870 int res;
10871 typval_T rettv;
10872 typval_T argv[3];
10873 int dummy;
10874 char_u *func_name;
10875 partial_T *partial = sortinfo->item_compare_partial;
10876
10877 /* shortcut after failure in previous call; compare all items equal */
10878 if (sortinfo->item_compare_func_err)
10879 return 0;
10880
10881 si1 = (sortItem_T *)s1;
10882 si2 = (sortItem_T *)s2;
10883
10884 if (partial == NULL)
10885 func_name = sortinfo->item_compare_func;
10886 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020010887 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010888
10889 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
10890 * in the copy without changing the original list items. */
10891 copy_tv(&si1->item->li_tv, &argv[0]);
10892 copy_tv(&si2->item->li_tv, &argv[1]);
10893
10894 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
10895 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020010896 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010897 partial, sortinfo->item_compare_selfdict);
10898 clear_tv(&argv[0]);
10899 clear_tv(&argv[1]);
10900
10901 if (res == FAIL)
10902 res = ITEM_COMPARE_FAIL;
10903 else
10904 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
10905 if (sortinfo->item_compare_func_err)
10906 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
10907 clear_tv(&rettv);
10908
10909 /* When the result would be zero, compare the pointers themselves. Makes
10910 * the sort stable. */
10911 if (res == 0 && !sortinfo->item_compare_keep_zero)
10912 res = si1->idx > si2->idx ? 1 : -1;
10913
10914 return res;
10915}
10916
10917/*
10918 * "sort({list})" function
10919 */
10920 static void
10921do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
10922{
10923 list_T *l;
10924 listitem_T *li;
10925 sortItem_T *ptrs;
10926 sortinfo_T *old_sortinfo;
10927 sortinfo_T info;
10928 long len;
10929 long i;
10930
10931 /* Pointer to current info struct used in compare function. Save and
10932 * restore the current one for nested calls. */
10933 old_sortinfo = sortinfo;
10934 sortinfo = &info;
10935
10936 if (argvars[0].v_type != VAR_LIST)
10937 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
10938 else
10939 {
10940 l = argvars[0].vval.v_list;
10941 if (l == NULL || tv_check_lock(l->lv_lock,
10942 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
10943 TRUE))
10944 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020010945 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010946
10947 len = list_len(l);
10948 if (len <= 1)
10949 goto theend; /* short list sorts pretty quickly */
10950
10951 info.item_compare_ic = FALSE;
10952 info.item_compare_numeric = FALSE;
10953 info.item_compare_numbers = FALSE;
10954#ifdef FEAT_FLOAT
10955 info.item_compare_float = FALSE;
10956#endif
10957 info.item_compare_func = NULL;
10958 info.item_compare_partial = NULL;
10959 info.item_compare_selfdict = NULL;
10960 if (argvars[1].v_type != VAR_UNKNOWN)
10961 {
10962 /* optional second argument: {func} */
10963 if (argvars[1].v_type == VAR_FUNC)
10964 info.item_compare_func = argvars[1].vval.v_string;
10965 else if (argvars[1].v_type == VAR_PARTIAL)
10966 info.item_compare_partial = argvars[1].vval.v_partial;
10967 else
10968 {
10969 int error = FALSE;
10970
10971 i = (long)get_tv_number_chk(&argvars[1], &error);
10972 if (error)
10973 goto theend; /* type error; errmsg already given */
10974 if (i == 1)
10975 info.item_compare_ic = TRUE;
10976 else if (argvars[1].v_type != VAR_NUMBER)
10977 info.item_compare_func = get_tv_string(&argvars[1]);
10978 else if (i != 0)
10979 {
10980 EMSG(_(e_invarg));
10981 goto theend;
10982 }
10983 if (info.item_compare_func != NULL)
10984 {
10985 if (*info.item_compare_func == NUL)
10986 {
10987 /* empty string means default sort */
10988 info.item_compare_func = NULL;
10989 }
10990 else if (STRCMP(info.item_compare_func, "n") == 0)
10991 {
10992 info.item_compare_func = NULL;
10993 info.item_compare_numeric = TRUE;
10994 }
10995 else if (STRCMP(info.item_compare_func, "N") == 0)
10996 {
10997 info.item_compare_func = NULL;
10998 info.item_compare_numbers = TRUE;
10999 }
11000#ifdef FEAT_FLOAT
11001 else if (STRCMP(info.item_compare_func, "f") == 0)
11002 {
11003 info.item_compare_func = NULL;
11004 info.item_compare_float = TRUE;
11005 }
11006#endif
11007 else if (STRCMP(info.item_compare_func, "i") == 0)
11008 {
11009 info.item_compare_func = NULL;
11010 info.item_compare_ic = TRUE;
11011 }
11012 }
11013 }
11014
11015 if (argvars[2].v_type != VAR_UNKNOWN)
11016 {
11017 /* optional third argument: {dict} */
11018 if (argvars[2].v_type != VAR_DICT)
11019 {
11020 EMSG(_(e_dictreq));
11021 goto theend;
11022 }
11023 info.item_compare_selfdict = argvars[2].vval.v_dict;
11024 }
11025 }
11026
11027 /* Make an array with each entry pointing to an item in the List. */
11028 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
11029 if (ptrs == NULL)
11030 goto theend;
11031
11032 i = 0;
11033 if (sort)
11034 {
11035 /* sort(): ptrs will be the list to sort */
11036 for (li = l->lv_first; li != NULL; li = li->li_next)
11037 {
11038 ptrs[i].item = li;
11039 ptrs[i].idx = i;
11040 ++i;
11041 }
11042
11043 info.item_compare_func_err = FALSE;
11044 info.item_compare_keep_zero = FALSE;
11045 /* test the compare function */
11046 if ((info.item_compare_func != NULL
11047 || info.item_compare_partial != NULL)
11048 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11049 == ITEM_COMPARE_FAIL)
11050 EMSG(_("E702: Sort compare function failed"));
11051 else
11052 {
11053 /* Sort the array with item pointers. */
11054 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
11055 info.item_compare_func == NULL
11056 && info.item_compare_partial == NULL
11057 ? item_compare : item_compare2);
11058
11059 if (!info.item_compare_func_err)
11060 {
11061 /* Clear the List and append the items in sorted order. */
11062 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
11063 l->lv_len = 0;
11064 for (i = 0; i < len; ++i)
11065 list_append(l, ptrs[i].item);
11066 }
11067 }
11068 }
11069 else
11070 {
11071 int (*item_compare_func_ptr)(const void *, const void *);
11072
11073 /* f_uniq(): ptrs will be a stack of items to remove */
11074 info.item_compare_func_err = FALSE;
11075 info.item_compare_keep_zero = TRUE;
11076 item_compare_func_ptr = info.item_compare_func != NULL
11077 || info.item_compare_partial != NULL
11078 ? item_compare2 : item_compare;
11079
11080 for (li = l->lv_first; li != NULL && li->li_next != NULL;
11081 li = li->li_next)
11082 {
11083 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
11084 == 0)
11085 ptrs[i++].item = li;
11086 if (info.item_compare_func_err)
11087 {
11088 EMSG(_("E882: Uniq compare function failed"));
11089 break;
11090 }
11091 }
11092
11093 if (!info.item_compare_func_err)
11094 {
11095 while (--i >= 0)
11096 {
11097 li = ptrs[i].item->li_next;
11098 ptrs[i].item->li_next = li->li_next;
11099 if (li->li_next != NULL)
11100 li->li_next->li_prev = ptrs[i].item;
11101 else
11102 l->lv_last = ptrs[i].item;
11103 list_fix_watch(l, li);
11104 listitem_free(li);
11105 l->lv_len--;
11106 }
11107 }
11108 }
11109
11110 vim_free(ptrs);
11111 }
11112theend:
11113 sortinfo = old_sortinfo;
11114}
11115
11116/*
11117 * "sort({list})" function
11118 */
11119 static void
11120f_sort(typval_T *argvars, typval_T *rettv)
11121{
11122 do_sort_uniq(argvars, rettv, TRUE);
11123}
11124
11125/*
11126 * "uniq({list})" function
11127 */
11128 static void
11129f_uniq(typval_T *argvars, typval_T *rettv)
11130{
11131 do_sort_uniq(argvars, rettv, FALSE);
11132}
11133
11134/*
11135 * "soundfold({word})" function
11136 */
11137 static void
11138f_soundfold(typval_T *argvars, typval_T *rettv)
11139{
11140 char_u *s;
11141
11142 rettv->v_type = VAR_STRING;
11143 s = get_tv_string(&argvars[0]);
11144#ifdef FEAT_SPELL
11145 rettv->vval.v_string = eval_soundfold(s);
11146#else
11147 rettv->vval.v_string = vim_strsave(s);
11148#endif
11149}
11150
11151/*
11152 * "spellbadword()" function
11153 */
11154 static void
11155f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
11156{
11157 char_u *word = (char_u *)"";
11158 hlf_T attr = HLF_COUNT;
11159 int len = 0;
11160
11161 if (rettv_list_alloc(rettv) == FAIL)
11162 return;
11163
11164#ifdef FEAT_SPELL
11165 if (argvars[0].v_type == VAR_UNKNOWN)
11166 {
11167 /* Find the start and length of the badly spelled word. */
11168 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
11169 if (len != 0)
11170 word = ml_get_cursor();
11171 }
11172 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
11173 {
11174 char_u *str = get_tv_string_chk(&argvars[0]);
11175 int capcol = -1;
11176
11177 if (str != NULL)
11178 {
11179 /* Check the argument for spelling. */
11180 while (*str != NUL)
11181 {
11182 len = spell_check(curwin, str, &attr, &capcol, FALSE);
11183 if (attr != HLF_COUNT)
11184 {
11185 word = str;
11186 break;
11187 }
11188 str += len;
11189 }
11190 }
11191 }
11192#endif
11193
11194 list_append_string(rettv->vval.v_list, word, len);
11195 list_append_string(rettv->vval.v_list, (char_u *)(
11196 attr == HLF_SPB ? "bad" :
11197 attr == HLF_SPR ? "rare" :
11198 attr == HLF_SPL ? "local" :
11199 attr == HLF_SPC ? "caps" :
11200 ""), -1);
11201}
11202
11203/*
11204 * "spellsuggest()" function
11205 */
11206 static void
11207f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
11208{
11209#ifdef FEAT_SPELL
11210 char_u *str;
11211 int typeerr = FALSE;
11212 int maxcount;
11213 garray_T ga;
11214 int i;
11215 listitem_T *li;
11216 int need_capital = FALSE;
11217#endif
11218
11219 if (rettv_list_alloc(rettv) == FAIL)
11220 return;
11221
11222#ifdef FEAT_SPELL
11223 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
11224 {
11225 str = get_tv_string(&argvars[0]);
11226 if (argvars[1].v_type != VAR_UNKNOWN)
11227 {
11228 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
11229 if (maxcount <= 0)
11230 return;
11231 if (argvars[2].v_type != VAR_UNKNOWN)
11232 {
11233 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
11234 if (typeerr)
11235 return;
11236 }
11237 }
11238 else
11239 maxcount = 25;
11240
11241 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
11242
11243 for (i = 0; i < ga.ga_len; ++i)
11244 {
11245 str = ((char_u **)ga.ga_data)[i];
11246
11247 li = listitem_alloc();
11248 if (li == NULL)
11249 vim_free(str);
11250 else
11251 {
11252 li->li_tv.v_type = VAR_STRING;
11253 li->li_tv.v_lock = 0;
11254 li->li_tv.vval.v_string = str;
11255 list_append(rettv->vval.v_list, li);
11256 }
11257 }
11258 ga_clear(&ga);
11259 }
11260#endif
11261}
11262
11263 static void
11264f_split(typval_T *argvars, typval_T *rettv)
11265{
11266 char_u *str;
11267 char_u *end;
11268 char_u *pat = NULL;
11269 regmatch_T regmatch;
11270 char_u patbuf[NUMBUFLEN];
11271 char_u *save_cpo;
11272 int match;
11273 colnr_T col = 0;
11274 int keepempty = FALSE;
11275 int typeerr = FALSE;
11276
11277 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11278 save_cpo = p_cpo;
11279 p_cpo = (char_u *)"";
11280
11281 str = get_tv_string(&argvars[0]);
11282 if (argvars[1].v_type != VAR_UNKNOWN)
11283 {
11284 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11285 if (pat == NULL)
11286 typeerr = TRUE;
11287 if (argvars[2].v_type != VAR_UNKNOWN)
11288 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
11289 }
11290 if (pat == NULL || *pat == NUL)
11291 pat = (char_u *)"[\\x01- ]\\+";
11292
11293 if (rettv_list_alloc(rettv) == FAIL)
11294 return;
11295 if (typeerr)
11296 return;
11297
11298 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11299 if (regmatch.regprog != NULL)
11300 {
11301 regmatch.rm_ic = FALSE;
11302 while (*str != NUL || keepempty)
11303 {
11304 if (*str == NUL)
11305 match = FALSE; /* empty item at the end */
11306 else
11307 match = vim_regexec_nl(&regmatch, str, col);
11308 if (match)
11309 end = regmatch.startp[0];
11310 else
11311 end = str + STRLEN(str);
11312 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
11313 && *str != NUL && match && end < regmatch.endp[0]))
11314 {
11315 if (list_append_string(rettv->vval.v_list, str,
11316 (int)(end - str)) == FAIL)
11317 break;
11318 }
11319 if (!match)
11320 break;
11321 /* Advance to just after the match. */
11322 if (regmatch.endp[0] > str)
11323 col = 0;
11324 else
11325 {
11326 /* Don't get stuck at the same match. */
11327#ifdef FEAT_MBYTE
11328 col = (*mb_ptr2len)(regmatch.endp[0]);
11329#else
11330 col = 1;
11331#endif
11332 }
11333 str = regmatch.endp[0];
11334 }
11335
11336 vim_regfree(regmatch.regprog);
11337 }
11338
11339 p_cpo = save_cpo;
11340}
11341
11342#ifdef FEAT_FLOAT
11343/*
11344 * "sqrt()" function
11345 */
11346 static void
11347f_sqrt(typval_T *argvars, typval_T *rettv)
11348{
11349 float_T f = 0.0;
11350
11351 rettv->v_type = VAR_FLOAT;
11352 if (get_float_arg(argvars, &f) == OK)
11353 rettv->vval.v_float = sqrt(f);
11354 else
11355 rettv->vval.v_float = 0.0;
11356}
11357
11358/*
11359 * "str2float()" function
11360 */
11361 static void
11362f_str2float(typval_T *argvars, typval_T *rettv)
11363{
11364 char_u *p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011365 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011366
Bram Moolenaar08243d22017-01-10 16:12:29 +010011367 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011368 p = skipwhite(p + 1);
11369 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011370 if (isneg)
11371 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011372 rettv->v_type = VAR_FLOAT;
11373}
11374#endif
11375
11376/*
11377 * "str2nr()" function
11378 */
11379 static void
11380f_str2nr(typval_T *argvars, typval_T *rettv)
11381{
11382 int base = 10;
11383 char_u *p;
11384 varnumber_T n;
11385 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010011386 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011387
11388 if (argvars[1].v_type != VAR_UNKNOWN)
11389 {
11390 base = (int)get_tv_number(&argvars[1]);
11391 if (base != 2 && base != 8 && base != 10 && base != 16)
11392 {
11393 EMSG(_(e_invarg));
11394 return;
11395 }
11396 }
11397
11398 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011399 isneg = (*p == '-');
11400 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011401 p = skipwhite(p + 1);
11402 switch (base)
11403 {
11404 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
11405 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
11406 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
11407 default: what = 0;
11408 }
11409 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011410 if (isneg)
11411 rettv->vval.v_number = -n;
11412 else
11413 rettv->vval.v_number = n;
11414
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011415}
11416
11417#ifdef HAVE_STRFTIME
11418/*
11419 * "strftime({format}[, {time}])" function
11420 */
11421 static void
11422f_strftime(typval_T *argvars, typval_T *rettv)
11423{
11424 char_u result_buf[256];
11425 struct tm *curtime;
11426 time_t seconds;
11427 char_u *p;
11428
11429 rettv->v_type = VAR_STRING;
11430
11431 p = get_tv_string(&argvars[0]);
11432 if (argvars[1].v_type == VAR_UNKNOWN)
11433 seconds = time(NULL);
11434 else
11435 seconds = (time_t)get_tv_number(&argvars[1]);
11436 curtime = localtime(&seconds);
11437 /* MSVC returns NULL for an invalid value of seconds. */
11438 if (curtime == NULL)
11439 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
11440 else
11441 {
11442# ifdef FEAT_MBYTE
11443 vimconv_T conv;
11444 char_u *enc;
11445
11446 conv.vc_type = CONV_NONE;
11447 enc = enc_locale();
11448 convert_setup(&conv, p_enc, enc);
11449 if (conv.vc_type != CONV_NONE)
11450 p = string_convert(&conv, p, NULL);
11451# endif
11452 if (p != NULL)
11453 (void)strftime((char *)result_buf, sizeof(result_buf),
11454 (char *)p, curtime);
11455 else
11456 result_buf[0] = NUL;
11457
11458# ifdef FEAT_MBYTE
11459 if (conv.vc_type != CONV_NONE)
11460 vim_free(p);
11461 convert_setup(&conv, enc, p_enc);
11462 if (conv.vc_type != CONV_NONE)
11463 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
11464 else
11465# endif
11466 rettv->vval.v_string = vim_strsave(result_buf);
11467
11468# ifdef FEAT_MBYTE
11469 /* Release conversion descriptors */
11470 convert_setup(&conv, NULL, NULL);
11471 vim_free(enc);
11472# endif
11473 }
11474}
11475#endif
11476
11477/*
11478 * "strgetchar()" function
11479 */
11480 static void
11481f_strgetchar(typval_T *argvars, typval_T *rettv)
11482{
11483 char_u *str;
11484 int len;
11485 int error = FALSE;
11486 int charidx;
11487
11488 rettv->vval.v_number = -1;
11489 str = get_tv_string_chk(&argvars[0]);
11490 if (str == NULL)
11491 return;
11492 len = (int)STRLEN(str);
11493 charidx = (int)get_tv_number_chk(&argvars[1], &error);
11494 if (error)
11495 return;
11496#ifdef FEAT_MBYTE
11497 {
11498 int byteidx = 0;
11499
11500 while (charidx >= 0 && byteidx < len)
11501 {
11502 if (charidx == 0)
11503 {
11504 rettv->vval.v_number = mb_ptr2char(str + byteidx);
11505 break;
11506 }
11507 --charidx;
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011508 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011509 }
11510 }
11511#else
11512 if (charidx < len)
11513 rettv->vval.v_number = str[charidx];
11514#endif
11515}
11516
11517/*
11518 * "stridx()" function
11519 */
11520 static void
11521f_stridx(typval_T *argvars, typval_T *rettv)
11522{
11523 char_u buf[NUMBUFLEN];
11524 char_u *needle;
11525 char_u *haystack;
11526 char_u *save_haystack;
11527 char_u *pos;
11528 int start_idx;
11529
11530 needle = get_tv_string_chk(&argvars[1]);
11531 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
11532 rettv->vval.v_number = -1;
11533 if (needle == NULL || haystack == NULL)
11534 return; /* type error; errmsg already given */
11535
11536 if (argvars[2].v_type != VAR_UNKNOWN)
11537 {
11538 int error = FALSE;
11539
11540 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
11541 if (error || start_idx >= (int)STRLEN(haystack))
11542 return;
11543 if (start_idx >= 0)
11544 haystack += start_idx;
11545 }
11546
11547 pos = (char_u *)strstr((char *)haystack, (char *)needle);
11548 if (pos != NULL)
11549 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
11550}
11551
11552/*
11553 * "string()" function
11554 */
11555 static void
11556f_string(typval_T *argvars, typval_T *rettv)
11557{
11558 char_u *tofree;
11559 char_u numbuf[NUMBUFLEN];
11560
11561 rettv->v_type = VAR_STRING;
11562 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
11563 get_copyID());
11564 /* Make a copy if we have a value but it's not in allocated memory. */
11565 if (rettv->vval.v_string != NULL && tofree == NULL)
11566 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
11567}
11568
11569/*
11570 * "strlen()" function
11571 */
11572 static void
11573f_strlen(typval_T *argvars, typval_T *rettv)
11574{
11575 rettv->vval.v_number = (varnumber_T)(STRLEN(
11576 get_tv_string(&argvars[0])));
11577}
11578
11579/*
11580 * "strchars()" function
11581 */
11582 static void
11583f_strchars(typval_T *argvars, typval_T *rettv)
11584{
11585 char_u *s = get_tv_string(&argvars[0]);
11586 int skipcc = 0;
11587#ifdef FEAT_MBYTE
11588 varnumber_T len = 0;
11589 int (*func_mb_ptr2char_adv)(char_u **pp);
11590#endif
11591
11592 if (argvars[1].v_type != VAR_UNKNOWN)
11593 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
11594 if (skipcc < 0 || skipcc > 1)
11595 EMSG(_(e_invarg));
11596 else
11597 {
11598#ifdef FEAT_MBYTE
11599 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
11600 while (*s != NUL)
11601 {
11602 func_mb_ptr2char_adv(&s);
11603 ++len;
11604 }
11605 rettv->vval.v_number = len;
11606#else
11607 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
11608#endif
11609 }
11610}
11611
11612/*
11613 * "strdisplaywidth()" function
11614 */
11615 static void
11616f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
11617{
11618 char_u *s = get_tv_string(&argvars[0]);
11619 int col = 0;
11620
11621 if (argvars[1].v_type != VAR_UNKNOWN)
11622 col = (int)get_tv_number(&argvars[1]);
11623
11624 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
11625}
11626
11627/*
11628 * "strwidth()" function
11629 */
11630 static void
11631f_strwidth(typval_T *argvars, typval_T *rettv)
11632{
11633 char_u *s = get_tv_string(&argvars[0]);
11634
11635 rettv->vval.v_number = (varnumber_T)(
11636#ifdef FEAT_MBYTE
11637 mb_string2cells(s, -1)
11638#else
11639 STRLEN(s)
11640#endif
11641 );
11642}
11643
11644/*
11645 * "strcharpart()" function
11646 */
11647 static void
11648f_strcharpart(typval_T *argvars, typval_T *rettv)
11649{
11650#ifdef FEAT_MBYTE
11651 char_u *p;
11652 int nchar;
11653 int nbyte = 0;
11654 int charlen;
11655 int len = 0;
11656 int slen;
11657 int error = FALSE;
11658
11659 p = get_tv_string(&argvars[0]);
11660 slen = (int)STRLEN(p);
11661
11662 nchar = (int)get_tv_number_chk(&argvars[1], &error);
11663 if (!error)
11664 {
11665 if (nchar > 0)
11666 while (nchar > 0 && nbyte < slen)
11667 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011668 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011669 --nchar;
11670 }
11671 else
11672 nbyte = nchar;
11673 if (argvars[2].v_type != VAR_UNKNOWN)
11674 {
11675 charlen = (int)get_tv_number(&argvars[2]);
11676 while (charlen > 0 && nbyte + len < slen)
11677 {
11678 int off = nbyte + len;
11679
11680 if (off < 0)
11681 len += 1;
11682 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011683 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011684 --charlen;
11685 }
11686 }
11687 else
11688 len = slen - nbyte; /* default: all bytes that are available. */
11689 }
11690
11691 /*
11692 * Only return the overlap between the specified part and the actual
11693 * string.
11694 */
11695 if (nbyte < 0)
11696 {
11697 len += nbyte;
11698 nbyte = 0;
11699 }
11700 else if (nbyte > slen)
11701 nbyte = slen;
11702 if (len < 0)
11703 len = 0;
11704 else if (nbyte + len > slen)
11705 len = slen - nbyte;
11706
11707 rettv->v_type = VAR_STRING;
11708 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
11709#else
11710 f_strpart(argvars, rettv);
11711#endif
11712}
11713
11714/*
11715 * "strpart()" function
11716 */
11717 static void
11718f_strpart(typval_T *argvars, typval_T *rettv)
11719{
11720 char_u *p;
11721 int n;
11722 int len;
11723 int slen;
11724 int error = FALSE;
11725
11726 p = get_tv_string(&argvars[0]);
11727 slen = (int)STRLEN(p);
11728
11729 n = (int)get_tv_number_chk(&argvars[1], &error);
11730 if (error)
11731 len = 0;
11732 else if (argvars[2].v_type != VAR_UNKNOWN)
11733 len = (int)get_tv_number(&argvars[2]);
11734 else
11735 len = slen - n; /* default len: all bytes that are available. */
11736
11737 /*
11738 * Only return the overlap between the specified part and the actual
11739 * string.
11740 */
11741 if (n < 0)
11742 {
11743 len += n;
11744 n = 0;
11745 }
11746 else if (n > slen)
11747 n = slen;
11748 if (len < 0)
11749 len = 0;
11750 else if (n + len > slen)
11751 len = slen - n;
11752
11753 rettv->v_type = VAR_STRING;
11754 rettv->vval.v_string = vim_strnsave(p + n, len);
11755}
11756
11757/*
11758 * "strridx()" function
11759 */
11760 static void
11761f_strridx(typval_T *argvars, typval_T *rettv)
11762{
11763 char_u buf[NUMBUFLEN];
11764 char_u *needle;
11765 char_u *haystack;
11766 char_u *rest;
11767 char_u *lastmatch = NULL;
11768 int haystack_len, end_idx;
11769
11770 needle = get_tv_string_chk(&argvars[1]);
11771 haystack = get_tv_string_buf_chk(&argvars[0], buf);
11772
11773 rettv->vval.v_number = -1;
11774 if (needle == NULL || haystack == NULL)
11775 return; /* type error; errmsg already given */
11776
11777 haystack_len = (int)STRLEN(haystack);
11778 if (argvars[2].v_type != VAR_UNKNOWN)
11779 {
11780 /* Third argument: upper limit for index */
11781 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
11782 if (end_idx < 0)
11783 return; /* can never find a match */
11784 }
11785 else
11786 end_idx = haystack_len;
11787
11788 if (*needle == NUL)
11789 {
11790 /* Empty string matches past the end. */
11791 lastmatch = haystack + end_idx;
11792 }
11793 else
11794 {
11795 for (rest = haystack; *rest != '\0'; ++rest)
11796 {
11797 rest = (char_u *)strstr((char *)rest, (char *)needle);
11798 if (rest == NULL || rest > haystack + end_idx)
11799 break;
11800 lastmatch = rest;
11801 }
11802 }
11803
11804 if (lastmatch == NULL)
11805 rettv->vval.v_number = -1;
11806 else
11807 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
11808}
11809
11810/*
11811 * "strtrans()" function
11812 */
11813 static void
11814f_strtrans(typval_T *argvars, typval_T *rettv)
11815{
11816 rettv->v_type = VAR_STRING;
11817 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
11818}
11819
11820/*
11821 * "submatch()" function
11822 */
11823 static void
11824f_submatch(typval_T *argvars, typval_T *rettv)
11825{
11826 int error = FALSE;
11827 int no;
11828 int retList = 0;
11829
11830 no = (int)get_tv_number_chk(&argvars[0], &error);
11831 if (error)
11832 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020011833 if (no < 0 || no >= NSUBEXP)
11834 {
Bram Moolenaar79518e22017-02-17 16:31:35 +010011835 EMSGN(_("E935: invalid submatch number: %d"), no);
11836 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020011837 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011838 if (argvars[1].v_type != VAR_UNKNOWN)
11839 retList = (int)get_tv_number_chk(&argvars[1], &error);
11840 if (error)
11841 return;
11842
11843 if (retList == 0)
11844 {
11845 rettv->v_type = VAR_STRING;
11846 rettv->vval.v_string = reg_submatch(no);
11847 }
11848 else
11849 {
11850 rettv->v_type = VAR_LIST;
11851 rettv->vval.v_list = reg_submatch_list(no);
11852 }
11853}
11854
11855/*
11856 * "substitute()" function
11857 */
11858 static void
11859f_substitute(typval_T *argvars, typval_T *rettv)
11860{
11861 char_u patbuf[NUMBUFLEN];
11862 char_u subbuf[NUMBUFLEN];
11863 char_u flagsbuf[NUMBUFLEN];
11864
11865 char_u *str = get_tv_string_chk(&argvars[0]);
11866 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011867 char_u *sub = NULL;
11868 typval_T *expr = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011869 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
11870
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011871 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
11872 expr = &argvars[2];
11873 else
11874 sub = get_tv_string_buf_chk(&argvars[2], subbuf);
11875
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011876 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011877 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
11878 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011879 rettv->vval.v_string = NULL;
11880 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011881 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011882}
11883
11884/*
11885 * "synID(lnum, col, trans)" function
11886 */
11887 static void
11888f_synID(typval_T *argvars UNUSED, typval_T *rettv)
11889{
11890 int id = 0;
11891#ifdef FEAT_SYN_HL
11892 linenr_T lnum;
11893 colnr_T col;
11894 int trans;
11895 int transerr = FALSE;
11896
11897 lnum = get_tv_lnum(argvars); /* -1 on type error */
11898 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
11899 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
11900
11901 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11902 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
11903 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
11904#endif
11905
11906 rettv->vval.v_number = id;
11907}
11908
11909/*
11910 * "synIDattr(id, what [, mode])" function
11911 */
11912 static void
11913f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
11914{
11915 char_u *p = NULL;
11916#ifdef FEAT_SYN_HL
11917 int id;
11918 char_u *what;
11919 char_u *mode;
11920 char_u modebuf[NUMBUFLEN];
11921 int modec;
11922
11923 id = (int)get_tv_number(&argvars[0]);
11924 what = get_tv_string(&argvars[1]);
11925 if (argvars[2].v_type != VAR_UNKNOWN)
11926 {
11927 mode = get_tv_string_buf(&argvars[2], modebuf);
11928 modec = TOLOWER_ASC(mode[0]);
11929 if (modec != 't' && modec != 'c' && modec != 'g')
11930 modec = 0; /* replace invalid with current */
11931 }
11932 else
11933 {
11934#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
11935 if (USE_24BIT)
11936 modec = 'g';
11937 else
11938#endif
11939 if (t_colors > 1)
11940 modec = 'c';
11941 else
11942 modec = 't';
11943 }
11944
11945
11946 switch (TOLOWER_ASC(what[0]))
11947 {
11948 case 'b':
11949 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
11950 p = highlight_color(id, what, modec);
11951 else /* bold */
11952 p = highlight_has_attr(id, HL_BOLD, modec);
11953 break;
11954
11955 case 'f': /* fg[#] or font */
11956 p = highlight_color(id, what, modec);
11957 break;
11958
11959 case 'i':
11960 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
11961 p = highlight_has_attr(id, HL_INVERSE, modec);
11962 else /* italic */
11963 p = highlight_has_attr(id, HL_ITALIC, modec);
11964 break;
11965
11966 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020011967 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011968 break;
11969
11970 case 'r': /* reverse */
11971 p = highlight_has_attr(id, HL_INVERSE, modec);
11972 break;
11973
11974 case 's':
11975 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
11976 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020011977 /* strikeout */
11978 else if (TOLOWER_ASC(what[1]) == 't' &&
11979 TOLOWER_ASC(what[2]) == 'r')
11980 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011981 else /* standout */
11982 p = highlight_has_attr(id, HL_STANDOUT, modec);
11983 break;
11984
11985 case 'u':
11986 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
11987 /* underline */
11988 p = highlight_has_attr(id, HL_UNDERLINE, modec);
11989 else
11990 /* undercurl */
11991 p = highlight_has_attr(id, HL_UNDERCURL, modec);
11992 break;
11993 }
11994
11995 if (p != NULL)
11996 p = vim_strsave(p);
11997#endif
11998 rettv->v_type = VAR_STRING;
11999 rettv->vval.v_string = p;
12000}
12001
12002/*
12003 * "synIDtrans(id)" function
12004 */
12005 static void
12006f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
12007{
12008 int id;
12009
12010#ifdef FEAT_SYN_HL
12011 id = (int)get_tv_number(&argvars[0]);
12012
12013 if (id > 0)
12014 id = syn_get_final_id(id);
12015 else
12016#endif
12017 id = 0;
12018
12019 rettv->vval.v_number = id;
12020}
12021
12022/*
12023 * "synconcealed(lnum, col)" function
12024 */
12025 static void
12026f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
12027{
12028#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12029 linenr_T lnum;
12030 colnr_T col;
12031 int syntax_flags = 0;
12032 int cchar;
12033 int matchid = 0;
12034 char_u str[NUMBUFLEN];
12035#endif
12036
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012037 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012038
12039#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12040 lnum = get_tv_lnum(argvars); /* -1 on type error */
12041 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12042
12043 vim_memset(str, NUL, sizeof(str));
12044
12045 if (rettv_list_alloc(rettv) != FAIL)
12046 {
12047 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12048 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12049 && curwin->w_p_cole > 0)
12050 {
12051 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
12052 syntax_flags = get_syntax_info(&matchid);
12053
12054 /* get the conceal character */
12055 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
12056 {
12057 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020012058 if (cchar == NUL && curwin->w_p_cole == 1)
12059 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012060 if (cchar != NUL)
12061 {
12062# ifdef FEAT_MBYTE
12063 if (has_mbyte)
12064 (*mb_char2bytes)(cchar, str);
12065 else
12066# endif
12067 str[0] = cchar;
12068 }
12069 }
12070 }
12071
12072 list_append_number(rettv->vval.v_list,
12073 (syntax_flags & HL_CONCEAL) != 0);
12074 /* -1 to auto-determine strlen */
12075 list_append_string(rettv->vval.v_list, str, -1);
12076 list_append_number(rettv->vval.v_list, matchid);
12077 }
12078#endif
12079}
12080
12081/*
12082 * "synstack(lnum, col)" function
12083 */
12084 static void
12085f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
12086{
12087#ifdef FEAT_SYN_HL
12088 linenr_T lnum;
12089 colnr_T col;
12090 int i;
12091 int id;
12092#endif
12093
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012094 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012095
12096#ifdef FEAT_SYN_HL
12097 lnum = get_tv_lnum(argvars); /* -1 on type error */
12098 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12099
12100 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12101 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12102 && rettv_list_alloc(rettv) != FAIL)
12103 {
12104 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
12105 for (i = 0; ; ++i)
12106 {
12107 id = syn_get_stack_item(i);
12108 if (id < 0)
12109 break;
12110 if (list_append_number(rettv->vval.v_list, id) == FAIL)
12111 break;
12112 }
12113 }
12114#endif
12115}
12116
12117 static void
12118get_cmd_output_as_rettv(
12119 typval_T *argvars,
12120 typval_T *rettv,
12121 int retlist)
12122{
12123 char_u *res = NULL;
12124 char_u *p;
12125 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012126 int err = FALSE;
12127 FILE *fd;
12128 list_T *list = NULL;
12129 int flags = SHELL_SILENT;
12130
12131 rettv->v_type = VAR_STRING;
12132 rettv->vval.v_string = NULL;
12133 if (check_restricted() || check_secure())
12134 goto errret;
12135
12136 if (argvars[1].v_type != VAR_UNKNOWN)
12137 {
12138 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010012139 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012140 * command.
12141 */
12142 if ((infile = vim_tempname('i', TRUE)) == NULL)
12143 {
12144 EMSG(_(e_notmp));
12145 goto errret;
12146 }
12147
12148 fd = mch_fopen((char *)infile, WRITEBIN);
12149 if (fd == NULL)
12150 {
12151 EMSG2(_(e_notopen), infile);
12152 goto errret;
12153 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010012154 if (argvars[1].v_type == VAR_NUMBER)
12155 {
12156 linenr_T lnum;
12157 buf_T *buf;
12158
12159 buf = buflist_findnr(argvars[1].vval.v_number);
12160 if (buf == NULL)
12161 {
12162 EMSGN(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010012163 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010012164 goto errret;
12165 }
12166
12167 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
12168 {
12169 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
12170 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
12171 {
12172 err = TRUE;
12173 break;
12174 }
12175 if (putc(NL, fd) == EOF)
12176 {
12177 err = TRUE;
12178 break;
12179 }
12180 }
12181 }
12182 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012183 {
12184 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
12185 err = TRUE;
12186 }
12187 else
12188 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010012189 size_t len;
12190 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012191
12192 p = get_tv_string_buf_chk(&argvars[1], buf);
12193 if (p == NULL)
12194 {
12195 fclose(fd);
12196 goto errret; /* type error; errmsg already given */
12197 }
12198 len = STRLEN(p);
12199 if (len > 0 && fwrite(p, len, 1, fd) != 1)
12200 err = TRUE;
12201 }
12202 if (fclose(fd) != 0)
12203 err = TRUE;
12204 if (err)
12205 {
12206 EMSG(_("E677: Error writing temp file"));
12207 goto errret;
12208 }
12209 }
12210
12211 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
12212 * echoes typeahead, that messes up the display. */
12213 if (!msg_silent)
12214 flags += SHELL_COOKED;
12215
12216 if (retlist)
12217 {
12218 int len;
12219 listitem_T *li;
12220 char_u *s = NULL;
12221 char_u *start;
12222 char_u *end;
12223 int i;
12224
12225 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
12226 if (res == NULL)
12227 goto errret;
12228
12229 list = list_alloc();
12230 if (list == NULL)
12231 goto errret;
12232
12233 for (i = 0; i < len; ++i)
12234 {
12235 start = res + i;
12236 while (i < len && res[i] != NL)
12237 ++i;
12238 end = res + i;
12239
12240 s = alloc((unsigned)(end - start + 1));
12241 if (s == NULL)
12242 goto errret;
12243
12244 for (p = s; start < end; ++p, ++start)
12245 *p = *start == NUL ? NL : *start;
12246 *p = NUL;
12247
12248 li = listitem_alloc();
12249 if (li == NULL)
12250 {
12251 vim_free(s);
12252 goto errret;
12253 }
12254 li->li_tv.v_type = VAR_STRING;
12255 li->li_tv.v_lock = 0;
12256 li->li_tv.vval.v_string = s;
12257 list_append(list, li);
12258 }
12259
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012260 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012261 list = NULL;
12262 }
12263 else
12264 {
12265 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
12266#ifdef USE_CR
12267 /* translate <CR> into <NL> */
12268 if (res != NULL)
12269 {
12270 char_u *s;
12271
12272 for (s = res; *s; ++s)
12273 {
12274 if (*s == CAR)
12275 *s = NL;
12276 }
12277 }
12278#else
12279# ifdef USE_CRNL
12280 /* translate <CR><NL> into <NL> */
12281 if (res != NULL)
12282 {
12283 char_u *s, *d;
12284
12285 d = res;
12286 for (s = res; *s; ++s)
12287 {
12288 if (s[0] == CAR && s[1] == NL)
12289 ++s;
12290 *d++ = *s;
12291 }
12292 *d = NUL;
12293 }
12294# endif
12295#endif
12296 rettv->vval.v_string = res;
12297 res = NULL;
12298 }
12299
12300errret:
12301 if (infile != NULL)
12302 {
12303 mch_remove(infile);
12304 vim_free(infile);
12305 }
12306 if (res != NULL)
12307 vim_free(res);
12308 if (list != NULL)
12309 list_free(list);
12310}
12311
12312/*
12313 * "system()" function
12314 */
12315 static void
12316f_system(typval_T *argvars, typval_T *rettv)
12317{
12318 get_cmd_output_as_rettv(argvars, rettv, FALSE);
12319}
12320
12321/*
12322 * "systemlist()" function
12323 */
12324 static void
12325f_systemlist(typval_T *argvars, typval_T *rettv)
12326{
12327 get_cmd_output_as_rettv(argvars, rettv, TRUE);
12328}
12329
12330/*
12331 * "tabpagebuflist()" function
12332 */
12333 static void
12334f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12335{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012336 tabpage_T *tp;
12337 win_T *wp = NULL;
12338
12339 if (argvars[0].v_type == VAR_UNKNOWN)
12340 wp = firstwin;
12341 else
12342 {
12343 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12344 if (tp != NULL)
12345 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12346 }
12347 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
12348 {
12349 for (; wp != NULL; wp = wp->w_next)
12350 if (list_append_number(rettv->vval.v_list,
12351 wp->w_buffer->b_fnum) == FAIL)
12352 break;
12353 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012354}
12355
12356
12357/*
12358 * "tabpagenr()" function
12359 */
12360 static void
12361f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
12362{
12363 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012364 char_u *arg;
12365
12366 if (argvars[0].v_type != VAR_UNKNOWN)
12367 {
12368 arg = get_tv_string_chk(&argvars[0]);
12369 nr = 0;
12370 if (arg != NULL)
12371 {
12372 if (STRCMP(arg, "$") == 0)
12373 nr = tabpage_index(NULL) - 1;
12374 else
12375 EMSG2(_(e_invexpr2), arg);
12376 }
12377 }
12378 else
12379 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012380 rettv->vval.v_number = nr;
12381}
12382
12383
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012384static int get_winnr(tabpage_T *tp, typval_T *argvar);
12385
12386/*
12387 * Common code for tabpagewinnr() and winnr().
12388 */
12389 static int
12390get_winnr(tabpage_T *tp, typval_T *argvar)
12391{
12392 win_T *twin;
12393 int nr = 1;
12394 win_T *wp;
12395 char_u *arg;
12396
12397 twin = (tp == curtab) ? curwin : tp->tp_curwin;
12398 if (argvar->v_type != VAR_UNKNOWN)
12399 {
12400 arg = get_tv_string_chk(argvar);
12401 if (arg == NULL)
12402 nr = 0; /* type error; errmsg already given */
12403 else if (STRCMP(arg, "$") == 0)
12404 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
12405 else if (STRCMP(arg, "#") == 0)
12406 {
12407 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
12408 if (twin == NULL)
12409 nr = 0;
12410 }
12411 else
12412 {
12413 EMSG2(_(e_invexpr2), arg);
12414 nr = 0;
12415 }
12416 }
12417
12418 if (nr > 0)
12419 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12420 wp != twin; wp = wp->w_next)
12421 {
12422 if (wp == NULL)
12423 {
12424 /* didn't find it in this tabpage */
12425 nr = 0;
12426 break;
12427 }
12428 ++nr;
12429 }
12430 return nr;
12431}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012432
12433/*
12434 * "tabpagewinnr()" function
12435 */
12436 static void
12437f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
12438{
12439 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012440 tabpage_T *tp;
12441
12442 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12443 if (tp == NULL)
12444 nr = 0;
12445 else
12446 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012447 rettv->vval.v_number = nr;
12448}
12449
12450
12451/*
12452 * "tagfiles()" function
12453 */
12454 static void
12455f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
12456{
12457 char_u *fname;
12458 tagname_T tn;
12459 int first;
12460
12461 if (rettv_list_alloc(rettv) == FAIL)
12462 return;
12463 fname = alloc(MAXPATHL);
12464 if (fname == NULL)
12465 return;
12466
12467 for (first = TRUE; ; first = FALSE)
12468 if (get_tagfname(&tn, first, fname) == FAIL
12469 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
12470 break;
12471 tagname_free(&tn);
12472 vim_free(fname);
12473}
12474
12475/*
12476 * "taglist()" function
12477 */
12478 static void
12479f_taglist(typval_T *argvars, typval_T *rettv)
12480{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012481 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012482 char_u *tag_pattern;
12483
12484 tag_pattern = get_tv_string(&argvars[0]);
12485
12486 rettv->vval.v_number = FALSE;
12487 if (*tag_pattern == NUL)
12488 return;
12489
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012490 if (argvars[1].v_type != VAR_UNKNOWN)
12491 fname = get_tv_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012492 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012493 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012494}
12495
12496/*
12497 * "tempname()" function
12498 */
12499 static void
12500f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
12501{
12502 static int x = 'A';
12503
12504 rettv->v_type = VAR_STRING;
12505 rettv->vval.v_string = vim_tempname(x, FALSE);
12506
12507 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
12508 * names. Skip 'I' and 'O', they are used for shell redirection. */
12509 do
12510 {
12511 if (x == 'Z')
12512 x = '0';
12513 else if (x == '9')
12514 x = 'A';
12515 else
12516 {
12517#ifdef EBCDIC
12518 if (x == 'I')
12519 x = 'J';
12520 else if (x == 'R')
12521 x = 'S';
12522 else
12523#endif
12524 ++x;
12525 }
12526 } while (x == 'I' || x == 'O');
12527}
12528
12529#ifdef FEAT_FLOAT
12530/*
12531 * "tan()" function
12532 */
12533 static void
12534f_tan(typval_T *argvars, typval_T *rettv)
12535{
12536 float_T f = 0.0;
12537
12538 rettv->v_type = VAR_FLOAT;
12539 if (get_float_arg(argvars, &f) == OK)
12540 rettv->vval.v_float = tan(f);
12541 else
12542 rettv->vval.v_float = 0.0;
12543}
12544
12545/*
12546 * "tanh()" function
12547 */
12548 static void
12549f_tanh(typval_T *argvars, typval_T *rettv)
12550{
12551 float_T f = 0.0;
12552
12553 rettv->v_type = VAR_FLOAT;
12554 if (get_float_arg(argvars, &f) == OK)
12555 rettv->vval.v_float = tanh(f);
12556 else
12557 rettv->vval.v_float = 0.0;
12558}
12559#endif
12560
12561/*
12562 * "test_alloc_fail(id, countdown, repeat)" function
12563 */
12564 static void
12565f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
12566{
12567 if (argvars[0].v_type != VAR_NUMBER
12568 || argvars[0].vval.v_number <= 0
12569 || argvars[1].v_type != VAR_NUMBER
12570 || argvars[1].vval.v_number < 0
12571 || argvars[2].v_type != VAR_NUMBER)
12572 EMSG(_(e_invarg));
12573 else
12574 {
12575 alloc_fail_id = argvars[0].vval.v_number;
12576 if (alloc_fail_id >= aid_last)
12577 EMSG(_(e_invarg));
12578 alloc_fail_countdown = argvars[1].vval.v_number;
12579 alloc_fail_repeat = argvars[2].vval.v_number;
12580 did_outofmem_msg = FALSE;
12581 }
12582}
12583
12584/*
12585 * "test_autochdir()"
12586 */
12587 static void
12588f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12589{
12590#if defined(FEAT_AUTOCHDIR)
12591 test_autochdir = TRUE;
12592#endif
12593}
12594
12595/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020012596 * "test_feedinput()"
12597 */
12598 static void
12599f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
12600{
12601#ifdef USE_INPUT_BUF
12602 char_u *val = get_tv_string_chk(&argvars[0]);
12603
12604 if (val != NULL)
12605 {
12606 trash_input_buf();
12607 add_to_input_buf_csi(val, (int)STRLEN(val));
12608 }
12609#endif
12610}
12611
12612/*
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012613 * "test_disable({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012614 */
12615 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012616f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012617{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012618 char_u *name = (char_u *)"";
12619 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012620 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012621
12622 if (argvars[0].v_type != VAR_STRING
12623 || (argvars[1].v_type) != VAR_NUMBER)
12624 EMSG(_(e_invarg));
12625 else
12626 {
12627 name = get_tv_string_chk(&argvars[0]);
12628 val = (int)get_tv_number(&argvars[1]);
12629
12630 if (STRCMP(name, (char_u *)"redraw") == 0)
12631 disable_redraw_for_testing = val;
12632 else if (STRCMP(name, (char_u *)"char_avail") == 0)
12633 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012634 else if (STRCMP(name, (char_u *)"starting") == 0)
12635 {
12636 if (val)
12637 {
12638 if (save_starting < 0)
12639 save_starting = starting;
12640 starting = 0;
12641 }
12642 else
12643 {
12644 starting = save_starting;
12645 save_starting = -1;
12646 }
12647 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012648 else if (STRCMP(name, (char_u *)"ALL") == 0)
12649 {
12650 disable_char_avail_for_testing = FALSE;
12651 disable_redraw_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012652 if (save_starting >= 0)
12653 {
12654 starting = save_starting;
12655 save_starting = -1;
12656 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012657 }
12658 else
12659 EMSG2(_(e_invarg2), name);
12660 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012661}
12662
12663/*
12664 * "test_garbagecollect_now()" function
12665 */
12666 static void
12667f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12668{
12669 /* This is dangerous, any Lists and Dicts used internally may be freed
12670 * while still in use. */
12671 garbage_collect(TRUE);
12672}
12673
Bram Moolenaare0c31f62017-03-01 15:07:05 +010012674/*
12675 * "test_ignore_error()" function
12676 */
12677 static void
12678f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
12679{
12680 ignore_error_for_testing(get_tv_string(&argvars[0]));
12681}
12682
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012683#ifdef FEAT_JOB_CHANNEL
12684 static void
12685f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
12686{
12687 rettv->v_type = VAR_CHANNEL;
12688 rettv->vval.v_channel = NULL;
12689}
12690#endif
12691
12692 static void
12693f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
12694{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012695 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012696}
12697
12698#ifdef FEAT_JOB_CHANNEL
12699 static void
12700f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
12701{
12702 rettv->v_type = VAR_JOB;
12703 rettv->vval.v_job = NULL;
12704}
12705#endif
12706
12707 static void
12708f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
12709{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012710 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012711}
12712
12713 static void
12714f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
12715{
12716 rettv->v_type = VAR_PARTIAL;
12717 rettv->vval.v_partial = NULL;
12718}
12719
12720 static void
12721f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
12722{
12723 rettv->v_type = VAR_STRING;
12724 rettv->vval.v_string = NULL;
12725}
12726
12727 static void
12728f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
12729{
12730 time_for_testing = (time_t)get_tv_number(&argvars[0]);
12731}
12732
12733#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
12734/*
12735 * Get a callback from "arg". It can be a Funcref or a function name.
12736 * When "arg" is zero return an empty string.
12737 * Return NULL for an invalid argument.
12738 */
12739 char_u *
12740get_callback(typval_T *arg, partial_T **pp)
12741{
12742 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
12743 {
12744 *pp = arg->vval.v_partial;
12745 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012746 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012747 }
12748 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012749 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012750 {
12751 func_ref(arg->vval.v_string);
12752 return arg->vval.v_string;
12753 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012754 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
12755 return (char_u *)"";
12756 EMSG(_("E921: Invalid callback argument"));
12757 return NULL;
12758}
12759
12760/*
12761 * Unref/free "callback" and "partial" retured by get_callback().
12762 */
12763 void
12764free_callback(char_u *callback, partial_T *partial)
12765{
12766 if (partial != NULL)
12767 partial_unref(partial);
12768 else if (callback != NULL)
12769 {
12770 func_unref(callback);
12771 vim_free(callback);
12772 }
12773}
12774#endif
12775
12776#ifdef FEAT_TIMERS
12777/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020012778 * "timer_info([timer])" function
12779 */
12780 static void
12781f_timer_info(typval_T *argvars, typval_T *rettv)
12782{
12783 timer_T *timer = NULL;
12784
12785 if (rettv_list_alloc(rettv) != OK)
12786 return;
12787 if (argvars[0].v_type != VAR_UNKNOWN)
12788 {
12789 if (argvars[0].v_type != VAR_NUMBER)
12790 EMSG(_(e_number_exp));
12791 else
12792 {
12793 timer = find_timer((int)get_tv_number(&argvars[0]));
12794 if (timer != NULL)
12795 add_timer_info(rettv, timer);
12796 }
12797 }
12798 else
12799 add_timer_info_all(rettv);
12800}
12801
12802/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012803 * "timer_pause(timer, paused)" function
12804 */
12805 static void
12806f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
12807{
12808 timer_T *timer = NULL;
12809 int paused = (int)get_tv_number(&argvars[1]);
12810
12811 if (argvars[0].v_type != VAR_NUMBER)
12812 EMSG(_(e_number_exp));
12813 else
12814 {
12815 timer = find_timer((int)get_tv_number(&argvars[0]));
12816 if (timer != NULL)
12817 timer->tr_paused = paused;
12818 }
12819}
12820
12821/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012822 * "timer_start(time, callback [, options])" function
12823 */
12824 static void
12825f_timer_start(typval_T *argvars, typval_T *rettv)
12826{
Bram Moolenaar75537a92016-09-05 22:45:28 +020012827 long msec = (long)get_tv_number(&argvars[0]);
12828 timer_T *timer;
12829 int repeat = 0;
12830 char_u *callback;
12831 dict_T *dict;
12832 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012833
Bram Moolenaar75537a92016-09-05 22:45:28 +020012834 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012835 if (check_secure())
12836 return;
12837 if (argvars[2].v_type != VAR_UNKNOWN)
12838 {
12839 if (argvars[2].v_type != VAR_DICT
12840 || (dict = argvars[2].vval.v_dict) == NULL)
12841 {
12842 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
12843 return;
12844 }
12845 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
12846 repeat = get_dict_number(dict, (char_u *)"repeat");
12847 }
12848
Bram Moolenaar75537a92016-09-05 22:45:28 +020012849 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012850 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020012851 return;
12852
12853 timer = create_timer(msec, repeat);
12854 if (timer == NULL)
12855 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012856 else
12857 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020012858 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020012859 timer->tr_callback = vim_strsave(callback);
12860 else
12861 /* pointer into the partial */
12862 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020012863 timer->tr_partial = partial;
12864 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012865 }
12866}
12867
12868/*
12869 * "timer_stop(timer)" function
12870 */
12871 static void
12872f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
12873{
12874 timer_T *timer;
12875
12876 if (argvars[0].v_type != VAR_NUMBER)
12877 {
12878 EMSG(_(e_number_exp));
12879 return;
12880 }
12881 timer = find_timer((int)get_tv_number(&argvars[0]));
12882 if (timer != NULL)
12883 stop_timer(timer);
12884}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012885
12886/*
12887 * "timer_stopall()" function
12888 */
12889 static void
12890f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12891{
12892 stop_all_timers();
12893}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012894#endif
12895
12896/*
12897 * "tolower(string)" function
12898 */
12899 static void
12900f_tolower(typval_T *argvars, typval_T *rettv)
12901{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012902 rettv->v_type = VAR_STRING;
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +010012903 rettv->vval.v_string = strlow_save(get_tv_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012904}
12905
12906/*
12907 * "toupper(string)" function
12908 */
12909 static void
12910f_toupper(typval_T *argvars, typval_T *rettv)
12911{
12912 rettv->v_type = VAR_STRING;
12913 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
12914}
12915
12916/*
12917 * "tr(string, fromstr, tostr)" function
12918 */
12919 static void
12920f_tr(typval_T *argvars, typval_T *rettv)
12921{
12922 char_u *in_str;
12923 char_u *fromstr;
12924 char_u *tostr;
12925 char_u *p;
12926#ifdef FEAT_MBYTE
12927 int inlen;
12928 int fromlen;
12929 int tolen;
12930 int idx;
12931 char_u *cpstr;
12932 int cplen;
12933 int first = TRUE;
12934#endif
12935 char_u buf[NUMBUFLEN];
12936 char_u buf2[NUMBUFLEN];
12937 garray_T ga;
12938
12939 in_str = get_tv_string(&argvars[0]);
12940 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
12941 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
12942
12943 /* Default return value: empty string. */
12944 rettv->v_type = VAR_STRING;
12945 rettv->vval.v_string = NULL;
12946 if (fromstr == NULL || tostr == NULL)
12947 return; /* type error; errmsg already given */
12948 ga_init2(&ga, (int)sizeof(char), 80);
12949
12950#ifdef FEAT_MBYTE
12951 if (!has_mbyte)
12952#endif
12953 /* not multi-byte: fromstr and tostr must be the same length */
12954 if (STRLEN(fromstr) != STRLEN(tostr))
12955 {
12956#ifdef FEAT_MBYTE
12957error:
12958#endif
12959 EMSG2(_(e_invarg2), fromstr);
12960 ga_clear(&ga);
12961 return;
12962 }
12963
12964 /* fromstr and tostr have to contain the same number of chars */
12965 while (*in_str != NUL)
12966 {
12967#ifdef FEAT_MBYTE
12968 if (has_mbyte)
12969 {
12970 inlen = (*mb_ptr2len)(in_str);
12971 cpstr = in_str;
12972 cplen = inlen;
12973 idx = 0;
12974 for (p = fromstr; *p != NUL; p += fromlen)
12975 {
12976 fromlen = (*mb_ptr2len)(p);
12977 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
12978 {
12979 for (p = tostr; *p != NUL; p += tolen)
12980 {
12981 tolen = (*mb_ptr2len)(p);
12982 if (idx-- == 0)
12983 {
12984 cplen = tolen;
12985 cpstr = p;
12986 break;
12987 }
12988 }
12989 if (*p == NUL) /* tostr is shorter than fromstr */
12990 goto error;
12991 break;
12992 }
12993 ++idx;
12994 }
12995
12996 if (first && cpstr == in_str)
12997 {
12998 /* Check that fromstr and tostr have the same number of
12999 * (multi-byte) characters. Done only once when a character
13000 * of in_str doesn't appear in fromstr. */
13001 first = FALSE;
13002 for (p = tostr; *p != NUL; p += tolen)
13003 {
13004 tolen = (*mb_ptr2len)(p);
13005 --idx;
13006 }
13007 if (idx != 0)
13008 goto error;
13009 }
13010
13011 (void)ga_grow(&ga, cplen);
13012 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
13013 ga.ga_len += cplen;
13014
13015 in_str += inlen;
13016 }
13017 else
13018#endif
13019 {
13020 /* When not using multi-byte chars we can do it faster. */
13021 p = vim_strchr(fromstr, *in_str);
13022 if (p != NULL)
13023 ga_append(&ga, tostr[p - fromstr]);
13024 else
13025 ga_append(&ga, *in_str);
13026 ++in_str;
13027 }
13028 }
13029
13030 /* add a terminating NUL */
13031 (void)ga_grow(&ga, 1);
13032 ga_append(&ga, NUL);
13033
13034 rettv->vval.v_string = ga.ga_data;
13035}
13036
13037#ifdef FEAT_FLOAT
13038/*
13039 * "trunc({float})" function
13040 */
13041 static void
13042f_trunc(typval_T *argvars, typval_T *rettv)
13043{
13044 float_T f = 0.0;
13045
13046 rettv->v_type = VAR_FLOAT;
13047 if (get_float_arg(argvars, &f) == OK)
13048 /* trunc() is not in C90, use floor() or ceil() instead. */
13049 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
13050 else
13051 rettv->vval.v_float = 0.0;
13052}
13053#endif
13054
13055/*
13056 * "type(expr)" function
13057 */
13058 static void
13059f_type(typval_T *argvars, typval_T *rettv)
13060{
13061 int n = -1;
13062
13063 switch (argvars[0].v_type)
13064 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020013065 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
13066 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013067 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020013068 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
13069 case VAR_LIST: n = VAR_TYPE_LIST; break;
13070 case VAR_DICT: n = VAR_TYPE_DICT; break;
13071 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013072 case VAR_SPECIAL:
13073 if (argvars[0].vval.v_number == VVAL_FALSE
13074 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020013075 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013076 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020013077 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013078 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020013079 case VAR_JOB: n = VAR_TYPE_JOB; break;
13080 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013081 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010013082 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013083 n = -1;
13084 break;
13085 }
13086 rettv->vval.v_number = n;
13087}
13088
13089/*
13090 * "undofile(name)" function
13091 */
13092 static void
13093f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
13094{
13095 rettv->v_type = VAR_STRING;
13096#ifdef FEAT_PERSISTENT_UNDO
13097 {
13098 char_u *fname = get_tv_string(&argvars[0]);
13099
13100 if (*fname == NUL)
13101 {
13102 /* If there is no file name there will be no undo file. */
13103 rettv->vval.v_string = NULL;
13104 }
13105 else
13106 {
13107 char_u *ffname = FullName_save(fname, FALSE);
13108
13109 if (ffname != NULL)
13110 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
13111 vim_free(ffname);
13112 }
13113 }
13114#else
13115 rettv->vval.v_string = NULL;
13116#endif
13117}
13118
13119/*
13120 * "undotree()" function
13121 */
13122 static void
13123f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
13124{
13125 if (rettv_dict_alloc(rettv) == OK)
13126 {
13127 dict_T *dict = rettv->vval.v_dict;
13128 list_T *list;
13129
13130 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
13131 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
13132 dict_add_nr_str(dict, "save_last",
13133 (long)curbuf->b_u_save_nr_last, NULL);
13134 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
13135 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
13136 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
13137
13138 list = list_alloc();
13139 if (list != NULL)
13140 {
13141 u_eval_tree(curbuf->b_u_oldhead, list);
13142 dict_add_list(dict, "entries", list);
13143 }
13144 }
13145}
13146
13147/*
13148 * "values(dict)" function
13149 */
13150 static void
13151f_values(typval_T *argvars, typval_T *rettv)
13152{
13153 dict_list(argvars, rettv, 1);
13154}
13155
13156/*
13157 * "virtcol(string)" function
13158 */
13159 static void
13160f_virtcol(typval_T *argvars, typval_T *rettv)
13161{
13162 colnr_T vcol = 0;
13163 pos_T *fp;
13164 int fnum = curbuf->b_fnum;
13165
13166 fp = var2fpos(&argvars[0], FALSE, &fnum);
13167 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
13168 && fnum == curbuf->b_fnum)
13169 {
13170 getvvcol(curwin, fp, NULL, NULL, &vcol);
13171 ++vcol;
13172 }
13173
13174 rettv->vval.v_number = vcol;
13175}
13176
13177/*
13178 * "visualmode()" function
13179 */
13180 static void
13181f_visualmode(typval_T *argvars, typval_T *rettv)
13182{
13183 char_u str[2];
13184
13185 rettv->v_type = VAR_STRING;
13186 str[0] = curbuf->b_visual_mode_eval;
13187 str[1] = NUL;
13188 rettv->vval.v_string = vim_strsave(str);
13189
13190 /* A non-zero number or non-empty string argument: reset mode. */
13191 if (non_zero_arg(&argvars[0]))
13192 curbuf->b_visual_mode_eval = NUL;
13193}
13194
13195/*
13196 * "wildmenumode()" function
13197 */
13198 static void
13199f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13200{
13201#ifdef FEAT_WILDMENU
13202 if (wild_menu_showing)
13203 rettv->vval.v_number = 1;
13204#endif
13205}
13206
13207/*
13208 * "winbufnr(nr)" function
13209 */
13210 static void
13211f_winbufnr(typval_T *argvars, typval_T *rettv)
13212{
13213 win_T *wp;
13214
13215 wp = find_win_by_nr(&argvars[0], NULL);
13216 if (wp == NULL)
13217 rettv->vval.v_number = -1;
13218 else
13219 rettv->vval.v_number = wp->w_buffer->b_fnum;
13220}
13221
13222/*
13223 * "wincol()" function
13224 */
13225 static void
13226f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
13227{
13228 validate_cursor();
13229 rettv->vval.v_number = curwin->w_wcol + 1;
13230}
13231
13232/*
13233 * "winheight(nr)" function
13234 */
13235 static void
13236f_winheight(typval_T *argvars, typval_T *rettv)
13237{
13238 win_T *wp;
13239
13240 wp = find_win_by_nr(&argvars[0], NULL);
13241 if (wp == NULL)
13242 rettv->vval.v_number = -1;
13243 else
13244 rettv->vval.v_number = wp->w_height;
13245}
13246
13247/*
13248 * "winline()" function
13249 */
13250 static void
13251f_winline(typval_T *argvars UNUSED, typval_T *rettv)
13252{
13253 validate_cursor();
13254 rettv->vval.v_number = curwin->w_wrow + 1;
13255}
13256
13257/*
13258 * "winnr()" function
13259 */
13260 static void
13261f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
13262{
13263 int nr = 1;
13264
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013265 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013266 rettv->vval.v_number = nr;
13267}
13268
13269/*
13270 * "winrestcmd()" function
13271 */
13272 static void
13273f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
13274{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013275 win_T *wp;
13276 int winnr = 1;
13277 garray_T ga;
13278 char_u buf[50];
13279
13280 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020013281 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013282 {
13283 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
13284 ga_concat(&ga, buf);
13285 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
13286 ga_concat(&ga, buf);
13287 ++winnr;
13288 }
13289 ga_append(&ga, NUL);
13290
13291 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013292 rettv->v_type = VAR_STRING;
13293}
13294
13295/*
13296 * "winrestview()" function
13297 */
13298 static void
13299f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
13300{
13301 dict_T *dict;
13302
13303 if (argvars[0].v_type != VAR_DICT
13304 || (dict = argvars[0].vval.v_dict) == NULL)
13305 EMSG(_(e_invarg));
13306 else
13307 {
13308 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
13309 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
13310 if (dict_find(dict, (char_u *)"col", -1) != NULL)
13311 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
13312#ifdef FEAT_VIRTUALEDIT
13313 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
13314 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
13315#endif
13316 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
13317 {
13318 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
13319 curwin->w_set_curswant = FALSE;
13320 }
13321
13322 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
13323 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
13324#ifdef FEAT_DIFF
13325 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
13326 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
13327#endif
13328 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
13329 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
13330 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
13331 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
13332
13333 check_cursor();
13334 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020013335 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013336 changed_window_setting();
13337
13338 if (curwin->w_topline <= 0)
13339 curwin->w_topline = 1;
13340 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
13341 curwin->w_topline = curbuf->b_ml.ml_line_count;
13342#ifdef FEAT_DIFF
13343 check_topfill(curwin, TRUE);
13344#endif
13345 }
13346}
13347
13348/*
13349 * "winsaveview()" function
13350 */
13351 static void
13352f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
13353{
13354 dict_T *dict;
13355
13356 if (rettv_dict_alloc(rettv) == FAIL)
13357 return;
13358 dict = rettv->vval.v_dict;
13359
13360 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
13361 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
13362#ifdef FEAT_VIRTUALEDIT
13363 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
13364#endif
13365 update_curswant();
13366 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
13367
13368 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
13369#ifdef FEAT_DIFF
13370 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
13371#endif
13372 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
13373 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
13374}
13375
13376/*
13377 * "winwidth(nr)" function
13378 */
13379 static void
13380f_winwidth(typval_T *argvars, typval_T *rettv)
13381{
13382 win_T *wp;
13383
13384 wp = find_win_by_nr(&argvars[0], NULL);
13385 if (wp == NULL)
13386 rettv->vval.v_number = -1;
13387 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013388 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013389}
13390
13391/*
13392 * "wordcount()" function
13393 */
13394 static void
13395f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
13396{
13397 if (rettv_dict_alloc(rettv) == FAIL)
13398 return;
13399 cursor_pos_info(rettv->vval.v_dict);
13400}
13401
13402/*
13403 * "writefile()" function
13404 */
13405 static void
13406f_writefile(typval_T *argvars, typval_T *rettv)
13407{
13408 int binary = FALSE;
13409 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013410#ifdef HAVE_FSYNC
13411 int do_fsync = p_fs;
13412#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013413 char_u *fname;
13414 FILE *fd;
13415 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013416 listitem_T *li;
13417 list_T *list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013418
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013419 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013420 if (check_restricted() || check_secure())
13421 return;
13422
13423 if (argvars[0].v_type != VAR_LIST)
13424 {
13425 EMSG2(_(e_listarg), "writefile()");
13426 return;
13427 }
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013428 list = argvars[0].vval.v_list;
13429 if (list == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013430 return;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013431 for (li = list->lv_first; li != NULL; li = li->li_next)
13432 if (get_tv_string_chk(&li->li_tv) == NULL)
13433 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013434
13435 if (argvars[2].v_type != VAR_UNKNOWN)
13436 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013437 char_u *arg2 = get_tv_string_chk(&argvars[2]);
13438
13439 if (arg2 == NULL)
13440 return;
13441 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013442 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013443 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013444 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013445#ifdef HAVE_FSYNC
13446 if (vim_strchr(arg2, 's') != NULL)
13447 do_fsync = TRUE;
13448 else if (vim_strchr(arg2, 'S') != NULL)
13449 do_fsync = FALSE;
13450#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013451 }
13452
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013453 fname = get_tv_string_chk(&argvars[1]);
13454 if (fname == NULL)
13455 return;
13456
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013457 /* Always open the file in binary mode, library functions have a mind of
13458 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013459 if (*fname == NUL || (fd = mch_fopen((char *)fname,
13460 append ? APPENDBIN : WRITEBIN)) == NULL)
13461 {
13462 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
13463 ret = -1;
13464 }
13465 else
13466 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013467 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013468 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013469#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010013470 else if (do_fsync)
13471 /* Ignore the error, the user wouldn't know what to do about it.
13472 * May happen for a device. */
13473 ignored = fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013474#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013475 fclose(fd);
13476 }
13477
13478 rettv->vval.v_number = ret;
13479}
13480
13481/*
13482 * "xor(expr, expr)" function
13483 */
13484 static void
13485f_xor(typval_T *argvars, typval_T *rettv)
13486{
13487 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
13488 ^ get_tv_number_chk(&argvars[1], NULL);
13489}
13490
13491
13492#endif /* FEAT_EVAL */