blob: 9aacd30786735759d777154940af80ff2f36d52f [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalfunc.c: Builtin functions
12 */
13#define USING_FLOAT_STUFF
14
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef VMS
24# include <float.h>
25#endif
26
Bram Moolenaard0573012017-10-28 21:11:06 +020027#ifdef MACOS_X
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020028# include <time.h> /* for time_t */
29#endif
30
31static char *e_listarg = N_("E686: Argument of %s must be a List");
32#ifdef FEAT_QUICKFIX
33static char *e_stringreq = N_("E928: String required");
34#endif
35
36#ifdef FEAT_FLOAT
37static void f_abs(typval_T *argvars, typval_T *rettv);
38static void f_acos(typval_T *argvars, typval_T *rettv);
39#endif
40static void f_add(typval_T *argvars, typval_T *rettv);
41static void f_and(typval_T *argvars, typval_T *rettv);
42static void f_append(typval_T *argvars, typval_T *rettv);
43static void f_argc(typval_T *argvars, typval_T *rettv);
44static void f_argidx(typval_T *argvars, typval_T *rettv);
45static void f_arglistid(typval_T *argvars, typval_T *rettv);
46static void f_argv(typval_T *argvars, typval_T *rettv);
47static void f_assert_equal(typval_T *argvars, typval_T *rettv);
48static void f_assert_exception(typval_T *argvars, typval_T *rettv);
49static void f_assert_fails(typval_T *argvars, typval_T *rettv);
50static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaar61c04492016-07-23 15:35:35 +020051static void f_assert_inrange(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020052static void f_assert_match(typval_T *argvars, typval_T *rettv);
53static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
54static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar42205552017-03-18 19:42:22 +010055static void f_assert_report(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020056static void f_assert_true(typval_T *argvars, typval_T *rettv);
57#ifdef FEAT_FLOAT
58static void f_asin(typval_T *argvars, typval_T *rettv);
59static void f_atan(typval_T *argvars, typval_T *rettv);
60static void f_atan2(typval_T *argvars, typval_T *rettv);
61#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010062#ifdef FEAT_BEVAL
63static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010064# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010065static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010066# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010067#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020068static void f_browse(typval_T *argvars, typval_T *rettv);
69static void f_browsedir(typval_T *argvars, typval_T *rettv);
70static void f_bufexists(typval_T *argvars, typval_T *rettv);
71static void f_buflisted(typval_T *argvars, typval_T *rettv);
72static void f_bufloaded(typval_T *argvars, typval_T *rettv);
73static void f_bufname(typval_T *argvars, typval_T *rettv);
74static void f_bufnr(typval_T *argvars, typval_T *rettv);
75static void f_bufwinid(typval_T *argvars, typval_T *rettv);
76static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
77static void f_byte2line(typval_T *argvars, typval_T *rettv);
78static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
79static void f_byteidx(typval_T *argvars, typval_T *rettv);
80static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
81static void f_call(typval_T *argvars, typval_T *rettv);
82#ifdef FEAT_FLOAT
83static void f_ceil(typval_T *argvars, typval_T *rettv);
84#endif
85#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +010086static void f_ch_canread(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020087static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0874a832016-09-01 15:11:51 +020088static void f_ch_close_in(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020089static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
90static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
91static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
92static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
93static void f_ch_info(typval_T *argvars, typval_T *rettv);
94static void f_ch_log(typval_T *argvars, typval_T *rettv);
95static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
96static void f_ch_open(typval_T *argvars, typval_T *rettv);
97static void f_ch_read(typval_T *argvars, typval_T *rettv);
98static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
99static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
100static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
101static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
102static void f_ch_status(typval_T *argvars, typval_T *rettv);
103#endif
104static void f_changenr(typval_T *argvars, typval_T *rettv);
105static void f_char2nr(typval_T *argvars, typval_T *rettv);
106static void f_cindent(typval_T *argvars, typval_T *rettv);
107static void f_clearmatches(typval_T *argvars, typval_T *rettv);
108static void f_col(typval_T *argvars, typval_T *rettv);
109#if defined(FEAT_INS_EXPAND)
110static void f_complete(typval_T *argvars, typval_T *rettv);
111static void f_complete_add(typval_T *argvars, typval_T *rettv);
112static void f_complete_check(typval_T *argvars, typval_T *rettv);
113#endif
114static void f_confirm(typval_T *argvars, typval_T *rettv);
115static void f_copy(typval_T *argvars, typval_T *rettv);
116#ifdef FEAT_FLOAT
117static void f_cos(typval_T *argvars, typval_T *rettv);
118static void f_cosh(typval_T *argvars, typval_T *rettv);
119#endif
120static void f_count(typval_T *argvars, typval_T *rettv);
121static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
122static void f_cursor(typval_T *argsvars, typval_T *rettv);
123static void f_deepcopy(typval_T *argvars, typval_T *rettv);
124static void f_delete(typval_T *argvars, typval_T *rettv);
125static void f_did_filetype(typval_T *argvars, typval_T *rettv);
126static void f_diff_filler(typval_T *argvars, typval_T *rettv);
127static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
128static void f_empty(typval_T *argvars, typval_T *rettv);
129static void f_escape(typval_T *argvars, typval_T *rettv);
130static void f_eval(typval_T *argvars, typval_T *rettv);
131static void f_eventhandler(typval_T *argvars, typval_T *rettv);
132static void f_executable(typval_T *argvars, typval_T *rettv);
133static void f_execute(typval_T *argvars, typval_T *rettv);
134static void f_exepath(typval_T *argvars, typval_T *rettv);
135static void f_exists(typval_T *argvars, typval_T *rettv);
136#ifdef FEAT_FLOAT
137static void f_exp(typval_T *argvars, typval_T *rettv);
138#endif
139static void f_expand(typval_T *argvars, typval_T *rettv);
140static void f_extend(typval_T *argvars, typval_T *rettv);
141static void f_feedkeys(typval_T *argvars, typval_T *rettv);
142static void f_filereadable(typval_T *argvars, typval_T *rettv);
143static void f_filewritable(typval_T *argvars, typval_T *rettv);
144static void f_filter(typval_T *argvars, typval_T *rettv);
145static void f_finddir(typval_T *argvars, typval_T *rettv);
146static void f_findfile(typval_T *argvars, typval_T *rettv);
147#ifdef FEAT_FLOAT
148static void f_float2nr(typval_T *argvars, typval_T *rettv);
149static void f_floor(typval_T *argvars, typval_T *rettv);
150static void f_fmod(typval_T *argvars, typval_T *rettv);
151#endif
152static void f_fnameescape(typval_T *argvars, typval_T *rettv);
153static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
154static void f_foldclosed(typval_T *argvars, typval_T *rettv);
155static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
156static void f_foldlevel(typval_T *argvars, typval_T *rettv);
157static void f_foldtext(typval_T *argvars, typval_T *rettv);
158static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
159static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200160static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200161static void f_function(typval_T *argvars, typval_T *rettv);
162static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
163static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200164static void f_getbufinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200165static void f_getbufline(typval_T *argvars, typval_T *rettv);
166static void f_getbufvar(typval_T *argvars, typval_T *rettv);
167static void f_getchar(typval_T *argvars, typval_T *rettv);
168static void f_getcharmod(typval_T *argvars, typval_T *rettv);
169static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
170static void f_getcmdline(typval_T *argvars, typval_T *rettv);
171#if defined(FEAT_CMDL_COMPL)
172static void f_getcompletion(typval_T *argvars, typval_T *rettv);
173#endif
174static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
175static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
176static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
177static void f_getcwd(typval_T *argvars, typval_T *rettv);
178static void f_getfontname(typval_T *argvars, typval_T *rettv);
179static void f_getfperm(typval_T *argvars, typval_T *rettv);
180static void f_getfsize(typval_T *argvars, typval_T *rettv);
181static void f_getftime(typval_T *argvars, typval_T *rettv);
182static void f_getftype(typval_T *argvars, typval_T *rettv);
183static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200184static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200185static void f_getmatches(typval_T *argvars, typval_T *rettv);
186static void f_getpid(typval_T *argvars, typval_T *rettv);
187static void f_getcurpos(typval_T *argvars, typval_T *rettv);
188static void f_getpos(typval_T *argvars, typval_T *rettv);
189static void f_getqflist(typval_T *argvars, typval_T *rettv);
190static void f_getreg(typval_T *argvars, typval_T *rettv);
191static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200192static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200193static void f_gettabvar(typval_T *argvars, typval_T *rettv);
194static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200195static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200196static void f_getwinposx(typval_T *argvars, typval_T *rettv);
197static void f_getwinposy(typval_T *argvars, typval_T *rettv);
198static void f_getwinvar(typval_T *argvars, typval_T *rettv);
199static void f_glob(typval_T *argvars, typval_T *rettv);
200static void f_globpath(typval_T *argvars, typval_T *rettv);
201static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
202static void f_has(typval_T *argvars, typval_T *rettv);
203static void f_has_key(typval_T *argvars, typval_T *rettv);
204static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
205static void f_hasmapto(typval_T *argvars, typval_T *rettv);
206static void f_histadd(typval_T *argvars, typval_T *rettv);
207static void f_histdel(typval_T *argvars, typval_T *rettv);
208static void f_histget(typval_T *argvars, typval_T *rettv);
209static void f_histnr(typval_T *argvars, typval_T *rettv);
210static void f_hlID(typval_T *argvars, typval_T *rettv);
211static void f_hlexists(typval_T *argvars, typval_T *rettv);
212static void f_hostname(typval_T *argvars, typval_T *rettv);
213static void f_iconv(typval_T *argvars, typval_T *rettv);
214static void f_indent(typval_T *argvars, typval_T *rettv);
215static void f_index(typval_T *argvars, typval_T *rettv);
216static void f_input(typval_T *argvars, typval_T *rettv);
217static void f_inputdialog(typval_T *argvars, typval_T *rettv);
218static void f_inputlist(typval_T *argvars, typval_T *rettv);
219static void f_inputrestore(typval_T *argvars, typval_T *rettv);
220static void f_inputsave(typval_T *argvars, typval_T *rettv);
221static void f_inputsecret(typval_T *argvars, typval_T *rettv);
222static void f_insert(typval_T *argvars, typval_T *rettv);
223static void f_invert(typval_T *argvars, typval_T *rettv);
224static void f_isdirectory(typval_T *argvars, typval_T *rettv);
225static void f_islocked(typval_T *argvars, typval_T *rettv);
226#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
227static void f_isnan(typval_T *argvars, typval_T *rettv);
228#endif
229static void f_items(typval_T *argvars, typval_T *rettv);
230#ifdef FEAT_JOB_CHANNEL
231static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
232static void f_job_info(typval_T *argvars, typval_T *rettv);
233static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
234static void f_job_start(typval_T *argvars, typval_T *rettv);
235static void f_job_stop(typval_T *argvars, typval_T *rettv);
236static void f_job_status(typval_T *argvars, typval_T *rettv);
237#endif
238static void f_join(typval_T *argvars, typval_T *rettv);
239static void f_js_decode(typval_T *argvars, typval_T *rettv);
240static void f_js_encode(typval_T *argvars, typval_T *rettv);
241static void f_json_decode(typval_T *argvars, typval_T *rettv);
242static void f_json_encode(typval_T *argvars, typval_T *rettv);
243static void f_keys(typval_T *argvars, typval_T *rettv);
244static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
245static void f_len(typval_T *argvars, typval_T *rettv);
246static void f_libcall(typval_T *argvars, typval_T *rettv);
247static void f_libcallnr(typval_T *argvars, typval_T *rettv);
248static void f_line(typval_T *argvars, typval_T *rettv);
249static void f_line2byte(typval_T *argvars, typval_T *rettv);
250static void f_lispindent(typval_T *argvars, typval_T *rettv);
251static void f_localtime(typval_T *argvars, typval_T *rettv);
252#ifdef FEAT_FLOAT
253static void f_log(typval_T *argvars, typval_T *rettv);
254static void f_log10(typval_T *argvars, typval_T *rettv);
255#endif
256#ifdef FEAT_LUA
257static void f_luaeval(typval_T *argvars, typval_T *rettv);
258#endif
259static void f_map(typval_T *argvars, typval_T *rettv);
260static void f_maparg(typval_T *argvars, typval_T *rettv);
261static void f_mapcheck(typval_T *argvars, typval_T *rettv);
262static void f_match(typval_T *argvars, typval_T *rettv);
263static void f_matchadd(typval_T *argvars, typval_T *rettv);
264static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
265static void f_matcharg(typval_T *argvars, typval_T *rettv);
266static void f_matchdelete(typval_T *argvars, typval_T *rettv);
267static void f_matchend(typval_T *argvars, typval_T *rettv);
268static void f_matchlist(typval_T *argvars, typval_T *rettv);
269static void f_matchstr(typval_T *argvars, typval_T *rettv);
270static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
271static void f_max(typval_T *argvars, typval_T *rettv);
272static void f_min(typval_T *argvars, typval_T *rettv);
273#ifdef vim_mkdir
274static void f_mkdir(typval_T *argvars, typval_T *rettv);
275#endif
276static void f_mode(typval_T *argvars, typval_T *rettv);
277#ifdef FEAT_MZSCHEME
278static void f_mzeval(typval_T *argvars, typval_T *rettv);
279#endif
280static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
281static void f_nr2char(typval_T *argvars, typval_T *rettv);
282static void f_or(typval_T *argvars, typval_T *rettv);
283static void f_pathshorten(typval_T *argvars, typval_T *rettv);
284#ifdef FEAT_PERL
285static void f_perleval(typval_T *argvars, typval_T *rettv);
286#endif
287#ifdef FEAT_FLOAT
288static void f_pow(typval_T *argvars, typval_T *rettv);
289#endif
290static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
291static void f_printf(typval_T *argvars, typval_T *rettv);
292static void f_pumvisible(typval_T *argvars, typval_T *rettv);
293#ifdef FEAT_PYTHON3
294static void f_py3eval(typval_T *argvars, typval_T *rettv);
295#endif
296#ifdef FEAT_PYTHON
297static void f_pyeval(typval_T *argvars, typval_T *rettv);
298#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100299#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
300static void f_pyxeval(typval_T *argvars, typval_T *rettv);
301#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200302static void f_range(typval_T *argvars, typval_T *rettv);
303static void f_readfile(typval_T *argvars, typval_T *rettv);
304static void f_reltime(typval_T *argvars, typval_T *rettv);
305#ifdef FEAT_FLOAT
306static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
307#endif
308static void f_reltimestr(typval_T *argvars, typval_T *rettv);
309static void f_remote_expr(typval_T *argvars, typval_T *rettv);
310static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
311static void f_remote_peek(typval_T *argvars, typval_T *rettv);
312static void f_remote_read(typval_T *argvars, typval_T *rettv);
313static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100314static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200315static void f_remove(typval_T *argvars, typval_T *rettv);
316static void f_rename(typval_T *argvars, typval_T *rettv);
317static void f_repeat(typval_T *argvars, typval_T *rettv);
318static void f_resolve(typval_T *argvars, typval_T *rettv);
319static void f_reverse(typval_T *argvars, typval_T *rettv);
320#ifdef FEAT_FLOAT
321static void f_round(typval_T *argvars, typval_T *rettv);
322#endif
323static void f_screenattr(typval_T *argvars, typval_T *rettv);
324static void f_screenchar(typval_T *argvars, typval_T *rettv);
325static void f_screencol(typval_T *argvars, typval_T *rettv);
326static void f_screenrow(typval_T *argvars, typval_T *rettv);
327static void f_search(typval_T *argvars, typval_T *rettv);
328static void f_searchdecl(typval_T *argvars, typval_T *rettv);
329static void f_searchpair(typval_T *argvars, typval_T *rettv);
330static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
331static void f_searchpos(typval_T *argvars, typval_T *rettv);
332static void f_server2client(typval_T *argvars, typval_T *rettv);
333static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200334static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200335static void f_setbufvar(typval_T *argvars, typval_T *rettv);
336static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
337static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
338static void f_setfperm(typval_T *argvars, typval_T *rettv);
339static void f_setline(typval_T *argvars, typval_T *rettv);
340static void f_setloclist(typval_T *argvars, typval_T *rettv);
341static void f_setmatches(typval_T *argvars, typval_T *rettv);
342static void f_setpos(typval_T *argvars, typval_T *rettv);
343static void f_setqflist(typval_T *argvars, typval_T *rettv);
344static void f_setreg(typval_T *argvars, typval_T *rettv);
345static void f_settabvar(typval_T *argvars, typval_T *rettv);
346static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
347static void f_setwinvar(typval_T *argvars, typval_T *rettv);
348#ifdef FEAT_CRYPT
349static void f_sha256(typval_T *argvars, typval_T *rettv);
350#endif /* FEAT_CRYPT */
351static void f_shellescape(typval_T *argvars, typval_T *rettv);
352static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
353static void f_simplify(typval_T *argvars, typval_T *rettv);
354#ifdef FEAT_FLOAT
355static void f_sin(typval_T *argvars, typval_T *rettv);
356static void f_sinh(typval_T *argvars, typval_T *rettv);
357#endif
358static void f_sort(typval_T *argvars, typval_T *rettv);
359static void f_soundfold(typval_T *argvars, typval_T *rettv);
360static void f_spellbadword(typval_T *argvars, typval_T *rettv);
361static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
362static void f_split(typval_T *argvars, typval_T *rettv);
363#ifdef FEAT_FLOAT
364static void f_sqrt(typval_T *argvars, typval_T *rettv);
365static void f_str2float(typval_T *argvars, typval_T *rettv);
366#endif
367static void f_str2nr(typval_T *argvars, typval_T *rettv);
368static void f_strchars(typval_T *argvars, typval_T *rettv);
369#ifdef HAVE_STRFTIME
370static void f_strftime(typval_T *argvars, typval_T *rettv);
371#endif
372static void f_strgetchar(typval_T *argvars, typval_T *rettv);
373static void f_stridx(typval_T *argvars, typval_T *rettv);
374static void f_string(typval_T *argvars, typval_T *rettv);
375static void f_strlen(typval_T *argvars, typval_T *rettv);
376static void f_strcharpart(typval_T *argvars, typval_T *rettv);
377static void f_strpart(typval_T *argvars, typval_T *rettv);
378static void f_strridx(typval_T *argvars, typval_T *rettv);
379static void f_strtrans(typval_T *argvars, typval_T *rettv);
380static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
381static void f_strwidth(typval_T *argvars, typval_T *rettv);
382static void f_submatch(typval_T *argvars, typval_T *rettv);
383static void f_substitute(typval_T *argvars, typval_T *rettv);
384static void f_synID(typval_T *argvars, typval_T *rettv);
385static void f_synIDattr(typval_T *argvars, typval_T *rettv);
386static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
387static void f_synstack(typval_T *argvars, typval_T *rettv);
388static void f_synconcealed(typval_T *argvars, typval_T *rettv);
389static void f_system(typval_T *argvars, typval_T *rettv);
390static void f_systemlist(typval_T *argvars, typval_T *rettv);
391static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
392static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
393static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
394static void f_taglist(typval_T *argvars, typval_T *rettv);
395static void f_tagfiles(typval_T *argvars, typval_T *rettv);
396static void f_tempname(typval_T *argvars, typval_T *rettv);
397static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
398static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200399static void f_test_feedinput(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100400static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200401static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100402static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200403#ifdef FEAT_JOB_CHANNEL
404static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
405#endif
406static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
407#ifdef FEAT_JOB_CHANNEL
408static void f_test_null_job(typval_T *argvars, typval_T *rettv);
409#endif
410static void f_test_null_list(typval_T *argvars, typval_T *rettv);
411static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
412static void f_test_null_string(typval_T *argvars, typval_T *rettv);
413static void f_test_settime(typval_T *argvars, typval_T *rettv);
414#ifdef FEAT_FLOAT
415static void f_tan(typval_T *argvars, typval_T *rettv);
416static void f_tanh(typval_T *argvars, typval_T *rettv);
417#endif
418#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200419static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200420static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200421static void f_timer_start(typval_T *argvars, typval_T *rettv);
422static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200423static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200424#endif
425static void f_tolower(typval_T *argvars, typval_T *rettv);
426static void f_toupper(typval_T *argvars, typval_T *rettv);
427static void f_tr(typval_T *argvars, typval_T *rettv);
428#ifdef FEAT_FLOAT
429static void f_trunc(typval_T *argvars, typval_T *rettv);
430#endif
431static void f_type(typval_T *argvars, typval_T *rettv);
432static void f_undofile(typval_T *argvars, typval_T *rettv);
433static void f_undotree(typval_T *argvars, typval_T *rettv);
434static void f_uniq(typval_T *argvars, typval_T *rettv);
435static void f_values(typval_T *argvars, typval_T *rettv);
436static void f_virtcol(typval_T *argvars, typval_T *rettv);
437static void f_visualmode(typval_T *argvars, typval_T *rettv);
438static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
439static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
440static void f_win_getid(typval_T *argvars, typval_T *rettv);
441static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
442static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
443static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100444static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200445static void f_winbufnr(typval_T *argvars, typval_T *rettv);
446static void f_wincol(typval_T *argvars, typval_T *rettv);
447static void f_winheight(typval_T *argvars, typval_T *rettv);
448static void f_winline(typval_T *argvars, typval_T *rettv);
449static void f_winnr(typval_T *argvars, typval_T *rettv);
450static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
451static void f_winrestview(typval_T *argvars, typval_T *rettv);
452static void f_winsaveview(typval_T *argvars, typval_T *rettv);
453static void f_winwidth(typval_T *argvars, typval_T *rettv);
454static void f_writefile(typval_T *argvars, typval_T *rettv);
455static void f_wordcount(typval_T *argvars, typval_T *rettv);
456static void f_xor(typval_T *argvars, typval_T *rettv);
457
458/*
459 * Array with names and number of arguments of all internal functions
460 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
461 */
462static struct fst
463{
464 char *f_name; /* function name */
465 char f_min_argc; /* minimal number of arguments */
466 char f_max_argc; /* maximal number of arguments */
467 void (*f_func)(typval_T *args, typval_T *rvar);
468 /* implementation of function */
469} functions[] =
470{
471#ifdef FEAT_FLOAT
472 {"abs", 1, 1, f_abs},
473 {"acos", 1, 1, f_acos}, /* WJMc */
474#endif
475 {"add", 2, 2, f_add},
476 {"and", 2, 2, f_and},
477 {"append", 2, 2, f_append},
478 {"argc", 0, 0, f_argc},
479 {"argidx", 0, 0, f_argidx},
480 {"arglistid", 0, 2, f_arglistid},
481 {"argv", 0, 1, f_argv},
482#ifdef FEAT_FLOAT
483 {"asin", 1, 1, f_asin}, /* WJMc */
484#endif
485 {"assert_equal", 2, 3, f_assert_equal},
486 {"assert_exception", 1, 2, f_assert_exception},
487 {"assert_fails", 1, 2, f_assert_fails},
488 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100489 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200490 {"assert_match", 2, 3, f_assert_match},
491 {"assert_notequal", 2, 3, f_assert_notequal},
492 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100493 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200494 {"assert_true", 1, 2, f_assert_true},
495#ifdef FEAT_FLOAT
496 {"atan", 1, 1, f_atan},
497 {"atan2", 2, 2, f_atan2},
498#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100499#ifdef FEAT_BEVAL
500 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100501# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100502 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100503# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100504#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200505 {"browse", 4, 4, f_browse},
506 {"browsedir", 2, 2, f_browsedir},
507 {"bufexists", 1, 1, f_bufexists},
508 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
509 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
510 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
511 {"buflisted", 1, 1, f_buflisted},
512 {"bufloaded", 1, 1, f_bufloaded},
513 {"bufname", 1, 1, f_bufname},
514 {"bufnr", 1, 2, f_bufnr},
515 {"bufwinid", 1, 1, f_bufwinid},
516 {"bufwinnr", 1, 1, f_bufwinnr},
517 {"byte2line", 1, 1, f_byte2line},
518 {"byteidx", 2, 2, f_byteidx},
519 {"byteidxcomp", 2, 2, f_byteidxcomp},
520 {"call", 2, 3, f_call},
521#ifdef FEAT_FLOAT
522 {"ceil", 1, 1, f_ceil},
523#endif
524#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100525 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200526 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200527 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200528 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
529 {"ch_evalraw", 2, 3, f_ch_evalraw},
530 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
531 {"ch_getjob", 1, 1, f_ch_getjob},
532 {"ch_info", 1, 1, f_ch_info},
533 {"ch_log", 1, 2, f_ch_log},
534 {"ch_logfile", 1, 2, f_ch_logfile},
535 {"ch_open", 1, 2, f_ch_open},
536 {"ch_read", 1, 2, f_ch_read},
537 {"ch_readraw", 1, 2, f_ch_readraw},
538 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
539 {"ch_sendraw", 2, 3, f_ch_sendraw},
540 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200541 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200542#endif
543 {"changenr", 0, 0, f_changenr},
544 {"char2nr", 1, 2, f_char2nr},
545 {"cindent", 1, 1, f_cindent},
546 {"clearmatches", 0, 0, f_clearmatches},
547 {"col", 1, 1, f_col},
548#if defined(FEAT_INS_EXPAND)
549 {"complete", 2, 2, f_complete},
550 {"complete_add", 1, 1, f_complete_add},
551 {"complete_check", 0, 0, f_complete_check},
552#endif
553 {"confirm", 1, 4, f_confirm},
554 {"copy", 1, 1, f_copy},
555#ifdef FEAT_FLOAT
556 {"cos", 1, 1, f_cos},
557 {"cosh", 1, 1, f_cosh},
558#endif
559 {"count", 2, 4, f_count},
560 {"cscope_connection",0,3, f_cscope_connection},
561 {"cursor", 1, 3, f_cursor},
562 {"deepcopy", 1, 2, f_deepcopy},
563 {"delete", 1, 2, f_delete},
564 {"did_filetype", 0, 0, f_did_filetype},
565 {"diff_filler", 1, 1, f_diff_filler},
566 {"diff_hlID", 2, 2, f_diff_hlID},
567 {"empty", 1, 1, f_empty},
568 {"escape", 2, 2, f_escape},
569 {"eval", 1, 1, f_eval},
570 {"eventhandler", 0, 0, f_eventhandler},
571 {"executable", 1, 1, f_executable},
572 {"execute", 1, 2, f_execute},
573 {"exepath", 1, 1, f_exepath},
574 {"exists", 1, 1, f_exists},
575#ifdef FEAT_FLOAT
576 {"exp", 1, 1, f_exp},
577#endif
578 {"expand", 1, 3, f_expand},
579 {"extend", 2, 3, f_extend},
580 {"feedkeys", 1, 2, f_feedkeys},
581 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
582 {"filereadable", 1, 1, f_filereadable},
583 {"filewritable", 1, 1, f_filewritable},
584 {"filter", 2, 2, f_filter},
585 {"finddir", 1, 3, f_finddir},
586 {"findfile", 1, 3, f_findfile},
587#ifdef FEAT_FLOAT
588 {"float2nr", 1, 1, f_float2nr},
589 {"floor", 1, 1, f_floor},
590 {"fmod", 2, 2, f_fmod},
591#endif
592 {"fnameescape", 1, 1, f_fnameescape},
593 {"fnamemodify", 2, 2, f_fnamemodify},
594 {"foldclosed", 1, 1, f_foldclosed},
595 {"foldclosedend", 1, 1, f_foldclosedend},
596 {"foldlevel", 1, 1, f_foldlevel},
597 {"foldtext", 0, 0, f_foldtext},
598 {"foldtextresult", 1, 1, f_foldtextresult},
599 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200600 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200601 {"function", 1, 3, f_function},
602 {"garbagecollect", 0, 1, f_garbagecollect},
603 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200604 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200605 {"getbufline", 2, 3, f_getbufline},
606 {"getbufvar", 2, 3, f_getbufvar},
607 {"getchar", 0, 1, f_getchar},
608 {"getcharmod", 0, 0, f_getcharmod},
609 {"getcharsearch", 0, 0, f_getcharsearch},
610 {"getcmdline", 0, 0, f_getcmdline},
611 {"getcmdpos", 0, 0, f_getcmdpos},
612 {"getcmdtype", 0, 0, f_getcmdtype},
613 {"getcmdwintype", 0, 0, f_getcmdwintype},
614#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200615 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200616#endif
617 {"getcurpos", 0, 0, f_getcurpos},
618 {"getcwd", 0, 2, f_getcwd},
619 {"getfontname", 0, 1, f_getfontname},
620 {"getfperm", 1, 1, f_getfperm},
621 {"getfsize", 1, 1, f_getfsize},
622 {"getftime", 1, 1, f_getftime},
623 {"getftype", 1, 1, f_getftype},
624 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200625 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200626 {"getmatches", 0, 0, f_getmatches},
627 {"getpid", 0, 0, f_getpid},
628 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200629 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200630 {"getreg", 0, 3, f_getreg},
631 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200632 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200633 {"gettabvar", 2, 3, f_gettabvar},
634 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200635 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200636 {"getwinposx", 0, 0, f_getwinposx},
637 {"getwinposy", 0, 0, f_getwinposy},
638 {"getwinvar", 2, 3, f_getwinvar},
639 {"glob", 1, 4, f_glob},
640 {"glob2regpat", 1, 1, f_glob2regpat},
641 {"globpath", 2, 5, f_globpath},
642 {"has", 1, 1, f_has},
643 {"has_key", 2, 2, f_has_key},
644 {"haslocaldir", 0, 2, f_haslocaldir},
645 {"hasmapto", 1, 3, f_hasmapto},
646 {"highlightID", 1, 1, f_hlID}, /* obsolete */
647 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
648 {"histadd", 2, 2, f_histadd},
649 {"histdel", 1, 2, f_histdel},
650 {"histget", 1, 2, f_histget},
651 {"histnr", 1, 1, f_histnr},
652 {"hlID", 1, 1, f_hlID},
653 {"hlexists", 1, 1, f_hlexists},
654 {"hostname", 0, 0, f_hostname},
655 {"iconv", 3, 3, f_iconv},
656 {"indent", 1, 1, f_indent},
657 {"index", 2, 4, f_index},
658 {"input", 1, 3, f_input},
659 {"inputdialog", 1, 3, f_inputdialog},
660 {"inputlist", 1, 1, f_inputlist},
661 {"inputrestore", 0, 0, f_inputrestore},
662 {"inputsave", 0, 0, f_inputsave},
663 {"inputsecret", 1, 2, f_inputsecret},
664 {"insert", 2, 3, f_insert},
665 {"invert", 1, 1, f_invert},
666 {"isdirectory", 1, 1, f_isdirectory},
667 {"islocked", 1, 1, f_islocked},
668#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
669 {"isnan", 1, 1, f_isnan},
670#endif
671 {"items", 1, 1, f_items},
672#ifdef FEAT_JOB_CHANNEL
673 {"job_getchannel", 1, 1, f_job_getchannel},
674 {"job_info", 1, 1, f_job_info},
675 {"job_setoptions", 2, 2, f_job_setoptions},
676 {"job_start", 1, 2, f_job_start},
677 {"job_status", 1, 1, f_job_status},
678 {"job_stop", 1, 2, f_job_stop},
679#endif
680 {"join", 1, 2, f_join},
681 {"js_decode", 1, 1, f_js_decode},
682 {"js_encode", 1, 1, f_js_encode},
683 {"json_decode", 1, 1, f_json_decode},
684 {"json_encode", 1, 1, f_json_encode},
685 {"keys", 1, 1, f_keys},
686 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
687 {"len", 1, 1, f_len},
688 {"libcall", 3, 3, f_libcall},
689 {"libcallnr", 3, 3, f_libcallnr},
690 {"line", 1, 1, f_line},
691 {"line2byte", 1, 1, f_line2byte},
692 {"lispindent", 1, 1, f_lispindent},
693 {"localtime", 0, 0, f_localtime},
694#ifdef FEAT_FLOAT
695 {"log", 1, 1, f_log},
696 {"log10", 1, 1, f_log10},
697#endif
698#ifdef FEAT_LUA
699 {"luaeval", 1, 2, f_luaeval},
700#endif
701 {"map", 2, 2, f_map},
702 {"maparg", 1, 4, f_maparg},
703 {"mapcheck", 1, 3, f_mapcheck},
704 {"match", 2, 4, f_match},
705 {"matchadd", 2, 5, f_matchadd},
706 {"matchaddpos", 2, 5, f_matchaddpos},
707 {"matcharg", 1, 1, f_matcharg},
708 {"matchdelete", 1, 1, f_matchdelete},
709 {"matchend", 2, 4, f_matchend},
710 {"matchlist", 2, 4, f_matchlist},
711 {"matchstr", 2, 4, f_matchstr},
712 {"matchstrpos", 2, 4, f_matchstrpos},
713 {"max", 1, 1, f_max},
714 {"min", 1, 1, f_min},
715#ifdef vim_mkdir
716 {"mkdir", 1, 3, f_mkdir},
717#endif
718 {"mode", 0, 1, f_mode},
719#ifdef FEAT_MZSCHEME
720 {"mzeval", 1, 1, f_mzeval},
721#endif
722 {"nextnonblank", 1, 1, f_nextnonblank},
723 {"nr2char", 1, 2, f_nr2char},
724 {"or", 2, 2, f_or},
725 {"pathshorten", 1, 1, f_pathshorten},
726#ifdef FEAT_PERL
727 {"perleval", 1, 1, f_perleval},
728#endif
729#ifdef FEAT_FLOAT
730 {"pow", 2, 2, f_pow},
731#endif
732 {"prevnonblank", 1, 1, f_prevnonblank},
733 {"printf", 2, 19, f_printf},
734 {"pumvisible", 0, 0, f_pumvisible},
735#ifdef FEAT_PYTHON3
736 {"py3eval", 1, 1, f_py3eval},
737#endif
738#ifdef FEAT_PYTHON
739 {"pyeval", 1, 1, f_pyeval},
740#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100741#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
742 {"pyxeval", 1, 1, f_pyxeval},
743#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200744 {"range", 1, 3, f_range},
745 {"readfile", 1, 3, f_readfile},
746 {"reltime", 0, 2, f_reltime},
747#ifdef FEAT_FLOAT
748 {"reltimefloat", 1, 1, f_reltimefloat},
749#endif
750 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100751 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200752 {"remote_foreground", 1, 1, f_remote_foreground},
753 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100754 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200755 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100756 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200757 {"remove", 2, 3, f_remove},
758 {"rename", 2, 2, f_rename},
759 {"repeat", 2, 2, f_repeat},
760 {"resolve", 1, 1, f_resolve},
761 {"reverse", 1, 1, f_reverse},
762#ifdef FEAT_FLOAT
763 {"round", 1, 1, f_round},
764#endif
765 {"screenattr", 2, 2, f_screenattr},
766 {"screenchar", 2, 2, f_screenchar},
767 {"screencol", 0, 0, f_screencol},
768 {"screenrow", 0, 0, f_screenrow},
769 {"search", 1, 4, f_search},
770 {"searchdecl", 1, 3, f_searchdecl},
771 {"searchpair", 3, 7, f_searchpair},
772 {"searchpairpos", 3, 7, f_searchpairpos},
773 {"searchpos", 1, 4, f_searchpos},
774 {"server2client", 2, 2, f_server2client},
775 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200776 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200777 {"setbufvar", 3, 3, f_setbufvar},
778 {"setcharsearch", 1, 1, f_setcharsearch},
779 {"setcmdpos", 1, 1, f_setcmdpos},
780 {"setfperm", 2, 2, f_setfperm},
781 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200782 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200783 {"setmatches", 1, 1, f_setmatches},
784 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200785 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200786 {"setreg", 2, 3, f_setreg},
787 {"settabvar", 3, 3, f_settabvar},
788 {"settabwinvar", 4, 4, f_settabwinvar},
789 {"setwinvar", 3, 3, f_setwinvar},
790#ifdef FEAT_CRYPT
791 {"sha256", 1, 1, f_sha256},
792#endif
793 {"shellescape", 1, 2, f_shellescape},
794 {"shiftwidth", 0, 0, f_shiftwidth},
795 {"simplify", 1, 1, f_simplify},
796#ifdef FEAT_FLOAT
797 {"sin", 1, 1, f_sin},
798 {"sinh", 1, 1, f_sinh},
799#endif
800 {"sort", 1, 3, f_sort},
801 {"soundfold", 1, 1, f_soundfold},
802 {"spellbadword", 0, 1, f_spellbadword},
803 {"spellsuggest", 1, 3, f_spellsuggest},
804 {"split", 1, 3, f_split},
805#ifdef FEAT_FLOAT
806 {"sqrt", 1, 1, f_sqrt},
807 {"str2float", 1, 1, f_str2float},
808#endif
809 {"str2nr", 1, 2, f_str2nr},
810 {"strcharpart", 2, 3, f_strcharpart},
811 {"strchars", 1, 2, f_strchars},
812 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
813#ifdef HAVE_STRFTIME
814 {"strftime", 1, 2, f_strftime},
815#endif
816 {"strgetchar", 2, 2, f_strgetchar},
817 {"stridx", 2, 3, f_stridx},
818 {"string", 1, 1, f_string},
819 {"strlen", 1, 1, f_strlen},
820 {"strpart", 2, 3, f_strpart},
821 {"strridx", 2, 3, f_strridx},
822 {"strtrans", 1, 1, f_strtrans},
823 {"strwidth", 1, 1, f_strwidth},
824 {"submatch", 1, 2, f_submatch},
825 {"substitute", 4, 4, f_substitute},
826 {"synID", 3, 3, f_synID},
827 {"synIDattr", 2, 3, f_synIDattr},
828 {"synIDtrans", 1, 1, f_synIDtrans},
829 {"synconcealed", 2, 2, f_synconcealed},
830 {"synstack", 2, 2, f_synstack},
831 {"system", 1, 2, f_system},
832 {"systemlist", 1, 2, f_systemlist},
833 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
834 {"tabpagenr", 0, 1, f_tabpagenr},
835 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
836 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100837 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200838#ifdef FEAT_FLOAT
839 {"tan", 1, 1, f_tan},
840 {"tanh", 1, 1, f_tanh},
841#endif
842 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200843#ifdef FEAT_TERMINAL
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200844 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200845 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200846 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200847 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200848 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200849 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200850 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200851 {"term_getstatus", 1, 1, f_term_getstatus},
852 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200853 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200854 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200855 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200856 {"term_sendkeys", 2, 2, f_term_sendkeys},
857 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200858 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200859#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200860 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
861 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200862 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200863 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100864 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200865#ifdef FEAT_JOB_CHANNEL
866 {"test_null_channel", 0, 0, f_test_null_channel},
867#endif
868 {"test_null_dict", 0, 0, f_test_null_dict},
869#ifdef FEAT_JOB_CHANNEL
870 {"test_null_job", 0, 0, f_test_null_job},
871#endif
872 {"test_null_list", 0, 0, f_test_null_list},
873 {"test_null_partial", 0, 0, f_test_null_partial},
874 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100875 {"test_override", 2, 2, f_test_override},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200876 {"test_settime", 1, 1, f_test_settime},
877#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200878 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200879 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200880 {"timer_start", 2, 3, f_timer_start},
881 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200882 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200883#endif
884 {"tolower", 1, 1, f_tolower},
885 {"toupper", 1, 1, f_toupper},
886 {"tr", 3, 3, f_tr},
887#ifdef FEAT_FLOAT
888 {"trunc", 1, 1, f_trunc},
889#endif
890 {"type", 1, 1, f_type},
891 {"undofile", 1, 1, f_undofile},
892 {"undotree", 0, 0, f_undotree},
893 {"uniq", 1, 3, f_uniq},
894 {"values", 1, 1, f_values},
895 {"virtcol", 1, 1, f_virtcol},
896 {"visualmode", 0, 1, f_visualmode},
897 {"wildmenumode", 0, 0, f_wildmenumode},
898 {"win_findbuf", 1, 1, f_win_findbuf},
899 {"win_getid", 0, 2, f_win_getid},
900 {"win_gotoid", 1, 1, f_win_gotoid},
901 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
902 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100903 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200904 {"winbufnr", 1, 1, f_winbufnr},
905 {"wincol", 0, 0, f_wincol},
906 {"winheight", 1, 1, f_winheight},
907 {"winline", 0, 0, f_winline},
908 {"winnr", 0, 1, f_winnr},
909 {"winrestcmd", 0, 0, f_winrestcmd},
910 {"winrestview", 1, 1, f_winrestview},
911 {"winsaveview", 0, 0, f_winsaveview},
912 {"winwidth", 1, 1, f_winwidth},
913 {"wordcount", 0, 0, f_wordcount},
914 {"writefile", 2, 3, f_writefile},
915 {"xor", 2, 2, f_xor},
916};
917
918#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
919
920/*
921 * Function given to ExpandGeneric() to obtain the list of internal
922 * or user defined function names.
923 */
924 char_u *
925get_function_name(expand_T *xp, int idx)
926{
927 static int intidx = -1;
928 char_u *name;
929
930 if (idx == 0)
931 intidx = -1;
932 if (intidx < 0)
933 {
934 name = get_user_func_name(xp, idx);
935 if (name != NULL)
936 return name;
937 }
938 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
939 {
940 STRCPY(IObuff, functions[intidx].f_name);
941 STRCAT(IObuff, "(");
942 if (functions[intidx].f_max_argc == 0)
943 STRCAT(IObuff, ")");
944 return IObuff;
945 }
946
947 return NULL;
948}
949
950/*
951 * Function given to ExpandGeneric() to obtain the list of internal or
952 * user defined variable or function names.
953 */
954 char_u *
955get_expr_name(expand_T *xp, int idx)
956{
957 static int intidx = -1;
958 char_u *name;
959
960 if (idx == 0)
961 intidx = -1;
962 if (intidx < 0)
963 {
964 name = get_function_name(xp, idx);
965 if (name != NULL)
966 return name;
967 }
968 return get_user_var_name(xp, ++intidx);
969}
970
971#endif /* FEAT_CMDL_COMPL */
972
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200973/*
974 * Find internal function in table above.
975 * Return index, or -1 if not found
976 */
977 int
978find_internal_func(
979 char_u *name) /* name of the function */
980{
981 int first = 0;
982 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
983 int cmp;
984 int x;
985
986 /*
987 * Find the function name in the table. Binary search.
988 */
989 while (first <= last)
990 {
991 x = first + ((unsigned)(last - first) >> 1);
992 cmp = STRCMP(name, functions[x].f_name);
993 if (cmp < 0)
994 last = x - 1;
995 else if (cmp > 0)
996 first = x + 1;
997 else
998 return x;
999 }
1000 return -1;
1001}
1002
1003 int
1004call_internal_func(
1005 char_u *name,
1006 int argcount,
1007 typval_T *argvars,
1008 typval_T *rettv)
1009{
1010 int i;
1011
1012 i = find_internal_func(name);
1013 if (i < 0)
1014 return ERROR_UNKNOWN;
1015 if (argcount < functions[i].f_min_argc)
1016 return ERROR_TOOFEW;
1017 if (argcount > functions[i].f_max_argc)
1018 return ERROR_TOOMANY;
1019 argvars[argcount].v_type = VAR_UNKNOWN;
1020 functions[i].f_func(argvars, rettv);
1021 return ERROR_NONE;
1022}
1023
1024/*
1025 * Return TRUE for a non-zero Number and a non-empty String.
1026 */
1027 static int
1028non_zero_arg(typval_T *argvars)
1029{
1030 return ((argvars[0].v_type == VAR_NUMBER
1031 && argvars[0].vval.v_number != 0)
1032 || (argvars[0].v_type == VAR_SPECIAL
1033 && argvars[0].vval.v_number == VVAL_TRUE)
1034 || (argvars[0].v_type == VAR_STRING
1035 && argvars[0].vval.v_string != NULL
1036 && *argvars[0].vval.v_string != NUL));
1037}
1038
1039/*
1040 * Get the lnum from the first argument.
1041 * Also accepts ".", "$", etc., but that only works for the current buffer.
1042 * Returns -1 on error.
1043 */
1044 static linenr_T
1045get_tv_lnum(typval_T *argvars)
1046{
1047 typval_T rettv;
1048 linenr_T lnum;
1049
1050 lnum = (linenr_T)get_tv_number_chk(&argvars[0], NULL);
1051 if (lnum == 0) /* no valid number, try using line() */
1052 {
1053 rettv.v_type = VAR_NUMBER;
1054 f_line(argvars, &rettv);
1055 lnum = (linenr_T)rettv.vval.v_number;
1056 clear_tv(&rettv);
1057 }
1058 return lnum;
1059}
1060
1061#ifdef FEAT_FLOAT
1062static int get_float_arg(typval_T *argvars, float_T *f);
1063
1064/*
1065 * Get the float value of "argvars[0]" into "f".
1066 * Returns FAIL when the argument is not a Number or Float.
1067 */
1068 static int
1069get_float_arg(typval_T *argvars, float_T *f)
1070{
1071 if (argvars[0].v_type == VAR_FLOAT)
1072 {
1073 *f = argvars[0].vval.v_float;
1074 return OK;
1075 }
1076 if (argvars[0].v_type == VAR_NUMBER)
1077 {
1078 *f = (float_T)argvars[0].vval.v_number;
1079 return OK;
1080 }
1081 EMSG(_("E808: Number or Float required"));
1082 return FAIL;
1083}
1084
1085/*
1086 * "abs(expr)" function
1087 */
1088 static void
1089f_abs(typval_T *argvars, typval_T *rettv)
1090{
1091 if (argvars[0].v_type == VAR_FLOAT)
1092 {
1093 rettv->v_type = VAR_FLOAT;
1094 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1095 }
1096 else
1097 {
1098 varnumber_T n;
1099 int error = FALSE;
1100
1101 n = get_tv_number_chk(&argvars[0], &error);
1102 if (error)
1103 rettv->vval.v_number = -1;
1104 else if (n > 0)
1105 rettv->vval.v_number = n;
1106 else
1107 rettv->vval.v_number = -n;
1108 }
1109}
1110
1111/*
1112 * "acos()" function
1113 */
1114 static void
1115f_acos(typval_T *argvars, typval_T *rettv)
1116{
1117 float_T f = 0.0;
1118
1119 rettv->v_type = VAR_FLOAT;
1120 if (get_float_arg(argvars, &f) == OK)
1121 rettv->vval.v_float = acos(f);
1122 else
1123 rettv->vval.v_float = 0.0;
1124}
1125#endif
1126
1127/*
1128 * "add(list, item)" function
1129 */
1130 static void
1131f_add(typval_T *argvars, typval_T *rettv)
1132{
1133 list_T *l;
1134
1135 rettv->vval.v_number = 1; /* Default: Failed */
1136 if (argvars[0].v_type == VAR_LIST)
1137 {
1138 if ((l = argvars[0].vval.v_list) != NULL
1139 && !tv_check_lock(l->lv_lock,
1140 (char_u *)N_("add() argument"), TRUE)
1141 && list_append_tv(l, &argvars[1]) == OK)
1142 copy_tv(&argvars[0], rettv);
1143 }
1144 else
1145 EMSG(_(e_listreq));
1146}
1147
1148/*
1149 * "and(expr, expr)" function
1150 */
1151 static void
1152f_and(typval_T *argvars, typval_T *rettv)
1153{
1154 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
1155 & get_tv_number_chk(&argvars[1], NULL);
1156}
1157
1158/*
1159 * "append(lnum, string/list)" function
1160 */
1161 static void
1162f_append(typval_T *argvars, typval_T *rettv)
1163{
1164 long lnum;
1165 char_u *line;
1166 list_T *l = NULL;
1167 listitem_T *li = NULL;
1168 typval_T *tv;
1169 long added = 0;
1170
1171 /* When coming here from Insert mode, sync undo, so that this can be
1172 * undone separately from what was previously inserted. */
1173 if (u_sync_once == 2)
1174 {
1175 u_sync_once = 1; /* notify that u_sync() was called */
1176 u_sync(TRUE);
1177 }
1178
1179 lnum = get_tv_lnum(argvars);
1180 if (lnum >= 0
1181 && lnum <= curbuf->b_ml.ml_line_count
1182 && u_save(lnum, lnum + 1) == OK)
1183 {
1184 if (argvars[1].v_type == VAR_LIST)
1185 {
1186 l = argvars[1].vval.v_list;
1187 if (l == NULL)
1188 return;
1189 li = l->lv_first;
1190 }
1191 for (;;)
1192 {
1193 if (l == NULL)
1194 tv = &argvars[1]; /* append a string */
1195 else if (li == NULL)
1196 break; /* end of list */
1197 else
1198 tv = &li->li_tv; /* append item from list */
1199 line = get_tv_string_chk(tv);
1200 if (line == NULL) /* type error */
1201 {
1202 rettv->vval.v_number = 1; /* Failed */
1203 break;
1204 }
1205 ml_append(lnum + added, line, (colnr_T)0, FALSE);
1206 ++added;
1207 if (l == NULL)
1208 break;
1209 li = li->li_next;
1210 }
1211
1212 appended_lines_mark(lnum, added);
1213 if (curwin->w_cursor.lnum > lnum)
1214 curwin->w_cursor.lnum += added;
1215 }
1216 else
1217 rettv->vval.v_number = 1; /* Failed */
1218}
1219
1220/*
1221 * "argc()" function
1222 */
1223 static void
1224f_argc(typval_T *argvars UNUSED, typval_T *rettv)
1225{
1226 rettv->vval.v_number = ARGCOUNT;
1227}
1228
1229/*
1230 * "argidx()" function
1231 */
1232 static void
1233f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1234{
1235 rettv->vval.v_number = curwin->w_arg_idx;
1236}
1237
1238/*
1239 * "arglistid()" function
1240 */
1241 static void
1242f_arglistid(typval_T *argvars, typval_T *rettv)
1243{
1244 win_T *wp;
1245
1246 rettv->vval.v_number = -1;
1247 wp = find_tabwin(&argvars[0], &argvars[1]);
1248 if (wp != NULL)
1249 rettv->vval.v_number = wp->w_alist->id;
1250}
1251
1252/*
1253 * "argv(nr)" function
1254 */
1255 static void
1256f_argv(typval_T *argvars, typval_T *rettv)
1257{
1258 int idx;
1259
1260 if (argvars[0].v_type != VAR_UNKNOWN)
1261 {
1262 idx = (int)get_tv_number_chk(&argvars[0], NULL);
1263 if (idx >= 0 && idx < ARGCOUNT)
1264 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
1265 else
1266 rettv->vval.v_string = NULL;
1267 rettv->v_type = VAR_STRING;
1268 }
1269 else if (rettv_list_alloc(rettv) == OK)
1270 for (idx = 0; idx < ARGCOUNT; ++idx)
1271 list_append_string(rettv->vval.v_list,
1272 alist_name(&ARGLIST[idx]), -1);
1273}
1274
1275/*
1276 * "assert_equal(expected, actual[, msg])" function
1277 */
1278 static void
1279f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
1280{
1281 assert_equal_common(argvars, ASSERT_EQUAL);
1282}
1283
1284/*
1285 * "assert_notequal(expected, actual[, msg])" function
1286 */
1287 static void
1288f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED)
1289{
1290 assert_equal_common(argvars, ASSERT_NOTEQUAL);
1291}
1292
1293/*
1294 * "assert_exception(string[, msg])" function
1295 */
1296 static void
1297f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
1298{
1299 assert_exception(argvars);
1300}
1301
1302/*
1303 * "assert_fails(cmd [, error])" function
1304 */
1305 static void
1306f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
1307{
1308 assert_fails(argvars);
1309}
1310
1311/*
1312 * "assert_false(actual[, msg])" function
1313 */
1314 static void
1315f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
1316{
1317 assert_bool(argvars, FALSE);
1318}
1319
1320/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001321 * "assert_inrange(lower, upper[, msg])" function
1322 */
1323 static void
1324f_assert_inrange(typval_T *argvars, typval_T *rettv UNUSED)
1325{
1326 assert_inrange(argvars);
1327}
1328
1329/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001330 * "assert_match(pattern, actual[, msg])" function
1331 */
1332 static void
1333f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
1334{
1335 assert_match_common(argvars, ASSERT_MATCH);
1336}
1337
1338/*
1339 * "assert_notmatch(pattern, actual[, msg])" function
1340 */
1341 static void
1342f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED)
1343{
1344 assert_match_common(argvars, ASSERT_NOTMATCH);
1345}
1346
1347/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001348 * "assert_report(msg)" function
1349 */
1350 static void
1351f_assert_report(typval_T *argvars, typval_T *rettv UNUSED)
1352{
1353 assert_report(argvars);
1354}
1355
1356/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001357 * "assert_true(actual[, msg])" function
1358 */
1359 static void
1360f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
1361{
1362 assert_bool(argvars, TRUE);
1363}
1364
1365#ifdef FEAT_FLOAT
1366/*
1367 * "asin()" function
1368 */
1369 static void
1370f_asin(typval_T *argvars, typval_T *rettv)
1371{
1372 float_T f = 0.0;
1373
1374 rettv->v_type = VAR_FLOAT;
1375 if (get_float_arg(argvars, &f) == OK)
1376 rettv->vval.v_float = asin(f);
1377 else
1378 rettv->vval.v_float = 0.0;
1379}
1380
1381/*
1382 * "atan()" function
1383 */
1384 static void
1385f_atan(typval_T *argvars, typval_T *rettv)
1386{
1387 float_T f = 0.0;
1388
1389 rettv->v_type = VAR_FLOAT;
1390 if (get_float_arg(argvars, &f) == OK)
1391 rettv->vval.v_float = atan(f);
1392 else
1393 rettv->vval.v_float = 0.0;
1394}
1395
1396/*
1397 * "atan2()" function
1398 */
1399 static void
1400f_atan2(typval_T *argvars, typval_T *rettv)
1401{
1402 float_T fx = 0.0, fy = 0.0;
1403
1404 rettv->v_type = VAR_FLOAT;
1405 if (get_float_arg(argvars, &fx) == OK
1406 && get_float_arg(&argvars[1], &fy) == OK)
1407 rettv->vval.v_float = atan2(fx, fy);
1408 else
1409 rettv->vval.v_float = 0.0;
1410}
1411#endif
1412
1413/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001414 * "balloon_show()" function
1415 */
1416#ifdef FEAT_BEVAL
1417 static void
1418f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1419{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001420 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001421 {
1422 if (argvars[0].v_type == VAR_LIST
1423# ifdef FEAT_GUI
1424 && !gui.in_use
1425# endif
1426 )
1427 post_balloon(balloonEval, NULL, argvars[0].vval.v_list);
1428 else
1429 post_balloon(balloonEval, get_tv_string_chk(&argvars[0]), NULL);
1430 }
1431}
1432
Bram Moolenaar669a8282017-11-19 20:13:05 +01001433# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001434 static void
1435f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1436{
1437 if (rettv_list_alloc(rettv) == OK)
1438 {
1439 char_u *msg = get_tv_string_chk(&argvars[0]);
1440
1441 if (msg != NULL)
1442 {
1443 pumitem_T *array;
1444 int size = split_message(msg, &array);
1445 int i;
1446
1447 /* Skip the first and last item, they are always empty. */
1448 for (i = 1; i < size - 1; ++i)
1449 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
1450 vim_free(array);
1451 }
1452 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001453}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001454# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001455#endif
1456
1457/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001458 * "browse(save, title, initdir, default)" function
1459 */
1460 static void
1461f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1462{
1463#ifdef FEAT_BROWSE
1464 int save;
1465 char_u *title;
1466 char_u *initdir;
1467 char_u *defname;
1468 char_u buf[NUMBUFLEN];
1469 char_u buf2[NUMBUFLEN];
1470 int error = FALSE;
1471
1472 save = (int)get_tv_number_chk(&argvars[0], &error);
1473 title = get_tv_string_chk(&argvars[1]);
1474 initdir = get_tv_string_buf_chk(&argvars[2], buf);
1475 defname = get_tv_string_buf_chk(&argvars[3], buf2);
1476
1477 if (error || title == NULL || initdir == NULL || defname == NULL)
1478 rettv->vval.v_string = NULL;
1479 else
1480 rettv->vval.v_string =
1481 do_browse(save ? BROWSE_SAVE : 0,
1482 title, defname, NULL, initdir, NULL, curbuf);
1483#else
1484 rettv->vval.v_string = NULL;
1485#endif
1486 rettv->v_type = VAR_STRING;
1487}
1488
1489/*
1490 * "browsedir(title, initdir)" function
1491 */
1492 static void
1493f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1494{
1495#ifdef FEAT_BROWSE
1496 char_u *title;
1497 char_u *initdir;
1498 char_u buf[NUMBUFLEN];
1499
1500 title = get_tv_string_chk(&argvars[0]);
1501 initdir = get_tv_string_buf_chk(&argvars[1], buf);
1502
1503 if (title == NULL || initdir == NULL)
1504 rettv->vval.v_string = NULL;
1505 else
1506 rettv->vval.v_string = do_browse(BROWSE_DIR,
1507 title, NULL, NULL, initdir, NULL, curbuf);
1508#else
1509 rettv->vval.v_string = NULL;
1510#endif
1511 rettv->v_type = VAR_STRING;
1512}
1513
1514static buf_T *find_buffer(typval_T *avar);
1515
1516/*
1517 * Find a buffer by number or exact name.
1518 */
1519 static buf_T *
1520find_buffer(typval_T *avar)
1521{
1522 buf_T *buf = NULL;
1523
1524 if (avar->v_type == VAR_NUMBER)
1525 buf = buflist_findnr((int)avar->vval.v_number);
1526 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1527 {
1528 buf = buflist_findname_exp(avar->vval.v_string);
1529 if (buf == NULL)
1530 {
1531 /* No full path name match, try a match with a URL or a "nofile"
1532 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001533 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001534 if (buf->b_fname != NULL
1535 && (path_with_url(buf->b_fname)
1536#ifdef FEAT_QUICKFIX
1537 || bt_nofile(buf)
1538#endif
1539 )
1540 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1541 break;
1542 }
1543 }
1544 return buf;
1545}
1546
1547/*
1548 * "bufexists(expr)" function
1549 */
1550 static void
1551f_bufexists(typval_T *argvars, typval_T *rettv)
1552{
1553 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1554}
1555
1556/*
1557 * "buflisted(expr)" function
1558 */
1559 static void
1560f_buflisted(typval_T *argvars, typval_T *rettv)
1561{
1562 buf_T *buf;
1563
1564 buf = find_buffer(&argvars[0]);
1565 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1566}
1567
1568/*
1569 * "bufloaded(expr)" function
1570 */
1571 static void
1572f_bufloaded(typval_T *argvars, typval_T *rettv)
1573{
1574 buf_T *buf;
1575
1576 buf = find_buffer(&argvars[0]);
1577 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1578}
1579
1580 buf_T *
1581buflist_find_by_name(char_u *name, int curtab_only)
1582{
1583 int save_magic;
1584 char_u *save_cpo;
1585 buf_T *buf;
1586
1587 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1588 save_magic = p_magic;
1589 p_magic = TRUE;
1590 save_cpo = p_cpo;
1591 p_cpo = (char_u *)"";
1592
1593 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1594 TRUE, FALSE, curtab_only));
1595
1596 p_magic = save_magic;
1597 p_cpo = save_cpo;
1598 return buf;
1599}
1600
1601/*
1602 * Get buffer by number or pattern.
1603 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001604 buf_T *
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001605get_buf_tv(typval_T *tv, int curtab_only)
1606{
1607 char_u *name = tv->vval.v_string;
1608 buf_T *buf;
1609
1610 if (tv->v_type == VAR_NUMBER)
1611 return buflist_findnr((int)tv->vval.v_number);
1612 if (tv->v_type != VAR_STRING)
1613 return NULL;
1614 if (name == NULL || *name == NUL)
1615 return curbuf;
1616 if (name[0] == '$' && name[1] == NUL)
1617 return lastbuf;
1618
1619 buf = buflist_find_by_name(name, curtab_only);
1620
1621 /* If not found, try expanding the name, like done for bufexists(). */
1622 if (buf == NULL)
1623 buf = find_buffer(tv);
1624
1625 return buf;
1626}
1627
1628/*
1629 * "bufname(expr)" function
1630 */
1631 static void
1632f_bufname(typval_T *argvars, typval_T *rettv)
1633{
1634 buf_T *buf;
1635
1636 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1637 ++emsg_off;
1638 buf = get_buf_tv(&argvars[0], FALSE);
1639 rettv->v_type = VAR_STRING;
1640 if (buf != NULL && buf->b_fname != NULL)
1641 rettv->vval.v_string = vim_strsave(buf->b_fname);
1642 else
1643 rettv->vval.v_string = NULL;
1644 --emsg_off;
1645}
1646
1647/*
1648 * "bufnr(expr)" function
1649 */
1650 static void
1651f_bufnr(typval_T *argvars, typval_T *rettv)
1652{
1653 buf_T *buf;
1654 int error = FALSE;
1655 char_u *name;
1656
1657 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1658 ++emsg_off;
1659 buf = get_buf_tv(&argvars[0], FALSE);
1660 --emsg_off;
1661
1662 /* If the buffer isn't found and the second argument is not zero create a
1663 * new buffer. */
1664 if (buf == NULL
1665 && argvars[1].v_type != VAR_UNKNOWN
1666 && get_tv_number_chk(&argvars[1], &error) != 0
1667 && !error
1668 && (name = get_tv_string_chk(&argvars[0])) != NULL
1669 && !error)
1670 buf = buflist_new(name, NULL, (linenr_T)1, 0);
1671
1672 if (buf != NULL)
1673 rettv->vval.v_number = buf->b_fnum;
1674 else
1675 rettv->vval.v_number = -1;
1676}
1677
1678 static void
1679buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
1680{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001681 win_T *wp;
1682 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001683 buf_T *buf;
1684
1685 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1686 ++emsg_off;
1687 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02001688 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001689 {
1690 ++winnr;
1691 if (wp->w_buffer == buf)
1692 break;
1693 }
1694 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001695 --emsg_off;
1696}
1697
1698/*
1699 * "bufwinid(nr)" function
1700 */
1701 static void
1702f_bufwinid(typval_T *argvars, typval_T *rettv)
1703{
1704 buf_win_common(argvars, rettv, FALSE);
1705}
1706
1707/*
1708 * "bufwinnr(nr)" function
1709 */
1710 static void
1711f_bufwinnr(typval_T *argvars, typval_T *rettv)
1712{
1713 buf_win_common(argvars, rettv, TRUE);
1714}
1715
1716/*
1717 * "byte2line(byte)" function
1718 */
1719 static void
1720f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1721{
1722#ifndef FEAT_BYTEOFF
1723 rettv->vval.v_number = -1;
1724#else
1725 long boff = 0;
1726
1727 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
1728 if (boff < 0)
1729 rettv->vval.v_number = -1;
1730 else
1731 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
1732 (linenr_T)0, &boff);
1733#endif
1734}
1735
1736 static void
1737byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
1738{
1739#ifdef FEAT_MBYTE
1740 char_u *t;
1741#endif
1742 char_u *str;
1743 varnumber_T idx;
1744
1745 str = get_tv_string_chk(&argvars[0]);
1746 idx = get_tv_number_chk(&argvars[1], NULL);
1747 rettv->vval.v_number = -1;
1748 if (str == NULL || idx < 0)
1749 return;
1750
1751#ifdef FEAT_MBYTE
1752 t = str;
1753 for ( ; idx > 0; idx--)
1754 {
1755 if (*t == NUL) /* EOL reached */
1756 return;
1757 if (enc_utf8 && comp)
1758 t += utf_ptr2len(t);
1759 else
1760 t += (*mb_ptr2len)(t);
1761 }
1762 rettv->vval.v_number = (varnumber_T)(t - str);
1763#else
1764 if ((size_t)idx <= STRLEN(str))
1765 rettv->vval.v_number = idx;
1766#endif
1767}
1768
1769/*
1770 * "byteidx()" function
1771 */
1772 static void
1773f_byteidx(typval_T *argvars, typval_T *rettv)
1774{
1775 byteidx(argvars, rettv, FALSE);
1776}
1777
1778/*
1779 * "byteidxcomp()" function
1780 */
1781 static void
1782f_byteidxcomp(typval_T *argvars, typval_T *rettv)
1783{
1784 byteidx(argvars, rettv, TRUE);
1785}
1786
1787/*
1788 * "call(func, arglist [, dict])" function
1789 */
1790 static void
1791f_call(typval_T *argvars, typval_T *rettv)
1792{
1793 char_u *func;
1794 partial_T *partial = NULL;
1795 dict_T *selfdict = NULL;
1796
1797 if (argvars[1].v_type != VAR_LIST)
1798 {
1799 EMSG(_(e_listreq));
1800 return;
1801 }
1802 if (argvars[1].vval.v_list == NULL)
1803 return;
1804
1805 if (argvars[0].v_type == VAR_FUNC)
1806 func = argvars[0].vval.v_string;
1807 else if (argvars[0].v_type == VAR_PARTIAL)
1808 {
1809 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001810 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001811 }
1812 else
1813 func = get_tv_string(&argvars[0]);
1814 if (*func == NUL)
1815 return; /* type error or empty name */
1816
1817 if (argvars[2].v_type != VAR_UNKNOWN)
1818 {
1819 if (argvars[2].v_type != VAR_DICT)
1820 {
1821 EMSG(_(e_dictreq));
1822 return;
1823 }
1824 selfdict = argvars[2].vval.v_dict;
1825 }
1826
1827 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
1828}
1829
1830#ifdef FEAT_FLOAT
1831/*
1832 * "ceil({float})" function
1833 */
1834 static void
1835f_ceil(typval_T *argvars, typval_T *rettv)
1836{
1837 float_T f = 0.0;
1838
1839 rettv->v_type = VAR_FLOAT;
1840 if (get_float_arg(argvars, &f) == OK)
1841 rettv->vval.v_float = ceil(f);
1842 else
1843 rettv->vval.v_float = 0.0;
1844}
1845#endif
1846
1847#ifdef FEAT_JOB_CHANNEL
1848/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01001849 * "ch_canread()" function
1850 */
1851 static void
1852f_ch_canread(typval_T *argvars, typval_T *rettv)
1853{
Bram Moolenaar958dc692016-12-01 15:34:12 +01001854 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01001855
1856 rettv->vval.v_number = 0;
1857 if (channel != NULL)
1858 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
1859 || channel_has_readahead(channel, PART_OUT)
1860 || channel_has_readahead(channel, PART_ERR);
1861}
1862
1863/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001864 * "ch_close()" function
1865 */
1866 static void
1867f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
1868{
1869 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
1870
1871 if (channel != NULL)
1872 {
1873 channel_close(channel, FALSE);
1874 channel_clear(channel);
1875 }
1876}
1877
1878/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02001879 * "ch_close()" function
1880 */
1881 static void
1882f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
1883{
1884 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
1885
1886 if (channel != NULL)
1887 channel_close_in(channel);
1888}
1889
1890/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001891 * "ch_getbufnr()" function
1892 */
1893 static void
1894f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
1895{
1896 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1897
1898 rettv->vval.v_number = -1;
1899 if (channel != NULL)
1900 {
1901 char_u *what = get_tv_string(&argvars[1]);
1902 int part;
1903
1904 if (STRCMP(what, "err") == 0)
1905 part = PART_ERR;
1906 else if (STRCMP(what, "out") == 0)
1907 part = PART_OUT;
1908 else if (STRCMP(what, "in") == 0)
1909 part = PART_IN;
1910 else
1911 part = PART_SOCK;
1912 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
1913 rettv->vval.v_number =
1914 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
1915 }
1916}
1917
1918/*
1919 * "ch_getjob()" function
1920 */
1921 static void
1922f_ch_getjob(typval_T *argvars, typval_T *rettv)
1923{
1924 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1925
1926 if (channel != NULL)
1927 {
1928 rettv->v_type = VAR_JOB;
1929 rettv->vval.v_job = channel->ch_job;
1930 if (channel->ch_job != NULL)
1931 ++channel->ch_job->jv_refcount;
1932 }
1933}
1934
1935/*
1936 * "ch_info()" function
1937 */
1938 static void
1939f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
1940{
1941 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1942
1943 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
1944 channel_info(channel, rettv->vval.v_dict);
1945}
1946
1947/*
1948 * "ch_log()" function
1949 */
1950 static void
1951f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
1952{
1953 char_u *msg = get_tv_string(&argvars[0]);
1954 channel_T *channel = NULL;
1955
1956 if (argvars[1].v_type != VAR_UNKNOWN)
1957 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
1958
1959 ch_log(channel, (char *)msg);
1960}
1961
1962/*
1963 * "ch_logfile()" function
1964 */
1965 static void
1966f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
1967{
1968 char_u *fname;
1969 char_u *opt = (char_u *)"";
1970 char_u buf[NUMBUFLEN];
1971
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02001972 /* Don't open a file in restricted mode. */
1973 if (check_restricted() || check_secure())
1974 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001975 fname = get_tv_string(&argvars[0]);
1976 if (argvars[1].v_type == VAR_STRING)
1977 opt = get_tv_string_buf(&argvars[1], buf);
1978 ch_logfile(fname, opt);
1979}
1980
1981/*
1982 * "ch_open()" function
1983 */
1984 static void
1985f_ch_open(typval_T *argvars, typval_T *rettv)
1986{
1987 rettv->v_type = VAR_CHANNEL;
1988 if (check_restricted() || check_secure())
1989 return;
1990 rettv->vval.v_channel = channel_open_func(argvars);
1991}
1992
1993/*
1994 * "ch_read()" function
1995 */
1996 static void
1997f_ch_read(typval_T *argvars, typval_T *rettv)
1998{
1999 common_channel_read(argvars, rettv, FALSE);
2000}
2001
2002/*
2003 * "ch_readraw()" function
2004 */
2005 static void
2006f_ch_readraw(typval_T *argvars, typval_T *rettv)
2007{
2008 common_channel_read(argvars, rettv, TRUE);
2009}
2010
2011/*
2012 * "ch_evalexpr()" function
2013 */
2014 static void
2015f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2016{
2017 ch_expr_common(argvars, rettv, TRUE);
2018}
2019
2020/*
2021 * "ch_sendexpr()" function
2022 */
2023 static void
2024f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2025{
2026 ch_expr_common(argvars, rettv, FALSE);
2027}
2028
2029/*
2030 * "ch_evalraw()" function
2031 */
2032 static void
2033f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2034{
2035 ch_raw_common(argvars, rettv, TRUE);
2036}
2037
2038/*
2039 * "ch_sendraw()" function
2040 */
2041 static void
2042f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2043{
2044 ch_raw_common(argvars, rettv, FALSE);
2045}
2046
2047/*
2048 * "ch_setoptions()" function
2049 */
2050 static void
2051f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2052{
2053 channel_T *channel;
2054 jobopt_T opt;
2055
2056 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2057 if (channel == NULL)
2058 return;
2059 clear_job_options(&opt);
2060 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002061 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002062 channel_set_options(channel, &opt);
2063 free_job_options(&opt);
2064}
2065
2066/*
2067 * "ch_status()" function
2068 */
2069 static void
2070f_ch_status(typval_T *argvars, typval_T *rettv)
2071{
2072 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002073 jobopt_T opt;
2074 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002075
2076 /* return an empty string by default */
2077 rettv->v_type = VAR_STRING;
2078 rettv->vval.v_string = NULL;
2079
2080 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002081
2082 if (argvars[1].v_type != VAR_UNKNOWN)
2083 {
2084 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002085 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002086 && (opt.jo_set & JO_PART))
2087 part = opt.jo_part;
2088 }
2089
2090 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002091}
2092#endif
2093
2094/*
2095 * "changenr()" function
2096 */
2097 static void
2098f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2099{
2100 rettv->vval.v_number = curbuf->b_u_seq_cur;
2101}
2102
2103/*
2104 * "char2nr(string)" function
2105 */
2106 static void
2107f_char2nr(typval_T *argvars, typval_T *rettv)
2108{
2109#ifdef FEAT_MBYTE
2110 if (has_mbyte)
2111 {
2112 int utf8 = 0;
2113
2114 if (argvars[1].v_type != VAR_UNKNOWN)
2115 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
2116
2117 if (utf8)
2118 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
2119 else
2120 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
2121 }
2122 else
2123#endif
2124 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
2125}
2126
2127/*
2128 * "cindent(lnum)" function
2129 */
2130 static void
2131f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2132{
2133#ifdef FEAT_CINDENT
2134 pos_T pos;
2135 linenr_T lnum;
2136
2137 pos = curwin->w_cursor;
2138 lnum = get_tv_lnum(argvars);
2139 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2140 {
2141 curwin->w_cursor.lnum = lnum;
2142 rettv->vval.v_number = get_c_indent();
2143 curwin->w_cursor = pos;
2144 }
2145 else
2146#endif
2147 rettv->vval.v_number = -1;
2148}
2149
2150/*
2151 * "clearmatches()" function
2152 */
2153 static void
2154f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2155{
2156#ifdef FEAT_SEARCH_EXTRA
2157 clear_matches(curwin);
2158#endif
2159}
2160
2161/*
2162 * "col(string)" function
2163 */
2164 static void
2165f_col(typval_T *argvars, typval_T *rettv)
2166{
2167 colnr_T col = 0;
2168 pos_T *fp;
2169 int fnum = curbuf->b_fnum;
2170
2171 fp = var2fpos(&argvars[0], FALSE, &fnum);
2172 if (fp != NULL && fnum == curbuf->b_fnum)
2173 {
2174 if (fp->col == MAXCOL)
2175 {
2176 /* '> can be MAXCOL, get the length of the line then */
2177 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2178 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2179 else
2180 col = MAXCOL;
2181 }
2182 else
2183 {
2184 col = fp->col + 1;
2185#ifdef FEAT_VIRTUALEDIT
2186 /* col(".") when the cursor is on the NUL at the end of the line
2187 * because of "coladd" can be seen as an extra column. */
2188 if (virtual_active() && fp == &curwin->w_cursor)
2189 {
2190 char_u *p = ml_get_cursor();
2191
2192 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2193 curwin->w_virtcol - curwin->w_cursor.coladd))
2194 {
2195# ifdef FEAT_MBYTE
2196 int l;
2197
2198 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2199 col += l;
2200# else
2201 if (*p != NUL && p[1] == NUL)
2202 ++col;
2203# endif
2204 }
2205 }
2206#endif
2207 }
2208 }
2209 rettv->vval.v_number = col;
2210}
2211
2212#if defined(FEAT_INS_EXPAND)
2213/*
2214 * "complete()" function
2215 */
2216 static void
2217f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2218{
2219 int startcol;
2220
2221 if ((State & INSERT) == 0)
2222 {
2223 EMSG(_("E785: complete() can only be used in Insert mode"));
2224 return;
2225 }
2226
2227 /* Check for undo allowed here, because if something was already inserted
2228 * the line was already saved for undo and this check isn't done. */
2229 if (!undo_allowed())
2230 return;
2231
2232 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2233 {
2234 EMSG(_(e_invarg));
2235 return;
2236 }
2237
2238 startcol = (int)get_tv_number_chk(&argvars[0], NULL);
2239 if (startcol <= 0)
2240 return;
2241
2242 set_completion(startcol - 1, argvars[1].vval.v_list);
2243}
2244
2245/*
2246 * "complete_add()" function
2247 */
2248 static void
2249f_complete_add(typval_T *argvars, typval_T *rettv)
2250{
2251 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2252}
2253
2254/*
2255 * "complete_check()" function
2256 */
2257 static void
2258f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2259{
2260 int saved = RedrawingDisabled;
2261
2262 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002263 ins_compl_check_keys(0, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002264 rettv->vval.v_number = compl_interrupted;
2265 RedrawingDisabled = saved;
2266}
2267#endif
2268
2269/*
2270 * "confirm(message, buttons[, default [, type]])" function
2271 */
2272 static void
2273f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2274{
2275#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2276 char_u *message;
2277 char_u *buttons = NULL;
2278 char_u buf[NUMBUFLEN];
2279 char_u buf2[NUMBUFLEN];
2280 int def = 1;
2281 int type = VIM_GENERIC;
2282 char_u *typestr;
2283 int error = FALSE;
2284
2285 message = get_tv_string_chk(&argvars[0]);
2286 if (message == NULL)
2287 error = TRUE;
2288 if (argvars[1].v_type != VAR_UNKNOWN)
2289 {
2290 buttons = get_tv_string_buf_chk(&argvars[1], buf);
2291 if (buttons == NULL)
2292 error = TRUE;
2293 if (argvars[2].v_type != VAR_UNKNOWN)
2294 {
2295 def = (int)get_tv_number_chk(&argvars[2], &error);
2296 if (argvars[3].v_type != VAR_UNKNOWN)
2297 {
2298 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
2299 if (typestr == NULL)
2300 error = TRUE;
2301 else
2302 {
2303 switch (TOUPPER_ASC(*typestr))
2304 {
2305 case 'E': type = VIM_ERROR; break;
2306 case 'Q': type = VIM_QUESTION; break;
2307 case 'I': type = VIM_INFO; break;
2308 case 'W': type = VIM_WARNING; break;
2309 case 'G': type = VIM_GENERIC; break;
2310 }
2311 }
2312 }
2313 }
2314 }
2315
2316 if (buttons == NULL || *buttons == NUL)
2317 buttons = (char_u *)_("&Ok");
2318
2319 if (!error)
2320 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2321 def, NULL, FALSE);
2322#endif
2323}
2324
2325/*
2326 * "copy()" function
2327 */
2328 static void
2329f_copy(typval_T *argvars, typval_T *rettv)
2330{
2331 item_copy(&argvars[0], rettv, FALSE, 0);
2332}
2333
2334#ifdef FEAT_FLOAT
2335/*
2336 * "cos()" function
2337 */
2338 static void
2339f_cos(typval_T *argvars, typval_T *rettv)
2340{
2341 float_T f = 0.0;
2342
2343 rettv->v_type = VAR_FLOAT;
2344 if (get_float_arg(argvars, &f) == OK)
2345 rettv->vval.v_float = cos(f);
2346 else
2347 rettv->vval.v_float = 0.0;
2348}
2349
2350/*
2351 * "cosh()" function
2352 */
2353 static void
2354f_cosh(typval_T *argvars, typval_T *rettv)
2355{
2356 float_T f = 0.0;
2357
2358 rettv->v_type = VAR_FLOAT;
2359 if (get_float_arg(argvars, &f) == OK)
2360 rettv->vval.v_float = cosh(f);
2361 else
2362 rettv->vval.v_float = 0.0;
2363}
2364#endif
2365
2366/*
2367 * "count()" function
2368 */
2369 static void
2370f_count(typval_T *argvars, typval_T *rettv)
2371{
2372 long n = 0;
2373 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002374 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002375
Bram Moolenaar9966b212017-07-28 16:46:57 +02002376 if (argvars[2].v_type != VAR_UNKNOWN)
2377 ic = (int)get_tv_number_chk(&argvars[2], &error);
2378
2379 if (argvars[0].v_type == VAR_STRING)
2380 {
2381 char_u *expr = get_tv_string_chk(&argvars[1]);
2382 char_u *p = argvars[0].vval.v_string;
2383 char_u *next;
2384
2385 if (!error && expr != NULL && p != NULL)
2386 {
2387 if (ic)
2388 {
2389 size_t len = STRLEN(expr);
2390
2391 while (*p != NUL)
2392 {
2393 if (MB_STRNICMP(p, expr, len) == 0)
2394 {
2395 ++n;
2396 p += len;
2397 }
2398 else
2399 MB_PTR_ADV(p);
2400 }
2401 }
2402 else
2403 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2404 != NULL)
2405 {
2406 ++n;
2407 p = next + STRLEN(expr);
2408 }
2409 }
2410
2411 }
2412 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002413 {
2414 listitem_T *li;
2415 list_T *l;
2416 long idx;
2417
2418 if ((l = argvars[0].vval.v_list) != NULL)
2419 {
2420 li = l->lv_first;
2421 if (argvars[2].v_type != VAR_UNKNOWN)
2422 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002423 if (argvars[3].v_type != VAR_UNKNOWN)
2424 {
2425 idx = (long)get_tv_number_chk(&argvars[3], &error);
2426 if (!error)
2427 {
2428 li = list_find(l, idx);
2429 if (li == NULL)
2430 EMSGN(_(e_listidx), idx);
2431 }
2432 }
2433 if (error)
2434 li = NULL;
2435 }
2436
2437 for ( ; li != NULL; li = li->li_next)
2438 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2439 ++n;
2440 }
2441 }
2442 else if (argvars[0].v_type == VAR_DICT)
2443 {
2444 int todo;
2445 dict_T *d;
2446 hashitem_T *hi;
2447
2448 if ((d = argvars[0].vval.v_dict) != NULL)
2449 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002450 if (argvars[2].v_type != VAR_UNKNOWN)
2451 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002452 if (argvars[3].v_type != VAR_UNKNOWN)
2453 EMSG(_(e_invarg));
2454 }
2455
2456 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2457 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2458 {
2459 if (!HASHITEM_EMPTY(hi))
2460 {
2461 --todo;
2462 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2463 ++n;
2464 }
2465 }
2466 }
2467 }
2468 else
2469 EMSG2(_(e_listdictarg), "count()");
2470 rettv->vval.v_number = n;
2471}
2472
2473/*
2474 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2475 *
2476 * Checks the existence of a cscope connection.
2477 */
2478 static void
2479f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2480{
2481#ifdef FEAT_CSCOPE
2482 int num = 0;
2483 char_u *dbpath = NULL;
2484 char_u *prepend = NULL;
2485 char_u buf[NUMBUFLEN];
2486
2487 if (argvars[0].v_type != VAR_UNKNOWN
2488 && argvars[1].v_type != VAR_UNKNOWN)
2489 {
2490 num = (int)get_tv_number(&argvars[0]);
2491 dbpath = get_tv_string(&argvars[1]);
2492 if (argvars[2].v_type != VAR_UNKNOWN)
2493 prepend = get_tv_string_buf(&argvars[2], buf);
2494 }
2495
2496 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2497#endif
2498}
2499
2500/*
2501 * "cursor(lnum, col)" function, or
2502 * "cursor(list)"
2503 *
2504 * Moves the cursor to the specified line and column.
2505 * Returns 0 when the position could be set, -1 otherwise.
2506 */
2507 static void
2508f_cursor(typval_T *argvars, typval_T *rettv)
2509{
2510 long line, col;
2511#ifdef FEAT_VIRTUALEDIT
2512 long coladd = 0;
2513#endif
2514 int set_curswant = TRUE;
2515
2516 rettv->vval.v_number = -1;
2517 if (argvars[1].v_type == VAR_UNKNOWN)
2518 {
2519 pos_T pos;
2520 colnr_T curswant = -1;
2521
2522 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2523 {
2524 EMSG(_(e_invarg));
2525 return;
2526 }
2527 line = pos.lnum;
2528 col = pos.col;
2529#ifdef FEAT_VIRTUALEDIT
2530 coladd = pos.coladd;
2531#endif
2532 if (curswant >= 0)
2533 {
2534 curwin->w_curswant = curswant - 1;
2535 set_curswant = FALSE;
2536 }
2537 }
2538 else
2539 {
2540 line = get_tv_lnum(argvars);
2541 col = (long)get_tv_number_chk(&argvars[1], NULL);
2542#ifdef FEAT_VIRTUALEDIT
2543 if (argvars[2].v_type != VAR_UNKNOWN)
2544 coladd = (long)get_tv_number_chk(&argvars[2], NULL);
2545#endif
2546 }
2547 if (line < 0 || col < 0
2548#ifdef FEAT_VIRTUALEDIT
2549 || coladd < 0
2550#endif
2551 )
2552 return; /* type error; errmsg already given */
2553 if (line > 0)
2554 curwin->w_cursor.lnum = line;
2555 if (col > 0)
2556 curwin->w_cursor.col = col - 1;
2557#ifdef FEAT_VIRTUALEDIT
2558 curwin->w_cursor.coladd = coladd;
2559#endif
2560
2561 /* Make sure the cursor is in a valid position. */
2562 check_cursor();
2563#ifdef FEAT_MBYTE
2564 /* Correct cursor for multi-byte character. */
2565 if (has_mbyte)
2566 mb_adjust_cursor();
2567#endif
2568
2569 curwin->w_set_curswant = set_curswant;
2570 rettv->vval.v_number = 0;
2571}
2572
2573/*
2574 * "deepcopy()" function
2575 */
2576 static void
2577f_deepcopy(typval_T *argvars, typval_T *rettv)
2578{
2579 int noref = 0;
2580 int copyID;
2581
2582 if (argvars[1].v_type != VAR_UNKNOWN)
2583 noref = (int)get_tv_number_chk(&argvars[1], NULL);
2584 if (noref < 0 || noref > 1)
2585 EMSG(_(e_invarg));
2586 else
2587 {
2588 copyID = get_copyID();
2589 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2590 }
2591}
2592
2593/*
2594 * "delete()" function
2595 */
2596 static void
2597f_delete(typval_T *argvars, typval_T *rettv)
2598{
2599 char_u nbuf[NUMBUFLEN];
2600 char_u *name;
2601 char_u *flags;
2602
2603 rettv->vval.v_number = -1;
2604 if (check_restricted() || check_secure())
2605 return;
2606
2607 name = get_tv_string(&argvars[0]);
2608 if (name == NULL || *name == NUL)
2609 {
2610 EMSG(_(e_invarg));
2611 return;
2612 }
2613
2614 if (argvars[1].v_type != VAR_UNKNOWN)
2615 flags = get_tv_string_buf(&argvars[1], nbuf);
2616 else
2617 flags = (char_u *)"";
2618
2619 if (*flags == NUL)
2620 /* delete a file */
2621 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2622 else if (STRCMP(flags, "d") == 0)
2623 /* delete an empty directory */
2624 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2625 else if (STRCMP(flags, "rf") == 0)
2626 /* delete a directory recursively */
2627 rettv->vval.v_number = delete_recursive(name);
2628 else
2629 EMSG2(_(e_invexpr2), flags);
2630}
2631
2632/*
2633 * "did_filetype()" function
2634 */
2635 static void
2636f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2637{
2638#ifdef FEAT_AUTOCMD
2639 rettv->vval.v_number = did_filetype;
2640#endif
2641}
2642
2643/*
2644 * "diff_filler()" function
2645 */
2646 static void
2647f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2648{
2649#ifdef FEAT_DIFF
2650 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
2651#endif
2652}
2653
2654/*
2655 * "diff_hlID()" function
2656 */
2657 static void
2658f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2659{
2660#ifdef FEAT_DIFF
2661 linenr_T lnum = get_tv_lnum(argvars);
2662 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01002663 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002664 static int fnum = 0;
2665 static int change_start = 0;
2666 static int change_end = 0;
2667 static hlf_T hlID = (hlf_T)0;
2668 int filler_lines;
2669 int col;
2670
2671 if (lnum < 0) /* ignore type error in {lnum} arg */
2672 lnum = 0;
2673 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002674 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002675 || fnum != curbuf->b_fnum)
2676 {
2677 /* New line, buffer, change: need to get the values. */
2678 filler_lines = diff_check(curwin, lnum);
2679 if (filler_lines < 0)
2680 {
2681 if (filler_lines == -1)
2682 {
2683 change_start = MAXCOL;
2684 change_end = -1;
2685 if (diff_find_change(curwin, lnum, &change_start, &change_end))
2686 hlID = HLF_ADD; /* added line */
2687 else
2688 hlID = HLF_CHD; /* changed line */
2689 }
2690 else
2691 hlID = HLF_ADD; /* added line */
2692 }
2693 else
2694 hlID = (hlf_T)0;
2695 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002696 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002697 fnum = curbuf->b_fnum;
2698 }
2699
2700 if (hlID == HLF_CHD || hlID == HLF_TXD)
2701 {
2702 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
2703 if (col >= change_start && col <= change_end)
2704 hlID = HLF_TXD; /* changed text */
2705 else
2706 hlID = HLF_CHD; /* changed line */
2707 }
2708 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
2709#endif
2710}
2711
2712/*
2713 * "empty({expr})" function
2714 */
2715 static void
2716f_empty(typval_T *argvars, typval_T *rettv)
2717{
2718 int n = FALSE;
2719
2720 switch (argvars[0].v_type)
2721 {
2722 case VAR_STRING:
2723 case VAR_FUNC:
2724 n = argvars[0].vval.v_string == NULL
2725 || *argvars[0].vval.v_string == NUL;
2726 break;
2727 case VAR_PARTIAL:
2728 n = FALSE;
2729 break;
2730 case VAR_NUMBER:
2731 n = argvars[0].vval.v_number == 0;
2732 break;
2733 case VAR_FLOAT:
2734#ifdef FEAT_FLOAT
2735 n = argvars[0].vval.v_float == 0.0;
2736 break;
2737#endif
2738 case VAR_LIST:
2739 n = argvars[0].vval.v_list == NULL
2740 || argvars[0].vval.v_list->lv_first == NULL;
2741 break;
2742 case VAR_DICT:
2743 n = argvars[0].vval.v_dict == NULL
2744 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2745 break;
2746 case VAR_SPECIAL:
2747 n = argvars[0].vval.v_number != VVAL_TRUE;
2748 break;
2749
2750 case VAR_JOB:
2751#ifdef FEAT_JOB_CHANNEL
2752 n = argvars[0].vval.v_job == NULL
2753 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2754 break;
2755#endif
2756 case VAR_CHANNEL:
2757#ifdef FEAT_JOB_CHANNEL
2758 n = argvars[0].vval.v_channel == NULL
2759 || !channel_is_open(argvars[0].vval.v_channel);
2760 break;
2761#endif
2762 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01002763 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002764 n = TRUE;
2765 break;
2766 }
2767
2768 rettv->vval.v_number = n;
2769}
2770
2771/*
2772 * "escape({string}, {chars})" function
2773 */
2774 static void
2775f_escape(typval_T *argvars, typval_T *rettv)
2776{
2777 char_u buf[NUMBUFLEN];
2778
2779 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
2780 get_tv_string_buf(&argvars[1], buf));
2781 rettv->v_type = VAR_STRING;
2782}
2783
2784/*
2785 * "eval()" function
2786 */
2787 static void
2788f_eval(typval_T *argvars, typval_T *rettv)
2789{
2790 char_u *s, *p;
2791
2792 s = get_tv_string_chk(&argvars[0]);
2793 if (s != NULL)
2794 s = skipwhite(s);
2795
2796 p = s;
2797 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
2798 {
2799 if (p != NULL && !aborting())
2800 EMSG2(_(e_invexpr2), p);
2801 need_clr_eos = FALSE;
2802 rettv->v_type = VAR_NUMBER;
2803 rettv->vval.v_number = 0;
2804 }
2805 else if (*s != NUL)
2806 EMSG(_(e_trailing));
2807}
2808
2809/*
2810 * "eventhandler()" function
2811 */
2812 static void
2813f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
2814{
2815 rettv->vval.v_number = vgetc_busy;
2816}
2817
2818/*
2819 * "executable()" function
2820 */
2821 static void
2822f_executable(typval_T *argvars, typval_T *rettv)
2823{
2824 char_u *name = get_tv_string(&argvars[0]);
2825
2826 /* Check in $PATH and also check directly if there is a directory name. */
2827 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
2828 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
2829}
2830
2831static garray_T redir_execute_ga;
2832
2833/*
2834 * Append "value[value_len]" to the execute() output.
2835 */
2836 void
2837execute_redir_str(char_u *value, int value_len)
2838{
2839 int len;
2840
2841 if (value_len == -1)
2842 len = (int)STRLEN(value); /* Append the entire string */
2843 else
2844 len = value_len; /* Append only "value_len" characters */
2845 if (ga_grow(&redir_execute_ga, len) == OK)
2846 {
2847 mch_memmove((char *)redir_execute_ga.ga_data
2848 + redir_execute_ga.ga_len, value, len);
2849 redir_execute_ga.ga_len += len;
2850 }
2851}
2852
2853/*
2854 * Get next line from a list.
2855 * Called by do_cmdline() to get the next line.
2856 * Returns allocated string, or NULL for end of function.
2857 */
2858
2859 static char_u *
2860get_list_line(
2861 int c UNUSED,
2862 void *cookie,
2863 int indent UNUSED)
2864{
2865 listitem_T **p = (listitem_T **)cookie;
2866 listitem_T *item = *p;
2867 char_u buf[NUMBUFLEN];
2868 char_u *s;
2869
2870 if (item == NULL)
2871 return NULL;
2872 s = get_tv_string_buf_chk(&item->li_tv, buf);
2873 *p = item->li_next;
2874 return s == NULL ? NULL : vim_strsave(s);
2875}
2876
2877/*
2878 * "execute()" function
2879 */
2880 static void
2881f_execute(typval_T *argvars, typval_T *rettv)
2882{
2883 char_u *cmd = NULL;
2884 list_T *list = NULL;
2885 int save_msg_silent = msg_silent;
2886 int save_emsg_silent = emsg_silent;
2887 int save_emsg_noredir = emsg_noredir;
2888 int save_redir_execute = redir_execute;
2889 garray_T save_ga;
2890
2891 rettv->vval.v_string = NULL;
2892 rettv->v_type = VAR_STRING;
2893
2894 if (argvars[0].v_type == VAR_LIST)
2895 {
2896 list = argvars[0].vval.v_list;
2897 if (list == NULL || list->lv_first == NULL)
2898 /* empty list, no commands, empty output */
2899 return;
2900 ++list->lv_refcount;
2901 }
2902 else
2903 {
2904 cmd = get_tv_string_chk(&argvars[0]);
2905 if (cmd == NULL)
2906 return;
2907 }
2908
2909 if (argvars[1].v_type != VAR_UNKNOWN)
2910 {
2911 char_u buf[NUMBUFLEN];
2912 char_u *s = get_tv_string_buf_chk(&argvars[1], buf);
2913
2914 if (s == NULL)
2915 return;
2916 if (STRNCMP(s, "silent", 6) == 0)
2917 ++msg_silent;
2918 if (STRCMP(s, "silent!") == 0)
2919 {
2920 emsg_silent = TRUE;
2921 emsg_noredir = TRUE;
2922 }
2923 }
2924 else
2925 ++msg_silent;
2926
2927 if (redir_execute)
2928 save_ga = redir_execute_ga;
2929 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
2930 redir_execute = TRUE;
2931
2932 if (cmd != NULL)
2933 do_cmdline_cmd(cmd);
2934 else
2935 {
2936 listitem_T *item = list->lv_first;
2937
2938 do_cmdline(NULL, get_list_line, (void *)&item,
2939 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
2940 --list->lv_refcount;
2941 }
2942
Bram Moolenaard297f352017-01-29 20:31:21 +01002943 /* Need to append a NUL to the result. */
2944 if (ga_grow(&redir_execute_ga, 1) == OK)
2945 {
2946 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
2947 rettv->vval.v_string = redir_execute_ga.ga_data;
2948 }
2949 else
2950 {
2951 ga_clear(&redir_execute_ga);
2952 rettv->vval.v_string = NULL;
2953 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002954 msg_silent = save_msg_silent;
2955 emsg_silent = save_emsg_silent;
2956 emsg_noredir = save_emsg_noredir;
2957
2958 redir_execute = save_redir_execute;
2959 if (redir_execute)
2960 redir_execute_ga = save_ga;
2961
2962 /* "silent reg" or "silent echo x" leaves msg_col somewhere in the
2963 * line. Put it back in the first column. */
2964 msg_col = 0;
2965}
2966
2967/*
2968 * "exepath()" function
2969 */
2970 static void
2971f_exepath(typval_T *argvars, typval_T *rettv)
2972{
2973 char_u *p = NULL;
2974
2975 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
2976 rettv->v_type = VAR_STRING;
2977 rettv->vval.v_string = p;
2978}
2979
2980/*
2981 * "exists()" function
2982 */
2983 static void
2984f_exists(typval_T *argvars, typval_T *rettv)
2985{
2986 char_u *p;
2987 char_u *name;
2988 int n = FALSE;
2989 int len = 0;
2990
2991 p = get_tv_string(&argvars[0]);
2992 if (*p == '$') /* environment variable */
2993 {
2994 /* first try "normal" environment variables (fast) */
2995 if (mch_getenv(p + 1) != NULL)
2996 n = TRUE;
2997 else
2998 {
2999 /* try expanding things like $VIM and ${HOME} */
3000 p = expand_env_save(p);
3001 if (p != NULL && *p != '$')
3002 n = TRUE;
3003 vim_free(p);
3004 }
3005 }
3006 else if (*p == '&' || *p == '+') /* option */
3007 {
3008 n = (get_option_tv(&p, NULL, TRUE) == OK);
3009 if (*skipwhite(p) != NUL)
3010 n = FALSE; /* trailing garbage */
3011 }
3012 else if (*p == '*') /* internal or user defined function */
3013 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003014 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003015 }
3016 else if (*p == ':')
3017 {
3018 n = cmd_exists(p + 1);
3019 }
3020 else if (*p == '#')
3021 {
3022#ifdef FEAT_AUTOCMD
3023 if (p[1] == '#')
3024 n = autocmd_supported(p + 2);
3025 else
3026 n = au_exists(p + 1);
3027#endif
3028 }
3029 else /* internal variable */
3030 {
3031 char_u *tofree;
3032 typval_T tv;
3033
3034 /* get_name_len() takes care of expanding curly braces */
3035 name = p;
3036 len = get_name_len(&p, &tofree, TRUE, FALSE);
3037 if (len > 0)
3038 {
3039 if (tofree != NULL)
3040 name = tofree;
3041 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
3042 if (n)
3043 {
3044 /* handle d.key, l[idx], f(expr) */
3045 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
3046 if (n)
3047 clear_tv(&tv);
3048 }
3049 }
3050 if (*p != NUL)
3051 n = FALSE;
3052
3053 vim_free(tofree);
3054 }
3055
3056 rettv->vval.v_number = n;
3057}
3058
3059#ifdef FEAT_FLOAT
3060/*
3061 * "exp()" function
3062 */
3063 static void
3064f_exp(typval_T *argvars, typval_T *rettv)
3065{
3066 float_T f = 0.0;
3067
3068 rettv->v_type = VAR_FLOAT;
3069 if (get_float_arg(argvars, &f) == OK)
3070 rettv->vval.v_float = exp(f);
3071 else
3072 rettv->vval.v_float = 0.0;
3073}
3074#endif
3075
3076/*
3077 * "expand()" function
3078 */
3079 static void
3080f_expand(typval_T *argvars, typval_T *rettv)
3081{
3082 char_u *s;
3083 int len;
3084 char_u *errormsg;
3085 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3086 expand_T xpc;
3087 int error = FALSE;
3088 char_u *result;
3089
3090 rettv->v_type = VAR_STRING;
3091 if (argvars[1].v_type != VAR_UNKNOWN
3092 && argvars[2].v_type != VAR_UNKNOWN
3093 && get_tv_number_chk(&argvars[2], &error)
3094 && !error)
3095 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003096 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003097 }
3098
3099 s = get_tv_string(&argvars[0]);
3100 if (*s == '%' || *s == '#' || *s == '<')
3101 {
3102 ++emsg_off;
3103 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3104 --emsg_off;
3105 if (rettv->v_type == VAR_LIST)
3106 {
3107 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3108 list_append_string(rettv->vval.v_list, result, -1);
3109 else
3110 vim_free(result);
3111 }
3112 else
3113 rettv->vval.v_string = result;
3114 }
3115 else
3116 {
3117 /* When the optional second argument is non-zero, don't remove matches
3118 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3119 if (argvars[1].v_type != VAR_UNKNOWN
3120 && get_tv_number_chk(&argvars[1], &error))
3121 options |= WILD_KEEP_ALL;
3122 if (!error)
3123 {
3124 ExpandInit(&xpc);
3125 xpc.xp_context = EXPAND_FILES;
3126 if (p_wic)
3127 options += WILD_ICASE;
3128 if (rettv->v_type == VAR_STRING)
3129 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3130 options, WILD_ALL);
3131 else if (rettv_list_alloc(rettv) != FAIL)
3132 {
3133 int i;
3134
3135 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3136 for (i = 0; i < xpc.xp_numfiles; i++)
3137 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3138 ExpandCleanup(&xpc);
3139 }
3140 }
3141 else
3142 rettv->vval.v_string = NULL;
3143 }
3144}
3145
3146/*
3147 * "extend(list, list [, idx])" function
3148 * "extend(dict, dict [, action])" function
3149 */
3150 static void
3151f_extend(typval_T *argvars, typval_T *rettv)
3152{
3153 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3154
3155 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3156 {
3157 list_T *l1, *l2;
3158 listitem_T *item;
3159 long before;
3160 int error = FALSE;
3161
3162 l1 = argvars[0].vval.v_list;
3163 l2 = argvars[1].vval.v_list;
3164 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
3165 && l2 != NULL)
3166 {
3167 if (argvars[2].v_type != VAR_UNKNOWN)
3168 {
3169 before = (long)get_tv_number_chk(&argvars[2], &error);
3170 if (error)
3171 return; /* type error; errmsg already given */
3172
3173 if (before == l1->lv_len)
3174 item = NULL;
3175 else
3176 {
3177 item = list_find(l1, before);
3178 if (item == NULL)
3179 {
3180 EMSGN(_(e_listidx), before);
3181 return;
3182 }
3183 }
3184 }
3185 else
3186 item = NULL;
3187 list_extend(l1, l2, item);
3188
3189 copy_tv(&argvars[0], rettv);
3190 }
3191 }
3192 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3193 {
3194 dict_T *d1, *d2;
3195 char_u *action;
3196 int i;
3197
3198 d1 = argvars[0].vval.v_dict;
3199 d2 = argvars[1].vval.v_dict;
3200 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
3201 && d2 != NULL)
3202 {
3203 /* Check the third argument. */
3204 if (argvars[2].v_type != VAR_UNKNOWN)
3205 {
3206 static char *(av[]) = {"keep", "force", "error"};
3207
3208 action = get_tv_string_chk(&argvars[2]);
3209 if (action == NULL)
3210 return; /* type error; errmsg already given */
3211 for (i = 0; i < 3; ++i)
3212 if (STRCMP(action, av[i]) == 0)
3213 break;
3214 if (i == 3)
3215 {
3216 EMSG2(_(e_invarg2), action);
3217 return;
3218 }
3219 }
3220 else
3221 action = (char_u *)"force";
3222
3223 dict_extend(d1, d2, action);
3224
3225 copy_tv(&argvars[0], rettv);
3226 }
3227 }
3228 else
3229 EMSG2(_(e_listdictarg), "extend()");
3230}
3231
3232/*
3233 * "feedkeys()" function
3234 */
3235 static void
3236f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3237{
3238 int remap = TRUE;
3239 int insert = FALSE;
3240 char_u *keys, *flags;
3241 char_u nbuf[NUMBUFLEN];
3242 int typed = FALSE;
3243 int execute = FALSE;
3244 int dangerous = FALSE;
3245 char_u *keys_esc;
3246
3247 /* This is not allowed in the sandbox. If the commands would still be
3248 * executed in the sandbox it would be OK, but it probably happens later,
3249 * when "sandbox" is no longer set. */
3250 if (check_secure())
3251 return;
3252
3253 keys = get_tv_string(&argvars[0]);
3254
3255 if (argvars[1].v_type != VAR_UNKNOWN)
3256 {
3257 flags = get_tv_string_buf(&argvars[1], nbuf);
3258 for ( ; *flags != NUL; ++flags)
3259 {
3260 switch (*flags)
3261 {
3262 case 'n': remap = FALSE; break;
3263 case 'm': remap = TRUE; break;
3264 case 't': typed = TRUE; break;
3265 case 'i': insert = TRUE; break;
3266 case 'x': execute = TRUE; break;
3267 case '!': dangerous = TRUE; break;
3268 }
3269 }
3270 }
3271
3272 if (*keys != NUL || execute)
3273 {
3274 /* Need to escape K_SPECIAL and CSI before putting the string in the
3275 * typeahead buffer. */
3276 keys_esc = vim_strsave_escape_csi(keys);
3277 if (keys_esc != NULL)
3278 {
3279 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
3280 insert ? 0 : typebuf.tb_len, !typed, FALSE);
3281 vim_free(keys_esc);
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003282 if (vgetc_busy
3283#ifdef FEAT_TIMERS
3284 || timer_busy
3285#endif
3286 )
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003287 typebuf_was_filled = TRUE;
3288 if (execute)
3289 {
3290 int save_msg_scroll = msg_scroll;
3291
3292 /* Avoid a 1 second delay when the keys start Insert mode. */
3293 msg_scroll = FALSE;
3294
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02003295#ifdef FEAT_TERMINAL
3296 if (term_use_loop())
3297 terminal_loop(FALSE);
3298 else
3299#endif
3300 {
3301 if (!dangerous)
3302 ++ex_normal_busy;
3303 exec_normal(TRUE);
3304 if (!dangerous)
3305 --ex_normal_busy;
3306 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003307 msg_scroll |= save_msg_scroll;
3308 }
3309 }
3310 }
3311}
3312
3313/*
3314 * "filereadable()" function
3315 */
3316 static void
3317f_filereadable(typval_T *argvars, typval_T *rettv)
3318{
3319 int fd;
3320 char_u *p;
3321 int n;
3322
3323#ifndef O_NONBLOCK
3324# define O_NONBLOCK 0
3325#endif
3326 p = get_tv_string(&argvars[0]);
3327 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3328 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3329 {
3330 n = TRUE;
3331 close(fd);
3332 }
3333 else
3334 n = FALSE;
3335
3336 rettv->vval.v_number = n;
3337}
3338
3339/*
3340 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3341 * rights to write into.
3342 */
3343 static void
3344f_filewritable(typval_T *argvars, typval_T *rettv)
3345{
3346 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
3347}
3348
3349 static void
3350findfilendir(
3351 typval_T *argvars UNUSED,
3352 typval_T *rettv,
3353 int find_what UNUSED)
3354{
3355#ifdef FEAT_SEARCHPATH
3356 char_u *fname;
3357 char_u *fresult = NULL;
3358 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3359 char_u *p;
3360 char_u pathbuf[NUMBUFLEN];
3361 int count = 1;
3362 int first = TRUE;
3363 int error = FALSE;
3364#endif
3365
3366 rettv->vval.v_string = NULL;
3367 rettv->v_type = VAR_STRING;
3368
3369#ifdef FEAT_SEARCHPATH
3370 fname = get_tv_string(&argvars[0]);
3371
3372 if (argvars[1].v_type != VAR_UNKNOWN)
3373 {
3374 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
3375 if (p == NULL)
3376 error = TRUE;
3377 else
3378 {
3379 if (*p != NUL)
3380 path = p;
3381
3382 if (argvars[2].v_type != VAR_UNKNOWN)
3383 count = (int)get_tv_number_chk(&argvars[2], &error);
3384 }
3385 }
3386
3387 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3388 error = TRUE;
3389
3390 if (*fname != NUL && !error)
3391 {
3392 do
3393 {
3394 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3395 vim_free(fresult);
3396 fresult = find_file_in_path_option(first ? fname : NULL,
3397 first ? (int)STRLEN(fname) : 0,
3398 0, first, path,
3399 find_what,
3400 curbuf->b_ffname,
3401 find_what == FINDFILE_DIR
3402 ? (char_u *)"" : curbuf->b_p_sua);
3403 first = FALSE;
3404
3405 if (fresult != NULL && rettv->v_type == VAR_LIST)
3406 list_append_string(rettv->vval.v_list, fresult, -1);
3407
3408 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3409 }
3410
3411 if (rettv->v_type == VAR_STRING)
3412 rettv->vval.v_string = fresult;
3413#endif
3414}
3415
3416/*
3417 * "filter()" function
3418 */
3419 static void
3420f_filter(typval_T *argvars, typval_T *rettv)
3421{
3422 filter_map(argvars, rettv, FALSE);
3423}
3424
3425/*
3426 * "finddir({fname}[, {path}[, {count}]])" function
3427 */
3428 static void
3429f_finddir(typval_T *argvars, typval_T *rettv)
3430{
3431 findfilendir(argvars, rettv, FINDFILE_DIR);
3432}
3433
3434/*
3435 * "findfile({fname}[, {path}[, {count}]])" function
3436 */
3437 static void
3438f_findfile(typval_T *argvars, typval_T *rettv)
3439{
3440 findfilendir(argvars, rettv, FINDFILE_FILE);
3441}
3442
3443#ifdef FEAT_FLOAT
3444/*
3445 * "float2nr({float})" function
3446 */
3447 static void
3448f_float2nr(typval_T *argvars, typval_T *rettv)
3449{
3450 float_T f = 0.0;
3451
3452 if (get_float_arg(argvars, &f) == OK)
3453 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003454 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003455 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003456 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003457 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003458 else
3459 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003460 }
3461}
3462
3463/*
3464 * "floor({float})" function
3465 */
3466 static void
3467f_floor(typval_T *argvars, typval_T *rettv)
3468{
3469 float_T f = 0.0;
3470
3471 rettv->v_type = VAR_FLOAT;
3472 if (get_float_arg(argvars, &f) == OK)
3473 rettv->vval.v_float = floor(f);
3474 else
3475 rettv->vval.v_float = 0.0;
3476}
3477
3478/*
3479 * "fmod()" function
3480 */
3481 static void
3482f_fmod(typval_T *argvars, typval_T *rettv)
3483{
3484 float_T fx = 0.0, fy = 0.0;
3485
3486 rettv->v_type = VAR_FLOAT;
3487 if (get_float_arg(argvars, &fx) == OK
3488 && get_float_arg(&argvars[1], &fy) == OK)
3489 rettv->vval.v_float = fmod(fx, fy);
3490 else
3491 rettv->vval.v_float = 0.0;
3492}
3493#endif
3494
3495/*
3496 * "fnameescape({string})" function
3497 */
3498 static void
3499f_fnameescape(typval_T *argvars, typval_T *rettv)
3500{
3501 rettv->vval.v_string = vim_strsave_fnameescape(
3502 get_tv_string(&argvars[0]), FALSE);
3503 rettv->v_type = VAR_STRING;
3504}
3505
3506/*
3507 * "fnamemodify({fname}, {mods})" function
3508 */
3509 static void
3510f_fnamemodify(typval_T *argvars, typval_T *rettv)
3511{
3512 char_u *fname;
3513 char_u *mods;
3514 int usedlen = 0;
3515 int len;
3516 char_u *fbuf = NULL;
3517 char_u buf[NUMBUFLEN];
3518
3519 fname = get_tv_string_chk(&argvars[0]);
3520 mods = get_tv_string_buf_chk(&argvars[1], buf);
3521 if (fname == NULL || mods == NULL)
3522 fname = NULL;
3523 else
3524 {
3525 len = (int)STRLEN(fname);
3526 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
3527 }
3528
3529 rettv->v_type = VAR_STRING;
3530 if (fname == NULL)
3531 rettv->vval.v_string = NULL;
3532 else
3533 rettv->vval.v_string = vim_strnsave(fname, len);
3534 vim_free(fbuf);
3535}
3536
3537static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
3538
3539/*
3540 * "foldclosed()" function
3541 */
3542 static void
3543foldclosed_both(
3544 typval_T *argvars UNUSED,
3545 typval_T *rettv,
3546 int end UNUSED)
3547{
3548#ifdef FEAT_FOLDING
3549 linenr_T lnum;
3550 linenr_T first, last;
3551
3552 lnum = get_tv_lnum(argvars);
3553 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3554 {
3555 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3556 {
3557 if (end)
3558 rettv->vval.v_number = (varnumber_T)last;
3559 else
3560 rettv->vval.v_number = (varnumber_T)first;
3561 return;
3562 }
3563 }
3564#endif
3565 rettv->vval.v_number = -1;
3566}
3567
3568/*
3569 * "foldclosed()" function
3570 */
3571 static void
3572f_foldclosed(typval_T *argvars, typval_T *rettv)
3573{
3574 foldclosed_both(argvars, rettv, FALSE);
3575}
3576
3577/*
3578 * "foldclosedend()" function
3579 */
3580 static void
3581f_foldclosedend(typval_T *argvars, typval_T *rettv)
3582{
3583 foldclosed_both(argvars, rettv, TRUE);
3584}
3585
3586/*
3587 * "foldlevel()" function
3588 */
3589 static void
3590f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3591{
3592#ifdef FEAT_FOLDING
3593 linenr_T lnum;
3594
3595 lnum = get_tv_lnum(argvars);
3596 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3597 rettv->vval.v_number = foldLevel(lnum);
3598#endif
3599}
3600
3601/*
3602 * "foldtext()" function
3603 */
3604 static void
3605f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
3606{
3607#ifdef FEAT_FOLDING
3608 linenr_T foldstart;
3609 linenr_T foldend;
3610 char_u *dashes;
3611 linenr_T lnum;
3612 char_u *s;
3613 char_u *r;
3614 int len;
3615 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003616 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003617#endif
3618
3619 rettv->v_type = VAR_STRING;
3620 rettv->vval.v_string = NULL;
3621#ifdef FEAT_FOLDING
3622 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
3623 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
3624 dashes = get_vim_var_str(VV_FOLDDASHES);
3625 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
3626 && dashes != NULL)
3627 {
3628 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02003629 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003630 if (!linewhite(lnum))
3631 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003632
3633 /* Find interesting text in this line. */
3634 s = skipwhite(ml_get(lnum));
3635 /* skip C comment-start */
3636 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
3637 {
3638 s = skipwhite(s + 2);
3639 if (*skipwhite(s) == NUL
3640 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
3641 {
3642 s = skipwhite(ml_get(lnum + 1));
3643 if (*s == '*')
3644 s = skipwhite(s + 1);
3645 }
3646 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02003647 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003648 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003649 r = alloc((unsigned)(STRLEN(txt)
3650 + STRLEN(dashes) /* for %s */
3651 + 20 /* for %3ld */
3652 + STRLEN(s))); /* concatenated */
3653 if (r != NULL)
3654 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02003655 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003656 len = (int)STRLEN(r);
3657 STRCAT(r, s);
3658 /* remove 'foldmarker' and 'commentstring' */
3659 foldtext_cleanup(r + len);
3660 rettv->vval.v_string = r;
3661 }
3662 }
3663#endif
3664}
3665
3666/*
3667 * "foldtextresult(lnum)" function
3668 */
3669 static void
3670f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
3671{
3672#ifdef FEAT_FOLDING
3673 linenr_T lnum;
3674 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003675 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003676 foldinfo_T foldinfo;
3677 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003678 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003679#endif
3680
3681 rettv->v_type = VAR_STRING;
3682 rettv->vval.v_string = NULL;
3683#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003684 if (entered)
3685 return; /* reject recursive use */
3686 entered = TRUE;
3687
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003688 lnum = get_tv_lnum(argvars);
3689 /* treat illegal types and illegal string values for {lnum} the same */
3690 if (lnum < 0)
3691 lnum = 0;
3692 fold_count = foldedCount(curwin, lnum, &foldinfo);
3693 if (fold_count > 0)
3694 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01003695 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
3696 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003697 if (text == buf)
3698 text = vim_strsave(text);
3699 rettv->vval.v_string = text;
3700 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003701
3702 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003703#endif
3704}
3705
3706/*
3707 * "foreground()" function
3708 */
3709 static void
3710f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3711{
3712#ifdef FEAT_GUI
3713 if (gui.in_use)
3714 gui_mch_set_foreground();
3715#else
3716# ifdef WIN32
3717 win32_set_foreground();
3718# endif
3719#endif
3720}
3721
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003722 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003723common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003724{
3725 char_u *s;
3726 char_u *name;
3727 int use_string = FALSE;
3728 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003729 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003730
3731 if (argvars[0].v_type == VAR_FUNC)
3732 {
3733 /* function(MyFunc, [arg], dict) */
3734 s = argvars[0].vval.v_string;
3735 }
3736 else if (argvars[0].v_type == VAR_PARTIAL
3737 && argvars[0].vval.v_partial != NULL)
3738 {
3739 /* function(dict.MyFunc, [arg]) */
3740 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003741 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003742 }
3743 else
3744 {
3745 /* function('MyFunc', [arg], dict) */
3746 s = get_tv_string(&argvars[0]);
3747 use_string = TRUE;
3748 }
3749
Bram Moolenaar843b8842016-08-21 14:36:15 +02003750 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003751 {
3752 name = s;
3753 trans_name = trans_function_name(&name, FALSE,
3754 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
3755 if (*name != NUL)
3756 s = NULL;
3757 }
3758
Bram Moolenaar843b8842016-08-21 14:36:15 +02003759 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
3760 || (is_funcref && trans_name == NULL))
Bram Moolenaar5582ef12016-09-14 22:16:13 +02003761 EMSG2(_(e_invarg2), use_string ? get_tv_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003762 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003763 else if (trans_name != NULL && (is_funcref
3764 ? find_func(trans_name) == NULL
3765 : !translated_function_exists(trans_name)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003766 EMSG2(_("E700: Unknown function: %s"), s);
3767 else
3768 {
3769 int dict_idx = 0;
3770 int arg_idx = 0;
3771 list_T *list = NULL;
3772
3773 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
3774 {
3775 char sid_buf[25];
3776 int off = *s == 's' ? 2 : 5;
3777
3778 /* Expand s: and <SID> into <SNR>nr_, so that the function can
3779 * also be called from another script. Using trans_function_name()
3780 * would also work, but some plugins depend on the name being
3781 * printable text. */
3782 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
3783 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
3784 if (name != NULL)
3785 {
3786 STRCPY(name, sid_buf);
3787 STRCAT(name, s + off);
3788 }
3789 }
3790 else
3791 name = vim_strsave(s);
3792
3793 if (argvars[1].v_type != VAR_UNKNOWN)
3794 {
3795 if (argvars[2].v_type != VAR_UNKNOWN)
3796 {
3797 /* function(name, [args], dict) */
3798 arg_idx = 1;
3799 dict_idx = 2;
3800 }
3801 else if (argvars[1].v_type == VAR_DICT)
3802 /* function(name, dict) */
3803 dict_idx = 1;
3804 else
3805 /* function(name, [args]) */
3806 arg_idx = 1;
3807 if (dict_idx > 0)
3808 {
3809 if (argvars[dict_idx].v_type != VAR_DICT)
3810 {
3811 EMSG(_("E922: expected a dict"));
3812 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003813 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003814 }
3815 if (argvars[dict_idx].vval.v_dict == NULL)
3816 dict_idx = 0;
3817 }
3818 if (arg_idx > 0)
3819 {
3820 if (argvars[arg_idx].v_type != VAR_LIST)
3821 {
3822 EMSG(_("E923: Second argument of function() must be a list or a dict"));
3823 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003824 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003825 }
3826 list = argvars[arg_idx].vval.v_list;
3827 if (list == NULL || list->lv_len == 0)
3828 arg_idx = 0;
3829 }
3830 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003831 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003832 {
3833 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
3834
3835 /* result is a VAR_PARTIAL */
3836 if (pt == NULL)
3837 vim_free(name);
3838 else
3839 {
3840 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
3841 {
3842 listitem_T *li;
3843 int i = 0;
3844 int arg_len = 0;
3845 int lv_len = 0;
3846
3847 if (arg_pt != NULL)
3848 arg_len = arg_pt->pt_argc;
3849 if (list != NULL)
3850 lv_len = list->lv_len;
3851 pt->pt_argc = arg_len + lv_len;
3852 pt->pt_argv = (typval_T *)alloc(
3853 sizeof(typval_T) * pt->pt_argc);
3854 if (pt->pt_argv == NULL)
3855 {
3856 vim_free(pt);
3857 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003858 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003859 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003860 for (i = 0; i < arg_len; i++)
3861 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
3862 if (lv_len > 0)
3863 for (li = list->lv_first; li != NULL;
3864 li = li->li_next)
3865 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003866 }
3867
3868 /* For "function(dict.func, [], dict)" and "func" is a partial
3869 * use "dict". That is backwards compatible. */
3870 if (dict_idx > 0)
3871 {
3872 /* The dict is bound explicitly, pt_auto is FALSE. */
3873 pt->pt_dict = argvars[dict_idx].vval.v_dict;
3874 ++pt->pt_dict->dv_refcount;
3875 }
3876 else if (arg_pt != NULL)
3877 {
3878 /* If the dict was bound automatically the result is also
3879 * bound automatically. */
3880 pt->pt_dict = arg_pt->pt_dict;
3881 pt->pt_auto = arg_pt->pt_auto;
3882 if (pt->pt_dict != NULL)
3883 ++pt->pt_dict->dv_refcount;
3884 }
3885
3886 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003887 if (arg_pt != NULL && arg_pt->pt_func != NULL)
3888 {
3889 pt->pt_func = arg_pt->pt_func;
3890 func_ptr_ref(pt->pt_func);
3891 vim_free(name);
3892 }
3893 else if (is_funcref)
3894 {
3895 pt->pt_func = find_func(trans_name);
3896 func_ptr_ref(pt->pt_func);
3897 vim_free(name);
3898 }
3899 else
3900 {
3901 pt->pt_name = name;
3902 func_ref(name);
3903 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003904 }
3905 rettv->v_type = VAR_PARTIAL;
3906 rettv->vval.v_partial = pt;
3907 }
3908 else
3909 {
3910 /* result is a VAR_FUNC */
3911 rettv->v_type = VAR_FUNC;
3912 rettv->vval.v_string = name;
3913 func_ref(name);
3914 }
3915 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003916theend:
3917 vim_free(trans_name);
3918}
3919
3920/*
3921 * "funcref()" function
3922 */
3923 static void
3924f_funcref(typval_T *argvars, typval_T *rettv)
3925{
3926 common_function(argvars, rettv, TRUE);
3927}
3928
3929/*
3930 * "function()" function
3931 */
3932 static void
3933f_function(typval_T *argvars, typval_T *rettv)
3934{
3935 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003936}
3937
3938/*
3939 * "garbagecollect()" function
3940 */
3941 static void
3942f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
3943{
3944 /* This is postponed until we are back at the toplevel, because we may be
3945 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
3946 want_garbage_collect = TRUE;
3947
3948 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
3949 garbage_collect_at_exit = TRUE;
3950}
3951
3952/*
3953 * "get()" function
3954 */
3955 static void
3956f_get(typval_T *argvars, typval_T *rettv)
3957{
3958 listitem_T *li;
3959 list_T *l;
3960 dictitem_T *di;
3961 dict_T *d;
3962 typval_T *tv = NULL;
3963
3964 if (argvars[0].v_type == VAR_LIST)
3965 {
3966 if ((l = argvars[0].vval.v_list) != NULL)
3967 {
3968 int error = FALSE;
3969
3970 li = list_find(l, (long)get_tv_number_chk(&argvars[1], &error));
3971 if (!error && li != NULL)
3972 tv = &li->li_tv;
3973 }
3974 }
3975 else if (argvars[0].v_type == VAR_DICT)
3976 {
3977 if ((d = argvars[0].vval.v_dict) != NULL)
3978 {
3979 di = dict_find(d, get_tv_string(&argvars[1]), -1);
3980 if (di != NULL)
3981 tv = &di->di_tv;
3982 }
3983 }
3984 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
3985 {
3986 partial_T *pt;
3987 partial_T fref_pt;
3988
3989 if (argvars[0].v_type == VAR_PARTIAL)
3990 pt = argvars[0].vval.v_partial;
3991 else
3992 {
3993 vim_memset(&fref_pt, 0, sizeof(fref_pt));
3994 fref_pt.pt_name = argvars[0].vval.v_string;
3995 pt = &fref_pt;
3996 }
3997
3998 if (pt != NULL)
3999 {
4000 char_u *what = get_tv_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004001 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004002
4003 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4004 {
4005 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004006 n = partial_name(pt);
4007 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004008 rettv->vval.v_string = NULL;
4009 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004010 {
4011 rettv->vval.v_string = vim_strsave(n);
4012 if (rettv->v_type == VAR_FUNC)
4013 func_ref(rettv->vval.v_string);
4014 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004015 }
4016 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004017 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004018 else if (STRCMP(what, "args") == 0)
4019 {
4020 rettv->v_type = VAR_LIST;
4021 if (rettv_list_alloc(rettv) == OK)
4022 {
4023 int i;
4024
4025 for (i = 0; i < pt->pt_argc; ++i)
4026 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4027 }
4028 }
4029 else
4030 EMSG2(_(e_invarg2), what);
4031 return;
4032 }
4033 }
4034 else
4035 EMSG2(_(e_listdictarg), "get()");
4036
4037 if (tv == NULL)
4038 {
4039 if (argvars[2].v_type != VAR_UNKNOWN)
4040 copy_tv(&argvars[2], rettv);
4041 }
4042 else
4043 copy_tv(tv, rettv);
4044}
4045
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004046#ifdef FEAT_SIGNS
4047/*
4048 * Returns information about signs placed in a buffer as list of dicts.
4049 */
4050 static void
4051get_buffer_signs(buf_T *buf, list_T *l)
4052{
4053 signlist_T *sign;
4054
4055 for (sign = buf->b_signlist; sign; sign = sign->next)
4056 {
4057 dict_T *d = dict_alloc();
4058
4059 if (d != NULL)
4060 {
4061 dict_add_nr_str(d, "id", sign->id, NULL);
4062 dict_add_nr_str(d, "lnum", sign->lnum, NULL);
Bram Moolenaar6a402ed2016-08-28 14:11:24 +02004063 dict_add_nr_str(d, "name", 0L, sign_typenr2name(sign->typenr));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004064
4065 list_append_dict(l, d);
4066 }
4067 }
4068}
4069#endif
4070
4071/*
4072 * Returns buffer options, variables and other attributes in a dictionary.
4073 */
4074 static dict_T *
4075get_buffer_info(buf_T *buf)
4076{
4077 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004078 tabpage_T *tp;
4079 win_T *wp;
4080 list_T *windows;
4081
4082 dict = dict_alloc();
4083 if (dict == NULL)
4084 return NULL;
4085
Bram Moolenaar33928832016-08-18 21:22:04 +02004086 dict_add_nr_str(dict, "bufnr", buf->b_fnum, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004087 dict_add_nr_str(dict, "name", 0L,
4088 buf->b_ffname != NULL ? buf->b_ffname : (char_u *)"");
Bram Moolenaarf845b872017-01-06 14:04:54 +01004089 dict_add_nr_str(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4090 : buflist_findlnum(buf), NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004091 dict_add_nr_str(dict, "loaded", buf->b_ml.ml_mfp != NULL, NULL);
4092 dict_add_nr_str(dict, "listed", buf->b_p_bl, NULL);
4093 dict_add_nr_str(dict, "changed", bufIsChanged(buf), NULL);
Bram Moolenaar95c526e2017-02-25 14:59:34 +01004094 dict_add_nr_str(dict, "changedtick", CHANGEDTICK(buf), NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004095 dict_add_nr_str(dict, "hidden",
4096 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0,
4097 NULL);
4098
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004099 /* Get a reference to buffer variables */
4100 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004101
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004102 /* List of windows displaying this buffer */
4103 windows = list_alloc();
4104 if (windows != NULL)
4105 {
4106 FOR_ALL_TAB_WINDOWS(tp, wp)
4107 if (wp->w_buffer == buf)
4108 list_append_number(windows, (varnumber_T)wp->w_id);
4109 dict_add_list(dict, "windows", windows);
4110 }
4111
4112#ifdef FEAT_SIGNS
4113 if (buf->b_signlist != NULL)
4114 {
4115 /* List of signs placed in this buffer */
4116 list_T *signs = list_alloc();
4117 if (signs != NULL)
4118 {
4119 get_buffer_signs(buf, signs);
4120 dict_add_list(dict, "signs", signs);
4121 }
4122 }
4123#endif
4124
4125 return dict;
4126}
4127
4128/*
4129 * "getbufinfo()" function
4130 */
4131 static void
4132f_getbufinfo(typval_T *argvars, typval_T *rettv)
4133{
4134 buf_T *buf = NULL;
4135 buf_T *argbuf = NULL;
4136 dict_T *d;
4137 int filtered = FALSE;
4138 int sel_buflisted = FALSE;
4139 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004140 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004141
4142 if (rettv_list_alloc(rettv) != OK)
4143 return;
4144
4145 /* List of all the buffers or selected buffers */
4146 if (argvars[0].v_type == VAR_DICT)
4147 {
4148 dict_T *sel_d = argvars[0].vval.v_dict;
4149
4150 if (sel_d != NULL)
4151 {
4152 dictitem_T *di;
4153
4154 filtered = TRUE;
4155
4156 di = dict_find(sel_d, (char_u *)"buflisted", -1);
4157 if (di != NULL && get_tv_number(&di->di_tv))
4158 sel_buflisted = TRUE;
4159
4160 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
4161 if (di != NULL && get_tv_number(&di->di_tv))
4162 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004163
4164 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
4165 if (di != NULL && get_tv_number(&di->di_tv))
4166 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004167 }
4168 }
4169 else if (argvars[0].v_type != VAR_UNKNOWN)
4170 {
4171 /* Information about one buffer. Argument specifies the buffer */
4172 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4173 ++emsg_off;
4174 argbuf = get_buf_tv(&argvars[0], FALSE);
4175 --emsg_off;
4176 if (argbuf == NULL)
4177 return;
4178 }
4179
4180 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004181 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004182 {
4183 if (argbuf != NULL && argbuf != buf)
4184 continue;
4185 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004186 || (sel_buflisted && !buf->b_p_bl)
4187 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004188 continue;
4189
4190 d = get_buffer_info(buf);
4191 if (d != NULL)
4192 list_append_dict(rettv->vval.v_list, d);
4193 if (argbuf != NULL)
4194 return;
4195 }
4196}
4197
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004198static void get_buffer_lines(buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv);
4199
4200/*
4201 * Get line or list of lines from buffer "buf" into "rettv".
4202 * Return a range (from start to end) of lines in rettv from the specified
4203 * buffer.
4204 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4205 */
4206 static void
4207get_buffer_lines(
4208 buf_T *buf,
4209 linenr_T start,
4210 linenr_T end,
4211 int retlist,
4212 typval_T *rettv)
4213{
4214 char_u *p;
4215
4216 rettv->v_type = VAR_STRING;
4217 rettv->vval.v_string = NULL;
4218 if (retlist && rettv_list_alloc(rettv) == FAIL)
4219 return;
4220
4221 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4222 return;
4223
4224 if (!retlist)
4225 {
4226 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4227 p = ml_get_buf(buf, start, FALSE);
4228 else
4229 p = (char_u *)"";
4230 rettv->vval.v_string = vim_strsave(p);
4231 }
4232 else
4233 {
4234 if (end < start)
4235 return;
4236
4237 if (start < 1)
4238 start = 1;
4239 if (end > buf->b_ml.ml_line_count)
4240 end = buf->b_ml.ml_line_count;
4241 while (start <= end)
4242 if (list_append_string(rettv->vval.v_list,
4243 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4244 break;
4245 }
4246}
4247
4248/*
4249 * Get the lnum from the first argument.
4250 * Also accepts "$", then "buf" is used.
4251 * Returns 0 on error.
4252 */
4253 static linenr_T
4254get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
4255{
4256 if (argvars[0].v_type == VAR_STRING
4257 && argvars[0].vval.v_string != NULL
4258 && argvars[0].vval.v_string[0] == '$'
4259 && buf != NULL)
4260 return buf->b_ml.ml_line_count;
4261 return (linenr_T)get_tv_number_chk(&argvars[0], NULL);
4262}
4263
4264/*
4265 * "getbufline()" function
4266 */
4267 static void
4268f_getbufline(typval_T *argvars, typval_T *rettv)
4269{
4270 linenr_T lnum;
4271 linenr_T end;
4272 buf_T *buf;
4273
4274 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4275 ++emsg_off;
4276 buf = get_buf_tv(&argvars[0], FALSE);
4277 --emsg_off;
4278
4279 lnum = get_tv_lnum_buf(&argvars[1], buf);
4280 if (argvars[2].v_type == VAR_UNKNOWN)
4281 end = lnum;
4282 else
4283 end = get_tv_lnum_buf(&argvars[2], buf);
4284
4285 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4286}
4287
4288/*
4289 * "getbufvar()" function
4290 */
4291 static void
4292f_getbufvar(typval_T *argvars, typval_T *rettv)
4293{
4294 buf_T *buf;
4295 buf_T *save_curbuf;
4296 char_u *varname;
4297 dictitem_T *v;
4298 int done = FALSE;
4299
4300 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4301 varname = get_tv_string_chk(&argvars[1]);
4302 ++emsg_off;
4303 buf = get_buf_tv(&argvars[0], FALSE);
4304
4305 rettv->v_type = VAR_STRING;
4306 rettv->vval.v_string = NULL;
4307
4308 if (buf != NULL && varname != NULL)
4309 {
4310 /* set curbuf to be our buf, temporarily */
4311 save_curbuf = curbuf;
4312 curbuf = buf;
4313
Bram Moolenaar30567352016-08-27 21:25:44 +02004314 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004315 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004316 if (varname[1] == NUL)
4317 {
4318 /* get all buffer-local options in a dict */
4319 dict_T *opts = get_winbuf_options(TRUE);
4320
4321 if (opts != NULL)
4322 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004323 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004324 done = TRUE;
4325 }
4326 }
4327 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4328 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004329 done = TRUE;
4330 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004331 else
4332 {
4333 /* Look up the variable. */
4334 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4335 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4336 'b', varname, FALSE);
4337 if (v != NULL)
4338 {
4339 copy_tv(&v->di_tv, rettv);
4340 done = TRUE;
4341 }
4342 }
4343
4344 /* restore previous notion of curbuf */
4345 curbuf = save_curbuf;
4346 }
4347
4348 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4349 /* use the default value */
4350 copy_tv(&argvars[2], rettv);
4351
4352 --emsg_off;
4353}
4354
4355/*
4356 * "getchar()" function
4357 */
4358 static void
4359f_getchar(typval_T *argvars, typval_T *rettv)
4360{
4361 varnumber_T n;
4362 int error = FALSE;
4363
4364 /* Position the cursor. Needed after a message that ends in a space. */
4365 windgoto(msg_row, msg_col);
4366
4367 ++no_mapping;
4368 ++allow_keys;
4369 for (;;)
4370 {
4371 if (argvars[0].v_type == VAR_UNKNOWN)
4372 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004373 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004374 else if (get_tv_number_chk(&argvars[0], &error) == 1)
4375 /* getchar(1): only check if char avail */
4376 n = vpeekc_any();
4377 else if (error || vpeekc_any() == NUL)
4378 /* illegal argument or getchar(0) and no char avail: return zero */
4379 n = 0;
4380 else
4381 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004382 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004383
4384 if (n == K_IGNORE)
4385 continue;
4386 break;
4387 }
4388 --no_mapping;
4389 --allow_keys;
4390
4391 set_vim_var_nr(VV_MOUSE_WIN, 0);
4392 set_vim_var_nr(VV_MOUSE_WINID, 0);
4393 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4394 set_vim_var_nr(VV_MOUSE_COL, 0);
4395
4396 rettv->vval.v_number = n;
4397 if (IS_SPECIAL(n) || mod_mask != 0)
4398 {
4399 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4400 int i = 0;
4401
4402 /* Turn a special key into three bytes, plus modifier. */
4403 if (mod_mask != 0)
4404 {
4405 temp[i++] = K_SPECIAL;
4406 temp[i++] = KS_MODIFIER;
4407 temp[i++] = mod_mask;
4408 }
4409 if (IS_SPECIAL(n))
4410 {
4411 temp[i++] = K_SPECIAL;
4412 temp[i++] = K_SECOND(n);
4413 temp[i++] = K_THIRD(n);
4414 }
4415#ifdef FEAT_MBYTE
4416 else if (has_mbyte)
4417 i += (*mb_char2bytes)(n, temp + i);
4418#endif
4419 else
4420 temp[i++] = n;
4421 temp[i++] = NUL;
4422 rettv->v_type = VAR_STRING;
4423 rettv->vval.v_string = vim_strsave(temp);
4424
4425#ifdef FEAT_MOUSE
4426 if (is_mouse_key(n))
4427 {
4428 int row = mouse_row;
4429 int col = mouse_col;
4430 win_T *win;
4431 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004432 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004433 int winnr = 1;
4434
4435 if (row >= 0 && col >= 0)
4436 {
4437 /* Find the window at the mouse coordinates and compute the
4438 * text position. */
4439 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004440 if (win == NULL)
4441 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004442 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004443 for (wp = firstwin; wp != win; wp = wp->w_next)
4444 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004445 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4446 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4447 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4448 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4449 }
4450 }
4451#endif
4452 }
4453}
4454
4455/*
4456 * "getcharmod()" function
4457 */
4458 static void
4459f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4460{
4461 rettv->vval.v_number = mod_mask;
4462}
4463
4464/*
4465 * "getcharsearch()" function
4466 */
4467 static void
4468f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4469{
4470 if (rettv_dict_alloc(rettv) != FAIL)
4471 {
4472 dict_T *dict = rettv->vval.v_dict;
4473
4474 dict_add_nr_str(dict, "char", 0L, last_csearch());
4475 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
4476 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
4477 }
4478}
4479
4480/*
4481 * "getcmdline()" function
4482 */
4483 static void
4484f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4485{
4486 rettv->v_type = VAR_STRING;
4487 rettv->vval.v_string = get_cmdline_str();
4488}
4489
4490/*
4491 * "getcmdpos()" function
4492 */
4493 static void
4494f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4495{
4496 rettv->vval.v_number = get_cmdline_pos() + 1;
4497}
4498
4499/*
4500 * "getcmdtype()" function
4501 */
4502 static void
4503f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4504{
4505 rettv->v_type = VAR_STRING;
4506 rettv->vval.v_string = alloc(2);
4507 if (rettv->vval.v_string != NULL)
4508 {
4509 rettv->vval.v_string[0] = get_cmdline_type();
4510 rettv->vval.v_string[1] = NUL;
4511 }
4512}
4513
4514/*
4515 * "getcmdwintype()" function
4516 */
4517 static void
4518f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4519{
4520 rettv->v_type = VAR_STRING;
4521 rettv->vval.v_string = NULL;
4522#ifdef FEAT_CMDWIN
4523 rettv->vval.v_string = alloc(2);
4524 if (rettv->vval.v_string != NULL)
4525 {
4526 rettv->vval.v_string[0] = cmdwin_type;
4527 rettv->vval.v_string[1] = NUL;
4528 }
4529#endif
4530}
4531
4532#if defined(FEAT_CMDL_COMPL)
4533/*
4534 * "getcompletion()" function
4535 */
4536 static void
4537f_getcompletion(typval_T *argvars, typval_T *rettv)
4538{
4539 char_u *pat;
4540 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004541 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004542 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
4543 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004544
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004545 if (argvars[2].v_type != VAR_UNKNOWN)
4546 filtered = get_tv_number_chk(&argvars[2], NULL);
4547
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004548 if (p_wic)
4549 options |= WILD_ICASE;
4550
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004551 /* For filtered results, 'wildignore' is used */
4552 if (!filtered)
4553 options |= WILD_KEEP_ALL;
4554
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004555 ExpandInit(&xpc);
4556 xpc.xp_pattern = get_tv_string(&argvars[0]);
4557 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4558 xpc.xp_context = cmdcomplete_str_to_type(get_tv_string(&argvars[1]));
4559 if (xpc.xp_context == EXPAND_NOTHING)
4560 {
4561 if (argvars[1].v_type == VAR_STRING)
4562 EMSG2(_(e_invarg2), argvars[1].vval.v_string);
4563 else
4564 EMSG(_(e_invarg));
4565 return;
4566 }
4567
4568# if defined(FEAT_MENU)
4569 if (xpc.xp_context == EXPAND_MENUS)
4570 {
4571 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4572 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4573 }
4574# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02004575#ifdef FEAT_CSCOPE
4576 if (xpc.xp_context == EXPAND_CSCOPE)
4577 {
4578 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4579 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4580 }
4581#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02004582#ifdef FEAT_SIGNS
4583 if (xpc.xp_context == EXPAND_SIGN)
4584 {
4585 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4586 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4587 }
4588#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004589
4590 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4591 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
4592 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004593 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004594
4595 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4596
4597 for (i = 0; i < xpc.xp_numfiles; i++)
4598 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4599 }
4600 vim_free(pat);
4601 ExpandCleanup(&xpc);
4602}
4603#endif
4604
4605/*
4606 * "getcwd()" function
4607 */
4608 static void
4609f_getcwd(typval_T *argvars, typval_T *rettv)
4610{
4611 win_T *wp = NULL;
4612 char_u *cwd;
4613
4614 rettv->v_type = VAR_STRING;
4615 rettv->vval.v_string = NULL;
4616
4617 wp = find_tabwin(&argvars[0], &argvars[1]);
4618 if (wp != NULL)
4619 {
4620 if (wp->w_localdir != NULL)
4621 rettv->vval.v_string = vim_strsave(wp->w_localdir);
4622 else if (globaldir != NULL)
4623 rettv->vval.v_string = vim_strsave(globaldir);
4624 else
4625 {
4626 cwd = alloc(MAXPATHL);
4627 if (cwd != NULL)
4628 {
4629 if (mch_dirname(cwd, MAXPATHL) != FAIL)
4630 rettv->vval.v_string = vim_strsave(cwd);
4631 vim_free(cwd);
4632 }
4633 }
4634#ifdef BACKSLASH_IN_FILENAME
4635 if (rettv->vval.v_string != NULL)
4636 slash_adjust(rettv->vval.v_string);
4637#endif
4638 }
4639}
4640
4641/*
4642 * "getfontname()" function
4643 */
4644 static void
4645f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4646{
4647 rettv->v_type = VAR_STRING;
4648 rettv->vval.v_string = NULL;
4649#ifdef FEAT_GUI
4650 if (gui.in_use)
4651 {
4652 GuiFont font;
4653 char_u *name = NULL;
4654
4655 if (argvars[0].v_type == VAR_UNKNOWN)
4656 {
4657 /* Get the "Normal" font. Either the name saved by
4658 * hl_set_font_name() or from the font ID. */
4659 font = gui.norm_font;
4660 name = hl_get_font_name();
4661 }
4662 else
4663 {
4664 name = get_tv_string(&argvars[0]);
4665 if (STRCMP(name, "*") == 0) /* don't use font dialog */
4666 return;
4667 font = gui_mch_get_font(name, FALSE);
4668 if (font == NOFONT)
4669 return; /* Invalid font name, return empty string. */
4670 }
4671 rettv->vval.v_string = gui_mch_get_fontname(font, name);
4672 if (argvars[0].v_type != VAR_UNKNOWN)
4673 gui_mch_free_font(font);
4674 }
4675#endif
4676}
4677
4678/*
4679 * "getfperm({fname})" function
4680 */
4681 static void
4682f_getfperm(typval_T *argvars, typval_T *rettv)
4683{
4684 char_u *fname;
4685 stat_T st;
4686 char_u *perm = NULL;
4687 char_u flags[] = "rwx";
4688 int i;
4689
4690 fname = get_tv_string(&argvars[0]);
4691
4692 rettv->v_type = VAR_STRING;
4693 if (mch_stat((char *)fname, &st) >= 0)
4694 {
4695 perm = vim_strsave((char_u *)"---------");
4696 if (perm != NULL)
4697 {
4698 for (i = 0; i < 9; i++)
4699 {
4700 if (st.st_mode & (1 << (8 - i)))
4701 perm[i] = flags[i % 3];
4702 }
4703 }
4704 }
4705 rettv->vval.v_string = perm;
4706}
4707
4708/*
4709 * "getfsize({fname})" function
4710 */
4711 static void
4712f_getfsize(typval_T *argvars, typval_T *rettv)
4713{
4714 char_u *fname;
4715 stat_T st;
4716
4717 fname = get_tv_string(&argvars[0]);
4718
4719 rettv->v_type = VAR_NUMBER;
4720
4721 if (mch_stat((char *)fname, &st) >= 0)
4722 {
4723 if (mch_isdir(fname))
4724 rettv->vval.v_number = 0;
4725 else
4726 {
4727 rettv->vval.v_number = (varnumber_T)st.st_size;
4728
4729 /* non-perfect check for overflow */
4730 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
4731 rettv->vval.v_number = -2;
4732 }
4733 }
4734 else
4735 rettv->vval.v_number = -1;
4736}
4737
4738/*
4739 * "getftime({fname})" function
4740 */
4741 static void
4742f_getftime(typval_T *argvars, typval_T *rettv)
4743{
4744 char_u *fname;
4745 stat_T st;
4746
4747 fname = get_tv_string(&argvars[0]);
4748
4749 if (mch_stat((char *)fname, &st) >= 0)
4750 rettv->vval.v_number = (varnumber_T)st.st_mtime;
4751 else
4752 rettv->vval.v_number = -1;
4753}
4754
4755/*
4756 * "getftype({fname})" function
4757 */
4758 static void
4759f_getftype(typval_T *argvars, typval_T *rettv)
4760{
4761 char_u *fname;
4762 stat_T st;
4763 char_u *type = NULL;
4764 char *t;
4765
4766 fname = get_tv_string(&argvars[0]);
4767
4768 rettv->v_type = VAR_STRING;
4769 if (mch_lstat((char *)fname, &st) >= 0)
4770 {
4771#ifdef S_ISREG
4772 if (S_ISREG(st.st_mode))
4773 t = "file";
4774 else if (S_ISDIR(st.st_mode))
4775 t = "dir";
4776# ifdef S_ISLNK
4777 else if (S_ISLNK(st.st_mode))
4778 t = "link";
4779# endif
4780# ifdef S_ISBLK
4781 else if (S_ISBLK(st.st_mode))
4782 t = "bdev";
4783# endif
4784# ifdef S_ISCHR
4785 else if (S_ISCHR(st.st_mode))
4786 t = "cdev";
4787# endif
4788# ifdef S_ISFIFO
4789 else if (S_ISFIFO(st.st_mode))
4790 t = "fifo";
4791# endif
4792# ifdef S_ISSOCK
4793 else if (S_ISSOCK(st.st_mode))
4794 t = "fifo";
4795# endif
4796 else
4797 t = "other";
4798#else
4799# ifdef S_IFMT
4800 switch (st.st_mode & S_IFMT)
4801 {
4802 case S_IFREG: t = "file"; break;
4803 case S_IFDIR: t = "dir"; break;
4804# ifdef S_IFLNK
4805 case S_IFLNK: t = "link"; break;
4806# endif
4807# ifdef S_IFBLK
4808 case S_IFBLK: t = "bdev"; break;
4809# endif
4810# ifdef S_IFCHR
4811 case S_IFCHR: t = "cdev"; break;
4812# endif
4813# ifdef S_IFIFO
4814 case S_IFIFO: t = "fifo"; break;
4815# endif
4816# ifdef S_IFSOCK
4817 case S_IFSOCK: t = "socket"; break;
4818# endif
4819 default: t = "other";
4820 }
4821# else
4822 if (mch_isdir(fname))
4823 t = "dir";
4824 else
4825 t = "file";
4826# endif
4827#endif
4828 type = vim_strsave((char_u *)t);
4829 }
4830 rettv->vval.v_string = type;
4831}
4832
4833/*
4834 * "getline(lnum, [end])" function
4835 */
4836 static void
4837f_getline(typval_T *argvars, typval_T *rettv)
4838{
4839 linenr_T lnum;
4840 linenr_T end;
4841 int retlist;
4842
4843 lnum = get_tv_lnum(argvars);
4844 if (argvars[1].v_type == VAR_UNKNOWN)
4845 {
4846 end = 0;
4847 retlist = FALSE;
4848 }
4849 else
4850 {
4851 end = get_tv_lnum(&argvars[1]);
4852 retlist = TRUE;
4853 }
4854
4855 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
4856}
4857
Bram Moolenaar4ae20952016-08-13 15:29:14 +02004858#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02004859 static void
4860get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
4861{
Bram Moolenaard823fa92016-08-12 16:29:27 +02004862 if (what_arg->v_type == VAR_UNKNOWN)
4863 {
4864 if (rettv_list_alloc(rettv) == OK)
4865 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004866 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004867 }
4868 else
4869 {
4870 if (rettv_dict_alloc(rettv) == OK)
4871 if (is_qf || (wp != NULL))
4872 {
4873 if (what_arg->v_type == VAR_DICT)
4874 {
4875 dict_T *d = what_arg->vval.v_dict;
4876
4877 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02004878 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004879 }
4880 else
4881 EMSG(_(e_dictreq));
4882 }
4883 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004884}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02004885#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02004886
4887/*
4888 * "getloclist()" function
4889 */
4890 static void
4891f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4892{
4893#ifdef FEAT_QUICKFIX
4894 win_T *wp;
4895
4896 wp = find_win_by_nr(&argvars[0], NULL);
4897 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
4898#endif
4899}
4900
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004901/*
4902 * "getmatches()" function
4903 */
4904 static void
4905f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4906{
4907#ifdef FEAT_SEARCH_EXTRA
4908 dict_T *dict;
4909 matchitem_T *cur = curwin->w_match_head;
4910 int i;
4911
4912 if (rettv_list_alloc(rettv) == OK)
4913 {
4914 while (cur != NULL)
4915 {
4916 dict = dict_alloc();
4917 if (dict == NULL)
4918 return;
4919 if (cur->match.regprog == NULL)
4920 {
4921 /* match added with matchaddpos() */
4922 for (i = 0; i < MAXPOSMATCH; ++i)
4923 {
4924 llpos_T *llpos;
4925 char buf[6];
4926 list_T *l;
4927
4928 llpos = &cur->pos.pos[i];
4929 if (llpos->lnum == 0)
4930 break;
4931 l = list_alloc();
4932 if (l == NULL)
4933 break;
4934 list_append_number(l, (varnumber_T)llpos->lnum);
4935 if (llpos->col > 0)
4936 {
4937 list_append_number(l, (varnumber_T)llpos->col);
4938 list_append_number(l, (varnumber_T)llpos->len);
4939 }
4940 sprintf(buf, "pos%d", i + 1);
4941 dict_add_list(dict, buf, l);
4942 }
4943 }
4944 else
4945 {
4946 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
4947 }
4948 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
4949 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
4950 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
4951# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
4952 if (cur->conceal_char)
4953 {
4954 char_u buf[MB_MAXBYTES + 1];
4955
4956 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
4957 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
4958 }
4959# endif
4960 list_append_dict(rettv->vval.v_list, dict);
4961 cur = cur->next;
4962 }
4963 }
4964#endif
4965}
4966
4967/*
4968 * "getpid()" function
4969 */
4970 static void
4971f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
4972{
4973 rettv->vval.v_number = mch_get_pid();
4974}
4975
4976 static void
4977getpos_both(
4978 typval_T *argvars,
4979 typval_T *rettv,
4980 int getcurpos)
4981{
4982 pos_T *fp;
4983 list_T *l;
4984 int fnum = -1;
4985
4986 if (rettv_list_alloc(rettv) == OK)
4987 {
4988 l = rettv->vval.v_list;
4989 if (getcurpos)
4990 fp = &curwin->w_cursor;
4991 else
4992 fp = var2fpos(&argvars[0], TRUE, &fnum);
4993 if (fnum != -1)
4994 list_append_number(l, (varnumber_T)fnum);
4995 else
4996 list_append_number(l, (varnumber_T)0);
4997 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
4998 : (varnumber_T)0);
4999 list_append_number(l, (fp != NULL)
5000 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5001 : (varnumber_T)0);
5002 list_append_number(l,
5003#ifdef FEAT_VIRTUALEDIT
5004 (fp != NULL) ? (varnumber_T)fp->coladd :
5005#endif
5006 (varnumber_T)0);
5007 if (getcurpos)
5008 {
5009 update_curswant();
5010 list_append_number(l, curwin->w_curswant == MAXCOL ?
5011 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
5012 }
5013 }
5014 else
5015 rettv->vval.v_number = FALSE;
5016}
5017
5018
5019/*
5020 * "getcurpos()" function
5021 */
5022 static void
5023f_getcurpos(typval_T *argvars, typval_T *rettv)
5024{
5025 getpos_both(argvars, rettv, TRUE);
5026}
5027
5028/*
5029 * "getpos(string)" function
5030 */
5031 static void
5032f_getpos(typval_T *argvars, typval_T *rettv)
5033{
5034 getpos_both(argvars, rettv, FALSE);
5035}
5036
5037/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005038 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005039 */
5040 static void
5041f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5042{
5043#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005044 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005045#endif
5046}
5047
5048/*
5049 * "getreg()" function
5050 */
5051 static void
5052f_getreg(typval_T *argvars, typval_T *rettv)
5053{
5054 char_u *strregname;
5055 int regname;
5056 int arg2 = FALSE;
5057 int return_list = FALSE;
5058 int error = FALSE;
5059
5060 if (argvars[0].v_type != VAR_UNKNOWN)
5061 {
5062 strregname = get_tv_string_chk(&argvars[0]);
5063 error = strregname == NULL;
5064 if (argvars[1].v_type != VAR_UNKNOWN)
5065 {
5066 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
5067 if (!error && argvars[2].v_type != VAR_UNKNOWN)
5068 return_list = (int)get_tv_number_chk(&argvars[2], &error);
5069 }
5070 }
5071 else
5072 strregname = get_vim_var_str(VV_REG);
5073
5074 if (error)
5075 return;
5076
5077 regname = (strregname == NULL ? '"' : *strregname);
5078 if (regname == 0)
5079 regname = '"';
5080
5081 if (return_list)
5082 {
5083 rettv->v_type = VAR_LIST;
5084 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5085 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5086 if (rettv->vval.v_list == NULL)
5087 (void)rettv_list_alloc(rettv);
5088 else
5089 ++rettv->vval.v_list->lv_refcount;
5090 }
5091 else
5092 {
5093 rettv->v_type = VAR_STRING;
5094 rettv->vval.v_string = get_reg_contents(regname,
5095 arg2 ? GREG_EXPR_SRC : 0);
5096 }
5097}
5098
5099/*
5100 * "getregtype()" function
5101 */
5102 static void
5103f_getregtype(typval_T *argvars, typval_T *rettv)
5104{
5105 char_u *strregname;
5106 int regname;
5107 char_u buf[NUMBUFLEN + 2];
5108 long reglen = 0;
5109
5110 if (argvars[0].v_type != VAR_UNKNOWN)
5111 {
5112 strregname = get_tv_string_chk(&argvars[0]);
5113 if (strregname == NULL) /* type error; errmsg already given */
5114 {
5115 rettv->v_type = VAR_STRING;
5116 rettv->vval.v_string = NULL;
5117 return;
5118 }
5119 }
5120 else
5121 /* Default to v:register */
5122 strregname = get_vim_var_str(VV_REG);
5123
5124 regname = (strregname == NULL ? '"' : *strregname);
5125 if (regname == 0)
5126 regname = '"';
5127
5128 buf[0] = NUL;
5129 buf[1] = NUL;
5130 switch (get_reg_type(regname, &reglen))
5131 {
5132 case MLINE: buf[0] = 'V'; break;
5133 case MCHAR: buf[0] = 'v'; break;
5134 case MBLOCK:
5135 buf[0] = Ctrl_V;
5136 sprintf((char *)buf + 1, "%ld", reglen + 1);
5137 break;
5138 }
5139 rettv->v_type = VAR_STRING;
5140 rettv->vval.v_string = vim_strsave(buf);
5141}
5142
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005143/*
5144 * Returns information (variables, options, etc.) about a tab page
5145 * as a dictionary.
5146 */
5147 static dict_T *
5148get_tabpage_info(tabpage_T *tp, int tp_idx)
5149{
5150 win_T *wp;
5151 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005152 list_T *l;
5153
5154 dict = dict_alloc();
5155 if (dict == NULL)
5156 return NULL;
5157
Bram Moolenaar33928832016-08-18 21:22:04 +02005158 dict_add_nr_str(dict, "tabnr", tp_idx, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005159
5160 l = list_alloc();
5161 if (l != NULL)
5162 {
5163 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5164 wp; wp = wp->w_next)
5165 list_append_number(l, (varnumber_T)wp->w_id);
5166 dict_add_list(dict, "windows", l);
5167 }
5168
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005169 /* Make a reference to tabpage variables */
5170 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005171
5172 return dict;
5173}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005174
5175/*
5176 * "gettabinfo()" function
5177 */
5178 static void
5179f_gettabinfo(typval_T *argvars, typval_T *rettv)
5180{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005181 tabpage_T *tp, *tparg = NULL;
5182 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005183 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005184
5185 if (rettv_list_alloc(rettv) != OK)
5186 return;
5187
5188 if (argvars[0].v_type != VAR_UNKNOWN)
5189 {
5190 /* Information about one tab page */
5191 tparg = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5192 if (tparg == NULL)
5193 return;
5194 }
5195
5196 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005197 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005198 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005199 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005200 if (tparg != NULL && tp != tparg)
5201 continue;
5202 d = get_tabpage_info(tp, tpnr);
5203 if (d != NULL)
5204 list_append_dict(rettv->vval.v_list, d);
5205 if (tparg != NULL)
5206 return;
5207 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005208}
5209
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005210/*
5211 * "gettabvar()" function
5212 */
5213 static void
5214f_gettabvar(typval_T *argvars, typval_T *rettv)
5215{
5216 win_T *oldcurwin;
5217 tabpage_T *tp, *oldtabpage;
5218 dictitem_T *v;
5219 char_u *varname;
5220 int done = FALSE;
5221
5222 rettv->v_type = VAR_STRING;
5223 rettv->vval.v_string = NULL;
5224
5225 varname = get_tv_string_chk(&argvars[1]);
5226 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5227 if (tp != NULL && varname != NULL)
5228 {
5229 /* Set tp to be our tabpage, temporarily. Also set the window to the
5230 * first window in the tabpage, otherwise the window is not valid. */
5231 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005232 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5233 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005234 {
5235 /* look up the variable */
5236 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5237 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5238 if (v != NULL)
5239 {
5240 copy_tv(&v->di_tv, rettv);
5241 done = TRUE;
5242 }
5243 }
5244
5245 /* restore previous notion of curwin */
5246 restore_win(oldcurwin, oldtabpage, TRUE);
5247 }
5248
5249 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5250 copy_tv(&argvars[2], rettv);
5251}
5252
5253/*
5254 * "gettabwinvar()" function
5255 */
5256 static void
5257f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5258{
5259 getwinvar(argvars, rettv, 1);
5260}
5261
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005262/*
5263 * Returns information about a window as a dictionary.
5264 */
5265 static dict_T *
5266get_win_info(win_T *wp, short tpnr, short winnr)
5267{
5268 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005269
5270 dict = dict_alloc();
5271 if (dict == NULL)
5272 return NULL;
5273
Bram Moolenaar33928832016-08-18 21:22:04 +02005274 dict_add_nr_str(dict, "tabnr", tpnr, NULL);
5275 dict_add_nr_str(dict, "winnr", winnr, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005276 dict_add_nr_str(dict, "winid", wp->w_id, NULL);
5277 dict_add_nr_str(dict, "height", wp->w_height, NULL);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005278#ifdef FEAT_MENU
5279 dict_add_nr_str(dict, "winbar", wp->w_winbar_height, NULL);
5280#endif
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005281 dict_add_nr_str(dict, "width", wp->w_width, NULL);
Bram Moolenaar33928832016-08-18 21:22:04 +02005282 dict_add_nr_str(dict, "bufnr", wp->w_buffer->b_fnum, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005283
Bram Moolenaar69905d12017-08-13 18:14:47 +02005284#ifdef FEAT_TERMINAL
5285 dict_add_nr_str(dict, "terminal", bt_terminal(wp->w_buffer), NULL);
5286#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005287#ifdef FEAT_QUICKFIX
5288 dict_add_nr_str(dict, "quickfix", bt_quickfix(wp->w_buffer), NULL);
5289 dict_add_nr_str(dict, "loclist",
5290 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL), NULL);
5291#endif
5292
Bram Moolenaar30567352016-08-27 21:25:44 +02005293 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005294 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005295
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005296 return dict;
5297}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005298
5299/*
5300 * "getwininfo()" function
5301 */
5302 static void
5303f_getwininfo(typval_T *argvars, typval_T *rettv)
5304{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005305 tabpage_T *tp;
5306 win_T *wp = NULL, *wparg = NULL;
5307 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005308 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005309
5310 if (rettv_list_alloc(rettv) != OK)
5311 return;
5312
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005313 if (argvars[0].v_type != VAR_UNKNOWN)
5314 {
5315 wparg = win_id2wp(argvars);
5316 if (wparg == NULL)
5317 return;
5318 }
5319
5320 /* Collect information about either all the windows across all the tab
5321 * pages or one particular window.
5322 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005323 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005324 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005325 tabnr++;
5326 winnr = 0;
5327 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005328 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005329 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005330 if (wparg != NULL && wp != wparg)
5331 continue;
5332 d = get_win_info(wp, tabnr, winnr);
5333 if (d != NULL)
5334 list_append_dict(rettv->vval.v_list, d);
5335 if (wparg != NULL)
5336 /* found information about a specific window */
5337 return;
5338 }
5339 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005340}
5341
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005342/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005343 * "win_findbuf()" function
5344 */
5345 static void
5346f_win_findbuf(typval_T *argvars, typval_T *rettv)
5347{
5348 if (rettv_list_alloc(rettv) != FAIL)
5349 win_findbuf(argvars, rettv->vval.v_list);
5350}
5351
5352/*
5353 * "win_getid()" function
5354 */
5355 static void
5356f_win_getid(typval_T *argvars, typval_T *rettv)
5357{
5358 rettv->vval.v_number = win_getid(argvars);
5359}
5360
5361/*
5362 * "win_gotoid()" function
5363 */
5364 static void
5365f_win_gotoid(typval_T *argvars, typval_T *rettv)
5366{
5367 rettv->vval.v_number = win_gotoid(argvars);
5368}
5369
5370/*
5371 * "win_id2tabwin()" function
5372 */
5373 static void
5374f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5375{
5376 if (rettv_list_alloc(rettv) != FAIL)
5377 win_id2tabwin(argvars, rettv->vval.v_list);
5378}
5379
5380/*
5381 * "win_id2win()" function
5382 */
5383 static void
5384f_win_id2win(typval_T *argvars, typval_T *rettv)
5385{
5386 rettv->vval.v_number = win_id2win(argvars);
5387}
5388
5389/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005390 * "win_screenpos()" function
5391 */
5392 static void
5393f_win_screenpos(typval_T *argvars, typval_T *rettv)
5394{
5395 win_T *wp;
5396
5397 if (rettv_list_alloc(rettv) == FAIL)
5398 return;
5399
5400 wp = find_win_by_nr(&argvars[0], NULL);
5401 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5402 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5403}
5404
5405/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005406 * "getwinposx()" function
5407 */
5408 static void
5409f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5410{
5411 rettv->vval.v_number = -1;
5412#ifdef FEAT_GUI
5413 if (gui.in_use)
5414 {
5415 int x, y;
5416
5417 if (gui_mch_get_winpos(&x, &y) == OK)
5418 rettv->vval.v_number = x;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005419 return;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005420 }
5421#endif
5422#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5423 {
5424 int x, y;
5425
5426 if (term_get_winpos(&x, &y) == OK)
5427 rettv->vval.v_number = x;
5428 }
5429#endif
5430}
5431
5432/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005433 * "getwinposy()" function
5434 */
5435 static void
5436f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5437{
5438 rettv->vval.v_number = -1;
5439#ifdef FEAT_GUI
5440 if (gui.in_use)
5441 {
5442 int x, y;
5443
5444 if (gui_mch_get_winpos(&x, &y) == OK)
5445 rettv->vval.v_number = y;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005446 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005447 }
5448#endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005449#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5450 {
5451 int x, y;
5452
5453 if (term_get_winpos(&x, &y) == OK)
5454 rettv->vval.v_number = y;
5455 }
5456#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005457}
5458
5459/*
5460 * "getwinvar()" function
5461 */
5462 static void
5463f_getwinvar(typval_T *argvars, typval_T *rettv)
5464{
5465 getwinvar(argvars, rettv, 0);
5466}
5467
5468/*
5469 * "glob()" function
5470 */
5471 static void
5472f_glob(typval_T *argvars, typval_T *rettv)
5473{
5474 int options = WILD_SILENT|WILD_USE_NL;
5475 expand_T xpc;
5476 int error = FALSE;
5477
5478 /* When the optional second argument is non-zero, don't remove matches
5479 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5480 rettv->v_type = VAR_STRING;
5481 if (argvars[1].v_type != VAR_UNKNOWN)
5482 {
5483 if (get_tv_number_chk(&argvars[1], &error))
5484 options |= WILD_KEEP_ALL;
5485 if (argvars[2].v_type != VAR_UNKNOWN)
5486 {
5487 if (get_tv_number_chk(&argvars[2], &error))
5488 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005489 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005490 }
5491 if (argvars[3].v_type != VAR_UNKNOWN
5492 && get_tv_number_chk(&argvars[3], &error))
5493 options |= WILD_ALLLINKS;
5494 }
5495 }
5496 if (!error)
5497 {
5498 ExpandInit(&xpc);
5499 xpc.xp_context = EXPAND_FILES;
5500 if (p_wic)
5501 options += WILD_ICASE;
5502 if (rettv->v_type == VAR_STRING)
5503 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
5504 NULL, options, WILD_ALL);
5505 else if (rettv_list_alloc(rettv) != FAIL)
5506 {
5507 int i;
5508
5509 ExpandOne(&xpc, get_tv_string(&argvars[0]),
5510 NULL, options, WILD_ALL_KEEP);
5511 for (i = 0; i < xpc.xp_numfiles; i++)
5512 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5513
5514 ExpandCleanup(&xpc);
5515 }
5516 }
5517 else
5518 rettv->vval.v_string = NULL;
5519}
5520
5521/*
5522 * "globpath()" function
5523 */
5524 static void
5525f_globpath(typval_T *argvars, typval_T *rettv)
5526{
5527 int flags = 0;
5528 char_u buf1[NUMBUFLEN];
5529 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
5530 int error = FALSE;
5531 garray_T ga;
5532 int i;
5533
5534 /* When the optional second argument is non-zero, don't remove matches
5535 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5536 rettv->v_type = VAR_STRING;
5537 if (argvars[2].v_type != VAR_UNKNOWN)
5538 {
5539 if (get_tv_number_chk(&argvars[2], &error))
5540 flags |= WILD_KEEP_ALL;
5541 if (argvars[3].v_type != VAR_UNKNOWN)
5542 {
5543 if (get_tv_number_chk(&argvars[3], &error))
5544 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005545 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005546 }
5547 if (argvars[4].v_type != VAR_UNKNOWN
5548 && get_tv_number_chk(&argvars[4], &error))
5549 flags |= WILD_ALLLINKS;
5550 }
5551 }
5552 if (file != NULL && !error)
5553 {
5554 ga_init2(&ga, (int)sizeof(char_u *), 10);
5555 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
5556 if (rettv->v_type == VAR_STRING)
5557 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
5558 else if (rettv_list_alloc(rettv) != FAIL)
5559 for (i = 0; i < ga.ga_len; ++i)
5560 list_append_string(rettv->vval.v_list,
5561 ((char_u **)(ga.ga_data))[i], -1);
5562 ga_clear_strings(&ga);
5563 }
5564 else
5565 rettv->vval.v_string = NULL;
5566}
5567
5568/*
5569 * "glob2regpat()" function
5570 */
5571 static void
5572f_glob2regpat(typval_T *argvars, typval_T *rettv)
5573{
5574 char_u *pat = get_tv_string_chk(&argvars[0]);
5575
5576 rettv->v_type = VAR_STRING;
5577 rettv->vval.v_string = (pat == NULL)
5578 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
5579}
5580
5581/* for VIM_VERSION_ defines */
5582#include "version.h"
5583
5584/*
5585 * "has()" function
5586 */
5587 static void
5588f_has(typval_T *argvars, typval_T *rettv)
5589{
5590 int i;
5591 char_u *name;
5592 int n = FALSE;
5593 static char *(has_list[]) =
5594 {
5595#ifdef AMIGA
5596 "amiga",
5597# ifdef FEAT_ARP
5598 "arp",
5599# endif
5600#endif
5601#ifdef __BEOS__
5602 "beos",
5603#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02005604#ifdef MACOS_X
5605 "mac", /* Mac OS X (and, once, Mac OS Classic) */
5606 "osx", /* Mac OS X */
5607# ifdef MACOS_X_DARWIN
5608 "macunix", /* Mac OS X, with the darwin feature */
5609 "osxdarwin", /* synonym for macunix */
5610# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005611#endif
5612#ifdef __QNX__
5613 "qnx",
5614#endif
5615#ifdef UNIX
5616 "unix",
5617#endif
5618#ifdef VMS
5619 "vms",
5620#endif
5621#ifdef WIN32
5622 "win32",
5623#endif
5624#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
5625 "win32unix",
5626#endif
5627#if defined(WIN64) || defined(_WIN64)
5628 "win64",
5629#endif
5630#ifdef EBCDIC
5631 "ebcdic",
5632#endif
5633#ifndef CASE_INSENSITIVE_FILENAME
5634 "fname_case",
5635#endif
5636#ifdef HAVE_ACL
5637 "acl",
5638#endif
5639#ifdef FEAT_ARABIC
5640 "arabic",
5641#endif
5642#ifdef FEAT_AUTOCMD
5643 "autocmd",
5644#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01005645#ifdef FEAT_AUTOSERVERNAME
5646 "autoservername",
5647#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01005648#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005649 "balloon_eval",
5650# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
5651 "balloon_multiline",
5652# endif
5653#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01005654#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01005655 "balloon_eval_term",
5656#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005657#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
5658 "builtin_terms",
5659# ifdef ALL_BUILTIN_TCAPS
5660 "all_builtin_terms",
5661# endif
5662#endif
5663#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
5664 || defined(FEAT_GUI_W32) \
5665 || defined(FEAT_GUI_MOTIF))
5666 "browsefilter",
5667#endif
5668#ifdef FEAT_BYTEOFF
5669 "byte_offset",
5670#endif
5671#ifdef FEAT_JOB_CHANNEL
5672 "channel",
5673#endif
5674#ifdef FEAT_CINDENT
5675 "cindent",
5676#endif
5677#ifdef FEAT_CLIENTSERVER
5678 "clientserver",
5679#endif
5680#ifdef FEAT_CLIPBOARD
5681 "clipboard",
5682#endif
5683#ifdef FEAT_CMDL_COMPL
5684 "cmdline_compl",
5685#endif
5686#ifdef FEAT_CMDHIST
5687 "cmdline_hist",
5688#endif
5689#ifdef FEAT_COMMENTS
5690 "comments",
5691#endif
5692#ifdef FEAT_CONCEAL
5693 "conceal",
5694#endif
5695#ifdef FEAT_CRYPT
5696 "cryptv",
5697 "crypt-blowfish",
5698 "crypt-blowfish2",
5699#endif
5700#ifdef FEAT_CSCOPE
5701 "cscope",
5702#endif
5703#ifdef FEAT_CURSORBIND
5704 "cursorbind",
5705#endif
5706#ifdef CURSOR_SHAPE
5707 "cursorshape",
5708#endif
5709#ifdef DEBUG
5710 "debug",
5711#endif
5712#ifdef FEAT_CON_DIALOG
5713 "dialog_con",
5714#endif
5715#ifdef FEAT_GUI_DIALOG
5716 "dialog_gui",
5717#endif
5718#ifdef FEAT_DIFF
5719 "diff",
5720#endif
5721#ifdef FEAT_DIGRAPHS
5722 "digraphs",
5723#endif
5724#ifdef FEAT_DIRECTX
5725 "directx",
5726#endif
5727#ifdef FEAT_DND
5728 "dnd",
5729#endif
5730#ifdef FEAT_EMACS_TAGS
5731 "emacs_tags",
5732#endif
5733 "eval", /* always present, of course! */
5734 "ex_extra", /* graduated feature */
5735#ifdef FEAT_SEARCH_EXTRA
5736 "extra_search",
5737#endif
5738#ifdef FEAT_FKMAP
5739 "farsi",
5740#endif
5741#ifdef FEAT_SEARCHPATH
5742 "file_in_path",
5743#endif
5744#ifdef FEAT_FILTERPIPE
5745 "filterpipe",
5746#endif
5747#ifdef FEAT_FIND_ID
5748 "find_in_path",
5749#endif
5750#ifdef FEAT_FLOAT
5751 "float",
5752#endif
5753#ifdef FEAT_FOLDING
5754 "folding",
5755#endif
5756#ifdef FEAT_FOOTER
5757 "footer",
5758#endif
5759#if !defined(USE_SYSTEM) && defined(UNIX)
5760 "fork",
5761#endif
5762#ifdef FEAT_GETTEXT
5763 "gettext",
5764#endif
5765#ifdef FEAT_GUI
5766 "gui",
5767#endif
5768#ifdef FEAT_GUI_ATHENA
5769# ifdef FEAT_GUI_NEXTAW
5770 "gui_neXtaw",
5771# else
5772 "gui_athena",
5773# endif
5774#endif
5775#ifdef FEAT_GUI_GTK
5776 "gui_gtk",
5777# ifdef USE_GTK3
5778 "gui_gtk3",
5779# else
5780 "gui_gtk2",
5781# endif
5782#endif
5783#ifdef FEAT_GUI_GNOME
5784 "gui_gnome",
5785#endif
5786#ifdef FEAT_GUI_MAC
5787 "gui_mac",
5788#endif
5789#ifdef FEAT_GUI_MOTIF
5790 "gui_motif",
5791#endif
5792#ifdef FEAT_GUI_PHOTON
5793 "gui_photon",
5794#endif
5795#ifdef FEAT_GUI_W32
5796 "gui_win32",
5797#endif
5798#ifdef FEAT_HANGULIN
5799 "hangul_input",
5800#endif
5801#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
5802 "iconv",
5803#endif
5804#ifdef FEAT_INS_EXPAND
5805 "insert_expand",
5806#endif
5807#ifdef FEAT_JOB_CHANNEL
5808 "job",
5809#endif
5810#ifdef FEAT_JUMPLIST
5811 "jumplist",
5812#endif
5813#ifdef FEAT_KEYMAP
5814 "keymap",
5815#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02005816 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005817#ifdef FEAT_LANGMAP
5818 "langmap",
5819#endif
5820#ifdef FEAT_LIBCALL
5821 "libcall",
5822#endif
5823#ifdef FEAT_LINEBREAK
5824 "linebreak",
5825#endif
5826#ifdef FEAT_LISP
5827 "lispindent",
5828#endif
5829#ifdef FEAT_LISTCMDS
5830 "listcmds",
5831#endif
5832#ifdef FEAT_LOCALMAP
5833 "localmap",
5834#endif
5835#ifdef FEAT_LUA
5836# ifndef DYNAMIC_LUA
5837 "lua",
5838# endif
5839#endif
5840#ifdef FEAT_MENU
5841 "menu",
5842#endif
5843#ifdef FEAT_SESSION
5844 "mksession",
5845#endif
5846#ifdef FEAT_MODIFY_FNAME
5847 "modify_fname",
5848#endif
5849#ifdef FEAT_MOUSE
5850 "mouse",
5851#endif
5852#ifdef FEAT_MOUSESHAPE
5853 "mouseshape",
5854#endif
5855#if defined(UNIX) || defined(VMS)
5856# ifdef FEAT_MOUSE_DEC
5857 "mouse_dec",
5858# endif
5859# ifdef FEAT_MOUSE_GPM
5860 "mouse_gpm",
5861# endif
5862# ifdef FEAT_MOUSE_JSB
5863 "mouse_jsbterm",
5864# endif
5865# ifdef FEAT_MOUSE_NET
5866 "mouse_netterm",
5867# endif
5868# ifdef FEAT_MOUSE_PTERM
5869 "mouse_pterm",
5870# endif
5871# ifdef FEAT_MOUSE_SGR
5872 "mouse_sgr",
5873# endif
5874# ifdef FEAT_SYSMOUSE
5875 "mouse_sysmouse",
5876# endif
5877# ifdef FEAT_MOUSE_URXVT
5878 "mouse_urxvt",
5879# endif
5880# ifdef FEAT_MOUSE_XTERM
5881 "mouse_xterm",
5882# endif
5883#endif
5884#ifdef FEAT_MBYTE
5885 "multi_byte",
5886#endif
5887#ifdef FEAT_MBYTE_IME
5888 "multi_byte_ime",
5889#endif
5890#ifdef FEAT_MULTI_LANG
5891 "multi_lang",
5892#endif
5893#ifdef FEAT_MZSCHEME
5894#ifndef DYNAMIC_MZSCHEME
5895 "mzscheme",
5896#endif
5897#endif
5898#ifdef FEAT_NUM64
5899 "num64",
5900#endif
5901#ifdef FEAT_OLE
5902 "ole",
5903#endif
5904 "packages",
5905#ifdef FEAT_PATH_EXTRA
5906 "path_extra",
5907#endif
5908#ifdef FEAT_PERL
5909#ifndef DYNAMIC_PERL
5910 "perl",
5911#endif
5912#endif
5913#ifdef FEAT_PERSISTENT_UNDO
5914 "persistent_undo",
5915#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005916#if defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005917 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005918 "pythonx",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005919#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005920#if defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005921 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005922 "pythonx",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005923#endif
5924#ifdef FEAT_POSTSCRIPT
5925 "postscript",
5926#endif
5927#ifdef FEAT_PRINTER
5928 "printer",
5929#endif
5930#ifdef FEAT_PROFILE
5931 "profile",
5932#endif
5933#ifdef FEAT_RELTIME
5934 "reltime",
5935#endif
5936#ifdef FEAT_QUICKFIX
5937 "quickfix",
5938#endif
5939#ifdef FEAT_RIGHTLEFT
5940 "rightleft",
5941#endif
5942#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
5943 "ruby",
5944#endif
5945#ifdef FEAT_SCROLLBIND
5946 "scrollbind",
5947#endif
5948#ifdef FEAT_CMDL_INFO
5949 "showcmd",
5950 "cmdline_info",
5951#endif
5952#ifdef FEAT_SIGNS
5953 "signs",
5954#endif
5955#ifdef FEAT_SMARTINDENT
5956 "smartindent",
5957#endif
5958#ifdef STARTUPTIME
5959 "startuptime",
5960#endif
5961#ifdef FEAT_STL_OPT
5962 "statusline",
5963#endif
5964#ifdef FEAT_SUN_WORKSHOP
5965 "sun_workshop",
5966#endif
5967#ifdef FEAT_NETBEANS_INTG
5968 "netbeans_intg",
5969#endif
5970#ifdef FEAT_SPELL
5971 "spell",
5972#endif
5973#ifdef FEAT_SYN_HL
5974 "syntax",
5975#endif
5976#if defined(USE_SYSTEM) || !defined(UNIX)
5977 "system",
5978#endif
5979#ifdef FEAT_TAG_BINS
5980 "tag_binary",
5981#endif
5982#ifdef FEAT_TAG_OLDSTATIC
5983 "tag_old_static",
5984#endif
5985#ifdef FEAT_TAG_ANYWHITE
5986 "tag_any_white",
5987#endif
5988#ifdef FEAT_TCL
5989# ifndef DYNAMIC_TCL
5990 "tcl",
5991# endif
5992#endif
5993#ifdef FEAT_TERMGUICOLORS
5994 "termguicolors",
5995#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02005996#if defined(FEAT_TERMINAL) && !defined(WIN3264)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02005997 "terminal",
5998#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005999#ifdef TERMINFO
6000 "terminfo",
6001#endif
6002#ifdef FEAT_TERMRESPONSE
6003 "termresponse",
6004#endif
6005#ifdef FEAT_TEXTOBJ
6006 "textobjects",
6007#endif
6008#ifdef HAVE_TGETENT
6009 "tgetent",
6010#endif
6011#ifdef FEAT_TIMERS
6012 "timers",
6013#endif
6014#ifdef FEAT_TITLE
6015 "title",
6016#endif
6017#ifdef FEAT_TOOLBAR
6018 "toolbar",
6019#endif
6020#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6021 "unnamedplus",
6022#endif
6023#ifdef FEAT_USR_CMDS
6024 "user-commands", /* was accidentally included in 5.4 */
6025 "user_commands",
6026#endif
6027#ifdef FEAT_VIMINFO
6028 "viminfo",
6029#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006030 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006031#ifdef FEAT_VIRTUALEDIT
6032 "virtualedit",
6033#endif
6034 "visual",
6035#ifdef FEAT_VISUALEXTRA
6036 "visualextra",
6037#endif
6038#ifdef FEAT_VREPLACE
6039 "vreplace",
6040#endif
6041#ifdef FEAT_WILDIGN
6042 "wildignore",
6043#endif
6044#ifdef FEAT_WILDMENU
6045 "wildmenu",
6046#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006047 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006048#ifdef FEAT_WAK
6049 "winaltkeys",
6050#endif
6051#ifdef FEAT_WRITEBACKUP
6052 "writebackup",
6053#endif
6054#ifdef FEAT_XIM
6055 "xim",
6056#endif
6057#ifdef FEAT_XFONTSET
6058 "xfontset",
6059#endif
6060#ifdef FEAT_XPM_W32
6061 "xpm",
6062 "xpm_w32", /* for backward compatibility */
6063#else
6064# if defined(HAVE_XPM)
6065 "xpm",
6066# endif
6067#endif
6068#ifdef USE_XSMP
6069 "xsmp",
6070#endif
6071#ifdef USE_XSMP_INTERACT
6072 "xsmp_interact",
6073#endif
6074#ifdef FEAT_XCLIPBOARD
6075 "xterm_clipboard",
6076#endif
6077#ifdef FEAT_XTERM_SAVE
6078 "xterm_save",
6079#endif
6080#if defined(UNIX) && defined(FEAT_X11)
6081 "X11",
6082#endif
6083 NULL
6084 };
6085
6086 name = get_tv_string(&argvars[0]);
6087 for (i = 0; has_list[i] != NULL; ++i)
6088 if (STRICMP(name, has_list[i]) == 0)
6089 {
6090 n = TRUE;
6091 break;
6092 }
6093
6094 if (n == FALSE)
6095 {
6096 if (STRNICMP(name, "patch", 5) == 0)
6097 {
6098 if (name[5] == '-'
6099 && STRLEN(name) >= 11
6100 && vim_isdigit(name[6])
6101 && vim_isdigit(name[8])
6102 && vim_isdigit(name[10]))
6103 {
6104 int major = atoi((char *)name + 6);
6105 int minor = atoi((char *)name + 8);
6106
6107 /* Expect "patch-9.9.01234". */
6108 n = (major < VIM_VERSION_MAJOR
6109 || (major == VIM_VERSION_MAJOR
6110 && (minor < VIM_VERSION_MINOR
6111 || (minor == VIM_VERSION_MINOR
6112 && has_patch(atoi((char *)name + 10))))));
6113 }
6114 else
6115 n = has_patch(atoi((char *)name + 5));
6116 }
6117 else if (STRICMP(name, "vim_starting") == 0)
6118 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006119 else if (STRICMP(name, "ttyin") == 0)
6120 n = mch_input_isatty();
6121 else if (STRICMP(name, "ttyout") == 0)
6122 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006123#ifdef FEAT_MBYTE
6124 else if (STRICMP(name, "multi_byte_encoding") == 0)
6125 n = has_mbyte;
6126#endif
6127#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
6128 else if (STRICMP(name, "balloon_multiline") == 0)
6129 n = multiline_balloon_available();
6130#endif
6131#ifdef DYNAMIC_TCL
6132 else if (STRICMP(name, "tcl") == 0)
6133 n = tcl_enabled(FALSE);
6134#endif
6135#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6136 else if (STRICMP(name, "iconv") == 0)
6137 n = iconv_enabled(FALSE);
6138#endif
6139#ifdef DYNAMIC_LUA
6140 else if (STRICMP(name, "lua") == 0)
6141 n = lua_enabled(FALSE);
6142#endif
6143#ifdef DYNAMIC_MZSCHEME
6144 else if (STRICMP(name, "mzscheme") == 0)
6145 n = mzscheme_enabled(FALSE);
6146#endif
6147#ifdef DYNAMIC_RUBY
6148 else if (STRICMP(name, "ruby") == 0)
6149 n = ruby_enabled(FALSE);
6150#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006151#ifdef DYNAMIC_PYTHON
6152 else if (STRICMP(name, "python") == 0)
6153 n = python_enabled(FALSE);
6154#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006155#ifdef DYNAMIC_PYTHON3
6156 else if (STRICMP(name, "python3") == 0)
6157 n = python3_enabled(FALSE);
6158#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006159#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6160 else if (STRICMP(name, "pythonx") == 0)
6161 {
6162# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6163 if (p_pyx == 0)
6164 n = python3_enabled(FALSE) || python_enabled(FALSE);
6165 else if (p_pyx == 3)
6166 n = python3_enabled(FALSE);
6167 else if (p_pyx == 2)
6168 n = python_enabled(FALSE);
6169# elif defined(DYNAMIC_PYTHON)
6170 n = python_enabled(FALSE);
6171# elif defined(DYNAMIC_PYTHON3)
6172 n = python3_enabled(FALSE);
6173# endif
6174 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006175#endif
6176#ifdef DYNAMIC_PERL
6177 else if (STRICMP(name, "perl") == 0)
6178 n = perl_enabled(FALSE);
6179#endif
6180#ifdef FEAT_GUI
6181 else if (STRICMP(name, "gui_running") == 0)
6182 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006183# ifdef FEAT_BROWSE
6184 else if (STRICMP(name, "browse") == 0)
6185 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6186# endif
6187#endif
6188#ifdef FEAT_SYN_HL
6189 else if (STRICMP(name, "syntax_items") == 0)
6190 n = syntax_present(curwin);
6191#endif
6192#if defined(WIN3264)
6193 else if (STRICMP(name, "win95") == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006194 n = FALSE; /* Win9x is no more supported. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006195#endif
6196#ifdef FEAT_NETBEANS_INTG
6197 else if (STRICMP(name, "netbeans_enabled") == 0)
6198 n = netbeans_active();
6199#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006200#if defined(FEAT_TERMINAL) && defined(WIN3264)
6201 else if (STRICMP(name, "terminal") == 0)
6202 n = terminal_enabled();
6203#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006204 }
6205
6206 rettv->vval.v_number = n;
6207}
6208
6209/*
6210 * "has_key()" function
6211 */
6212 static void
6213f_has_key(typval_T *argvars, typval_T *rettv)
6214{
6215 if (argvars[0].v_type != VAR_DICT)
6216 {
6217 EMSG(_(e_dictreq));
6218 return;
6219 }
6220 if (argvars[0].vval.v_dict == NULL)
6221 return;
6222
6223 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
6224 get_tv_string(&argvars[1]), -1) != NULL;
6225}
6226
6227/*
6228 * "haslocaldir()" function
6229 */
6230 static void
6231f_haslocaldir(typval_T *argvars, typval_T *rettv)
6232{
6233 win_T *wp = NULL;
6234
6235 wp = find_tabwin(&argvars[0], &argvars[1]);
6236 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6237}
6238
6239/*
6240 * "hasmapto()" function
6241 */
6242 static void
6243f_hasmapto(typval_T *argvars, typval_T *rettv)
6244{
6245 char_u *name;
6246 char_u *mode;
6247 char_u buf[NUMBUFLEN];
6248 int abbr = FALSE;
6249
6250 name = get_tv_string(&argvars[0]);
6251 if (argvars[1].v_type == VAR_UNKNOWN)
6252 mode = (char_u *)"nvo";
6253 else
6254 {
6255 mode = get_tv_string_buf(&argvars[1], buf);
6256 if (argvars[2].v_type != VAR_UNKNOWN)
6257 abbr = (int)get_tv_number(&argvars[2]);
6258 }
6259
6260 if (map_to_exists(name, mode, abbr))
6261 rettv->vval.v_number = TRUE;
6262 else
6263 rettv->vval.v_number = FALSE;
6264}
6265
6266/*
6267 * "histadd()" function
6268 */
6269 static void
6270f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6271{
6272#ifdef FEAT_CMDHIST
6273 int histype;
6274 char_u *str;
6275 char_u buf[NUMBUFLEN];
6276#endif
6277
6278 rettv->vval.v_number = FALSE;
6279 if (check_restricted() || check_secure())
6280 return;
6281#ifdef FEAT_CMDHIST
6282 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6283 histype = str != NULL ? get_histtype(str) : -1;
6284 if (histype >= 0)
6285 {
6286 str = get_tv_string_buf(&argvars[1], buf);
6287 if (*str != NUL)
6288 {
6289 init_history();
6290 add_to_history(histype, str, FALSE, NUL);
6291 rettv->vval.v_number = TRUE;
6292 return;
6293 }
6294 }
6295#endif
6296}
6297
6298/*
6299 * "histdel()" function
6300 */
6301 static void
6302f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6303{
6304#ifdef FEAT_CMDHIST
6305 int n;
6306 char_u buf[NUMBUFLEN];
6307 char_u *str;
6308
6309 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6310 if (str == NULL)
6311 n = 0;
6312 else if (argvars[1].v_type == VAR_UNKNOWN)
6313 /* only one argument: clear entire history */
6314 n = clr_history(get_histtype(str));
6315 else if (argvars[1].v_type == VAR_NUMBER)
6316 /* index given: remove that entry */
6317 n = del_history_idx(get_histtype(str),
6318 (int)get_tv_number(&argvars[1]));
6319 else
6320 /* string given: remove all matching entries */
6321 n = del_history_entry(get_histtype(str),
6322 get_tv_string_buf(&argvars[1], buf));
6323 rettv->vval.v_number = n;
6324#endif
6325}
6326
6327/*
6328 * "histget()" function
6329 */
6330 static void
6331f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6332{
6333#ifdef FEAT_CMDHIST
6334 int type;
6335 int idx;
6336 char_u *str;
6337
6338 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6339 if (str == NULL)
6340 rettv->vval.v_string = NULL;
6341 else
6342 {
6343 type = get_histtype(str);
6344 if (argvars[1].v_type == VAR_UNKNOWN)
6345 idx = get_history_idx(type);
6346 else
6347 idx = (int)get_tv_number_chk(&argvars[1], NULL);
6348 /* -1 on type error */
6349 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6350 }
6351#else
6352 rettv->vval.v_string = NULL;
6353#endif
6354 rettv->v_type = VAR_STRING;
6355}
6356
6357/*
6358 * "histnr()" function
6359 */
6360 static void
6361f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6362{
6363 int i;
6364
6365#ifdef FEAT_CMDHIST
6366 char_u *history = get_tv_string_chk(&argvars[0]);
6367
6368 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6369 if (i >= HIST_CMD && i < HIST_COUNT)
6370 i = get_history_idx(i);
6371 else
6372#endif
6373 i = -1;
6374 rettv->vval.v_number = i;
6375}
6376
6377/*
6378 * "highlightID(name)" function
6379 */
6380 static void
6381f_hlID(typval_T *argvars, typval_T *rettv)
6382{
6383 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
6384}
6385
6386/*
6387 * "highlight_exists()" function
6388 */
6389 static void
6390f_hlexists(typval_T *argvars, typval_T *rettv)
6391{
6392 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
6393}
6394
6395/*
6396 * "hostname()" function
6397 */
6398 static void
6399f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6400{
6401 char_u hostname[256];
6402
6403 mch_get_host_name(hostname, 256);
6404 rettv->v_type = VAR_STRING;
6405 rettv->vval.v_string = vim_strsave(hostname);
6406}
6407
6408/*
6409 * iconv() function
6410 */
6411 static void
6412f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6413{
6414#ifdef FEAT_MBYTE
6415 char_u buf1[NUMBUFLEN];
6416 char_u buf2[NUMBUFLEN];
6417 char_u *from, *to, *str;
6418 vimconv_T vimconv;
6419#endif
6420
6421 rettv->v_type = VAR_STRING;
6422 rettv->vval.v_string = NULL;
6423
6424#ifdef FEAT_MBYTE
6425 str = get_tv_string(&argvars[0]);
6426 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
6427 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
6428 vimconv.vc_type = CONV_NONE;
6429 convert_setup(&vimconv, from, to);
6430
6431 /* If the encodings are equal, no conversion needed. */
6432 if (vimconv.vc_type == CONV_NONE)
6433 rettv->vval.v_string = vim_strsave(str);
6434 else
6435 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6436
6437 convert_setup(&vimconv, NULL, NULL);
6438 vim_free(from);
6439 vim_free(to);
6440#endif
6441}
6442
6443/*
6444 * "indent()" function
6445 */
6446 static void
6447f_indent(typval_T *argvars, typval_T *rettv)
6448{
6449 linenr_T lnum;
6450
6451 lnum = get_tv_lnum(argvars);
6452 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6453 rettv->vval.v_number = get_indent_lnum(lnum);
6454 else
6455 rettv->vval.v_number = -1;
6456}
6457
6458/*
6459 * "index()" function
6460 */
6461 static void
6462f_index(typval_T *argvars, typval_T *rettv)
6463{
6464 list_T *l;
6465 listitem_T *item;
6466 long idx = 0;
6467 int ic = FALSE;
6468
6469 rettv->vval.v_number = -1;
6470 if (argvars[0].v_type != VAR_LIST)
6471 {
6472 EMSG(_(e_listreq));
6473 return;
6474 }
6475 l = argvars[0].vval.v_list;
6476 if (l != NULL)
6477 {
6478 item = l->lv_first;
6479 if (argvars[2].v_type != VAR_UNKNOWN)
6480 {
6481 int error = FALSE;
6482
6483 /* Start at specified item. Use the cached index that list_find()
6484 * sets, so that a negative number also works. */
6485 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
6486 idx = l->lv_idx;
6487 if (argvars[3].v_type != VAR_UNKNOWN)
6488 ic = (int)get_tv_number_chk(&argvars[3], &error);
6489 if (error)
6490 item = NULL;
6491 }
6492
6493 for ( ; item != NULL; item = item->li_next, ++idx)
6494 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6495 {
6496 rettv->vval.v_number = idx;
6497 break;
6498 }
6499 }
6500}
6501
6502static int inputsecret_flag = 0;
6503
6504/*
6505 * "input()" function
6506 * Also handles inputsecret() when inputsecret is set.
6507 */
6508 static void
6509f_input(typval_T *argvars, typval_T *rettv)
6510{
6511 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6512}
6513
6514/*
6515 * "inputdialog()" function
6516 */
6517 static void
6518f_inputdialog(typval_T *argvars, typval_T *rettv)
6519{
6520#if defined(FEAT_GUI_TEXTDIALOG)
6521 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
6522 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6523 {
6524 char_u *message;
6525 char_u buf[NUMBUFLEN];
6526 char_u *defstr = (char_u *)"";
6527
6528 message = get_tv_string_chk(&argvars[0]);
6529 if (argvars[1].v_type != VAR_UNKNOWN
6530 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
6531 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6532 else
6533 IObuff[0] = NUL;
6534 if (message != NULL && defstr != NULL
6535 && do_dialog(VIM_QUESTION, NULL, message,
6536 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6537 rettv->vval.v_string = vim_strsave(IObuff);
6538 else
6539 {
6540 if (message != NULL && defstr != NULL
6541 && argvars[1].v_type != VAR_UNKNOWN
6542 && argvars[2].v_type != VAR_UNKNOWN)
6543 rettv->vval.v_string = vim_strsave(
6544 get_tv_string_buf(&argvars[2], buf));
6545 else
6546 rettv->vval.v_string = NULL;
6547 }
6548 rettv->v_type = VAR_STRING;
6549 }
6550 else
6551#endif
6552 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6553}
6554
6555/*
6556 * "inputlist()" function
6557 */
6558 static void
6559f_inputlist(typval_T *argvars, typval_T *rettv)
6560{
6561 listitem_T *li;
6562 int selected;
6563 int mouse_used;
6564
6565#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02006566 /* While starting up, there is no place to enter text. When running tests
6567 * with --not-a-term we assume feedkeys() will be used. */
6568 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006569 return;
6570#endif
6571 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6572 {
6573 EMSG2(_(e_listarg), "inputlist()");
6574 return;
6575 }
6576
6577 msg_start();
6578 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
6579 lines_left = Rows; /* avoid more prompt */
6580 msg_scroll = TRUE;
6581 msg_clr_eos();
6582
6583 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
6584 {
6585 msg_puts(get_tv_string(&li->li_tv));
6586 msg_putchar('\n');
6587 }
6588
6589 /* Ask for choice. */
6590 selected = prompt_for_number(&mouse_used);
6591 if (mouse_used)
6592 selected -= lines_left;
6593
6594 rettv->vval.v_number = selected;
6595}
6596
6597
6598static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6599
6600/*
6601 * "inputrestore()" function
6602 */
6603 static void
6604f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6605{
6606 if (ga_userinput.ga_len > 0)
6607 {
6608 --ga_userinput.ga_len;
6609 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
6610 + ga_userinput.ga_len);
6611 /* default return is zero == OK */
6612 }
6613 else if (p_verbose > 1)
6614 {
6615 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
6616 rettv->vval.v_number = 1; /* Failed */
6617 }
6618}
6619
6620/*
6621 * "inputsave()" function
6622 */
6623 static void
6624f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
6625{
6626 /* Add an entry to the stack of typeahead storage. */
6627 if (ga_grow(&ga_userinput, 1) == OK)
6628 {
6629 save_typeahead((tasave_T *)(ga_userinput.ga_data)
6630 + ga_userinput.ga_len);
6631 ++ga_userinput.ga_len;
6632 /* default return is zero == OK */
6633 }
6634 else
6635 rettv->vval.v_number = 1; /* Failed */
6636}
6637
6638/*
6639 * "inputsecret()" function
6640 */
6641 static void
6642f_inputsecret(typval_T *argvars, typval_T *rettv)
6643{
6644 ++cmdline_star;
6645 ++inputsecret_flag;
6646 f_input(argvars, rettv);
6647 --cmdline_star;
6648 --inputsecret_flag;
6649}
6650
6651/*
6652 * "insert()" function
6653 */
6654 static void
6655f_insert(typval_T *argvars, typval_T *rettv)
6656{
6657 long before = 0;
6658 listitem_T *item;
6659 list_T *l;
6660 int error = FALSE;
6661
6662 if (argvars[0].v_type != VAR_LIST)
6663 EMSG2(_(e_listarg), "insert()");
6664 else if ((l = argvars[0].vval.v_list) != NULL
6665 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
6666 {
6667 if (argvars[2].v_type != VAR_UNKNOWN)
6668 before = (long)get_tv_number_chk(&argvars[2], &error);
6669 if (error)
6670 return; /* type error; errmsg already given */
6671
6672 if (before == l->lv_len)
6673 item = NULL;
6674 else
6675 {
6676 item = list_find(l, before);
6677 if (item == NULL)
6678 {
6679 EMSGN(_(e_listidx), before);
6680 l = NULL;
6681 }
6682 }
6683 if (l != NULL)
6684 {
6685 list_insert_tv(l, &argvars[1], item);
6686 copy_tv(&argvars[0], rettv);
6687 }
6688 }
6689}
6690
6691/*
6692 * "invert(expr)" function
6693 */
6694 static void
6695f_invert(typval_T *argvars, typval_T *rettv)
6696{
6697 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
6698}
6699
6700/*
6701 * "isdirectory()" function
6702 */
6703 static void
6704f_isdirectory(typval_T *argvars, typval_T *rettv)
6705{
6706 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
6707}
6708
6709/*
6710 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
6711 * or it refers to a List or Dictionary that is locked.
6712 */
6713 static int
6714tv_islocked(typval_T *tv)
6715{
6716 return (tv->v_lock & VAR_LOCKED)
6717 || (tv->v_type == VAR_LIST
6718 && tv->vval.v_list != NULL
6719 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
6720 || (tv->v_type == VAR_DICT
6721 && tv->vval.v_dict != NULL
6722 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
6723}
6724
6725/*
6726 * "islocked()" function
6727 */
6728 static void
6729f_islocked(typval_T *argvars, typval_T *rettv)
6730{
6731 lval_T lv;
6732 char_u *end;
6733 dictitem_T *di;
6734
6735 rettv->vval.v_number = -1;
6736 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01006737 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006738 if (end != NULL && lv.ll_name != NULL)
6739 {
6740 if (*end != NUL)
6741 EMSG(_(e_trailing));
6742 else
6743 {
6744 if (lv.ll_tv == NULL)
6745 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006746 di = find_var(lv.ll_name, NULL, TRUE);
6747 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006748 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006749 /* Consider a variable locked when:
6750 * 1. the variable itself is locked
6751 * 2. the value of the variable is locked.
6752 * 3. the List or Dict value is locked.
6753 */
6754 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
6755 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006756 }
6757 }
6758 else if (lv.ll_range)
6759 EMSG(_("E786: Range not allowed"));
6760 else if (lv.ll_newkey != NULL)
6761 EMSG2(_(e_dictkey), lv.ll_newkey);
6762 else if (lv.ll_list != NULL)
6763 /* List item. */
6764 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
6765 else
6766 /* Dictionary item. */
6767 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
6768 }
6769 }
6770
6771 clear_lval(&lv);
6772}
6773
6774#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
6775/*
6776 * "isnan()" function
6777 */
6778 static void
6779f_isnan(typval_T *argvars, typval_T *rettv)
6780{
6781 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
6782 && isnan(argvars[0].vval.v_float);
6783}
6784#endif
6785
6786/*
6787 * "items(dict)" function
6788 */
6789 static void
6790f_items(typval_T *argvars, typval_T *rettv)
6791{
6792 dict_list(argvars, rettv, 2);
6793}
6794
6795#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
6796/*
6797 * Get the job from the argument.
6798 * Returns NULL if the job is invalid.
6799 */
6800 static job_T *
6801get_job_arg(typval_T *tv)
6802{
6803 job_T *job;
6804
6805 if (tv->v_type != VAR_JOB)
6806 {
6807 EMSG2(_(e_invarg2), get_tv_string(tv));
6808 return NULL;
6809 }
6810 job = tv->vval.v_job;
6811
6812 if (job == NULL)
6813 EMSG(_("E916: not a valid job"));
6814 return job;
6815}
6816
6817/*
6818 * "job_getchannel()" function
6819 */
6820 static void
6821f_job_getchannel(typval_T *argvars, typval_T *rettv)
6822{
6823 job_T *job = get_job_arg(&argvars[0]);
6824
6825 if (job != NULL)
6826 {
6827 rettv->v_type = VAR_CHANNEL;
6828 rettv->vval.v_channel = job->jv_channel;
6829 if (job->jv_channel != NULL)
6830 ++job->jv_channel->ch_refcount;
6831 }
6832}
6833
6834/*
6835 * "job_info()" function
6836 */
6837 static void
6838f_job_info(typval_T *argvars, typval_T *rettv)
6839{
6840 job_T *job = get_job_arg(&argvars[0]);
6841
6842 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
6843 job_info(job, rettv->vval.v_dict);
6844}
6845
6846/*
6847 * "job_setoptions()" function
6848 */
6849 static void
6850f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
6851{
6852 job_T *job = get_job_arg(&argvars[0]);
6853 jobopt_T opt;
6854
6855 if (job == NULL)
6856 return;
6857 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02006858 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006859 job_set_options(job, &opt);
6860 free_job_options(&opt);
6861}
6862
6863/*
6864 * "job_start()" function
6865 */
6866 static void
6867f_job_start(typval_T *argvars, typval_T *rettv)
6868{
6869 rettv->v_type = VAR_JOB;
6870 if (check_restricted() || check_secure())
6871 return;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006872 rettv->vval.v_job = job_start(argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006873}
6874
6875/*
6876 * "job_status()" function
6877 */
6878 static void
6879f_job_status(typval_T *argvars, typval_T *rettv)
6880{
6881 job_T *job = get_job_arg(&argvars[0]);
6882
6883 if (job != NULL)
6884 {
6885 rettv->v_type = VAR_STRING;
6886 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
6887 }
6888}
6889
6890/*
6891 * "job_stop()" function
6892 */
6893 static void
6894f_job_stop(typval_T *argvars, typval_T *rettv)
6895{
6896 job_T *job = get_job_arg(&argvars[0]);
6897
6898 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02006899 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006900}
6901#endif
6902
6903/*
6904 * "join()" function
6905 */
6906 static void
6907f_join(typval_T *argvars, typval_T *rettv)
6908{
6909 garray_T ga;
6910 char_u *sep;
6911
6912 if (argvars[0].v_type != VAR_LIST)
6913 {
6914 EMSG(_(e_listreq));
6915 return;
6916 }
6917 if (argvars[0].vval.v_list == NULL)
6918 return;
6919 if (argvars[1].v_type == VAR_UNKNOWN)
6920 sep = (char_u *)" ";
6921 else
6922 sep = get_tv_string_chk(&argvars[1]);
6923
6924 rettv->v_type = VAR_STRING;
6925
6926 if (sep != NULL)
6927 {
6928 ga_init2(&ga, (int)sizeof(char), 80);
6929 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
6930 ga_append(&ga, NUL);
6931 rettv->vval.v_string = (char_u *)ga.ga_data;
6932 }
6933 else
6934 rettv->vval.v_string = NULL;
6935}
6936
6937/*
6938 * "js_decode()" function
6939 */
6940 static void
6941f_js_decode(typval_T *argvars, typval_T *rettv)
6942{
6943 js_read_T reader;
6944
6945 reader.js_buf = get_tv_string(&argvars[0]);
6946 reader.js_fill = NULL;
6947 reader.js_used = 0;
6948 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
6949 EMSG(_(e_invarg));
6950}
6951
6952/*
6953 * "js_encode()" function
6954 */
6955 static void
6956f_js_encode(typval_T *argvars, typval_T *rettv)
6957{
6958 rettv->v_type = VAR_STRING;
6959 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
6960}
6961
6962/*
6963 * "json_decode()" function
6964 */
6965 static void
6966f_json_decode(typval_T *argvars, typval_T *rettv)
6967{
6968 js_read_T reader;
6969
6970 reader.js_buf = get_tv_string(&argvars[0]);
6971 reader.js_fill = NULL;
6972 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01006973 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006974}
6975
6976/*
6977 * "json_encode()" function
6978 */
6979 static void
6980f_json_encode(typval_T *argvars, typval_T *rettv)
6981{
6982 rettv->v_type = VAR_STRING;
6983 rettv->vval.v_string = json_encode(&argvars[0], 0);
6984}
6985
6986/*
6987 * "keys()" function
6988 */
6989 static void
6990f_keys(typval_T *argvars, typval_T *rettv)
6991{
6992 dict_list(argvars, rettv, 0);
6993}
6994
6995/*
6996 * "last_buffer_nr()" function.
6997 */
6998 static void
6999f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7000{
7001 int n = 0;
7002 buf_T *buf;
7003
Bram Moolenaar29323592016-07-24 22:04:11 +02007004 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007005 if (n < buf->b_fnum)
7006 n = buf->b_fnum;
7007
7008 rettv->vval.v_number = n;
7009}
7010
7011/*
7012 * "len()" function
7013 */
7014 static void
7015f_len(typval_T *argvars, typval_T *rettv)
7016{
7017 switch (argvars[0].v_type)
7018 {
7019 case VAR_STRING:
7020 case VAR_NUMBER:
7021 rettv->vval.v_number = (varnumber_T)STRLEN(
7022 get_tv_string(&argvars[0]));
7023 break;
7024 case VAR_LIST:
7025 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7026 break;
7027 case VAR_DICT:
7028 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7029 break;
7030 case VAR_UNKNOWN:
7031 case VAR_SPECIAL:
7032 case VAR_FLOAT:
7033 case VAR_FUNC:
7034 case VAR_PARTIAL:
7035 case VAR_JOB:
7036 case VAR_CHANNEL:
7037 EMSG(_("E701: Invalid type for len()"));
7038 break;
7039 }
7040}
7041
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007042 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007043libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007044{
7045#ifdef FEAT_LIBCALL
7046 char_u *string_in;
7047 char_u **string_result;
7048 int nr_result;
7049#endif
7050
7051 rettv->v_type = type;
7052 if (type != VAR_NUMBER)
7053 rettv->vval.v_string = NULL;
7054
7055 if (check_restricted() || check_secure())
7056 return;
7057
7058#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007059 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007060 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7061 {
7062 string_in = NULL;
7063 if (argvars[2].v_type == VAR_STRING)
7064 string_in = argvars[2].vval.v_string;
7065 if (type == VAR_NUMBER)
7066 string_result = NULL;
7067 else
7068 string_result = &rettv->vval.v_string;
7069 if (mch_libcall(argvars[0].vval.v_string,
7070 argvars[1].vval.v_string,
7071 string_in,
7072 argvars[2].vval.v_number,
7073 string_result,
7074 &nr_result) == OK
7075 && type == VAR_NUMBER)
7076 rettv->vval.v_number = nr_result;
7077 }
7078#endif
7079}
7080
7081/*
7082 * "libcall()" function
7083 */
7084 static void
7085f_libcall(typval_T *argvars, typval_T *rettv)
7086{
7087 libcall_common(argvars, rettv, VAR_STRING);
7088}
7089
7090/*
7091 * "libcallnr()" function
7092 */
7093 static void
7094f_libcallnr(typval_T *argvars, typval_T *rettv)
7095{
7096 libcall_common(argvars, rettv, VAR_NUMBER);
7097}
7098
7099/*
7100 * "line(string)" function
7101 */
7102 static void
7103f_line(typval_T *argvars, typval_T *rettv)
7104{
7105 linenr_T lnum = 0;
7106 pos_T *fp;
7107 int fnum;
7108
7109 fp = var2fpos(&argvars[0], TRUE, &fnum);
7110 if (fp != NULL)
7111 lnum = fp->lnum;
7112 rettv->vval.v_number = lnum;
7113}
7114
7115/*
7116 * "line2byte(lnum)" function
7117 */
7118 static void
7119f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7120{
7121#ifndef FEAT_BYTEOFF
7122 rettv->vval.v_number = -1;
7123#else
7124 linenr_T lnum;
7125
7126 lnum = get_tv_lnum(argvars);
7127 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7128 rettv->vval.v_number = -1;
7129 else
7130 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7131 if (rettv->vval.v_number >= 0)
7132 ++rettv->vval.v_number;
7133#endif
7134}
7135
7136/*
7137 * "lispindent(lnum)" function
7138 */
7139 static void
7140f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7141{
7142#ifdef FEAT_LISP
7143 pos_T pos;
7144 linenr_T lnum;
7145
7146 pos = curwin->w_cursor;
7147 lnum = get_tv_lnum(argvars);
7148 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7149 {
7150 curwin->w_cursor.lnum = lnum;
7151 rettv->vval.v_number = get_lisp_indent();
7152 curwin->w_cursor = pos;
7153 }
7154 else
7155#endif
7156 rettv->vval.v_number = -1;
7157}
7158
7159/*
7160 * "localtime()" function
7161 */
7162 static void
7163f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7164{
7165 rettv->vval.v_number = (varnumber_T)time(NULL);
7166}
7167
7168static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
7169
7170 static void
7171get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7172{
7173 char_u *keys;
7174 char_u *which;
7175 char_u buf[NUMBUFLEN];
7176 char_u *keys_buf = NULL;
7177 char_u *rhs;
7178 int mode;
7179 int abbr = FALSE;
7180 int get_dict = FALSE;
7181 mapblock_T *mp;
7182 int buffer_local;
7183
7184 /* return empty string for failure */
7185 rettv->v_type = VAR_STRING;
7186 rettv->vval.v_string = NULL;
7187
7188 keys = get_tv_string(&argvars[0]);
7189 if (*keys == NUL)
7190 return;
7191
7192 if (argvars[1].v_type != VAR_UNKNOWN)
7193 {
7194 which = get_tv_string_buf_chk(&argvars[1], buf);
7195 if (argvars[2].v_type != VAR_UNKNOWN)
7196 {
7197 abbr = (int)get_tv_number(&argvars[2]);
7198 if (argvars[3].v_type != VAR_UNKNOWN)
7199 get_dict = (int)get_tv_number(&argvars[3]);
7200 }
7201 }
7202 else
7203 which = (char_u *)"";
7204 if (which == NULL)
7205 return;
7206
7207 mode = get_map_mode(&which, 0);
7208
7209 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7210 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7211 vim_free(keys_buf);
7212
7213 if (!get_dict)
7214 {
7215 /* Return a string. */
7216 if (rhs != NULL)
7217 rettv->vval.v_string = str2special_save(rhs, FALSE);
7218
7219 }
7220 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7221 {
7222 /* Return a dictionary. */
7223 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7224 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7225 dict_T *dict = rettv->vval.v_dict;
7226
7227 dict_add_nr_str(dict, "lhs", 0L, lhs);
7228 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
7229 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
7230 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
7231 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
7232 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
7233 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
7234 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
7235 dict_add_nr_str(dict, "mode", 0L, mapmode);
7236
7237 vim_free(lhs);
7238 vim_free(mapmode);
7239 }
7240}
7241
7242#ifdef FEAT_FLOAT
7243/*
7244 * "log()" function
7245 */
7246 static void
7247f_log(typval_T *argvars, typval_T *rettv)
7248{
7249 float_T f = 0.0;
7250
7251 rettv->v_type = VAR_FLOAT;
7252 if (get_float_arg(argvars, &f) == OK)
7253 rettv->vval.v_float = log(f);
7254 else
7255 rettv->vval.v_float = 0.0;
7256}
7257
7258/*
7259 * "log10()" function
7260 */
7261 static void
7262f_log10(typval_T *argvars, typval_T *rettv)
7263{
7264 float_T f = 0.0;
7265
7266 rettv->v_type = VAR_FLOAT;
7267 if (get_float_arg(argvars, &f) == OK)
7268 rettv->vval.v_float = log10(f);
7269 else
7270 rettv->vval.v_float = 0.0;
7271}
7272#endif
7273
7274#ifdef FEAT_LUA
7275/*
7276 * "luaeval()" function
7277 */
7278 static void
7279f_luaeval(typval_T *argvars, typval_T *rettv)
7280{
7281 char_u *str;
7282 char_u buf[NUMBUFLEN];
7283
7284 str = get_tv_string_buf(&argvars[0], buf);
7285 do_luaeval(str, argvars + 1, rettv);
7286}
7287#endif
7288
7289/*
7290 * "map()" function
7291 */
7292 static void
7293f_map(typval_T *argvars, typval_T *rettv)
7294{
7295 filter_map(argvars, rettv, TRUE);
7296}
7297
7298/*
7299 * "maparg()" function
7300 */
7301 static void
7302f_maparg(typval_T *argvars, typval_T *rettv)
7303{
7304 get_maparg(argvars, rettv, TRUE);
7305}
7306
7307/*
7308 * "mapcheck()" function
7309 */
7310 static void
7311f_mapcheck(typval_T *argvars, typval_T *rettv)
7312{
7313 get_maparg(argvars, rettv, FALSE);
7314}
7315
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007316typedef enum
7317{
7318 MATCH_END, /* matchend() */
7319 MATCH_MATCH, /* match() */
7320 MATCH_STR, /* matchstr() */
7321 MATCH_LIST, /* matchlist() */
7322 MATCH_POS /* matchstrpos() */
7323} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007324
7325 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007326find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007327{
7328 char_u *str = NULL;
7329 long len = 0;
7330 char_u *expr = NULL;
7331 char_u *pat;
7332 regmatch_T regmatch;
7333 char_u patbuf[NUMBUFLEN];
7334 char_u strbuf[NUMBUFLEN];
7335 char_u *save_cpo;
7336 long start = 0;
7337 long nth = 1;
7338 colnr_T startcol = 0;
7339 int match = 0;
7340 list_T *l = NULL;
7341 listitem_T *li = NULL;
7342 long idx = 0;
7343 char_u *tofree = NULL;
7344
7345 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7346 save_cpo = p_cpo;
7347 p_cpo = (char_u *)"";
7348
7349 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007350 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007351 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007352 /* type MATCH_LIST: return empty list when there are no matches.
7353 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007354 if (rettv_list_alloc(rettv) == FAIL)
7355 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007356 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007357 && (list_append_string(rettv->vval.v_list,
7358 (char_u *)"", 0) == FAIL
7359 || list_append_number(rettv->vval.v_list,
7360 (varnumber_T)-1) == FAIL
7361 || list_append_number(rettv->vval.v_list,
7362 (varnumber_T)-1) == FAIL
7363 || list_append_number(rettv->vval.v_list,
7364 (varnumber_T)-1) == FAIL))
7365 {
7366 list_free(rettv->vval.v_list);
7367 rettv->vval.v_list = NULL;
7368 goto theend;
7369 }
7370 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007371 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007372 {
7373 rettv->v_type = VAR_STRING;
7374 rettv->vval.v_string = NULL;
7375 }
7376
7377 if (argvars[0].v_type == VAR_LIST)
7378 {
7379 if ((l = argvars[0].vval.v_list) == NULL)
7380 goto theend;
7381 li = l->lv_first;
7382 }
7383 else
7384 {
7385 expr = str = get_tv_string(&argvars[0]);
7386 len = (long)STRLEN(str);
7387 }
7388
7389 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
7390 if (pat == NULL)
7391 goto theend;
7392
7393 if (argvars[2].v_type != VAR_UNKNOWN)
7394 {
7395 int error = FALSE;
7396
7397 start = (long)get_tv_number_chk(&argvars[2], &error);
7398 if (error)
7399 goto theend;
7400 if (l != NULL)
7401 {
7402 li = list_find(l, start);
7403 if (li == NULL)
7404 goto theend;
7405 idx = l->lv_idx; /* use the cached index */
7406 }
7407 else
7408 {
7409 if (start < 0)
7410 start = 0;
7411 if (start > len)
7412 goto theend;
7413 /* When "count" argument is there ignore matches before "start",
7414 * otherwise skip part of the string. Differs when pattern is "^"
7415 * or "\<". */
7416 if (argvars[3].v_type != VAR_UNKNOWN)
7417 startcol = start;
7418 else
7419 {
7420 str += start;
7421 len -= start;
7422 }
7423 }
7424
7425 if (argvars[3].v_type != VAR_UNKNOWN)
7426 nth = (long)get_tv_number_chk(&argvars[3], &error);
7427 if (error)
7428 goto theend;
7429 }
7430
7431 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7432 if (regmatch.regprog != NULL)
7433 {
7434 regmatch.rm_ic = p_ic;
7435
7436 for (;;)
7437 {
7438 if (l != NULL)
7439 {
7440 if (li == NULL)
7441 {
7442 match = FALSE;
7443 break;
7444 }
7445 vim_free(tofree);
7446 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7447 if (str == NULL)
7448 break;
7449 }
7450
7451 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7452
7453 if (match && --nth <= 0)
7454 break;
7455 if (l == NULL && !match)
7456 break;
7457
7458 /* Advance to just after the match. */
7459 if (l != NULL)
7460 {
7461 li = li->li_next;
7462 ++idx;
7463 }
7464 else
7465 {
7466#ifdef FEAT_MBYTE
7467 startcol = (colnr_T)(regmatch.startp[0]
7468 + (*mb_ptr2len)(regmatch.startp[0]) - str);
7469#else
7470 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
7471#endif
7472 if (startcol > (colnr_T)len
7473 || str + startcol <= regmatch.startp[0])
7474 {
7475 match = FALSE;
7476 break;
7477 }
7478 }
7479 }
7480
7481 if (match)
7482 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007483 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007484 {
7485 listitem_T *li1 = rettv->vval.v_list->lv_first;
7486 listitem_T *li2 = li1->li_next;
7487 listitem_T *li3 = li2->li_next;
7488 listitem_T *li4 = li3->li_next;
7489
7490 vim_free(li1->li_tv.vval.v_string);
7491 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7492 (int)(regmatch.endp[0] - regmatch.startp[0]));
7493 li3->li_tv.vval.v_number =
7494 (varnumber_T)(regmatch.startp[0] - expr);
7495 li4->li_tv.vval.v_number =
7496 (varnumber_T)(regmatch.endp[0] - expr);
7497 if (l != NULL)
7498 li2->li_tv.vval.v_number = (varnumber_T)idx;
7499 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007500 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007501 {
7502 int i;
7503
7504 /* return list with matched string and submatches */
7505 for (i = 0; i < NSUBEXP; ++i)
7506 {
7507 if (regmatch.endp[i] == NULL)
7508 {
7509 if (list_append_string(rettv->vval.v_list,
7510 (char_u *)"", 0) == FAIL)
7511 break;
7512 }
7513 else if (list_append_string(rettv->vval.v_list,
7514 regmatch.startp[i],
7515 (int)(regmatch.endp[i] - regmatch.startp[i]))
7516 == FAIL)
7517 break;
7518 }
7519 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007520 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007521 {
7522 /* return matched string */
7523 if (l != NULL)
7524 copy_tv(&li->li_tv, rettv);
7525 else
7526 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
7527 (int)(regmatch.endp[0] - regmatch.startp[0]));
7528 }
7529 else if (l != NULL)
7530 rettv->vval.v_number = idx;
7531 else
7532 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007533 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007534 rettv->vval.v_number =
7535 (varnumber_T)(regmatch.startp[0] - str);
7536 else
7537 rettv->vval.v_number =
7538 (varnumber_T)(regmatch.endp[0] - str);
7539 rettv->vval.v_number += (varnumber_T)(str - expr);
7540 }
7541 }
7542 vim_regfree(regmatch.regprog);
7543 }
7544
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007545theend:
7546 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007547 /* matchstrpos() without a list: drop the second item. */
7548 listitem_remove(rettv->vval.v_list,
7549 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007550 vim_free(tofree);
7551 p_cpo = save_cpo;
7552}
7553
7554/*
7555 * "match()" function
7556 */
7557 static void
7558f_match(typval_T *argvars, typval_T *rettv)
7559{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007560 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007561}
7562
7563/*
7564 * "matchadd()" function
7565 */
7566 static void
7567f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7568{
7569#ifdef FEAT_SEARCH_EXTRA
7570 char_u buf[NUMBUFLEN];
7571 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
7572 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
7573 int prio = 10; /* default priority */
7574 int id = -1;
7575 int error = FALSE;
7576 char_u *conceal_char = NULL;
7577
7578 rettv->vval.v_number = -1;
7579
7580 if (grp == NULL || pat == NULL)
7581 return;
7582 if (argvars[2].v_type != VAR_UNKNOWN)
7583 {
7584 prio = (int)get_tv_number_chk(&argvars[2], &error);
7585 if (argvars[3].v_type != VAR_UNKNOWN)
7586 {
7587 id = (int)get_tv_number_chk(&argvars[3], &error);
7588 if (argvars[4].v_type != VAR_UNKNOWN)
7589 {
7590 if (argvars[4].v_type != VAR_DICT)
7591 {
7592 EMSG(_(e_dictreq));
7593 return;
7594 }
7595 if (dict_find(argvars[4].vval.v_dict,
7596 (char_u *)"conceal", -1) != NULL)
7597 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7598 (char_u *)"conceal", FALSE);
7599 }
7600 }
7601 }
7602 if (error == TRUE)
7603 return;
7604 if (id >= 1 && id <= 3)
7605 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007606 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007607 return;
7608 }
7609
7610 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
7611 conceal_char);
7612#endif
7613}
7614
7615/*
7616 * "matchaddpos()" function
7617 */
7618 static void
7619f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7620{
7621#ifdef FEAT_SEARCH_EXTRA
7622 char_u buf[NUMBUFLEN];
7623 char_u *group;
7624 int prio = 10;
7625 int id = -1;
7626 int error = FALSE;
7627 list_T *l;
7628 char_u *conceal_char = NULL;
7629
7630 rettv->vval.v_number = -1;
7631
7632 group = get_tv_string_buf_chk(&argvars[0], buf);
7633 if (group == NULL)
7634 return;
7635
7636 if (argvars[1].v_type != VAR_LIST)
7637 {
7638 EMSG2(_(e_listarg), "matchaddpos()");
7639 return;
7640 }
7641 l = argvars[1].vval.v_list;
7642 if (l == NULL)
7643 return;
7644
7645 if (argvars[2].v_type != VAR_UNKNOWN)
7646 {
7647 prio = (int)get_tv_number_chk(&argvars[2], &error);
7648 if (argvars[3].v_type != VAR_UNKNOWN)
7649 {
7650 id = (int)get_tv_number_chk(&argvars[3], &error);
7651 if (argvars[4].v_type != VAR_UNKNOWN)
7652 {
7653 if (argvars[4].v_type != VAR_DICT)
7654 {
7655 EMSG(_(e_dictreq));
7656 return;
7657 }
7658 if (dict_find(argvars[4].vval.v_dict,
7659 (char_u *)"conceal", -1) != NULL)
7660 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7661 (char_u *)"conceal", FALSE);
7662 }
7663 }
7664 }
7665 if (error == TRUE)
7666 return;
7667
7668 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
7669 if (id == 1 || id == 2)
7670 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007671 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007672 return;
7673 }
7674
7675 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
7676 conceal_char);
7677#endif
7678}
7679
7680/*
7681 * "matcharg()" function
7682 */
7683 static void
7684f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
7685{
7686 if (rettv_list_alloc(rettv) == OK)
7687 {
7688#ifdef FEAT_SEARCH_EXTRA
7689 int id = (int)get_tv_number(&argvars[0]);
7690 matchitem_T *m;
7691
7692 if (id >= 1 && id <= 3)
7693 {
7694 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
7695 {
7696 list_append_string(rettv->vval.v_list,
7697 syn_id2name(m->hlg_id), -1);
7698 list_append_string(rettv->vval.v_list, m->pattern, -1);
7699 }
7700 else
7701 {
7702 list_append_string(rettv->vval.v_list, NULL, -1);
7703 list_append_string(rettv->vval.v_list, NULL, -1);
7704 }
7705 }
7706#endif
7707 }
7708}
7709
7710/*
7711 * "matchdelete()" function
7712 */
7713 static void
7714f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7715{
7716#ifdef FEAT_SEARCH_EXTRA
7717 rettv->vval.v_number = match_delete(curwin,
7718 (int)get_tv_number(&argvars[0]), TRUE);
7719#endif
7720}
7721
7722/*
7723 * "matchend()" function
7724 */
7725 static void
7726f_matchend(typval_T *argvars, typval_T *rettv)
7727{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007728 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007729}
7730
7731/*
7732 * "matchlist()" function
7733 */
7734 static void
7735f_matchlist(typval_T *argvars, typval_T *rettv)
7736{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007737 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007738}
7739
7740/*
7741 * "matchstr()" function
7742 */
7743 static void
7744f_matchstr(typval_T *argvars, typval_T *rettv)
7745{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007746 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007747}
7748
7749/*
7750 * "matchstrpos()" function
7751 */
7752 static void
7753f_matchstrpos(typval_T *argvars, typval_T *rettv)
7754{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007755 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007756}
7757
7758static void max_min(typval_T *argvars, typval_T *rettv, int domax);
7759
7760 static void
7761max_min(typval_T *argvars, typval_T *rettv, int domax)
7762{
7763 varnumber_T n = 0;
7764 varnumber_T i;
7765 int error = FALSE;
7766
7767 if (argvars[0].v_type == VAR_LIST)
7768 {
7769 list_T *l;
7770 listitem_T *li;
7771
7772 l = argvars[0].vval.v_list;
7773 if (l != NULL)
7774 {
7775 li = l->lv_first;
7776 if (li != NULL)
7777 {
7778 n = get_tv_number_chk(&li->li_tv, &error);
7779 for (;;)
7780 {
7781 li = li->li_next;
7782 if (li == NULL)
7783 break;
7784 i = get_tv_number_chk(&li->li_tv, &error);
7785 if (domax ? i > n : i < n)
7786 n = i;
7787 }
7788 }
7789 }
7790 }
7791 else if (argvars[0].v_type == VAR_DICT)
7792 {
7793 dict_T *d;
7794 int first = TRUE;
7795 hashitem_T *hi;
7796 int todo;
7797
7798 d = argvars[0].vval.v_dict;
7799 if (d != NULL)
7800 {
7801 todo = (int)d->dv_hashtab.ht_used;
7802 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
7803 {
7804 if (!HASHITEM_EMPTY(hi))
7805 {
7806 --todo;
7807 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
7808 if (first)
7809 {
7810 n = i;
7811 first = FALSE;
7812 }
7813 else if (domax ? i > n : i < n)
7814 n = i;
7815 }
7816 }
7817 }
7818 }
7819 else
Bram Moolenaar26b84332016-09-04 21:42:36 +02007820 EMSG2(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007821 rettv->vval.v_number = error ? 0 : n;
7822}
7823
7824/*
7825 * "max()" function
7826 */
7827 static void
7828f_max(typval_T *argvars, typval_T *rettv)
7829{
7830 max_min(argvars, rettv, TRUE);
7831}
7832
7833/*
7834 * "min()" function
7835 */
7836 static void
7837f_min(typval_T *argvars, typval_T *rettv)
7838{
7839 max_min(argvars, rettv, FALSE);
7840}
7841
7842static int mkdir_recurse(char_u *dir, int prot);
7843
7844/*
7845 * Create the directory in which "dir" is located, and higher levels when
7846 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02007847 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007848 */
7849 static int
7850mkdir_recurse(char_u *dir, int prot)
7851{
7852 char_u *p;
7853 char_u *updir;
7854 int r = FAIL;
7855
7856 /* Get end of directory name in "dir".
7857 * We're done when it's "/" or "c:/". */
7858 p = gettail_sep(dir);
7859 if (p <= get_past_head(dir))
7860 return OK;
7861
7862 /* If the directory exists we're done. Otherwise: create it.*/
7863 updir = vim_strnsave(dir, (int)(p - dir));
7864 if (updir == NULL)
7865 return FAIL;
7866 if (mch_isdir(updir))
7867 r = OK;
7868 else if (mkdir_recurse(updir, prot) == OK)
7869 r = vim_mkdir_emsg(updir, prot);
7870 vim_free(updir);
7871 return r;
7872}
7873
7874#ifdef vim_mkdir
7875/*
7876 * "mkdir()" function
7877 */
7878 static void
7879f_mkdir(typval_T *argvars, typval_T *rettv)
7880{
7881 char_u *dir;
7882 char_u buf[NUMBUFLEN];
7883 int prot = 0755;
7884
7885 rettv->vval.v_number = FAIL;
7886 if (check_restricted() || check_secure())
7887 return;
7888
7889 dir = get_tv_string_buf(&argvars[0], buf);
7890 if (*dir == NUL)
7891 rettv->vval.v_number = FAIL;
7892 else
7893 {
7894 if (*gettail(dir) == NUL)
7895 /* remove trailing slashes */
7896 *gettail_sep(dir) = NUL;
7897
7898 if (argvars[1].v_type != VAR_UNKNOWN)
7899 {
7900 if (argvars[2].v_type != VAR_UNKNOWN)
7901 prot = (int)get_tv_number_chk(&argvars[2], NULL);
7902 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
7903 mkdir_recurse(dir, prot);
7904 }
7905 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
7906 }
7907}
7908#endif
7909
7910/*
7911 * "mode()" function
7912 */
7913 static void
7914f_mode(typval_T *argvars, typval_T *rettv)
7915{
7916 char_u buf[3];
7917
7918 buf[1] = NUL;
7919 buf[2] = NUL;
7920
7921 if (time_for_testing == 93784)
7922 {
7923 /* Testing the two-character code. */
7924 buf[0] = 'x';
7925 buf[1] = '!';
7926 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02007927#ifdef FEAT_TERMINAL
7928 else if (term_use_loop())
7929 buf[0] = 't';
7930#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007931 else if (VIsual_active)
7932 {
7933 if (VIsual_select)
7934 buf[0] = VIsual_mode + 's' - 'v';
7935 else
7936 buf[0] = VIsual_mode;
7937 }
7938 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
7939 || State == CONFIRM)
7940 {
7941 buf[0] = 'r';
7942 if (State == ASKMORE)
7943 buf[1] = 'm';
7944 else if (State == CONFIRM)
7945 buf[1] = '?';
7946 }
7947 else if (State == EXTERNCMD)
7948 buf[0] = '!';
7949 else if (State & INSERT)
7950 {
7951#ifdef FEAT_VREPLACE
7952 if (State & VREPLACE_FLAG)
7953 {
7954 buf[0] = 'R';
7955 buf[1] = 'v';
7956 }
7957 else
7958#endif
Bram Moolenaare90858d2017-02-01 17:24:34 +01007959 {
7960 if (State & REPLACE_FLAG)
7961 buf[0] = 'R';
7962 else
7963 buf[0] = 'i';
7964#ifdef FEAT_INS_EXPAND
7965 if (ins_compl_active())
7966 buf[1] = 'c';
7967 else if (ctrl_x_mode == 1)
7968 buf[1] = 'x';
7969#endif
7970 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007971 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01007972 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007973 {
7974 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01007975 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007976 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01007977 else if (exmode_active == EXMODE_NORMAL)
7978 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007979 }
7980 else
7981 {
7982 buf[0] = 'n';
7983 if (finish_op)
7984 buf[1] = 'o';
7985 }
7986
7987 /* Clear out the minor mode when the argument is not a non-zero number or
7988 * non-empty string. */
7989 if (!non_zero_arg(&argvars[0]))
7990 buf[1] = NUL;
7991
7992 rettv->vval.v_string = vim_strsave(buf);
7993 rettv->v_type = VAR_STRING;
7994}
7995
7996#if defined(FEAT_MZSCHEME) || defined(PROTO)
7997/*
7998 * "mzeval()" function
7999 */
8000 static void
8001f_mzeval(typval_T *argvars, typval_T *rettv)
8002{
8003 char_u *str;
8004 char_u buf[NUMBUFLEN];
8005
8006 str = get_tv_string_buf(&argvars[0], buf);
8007 do_mzeval(str, rettv);
8008}
8009
8010 void
8011mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8012{
8013 typval_T argvars[3];
8014
8015 argvars[0].v_type = VAR_STRING;
8016 argvars[0].vval.v_string = name;
8017 copy_tv(args, &argvars[1]);
8018 argvars[2].v_type = VAR_UNKNOWN;
8019 f_call(argvars, rettv);
8020 clear_tv(&argvars[1]);
8021}
8022#endif
8023
8024/*
8025 * "nextnonblank()" function
8026 */
8027 static void
8028f_nextnonblank(typval_T *argvars, typval_T *rettv)
8029{
8030 linenr_T lnum;
8031
8032 for (lnum = get_tv_lnum(argvars); ; ++lnum)
8033 {
8034 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8035 {
8036 lnum = 0;
8037 break;
8038 }
8039 if (*skipwhite(ml_get(lnum)) != NUL)
8040 break;
8041 }
8042 rettv->vval.v_number = lnum;
8043}
8044
8045/*
8046 * "nr2char()" function
8047 */
8048 static void
8049f_nr2char(typval_T *argvars, typval_T *rettv)
8050{
8051 char_u buf[NUMBUFLEN];
8052
8053#ifdef FEAT_MBYTE
8054 if (has_mbyte)
8055 {
8056 int utf8 = 0;
8057
8058 if (argvars[1].v_type != VAR_UNKNOWN)
8059 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
8060 if (utf8)
8061 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8062 else
8063 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8064 }
8065 else
8066#endif
8067 {
8068 buf[0] = (char_u)get_tv_number(&argvars[0]);
8069 buf[1] = NUL;
8070 }
8071 rettv->v_type = VAR_STRING;
8072 rettv->vval.v_string = vim_strsave(buf);
8073}
8074
8075/*
8076 * "or(expr, expr)" function
8077 */
8078 static void
8079f_or(typval_T *argvars, typval_T *rettv)
8080{
8081 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8082 | get_tv_number_chk(&argvars[1], NULL);
8083}
8084
8085/*
8086 * "pathshorten()" function
8087 */
8088 static void
8089f_pathshorten(typval_T *argvars, typval_T *rettv)
8090{
8091 char_u *p;
8092
8093 rettv->v_type = VAR_STRING;
8094 p = get_tv_string_chk(&argvars[0]);
8095 if (p == NULL)
8096 rettv->vval.v_string = NULL;
8097 else
8098 {
8099 p = vim_strsave(p);
8100 rettv->vval.v_string = p;
8101 if (p != NULL)
8102 shorten_dir(p);
8103 }
8104}
8105
8106#ifdef FEAT_PERL
8107/*
8108 * "perleval()" function
8109 */
8110 static void
8111f_perleval(typval_T *argvars, typval_T *rettv)
8112{
8113 char_u *str;
8114 char_u buf[NUMBUFLEN];
8115
8116 str = get_tv_string_buf(&argvars[0], buf);
8117 do_perleval(str, rettv);
8118}
8119#endif
8120
8121#ifdef FEAT_FLOAT
8122/*
8123 * "pow()" function
8124 */
8125 static void
8126f_pow(typval_T *argvars, typval_T *rettv)
8127{
8128 float_T fx = 0.0, fy = 0.0;
8129
8130 rettv->v_type = VAR_FLOAT;
8131 if (get_float_arg(argvars, &fx) == OK
8132 && get_float_arg(&argvars[1], &fy) == OK)
8133 rettv->vval.v_float = pow(fx, fy);
8134 else
8135 rettv->vval.v_float = 0.0;
8136}
8137#endif
8138
8139/*
8140 * "prevnonblank()" function
8141 */
8142 static void
8143f_prevnonblank(typval_T *argvars, typval_T *rettv)
8144{
8145 linenr_T lnum;
8146
8147 lnum = get_tv_lnum(argvars);
8148 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8149 lnum = 0;
8150 else
8151 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8152 --lnum;
8153 rettv->vval.v_number = lnum;
8154}
8155
8156/* This dummy va_list is here because:
8157 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8158 * - locally in the function results in a "used before set" warning
8159 * - using va_start() to initialize it gives "function with fixed args" error */
8160static va_list ap;
8161
8162/*
8163 * "printf()" function
8164 */
8165 static void
8166f_printf(typval_T *argvars, typval_T *rettv)
8167{
8168 char_u buf[NUMBUFLEN];
8169 int len;
8170 char_u *s;
8171 int saved_did_emsg = did_emsg;
8172 char *fmt;
8173
8174 rettv->v_type = VAR_STRING;
8175 rettv->vval.v_string = NULL;
8176
8177 /* Get the required length, allocate the buffer and do it for real. */
8178 did_emsg = FALSE;
8179 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008180 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008181 if (!did_emsg)
8182 {
8183 s = alloc(len + 1);
8184 if (s != NULL)
8185 {
8186 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008187 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8188 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008189 }
8190 }
8191 did_emsg |= saved_did_emsg;
8192}
8193
8194/*
8195 * "pumvisible()" function
8196 */
8197 static void
8198f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8199{
8200#ifdef FEAT_INS_EXPAND
8201 if (pum_visible())
8202 rettv->vval.v_number = 1;
8203#endif
8204}
8205
8206#ifdef FEAT_PYTHON3
8207/*
8208 * "py3eval()" function
8209 */
8210 static void
8211f_py3eval(typval_T *argvars, typval_T *rettv)
8212{
8213 char_u *str;
8214 char_u buf[NUMBUFLEN];
8215
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008216 if (p_pyx == 0)
8217 p_pyx = 3;
8218
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008219 str = get_tv_string_buf(&argvars[0], buf);
8220 do_py3eval(str, rettv);
8221}
8222#endif
8223
8224#ifdef FEAT_PYTHON
8225/*
8226 * "pyeval()" function
8227 */
8228 static void
8229f_pyeval(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 = 2;
8236
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008237 str = get_tv_string_buf(&argvars[0], buf);
8238 do_pyeval(str, rettv);
8239}
8240#endif
8241
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008242#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8243/*
8244 * "pyxeval()" function
8245 */
8246 static void
8247f_pyxeval(typval_T *argvars, typval_T *rettv)
8248{
8249# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8250 init_pyxversion();
8251 if (p_pyx == 2)
8252 f_pyeval(argvars, rettv);
8253 else
8254 f_py3eval(argvars, rettv);
8255# elif defined(FEAT_PYTHON)
8256 f_pyeval(argvars, rettv);
8257# elif defined(FEAT_PYTHON3)
8258 f_py3eval(argvars, rettv);
8259# endif
8260}
8261#endif
8262
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008263/*
8264 * "range()" function
8265 */
8266 static void
8267f_range(typval_T *argvars, typval_T *rettv)
8268{
8269 varnumber_T start;
8270 varnumber_T end;
8271 varnumber_T stride = 1;
8272 varnumber_T i;
8273 int error = FALSE;
8274
8275 start = get_tv_number_chk(&argvars[0], &error);
8276 if (argvars[1].v_type == VAR_UNKNOWN)
8277 {
8278 end = start - 1;
8279 start = 0;
8280 }
8281 else
8282 {
8283 end = get_tv_number_chk(&argvars[1], &error);
8284 if (argvars[2].v_type != VAR_UNKNOWN)
8285 stride = get_tv_number_chk(&argvars[2], &error);
8286 }
8287
8288 if (error)
8289 return; /* type error; errmsg already given */
8290 if (stride == 0)
8291 EMSG(_("E726: Stride is zero"));
8292 else if (stride > 0 ? end + 1 < start : end - 1 > start)
8293 EMSG(_("E727: Start past end"));
8294 else
8295 {
8296 if (rettv_list_alloc(rettv) == OK)
8297 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8298 if (list_append_number(rettv->vval.v_list,
8299 (varnumber_T)i) == FAIL)
8300 break;
8301 }
8302}
8303
8304/*
8305 * "readfile()" function
8306 */
8307 static void
8308f_readfile(typval_T *argvars, typval_T *rettv)
8309{
8310 int binary = FALSE;
8311 int failed = FALSE;
8312 char_u *fname;
8313 FILE *fd;
8314 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8315 int io_size = sizeof(buf);
8316 int readlen; /* size of last fread() */
8317 char_u *prev = NULL; /* previously read bytes, if any */
8318 long prevlen = 0; /* length of data in prev */
8319 long prevsize = 0; /* size of prev buffer */
8320 long maxline = MAXLNUM;
8321 long cnt = 0;
8322 char_u *p; /* position in buf */
8323 char_u *start; /* start of current line */
8324
8325 if (argvars[1].v_type != VAR_UNKNOWN)
8326 {
8327 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
8328 binary = TRUE;
8329 if (argvars[2].v_type != VAR_UNKNOWN)
8330 maxline = (long)get_tv_number(&argvars[2]);
8331 }
8332
8333 if (rettv_list_alloc(rettv) == FAIL)
8334 return;
8335
8336 /* Always open the file in binary mode, library functions have a mind of
8337 * their own about CR-LF conversion. */
8338 fname = get_tv_string(&argvars[0]);
8339 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8340 {
8341 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
8342 return;
8343 }
8344
8345 while (cnt < maxline || maxline < 0)
8346 {
8347 readlen = (int)fread(buf, 1, io_size, fd);
8348
8349 /* This for loop processes what was read, but is also entered at end
8350 * of file so that either:
8351 * - an incomplete line gets written
8352 * - a "binary" file gets an empty line at the end if it ends in a
8353 * newline. */
8354 for (p = buf, start = buf;
8355 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8356 ++p)
8357 {
8358 if (*p == '\n' || readlen <= 0)
8359 {
8360 listitem_T *li;
8361 char_u *s = NULL;
8362 long_u len = p - start;
8363
8364 /* Finished a line. Remove CRs before NL. */
8365 if (readlen > 0 && !binary)
8366 {
8367 while (len > 0 && start[len - 1] == '\r')
8368 --len;
8369 /* removal may cross back to the "prev" string */
8370 if (len == 0)
8371 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8372 --prevlen;
8373 }
8374 if (prevlen == 0)
8375 s = vim_strnsave(start, (int)len);
8376 else
8377 {
8378 /* Change "prev" buffer to be the right size. This way
8379 * the bytes are only copied once, and very long lines are
8380 * allocated only once. */
8381 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8382 {
8383 mch_memmove(s + prevlen, start, len);
8384 s[prevlen + len] = NUL;
8385 prev = NULL; /* the list will own the string */
8386 prevlen = prevsize = 0;
8387 }
8388 }
8389 if (s == NULL)
8390 {
8391 do_outofmem_msg((long_u) prevlen + len + 1);
8392 failed = TRUE;
8393 break;
8394 }
8395
8396 if ((li = listitem_alloc()) == NULL)
8397 {
8398 vim_free(s);
8399 failed = TRUE;
8400 break;
8401 }
8402 li->li_tv.v_type = VAR_STRING;
8403 li->li_tv.v_lock = 0;
8404 li->li_tv.vval.v_string = s;
8405 list_append(rettv->vval.v_list, li);
8406
8407 start = p + 1; /* step over newline */
8408 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
8409 break;
8410 }
8411 else if (*p == NUL)
8412 *p = '\n';
8413#ifdef FEAT_MBYTE
8414 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
8415 * when finding the BF and check the previous two bytes. */
8416 else if (*p == 0xbf && enc_utf8 && !binary)
8417 {
8418 /* Find the two bytes before the 0xbf. If p is at buf, or buf
8419 * + 1, these may be in the "prev" string. */
8420 char_u back1 = p >= buf + 1 ? p[-1]
8421 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
8422 char_u back2 = p >= buf + 2 ? p[-2]
8423 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
8424 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
8425
8426 if (back2 == 0xef && back1 == 0xbb)
8427 {
8428 char_u *dest = p - 2;
8429
8430 /* Usually a BOM is at the beginning of a file, and so at
8431 * the beginning of a line; then we can just step over it.
8432 */
8433 if (start == dest)
8434 start = p + 1;
8435 else
8436 {
8437 /* have to shuffle buf to close gap */
8438 int adjust_prevlen = 0;
8439
8440 if (dest < buf)
8441 {
8442 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
8443 dest = buf;
8444 }
8445 if (readlen > p - buf + 1)
8446 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
8447 readlen -= 3 - adjust_prevlen;
8448 prevlen -= adjust_prevlen;
8449 p = dest - 1;
8450 }
8451 }
8452 }
8453#endif
8454 } /* for */
8455
8456 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
8457 break;
8458 if (start < p)
8459 {
8460 /* There's part of a line in buf, store it in "prev". */
8461 if (p - start + prevlen >= prevsize)
8462 {
8463 /* need bigger "prev" buffer */
8464 char_u *newprev;
8465
8466 /* A common use case is ordinary text files and "prev" gets a
8467 * fragment of a line, so the first allocation is made
8468 * small, to avoid repeatedly 'allocing' large and
8469 * 'reallocing' small. */
8470 if (prevsize == 0)
8471 prevsize = (long)(p - start);
8472 else
8473 {
8474 long grow50pc = (prevsize * 3) / 2;
8475 long growmin = (long)((p - start) * 2 + prevlen);
8476 prevsize = grow50pc > growmin ? grow50pc : growmin;
8477 }
8478 newprev = prev == NULL ? alloc(prevsize)
8479 : vim_realloc(prev, prevsize);
8480 if (newprev == NULL)
8481 {
8482 do_outofmem_msg((long_u)prevsize);
8483 failed = TRUE;
8484 break;
8485 }
8486 prev = newprev;
8487 }
8488 /* Add the line part to end of "prev". */
8489 mch_memmove(prev + prevlen, start, p - start);
8490 prevlen += (long)(p - start);
8491 }
8492 } /* while */
8493
8494 /*
8495 * For a negative line count use only the lines at the end of the file,
8496 * free the rest.
8497 */
8498 if (!failed && maxline < 0)
8499 while (cnt > -maxline)
8500 {
8501 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
8502 --cnt;
8503 }
8504
8505 if (failed)
8506 {
8507 list_free(rettv->vval.v_list);
8508 /* readfile doc says an empty list is returned on error */
8509 rettv->vval.v_list = list_alloc();
8510 }
8511
8512 vim_free(prev);
8513 fclose(fd);
8514}
8515
8516#if defined(FEAT_RELTIME)
8517static int list2proftime(typval_T *arg, proftime_T *tm);
8518
8519/*
8520 * Convert a List to proftime_T.
8521 * Return FAIL when there is something wrong.
8522 */
8523 static int
8524list2proftime(typval_T *arg, proftime_T *tm)
8525{
8526 long n1, n2;
8527 int error = FALSE;
8528
8529 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
8530 || arg->vval.v_list->lv_len != 2)
8531 return FAIL;
8532 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
8533 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
8534# ifdef WIN3264
8535 tm->HighPart = n1;
8536 tm->LowPart = n2;
8537# else
8538 tm->tv_sec = n1;
8539 tm->tv_usec = n2;
8540# endif
8541 return error ? FAIL : OK;
8542}
8543#endif /* FEAT_RELTIME */
8544
8545/*
8546 * "reltime()" function
8547 */
8548 static void
8549f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8550{
8551#ifdef FEAT_RELTIME
8552 proftime_T res;
8553 proftime_T start;
8554
8555 if (argvars[0].v_type == VAR_UNKNOWN)
8556 {
8557 /* No arguments: get current time. */
8558 profile_start(&res);
8559 }
8560 else if (argvars[1].v_type == VAR_UNKNOWN)
8561 {
8562 if (list2proftime(&argvars[0], &res) == FAIL)
8563 return;
8564 profile_end(&res);
8565 }
8566 else
8567 {
8568 /* Two arguments: compute the difference. */
8569 if (list2proftime(&argvars[0], &start) == FAIL
8570 || list2proftime(&argvars[1], &res) == FAIL)
8571 return;
8572 profile_sub(&res, &start);
8573 }
8574
8575 if (rettv_list_alloc(rettv) == OK)
8576 {
8577 long n1, n2;
8578
8579# ifdef WIN3264
8580 n1 = res.HighPart;
8581 n2 = res.LowPart;
8582# else
8583 n1 = res.tv_sec;
8584 n2 = res.tv_usec;
8585# endif
8586 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
8587 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
8588 }
8589#endif
8590}
8591
8592#ifdef FEAT_FLOAT
8593/*
8594 * "reltimefloat()" function
8595 */
8596 static void
8597f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
8598{
8599# ifdef FEAT_RELTIME
8600 proftime_T tm;
8601# endif
8602
8603 rettv->v_type = VAR_FLOAT;
8604 rettv->vval.v_float = 0;
8605# ifdef FEAT_RELTIME
8606 if (list2proftime(&argvars[0], &tm) == OK)
8607 rettv->vval.v_float = profile_float(&tm);
8608# endif
8609}
8610#endif
8611
8612/*
8613 * "reltimestr()" function
8614 */
8615 static void
8616f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
8617{
8618#ifdef FEAT_RELTIME
8619 proftime_T tm;
8620#endif
8621
8622 rettv->v_type = VAR_STRING;
8623 rettv->vval.v_string = NULL;
8624#ifdef FEAT_RELTIME
8625 if (list2proftime(&argvars[0], &tm) == OK)
8626 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
8627#endif
8628}
8629
8630#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
8631static void make_connection(void);
8632static int check_connection(void);
8633
8634 static void
8635make_connection(void)
8636{
8637 if (X_DISPLAY == NULL
8638# ifdef FEAT_GUI
8639 && !gui.in_use
8640# endif
8641 )
8642 {
8643 x_force_connect = TRUE;
8644 setup_term_clip();
8645 x_force_connect = FALSE;
8646 }
8647}
8648
8649 static int
8650check_connection(void)
8651{
8652 make_connection();
8653 if (X_DISPLAY == NULL)
8654 {
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01008655 EMSG(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008656 return FAIL;
8657 }
8658 return OK;
8659}
8660#endif
8661
8662#ifdef FEAT_CLIENTSERVER
8663 static void
8664remote_common(typval_T *argvars, typval_T *rettv, int expr)
8665{
8666 char_u *server_name;
8667 char_u *keys;
8668 char_u *r = NULL;
8669 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008670 int timeout = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008671# ifdef WIN32
8672 HWND w;
8673# else
8674 Window w;
8675# endif
8676
8677 if (check_restricted() || check_secure())
8678 return;
8679
8680# ifdef FEAT_X11
8681 if (check_connection() == FAIL)
8682 return;
8683# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008684 if (argvars[2].v_type != VAR_UNKNOWN
8685 && argvars[3].v_type != VAR_UNKNOWN)
8686 timeout = get_tv_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008687
8688 server_name = get_tv_string_chk(&argvars[0]);
8689 if (server_name == NULL)
8690 return; /* type error; errmsg already given */
8691 keys = get_tv_string_buf(&argvars[1], buf);
8692# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008693 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008694# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008695 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
8696 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008697# endif
8698 {
8699 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008700 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008701 EMSG(r); /* sending worked but evaluation failed */
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008702 vim_free(r);
8703 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008704 else
8705 EMSG2(_("E241: Unable to send to %s"), server_name);
8706 return;
8707 }
8708
8709 rettv->vval.v_string = r;
8710
8711 if (argvars[2].v_type != VAR_UNKNOWN)
8712 {
8713 dictitem_T v;
8714 char_u str[30];
8715 char_u *idvar;
8716
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008717 idvar = get_tv_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008718 if (idvar != NULL && *idvar != NUL)
8719 {
8720 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
8721 v.di_tv.v_type = VAR_STRING;
8722 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008723 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008724 vim_free(v.di_tv.vval.v_string);
8725 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008726 }
8727}
8728#endif
8729
8730/*
8731 * "remote_expr()" function
8732 */
8733 static void
8734f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
8735{
8736 rettv->v_type = VAR_STRING;
8737 rettv->vval.v_string = NULL;
8738#ifdef FEAT_CLIENTSERVER
8739 remote_common(argvars, rettv, TRUE);
8740#endif
8741}
8742
8743/*
8744 * "remote_foreground()" function
8745 */
8746 static void
8747f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8748{
8749#ifdef FEAT_CLIENTSERVER
8750# ifdef WIN32
8751 /* On Win32 it's done in this application. */
8752 {
8753 char_u *server_name = get_tv_string_chk(&argvars[0]);
8754
8755 if (server_name != NULL)
8756 serverForeground(server_name);
8757 }
8758# else
8759 /* Send a foreground() expression to the server. */
8760 argvars[1].v_type = VAR_STRING;
8761 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
8762 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008763 rettv->v_type = VAR_STRING;
8764 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008765 remote_common(argvars, rettv, TRUE);
8766 vim_free(argvars[1].vval.v_string);
8767# endif
8768#endif
8769}
8770
8771 static void
8772f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
8773{
8774#ifdef FEAT_CLIENTSERVER
8775 dictitem_T v;
8776 char_u *s = NULL;
8777# ifdef WIN32
8778 long_u n = 0;
8779# endif
8780 char_u *serverid;
8781
8782 if (check_restricted() || check_secure())
8783 {
8784 rettv->vval.v_number = -1;
8785 return;
8786 }
8787 serverid = get_tv_string_chk(&argvars[0]);
8788 if (serverid == NULL)
8789 {
8790 rettv->vval.v_number = -1;
8791 return; /* type error; errmsg already given */
8792 }
8793# ifdef WIN32
8794 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
8795 if (n == 0)
8796 rettv->vval.v_number = -1;
8797 else
8798 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008799 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008800 rettv->vval.v_number = (s != NULL);
8801 }
8802# else
8803 if (check_connection() == FAIL)
8804 return;
8805
8806 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
8807 serverStrToWin(serverid), &s);
8808# endif
8809
8810 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
8811 {
8812 char_u *retvar;
8813
8814 v.di_tv.v_type = VAR_STRING;
8815 v.di_tv.vval.v_string = vim_strsave(s);
8816 retvar = get_tv_string_chk(&argvars[1]);
8817 if (retvar != NULL)
8818 set_var(retvar, &v.di_tv, FALSE);
8819 vim_free(v.di_tv.vval.v_string);
8820 }
8821#else
8822 rettv->vval.v_number = -1;
8823#endif
8824}
8825
8826 static void
8827f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
8828{
8829 char_u *r = NULL;
8830
8831#ifdef FEAT_CLIENTSERVER
8832 char_u *serverid = get_tv_string_chk(&argvars[0]);
8833
8834 if (serverid != NULL && !check_restricted() && !check_secure())
8835 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008836 int timeout = 0;
Bram Moolenaar1662ce12017-03-19 21:47:50 +01008837# ifdef WIN32
8838 /* The server's HWND is encoded in the 'id' parameter */
8839 long_u n = 0;
8840# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008841
8842 if (argvars[1].v_type != VAR_UNKNOWN)
8843 timeout = get_tv_number(&argvars[1]);
8844
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008845# ifdef WIN32
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008846 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
8847 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008848 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008849 if (r == NULL)
8850# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008851 if (check_connection() == FAIL
8852 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
8853 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008854# endif
8855 EMSG(_("E277: Unable to read a server reply"));
8856 }
8857#endif
8858 rettv->v_type = VAR_STRING;
8859 rettv->vval.v_string = r;
8860}
8861
8862/*
8863 * "remote_send()" function
8864 */
8865 static void
8866f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
8867{
8868 rettv->v_type = VAR_STRING;
8869 rettv->vval.v_string = NULL;
8870#ifdef FEAT_CLIENTSERVER
8871 remote_common(argvars, rettv, FALSE);
8872#endif
8873}
8874
8875/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01008876 * "remote_startserver()" function
8877 */
8878 static void
8879f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8880{
8881#ifdef FEAT_CLIENTSERVER
8882 char_u *server = get_tv_string_chk(&argvars[0]);
8883
8884 if (server == NULL)
8885 return; /* type error; errmsg already given */
8886 if (serverName != NULL)
8887 EMSG(_("E941: already started a server"));
8888 else
8889 {
8890# ifdef FEAT_X11
8891 if (check_connection() == OK)
8892 serverRegisterName(X_DISPLAY, server);
8893# else
8894 serverSetName(server);
8895# endif
8896 }
8897#else
8898 EMSG(_("E942: +clientserver feature not available"));
8899#endif
8900}
8901
8902/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008903 * "remove()" function
8904 */
8905 static void
8906f_remove(typval_T *argvars, typval_T *rettv)
8907{
8908 list_T *l;
8909 listitem_T *item, *item2;
8910 listitem_T *li;
8911 long idx;
8912 long end;
8913 char_u *key;
8914 dict_T *d;
8915 dictitem_T *di;
8916 char_u *arg_errmsg = (char_u *)N_("remove() argument");
8917
8918 if (argvars[0].v_type == VAR_DICT)
8919 {
8920 if (argvars[2].v_type != VAR_UNKNOWN)
8921 EMSG2(_(e_toomanyarg), "remove()");
8922 else if ((d = argvars[0].vval.v_dict) != NULL
8923 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
8924 {
8925 key = get_tv_string_chk(&argvars[1]);
8926 if (key != NULL)
8927 {
8928 di = dict_find(d, key, -1);
8929 if (di == NULL)
8930 EMSG2(_(e_dictkey), key);
8931 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
8932 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
8933 {
8934 *rettv = di->di_tv;
8935 init_tv(&di->di_tv);
8936 dictitem_remove(d, di);
8937 }
8938 }
8939 }
8940 }
8941 else if (argvars[0].v_type != VAR_LIST)
8942 EMSG2(_(e_listdictarg), "remove()");
8943 else if ((l = argvars[0].vval.v_list) != NULL
8944 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
8945 {
8946 int error = FALSE;
8947
8948 idx = (long)get_tv_number_chk(&argvars[1], &error);
8949 if (error)
8950 ; /* type error: do nothing, errmsg already given */
8951 else if ((item = list_find(l, idx)) == NULL)
8952 EMSGN(_(e_listidx), idx);
8953 else
8954 {
8955 if (argvars[2].v_type == VAR_UNKNOWN)
8956 {
8957 /* Remove one item, return its value. */
8958 vimlist_remove(l, item, item);
8959 *rettv = item->li_tv;
8960 vim_free(item);
8961 }
8962 else
8963 {
8964 /* Remove range of items, return list with values. */
8965 end = (long)get_tv_number_chk(&argvars[2], &error);
8966 if (error)
8967 ; /* type error: do nothing */
8968 else if ((item2 = list_find(l, end)) == NULL)
8969 EMSGN(_(e_listidx), end);
8970 else
8971 {
8972 int cnt = 0;
8973
8974 for (li = item; li != NULL; li = li->li_next)
8975 {
8976 ++cnt;
8977 if (li == item2)
8978 break;
8979 }
8980 if (li == NULL) /* didn't find "item2" after "item" */
8981 EMSG(_(e_invrange));
8982 else
8983 {
8984 vimlist_remove(l, item, item2);
8985 if (rettv_list_alloc(rettv) == OK)
8986 {
8987 l = rettv->vval.v_list;
8988 l->lv_first = item;
8989 l->lv_last = item2;
8990 item->li_prev = NULL;
8991 item2->li_next = NULL;
8992 l->lv_len = cnt;
8993 }
8994 }
8995 }
8996 }
8997 }
8998 }
8999}
9000
9001/*
9002 * "rename({from}, {to})" function
9003 */
9004 static void
9005f_rename(typval_T *argvars, typval_T *rettv)
9006{
9007 char_u buf[NUMBUFLEN];
9008
9009 if (check_restricted() || check_secure())
9010 rettv->vval.v_number = -1;
9011 else
9012 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
9013 get_tv_string_buf(&argvars[1], buf));
9014}
9015
9016/*
9017 * "repeat()" function
9018 */
9019 static void
9020f_repeat(typval_T *argvars, typval_T *rettv)
9021{
9022 char_u *p;
9023 int n;
9024 int slen;
9025 int len;
9026 char_u *r;
9027 int i;
9028
9029 n = (int)get_tv_number(&argvars[1]);
9030 if (argvars[0].v_type == VAR_LIST)
9031 {
9032 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9033 while (n-- > 0)
9034 if (list_extend(rettv->vval.v_list,
9035 argvars[0].vval.v_list, NULL) == FAIL)
9036 break;
9037 }
9038 else
9039 {
9040 p = get_tv_string(&argvars[0]);
9041 rettv->v_type = VAR_STRING;
9042 rettv->vval.v_string = NULL;
9043
9044 slen = (int)STRLEN(p);
9045 len = slen * n;
9046 if (len <= 0)
9047 return;
9048
9049 r = alloc(len + 1);
9050 if (r != NULL)
9051 {
9052 for (i = 0; i < n; i++)
9053 mch_memmove(r + i * slen, p, (size_t)slen);
9054 r[len] = NUL;
9055 }
9056
9057 rettv->vval.v_string = r;
9058 }
9059}
9060
9061/*
9062 * "resolve()" function
9063 */
9064 static void
9065f_resolve(typval_T *argvars, typval_T *rettv)
9066{
9067 char_u *p;
9068#ifdef HAVE_READLINK
9069 char_u *buf = NULL;
9070#endif
9071
9072 p = get_tv_string(&argvars[0]);
9073#ifdef FEAT_SHORTCUT
9074 {
9075 char_u *v = NULL;
9076
9077 v = mch_resolve_shortcut(p);
9078 if (v != NULL)
9079 rettv->vval.v_string = v;
9080 else
9081 rettv->vval.v_string = vim_strsave(p);
9082 }
9083#else
9084# ifdef HAVE_READLINK
9085 {
9086 char_u *cpy;
9087 int len;
9088 char_u *remain = NULL;
9089 char_u *q;
9090 int is_relative_to_current = FALSE;
9091 int has_trailing_pathsep = FALSE;
9092 int limit = 100;
9093
9094 p = vim_strsave(p);
9095
9096 if (p[0] == '.' && (vim_ispathsep(p[1])
9097 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9098 is_relative_to_current = TRUE;
9099
9100 len = STRLEN(p);
9101 if (len > 0 && after_pathsep(p, p + len))
9102 {
9103 has_trailing_pathsep = TRUE;
9104 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9105 }
9106
9107 q = getnextcomp(p);
9108 if (*q != NUL)
9109 {
9110 /* Separate the first path component in "p", and keep the
9111 * remainder (beginning with the path separator). */
9112 remain = vim_strsave(q - 1);
9113 q[-1] = NUL;
9114 }
9115
9116 buf = alloc(MAXPATHL + 1);
9117 if (buf == NULL)
9118 goto fail;
9119
9120 for (;;)
9121 {
9122 for (;;)
9123 {
9124 len = readlink((char *)p, (char *)buf, MAXPATHL);
9125 if (len <= 0)
9126 break;
9127 buf[len] = NUL;
9128
9129 if (limit-- == 0)
9130 {
9131 vim_free(p);
9132 vim_free(remain);
9133 EMSG(_("E655: Too many symbolic links (cycle?)"));
9134 rettv->vval.v_string = NULL;
9135 goto fail;
9136 }
9137
9138 /* Ensure that the result will have a trailing path separator
9139 * if the argument has one. */
9140 if (remain == NULL && has_trailing_pathsep)
9141 add_pathsep(buf);
9142
9143 /* Separate the first path component in the link value and
9144 * concatenate the remainders. */
9145 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9146 if (*q != NUL)
9147 {
9148 if (remain == NULL)
9149 remain = vim_strsave(q - 1);
9150 else
9151 {
9152 cpy = concat_str(q - 1, remain);
9153 if (cpy != NULL)
9154 {
9155 vim_free(remain);
9156 remain = cpy;
9157 }
9158 }
9159 q[-1] = NUL;
9160 }
9161
9162 q = gettail(p);
9163 if (q > p && *q == NUL)
9164 {
9165 /* Ignore trailing path separator. */
9166 q[-1] = NUL;
9167 q = gettail(p);
9168 }
9169 if (q > p && !mch_isFullName(buf))
9170 {
9171 /* symlink is relative to directory of argument */
9172 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
9173 if (cpy != NULL)
9174 {
9175 STRCPY(cpy, p);
9176 STRCPY(gettail(cpy), buf);
9177 vim_free(p);
9178 p = cpy;
9179 }
9180 }
9181 else
9182 {
9183 vim_free(p);
9184 p = vim_strsave(buf);
9185 }
9186 }
9187
9188 if (remain == NULL)
9189 break;
9190
9191 /* Append the first path component of "remain" to "p". */
9192 q = getnextcomp(remain + 1);
9193 len = q - remain - (*q != NUL);
9194 cpy = vim_strnsave(p, STRLEN(p) + len);
9195 if (cpy != NULL)
9196 {
9197 STRNCAT(cpy, remain, len);
9198 vim_free(p);
9199 p = cpy;
9200 }
9201 /* Shorten "remain". */
9202 if (*q != NUL)
9203 STRMOVE(remain, q - 1);
9204 else
9205 {
9206 vim_free(remain);
9207 remain = NULL;
9208 }
9209 }
9210
9211 /* If the result is a relative path name, make it explicitly relative to
9212 * the current directory if and only if the argument had this form. */
9213 if (!vim_ispathsep(*p))
9214 {
9215 if (is_relative_to_current
9216 && *p != NUL
9217 && !(p[0] == '.'
9218 && (p[1] == NUL
9219 || vim_ispathsep(p[1])
9220 || (p[1] == '.'
9221 && (p[2] == NUL
9222 || vim_ispathsep(p[2]))))))
9223 {
9224 /* Prepend "./". */
9225 cpy = concat_str((char_u *)"./", p);
9226 if (cpy != NULL)
9227 {
9228 vim_free(p);
9229 p = cpy;
9230 }
9231 }
9232 else if (!is_relative_to_current)
9233 {
9234 /* Strip leading "./". */
9235 q = p;
9236 while (q[0] == '.' && vim_ispathsep(q[1]))
9237 q += 2;
9238 if (q > p)
9239 STRMOVE(p, p + 2);
9240 }
9241 }
9242
9243 /* Ensure that the result will have no trailing path separator
9244 * if the argument had none. But keep "/" or "//". */
9245 if (!has_trailing_pathsep)
9246 {
9247 q = p + STRLEN(p);
9248 if (after_pathsep(p, q))
9249 *gettail_sep(p) = NUL;
9250 }
9251
9252 rettv->vval.v_string = p;
9253 }
9254# else
9255 rettv->vval.v_string = vim_strsave(p);
9256# endif
9257#endif
9258
9259 simplify_filename(rettv->vval.v_string);
9260
9261#ifdef HAVE_READLINK
9262fail:
9263 vim_free(buf);
9264#endif
9265 rettv->v_type = VAR_STRING;
9266}
9267
9268/*
9269 * "reverse({list})" function
9270 */
9271 static void
9272f_reverse(typval_T *argvars, typval_T *rettv)
9273{
9274 list_T *l;
9275 listitem_T *li, *ni;
9276
9277 if (argvars[0].v_type != VAR_LIST)
9278 EMSG2(_(e_listarg), "reverse()");
9279 else if ((l = argvars[0].vval.v_list) != NULL
9280 && !tv_check_lock(l->lv_lock,
9281 (char_u *)N_("reverse() argument"), TRUE))
9282 {
9283 li = l->lv_last;
9284 l->lv_first = l->lv_last = NULL;
9285 l->lv_len = 0;
9286 while (li != NULL)
9287 {
9288 ni = li->li_prev;
9289 list_append(l, li);
9290 li = ni;
9291 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009292 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009293 l->lv_idx = l->lv_len - l->lv_idx - 1;
9294 }
9295}
9296
9297#define SP_NOMOVE 0x01 /* don't move cursor */
9298#define SP_REPEAT 0x02 /* repeat to find outer pair */
9299#define SP_RETCOUNT 0x04 /* return matchcount */
9300#define SP_SETPCMARK 0x08 /* set previous context mark */
9301#define SP_START 0x10 /* accept match at start position */
9302#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9303#define SP_END 0x40 /* leave cursor at end of match */
9304#define SP_COLUMN 0x80 /* start at cursor column */
9305
9306static int get_search_arg(typval_T *varp, int *flagsp);
9307
9308/*
9309 * Get flags for a search function.
9310 * Possibly sets "p_ws".
9311 * Returns BACKWARD, FORWARD or zero (for an error).
9312 */
9313 static int
9314get_search_arg(typval_T *varp, int *flagsp)
9315{
9316 int dir = FORWARD;
9317 char_u *flags;
9318 char_u nbuf[NUMBUFLEN];
9319 int mask;
9320
9321 if (varp->v_type != VAR_UNKNOWN)
9322 {
9323 flags = get_tv_string_buf_chk(varp, nbuf);
9324 if (flags == NULL)
9325 return 0; /* type error; errmsg already given */
9326 while (*flags != NUL)
9327 {
9328 switch (*flags)
9329 {
9330 case 'b': dir = BACKWARD; break;
9331 case 'w': p_ws = TRUE; break;
9332 case 'W': p_ws = FALSE; break;
9333 default: mask = 0;
9334 if (flagsp != NULL)
9335 switch (*flags)
9336 {
9337 case 'c': mask = SP_START; break;
9338 case 'e': mask = SP_END; break;
9339 case 'm': mask = SP_RETCOUNT; break;
9340 case 'n': mask = SP_NOMOVE; break;
9341 case 'p': mask = SP_SUBPAT; break;
9342 case 'r': mask = SP_REPEAT; break;
9343 case 's': mask = SP_SETPCMARK; break;
9344 case 'z': mask = SP_COLUMN; break;
9345 }
9346 if (mask == 0)
9347 {
9348 EMSG2(_(e_invarg2), flags);
9349 dir = 0;
9350 }
9351 else
9352 *flagsp |= mask;
9353 }
9354 if (dir == 0)
9355 break;
9356 ++flags;
9357 }
9358 }
9359 return dir;
9360}
9361
9362/*
9363 * Shared by search() and searchpos() functions.
9364 */
9365 static int
9366search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9367{
9368 int flags;
9369 char_u *pat;
9370 pos_T pos;
9371 pos_T save_cursor;
9372 int save_p_ws = p_ws;
9373 int dir;
9374 int retval = 0; /* default: FAIL */
9375 long lnum_stop = 0;
9376 proftime_T tm;
9377#ifdef FEAT_RELTIME
9378 long time_limit = 0;
9379#endif
9380 int options = SEARCH_KEEP;
9381 int subpatnum;
9382
9383 pat = get_tv_string(&argvars[0]);
9384 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
9385 if (dir == 0)
9386 goto theend;
9387 flags = *flagsp;
9388 if (flags & SP_START)
9389 options |= SEARCH_START;
9390 if (flags & SP_END)
9391 options |= SEARCH_END;
9392 if (flags & SP_COLUMN)
9393 options |= SEARCH_COL;
9394
9395 /* Optional arguments: line number to stop searching and timeout. */
9396 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9397 {
9398 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
9399 if (lnum_stop < 0)
9400 goto theend;
9401#ifdef FEAT_RELTIME
9402 if (argvars[3].v_type != VAR_UNKNOWN)
9403 {
9404 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
9405 if (time_limit < 0)
9406 goto theend;
9407 }
9408#endif
9409 }
9410
9411#ifdef FEAT_RELTIME
9412 /* Set the time limit, if there is one. */
9413 profile_setlimit(time_limit, &tm);
9414#endif
9415
9416 /*
9417 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
9418 * Check to make sure only those flags are set.
9419 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
9420 * flags cannot be set. Check for that condition also.
9421 */
9422 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
9423 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9424 {
9425 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
9426 goto theend;
9427 }
9428
9429 pos = save_cursor = curwin->w_cursor;
9430 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009431 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009432 if (subpatnum != FAIL)
9433 {
9434 if (flags & SP_SUBPAT)
9435 retval = subpatnum;
9436 else
9437 retval = pos.lnum;
9438 if (flags & SP_SETPCMARK)
9439 setpcmark();
9440 curwin->w_cursor = pos;
9441 if (match_pos != NULL)
9442 {
9443 /* Store the match cursor position */
9444 match_pos->lnum = pos.lnum;
9445 match_pos->col = pos.col + 1;
9446 }
9447 /* "/$" will put the cursor after the end of the line, may need to
9448 * correct that here */
9449 check_cursor();
9450 }
9451
9452 /* If 'n' flag is used: restore cursor position. */
9453 if (flags & SP_NOMOVE)
9454 curwin->w_cursor = save_cursor;
9455 else
9456 curwin->w_set_curswant = TRUE;
9457theend:
9458 p_ws = save_p_ws;
9459
9460 return retval;
9461}
9462
9463#ifdef FEAT_FLOAT
9464
9465/*
9466 * round() is not in C90, use ceil() or floor() instead.
9467 */
9468 float_T
9469vim_round(float_T f)
9470{
9471 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
9472}
9473
9474/*
9475 * "round({float})" function
9476 */
9477 static void
9478f_round(typval_T *argvars, typval_T *rettv)
9479{
9480 float_T f = 0.0;
9481
9482 rettv->v_type = VAR_FLOAT;
9483 if (get_float_arg(argvars, &f) == OK)
9484 rettv->vval.v_float = vim_round(f);
9485 else
9486 rettv->vval.v_float = 0.0;
9487}
9488#endif
9489
9490/*
9491 * "screenattr()" function
9492 */
9493 static void
9494f_screenattr(typval_T *argvars, typval_T *rettv)
9495{
9496 int row;
9497 int col;
9498 int c;
9499
9500 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9501 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9502 if (row < 0 || row >= screen_Rows
9503 || col < 0 || col >= screen_Columns)
9504 c = -1;
9505 else
9506 c = ScreenAttrs[LineOffset[row] + col];
9507 rettv->vval.v_number = c;
9508}
9509
9510/*
9511 * "screenchar()" function
9512 */
9513 static void
9514f_screenchar(typval_T *argvars, typval_T *rettv)
9515{
9516 int row;
9517 int col;
9518 int off;
9519 int c;
9520
9521 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9522 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9523 if (row < 0 || row >= screen_Rows
9524 || col < 0 || col >= screen_Columns)
9525 c = -1;
9526 else
9527 {
9528 off = LineOffset[row] + col;
9529#ifdef FEAT_MBYTE
9530 if (enc_utf8 && ScreenLinesUC[off] != 0)
9531 c = ScreenLinesUC[off];
9532 else
9533#endif
9534 c = ScreenLines[off];
9535 }
9536 rettv->vval.v_number = c;
9537}
9538
9539/*
9540 * "screencol()" function
9541 *
9542 * First column is 1 to be consistent with virtcol().
9543 */
9544 static void
9545f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
9546{
9547 rettv->vval.v_number = screen_screencol() + 1;
9548}
9549
9550/*
9551 * "screenrow()" function
9552 */
9553 static void
9554f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
9555{
9556 rettv->vval.v_number = screen_screenrow() + 1;
9557}
9558
9559/*
9560 * "search()" function
9561 */
9562 static void
9563f_search(typval_T *argvars, typval_T *rettv)
9564{
9565 int flags = 0;
9566
9567 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
9568}
9569
9570/*
9571 * "searchdecl()" function
9572 */
9573 static void
9574f_searchdecl(typval_T *argvars, typval_T *rettv)
9575{
9576 int locally = 1;
9577 int thisblock = 0;
9578 int error = FALSE;
9579 char_u *name;
9580
9581 rettv->vval.v_number = 1; /* default: FAIL */
9582
9583 name = get_tv_string_chk(&argvars[0]);
9584 if (argvars[1].v_type != VAR_UNKNOWN)
9585 {
9586 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
9587 if (!error && argvars[2].v_type != VAR_UNKNOWN)
9588 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
9589 }
9590 if (!error && name != NULL)
9591 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
9592 locally, thisblock, SEARCH_KEEP) == FAIL;
9593}
9594
9595/*
9596 * Used by searchpair() and searchpairpos()
9597 */
9598 static int
9599searchpair_cmn(typval_T *argvars, pos_T *match_pos)
9600{
9601 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01009602 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009603 int save_p_ws = p_ws;
9604 int dir;
9605 int flags = 0;
9606 char_u nbuf1[NUMBUFLEN];
9607 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009608 int retval = 0; /* default: FAIL */
9609 long lnum_stop = 0;
9610 long time_limit = 0;
9611
9612 /* Get the three pattern arguments: start, middle, end. */
9613 spat = get_tv_string_chk(&argvars[0]);
9614 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
9615 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
9616 if (spat == NULL || mpat == NULL || epat == NULL)
9617 goto theend; /* type error */
9618
9619 /* Handle the optional fourth argument: flags */
9620 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
9621 if (dir == 0)
9622 goto theend;
9623
9624 /* Don't accept SP_END or SP_SUBPAT.
9625 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
9626 */
9627 if ((flags & (SP_END | SP_SUBPAT)) != 0
9628 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9629 {
9630 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
9631 goto theend;
9632 }
9633
9634 /* Using 'r' implies 'W', otherwise it doesn't work. */
9635 if (flags & SP_REPEAT)
9636 p_ws = FALSE;
9637
9638 /* Optional fifth argument: skip expression */
9639 if (argvars[3].v_type == VAR_UNKNOWN
9640 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01009641 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009642 else
9643 {
Bram Moolenaar48570482017-10-30 21:48:41 +01009644 skip = &argvars[4];
9645 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
9646 && skip->v_type != VAR_STRING)
9647 {
9648 /* Type error */
9649 goto theend;
9650 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009651 if (argvars[5].v_type != VAR_UNKNOWN)
9652 {
9653 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
9654 if (lnum_stop < 0)
9655 goto theend;
9656#ifdef FEAT_RELTIME
9657 if (argvars[6].v_type != VAR_UNKNOWN)
9658 {
9659 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
9660 if (time_limit < 0)
9661 goto theend;
9662 }
9663#endif
9664 }
9665 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009666
9667 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
9668 match_pos, lnum_stop, time_limit);
9669
9670theend:
9671 p_ws = save_p_ws;
9672
9673 return retval;
9674}
9675
9676/*
9677 * "searchpair()" function
9678 */
9679 static void
9680f_searchpair(typval_T *argvars, typval_T *rettv)
9681{
9682 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
9683}
9684
9685/*
9686 * "searchpairpos()" function
9687 */
9688 static void
9689f_searchpairpos(typval_T *argvars, typval_T *rettv)
9690{
9691 pos_T match_pos;
9692 int lnum = 0;
9693 int col = 0;
9694
9695 if (rettv_list_alloc(rettv) == FAIL)
9696 return;
9697
9698 if (searchpair_cmn(argvars, &match_pos) > 0)
9699 {
9700 lnum = match_pos.lnum;
9701 col = match_pos.col;
9702 }
9703
9704 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9705 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9706}
9707
9708/*
9709 * Search for a start/middle/end thing.
9710 * Used by searchpair(), see its documentation for the details.
9711 * Returns 0 or -1 for no match,
9712 */
9713 long
9714do_searchpair(
9715 char_u *spat, /* start pattern */
9716 char_u *mpat, /* middle pattern */
9717 char_u *epat, /* end pattern */
9718 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +01009719 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009720 int flags, /* SP_SETPCMARK and other SP_ values */
9721 pos_T *match_pos,
9722 linenr_T lnum_stop, /* stop at this line if not zero */
9723 long time_limit UNUSED) /* stop after this many msec */
9724{
9725 char_u *save_cpo;
9726 char_u *pat, *pat2 = NULL, *pat3 = NULL;
9727 long retval = 0;
9728 pos_T pos;
9729 pos_T firstpos;
9730 pos_T foundpos;
9731 pos_T save_cursor;
9732 pos_T save_pos;
9733 int n;
9734 int r;
9735 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01009736 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009737 int err;
9738 int options = SEARCH_KEEP;
9739 proftime_T tm;
9740
9741 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
9742 save_cpo = p_cpo;
9743 p_cpo = empty_option;
9744
9745#ifdef FEAT_RELTIME
9746 /* Set the time limit, if there is one. */
9747 profile_setlimit(time_limit, &tm);
9748#endif
9749
9750 /* Make two search patterns: start/end (pat2, for in nested pairs) and
9751 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009752 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
9753 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009754 if (pat2 == NULL || pat3 == NULL)
9755 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009756 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009757 if (*mpat == NUL)
9758 STRCPY(pat3, pat2);
9759 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009760 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009761 spat, epat, mpat);
9762 if (flags & SP_START)
9763 options |= SEARCH_START;
9764
Bram Moolenaar48570482017-10-30 21:48:41 +01009765 if (skip != NULL)
9766 {
9767 /* Empty string means to not use the skip expression. */
9768 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
9769 use_skip = skip->vval.v_string != NULL
9770 && *skip->vval.v_string != NUL;
9771 }
9772
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009773 save_cursor = curwin->w_cursor;
9774 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009775 CLEAR_POS(&firstpos);
9776 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009777 pat = pat3;
9778 for (;;)
9779 {
9780 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009781 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009782 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009783 /* didn't find it or found the first match again: FAIL */
9784 break;
9785
9786 if (firstpos.lnum == 0)
9787 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009788 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009789 {
9790 /* Found the same position again. Can happen with a pattern that
9791 * has "\zs" at the end and searching backwards. Advance one
9792 * character and try again. */
9793 if (dir == BACKWARD)
9794 decl(&pos);
9795 else
9796 incl(&pos);
9797 }
9798 foundpos = pos;
9799
9800 /* clear the start flag to avoid getting stuck here */
9801 options &= ~SEARCH_START;
9802
9803 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +01009804 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009805 {
9806 save_pos = curwin->w_cursor;
9807 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01009808 err = FALSE;
9809 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009810 curwin->w_cursor = save_pos;
9811 if (err)
9812 {
9813 /* Evaluating {skip} caused an error, break here. */
9814 curwin->w_cursor = save_cursor;
9815 retval = -1;
9816 break;
9817 }
9818 if (r)
9819 continue;
9820 }
9821
9822 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
9823 {
9824 /* Found end when searching backwards or start when searching
9825 * forward: nested pair. */
9826 ++nest;
9827 pat = pat2; /* nested, don't search for middle */
9828 }
9829 else
9830 {
9831 /* Found end when searching forward or start when searching
9832 * backward: end of (nested) pair; or found middle in outer pair. */
9833 if (--nest == 1)
9834 pat = pat3; /* outer level, search for middle */
9835 }
9836
9837 if (nest == 0)
9838 {
9839 /* Found the match: return matchcount or line number. */
9840 if (flags & SP_RETCOUNT)
9841 ++retval;
9842 else
9843 retval = pos.lnum;
9844 if (flags & SP_SETPCMARK)
9845 setpcmark();
9846 curwin->w_cursor = pos;
9847 if (!(flags & SP_REPEAT))
9848 break;
9849 nest = 1; /* search for next unmatched */
9850 }
9851 }
9852
9853 if (match_pos != NULL)
9854 {
9855 /* Store the match cursor position */
9856 match_pos->lnum = curwin->w_cursor.lnum;
9857 match_pos->col = curwin->w_cursor.col + 1;
9858 }
9859
9860 /* If 'n' flag is used or search failed: restore cursor position. */
9861 if ((flags & SP_NOMOVE) || retval == 0)
9862 curwin->w_cursor = save_cursor;
9863
9864theend:
9865 vim_free(pat2);
9866 vim_free(pat3);
9867 if (p_cpo == empty_option)
9868 p_cpo = save_cpo;
9869 else
9870 /* Darn, evaluating the {skip} expression changed the value. */
9871 free_string_option(save_cpo);
9872
9873 return retval;
9874}
9875
9876/*
9877 * "searchpos()" function
9878 */
9879 static void
9880f_searchpos(typval_T *argvars, typval_T *rettv)
9881{
9882 pos_T match_pos;
9883 int lnum = 0;
9884 int col = 0;
9885 int n;
9886 int flags = 0;
9887
9888 if (rettv_list_alloc(rettv) == FAIL)
9889 return;
9890
9891 n = search_cmn(argvars, &match_pos, &flags);
9892 if (n > 0)
9893 {
9894 lnum = match_pos.lnum;
9895 col = match_pos.col;
9896 }
9897
9898 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9899 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9900 if (flags & SP_SUBPAT)
9901 list_append_number(rettv->vval.v_list, (varnumber_T)n);
9902}
9903
9904 static void
9905f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
9906{
9907#ifdef FEAT_CLIENTSERVER
9908 char_u buf[NUMBUFLEN];
9909 char_u *server = get_tv_string_chk(&argvars[0]);
9910 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
9911
9912 rettv->vval.v_number = -1;
9913 if (server == NULL || reply == NULL)
9914 return;
9915 if (check_restricted() || check_secure())
9916 return;
9917# ifdef FEAT_X11
9918 if (check_connection() == FAIL)
9919 return;
9920# endif
9921
9922 if (serverSendReply(server, reply) < 0)
9923 {
9924 EMSG(_("E258: Unable to send to client"));
9925 return;
9926 }
9927 rettv->vval.v_number = 0;
9928#else
9929 rettv->vval.v_number = -1;
9930#endif
9931}
9932
9933 static void
9934f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
9935{
9936 char_u *r = NULL;
9937
9938#ifdef FEAT_CLIENTSERVER
9939# ifdef WIN32
9940 r = serverGetVimNames();
9941# else
9942 make_connection();
9943 if (X_DISPLAY != NULL)
9944 r = serverGetVimNames(X_DISPLAY);
9945# endif
9946#endif
9947 rettv->v_type = VAR_STRING;
9948 rettv->vval.v_string = r;
9949}
9950
9951/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +02009952 * Set line or list of lines in buffer "buf".
9953 */
9954 static void
9955set_buffer_lines(buf_T *buf, linenr_T lnum, typval_T *lines, typval_T *rettv)
9956{
9957 char_u *line = NULL;
9958 list_T *l = NULL;
9959 listitem_T *li = NULL;
9960 long added = 0;
9961 linenr_T lcount;
Bram Moolenaar0c4dc882017-11-06 21:32:54 +01009962 buf_T *curbuf_save = NULL;
9963 win_T *curwin_save = NULL;
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +02009964 int is_curbuf = buf == curbuf;
9965
Bram Moolenaar9d954202017-09-04 20:34:19 +02009966 /* When using the current buffer ml_mfp will be set if needed. Useful when
9967 * setline() is used on startup. For other buffers the buffer must be
9968 * loaded. */
9969 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +02009970 {
9971 rettv->vval.v_number = 1; /* FAIL */
9972 return;
9973 }
9974
Bram Moolenaar0c4dc882017-11-06 21:32:54 +01009975 if (!is_curbuf)
9976 {
9977 wininfo_T *wip;
9978
9979 curbuf_save = curbuf;
9980 curwin_save = curwin;
9981 curbuf = buf;
9982 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
9983 {
9984 if (wip->wi_win != NULL)
9985 {
9986 curwin = wip->wi_win;
9987 break;
9988 }
9989 }
9990 }
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +02009991
9992 lcount = curbuf->b_ml.ml_line_count;
9993
9994 if (lines->v_type == VAR_LIST)
9995 {
9996 l = lines->vval.v_list;
9997 li = l->lv_first;
9998 }
9999 else
10000 line = get_tv_string_chk(lines);
10001
10002 /* default result is zero == OK */
10003 for (;;)
10004 {
10005 if (l != NULL)
10006 {
10007 /* list argument, get next string */
10008 if (li == NULL)
10009 break;
10010 line = get_tv_string_chk(&li->li_tv);
10011 li = li->li_next;
10012 }
10013
10014 rettv->vval.v_number = 1; /* FAIL */
10015 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
10016 break;
10017
10018 /* When coming here from Insert mode, sync undo, so that this can be
10019 * undone separately from what was previously inserted. */
10020 if (u_sync_once == 2)
10021 {
10022 u_sync_once = 1; /* notify that u_sync() was called */
10023 u_sync(TRUE);
10024 }
10025
10026 if (lnum <= curbuf->b_ml.ml_line_count)
10027 {
10028 /* existing line, replace it */
10029 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
10030 {
10031 changed_bytes(lnum, 0);
10032 if (is_curbuf && lnum == curwin->w_cursor.lnum)
10033 check_cursor_col();
10034 rettv->vval.v_number = 0; /* OK */
10035 }
10036 }
10037 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
10038 {
10039 /* lnum is one past the last line, append the line */
10040 ++added;
10041 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
10042 rettv->vval.v_number = 0; /* OK */
10043 }
10044
10045 if (l == NULL) /* only one string argument */
10046 break;
10047 ++lnum;
10048 }
10049
10050 if (added > 0)
10051 appended_lines_mark(lcount, added);
10052
Bram Moolenaar0c4dc882017-11-06 21:32:54 +010010053 if (!is_curbuf)
10054 {
10055 curbuf = curbuf_save;
10056 curwin = curwin_save;
10057 }
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010058}
10059
10060/*
10061 * "setbufline()" function
10062 */
10063 static void
10064f_setbufline(argvars, rettv)
10065 typval_T *argvars;
10066 typval_T *rettv;
10067{
10068 linenr_T lnum;
10069 buf_T *buf;
10070
10071 buf = get_buf_tv(&argvars[0], FALSE);
10072 if (buf == NULL)
10073 rettv->vval.v_number = 1; /* FAIL */
10074 else
10075 {
10076 lnum = get_tv_lnum_buf(&argvars[1], buf);
10077
10078 set_buffer_lines(buf, lnum, &argvars[2], rettv);
10079 }
10080}
10081
10082/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010083 * "setbufvar()" function
10084 */
10085 static void
10086f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10087{
10088 buf_T *buf;
10089 char_u *varname, *bufvarname;
10090 typval_T *varp;
10091 char_u nbuf[NUMBUFLEN];
10092
10093 if (check_restricted() || check_secure())
10094 return;
10095 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10096 varname = get_tv_string_chk(&argvars[1]);
10097 buf = get_buf_tv(&argvars[0], FALSE);
10098 varp = &argvars[2];
10099
10100 if (buf != NULL && varname != NULL && varp != NULL)
10101 {
10102 if (*varname == '&')
10103 {
10104 long numval;
10105 char_u *strval;
10106 int error = FALSE;
10107 aco_save_T aco;
10108
10109 /* set curbuf to be our buf, temporarily */
10110 aucmd_prepbuf(&aco, buf);
10111
10112 ++varname;
10113 numval = (long)get_tv_number_chk(varp, &error);
10114 strval = get_tv_string_buf_chk(varp, nbuf);
10115 if (!error && strval != NULL)
10116 set_option_value(varname, numval, strval, OPT_LOCAL);
10117
10118 /* reset notion of buffer */
10119 aucmd_restbuf(&aco);
10120 }
10121 else
10122 {
10123 buf_T *save_curbuf = curbuf;
10124
10125 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
10126 if (bufvarname != NULL)
10127 {
10128 curbuf = buf;
10129 STRCPY(bufvarname, "b:");
10130 STRCPY(bufvarname + 2, varname);
10131 set_var(bufvarname, varp, TRUE);
10132 vim_free(bufvarname);
10133 curbuf = save_curbuf;
10134 }
10135 }
10136 }
10137}
10138
10139 static void
10140f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10141{
10142 dict_T *d;
10143 dictitem_T *di;
10144 char_u *csearch;
10145
10146 if (argvars[0].v_type != VAR_DICT)
10147 {
10148 EMSG(_(e_dictreq));
10149 return;
10150 }
10151
10152 if ((d = argvars[0].vval.v_dict) != NULL)
10153 {
10154 csearch = get_dict_string(d, (char_u *)"char", FALSE);
10155 if (csearch != NULL)
10156 {
10157#ifdef FEAT_MBYTE
10158 if (enc_utf8)
10159 {
10160 int pcc[MAX_MCO];
10161 int c = utfc_ptr2char(csearch, pcc);
10162
10163 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10164 }
10165 else
10166#endif
10167 set_last_csearch(PTR2CHAR(csearch),
10168 csearch, MB_PTR2LEN(csearch));
10169 }
10170
10171 di = dict_find(d, (char_u *)"forward", -1);
10172 if (di != NULL)
10173 set_csearch_direction((int)get_tv_number(&di->di_tv)
10174 ? FORWARD : BACKWARD);
10175
10176 di = dict_find(d, (char_u *)"until", -1);
10177 if (di != NULL)
10178 set_csearch_until(!!get_tv_number(&di->di_tv));
10179 }
10180}
10181
10182/*
10183 * "setcmdpos()" function
10184 */
10185 static void
10186f_setcmdpos(typval_T *argvars, typval_T *rettv)
10187{
10188 int pos = (int)get_tv_number(&argvars[0]) - 1;
10189
10190 if (pos >= 0)
10191 rettv->vval.v_number = set_cmdline_pos(pos);
10192}
10193
10194/*
10195 * "setfperm({fname}, {mode})" function
10196 */
10197 static void
10198f_setfperm(typval_T *argvars, typval_T *rettv)
10199{
10200 char_u *fname;
10201 char_u modebuf[NUMBUFLEN];
10202 char_u *mode_str;
10203 int i;
10204 int mask;
10205 int mode = 0;
10206
10207 rettv->vval.v_number = 0;
10208 fname = get_tv_string_chk(&argvars[0]);
10209 if (fname == NULL)
10210 return;
10211 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
10212 if (mode_str == NULL)
10213 return;
10214 if (STRLEN(mode_str) != 9)
10215 {
10216 EMSG2(_(e_invarg2), mode_str);
10217 return;
10218 }
10219
10220 mask = 1;
10221 for (i = 8; i >= 0; --i)
10222 {
10223 if (mode_str[i] != '-')
10224 mode |= mask;
10225 mask = mask << 1;
10226 }
10227 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10228}
10229
10230/*
10231 * "setline()" function
10232 */
10233 static void
10234f_setline(typval_T *argvars, typval_T *rettv)
10235{
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010236 linenr_T lnum = get_tv_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010237
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010238 set_buffer_lines(curbuf, lnum, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010239}
10240
Bram Moolenaard823fa92016-08-12 16:29:27 +020010241static 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 +020010242
10243/*
10244 * Used by "setqflist()" and "setloclist()" functions
10245 */
10246 static void
10247set_qf_ll_list(
10248 win_T *wp UNUSED,
10249 typval_T *list_arg UNUSED,
10250 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010251 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010252 typval_T *rettv)
10253{
10254#ifdef FEAT_QUICKFIX
10255 static char *e_invact = N_("E927: Invalid action: '%s'");
10256 char_u *act;
10257 int action = 0;
10258#endif
10259
10260 rettv->vval.v_number = -1;
10261
10262#ifdef FEAT_QUICKFIX
10263 if (list_arg->v_type != VAR_LIST)
10264 EMSG(_(e_listreq));
10265 else
10266 {
10267 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010268 dict_T *d = NULL;
10269 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010270
10271 if (action_arg->v_type == VAR_STRING)
10272 {
10273 act = get_tv_string_chk(action_arg);
10274 if (act == NULL)
10275 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020010276 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
10277 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010278 action = *act;
10279 else
10280 EMSG2(_(e_invact), act);
10281 }
10282 else if (action_arg->v_type == VAR_UNKNOWN)
10283 action = ' ';
10284 else
10285 EMSG(_(e_stringreq));
10286
Bram Moolenaard823fa92016-08-12 16:29:27 +020010287 if (action_arg->v_type != VAR_UNKNOWN
10288 && what_arg->v_type != VAR_UNKNOWN)
10289 {
10290 if (what_arg->v_type == VAR_DICT)
10291 d = what_arg->vval.v_dict;
10292 else
10293 {
10294 EMSG(_(e_dictreq));
10295 valid_dict = FALSE;
10296 }
10297 }
10298
10299 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
10300 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()"), d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010301 rettv->vval.v_number = 0;
10302 }
10303#endif
10304}
10305
10306/*
10307 * "setloclist()" function
10308 */
10309 static void
10310f_setloclist(typval_T *argvars, typval_T *rettv)
10311{
10312 win_T *win;
10313
10314 rettv->vval.v_number = -1;
10315
10316 win = find_win_by_nr(&argvars[0], NULL);
10317 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020010318 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010319}
10320
10321/*
10322 * "setmatches()" function
10323 */
10324 static void
10325f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10326{
10327#ifdef FEAT_SEARCH_EXTRA
10328 list_T *l;
10329 listitem_T *li;
10330 dict_T *d;
10331 list_T *s = NULL;
10332
10333 rettv->vval.v_number = -1;
10334 if (argvars[0].v_type != VAR_LIST)
10335 {
10336 EMSG(_(e_listreq));
10337 return;
10338 }
10339 if ((l = argvars[0].vval.v_list) != NULL)
10340 {
10341
10342 /* To some extent make sure that we are dealing with a list from
10343 * "getmatches()". */
10344 li = l->lv_first;
10345 while (li != NULL)
10346 {
10347 if (li->li_tv.v_type != VAR_DICT
10348 || (d = li->li_tv.vval.v_dict) == NULL)
10349 {
10350 EMSG(_(e_invarg));
10351 return;
10352 }
10353 if (!(dict_find(d, (char_u *)"group", -1) != NULL
10354 && (dict_find(d, (char_u *)"pattern", -1) != NULL
10355 || dict_find(d, (char_u *)"pos1", -1) != NULL)
10356 && dict_find(d, (char_u *)"priority", -1) != NULL
10357 && dict_find(d, (char_u *)"id", -1) != NULL))
10358 {
10359 EMSG(_(e_invarg));
10360 return;
10361 }
10362 li = li->li_next;
10363 }
10364
10365 clear_matches(curwin);
10366 li = l->lv_first;
10367 while (li != NULL)
10368 {
10369 int i = 0;
10370 char_u buf[5];
10371 dictitem_T *di;
10372 char_u *group;
10373 int priority;
10374 int id;
10375 char_u *conceal;
10376
10377 d = li->li_tv.vval.v_dict;
10378 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
10379 {
10380 if (s == NULL)
10381 {
10382 s = list_alloc();
10383 if (s == NULL)
10384 return;
10385 }
10386
10387 /* match from matchaddpos() */
10388 for (i = 1; i < 9; i++)
10389 {
10390 sprintf((char *)buf, (char *)"pos%d", i);
10391 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
10392 {
10393 if (di->di_tv.v_type != VAR_LIST)
10394 return;
10395
10396 list_append_tv(s, &di->di_tv);
10397 s->lv_refcount++;
10398 }
10399 else
10400 break;
10401 }
10402 }
10403
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010404 group = get_dict_string(d, (char_u *)"group", TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010405 priority = (int)get_dict_number(d, (char_u *)"priority");
10406 id = (int)get_dict_number(d, (char_u *)"id");
10407 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010408 ? get_dict_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010409 : NULL;
10410 if (i == 0)
10411 {
10412 match_add(curwin, group,
10413 get_dict_string(d, (char_u *)"pattern", FALSE),
10414 priority, id, NULL, conceal);
10415 }
10416 else
10417 {
10418 match_add(curwin, group, NULL, priority, id, s, conceal);
10419 list_unref(s);
10420 s = NULL;
10421 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010422 vim_free(group);
10423 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010424
10425 li = li->li_next;
10426 }
10427 rettv->vval.v_number = 0;
10428 }
10429#endif
10430}
10431
10432/*
10433 * "setpos()" function
10434 */
10435 static void
10436f_setpos(typval_T *argvars, typval_T *rettv)
10437{
10438 pos_T pos;
10439 int fnum;
10440 char_u *name;
10441 colnr_T curswant = -1;
10442
10443 rettv->vval.v_number = -1;
10444 name = get_tv_string_chk(argvars);
10445 if (name != NULL)
10446 {
10447 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10448 {
10449 if (--pos.col < 0)
10450 pos.col = 0;
10451 if (name[0] == '.' && name[1] == NUL)
10452 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010453 /* set cursor; "fnum" is ignored */
10454 curwin->w_cursor = pos;
10455 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010456 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010457 curwin->w_curswant = curswant - 1;
10458 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010459 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010460 check_cursor();
10461 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010462 }
10463 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10464 {
10465 /* set mark */
10466 if (setmark_pos(name[1], &pos, fnum) == OK)
10467 rettv->vval.v_number = 0;
10468 }
10469 else
10470 EMSG(_(e_invarg));
10471 }
10472 }
10473}
10474
10475/*
10476 * "setqflist()" function
10477 */
10478 static void
10479f_setqflist(typval_T *argvars, typval_T *rettv)
10480{
Bram Moolenaard823fa92016-08-12 16:29:27 +020010481 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010482}
10483
10484/*
10485 * "setreg()" function
10486 */
10487 static void
10488f_setreg(typval_T *argvars, typval_T *rettv)
10489{
10490 int regname;
10491 char_u *strregname;
10492 char_u *stropt;
10493 char_u *strval;
10494 int append;
10495 char_u yank_type;
10496 long block_len;
10497
10498 block_len = -1;
10499 yank_type = MAUTO;
10500 append = FALSE;
10501
10502 strregname = get_tv_string_chk(argvars);
10503 rettv->vval.v_number = 1; /* FAIL is default */
10504
10505 if (strregname == NULL)
10506 return; /* type error; errmsg already given */
10507 regname = *strregname;
10508 if (regname == 0 || regname == '@')
10509 regname = '"';
10510
10511 if (argvars[2].v_type != VAR_UNKNOWN)
10512 {
10513 stropt = get_tv_string_chk(&argvars[2]);
10514 if (stropt == NULL)
10515 return; /* type error */
10516 for (; *stropt != NUL; ++stropt)
10517 switch (*stropt)
10518 {
10519 case 'a': case 'A': /* append */
10520 append = TRUE;
10521 break;
10522 case 'v': case 'c': /* character-wise selection */
10523 yank_type = MCHAR;
10524 break;
10525 case 'V': case 'l': /* line-wise selection */
10526 yank_type = MLINE;
10527 break;
10528 case 'b': case Ctrl_V: /* block-wise selection */
10529 yank_type = MBLOCK;
10530 if (VIM_ISDIGIT(stropt[1]))
10531 {
10532 ++stropt;
10533 block_len = getdigits(&stropt) - 1;
10534 --stropt;
10535 }
10536 break;
10537 }
10538 }
10539
10540 if (argvars[1].v_type == VAR_LIST)
10541 {
10542 char_u **lstval;
10543 char_u **allocval;
10544 char_u buf[NUMBUFLEN];
10545 char_u **curval;
10546 char_u **curallocval;
10547 list_T *ll = argvars[1].vval.v_list;
10548 listitem_T *li;
10549 int len;
10550
10551 /* If the list is NULL handle like an empty list. */
10552 len = ll == NULL ? 0 : ll->lv_len;
10553
10554 /* First half: use for pointers to result lines; second half: use for
10555 * pointers to allocated copies. */
10556 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
10557 if (lstval == NULL)
10558 return;
10559 curval = lstval;
10560 allocval = lstval + len + 2;
10561 curallocval = allocval;
10562
10563 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
10564 li = li->li_next)
10565 {
10566 strval = get_tv_string_buf_chk(&li->li_tv, buf);
10567 if (strval == NULL)
10568 goto free_lstval;
10569 if (strval == buf)
10570 {
10571 /* Need to make a copy, next get_tv_string_buf_chk() will
10572 * overwrite the string. */
10573 strval = vim_strsave(buf);
10574 if (strval == NULL)
10575 goto free_lstval;
10576 *curallocval++ = strval;
10577 }
10578 *curval++ = strval;
10579 }
10580 *curval++ = NULL;
10581
10582 write_reg_contents_lst(regname, lstval, -1,
10583 append, yank_type, block_len);
10584free_lstval:
10585 while (curallocval > allocval)
10586 vim_free(*--curallocval);
10587 vim_free(lstval);
10588 }
10589 else
10590 {
10591 strval = get_tv_string_chk(&argvars[1]);
10592 if (strval == NULL)
10593 return;
10594 write_reg_contents_ex(regname, strval, -1,
10595 append, yank_type, block_len);
10596 }
10597 rettv->vval.v_number = 0;
10598}
10599
10600/*
10601 * "settabvar()" function
10602 */
10603 static void
10604f_settabvar(typval_T *argvars, typval_T *rettv)
10605{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010606 tabpage_T *save_curtab;
10607 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010608 char_u *varname, *tabvarname;
10609 typval_T *varp;
10610
10611 rettv->vval.v_number = 0;
10612
10613 if (check_restricted() || check_secure())
10614 return;
10615
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010616 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010617 varname = get_tv_string_chk(&argvars[1]);
10618 varp = &argvars[2];
10619
Bram Moolenaar4033c552017-09-16 20:54:51 +020010620 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010621 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010622 save_curtab = curtab;
10623 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010624
10625 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
10626 if (tabvarname != NULL)
10627 {
10628 STRCPY(tabvarname, "t:");
10629 STRCPY(tabvarname + 2, varname);
10630 set_var(tabvarname, varp, TRUE);
10631 vim_free(tabvarname);
10632 }
10633
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010634 /* Restore current tabpage */
10635 if (valid_tabpage(save_curtab))
10636 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010637 }
10638}
10639
10640/*
10641 * "settabwinvar()" function
10642 */
10643 static void
10644f_settabwinvar(typval_T *argvars, typval_T *rettv)
10645{
10646 setwinvar(argvars, rettv, 1);
10647}
10648
10649/*
10650 * "setwinvar()" function
10651 */
10652 static void
10653f_setwinvar(typval_T *argvars, typval_T *rettv)
10654{
10655 setwinvar(argvars, rettv, 0);
10656}
10657
10658#ifdef FEAT_CRYPT
10659/*
10660 * "sha256({string})" function
10661 */
10662 static void
10663f_sha256(typval_T *argvars, typval_T *rettv)
10664{
10665 char_u *p;
10666
10667 p = get_tv_string(&argvars[0]);
10668 rettv->vval.v_string = vim_strsave(
10669 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
10670 rettv->v_type = VAR_STRING;
10671}
10672#endif /* FEAT_CRYPT */
10673
10674/*
10675 * "shellescape({string})" function
10676 */
10677 static void
10678f_shellescape(typval_T *argvars, typval_T *rettv)
10679{
Bram Moolenaar20615522017-06-05 18:46:26 +020010680 int do_special = non_zero_arg(&argvars[1]);
10681
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010682 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar20615522017-06-05 18:46:26 +020010683 get_tv_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010684 rettv->v_type = VAR_STRING;
10685}
10686
10687/*
10688 * shiftwidth() function
10689 */
10690 static void
10691f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
10692{
10693 rettv->vval.v_number = get_sw_value(curbuf);
10694}
10695
10696/*
10697 * "simplify()" function
10698 */
10699 static void
10700f_simplify(typval_T *argvars, typval_T *rettv)
10701{
10702 char_u *p;
10703
10704 p = get_tv_string(&argvars[0]);
10705 rettv->vval.v_string = vim_strsave(p);
10706 simplify_filename(rettv->vval.v_string); /* simplify in place */
10707 rettv->v_type = VAR_STRING;
10708}
10709
10710#ifdef FEAT_FLOAT
10711/*
10712 * "sin()" function
10713 */
10714 static void
10715f_sin(typval_T *argvars, typval_T *rettv)
10716{
10717 float_T f = 0.0;
10718
10719 rettv->v_type = VAR_FLOAT;
10720 if (get_float_arg(argvars, &f) == OK)
10721 rettv->vval.v_float = sin(f);
10722 else
10723 rettv->vval.v_float = 0.0;
10724}
10725
10726/*
10727 * "sinh()" function
10728 */
10729 static void
10730f_sinh(typval_T *argvars, typval_T *rettv)
10731{
10732 float_T f = 0.0;
10733
10734 rettv->v_type = VAR_FLOAT;
10735 if (get_float_arg(argvars, &f) == OK)
10736 rettv->vval.v_float = sinh(f);
10737 else
10738 rettv->vval.v_float = 0.0;
10739}
10740#endif
10741
10742static int
10743#ifdef __BORLANDC__
10744 _RTLENTRYF
10745#endif
10746 item_compare(const void *s1, const void *s2);
10747static int
10748#ifdef __BORLANDC__
10749 _RTLENTRYF
10750#endif
10751 item_compare2(const void *s1, const void *s2);
10752
10753/* struct used in the array that's given to qsort() */
10754typedef struct
10755{
10756 listitem_T *item;
10757 int idx;
10758} sortItem_T;
10759
10760/* struct storing information about current sort */
10761typedef struct
10762{
10763 int item_compare_ic;
10764 int item_compare_numeric;
10765 int item_compare_numbers;
10766#ifdef FEAT_FLOAT
10767 int item_compare_float;
10768#endif
10769 char_u *item_compare_func;
10770 partial_T *item_compare_partial;
10771 dict_T *item_compare_selfdict;
10772 int item_compare_func_err;
10773 int item_compare_keep_zero;
10774} sortinfo_T;
10775static sortinfo_T *sortinfo = NULL;
10776static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
10777#define ITEM_COMPARE_FAIL 999
10778
10779/*
10780 * Compare functions for f_sort() and f_uniq() below.
10781 */
10782 static int
10783#ifdef __BORLANDC__
10784_RTLENTRYF
10785#endif
10786item_compare(const void *s1, const void *s2)
10787{
10788 sortItem_T *si1, *si2;
10789 typval_T *tv1, *tv2;
10790 char_u *p1, *p2;
10791 char_u *tofree1 = NULL, *tofree2 = NULL;
10792 int res;
10793 char_u numbuf1[NUMBUFLEN];
10794 char_u numbuf2[NUMBUFLEN];
10795
10796 si1 = (sortItem_T *)s1;
10797 si2 = (sortItem_T *)s2;
10798 tv1 = &si1->item->li_tv;
10799 tv2 = &si2->item->li_tv;
10800
10801 if (sortinfo->item_compare_numbers)
10802 {
10803 varnumber_T v1 = get_tv_number(tv1);
10804 varnumber_T v2 = get_tv_number(tv2);
10805
10806 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10807 }
10808
10809#ifdef FEAT_FLOAT
10810 if (sortinfo->item_compare_float)
10811 {
10812 float_T v1 = get_tv_float(tv1);
10813 float_T v2 = get_tv_float(tv2);
10814
10815 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10816 }
10817#endif
10818
10819 /* tv2string() puts quotes around a string and allocates memory. Don't do
10820 * that for string variables. Use a single quote when comparing with a
10821 * non-string to do what the docs promise. */
10822 if (tv1->v_type == VAR_STRING)
10823 {
10824 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
10825 p1 = (char_u *)"'";
10826 else
10827 p1 = tv1->vval.v_string;
10828 }
10829 else
10830 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
10831 if (tv2->v_type == VAR_STRING)
10832 {
10833 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
10834 p2 = (char_u *)"'";
10835 else
10836 p2 = tv2->vval.v_string;
10837 }
10838 else
10839 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
10840 if (p1 == NULL)
10841 p1 = (char_u *)"";
10842 if (p2 == NULL)
10843 p2 = (char_u *)"";
10844 if (!sortinfo->item_compare_numeric)
10845 {
10846 if (sortinfo->item_compare_ic)
10847 res = STRICMP(p1, p2);
10848 else
10849 res = STRCMP(p1, p2);
10850 }
10851 else
10852 {
10853 double n1, n2;
10854 n1 = strtod((char *)p1, (char **)&p1);
10855 n2 = strtod((char *)p2, (char **)&p2);
10856 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
10857 }
10858
10859 /* When the result would be zero, compare the item indexes. Makes the
10860 * sort stable. */
10861 if (res == 0 && !sortinfo->item_compare_keep_zero)
10862 res = si1->idx > si2->idx ? 1 : -1;
10863
10864 vim_free(tofree1);
10865 vim_free(tofree2);
10866 return res;
10867}
10868
10869 static int
10870#ifdef __BORLANDC__
10871_RTLENTRYF
10872#endif
10873item_compare2(const void *s1, const void *s2)
10874{
10875 sortItem_T *si1, *si2;
10876 int res;
10877 typval_T rettv;
10878 typval_T argv[3];
10879 int dummy;
10880 char_u *func_name;
10881 partial_T *partial = sortinfo->item_compare_partial;
10882
10883 /* shortcut after failure in previous call; compare all items equal */
10884 if (sortinfo->item_compare_func_err)
10885 return 0;
10886
10887 si1 = (sortItem_T *)s1;
10888 si2 = (sortItem_T *)s2;
10889
10890 if (partial == NULL)
10891 func_name = sortinfo->item_compare_func;
10892 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020010893 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010894
10895 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
10896 * in the copy without changing the original list items. */
10897 copy_tv(&si1->item->li_tv, &argv[0]);
10898 copy_tv(&si2->item->li_tv, &argv[1]);
10899
10900 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
10901 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020010902 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010903 partial, sortinfo->item_compare_selfdict);
10904 clear_tv(&argv[0]);
10905 clear_tv(&argv[1]);
10906
10907 if (res == FAIL)
10908 res = ITEM_COMPARE_FAIL;
10909 else
10910 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
10911 if (sortinfo->item_compare_func_err)
10912 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
10913 clear_tv(&rettv);
10914
10915 /* When the result would be zero, compare the pointers themselves. Makes
10916 * the sort stable. */
10917 if (res == 0 && !sortinfo->item_compare_keep_zero)
10918 res = si1->idx > si2->idx ? 1 : -1;
10919
10920 return res;
10921}
10922
10923/*
10924 * "sort({list})" function
10925 */
10926 static void
10927do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
10928{
10929 list_T *l;
10930 listitem_T *li;
10931 sortItem_T *ptrs;
10932 sortinfo_T *old_sortinfo;
10933 sortinfo_T info;
10934 long len;
10935 long i;
10936
10937 /* Pointer to current info struct used in compare function. Save and
10938 * restore the current one for nested calls. */
10939 old_sortinfo = sortinfo;
10940 sortinfo = &info;
10941
10942 if (argvars[0].v_type != VAR_LIST)
10943 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
10944 else
10945 {
10946 l = argvars[0].vval.v_list;
10947 if (l == NULL || tv_check_lock(l->lv_lock,
10948 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
10949 TRUE))
10950 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020010951 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010952
10953 len = list_len(l);
10954 if (len <= 1)
10955 goto theend; /* short list sorts pretty quickly */
10956
10957 info.item_compare_ic = FALSE;
10958 info.item_compare_numeric = FALSE;
10959 info.item_compare_numbers = FALSE;
10960#ifdef FEAT_FLOAT
10961 info.item_compare_float = FALSE;
10962#endif
10963 info.item_compare_func = NULL;
10964 info.item_compare_partial = NULL;
10965 info.item_compare_selfdict = NULL;
10966 if (argvars[1].v_type != VAR_UNKNOWN)
10967 {
10968 /* optional second argument: {func} */
10969 if (argvars[1].v_type == VAR_FUNC)
10970 info.item_compare_func = argvars[1].vval.v_string;
10971 else if (argvars[1].v_type == VAR_PARTIAL)
10972 info.item_compare_partial = argvars[1].vval.v_partial;
10973 else
10974 {
10975 int error = FALSE;
10976
10977 i = (long)get_tv_number_chk(&argvars[1], &error);
10978 if (error)
10979 goto theend; /* type error; errmsg already given */
10980 if (i == 1)
10981 info.item_compare_ic = TRUE;
10982 else if (argvars[1].v_type != VAR_NUMBER)
10983 info.item_compare_func = get_tv_string(&argvars[1]);
10984 else if (i != 0)
10985 {
10986 EMSG(_(e_invarg));
10987 goto theend;
10988 }
10989 if (info.item_compare_func != NULL)
10990 {
10991 if (*info.item_compare_func == NUL)
10992 {
10993 /* empty string means default sort */
10994 info.item_compare_func = NULL;
10995 }
10996 else if (STRCMP(info.item_compare_func, "n") == 0)
10997 {
10998 info.item_compare_func = NULL;
10999 info.item_compare_numeric = TRUE;
11000 }
11001 else if (STRCMP(info.item_compare_func, "N") == 0)
11002 {
11003 info.item_compare_func = NULL;
11004 info.item_compare_numbers = TRUE;
11005 }
11006#ifdef FEAT_FLOAT
11007 else if (STRCMP(info.item_compare_func, "f") == 0)
11008 {
11009 info.item_compare_func = NULL;
11010 info.item_compare_float = TRUE;
11011 }
11012#endif
11013 else if (STRCMP(info.item_compare_func, "i") == 0)
11014 {
11015 info.item_compare_func = NULL;
11016 info.item_compare_ic = TRUE;
11017 }
11018 }
11019 }
11020
11021 if (argvars[2].v_type != VAR_UNKNOWN)
11022 {
11023 /* optional third argument: {dict} */
11024 if (argvars[2].v_type != VAR_DICT)
11025 {
11026 EMSG(_(e_dictreq));
11027 goto theend;
11028 }
11029 info.item_compare_selfdict = argvars[2].vval.v_dict;
11030 }
11031 }
11032
11033 /* Make an array with each entry pointing to an item in the List. */
11034 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
11035 if (ptrs == NULL)
11036 goto theend;
11037
11038 i = 0;
11039 if (sort)
11040 {
11041 /* sort(): ptrs will be the list to sort */
11042 for (li = l->lv_first; li != NULL; li = li->li_next)
11043 {
11044 ptrs[i].item = li;
11045 ptrs[i].idx = i;
11046 ++i;
11047 }
11048
11049 info.item_compare_func_err = FALSE;
11050 info.item_compare_keep_zero = FALSE;
11051 /* test the compare function */
11052 if ((info.item_compare_func != NULL
11053 || info.item_compare_partial != NULL)
11054 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11055 == ITEM_COMPARE_FAIL)
11056 EMSG(_("E702: Sort compare function failed"));
11057 else
11058 {
11059 /* Sort the array with item pointers. */
11060 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
11061 info.item_compare_func == NULL
11062 && info.item_compare_partial == NULL
11063 ? item_compare : item_compare2);
11064
11065 if (!info.item_compare_func_err)
11066 {
11067 /* Clear the List and append the items in sorted order. */
11068 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
11069 l->lv_len = 0;
11070 for (i = 0; i < len; ++i)
11071 list_append(l, ptrs[i].item);
11072 }
11073 }
11074 }
11075 else
11076 {
11077 int (*item_compare_func_ptr)(const void *, const void *);
11078
11079 /* f_uniq(): ptrs will be a stack of items to remove */
11080 info.item_compare_func_err = FALSE;
11081 info.item_compare_keep_zero = TRUE;
11082 item_compare_func_ptr = info.item_compare_func != NULL
11083 || info.item_compare_partial != NULL
11084 ? item_compare2 : item_compare;
11085
11086 for (li = l->lv_first; li != NULL && li->li_next != NULL;
11087 li = li->li_next)
11088 {
11089 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
11090 == 0)
11091 ptrs[i++].item = li;
11092 if (info.item_compare_func_err)
11093 {
11094 EMSG(_("E882: Uniq compare function failed"));
11095 break;
11096 }
11097 }
11098
11099 if (!info.item_compare_func_err)
11100 {
11101 while (--i >= 0)
11102 {
11103 li = ptrs[i].item->li_next;
11104 ptrs[i].item->li_next = li->li_next;
11105 if (li->li_next != NULL)
11106 li->li_next->li_prev = ptrs[i].item;
11107 else
11108 l->lv_last = ptrs[i].item;
11109 list_fix_watch(l, li);
11110 listitem_free(li);
11111 l->lv_len--;
11112 }
11113 }
11114 }
11115
11116 vim_free(ptrs);
11117 }
11118theend:
11119 sortinfo = old_sortinfo;
11120}
11121
11122/*
11123 * "sort({list})" function
11124 */
11125 static void
11126f_sort(typval_T *argvars, typval_T *rettv)
11127{
11128 do_sort_uniq(argvars, rettv, TRUE);
11129}
11130
11131/*
11132 * "uniq({list})" function
11133 */
11134 static void
11135f_uniq(typval_T *argvars, typval_T *rettv)
11136{
11137 do_sort_uniq(argvars, rettv, FALSE);
11138}
11139
11140/*
11141 * "soundfold({word})" function
11142 */
11143 static void
11144f_soundfold(typval_T *argvars, typval_T *rettv)
11145{
11146 char_u *s;
11147
11148 rettv->v_type = VAR_STRING;
11149 s = get_tv_string(&argvars[0]);
11150#ifdef FEAT_SPELL
11151 rettv->vval.v_string = eval_soundfold(s);
11152#else
11153 rettv->vval.v_string = vim_strsave(s);
11154#endif
11155}
11156
11157/*
11158 * "spellbadword()" function
11159 */
11160 static void
11161f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
11162{
11163 char_u *word = (char_u *)"";
11164 hlf_T attr = HLF_COUNT;
11165 int len = 0;
11166
11167 if (rettv_list_alloc(rettv) == FAIL)
11168 return;
11169
11170#ifdef FEAT_SPELL
11171 if (argvars[0].v_type == VAR_UNKNOWN)
11172 {
11173 /* Find the start and length of the badly spelled word. */
11174 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
11175 if (len != 0)
11176 word = ml_get_cursor();
11177 }
11178 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
11179 {
11180 char_u *str = get_tv_string_chk(&argvars[0]);
11181 int capcol = -1;
11182
11183 if (str != NULL)
11184 {
11185 /* Check the argument for spelling. */
11186 while (*str != NUL)
11187 {
11188 len = spell_check(curwin, str, &attr, &capcol, FALSE);
11189 if (attr != HLF_COUNT)
11190 {
11191 word = str;
11192 break;
11193 }
11194 str += len;
11195 }
11196 }
11197 }
11198#endif
11199
11200 list_append_string(rettv->vval.v_list, word, len);
11201 list_append_string(rettv->vval.v_list, (char_u *)(
11202 attr == HLF_SPB ? "bad" :
11203 attr == HLF_SPR ? "rare" :
11204 attr == HLF_SPL ? "local" :
11205 attr == HLF_SPC ? "caps" :
11206 ""), -1);
11207}
11208
11209/*
11210 * "spellsuggest()" function
11211 */
11212 static void
11213f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
11214{
11215#ifdef FEAT_SPELL
11216 char_u *str;
11217 int typeerr = FALSE;
11218 int maxcount;
11219 garray_T ga;
11220 int i;
11221 listitem_T *li;
11222 int need_capital = FALSE;
11223#endif
11224
11225 if (rettv_list_alloc(rettv) == FAIL)
11226 return;
11227
11228#ifdef FEAT_SPELL
11229 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
11230 {
11231 str = get_tv_string(&argvars[0]);
11232 if (argvars[1].v_type != VAR_UNKNOWN)
11233 {
11234 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
11235 if (maxcount <= 0)
11236 return;
11237 if (argvars[2].v_type != VAR_UNKNOWN)
11238 {
11239 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
11240 if (typeerr)
11241 return;
11242 }
11243 }
11244 else
11245 maxcount = 25;
11246
11247 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
11248
11249 for (i = 0; i < ga.ga_len; ++i)
11250 {
11251 str = ((char_u **)ga.ga_data)[i];
11252
11253 li = listitem_alloc();
11254 if (li == NULL)
11255 vim_free(str);
11256 else
11257 {
11258 li->li_tv.v_type = VAR_STRING;
11259 li->li_tv.v_lock = 0;
11260 li->li_tv.vval.v_string = str;
11261 list_append(rettv->vval.v_list, li);
11262 }
11263 }
11264 ga_clear(&ga);
11265 }
11266#endif
11267}
11268
11269 static void
11270f_split(typval_T *argvars, typval_T *rettv)
11271{
11272 char_u *str;
11273 char_u *end;
11274 char_u *pat = NULL;
11275 regmatch_T regmatch;
11276 char_u patbuf[NUMBUFLEN];
11277 char_u *save_cpo;
11278 int match;
11279 colnr_T col = 0;
11280 int keepempty = FALSE;
11281 int typeerr = FALSE;
11282
11283 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11284 save_cpo = p_cpo;
11285 p_cpo = (char_u *)"";
11286
11287 str = get_tv_string(&argvars[0]);
11288 if (argvars[1].v_type != VAR_UNKNOWN)
11289 {
11290 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11291 if (pat == NULL)
11292 typeerr = TRUE;
11293 if (argvars[2].v_type != VAR_UNKNOWN)
11294 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
11295 }
11296 if (pat == NULL || *pat == NUL)
11297 pat = (char_u *)"[\\x01- ]\\+";
11298
11299 if (rettv_list_alloc(rettv) == FAIL)
11300 return;
11301 if (typeerr)
11302 return;
11303
11304 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11305 if (regmatch.regprog != NULL)
11306 {
11307 regmatch.rm_ic = FALSE;
11308 while (*str != NUL || keepempty)
11309 {
11310 if (*str == NUL)
11311 match = FALSE; /* empty item at the end */
11312 else
11313 match = vim_regexec_nl(&regmatch, str, col);
11314 if (match)
11315 end = regmatch.startp[0];
11316 else
11317 end = str + STRLEN(str);
11318 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
11319 && *str != NUL && match && end < regmatch.endp[0]))
11320 {
11321 if (list_append_string(rettv->vval.v_list, str,
11322 (int)(end - str)) == FAIL)
11323 break;
11324 }
11325 if (!match)
11326 break;
11327 /* Advance to just after the match. */
11328 if (regmatch.endp[0] > str)
11329 col = 0;
11330 else
11331 {
11332 /* Don't get stuck at the same match. */
11333#ifdef FEAT_MBYTE
11334 col = (*mb_ptr2len)(regmatch.endp[0]);
11335#else
11336 col = 1;
11337#endif
11338 }
11339 str = regmatch.endp[0];
11340 }
11341
11342 vim_regfree(regmatch.regprog);
11343 }
11344
11345 p_cpo = save_cpo;
11346}
11347
11348#ifdef FEAT_FLOAT
11349/*
11350 * "sqrt()" function
11351 */
11352 static void
11353f_sqrt(typval_T *argvars, typval_T *rettv)
11354{
11355 float_T f = 0.0;
11356
11357 rettv->v_type = VAR_FLOAT;
11358 if (get_float_arg(argvars, &f) == OK)
11359 rettv->vval.v_float = sqrt(f);
11360 else
11361 rettv->vval.v_float = 0.0;
11362}
11363
11364/*
11365 * "str2float()" function
11366 */
11367 static void
11368f_str2float(typval_T *argvars, typval_T *rettv)
11369{
11370 char_u *p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011371 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011372
Bram Moolenaar08243d22017-01-10 16:12:29 +010011373 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011374 p = skipwhite(p + 1);
11375 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011376 if (isneg)
11377 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011378 rettv->v_type = VAR_FLOAT;
11379}
11380#endif
11381
11382/*
11383 * "str2nr()" function
11384 */
11385 static void
11386f_str2nr(typval_T *argvars, typval_T *rettv)
11387{
11388 int base = 10;
11389 char_u *p;
11390 varnumber_T n;
11391 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010011392 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011393
11394 if (argvars[1].v_type != VAR_UNKNOWN)
11395 {
11396 base = (int)get_tv_number(&argvars[1]);
11397 if (base != 2 && base != 8 && base != 10 && base != 16)
11398 {
11399 EMSG(_(e_invarg));
11400 return;
11401 }
11402 }
11403
11404 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011405 isneg = (*p == '-');
11406 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011407 p = skipwhite(p + 1);
11408 switch (base)
11409 {
11410 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
11411 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
11412 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
11413 default: what = 0;
11414 }
11415 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011416 if (isneg)
11417 rettv->vval.v_number = -n;
11418 else
11419 rettv->vval.v_number = n;
11420
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011421}
11422
11423#ifdef HAVE_STRFTIME
11424/*
11425 * "strftime({format}[, {time}])" function
11426 */
11427 static void
11428f_strftime(typval_T *argvars, typval_T *rettv)
11429{
11430 char_u result_buf[256];
11431 struct tm *curtime;
11432 time_t seconds;
11433 char_u *p;
11434
11435 rettv->v_type = VAR_STRING;
11436
11437 p = get_tv_string(&argvars[0]);
11438 if (argvars[1].v_type == VAR_UNKNOWN)
11439 seconds = time(NULL);
11440 else
11441 seconds = (time_t)get_tv_number(&argvars[1]);
11442 curtime = localtime(&seconds);
11443 /* MSVC returns NULL for an invalid value of seconds. */
11444 if (curtime == NULL)
11445 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
11446 else
11447 {
11448# ifdef FEAT_MBYTE
11449 vimconv_T conv;
11450 char_u *enc;
11451
11452 conv.vc_type = CONV_NONE;
11453 enc = enc_locale();
11454 convert_setup(&conv, p_enc, enc);
11455 if (conv.vc_type != CONV_NONE)
11456 p = string_convert(&conv, p, NULL);
11457# endif
11458 if (p != NULL)
11459 (void)strftime((char *)result_buf, sizeof(result_buf),
11460 (char *)p, curtime);
11461 else
11462 result_buf[0] = NUL;
11463
11464# ifdef FEAT_MBYTE
11465 if (conv.vc_type != CONV_NONE)
11466 vim_free(p);
11467 convert_setup(&conv, enc, p_enc);
11468 if (conv.vc_type != CONV_NONE)
11469 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
11470 else
11471# endif
11472 rettv->vval.v_string = vim_strsave(result_buf);
11473
11474# ifdef FEAT_MBYTE
11475 /* Release conversion descriptors */
11476 convert_setup(&conv, NULL, NULL);
11477 vim_free(enc);
11478# endif
11479 }
11480}
11481#endif
11482
11483/*
11484 * "strgetchar()" function
11485 */
11486 static void
11487f_strgetchar(typval_T *argvars, typval_T *rettv)
11488{
11489 char_u *str;
11490 int len;
11491 int error = FALSE;
11492 int charidx;
11493
11494 rettv->vval.v_number = -1;
11495 str = get_tv_string_chk(&argvars[0]);
11496 if (str == NULL)
11497 return;
11498 len = (int)STRLEN(str);
11499 charidx = (int)get_tv_number_chk(&argvars[1], &error);
11500 if (error)
11501 return;
11502#ifdef FEAT_MBYTE
11503 {
11504 int byteidx = 0;
11505
11506 while (charidx >= 0 && byteidx < len)
11507 {
11508 if (charidx == 0)
11509 {
11510 rettv->vval.v_number = mb_ptr2char(str + byteidx);
11511 break;
11512 }
11513 --charidx;
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011514 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011515 }
11516 }
11517#else
11518 if (charidx < len)
11519 rettv->vval.v_number = str[charidx];
11520#endif
11521}
11522
11523/*
11524 * "stridx()" function
11525 */
11526 static void
11527f_stridx(typval_T *argvars, typval_T *rettv)
11528{
11529 char_u buf[NUMBUFLEN];
11530 char_u *needle;
11531 char_u *haystack;
11532 char_u *save_haystack;
11533 char_u *pos;
11534 int start_idx;
11535
11536 needle = get_tv_string_chk(&argvars[1]);
11537 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
11538 rettv->vval.v_number = -1;
11539 if (needle == NULL || haystack == NULL)
11540 return; /* type error; errmsg already given */
11541
11542 if (argvars[2].v_type != VAR_UNKNOWN)
11543 {
11544 int error = FALSE;
11545
11546 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
11547 if (error || start_idx >= (int)STRLEN(haystack))
11548 return;
11549 if (start_idx >= 0)
11550 haystack += start_idx;
11551 }
11552
11553 pos = (char_u *)strstr((char *)haystack, (char *)needle);
11554 if (pos != NULL)
11555 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
11556}
11557
11558/*
11559 * "string()" function
11560 */
11561 static void
11562f_string(typval_T *argvars, typval_T *rettv)
11563{
11564 char_u *tofree;
11565 char_u numbuf[NUMBUFLEN];
11566
11567 rettv->v_type = VAR_STRING;
11568 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
11569 get_copyID());
11570 /* Make a copy if we have a value but it's not in allocated memory. */
11571 if (rettv->vval.v_string != NULL && tofree == NULL)
11572 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
11573}
11574
11575/*
11576 * "strlen()" function
11577 */
11578 static void
11579f_strlen(typval_T *argvars, typval_T *rettv)
11580{
11581 rettv->vval.v_number = (varnumber_T)(STRLEN(
11582 get_tv_string(&argvars[0])));
11583}
11584
11585/*
11586 * "strchars()" function
11587 */
11588 static void
11589f_strchars(typval_T *argvars, typval_T *rettv)
11590{
11591 char_u *s = get_tv_string(&argvars[0]);
11592 int skipcc = 0;
11593#ifdef FEAT_MBYTE
11594 varnumber_T len = 0;
11595 int (*func_mb_ptr2char_adv)(char_u **pp);
11596#endif
11597
11598 if (argvars[1].v_type != VAR_UNKNOWN)
11599 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
11600 if (skipcc < 0 || skipcc > 1)
11601 EMSG(_(e_invarg));
11602 else
11603 {
11604#ifdef FEAT_MBYTE
11605 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
11606 while (*s != NUL)
11607 {
11608 func_mb_ptr2char_adv(&s);
11609 ++len;
11610 }
11611 rettv->vval.v_number = len;
11612#else
11613 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
11614#endif
11615 }
11616}
11617
11618/*
11619 * "strdisplaywidth()" function
11620 */
11621 static void
11622f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
11623{
11624 char_u *s = get_tv_string(&argvars[0]);
11625 int col = 0;
11626
11627 if (argvars[1].v_type != VAR_UNKNOWN)
11628 col = (int)get_tv_number(&argvars[1]);
11629
11630 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
11631}
11632
11633/*
11634 * "strwidth()" function
11635 */
11636 static void
11637f_strwidth(typval_T *argvars, typval_T *rettv)
11638{
11639 char_u *s = get_tv_string(&argvars[0]);
11640
11641 rettv->vval.v_number = (varnumber_T)(
11642#ifdef FEAT_MBYTE
11643 mb_string2cells(s, -1)
11644#else
11645 STRLEN(s)
11646#endif
11647 );
11648}
11649
11650/*
11651 * "strcharpart()" function
11652 */
11653 static void
11654f_strcharpart(typval_T *argvars, typval_T *rettv)
11655{
11656#ifdef FEAT_MBYTE
11657 char_u *p;
11658 int nchar;
11659 int nbyte = 0;
11660 int charlen;
11661 int len = 0;
11662 int slen;
11663 int error = FALSE;
11664
11665 p = get_tv_string(&argvars[0]);
11666 slen = (int)STRLEN(p);
11667
11668 nchar = (int)get_tv_number_chk(&argvars[1], &error);
11669 if (!error)
11670 {
11671 if (nchar > 0)
11672 while (nchar > 0 && nbyte < slen)
11673 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011674 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011675 --nchar;
11676 }
11677 else
11678 nbyte = nchar;
11679 if (argvars[2].v_type != VAR_UNKNOWN)
11680 {
11681 charlen = (int)get_tv_number(&argvars[2]);
11682 while (charlen > 0 && nbyte + len < slen)
11683 {
11684 int off = nbyte + len;
11685
11686 if (off < 0)
11687 len += 1;
11688 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011689 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011690 --charlen;
11691 }
11692 }
11693 else
11694 len = slen - nbyte; /* default: all bytes that are available. */
11695 }
11696
11697 /*
11698 * Only return the overlap between the specified part and the actual
11699 * string.
11700 */
11701 if (nbyte < 0)
11702 {
11703 len += nbyte;
11704 nbyte = 0;
11705 }
11706 else if (nbyte > slen)
11707 nbyte = slen;
11708 if (len < 0)
11709 len = 0;
11710 else if (nbyte + len > slen)
11711 len = slen - nbyte;
11712
11713 rettv->v_type = VAR_STRING;
11714 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
11715#else
11716 f_strpart(argvars, rettv);
11717#endif
11718}
11719
11720/*
11721 * "strpart()" function
11722 */
11723 static void
11724f_strpart(typval_T *argvars, typval_T *rettv)
11725{
11726 char_u *p;
11727 int n;
11728 int len;
11729 int slen;
11730 int error = FALSE;
11731
11732 p = get_tv_string(&argvars[0]);
11733 slen = (int)STRLEN(p);
11734
11735 n = (int)get_tv_number_chk(&argvars[1], &error);
11736 if (error)
11737 len = 0;
11738 else if (argvars[2].v_type != VAR_UNKNOWN)
11739 len = (int)get_tv_number(&argvars[2]);
11740 else
11741 len = slen - n; /* default len: all bytes that are available. */
11742
11743 /*
11744 * Only return the overlap between the specified part and the actual
11745 * string.
11746 */
11747 if (n < 0)
11748 {
11749 len += n;
11750 n = 0;
11751 }
11752 else if (n > slen)
11753 n = slen;
11754 if (len < 0)
11755 len = 0;
11756 else if (n + len > slen)
11757 len = slen - n;
11758
11759 rettv->v_type = VAR_STRING;
11760 rettv->vval.v_string = vim_strnsave(p + n, len);
11761}
11762
11763/*
11764 * "strridx()" function
11765 */
11766 static void
11767f_strridx(typval_T *argvars, typval_T *rettv)
11768{
11769 char_u buf[NUMBUFLEN];
11770 char_u *needle;
11771 char_u *haystack;
11772 char_u *rest;
11773 char_u *lastmatch = NULL;
11774 int haystack_len, end_idx;
11775
11776 needle = get_tv_string_chk(&argvars[1]);
11777 haystack = get_tv_string_buf_chk(&argvars[0], buf);
11778
11779 rettv->vval.v_number = -1;
11780 if (needle == NULL || haystack == NULL)
11781 return; /* type error; errmsg already given */
11782
11783 haystack_len = (int)STRLEN(haystack);
11784 if (argvars[2].v_type != VAR_UNKNOWN)
11785 {
11786 /* Third argument: upper limit for index */
11787 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
11788 if (end_idx < 0)
11789 return; /* can never find a match */
11790 }
11791 else
11792 end_idx = haystack_len;
11793
11794 if (*needle == NUL)
11795 {
11796 /* Empty string matches past the end. */
11797 lastmatch = haystack + end_idx;
11798 }
11799 else
11800 {
11801 for (rest = haystack; *rest != '\0'; ++rest)
11802 {
11803 rest = (char_u *)strstr((char *)rest, (char *)needle);
11804 if (rest == NULL || rest > haystack + end_idx)
11805 break;
11806 lastmatch = rest;
11807 }
11808 }
11809
11810 if (lastmatch == NULL)
11811 rettv->vval.v_number = -1;
11812 else
11813 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
11814}
11815
11816/*
11817 * "strtrans()" function
11818 */
11819 static void
11820f_strtrans(typval_T *argvars, typval_T *rettv)
11821{
11822 rettv->v_type = VAR_STRING;
11823 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
11824}
11825
11826/*
11827 * "submatch()" function
11828 */
11829 static void
11830f_submatch(typval_T *argvars, typval_T *rettv)
11831{
11832 int error = FALSE;
11833 int no;
11834 int retList = 0;
11835
11836 no = (int)get_tv_number_chk(&argvars[0], &error);
11837 if (error)
11838 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020011839 if (no < 0 || no >= NSUBEXP)
11840 {
Bram Moolenaar79518e22017-02-17 16:31:35 +010011841 EMSGN(_("E935: invalid submatch number: %d"), no);
11842 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020011843 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011844 if (argvars[1].v_type != VAR_UNKNOWN)
11845 retList = (int)get_tv_number_chk(&argvars[1], &error);
11846 if (error)
11847 return;
11848
11849 if (retList == 0)
11850 {
11851 rettv->v_type = VAR_STRING;
11852 rettv->vval.v_string = reg_submatch(no);
11853 }
11854 else
11855 {
11856 rettv->v_type = VAR_LIST;
11857 rettv->vval.v_list = reg_submatch_list(no);
11858 }
11859}
11860
11861/*
11862 * "substitute()" function
11863 */
11864 static void
11865f_substitute(typval_T *argvars, typval_T *rettv)
11866{
11867 char_u patbuf[NUMBUFLEN];
11868 char_u subbuf[NUMBUFLEN];
11869 char_u flagsbuf[NUMBUFLEN];
11870
11871 char_u *str = get_tv_string_chk(&argvars[0]);
11872 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011873 char_u *sub = NULL;
11874 typval_T *expr = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011875 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
11876
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011877 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
11878 expr = &argvars[2];
11879 else
11880 sub = get_tv_string_buf_chk(&argvars[2], subbuf);
11881
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011882 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011883 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
11884 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011885 rettv->vval.v_string = NULL;
11886 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011887 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011888}
11889
11890/*
11891 * "synID(lnum, col, trans)" function
11892 */
11893 static void
11894f_synID(typval_T *argvars UNUSED, typval_T *rettv)
11895{
11896 int id = 0;
11897#ifdef FEAT_SYN_HL
11898 linenr_T lnum;
11899 colnr_T col;
11900 int trans;
11901 int transerr = FALSE;
11902
11903 lnum = get_tv_lnum(argvars); /* -1 on type error */
11904 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
11905 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
11906
11907 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11908 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
11909 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
11910#endif
11911
11912 rettv->vval.v_number = id;
11913}
11914
11915/*
11916 * "synIDattr(id, what [, mode])" function
11917 */
11918 static void
11919f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
11920{
11921 char_u *p = NULL;
11922#ifdef FEAT_SYN_HL
11923 int id;
11924 char_u *what;
11925 char_u *mode;
11926 char_u modebuf[NUMBUFLEN];
11927 int modec;
11928
11929 id = (int)get_tv_number(&argvars[0]);
11930 what = get_tv_string(&argvars[1]);
11931 if (argvars[2].v_type != VAR_UNKNOWN)
11932 {
11933 mode = get_tv_string_buf(&argvars[2], modebuf);
11934 modec = TOLOWER_ASC(mode[0]);
11935 if (modec != 't' && modec != 'c' && modec != 'g')
11936 modec = 0; /* replace invalid with current */
11937 }
11938 else
11939 {
11940#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
11941 if (USE_24BIT)
11942 modec = 'g';
11943 else
11944#endif
11945 if (t_colors > 1)
11946 modec = 'c';
11947 else
11948 modec = 't';
11949 }
11950
11951
11952 switch (TOLOWER_ASC(what[0]))
11953 {
11954 case 'b':
11955 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
11956 p = highlight_color(id, what, modec);
11957 else /* bold */
11958 p = highlight_has_attr(id, HL_BOLD, modec);
11959 break;
11960
11961 case 'f': /* fg[#] or font */
11962 p = highlight_color(id, what, modec);
11963 break;
11964
11965 case 'i':
11966 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
11967 p = highlight_has_attr(id, HL_INVERSE, modec);
11968 else /* italic */
11969 p = highlight_has_attr(id, HL_ITALIC, modec);
11970 break;
11971
11972 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020011973 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011974 break;
11975
11976 case 'r': /* reverse */
11977 p = highlight_has_attr(id, HL_INVERSE, modec);
11978 break;
11979
11980 case 's':
11981 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
11982 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020011983 /* strikeout */
11984 else if (TOLOWER_ASC(what[1]) == 't' &&
11985 TOLOWER_ASC(what[2]) == 'r')
11986 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011987 else /* standout */
11988 p = highlight_has_attr(id, HL_STANDOUT, modec);
11989 break;
11990
11991 case 'u':
11992 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
11993 /* underline */
11994 p = highlight_has_attr(id, HL_UNDERLINE, modec);
11995 else
11996 /* undercurl */
11997 p = highlight_has_attr(id, HL_UNDERCURL, modec);
11998 break;
11999 }
12000
12001 if (p != NULL)
12002 p = vim_strsave(p);
12003#endif
12004 rettv->v_type = VAR_STRING;
12005 rettv->vval.v_string = p;
12006}
12007
12008/*
12009 * "synIDtrans(id)" function
12010 */
12011 static void
12012f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
12013{
12014 int id;
12015
12016#ifdef FEAT_SYN_HL
12017 id = (int)get_tv_number(&argvars[0]);
12018
12019 if (id > 0)
12020 id = syn_get_final_id(id);
12021 else
12022#endif
12023 id = 0;
12024
12025 rettv->vval.v_number = id;
12026}
12027
12028/*
12029 * "synconcealed(lnum, col)" function
12030 */
12031 static void
12032f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
12033{
12034#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12035 linenr_T lnum;
12036 colnr_T col;
12037 int syntax_flags = 0;
12038 int cchar;
12039 int matchid = 0;
12040 char_u str[NUMBUFLEN];
12041#endif
12042
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012043 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012044
12045#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12046 lnum = get_tv_lnum(argvars); /* -1 on type error */
12047 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12048
12049 vim_memset(str, NUL, sizeof(str));
12050
12051 if (rettv_list_alloc(rettv) != FAIL)
12052 {
12053 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12054 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12055 && curwin->w_p_cole > 0)
12056 {
12057 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
12058 syntax_flags = get_syntax_info(&matchid);
12059
12060 /* get the conceal character */
12061 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
12062 {
12063 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020012064 if (cchar == NUL && curwin->w_p_cole == 1)
12065 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012066 if (cchar != NUL)
12067 {
12068# ifdef FEAT_MBYTE
12069 if (has_mbyte)
12070 (*mb_char2bytes)(cchar, str);
12071 else
12072# endif
12073 str[0] = cchar;
12074 }
12075 }
12076 }
12077
12078 list_append_number(rettv->vval.v_list,
12079 (syntax_flags & HL_CONCEAL) != 0);
12080 /* -1 to auto-determine strlen */
12081 list_append_string(rettv->vval.v_list, str, -1);
12082 list_append_number(rettv->vval.v_list, matchid);
12083 }
12084#endif
12085}
12086
12087/*
12088 * "synstack(lnum, col)" function
12089 */
12090 static void
12091f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
12092{
12093#ifdef FEAT_SYN_HL
12094 linenr_T lnum;
12095 colnr_T col;
12096 int i;
12097 int id;
12098#endif
12099
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012100 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012101
12102#ifdef FEAT_SYN_HL
12103 lnum = get_tv_lnum(argvars); /* -1 on type error */
12104 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12105
12106 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12107 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12108 && rettv_list_alloc(rettv) != FAIL)
12109 {
12110 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
12111 for (i = 0; ; ++i)
12112 {
12113 id = syn_get_stack_item(i);
12114 if (id < 0)
12115 break;
12116 if (list_append_number(rettv->vval.v_list, id) == FAIL)
12117 break;
12118 }
12119 }
12120#endif
12121}
12122
12123 static void
12124get_cmd_output_as_rettv(
12125 typval_T *argvars,
12126 typval_T *rettv,
12127 int retlist)
12128{
12129 char_u *res = NULL;
12130 char_u *p;
12131 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012132 int err = FALSE;
12133 FILE *fd;
12134 list_T *list = NULL;
12135 int flags = SHELL_SILENT;
12136
12137 rettv->v_type = VAR_STRING;
12138 rettv->vval.v_string = NULL;
12139 if (check_restricted() || check_secure())
12140 goto errret;
12141
12142 if (argvars[1].v_type != VAR_UNKNOWN)
12143 {
12144 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010012145 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012146 * command.
12147 */
12148 if ((infile = vim_tempname('i', TRUE)) == NULL)
12149 {
12150 EMSG(_(e_notmp));
12151 goto errret;
12152 }
12153
12154 fd = mch_fopen((char *)infile, WRITEBIN);
12155 if (fd == NULL)
12156 {
12157 EMSG2(_(e_notopen), infile);
12158 goto errret;
12159 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010012160 if (argvars[1].v_type == VAR_NUMBER)
12161 {
12162 linenr_T lnum;
12163 buf_T *buf;
12164
12165 buf = buflist_findnr(argvars[1].vval.v_number);
12166 if (buf == NULL)
12167 {
12168 EMSGN(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010012169 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010012170 goto errret;
12171 }
12172
12173 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
12174 {
12175 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
12176 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
12177 {
12178 err = TRUE;
12179 break;
12180 }
12181 if (putc(NL, fd) == EOF)
12182 {
12183 err = TRUE;
12184 break;
12185 }
12186 }
12187 }
12188 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012189 {
12190 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
12191 err = TRUE;
12192 }
12193 else
12194 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010012195 size_t len;
12196 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012197
12198 p = get_tv_string_buf_chk(&argvars[1], buf);
12199 if (p == NULL)
12200 {
12201 fclose(fd);
12202 goto errret; /* type error; errmsg already given */
12203 }
12204 len = STRLEN(p);
12205 if (len > 0 && fwrite(p, len, 1, fd) != 1)
12206 err = TRUE;
12207 }
12208 if (fclose(fd) != 0)
12209 err = TRUE;
12210 if (err)
12211 {
12212 EMSG(_("E677: Error writing temp file"));
12213 goto errret;
12214 }
12215 }
12216
12217 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
12218 * echoes typeahead, that messes up the display. */
12219 if (!msg_silent)
12220 flags += SHELL_COOKED;
12221
12222 if (retlist)
12223 {
12224 int len;
12225 listitem_T *li;
12226 char_u *s = NULL;
12227 char_u *start;
12228 char_u *end;
12229 int i;
12230
12231 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
12232 if (res == NULL)
12233 goto errret;
12234
12235 list = list_alloc();
12236 if (list == NULL)
12237 goto errret;
12238
12239 for (i = 0; i < len; ++i)
12240 {
12241 start = res + i;
12242 while (i < len && res[i] != NL)
12243 ++i;
12244 end = res + i;
12245
12246 s = alloc((unsigned)(end - start + 1));
12247 if (s == NULL)
12248 goto errret;
12249
12250 for (p = s; start < end; ++p, ++start)
12251 *p = *start == NUL ? NL : *start;
12252 *p = NUL;
12253
12254 li = listitem_alloc();
12255 if (li == NULL)
12256 {
12257 vim_free(s);
12258 goto errret;
12259 }
12260 li->li_tv.v_type = VAR_STRING;
12261 li->li_tv.v_lock = 0;
12262 li->li_tv.vval.v_string = s;
12263 list_append(list, li);
12264 }
12265
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012266 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012267 list = NULL;
12268 }
12269 else
12270 {
12271 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
12272#ifdef USE_CR
12273 /* translate <CR> into <NL> */
12274 if (res != NULL)
12275 {
12276 char_u *s;
12277
12278 for (s = res; *s; ++s)
12279 {
12280 if (*s == CAR)
12281 *s = NL;
12282 }
12283 }
12284#else
12285# ifdef USE_CRNL
12286 /* translate <CR><NL> into <NL> */
12287 if (res != NULL)
12288 {
12289 char_u *s, *d;
12290
12291 d = res;
12292 for (s = res; *s; ++s)
12293 {
12294 if (s[0] == CAR && s[1] == NL)
12295 ++s;
12296 *d++ = *s;
12297 }
12298 *d = NUL;
12299 }
12300# endif
12301#endif
12302 rettv->vval.v_string = res;
12303 res = NULL;
12304 }
12305
12306errret:
12307 if (infile != NULL)
12308 {
12309 mch_remove(infile);
12310 vim_free(infile);
12311 }
12312 if (res != NULL)
12313 vim_free(res);
12314 if (list != NULL)
12315 list_free(list);
12316}
12317
12318/*
12319 * "system()" function
12320 */
12321 static void
12322f_system(typval_T *argvars, typval_T *rettv)
12323{
12324 get_cmd_output_as_rettv(argvars, rettv, FALSE);
12325}
12326
12327/*
12328 * "systemlist()" function
12329 */
12330 static void
12331f_systemlist(typval_T *argvars, typval_T *rettv)
12332{
12333 get_cmd_output_as_rettv(argvars, rettv, TRUE);
12334}
12335
12336/*
12337 * "tabpagebuflist()" function
12338 */
12339 static void
12340f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12341{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012342 tabpage_T *tp;
12343 win_T *wp = NULL;
12344
12345 if (argvars[0].v_type == VAR_UNKNOWN)
12346 wp = firstwin;
12347 else
12348 {
12349 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12350 if (tp != NULL)
12351 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12352 }
12353 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
12354 {
12355 for (; wp != NULL; wp = wp->w_next)
12356 if (list_append_number(rettv->vval.v_list,
12357 wp->w_buffer->b_fnum) == FAIL)
12358 break;
12359 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012360}
12361
12362
12363/*
12364 * "tabpagenr()" function
12365 */
12366 static void
12367f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
12368{
12369 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012370 char_u *arg;
12371
12372 if (argvars[0].v_type != VAR_UNKNOWN)
12373 {
12374 arg = get_tv_string_chk(&argvars[0]);
12375 nr = 0;
12376 if (arg != NULL)
12377 {
12378 if (STRCMP(arg, "$") == 0)
12379 nr = tabpage_index(NULL) - 1;
12380 else
12381 EMSG2(_(e_invexpr2), arg);
12382 }
12383 }
12384 else
12385 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012386 rettv->vval.v_number = nr;
12387}
12388
12389
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012390static int get_winnr(tabpage_T *tp, typval_T *argvar);
12391
12392/*
12393 * Common code for tabpagewinnr() and winnr().
12394 */
12395 static int
12396get_winnr(tabpage_T *tp, typval_T *argvar)
12397{
12398 win_T *twin;
12399 int nr = 1;
12400 win_T *wp;
12401 char_u *arg;
12402
12403 twin = (tp == curtab) ? curwin : tp->tp_curwin;
12404 if (argvar->v_type != VAR_UNKNOWN)
12405 {
12406 arg = get_tv_string_chk(argvar);
12407 if (arg == NULL)
12408 nr = 0; /* type error; errmsg already given */
12409 else if (STRCMP(arg, "$") == 0)
12410 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
12411 else if (STRCMP(arg, "#") == 0)
12412 {
12413 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
12414 if (twin == NULL)
12415 nr = 0;
12416 }
12417 else
12418 {
12419 EMSG2(_(e_invexpr2), arg);
12420 nr = 0;
12421 }
12422 }
12423
12424 if (nr > 0)
12425 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12426 wp != twin; wp = wp->w_next)
12427 {
12428 if (wp == NULL)
12429 {
12430 /* didn't find it in this tabpage */
12431 nr = 0;
12432 break;
12433 }
12434 ++nr;
12435 }
12436 return nr;
12437}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012438
12439/*
12440 * "tabpagewinnr()" function
12441 */
12442 static void
12443f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
12444{
12445 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012446 tabpage_T *tp;
12447
12448 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12449 if (tp == NULL)
12450 nr = 0;
12451 else
12452 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012453 rettv->vval.v_number = nr;
12454}
12455
12456
12457/*
12458 * "tagfiles()" function
12459 */
12460 static void
12461f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
12462{
12463 char_u *fname;
12464 tagname_T tn;
12465 int first;
12466
12467 if (rettv_list_alloc(rettv) == FAIL)
12468 return;
12469 fname = alloc(MAXPATHL);
12470 if (fname == NULL)
12471 return;
12472
12473 for (first = TRUE; ; first = FALSE)
12474 if (get_tagfname(&tn, first, fname) == FAIL
12475 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
12476 break;
12477 tagname_free(&tn);
12478 vim_free(fname);
12479}
12480
12481/*
12482 * "taglist()" function
12483 */
12484 static void
12485f_taglist(typval_T *argvars, typval_T *rettv)
12486{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012487 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012488 char_u *tag_pattern;
12489
12490 tag_pattern = get_tv_string(&argvars[0]);
12491
12492 rettv->vval.v_number = FALSE;
12493 if (*tag_pattern == NUL)
12494 return;
12495
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012496 if (argvars[1].v_type != VAR_UNKNOWN)
12497 fname = get_tv_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012498 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012499 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012500}
12501
12502/*
12503 * "tempname()" function
12504 */
12505 static void
12506f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
12507{
12508 static int x = 'A';
12509
12510 rettv->v_type = VAR_STRING;
12511 rettv->vval.v_string = vim_tempname(x, FALSE);
12512
12513 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
12514 * names. Skip 'I' and 'O', they are used for shell redirection. */
12515 do
12516 {
12517 if (x == 'Z')
12518 x = '0';
12519 else if (x == '9')
12520 x = 'A';
12521 else
12522 {
12523#ifdef EBCDIC
12524 if (x == 'I')
12525 x = 'J';
12526 else if (x == 'R')
12527 x = 'S';
12528 else
12529#endif
12530 ++x;
12531 }
12532 } while (x == 'I' || x == 'O');
12533}
12534
12535#ifdef FEAT_FLOAT
12536/*
12537 * "tan()" function
12538 */
12539 static void
12540f_tan(typval_T *argvars, typval_T *rettv)
12541{
12542 float_T f = 0.0;
12543
12544 rettv->v_type = VAR_FLOAT;
12545 if (get_float_arg(argvars, &f) == OK)
12546 rettv->vval.v_float = tan(f);
12547 else
12548 rettv->vval.v_float = 0.0;
12549}
12550
12551/*
12552 * "tanh()" function
12553 */
12554 static void
12555f_tanh(typval_T *argvars, typval_T *rettv)
12556{
12557 float_T f = 0.0;
12558
12559 rettv->v_type = VAR_FLOAT;
12560 if (get_float_arg(argvars, &f) == OK)
12561 rettv->vval.v_float = tanh(f);
12562 else
12563 rettv->vval.v_float = 0.0;
12564}
12565#endif
12566
12567/*
12568 * "test_alloc_fail(id, countdown, repeat)" function
12569 */
12570 static void
12571f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
12572{
12573 if (argvars[0].v_type != VAR_NUMBER
12574 || argvars[0].vval.v_number <= 0
12575 || argvars[1].v_type != VAR_NUMBER
12576 || argvars[1].vval.v_number < 0
12577 || argvars[2].v_type != VAR_NUMBER)
12578 EMSG(_(e_invarg));
12579 else
12580 {
12581 alloc_fail_id = argvars[0].vval.v_number;
12582 if (alloc_fail_id >= aid_last)
12583 EMSG(_(e_invarg));
12584 alloc_fail_countdown = argvars[1].vval.v_number;
12585 alloc_fail_repeat = argvars[2].vval.v_number;
12586 did_outofmem_msg = FALSE;
12587 }
12588}
12589
12590/*
12591 * "test_autochdir()"
12592 */
12593 static void
12594f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12595{
12596#if defined(FEAT_AUTOCHDIR)
12597 test_autochdir = TRUE;
12598#endif
12599}
12600
12601/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020012602 * "test_feedinput()"
12603 */
12604 static void
12605f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
12606{
12607#ifdef USE_INPUT_BUF
12608 char_u *val = get_tv_string_chk(&argvars[0]);
12609
12610 if (val != NULL)
12611 {
12612 trash_input_buf();
12613 add_to_input_buf_csi(val, (int)STRLEN(val));
12614 }
12615#endif
12616}
12617
12618/*
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012619 * "test_disable({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012620 */
12621 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012622f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012623{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012624 char_u *name = (char_u *)"";
12625 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012626 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012627
12628 if (argvars[0].v_type != VAR_STRING
12629 || (argvars[1].v_type) != VAR_NUMBER)
12630 EMSG(_(e_invarg));
12631 else
12632 {
12633 name = get_tv_string_chk(&argvars[0]);
12634 val = (int)get_tv_number(&argvars[1]);
12635
12636 if (STRCMP(name, (char_u *)"redraw") == 0)
12637 disable_redraw_for_testing = val;
12638 else if (STRCMP(name, (char_u *)"char_avail") == 0)
12639 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012640 else if (STRCMP(name, (char_u *)"starting") == 0)
12641 {
12642 if (val)
12643 {
12644 if (save_starting < 0)
12645 save_starting = starting;
12646 starting = 0;
12647 }
12648 else
12649 {
12650 starting = save_starting;
12651 save_starting = -1;
12652 }
12653 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012654 else if (STRCMP(name, (char_u *)"ALL") == 0)
12655 {
12656 disable_char_avail_for_testing = FALSE;
12657 disable_redraw_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012658 if (save_starting >= 0)
12659 {
12660 starting = save_starting;
12661 save_starting = -1;
12662 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012663 }
12664 else
12665 EMSG2(_(e_invarg2), name);
12666 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012667}
12668
12669/*
12670 * "test_garbagecollect_now()" function
12671 */
12672 static void
12673f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12674{
12675 /* This is dangerous, any Lists and Dicts used internally may be freed
12676 * while still in use. */
12677 garbage_collect(TRUE);
12678}
12679
Bram Moolenaare0c31f62017-03-01 15:07:05 +010012680/*
12681 * "test_ignore_error()" function
12682 */
12683 static void
12684f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
12685{
12686 ignore_error_for_testing(get_tv_string(&argvars[0]));
12687}
12688
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012689#ifdef FEAT_JOB_CHANNEL
12690 static void
12691f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
12692{
12693 rettv->v_type = VAR_CHANNEL;
12694 rettv->vval.v_channel = NULL;
12695}
12696#endif
12697
12698 static void
12699f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
12700{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012701 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012702}
12703
12704#ifdef FEAT_JOB_CHANNEL
12705 static void
12706f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
12707{
12708 rettv->v_type = VAR_JOB;
12709 rettv->vval.v_job = NULL;
12710}
12711#endif
12712
12713 static void
12714f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
12715{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012716 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012717}
12718
12719 static void
12720f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
12721{
12722 rettv->v_type = VAR_PARTIAL;
12723 rettv->vval.v_partial = NULL;
12724}
12725
12726 static void
12727f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
12728{
12729 rettv->v_type = VAR_STRING;
12730 rettv->vval.v_string = NULL;
12731}
12732
12733 static void
12734f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
12735{
12736 time_for_testing = (time_t)get_tv_number(&argvars[0]);
12737}
12738
12739#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
12740/*
12741 * Get a callback from "arg". It can be a Funcref or a function name.
12742 * When "arg" is zero return an empty string.
12743 * Return NULL for an invalid argument.
12744 */
12745 char_u *
12746get_callback(typval_T *arg, partial_T **pp)
12747{
12748 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
12749 {
12750 *pp = arg->vval.v_partial;
12751 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012752 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012753 }
12754 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012755 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012756 {
12757 func_ref(arg->vval.v_string);
12758 return arg->vval.v_string;
12759 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012760 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
12761 return (char_u *)"";
12762 EMSG(_("E921: Invalid callback argument"));
12763 return NULL;
12764}
12765
12766/*
12767 * Unref/free "callback" and "partial" retured by get_callback().
12768 */
12769 void
12770free_callback(char_u *callback, partial_T *partial)
12771{
12772 if (partial != NULL)
12773 partial_unref(partial);
12774 else if (callback != NULL)
12775 {
12776 func_unref(callback);
12777 vim_free(callback);
12778 }
12779}
12780#endif
12781
12782#ifdef FEAT_TIMERS
12783/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020012784 * "timer_info([timer])" function
12785 */
12786 static void
12787f_timer_info(typval_T *argvars, typval_T *rettv)
12788{
12789 timer_T *timer = NULL;
12790
12791 if (rettv_list_alloc(rettv) != OK)
12792 return;
12793 if (argvars[0].v_type != VAR_UNKNOWN)
12794 {
12795 if (argvars[0].v_type != VAR_NUMBER)
12796 EMSG(_(e_number_exp));
12797 else
12798 {
12799 timer = find_timer((int)get_tv_number(&argvars[0]));
12800 if (timer != NULL)
12801 add_timer_info(rettv, timer);
12802 }
12803 }
12804 else
12805 add_timer_info_all(rettv);
12806}
12807
12808/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012809 * "timer_pause(timer, paused)" function
12810 */
12811 static void
12812f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
12813{
12814 timer_T *timer = NULL;
12815 int paused = (int)get_tv_number(&argvars[1]);
12816
12817 if (argvars[0].v_type != VAR_NUMBER)
12818 EMSG(_(e_number_exp));
12819 else
12820 {
12821 timer = find_timer((int)get_tv_number(&argvars[0]));
12822 if (timer != NULL)
12823 timer->tr_paused = paused;
12824 }
12825}
12826
12827/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012828 * "timer_start(time, callback [, options])" function
12829 */
12830 static void
12831f_timer_start(typval_T *argvars, typval_T *rettv)
12832{
Bram Moolenaar75537a92016-09-05 22:45:28 +020012833 long msec = (long)get_tv_number(&argvars[0]);
12834 timer_T *timer;
12835 int repeat = 0;
12836 char_u *callback;
12837 dict_T *dict;
12838 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012839
Bram Moolenaar75537a92016-09-05 22:45:28 +020012840 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012841 if (check_secure())
12842 return;
12843 if (argvars[2].v_type != VAR_UNKNOWN)
12844 {
12845 if (argvars[2].v_type != VAR_DICT
12846 || (dict = argvars[2].vval.v_dict) == NULL)
12847 {
12848 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
12849 return;
12850 }
12851 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
12852 repeat = get_dict_number(dict, (char_u *)"repeat");
12853 }
12854
Bram Moolenaar75537a92016-09-05 22:45:28 +020012855 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012856 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020012857 return;
12858
12859 timer = create_timer(msec, repeat);
12860 if (timer == NULL)
12861 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012862 else
12863 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020012864 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020012865 timer->tr_callback = vim_strsave(callback);
12866 else
12867 /* pointer into the partial */
12868 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020012869 timer->tr_partial = partial;
12870 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012871 }
12872}
12873
12874/*
12875 * "timer_stop(timer)" function
12876 */
12877 static void
12878f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
12879{
12880 timer_T *timer;
12881
12882 if (argvars[0].v_type != VAR_NUMBER)
12883 {
12884 EMSG(_(e_number_exp));
12885 return;
12886 }
12887 timer = find_timer((int)get_tv_number(&argvars[0]));
12888 if (timer != NULL)
12889 stop_timer(timer);
12890}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012891
12892/*
12893 * "timer_stopall()" function
12894 */
12895 static void
12896f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12897{
12898 stop_all_timers();
12899}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012900#endif
12901
12902/*
12903 * "tolower(string)" function
12904 */
12905 static void
12906f_tolower(typval_T *argvars, typval_T *rettv)
12907{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012908 rettv->v_type = VAR_STRING;
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +010012909 rettv->vval.v_string = strlow_save(get_tv_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012910}
12911
12912/*
12913 * "toupper(string)" function
12914 */
12915 static void
12916f_toupper(typval_T *argvars, typval_T *rettv)
12917{
12918 rettv->v_type = VAR_STRING;
12919 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
12920}
12921
12922/*
12923 * "tr(string, fromstr, tostr)" function
12924 */
12925 static void
12926f_tr(typval_T *argvars, typval_T *rettv)
12927{
12928 char_u *in_str;
12929 char_u *fromstr;
12930 char_u *tostr;
12931 char_u *p;
12932#ifdef FEAT_MBYTE
12933 int inlen;
12934 int fromlen;
12935 int tolen;
12936 int idx;
12937 char_u *cpstr;
12938 int cplen;
12939 int first = TRUE;
12940#endif
12941 char_u buf[NUMBUFLEN];
12942 char_u buf2[NUMBUFLEN];
12943 garray_T ga;
12944
12945 in_str = get_tv_string(&argvars[0]);
12946 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
12947 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
12948
12949 /* Default return value: empty string. */
12950 rettv->v_type = VAR_STRING;
12951 rettv->vval.v_string = NULL;
12952 if (fromstr == NULL || tostr == NULL)
12953 return; /* type error; errmsg already given */
12954 ga_init2(&ga, (int)sizeof(char), 80);
12955
12956#ifdef FEAT_MBYTE
12957 if (!has_mbyte)
12958#endif
12959 /* not multi-byte: fromstr and tostr must be the same length */
12960 if (STRLEN(fromstr) != STRLEN(tostr))
12961 {
12962#ifdef FEAT_MBYTE
12963error:
12964#endif
12965 EMSG2(_(e_invarg2), fromstr);
12966 ga_clear(&ga);
12967 return;
12968 }
12969
12970 /* fromstr and tostr have to contain the same number of chars */
12971 while (*in_str != NUL)
12972 {
12973#ifdef FEAT_MBYTE
12974 if (has_mbyte)
12975 {
12976 inlen = (*mb_ptr2len)(in_str);
12977 cpstr = in_str;
12978 cplen = inlen;
12979 idx = 0;
12980 for (p = fromstr; *p != NUL; p += fromlen)
12981 {
12982 fromlen = (*mb_ptr2len)(p);
12983 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
12984 {
12985 for (p = tostr; *p != NUL; p += tolen)
12986 {
12987 tolen = (*mb_ptr2len)(p);
12988 if (idx-- == 0)
12989 {
12990 cplen = tolen;
12991 cpstr = p;
12992 break;
12993 }
12994 }
12995 if (*p == NUL) /* tostr is shorter than fromstr */
12996 goto error;
12997 break;
12998 }
12999 ++idx;
13000 }
13001
13002 if (first && cpstr == in_str)
13003 {
13004 /* Check that fromstr and tostr have the same number of
13005 * (multi-byte) characters. Done only once when a character
13006 * of in_str doesn't appear in fromstr. */
13007 first = FALSE;
13008 for (p = tostr; *p != NUL; p += tolen)
13009 {
13010 tolen = (*mb_ptr2len)(p);
13011 --idx;
13012 }
13013 if (idx != 0)
13014 goto error;
13015 }
13016
13017 (void)ga_grow(&ga, cplen);
13018 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
13019 ga.ga_len += cplen;
13020
13021 in_str += inlen;
13022 }
13023 else
13024#endif
13025 {
13026 /* When not using multi-byte chars we can do it faster. */
13027 p = vim_strchr(fromstr, *in_str);
13028 if (p != NULL)
13029 ga_append(&ga, tostr[p - fromstr]);
13030 else
13031 ga_append(&ga, *in_str);
13032 ++in_str;
13033 }
13034 }
13035
13036 /* add a terminating NUL */
13037 (void)ga_grow(&ga, 1);
13038 ga_append(&ga, NUL);
13039
13040 rettv->vval.v_string = ga.ga_data;
13041}
13042
13043#ifdef FEAT_FLOAT
13044/*
13045 * "trunc({float})" function
13046 */
13047 static void
13048f_trunc(typval_T *argvars, typval_T *rettv)
13049{
13050 float_T f = 0.0;
13051
13052 rettv->v_type = VAR_FLOAT;
13053 if (get_float_arg(argvars, &f) == OK)
13054 /* trunc() is not in C90, use floor() or ceil() instead. */
13055 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
13056 else
13057 rettv->vval.v_float = 0.0;
13058}
13059#endif
13060
13061/*
13062 * "type(expr)" function
13063 */
13064 static void
13065f_type(typval_T *argvars, typval_T *rettv)
13066{
13067 int n = -1;
13068
13069 switch (argvars[0].v_type)
13070 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020013071 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
13072 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013073 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020013074 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
13075 case VAR_LIST: n = VAR_TYPE_LIST; break;
13076 case VAR_DICT: n = VAR_TYPE_DICT; break;
13077 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013078 case VAR_SPECIAL:
13079 if (argvars[0].vval.v_number == VVAL_FALSE
13080 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020013081 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013082 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020013083 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013084 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020013085 case VAR_JOB: n = VAR_TYPE_JOB; break;
13086 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013087 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010013088 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013089 n = -1;
13090 break;
13091 }
13092 rettv->vval.v_number = n;
13093}
13094
13095/*
13096 * "undofile(name)" function
13097 */
13098 static void
13099f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
13100{
13101 rettv->v_type = VAR_STRING;
13102#ifdef FEAT_PERSISTENT_UNDO
13103 {
13104 char_u *fname = get_tv_string(&argvars[0]);
13105
13106 if (*fname == NUL)
13107 {
13108 /* If there is no file name there will be no undo file. */
13109 rettv->vval.v_string = NULL;
13110 }
13111 else
13112 {
13113 char_u *ffname = FullName_save(fname, FALSE);
13114
13115 if (ffname != NULL)
13116 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
13117 vim_free(ffname);
13118 }
13119 }
13120#else
13121 rettv->vval.v_string = NULL;
13122#endif
13123}
13124
13125/*
13126 * "undotree()" function
13127 */
13128 static void
13129f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
13130{
13131 if (rettv_dict_alloc(rettv) == OK)
13132 {
13133 dict_T *dict = rettv->vval.v_dict;
13134 list_T *list;
13135
13136 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
13137 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
13138 dict_add_nr_str(dict, "save_last",
13139 (long)curbuf->b_u_save_nr_last, NULL);
13140 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
13141 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
13142 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
13143
13144 list = list_alloc();
13145 if (list != NULL)
13146 {
13147 u_eval_tree(curbuf->b_u_oldhead, list);
13148 dict_add_list(dict, "entries", list);
13149 }
13150 }
13151}
13152
13153/*
13154 * "values(dict)" function
13155 */
13156 static void
13157f_values(typval_T *argvars, typval_T *rettv)
13158{
13159 dict_list(argvars, rettv, 1);
13160}
13161
13162/*
13163 * "virtcol(string)" function
13164 */
13165 static void
13166f_virtcol(typval_T *argvars, typval_T *rettv)
13167{
13168 colnr_T vcol = 0;
13169 pos_T *fp;
13170 int fnum = curbuf->b_fnum;
13171
13172 fp = var2fpos(&argvars[0], FALSE, &fnum);
13173 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
13174 && fnum == curbuf->b_fnum)
13175 {
13176 getvvcol(curwin, fp, NULL, NULL, &vcol);
13177 ++vcol;
13178 }
13179
13180 rettv->vval.v_number = vcol;
13181}
13182
13183/*
13184 * "visualmode()" function
13185 */
13186 static void
13187f_visualmode(typval_T *argvars, typval_T *rettv)
13188{
13189 char_u str[2];
13190
13191 rettv->v_type = VAR_STRING;
13192 str[0] = curbuf->b_visual_mode_eval;
13193 str[1] = NUL;
13194 rettv->vval.v_string = vim_strsave(str);
13195
13196 /* A non-zero number or non-empty string argument: reset mode. */
13197 if (non_zero_arg(&argvars[0]))
13198 curbuf->b_visual_mode_eval = NUL;
13199}
13200
13201/*
13202 * "wildmenumode()" function
13203 */
13204 static void
13205f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13206{
13207#ifdef FEAT_WILDMENU
13208 if (wild_menu_showing)
13209 rettv->vval.v_number = 1;
13210#endif
13211}
13212
13213/*
13214 * "winbufnr(nr)" function
13215 */
13216 static void
13217f_winbufnr(typval_T *argvars, typval_T *rettv)
13218{
13219 win_T *wp;
13220
13221 wp = find_win_by_nr(&argvars[0], NULL);
13222 if (wp == NULL)
13223 rettv->vval.v_number = -1;
13224 else
13225 rettv->vval.v_number = wp->w_buffer->b_fnum;
13226}
13227
13228/*
13229 * "wincol()" function
13230 */
13231 static void
13232f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
13233{
13234 validate_cursor();
13235 rettv->vval.v_number = curwin->w_wcol + 1;
13236}
13237
13238/*
13239 * "winheight(nr)" function
13240 */
13241 static void
13242f_winheight(typval_T *argvars, typval_T *rettv)
13243{
13244 win_T *wp;
13245
13246 wp = find_win_by_nr(&argvars[0], NULL);
13247 if (wp == NULL)
13248 rettv->vval.v_number = -1;
13249 else
13250 rettv->vval.v_number = wp->w_height;
13251}
13252
13253/*
13254 * "winline()" function
13255 */
13256 static void
13257f_winline(typval_T *argvars UNUSED, typval_T *rettv)
13258{
13259 validate_cursor();
13260 rettv->vval.v_number = curwin->w_wrow + 1;
13261}
13262
13263/*
13264 * "winnr()" function
13265 */
13266 static void
13267f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
13268{
13269 int nr = 1;
13270
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013271 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013272 rettv->vval.v_number = nr;
13273}
13274
13275/*
13276 * "winrestcmd()" function
13277 */
13278 static void
13279f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
13280{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013281 win_T *wp;
13282 int winnr = 1;
13283 garray_T ga;
13284 char_u buf[50];
13285
13286 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020013287 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013288 {
13289 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
13290 ga_concat(&ga, buf);
13291 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
13292 ga_concat(&ga, buf);
13293 ++winnr;
13294 }
13295 ga_append(&ga, NUL);
13296
13297 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013298 rettv->v_type = VAR_STRING;
13299}
13300
13301/*
13302 * "winrestview()" function
13303 */
13304 static void
13305f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
13306{
13307 dict_T *dict;
13308
13309 if (argvars[0].v_type != VAR_DICT
13310 || (dict = argvars[0].vval.v_dict) == NULL)
13311 EMSG(_(e_invarg));
13312 else
13313 {
13314 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
13315 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
13316 if (dict_find(dict, (char_u *)"col", -1) != NULL)
13317 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
13318#ifdef FEAT_VIRTUALEDIT
13319 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
13320 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
13321#endif
13322 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
13323 {
13324 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
13325 curwin->w_set_curswant = FALSE;
13326 }
13327
13328 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
13329 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
13330#ifdef FEAT_DIFF
13331 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
13332 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
13333#endif
13334 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
13335 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
13336 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
13337 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
13338
13339 check_cursor();
13340 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020013341 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013342 changed_window_setting();
13343
13344 if (curwin->w_topline <= 0)
13345 curwin->w_topline = 1;
13346 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
13347 curwin->w_topline = curbuf->b_ml.ml_line_count;
13348#ifdef FEAT_DIFF
13349 check_topfill(curwin, TRUE);
13350#endif
13351 }
13352}
13353
13354/*
13355 * "winsaveview()" function
13356 */
13357 static void
13358f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
13359{
13360 dict_T *dict;
13361
13362 if (rettv_dict_alloc(rettv) == FAIL)
13363 return;
13364 dict = rettv->vval.v_dict;
13365
13366 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
13367 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
13368#ifdef FEAT_VIRTUALEDIT
13369 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
13370#endif
13371 update_curswant();
13372 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
13373
13374 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
13375#ifdef FEAT_DIFF
13376 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
13377#endif
13378 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
13379 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
13380}
13381
13382/*
13383 * "winwidth(nr)" function
13384 */
13385 static void
13386f_winwidth(typval_T *argvars, typval_T *rettv)
13387{
13388 win_T *wp;
13389
13390 wp = find_win_by_nr(&argvars[0], NULL);
13391 if (wp == NULL)
13392 rettv->vval.v_number = -1;
13393 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013394 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013395}
13396
13397/*
13398 * "wordcount()" function
13399 */
13400 static void
13401f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
13402{
13403 if (rettv_dict_alloc(rettv) == FAIL)
13404 return;
13405 cursor_pos_info(rettv->vval.v_dict);
13406}
13407
13408/*
13409 * "writefile()" function
13410 */
13411 static void
13412f_writefile(typval_T *argvars, typval_T *rettv)
13413{
13414 int binary = FALSE;
13415 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013416#ifdef HAVE_FSYNC
13417 int do_fsync = p_fs;
13418#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013419 char_u *fname;
13420 FILE *fd;
13421 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013422 listitem_T *li;
13423 list_T *list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013424
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013425 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013426 if (check_restricted() || check_secure())
13427 return;
13428
13429 if (argvars[0].v_type != VAR_LIST)
13430 {
13431 EMSG2(_(e_listarg), "writefile()");
13432 return;
13433 }
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013434 list = argvars[0].vval.v_list;
13435 if (list == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013436 return;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013437 for (li = list->lv_first; li != NULL; li = li->li_next)
13438 if (get_tv_string_chk(&li->li_tv) == NULL)
13439 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013440
13441 if (argvars[2].v_type != VAR_UNKNOWN)
13442 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013443 char_u *arg2 = get_tv_string_chk(&argvars[2]);
13444
13445 if (arg2 == NULL)
13446 return;
13447 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013448 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013449 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013450 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013451#ifdef HAVE_FSYNC
13452 if (vim_strchr(arg2, 's') != NULL)
13453 do_fsync = TRUE;
13454 else if (vim_strchr(arg2, 'S') != NULL)
13455 do_fsync = FALSE;
13456#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013457 }
13458
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013459 fname = get_tv_string_chk(&argvars[1]);
13460 if (fname == NULL)
13461 return;
13462
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013463 /* Always open the file in binary mode, library functions have a mind of
13464 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013465 if (*fname == NUL || (fd = mch_fopen((char *)fname,
13466 append ? APPENDBIN : WRITEBIN)) == NULL)
13467 {
13468 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
13469 ret = -1;
13470 }
13471 else
13472 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013473 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013474 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013475#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010013476 else if (do_fsync)
13477 /* Ignore the error, the user wouldn't know what to do about it.
13478 * May happen for a device. */
13479 ignored = fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013480#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013481 fclose(fd);
13482 }
13483
13484 rettv->vval.v_number = ret;
13485}
13486
13487/*
13488 * "xor(expr, expr)" function
13489 */
13490 static void
13491f_xor(typval_T *argvars, typval_T *rettv)
13492{
13493 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
13494 ^ get_tv_number_chk(&argvars[1], NULL);
13495}
13496
13497
13498#endif /* FEAT_EVAL */