blob: 632b982de080b66c3093c14db391849eab689e75 [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;
Bram Moolenaar54591292018-02-09 20:53:59 +01004616 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004617
4618 rettv->v_type = VAR_STRING;
4619 rettv->vval.v_string = NULL;
4620
Bram Moolenaar54591292018-02-09 20:53:59 +01004621 if (argvars[0].v_type == VAR_NUMBER && argvars[0].vval.v_number == -1)
4622 global = TRUE;
4623 else
4624 wp = find_tabwin(&argvars[0], &argvars[1]);
4625
4626 if (wp != NULL && wp->w_localdir != NULL)
4627 rettv->vval.v_string = vim_strsave(wp->w_localdir);
4628 else if (wp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004629 {
Bram Moolenaar54591292018-02-09 20:53:59 +01004630 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004631 rettv->vval.v_string = vim_strsave(globaldir);
4632 else
4633 {
4634 cwd = alloc(MAXPATHL);
4635 if (cwd != NULL)
4636 {
4637 if (mch_dirname(cwd, MAXPATHL) != FAIL)
4638 rettv->vval.v_string = vim_strsave(cwd);
4639 vim_free(cwd);
4640 }
4641 }
4642#ifdef BACKSLASH_IN_FILENAME
4643 if (rettv->vval.v_string != NULL)
4644 slash_adjust(rettv->vval.v_string);
4645#endif
4646 }
4647}
4648
4649/*
4650 * "getfontname()" function
4651 */
4652 static void
4653f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4654{
4655 rettv->v_type = VAR_STRING;
4656 rettv->vval.v_string = NULL;
4657#ifdef FEAT_GUI
4658 if (gui.in_use)
4659 {
4660 GuiFont font;
4661 char_u *name = NULL;
4662
4663 if (argvars[0].v_type == VAR_UNKNOWN)
4664 {
4665 /* Get the "Normal" font. Either the name saved by
4666 * hl_set_font_name() or from the font ID. */
4667 font = gui.norm_font;
4668 name = hl_get_font_name();
4669 }
4670 else
4671 {
4672 name = get_tv_string(&argvars[0]);
4673 if (STRCMP(name, "*") == 0) /* don't use font dialog */
4674 return;
4675 font = gui_mch_get_font(name, FALSE);
4676 if (font == NOFONT)
4677 return; /* Invalid font name, return empty string. */
4678 }
4679 rettv->vval.v_string = gui_mch_get_fontname(font, name);
4680 if (argvars[0].v_type != VAR_UNKNOWN)
4681 gui_mch_free_font(font);
4682 }
4683#endif
4684}
4685
4686/*
4687 * "getfperm({fname})" function
4688 */
4689 static void
4690f_getfperm(typval_T *argvars, typval_T *rettv)
4691{
4692 char_u *fname;
4693 stat_T st;
4694 char_u *perm = NULL;
4695 char_u flags[] = "rwx";
4696 int i;
4697
4698 fname = get_tv_string(&argvars[0]);
4699
4700 rettv->v_type = VAR_STRING;
4701 if (mch_stat((char *)fname, &st) >= 0)
4702 {
4703 perm = vim_strsave((char_u *)"---------");
4704 if (perm != NULL)
4705 {
4706 for (i = 0; i < 9; i++)
4707 {
4708 if (st.st_mode & (1 << (8 - i)))
4709 perm[i] = flags[i % 3];
4710 }
4711 }
4712 }
4713 rettv->vval.v_string = perm;
4714}
4715
4716/*
4717 * "getfsize({fname})" function
4718 */
4719 static void
4720f_getfsize(typval_T *argvars, typval_T *rettv)
4721{
4722 char_u *fname;
4723 stat_T st;
4724
4725 fname = get_tv_string(&argvars[0]);
4726
4727 rettv->v_type = VAR_NUMBER;
4728
4729 if (mch_stat((char *)fname, &st) >= 0)
4730 {
4731 if (mch_isdir(fname))
4732 rettv->vval.v_number = 0;
4733 else
4734 {
4735 rettv->vval.v_number = (varnumber_T)st.st_size;
4736
4737 /* non-perfect check for overflow */
4738 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
4739 rettv->vval.v_number = -2;
4740 }
4741 }
4742 else
4743 rettv->vval.v_number = -1;
4744}
4745
4746/*
4747 * "getftime({fname})" function
4748 */
4749 static void
4750f_getftime(typval_T *argvars, typval_T *rettv)
4751{
4752 char_u *fname;
4753 stat_T st;
4754
4755 fname = get_tv_string(&argvars[0]);
4756
4757 if (mch_stat((char *)fname, &st) >= 0)
4758 rettv->vval.v_number = (varnumber_T)st.st_mtime;
4759 else
4760 rettv->vval.v_number = -1;
4761}
4762
4763/*
4764 * "getftype({fname})" function
4765 */
4766 static void
4767f_getftype(typval_T *argvars, typval_T *rettv)
4768{
4769 char_u *fname;
4770 stat_T st;
4771 char_u *type = NULL;
4772 char *t;
4773
4774 fname = get_tv_string(&argvars[0]);
4775
4776 rettv->v_type = VAR_STRING;
4777 if (mch_lstat((char *)fname, &st) >= 0)
4778 {
4779#ifdef S_ISREG
4780 if (S_ISREG(st.st_mode))
4781 t = "file";
4782 else if (S_ISDIR(st.st_mode))
4783 t = "dir";
4784# ifdef S_ISLNK
4785 else if (S_ISLNK(st.st_mode))
4786 t = "link";
4787# endif
4788# ifdef S_ISBLK
4789 else if (S_ISBLK(st.st_mode))
4790 t = "bdev";
4791# endif
4792# ifdef S_ISCHR
4793 else if (S_ISCHR(st.st_mode))
4794 t = "cdev";
4795# endif
4796# ifdef S_ISFIFO
4797 else if (S_ISFIFO(st.st_mode))
4798 t = "fifo";
4799# endif
4800# ifdef S_ISSOCK
4801 else if (S_ISSOCK(st.st_mode))
4802 t = "fifo";
4803# endif
4804 else
4805 t = "other";
4806#else
4807# ifdef S_IFMT
4808 switch (st.st_mode & S_IFMT)
4809 {
4810 case S_IFREG: t = "file"; break;
4811 case S_IFDIR: t = "dir"; break;
4812# ifdef S_IFLNK
4813 case S_IFLNK: t = "link"; break;
4814# endif
4815# ifdef S_IFBLK
4816 case S_IFBLK: t = "bdev"; break;
4817# endif
4818# ifdef S_IFCHR
4819 case S_IFCHR: t = "cdev"; break;
4820# endif
4821# ifdef S_IFIFO
4822 case S_IFIFO: t = "fifo"; break;
4823# endif
4824# ifdef S_IFSOCK
4825 case S_IFSOCK: t = "socket"; break;
4826# endif
4827 default: t = "other";
4828 }
4829# else
4830 if (mch_isdir(fname))
4831 t = "dir";
4832 else
4833 t = "file";
4834# endif
4835#endif
4836 type = vim_strsave((char_u *)t);
4837 }
4838 rettv->vval.v_string = type;
4839}
4840
4841/*
4842 * "getline(lnum, [end])" function
4843 */
4844 static void
4845f_getline(typval_T *argvars, typval_T *rettv)
4846{
4847 linenr_T lnum;
4848 linenr_T end;
4849 int retlist;
4850
4851 lnum = get_tv_lnum(argvars);
4852 if (argvars[1].v_type == VAR_UNKNOWN)
4853 {
4854 end = 0;
4855 retlist = FALSE;
4856 }
4857 else
4858 {
4859 end = get_tv_lnum(&argvars[1]);
4860 retlist = TRUE;
4861 }
4862
4863 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
4864}
4865
Bram Moolenaar4ae20952016-08-13 15:29:14 +02004866#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02004867 static void
4868get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
4869{
Bram Moolenaard823fa92016-08-12 16:29:27 +02004870 if (what_arg->v_type == VAR_UNKNOWN)
4871 {
4872 if (rettv_list_alloc(rettv) == OK)
4873 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004874 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004875 }
4876 else
4877 {
4878 if (rettv_dict_alloc(rettv) == OK)
4879 if (is_qf || (wp != NULL))
4880 {
4881 if (what_arg->v_type == VAR_DICT)
4882 {
4883 dict_T *d = what_arg->vval.v_dict;
4884
4885 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02004886 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004887 }
4888 else
4889 EMSG(_(e_dictreq));
4890 }
4891 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004892}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02004893#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02004894
4895/*
4896 * "getloclist()" function
4897 */
4898 static void
4899f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4900{
4901#ifdef FEAT_QUICKFIX
4902 win_T *wp;
4903
4904 wp = find_win_by_nr(&argvars[0], NULL);
4905 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
4906#endif
4907}
4908
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004909/*
4910 * "getmatches()" function
4911 */
4912 static void
4913f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4914{
4915#ifdef FEAT_SEARCH_EXTRA
4916 dict_T *dict;
4917 matchitem_T *cur = curwin->w_match_head;
4918 int i;
4919
4920 if (rettv_list_alloc(rettv) == OK)
4921 {
4922 while (cur != NULL)
4923 {
4924 dict = dict_alloc();
4925 if (dict == NULL)
4926 return;
4927 if (cur->match.regprog == NULL)
4928 {
4929 /* match added with matchaddpos() */
4930 for (i = 0; i < MAXPOSMATCH; ++i)
4931 {
4932 llpos_T *llpos;
4933 char buf[6];
4934 list_T *l;
4935
4936 llpos = &cur->pos.pos[i];
4937 if (llpos->lnum == 0)
4938 break;
4939 l = list_alloc();
4940 if (l == NULL)
4941 break;
4942 list_append_number(l, (varnumber_T)llpos->lnum);
4943 if (llpos->col > 0)
4944 {
4945 list_append_number(l, (varnumber_T)llpos->col);
4946 list_append_number(l, (varnumber_T)llpos->len);
4947 }
4948 sprintf(buf, "pos%d", i + 1);
4949 dict_add_list(dict, buf, l);
4950 }
4951 }
4952 else
4953 {
4954 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
4955 }
4956 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
4957 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
4958 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
4959# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
4960 if (cur->conceal_char)
4961 {
4962 char_u buf[MB_MAXBYTES + 1];
4963
4964 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
4965 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
4966 }
4967# endif
4968 list_append_dict(rettv->vval.v_list, dict);
4969 cur = cur->next;
4970 }
4971 }
4972#endif
4973}
4974
4975/*
4976 * "getpid()" function
4977 */
4978 static void
4979f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
4980{
4981 rettv->vval.v_number = mch_get_pid();
4982}
4983
4984 static void
4985getpos_both(
4986 typval_T *argvars,
4987 typval_T *rettv,
4988 int getcurpos)
4989{
4990 pos_T *fp;
4991 list_T *l;
4992 int fnum = -1;
4993
4994 if (rettv_list_alloc(rettv) == OK)
4995 {
4996 l = rettv->vval.v_list;
4997 if (getcurpos)
4998 fp = &curwin->w_cursor;
4999 else
5000 fp = var2fpos(&argvars[0], TRUE, &fnum);
5001 if (fnum != -1)
5002 list_append_number(l, (varnumber_T)fnum);
5003 else
5004 list_append_number(l, (varnumber_T)0);
5005 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5006 : (varnumber_T)0);
5007 list_append_number(l, (fp != NULL)
5008 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5009 : (varnumber_T)0);
5010 list_append_number(l,
5011#ifdef FEAT_VIRTUALEDIT
5012 (fp != NULL) ? (varnumber_T)fp->coladd :
5013#endif
5014 (varnumber_T)0);
5015 if (getcurpos)
5016 {
5017 update_curswant();
5018 list_append_number(l, curwin->w_curswant == MAXCOL ?
5019 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
5020 }
5021 }
5022 else
5023 rettv->vval.v_number = FALSE;
5024}
5025
5026
5027/*
5028 * "getcurpos()" function
5029 */
5030 static void
5031f_getcurpos(typval_T *argvars, typval_T *rettv)
5032{
5033 getpos_both(argvars, rettv, TRUE);
5034}
5035
5036/*
5037 * "getpos(string)" function
5038 */
5039 static void
5040f_getpos(typval_T *argvars, typval_T *rettv)
5041{
5042 getpos_both(argvars, rettv, FALSE);
5043}
5044
5045/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005046 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005047 */
5048 static void
5049f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5050{
5051#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005052 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005053#endif
5054}
5055
5056/*
5057 * "getreg()" function
5058 */
5059 static void
5060f_getreg(typval_T *argvars, typval_T *rettv)
5061{
5062 char_u *strregname;
5063 int regname;
5064 int arg2 = FALSE;
5065 int return_list = FALSE;
5066 int error = FALSE;
5067
5068 if (argvars[0].v_type != VAR_UNKNOWN)
5069 {
5070 strregname = get_tv_string_chk(&argvars[0]);
5071 error = strregname == NULL;
5072 if (argvars[1].v_type != VAR_UNKNOWN)
5073 {
5074 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
5075 if (!error && argvars[2].v_type != VAR_UNKNOWN)
5076 return_list = (int)get_tv_number_chk(&argvars[2], &error);
5077 }
5078 }
5079 else
5080 strregname = get_vim_var_str(VV_REG);
5081
5082 if (error)
5083 return;
5084
5085 regname = (strregname == NULL ? '"' : *strregname);
5086 if (regname == 0)
5087 regname = '"';
5088
5089 if (return_list)
5090 {
5091 rettv->v_type = VAR_LIST;
5092 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5093 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5094 if (rettv->vval.v_list == NULL)
5095 (void)rettv_list_alloc(rettv);
5096 else
5097 ++rettv->vval.v_list->lv_refcount;
5098 }
5099 else
5100 {
5101 rettv->v_type = VAR_STRING;
5102 rettv->vval.v_string = get_reg_contents(regname,
5103 arg2 ? GREG_EXPR_SRC : 0);
5104 }
5105}
5106
5107/*
5108 * "getregtype()" function
5109 */
5110 static void
5111f_getregtype(typval_T *argvars, typval_T *rettv)
5112{
5113 char_u *strregname;
5114 int regname;
5115 char_u buf[NUMBUFLEN + 2];
5116 long reglen = 0;
5117
5118 if (argvars[0].v_type != VAR_UNKNOWN)
5119 {
5120 strregname = get_tv_string_chk(&argvars[0]);
5121 if (strregname == NULL) /* type error; errmsg already given */
5122 {
5123 rettv->v_type = VAR_STRING;
5124 rettv->vval.v_string = NULL;
5125 return;
5126 }
5127 }
5128 else
5129 /* Default to v:register */
5130 strregname = get_vim_var_str(VV_REG);
5131
5132 regname = (strregname == NULL ? '"' : *strregname);
5133 if (regname == 0)
5134 regname = '"';
5135
5136 buf[0] = NUL;
5137 buf[1] = NUL;
5138 switch (get_reg_type(regname, &reglen))
5139 {
5140 case MLINE: buf[0] = 'V'; break;
5141 case MCHAR: buf[0] = 'v'; break;
5142 case MBLOCK:
5143 buf[0] = Ctrl_V;
5144 sprintf((char *)buf + 1, "%ld", reglen + 1);
5145 break;
5146 }
5147 rettv->v_type = VAR_STRING;
5148 rettv->vval.v_string = vim_strsave(buf);
5149}
5150
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005151/*
5152 * Returns information (variables, options, etc.) about a tab page
5153 * as a dictionary.
5154 */
5155 static dict_T *
5156get_tabpage_info(tabpage_T *tp, int tp_idx)
5157{
5158 win_T *wp;
5159 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005160 list_T *l;
5161
5162 dict = dict_alloc();
5163 if (dict == NULL)
5164 return NULL;
5165
Bram Moolenaar33928832016-08-18 21:22:04 +02005166 dict_add_nr_str(dict, "tabnr", tp_idx, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005167
5168 l = list_alloc();
5169 if (l != NULL)
5170 {
5171 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5172 wp; wp = wp->w_next)
5173 list_append_number(l, (varnumber_T)wp->w_id);
5174 dict_add_list(dict, "windows", l);
5175 }
5176
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005177 /* Make a reference to tabpage variables */
5178 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005179
5180 return dict;
5181}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005182
5183/*
5184 * "gettabinfo()" function
5185 */
5186 static void
5187f_gettabinfo(typval_T *argvars, typval_T *rettv)
5188{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005189 tabpage_T *tp, *tparg = NULL;
5190 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005191 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005192
5193 if (rettv_list_alloc(rettv) != OK)
5194 return;
5195
5196 if (argvars[0].v_type != VAR_UNKNOWN)
5197 {
5198 /* Information about one tab page */
5199 tparg = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5200 if (tparg == NULL)
5201 return;
5202 }
5203
5204 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005205 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005206 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005207 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005208 if (tparg != NULL && tp != tparg)
5209 continue;
5210 d = get_tabpage_info(tp, tpnr);
5211 if (d != NULL)
5212 list_append_dict(rettv->vval.v_list, d);
5213 if (tparg != NULL)
5214 return;
5215 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005216}
5217
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005218/*
5219 * "gettabvar()" function
5220 */
5221 static void
5222f_gettabvar(typval_T *argvars, typval_T *rettv)
5223{
5224 win_T *oldcurwin;
5225 tabpage_T *tp, *oldtabpage;
5226 dictitem_T *v;
5227 char_u *varname;
5228 int done = FALSE;
5229
5230 rettv->v_type = VAR_STRING;
5231 rettv->vval.v_string = NULL;
5232
5233 varname = get_tv_string_chk(&argvars[1]);
5234 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5235 if (tp != NULL && varname != NULL)
5236 {
5237 /* Set tp to be our tabpage, temporarily. Also set the window to the
5238 * first window in the tabpage, otherwise the window is not valid. */
5239 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005240 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5241 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005242 {
5243 /* look up the variable */
5244 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5245 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5246 if (v != NULL)
5247 {
5248 copy_tv(&v->di_tv, rettv);
5249 done = TRUE;
5250 }
5251 }
5252
5253 /* restore previous notion of curwin */
5254 restore_win(oldcurwin, oldtabpage, TRUE);
5255 }
5256
5257 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5258 copy_tv(&argvars[2], rettv);
5259}
5260
5261/*
5262 * "gettabwinvar()" function
5263 */
5264 static void
5265f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5266{
5267 getwinvar(argvars, rettv, 1);
5268}
5269
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005270/*
5271 * Returns information about a window as a dictionary.
5272 */
5273 static dict_T *
5274get_win_info(win_T *wp, short tpnr, short winnr)
5275{
5276 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005277
5278 dict = dict_alloc();
5279 if (dict == NULL)
5280 return NULL;
5281
Bram Moolenaar33928832016-08-18 21:22:04 +02005282 dict_add_nr_str(dict, "tabnr", tpnr, NULL);
5283 dict_add_nr_str(dict, "winnr", winnr, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005284 dict_add_nr_str(dict, "winid", wp->w_id, NULL);
5285 dict_add_nr_str(dict, "height", wp->w_height, NULL);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005286#ifdef FEAT_MENU
5287 dict_add_nr_str(dict, "winbar", wp->w_winbar_height, NULL);
5288#endif
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005289 dict_add_nr_str(dict, "width", wp->w_width, NULL);
Bram Moolenaar33928832016-08-18 21:22:04 +02005290 dict_add_nr_str(dict, "bufnr", wp->w_buffer->b_fnum, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005291
Bram Moolenaar69905d12017-08-13 18:14:47 +02005292#ifdef FEAT_TERMINAL
5293 dict_add_nr_str(dict, "terminal", bt_terminal(wp->w_buffer), NULL);
5294#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005295#ifdef FEAT_QUICKFIX
5296 dict_add_nr_str(dict, "quickfix", bt_quickfix(wp->w_buffer), NULL);
5297 dict_add_nr_str(dict, "loclist",
5298 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL), NULL);
5299#endif
5300
Bram Moolenaar30567352016-08-27 21:25:44 +02005301 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005302 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005303
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005304 return dict;
5305}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005306
5307/*
5308 * "getwininfo()" function
5309 */
5310 static void
5311f_getwininfo(typval_T *argvars, typval_T *rettv)
5312{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005313 tabpage_T *tp;
5314 win_T *wp = NULL, *wparg = NULL;
5315 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005316 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005317
5318 if (rettv_list_alloc(rettv) != OK)
5319 return;
5320
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005321 if (argvars[0].v_type != VAR_UNKNOWN)
5322 {
5323 wparg = win_id2wp(argvars);
5324 if (wparg == NULL)
5325 return;
5326 }
5327
5328 /* Collect information about either all the windows across all the tab
5329 * pages or one particular window.
5330 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005331 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005332 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005333 tabnr++;
5334 winnr = 0;
5335 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005336 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005337 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005338 if (wparg != NULL && wp != wparg)
5339 continue;
5340 d = get_win_info(wp, tabnr, winnr);
5341 if (d != NULL)
5342 list_append_dict(rettv->vval.v_list, d);
5343 if (wparg != NULL)
5344 /* found information about a specific window */
5345 return;
5346 }
5347 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005348}
5349
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005350/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005351 * "win_findbuf()" function
5352 */
5353 static void
5354f_win_findbuf(typval_T *argvars, typval_T *rettv)
5355{
5356 if (rettv_list_alloc(rettv) != FAIL)
5357 win_findbuf(argvars, rettv->vval.v_list);
5358}
5359
5360/*
5361 * "win_getid()" function
5362 */
5363 static void
5364f_win_getid(typval_T *argvars, typval_T *rettv)
5365{
5366 rettv->vval.v_number = win_getid(argvars);
5367}
5368
5369/*
5370 * "win_gotoid()" function
5371 */
5372 static void
5373f_win_gotoid(typval_T *argvars, typval_T *rettv)
5374{
5375 rettv->vval.v_number = win_gotoid(argvars);
5376}
5377
5378/*
5379 * "win_id2tabwin()" function
5380 */
5381 static void
5382f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5383{
5384 if (rettv_list_alloc(rettv) != FAIL)
5385 win_id2tabwin(argvars, rettv->vval.v_list);
5386}
5387
5388/*
5389 * "win_id2win()" function
5390 */
5391 static void
5392f_win_id2win(typval_T *argvars, typval_T *rettv)
5393{
5394 rettv->vval.v_number = win_id2win(argvars);
5395}
5396
5397/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005398 * "win_screenpos()" function
5399 */
5400 static void
5401f_win_screenpos(typval_T *argvars, typval_T *rettv)
5402{
5403 win_T *wp;
5404
5405 if (rettv_list_alloc(rettv) == FAIL)
5406 return;
5407
5408 wp = find_win_by_nr(&argvars[0], NULL);
5409 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5410 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5411}
5412
5413/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005414 * "getwinposx()" function
5415 */
5416 static void
5417f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5418{
5419 rettv->vval.v_number = -1;
5420#ifdef FEAT_GUI
5421 if (gui.in_use)
5422 {
5423 int x, y;
5424
5425 if (gui_mch_get_winpos(&x, &y) == OK)
5426 rettv->vval.v_number = x;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005427 return;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005428 }
5429#endif
5430#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5431 {
5432 int x, y;
5433
5434 if (term_get_winpos(&x, &y) == OK)
5435 rettv->vval.v_number = x;
5436 }
5437#endif
5438}
5439
5440/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005441 * "getwinposy()" function
5442 */
5443 static void
5444f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5445{
5446 rettv->vval.v_number = -1;
5447#ifdef FEAT_GUI
5448 if (gui.in_use)
5449 {
5450 int x, y;
5451
5452 if (gui_mch_get_winpos(&x, &y) == OK)
5453 rettv->vval.v_number = y;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005454 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005455 }
5456#endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005457#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5458 {
5459 int x, y;
5460
5461 if (term_get_winpos(&x, &y) == OK)
5462 rettv->vval.v_number = y;
5463 }
5464#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005465}
5466
5467/*
5468 * "getwinvar()" function
5469 */
5470 static void
5471f_getwinvar(typval_T *argvars, typval_T *rettv)
5472{
5473 getwinvar(argvars, rettv, 0);
5474}
5475
5476/*
5477 * "glob()" function
5478 */
5479 static void
5480f_glob(typval_T *argvars, typval_T *rettv)
5481{
5482 int options = WILD_SILENT|WILD_USE_NL;
5483 expand_T xpc;
5484 int error = FALSE;
5485
5486 /* When the optional second argument is non-zero, don't remove matches
5487 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5488 rettv->v_type = VAR_STRING;
5489 if (argvars[1].v_type != VAR_UNKNOWN)
5490 {
5491 if (get_tv_number_chk(&argvars[1], &error))
5492 options |= WILD_KEEP_ALL;
5493 if (argvars[2].v_type != VAR_UNKNOWN)
5494 {
5495 if (get_tv_number_chk(&argvars[2], &error))
5496 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005497 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005498 }
5499 if (argvars[3].v_type != VAR_UNKNOWN
5500 && get_tv_number_chk(&argvars[3], &error))
5501 options |= WILD_ALLLINKS;
5502 }
5503 }
5504 if (!error)
5505 {
5506 ExpandInit(&xpc);
5507 xpc.xp_context = EXPAND_FILES;
5508 if (p_wic)
5509 options += WILD_ICASE;
5510 if (rettv->v_type == VAR_STRING)
5511 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
5512 NULL, options, WILD_ALL);
5513 else if (rettv_list_alloc(rettv) != FAIL)
5514 {
5515 int i;
5516
5517 ExpandOne(&xpc, get_tv_string(&argvars[0]),
5518 NULL, options, WILD_ALL_KEEP);
5519 for (i = 0; i < xpc.xp_numfiles; i++)
5520 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5521
5522 ExpandCleanup(&xpc);
5523 }
5524 }
5525 else
5526 rettv->vval.v_string = NULL;
5527}
5528
5529/*
5530 * "globpath()" function
5531 */
5532 static void
5533f_globpath(typval_T *argvars, typval_T *rettv)
5534{
5535 int flags = 0;
5536 char_u buf1[NUMBUFLEN];
5537 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
5538 int error = FALSE;
5539 garray_T ga;
5540 int i;
5541
5542 /* When the optional second argument is non-zero, don't remove matches
5543 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5544 rettv->v_type = VAR_STRING;
5545 if (argvars[2].v_type != VAR_UNKNOWN)
5546 {
5547 if (get_tv_number_chk(&argvars[2], &error))
5548 flags |= WILD_KEEP_ALL;
5549 if (argvars[3].v_type != VAR_UNKNOWN)
5550 {
5551 if (get_tv_number_chk(&argvars[3], &error))
5552 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005553 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005554 }
5555 if (argvars[4].v_type != VAR_UNKNOWN
5556 && get_tv_number_chk(&argvars[4], &error))
5557 flags |= WILD_ALLLINKS;
5558 }
5559 }
5560 if (file != NULL && !error)
5561 {
5562 ga_init2(&ga, (int)sizeof(char_u *), 10);
5563 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
5564 if (rettv->v_type == VAR_STRING)
5565 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
5566 else if (rettv_list_alloc(rettv) != FAIL)
5567 for (i = 0; i < ga.ga_len; ++i)
5568 list_append_string(rettv->vval.v_list,
5569 ((char_u **)(ga.ga_data))[i], -1);
5570 ga_clear_strings(&ga);
5571 }
5572 else
5573 rettv->vval.v_string = NULL;
5574}
5575
5576/*
5577 * "glob2regpat()" function
5578 */
5579 static void
5580f_glob2regpat(typval_T *argvars, typval_T *rettv)
5581{
5582 char_u *pat = get_tv_string_chk(&argvars[0]);
5583
5584 rettv->v_type = VAR_STRING;
5585 rettv->vval.v_string = (pat == NULL)
5586 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
5587}
5588
5589/* for VIM_VERSION_ defines */
5590#include "version.h"
5591
5592/*
5593 * "has()" function
5594 */
5595 static void
5596f_has(typval_T *argvars, typval_T *rettv)
5597{
5598 int i;
5599 char_u *name;
5600 int n = FALSE;
5601 static char *(has_list[]) =
5602 {
5603#ifdef AMIGA
5604 "amiga",
5605# ifdef FEAT_ARP
5606 "arp",
5607# endif
5608#endif
5609#ifdef __BEOS__
5610 "beos",
5611#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02005612#ifdef MACOS_X
5613 "mac", /* Mac OS X (and, once, Mac OS Classic) */
5614 "osx", /* Mac OS X */
5615# ifdef MACOS_X_DARWIN
5616 "macunix", /* Mac OS X, with the darwin feature */
5617 "osxdarwin", /* synonym for macunix */
5618# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005619#endif
5620#ifdef __QNX__
5621 "qnx",
5622#endif
5623#ifdef UNIX
5624 "unix",
5625#endif
5626#ifdef VMS
5627 "vms",
5628#endif
5629#ifdef WIN32
5630 "win32",
5631#endif
5632#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
5633 "win32unix",
5634#endif
5635#if defined(WIN64) || defined(_WIN64)
5636 "win64",
5637#endif
5638#ifdef EBCDIC
5639 "ebcdic",
5640#endif
5641#ifndef CASE_INSENSITIVE_FILENAME
5642 "fname_case",
5643#endif
5644#ifdef HAVE_ACL
5645 "acl",
5646#endif
5647#ifdef FEAT_ARABIC
5648 "arabic",
5649#endif
5650#ifdef FEAT_AUTOCMD
5651 "autocmd",
5652#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01005653#ifdef FEAT_AUTOSERVERNAME
5654 "autoservername",
5655#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01005656#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005657 "balloon_eval",
5658# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
5659 "balloon_multiline",
5660# endif
5661#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01005662#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01005663 "balloon_eval_term",
5664#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005665#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
5666 "builtin_terms",
5667# ifdef ALL_BUILTIN_TCAPS
5668 "all_builtin_terms",
5669# endif
5670#endif
5671#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
5672 || defined(FEAT_GUI_W32) \
5673 || defined(FEAT_GUI_MOTIF))
5674 "browsefilter",
5675#endif
5676#ifdef FEAT_BYTEOFF
5677 "byte_offset",
5678#endif
5679#ifdef FEAT_JOB_CHANNEL
5680 "channel",
5681#endif
5682#ifdef FEAT_CINDENT
5683 "cindent",
5684#endif
5685#ifdef FEAT_CLIENTSERVER
5686 "clientserver",
5687#endif
5688#ifdef FEAT_CLIPBOARD
5689 "clipboard",
5690#endif
5691#ifdef FEAT_CMDL_COMPL
5692 "cmdline_compl",
5693#endif
5694#ifdef FEAT_CMDHIST
5695 "cmdline_hist",
5696#endif
5697#ifdef FEAT_COMMENTS
5698 "comments",
5699#endif
5700#ifdef FEAT_CONCEAL
5701 "conceal",
5702#endif
5703#ifdef FEAT_CRYPT
5704 "cryptv",
5705 "crypt-blowfish",
5706 "crypt-blowfish2",
5707#endif
5708#ifdef FEAT_CSCOPE
5709 "cscope",
5710#endif
5711#ifdef FEAT_CURSORBIND
5712 "cursorbind",
5713#endif
5714#ifdef CURSOR_SHAPE
5715 "cursorshape",
5716#endif
5717#ifdef DEBUG
5718 "debug",
5719#endif
5720#ifdef FEAT_CON_DIALOG
5721 "dialog_con",
5722#endif
5723#ifdef FEAT_GUI_DIALOG
5724 "dialog_gui",
5725#endif
5726#ifdef FEAT_DIFF
5727 "diff",
5728#endif
5729#ifdef FEAT_DIGRAPHS
5730 "digraphs",
5731#endif
5732#ifdef FEAT_DIRECTX
5733 "directx",
5734#endif
5735#ifdef FEAT_DND
5736 "dnd",
5737#endif
5738#ifdef FEAT_EMACS_TAGS
5739 "emacs_tags",
5740#endif
5741 "eval", /* always present, of course! */
5742 "ex_extra", /* graduated feature */
5743#ifdef FEAT_SEARCH_EXTRA
5744 "extra_search",
5745#endif
5746#ifdef FEAT_FKMAP
5747 "farsi",
5748#endif
5749#ifdef FEAT_SEARCHPATH
5750 "file_in_path",
5751#endif
5752#ifdef FEAT_FILTERPIPE
5753 "filterpipe",
5754#endif
5755#ifdef FEAT_FIND_ID
5756 "find_in_path",
5757#endif
5758#ifdef FEAT_FLOAT
5759 "float",
5760#endif
5761#ifdef FEAT_FOLDING
5762 "folding",
5763#endif
5764#ifdef FEAT_FOOTER
5765 "footer",
5766#endif
5767#if !defined(USE_SYSTEM) && defined(UNIX)
5768 "fork",
5769#endif
5770#ifdef FEAT_GETTEXT
5771 "gettext",
5772#endif
5773#ifdef FEAT_GUI
5774 "gui",
5775#endif
5776#ifdef FEAT_GUI_ATHENA
5777# ifdef FEAT_GUI_NEXTAW
5778 "gui_neXtaw",
5779# else
5780 "gui_athena",
5781# endif
5782#endif
5783#ifdef FEAT_GUI_GTK
5784 "gui_gtk",
5785# ifdef USE_GTK3
5786 "gui_gtk3",
5787# else
5788 "gui_gtk2",
5789# endif
5790#endif
5791#ifdef FEAT_GUI_GNOME
5792 "gui_gnome",
5793#endif
5794#ifdef FEAT_GUI_MAC
5795 "gui_mac",
5796#endif
5797#ifdef FEAT_GUI_MOTIF
5798 "gui_motif",
5799#endif
5800#ifdef FEAT_GUI_PHOTON
5801 "gui_photon",
5802#endif
5803#ifdef FEAT_GUI_W32
5804 "gui_win32",
5805#endif
5806#ifdef FEAT_HANGULIN
5807 "hangul_input",
5808#endif
5809#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
5810 "iconv",
5811#endif
5812#ifdef FEAT_INS_EXPAND
5813 "insert_expand",
5814#endif
5815#ifdef FEAT_JOB_CHANNEL
5816 "job",
5817#endif
5818#ifdef FEAT_JUMPLIST
5819 "jumplist",
5820#endif
5821#ifdef FEAT_KEYMAP
5822 "keymap",
5823#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02005824 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005825#ifdef FEAT_LANGMAP
5826 "langmap",
5827#endif
5828#ifdef FEAT_LIBCALL
5829 "libcall",
5830#endif
5831#ifdef FEAT_LINEBREAK
5832 "linebreak",
5833#endif
5834#ifdef FEAT_LISP
5835 "lispindent",
5836#endif
5837#ifdef FEAT_LISTCMDS
5838 "listcmds",
5839#endif
5840#ifdef FEAT_LOCALMAP
5841 "localmap",
5842#endif
5843#ifdef FEAT_LUA
5844# ifndef DYNAMIC_LUA
5845 "lua",
5846# endif
5847#endif
5848#ifdef FEAT_MENU
5849 "menu",
5850#endif
5851#ifdef FEAT_SESSION
5852 "mksession",
5853#endif
5854#ifdef FEAT_MODIFY_FNAME
5855 "modify_fname",
5856#endif
5857#ifdef FEAT_MOUSE
5858 "mouse",
5859#endif
5860#ifdef FEAT_MOUSESHAPE
5861 "mouseshape",
5862#endif
5863#if defined(UNIX) || defined(VMS)
5864# ifdef FEAT_MOUSE_DEC
5865 "mouse_dec",
5866# endif
5867# ifdef FEAT_MOUSE_GPM
5868 "mouse_gpm",
5869# endif
5870# ifdef FEAT_MOUSE_JSB
5871 "mouse_jsbterm",
5872# endif
5873# ifdef FEAT_MOUSE_NET
5874 "mouse_netterm",
5875# endif
5876# ifdef FEAT_MOUSE_PTERM
5877 "mouse_pterm",
5878# endif
5879# ifdef FEAT_MOUSE_SGR
5880 "mouse_sgr",
5881# endif
5882# ifdef FEAT_SYSMOUSE
5883 "mouse_sysmouse",
5884# endif
5885# ifdef FEAT_MOUSE_URXVT
5886 "mouse_urxvt",
5887# endif
5888# ifdef FEAT_MOUSE_XTERM
5889 "mouse_xterm",
5890# endif
5891#endif
5892#ifdef FEAT_MBYTE
5893 "multi_byte",
5894#endif
5895#ifdef FEAT_MBYTE_IME
5896 "multi_byte_ime",
5897#endif
5898#ifdef FEAT_MULTI_LANG
5899 "multi_lang",
5900#endif
5901#ifdef FEAT_MZSCHEME
5902#ifndef DYNAMIC_MZSCHEME
5903 "mzscheme",
5904#endif
5905#endif
5906#ifdef FEAT_NUM64
5907 "num64",
5908#endif
5909#ifdef FEAT_OLE
5910 "ole",
5911#endif
5912 "packages",
5913#ifdef FEAT_PATH_EXTRA
5914 "path_extra",
5915#endif
5916#ifdef FEAT_PERL
5917#ifndef DYNAMIC_PERL
5918 "perl",
5919#endif
5920#endif
5921#ifdef FEAT_PERSISTENT_UNDO
5922 "persistent_undo",
5923#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01005924#if defined(FEAT_PYTHON)
5925 "python_compiled",
5926# if defined(DYNAMIC_PYTHON)
5927 "python_dynamic",
5928# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005929 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005930 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01005931# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005932#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01005933#if defined(FEAT_PYTHON3)
5934 "python3_compiled",
5935# if defined(DYNAMIC_PYTHON3)
5936 "python3_dynamic",
5937# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005938 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005939 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01005940# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005941#endif
5942#ifdef FEAT_POSTSCRIPT
5943 "postscript",
5944#endif
5945#ifdef FEAT_PRINTER
5946 "printer",
5947#endif
5948#ifdef FEAT_PROFILE
5949 "profile",
5950#endif
5951#ifdef FEAT_RELTIME
5952 "reltime",
5953#endif
5954#ifdef FEAT_QUICKFIX
5955 "quickfix",
5956#endif
5957#ifdef FEAT_RIGHTLEFT
5958 "rightleft",
5959#endif
5960#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
5961 "ruby",
5962#endif
5963#ifdef FEAT_SCROLLBIND
5964 "scrollbind",
5965#endif
5966#ifdef FEAT_CMDL_INFO
5967 "showcmd",
5968 "cmdline_info",
5969#endif
5970#ifdef FEAT_SIGNS
5971 "signs",
5972#endif
5973#ifdef FEAT_SMARTINDENT
5974 "smartindent",
5975#endif
5976#ifdef STARTUPTIME
5977 "startuptime",
5978#endif
5979#ifdef FEAT_STL_OPT
5980 "statusline",
5981#endif
5982#ifdef FEAT_SUN_WORKSHOP
5983 "sun_workshop",
5984#endif
5985#ifdef FEAT_NETBEANS_INTG
5986 "netbeans_intg",
5987#endif
5988#ifdef FEAT_SPELL
5989 "spell",
5990#endif
5991#ifdef FEAT_SYN_HL
5992 "syntax",
5993#endif
5994#if defined(USE_SYSTEM) || !defined(UNIX)
5995 "system",
5996#endif
5997#ifdef FEAT_TAG_BINS
5998 "tag_binary",
5999#endif
6000#ifdef FEAT_TAG_OLDSTATIC
6001 "tag_old_static",
6002#endif
6003#ifdef FEAT_TAG_ANYWHITE
6004 "tag_any_white",
6005#endif
6006#ifdef FEAT_TCL
6007# ifndef DYNAMIC_TCL
6008 "tcl",
6009# endif
6010#endif
6011#ifdef FEAT_TERMGUICOLORS
6012 "termguicolors",
6013#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006014#if defined(FEAT_TERMINAL) && !defined(WIN3264)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006015 "terminal",
6016#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006017#ifdef TERMINFO
6018 "terminfo",
6019#endif
6020#ifdef FEAT_TERMRESPONSE
6021 "termresponse",
6022#endif
6023#ifdef FEAT_TEXTOBJ
6024 "textobjects",
6025#endif
6026#ifdef HAVE_TGETENT
6027 "tgetent",
6028#endif
6029#ifdef FEAT_TIMERS
6030 "timers",
6031#endif
6032#ifdef FEAT_TITLE
6033 "title",
6034#endif
6035#ifdef FEAT_TOOLBAR
6036 "toolbar",
6037#endif
6038#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6039 "unnamedplus",
6040#endif
6041#ifdef FEAT_USR_CMDS
6042 "user-commands", /* was accidentally included in 5.4 */
6043 "user_commands",
6044#endif
6045#ifdef FEAT_VIMINFO
6046 "viminfo",
6047#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006048 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006049#ifdef FEAT_VIRTUALEDIT
6050 "virtualedit",
6051#endif
6052 "visual",
6053#ifdef FEAT_VISUALEXTRA
6054 "visualextra",
6055#endif
6056#ifdef FEAT_VREPLACE
6057 "vreplace",
6058#endif
6059#ifdef FEAT_WILDIGN
6060 "wildignore",
6061#endif
6062#ifdef FEAT_WILDMENU
6063 "wildmenu",
6064#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006065 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006066#ifdef FEAT_WAK
6067 "winaltkeys",
6068#endif
6069#ifdef FEAT_WRITEBACKUP
6070 "writebackup",
6071#endif
6072#ifdef FEAT_XIM
6073 "xim",
6074#endif
6075#ifdef FEAT_XFONTSET
6076 "xfontset",
6077#endif
6078#ifdef FEAT_XPM_W32
6079 "xpm",
6080 "xpm_w32", /* for backward compatibility */
6081#else
6082# if defined(HAVE_XPM)
6083 "xpm",
6084# endif
6085#endif
6086#ifdef USE_XSMP
6087 "xsmp",
6088#endif
6089#ifdef USE_XSMP_INTERACT
6090 "xsmp_interact",
6091#endif
6092#ifdef FEAT_XCLIPBOARD
6093 "xterm_clipboard",
6094#endif
6095#ifdef FEAT_XTERM_SAVE
6096 "xterm_save",
6097#endif
6098#if defined(UNIX) && defined(FEAT_X11)
6099 "X11",
6100#endif
6101 NULL
6102 };
6103
6104 name = get_tv_string(&argvars[0]);
6105 for (i = 0; has_list[i] != NULL; ++i)
6106 if (STRICMP(name, has_list[i]) == 0)
6107 {
6108 n = TRUE;
6109 break;
6110 }
6111
6112 if (n == FALSE)
6113 {
6114 if (STRNICMP(name, "patch", 5) == 0)
6115 {
6116 if (name[5] == '-'
6117 && STRLEN(name) >= 11
6118 && vim_isdigit(name[6])
6119 && vim_isdigit(name[8])
6120 && vim_isdigit(name[10]))
6121 {
6122 int major = atoi((char *)name + 6);
6123 int minor = atoi((char *)name + 8);
6124
6125 /* Expect "patch-9.9.01234". */
6126 n = (major < VIM_VERSION_MAJOR
6127 || (major == VIM_VERSION_MAJOR
6128 && (minor < VIM_VERSION_MINOR
6129 || (minor == VIM_VERSION_MINOR
6130 && has_patch(atoi((char *)name + 10))))));
6131 }
6132 else
6133 n = has_patch(atoi((char *)name + 5));
6134 }
6135 else if (STRICMP(name, "vim_starting") == 0)
6136 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006137 else if (STRICMP(name, "ttyin") == 0)
6138 n = mch_input_isatty();
6139 else if (STRICMP(name, "ttyout") == 0)
6140 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006141#ifdef FEAT_MBYTE
6142 else if (STRICMP(name, "multi_byte_encoding") == 0)
6143 n = has_mbyte;
6144#endif
6145#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
6146 else if (STRICMP(name, "balloon_multiline") == 0)
6147 n = multiline_balloon_available();
6148#endif
6149#ifdef DYNAMIC_TCL
6150 else if (STRICMP(name, "tcl") == 0)
6151 n = tcl_enabled(FALSE);
6152#endif
6153#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6154 else if (STRICMP(name, "iconv") == 0)
6155 n = iconv_enabled(FALSE);
6156#endif
6157#ifdef DYNAMIC_LUA
6158 else if (STRICMP(name, "lua") == 0)
6159 n = lua_enabled(FALSE);
6160#endif
6161#ifdef DYNAMIC_MZSCHEME
6162 else if (STRICMP(name, "mzscheme") == 0)
6163 n = mzscheme_enabled(FALSE);
6164#endif
6165#ifdef DYNAMIC_RUBY
6166 else if (STRICMP(name, "ruby") == 0)
6167 n = ruby_enabled(FALSE);
6168#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006169#ifdef DYNAMIC_PYTHON
6170 else if (STRICMP(name, "python") == 0)
6171 n = python_enabled(FALSE);
6172#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006173#ifdef DYNAMIC_PYTHON3
6174 else if (STRICMP(name, "python3") == 0)
6175 n = python3_enabled(FALSE);
6176#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006177#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6178 else if (STRICMP(name, "pythonx") == 0)
6179 {
6180# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6181 if (p_pyx == 0)
6182 n = python3_enabled(FALSE) || python_enabled(FALSE);
6183 else if (p_pyx == 3)
6184 n = python3_enabled(FALSE);
6185 else if (p_pyx == 2)
6186 n = python_enabled(FALSE);
6187# elif defined(DYNAMIC_PYTHON)
6188 n = python_enabled(FALSE);
6189# elif defined(DYNAMIC_PYTHON3)
6190 n = python3_enabled(FALSE);
6191# endif
6192 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006193#endif
6194#ifdef DYNAMIC_PERL
6195 else if (STRICMP(name, "perl") == 0)
6196 n = perl_enabled(FALSE);
6197#endif
6198#ifdef FEAT_GUI
6199 else if (STRICMP(name, "gui_running") == 0)
6200 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006201# ifdef FEAT_BROWSE
6202 else if (STRICMP(name, "browse") == 0)
6203 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6204# endif
6205#endif
6206#ifdef FEAT_SYN_HL
6207 else if (STRICMP(name, "syntax_items") == 0)
6208 n = syntax_present(curwin);
6209#endif
6210#if defined(WIN3264)
6211 else if (STRICMP(name, "win95") == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006212 n = FALSE; /* Win9x is no more supported. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006213#endif
6214#ifdef FEAT_NETBEANS_INTG
6215 else if (STRICMP(name, "netbeans_enabled") == 0)
6216 n = netbeans_active();
6217#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006218#if defined(FEAT_TERMINAL) && defined(WIN3264)
6219 else if (STRICMP(name, "terminal") == 0)
6220 n = terminal_enabled();
6221#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006222 }
6223
6224 rettv->vval.v_number = n;
6225}
6226
6227/*
6228 * "has_key()" function
6229 */
6230 static void
6231f_has_key(typval_T *argvars, typval_T *rettv)
6232{
6233 if (argvars[0].v_type != VAR_DICT)
6234 {
6235 EMSG(_(e_dictreq));
6236 return;
6237 }
6238 if (argvars[0].vval.v_dict == NULL)
6239 return;
6240
6241 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
6242 get_tv_string(&argvars[1]), -1) != NULL;
6243}
6244
6245/*
6246 * "haslocaldir()" function
6247 */
6248 static void
6249f_haslocaldir(typval_T *argvars, typval_T *rettv)
6250{
6251 win_T *wp = NULL;
6252
6253 wp = find_tabwin(&argvars[0], &argvars[1]);
6254 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6255}
6256
6257/*
6258 * "hasmapto()" function
6259 */
6260 static void
6261f_hasmapto(typval_T *argvars, typval_T *rettv)
6262{
6263 char_u *name;
6264 char_u *mode;
6265 char_u buf[NUMBUFLEN];
6266 int abbr = FALSE;
6267
6268 name = get_tv_string(&argvars[0]);
6269 if (argvars[1].v_type == VAR_UNKNOWN)
6270 mode = (char_u *)"nvo";
6271 else
6272 {
6273 mode = get_tv_string_buf(&argvars[1], buf);
6274 if (argvars[2].v_type != VAR_UNKNOWN)
6275 abbr = (int)get_tv_number(&argvars[2]);
6276 }
6277
6278 if (map_to_exists(name, mode, abbr))
6279 rettv->vval.v_number = TRUE;
6280 else
6281 rettv->vval.v_number = FALSE;
6282}
6283
6284/*
6285 * "histadd()" function
6286 */
6287 static void
6288f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6289{
6290#ifdef FEAT_CMDHIST
6291 int histype;
6292 char_u *str;
6293 char_u buf[NUMBUFLEN];
6294#endif
6295
6296 rettv->vval.v_number = FALSE;
6297 if (check_restricted() || check_secure())
6298 return;
6299#ifdef FEAT_CMDHIST
6300 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6301 histype = str != NULL ? get_histtype(str) : -1;
6302 if (histype >= 0)
6303 {
6304 str = get_tv_string_buf(&argvars[1], buf);
6305 if (*str != NUL)
6306 {
6307 init_history();
6308 add_to_history(histype, str, FALSE, NUL);
6309 rettv->vval.v_number = TRUE;
6310 return;
6311 }
6312 }
6313#endif
6314}
6315
6316/*
6317 * "histdel()" function
6318 */
6319 static void
6320f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6321{
6322#ifdef FEAT_CMDHIST
6323 int n;
6324 char_u buf[NUMBUFLEN];
6325 char_u *str;
6326
6327 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6328 if (str == NULL)
6329 n = 0;
6330 else if (argvars[1].v_type == VAR_UNKNOWN)
6331 /* only one argument: clear entire history */
6332 n = clr_history(get_histtype(str));
6333 else if (argvars[1].v_type == VAR_NUMBER)
6334 /* index given: remove that entry */
6335 n = del_history_idx(get_histtype(str),
6336 (int)get_tv_number(&argvars[1]));
6337 else
6338 /* string given: remove all matching entries */
6339 n = del_history_entry(get_histtype(str),
6340 get_tv_string_buf(&argvars[1], buf));
6341 rettv->vval.v_number = n;
6342#endif
6343}
6344
6345/*
6346 * "histget()" function
6347 */
6348 static void
6349f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6350{
6351#ifdef FEAT_CMDHIST
6352 int type;
6353 int idx;
6354 char_u *str;
6355
6356 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6357 if (str == NULL)
6358 rettv->vval.v_string = NULL;
6359 else
6360 {
6361 type = get_histtype(str);
6362 if (argvars[1].v_type == VAR_UNKNOWN)
6363 idx = get_history_idx(type);
6364 else
6365 idx = (int)get_tv_number_chk(&argvars[1], NULL);
6366 /* -1 on type error */
6367 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6368 }
6369#else
6370 rettv->vval.v_string = NULL;
6371#endif
6372 rettv->v_type = VAR_STRING;
6373}
6374
6375/*
6376 * "histnr()" function
6377 */
6378 static void
6379f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6380{
6381 int i;
6382
6383#ifdef FEAT_CMDHIST
6384 char_u *history = get_tv_string_chk(&argvars[0]);
6385
6386 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6387 if (i >= HIST_CMD && i < HIST_COUNT)
6388 i = get_history_idx(i);
6389 else
6390#endif
6391 i = -1;
6392 rettv->vval.v_number = i;
6393}
6394
6395/*
6396 * "highlightID(name)" function
6397 */
6398 static void
6399f_hlID(typval_T *argvars, typval_T *rettv)
6400{
6401 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
6402}
6403
6404/*
6405 * "highlight_exists()" function
6406 */
6407 static void
6408f_hlexists(typval_T *argvars, typval_T *rettv)
6409{
6410 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
6411}
6412
6413/*
6414 * "hostname()" function
6415 */
6416 static void
6417f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6418{
6419 char_u hostname[256];
6420
6421 mch_get_host_name(hostname, 256);
6422 rettv->v_type = VAR_STRING;
6423 rettv->vval.v_string = vim_strsave(hostname);
6424}
6425
6426/*
6427 * iconv() function
6428 */
6429 static void
6430f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6431{
6432#ifdef FEAT_MBYTE
6433 char_u buf1[NUMBUFLEN];
6434 char_u buf2[NUMBUFLEN];
6435 char_u *from, *to, *str;
6436 vimconv_T vimconv;
6437#endif
6438
6439 rettv->v_type = VAR_STRING;
6440 rettv->vval.v_string = NULL;
6441
6442#ifdef FEAT_MBYTE
6443 str = get_tv_string(&argvars[0]);
6444 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
6445 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
6446 vimconv.vc_type = CONV_NONE;
6447 convert_setup(&vimconv, from, to);
6448
6449 /* If the encodings are equal, no conversion needed. */
6450 if (vimconv.vc_type == CONV_NONE)
6451 rettv->vval.v_string = vim_strsave(str);
6452 else
6453 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6454
6455 convert_setup(&vimconv, NULL, NULL);
6456 vim_free(from);
6457 vim_free(to);
6458#endif
6459}
6460
6461/*
6462 * "indent()" function
6463 */
6464 static void
6465f_indent(typval_T *argvars, typval_T *rettv)
6466{
6467 linenr_T lnum;
6468
6469 lnum = get_tv_lnum(argvars);
6470 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6471 rettv->vval.v_number = get_indent_lnum(lnum);
6472 else
6473 rettv->vval.v_number = -1;
6474}
6475
6476/*
6477 * "index()" function
6478 */
6479 static void
6480f_index(typval_T *argvars, typval_T *rettv)
6481{
6482 list_T *l;
6483 listitem_T *item;
6484 long idx = 0;
6485 int ic = FALSE;
6486
6487 rettv->vval.v_number = -1;
6488 if (argvars[0].v_type != VAR_LIST)
6489 {
6490 EMSG(_(e_listreq));
6491 return;
6492 }
6493 l = argvars[0].vval.v_list;
6494 if (l != NULL)
6495 {
6496 item = l->lv_first;
6497 if (argvars[2].v_type != VAR_UNKNOWN)
6498 {
6499 int error = FALSE;
6500
6501 /* Start at specified item. Use the cached index that list_find()
6502 * sets, so that a negative number also works. */
6503 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
6504 idx = l->lv_idx;
6505 if (argvars[3].v_type != VAR_UNKNOWN)
6506 ic = (int)get_tv_number_chk(&argvars[3], &error);
6507 if (error)
6508 item = NULL;
6509 }
6510
6511 for ( ; item != NULL; item = item->li_next, ++idx)
6512 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6513 {
6514 rettv->vval.v_number = idx;
6515 break;
6516 }
6517 }
6518}
6519
6520static int inputsecret_flag = 0;
6521
6522/*
6523 * "input()" function
6524 * Also handles inputsecret() when inputsecret is set.
6525 */
6526 static void
6527f_input(typval_T *argvars, typval_T *rettv)
6528{
6529 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6530}
6531
6532/*
6533 * "inputdialog()" function
6534 */
6535 static void
6536f_inputdialog(typval_T *argvars, typval_T *rettv)
6537{
6538#if defined(FEAT_GUI_TEXTDIALOG)
6539 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
6540 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6541 {
6542 char_u *message;
6543 char_u buf[NUMBUFLEN];
6544 char_u *defstr = (char_u *)"";
6545
6546 message = get_tv_string_chk(&argvars[0]);
6547 if (argvars[1].v_type != VAR_UNKNOWN
6548 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
6549 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6550 else
6551 IObuff[0] = NUL;
6552 if (message != NULL && defstr != NULL
6553 && do_dialog(VIM_QUESTION, NULL, message,
6554 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6555 rettv->vval.v_string = vim_strsave(IObuff);
6556 else
6557 {
6558 if (message != NULL && defstr != NULL
6559 && argvars[1].v_type != VAR_UNKNOWN
6560 && argvars[2].v_type != VAR_UNKNOWN)
6561 rettv->vval.v_string = vim_strsave(
6562 get_tv_string_buf(&argvars[2], buf));
6563 else
6564 rettv->vval.v_string = NULL;
6565 }
6566 rettv->v_type = VAR_STRING;
6567 }
6568 else
6569#endif
6570 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6571}
6572
6573/*
6574 * "inputlist()" function
6575 */
6576 static void
6577f_inputlist(typval_T *argvars, typval_T *rettv)
6578{
6579 listitem_T *li;
6580 int selected;
6581 int mouse_used;
6582
6583#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02006584 /* While starting up, there is no place to enter text. When running tests
6585 * with --not-a-term we assume feedkeys() will be used. */
6586 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006587 return;
6588#endif
6589 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6590 {
6591 EMSG2(_(e_listarg), "inputlist()");
6592 return;
6593 }
6594
6595 msg_start();
6596 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
6597 lines_left = Rows; /* avoid more prompt */
6598 msg_scroll = TRUE;
6599 msg_clr_eos();
6600
6601 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
6602 {
6603 msg_puts(get_tv_string(&li->li_tv));
6604 msg_putchar('\n');
6605 }
6606
6607 /* Ask for choice. */
6608 selected = prompt_for_number(&mouse_used);
6609 if (mouse_used)
6610 selected -= lines_left;
6611
6612 rettv->vval.v_number = selected;
6613}
6614
6615
6616static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6617
6618/*
6619 * "inputrestore()" function
6620 */
6621 static void
6622f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6623{
6624 if (ga_userinput.ga_len > 0)
6625 {
6626 --ga_userinput.ga_len;
6627 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
6628 + ga_userinput.ga_len);
6629 /* default return is zero == OK */
6630 }
6631 else if (p_verbose > 1)
6632 {
6633 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
6634 rettv->vval.v_number = 1; /* Failed */
6635 }
6636}
6637
6638/*
6639 * "inputsave()" function
6640 */
6641 static void
6642f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
6643{
6644 /* Add an entry to the stack of typeahead storage. */
6645 if (ga_grow(&ga_userinput, 1) == OK)
6646 {
6647 save_typeahead((tasave_T *)(ga_userinput.ga_data)
6648 + ga_userinput.ga_len);
6649 ++ga_userinput.ga_len;
6650 /* default return is zero == OK */
6651 }
6652 else
6653 rettv->vval.v_number = 1; /* Failed */
6654}
6655
6656/*
6657 * "inputsecret()" function
6658 */
6659 static void
6660f_inputsecret(typval_T *argvars, typval_T *rettv)
6661{
6662 ++cmdline_star;
6663 ++inputsecret_flag;
6664 f_input(argvars, rettv);
6665 --cmdline_star;
6666 --inputsecret_flag;
6667}
6668
6669/*
6670 * "insert()" function
6671 */
6672 static void
6673f_insert(typval_T *argvars, typval_T *rettv)
6674{
6675 long before = 0;
6676 listitem_T *item;
6677 list_T *l;
6678 int error = FALSE;
6679
6680 if (argvars[0].v_type != VAR_LIST)
6681 EMSG2(_(e_listarg), "insert()");
6682 else if ((l = argvars[0].vval.v_list) != NULL
6683 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
6684 {
6685 if (argvars[2].v_type != VAR_UNKNOWN)
6686 before = (long)get_tv_number_chk(&argvars[2], &error);
6687 if (error)
6688 return; /* type error; errmsg already given */
6689
6690 if (before == l->lv_len)
6691 item = NULL;
6692 else
6693 {
6694 item = list_find(l, before);
6695 if (item == NULL)
6696 {
6697 EMSGN(_(e_listidx), before);
6698 l = NULL;
6699 }
6700 }
6701 if (l != NULL)
6702 {
6703 list_insert_tv(l, &argvars[1], item);
6704 copy_tv(&argvars[0], rettv);
6705 }
6706 }
6707}
6708
6709/*
6710 * "invert(expr)" function
6711 */
6712 static void
6713f_invert(typval_T *argvars, typval_T *rettv)
6714{
6715 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
6716}
6717
6718/*
6719 * "isdirectory()" function
6720 */
6721 static void
6722f_isdirectory(typval_T *argvars, typval_T *rettv)
6723{
6724 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
6725}
6726
6727/*
6728 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
6729 * or it refers to a List or Dictionary that is locked.
6730 */
6731 static int
6732tv_islocked(typval_T *tv)
6733{
6734 return (tv->v_lock & VAR_LOCKED)
6735 || (tv->v_type == VAR_LIST
6736 && tv->vval.v_list != NULL
6737 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
6738 || (tv->v_type == VAR_DICT
6739 && tv->vval.v_dict != NULL
6740 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
6741}
6742
6743/*
6744 * "islocked()" function
6745 */
6746 static void
6747f_islocked(typval_T *argvars, typval_T *rettv)
6748{
6749 lval_T lv;
6750 char_u *end;
6751 dictitem_T *di;
6752
6753 rettv->vval.v_number = -1;
6754 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01006755 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006756 if (end != NULL && lv.ll_name != NULL)
6757 {
6758 if (*end != NUL)
6759 EMSG(_(e_trailing));
6760 else
6761 {
6762 if (lv.ll_tv == NULL)
6763 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006764 di = find_var(lv.ll_name, NULL, TRUE);
6765 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006766 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006767 /* Consider a variable locked when:
6768 * 1. the variable itself is locked
6769 * 2. the value of the variable is locked.
6770 * 3. the List or Dict value is locked.
6771 */
6772 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
6773 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006774 }
6775 }
6776 else if (lv.ll_range)
6777 EMSG(_("E786: Range not allowed"));
6778 else if (lv.ll_newkey != NULL)
6779 EMSG2(_(e_dictkey), lv.ll_newkey);
6780 else if (lv.ll_list != NULL)
6781 /* List item. */
6782 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
6783 else
6784 /* Dictionary item. */
6785 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
6786 }
6787 }
6788
6789 clear_lval(&lv);
6790}
6791
6792#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
6793/*
6794 * "isnan()" function
6795 */
6796 static void
6797f_isnan(typval_T *argvars, typval_T *rettv)
6798{
6799 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
6800 && isnan(argvars[0].vval.v_float);
6801}
6802#endif
6803
6804/*
6805 * "items(dict)" function
6806 */
6807 static void
6808f_items(typval_T *argvars, typval_T *rettv)
6809{
6810 dict_list(argvars, rettv, 2);
6811}
6812
6813#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
6814/*
6815 * Get the job from the argument.
6816 * Returns NULL if the job is invalid.
6817 */
6818 static job_T *
6819get_job_arg(typval_T *tv)
6820{
6821 job_T *job;
6822
6823 if (tv->v_type != VAR_JOB)
6824 {
6825 EMSG2(_(e_invarg2), get_tv_string(tv));
6826 return NULL;
6827 }
6828 job = tv->vval.v_job;
6829
6830 if (job == NULL)
6831 EMSG(_("E916: not a valid job"));
6832 return job;
6833}
6834
6835/*
6836 * "job_getchannel()" function
6837 */
6838 static void
6839f_job_getchannel(typval_T *argvars, typval_T *rettv)
6840{
6841 job_T *job = get_job_arg(&argvars[0]);
6842
6843 if (job != NULL)
6844 {
6845 rettv->v_type = VAR_CHANNEL;
6846 rettv->vval.v_channel = job->jv_channel;
6847 if (job->jv_channel != NULL)
6848 ++job->jv_channel->ch_refcount;
6849 }
6850}
6851
6852/*
6853 * "job_info()" function
6854 */
6855 static void
6856f_job_info(typval_T *argvars, typval_T *rettv)
6857{
6858 job_T *job = get_job_arg(&argvars[0]);
6859
6860 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
6861 job_info(job, rettv->vval.v_dict);
6862}
6863
6864/*
6865 * "job_setoptions()" function
6866 */
6867 static void
6868f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
6869{
6870 job_T *job = get_job_arg(&argvars[0]);
6871 jobopt_T opt;
6872
6873 if (job == NULL)
6874 return;
6875 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02006876 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006877 job_set_options(job, &opt);
6878 free_job_options(&opt);
6879}
6880
6881/*
6882 * "job_start()" function
6883 */
6884 static void
6885f_job_start(typval_T *argvars, typval_T *rettv)
6886{
6887 rettv->v_type = VAR_JOB;
6888 if (check_restricted() || check_secure())
6889 return;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006890 rettv->vval.v_job = job_start(argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006891}
6892
6893/*
6894 * "job_status()" function
6895 */
6896 static void
6897f_job_status(typval_T *argvars, typval_T *rettv)
6898{
6899 job_T *job = get_job_arg(&argvars[0]);
6900
6901 if (job != NULL)
6902 {
6903 rettv->v_type = VAR_STRING;
6904 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
6905 }
6906}
6907
6908/*
6909 * "job_stop()" function
6910 */
6911 static void
6912f_job_stop(typval_T *argvars, typval_T *rettv)
6913{
6914 job_T *job = get_job_arg(&argvars[0]);
6915
6916 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02006917 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006918}
6919#endif
6920
6921/*
6922 * "join()" function
6923 */
6924 static void
6925f_join(typval_T *argvars, typval_T *rettv)
6926{
6927 garray_T ga;
6928 char_u *sep;
6929
6930 if (argvars[0].v_type != VAR_LIST)
6931 {
6932 EMSG(_(e_listreq));
6933 return;
6934 }
6935 if (argvars[0].vval.v_list == NULL)
6936 return;
6937 if (argvars[1].v_type == VAR_UNKNOWN)
6938 sep = (char_u *)" ";
6939 else
6940 sep = get_tv_string_chk(&argvars[1]);
6941
6942 rettv->v_type = VAR_STRING;
6943
6944 if (sep != NULL)
6945 {
6946 ga_init2(&ga, (int)sizeof(char), 80);
6947 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
6948 ga_append(&ga, NUL);
6949 rettv->vval.v_string = (char_u *)ga.ga_data;
6950 }
6951 else
6952 rettv->vval.v_string = NULL;
6953}
6954
6955/*
6956 * "js_decode()" function
6957 */
6958 static void
6959f_js_decode(typval_T *argvars, typval_T *rettv)
6960{
6961 js_read_T reader;
6962
6963 reader.js_buf = get_tv_string(&argvars[0]);
6964 reader.js_fill = NULL;
6965 reader.js_used = 0;
6966 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
6967 EMSG(_(e_invarg));
6968}
6969
6970/*
6971 * "js_encode()" function
6972 */
6973 static void
6974f_js_encode(typval_T *argvars, typval_T *rettv)
6975{
6976 rettv->v_type = VAR_STRING;
6977 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
6978}
6979
6980/*
6981 * "json_decode()" function
6982 */
6983 static void
6984f_json_decode(typval_T *argvars, typval_T *rettv)
6985{
6986 js_read_T reader;
6987
6988 reader.js_buf = get_tv_string(&argvars[0]);
6989 reader.js_fill = NULL;
6990 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01006991 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006992}
6993
6994/*
6995 * "json_encode()" function
6996 */
6997 static void
6998f_json_encode(typval_T *argvars, typval_T *rettv)
6999{
7000 rettv->v_type = VAR_STRING;
7001 rettv->vval.v_string = json_encode(&argvars[0], 0);
7002}
7003
7004/*
7005 * "keys()" function
7006 */
7007 static void
7008f_keys(typval_T *argvars, typval_T *rettv)
7009{
7010 dict_list(argvars, rettv, 0);
7011}
7012
7013/*
7014 * "last_buffer_nr()" function.
7015 */
7016 static void
7017f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7018{
7019 int n = 0;
7020 buf_T *buf;
7021
Bram Moolenaar29323592016-07-24 22:04:11 +02007022 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007023 if (n < buf->b_fnum)
7024 n = buf->b_fnum;
7025
7026 rettv->vval.v_number = n;
7027}
7028
7029/*
7030 * "len()" function
7031 */
7032 static void
7033f_len(typval_T *argvars, typval_T *rettv)
7034{
7035 switch (argvars[0].v_type)
7036 {
7037 case VAR_STRING:
7038 case VAR_NUMBER:
7039 rettv->vval.v_number = (varnumber_T)STRLEN(
7040 get_tv_string(&argvars[0]));
7041 break;
7042 case VAR_LIST:
7043 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7044 break;
7045 case VAR_DICT:
7046 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7047 break;
7048 case VAR_UNKNOWN:
7049 case VAR_SPECIAL:
7050 case VAR_FLOAT:
7051 case VAR_FUNC:
7052 case VAR_PARTIAL:
7053 case VAR_JOB:
7054 case VAR_CHANNEL:
7055 EMSG(_("E701: Invalid type for len()"));
7056 break;
7057 }
7058}
7059
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007060 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007061libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007062{
7063#ifdef FEAT_LIBCALL
7064 char_u *string_in;
7065 char_u **string_result;
7066 int nr_result;
7067#endif
7068
7069 rettv->v_type = type;
7070 if (type != VAR_NUMBER)
7071 rettv->vval.v_string = NULL;
7072
7073 if (check_restricted() || check_secure())
7074 return;
7075
7076#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007077 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007078 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7079 {
7080 string_in = NULL;
7081 if (argvars[2].v_type == VAR_STRING)
7082 string_in = argvars[2].vval.v_string;
7083 if (type == VAR_NUMBER)
7084 string_result = NULL;
7085 else
7086 string_result = &rettv->vval.v_string;
7087 if (mch_libcall(argvars[0].vval.v_string,
7088 argvars[1].vval.v_string,
7089 string_in,
7090 argvars[2].vval.v_number,
7091 string_result,
7092 &nr_result) == OK
7093 && type == VAR_NUMBER)
7094 rettv->vval.v_number = nr_result;
7095 }
7096#endif
7097}
7098
7099/*
7100 * "libcall()" function
7101 */
7102 static void
7103f_libcall(typval_T *argvars, typval_T *rettv)
7104{
7105 libcall_common(argvars, rettv, VAR_STRING);
7106}
7107
7108/*
7109 * "libcallnr()" function
7110 */
7111 static void
7112f_libcallnr(typval_T *argvars, typval_T *rettv)
7113{
7114 libcall_common(argvars, rettv, VAR_NUMBER);
7115}
7116
7117/*
7118 * "line(string)" function
7119 */
7120 static void
7121f_line(typval_T *argvars, typval_T *rettv)
7122{
7123 linenr_T lnum = 0;
7124 pos_T *fp;
7125 int fnum;
7126
7127 fp = var2fpos(&argvars[0], TRUE, &fnum);
7128 if (fp != NULL)
7129 lnum = fp->lnum;
7130 rettv->vval.v_number = lnum;
7131}
7132
7133/*
7134 * "line2byte(lnum)" function
7135 */
7136 static void
7137f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7138{
7139#ifndef FEAT_BYTEOFF
7140 rettv->vval.v_number = -1;
7141#else
7142 linenr_T lnum;
7143
7144 lnum = get_tv_lnum(argvars);
7145 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7146 rettv->vval.v_number = -1;
7147 else
7148 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7149 if (rettv->vval.v_number >= 0)
7150 ++rettv->vval.v_number;
7151#endif
7152}
7153
7154/*
7155 * "lispindent(lnum)" function
7156 */
7157 static void
7158f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7159{
7160#ifdef FEAT_LISP
7161 pos_T pos;
7162 linenr_T lnum;
7163
7164 pos = curwin->w_cursor;
7165 lnum = get_tv_lnum(argvars);
7166 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7167 {
7168 curwin->w_cursor.lnum = lnum;
7169 rettv->vval.v_number = get_lisp_indent();
7170 curwin->w_cursor = pos;
7171 }
7172 else
7173#endif
7174 rettv->vval.v_number = -1;
7175}
7176
7177/*
7178 * "localtime()" function
7179 */
7180 static void
7181f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7182{
7183 rettv->vval.v_number = (varnumber_T)time(NULL);
7184}
7185
7186static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
7187
7188 static void
7189get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7190{
7191 char_u *keys;
7192 char_u *which;
7193 char_u buf[NUMBUFLEN];
7194 char_u *keys_buf = NULL;
7195 char_u *rhs;
7196 int mode;
7197 int abbr = FALSE;
7198 int get_dict = FALSE;
7199 mapblock_T *mp;
7200 int buffer_local;
7201
7202 /* return empty string for failure */
7203 rettv->v_type = VAR_STRING;
7204 rettv->vval.v_string = NULL;
7205
7206 keys = get_tv_string(&argvars[0]);
7207 if (*keys == NUL)
7208 return;
7209
7210 if (argvars[1].v_type != VAR_UNKNOWN)
7211 {
7212 which = get_tv_string_buf_chk(&argvars[1], buf);
7213 if (argvars[2].v_type != VAR_UNKNOWN)
7214 {
7215 abbr = (int)get_tv_number(&argvars[2]);
7216 if (argvars[3].v_type != VAR_UNKNOWN)
7217 get_dict = (int)get_tv_number(&argvars[3]);
7218 }
7219 }
7220 else
7221 which = (char_u *)"";
7222 if (which == NULL)
7223 return;
7224
7225 mode = get_map_mode(&which, 0);
7226
7227 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7228 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7229 vim_free(keys_buf);
7230
7231 if (!get_dict)
7232 {
7233 /* Return a string. */
7234 if (rhs != NULL)
7235 rettv->vval.v_string = str2special_save(rhs, FALSE);
7236
7237 }
7238 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7239 {
7240 /* Return a dictionary. */
7241 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7242 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7243 dict_T *dict = rettv->vval.v_dict;
7244
7245 dict_add_nr_str(dict, "lhs", 0L, lhs);
7246 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
7247 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
7248 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
7249 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
7250 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
7251 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
7252 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
7253 dict_add_nr_str(dict, "mode", 0L, mapmode);
7254
7255 vim_free(lhs);
7256 vim_free(mapmode);
7257 }
7258}
7259
7260#ifdef FEAT_FLOAT
7261/*
7262 * "log()" function
7263 */
7264 static void
7265f_log(typval_T *argvars, typval_T *rettv)
7266{
7267 float_T f = 0.0;
7268
7269 rettv->v_type = VAR_FLOAT;
7270 if (get_float_arg(argvars, &f) == OK)
7271 rettv->vval.v_float = log(f);
7272 else
7273 rettv->vval.v_float = 0.0;
7274}
7275
7276/*
7277 * "log10()" function
7278 */
7279 static void
7280f_log10(typval_T *argvars, typval_T *rettv)
7281{
7282 float_T f = 0.0;
7283
7284 rettv->v_type = VAR_FLOAT;
7285 if (get_float_arg(argvars, &f) == OK)
7286 rettv->vval.v_float = log10(f);
7287 else
7288 rettv->vval.v_float = 0.0;
7289}
7290#endif
7291
7292#ifdef FEAT_LUA
7293/*
7294 * "luaeval()" function
7295 */
7296 static void
7297f_luaeval(typval_T *argvars, typval_T *rettv)
7298{
7299 char_u *str;
7300 char_u buf[NUMBUFLEN];
7301
7302 str = get_tv_string_buf(&argvars[0], buf);
7303 do_luaeval(str, argvars + 1, rettv);
7304}
7305#endif
7306
7307/*
7308 * "map()" function
7309 */
7310 static void
7311f_map(typval_T *argvars, typval_T *rettv)
7312{
7313 filter_map(argvars, rettv, TRUE);
7314}
7315
7316/*
7317 * "maparg()" function
7318 */
7319 static void
7320f_maparg(typval_T *argvars, typval_T *rettv)
7321{
7322 get_maparg(argvars, rettv, TRUE);
7323}
7324
7325/*
7326 * "mapcheck()" function
7327 */
7328 static void
7329f_mapcheck(typval_T *argvars, typval_T *rettv)
7330{
7331 get_maparg(argvars, rettv, FALSE);
7332}
7333
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007334typedef enum
7335{
7336 MATCH_END, /* matchend() */
7337 MATCH_MATCH, /* match() */
7338 MATCH_STR, /* matchstr() */
7339 MATCH_LIST, /* matchlist() */
7340 MATCH_POS /* matchstrpos() */
7341} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007342
7343 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007344find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007345{
7346 char_u *str = NULL;
7347 long len = 0;
7348 char_u *expr = NULL;
7349 char_u *pat;
7350 regmatch_T regmatch;
7351 char_u patbuf[NUMBUFLEN];
7352 char_u strbuf[NUMBUFLEN];
7353 char_u *save_cpo;
7354 long start = 0;
7355 long nth = 1;
7356 colnr_T startcol = 0;
7357 int match = 0;
7358 list_T *l = NULL;
7359 listitem_T *li = NULL;
7360 long idx = 0;
7361 char_u *tofree = NULL;
7362
7363 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7364 save_cpo = p_cpo;
7365 p_cpo = (char_u *)"";
7366
7367 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007368 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007369 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007370 /* type MATCH_LIST: return empty list when there are no matches.
7371 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007372 if (rettv_list_alloc(rettv) == FAIL)
7373 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007374 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007375 && (list_append_string(rettv->vval.v_list,
7376 (char_u *)"", 0) == FAIL
7377 || list_append_number(rettv->vval.v_list,
7378 (varnumber_T)-1) == FAIL
7379 || list_append_number(rettv->vval.v_list,
7380 (varnumber_T)-1) == FAIL
7381 || list_append_number(rettv->vval.v_list,
7382 (varnumber_T)-1) == FAIL))
7383 {
7384 list_free(rettv->vval.v_list);
7385 rettv->vval.v_list = NULL;
7386 goto theend;
7387 }
7388 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007389 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007390 {
7391 rettv->v_type = VAR_STRING;
7392 rettv->vval.v_string = NULL;
7393 }
7394
7395 if (argvars[0].v_type == VAR_LIST)
7396 {
7397 if ((l = argvars[0].vval.v_list) == NULL)
7398 goto theend;
7399 li = l->lv_first;
7400 }
7401 else
7402 {
7403 expr = str = get_tv_string(&argvars[0]);
7404 len = (long)STRLEN(str);
7405 }
7406
7407 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
7408 if (pat == NULL)
7409 goto theend;
7410
7411 if (argvars[2].v_type != VAR_UNKNOWN)
7412 {
7413 int error = FALSE;
7414
7415 start = (long)get_tv_number_chk(&argvars[2], &error);
7416 if (error)
7417 goto theend;
7418 if (l != NULL)
7419 {
7420 li = list_find(l, start);
7421 if (li == NULL)
7422 goto theend;
7423 idx = l->lv_idx; /* use the cached index */
7424 }
7425 else
7426 {
7427 if (start < 0)
7428 start = 0;
7429 if (start > len)
7430 goto theend;
7431 /* When "count" argument is there ignore matches before "start",
7432 * otherwise skip part of the string. Differs when pattern is "^"
7433 * or "\<". */
7434 if (argvars[3].v_type != VAR_UNKNOWN)
7435 startcol = start;
7436 else
7437 {
7438 str += start;
7439 len -= start;
7440 }
7441 }
7442
7443 if (argvars[3].v_type != VAR_UNKNOWN)
7444 nth = (long)get_tv_number_chk(&argvars[3], &error);
7445 if (error)
7446 goto theend;
7447 }
7448
7449 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7450 if (regmatch.regprog != NULL)
7451 {
7452 regmatch.rm_ic = p_ic;
7453
7454 for (;;)
7455 {
7456 if (l != NULL)
7457 {
7458 if (li == NULL)
7459 {
7460 match = FALSE;
7461 break;
7462 }
7463 vim_free(tofree);
7464 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7465 if (str == NULL)
7466 break;
7467 }
7468
7469 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7470
7471 if (match && --nth <= 0)
7472 break;
7473 if (l == NULL && !match)
7474 break;
7475
7476 /* Advance to just after the match. */
7477 if (l != NULL)
7478 {
7479 li = li->li_next;
7480 ++idx;
7481 }
7482 else
7483 {
7484#ifdef FEAT_MBYTE
7485 startcol = (colnr_T)(regmatch.startp[0]
7486 + (*mb_ptr2len)(regmatch.startp[0]) - str);
7487#else
7488 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
7489#endif
7490 if (startcol > (colnr_T)len
7491 || str + startcol <= regmatch.startp[0])
7492 {
7493 match = FALSE;
7494 break;
7495 }
7496 }
7497 }
7498
7499 if (match)
7500 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007501 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007502 {
7503 listitem_T *li1 = rettv->vval.v_list->lv_first;
7504 listitem_T *li2 = li1->li_next;
7505 listitem_T *li3 = li2->li_next;
7506 listitem_T *li4 = li3->li_next;
7507
7508 vim_free(li1->li_tv.vval.v_string);
7509 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7510 (int)(regmatch.endp[0] - regmatch.startp[0]));
7511 li3->li_tv.vval.v_number =
7512 (varnumber_T)(regmatch.startp[0] - expr);
7513 li4->li_tv.vval.v_number =
7514 (varnumber_T)(regmatch.endp[0] - expr);
7515 if (l != NULL)
7516 li2->li_tv.vval.v_number = (varnumber_T)idx;
7517 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007518 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007519 {
7520 int i;
7521
7522 /* return list with matched string and submatches */
7523 for (i = 0; i < NSUBEXP; ++i)
7524 {
7525 if (regmatch.endp[i] == NULL)
7526 {
7527 if (list_append_string(rettv->vval.v_list,
7528 (char_u *)"", 0) == FAIL)
7529 break;
7530 }
7531 else if (list_append_string(rettv->vval.v_list,
7532 regmatch.startp[i],
7533 (int)(regmatch.endp[i] - regmatch.startp[i]))
7534 == FAIL)
7535 break;
7536 }
7537 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007538 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007539 {
7540 /* return matched string */
7541 if (l != NULL)
7542 copy_tv(&li->li_tv, rettv);
7543 else
7544 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
7545 (int)(regmatch.endp[0] - regmatch.startp[0]));
7546 }
7547 else if (l != NULL)
7548 rettv->vval.v_number = idx;
7549 else
7550 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007551 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007552 rettv->vval.v_number =
7553 (varnumber_T)(regmatch.startp[0] - str);
7554 else
7555 rettv->vval.v_number =
7556 (varnumber_T)(regmatch.endp[0] - str);
7557 rettv->vval.v_number += (varnumber_T)(str - expr);
7558 }
7559 }
7560 vim_regfree(regmatch.regprog);
7561 }
7562
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007563theend:
7564 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007565 /* matchstrpos() without a list: drop the second item. */
7566 listitem_remove(rettv->vval.v_list,
7567 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007568 vim_free(tofree);
7569 p_cpo = save_cpo;
7570}
7571
7572/*
7573 * "match()" function
7574 */
7575 static void
7576f_match(typval_T *argvars, typval_T *rettv)
7577{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007578 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007579}
7580
7581/*
7582 * "matchadd()" function
7583 */
7584 static void
7585f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7586{
7587#ifdef FEAT_SEARCH_EXTRA
7588 char_u buf[NUMBUFLEN];
7589 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
7590 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
7591 int prio = 10; /* default priority */
7592 int id = -1;
7593 int error = FALSE;
7594 char_u *conceal_char = NULL;
7595
7596 rettv->vval.v_number = -1;
7597
7598 if (grp == NULL || pat == NULL)
7599 return;
7600 if (argvars[2].v_type != VAR_UNKNOWN)
7601 {
7602 prio = (int)get_tv_number_chk(&argvars[2], &error);
7603 if (argvars[3].v_type != VAR_UNKNOWN)
7604 {
7605 id = (int)get_tv_number_chk(&argvars[3], &error);
7606 if (argvars[4].v_type != VAR_UNKNOWN)
7607 {
7608 if (argvars[4].v_type != VAR_DICT)
7609 {
7610 EMSG(_(e_dictreq));
7611 return;
7612 }
7613 if (dict_find(argvars[4].vval.v_dict,
7614 (char_u *)"conceal", -1) != NULL)
7615 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7616 (char_u *)"conceal", FALSE);
7617 }
7618 }
7619 }
7620 if (error == TRUE)
7621 return;
7622 if (id >= 1 && id <= 3)
7623 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007624 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007625 return;
7626 }
7627
7628 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
7629 conceal_char);
7630#endif
7631}
7632
7633/*
7634 * "matchaddpos()" function
7635 */
7636 static void
7637f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7638{
7639#ifdef FEAT_SEARCH_EXTRA
7640 char_u buf[NUMBUFLEN];
7641 char_u *group;
7642 int prio = 10;
7643 int id = -1;
7644 int error = FALSE;
7645 list_T *l;
7646 char_u *conceal_char = NULL;
7647
7648 rettv->vval.v_number = -1;
7649
7650 group = get_tv_string_buf_chk(&argvars[0], buf);
7651 if (group == NULL)
7652 return;
7653
7654 if (argvars[1].v_type != VAR_LIST)
7655 {
7656 EMSG2(_(e_listarg), "matchaddpos()");
7657 return;
7658 }
7659 l = argvars[1].vval.v_list;
7660 if (l == NULL)
7661 return;
7662
7663 if (argvars[2].v_type != VAR_UNKNOWN)
7664 {
7665 prio = (int)get_tv_number_chk(&argvars[2], &error);
7666 if (argvars[3].v_type != VAR_UNKNOWN)
7667 {
7668 id = (int)get_tv_number_chk(&argvars[3], &error);
7669 if (argvars[4].v_type != VAR_UNKNOWN)
7670 {
7671 if (argvars[4].v_type != VAR_DICT)
7672 {
7673 EMSG(_(e_dictreq));
7674 return;
7675 }
7676 if (dict_find(argvars[4].vval.v_dict,
7677 (char_u *)"conceal", -1) != NULL)
7678 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7679 (char_u *)"conceal", FALSE);
7680 }
7681 }
7682 }
7683 if (error == TRUE)
7684 return;
7685
7686 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
7687 if (id == 1 || id == 2)
7688 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007689 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007690 return;
7691 }
7692
7693 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
7694 conceal_char);
7695#endif
7696}
7697
7698/*
7699 * "matcharg()" function
7700 */
7701 static void
7702f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
7703{
7704 if (rettv_list_alloc(rettv) == OK)
7705 {
7706#ifdef FEAT_SEARCH_EXTRA
7707 int id = (int)get_tv_number(&argvars[0]);
7708 matchitem_T *m;
7709
7710 if (id >= 1 && id <= 3)
7711 {
7712 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
7713 {
7714 list_append_string(rettv->vval.v_list,
7715 syn_id2name(m->hlg_id), -1);
7716 list_append_string(rettv->vval.v_list, m->pattern, -1);
7717 }
7718 else
7719 {
7720 list_append_string(rettv->vval.v_list, NULL, -1);
7721 list_append_string(rettv->vval.v_list, NULL, -1);
7722 }
7723 }
7724#endif
7725 }
7726}
7727
7728/*
7729 * "matchdelete()" function
7730 */
7731 static void
7732f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7733{
7734#ifdef FEAT_SEARCH_EXTRA
7735 rettv->vval.v_number = match_delete(curwin,
7736 (int)get_tv_number(&argvars[0]), TRUE);
7737#endif
7738}
7739
7740/*
7741 * "matchend()" function
7742 */
7743 static void
7744f_matchend(typval_T *argvars, typval_T *rettv)
7745{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007746 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007747}
7748
7749/*
7750 * "matchlist()" function
7751 */
7752 static void
7753f_matchlist(typval_T *argvars, typval_T *rettv)
7754{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007755 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007756}
7757
7758/*
7759 * "matchstr()" function
7760 */
7761 static void
7762f_matchstr(typval_T *argvars, typval_T *rettv)
7763{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007764 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007765}
7766
7767/*
7768 * "matchstrpos()" function
7769 */
7770 static void
7771f_matchstrpos(typval_T *argvars, typval_T *rettv)
7772{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007773 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007774}
7775
7776static void max_min(typval_T *argvars, typval_T *rettv, int domax);
7777
7778 static void
7779max_min(typval_T *argvars, typval_T *rettv, int domax)
7780{
7781 varnumber_T n = 0;
7782 varnumber_T i;
7783 int error = FALSE;
7784
7785 if (argvars[0].v_type == VAR_LIST)
7786 {
7787 list_T *l;
7788 listitem_T *li;
7789
7790 l = argvars[0].vval.v_list;
7791 if (l != NULL)
7792 {
7793 li = l->lv_first;
7794 if (li != NULL)
7795 {
7796 n = get_tv_number_chk(&li->li_tv, &error);
7797 for (;;)
7798 {
7799 li = li->li_next;
7800 if (li == NULL)
7801 break;
7802 i = get_tv_number_chk(&li->li_tv, &error);
7803 if (domax ? i > n : i < n)
7804 n = i;
7805 }
7806 }
7807 }
7808 }
7809 else if (argvars[0].v_type == VAR_DICT)
7810 {
7811 dict_T *d;
7812 int first = TRUE;
7813 hashitem_T *hi;
7814 int todo;
7815
7816 d = argvars[0].vval.v_dict;
7817 if (d != NULL)
7818 {
7819 todo = (int)d->dv_hashtab.ht_used;
7820 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
7821 {
7822 if (!HASHITEM_EMPTY(hi))
7823 {
7824 --todo;
7825 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
7826 if (first)
7827 {
7828 n = i;
7829 first = FALSE;
7830 }
7831 else if (domax ? i > n : i < n)
7832 n = i;
7833 }
7834 }
7835 }
7836 }
7837 else
Bram Moolenaar26b84332016-09-04 21:42:36 +02007838 EMSG2(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007839 rettv->vval.v_number = error ? 0 : n;
7840}
7841
7842/*
7843 * "max()" function
7844 */
7845 static void
7846f_max(typval_T *argvars, typval_T *rettv)
7847{
7848 max_min(argvars, rettv, TRUE);
7849}
7850
7851/*
7852 * "min()" function
7853 */
7854 static void
7855f_min(typval_T *argvars, typval_T *rettv)
7856{
7857 max_min(argvars, rettv, FALSE);
7858}
7859
7860static int mkdir_recurse(char_u *dir, int prot);
7861
7862/*
7863 * Create the directory in which "dir" is located, and higher levels when
7864 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02007865 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007866 */
7867 static int
7868mkdir_recurse(char_u *dir, int prot)
7869{
7870 char_u *p;
7871 char_u *updir;
7872 int r = FAIL;
7873
7874 /* Get end of directory name in "dir".
7875 * We're done when it's "/" or "c:/". */
7876 p = gettail_sep(dir);
7877 if (p <= get_past_head(dir))
7878 return OK;
7879
7880 /* If the directory exists we're done. Otherwise: create it.*/
7881 updir = vim_strnsave(dir, (int)(p - dir));
7882 if (updir == NULL)
7883 return FAIL;
7884 if (mch_isdir(updir))
7885 r = OK;
7886 else if (mkdir_recurse(updir, prot) == OK)
7887 r = vim_mkdir_emsg(updir, prot);
7888 vim_free(updir);
7889 return r;
7890}
7891
7892#ifdef vim_mkdir
7893/*
7894 * "mkdir()" function
7895 */
7896 static void
7897f_mkdir(typval_T *argvars, typval_T *rettv)
7898{
7899 char_u *dir;
7900 char_u buf[NUMBUFLEN];
7901 int prot = 0755;
7902
7903 rettv->vval.v_number = FAIL;
7904 if (check_restricted() || check_secure())
7905 return;
7906
7907 dir = get_tv_string_buf(&argvars[0], buf);
7908 if (*dir == NUL)
7909 rettv->vval.v_number = FAIL;
7910 else
7911 {
7912 if (*gettail(dir) == NUL)
7913 /* remove trailing slashes */
7914 *gettail_sep(dir) = NUL;
7915
7916 if (argvars[1].v_type != VAR_UNKNOWN)
7917 {
7918 if (argvars[2].v_type != VAR_UNKNOWN)
7919 prot = (int)get_tv_number_chk(&argvars[2], NULL);
7920 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
7921 mkdir_recurse(dir, prot);
7922 }
7923 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
7924 }
7925}
7926#endif
7927
7928/*
7929 * "mode()" function
7930 */
7931 static void
7932f_mode(typval_T *argvars, typval_T *rettv)
7933{
7934 char_u buf[3];
7935
7936 buf[1] = NUL;
7937 buf[2] = NUL;
7938
7939 if (time_for_testing == 93784)
7940 {
7941 /* Testing the two-character code. */
7942 buf[0] = 'x';
7943 buf[1] = '!';
7944 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02007945#ifdef FEAT_TERMINAL
7946 else if (term_use_loop())
7947 buf[0] = 't';
7948#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007949 else if (VIsual_active)
7950 {
7951 if (VIsual_select)
7952 buf[0] = VIsual_mode + 's' - 'v';
7953 else
7954 buf[0] = VIsual_mode;
7955 }
7956 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
7957 || State == CONFIRM)
7958 {
7959 buf[0] = 'r';
7960 if (State == ASKMORE)
7961 buf[1] = 'm';
7962 else if (State == CONFIRM)
7963 buf[1] = '?';
7964 }
7965 else if (State == EXTERNCMD)
7966 buf[0] = '!';
7967 else if (State & INSERT)
7968 {
7969#ifdef FEAT_VREPLACE
7970 if (State & VREPLACE_FLAG)
7971 {
7972 buf[0] = 'R';
7973 buf[1] = 'v';
7974 }
7975 else
7976#endif
Bram Moolenaare90858d2017-02-01 17:24:34 +01007977 {
7978 if (State & REPLACE_FLAG)
7979 buf[0] = 'R';
7980 else
7981 buf[0] = 'i';
7982#ifdef FEAT_INS_EXPAND
7983 if (ins_compl_active())
7984 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01007985 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01007986 buf[1] = 'x';
7987#endif
7988 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007989 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01007990 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007991 {
7992 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01007993 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007994 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01007995 else if (exmode_active == EXMODE_NORMAL)
7996 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007997 }
7998 else
7999 {
8000 buf[0] = 'n';
8001 if (finish_op)
8002 buf[1] = 'o';
8003 }
8004
8005 /* Clear out the minor mode when the argument is not a non-zero number or
8006 * non-empty string. */
8007 if (!non_zero_arg(&argvars[0]))
8008 buf[1] = NUL;
8009
8010 rettv->vval.v_string = vim_strsave(buf);
8011 rettv->v_type = VAR_STRING;
8012}
8013
8014#if defined(FEAT_MZSCHEME) || defined(PROTO)
8015/*
8016 * "mzeval()" function
8017 */
8018 static void
8019f_mzeval(typval_T *argvars, typval_T *rettv)
8020{
8021 char_u *str;
8022 char_u buf[NUMBUFLEN];
8023
8024 str = get_tv_string_buf(&argvars[0], buf);
8025 do_mzeval(str, rettv);
8026}
8027
8028 void
8029mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8030{
8031 typval_T argvars[3];
8032
8033 argvars[0].v_type = VAR_STRING;
8034 argvars[0].vval.v_string = name;
8035 copy_tv(args, &argvars[1]);
8036 argvars[2].v_type = VAR_UNKNOWN;
8037 f_call(argvars, rettv);
8038 clear_tv(&argvars[1]);
8039}
8040#endif
8041
8042/*
8043 * "nextnonblank()" function
8044 */
8045 static void
8046f_nextnonblank(typval_T *argvars, typval_T *rettv)
8047{
8048 linenr_T lnum;
8049
8050 for (lnum = get_tv_lnum(argvars); ; ++lnum)
8051 {
8052 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8053 {
8054 lnum = 0;
8055 break;
8056 }
8057 if (*skipwhite(ml_get(lnum)) != NUL)
8058 break;
8059 }
8060 rettv->vval.v_number = lnum;
8061}
8062
8063/*
8064 * "nr2char()" function
8065 */
8066 static void
8067f_nr2char(typval_T *argvars, typval_T *rettv)
8068{
8069 char_u buf[NUMBUFLEN];
8070
8071#ifdef FEAT_MBYTE
8072 if (has_mbyte)
8073 {
8074 int utf8 = 0;
8075
8076 if (argvars[1].v_type != VAR_UNKNOWN)
8077 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
8078 if (utf8)
8079 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8080 else
8081 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8082 }
8083 else
8084#endif
8085 {
8086 buf[0] = (char_u)get_tv_number(&argvars[0]);
8087 buf[1] = NUL;
8088 }
8089 rettv->v_type = VAR_STRING;
8090 rettv->vval.v_string = vim_strsave(buf);
8091}
8092
8093/*
8094 * "or(expr, expr)" function
8095 */
8096 static void
8097f_or(typval_T *argvars, typval_T *rettv)
8098{
8099 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8100 | get_tv_number_chk(&argvars[1], NULL);
8101}
8102
8103/*
8104 * "pathshorten()" function
8105 */
8106 static void
8107f_pathshorten(typval_T *argvars, typval_T *rettv)
8108{
8109 char_u *p;
8110
8111 rettv->v_type = VAR_STRING;
8112 p = get_tv_string_chk(&argvars[0]);
8113 if (p == NULL)
8114 rettv->vval.v_string = NULL;
8115 else
8116 {
8117 p = vim_strsave(p);
8118 rettv->vval.v_string = p;
8119 if (p != NULL)
8120 shorten_dir(p);
8121 }
8122}
8123
8124#ifdef FEAT_PERL
8125/*
8126 * "perleval()" function
8127 */
8128 static void
8129f_perleval(typval_T *argvars, typval_T *rettv)
8130{
8131 char_u *str;
8132 char_u buf[NUMBUFLEN];
8133
8134 str = get_tv_string_buf(&argvars[0], buf);
8135 do_perleval(str, rettv);
8136}
8137#endif
8138
8139#ifdef FEAT_FLOAT
8140/*
8141 * "pow()" function
8142 */
8143 static void
8144f_pow(typval_T *argvars, typval_T *rettv)
8145{
8146 float_T fx = 0.0, fy = 0.0;
8147
8148 rettv->v_type = VAR_FLOAT;
8149 if (get_float_arg(argvars, &fx) == OK
8150 && get_float_arg(&argvars[1], &fy) == OK)
8151 rettv->vval.v_float = pow(fx, fy);
8152 else
8153 rettv->vval.v_float = 0.0;
8154}
8155#endif
8156
8157/*
8158 * "prevnonblank()" function
8159 */
8160 static void
8161f_prevnonblank(typval_T *argvars, typval_T *rettv)
8162{
8163 linenr_T lnum;
8164
8165 lnum = get_tv_lnum(argvars);
8166 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8167 lnum = 0;
8168 else
8169 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8170 --lnum;
8171 rettv->vval.v_number = lnum;
8172}
8173
8174/* This dummy va_list is here because:
8175 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8176 * - locally in the function results in a "used before set" warning
8177 * - using va_start() to initialize it gives "function with fixed args" error */
8178static va_list ap;
8179
8180/*
8181 * "printf()" function
8182 */
8183 static void
8184f_printf(typval_T *argvars, typval_T *rettv)
8185{
8186 char_u buf[NUMBUFLEN];
8187 int len;
8188 char_u *s;
8189 int saved_did_emsg = did_emsg;
8190 char *fmt;
8191
8192 rettv->v_type = VAR_STRING;
8193 rettv->vval.v_string = NULL;
8194
8195 /* Get the required length, allocate the buffer and do it for real. */
8196 did_emsg = FALSE;
8197 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008198 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008199 if (!did_emsg)
8200 {
8201 s = alloc(len + 1);
8202 if (s != NULL)
8203 {
8204 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008205 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8206 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008207 }
8208 }
8209 did_emsg |= saved_did_emsg;
8210}
8211
8212/*
8213 * "pumvisible()" function
8214 */
8215 static void
8216f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8217{
8218#ifdef FEAT_INS_EXPAND
8219 if (pum_visible())
8220 rettv->vval.v_number = 1;
8221#endif
8222}
8223
8224#ifdef FEAT_PYTHON3
8225/*
8226 * "py3eval()" function
8227 */
8228 static void
8229f_py3eval(typval_T *argvars, typval_T *rettv)
8230{
8231 char_u *str;
8232 char_u buf[NUMBUFLEN];
8233
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008234 if (p_pyx == 0)
8235 p_pyx = 3;
8236
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008237 str = get_tv_string_buf(&argvars[0], buf);
8238 do_py3eval(str, rettv);
8239}
8240#endif
8241
8242#ifdef FEAT_PYTHON
8243/*
8244 * "pyeval()" function
8245 */
8246 static void
8247f_pyeval(typval_T *argvars, typval_T *rettv)
8248{
8249 char_u *str;
8250 char_u buf[NUMBUFLEN];
8251
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008252 if (p_pyx == 0)
8253 p_pyx = 2;
8254
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008255 str = get_tv_string_buf(&argvars[0], buf);
8256 do_pyeval(str, rettv);
8257}
8258#endif
8259
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008260#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8261/*
8262 * "pyxeval()" function
8263 */
8264 static void
8265f_pyxeval(typval_T *argvars, typval_T *rettv)
8266{
8267# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8268 init_pyxversion();
8269 if (p_pyx == 2)
8270 f_pyeval(argvars, rettv);
8271 else
8272 f_py3eval(argvars, rettv);
8273# elif defined(FEAT_PYTHON)
8274 f_pyeval(argvars, rettv);
8275# elif defined(FEAT_PYTHON3)
8276 f_py3eval(argvars, rettv);
8277# endif
8278}
8279#endif
8280
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008281/*
8282 * "range()" function
8283 */
8284 static void
8285f_range(typval_T *argvars, typval_T *rettv)
8286{
8287 varnumber_T start;
8288 varnumber_T end;
8289 varnumber_T stride = 1;
8290 varnumber_T i;
8291 int error = FALSE;
8292
8293 start = get_tv_number_chk(&argvars[0], &error);
8294 if (argvars[1].v_type == VAR_UNKNOWN)
8295 {
8296 end = start - 1;
8297 start = 0;
8298 }
8299 else
8300 {
8301 end = get_tv_number_chk(&argvars[1], &error);
8302 if (argvars[2].v_type != VAR_UNKNOWN)
8303 stride = get_tv_number_chk(&argvars[2], &error);
8304 }
8305
8306 if (error)
8307 return; /* type error; errmsg already given */
8308 if (stride == 0)
8309 EMSG(_("E726: Stride is zero"));
8310 else if (stride > 0 ? end + 1 < start : end - 1 > start)
8311 EMSG(_("E727: Start past end"));
8312 else
8313 {
8314 if (rettv_list_alloc(rettv) == OK)
8315 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8316 if (list_append_number(rettv->vval.v_list,
8317 (varnumber_T)i) == FAIL)
8318 break;
8319 }
8320}
8321
8322/*
8323 * "readfile()" function
8324 */
8325 static void
8326f_readfile(typval_T *argvars, typval_T *rettv)
8327{
8328 int binary = FALSE;
8329 int failed = FALSE;
8330 char_u *fname;
8331 FILE *fd;
8332 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8333 int io_size = sizeof(buf);
8334 int readlen; /* size of last fread() */
8335 char_u *prev = NULL; /* previously read bytes, if any */
8336 long prevlen = 0; /* length of data in prev */
8337 long prevsize = 0; /* size of prev buffer */
8338 long maxline = MAXLNUM;
8339 long cnt = 0;
8340 char_u *p; /* position in buf */
8341 char_u *start; /* start of current line */
8342
8343 if (argvars[1].v_type != VAR_UNKNOWN)
8344 {
8345 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
8346 binary = TRUE;
8347 if (argvars[2].v_type != VAR_UNKNOWN)
8348 maxline = (long)get_tv_number(&argvars[2]);
8349 }
8350
8351 if (rettv_list_alloc(rettv) == FAIL)
8352 return;
8353
8354 /* Always open the file in binary mode, library functions have a mind of
8355 * their own about CR-LF conversion. */
8356 fname = get_tv_string(&argvars[0]);
8357 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8358 {
8359 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
8360 return;
8361 }
8362
8363 while (cnt < maxline || maxline < 0)
8364 {
8365 readlen = (int)fread(buf, 1, io_size, fd);
8366
8367 /* This for loop processes what was read, but is also entered at end
8368 * of file so that either:
8369 * - an incomplete line gets written
8370 * - a "binary" file gets an empty line at the end if it ends in a
8371 * newline. */
8372 for (p = buf, start = buf;
8373 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8374 ++p)
8375 {
8376 if (*p == '\n' || readlen <= 0)
8377 {
8378 listitem_T *li;
8379 char_u *s = NULL;
8380 long_u len = p - start;
8381
8382 /* Finished a line. Remove CRs before NL. */
8383 if (readlen > 0 && !binary)
8384 {
8385 while (len > 0 && start[len - 1] == '\r')
8386 --len;
8387 /* removal may cross back to the "prev" string */
8388 if (len == 0)
8389 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8390 --prevlen;
8391 }
8392 if (prevlen == 0)
8393 s = vim_strnsave(start, (int)len);
8394 else
8395 {
8396 /* Change "prev" buffer to be the right size. This way
8397 * the bytes are only copied once, and very long lines are
8398 * allocated only once. */
8399 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8400 {
8401 mch_memmove(s + prevlen, start, len);
8402 s[prevlen + len] = NUL;
8403 prev = NULL; /* the list will own the string */
8404 prevlen = prevsize = 0;
8405 }
8406 }
8407 if (s == NULL)
8408 {
8409 do_outofmem_msg((long_u) prevlen + len + 1);
8410 failed = TRUE;
8411 break;
8412 }
8413
8414 if ((li = listitem_alloc()) == NULL)
8415 {
8416 vim_free(s);
8417 failed = TRUE;
8418 break;
8419 }
8420 li->li_tv.v_type = VAR_STRING;
8421 li->li_tv.v_lock = 0;
8422 li->li_tv.vval.v_string = s;
8423 list_append(rettv->vval.v_list, li);
8424
8425 start = p + 1; /* step over newline */
8426 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
8427 break;
8428 }
8429 else if (*p == NUL)
8430 *p = '\n';
8431#ifdef FEAT_MBYTE
8432 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
8433 * when finding the BF and check the previous two bytes. */
8434 else if (*p == 0xbf && enc_utf8 && !binary)
8435 {
8436 /* Find the two bytes before the 0xbf. If p is at buf, or buf
8437 * + 1, these may be in the "prev" string. */
8438 char_u back1 = p >= buf + 1 ? p[-1]
8439 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
8440 char_u back2 = p >= buf + 2 ? p[-2]
8441 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
8442 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
8443
8444 if (back2 == 0xef && back1 == 0xbb)
8445 {
8446 char_u *dest = p - 2;
8447
8448 /* Usually a BOM is at the beginning of a file, and so at
8449 * the beginning of a line; then we can just step over it.
8450 */
8451 if (start == dest)
8452 start = p + 1;
8453 else
8454 {
8455 /* have to shuffle buf to close gap */
8456 int adjust_prevlen = 0;
8457
8458 if (dest < buf)
8459 {
8460 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
8461 dest = buf;
8462 }
8463 if (readlen > p - buf + 1)
8464 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
8465 readlen -= 3 - adjust_prevlen;
8466 prevlen -= adjust_prevlen;
8467 p = dest - 1;
8468 }
8469 }
8470 }
8471#endif
8472 } /* for */
8473
8474 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
8475 break;
8476 if (start < p)
8477 {
8478 /* There's part of a line in buf, store it in "prev". */
8479 if (p - start + prevlen >= prevsize)
8480 {
8481 /* need bigger "prev" buffer */
8482 char_u *newprev;
8483
8484 /* A common use case is ordinary text files and "prev" gets a
8485 * fragment of a line, so the first allocation is made
8486 * small, to avoid repeatedly 'allocing' large and
8487 * 'reallocing' small. */
8488 if (prevsize == 0)
8489 prevsize = (long)(p - start);
8490 else
8491 {
8492 long grow50pc = (prevsize * 3) / 2;
8493 long growmin = (long)((p - start) * 2 + prevlen);
8494 prevsize = grow50pc > growmin ? grow50pc : growmin;
8495 }
8496 newprev = prev == NULL ? alloc(prevsize)
8497 : vim_realloc(prev, prevsize);
8498 if (newprev == NULL)
8499 {
8500 do_outofmem_msg((long_u)prevsize);
8501 failed = TRUE;
8502 break;
8503 }
8504 prev = newprev;
8505 }
8506 /* Add the line part to end of "prev". */
8507 mch_memmove(prev + prevlen, start, p - start);
8508 prevlen += (long)(p - start);
8509 }
8510 } /* while */
8511
8512 /*
8513 * For a negative line count use only the lines at the end of the file,
8514 * free the rest.
8515 */
8516 if (!failed && maxline < 0)
8517 while (cnt > -maxline)
8518 {
8519 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
8520 --cnt;
8521 }
8522
8523 if (failed)
8524 {
8525 list_free(rettv->vval.v_list);
8526 /* readfile doc says an empty list is returned on error */
8527 rettv->vval.v_list = list_alloc();
8528 }
8529
8530 vim_free(prev);
8531 fclose(fd);
8532}
8533
8534#if defined(FEAT_RELTIME)
8535static int list2proftime(typval_T *arg, proftime_T *tm);
8536
8537/*
8538 * Convert a List to proftime_T.
8539 * Return FAIL when there is something wrong.
8540 */
8541 static int
8542list2proftime(typval_T *arg, proftime_T *tm)
8543{
8544 long n1, n2;
8545 int error = FALSE;
8546
8547 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
8548 || arg->vval.v_list->lv_len != 2)
8549 return FAIL;
8550 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
8551 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
8552# ifdef WIN3264
8553 tm->HighPart = n1;
8554 tm->LowPart = n2;
8555# else
8556 tm->tv_sec = n1;
8557 tm->tv_usec = n2;
8558# endif
8559 return error ? FAIL : OK;
8560}
8561#endif /* FEAT_RELTIME */
8562
8563/*
8564 * "reltime()" function
8565 */
8566 static void
8567f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8568{
8569#ifdef FEAT_RELTIME
8570 proftime_T res;
8571 proftime_T start;
8572
8573 if (argvars[0].v_type == VAR_UNKNOWN)
8574 {
8575 /* No arguments: get current time. */
8576 profile_start(&res);
8577 }
8578 else if (argvars[1].v_type == VAR_UNKNOWN)
8579 {
8580 if (list2proftime(&argvars[0], &res) == FAIL)
8581 return;
8582 profile_end(&res);
8583 }
8584 else
8585 {
8586 /* Two arguments: compute the difference. */
8587 if (list2proftime(&argvars[0], &start) == FAIL
8588 || list2proftime(&argvars[1], &res) == FAIL)
8589 return;
8590 profile_sub(&res, &start);
8591 }
8592
8593 if (rettv_list_alloc(rettv) == OK)
8594 {
8595 long n1, n2;
8596
8597# ifdef WIN3264
8598 n1 = res.HighPart;
8599 n2 = res.LowPart;
8600# else
8601 n1 = res.tv_sec;
8602 n2 = res.tv_usec;
8603# endif
8604 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
8605 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
8606 }
8607#endif
8608}
8609
8610#ifdef FEAT_FLOAT
8611/*
8612 * "reltimefloat()" function
8613 */
8614 static void
8615f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
8616{
8617# ifdef FEAT_RELTIME
8618 proftime_T tm;
8619# endif
8620
8621 rettv->v_type = VAR_FLOAT;
8622 rettv->vval.v_float = 0;
8623# ifdef FEAT_RELTIME
8624 if (list2proftime(&argvars[0], &tm) == OK)
8625 rettv->vval.v_float = profile_float(&tm);
8626# endif
8627}
8628#endif
8629
8630/*
8631 * "reltimestr()" function
8632 */
8633 static void
8634f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
8635{
8636#ifdef FEAT_RELTIME
8637 proftime_T tm;
8638#endif
8639
8640 rettv->v_type = VAR_STRING;
8641 rettv->vval.v_string = NULL;
8642#ifdef FEAT_RELTIME
8643 if (list2proftime(&argvars[0], &tm) == OK)
8644 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
8645#endif
8646}
8647
8648#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
8649static void make_connection(void);
8650static int check_connection(void);
8651
8652 static void
8653make_connection(void)
8654{
8655 if (X_DISPLAY == NULL
8656# ifdef FEAT_GUI
8657 && !gui.in_use
8658# endif
8659 )
8660 {
8661 x_force_connect = TRUE;
8662 setup_term_clip();
8663 x_force_connect = FALSE;
8664 }
8665}
8666
8667 static int
8668check_connection(void)
8669{
8670 make_connection();
8671 if (X_DISPLAY == NULL)
8672 {
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01008673 EMSG(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008674 return FAIL;
8675 }
8676 return OK;
8677}
8678#endif
8679
8680#ifdef FEAT_CLIENTSERVER
8681 static void
8682remote_common(typval_T *argvars, typval_T *rettv, int expr)
8683{
8684 char_u *server_name;
8685 char_u *keys;
8686 char_u *r = NULL;
8687 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008688 int timeout = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008689# ifdef WIN32
8690 HWND w;
8691# else
8692 Window w;
8693# endif
8694
8695 if (check_restricted() || check_secure())
8696 return;
8697
8698# ifdef FEAT_X11
8699 if (check_connection() == FAIL)
8700 return;
8701# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008702 if (argvars[2].v_type != VAR_UNKNOWN
8703 && argvars[3].v_type != VAR_UNKNOWN)
8704 timeout = get_tv_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008705
8706 server_name = get_tv_string_chk(&argvars[0]);
8707 if (server_name == NULL)
8708 return; /* type error; errmsg already given */
8709 keys = get_tv_string_buf(&argvars[1], buf);
8710# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008711 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008712# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008713 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
8714 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008715# endif
8716 {
8717 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008718 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008719 EMSG(r); /* sending worked but evaluation failed */
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008720 vim_free(r);
8721 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008722 else
8723 EMSG2(_("E241: Unable to send to %s"), server_name);
8724 return;
8725 }
8726
8727 rettv->vval.v_string = r;
8728
8729 if (argvars[2].v_type != VAR_UNKNOWN)
8730 {
8731 dictitem_T v;
8732 char_u str[30];
8733 char_u *idvar;
8734
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008735 idvar = get_tv_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008736 if (idvar != NULL && *idvar != NUL)
8737 {
8738 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
8739 v.di_tv.v_type = VAR_STRING;
8740 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008741 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008742 vim_free(v.di_tv.vval.v_string);
8743 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008744 }
8745}
8746#endif
8747
8748/*
8749 * "remote_expr()" function
8750 */
8751 static void
8752f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
8753{
8754 rettv->v_type = VAR_STRING;
8755 rettv->vval.v_string = NULL;
8756#ifdef FEAT_CLIENTSERVER
8757 remote_common(argvars, rettv, TRUE);
8758#endif
8759}
8760
8761/*
8762 * "remote_foreground()" function
8763 */
8764 static void
8765f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8766{
8767#ifdef FEAT_CLIENTSERVER
8768# ifdef WIN32
8769 /* On Win32 it's done in this application. */
8770 {
8771 char_u *server_name = get_tv_string_chk(&argvars[0]);
8772
8773 if (server_name != NULL)
8774 serverForeground(server_name);
8775 }
8776# else
8777 /* Send a foreground() expression to the server. */
8778 argvars[1].v_type = VAR_STRING;
8779 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
8780 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008781 rettv->v_type = VAR_STRING;
8782 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008783 remote_common(argvars, rettv, TRUE);
8784 vim_free(argvars[1].vval.v_string);
8785# endif
8786#endif
8787}
8788
8789 static void
8790f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
8791{
8792#ifdef FEAT_CLIENTSERVER
8793 dictitem_T v;
8794 char_u *s = NULL;
8795# ifdef WIN32
8796 long_u n = 0;
8797# endif
8798 char_u *serverid;
8799
8800 if (check_restricted() || check_secure())
8801 {
8802 rettv->vval.v_number = -1;
8803 return;
8804 }
8805 serverid = get_tv_string_chk(&argvars[0]);
8806 if (serverid == NULL)
8807 {
8808 rettv->vval.v_number = -1;
8809 return; /* type error; errmsg already given */
8810 }
8811# ifdef WIN32
8812 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
8813 if (n == 0)
8814 rettv->vval.v_number = -1;
8815 else
8816 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008817 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008818 rettv->vval.v_number = (s != NULL);
8819 }
8820# else
8821 if (check_connection() == FAIL)
8822 return;
8823
8824 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
8825 serverStrToWin(serverid), &s);
8826# endif
8827
8828 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
8829 {
8830 char_u *retvar;
8831
8832 v.di_tv.v_type = VAR_STRING;
8833 v.di_tv.vval.v_string = vim_strsave(s);
8834 retvar = get_tv_string_chk(&argvars[1]);
8835 if (retvar != NULL)
8836 set_var(retvar, &v.di_tv, FALSE);
8837 vim_free(v.di_tv.vval.v_string);
8838 }
8839#else
8840 rettv->vval.v_number = -1;
8841#endif
8842}
8843
8844 static void
8845f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
8846{
8847 char_u *r = NULL;
8848
8849#ifdef FEAT_CLIENTSERVER
8850 char_u *serverid = get_tv_string_chk(&argvars[0]);
8851
8852 if (serverid != NULL && !check_restricted() && !check_secure())
8853 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008854 int timeout = 0;
Bram Moolenaar1662ce12017-03-19 21:47:50 +01008855# ifdef WIN32
8856 /* The server's HWND is encoded in the 'id' parameter */
8857 long_u n = 0;
8858# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008859
8860 if (argvars[1].v_type != VAR_UNKNOWN)
8861 timeout = get_tv_number(&argvars[1]);
8862
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008863# ifdef WIN32
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008864 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
8865 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008866 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008867 if (r == NULL)
8868# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008869 if (check_connection() == FAIL
8870 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
8871 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008872# endif
8873 EMSG(_("E277: Unable to read a server reply"));
8874 }
8875#endif
8876 rettv->v_type = VAR_STRING;
8877 rettv->vval.v_string = r;
8878}
8879
8880/*
8881 * "remote_send()" function
8882 */
8883 static void
8884f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
8885{
8886 rettv->v_type = VAR_STRING;
8887 rettv->vval.v_string = NULL;
8888#ifdef FEAT_CLIENTSERVER
8889 remote_common(argvars, rettv, FALSE);
8890#endif
8891}
8892
8893/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01008894 * "remote_startserver()" function
8895 */
8896 static void
8897f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8898{
8899#ifdef FEAT_CLIENTSERVER
8900 char_u *server = get_tv_string_chk(&argvars[0]);
8901
8902 if (server == NULL)
8903 return; /* type error; errmsg already given */
8904 if (serverName != NULL)
8905 EMSG(_("E941: already started a server"));
8906 else
8907 {
8908# ifdef FEAT_X11
8909 if (check_connection() == OK)
8910 serverRegisterName(X_DISPLAY, server);
8911# else
8912 serverSetName(server);
8913# endif
8914 }
8915#else
8916 EMSG(_("E942: +clientserver feature not available"));
8917#endif
8918}
8919
8920/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008921 * "remove()" function
8922 */
8923 static void
8924f_remove(typval_T *argvars, typval_T *rettv)
8925{
8926 list_T *l;
8927 listitem_T *item, *item2;
8928 listitem_T *li;
8929 long idx;
8930 long end;
8931 char_u *key;
8932 dict_T *d;
8933 dictitem_T *di;
8934 char_u *arg_errmsg = (char_u *)N_("remove() argument");
8935
8936 if (argvars[0].v_type == VAR_DICT)
8937 {
8938 if (argvars[2].v_type != VAR_UNKNOWN)
8939 EMSG2(_(e_toomanyarg), "remove()");
8940 else if ((d = argvars[0].vval.v_dict) != NULL
8941 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
8942 {
8943 key = get_tv_string_chk(&argvars[1]);
8944 if (key != NULL)
8945 {
8946 di = dict_find(d, key, -1);
8947 if (di == NULL)
8948 EMSG2(_(e_dictkey), key);
8949 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
8950 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
8951 {
8952 *rettv = di->di_tv;
8953 init_tv(&di->di_tv);
8954 dictitem_remove(d, di);
8955 }
8956 }
8957 }
8958 }
8959 else if (argvars[0].v_type != VAR_LIST)
8960 EMSG2(_(e_listdictarg), "remove()");
8961 else if ((l = argvars[0].vval.v_list) != NULL
8962 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
8963 {
8964 int error = FALSE;
8965
8966 idx = (long)get_tv_number_chk(&argvars[1], &error);
8967 if (error)
8968 ; /* type error: do nothing, errmsg already given */
8969 else if ((item = list_find(l, idx)) == NULL)
8970 EMSGN(_(e_listidx), idx);
8971 else
8972 {
8973 if (argvars[2].v_type == VAR_UNKNOWN)
8974 {
8975 /* Remove one item, return its value. */
8976 vimlist_remove(l, item, item);
8977 *rettv = item->li_tv;
8978 vim_free(item);
8979 }
8980 else
8981 {
8982 /* Remove range of items, return list with values. */
8983 end = (long)get_tv_number_chk(&argvars[2], &error);
8984 if (error)
8985 ; /* type error: do nothing */
8986 else if ((item2 = list_find(l, end)) == NULL)
8987 EMSGN(_(e_listidx), end);
8988 else
8989 {
8990 int cnt = 0;
8991
8992 for (li = item; li != NULL; li = li->li_next)
8993 {
8994 ++cnt;
8995 if (li == item2)
8996 break;
8997 }
8998 if (li == NULL) /* didn't find "item2" after "item" */
8999 EMSG(_(e_invrange));
9000 else
9001 {
9002 vimlist_remove(l, item, item2);
9003 if (rettv_list_alloc(rettv) == OK)
9004 {
9005 l = rettv->vval.v_list;
9006 l->lv_first = item;
9007 l->lv_last = item2;
9008 item->li_prev = NULL;
9009 item2->li_next = NULL;
9010 l->lv_len = cnt;
9011 }
9012 }
9013 }
9014 }
9015 }
9016 }
9017}
9018
9019/*
9020 * "rename({from}, {to})" function
9021 */
9022 static void
9023f_rename(typval_T *argvars, typval_T *rettv)
9024{
9025 char_u buf[NUMBUFLEN];
9026
9027 if (check_restricted() || check_secure())
9028 rettv->vval.v_number = -1;
9029 else
9030 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
9031 get_tv_string_buf(&argvars[1], buf));
9032}
9033
9034/*
9035 * "repeat()" function
9036 */
9037 static void
9038f_repeat(typval_T *argvars, typval_T *rettv)
9039{
9040 char_u *p;
9041 int n;
9042 int slen;
9043 int len;
9044 char_u *r;
9045 int i;
9046
9047 n = (int)get_tv_number(&argvars[1]);
9048 if (argvars[0].v_type == VAR_LIST)
9049 {
9050 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9051 while (n-- > 0)
9052 if (list_extend(rettv->vval.v_list,
9053 argvars[0].vval.v_list, NULL) == FAIL)
9054 break;
9055 }
9056 else
9057 {
9058 p = get_tv_string(&argvars[0]);
9059 rettv->v_type = VAR_STRING;
9060 rettv->vval.v_string = NULL;
9061
9062 slen = (int)STRLEN(p);
9063 len = slen * n;
9064 if (len <= 0)
9065 return;
9066
9067 r = alloc(len + 1);
9068 if (r != NULL)
9069 {
9070 for (i = 0; i < n; i++)
9071 mch_memmove(r + i * slen, p, (size_t)slen);
9072 r[len] = NUL;
9073 }
9074
9075 rettv->vval.v_string = r;
9076 }
9077}
9078
9079/*
9080 * "resolve()" function
9081 */
9082 static void
9083f_resolve(typval_T *argvars, typval_T *rettv)
9084{
9085 char_u *p;
9086#ifdef HAVE_READLINK
9087 char_u *buf = NULL;
9088#endif
9089
9090 p = get_tv_string(&argvars[0]);
9091#ifdef FEAT_SHORTCUT
9092 {
9093 char_u *v = NULL;
9094
9095 v = mch_resolve_shortcut(p);
9096 if (v != NULL)
9097 rettv->vval.v_string = v;
9098 else
9099 rettv->vval.v_string = vim_strsave(p);
9100 }
9101#else
9102# ifdef HAVE_READLINK
9103 {
9104 char_u *cpy;
9105 int len;
9106 char_u *remain = NULL;
9107 char_u *q;
9108 int is_relative_to_current = FALSE;
9109 int has_trailing_pathsep = FALSE;
9110 int limit = 100;
9111
9112 p = vim_strsave(p);
9113
9114 if (p[0] == '.' && (vim_ispathsep(p[1])
9115 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9116 is_relative_to_current = TRUE;
9117
9118 len = STRLEN(p);
9119 if (len > 0 && after_pathsep(p, p + len))
9120 {
9121 has_trailing_pathsep = TRUE;
9122 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9123 }
9124
9125 q = getnextcomp(p);
9126 if (*q != NUL)
9127 {
9128 /* Separate the first path component in "p", and keep the
9129 * remainder (beginning with the path separator). */
9130 remain = vim_strsave(q - 1);
9131 q[-1] = NUL;
9132 }
9133
9134 buf = alloc(MAXPATHL + 1);
9135 if (buf == NULL)
9136 goto fail;
9137
9138 for (;;)
9139 {
9140 for (;;)
9141 {
9142 len = readlink((char *)p, (char *)buf, MAXPATHL);
9143 if (len <= 0)
9144 break;
9145 buf[len] = NUL;
9146
9147 if (limit-- == 0)
9148 {
9149 vim_free(p);
9150 vim_free(remain);
9151 EMSG(_("E655: Too many symbolic links (cycle?)"));
9152 rettv->vval.v_string = NULL;
9153 goto fail;
9154 }
9155
9156 /* Ensure that the result will have a trailing path separator
9157 * if the argument has one. */
9158 if (remain == NULL && has_trailing_pathsep)
9159 add_pathsep(buf);
9160
9161 /* Separate the first path component in the link value and
9162 * concatenate the remainders. */
9163 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9164 if (*q != NUL)
9165 {
9166 if (remain == NULL)
9167 remain = vim_strsave(q - 1);
9168 else
9169 {
9170 cpy = concat_str(q - 1, remain);
9171 if (cpy != NULL)
9172 {
9173 vim_free(remain);
9174 remain = cpy;
9175 }
9176 }
9177 q[-1] = NUL;
9178 }
9179
9180 q = gettail(p);
9181 if (q > p && *q == NUL)
9182 {
9183 /* Ignore trailing path separator. */
9184 q[-1] = NUL;
9185 q = gettail(p);
9186 }
9187 if (q > p && !mch_isFullName(buf))
9188 {
9189 /* symlink is relative to directory of argument */
9190 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
9191 if (cpy != NULL)
9192 {
9193 STRCPY(cpy, p);
9194 STRCPY(gettail(cpy), buf);
9195 vim_free(p);
9196 p = cpy;
9197 }
9198 }
9199 else
9200 {
9201 vim_free(p);
9202 p = vim_strsave(buf);
9203 }
9204 }
9205
9206 if (remain == NULL)
9207 break;
9208
9209 /* Append the first path component of "remain" to "p". */
9210 q = getnextcomp(remain + 1);
9211 len = q - remain - (*q != NUL);
9212 cpy = vim_strnsave(p, STRLEN(p) + len);
9213 if (cpy != NULL)
9214 {
9215 STRNCAT(cpy, remain, len);
9216 vim_free(p);
9217 p = cpy;
9218 }
9219 /* Shorten "remain". */
9220 if (*q != NUL)
9221 STRMOVE(remain, q - 1);
9222 else
9223 {
9224 vim_free(remain);
9225 remain = NULL;
9226 }
9227 }
9228
9229 /* If the result is a relative path name, make it explicitly relative to
9230 * the current directory if and only if the argument had this form. */
9231 if (!vim_ispathsep(*p))
9232 {
9233 if (is_relative_to_current
9234 && *p != NUL
9235 && !(p[0] == '.'
9236 && (p[1] == NUL
9237 || vim_ispathsep(p[1])
9238 || (p[1] == '.'
9239 && (p[2] == NUL
9240 || vim_ispathsep(p[2]))))))
9241 {
9242 /* Prepend "./". */
9243 cpy = concat_str((char_u *)"./", p);
9244 if (cpy != NULL)
9245 {
9246 vim_free(p);
9247 p = cpy;
9248 }
9249 }
9250 else if (!is_relative_to_current)
9251 {
9252 /* Strip leading "./". */
9253 q = p;
9254 while (q[0] == '.' && vim_ispathsep(q[1]))
9255 q += 2;
9256 if (q > p)
9257 STRMOVE(p, p + 2);
9258 }
9259 }
9260
9261 /* Ensure that the result will have no trailing path separator
9262 * if the argument had none. But keep "/" or "//". */
9263 if (!has_trailing_pathsep)
9264 {
9265 q = p + STRLEN(p);
9266 if (after_pathsep(p, q))
9267 *gettail_sep(p) = NUL;
9268 }
9269
9270 rettv->vval.v_string = p;
9271 }
9272# else
9273 rettv->vval.v_string = vim_strsave(p);
9274# endif
9275#endif
9276
9277 simplify_filename(rettv->vval.v_string);
9278
9279#ifdef HAVE_READLINK
9280fail:
9281 vim_free(buf);
9282#endif
9283 rettv->v_type = VAR_STRING;
9284}
9285
9286/*
9287 * "reverse({list})" function
9288 */
9289 static void
9290f_reverse(typval_T *argvars, typval_T *rettv)
9291{
9292 list_T *l;
9293 listitem_T *li, *ni;
9294
9295 if (argvars[0].v_type != VAR_LIST)
9296 EMSG2(_(e_listarg), "reverse()");
9297 else if ((l = argvars[0].vval.v_list) != NULL
9298 && !tv_check_lock(l->lv_lock,
9299 (char_u *)N_("reverse() argument"), TRUE))
9300 {
9301 li = l->lv_last;
9302 l->lv_first = l->lv_last = NULL;
9303 l->lv_len = 0;
9304 while (li != NULL)
9305 {
9306 ni = li->li_prev;
9307 list_append(l, li);
9308 li = ni;
9309 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009310 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009311 l->lv_idx = l->lv_len - l->lv_idx - 1;
9312 }
9313}
9314
9315#define SP_NOMOVE 0x01 /* don't move cursor */
9316#define SP_REPEAT 0x02 /* repeat to find outer pair */
9317#define SP_RETCOUNT 0x04 /* return matchcount */
9318#define SP_SETPCMARK 0x08 /* set previous context mark */
9319#define SP_START 0x10 /* accept match at start position */
9320#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9321#define SP_END 0x40 /* leave cursor at end of match */
9322#define SP_COLUMN 0x80 /* start at cursor column */
9323
9324static int get_search_arg(typval_T *varp, int *flagsp);
9325
9326/*
9327 * Get flags for a search function.
9328 * Possibly sets "p_ws".
9329 * Returns BACKWARD, FORWARD or zero (for an error).
9330 */
9331 static int
9332get_search_arg(typval_T *varp, int *flagsp)
9333{
9334 int dir = FORWARD;
9335 char_u *flags;
9336 char_u nbuf[NUMBUFLEN];
9337 int mask;
9338
9339 if (varp->v_type != VAR_UNKNOWN)
9340 {
9341 flags = get_tv_string_buf_chk(varp, nbuf);
9342 if (flags == NULL)
9343 return 0; /* type error; errmsg already given */
9344 while (*flags != NUL)
9345 {
9346 switch (*flags)
9347 {
9348 case 'b': dir = BACKWARD; break;
9349 case 'w': p_ws = TRUE; break;
9350 case 'W': p_ws = FALSE; break;
9351 default: mask = 0;
9352 if (flagsp != NULL)
9353 switch (*flags)
9354 {
9355 case 'c': mask = SP_START; break;
9356 case 'e': mask = SP_END; break;
9357 case 'm': mask = SP_RETCOUNT; break;
9358 case 'n': mask = SP_NOMOVE; break;
9359 case 'p': mask = SP_SUBPAT; break;
9360 case 'r': mask = SP_REPEAT; break;
9361 case 's': mask = SP_SETPCMARK; break;
9362 case 'z': mask = SP_COLUMN; break;
9363 }
9364 if (mask == 0)
9365 {
9366 EMSG2(_(e_invarg2), flags);
9367 dir = 0;
9368 }
9369 else
9370 *flagsp |= mask;
9371 }
9372 if (dir == 0)
9373 break;
9374 ++flags;
9375 }
9376 }
9377 return dir;
9378}
9379
9380/*
9381 * Shared by search() and searchpos() functions.
9382 */
9383 static int
9384search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9385{
9386 int flags;
9387 char_u *pat;
9388 pos_T pos;
9389 pos_T save_cursor;
9390 int save_p_ws = p_ws;
9391 int dir;
9392 int retval = 0; /* default: FAIL */
9393 long lnum_stop = 0;
9394 proftime_T tm;
9395#ifdef FEAT_RELTIME
9396 long time_limit = 0;
9397#endif
9398 int options = SEARCH_KEEP;
9399 int subpatnum;
9400
9401 pat = get_tv_string(&argvars[0]);
9402 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
9403 if (dir == 0)
9404 goto theend;
9405 flags = *flagsp;
9406 if (flags & SP_START)
9407 options |= SEARCH_START;
9408 if (flags & SP_END)
9409 options |= SEARCH_END;
9410 if (flags & SP_COLUMN)
9411 options |= SEARCH_COL;
9412
9413 /* Optional arguments: line number to stop searching and timeout. */
9414 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9415 {
9416 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
9417 if (lnum_stop < 0)
9418 goto theend;
9419#ifdef FEAT_RELTIME
9420 if (argvars[3].v_type != VAR_UNKNOWN)
9421 {
9422 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
9423 if (time_limit < 0)
9424 goto theend;
9425 }
9426#endif
9427 }
9428
9429#ifdef FEAT_RELTIME
9430 /* Set the time limit, if there is one. */
9431 profile_setlimit(time_limit, &tm);
9432#endif
9433
9434 /*
9435 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
9436 * Check to make sure only those flags are set.
9437 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
9438 * flags cannot be set. Check for that condition also.
9439 */
9440 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
9441 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9442 {
9443 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
9444 goto theend;
9445 }
9446
9447 pos = save_cursor = curwin->w_cursor;
9448 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009449 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009450 if (subpatnum != FAIL)
9451 {
9452 if (flags & SP_SUBPAT)
9453 retval = subpatnum;
9454 else
9455 retval = pos.lnum;
9456 if (flags & SP_SETPCMARK)
9457 setpcmark();
9458 curwin->w_cursor = pos;
9459 if (match_pos != NULL)
9460 {
9461 /* Store the match cursor position */
9462 match_pos->lnum = pos.lnum;
9463 match_pos->col = pos.col + 1;
9464 }
9465 /* "/$" will put the cursor after the end of the line, may need to
9466 * correct that here */
9467 check_cursor();
9468 }
9469
9470 /* If 'n' flag is used: restore cursor position. */
9471 if (flags & SP_NOMOVE)
9472 curwin->w_cursor = save_cursor;
9473 else
9474 curwin->w_set_curswant = TRUE;
9475theend:
9476 p_ws = save_p_ws;
9477
9478 return retval;
9479}
9480
9481#ifdef FEAT_FLOAT
9482
9483/*
9484 * round() is not in C90, use ceil() or floor() instead.
9485 */
9486 float_T
9487vim_round(float_T f)
9488{
9489 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
9490}
9491
9492/*
9493 * "round({float})" function
9494 */
9495 static void
9496f_round(typval_T *argvars, typval_T *rettv)
9497{
9498 float_T f = 0.0;
9499
9500 rettv->v_type = VAR_FLOAT;
9501 if (get_float_arg(argvars, &f) == OK)
9502 rettv->vval.v_float = vim_round(f);
9503 else
9504 rettv->vval.v_float = 0.0;
9505}
9506#endif
9507
9508/*
9509 * "screenattr()" function
9510 */
9511 static void
9512f_screenattr(typval_T *argvars, typval_T *rettv)
9513{
9514 int row;
9515 int col;
9516 int c;
9517
9518 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9519 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9520 if (row < 0 || row >= screen_Rows
9521 || col < 0 || col >= screen_Columns)
9522 c = -1;
9523 else
9524 c = ScreenAttrs[LineOffset[row] + col];
9525 rettv->vval.v_number = c;
9526}
9527
9528/*
9529 * "screenchar()" function
9530 */
9531 static void
9532f_screenchar(typval_T *argvars, typval_T *rettv)
9533{
9534 int row;
9535 int col;
9536 int off;
9537 int c;
9538
9539 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9540 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9541 if (row < 0 || row >= screen_Rows
9542 || col < 0 || col >= screen_Columns)
9543 c = -1;
9544 else
9545 {
9546 off = LineOffset[row] + col;
9547#ifdef FEAT_MBYTE
9548 if (enc_utf8 && ScreenLinesUC[off] != 0)
9549 c = ScreenLinesUC[off];
9550 else
9551#endif
9552 c = ScreenLines[off];
9553 }
9554 rettv->vval.v_number = c;
9555}
9556
9557/*
9558 * "screencol()" function
9559 *
9560 * First column is 1 to be consistent with virtcol().
9561 */
9562 static void
9563f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
9564{
9565 rettv->vval.v_number = screen_screencol() + 1;
9566}
9567
9568/*
9569 * "screenrow()" function
9570 */
9571 static void
9572f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
9573{
9574 rettv->vval.v_number = screen_screenrow() + 1;
9575}
9576
9577/*
9578 * "search()" function
9579 */
9580 static void
9581f_search(typval_T *argvars, typval_T *rettv)
9582{
9583 int flags = 0;
9584
9585 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
9586}
9587
9588/*
9589 * "searchdecl()" function
9590 */
9591 static void
9592f_searchdecl(typval_T *argvars, typval_T *rettv)
9593{
9594 int locally = 1;
9595 int thisblock = 0;
9596 int error = FALSE;
9597 char_u *name;
9598
9599 rettv->vval.v_number = 1; /* default: FAIL */
9600
9601 name = get_tv_string_chk(&argvars[0]);
9602 if (argvars[1].v_type != VAR_UNKNOWN)
9603 {
9604 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
9605 if (!error && argvars[2].v_type != VAR_UNKNOWN)
9606 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
9607 }
9608 if (!error && name != NULL)
9609 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
9610 locally, thisblock, SEARCH_KEEP) == FAIL;
9611}
9612
9613/*
9614 * Used by searchpair() and searchpairpos()
9615 */
9616 static int
9617searchpair_cmn(typval_T *argvars, pos_T *match_pos)
9618{
9619 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01009620 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009621 int save_p_ws = p_ws;
9622 int dir;
9623 int flags = 0;
9624 char_u nbuf1[NUMBUFLEN];
9625 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009626 int retval = 0; /* default: FAIL */
9627 long lnum_stop = 0;
9628 long time_limit = 0;
9629
9630 /* Get the three pattern arguments: start, middle, end. */
9631 spat = get_tv_string_chk(&argvars[0]);
9632 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
9633 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
9634 if (spat == NULL || mpat == NULL || epat == NULL)
9635 goto theend; /* type error */
9636
9637 /* Handle the optional fourth argument: flags */
9638 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
9639 if (dir == 0)
9640 goto theend;
9641
9642 /* Don't accept SP_END or SP_SUBPAT.
9643 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
9644 */
9645 if ((flags & (SP_END | SP_SUBPAT)) != 0
9646 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9647 {
9648 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
9649 goto theend;
9650 }
9651
9652 /* Using 'r' implies 'W', otherwise it doesn't work. */
9653 if (flags & SP_REPEAT)
9654 p_ws = FALSE;
9655
9656 /* Optional fifth argument: skip expression */
9657 if (argvars[3].v_type == VAR_UNKNOWN
9658 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01009659 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009660 else
9661 {
Bram Moolenaar48570482017-10-30 21:48:41 +01009662 skip = &argvars[4];
9663 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
9664 && skip->v_type != VAR_STRING)
9665 {
9666 /* Type error */
9667 goto theend;
9668 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009669 if (argvars[5].v_type != VAR_UNKNOWN)
9670 {
9671 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
9672 if (lnum_stop < 0)
9673 goto theend;
9674#ifdef FEAT_RELTIME
9675 if (argvars[6].v_type != VAR_UNKNOWN)
9676 {
9677 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
9678 if (time_limit < 0)
9679 goto theend;
9680 }
9681#endif
9682 }
9683 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009684
9685 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
9686 match_pos, lnum_stop, time_limit);
9687
9688theend:
9689 p_ws = save_p_ws;
9690
9691 return retval;
9692}
9693
9694/*
9695 * "searchpair()" function
9696 */
9697 static void
9698f_searchpair(typval_T *argvars, typval_T *rettv)
9699{
9700 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
9701}
9702
9703/*
9704 * "searchpairpos()" function
9705 */
9706 static void
9707f_searchpairpos(typval_T *argvars, typval_T *rettv)
9708{
9709 pos_T match_pos;
9710 int lnum = 0;
9711 int col = 0;
9712
9713 if (rettv_list_alloc(rettv) == FAIL)
9714 return;
9715
9716 if (searchpair_cmn(argvars, &match_pos) > 0)
9717 {
9718 lnum = match_pos.lnum;
9719 col = match_pos.col;
9720 }
9721
9722 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9723 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9724}
9725
9726/*
9727 * Search for a start/middle/end thing.
9728 * Used by searchpair(), see its documentation for the details.
9729 * Returns 0 or -1 for no match,
9730 */
9731 long
9732do_searchpair(
9733 char_u *spat, /* start pattern */
9734 char_u *mpat, /* middle pattern */
9735 char_u *epat, /* end pattern */
9736 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +01009737 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009738 int flags, /* SP_SETPCMARK and other SP_ values */
9739 pos_T *match_pos,
9740 linenr_T lnum_stop, /* stop at this line if not zero */
9741 long time_limit UNUSED) /* stop after this many msec */
9742{
9743 char_u *save_cpo;
9744 char_u *pat, *pat2 = NULL, *pat3 = NULL;
9745 long retval = 0;
9746 pos_T pos;
9747 pos_T firstpos;
9748 pos_T foundpos;
9749 pos_T save_cursor;
9750 pos_T save_pos;
9751 int n;
9752 int r;
9753 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01009754 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009755 int err;
9756 int options = SEARCH_KEEP;
9757 proftime_T tm;
9758
9759 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
9760 save_cpo = p_cpo;
9761 p_cpo = empty_option;
9762
9763#ifdef FEAT_RELTIME
9764 /* Set the time limit, if there is one. */
9765 profile_setlimit(time_limit, &tm);
9766#endif
9767
9768 /* Make two search patterns: start/end (pat2, for in nested pairs) and
9769 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009770 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
9771 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009772 if (pat2 == NULL || pat3 == NULL)
9773 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009774 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009775 if (*mpat == NUL)
9776 STRCPY(pat3, pat2);
9777 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009778 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009779 spat, epat, mpat);
9780 if (flags & SP_START)
9781 options |= SEARCH_START;
9782
Bram Moolenaar48570482017-10-30 21:48:41 +01009783 if (skip != NULL)
9784 {
9785 /* Empty string means to not use the skip expression. */
9786 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
9787 use_skip = skip->vval.v_string != NULL
9788 && *skip->vval.v_string != NUL;
9789 }
9790
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009791 save_cursor = curwin->w_cursor;
9792 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009793 CLEAR_POS(&firstpos);
9794 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009795 pat = pat3;
9796 for (;;)
9797 {
9798 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009799 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009800 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009801 /* didn't find it or found the first match again: FAIL */
9802 break;
9803
9804 if (firstpos.lnum == 0)
9805 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009806 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009807 {
9808 /* Found the same position again. Can happen with a pattern that
9809 * has "\zs" at the end and searching backwards. Advance one
9810 * character and try again. */
9811 if (dir == BACKWARD)
9812 decl(&pos);
9813 else
9814 incl(&pos);
9815 }
9816 foundpos = pos;
9817
9818 /* clear the start flag to avoid getting stuck here */
9819 options &= ~SEARCH_START;
9820
9821 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +01009822 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009823 {
9824 save_pos = curwin->w_cursor;
9825 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01009826 err = FALSE;
9827 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009828 curwin->w_cursor = save_pos;
9829 if (err)
9830 {
9831 /* Evaluating {skip} caused an error, break here. */
9832 curwin->w_cursor = save_cursor;
9833 retval = -1;
9834 break;
9835 }
9836 if (r)
9837 continue;
9838 }
9839
9840 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
9841 {
9842 /* Found end when searching backwards or start when searching
9843 * forward: nested pair. */
9844 ++nest;
9845 pat = pat2; /* nested, don't search for middle */
9846 }
9847 else
9848 {
9849 /* Found end when searching forward or start when searching
9850 * backward: end of (nested) pair; or found middle in outer pair. */
9851 if (--nest == 1)
9852 pat = pat3; /* outer level, search for middle */
9853 }
9854
9855 if (nest == 0)
9856 {
9857 /* Found the match: return matchcount or line number. */
9858 if (flags & SP_RETCOUNT)
9859 ++retval;
9860 else
9861 retval = pos.lnum;
9862 if (flags & SP_SETPCMARK)
9863 setpcmark();
9864 curwin->w_cursor = pos;
9865 if (!(flags & SP_REPEAT))
9866 break;
9867 nest = 1; /* search for next unmatched */
9868 }
9869 }
9870
9871 if (match_pos != NULL)
9872 {
9873 /* Store the match cursor position */
9874 match_pos->lnum = curwin->w_cursor.lnum;
9875 match_pos->col = curwin->w_cursor.col + 1;
9876 }
9877
9878 /* If 'n' flag is used or search failed: restore cursor position. */
9879 if ((flags & SP_NOMOVE) || retval == 0)
9880 curwin->w_cursor = save_cursor;
9881
9882theend:
9883 vim_free(pat2);
9884 vim_free(pat3);
9885 if (p_cpo == empty_option)
9886 p_cpo = save_cpo;
9887 else
9888 /* Darn, evaluating the {skip} expression changed the value. */
9889 free_string_option(save_cpo);
9890
9891 return retval;
9892}
9893
9894/*
9895 * "searchpos()" function
9896 */
9897 static void
9898f_searchpos(typval_T *argvars, typval_T *rettv)
9899{
9900 pos_T match_pos;
9901 int lnum = 0;
9902 int col = 0;
9903 int n;
9904 int flags = 0;
9905
9906 if (rettv_list_alloc(rettv) == FAIL)
9907 return;
9908
9909 n = search_cmn(argvars, &match_pos, &flags);
9910 if (n > 0)
9911 {
9912 lnum = match_pos.lnum;
9913 col = match_pos.col;
9914 }
9915
9916 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9917 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9918 if (flags & SP_SUBPAT)
9919 list_append_number(rettv->vval.v_list, (varnumber_T)n);
9920}
9921
9922 static void
9923f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
9924{
9925#ifdef FEAT_CLIENTSERVER
9926 char_u buf[NUMBUFLEN];
9927 char_u *server = get_tv_string_chk(&argvars[0]);
9928 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
9929
9930 rettv->vval.v_number = -1;
9931 if (server == NULL || reply == NULL)
9932 return;
9933 if (check_restricted() || check_secure())
9934 return;
9935# ifdef FEAT_X11
9936 if (check_connection() == FAIL)
9937 return;
9938# endif
9939
9940 if (serverSendReply(server, reply) < 0)
9941 {
9942 EMSG(_("E258: Unable to send to client"));
9943 return;
9944 }
9945 rettv->vval.v_number = 0;
9946#else
9947 rettv->vval.v_number = -1;
9948#endif
9949}
9950
9951 static void
9952f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
9953{
9954 char_u *r = NULL;
9955
9956#ifdef FEAT_CLIENTSERVER
9957# ifdef WIN32
9958 r = serverGetVimNames();
9959# else
9960 make_connection();
9961 if (X_DISPLAY != NULL)
9962 r = serverGetVimNames(X_DISPLAY);
9963# endif
9964#endif
9965 rettv->v_type = VAR_STRING;
9966 rettv->vval.v_string = r;
9967}
9968
9969/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +02009970 * Set line or list of lines in buffer "buf".
9971 */
9972 static void
9973set_buffer_lines(buf_T *buf, linenr_T lnum, typval_T *lines, typval_T *rettv)
9974{
9975 char_u *line = NULL;
9976 list_T *l = NULL;
9977 listitem_T *li = NULL;
9978 long added = 0;
9979 linenr_T lcount;
Bram Moolenaar0c4dc882017-11-06 21:32:54 +01009980 buf_T *curbuf_save = NULL;
9981 win_T *curwin_save = NULL;
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +02009982 int is_curbuf = buf == curbuf;
9983
Bram Moolenaar9d954202017-09-04 20:34:19 +02009984 /* When using the current buffer ml_mfp will be set if needed. Useful when
9985 * setline() is used on startup. For other buffers the buffer must be
9986 * loaded. */
9987 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +02009988 {
9989 rettv->vval.v_number = 1; /* FAIL */
9990 return;
9991 }
9992
Bram Moolenaar0c4dc882017-11-06 21:32:54 +01009993 if (!is_curbuf)
9994 {
9995 wininfo_T *wip;
9996
9997 curbuf_save = curbuf;
9998 curwin_save = curwin;
9999 curbuf = buf;
10000 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
10001 {
10002 if (wip->wi_win != NULL)
10003 {
10004 curwin = wip->wi_win;
10005 break;
10006 }
10007 }
10008 }
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010009
10010 lcount = curbuf->b_ml.ml_line_count;
10011
10012 if (lines->v_type == VAR_LIST)
10013 {
10014 l = lines->vval.v_list;
10015 li = l->lv_first;
10016 }
10017 else
10018 line = get_tv_string_chk(lines);
10019
10020 /* default result is zero == OK */
10021 for (;;)
10022 {
10023 if (l != NULL)
10024 {
10025 /* list argument, get next string */
10026 if (li == NULL)
10027 break;
10028 line = get_tv_string_chk(&li->li_tv);
10029 li = li->li_next;
10030 }
10031
10032 rettv->vval.v_number = 1; /* FAIL */
10033 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
10034 break;
10035
10036 /* When coming here from Insert mode, sync undo, so that this can be
10037 * undone separately from what was previously inserted. */
10038 if (u_sync_once == 2)
10039 {
10040 u_sync_once = 1; /* notify that u_sync() was called */
10041 u_sync(TRUE);
10042 }
10043
10044 if (lnum <= curbuf->b_ml.ml_line_count)
10045 {
10046 /* existing line, replace it */
10047 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
10048 {
10049 changed_bytes(lnum, 0);
10050 if (is_curbuf && lnum == curwin->w_cursor.lnum)
10051 check_cursor_col();
10052 rettv->vval.v_number = 0; /* OK */
10053 }
10054 }
10055 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
10056 {
10057 /* lnum is one past the last line, append the line */
10058 ++added;
10059 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
10060 rettv->vval.v_number = 0; /* OK */
10061 }
10062
10063 if (l == NULL) /* only one string argument */
10064 break;
10065 ++lnum;
10066 }
10067
10068 if (added > 0)
10069 appended_lines_mark(lcount, added);
10070
Bram Moolenaar0c4dc882017-11-06 21:32:54 +010010071 if (!is_curbuf)
10072 {
10073 curbuf = curbuf_save;
10074 curwin = curwin_save;
10075 }
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010076}
10077
10078/*
10079 * "setbufline()" function
10080 */
10081 static void
10082f_setbufline(argvars, rettv)
10083 typval_T *argvars;
10084 typval_T *rettv;
10085{
10086 linenr_T lnum;
10087 buf_T *buf;
10088
10089 buf = get_buf_tv(&argvars[0], FALSE);
10090 if (buf == NULL)
10091 rettv->vval.v_number = 1; /* FAIL */
10092 else
10093 {
10094 lnum = get_tv_lnum_buf(&argvars[1], buf);
10095
10096 set_buffer_lines(buf, lnum, &argvars[2], rettv);
10097 }
10098}
10099
10100/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010101 * "setbufvar()" function
10102 */
10103 static void
10104f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10105{
10106 buf_T *buf;
10107 char_u *varname, *bufvarname;
10108 typval_T *varp;
10109 char_u nbuf[NUMBUFLEN];
10110
10111 if (check_restricted() || check_secure())
10112 return;
10113 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10114 varname = get_tv_string_chk(&argvars[1]);
10115 buf = get_buf_tv(&argvars[0], FALSE);
10116 varp = &argvars[2];
10117
10118 if (buf != NULL && varname != NULL && varp != NULL)
10119 {
10120 if (*varname == '&')
10121 {
10122 long numval;
10123 char_u *strval;
10124 int error = FALSE;
10125 aco_save_T aco;
10126
10127 /* set curbuf to be our buf, temporarily */
10128 aucmd_prepbuf(&aco, buf);
10129
10130 ++varname;
10131 numval = (long)get_tv_number_chk(varp, &error);
10132 strval = get_tv_string_buf_chk(varp, nbuf);
10133 if (!error && strval != NULL)
10134 set_option_value(varname, numval, strval, OPT_LOCAL);
10135
10136 /* reset notion of buffer */
10137 aucmd_restbuf(&aco);
10138 }
10139 else
10140 {
10141 buf_T *save_curbuf = curbuf;
10142
10143 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
10144 if (bufvarname != NULL)
10145 {
10146 curbuf = buf;
10147 STRCPY(bufvarname, "b:");
10148 STRCPY(bufvarname + 2, varname);
10149 set_var(bufvarname, varp, TRUE);
10150 vim_free(bufvarname);
10151 curbuf = save_curbuf;
10152 }
10153 }
10154 }
10155}
10156
10157 static void
10158f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10159{
10160 dict_T *d;
10161 dictitem_T *di;
10162 char_u *csearch;
10163
10164 if (argvars[0].v_type != VAR_DICT)
10165 {
10166 EMSG(_(e_dictreq));
10167 return;
10168 }
10169
10170 if ((d = argvars[0].vval.v_dict) != NULL)
10171 {
10172 csearch = get_dict_string(d, (char_u *)"char", FALSE);
10173 if (csearch != NULL)
10174 {
10175#ifdef FEAT_MBYTE
10176 if (enc_utf8)
10177 {
10178 int pcc[MAX_MCO];
10179 int c = utfc_ptr2char(csearch, pcc);
10180
10181 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10182 }
10183 else
10184#endif
10185 set_last_csearch(PTR2CHAR(csearch),
10186 csearch, MB_PTR2LEN(csearch));
10187 }
10188
10189 di = dict_find(d, (char_u *)"forward", -1);
10190 if (di != NULL)
10191 set_csearch_direction((int)get_tv_number(&di->di_tv)
10192 ? FORWARD : BACKWARD);
10193
10194 di = dict_find(d, (char_u *)"until", -1);
10195 if (di != NULL)
10196 set_csearch_until(!!get_tv_number(&di->di_tv));
10197 }
10198}
10199
10200/*
10201 * "setcmdpos()" function
10202 */
10203 static void
10204f_setcmdpos(typval_T *argvars, typval_T *rettv)
10205{
10206 int pos = (int)get_tv_number(&argvars[0]) - 1;
10207
10208 if (pos >= 0)
10209 rettv->vval.v_number = set_cmdline_pos(pos);
10210}
10211
10212/*
10213 * "setfperm({fname}, {mode})" function
10214 */
10215 static void
10216f_setfperm(typval_T *argvars, typval_T *rettv)
10217{
10218 char_u *fname;
10219 char_u modebuf[NUMBUFLEN];
10220 char_u *mode_str;
10221 int i;
10222 int mask;
10223 int mode = 0;
10224
10225 rettv->vval.v_number = 0;
10226 fname = get_tv_string_chk(&argvars[0]);
10227 if (fname == NULL)
10228 return;
10229 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
10230 if (mode_str == NULL)
10231 return;
10232 if (STRLEN(mode_str) != 9)
10233 {
10234 EMSG2(_(e_invarg2), mode_str);
10235 return;
10236 }
10237
10238 mask = 1;
10239 for (i = 8; i >= 0; --i)
10240 {
10241 if (mode_str[i] != '-')
10242 mode |= mask;
10243 mask = mask << 1;
10244 }
10245 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10246}
10247
10248/*
10249 * "setline()" function
10250 */
10251 static void
10252f_setline(typval_T *argvars, typval_T *rettv)
10253{
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010254 linenr_T lnum = get_tv_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010255
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010256 set_buffer_lines(curbuf, lnum, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010257}
10258
Bram Moolenaard823fa92016-08-12 16:29:27 +020010259static 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 +020010260
10261/*
10262 * Used by "setqflist()" and "setloclist()" functions
10263 */
10264 static void
10265set_qf_ll_list(
10266 win_T *wp UNUSED,
10267 typval_T *list_arg UNUSED,
10268 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010269 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010270 typval_T *rettv)
10271{
10272#ifdef FEAT_QUICKFIX
10273 static char *e_invact = N_("E927: Invalid action: '%s'");
10274 char_u *act;
10275 int action = 0;
10276#endif
10277
10278 rettv->vval.v_number = -1;
10279
10280#ifdef FEAT_QUICKFIX
10281 if (list_arg->v_type != VAR_LIST)
10282 EMSG(_(e_listreq));
10283 else
10284 {
10285 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010286 dict_T *d = NULL;
10287 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010288
10289 if (action_arg->v_type == VAR_STRING)
10290 {
10291 act = get_tv_string_chk(action_arg);
10292 if (act == NULL)
10293 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020010294 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
10295 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010296 action = *act;
10297 else
10298 EMSG2(_(e_invact), act);
10299 }
10300 else if (action_arg->v_type == VAR_UNKNOWN)
10301 action = ' ';
10302 else
10303 EMSG(_(e_stringreq));
10304
Bram Moolenaard823fa92016-08-12 16:29:27 +020010305 if (action_arg->v_type != VAR_UNKNOWN
10306 && what_arg->v_type != VAR_UNKNOWN)
10307 {
10308 if (what_arg->v_type == VAR_DICT)
10309 d = what_arg->vval.v_dict;
10310 else
10311 {
10312 EMSG(_(e_dictreq));
10313 valid_dict = FALSE;
10314 }
10315 }
10316
10317 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
10318 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()"), d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010319 rettv->vval.v_number = 0;
10320 }
10321#endif
10322}
10323
10324/*
10325 * "setloclist()" function
10326 */
10327 static void
10328f_setloclist(typval_T *argvars, typval_T *rettv)
10329{
10330 win_T *win;
10331
10332 rettv->vval.v_number = -1;
10333
10334 win = find_win_by_nr(&argvars[0], NULL);
10335 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020010336 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010337}
10338
10339/*
10340 * "setmatches()" function
10341 */
10342 static void
10343f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10344{
10345#ifdef FEAT_SEARCH_EXTRA
10346 list_T *l;
10347 listitem_T *li;
10348 dict_T *d;
10349 list_T *s = NULL;
10350
10351 rettv->vval.v_number = -1;
10352 if (argvars[0].v_type != VAR_LIST)
10353 {
10354 EMSG(_(e_listreq));
10355 return;
10356 }
10357 if ((l = argvars[0].vval.v_list) != NULL)
10358 {
10359
10360 /* To some extent make sure that we are dealing with a list from
10361 * "getmatches()". */
10362 li = l->lv_first;
10363 while (li != NULL)
10364 {
10365 if (li->li_tv.v_type != VAR_DICT
10366 || (d = li->li_tv.vval.v_dict) == NULL)
10367 {
10368 EMSG(_(e_invarg));
10369 return;
10370 }
10371 if (!(dict_find(d, (char_u *)"group", -1) != NULL
10372 && (dict_find(d, (char_u *)"pattern", -1) != NULL
10373 || dict_find(d, (char_u *)"pos1", -1) != NULL)
10374 && dict_find(d, (char_u *)"priority", -1) != NULL
10375 && dict_find(d, (char_u *)"id", -1) != NULL))
10376 {
10377 EMSG(_(e_invarg));
10378 return;
10379 }
10380 li = li->li_next;
10381 }
10382
10383 clear_matches(curwin);
10384 li = l->lv_first;
10385 while (li != NULL)
10386 {
10387 int i = 0;
10388 char_u buf[5];
10389 dictitem_T *di;
10390 char_u *group;
10391 int priority;
10392 int id;
10393 char_u *conceal;
10394
10395 d = li->li_tv.vval.v_dict;
10396 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
10397 {
10398 if (s == NULL)
10399 {
10400 s = list_alloc();
10401 if (s == NULL)
10402 return;
10403 }
10404
10405 /* match from matchaddpos() */
10406 for (i = 1; i < 9; i++)
10407 {
10408 sprintf((char *)buf, (char *)"pos%d", i);
10409 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
10410 {
10411 if (di->di_tv.v_type != VAR_LIST)
10412 return;
10413
10414 list_append_tv(s, &di->di_tv);
10415 s->lv_refcount++;
10416 }
10417 else
10418 break;
10419 }
10420 }
10421
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010422 group = get_dict_string(d, (char_u *)"group", TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010423 priority = (int)get_dict_number(d, (char_u *)"priority");
10424 id = (int)get_dict_number(d, (char_u *)"id");
10425 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010426 ? get_dict_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010427 : NULL;
10428 if (i == 0)
10429 {
10430 match_add(curwin, group,
10431 get_dict_string(d, (char_u *)"pattern", FALSE),
10432 priority, id, NULL, conceal);
10433 }
10434 else
10435 {
10436 match_add(curwin, group, NULL, priority, id, s, conceal);
10437 list_unref(s);
10438 s = NULL;
10439 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010440 vim_free(group);
10441 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010442
10443 li = li->li_next;
10444 }
10445 rettv->vval.v_number = 0;
10446 }
10447#endif
10448}
10449
10450/*
10451 * "setpos()" function
10452 */
10453 static void
10454f_setpos(typval_T *argvars, typval_T *rettv)
10455{
10456 pos_T pos;
10457 int fnum;
10458 char_u *name;
10459 colnr_T curswant = -1;
10460
10461 rettv->vval.v_number = -1;
10462 name = get_tv_string_chk(argvars);
10463 if (name != NULL)
10464 {
10465 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10466 {
10467 if (--pos.col < 0)
10468 pos.col = 0;
10469 if (name[0] == '.' && name[1] == NUL)
10470 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010471 /* set cursor; "fnum" is ignored */
10472 curwin->w_cursor = pos;
10473 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010474 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010475 curwin->w_curswant = curswant - 1;
10476 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010477 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010478 check_cursor();
10479 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010480 }
10481 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10482 {
10483 /* set mark */
10484 if (setmark_pos(name[1], &pos, fnum) == OK)
10485 rettv->vval.v_number = 0;
10486 }
10487 else
10488 EMSG(_(e_invarg));
10489 }
10490 }
10491}
10492
10493/*
10494 * "setqflist()" function
10495 */
10496 static void
10497f_setqflist(typval_T *argvars, typval_T *rettv)
10498{
Bram Moolenaard823fa92016-08-12 16:29:27 +020010499 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010500}
10501
10502/*
10503 * "setreg()" function
10504 */
10505 static void
10506f_setreg(typval_T *argvars, typval_T *rettv)
10507{
10508 int regname;
10509 char_u *strregname;
10510 char_u *stropt;
10511 char_u *strval;
10512 int append;
10513 char_u yank_type;
10514 long block_len;
10515
10516 block_len = -1;
10517 yank_type = MAUTO;
10518 append = FALSE;
10519
10520 strregname = get_tv_string_chk(argvars);
10521 rettv->vval.v_number = 1; /* FAIL is default */
10522
10523 if (strregname == NULL)
10524 return; /* type error; errmsg already given */
10525 regname = *strregname;
10526 if (regname == 0 || regname == '@')
10527 regname = '"';
10528
10529 if (argvars[2].v_type != VAR_UNKNOWN)
10530 {
10531 stropt = get_tv_string_chk(&argvars[2]);
10532 if (stropt == NULL)
10533 return; /* type error */
10534 for (; *stropt != NUL; ++stropt)
10535 switch (*stropt)
10536 {
10537 case 'a': case 'A': /* append */
10538 append = TRUE;
10539 break;
10540 case 'v': case 'c': /* character-wise selection */
10541 yank_type = MCHAR;
10542 break;
10543 case 'V': case 'l': /* line-wise selection */
10544 yank_type = MLINE;
10545 break;
10546 case 'b': case Ctrl_V: /* block-wise selection */
10547 yank_type = MBLOCK;
10548 if (VIM_ISDIGIT(stropt[1]))
10549 {
10550 ++stropt;
10551 block_len = getdigits(&stropt) - 1;
10552 --stropt;
10553 }
10554 break;
10555 }
10556 }
10557
10558 if (argvars[1].v_type == VAR_LIST)
10559 {
10560 char_u **lstval;
10561 char_u **allocval;
10562 char_u buf[NUMBUFLEN];
10563 char_u **curval;
10564 char_u **curallocval;
10565 list_T *ll = argvars[1].vval.v_list;
10566 listitem_T *li;
10567 int len;
10568
10569 /* If the list is NULL handle like an empty list. */
10570 len = ll == NULL ? 0 : ll->lv_len;
10571
10572 /* First half: use for pointers to result lines; second half: use for
10573 * pointers to allocated copies. */
10574 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
10575 if (lstval == NULL)
10576 return;
10577 curval = lstval;
10578 allocval = lstval + len + 2;
10579 curallocval = allocval;
10580
10581 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
10582 li = li->li_next)
10583 {
10584 strval = get_tv_string_buf_chk(&li->li_tv, buf);
10585 if (strval == NULL)
10586 goto free_lstval;
10587 if (strval == buf)
10588 {
10589 /* Need to make a copy, next get_tv_string_buf_chk() will
10590 * overwrite the string. */
10591 strval = vim_strsave(buf);
10592 if (strval == NULL)
10593 goto free_lstval;
10594 *curallocval++ = strval;
10595 }
10596 *curval++ = strval;
10597 }
10598 *curval++ = NULL;
10599
10600 write_reg_contents_lst(regname, lstval, -1,
10601 append, yank_type, block_len);
10602free_lstval:
10603 while (curallocval > allocval)
10604 vim_free(*--curallocval);
10605 vim_free(lstval);
10606 }
10607 else
10608 {
10609 strval = get_tv_string_chk(&argvars[1]);
10610 if (strval == NULL)
10611 return;
10612 write_reg_contents_ex(regname, strval, -1,
10613 append, yank_type, block_len);
10614 }
10615 rettv->vval.v_number = 0;
10616}
10617
10618/*
10619 * "settabvar()" function
10620 */
10621 static void
10622f_settabvar(typval_T *argvars, typval_T *rettv)
10623{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010624 tabpage_T *save_curtab;
10625 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010626 char_u *varname, *tabvarname;
10627 typval_T *varp;
10628
10629 rettv->vval.v_number = 0;
10630
10631 if (check_restricted() || check_secure())
10632 return;
10633
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010634 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010635 varname = get_tv_string_chk(&argvars[1]);
10636 varp = &argvars[2];
10637
Bram Moolenaar4033c552017-09-16 20:54:51 +020010638 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010639 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010640 save_curtab = curtab;
10641 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010642
10643 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
10644 if (tabvarname != NULL)
10645 {
10646 STRCPY(tabvarname, "t:");
10647 STRCPY(tabvarname + 2, varname);
10648 set_var(tabvarname, varp, TRUE);
10649 vim_free(tabvarname);
10650 }
10651
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010652 /* Restore current tabpage */
10653 if (valid_tabpage(save_curtab))
10654 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010655 }
10656}
10657
10658/*
10659 * "settabwinvar()" function
10660 */
10661 static void
10662f_settabwinvar(typval_T *argvars, typval_T *rettv)
10663{
10664 setwinvar(argvars, rettv, 1);
10665}
10666
10667/*
10668 * "setwinvar()" function
10669 */
10670 static void
10671f_setwinvar(typval_T *argvars, typval_T *rettv)
10672{
10673 setwinvar(argvars, rettv, 0);
10674}
10675
10676#ifdef FEAT_CRYPT
10677/*
10678 * "sha256({string})" function
10679 */
10680 static void
10681f_sha256(typval_T *argvars, typval_T *rettv)
10682{
10683 char_u *p;
10684
10685 p = get_tv_string(&argvars[0]);
10686 rettv->vval.v_string = vim_strsave(
10687 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
10688 rettv->v_type = VAR_STRING;
10689}
10690#endif /* FEAT_CRYPT */
10691
10692/*
10693 * "shellescape({string})" function
10694 */
10695 static void
10696f_shellescape(typval_T *argvars, typval_T *rettv)
10697{
Bram Moolenaar20615522017-06-05 18:46:26 +020010698 int do_special = non_zero_arg(&argvars[1]);
10699
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010700 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar20615522017-06-05 18:46:26 +020010701 get_tv_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010702 rettv->v_type = VAR_STRING;
10703}
10704
10705/*
10706 * shiftwidth() function
10707 */
10708 static void
10709f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
10710{
10711 rettv->vval.v_number = get_sw_value(curbuf);
10712}
10713
10714/*
10715 * "simplify()" function
10716 */
10717 static void
10718f_simplify(typval_T *argvars, typval_T *rettv)
10719{
10720 char_u *p;
10721
10722 p = get_tv_string(&argvars[0]);
10723 rettv->vval.v_string = vim_strsave(p);
10724 simplify_filename(rettv->vval.v_string); /* simplify in place */
10725 rettv->v_type = VAR_STRING;
10726}
10727
10728#ifdef FEAT_FLOAT
10729/*
10730 * "sin()" function
10731 */
10732 static void
10733f_sin(typval_T *argvars, typval_T *rettv)
10734{
10735 float_T f = 0.0;
10736
10737 rettv->v_type = VAR_FLOAT;
10738 if (get_float_arg(argvars, &f) == OK)
10739 rettv->vval.v_float = sin(f);
10740 else
10741 rettv->vval.v_float = 0.0;
10742}
10743
10744/*
10745 * "sinh()" function
10746 */
10747 static void
10748f_sinh(typval_T *argvars, typval_T *rettv)
10749{
10750 float_T f = 0.0;
10751
10752 rettv->v_type = VAR_FLOAT;
10753 if (get_float_arg(argvars, &f) == OK)
10754 rettv->vval.v_float = sinh(f);
10755 else
10756 rettv->vval.v_float = 0.0;
10757}
10758#endif
10759
10760static int
10761#ifdef __BORLANDC__
10762 _RTLENTRYF
10763#endif
10764 item_compare(const void *s1, const void *s2);
10765static int
10766#ifdef __BORLANDC__
10767 _RTLENTRYF
10768#endif
10769 item_compare2(const void *s1, const void *s2);
10770
10771/* struct used in the array that's given to qsort() */
10772typedef struct
10773{
10774 listitem_T *item;
10775 int idx;
10776} sortItem_T;
10777
10778/* struct storing information about current sort */
10779typedef struct
10780{
10781 int item_compare_ic;
10782 int item_compare_numeric;
10783 int item_compare_numbers;
10784#ifdef FEAT_FLOAT
10785 int item_compare_float;
10786#endif
10787 char_u *item_compare_func;
10788 partial_T *item_compare_partial;
10789 dict_T *item_compare_selfdict;
10790 int item_compare_func_err;
10791 int item_compare_keep_zero;
10792} sortinfo_T;
10793static sortinfo_T *sortinfo = NULL;
10794static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
10795#define ITEM_COMPARE_FAIL 999
10796
10797/*
10798 * Compare functions for f_sort() and f_uniq() below.
10799 */
10800 static int
10801#ifdef __BORLANDC__
10802_RTLENTRYF
10803#endif
10804item_compare(const void *s1, const void *s2)
10805{
10806 sortItem_T *si1, *si2;
10807 typval_T *tv1, *tv2;
10808 char_u *p1, *p2;
10809 char_u *tofree1 = NULL, *tofree2 = NULL;
10810 int res;
10811 char_u numbuf1[NUMBUFLEN];
10812 char_u numbuf2[NUMBUFLEN];
10813
10814 si1 = (sortItem_T *)s1;
10815 si2 = (sortItem_T *)s2;
10816 tv1 = &si1->item->li_tv;
10817 tv2 = &si2->item->li_tv;
10818
10819 if (sortinfo->item_compare_numbers)
10820 {
10821 varnumber_T v1 = get_tv_number(tv1);
10822 varnumber_T v2 = get_tv_number(tv2);
10823
10824 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10825 }
10826
10827#ifdef FEAT_FLOAT
10828 if (sortinfo->item_compare_float)
10829 {
10830 float_T v1 = get_tv_float(tv1);
10831 float_T v2 = get_tv_float(tv2);
10832
10833 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10834 }
10835#endif
10836
10837 /* tv2string() puts quotes around a string and allocates memory. Don't do
10838 * that for string variables. Use a single quote when comparing with a
10839 * non-string to do what the docs promise. */
10840 if (tv1->v_type == VAR_STRING)
10841 {
10842 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
10843 p1 = (char_u *)"'";
10844 else
10845 p1 = tv1->vval.v_string;
10846 }
10847 else
10848 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
10849 if (tv2->v_type == VAR_STRING)
10850 {
10851 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
10852 p2 = (char_u *)"'";
10853 else
10854 p2 = tv2->vval.v_string;
10855 }
10856 else
10857 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
10858 if (p1 == NULL)
10859 p1 = (char_u *)"";
10860 if (p2 == NULL)
10861 p2 = (char_u *)"";
10862 if (!sortinfo->item_compare_numeric)
10863 {
10864 if (sortinfo->item_compare_ic)
10865 res = STRICMP(p1, p2);
10866 else
10867 res = STRCMP(p1, p2);
10868 }
10869 else
10870 {
10871 double n1, n2;
10872 n1 = strtod((char *)p1, (char **)&p1);
10873 n2 = strtod((char *)p2, (char **)&p2);
10874 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
10875 }
10876
10877 /* When the result would be zero, compare the item indexes. Makes the
10878 * sort stable. */
10879 if (res == 0 && !sortinfo->item_compare_keep_zero)
10880 res = si1->idx > si2->idx ? 1 : -1;
10881
10882 vim_free(tofree1);
10883 vim_free(tofree2);
10884 return res;
10885}
10886
10887 static int
10888#ifdef __BORLANDC__
10889_RTLENTRYF
10890#endif
10891item_compare2(const void *s1, const void *s2)
10892{
10893 sortItem_T *si1, *si2;
10894 int res;
10895 typval_T rettv;
10896 typval_T argv[3];
10897 int dummy;
10898 char_u *func_name;
10899 partial_T *partial = sortinfo->item_compare_partial;
10900
10901 /* shortcut after failure in previous call; compare all items equal */
10902 if (sortinfo->item_compare_func_err)
10903 return 0;
10904
10905 si1 = (sortItem_T *)s1;
10906 si2 = (sortItem_T *)s2;
10907
10908 if (partial == NULL)
10909 func_name = sortinfo->item_compare_func;
10910 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020010911 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010912
10913 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
10914 * in the copy without changing the original list items. */
10915 copy_tv(&si1->item->li_tv, &argv[0]);
10916 copy_tv(&si2->item->li_tv, &argv[1]);
10917
10918 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
10919 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020010920 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010921 partial, sortinfo->item_compare_selfdict);
10922 clear_tv(&argv[0]);
10923 clear_tv(&argv[1]);
10924
10925 if (res == FAIL)
10926 res = ITEM_COMPARE_FAIL;
10927 else
10928 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
10929 if (sortinfo->item_compare_func_err)
10930 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
10931 clear_tv(&rettv);
10932
10933 /* When the result would be zero, compare the pointers themselves. Makes
10934 * the sort stable. */
10935 if (res == 0 && !sortinfo->item_compare_keep_zero)
10936 res = si1->idx > si2->idx ? 1 : -1;
10937
10938 return res;
10939}
10940
10941/*
10942 * "sort({list})" function
10943 */
10944 static void
10945do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
10946{
10947 list_T *l;
10948 listitem_T *li;
10949 sortItem_T *ptrs;
10950 sortinfo_T *old_sortinfo;
10951 sortinfo_T info;
10952 long len;
10953 long i;
10954
10955 /* Pointer to current info struct used in compare function. Save and
10956 * restore the current one for nested calls. */
10957 old_sortinfo = sortinfo;
10958 sortinfo = &info;
10959
10960 if (argvars[0].v_type != VAR_LIST)
10961 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
10962 else
10963 {
10964 l = argvars[0].vval.v_list;
10965 if (l == NULL || tv_check_lock(l->lv_lock,
10966 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
10967 TRUE))
10968 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020010969 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010970
10971 len = list_len(l);
10972 if (len <= 1)
10973 goto theend; /* short list sorts pretty quickly */
10974
10975 info.item_compare_ic = FALSE;
10976 info.item_compare_numeric = FALSE;
10977 info.item_compare_numbers = FALSE;
10978#ifdef FEAT_FLOAT
10979 info.item_compare_float = FALSE;
10980#endif
10981 info.item_compare_func = NULL;
10982 info.item_compare_partial = NULL;
10983 info.item_compare_selfdict = NULL;
10984 if (argvars[1].v_type != VAR_UNKNOWN)
10985 {
10986 /* optional second argument: {func} */
10987 if (argvars[1].v_type == VAR_FUNC)
10988 info.item_compare_func = argvars[1].vval.v_string;
10989 else if (argvars[1].v_type == VAR_PARTIAL)
10990 info.item_compare_partial = argvars[1].vval.v_partial;
10991 else
10992 {
10993 int error = FALSE;
10994
10995 i = (long)get_tv_number_chk(&argvars[1], &error);
10996 if (error)
10997 goto theend; /* type error; errmsg already given */
10998 if (i == 1)
10999 info.item_compare_ic = TRUE;
11000 else if (argvars[1].v_type != VAR_NUMBER)
11001 info.item_compare_func = get_tv_string(&argvars[1]);
11002 else if (i != 0)
11003 {
11004 EMSG(_(e_invarg));
11005 goto theend;
11006 }
11007 if (info.item_compare_func != NULL)
11008 {
11009 if (*info.item_compare_func == NUL)
11010 {
11011 /* empty string means default sort */
11012 info.item_compare_func = NULL;
11013 }
11014 else if (STRCMP(info.item_compare_func, "n") == 0)
11015 {
11016 info.item_compare_func = NULL;
11017 info.item_compare_numeric = TRUE;
11018 }
11019 else if (STRCMP(info.item_compare_func, "N") == 0)
11020 {
11021 info.item_compare_func = NULL;
11022 info.item_compare_numbers = TRUE;
11023 }
11024#ifdef FEAT_FLOAT
11025 else if (STRCMP(info.item_compare_func, "f") == 0)
11026 {
11027 info.item_compare_func = NULL;
11028 info.item_compare_float = TRUE;
11029 }
11030#endif
11031 else if (STRCMP(info.item_compare_func, "i") == 0)
11032 {
11033 info.item_compare_func = NULL;
11034 info.item_compare_ic = TRUE;
11035 }
11036 }
11037 }
11038
11039 if (argvars[2].v_type != VAR_UNKNOWN)
11040 {
11041 /* optional third argument: {dict} */
11042 if (argvars[2].v_type != VAR_DICT)
11043 {
11044 EMSG(_(e_dictreq));
11045 goto theend;
11046 }
11047 info.item_compare_selfdict = argvars[2].vval.v_dict;
11048 }
11049 }
11050
11051 /* Make an array with each entry pointing to an item in the List. */
11052 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
11053 if (ptrs == NULL)
11054 goto theend;
11055
11056 i = 0;
11057 if (sort)
11058 {
11059 /* sort(): ptrs will be the list to sort */
11060 for (li = l->lv_first; li != NULL; li = li->li_next)
11061 {
11062 ptrs[i].item = li;
11063 ptrs[i].idx = i;
11064 ++i;
11065 }
11066
11067 info.item_compare_func_err = FALSE;
11068 info.item_compare_keep_zero = FALSE;
11069 /* test the compare function */
11070 if ((info.item_compare_func != NULL
11071 || info.item_compare_partial != NULL)
11072 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11073 == ITEM_COMPARE_FAIL)
11074 EMSG(_("E702: Sort compare function failed"));
11075 else
11076 {
11077 /* Sort the array with item pointers. */
11078 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
11079 info.item_compare_func == NULL
11080 && info.item_compare_partial == NULL
11081 ? item_compare : item_compare2);
11082
11083 if (!info.item_compare_func_err)
11084 {
11085 /* Clear the List and append the items in sorted order. */
11086 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
11087 l->lv_len = 0;
11088 for (i = 0; i < len; ++i)
11089 list_append(l, ptrs[i].item);
11090 }
11091 }
11092 }
11093 else
11094 {
11095 int (*item_compare_func_ptr)(const void *, const void *);
11096
11097 /* f_uniq(): ptrs will be a stack of items to remove */
11098 info.item_compare_func_err = FALSE;
11099 info.item_compare_keep_zero = TRUE;
11100 item_compare_func_ptr = info.item_compare_func != NULL
11101 || info.item_compare_partial != NULL
11102 ? item_compare2 : item_compare;
11103
11104 for (li = l->lv_first; li != NULL && li->li_next != NULL;
11105 li = li->li_next)
11106 {
11107 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
11108 == 0)
11109 ptrs[i++].item = li;
11110 if (info.item_compare_func_err)
11111 {
11112 EMSG(_("E882: Uniq compare function failed"));
11113 break;
11114 }
11115 }
11116
11117 if (!info.item_compare_func_err)
11118 {
11119 while (--i >= 0)
11120 {
11121 li = ptrs[i].item->li_next;
11122 ptrs[i].item->li_next = li->li_next;
11123 if (li->li_next != NULL)
11124 li->li_next->li_prev = ptrs[i].item;
11125 else
11126 l->lv_last = ptrs[i].item;
11127 list_fix_watch(l, li);
11128 listitem_free(li);
11129 l->lv_len--;
11130 }
11131 }
11132 }
11133
11134 vim_free(ptrs);
11135 }
11136theend:
11137 sortinfo = old_sortinfo;
11138}
11139
11140/*
11141 * "sort({list})" function
11142 */
11143 static void
11144f_sort(typval_T *argvars, typval_T *rettv)
11145{
11146 do_sort_uniq(argvars, rettv, TRUE);
11147}
11148
11149/*
11150 * "uniq({list})" function
11151 */
11152 static void
11153f_uniq(typval_T *argvars, typval_T *rettv)
11154{
11155 do_sort_uniq(argvars, rettv, FALSE);
11156}
11157
11158/*
11159 * "soundfold({word})" function
11160 */
11161 static void
11162f_soundfold(typval_T *argvars, typval_T *rettv)
11163{
11164 char_u *s;
11165
11166 rettv->v_type = VAR_STRING;
11167 s = get_tv_string(&argvars[0]);
11168#ifdef FEAT_SPELL
11169 rettv->vval.v_string = eval_soundfold(s);
11170#else
11171 rettv->vval.v_string = vim_strsave(s);
11172#endif
11173}
11174
11175/*
11176 * "spellbadword()" function
11177 */
11178 static void
11179f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
11180{
11181 char_u *word = (char_u *)"";
11182 hlf_T attr = HLF_COUNT;
11183 int len = 0;
11184
11185 if (rettv_list_alloc(rettv) == FAIL)
11186 return;
11187
11188#ifdef FEAT_SPELL
11189 if (argvars[0].v_type == VAR_UNKNOWN)
11190 {
11191 /* Find the start and length of the badly spelled word. */
11192 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
11193 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011194 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011195 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011196 curwin->w_set_curswant = TRUE;
11197 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011198 }
11199 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
11200 {
11201 char_u *str = get_tv_string_chk(&argvars[0]);
11202 int capcol = -1;
11203
11204 if (str != NULL)
11205 {
11206 /* Check the argument for spelling. */
11207 while (*str != NUL)
11208 {
11209 len = spell_check(curwin, str, &attr, &capcol, FALSE);
11210 if (attr != HLF_COUNT)
11211 {
11212 word = str;
11213 break;
11214 }
11215 str += len;
11216 }
11217 }
11218 }
11219#endif
11220
11221 list_append_string(rettv->vval.v_list, word, len);
11222 list_append_string(rettv->vval.v_list, (char_u *)(
11223 attr == HLF_SPB ? "bad" :
11224 attr == HLF_SPR ? "rare" :
11225 attr == HLF_SPL ? "local" :
11226 attr == HLF_SPC ? "caps" :
11227 ""), -1);
11228}
11229
11230/*
11231 * "spellsuggest()" function
11232 */
11233 static void
11234f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
11235{
11236#ifdef FEAT_SPELL
11237 char_u *str;
11238 int typeerr = FALSE;
11239 int maxcount;
11240 garray_T ga;
11241 int i;
11242 listitem_T *li;
11243 int need_capital = FALSE;
11244#endif
11245
11246 if (rettv_list_alloc(rettv) == FAIL)
11247 return;
11248
11249#ifdef FEAT_SPELL
11250 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
11251 {
11252 str = get_tv_string(&argvars[0]);
11253 if (argvars[1].v_type != VAR_UNKNOWN)
11254 {
11255 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
11256 if (maxcount <= 0)
11257 return;
11258 if (argvars[2].v_type != VAR_UNKNOWN)
11259 {
11260 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
11261 if (typeerr)
11262 return;
11263 }
11264 }
11265 else
11266 maxcount = 25;
11267
11268 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
11269
11270 for (i = 0; i < ga.ga_len; ++i)
11271 {
11272 str = ((char_u **)ga.ga_data)[i];
11273
11274 li = listitem_alloc();
11275 if (li == NULL)
11276 vim_free(str);
11277 else
11278 {
11279 li->li_tv.v_type = VAR_STRING;
11280 li->li_tv.v_lock = 0;
11281 li->li_tv.vval.v_string = str;
11282 list_append(rettv->vval.v_list, li);
11283 }
11284 }
11285 ga_clear(&ga);
11286 }
11287#endif
11288}
11289
11290 static void
11291f_split(typval_T *argvars, typval_T *rettv)
11292{
11293 char_u *str;
11294 char_u *end;
11295 char_u *pat = NULL;
11296 regmatch_T regmatch;
11297 char_u patbuf[NUMBUFLEN];
11298 char_u *save_cpo;
11299 int match;
11300 colnr_T col = 0;
11301 int keepempty = FALSE;
11302 int typeerr = FALSE;
11303
11304 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11305 save_cpo = p_cpo;
11306 p_cpo = (char_u *)"";
11307
11308 str = get_tv_string(&argvars[0]);
11309 if (argvars[1].v_type != VAR_UNKNOWN)
11310 {
11311 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11312 if (pat == NULL)
11313 typeerr = TRUE;
11314 if (argvars[2].v_type != VAR_UNKNOWN)
11315 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
11316 }
11317 if (pat == NULL || *pat == NUL)
11318 pat = (char_u *)"[\\x01- ]\\+";
11319
11320 if (rettv_list_alloc(rettv) == FAIL)
11321 return;
11322 if (typeerr)
11323 return;
11324
11325 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11326 if (regmatch.regprog != NULL)
11327 {
11328 regmatch.rm_ic = FALSE;
11329 while (*str != NUL || keepempty)
11330 {
11331 if (*str == NUL)
11332 match = FALSE; /* empty item at the end */
11333 else
11334 match = vim_regexec_nl(&regmatch, str, col);
11335 if (match)
11336 end = regmatch.startp[0];
11337 else
11338 end = str + STRLEN(str);
11339 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
11340 && *str != NUL && match && end < regmatch.endp[0]))
11341 {
11342 if (list_append_string(rettv->vval.v_list, str,
11343 (int)(end - str)) == FAIL)
11344 break;
11345 }
11346 if (!match)
11347 break;
11348 /* Advance to just after the match. */
11349 if (regmatch.endp[0] > str)
11350 col = 0;
11351 else
11352 {
11353 /* Don't get stuck at the same match. */
11354#ifdef FEAT_MBYTE
11355 col = (*mb_ptr2len)(regmatch.endp[0]);
11356#else
11357 col = 1;
11358#endif
11359 }
11360 str = regmatch.endp[0];
11361 }
11362
11363 vim_regfree(regmatch.regprog);
11364 }
11365
11366 p_cpo = save_cpo;
11367}
11368
11369#ifdef FEAT_FLOAT
11370/*
11371 * "sqrt()" function
11372 */
11373 static void
11374f_sqrt(typval_T *argvars, typval_T *rettv)
11375{
11376 float_T f = 0.0;
11377
11378 rettv->v_type = VAR_FLOAT;
11379 if (get_float_arg(argvars, &f) == OK)
11380 rettv->vval.v_float = sqrt(f);
11381 else
11382 rettv->vval.v_float = 0.0;
11383}
11384
11385/*
11386 * "str2float()" function
11387 */
11388 static void
11389f_str2float(typval_T *argvars, typval_T *rettv)
11390{
11391 char_u *p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011392 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011393
Bram Moolenaar08243d22017-01-10 16:12:29 +010011394 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011395 p = skipwhite(p + 1);
11396 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011397 if (isneg)
11398 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011399 rettv->v_type = VAR_FLOAT;
11400}
11401#endif
11402
11403/*
11404 * "str2nr()" function
11405 */
11406 static void
11407f_str2nr(typval_T *argvars, typval_T *rettv)
11408{
11409 int base = 10;
11410 char_u *p;
11411 varnumber_T n;
11412 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010011413 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011414
11415 if (argvars[1].v_type != VAR_UNKNOWN)
11416 {
11417 base = (int)get_tv_number(&argvars[1]);
11418 if (base != 2 && base != 8 && base != 10 && base != 16)
11419 {
11420 EMSG(_(e_invarg));
11421 return;
11422 }
11423 }
11424
11425 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011426 isneg = (*p == '-');
11427 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011428 p = skipwhite(p + 1);
11429 switch (base)
11430 {
11431 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
11432 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
11433 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
11434 default: what = 0;
11435 }
11436 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011437 if (isneg)
11438 rettv->vval.v_number = -n;
11439 else
11440 rettv->vval.v_number = n;
11441
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011442}
11443
11444#ifdef HAVE_STRFTIME
11445/*
11446 * "strftime({format}[, {time}])" function
11447 */
11448 static void
11449f_strftime(typval_T *argvars, typval_T *rettv)
11450{
11451 char_u result_buf[256];
11452 struct tm *curtime;
11453 time_t seconds;
11454 char_u *p;
11455
11456 rettv->v_type = VAR_STRING;
11457
11458 p = get_tv_string(&argvars[0]);
11459 if (argvars[1].v_type == VAR_UNKNOWN)
11460 seconds = time(NULL);
11461 else
11462 seconds = (time_t)get_tv_number(&argvars[1]);
11463 curtime = localtime(&seconds);
11464 /* MSVC returns NULL for an invalid value of seconds. */
11465 if (curtime == NULL)
11466 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
11467 else
11468 {
11469# ifdef FEAT_MBYTE
11470 vimconv_T conv;
11471 char_u *enc;
11472
11473 conv.vc_type = CONV_NONE;
11474 enc = enc_locale();
11475 convert_setup(&conv, p_enc, enc);
11476 if (conv.vc_type != CONV_NONE)
11477 p = string_convert(&conv, p, NULL);
11478# endif
11479 if (p != NULL)
11480 (void)strftime((char *)result_buf, sizeof(result_buf),
11481 (char *)p, curtime);
11482 else
11483 result_buf[0] = NUL;
11484
11485# ifdef FEAT_MBYTE
11486 if (conv.vc_type != CONV_NONE)
11487 vim_free(p);
11488 convert_setup(&conv, enc, p_enc);
11489 if (conv.vc_type != CONV_NONE)
11490 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
11491 else
11492# endif
11493 rettv->vval.v_string = vim_strsave(result_buf);
11494
11495# ifdef FEAT_MBYTE
11496 /* Release conversion descriptors */
11497 convert_setup(&conv, NULL, NULL);
11498 vim_free(enc);
11499# endif
11500 }
11501}
11502#endif
11503
11504/*
11505 * "strgetchar()" function
11506 */
11507 static void
11508f_strgetchar(typval_T *argvars, typval_T *rettv)
11509{
11510 char_u *str;
11511 int len;
11512 int error = FALSE;
11513 int charidx;
11514
11515 rettv->vval.v_number = -1;
11516 str = get_tv_string_chk(&argvars[0]);
11517 if (str == NULL)
11518 return;
11519 len = (int)STRLEN(str);
11520 charidx = (int)get_tv_number_chk(&argvars[1], &error);
11521 if (error)
11522 return;
11523#ifdef FEAT_MBYTE
11524 {
11525 int byteidx = 0;
11526
11527 while (charidx >= 0 && byteidx < len)
11528 {
11529 if (charidx == 0)
11530 {
11531 rettv->vval.v_number = mb_ptr2char(str + byteidx);
11532 break;
11533 }
11534 --charidx;
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011535 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011536 }
11537 }
11538#else
11539 if (charidx < len)
11540 rettv->vval.v_number = str[charidx];
11541#endif
11542}
11543
11544/*
11545 * "stridx()" function
11546 */
11547 static void
11548f_stridx(typval_T *argvars, typval_T *rettv)
11549{
11550 char_u buf[NUMBUFLEN];
11551 char_u *needle;
11552 char_u *haystack;
11553 char_u *save_haystack;
11554 char_u *pos;
11555 int start_idx;
11556
11557 needle = get_tv_string_chk(&argvars[1]);
11558 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
11559 rettv->vval.v_number = -1;
11560 if (needle == NULL || haystack == NULL)
11561 return; /* type error; errmsg already given */
11562
11563 if (argvars[2].v_type != VAR_UNKNOWN)
11564 {
11565 int error = FALSE;
11566
11567 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
11568 if (error || start_idx >= (int)STRLEN(haystack))
11569 return;
11570 if (start_idx >= 0)
11571 haystack += start_idx;
11572 }
11573
11574 pos = (char_u *)strstr((char *)haystack, (char *)needle);
11575 if (pos != NULL)
11576 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
11577}
11578
11579/*
11580 * "string()" function
11581 */
11582 static void
11583f_string(typval_T *argvars, typval_T *rettv)
11584{
11585 char_u *tofree;
11586 char_u numbuf[NUMBUFLEN];
11587
11588 rettv->v_type = VAR_STRING;
11589 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
11590 get_copyID());
11591 /* Make a copy if we have a value but it's not in allocated memory. */
11592 if (rettv->vval.v_string != NULL && tofree == NULL)
11593 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
11594}
11595
11596/*
11597 * "strlen()" function
11598 */
11599 static void
11600f_strlen(typval_T *argvars, typval_T *rettv)
11601{
11602 rettv->vval.v_number = (varnumber_T)(STRLEN(
11603 get_tv_string(&argvars[0])));
11604}
11605
11606/*
11607 * "strchars()" function
11608 */
11609 static void
11610f_strchars(typval_T *argvars, typval_T *rettv)
11611{
11612 char_u *s = get_tv_string(&argvars[0]);
11613 int skipcc = 0;
11614#ifdef FEAT_MBYTE
11615 varnumber_T len = 0;
11616 int (*func_mb_ptr2char_adv)(char_u **pp);
11617#endif
11618
11619 if (argvars[1].v_type != VAR_UNKNOWN)
11620 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
11621 if (skipcc < 0 || skipcc > 1)
11622 EMSG(_(e_invarg));
11623 else
11624 {
11625#ifdef FEAT_MBYTE
11626 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
11627 while (*s != NUL)
11628 {
11629 func_mb_ptr2char_adv(&s);
11630 ++len;
11631 }
11632 rettv->vval.v_number = len;
11633#else
11634 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
11635#endif
11636 }
11637}
11638
11639/*
11640 * "strdisplaywidth()" function
11641 */
11642 static void
11643f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
11644{
11645 char_u *s = get_tv_string(&argvars[0]);
11646 int col = 0;
11647
11648 if (argvars[1].v_type != VAR_UNKNOWN)
11649 col = (int)get_tv_number(&argvars[1]);
11650
11651 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
11652}
11653
11654/*
11655 * "strwidth()" function
11656 */
11657 static void
11658f_strwidth(typval_T *argvars, typval_T *rettv)
11659{
11660 char_u *s = get_tv_string(&argvars[0]);
11661
11662 rettv->vval.v_number = (varnumber_T)(
11663#ifdef FEAT_MBYTE
11664 mb_string2cells(s, -1)
11665#else
11666 STRLEN(s)
11667#endif
11668 );
11669}
11670
11671/*
11672 * "strcharpart()" function
11673 */
11674 static void
11675f_strcharpart(typval_T *argvars, typval_T *rettv)
11676{
11677#ifdef FEAT_MBYTE
11678 char_u *p;
11679 int nchar;
11680 int nbyte = 0;
11681 int charlen;
11682 int len = 0;
11683 int slen;
11684 int error = FALSE;
11685
11686 p = get_tv_string(&argvars[0]);
11687 slen = (int)STRLEN(p);
11688
11689 nchar = (int)get_tv_number_chk(&argvars[1], &error);
11690 if (!error)
11691 {
11692 if (nchar > 0)
11693 while (nchar > 0 && nbyte < slen)
11694 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011695 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011696 --nchar;
11697 }
11698 else
11699 nbyte = nchar;
11700 if (argvars[2].v_type != VAR_UNKNOWN)
11701 {
11702 charlen = (int)get_tv_number(&argvars[2]);
11703 while (charlen > 0 && nbyte + len < slen)
11704 {
11705 int off = nbyte + len;
11706
11707 if (off < 0)
11708 len += 1;
11709 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011710 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011711 --charlen;
11712 }
11713 }
11714 else
11715 len = slen - nbyte; /* default: all bytes that are available. */
11716 }
11717
11718 /*
11719 * Only return the overlap between the specified part and the actual
11720 * string.
11721 */
11722 if (nbyte < 0)
11723 {
11724 len += nbyte;
11725 nbyte = 0;
11726 }
11727 else if (nbyte > slen)
11728 nbyte = slen;
11729 if (len < 0)
11730 len = 0;
11731 else if (nbyte + len > slen)
11732 len = slen - nbyte;
11733
11734 rettv->v_type = VAR_STRING;
11735 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
11736#else
11737 f_strpart(argvars, rettv);
11738#endif
11739}
11740
11741/*
11742 * "strpart()" function
11743 */
11744 static void
11745f_strpart(typval_T *argvars, typval_T *rettv)
11746{
11747 char_u *p;
11748 int n;
11749 int len;
11750 int slen;
11751 int error = FALSE;
11752
11753 p = get_tv_string(&argvars[0]);
11754 slen = (int)STRLEN(p);
11755
11756 n = (int)get_tv_number_chk(&argvars[1], &error);
11757 if (error)
11758 len = 0;
11759 else if (argvars[2].v_type != VAR_UNKNOWN)
11760 len = (int)get_tv_number(&argvars[2]);
11761 else
11762 len = slen - n; /* default len: all bytes that are available. */
11763
11764 /*
11765 * Only return the overlap between the specified part and the actual
11766 * string.
11767 */
11768 if (n < 0)
11769 {
11770 len += n;
11771 n = 0;
11772 }
11773 else if (n > slen)
11774 n = slen;
11775 if (len < 0)
11776 len = 0;
11777 else if (n + len > slen)
11778 len = slen - n;
11779
11780 rettv->v_type = VAR_STRING;
11781 rettv->vval.v_string = vim_strnsave(p + n, len);
11782}
11783
11784/*
11785 * "strridx()" function
11786 */
11787 static void
11788f_strridx(typval_T *argvars, typval_T *rettv)
11789{
11790 char_u buf[NUMBUFLEN];
11791 char_u *needle;
11792 char_u *haystack;
11793 char_u *rest;
11794 char_u *lastmatch = NULL;
11795 int haystack_len, end_idx;
11796
11797 needle = get_tv_string_chk(&argvars[1]);
11798 haystack = get_tv_string_buf_chk(&argvars[0], buf);
11799
11800 rettv->vval.v_number = -1;
11801 if (needle == NULL || haystack == NULL)
11802 return; /* type error; errmsg already given */
11803
11804 haystack_len = (int)STRLEN(haystack);
11805 if (argvars[2].v_type != VAR_UNKNOWN)
11806 {
11807 /* Third argument: upper limit for index */
11808 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
11809 if (end_idx < 0)
11810 return; /* can never find a match */
11811 }
11812 else
11813 end_idx = haystack_len;
11814
11815 if (*needle == NUL)
11816 {
11817 /* Empty string matches past the end. */
11818 lastmatch = haystack + end_idx;
11819 }
11820 else
11821 {
11822 for (rest = haystack; *rest != '\0'; ++rest)
11823 {
11824 rest = (char_u *)strstr((char *)rest, (char *)needle);
11825 if (rest == NULL || rest > haystack + end_idx)
11826 break;
11827 lastmatch = rest;
11828 }
11829 }
11830
11831 if (lastmatch == NULL)
11832 rettv->vval.v_number = -1;
11833 else
11834 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
11835}
11836
11837/*
11838 * "strtrans()" function
11839 */
11840 static void
11841f_strtrans(typval_T *argvars, typval_T *rettv)
11842{
11843 rettv->v_type = VAR_STRING;
11844 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
11845}
11846
11847/*
11848 * "submatch()" function
11849 */
11850 static void
11851f_submatch(typval_T *argvars, typval_T *rettv)
11852{
11853 int error = FALSE;
11854 int no;
11855 int retList = 0;
11856
11857 no = (int)get_tv_number_chk(&argvars[0], &error);
11858 if (error)
11859 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020011860 if (no < 0 || no >= NSUBEXP)
11861 {
Bram Moolenaar79518e22017-02-17 16:31:35 +010011862 EMSGN(_("E935: invalid submatch number: %d"), no);
11863 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020011864 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011865 if (argvars[1].v_type != VAR_UNKNOWN)
11866 retList = (int)get_tv_number_chk(&argvars[1], &error);
11867 if (error)
11868 return;
11869
11870 if (retList == 0)
11871 {
11872 rettv->v_type = VAR_STRING;
11873 rettv->vval.v_string = reg_submatch(no);
11874 }
11875 else
11876 {
11877 rettv->v_type = VAR_LIST;
11878 rettv->vval.v_list = reg_submatch_list(no);
11879 }
11880}
11881
11882/*
11883 * "substitute()" function
11884 */
11885 static void
11886f_substitute(typval_T *argvars, typval_T *rettv)
11887{
11888 char_u patbuf[NUMBUFLEN];
11889 char_u subbuf[NUMBUFLEN];
11890 char_u flagsbuf[NUMBUFLEN];
11891
11892 char_u *str = get_tv_string_chk(&argvars[0]);
11893 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011894 char_u *sub = NULL;
11895 typval_T *expr = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011896 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
11897
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011898 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
11899 expr = &argvars[2];
11900 else
11901 sub = get_tv_string_buf_chk(&argvars[2], subbuf);
11902
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011903 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011904 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
11905 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011906 rettv->vval.v_string = NULL;
11907 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011908 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011909}
11910
11911/*
11912 * "synID(lnum, col, trans)" function
11913 */
11914 static void
11915f_synID(typval_T *argvars UNUSED, typval_T *rettv)
11916{
11917 int id = 0;
11918#ifdef FEAT_SYN_HL
11919 linenr_T lnum;
11920 colnr_T col;
11921 int trans;
11922 int transerr = FALSE;
11923
11924 lnum = get_tv_lnum(argvars); /* -1 on type error */
11925 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
11926 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
11927
11928 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11929 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
11930 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
11931#endif
11932
11933 rettv->vval.v_number = id;
11934}
11935
11936/*
11937 * "synIDattr(id, what [, mode])" function
11938 */
11939 static void
11940f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
11941{
11942 char_u *p = NULL;
11943#ifdef FEAT_SYN_HL
11944 int id;
11945 char_u *what;
11946 char_u *mode;
11947 char_u modebuf[NUMBUFLEN];
11948 int modec;
11949
11950 id = (int)get_tv_number(&argvars[0]);
11951 what = get_tv_string(&argvars[1]);
11952 if (argvars[2].v_type != VAR_UNKNOWN)
11953 {
11954 mode = get_tv_string_buf(&argvars[2], modebuf);
11955 modec = TOLOWER_ASC(mode[0]);
11956 if (modec != 't' && modec != 'c' && modec != 'g')
11957 modec = 0; /* replace invalid with current */
11958 }
11959 else
11960 {
11961#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
11962 if (USE_24BIT)
11963 modec = 'g';
11964 else
11965#endif
11966 if (t_colors > 1)
11967 modec = 'c';
11968 else
11969 modec = 't';
11970 }
11971
11972
11973 switch (TOLOWER_ASC(what[0]))
11974 {
11975 case 'b':
11976 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
11977 p = highlight_color(id, what, modec);
11978 else /* bold */
11979 p = highlight_has_attr(id, HL_BOLD, modec);
11980 break;
11981
11982 case 'f': /* fg[#] or font */
11983 p = highlight_color(id, what, modec);
11984 break;
11985
11986 case 'i':
11987 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
11988 p = highlight_has_attr(id, HL_INVERSE, modec);
11989 else /* italic */
11990 p = highlight_has_attr(id, HL_ITALIC, modec);
11991 break;
11992
11993 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020011994 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011995 break;
11996
11997 case 'r': /* reverse */
11998 p = highlight_has_attr(id, HL_INVERSE, modec);
11999 break;
12000
12001 case 's':
12002 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
12003 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020012004 /* strikeout */
12005 else if (TOLOWER_ASC(what[1]) == 't' &&
12006 TOLOWER_ASC(what[2]) == 'r')
12007 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012008 else /* standout */
12009 p = highlight_has_attr(id, HL_STANDOUT, modec);
12010 break;
12011
12012 case 'u':
12013 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
12014 /* underline */
12015 p = highlight_has_attr(id, HL_UNDERLINE, modec);
12016 else
12017 /* undercurl */
12018 p = highlight_has_attr(id, HL_UNDERCURL, modec);
12019 break;
12020 }
12021
12022 if (p != NULL)
12023 p = vim_strsave(p);
12024#endif
12025 rettv->v_type = VAR_STRING;
12026 rettv->vval.v_string = p;
12027}
12028
12029/*
12030 * "synIDtrans(id)" function
12031 */
12032 static void
12033f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
12034{
12035 int id;
12036
12037#ifdef FEAT_SYN_HL
12038 id = (int)get_tv_number(&argvars[0]);
12039
12040 if (id > 0)
12041 id = syn_get_final_id(id);
12042 else
12043#endif
12044 id = 0;
12045
12046 rettv->vval.v_number = id;
12047}
12048
12049/*
12050 * "synconcealed(lnum, col)" function
12051 */
12052 static void
12053f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
12054{
12055#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12056 linenr_T lnum;
12057 colnr_T col;
12058 int syntax_flags = 0;
12059 int cchar;
12060 int matchid = 0;
12061 char_u str[NUMBUFLEN];
12062#endif
12063
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012064 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012065
12066#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12067 lnum = get_tv_lnum(argvars); /* -1 on type error */
12068 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12069
12070 vim_memset(str, NUL, sizeof(str));
12071
12072 if (rettv_list_alloc(rettv) != FAIL)
12073 {
12074 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12075 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12076 && curwin->w_p_cole > 0)
12077 {
12078 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
12079 syntax_flags = get_syntax_info(&matchid);
12080
12081 /* get the conceal character */
12082 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
12083 {
12084 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020012085 if (cchar == NUL && curwin->w_p_cole == 1)
12086 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012087 if (cchar != NUL)
12088 {
12089# ifdef FEAT_MBYTE
12090 if (has_mbyte)
12091 (*mb_char2bytes)(cchar, str);
12092 else
12093# endif
12094 str[0] = cchar;
12095 }
12096 }
12097 }
12098
12099 list_append_number(rettv->vval.v_list,
12100 (syntax_flags & HL_CONCEAL) != 0);
12101 /* -1 to auto-determine strlen */
12102 list_append_string(rettv->vval.v_list, str, -1);
12103 list_append_number(rettv->vval.v_list, matchid);
12104 }
12105#endif
12106}
12107
12108/*
12109 * "synstack(lnum, col)" function
12110 */
12111 static void
12112f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
12113{
12114#ifdef FEAT_SYN_HL
12115 linenr_T lnum;
12116 colnr_T col;
12117 int i;
12118 int id;
12119#endif
12120
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012121 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012122
12123#ifdef FEAT_SYN_HL
12124 lnum = get_tv_lnum(argvars); /* -1 on type error */
12125 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12126
12127 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12128 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12129 && rettv_list_alloc(rettv) != FAIL)
12130 {
12131 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
12132 for (i = 0; ; ++i)
12133 {
12134 id = syn_get_stack_item(i);
12135 if (id < 0)
12136 break;
12137 if (list_append_number(rettv->vval.v_list, id) == FAIL)
12138 break;
12139 }
12140 }
12141#endif
12142}
12143
12144 static void
12145get_cmd_output_as_rettv(
12146 typval_T *argvars,
12147 typval_T *rettv,
12148 int retlist)
12149{
12150 char_u *res = NULL;
12151 char_u *p;
12152 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012153 int err = FALSE;
12154 FILE *fd;
12155 list_T *list = NULL;
12156 int flags = SHELL_SILENT;
12157
12158 rettv->v_type = VAR_STRING;
12159 rettv->vval.v_string = NULL;
12160 if (check_restricted() || check_secure())
12161 goto errret;
12162
12163 if (argvars[1].v_type != VAR_UNKNOWN)
12164 {
12165 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010012166 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012167 * command.
12168 */
12169 if ((infile = vim_tempname('i', TRUE)) == NULL)
12170 {
12171 EMSG(_(e_notmp));
12172 goto errret;
12173 }
12174
12175 fd = mch_fopen((char *)infile, WRITEBIN);
12176 if (fd == NULL)
12177 {
12178 EMSG2(_(e_notopen), infile);
12179 goto errret;
12180 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010012181 if (argvars[1].v_type == VAR_NUMBER)
12182 {
12183 linenr_T lnum;
12184 buf_T *buf;
12185
12186 buf = buflist_findnr(argvars[1].vval.v_number);
12187 if (buf == NULL)
12188 {
12189 EMSGN(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010012190 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010012191 goto errret;
12192 }
12193
12194 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
12195 {
12196 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
12197 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
12198 {
12199 err = TRUE;
12200 break;
12201 }
12202 if (putc(NL, fd) == EOF)
12203 {
12204 err = TRUE;
12205 break;
12206 }
12207 }
12208 }
12209 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012210 {
12211 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
12212 err = TRUE;
12213 }
12214 else
12215 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010012216 size_t len;
12217 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012218
12219 p = get_tv_string_buf_chk(&argvars[1], buf);
12220 if (p == NULL)
12221 {
12222 fclose(fd);
12223 goto errret; /* type error; errmsg already given */
12224 }
12225 len = STRLEN(p);
12226 if (len > 0 && fwrite(p, len, 1, fd) != 1)
12227 err = TRUE;
12228 }
12229 if (fclose(fd) != 0)
12230 err = TRUE;
12231 if (err)
12232 {
12233 EMSG(_("E677: Error writing temp file"));
12234 goto errret;
12235 }
12236 }
12237
12238 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
12239 * echoes typeahead, that messes up the display. */
12240 if (!msg_silent)
12241 flags += SHELL_COOKED;
12242
12243 if (retlist)
12244 {
12245 int len;
12246 listitem_T *li;
12247 char_u *s = NULL;
12248 char_u *start;
12249 char_u *end;
12250 int i;
12251
12252 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
12253 if (res == NULL)
12254 goto errret;
12255
12256 list = list_alloc();
12257 if (list == NULL)
12258 goto errret;
12259
12260 for (i = 0; i < len; ++i)
12261 {
12262 start = res + i;
12263 while (i < len && res[i] != NL)
12264 ++i;
12265 end = res + i;
12266
12267 s = alloc((unsigned)(end - start + 1));
12268 if (s == NULL)
12269 goto errret;
12270
12271 for (p = s; start < end; ++p, ++start)
12272 *p = *start == NUL ? NL : *start;
12273 *p = NUL;
12274
12275 li = listitem_alloc();
12276 if (li == NULL)
12277 {
12278 vim_free(s);
12279 goto errret;
12280 }
12281 li->li_tv.v_type = VAR_STRING;
12282 li->li_tv.v_lock = 0;
12283 li->li_tv.vval.v_string = s;
12284 list_append(list, li);
12285 }
12286
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012287 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012288 list = NULL;
12289 }
12290 else
12291 {
12292 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
12293#ifdef USE_CR
12294 /* translate <CR> into <NL> */
12295 if (res != NULL)
12296 {
12297 char_u *s;
12298
12299 for (s = res; *s; ++s)
12300 {
12301 if (*s == CAR)
12302 *s = NL;
12303 }
12304 }
12305#else
12306# ifdef USE_CRNL
12307 /* translate <CR><NL> into <NL> */
12308 if (res != NULL)
12309 {
12310 char_u *s, *d;
12311
12312 d = res;
12313 for (s = res; *s; ++s)
12314 {
12315 if (s[0] == CAR && s[1] == NL)
12316 ++s;
12317 *d++ = *s;
12318 }
12319 *d = NUL;
12320 }
12321# endif
12322#endif
12323 rettv->vval.v_string = res;
12324 res = NULL;
12325 }
12326
12327errret:
12328 if (infile != NULL)
12329 {
12330 mch_remove(infile);
12331 vim_free(infile);
12332 }
12333 if (res != NULL)
12334 vim_free(res);
12335 if (list != NULL)
12336 list_free(list);
12337}
12338
12339/*
12340 * "system()" function
12341 */
12342 static void
12343f_system(typval_T *argvars, typval_T *rettv)
12344{
12345 get_cmd_output_as_rettv(argvars, rettv, FALSE);
12346}
12347
12348/*
12349 * "systemlist()" function
12350 */
12351 static void
12352f_systemlist(typval_T *argvars, typval_T *rettv)
12353{
12354 get_cmd_output_as_rettv(argvars, rettv, TRUE);
12355}
12356
12357/*
12358 * "tabpagebuflist()" function
12359 */
12360 static void
12361f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12362{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012363 tabpage_T *tp;
12364 win_T *wp = NULL;
12365
12366 if (argvars[0].v_type == VAR_UNKNOWN)
12367 wp = firstwin;
12368 else
12369 {
12370 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12371 if (tp != NULL)
12372 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12373 }
12374 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
12375 {
12376 for (; wp != NULL; wp = wp->w_next)
12377 if (list_append_number(rettv->vval.v_list,
12378 wp->w_buffer->b_fnum) == FAIL)
12379 break;
12380 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012381}
12382
12383
12384/*
12385 * "tabpagenr()" function
12386 */
12387 static void
12388f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
12389{
12390 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012391 char_u *arg;
12392
12393 if (argvars[0].v_type != VAR_UNKNOWN)
12394 {
12395 arg = get_tv_string_chk(&argvars[0]);
12396 nr = 0;
12397 if (arg != NULL)
12398 {
12399 if (STRCMP(arg, "$") == 0)
12400 nr = tabpage_index(NULL) - 1;
12401 else
12402 EMSG2(_(e_invexpr2), arg);
12403 }
12404 }
12405 else
12406 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012407 rettv->vval.v_number = nr;
12408}
12409
12410
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012411static int get_winnr(tabpage_T *tp, typval_T *argvar);
12412
12413/*
12414 * Common code for tabpagewinnr() and winnr().
12415 */
12416 static int
12417get_winnr(tabpage_T *tp, typval_T *argvar)
12418{
12419 win_T *twin;
12420 int nr = 1;
12421 win_T *wp;
12422 char_u *arg;
12423
12424 twin = (tp == curtab) ? curwin : tp->tp_curwin;
12425 if (argvar->v_type != VAR_UNKNOWN)
12426 {
12427 arg = get_tv_string_chk(argvar);
12428 if (arg == NULL)
12429 nr = 0; /* type error; errmsg already given */
12430 else if (STRCMP(arg, "$") == 0)
12431 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
12432 else if (STRCMP(arg, "#") == 0)
12433 {
12434 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
12435 if (twin == NULL)
12436 nr = 0;
12437 }
12438 else
12439 {
12440 EMSG2(_(e_invexpr2), arg);
12441 nr = 0;
12442 }
12443 }
12444
12445 if (nr > 0)
12446 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12447 wp != twin; wp = wp->w_next)
12448 {
12449 if (wp == NULL)
12450 {
12451 /* didn't find it in this tabpage */
12452 nr = 0;
12453 break;
12454 }
12455 ++nr;
12456 }
12457 return nr;
12458}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012459
12460/*
12461 * "tabpagewinnr()" function
12462 */
12463 static void
12464f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
12465{
12466 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012467 tabpage_T *tp;
12468
12469 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12470 if (tp == NULL)
12471 nr = 0;
12472 else
12473 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012474 rettv->vval.v_number = nr;
12475}
12476
12477
12478/*
12479 * "tagfiles()" function
12480 */
12481 static void
12482f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
12483{
12484 char_u *fname;
12485 tagname_T tn;
12486 int first;
12487
12488 if (rettv_list_alloc(rettv) == FAIL)
12489 return;
12490 fname = alloc(MAXPATHL);
12491 if (fname == NULL)
12492 return;
12493
12494 for (first = TRUE; ; first = FALSE)
12495 if (get_tagfname(&tn, first, fname) == FAIL
12496 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
12497 break;
12498 tagname_free(&tn);
12499 vim_free(fname);
12500}
12501
12502/*
12503 * "taglist()" function
12504 */
12505 static void
12506f_taglist(typval_T *argvars, typval_T *rettv)
12507{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012508 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012509 char_u *tag_pattern;
12510
12511 tag_pattern = get_tv_string(&argvars[0]);
12512
12513 rettv->vval.v_number = FALSE;
12514 if (*tag_pattern == NUL)
12515 return;
12516
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012517 if (argvars[1].v_type != VAR_UNKNOWN)
12518 fname = get_tv_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012519 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012520 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012521}
12522
12523/*
12524 * "tempname()" function
12525 */
12526 static void
12527f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
12528{
12529 static int x = 'A';
12530
12531 rettv->v_type = VAR_STRING;
12532 rettv->vval.v_string = vim_tempname(x, FALSE);
12533
12534 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
12535 * names. Skip 'I' and 'O', they are used for shell redirection. */
12536 do
12537 {
12538 if (x == 'Z')
12539 x = '0';
12540 else if (x == '9')
12541 x = 'A';
12542 else
12543 {
12544#ifdef EBCDIC
12545 if (x == 'I')
12546 x = 'J';
12547 else if (x == 'R')
12548 x = 'S';
12549 else
12550#endif
12551 ++x;
12552 }
12553 } while (x == 'I' || x == 'O');
12554}
12555
12556#ifdef FEAT_FLOAT
12557/*
12558 * "tan()" function
12559 */
12560 static void
12561f_tan(typval_T *argvars, typval_T *rettv)
12562{
12563 float_T f = 0.0;
12564
12565 rettv->v_type = VAR_FLOAT;
12566 if (get_float_arg(argvars, &f) == OK)
12567 rettv->vval.v_float = tan(f);
12568 else
12569 rettv->vval.v_float = 0.0;
12570}
12571
12572/*
12573 * "tanh()" function
12574 */
12575 static void
12576f_tanh(typval_T *argvars, typval_T *rettv)
12577{
12578 float_T f = 0.0;
12579
12580 rettv->v_type = VAR_FLOAT;
12581 if (get_float_arg(argvars, &f) == OK)
12582 rettv->vval.v_float = tanh(f);
12583 else
12584 rettv->vval.v_float = 0.0;
12585}
12586#endif
12587
12588/*
12589 * "test_alloc_fail(id, countdown, repeat)" function
12590 */
12591 static void
12592f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
12593{
12594 if (argvars[0].v_type != VAR_NUMBER
12595 || argvars[0].vval.v_number <= 0
12596 || argvars[1].v_type != VAR_NUMBER
12597 || argvars[1].vval.v_number < 0
12598 || argvars[2].v_type != VAR_NUMBER)
12599 EMSG(_(e_invarg));
12600 else
12601 {
12602 alloc_fail_id = argvars[0].vval.v_number;
12603 if (alloc_fail_id >= aid_last)
12604 EMSG(_(e_invarg));
12605 alloc_fail_countdown = argvars[1].vval.v_number;
12606 alloc_fail_repeat = argvars[2].vval.v_number;
12607 did_outofmem_msg = FALSE;
12608 }
12609}
12610
12611/*
12612 * "test_autochdir()"
12613 */
12614 static void
12615f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12616{
12617#if defined(FEAT_AUTOCHDIR)
12618 test_autochdir = TRUE;
12619#endif
12620}
12621
12622/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020012623 * "test_feedinput()"
12624 */
12625 static void
12626f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
12627{
12628#ifdef USE_INPUT_BUF
12629 char_u *val = get_tv_string_chk(&argvars[0]);
12630
12631 if (val != NULL)
12632 {
12633 trash_input_buf();
12634 add_to_input_buf_csi(val, (int)STRLEN(val));
12635 }
12636#endif
12637}
12638
12639/*
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012640 * "test_disable({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012641 */
12642 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012643f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012644{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012645 char_u *name = (char_u *)"";
12646 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012647 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012648
12649 if (argvars[0].v_type != VAR_STRING
12650 || (argvars[1].v_type) != VAR_NUMBER)
12651 EMSG(_(e_invarg));
12652 else
12653 {
12654 name = get_tv_string_chk(&argvars[0]);
12655 val = (int)get_tv_number(&argvars[1]);
12656
12657 if (STRCMP(name, (char_u *)"redraw") == 0)
12658 disable_redraw_for_testing = val;
12659 else if (STRCMP(name, (char_u *)"char_avail") == 0)
12660 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012661 else if (STRCMP(name, (char_u *)"starting") == 0)
12662 {
12663 if (val)
12664 {
12665 if (save_starting < 0)
12666 save_starting = starting;
12667 starting = 0;
12668 }
12669 else
12670 {
12671 starting = save_starting;
12672 save_starting = -1;
12673 }
12674 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012675 else if (STRCMP(name, (char_u *)"ALL") == 0)
12676 {
12677 disable_char_avail_for_testing = FALSE;
12678 disable_redraw_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012679 if (save_starting >= 0)
12680 {
12681 starting = save_starting;
12682 save_starting = -1;
12683 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012684 }
12685 else
12686 EMSG2(_(e_invarg2), name);
12687 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012688}
12689
12690/*
12691 * "test_garbagecollect_now()" function
12692 */
12693 static void
12694f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12695{
12696 /* This is dangerous, any Lists and Dicts used internally may be freed
12697 * while still in use. */
12698 garbage_collect(TRUE);
12699}
12700
Bram Moolenaare0c31f62017-03-01 15:07:05 +010012701/*
12702 * "test_ignore_error()" function
12703 */
12704 static void
12705f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
12706{
12707 ignore_error_for_testing(get_tv_string(&argvars[0]));
12708}
12709
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012710#ifdef FEAT_JOB_CHANNEL
12711 static void
12712f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
12713{
12714 rettv->v_type = VAR_CHANNEL;
12715 rettv->vval.v_channel = NULL;
12716}
12717#endif
12718
12719 static void
12720f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
12721{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012722 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012723}
12724
12725#ifdef FEAT_JOB_CHANNEL
12726 static void
12727f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
12728{
12729 rettv->v_type = VAR_JOB;
12730 rettv->vval.v_job = NULL;
12731}
12732#endif
12733
12734 static void
12735f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
12736{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012737 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012738}
12739
12740 static void
12741f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
12742{
12743 rettv->v_type = VAR_PARTIAL;
12744 rettv->vval.v_partial = NULL;
12745}
12746
12747 static void
12748f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
12749{
12750 rettv->v_type = VAR_STRING;
12751 rettv->vval.v_string = NULL;
12752}
12753
12754 static void
12755f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
12756{
12757 time_for_testing = (time_t)get_tv_number(&argvars[0]);
12758}
12759
12760#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
12761/*
12762 * Get a callback from "arg". It can be a Funcref or a function name.
12763 * When "arg" is zero return an empty string.
12764 * Return NULL for an invalid argument.
12765 */
12766 char_u *
12767get_callback(typval_T *arg, partial_T **pp)
12768{
12769 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
12770 {
12771 *pp = arg->vval.v_partial;
12772 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012773 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012774 }
12775 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012776 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012777 {
12778 func_ref(arg->vval.v_string);
12779 return arg->vval.v_string;
12780 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012781 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
12782 return (char_u *)"";
12783 EMSG(_("E921: Invalid callback argument"));
12784 return NULL;
12785}
12786
12787/*
12788 * Unref/free "callback" and "partial" retured by get_callback().
12789 */
12790 void
12791free_callback(char_u *callback, partial_T *partial)
12792{
12793 if (partial != NULL)
12794 partial_unref(partial);
12795 else if (callback != NULL)
12796 {
12797 func_unref(callback);
12798 vim_free(callback);
12799 }
12800}
12801#endif
12802
12803#ifdef FEAT_TIMERS
12804/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020012805 * "timer_info([timer])" function
12806 */
12807 static void
12808f_timer_info(typval_T *argvars, typval_T *rettv)
12809{
12810 timer_T *timer = NULL;
12811
12812 if (rettv_list_alloc(rettv) != OK)
12813 return;
12814 if (argvars[0].v_type != VAR_UNKNOWN)
12815 {
12816 if (argvars[0].v_type != VAR_NUMBER)
12817 EMSG(_(e_number_exp));
12818 else
12819 {
12820 timer = find_timer((int)get_tv_number(&argvars[0]));
12821 if (timer != NULL)
12822 add_timer_info(rettv, timer);
12823 }
12824 }
12825 else
12826 add_timer_info_all(rettv);
12827}
12828
12829/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012830 * "timer_pause(timer, paused)" function
12831 */
12832 static void
12833f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
12834{
12835 timer_T *timer = NULL;
12836 int paused = (int)get_tv_number(&argvars[1]);
12837
12838 if (argvars[0].v_type != VAR_NUMBER)
12839 EMSG(_(e_number_exp));
12840 else
12841 {
12842 timer = find_timer((int)get_tv_number(&argvars[0]));
12843 if (timer != NULL)
12844 timer->tr_paused = paused;
12845 }
12846}
12847
12848/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012849 * "timer_start(time, callback [, options])" function
12850 */
12851 static void
12852f_timer_start(typval_T *argvars, typval_T *rettv)
12853{
Bram Moolenaar75537a92016-09-05 22:45:28 +020012854 long msec = (long)get_tv_number(&argvars[0]);
12855 timer_T *timer;
12856 int repeat = 0;
12857 char_u *callback;
12858 dict_T *dict;
12859 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012860
Bram Moolenaar75537a92016-09-05 22:45:28 +020012861 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012862 if (check_secure())
12863 return;
12864 if (argvars[2].v_type != VAR_UNKNOWN)
12865 {
12866 if (argvars[2].v_type != VAR_DICT
12867 || (dict = argvars[2].vval.v_dict) == NULL)
12868 {
12869 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
12870 return;
12871 }
12872 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
12873 repeat = get_dict_number(dict, (char_u *)"repeat");
12874 }
12875
Bram Moolenaar75537a92016-09-05 22:45:28 +020012876 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012877 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020012878 return;
12879
12880 timer = create_timer(msec, repeat);
12881 if (timer == NULL)
12882 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012883 else
12884 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020012885 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020012886 timer->tr_callback = vim_strsave(callback);
12887 else
12888 /* pointer into the partial */
12889 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020012890 timer->tr_partial = partial;
12891 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012892 }
12893}
12894
12895/*
12896 * "timer_stop(timer)" function
12897 */
12898 static void
12899f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
12900{
12901 timer_T *timer;
12902
12903 if (argvars[0].v_type != VAR_NUMBER)
12904 {
12905 EMSG(_(e_number_exp));
12906 return;
12907 }
12908 timer = find_timer((int)get_tv_number(&argvars[0]));
12909 if (timer != NULL)
12910 stop_timer(timer);
12911}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012912
12913/*
12914 * "timer_stopall()" function
12915 */
12916 static void
12917f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12918{
12919 stop_all_timers();
12920}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012921#endif
12922
12923/*
12924 * "tolower(string)" function
12925 */
12926 static void
12927f_tolower(typval_T *argvars, typval_T *rettv)
12928{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012929 rettv->v_type = VAR_STRING;
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +010012930 rettv->vval.v_string = strlow_save(get_tv_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012931}
12932
12933/*
12934 * "toupper(string)" function
12935 */
12936 static void
12937f_toupper(typval_T *argvars, typval_T *rettv)
12938{
12939 rettv->v_type = VAR_STRING;
12940 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
12941}
12942
12943/*
12944 * "tr(string, fromstr, tostr)" function
12945 */
12946 static void
12947f_tr(typval_T *argvars, typval_T *rettv)
12948{
12949 char_u *in_str;
12950 char_u *fromstr;
12951 char_u *tostr;
12952 char_u *p;
12953#ifdef FEAT_MBYTE
12954 int inlen;
12955 int fromlen;
12956 int tolen;
12957 int idx;
12958 char_u *cpstr;
12959 int cplen;
12960 int first = TRUE;
12961#endif
12962 char_u buf[NUMBUFLEN];
12963 char_u buf2[NUMBUFLEN];
12964 garray_T ga;
12965
12966 in_str = get_tv_string(&argvars[0]);
12967 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
12968 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
12969
12970 /* Default return value: empty string. */
12971 rettv->v_type = VAR_STRING;
12972 rettv->vval.v_string = NULL;
12973 if (fromstr == NULL || tostr == NULL)
12974 return; /* type error; errmsg already given */
12975 ga_init2(&ga, (int)sizeof(char), 80);
12976
12977#ifdef FEAT_MBYTE
12978 if (!has_mbyte)
12979#endif
12980 /* not multi-byte: fromstr and tostr must be the same length */
12981 if (STRLEN(fromstr) != STRLEN(tostr))
12982 {
12983#ifdef FEAT_MBYTE
12984error:
12985#endif
12986 EMSG2(_(e_invarg2), fromstr);
12987 ga_clear(&ga);
12988 return;
12989 }
12990
12991 /* fromstr and tostr have to contain the same number of chars */
12992 while (*in_str != NUL)
12993 {
12994#ifdef FEAT_MBYTE
12995 if (has_mbyte)
12996 {
12997 inlen = (*mb_ptr2len)(in_str);
12998 cpstr = in_str;
12999 cplen = inlen;
13000 idx = 0;
13001 for (p = fromstr; *p != NUL; p += fromlen)
13002 {
13003 fromlen = (*mb_ptr2len)(p);
13004 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
13005 {
13006 for (p = tostr; *p != NUL; p += tolen)
13007 {
13008 tolen = (*mb_ptr2len)(p);
13009 if (idx-- == 0)
13010 {
13011 cplen = tolen;
13012 cpstr = p;
13013 break;
13014 }
13015 }
13016 if (*p == NUL) /* tostr is shorter than fromstr */
13017 goto error;
13018 break;
13019 }
13020 ++idx;
13021 }
13022
13023 if (first && cpstr == in_str)
13024 {
13025 /* Check that fromstr and tostr have the same number of
13026 * (multi-byte) characters. Done only once when a character
13027 * of in_str doesn't appear in fromstr. */
13028 first = FALSE;
13029 for (p = tostr; *p != NUL; p += tolen)
13030 {
13031 tolen = (*mb_ptr2len)(p);
13032 --idx;
13033 }
13034 if (idx != 0)
13035 goto error;
13036 }
13037
13038 (void)ga_grow(&ga, cplen);
13039 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
13040 ga.ga_len += cplen;
13041
13042 in_str += inlen;
13043 }
13044 else
13045#endif
13046 {
13047 /* When not using multi-byte chars we can do it faster. */
13048 p = vim_strchr(fromstr, *in_str);
13049 if (p != NULL)
13050 ga_append(&ga, tostr[p - fromstr]);
13051 else
13052 ga_append(&ga, *in_str);
13053 ++in_str;
13054 }
13055 }
13056
13057 /* add a terminating NUL */
13058 (void)ga_grow(&ga, 1);
13059 ga_append(&ga, NUL);
13060
13061 rettv->vval.v_string = ga.ga_data;
13062}
13063
13064#ifdef FEAT_FLOAT
13065/*
13066 * "trunc({float})" function
13067 */
13068 static void
13069f_trunc(typval_T *argvars, typval_T *rettv)
13070{
13071 float_T f = 0.0;
13072
13073 rettv->v_type = VAR_FLOAT;
13074 if (get_float_arg(argvars, &f) == OK)
13075 /* trunc() is not in C90, use floor() or ceil() instead. */
13076 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
13077 else
13078 rettv->vval.v_float = 0.0;
13079}
13080#endif
13081
13082/*
13083 * "type(expr)" function
13084 */
13085 static void
13086f_type(typval_T *argvars, typval_T *rettv)
13087{
13088 int n = -1;
13089
13090 switch (argvars[0].v_type)
13091 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020013092 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
13093 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013094 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020013095 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
13096 case VAR_LIST: n = VAR_TYPE_LIST; break;
13097 case VAR_DICT: n = VAR_TYPE_DICT; break;
13098 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013099 case VAR_SPECIAL:
13100 if (argvars[0].vval.v_number == VVAL_FALSE
13101 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020013102 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013103 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020013104 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013105 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020013106 case VAR_JOB: n = VAR_TYPE_JOB; break;
13107 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013108 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010013109 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013110 n = -1;
13111 break;
13112 }
13113 rettv->vval.v_number = n;
13114}
13115
13116/*
13117 * "undofile(name)" function
13118 */
13119 static void
13120f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
13121{
13122 rettv->v_type = VAR_STRING;
13123#ifdef FEAT_PERSISTENT_UNDO
13124 {
13125 char_u *fname = get_tv_string(&argvars[0]);
13126
13127 if (*fname == NUL)
13128 {
13129 /* If there is no file name there will be no undo file. */
13130 rettv->vval.v_string = NULL;
13131 }
13132 else
13133 {
13134 char_u *ffname = FullName_save(fname, FALSE);
13135
13136 if (ffname != NULL)
13137 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
13138 vim_free(ffname);
13139 }
13140 }
13141#else
13142 rettv->vval.v_string = NULL;
13143#endif
13144}
13145
13146/*
13147 * "undotree()" function
13148 */
13149 static void
13150f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
13151{
13152 if (rettv_dict_alloc(rettv) == OK)
13153 {
13154 dict_T *dict = rettv->vval.v_dict;
13155 list_T *list;
13156
13157 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
13158 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
13159 dict_add_nr_str(dict, "save_last",
13160 (long)curbuf->b_u_save_nr_last, NULL);
13161 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
13162 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
13163 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
13164
13165 list = list_alloc();
13166 if (list != NULL)
13167 {
13168 u_eval_tree(curbuf->b_u_oldhead, list);
13169 dict_add_list(dict, "entries", list);
13170 }
13171 }
13172}
13173
13174/*
13175 * "values(dict)" function
13176 */
13177 static void
13178f_values(typval_T *argvars, typval_T *rettv)
13179{
13180 dict_list(argvars, rettv, 1);
13181}
13182
13183/*
13184 * "virtcol(string)" function
13185 */
13186 static void
13187f_virtcol(typval_T *argvars, typval_T *rettv)
13188{
13189 colnr_T vcol = 0;
13190 pos_T *fp;
13191 int fnum = curbuf->b_fnum;
13192
13193 fp = var2fpos(&argvars[0], FALSE, &fnum);
13194 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
13195 && fnum == curbuf->b_fnum)
13196 {
13197 getvvcol(curwin, fp, NULL, NULL, &vcol);
13198 ++vcol;
13199 }
13200
13201 rettv->vval.v_number = vcol;
13202}
13203
13204/*
13205 * "visualmode()" function
13206 */
13207 static void
13208f_visualmode(typval_T *argvars, typval_T *rettv)
13209{
13210 char_u str[2];
13211
13212 rettv->v_type = VAR_STRING;
13213 str[0] = curbuf->b_visual_mode_eval;
13214 str[1] = NUL;
13215 rettv->vval.v_string = vim_strsave(str);
13216
13217 /* A non-zero number or non-empty string argument: reset mode. */
13218 if (non_zero_arg(&argvars[0]))
13219 curbuf->b_visual_mode_eval = NUL;
13220}
13221
13222/*
13223 * "wildmenumode()" function
13224 */
13225 static void
13226f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13227{
13228#ifdef FEAT_WILDMENU
13229 if (wild_menu_showing)
13230 rettv->vval.v_number = 1;
13231#endif
13232}
13233
13234/*
13235 * "winbufnr(nr)" function
13236 */
13237 static void
13238f_winbufnr(typval_T *argvars, typval_T *rettv)
13239{
13240 win_T *wp;
13241
13242 wp = find_win_by_nr(&argvars[0], NULL);
13243 if (wp == NULL)
13244 rettv->vval.v_number = -1;
13245 else
13246 rettv->vval.v_number = wp->w_buffer->b_fnum;
13247}
13248
13249/*
13250 * "wincol()" function
13251 */
13252 static void
13253f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
13254{
13255 validate_cursor();
13256 rettv->vval.v_number = curwin->w_wcol + 1;
13257}
13258
13259/*
13260 * "winheight(nr)" function
13261 */
13262 static void
13263f_winheight(typval_T *argvars, typval_T *rettv)
13264{
13265 win_T *wp;
13266
13267 wp = find_win_by_nr(&argvars[0], NULL);
13268 if (wp == NULL)
13269 rettv->vval.v_number = -1;
13270 else
13271 rettv->vval.v_number = wp->w_height;
13272}
13273
13274/*
13275 * "winline()" function
13276 */
13277 static void
13278f_winline(typval_T *argvars UNUSED, typval_T *rettv)
13279{
13280 validate_cursor();
13281 rettv->vval.v_number = curwin->w_wrow + 1;
13282}
13283
13284/*
13285 * "winnr()" function
13286 */
13287 static void
13288f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
13289{
13290 int nr = 1;
13291
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013292 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013293 rettv->vval.v_number = nr;
13294}
13295
13296/*
13297 * "winrestcmd()" function
13298 */
13299 static void
13300f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
13301{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013302 win_T *wp;
13303 int winnr = 1;
13304 garray_T ga;
13305 char_u buf[50];
13306
13307 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020013308 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013309 {
13310 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
13311 ga_concat(&ga, buf);
13312 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
13313 ga_concat(&ga, buf);
13314 ++winnr;
13315 }
13316 ga_append(&ga, NUL);
13317
13318 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013319 rettv->v_type = VAR_STRING;
13320}
13321
13322/*
13323 * "winrestview()" function
13324 */
13325 static void
13326f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
13327{
13328 dict_T *dict;
13329
13330 if (argvars[0].v_type != VAR_DICT
13331 || (dict = argvars[0].vval.v_dict) == NULL)
13332 EMSG(_(e_invarg));
13333 else
13334 {
13335 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
13336 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
13337 if (dict_find(dict, (char_u *)"col", -1) != NULL)
13338 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
13339#ifdef FEAT_VIRTUALEDIT
13340 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
13341 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
13342#endif
13343 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
13344 {
13345 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
13346 curwin->w_set_curswant = FALSE;
13347 }
13348
13349 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
13350 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
13351#ifdef FEAT_DIFF
13352 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
13353 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
13354#endif
13355 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
13356 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
13357 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
13358 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
13359
13360 check_cursor();
13361 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020013362 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013363 changed_window_setting();
13364
13365 if (curwin->w_topline <= 0)
13366 curwin->w_topline = 1;
13367 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
13368 curwin->w_topline = curbuf->b_ml.ml_line_count;
13369#ifdef FEAT_DIFF
13370 check_topfill(curwin, TRUE);
13371#endif
13372 }
13373}
13374
13375/*
13376 * "winsaveview()" function
13377 */
13378 static void
13379f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
13380{
13381 dict_T *dict;
13382
13383 if (rettv_dict_alloc(rettv) == FAIL)
13384 return;
13385 dict = rettv->vval.v_dict;
13386
13387 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
13388 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
13389#ifdef FEAT_VIRTUALEDIT
13390 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
13391#endif
13392 update_curswant();
13393 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
13394
13395 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
13396#ifdef FEAT_DIFF
13397 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
13398#endif
13399 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
13400 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
13401}
13402
13403/*
13404 * "winwidth(nr)" function
13405 */
13406 static void
13407f_winwidth(typval_T *argvars, typval_T *rettv)
13408{
13409 win_T *wp;
13410
13411 wp = find_win_by_nr(&argvars[0], NULL);
13412 if (wp == NULL)
13413 rettv->vval.v_number = -1;
13414 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013415 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013416}
13417
13418/*
13419 * "wordcount()" function
13420 */
13421 static void
13422f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
13423{
13424 if (rettv_dict_alloc(rettv) == FAIL)
13425 return;
13426 cursor_pos_info(rettv->vval.v_dict);
13427}
13428
13429/*
13430 * "writefile()" function
13431 */
13432 static void
13433f_writefile(typval_T *argvars, typval_T *rettv)
13434{
13435 int binary = FALSE;
13436 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013437#ifdef HAVE_FSYNC
13438 int do_fsync = p_fs;
13439#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013440 char_u *fname;
13441 FILE *fd;
13442 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013443 listitem_T *li;
13444 list_T *list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013445
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013446 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013447 if (check_restricted() || check_secure())
13448 return;
13449
13450 if (argvars[0].v_type != VAR_LIST)
13451 {
13452 EMSG2(_(e_listarg), "writefile()");
13453 return;
13454 }
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013455 list = argvars[0].vval.v_list;
13456 if (list == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013457 return;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013458 for (li = list->lv_first; li != NULL; li = li->li_next)
13459 if (get_tv_string_chk(&li->li_tv) == NULL)
13460 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013461
13462 if (argvars[2].v_type != VAR_UNKNOWN)
13463 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013464 char_u *arg2 = get_tv_string_chk(&argvars[2]);
13465
13466 if (arg2 == NULL)
13467 return;
13468 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013469 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013470 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013471 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013472#ifdef HAVE_FSYNC
13473 if (vim_strchr(arg2, 's') != NULL)
13474 do_fsync = TRUE;
13475 else if (vim_strchr(arg2, 'S') != NULL)
13476 do_fsync = FALSE;
13477#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013478 }
13479
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013480 fname = get_tv_string_chk(&argvars[1]);
13481 if (fname == NULL)
13482 return;
13483
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013484 /* Always open the file in binary mode, library functions have a mind of
13485 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013486 if (*fname == NUL || (fd = mch_fopen((char *)fname,
13487 append ? APPENDBIN : WRITEBIN)) == NULL)
13488 {
13489 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
13490 ret = -1;
13491 }
13492 else
13493 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013494 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013495 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013496#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010013497 else if (do_fsync)
13498 /* Ignore the error, the user wouldn't know what to do about it.
13499 * May happen for a device. */
13500 ignored = fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013501#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013502 fclose(fd);
13503 }
13504
13505 rettv->vval.v_number = ret;
13506}
13507
13508/*
13509 * "xor(expr, expr)" function
13510 */
13511 static void
13512f_xor(typval_T *argvars, typval_T *rettv)
13513{
13514 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
13515 ^ get_tv_number_chk(&argvars[1], NULL);
13516}
13517
13518
13519#endif /* FEAT_EVAL */