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