blob: 3977944ca3a610a3de9871b7c5712eccd838120c [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalfunc.c: Builtin functions
12 */
13#define USING_FLOAT_STUFF
14
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef VMS
24# include <float.h>
25#endif
26
Bram Moolenaard0573012017-10-28 21:11:06 +020027#ifdef MACOS_X
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020028# include <time.h> /* for time_t */
29#endif
30
31static char *e_listarg = N_("E686: Argument of %s must be a List");
32#ifdef FEAT_QUICKFIX
33static char *e_stringreq = N_("E928: String required");
34#endif
35
36#ifdef FEAT_FLOAT
37static void f_abs(typval_T *argvars, typval_T *rettv);
38static void f_acos(typval_T *argvars, typval_T *rettv);
39#endif
40static void f_add(typval_T *argvars, typval_T *rettv);
41static void f_and(typval_T *argvars, typval_T *rettv);
42static void f_append(typval_T *argvars, typval_T *rettv);
43static void f_argc(typval_T *argvars, typval_T *rettv);
44static void f_argidx(typval_T *argvars, typval_T *rettv);
45static void f_arglistid(typval_T *argvars, typval_T *rettv);
46static void f_argv(typval_T *argvars, typval_T *rettv);
47static void f_assert_equal(typval_T *argvars, typval_T *rettv);
48static void f_assert_exception(typval_T *argvars, typval_T *rettv);
49static void f_assert_fails(typval_T *argvars, typval_T *rettv);
50static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaar61c04492016-07-23 15:35:35 +020051static void f_assert_inrange(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020052static void f_assert_match(typval_T *argvars, typval_T *rettv);
53static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
54static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar42205552017-03-18 19:42:22 +010055static void f_assert_report(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020056static void f_assert_true(typval_T *argvars, typval_T *rettv);
57#ifdef FEAT_FLOAT
58static void f_asin(typval_T *argvars, typval_T *rettv);
59static void f_atan(typval_T *argvars, typval_T *rettv);
60static void f_atan2(typval_T *argvars, typval_T *rettv);
61#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010062#ifdef FEAT_BEVAL
63static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010064# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010065static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010066# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010067#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020068static void f_browse(typval_T *argvars, typval_T *rettv);
69static void f_browsedir(typval_T *argvars, typval_T *rettv);
70static void f_bufexists(typval_T *argvars, typval_T *rettv);
71static void f_buflisted(typval_T *argvars, typval_T *rettv);
72static void f_bufloaded(typval_T *argvars, typval_T *rettv);
73static void f_bufname(typval_T *argvars, typval_T *rettv);
74static void f_bufnr(typval_T *argvars, typval_T *rettv);
75static void f_bufwinid(typval_T *argvars, typval_T *rettv);
76static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
77static void f_byte2line(typval_T *argvars, typval_T *rettv);
78static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
79static void f_byteidx(typval_T *argvars, typval_T *rettv);
80static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
81static void f_call(typval_T *argvars, typval_T *rettv);
82#ifdef FEAT_FLOAT
83static void f_ceil(typval_T *argvars, typval_T *rettv);
84#endif
85#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +010086static void f_ch_canread(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020087static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0874a832016-09-01 15:11:51 +020088static void f_ch_close_in(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020089static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
90static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
91static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
92static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
93static void f_ch_info(typval_T *argvars, typval_T *rettv);
94static void f_ch_log(typval_T *argvars, typval_T *rettv);
95static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
96static void f_ch_open(typval_T *argvars, typval_T *rettv);
97static void f_ch_read(typval_T *argvars, typval_T *rettv);
98static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
99static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
100static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
101static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
102static void f_ch_status(typval_T *argvars, typval_T *rettv);
103#endif
104static void f_changenr(typval_T *argvars, typval_T *rettv);
105static void f_char2nr(typval_T *argvars, typval_T *rettv);
106static void f_cindent(typval_T *argvars, typval_T *rettv);
107static void f_clearmatches(typval_T *argvars, typval_T *rettv);
108static void f_col(typval_T *argvars, typval_T *rettv);
109#if defined(FEAT_INS_EXPAND)
110static void f_complete(typval_T *argvars, typval_T *rettv);
111static void f_complete_add(typval_T *argvars, typval_T *rettv);
112static void f_complete_check(typval_T *argvars, typval_T *rettv);
113#endif
114static void f_confirm(typval_T *argvars, typval_T *rettv);
115static void f_copy(typval_T *argvars, typval_T *rettv);
116#ifdef FEAT_FLOAT
117static void f_cos(typval_T *argvars, typval_T *rettv);
118static void f_cosh(typval_T *argvars, typval_T *rettv);
119#endif
120static void f_count(typval_T *argvars, typval_T *rettv);
121static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
122static void f_cursor(typval_T *argsvars, typval_T *rettv);
123static void f_deepcopy(typval_T *argvars, typval_T *rettv);
124static void f_delete(typval_T *argvars, typval_T *rettv);
125static void f_did_filetype(typval_T *argvars, typval_T *rettv);
126static void f_diff_filler(typval_T *argvars, typval_T *rettv);
127static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
128static void f_empty(typval_T *argvars, typval_T *rettv);
129static void f_escape(typval_T *argvars, typval_T *rettv);
130static void f_eval(typval_T *argvars, typval_T *rettv);
131static void f_eventhandler(typval_T *argvars, typval_T *rettv);
132static void f_executable(typval_T *argvars, typval_T *rettv);
133static void f_execute(typval_T *argvars, typval_T *rettv);
134static void f_exepath(typval_T *argvars, typval_T *rettv);
135static void f_exists(typval_T *argvars, typval_T *rettv);
136#ifdef FEAT_FLOAT
137static void f_exp(typval_T *argvars, typval_T *rettv);
138#endif
139static void f_expand(typval_T *argvars, typval_T *rettv);
140static void f_extend(typval_T *argvars, typval_T *rettv);
141static void f_feedkeys(typval_T *argvars, typval_T *rettv);
142static void f_filereadable(typval_T *argvars, typval_T *rettv);
143static void f_filewritable(typval_T *argvars, typval_T *rettv);
144static void f_filter(typval_T *argvars, typval_T *rettv);
145static void f_finddir(typval_T *argvars, typval_T *rettv);
146static void f_findfile(typval_T *argvars, typval_T *rettv);
147#ifdef FEAT_FLOAT
148static void f_float2nr(typval_T *argvars, typval_T *rettv);
149static void f_floor(typval_T *argvars, typval_T *rettv);
150static void f_fmod(typval_T *argvars, typval_T *rettv);
151#endif
152static void f_fnameescape(typval_T *argvars, typval_T *rettv);
153static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
154static void f_foldclosed(typval_T *argvars, typval_T *rettv);
155static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
156static void f_foldlevel(typval_T *argvars, typval_T *rettv);
157static void f_foldtext(typval_T *argvars, typval_T *rettv);
158static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
159static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200160static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200161static void f_function(typval_T *argvars, typval_T *rettv);
162static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
163static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200164static void f_getbufinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200165static void f_getbufline(typval_T *argvars, typval_T *rettv);
166static void f_getbufvar(typval_T *argvars, typval_T *rettv);
167static void f_getchar(typval_T *argvars, typval_T *rettv);
168static void f_getcharmod(typval_T *argvars, typval_T *rettv);
169static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
170static void f_getcmdline(typval_T *argvars, typval_T *rettv);
171#if defined(FEAT_CMDL_COMPL)
172static void f_getcompletion(typval_T *argvars, typval_T *rettv);
173#endif
174static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
175static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
176static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
177static void f_getcwd(typval_T *argvars, typval_T *rettv);
178static void f_getfontname(typval_T *argvars, typval_T *rettv);
179static void f_getfperm(typval_T *argvars, typval_T *rettv);
180static void f_getfsize(typval_T *argvars, typval_T *rettv);
181static void f_getftime(typval_T *argvars, typval_T *rettv);
182static void f_getftype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +0100183static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200184static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200185static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200186static void f_getmatches(typval_T *argvars, typval_T *rettv);
187static void f_getpid(typval_T *argvars, typval_T *rettv);
188static void f_getcurpos(typval_T *argvars, typval_T *rettv);
189static void f_getpos(typval_T *argvars, typval_T *rettv);
190static void f_getqflist(typval_T *argvars, typval_T *rettv);
191static void f_getreg(typval_T *argvars, typval_T *rettv);
192static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200193static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200194static void f_gettabvar(typval_T *argvars, typval_T *rettv);
195static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200196static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200197static void f_getwinposx(typval_T *argvars, typval_T *rettv);
198static void f_getwinposy(typval_T *argvars, typval_T *rettv);
199static void f_getwinvar(typval_T *argvars, typval_T *rettv);
200static void f_glob(typval_T *argvars, typval_T *rettv);
201static void f_globpath(typval_T *argvars, typval_T *rettv);
202static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
203static void f_has(typval_T *argvars, typval_T *rettv);
204static void f_has_key(typval_T *argvars, typval_T *rettv);
205static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
206static void f_hasmapto(typval_T *argvars, typval_T *rettv);
207static void f_histadd(typval_T *argvars, typval_T *rettv);
208static void f_histdel(typval_T *argvars, typval_T *rettv);
209static void f_histget(typval_T *argvars, typval_T *rettv);
210static void f_histnr(typval_T *argvars, typval_T *rettv);
211static void f_hlID(typval_T *argvars, typval_T *rettv);
212static void f_hlexists(typval_T *argvars, typval_T *rettv);
213static void f_hostname(typval_T *argvars, typval_T *rettv);
214static void f_iconv(typval_T *argvars, typval_T *rettv);
215static void f_indent(typval_T *argvars, typval_T *rettv);
216static void f_index(typval_T *argvars, typval_T *rettv);
217static void f_input(typval_T *argvars, typval_T *rettv);
218static void f_inputdialog(typval_T *argvars, typval_T *rettv);
219static void f_inputlist(typval_T *argvars, typval_T *rettv);
220static void f_inputrestore(typval_T *argvars, typval_T *rettv);
221static void f_inputsave(typval_T *argvars, typval_T *rettv);
222static void f_inputsecret(typval_T *argvars, typval_T *rettv);
223static void f_insert(typval_T *argvars, typval_T *rettv);
224static void f_invert(typval_T *argvars, typval_T *rettv);
225static void f_isdirectory(typval_T *argvars, typval_T *rettv);
226static void f_islocked(typval_T *argvars, typval_T *rettv);
227#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
228static void f_isnan(typval_T *argvars, typval_T *rettv);
229#endif
230static void f_items(typval_T *argvars, typval_T *rettv);
231#ifdef FEAT_JOB_CHANNEL
232static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
233static void f_job_info(typval_T *argvars, typval_T *rettv);
234static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
235static void f_job_start(typval_T *argvars, typval_T *rettv);
236static void f_job_stop(typval_T *argvars, typval_T *rettv);
237static void f_job_status(typval_T *argvars, typval_T *rettv);
238#endif
239static void f_join(typval_T *argvars, typval_T *rettv);
240static void f_js_decode(typval_T *argvars, typval_T *rettv);
241static void f_js_encode(typval_T *argvars, typval_T *rettv);
242static void f_json_decode(typval_T *argvars, typval_T *rettv);
243static void f_json_encode(typval_T *argvars, typval_T *rettv);
244static void f_keys(typval_T *argvars, typval_T *rettv);
245static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
246static void f_len(typval_T *argvars, typval_T *rettv);
247static void f_libcall(typval_T *argvars, typval_T *rettv);
248static void f_libcallnr(typval_T *argvars, typval_T *rettv);
249static void f_line(typval_T *argvars, typval_T *rettv);
250static void f_line2byte(typval_T *argvars, typval_T *rettv);
251static void f_lispindent(typval_T *argvars, typval_T *rettv);
252static void f_localtime(typval_T *argvars, typval_T *rettv);
253#ifdef FEAT_FLOAT
254static void f_log(typval_T *argvars, typval_T *rettv);
255static void f_log10(typval_T *argvars, typval_T *rettv);
256#endif
257#ifdef FEAT_LUA
258static void f_luaeval(typval_T *argvars, typval_T *rettv);
259#endif
260static void f_map(typval_T *argvars, typval_T *rettv);
261static void f_maparg(typval_T *argvars, typval_T *rettv);
262static void f_mapcheck(typval_T *argvars, typval_T *rettv);
263static void f_match(typval_T *argvars, typval_T *rettv);
264static void f_matchadd(typval_T *argvars, typval_T *rettv);
265static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
266static void f_matcharg(typval_T *argvars, typval_T *rettv);
267static void f_matchdelete(typval_T *argvars, typval_T *rettv);
268static void f_matchend(typval_T *argvars, typval_T *rettv);
269static void f_matchlist(typval_T *argvars, typval_T *rettv);
270static void f_matchstr(typval_T *argvars, typval_T *rettv);
271static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
272static void f_max(typval_T *argvars, typval_T *rettv);
273static void f_min(typval_T *argvars, typval_T *rettv);
274#ifdef vim_mkdir
275static void f_mkdir(typval_T *argvars, typval_T *rettv);
276#endif
277static void f_mode(typval_T *argvars, typval_T *rettv);
278#ifdef FEAT_MZSCHEME
279static void f_mzeval(typval_T *argvars, typval_T *rettv);
280#endif
281static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
282static void f_nr2char(typval_T *argvars, typval_T *rettv);
283static void f_or(typval_T *argvars, typval_T *rettv);
284static void f_pathshorten(typval_T *argvars, typval_T *rettv);
285#ifdef FEAT_PERL
286static void f_perleval(typval_T *argvars, typval_T *rettv);
287#endif
288#ifdef FEAT_FLOAT
289static void f_pow(typval_T *argvars, typval_T *rettv);
290#endif
291static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
292static void f_printf(typval_T *argvars, typval_T *rettv);
293static void f_pumvisible(typval_T *argvars, typval_T *rettv);
294#ifdef FEAT_PYTHON3
295static void f_py3eval(typval_T *argvars, typval_T *rettv);
296#endif
297#ifdef FEAT_PYTHON
298static void f_pyeval(typval_T *argvars, typval_T *rettv);
299#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100300#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
301static void f_pyxeval(typval_T *argvars, typval_T *rettv);
302#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200303static void f_range(typval_T *argvars, typval_T *rettv);
304static void f_readfile(typval_T *argvars, typval_T *rettv);
305static void f_reltime(typval_T *argvars, typval_T *rettv);
306#ifdef FEAT_FLOAT
307static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
308#endif
309static void f_reltimestr(typval_T *argvars, typval_T *rettv);
310static void f_remote_expr(typval_T *argvars, typval_T *rettv);
311static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
312static void f_remote_peek(typval_T *argvars, typval_T *rettv);
313static void f_remote_read(typval_T *argvars, typval_T *rettv);
314static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100315static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200316static void f_remove(typval_T *argvars, typval_T *rettv);
317static void f_rename(typval_T *argvars, typval_T *rettv);
318static void f_repeat(typval_T *argvars, typval_T *rettv);
319static void f_resolve(typval_T *argvars, typval_T *rettv);
320static void f_reverse(typval_T *argvars, typval_T *rettv);
321#ifdef FEAT_FLOAT
322static void f_round(typval_T *argvars, typval_T *rettv);
323#endif
324static void f_screenattr(typval_T *argvars, typval_T *rettv);
325static void f_screenchar(typval_T *argvars, typval_T *rettv);
326static void f_screencol(typval_T *argvars, typval_T *rettv);
327static void f_screenrow(typval_T *argvars, typval_T *rettv);
328static void f_search(typval_T *argvars, typval_T *rettv);
329static void f_searchdecl(typval_T *argvars, typval_T *rettv);
330static void f_searchpair(typval_T *argvars, typval_T *rettv);
331static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
332static void f_searchpos(typval_T *argvars, typval_T *rettv);
333static void f_server2client(typval_T *argvars, typval_T *rettv);
334static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200335static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200336static void f_setbufvar(typval_T *argvars, typval_T *rettv);
337static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
338static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
339static void f_setfperm(typval_T *argvars, typval_T *rettv);
340static void f_setline(typval_T *argvars, typval_T *rettv);
341static void f_setloclist(typval_T *argvars, typval_T *rettv);
342static void f_setmatches(typval_T *argvars, typval_T *rettv);
343static void f_setpos(typval_T *argvars, typval_T *rettv);
344static void f_setqflist(typval_T *argvars, typval_T *rettv);
345static void f_setreg(typval_T *argvars, typval_T *rettv);
346static void f_settabvar(typval_T *argvars, typval_T *rettv);
347static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
348static void f_setwinvar(typval_T *argvars, typval_T *rettv);
349#ifdef FEAT_CRYPT
350static void f_sha256(typval_T *argvars, typval_T *rettv);
351#endif /* FEAT_CRYPT */
352static void f_shellescape(typval_T *argvars, typval_T *rettv);
353static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
354static void f_simplify(typval_T *argvars, typval_T *rettv);
355#ifdef FEAT_FLOAT
356static void f_sin(typval_T *argvars, typval_T *rettv);
357static void f_sinh(typval_T *argvars, typval_T *rettv);
358#endif
359static void f_sort(typval_T *argvars, typval_T *rettv);
360static void f_soundfold(typval_T *argvars, typval_T *rettv);
361static void f_spellbadword(typval_T *argvars, typval_T *rettv);
362static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
363static void f_split(typval_T *argvars, typval_T *rettv);
364#ifdef FEAT_FLOAT
365static void f_sqrt(typval_T *argvars, typval_T *rettv);
366static void f_str2float(typval_T *argvars, typval_T *rettv);
367#endif
368static void f_str2nr(typval_T *argvars, typval_T *rettv);
369static void f_strchars(typval_T *argvars, typval_T *rettv);
370#ifdef HAVE_STRFTIME
371static void f_strftime(typval_T *argvars, typval_T *rettv);
372#endif
373static void f_strgetchar(typval_T *argvars, typval_T *rettv);
374static void f_stridx(typval_T *argvars, typval_T *rettv);
375static void f_string(typval_T *argvars, typval_T *rettv);
376static void f_strlen(typval_T *argvars, typval_T *rettv);
377static void f_strcharpart(typval_T *argvars, typval_T *rettv);
378static void f_strpart(typval_T *argvars, typval_T *rettv);
379static void f_strridx(typval_T *argvars, typval_T *rettv);
380static void f_strtrans(typval_T *argvars, typval_T *rettv);
381static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
382static void f_strwidth(typval_T *argvars, typval_T *rettv);
383static void f_submatch(typval_T *argvars, typval_T *rettv);
384static void f_substitute(typval_T *argvars, typval_T *rettv);
385static void f_synID(typval_T *argvars, typval_T *rettv);
386static void f_synIDattr(typval_T *argvars, typval_T *rettv);
387static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
388static void f_synstack(typval_T *argvars, typval_T *rettv);
389static void f_synconcealed(typval_T *argvars, typval_T *rettv);
390static void f_system(typval_T *argvars, typval_T *rettv);
391static void f_systemlist(typval_T *argvars, typval_T *rettv);
392static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
393static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
394static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
395static void f_taglist(typval_T *argvars, typval_T *rettv);
396static void f_tagfiles(typval_T *argvars, typval_T *rettv);
397static void f_tempname(typval_T *argvars, typval_T *rettv);
398static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
399static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200400static void f_test_feedinput(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100401static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200402static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100403static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200404#ifdef FEAT_JOB_CHANNEL
405static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
406#endif
407static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
408#ifdef FEAT_JOB_CHANNEL
409static void f_test_null_job(typval_T *argvars, typval_T *rettv);
410#endif
411static void f_test_null_list(typval_T *argvars, typval_T *rettv);
412static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
413static void f_test_null_string(typval_T *argvars, typval_T *rettv);
414static void f_test_settime(typval_T *argvars, typval_T *rettv);
415#ifdef FEAT_FLOAT
416static void f_tan(typval_T *argvars, typval_T *rettv);
417static void f_tanh(typval_T *argvars, typval_T *rettv);
418#endif
419#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200420static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200421static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200422static void f_timer_start(typval_T *argvars, typval_T *rettv);
423static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200424static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200425#endif
426static void f_tolower(typval_T *argvars, typval_T *rettv);
427static void f_toupper(typval_T *argvars, typval_T *rettv);
428static void f_tr(typval_T *argvars, typval_T *rettv);
429#ifdef FEAT_FLOAT
430static void f_trunc(typval_T *argvars, typval_T *rettv);
431#endif
432static void f_type(typval_T *argvars, typval_T *rettv);
433static void f_undofile(typval_T *argvars, typval_T *rettv);
434static void f_undotree(typval_T *argvars, typval_T *rettv);
435static void f_uniq(typval_T *argvars, typval_T *rettv);
436static void f_values(typval_T *argvars, typval_T *rettv);
437static void f_virtcol(typval_T *argvars, typval_T *rettv);
438static void f_visualmode(typval_T *argvars, typval_T *rettv);
439static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
440static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
441static void f_win_getid(typval_T *argvars, typval_T *rettv);
442static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
443static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
444static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100445static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200446static void f_winbufnr(typval_T *argvars, typval_T *rettv);
447static void f_wincol(typval_T *argvars, typval_T *rettv);
448static void f_winheight(typval_T *argvars, typval_T *rettv);
449static void f_winline(typval_T *argvars, typval_T *rettv);
450static void f_winnr(typval_T *argvars, typval_T *rettv);
451static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
452static void f_winrestview(typval_T *argvars, typval_T *rettv);
453static void f_winsaveview(typval_T *argvars, typval_T *rettv);
454static void f_winwidth(typval_T *argvars, typval_T *rettv);
455static void f_writefile(typval_T *argvars, typval_T *rettv);
456static void f_wordcount(typval_T *argvars, typval_T *rettv);
457static void f_xor(typval_T *argvars, typval_T *rettv);
458
459/*
460 * Array with names and number of arguments of all internal functions
461 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
462 */
463static struct fst
464{
465 char *f_name; /* function name */
466 char f_min_argc; /* minimal number of arguments */
467 char f_max_argc; /* maximal number of arguments */
468 void (*f_func)(typval_T *args, typval_T *rvar);
469 /* implementation of function */
470} functions[] =
471{
472#ifdef FEAT_FLOAT
473 {"abs", 1, 1, f_abs},
474 {"acos", 1, 1, f_acos}, /* WJMc */
475#endif
476 {"add", 2, 2, f_add},
477 {"and", 2, 2, f_and},
478 {"append", 2, 2, f_append},
479 {"argc", 0, 0, f_argc},
480 {"argidx", 0, 0, f_argidx},
481 {"arglistid", 0, 2, f_arglistid},
482 {"argv", 0, 1, f_argv},
483#ifdef FEAT_FLOAT
484 {"asin", 1, 1, f_asin}, /* WJMc */
485#endif
486 {"assert_equal", 2, 3, f_assert_equal},
487 {"assert_exception", 1, 2, f_assert_exception},
488 {"assert_fails", 1, 2, f_assert_fails},
489 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100490 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200491 {"assert_match", 2, 3, f_assert_match},
492 {"assert_notequal", 2, 3, f_assert_notequal},
493 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100494 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200495 {"assert_true", 1, 2, f_assert_true},
496#ifdef FEAT_FLOAT
497 {"atan", 1, 1, f_atan},
498 {"atan2", 2, 2, f_atan2},
499#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100500#ifdef FEAT_BEVAL
501 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100502# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100503 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100504# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100505#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200506 {"browse", 4, 4, f_browse},
507 {"browsedir", 2, 2, f_browsedir},
508 {"bufexists", 1, 1, f_bufexists},
509 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
510 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
511 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
512 {"buflisted", 1, 1, f_buflisted},
513 {"bufloaded", 1, 1, f_bufloaded},
514 {"bufname", 1, 1, f_bufname},
515 {"bufnr", 1, 2, f_bufnr},
516 {"bufwinid", 1, 1, f_bufwinid},
517 {"bufwinnr", 1, 1, f_bufwinnr},
518 {"byte2line", 1, 1, f_byte2line},
519 {"byteidx", 2, 2, f_byteidx},
520 {"byteidxcomp", 2, 2, f_byteidxcomp},
521 {"call", 2, 3, f_call},
522#ifdef FEAT_FLOAT
523 {"ceil", 1, 1, f_ceil},
524#endif
525#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100526 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200527 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200528 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200529 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
530 {"ch_evalraw", 2, 3, f_ch_evalraw},
531 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
532 {"ch_getjob", 1, 1, f_ch_getjob},
533 {"ch_info", 1, 1, f_ch_info},
534 {"ch_log", 1, 2, f_ch_log},
535 {"ch_logfile", 1, 2, f_ch_logfile},
536 {"ch_open", 1, 2, f_ch_open},
537 {"ch_read", 1, 2, f_ch_read},
538 {"ch_readraw", 1, 2, f_ch_readraw},
539 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
540 {"ch_sendraw", 2, 3, f_ch_sendraw},
541 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200542 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200543#endif
544 {"changenr", 0, 0, f_changenr},
545 {"char2nr", 1, 2, f_char2nr},
546 {"cindent", 1, 1, f_cindent},
547 {"clearmatches", 0, 0, f_clearmatches},
548 {"col", 1, 1, f_col},
549#if defined(FEAT_INS_EXPAND)
550 {"complete", 2, 2, f_complete},
551 {"complete_add", 1, 1, f_complete_add},
552 {"complete_check", 0, 0, f_complete_check},
553#endif
554 {"confirm", 1, 4, f_confirm},
555 {"copy", 1, 1, f_copy},
556#ifdef FEAT_FLOAT
557 {"cos", 1, 1, f_cos},
558 {"cosh", 1, 1, f_cosh},
559#endif
560 {"count", 2, 4, f_count},
561 {"cscope_connection",0,3, f_cscope_connection},
562 {"cursor", 1, 3, f_cursor},
563 {"deepcopy", 1, 2, f_deepcopy},
564 {"delete", 1, 2, f_delete},
565 {"did_filetype", 0, 0, f_did_filetype},
566 {"diff_filler", 1, 1, f_diff_filler},
567 {"diff_hlID", 2, 2, f_diff_hlID},
568 {"empty", 1, 1, f_empty},
569 {"escape", 2, 2, f_escape},
570 {"eval", 1, 1, f_eval},
571 {"eventhandler", 0, 0, f_eventhandler},
572 {"executable", 1, 1, f_executable},
573 {"execute", 1, 2, f_execute},
574 {"exepath", 1, 1, f_exepath},
575 {"exists", 1, 1, f_exists},
576#ifdef FEAT_FLOAT
577 {"exp", 1, 1, f_exp},
578#endif
579 {"expand", 1, 3, f_expand},
580 {"extend", 2, 3, f_extend},
581 {"feedkeys", 1, 2, f_feedkeys},
582 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
583 {"filereadable", 1, 1, f_filereadable},
584 {"filewritable", 1, 1, f_filewritable},
585 {"filter", 2, 2, f_filter},
586 {"finddir", 1, 3, f_finddir},
587 {"findfile", 1, 3, f_findfile},
588#ifdef FEAT_FLOAT
589 {"float2nr", 1, 1, f_float2nr},
590 {"floor", 1, 1, f_floor},
591 {"fmod", 2, 2, f_fmod},
592#endif
593 {"fnameescape", 1, 1, f_fnameescape},
594 {"fnamemodify", 2, 2, f_fnamemodify},
595 {"foldclosed", 1, 1, f_foldclosed},
596 {"foldclosedend", 1, 1, f_foldclosedend},
597 {"foldlevel", 1, 1, f_foldlevel},
598 {"foldtext", 0, 0, f_foldtext},
599 {"foldtextresult", 1, 1, f_foldtextresult},
600 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200601 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200602 {"function", 1, 3, f_function},
603 {"garbagecollect", 0, 1, f_garbagecollect},
604 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200605 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200606 {"getbufline", 2, 3, f_getbufline},
607 {"getbufvar", 2, 3, f_getbufvar},
608 {"getchar", 0, 1, f_getchar},
609 {"getcharmod", 0, 0, f_getcharmod},
610 {"getcharsearch", 0, 0, f_getcharsearch},
611 {"getcmdline", 0, 0, f_getcmdline},
612 {"getcmdpos", 0, 0, f_getcmdpos},
613 {"getcmdtype", 0, 0, f_getcmdtype},
614 {"getcmdwintype", 0, 0, f_getcmdwintype},
615#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200616 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200617#endif
618 {"getcurpos", 0, 0, f_getcurpos},
619 {"getcwd", 0, 2, f_getcwd},
620 {"getfontname", 0, 1, f_getfontname},
621 {"getfperm", 1, 1, f_getfperm},
622 {"getfsize", 1, 1, f_getfsize},
623 {"getftime", 1, 1, f_getftime},
624 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100625 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200626 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200627 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200628 {"getmatches", 0, 0, f_getmatches},
629 {"getpid", 0, 0, f_getpid},
630 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200631 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200632 {"getreg", 0, 3, f_getreg},
633 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200634 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200635 {"gettabvar", 2, 3, f_gettabvar},
636 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200637 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200638 {"getwinposx", 0, 0, f_getwinposx},
639 {"getwinposy", 0, 0, f_getwinposy},
640 {"getwinvar", 2, 3, f_getwinvar},
641 {"glob", 1, 4, f_glob},
642 {"glob2regpat", 1, 1, f_glob2regpat},
643 {"globpath", 2, 5, f_globpath},
644 {"has", 1, 1, f_has},
645 {"has_key", 2, 2, f_has_key},
646 {"haslocaldir", 0, 2, f_haslocaldir},
647 {"hasmapto", 1, 3, f_hasmapto},
648 {"highlightID", 1, 1, f_hlID}, /* obsolete */
649 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
650 {"histadd", 2, 2, f_histadd},
651 {"histdel", 1, 2, f_histdel},
652 {"histget", 1, 2, f_histget},
653 {"histnr", 1, 1, f_histnr},
654 {"hlID", 1, 1, f_hlID},
655 {"hlexists", 1, 1, f_hlexists},
656 {"hostname", 0, 0, f_hostname},
657 {"iconv", 3, 3, f_iconv},
658 {"indent", 1, 1, f_indent},
659 {"index", 2, 4, f_index},
660 {"input", 1, 3, f_input},
661 {"inputdialog", 1, 3, f_inputdialog},
662 {"inputlist", 1, 1, f_inputlist},
663 {"inputrestore", 0, 0, f_inputrestore},
664 {"inputsave", 0, 0, f_inputsave},
665 {"inputsecret", 1, 2, f_inputsecret},
666 {"insert", 2, 3, f_insert},
667 {"invert", 1, 1, f_invert},
668 {"isdirectory", 1, 1, f_isdirectory},
669 {"islocked", 1, 1, f_islocked},
670#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
671 {"isnan", 1, 1, f_isnan},
672#endif
673 {"items", 1, 1, f_items},
674#ifdef FEAT_JOB_CHANNEL
675 {"job_getchannel", 1, 1, f_job_getchannel},
676 {"job_info", 1, 1, f_job_info},
677 {"job_setoptions", 2, 2, f_job_setoptions},
678 {"job_start", 1, 2, f_job_start},
679 {"job_status", 1, 1, f_job_status},
680 {"job_stop", 1, 2, f_job_stop},
681#endif
682 {"join", 1, 2, f_join},
683 {"js_decode", 1, 1, f_js_decode},
684 {"js_encode", 1, 1, f_js_encode},
685 {"json_decode", 1, 1, f_json_decode},
686 {"json_encode", 1, 1, f_json_encode},
687 {"keys", 1, 1, f_keys},
688 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
689 {"len", 1, 1, f_len},
690 {"libcall", 3, 3, f_libcall},
691 {"libcallnr", 3, 3, f_libcallnr},
692 {"line", 1, 1, f_line},
693 {"line2byte", 1, 1, f_line2byte},
694 {"lispindent", 1, 1, f_lispindent},
695 {"localtime", 0, 0, f_localtime},
696#ifdef FEAT_FLOAT
697 {"log", 1, 1, f_log},
698 {"log10", 1, 1, f_log10},
699#endif
700#ifdef FEAT_LUA
701 {"luaeval", 1, 2, f_luaeval},
702#endif
703 {"map", 2, 2, f_map},
704 {"maparg", 1, 4, f_maparg},
705 {"mapcheck", 1, 3, f_mapcheck},
706 {"match", 2, 4, f_match},
707 {"matchadd", 2, 5, f_matchadd},
708 {"matchaddpos", 2, 5, f_matchaddpos},
709 {"matcharg", 1, 1, f_matcharg},
710 {"matchdelete", 1, 1, f_matchdelete},
711 {"matchend", 2, 4, f_matchend},
712 {"matchlist", 2, 4, f_matchlist},
713 {"matchstr", 2, 4, f_matchstr},
714 {"matchstrpos", 2, 4, f_matchstrpos},
715 {"max", 1, 1, f_max},
716 {"min", 1, 1, f_min},
717#ifdef vim_mkdir
718 {"mkdir", 1, 3, f_mkdir},
719#endif
720 {"mode", 0, 1, f_mode},
721#ifdef FEAT_MZSCHEME
722 {"mzeval", 1, 1, f_mzeval},
723#endif
724 {"nextnonblank", 1, 1, f_nextnonblank},
725 {"nr2char", 1, 2, f_nr2char},
726 {"or", 2, 2, f_or},
727 {"pathshorten", 1, 1, f_pathshorten},
728#ifdef FEAT_PERL
729 {"perleval", 1, 1, f_perleval},
730#endif
731#ifdef FEAT_FLOAT
732 {"pow", 2, 2, f_pow},
733#endif
734 {"prevnonblank", 1, 1, f_prevnonblank},
735 {"printf", 2, 19, f_printf},
736 {"pumvisible", 0, 0, f_pumvisible},
737#ifdef FEAT_PYTHON3
738 {"py3eval", 1, 1, f_py3eval},
739#endif
740#ifdef FEAT_PYTHON
741 {"pyeval", 1, 1, f_pyeval},
742#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100743#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
744 {"pyxeval", 1, 1, f_pyxeval},
745#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200746 {"range", 1, 3, f_range},
747 {"readfile", 1, 3, f_readfile},
748 {"reltime", 0, 2, f_reltime},
749#ifdef FEAT_FLOAT
750 {"reltimefloat", 1, 1, f_reltimefloat},
751#endif
752 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100753 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200754 {"remote_foreground", 1, 1, f_remote_foreground},
755 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100756 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200757 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100758 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200759 {"remove", 2, 3, f_remove},
760 {"rename", 2, 2, f_rename},
761 {"repeat", 2, 2, f_repeat},
762 {"resolve", 1, 1, f_resolve},
763 {"reverse", 1, 1, f_reverse},
764#ifdef FEAT_FLOAT
765 {"round", 1, 1, f_round},
766#endif
767 {"screenattr", 2, 2, f_screenattr},
768 {"screenchar", 2, 2, f_screenchar},
769 {"screencol", 0, 0, f_screencol},
770 {"screenrow", 0, 0, f_screenrow},
771 {"search", 1, 4, f_search},
772 {"searchdecl", 1, 3, f_searchdecl},
773 {"searchpair", 3, 7, f_searchpair},
774 {"searchpairpos", 3, 7, f_searchpairpos},
775 {"searchpos", 1, 4, f_searchpos},
776 {"server2client", 2, 2, f_server2client},
777 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200778 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200779 {"setbufvar", 3, 3, f_setbufvar},
780 {"setcharsearch", 1, 1, f_setcharsearch},
781 {"setcmdpos", 1, 1, f_setcmdpos},
782 {"setfperm", 2, 2, f_setfperm},
783 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200784 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200785 {"setmatches", 1, 1, f_setmatches},
786 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200787 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200788 {"setreg", 2, 3, f_setreg},
789 {"settabvar", 3, 3, f_settabvar},
790 {"settabwinvar", 4, 4, f_settabwinvar},
791 {"setwinvar", 3, 3, f_setwinvar},
792#ifdef FEAT_CRYPT
793 {"sha256", 1, 1, f_sha256},
794#endif
795 {"shellescape", 1, 2, f_shellescape},
796 {"shiftwidth", 0, 0, f_shiftwidth},
797 {"simplify", 1, 1, f_simplify},
798#ifdef FEAT_FLOAT
799 {"sin", 1, 1, f_sin},
800 {"sinh", 1, 1, f_sinh},
801#endif
802 {"sort", 1, 3, f_sort},
803 {"soundfold", 1, 1, f_soundfold},
804 {"spellbadword", 0, 1, f_spellbadword},
805 {"spellsuggest", 1, 3, f_spellsuggest},
806 {"split", 1, 3, f_split},
807#ifdef FEAT_FLOAT
808 {"sqrt", 1, 1, f_sqrt},
809 {"str2float", 1, 1, f_str2float},
810#endif
811 {"str2nr", 1, 2, f_str2nr},
812 {"strcharpart", 2, 3, f_strcharpart},
813 {"strchars", 1, 2, f_strchars},
814 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
815#ifdef HAVE_STRFTIME
816 {"strftime", 1, 2, f_strftime},
817#endif
818 {"strgetchar", 2, 2, f_strgetchar},
819 {"stridx", 2, 3, f_stridx},
820 {"string", 1, 1, f_string},
821 {"strlen", 1, 1, f_strlen},
822 {"strpart", 2, 3, f_strpart},
823 {"strridx", 2, 3, f_strridx},
824 {"strtrans", 1, 1, f_strtrans},
825 {"strwidth", 1, 1, f_strwidth},
826 {"submatch", 1, 2, f_submatch},
827 {"substitute", 4, 4, f_substitute},
828 {"synID", 3, 3, f_synID},
829 {"synIDattr", 2, 3, f_synIDattr},
830 {"synIDtrans", 1, 1, f_synIDtrans},
831 {"synconcealed", 2, 2, f_synconcealed},
832 {"synstack", 2, 2, f_synstack},
833 {"system", 1, 2, f_system},
834 {"systemlist", 1, 2, f_systemlist},
835 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
836 {"tabpagenr", 0, 1, f_tabpagenr},
837 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
838 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100839 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200840#ifdef FEAT_FLOAT
841 {"tan", 1, 1, f_tan},
842 {"tanh", 1, 1, f_tanh},
843#endif
844 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200845#ifdef FEAT_TERMINAL
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200846 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200847 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200848 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200849 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200850 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200851 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200852 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200853 {"term_getstatus", 1, 1, f_term_getstatus},
854 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200855 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200856 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200857 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200858 {"term_sendkeys", 2, 2, f_term_sendkeys},
859 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200860 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200861#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200862 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
863 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200864 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200865 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100866 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200867#ifdef FEAT_JOB_CHANNEL
868 {"test_null_channel", 0, 0, f_test_null_channel},
869#endif
870 {"test_null_dict", 0, 0, f_test_null_dict},
871#ifdef FEAT_JOB_CHANNEL
872 {"test_null_job", 0, 0, f_test_null_job},
873#endif
874 {"test_null_list", 0, 0, f_test_null_list},
875 {"test_null_partial", 0, 0, f_test_null_partial},
876 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100877 {"test_override", 2, 2, f_test_override},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200878 {"test_settime", 1, 1, f_test_settime},
879#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200880 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200881 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200882 {"timer_start", 2, 3, f_timer_start},
883 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200884 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200885#endif
886 {"tolower", 1, 1, f_tolower},
887 {"toupper", 1, 1, f_toupper},
888 {"tr", 3, 3, f_tr},
889#ifdef FEAT_FLOAT
890 {"trunc", 1, 1, f_trunc},
891#endif
892 {"type", 1, 1, f_type},
893 {"undofile", 1, 1, f_undofile},
894 {"undotree", 0, 0, f_undotree},
895 {"uniq", 1, 3, f_uniq},
896 {"values", 1, 1, f_values},
897 {"virtcol", 1, 1, f_virtcol},
898 {"visualmode", 0, 1, f_visualmode},
899 {"wildmenumode", 0, 0, f_wildmenumode},
900 {"win_findbuf", 1, 1, f_win_findbuf},
901 {"win_getid", 0, 2, f_win_getid},
902 {"win_gotoid", 1, 1, f_win_gotoid},
903 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
904 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100905 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200906 {"winbufnr", 1, 1, f_winbufnr},
907 {"wincol", 0, 0, f_wincol},
908 {"winheight", 1, 1, f_winheight},
909 {"winline", 0, 0, f_winline},
910 {"winnr", 0, 1, f_winnr},
911 {"winrestcmd", 0, 0, f_winrestcmd},
912 {"winrestview", 1, 1, f_winrestview},
913 {"winsaveview", 0, 0, f_winsaveview},
914 {"winwidth", 1, 1, f_winwidth},
915 {"wordcount", 0, 0, f_wordcount},
916 {"writefile", 2, 3, f_writefile},
917 {"xor", 2, 2, f_xor},
918};
919
920#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
921
922/*
923 * Function given to ExpandGeneric() to obtain the list of internal
924 * or user defined function names.
925 */
926 char_u *
927get_function_name(expand_T *xp, int idx)
928{
929 static int intidx = -1;
930 char_u *name;
931
932 if (idx == 0)
933 intidx = -1;
934 if (intidx < 0)
935 {
936 name = get_user_func_name(xp, idx);
937 if (name != NULL)
938 return name;
939 }
940 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
941 {
942 STRCPY(IObuff, functions[intidx].f_name);
943 STRCAT(IObuff, "(");
944 if (functions[intidx].f_max_argc == 0)
945 STRCAT(IObuff, ")");
946 return IObuff;
947 }
948
949 return NULL;
950}
951
952/*
953 * Function given to ExpandGeneric() to obtain the list of internal or
954 * user defined variable or function names.
955 */
956 char_u *
957get_expr_name(expand_T *xp, int idx)
958{
959 static int intidx = -1;
960 char_u *name;
961
962 if (idx == 0)
963 intidx = -1;
964 if (intidx < 0)
965 {
966 name = get_function_name(xp, idx);
967 if (name != NULL)
968 return name;
969 }
970 return get_user_var_name(xp, ++intidx);
971}
972
973#endif /* FEAT_CMDL_COMPL */
974
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200975/*
976 * Find internal function in table above.
977 * Return index, or -1 if not found
978 */
979 int
980find_internal_func(
981 char_u *name) /* name of the function */
982{
983 int first = 0;
984 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
985 int cmp;
986 int x;
987
988 /*
989 * Find the function name in the table. Binary search.
990 */
991 while (first <= last)
992 {
993 x = first + ((unsigned)(last - first) >> 1);
994 cmp = STRCMP(name, functions[x].f_name);
995 if (cmp < 0)
996 last = x - 1;
997 else if (cmp > 0)
998 first = x + 1;
999 else
1000 return x;
1001 }
1002 return -1;
1003}
1004
1005 int
1006call_internal_func(
1007 char_u *name,
1008 int argcount,
1009 typval_T *argvars,
1010 typval_T *rettv)
1011{
1012 int i;
1013
1014 i = find_internal_func(name);
1015 if (i < 0)
1016 return ERROR_UNKNOWN;
1017 if (argcount < functions[i].f_min_argc)
1018 return ERROR_TOOFEW;
1019 if (argcount > functions[i].f_max_argc)
1020 return ERROR_TOOMANY;
1021 argvars[argcount].v_type = VAR_UNKNOWN;
1022 functions[i].f_func(argvars, rettv);
1023 return ERROR_NONE;
1024}
1025
1026/*
1027 * Return TRUE for a non-zero Number and a non-empty String.
1028 */
1029 static int
1030non_zero_arg(typval_T *argvars)
1031{
1032 return ((argvars[0].v_type == VAR_NUMBER
1033 && argvars[0].vval.v_number != 0)
1034 || (argvars[0].v_type == VAR_SPECIAL
1035 && argvars[0].vval.v_number == VVAL_TRUE)
1036 || (argvars[0].v_type == VAR_STRING
1037 && argvars[0].vval.v_string != NULL
1038 && *argvars[0].vval.v_string != NUL));
1039}
1040
1041/*
1042 * Get the lnum from the first argument.
1043 * Also accepts ".", "$", etc., but that only works for the current buffer.
1044 * Returns -1 on error.
1045 */
1046 static linenr_T
1047get_tv_lnum(typval_T *argvars)
1048{
1049 typval_T rettv;
1050 linenr_T lnum;
1051
1052 lnum = (linenr_T)get_tv_number_chk(&argvars[0], NULL);
1053 if (lnum == 0) /* no valid number, try using line() */
1054 {
1055 rettv.v_type = VAR_NUMBER;
1056 f_line(argvars, &rettv);
1057 lnum = (linenr_T)rettv.vval.v_number;
1058 clear_tv(&rettv);
1059 }
1060 return lnum;
1061}
1062
1063#ifdef FEAT_FLOAT
1064static int get_float_arg(typval_T *argvars, float_T *f);
1065
1066/*
1067 * Get the float value of "argvars[0]" into "f".
1068 * Returns FAIL when the argument is not a Number or Float.
1069 */
1070 static int
1071get_float_arg(typval_T *argvars, float_T *f)
1072{
1073 if (argvars[0].v_type == VAR_FLOAT)
1074 {
1075 *f = argvars[0].vval.v_float;
1076 return OK;
1077 }
1078 if (argvars[0].v_type == VAR_NUMBER)
1079 {
1080 *f = (float_T)argvars[0].vval.v_number;
1081 return OK;
1082 }
1083 EMSG(_("E808: Number or Float required"));
1084 return FAIL;
1085}
1086
1087/*
1088 * "abs(expr)" function
1089 */
1090 static void
1091f_abs(typval_T *argvars, typval_T *rettv)
1092{
1093 if (argvars[0].v_type == VAR_FLOAT)
1094 {
1095 rettv->v_type = VAR_FLOAT;
1096 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1097 }
1098 else
1099 {
1100 varnumber_T n;
1101 int error = FALSE;
1102
1103 n = get_tv_number_chk(&argvars[0], &error);
1104 if (error)
1105 rettv->vval.v_number = -1;
1106 else if (n > 0)
1107 rettv->vval.v_number = n;
1108 else
1109 rettv->vval.v_number = -n;
1110 }
1111}
1112
1113/*
1114 * "acos()" function
1115 */
1116 static void
1117f_acos(typval_T *argvars, typval_T *rettv)
1118{
1119 float_T f = 0.0;
1120
1121 rettv->v_type = VAR_FLOAT;
1122 if (get_float_arg(argvars, &f) == OK)
1123 rettv->vval.v_float = acos(f);
1124 else
1125 rettv->vval.v_float = 0.0;
1126}
1127#endif
1128
1129/*
1130 * "add(list, item)" function
1131 */
1132 static void
1133f_add(typval_T *argvars, typval_T *rettv)
1134{
1135 list_T *l;
1136
1137 rettv->vval.v_number = 1; /* Default: Failed */
1138 if (argvars[0].v_type == VAR_LIST)
1139 {
1140 if ((l = argvars[0].vval.v_list) != NULL
1141 && !tv_check_lock(l->lv_lock,
1142 (char_u *)N_("add() argument"), TRUE)
1143 && list_append_tv(l, &argvars[1]) == OK)
1144 copy_tv(&argvars[0], rettv);
1145 }
1146 else
1147 EMSG(_(e_listreq));
1148}
1149
1150/*
1151 * "and(expr, expr)" function
1152 */
1153 static void
1154f_and(typval_T *argvars, typval_T *rettv)
1155{
1156 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
1157 & get_tv_number_chk(&argvars[1], NULL);
1158}
1159
1160/*
1161 * "append(lnum, string/list)" function
1162 */
1163 static void
1164f_append(typval_T *argvars, typval_T *rettv)
1165{
1166 long lnum;
1167 char_u *line;
1168 list_T *l = NULL;
1169 listitem_T *li = NULL;
1170 typval_T *tv;
1171 long added = 0;
1172
1173 /* When coming here from Insert mode, sync undo, so that this can be
1174 * undone separately from what was previously inserted. */
1175 if (u_sync_once == 2)
1176 {
1177 u_sync_once = 1; /* notify that u_sync() was called */
1178 u_sync(TRUE);
1179 }
1180
1181 lnum = get_tv_lnum(argvars);
1182 if (lnum >= 0
1183 && lnum <= curbuf->b_ml.ml_line_count
1184 && u_save(lnum, lnum + 1) == OK)
1185 {
1186 if (argvars[1].v_type == VAR_LIST)
1187 {
1188 l = argvars[1].vval.v_list;
1189 if (l == NULL)
1190 return;
1191 li = l->lv_first;
1192 }
1193 for (;;)
1194 {
1195 if (l == NULL)
1196 tv = &argvars[1]; /* append a string */
1197 else if (li == NULL)
1198 break; /* end of list */
1199 else
1200 tv = &li->li_tv; /* append item from list */
1201 line = get_tv_string_chk(tv);
1202 if (line == NULL) /* type error */
1203 {
1204 rettv->vval.v_number = 1; /* Failed */
1205 break;
1206 }
1207 ml_append(lnum + added, line, (colnr_T)0, FALSE);
1208 ++added;
1209 if (l == NULL)
1210 break;
1211 li = li->li_next;
1212 }
1213
1214 appended_lines_mark(lnum, added);
1215 if (curwin->w_cursor.lnum > lnum)
1216 curwin->w_cursor.lnum += added;
1217 }
1218 else
1219 rettv->vval.v_number = 1; /* Failed */
1220}
1221
1222/*
1223 * "argc()" function
1224 */
1225 static void
1226f_argc(typval_T *argvars UNUSED, typval_T *rettv)
1227{
1228 rettv->vval.v_number = ARGCOUNT;
1229}
1230
1231/*
1232 * "argidx()" function
1233 */
1234 static void
1235f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1236{
1237 rettv->vval.v_number = curwin->w_arg_idx;
1238}
1239
1240/*
1241 * "arglistid()" function
1242 */
1243 static void
1244f_arglistid(typval_T *argvars, typval_T *rettv)
1245{
1246 win_T *wp;
1247
1248 rettv->vval.v_number = -1;
1249 wp = find_tabwin(&argvars[0], &argvars[1]);
1250 if (wp != NULL)
1251 rettv->vval.v_number = wp->w_alist->id;
1252}
1253
1254/*
1255 * "argv(nr)" function
1256 */
1257 static void
1258f_argv(typval_T *argvars, typval_T *rettv)
1259{
1260 int idx;
1261
1262 if (argvars[0].v_type != VAR_UNKNOWN)
1263 {
1264 idx = (int)get_tv_number_chk(&argvars[0], NULL);
1265 if (idx >= 0 && idx < ARGCOUNT)
1266 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
1267 else
1268 rettv->vval.v_string = NULL;
1269 rettv->v_type = VAR_STRING;
1270 }
1271 else if (rettv_list_alloc(rettv) == OK)
1272 for (idx = 0; idx < ARGCOUNT; ++idx)
1273 list_append_string(rettv->vval.v_list,
1274 alist_name(&ARGLIST[idx]), -1);
1275}
1276
1277/*
1278 * "assert_equal(expected, actual[, msg])" function
1279 */
1280 static void
1281f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
1282{
1283 assert_equal_common(argvars, ASSERT_EQUAL);
1284}
1285
1286/*
1287 * "assert_notequal(expected, actual[, msg])" function
1288 */
1289 static void
1290f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED)
1291{
1292 assert_equal_common(argvars, ASSERT_NOTEQUAL);
1293}
1294
1295/*
1296 * "assert_exception(string[, msg])" function
1297 */
1298 static void
1299f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
1300{
1301 assert_exception(argvars);
1302}
1303
1304/*
1305 * "assert_fails(cmd [, error])" function
1306 */
1307 static void
1308f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
1309{
1310 assert_fails(argvars);
1311}
1312
1313/*
1314 * "assert_false(actual[, msg])" function
1315 */
1316 static void
1317f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
1318{
1319 assert_bool(argvars, FALSE);
1320}
1321
1322/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001323 * "assert_inrange(lower, upper[, msg])" function
1324 */
1325 static void
1326f_assert_inrange(typval_T *argvars, typval_T *rettv UNUSED)
1327{
1328 assert_inrange(argvars);
1329}
1330
1331/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001332 * "assert_match(pattern, actual[, msg])" function
1333 */
1334 static void
1335f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
1336{
1337 assert_match_common(argvars, ASSERT_MATCH);
1338}
1339
1340/*
1341 * "assert_notmatch(pattern, actual[, msg])" function
1342 */
1343 static void
1344f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED)
1345{
1346 assert_match_common(argvars, ASSERT_NOTMATCH);
1347}
1348
1349/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001350 * "assert_report(msg)" function
1351 */
1352 static void
1353f_assert_report(typval_T *argvars, typval_T *rettv UNUSED)
1354{
1355 assert_report(argvars);
1356}
1357
1358/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001359 * "assert_true(actual[, msg])" function
1360 */
1361 static void
1362f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
1363{
1364 assert_bool(argvars, TRUE);
1365}
1366
1367#ifdef FEAT_FLOAT
1368/*
1369 * "asin()" function
1370 */
1371 static void
1372f_asin(typval_T *argvars, typval_T *rettv)
1373{
1374 float_T f = 0.0;
1375
1376 rettv->v_type = VAR_FLOAT;
1377 if (get_float_arg(argvars, &f) == OK)
1378 rettv->vval.v_float = asin(f);
1379 else
1380 rettv->vval.v_float = 0.0;
1381}
1382
1383/*
1384 * "atan()" function
1385 */
1386 static void
1387f_atan(typval_T *argvars, typval_T *rettv)
1388{
1389 float_T f = 0.0;
1390
1391 rettv->v_type = VAR_FLOAT;
1392 if (get_float_arg(argvars, &f) == OK)
1393 rettv->vval.v_float = atan(f);
1394 else
1395 rettv->vval.v_float = 0.0;
1396}
1397
1398/*
1399 * "atan2()" function
1400 */
1401 static void
1402f_atan2(typval_T *argvars, typval_T *rettv)
1403{
1404 float_T fx = 0.0, fy = 0.0;
1405
1406 rettv->v_type = VAR_FLOAT;
1407 if (get_float_arg(argvars, &fx) == OK
1408 && get_float_arg(&argvars[1], &fy) == OK)
1409 rettv->vval.v_float = atan2(fx, fy);
1410 else
1411 rettv->vval.v_float = 0.0;
1412}
1413#endif
1414
1415/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001416 * "balloon_show()" function
1417 */
1418#ifdef FEAT_BEVAL
1419 static void
1420f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1421{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001422 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001423 {
1424 if (argvars[0].v_type == VAR_LIST
1425# ifdef FEAT_GUI
1426 && !gui.in_use
1427# endif
1428 )
1429 post_balloon(balloonEval, NULL, argvars[0].vval.v_list);
1430 else
1431 post_balloon(balloonEval, get_tv_string_chk(&argvars[0]), NULL);
1432 }
1433}
1434
Bram Moolenaar669a8282017-11-19 20:13:05 +01001435# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001436 static void
1437f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1438{
1439 if (rettv_list_alloc(rettv) == OK)
1440 {
1441 char_u *msg = get_tv_string_chk(&argvars[0]);
1442
1443 if (msg != NULL)
1444 {
1445 pumitem_T *array;
1446 int size = split_message(msg, &array);
1447 int i;
1448
1449 /* Skip the first and last item, they are always empty. */
1450 for (i = 1; i < size - 1; ++i)
1451 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001452 while (size > 0)
1453 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001454 vim_free(array);
1455 }
1456 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001457}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001458# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001459#endif
1460
1461/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001462 * "browse(save, title, initdir, default)" function
1463 */
1464 static void
1465f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1466{
1467#ifdef FEAT_BROWSE
1468 int save;
1469 char_u *title;
1470 char_u *initdir;
1471 char_u *defname;
1472 char_u buf[NUMBUFLEN];
1473 char_u buf2[NUMBUFLEN];
1474 int error = FALSE;
1475
1476 save = (int)get_tv_number_chk(&argvars[0], &error);
1477 title = get_tv_string_chk(&argvars[1]);
1478 initdir = get_tv_string_buf_chk(&argvars[2], buf);
1479 defname = get_tv_string_buf_chk(&argvars[3], buf2);
1480
1481 if (error || title == NULL || initdir == NULL || defname == NULL)
1482 rettv->vval.v_string = NULL;
1483 else
1484 rettv->vval.v_string =
1485 do_browse(save ? BROWSE_SAVE : 0,
1486 title, defname, NULL, initdir, NULL, curbuf);
1487#else
1488 rettv->vval.v_string = NULL;
1489#endif
1490 rettv->v_type = VAR_STRING;
1491}
1492
1493/*
1494 * "browsedir(title, initdir)" function
1495 */
1496 static void
1497f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1498{
1499#ifdef FEAT_BROWSE
1500 char_u *title;
1501 char_u *initdir;
1502 char_u buf[NUMBUFLEN];
1503
1504 title = get_tv_string_chk(&argvars[0]);
1505 initdir = get_tv_string_buf_chk(&argvars[1], buf);
1506
1507 if (title == NULL || initdir == NULL)
1508 rettv->vval.v_string = NULL;
1509 else
1510 rettv->vval.v_string = do_browse(BROWSE_DIR,
1511 title, NULL, NULL, initdir, NULL, curbuf);
1512#else
1513 rettv->vval.v_string = NULL;
1514#endif
1515 rettv->v_type = VAR_STRING;
1516}
1517
1518static buf_T *find_buffer(typval_T *avar);
1519
1520/*
1521 * Find a buffer by number or exact name.
1522 */
1523 static buf_T *
1524find_buffer(typval_T *avar)
1525{
1526 buf_T *buf = NULL;
1527
1528 if (avar->v_type == VAR_NUMBER)
1529 buf = buflist_findnr((int)avar->vval.v_number);
1530 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1531 {
1532 buf = buflist_findname_exp(avar->vval.v_string);
1533 if (buf == NULL)
1534 {
1535 /* No full path name match, try a match with a URL or a "nofile"
1536 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001537 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001538 if (buf->b_fname != NULL
1539 && (path_with_url(buf->b_fname)
1540#ifdef FEAT_QUICKFIX
1541 || bt_nofile(buf)
1542#endif
1543 )
1544 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1545 break;
1546 }
1547 }
1548 return buf;
1549}
1550
1551/*
1552 * "bufexists(expr)" function
1553 */
1554 static void
1555f_bufexists(typval_T *argvars, typval_T *rettv)
1556{
1557 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1558}
1559
1560/*
1561 * "buflisted(expr)" function
1562 */
1563 static void
1564f_buflisted(typval_T *argvars, typval_T *rettv)
1565{
1566 buf_T *buf;
1567
1568 buf = find_buffer(&argvars[0]);
1569 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1570}
1571
1572/*
1573 * "bufloaded(expr)" function
1574 */
1575 static void
1576f_bufloaded(typval_T *argvars, typval_T *rettv)
1577{
1578 buf_T *buf;
1579
1580 buf = find_buffer(&argvars[0]);
1581 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1582}
1583
1584 buf_T *
1585buflist_find_by_name(char_u *name, int curtab_only)
1586{
1587 int save_magic;
1588 char_u *save_cpo;
1589 buf_T *buf;
1590
1591 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1592 save_magic = p_magic;
1593 p_magic = TRUE;
1594 save_cpo = p_cpo;
1595 p_cpo = (char_u *)"";
1596
1597 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1598 TRUE, FALSE, curtab_only));
1599
1600 p_magic = save_magic;
1601 p_cpo = save_cpo;
1602 return buf;
1603}
1604
1605/*
1606 * Get buffer by number or pattern.
1607 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001608 buf_T *
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001609get_buf_tv(typval_T *tv, int curtab_only)
1610{
1611 char_u *name = tv->vval.v_string;
1612 buf_T *buf;
1613
1614 if (tv->v_type == VAR_NUMBER)
1615 return buflist_findnr((int)tv->vval.v_number);
1616 if (tv->v_type != VAR_STRING)
1617 return NULL;
1618 if (name == NULL || *name == NUL)
1619 return curbuf;
1620 if (name[0] == '$' && name[1] == NUL)
1621 return lastbuf;
1622
1623 buf = buflist_find_by_name(name, curtab_only);
1624
1625 /* If not found, try expanding the name, like done for bufexists(). */
1626 if (buf == NULL)
1627 buf = find_buffer(tv);
1628
1629 return buf;
1630}
1631
1632/*
1633 * "bufname(expr)" function
1634 */
1635 static void
1636f_bufname(typval_T *argvars, typval_T *rettv)
1637{
1638 buf_T *buf;
1639
1640 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1641 ++emsg_off;
1642 buf = get_buf_tv(&argvars[0], FALSE);
1643 rettv->v_type = VAR_STRING;
1644 if (buf != NULL && buf->b_fname != NULL)
1645 rettv->vval.v_string = vim_strsave(buf->b_fname);
1646 else
1647 rettv->vval.v_string = NULL;
1648 --emsg_off;
1649}
1650
1651/*
1652 * "bufnr(expr)" function
1653 */
1654 static void
1655f_bufnr(typval_T *argvars, typval_T *rettv)
1656{
1657 buf_T *buf;
1658 int error = FALSE;
1659 char_u *name;
1660
1661 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1662 ++emsg_off;
1663 buf = get_buf_tv(&argvars[0], FALSE);
1664 --emsg_off;
1665
1666 /* If the buffer isn't found and the second argument is not zero create a
1667 * new buffer. */
1668 if (buf == NULL
1669 && argvars[1].v_type != VAR_UNKNOWN
1670 && get_tv_number_chk(&argvars[1], &error) != 0
1671 && !error
1672 && (name = get_tv_string_chk(&argvars[0])) != NULL
1673 && !error)
1674 buf = buflist_new(name, NULL, (linenr_T)1, 0);
1675
1676 if (buf != NULL)
1677 rettv->vval.v_number = buf->b_fnum;
1678 else
1679 rettv->vval.v_number = -1;
1680}
1681
1682 static void
1683buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
1684{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001685 win_T *wp;
1686 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001687 buf_T *buf;
1688
1689 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1690 ++emsg_off;
1691 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02001692 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001693 {
1694 ++winnr;
1695 if (wp->w_buffer == buf)
1696 break;
1697 }
1698 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001699 --emsg_off;
1700}
1701
1702/*
1703 * "bufwinid(nr)" function
1704 */
1705 static void
1706f_bufwinid(typval_T *argvars, typval_T *rettv)
1707{
1708 buf_win_common(argvars, rettv, FALSE);
1709}
1710
1711/*
1712 * "bufwinnr(nr)" function
1713 */
1714 static void
1715f_bufwinnr(typval_T *argvars, typval_T *rettv)
1716{
1717 buf_win_common(argvars, rettv, TRUE);
1718}
1719
1720/*
1721 * "byte2line(byte)" function
1722 */
1723 static void
1724f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1725{
1726#ifndef FEAT_BYTEOFF
1727 rettv->vval.v_number = -1;
1728#else
1729 long boff = 0;
1730
1731 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
1732 if (boff < 0)
1733 rettv->vval.v_number = -1;
1734 else
1735 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
1736 (linenr_T)0, &boff);
1737#endif
1738}
1739
1740 static void
1741byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
1742{
1743#ifdef FEAT_MBYTE
1744 char_u *t;
1745#endif
1746 char_u *str;
1747 varnumber_T idx;
1748
1749 str = get_tv_string_chk(&argvars[0]);
1750 idx = get_tv_number_chk(&argvars[1], NULL);
1751 rettv->vval.v_number = -1;
1752 if (str == NULL || idx < 0)
1753 return;
1754
1755#ifdef FEAT_MBYTE
1756 t = str;
1757 for ( ; idx > 0; idx--)
1758 {
1759 if (*t == NUL) /* EOL reached */
1760 return;
1761 if (enc_utf8 && comp)
1762 t += utf_ptr2len(t);
1763 else
1764 t += (*mb_ptr2len)(t);
1765 }
1766 rettv->vval.v_number = (varnumber_T)(t - str);
1767#else
1768 if ((size_t)idx <= STRLEN(str))
1769 rettv->vval.v_number = idx;
1770#endif
1771}
1772
1773/*
1774 * "byteidx()" function
1775 */
1776 static void
1777f_byteidx(typval_T *argvars, typval_T *rettv)
1778{
1779 byteidx(argvars, rettv, FALSE);
1780}
1781
1782/*
1783 * "byteidxcomp()" function
1784 */
1785 static void
1786f_byteidxcomp(typval_T *argvars, typval_T *rettv)
1787{
1788 byteidx(argvars, rettv, TRUE);
1789}
1790
1791/*
1792 * "call(func, arglist [, dict])" function
1793 */
1794 static void
1795f_call(typval_T *argvars, typval_T *rettv)
1796{
1797 char_u *func;
1798 partial_T *partial = NULL;
1799 dict_T *selfdict = NULL;
1800
1801 if (argvars[1].v_type != VAR_LIST)
1802 {
1803 EMSG(_(e_listreq));
1804 return;
1805 }
1806 if (argvars[1].vval.v_list == NULL)
1807 return;
1808
1809 if (argvars[0].v_type == VAR_FUNC)
1810 func = argvars[0].vval.v_string;
1811 else if (argvars[0].v_type == VAR_PARTIAL)
1812 {
1813 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001814 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001815 }
1816 else
1817 func = get_tv_string(&argvars[0]);
1818 if (*func == NUL)
1819 return; /* type error or empty name */
1820
1821 if (argvars[2].v_type != VAR_UNKNOWN)
1822 {
1823 if (argvars[2].v_type != VAR_DICT)
1824 {
1825 EMSG(_(e_dictreq));
1826 return;
1827 }
1828 selfdict = argvars[2].vval.v_dict;
1829 }
1830
1831 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
1832}
1833
1834#ifdef FEAT_FLOAT
1835/*
1836 * "ceil({float})" function
1837 */
1838 static void
1839f_ceil(typval_T *argvars, typval_T *rettv)
1840{
1841 float_T f = 0.0;
1842
1843 rettv->v_type = VAR_FLOAT;
1844 if (get_float_arg(argvars, &f) == OK)
1845 rettv->vval.v_float = ceil(f);
1846 else
1847 rettv->vval.v_float = 0.0;
1848}
1849#endif
1850
1851#ifdef FEAT_JOB_CHANNEL
1852/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01001853 * "ch_canread()" function
1854 */
1855 static void
1856f_ch_canread(typval_T *argvars, typval_T *rettv)
1857{
Bram Moolenaar958dc692016-12-01 15:34:12 +01001858 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01001859
1860 rettv->vval.v_number = 0;
1861 if (channel != NULL)
1862 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
1863 || channel_has_readahead(channel, PART_OUT)
1864 || channel_has_readahead(channel, PART_ERR);
1865}
1866
1867/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001868 * "ch_close()" function
1869 */
1870 static void
1871f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
1872{
1873 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
1874
1875 if (channel != NULL)
1876 {
1877 channel_close(channel, FALSE);
1878 channel_clear(channel);
1879 }
1880}
1881
1882/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02001883 * "ch_close()" function
1884 */
1885 static void
1886f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
1887{
1888 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
1889
1890 if (channel != NULL)
1891 channel_close_in(channel);
1892}
1893
1894/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001895 * "ch_getbufnr()" function
1896 */
1897 static void
1898f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
1899{
1900 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1901
1902 rettv->vval.v_number = -1;
1903 if (channel != NULL)
1904 {
1905 char_u *what = get_tv_string(&argvars[1]);
1906 int part;
1907
1908 if (STRCMP(what, "err") == 0)
1909 part = PART_ERR;
1910 else if (STRCMP(what, "out") == 0)
1911 part = PART_OUT;
1912 else if (STRCMP(what, "in") == 0)
1913 part = PART_IN;
1914 else
1915 part = PART_SOCK;
1916 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
1917 rettv->vval.v_number =
1918 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
1919 }
1920}
1921
1922/*
1923 * "ch_getjob()" function
1924 */
1925 static void
1926f_ch_getjob(typval_T *argvars, typval_T *rettv)
1927{
1928 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1929
1930 if (channel != NULL)
1931 {
1932 rettv->v_type = VAR_JOB;
1933 rettv->vval.v_job = channel->ch_job;
1934 if (channel->ch_job != NULL)
1935 ++channel->ch_job->jv_refcount;
1936 }
1937}
1938
1939/*
1940 * "ch_info()" function
1941 */
1942 static void
1943f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
1944{
1945 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1946
1947 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
1948 channel_info(channel, rettv->vval.v_dict);
1949}
1950
1951/*
1952 * "ch_log()" function
1953 */
1954 static void
1955f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
1956{
1957 char_u *msg = get_tv_string(&argvars[0]);
1958 channel_T *channel = NULL;
1959
1960 if (argvars[1].v_type != VAR_UNKNOWN)
1961 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
1962
1963 ch_log(channel, (char *)msg);
1964}
1965
1966/*
1967 * "ch_logfile()" function
1968 */
1969 static void
1970f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
1971{
1972 char_u *fname;
1973 char_u *opt = (char_u *)"";
1974 char_u buf[NUMBUFLEN];
1975
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02001976 /* Don't open a file in restricted mode. */
1977 if (check_restricted() || check_secure())
1978 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001979 fname = get_tv_string(&argvars[0]);
1980 if (argvars[1].v_type == VAR_STRING)
1981 opt = get_tv_string_buf(&argvars[1], buf);
1982 ch_logfile(fname, opt);
1983}
1984
1985/*
1986 * "ch_open()" function
1987 */
1988 static void
1989f_ch_open(typval_T *argvars, typval_T *rettv)
1990{
1991 rettv->v_type = VAR_CHANNEL;
1992 if (check_restricted() || check_secure())
1993 return;
1994 rettv->vval.v_channel = channel_open_func(argvars);
1995}
1996
1997/*
1998 * "ch_read()" function
1999 */
2000 static void
2001f_ch_read(typval_T *argvars, typval_T *rettv)
2002{
2003 common_channel_read(argvars, rettv, FALSE);
2004}
2005
2006/*
2007 * "ch_readraw()" function
2008 */
2009 static void
2010f_ch_readraw(typval_T *argvars, typval_T *rettv)
2011{
2012 common_channel_read(argvars, rettv, TRUE);
2013}
2014
2015/*
2016 * "ch_evalexpr()" function
2017 */
2018 static void
2019f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2020{
2021 ch_expr_common(argvars, rettv, TRUE);
2022}
2023
2024/*
2025 * "ch_sendexpr()" function
2026 */
2027 static void
2028f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2029{
2030 ch_expr_common(argvars, rettv, FALSE);
2031}
2032
2033/*
2034 * "ch_evalraw()" function
2035 */
2036 static void
2037f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2038{
2039 ch_raw_common(argvars, rettv, TRUE);
2040}
2041
2042/*
2043 * "ch_sendraw()" function
2044 */
2045 static void
2046f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2047{
2048 ch_raw_common(argvars, rettv, FALSE);
2049}
2050
2051/*
2052 * "ch_setoptions()" function
2053 */
2054 static void
2055f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2056{
2057 channel_T *channel;
2058 jobopt_T opt;
2059
2060 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2061 if (channel == NULL)
2062 return;
2063 clear_job_options(&opt);
2064 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002065 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002066 channel_set_options(channel, &opt);
2067 free_job_options(&opt);
2068}
2069
2070/*
2071 * "ch_status()" function
2072 */
2073 static void
2074f_ch_status(typval_T *argvars, typval_T *rettv)
2075{
2076 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002077 jobopt_T opt;
2078 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002079
2080 /* return an empty string by default */
2081 rettv->v_type = VAR_STRING;
2082 rettv->vval.v_string = NULL;
2083
2084 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002085
2086 if (argvars[1].v_type != VAR_UNKNOWN)
2087 {
2088 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002089 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002090 && (opt.jo_set & JO_PART))
2091 part = opt.jo_part;
2092 }
2093
2094 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002095}
2096#endif
2097
2098/*
2099 * "changenr()" function
2100 */
2101 static void
2102f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2103{
2104 rettv->vval.v_number = curbuf->b_u_seq_cur;
2105}
2106
2107/*
2108 * "char2nr(string)" function
2109 */
2110 static void
2111f_char2nr(typval_T *argvars, typval_T *rettv)
2112{
2113#ifdef FEAT_MBYTE
2114 if (has_mbyte)
2115 {
2116 int utf8 = 0;
2117
2118 if (argvars[1].v_type != VAR_UNKNOWN)
2119 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
2120
2121 if (utf8)
2122 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
2123 else
2124 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
2125 }
2126 else
2127#endif
2128 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
2129}
2130
2131/*
2132 * "cindent(lnum)" function
2133 */
2134 static void
2135f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2136{
2137#ifdef FEAT_CINDENT
2138 pos_T pos;
2139 linenr_T lnum;
2140
2141 pos = curwin->w_cursor;
2142 lnum = get_tv_lnum(argvars);
2143 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2144 {
2145 curwin->w_cursor.lnum = lnum;
2146 rettv->vval.v_number = get_c_indent();
2147 curwin->w_cursor = pos;
2148 }
2149 else
2150#endif
2151 rettv->vval.v_number = -1;
2152}
2153
2154/*
2155 * "clearmatches()" function
2156 */
2157 static void
2158f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2159{
2160#ifdef FEAT_SEARCH_EXTRA
2161 clear_matches(curwin);
2162#endif
2163}
2164
2165/*
2166 * "col(string)" function
2167 */
2168 static void
2169f_col(typval_T *argvars, typval_T *rettv)
2170{
2171 colnr_T col = 0;
2172 pos_T *fp;
2173 int fnum = curbuf->b_fnum;
2174
2175 fp = var2fpos(&argvars[0], FALSE, &fnum);
2176 if (fp != NULL && fnum == curbuf->b_fnum)
2177 {
2178 if (fp->col == MAXCOL)
2179 {
2180 /* '> can be MAXCOL, get the length of the line then */
2181 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2182 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2183 else
2184 col = MAXCOL;
2185 }
2186 else
2187 {
2188 col = fp->col + 1;
2189#ifdef FEAT_VIRTUALEDIT
2190 /* col(".") when the cursor is on the NUL at the end of the line
2191 * because of "coladd" can be seen as an extra column. */
2192 if (virtual_active() && fp == &curwin->w_cursor)
2193 {
2194 char_u *p = ml_get_cursor();
2195
2196 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2197 curwin->w_virtcol - curwin->w_cursor.coladd))
2198 {
2199# ifdef FEAT_MBYTE
2200 int l;
2201
2202 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2203 col += l;
2204# else
2205 if (*p != NUL && p[1] == NUL)
2206 ++col;
2207# endif
2208 }
2209 }
2210#endif
2211 }
2212 }
2213 rettv->vval.v_number = col;
2214}
2215
2216#if defined(FEAT_INS_EXPAND)
2217/*
2218 * "complete()" function
2219 */
2220 static void
2221f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2222{
2223 int startcol;
2224
2225 if ((State & INSERT) == 0)
2226 {
2227 EMSG(_("E785: complete() can only be used in Insert mode"));
2228 return;
2229 }
2230
2231 /* Check for undo allowed here, because if something was already inserted
2232 * the line was already saved for undo and this check isn't done. */
2233 if (!undo_allowed())
2234 return;
2235
2236 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2237 {
2238 EMSG(_(e_invarg));
2239 return;
2240 }
2241
2242 startcol = (int)get_tv_number_chk(&argvars[0], NULL);
2243 if (startcol <= 0)
2244 return;
2245
2246 set_completion(startcol - 1, argvars[1].vval.v_list);
2247}
2248
2249/*
2250 * "complete_add()" function
2251 */
2252 static void
2253f_complete_add(typval_T *argvars, typval_T *rettv)
2254{
2255 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2256}
2257
2258/*
2259 * "complete_check()" function
2260 */
2261 static void
2262f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2263{
2264 int saved = RedrawingDisabled;
2265
2266 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002267 ins_compl_check_keys(0, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002268 rettv->vval.v_number = compl_interrupted;
2269 RedrawingDisabled = saved;
2270}
2271#endif
2272
2273/*
2274 * "confirm(message, buttons[, default [, type]])" function
2275 */
2276 static void
2277f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2278{
2279#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2280 char_u *message;
2281 char_u *buttons = NULL;
2282 char_u buf[NUMBUFLEN];
2283 char_u buf2[NUMBUFLEN];
2284 int def = 1;
2285 int type = VIM_GENERIC;
2286 char_u *typestr;
2287 int error = FALSE;
2288
2289 message = get_tv_string_chk(&argvars[0]);
2290 if (message == NULL)
2291 error = TRUE;
2292 if (argvars[1].v_type != VAR_UNKNOWN)
2293 {
2294 buttons = get_tv_string_buf_chk(&argvars[1], buf);
2295 if (buttons == NULL)
2296 error = TRUE;
2297 if (argvars[2].v_type != VAR_UNKNOWN)
2298 {
2299 def = (int)get_tv_number_chk(&argvars[2], &error);
2300 if (argvars[3].v_type != VAR_UNKNOWN)
2301 {
2302 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
2303 if (typestr == NULL)
2304 error = TRUE;
2305 else
2306 {
2307 switch (TOUPPER_ASC(*typestr))
2308 {
2309 case 'E': type = VIM_ERROR; break;
2310 case 'Q': type = VIM_QUESTION; break;
2311 case 'I': type = VIM_INFO; break;
2312 case 'W': type = VIM_WARNING; break;
2313 case 'G': type = VIM_GENERIC; break;
2314 }
2315 }
2316 }
2317 }
2318 }
2319
2320 if (buttons == NULL || *buttons == NUL)
2321 buttons = (char_u *)_("&Ok");
2322
2323 if (!error)
2324 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2325 def, NULL, FALSE);
2326#endif
2327}
2328
2329/*
2330 * "copy()" function
2331 */
2332 static void
2333f_copy(typval_T *argvars, typval_T *rettv)
2334{
2335 item_copy(&argvars[0], rettv, FALSE, 0);
2336}
2337
2338#ifdef FEAT_FLOAT
2339/*
2340 * "cos()" function
2341 */
2342 static void
2343f_cos(typval_T *argvars, typval_T *rettv)
2344{
2345 float_T f = 0.0;
2346
2347 rettv->v_type = VAR_FLOAT;
2348 if (get_float_arg(argvars, &f) == OK)
2349 rettv->vval.v_float = cos(f);
2350 else
2351 rettv->vval.v_float = 0.0;
2352}
2353
2354/*
2355 * "cosh()" function
2356 */
2357 static void
2358f_cosh(typval_T *argvars, typval_T *rettv)
2359{
2360 float_T f = 0.0;
2361
2362 rettv->v_type = VAR_FLOAT;
2363 if (get_float_arg(argvars, &f) == OK)
2364 rettv->vval.v_float = cosh(f);
2365 else
2366 rettv->vval.v_float = 0.0;
2367}
2368#endif
2369
2370/*
2371 * "count()" function
2372 */
2373 static void
2374f_count(typval_T *argvars, typval_T *rettv)
2375{
2376 long n = 0;
2377 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002378 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002379
Bram Moolenaar9966b212017-07-28 16:46:57 +02002380 if (argvars[2].v_type != VAR_UNKNOWN)
2381 ic = (int)get_tv_number_chk(&argvars[2], &error);
2382
2383 if (argvars[0].v_type == VAR_STRING)
2384 {
2385 char_u *expr = get_tv_string_chk(&argvars[1]);
2386 char_u *p = argvars[0].vval.v_string;
2387 char_u *next;
2388
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002389 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002390 {
2391 if (ic)
2392 {
2393 size_t len = STRLEN(expr);
2394
2395 while (*p != NUL)
2396 {
2397 if (MB_STRNICMP(p, expr, len) == 0)
2398 {
2399 ++n;
2400 p += len;
2401 }
2402 else
2403 MB_PTR_ADV(p);
2404 }
2405 }
2406 else
2407 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2408 != NULL)
2409 {
2410 ++n;
2411 p = next + STRLEN(expr);
2412 }
2413 }
2414
2415 }
2416 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002417 {
2418 listitem_T *li;
2419 list_T *l;
2420 long idx;
2421
2422 if ((l = argvars[0].vval.v_list) != NULL)
2423 {
2424 li = l->lv_first;
2425 if (argvars[2].v_type != VAR_UNKNOWN)
2426 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002427 if (argvars[3].v_type != VAR_UNKNOWN)
2428 {
2429 idx = (long)get_tv_number_chk(&argvars[3], &error);
2430 if (!error)
2431 {
2432 li = list_find(l, idx);
2433 if (li == NULL)
2434 EMSGN(_(e_listidx), idx);
2435 }
2436 }
2437 if (error)
2438 li = NULL;
2439 }
2440
2441 for ( ; li != NULL; li = li->li_next)
2442 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2443 ++n;
2444 }
2445 }
2446 else if (argvars[0].v_type == VAR_DICT)
2447 {
2448 int todo;
2449 dict_T *d;
2450 hashitem_T *hi;
2451
2452 if ((d = argvars[0].vval.v_dict) != NULL)
2453 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002454 if (argvars[2].v_type != VAR_UNKNOWN)
2455 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002456 if (argvars[3].v_type != VAR_UNKNOWN)
2457 EMSG(_(e_invarg));
2458 }
2459
2460 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2461 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2462 {
2463 if (!HASHITEM_EMPTY(hi))
2464 {
2465 --todo;
2466 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2467 ++n;
2468 }
2469 }
2470 }
2471 }
2472 else
2473 EMSG2(_(e_listdictarg), "count()");
2474 rettv->vval.v_number = n;
2475}
2476
2477/*
2478 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2479 *
2480 * Checks the existence of a cscope connection.
2481 */
2482 static void
2483f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2484{
2485#ifdef FEAT_CSCOPE
2486 int num = 0;
2487 char_u *dbpath = NULL;
2488 char_u *prepend = NULL;
2489 char_u buf[NUMBUFLEN];
2490
2491 if (argvars[0].v_type != VAR_UNKNOWN
2492 && argvars[1].v_type != VAR_UNKNOWN)
2493 {
2494 num = (int)get_tv_number(&argvars[0]);
2495 dbpath = get_tv_string(&argvars[1]);
2496 if (argvars[2].v_type != VAR_UNKNOWN)
2497 prepend = get_tv_string_buf(&argvars[2], buf);
2498 }
2499
2500 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2501#endif
2502}
2503
2504/*
2505 * "cursor(lnum, col)" function, or
2506 * "cursor(list)"
2507 *
2508 * Moves the cursor to the specified line and column.
2509 * Returns 0 when the position could be set, -1 otherwise.
2510 */
2511 static void
2512f_cursor(typval_T *argvars, typval_T *rettv)
2513{
2514 long line, col;
2515#ifdef FEAT_VIRTUALEDIT
2516 long coladd = 0;
2517#endif
2518 int set_curswant = TRUE;
2519
2520 rettv->vval.v_number = -1;
2521 if (argvars[1].v_type == VAR_UNKNOWN)
2522 {
2523 pos_T pos;
2524 colnr_T curswant = -1;
2525
2526 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2527 {
2528 EMSG(_(e_invarg));
2529 return;
2530 }
2531 line = pos.lnum;
2532 col = pos.col;
2533#ifdef FEAT_VIRTUALEDIT
2534 coladd = pos.coladd;
2535#endif
2536 if (curswant >= 0)
2537 {
2538 curwin->w_curswant = curswant - 1;
2539 set_curswant = FALSE;
2540 }
2541 }
2542 else
2543 {
2544 line = get_tv_lnum(argvars);
2545 col = (long)get_tv_number_chk(&argvars[1], NULL);
2546#ifdef FEAT_VIRTUALEDIT
2547 if (argvars[2].v_type != VAR_UNKNOWN)
2548 coladd = (long)get_tv_number_chk(&argvars[2], NULL);
2549#endif
2550 }
2551 if (line < 0 || col < 0
2552#ifdef FEAT_VIRTUALEDIT
2553 || coladd < 0
2554#endif
2555 )
2556 return; /* type error; errmsg already given */
2557 if (line > 0)
2558 curwin->w_cursor.lnum = line;
2559 if (col > 0)
2560 curwin->w_cursor.col = col - 1;
2561#ifdef FEAT_VIRTUALEDIT
2562 curwin->w_cursor.coladd = coladd;
2563#endif
2564
2565 /* Make sure the cursor is in a valid position. */
2566 check_cursor();
2567#ifdef FEAT_MBYTE
2568 /* Correct cursor for multi-byte character. */
2569 if (has_mbyte)
2570 mb_adjust_cursor();
2571#endif
2572
2573 curwin->w_set_curswant = set_curswant;
2574 rettv->vval.v_number = 0;
2575}
2576
2577/*
2578 * "deepcopy()" function
2579 */
2580 static void
2581f_deepcopy(typval_T *argvars, typval_T *rettv)
2582{
2583 int noref = 0;
2584 int copyID;
2585
2586 if (argvars[1].v_type != VAR_UNKNOWN)
2587 noref = (int)get_tv_number_chk(&argvars[1], NULL);
2588 if (noref < 0 || noref > 1)
2589 EMSG(_(e_invarg));
2590 else
2591 {
2592 copyID = get_copyID();
2593 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2594 }
2595}
2596
2597/*
2598 * "delete()" function
2599 */
2600 static void
2601f_delete(typval_T *argvars, typval_T *rettv)
2602{
2603 char_u nbuf[NUMBUFLEN];
2604 char_u *name;
2605 char_u *flags;
2606
2607 rettv->vval.v_number = -1;
2608 if (check_restricted() || check_secure())
2609 return;
2610
2611 name = get_tv_string(&argvars[0]);
2612 if (name == NULL || *name == NUL)
2613 {
2614 EMSG(_(e_invarg));
2615 return;
2616 }
2617
2618 if (argvars[1].v_type != VAR_UNKNOWN)
2619 flags = get_tv_string_buf(&argvars[1], nbuf);
2620 else
2621 flags = (char_u *)"";
2622
2623 if (*flags == NUL)
2624 /* delete a file */
2625 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2626 else if (STRCMP(flags, "d") == 0)
2627 /* delete an empty directory */
2628 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2629 else if (STRCMP(flags, "rf") == 0)
2630 /* delete a directory recursively */
2631 rettv->vval.v_number = delete_recursive(name);
2632 else
2633 EMSG2(_(e_invexpr2), flags);
2634}
2635
2636/*
2637 * "did_filetype()" function
2638 */
2639 static void
2640f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2641{
2642#ifdef FEAT_AUTOCMD
2643 rettv->vval.v_number = did_filetype;
2644#endif
2645}
2646
2647/*
2648 * "diff_filler()" function
2649 */
2650 static void
2651f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2652{
2653#ifdef FEAT_DIFF
2654 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
2655#endif
2656}
2657
2658/*
2659 * "diff_hlID()" function
2660 */
2661 static void
2662f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2663{
2664#ifdef FEAT_DIFF
2665 linenr_T lnum = get_tv_lnum(argvars);
2666 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01002667 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002668 static int fnum = 0;
2669 static int change_start = 0;
2670 static int change_end = 0;
2671 static hlf_T hlID = (hlf_T)0;
2672 int filler_lines;
2673 int col;
2674
2675 if (lnum < 0) /* ignore type error in {lnum} arg */
2676 lnum = 0;
2677 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002678 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002679 || fnum != curbuf->b_fnum)
2680 {
2681 /* New line, buffer, change: need to get the values. */
2682 filler_lines = diff_check(curwin, lnum);
2683 if (filler_lines < 0)
2684 {
2685 if (filler_lines == -1)
2686 {
2687 change_start = MAXCOL;
2688 change_end = -1;
2689 if (diff_find_change(curwin, lnum, &change_start, &change_end))
2690 hlID = HLF_ADD; /* added line */
2691 else
2692 hlID = HLF_CHD; /* changed line */
2693 }
2694 else
2695 hlID = HLF_ADD; /* added line */
2696 }
2697 else
2698 hlID = (hlf_T)0;
2699 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002700 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002701 fnum = curbuf->b_fnum;
2702 }
2703
2704 if (hlID == HLF_CHD || hlID == HLF_TXD)
2705 {
2706 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
2707 if (col >= change_start && col <= change_end)
2708 hlID = HLF_TXD; /* changed text */
2709 else
2710 hlID = HLF_CHD; /* changed line */
2711 }
2712 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
2713#endif
2714}
2715
2716/*
2717 * "empty({expr})" function
2718 */
2719 static void
2720f_empty(typval_T *argvars, typval_T *rettv)
2721{
2722 int n = FALSE;
2723
2724 switch (argvars[0].v_type)
2725 {
2726 case VAR_STRING:
2727 case VAR_FUNC:
2728 n = argvars[0].vval.v_string == NULL
2729 || *argvars[0].vval.v_string == NUL;
2730 break;
2731 case VAR_PARTIAL:
2732 n = FALSE;
2733 break;
2734 case VAR_NUMBER:
2735 n = argvars[0].vval.v_number == 0;
2736 break;
2737 case VAR_FLOAT:
2738#ifdef FEAT_FLOAT
2739 n = argvars[0].vval.v_float == 0.0;
2740 break;
2741#endif
2742 case VAR_LIST:
2743 n = argvars[0].vval.v_list == NULL
2744 || argvars[0].vval.v_list->lv_first == NULL;
2745 break;
2746 case VAR_DICT:
2747 n = argvars[0].vval.v_dict == NULL
2748 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2749 break;
2750 case VAR_SPECIAL:
2751 n = argvars[0].vval.v_number != VVAL_TRUE;
2752 break;
2753
2754 case VAR_JOB:
2755#ifdef FEAT_JOB_CHANNEL
2756 n = argvars[0].vval.v_job == NULL
2757 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2758 break;
2759#endif
2760 case VAR_CHANNEL:
2761#ifdef FEAT_JOB_CHANNEL
2762 n = argvars[0].vval.v_channel == NULL
2763 || !channel_is_open(argvars[0].vval.v_channel);
2764 break;
2765#endif
2766 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01002767 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002768 n = TRUE;
2769 break;
2770 }
2771
2772 rettv->vval.v_number = n;
2773}
2774
2775/*
2776 * "escape({string}, {chars})" function
2777 */
2778 static void
2779f_escape(typval_T *argvars, typval_T *rettv)
2780{
2781 char_u buf[NUMBUFLEN];
2782
2783 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
2784 get_tv_string_buf(&argvars[1], buf));
2785 rettv->v_type = VAR_STRING;
2786}
2787
2788/*
2789 * "eval()" function
2790 */
2791 static void
2792f_eval(typval_T *argvars, typval_T *rettv)
2793{
2794 char_u *s, *p;
2795
2796 s = get_tv_string_chk(&argvars[0]);
2797 if (s != NULL)
2798 s = skipwhite(s);
2799
2800 p = s;
2801 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
2802 {
2803 if (p != NULL && !aborting())
2804 EMSG2(_(e_invexpr2), p);
2805 need_clr_eos = FALSE;
2806 rettv->v_type = VAR_NUMBER;
2807 rettv->vval.v_number = 0;
2808 }
2809 else if (*s != NUL)
2810 EMSG(_(e_trailing));
2811}
2812
2813/*
2814 * "eventhandler()" function
2815 */
2816 static void
2817f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
2818{
2819 rettv->vval.v_number = vgetc_busy;
2820}
2821
2822/*
2823 * "executable()" function
2824 */
2825 static void
2826f_executable(typval_T *argvars, typval_T *rettv)
2827{
2828 char_u *name = get_tv_string(&argvars[0]);
2829
2830 /* Check in $PATH and also check directly if there is a directory name. */
2831 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
2832 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
2833}
2834
2835static garray_T redir_execute_ga;
2836
2837/*
2838 * Append "value[value_len]" to the execute() output.
2839 */
2840 void
2841execute_redir_str(char_u *value, int value_len)
2842{
2843 int len;
2844
2845 if (value_len == -1)
2846 len = (int)STRLEN(value); /* Append the entire string */
2847 else
2848 len = value_len; /* Append only "value_len" characters */
2849 if (ga_grow(&redir_execute_ga, len) == OK)
2850 {
2851 mch_memmove((char *)redir_execute_ga.ga_data
2852 + redir_execute_ga.ga_len, value, len);
2853 redir_execute_ga.ga_len += len;
2854 }
2855}
2856
2857/*
2858 * Get next line from a list.
2859 * Called by do_cmdline() to get the next line.
2860 * Returns allocated string, or NULL for end of function.
2861 */
2862
2863 static char_u *
2864get_list_line(
2865 int c UNUSED,
2866 void *cookie,
2867 int indent UNUSED)
2868{
2869 listitem_T **p = (listitem_T **)cookie;
2870 listitem_T *item = *p;
2871 char_u buf[NUMBUFLEN];
2872 char_u *s;
2873
2874 if (item == NULL)
2875 return NULL;
2876 s = get_tv_string_buf_chk(&item->li_tv, buf);
2877 *p = item->li_next;
2878 return s == NULL ? NULL : vim_strsave(s);
2879}
2880
2881/*
2882 * "execute()" function
2883 */
2884 static void
2885f_execute(typval_T *argvars, typval_T *rettv)
2886{
2887 char_u *cmd = NULL;
2888 list_T *list = NULL;
2889 int save_msg_silent = msg_silent;
2890 int save_emsg_silent = emsg_silent;
2891 int save_emsg_noredir = emsg_noredir;
2892 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01002893 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002894 garray_T save_ga;
2895
2896 rettv->vval.v_string = NULL;
2897 rettv->v_type = VAR_STRING;
2898
2899 if (argvars[0].v_type == VAR_LIST)
2900 {
2901 list = argvars[0].vval.v_list;
2902 if (list == NULL || list->lv_first == NULL)
2903 /* empty list, no commands, empty output */
2904 return;
2905 ++list->lv_refcount;
2906 }
2907 else
2908 {
2909 cmd = get_tv_string_chk(&argvars[0]);
2910 if (cmd == NULL)
2911 return;
2912 }
2913
2914 if (argvars[1].v_type != VAR_UNKNOWN)
2915 {
2916 char_u buf[NUMBUFLEN];
2917 char_u *s = get_tv_string_buf_chk(&argvars[1], buf);
2918
2919 if (s == NULL)
2920 return;
2921 if (STRNCMP(s, "silent", 6) == 0)
2922 ++msg_silent;
2923 if (STRCMP(s, "silent!") == 0)
2924 {
2925 emsg_silent = TRUE;
2926 emsg_noredir = TRUE;
2927 }
2928 }
2929 else
2930 ++msg_silent;
2931
2932 if (redir_execute)
2933 save_ga = redir_execute_ga;
2934 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
2935 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01002936 redir_off = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002937
2938 if (cmd != NULL)
2939 do_cmdline_cmd(cmd);
2940 else
2941 {
2942 listitem_T *item = list->lv_first;
2943
2944 do_cmdline(NULL, get_list_line, (void *)&item,
2945 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
2946 --list->lv_refcount;
2947 }
2948
Bram Moolenaard297f352017-01-29 20:31:21 +01002949 /* Need to append a NUL to the result. */
2950 if (ga_grow(&redir_execute_ga, 1) == OK)
2951 {
2952 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
2953 rettv->vval.v_string = redir_execute_ga.ga_data;
2954 }
2955 else
2956 {
2957 ga_clear(&redir_execute_ga);
2958 rettv->vval.v_string = NULL;
2959 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002960 msg_silent = save_msg_silent;
2961 emsg_silent = save_emsg_silent;
2962 emsg_noredir = save_emsg_noredir;
2963
2964 redir_execute = save_redir_execute;
2965 if (redir_execute)
2966 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01002967 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002968
2969 /* "silent reg" or "silent echo x" leaves msg_col somewhere in the
2970 * line. Put it back in the first column. */
2971 msg_col = 0;
2972}
2973
2974/*
2975 * "exepath()" function
2976 */
2977 static void
2978f_exepath(typval_T *argvars, typval_T *rettv)
2979{
2980 char_u *p = NULL;
2981
2982 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
2983 rettv->v_type = VAR_STRING;
2984 rettv->vval.v_string = p;
2985}
2986
2987/*
2988 * "exists()" function
2989 */
2990 static void
2991f_exists(typval_T *argvars, typval_T *rettv)
2992{
2993 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002994 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002995
2996 p = get_tv_string(&argvars[0]);
2997 if (*p == '$') /* environment variable */
2998 {
2999 /* first try "normal" environment variables (fast) */
3000 if (mch_getenv(p + 1) != NULL)
3001 n = TRUE;
3002 else
3003 {
3004 /* try expanding things like $VIM and ${HOME} */
3005 p = expand_env_save(p);
3006 if (p != NULL && *p != '$')
3007 n = TRUE;
3008 vim_free(p);
3009 }
3010 }
3011 else if (*p == '&' || *p == '+') /* option */
3012 {
3013 n = (get_option_tv(&p, NULL, TRUE) == OK);
3014 if (*skipwhite(p) != NUL)
3015 n = FALSE; /* trailing garbage */
3016 }
3017 else if (*p == '*') /* internal or user defined function */
3018 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003019 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003020 }
3021 else if (*p == ':')
3022 {
3023 n = cmd_exists(p + 1);
3024 }
3025 else if (*p == '#')
3026 {
3027#ifdef FEAT_AUTOCMD
3028 if (p[1] == '#')
3029 n = autocmd_supported(p + 2);
3030 else
3031 n = au_exists(p + 1);
3032#endif
3033 }
3034 else /* internal variable */
3035 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003036 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003037 }
3038
3039 rettv->vval.v_number = n;
3040}
3041
3042#ifdef FEAT_FLOAT
3043/*
3044 * "exp()" function
3045 */
3046 static void
3047f_exp(typval_T *argvars, typval_T *rettv)
3048{
3049 float_T f = 0.0;
3050
3051 rettv->v_type = VAR_FLOAT;
3052 if (get_float_arg(argvars, &f) == OK)
3053 rettv->vval.v_float = exp(f);
3054 else
3055 rettv->vval.v_float = 0.0;
3056}
3057#endif
3058
3059/*
3060 * "expand()" function
3061 */
3062 static void
3063f_expand(typval_T *argvars, typval_T *rettv)
3064{
3065 char_u *s;
3066 int len;
3067 char_u *errormsg;
3068 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3069 expand_T xpc;
3070 int error = FALSE;
3071 char_u *result;
3072
3073 rettv->v_type = VAR_STRING;
3074 if (argvars[1].v_type != VAR_UNKNOWN
3075 && argvars[2].v_type != VAR_UNKNOWN
3076 && get_tv_number_chk(&argvars[2], &error)
3077 && !error)
3078 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003079 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003080 }
3081
3082 s = get_tv_string(&argvars[0]);
3083 if (*s == '%' || *s == '#' || *s == '<')
3084 {
3085 ++emsg_off;
3086 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3087 --emsg_off;
3088 if (rettv->v_type == VAR_LIST)
3089 {
3090 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3091 list_append_string(rettv->vval.v_list, result, -1);
3092 else
3093 vim_free(result);
3094 }
3095 else
3096 rettv->vval.v_string = result;
3097 }
3098 else
3099 {
3100 /* When the optional second argument is non-zero, don't remove matches
3101 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3102 if (argvars[1].v_type != VAR_UNKNOWN
3103 && get_tv_number_chk(&argvars[1], &error))
3104 options |= WILD_KEEP_ALL;
3105 if (!error)
3106 {
3107 ExpandInit(&xpc);
3108 xpc.xp_context = EXPAND_FILES;
3109 if (p_wic)
3110 options += WILD_ICASE;
3111 if (rettv->v_type == VAR_STRING)
3112 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3113 options, WILD_ALL);
3114 else if (rettv_list_alloc(rettv) != FAIL)
3115 {
3116 int i;
3117
3118 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3119 for (i = 0; i < xpc.xp_numfiles; i++)
3120 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3121 ExpandCleanup(&xpc);
3122 }
3123 }
3124 else
3125 rettv->vval.v_string = NULL;
3126 }
3127}
3128
3129/*
3130 * "extend(list, list [, idx])" function
3131 * "extend(dict, dict [, action])" function
3132 */
3133 static void
3134f_extend(typval_T *argvars, typval_T *rettv)
3135{
3136 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3137
3138 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3139 {
3140 list_T *l1, *l2;
3141 listitem_T *item;
3142 long before;
3143 int error = FALSE;
3144
3145 l1 = argvars[0].vval.v_list;
3146 l2 = argvars[1].vval.v_list;
3147 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
3148 && l2 != NULL)
3149 {
3150 if (argvars[2].v_type != VAR_UNKNOWN)
3151 {
3152 before = (long)get_tv_number_chk(&argvars[2], &error);
3153 if (error)
3154 return; /* type error; errmsg already given */
3155
3156 if (before == l1->lv_len)
3157 item = NULL;
3158 else
3159 {
3160 item = list_find(l1, before);
3161 if (item == NULL)
3162 {
3163 EMSGN(_(e_listidx), before);
3164 return;
3165 }
3166 }
3167 }
3168 else
3169 item = NULL;
3170 list_extend(l1, l2, item);
3171
3172 copy_tv(&argvars[0], rettv);
3173 }
3174 }
3175 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3176 {
3177 dict_T *d1, *d2;
3178 char_u *action;
3179 int i;
3180
3181 d1 = argvars[0].vval.v_dict;
3182 d2 = argvars[1].vval.v_dict;
3183 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
3184 && d2 != NULL)
3185 {
3186 /* Check the third argument. */
3187 if (argvars[2].v_type != VAR_UNKNOWN)
3188 {
3189 static char *(av[]) = {"keep", "force", "error"};
3190
3191 action = get_tv_string_chk(&argvars[2]);
3192 if (action == NULL)
3193 return; /* type error; errmsg already given */
3194 for (i = 0; i < 3; ++i)
3195 if (STRCMP(action, av[i]) == 0)
3196 break;
3197 if (i == 3)
3198 {
3199 EMSG2(_(e_invarg2), action);
3200 return;
3201 }
3202 }
3203 else
3204 action = (char_u *)"force";
3205
3206 dict_extend(d1, d2, action);
3207
3208 copy_tv(&argvars[0], rettv);
3209 }
3210 }
3211 else
3212 EMSG2(_(e_listdictarg), "extend()");
3213}
3214
3215/*
3216 * "feedkeys()" function
3217 */
3218 static void
3219f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3220{
3221 int remap = TRUE;
3222 int insert = FALSE;
3223 char_u *keys, *flags;
3224 char_u nbuf[NUMBUFLEN];
3225 int typed = FALSE;
3226 int execute = FALSE;
3227 int dangerous = FALSE;
3228 char_u *keys_esc;
3229
3230 /* This is not allowed in the sandbox. If the commands would still be
3231 * executed in the sandbox it would be OK, but it probably happens later,
3232 * when "sandbox" is no longer set. */
3233 if (check_secure())
3234 return;
3235
3236 keys = get_tv_string(&argvars[0]);
3237
3238 if (argvars[1].v_type != VAR_UNKNOWN)
3239 {
3240 flags = get_tv_string_buf(&argvars[1], nbuf);
3241 for ( ; *flags != NUL; ++flags)
3242 {
3243 switch (*flags)
3244 {
3245 case 'n': remap = FALSE; break;
3246 case 'm': remap = TRUE; break;
3247 case 't': typed = TRUE; break;
3248 case 'i': insert = TRUE; break;
3249 case 'x': execute = TRUE; break;
3250 case '!': dangerous = TRUE; break;
3251 }
3252 }
3253 }
3254
3255 if (*keys != NUL || execute)
3256 {
3257 /* Need to escape K_SPECIAL and CSI before putting the string in the
3258 * typeahead buffer. */
3259 keys_esc = vim_strsave_escape_csi(keys);
3260 if (keys_esc != NULL)
3261 {
3262 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
3263 insert ? 0 : typebuf.tb_len, !typed, FALSE);
3264 vim_free(keys_esc);
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003265 if (vgetc_busy
3266#ifdef FEAT_TIMERS
3267 || timer_busy
3268#endif
3269 )
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003270 typebuf_was_filled = TRUE;
3271 if (execute)
3272 {
3273 int save_msg_scroll = msg_scroll;
3274
3275 /* Avoid a 1 second delay when the keys start Insert mode. */
3276 msg_scroll = FALSE;
3277
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02003278#ifdef FEAT_TERMINAL
3279 if (term_use_loop())
3280 terminal_loop(FALSE);
3281 else
3282#endif
3283 {
3284 if (!dangerous)
3285 ++ex_normal_busy;
3286 exec_normal(TRUE);
3287 if (!dangerous)
3288 --ex_normal_busy;
3289 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003290 msg_scroll |= save_msg_scroll;
3291 }
3292 }
3293 }
3294}
3295
3296/*
3297 * "filereadable()" function
3298 */
3299 static void
3300f_filereadable(typval_T *argvars, typval_T *rettv)
3301{
3302 int fd;
3303 char_u *p;
3304 int n;
3305
3306#ifndef O_NONBLOCK
3307# define O_NONBLOCK 0
3308#endif
3309 p = get_tv_string(&argvars[0]);
3310 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3311 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3312 {
3313 n = TRUE;
3314 close(fd);
3315 }
3316 else
3317 n = FALSE;
3318
3319 rettv->vval.v_number = n;
3320}
3321
3322/*
3323 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3324 * rights to write into.
3325 */
3326 static void
3327f_filewritable(typval_T *argvars, typval_T *rettv)
3328{
3329 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
3330}
3331
3332 static void
3333findfilendir(
3334 typval_T *argvars UNUSED,
3335 typval_T *rettv,
3336 int find_what UNUSED)
3337{
3338#ifdef FEAT_SEARCHPATH
3339 char_u *fname;
3340 char_u *fresult = NULL;
3341 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3342 char_u *p;
3343 char_u pathbuf[NUMBUFLEN];
3344 int count = 1;
3345 int first = TRUE;
3346 int error = FALSE;
3347#endif
3348
3349 rettv->vval.v_string = NULL;
3350 rettv->v_type = VAR_STRING;
3351
3352#ifdef FEAT_SEARCHPATH
3353 fname = get_tv_string(&argvars[0]);
3354
3355 if (argvars[1].v_type != VAR_UNKNOWN)
3356 {
3357 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
3358 if (p == NULL)
3359 error = TRUE;
3360 else
3361 {
3362 if (*p != NUL)
3363 path = p;
3364
3365 if (argvars[2].v_type != VAR_UNKNOWN)
3366 count = (int)get_tv_number_chk(&argvars[2], &error);
3367 }
3368 }
3369
3370 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3371 error = TRUE;
3372
3373 if (*fname != NUL && !error)
3374 {
3375 do
3376 {
3377 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3378 vim_free(fresult);
3379 fresult = find_file_in_path_option(first ? fname : NULL,
3380 first ? (int)STRLEN(fname) : 0,
3381 0, first, path,
3382 find_what,
3383 curbuf->b_ffname,
3384 find_what == FINDFILE_DIR
3385 ? (char_u *)"" : curbuf->b_p_sua);
3386 first = FALSE;
3387
3388 if (fresult != NULL && rettv->v_type == VAR_LIST)
3389 list_append_string(rettv->vval.v_list, fresult, -1);
3390
3391 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3392 }
3393
3394 if (rettv->v_type == VAR_STRING)
3395 rettv->vval.v_string = fresult;
3396#endif
3397}
3398
3399/*
3400 * "filter()" function
3401 */
3402 static void
3403f_filter(typval_T *argvars, typval_T *rettv)
3404{
3405 filter_map(argvars, rettv, FALSE);
3406}
3407
3408/*
3409 * "finddir({fname}[, {path}[, {count}]])" function
3410 */
3411 static void
3412f_finddir(typval_T *argvars, typval_T *rettv)
3413{
3414 findfilendir(argvars, rettv, FINDFILE_DIR);
3415}
3416
3417/*
3418 * "findfile({fname}[, {path}[, {count}]])" function
3419 */
3420 static void
3421f_findfile(typval_T *argvars, typval_T *rettv)
3422{
3423 findfilendir(argvars, rettv, FINDFILE_FILE);
3424}
3425
3426#ifdef FEAT_FLOAT
3427/*
3428 * "float2nr({float})" function
3429 */
3430 static void
3431f_float2nr(typval_T *argvars, typval_T *rettv)
3432{
3433 float_T f = 0.0;
3434
3435 if (get_float_arg(argvars, &f) == OK)
3436 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003437 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003438 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003439 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003440 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003441 else
3442 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003443 }
3444}
3445
3446/*
3447 * "floor({float})" function
3448 */
3449 static void
3450f_floor(typval_T *argvars, typval_T *rettv)
3451{
3452 float_T f = 0.0;
3453
3454 rettv->v_type = VAR_FLOAT;
3455 if (get_float_arg(argvars, &f) == OK)
3456 rettv->vval.v_float = floor(f);
3457 else
3458 rettv->vval.v_float = 0.0;
3459}
3460
3461/*
3462 * "fmod()" function
3463 */
3464 static void
3465f_fmod(typval_T *argvars, typval_T *rettv)
3466{
3467 float_T fx = 0.0, fy = 0.0;
3468
3469 rettv->v_type = VAR_FLOAT;
3470 if (get_float_arg(argvars, &fx) == OK
3471 && get_float_arg(&argvars[1], &fy) == OK)
3472 rettv->vval.v_float = fmod(fx, fy);
3473 else
3474 rettv->vval.v_float = 0.0;
3475}
3476#endif
3477
3478/*
3479 * "fnameescape({string})" function
3480 */
3481 static void
3482f_fnameescape(typval_T *argvars, typval_T *rettv)
3483{
3484 rettv->vval.v_string = vim_strsave_fnameescape(
3485 get_tv_string(&argvars[0]), FALSE);
3486 rettv->v_type = VAR_STRING;
3487}
3488
3489/*
3490 * "fnamemodify({fname}, {mods})" function
3491 */
3492 static void
3493f_fnamemodify(typval_T *argvars, typval_T *rettv)
3494{
3495 char_u *fname;
3496 char_u *mods;
3497 int usedlen = 0;
3498 int len;
3499 char_u *fbuf = NULL;
3500 char_u buf[NUMBUFLEN];
3501
3502 fname = get_tv_string_chk(&argvars[0]);
3503 mods = get_tv_string_buf_chk(&argvars[1], buf);
3504 if (fname == NULL || mods == NULL)
3505 fname = NULL;
3506 else
3507 {
3508 len = (int)STRLEN(fname);
3509 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
3510 }
3511
3512 rettv->v_type = VAR_STRING;
3513 if (fname == NULL)
3514 rettv->vval.v_string = NULL;
3515 else
3516 rettv->vval.v_string = vim_strnsave(fname, len);
3517 vim_free(fbuf);
3518}
3519
3520static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
3521
3522/*
3523 * "foldclosed()" function
3524 */
3525 static void
3526foldclosed_both(
3527 typval_T *argvars UNUSED,
3528 typval_T *rettv,
3529 int end UNUSED)
3530{
3531#ifdef FEAT_FOLDING
3532 linenr_T lnum;
3533 linenr_T first, last;
3534
3535 lnum = get_tv_lnum(argvars);
3536 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3537 {
3538 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3539 {
3540 if (end)
3541 rettv->vval.v_number = (varnumber_T)last;
3542 else
3543 rettv->vval.v_number = (varnumber_T)first;
3544 return;
3545 }
3546 }
3547#endif
3548 rettv->vval.v_number = -1;
3549}
3550
3551/*
3552 * "foldclosed()" function
3553 */
3554 static void
3555f_foldclosed(typval_T *argvars, typval_T *rettv)
3556{
3557 foldclosed_both(argvars, rettv, FALSE);
3558}
3559
3560/*
3561 * "foldclosedend()" function
3562 */
3563 static void
3564f_foldclosedend(typval_T *argvars, typval_T *rettv)
3565{
3566 foldclosed_both(argvars, rettv, TRUE);
3567}
3568
3569/*
3570 * "foldlevel()" function
3571 */
3572 static void
3573f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3574{
3575#ifdef FEAT_FOLDING
3576 linenr_T lnum;
3577
3578 lnum = get_tv_lnum(argvars);
3579 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3580 rettv->vval.v_number = foldLevel(lnum);
3581#endif
3582}
3583
3584/*
3585 * "foldtext()" function
3586 */
3587 static void
3588f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
3589{
3590#ifdef FEAT_FOLDING
3591 linenr_T foldstart;
3592 linenr_T foldend;
3593 char_u *dashes;
3594 linenr_T lnum;
3595 char_u *s;
3596 char_u *r;
3597 int len;
3598 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003599 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003600#endif
3601
3602 rettv->v_type = VAR_STRING;
3603 rettv->vval.v_string = NULL;
3604#ifdef FEAT_FOLDING
3605 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
3606 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
3607 dashes = get_vim_var_str(VV_FOLDDASHES);
3608 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
3609 && dashes != NULL)
3610 {
3611 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02003612 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003613 if (!linewhite(lnum))
3614 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003615
3616 /* Find interesting text in this line. */
3617 s = skipwhite(ml_get(lnum));
3618 /* skip C comment-start */
3619 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
3620 {
3621 s = skipwhite(s + 2);
3622 if (*skipwhite(s) == NUL
3623 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
3624 {
3625 s = skipwhite(ml_get(lnum + 1));
3626 if (*s == '*')
3627 s = skipwhite(s + 1);
3628 }
3629 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02003630 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003631 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003632 r = alloc((unsigned)(STRLEN(txt)
3633 + STRLEN(dashes) /* for %s */
3634 + 20 /* for %3ld */
3635 + STRLEN(s))); /* concatenated */
3636 if (r != NULL)
3637 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02003638 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003639 len = (int)STRLEN(r);
3640 STRCAT(r, s);
3641 /* remove 'foldmarker' and 'commentstring' */
3642 foldtext_cleanup(r + len);
3643 rettv->vval.v_string = r;
3644 }
3645 }
3646#endif
3647}
3648
3649/*
3650 * "foldtextresult(lnum)" function
3651 */
3652 static void
3653f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
3654{
3655#ifdef FEAT_FOLDING
3656 linenr_T lnum;
3657 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003658 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003659 foldinfo_T foldinfo;
3660 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003661 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003662#endif
3663
3664 rettv->v_type = VAR_STRING;
3665 rettv->vval.v_string = NULL;
3666#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003667 if (entered)
3668 return; /* reject recursive use */
3669 entered = TRUE;
3670
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003671 lnum = get_tv_lnum(argvars);
3672 /* treat illegal types and illegal string values for {lnum} the same */
3673 if (lnum < 0)
3674 lnum = 0;
3675 fold_count = foldedCount(curwin, lnum, &foldinfo);
3676 if (fold_count > 0)
3677 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01003678 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
3679 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003680 if (text == buf)
3681 text = vim_strsave(text);
3682 rettv->vval.v_string = text;
3683 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003684
3685 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003686#endif
3687}
3688
3689/*
3690 * "foreground()" function
3691 */
3692 static void
3693f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3694{
3695#ifdef FEAT_GUI
3696 if (gui.in_use)
3697 gui_mch_set_foreground();
3698#else
3699# ifdef WIN32
3700 win32_set_foreground();
3701# endif
3702#endif
3703}
3704
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003705 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003706common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003707{
3708 char_u *s;
3709 char_u *name;
3710 int use_string = FALSE;
3711 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003712 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003713
3714 if (argvars[0].v_type == VAR_FUNC)
3715 {
3716 /* function(MyFunc, [arg], dict) */
3717 s = argvars[0].vval.v_string;
3718 }
3719 else if (argvars[0].v_type == VAR_PARTIAL
3720 && argvars[0].vval.v_partial != NULL)
3721 {
3722 /* function(dict.MyFunc, [arg]) */
3723 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003724 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003725 }
3726 else
3727 {
3728 /* function('MyFunc', [arg], dict) */
3729 s = get_tv_string(&argvars[0]);
3730 use_string = TRUE;
3731 }
3732
Bram Moolenaar843b8842016-08-21 14:36:15 +02003733 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003734 {
3735 name = s;
3736 trans_name = trans_function_name(&name, FALSE,
3737 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
3738 if (*name != NUL)
3739 s = NULL;
3740 }
3741
Bram Moolenaar843b8842016-08-21 14:36:15 +02003742 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
3743 || (is_funcref && trans_name == NULL))
Bram Moolenaar5582ef12016-09-14 22:16:13 +02003744 EMSG2(_(e_invarg2), use_string ? get_tv_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003745 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003746 else if (trans_name != NULL && (is_funcref
3747 ? find_func(trans_name) == NULL
3748 : !translated_function_exists(trans_name)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003749 EMSG2(_("E700: Unknown function: %s"), s);
3750 else
3751 {
3752 int dict_idx = 0;
3753 int arg_idx = 0;
3754 list_T *list = NULL;
3755
3756 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
3757 {
3758 char sid_buf[25];
3759 int off = *s == 's' ? 2 : 5;
3760
3761 /* Expand s: and <SID> into <SNR>nr_, so that the function can
3762 * also be called from another script. Using trans_function_name()
3763 * would also work, but some plugins depend on the name being
3764 * printable text. */
3765 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
3766 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
3767 if (name != NULL)
3768 {
3769 STRCPY(name, sid_buf);
3770 STRCAT(name, s + off);
3771 }
3772 }
3773 else
3774 name = vim_strsave(s);
3775
3776 if (argvars[1].v_type != VAR_UNKNOWN)
3777 {
3778 if (argvars[2].v_type != VAR_UNKNOWN)
3779 {
3780 /* function(name, [args], dict) */
3781 arg_idx = 1;
3782 dict_idx = 2;
3783 }
3784 else if (argvars[1].v_type == VAR_DICT)
3785 /* function(name, dict) */
3786 dict_idx = 1;
3787 else
3788 /* function(name, [args]) */
3789 arg_idx = 1;
3790 if (dict_idx > 0)
3791 {
3792 if (argvars[dict_idx].v_type != VAR_DICT)
3793 {
3794 EMSG(_("E922: expected a dict"));
3795 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003796 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003797 }
3798 if (argvars[dict_idx].vval.v_dict == NULL)
3799 dict_idx = 0;
3800 }
3801 if (arg_idx > 0)
3802 {
3803 if (argvars[arg_idx].v_type != VAR_LIST)
3804 {
3805 EMSG(_("E923: Second argument of function() must be a list or a dict"));
3806 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003807 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003808 }
3809 list = argvars[arg_idx].vval.v_list;
3810 if (list == NULL || list->lv_len == 0)
3811 arg_idx = 0;
3812 }
3813 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003814 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003815 {
3816 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
3817
3818 /* result is a VAR_PARTIAL */
3819 if (pt == NULL)
3820 vim_free(name);
3821 else
3822 {
3823 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
3824 {
3825 listitem_T *li;
3826 int i = 0;
3827 int arg_len = 0;
3828 int lv_len = 0;
3829
3830 if (arg_pt != NULL)
3831 arg_len = arg_pt->pt_argc;
3832 if (list != NULL)
3833 lv_len = list->lv_len;
3834 pt->pt_argc = arg_len + lv_len;
3835 pt->pt_argv = (typval_T *)alloc(
3836 sizeof(typval_T) * pt->pt_argc);
3837 if (pt->pt_argv == NULL)
3838 {
3839 vim_free(pt);
3840 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003841 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003842 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003843 for (i = 0; i < arg_len; i++)
3844 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
3845 if (lv_len > 0)
3846 for (li = list->lv_first; li != NULL;
3847 li = li->li_next)
3848 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003849 }
3850
3851 /* For "function(dict.func, [], dict)" and "func" is a partial
3852 * use "dict". That is backwards compatible. */
3853 if (dict_idx > 0)
3854 {
3855 /* The dict is bound explicitly, pt_auto is FALSE. */
3856 pt->pt_dict = argvars[dict_idx].vval.v_dict;
3857 ++pt->pt_dict->dv_refcount;
3858 }
3859 else if (arg_pt != NULL)
3860 {
3861 /* If the dict was bound automatically the result is also
3862 * bound automatically. */
3863 pt->pt_dict = arg_pt->pt_dict;
3864 pt->pt_auto = arg_pt->pt_auto;
3865 if (pt->pt_dict != NULL)
3866 ++pt->pt_dict->dv_refcount;
3867 }
3868
3869 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003870 if (arg_pt != NULL && arg_pt->pt_func != NULL)
3871 {
3872 pt->pt_func = arg_pt->pt_func;
3873 func_ptr_ref(pt->pt_func);
3874 vim_free(name);
3875 }
3876 else if (is_funcref)
3877 {
3878 pt->pt_func = find_func(trans_name);
3879 func_ptr_ref(pt->pt_func);
3880 vim_free(name);
3881 }
3882 else
3883 {
3884 pt->pt_name = name;
3885 func_ref(name);
3886 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003887 }
3888 rettv->v_type = VAR_PARTIAL;
3889 rettv->vval.v_partial = pt;
3890 }
3891 else
3892 {
3893 /* result is a VAR_FUNC */
3894 rettv->v_type = VAR_FUNC;
3895 rettv->vval.v_string = name;
3896 func_ref(name);
3897 }
3898 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003899theend:
3900 vim_free(trans_name);
3901}
3902
3903/*
3904 * "funcref()" function
3905 */
3906 static void
3907f_funcref(typval_T *argvars, typval_T *rettv)
3908{
3909 common_function(argvars, rettv, TRUE);
3910}
3911
3912/*
3913 * "function()" function
3914 */
3915 static void
3916f_function(typval_T *argvars, typval_T *rettv)
3917{
3918 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003919}
3920
3921/*
3922 * "garbagecollect()" function
3923 */
3924 static void
3925f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
3926{
3927 /* This is postponed until we are back at the toplevel, because we may be
3928 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
3929 want_garbage_collect = TRUE;
3930
3931 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
3932 garbage_collect_at_exit = TRUE;
3933}
3934
3935/*
3936 * "get()" function
3937 */
3938 static void
3939f_get(typval_T *argvars, typval_T *rettv)
3940{
3941 listitem_T *li;
3942 list_T *l;
3943 dictitem_T *di;
3944 dict_T *d;
3945 typval_T *tv = NULL;
3946
3947 if (argvars[0].v_type == VAR_LIST)
3948 {
3949 if ((l = argvars[0].vval.v_list) != NULL)
3950 {
3951 int error = FALSE;
3952
3953 li = list_find(l, (long)get_tv_number_chk(&argvars[1], &error));
3954 if (!error && li != NULL)
3955 tv = &li->li_tv;
3956 }
3957 }
3958 else if (argvars[0].v_type == VAR_DICT)
3959 {
3960 if ((d = argvars[0].vval.v_dict) != NULL)
3961 {
3962 di = dict_find(d, get_tv_string(&argvars[1]), -1);
3963 if (di != NULL)
3964 tv = &di->di_tv;
3965 }
3966 }
3967 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
3968 {
3969 partial_T *pt;
3970 partial_T fref_pt;
3971
3972 if (argvars[0].v_type == VAR_PARTIAL)
3973 pt = argvars[0].vval.v_partial;
3974 else
3975 {
3976 vim_memset(&fref_pt, 0, sizeof(fref_pt));
3977 fref_pt.pt_name = argvars[0].vval.v_string;
3978 pt = &fref_pt;
3979 }
3980
3981 if (pt != NULL)
3982 {
3983 char_u *what = get_tv_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003984 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003985
3986 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
3987 {
3988 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003989 n = partial_name(pt);
3990 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003991 rettv->vval.v_string = NULL;
3992 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003993 {
3994 rettv->vval.v_string = vim_strsave(n);
3995 if (rettv->v_type == VAR_FUNC)
3996 func_ref(rettv->vval.v_string);
3997 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003998 }
3999 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004000 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004001 else if (STRCMP(what, "args") == 0)
4002 {
4003 rettv->v_type = VAR_LIST;
4004 if (rettv_list_alloc(rettv) == OK)
4005 {
4006 int i;
4007
4008 for (i = 0; i < pt->pt_argc; ++i)
4009 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4010 }
4011 }
4012 else
4013 EMSG2(_(e_invarg2), what);
4014 return;
4015 }
4016 }
4017 else
4018 EMSG2(_(e_listdictarg), "get()");
4019
4020 if (tv == NULL)
4021 {
4022 if (argvars[2].v_type != VAR_UNKNOWN)
4023 copy_tv(&argvars[2], rettv);
4024 }
4025 else
4026 copy_tv(tv, rettv);
4027}
4028
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004029#ifdef FEAT_SIGNS
4030/*
4031 * Returns information about signs placed in a buffer as list of dicts.
4032 */
4033 static void
4034get_buffer_signs(buf_T *buf, list_T *l)
4035{
4036 signlist_T *sign;
4037
4038 for (sign = buf->b_signlist; sign; sign = sign->next)
4039 {
4040 dict_T *d = dict_alloc();
4041
4042 if (d != NULL)
4043 {
4044 dict_add_nr_str(d, "id", sign->id, NULL);
4045 dict_add_nr_str(d, "lnum", sign->lnum, NULL);
Bram Moolenaar6a402ed2016-08-28 14:11:24 +02004046 dict_add_nr_str(d, "name", 0L, sign_typenr2name(sign->typenr));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004047
4048 list_append_dict(l, d);
4049 }
4050 }
4051}
4052#endif
4053
4054/*
4055 * Returns buffer options, variables and other attributes in a dictionary.
4056 */
4057 static dict_T *
4058get_buffer_info(buf_T *buf)
4059{
4060 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004061 tabpage_T *tp;
4062 win_T *wp;
4063 list_T *windows;
4064
4065 dict = dict_alloc();
4066 if (dict == NULL)
4067 return NULL;
4068
Bram Moolenaar33928832016-08-18 21:22:04 +02004069 dict_add_nr_str(dict, "bufnr", buf->b_fnum, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004070 dict_add_nr_str(dict, "name", 0L,
4071 buf->b_ffname != NULL ? buf->b_ffname : (char_u *)"");
Bram Moolenaarf845b872017-01-06 14:04:54 +01004072 dict_add_nr_str(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4073 : buflist_findlnum(buf), NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004074 dict_add_nr_str(dict, "loaded", buf->b_ml.ml_mfp != NULL, NULL);
4075 dict_add_nr_str(dict, "listed", buf->b_p_bl, NULL);
4076 dict_add_nr_str(dict, "changed", bufIsChanged(buf), NULL);
Bram Moolenaar95c526e2017-02-25 14:59:34 +01004077 dict_add_nr_str(dict, "changedtick", CHANGEDTICK(buf), NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004078 dict_add_nr_str(dict, "hidden",
4079 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0,
4080 NULL);
4081
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004082 /* Get a reference to buffer variables */
4083 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004084
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004085 /* List of windows displaying this buffer */
4086 windows = list_alloc();
4087 if (windows != NULL)
4088 {
4089 FOR_ALL_TAB_WINDOWS(tp, wp)
4090 if (wp->w_buffer == buf)
4091 list_append_number(windows, (varnumber_T)wp->w_id);
4092 dict_add_list(dict, "windows", windows);
4093 }
4094
4095#ifdef FEAT_SIGNS
4096 if (buf->b_signlist != NULL)
4097 {
4098 /* List of signs placed in this buffer */
4099 list_T *signs = list_alloc();
4100 if (signs != NULL)
4101 {
4102 get_buffer_signs(buf, signs);
4103 dict_add_list(dict, "signs", signs);
4104 }
4105 }
4106#endif
4107
4108 return dict;
4109}
4110
4111/*
4112 * "getbufinfo()" function
4113 */
4114 static void
4115f_getbufinfo(typval_T *argvars, typval_T *rettv)
4116{
4117 buf_T *buf = NULL;
4118 buf_T *argbuf = NULL;
4119 dict_T *d;
4120 int filtered = FALSE;
4121 int sel_buflisted = FALSE;
4122 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004123 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004124
4125 if (rettv_list_alloc(rettv) != OK)
4126 return;
4127
4128 /* List of all the buffers or selected buffers */
4129 if (argvars[0].v_type == VAR_DICT)
4130 {
4131 dict_T *sel_d = argvars[0].vval.v_dict;
4132
4133 if (sel_d != NULL)
4134 {
4135 dictitem_T *di;
4136
4137 filtered = TRUE;
4138
4139 di = dict_find(sel_d, (char_u *)"buflisted", -1);
4140 if (di != NULL && get_tv_number(&di->di_tv))
4141 sel_buflisted = TRUE;
4142
4143 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
4144 if (di != NULL && get_tv_number(&di->di_tv))
4145 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004146
4147 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
4148 if (di != NULL && get_tv_number(&di->di_tv))
4149 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004150 }
4151 }
4152 else if (argvars[0].v_type != VAR_UNKNOWN)
4153 {
4154 /* Information about one buffer. Argument specifies the buffer */
4155 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4156 ++emsg_off;
4157 argbuf = get_buf_tv(&argvars[0], FALSE);
4158 --emsg_off;
4159 if (argbuf == NULL)
4160 return;
4161 }
4162
4163 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004164 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004165 {
4166 if (argbuf != NULL && argbuf != buf)
4167 continue;
4168 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004169 || (sel_buflisted && !buf->b_p_bl)
4170 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004171 continue;
4172
4173 d = get_buffer_info(buf);
4174 if (d != NULL)
4175 list_append_dict(rettv->vval.v_list, d);
4176 if (argbuf != NULL)
4177 return;
4178 }
4179}
4180
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004181static void get_buffer_lines(buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv);
4182
4183/*
4184 * Get line or list of lines from buffer "buf" into "rettv".
4185 * Return a range (from start to end) of lines in rettv from the specified
4186 * buffer.
4187 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4188 */
4189 static void
4190get_buffer_lines(
4191 buf_T *buf,
4192 linenr_T start,
4193 linenr_T end,
4194 int retlist,
4195 typval_T *rettv)
4196{
4197 char_u *p;
4198
4199 rettv->v_type = VAR_STRING;
4200 rettv->vval.v_string = NULL;
4201 if (retlist && rettv_list_alloc(rettv) == FAIL)
4202 return;
4203
4204 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4205 return;
4206
4207 if (!retlist)
4208 {
4209 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4210 p = ml_get_buf(buf, start, FALSE);
4211 else
4212 p = (char_u *)"";
4213 rettv->vval.v_string = vim_strsave(p);
4214 }
4215 else
4216 {
4217 if (end < start)
4218 return;
4219
4220 if (start < 1)
4221 start = 1;
4222 if (end > buf->b_ml.ml_line_count)
4223 end = buf->b_ml.ml_line_count;
4224 while (start <= end)
4225 if (list_append_string(rettv->vval.v_list,
4226 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4227 break;
4228 }
4229}
4230
4231/*
4232 * Get the lnum from the first argument.
4233 * Also accepts "$", then "buf" is used.
4234 * Returns 0 on error.
4235 */
4236 static linenr_T
4237get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
4238{
4239 if (argvars[0].v_type == VAR_STRING
4240 && argvars[0].vval.v_string != NULL
4241 && argvars[0].vval.v_string[0] == '$'
4242 && buf != NULL)
4243 return buf->b_ml.ml_line_count;
4244 return (linenr_T)get_tv_number_chk(&argvars[0], NULL);
4245}
4246
4247/*
4248 * "getbufline()" function
4249 */
4250 static void
4251f_getbufline(typval_T *argvars, typval_T *rettv)
4252{
4253 linenr_T lnum;
4254 linenr_T end;
4255 buf_T *buf;
4256
4257 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4258 ++emsg_off;
4259 buf = get_buf_tv(&argvars[0], FALSE);
4260 --emsg_off;
4261
4262 lnum = get_tv_lnum_buf(&argvars[1], buf);
4263 if (argvars[2].v_type == VAR_UNKNOWN)
4264 end = lnum;
4265 else
4266 end = get_tv_lnum_buf(&argvars[2], buf);
4267
4268 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4269}
4270
4271/*
4272 * "getbufvar()" function
4273 */
4274 static void
4275f_getbufvar(typval_T *argvars, typval_T *rettv)
4276{
4277 buf_T *buf;
4278 buf_T *save_curbuf;
4279 char_u *varname;
4280 dictitem_T *v;
4281 int done = FALSE;
4282
4283 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4284 varname = get_tv_string_chk(&argvars[1]);
4285 ++emsg_off;
4286 buf = get_buf_tv(&argvars[0], FALSE);
4287
4288 rettv->v_type = VAR_STRING;
4289 rettv->vval.v_string = NULL;
4290
4291 if (buf != NULL && varname != NULL)
4292 {
4293 /* set curbuf to be our buf, temporarily */
4294 save_curbuf = curbuf;
4295 curbuf = buf;
4296
Bram Moolenaar30567352016-08-27 21:25:44 +02004297 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004298 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004299 if (varname[1] == NUL)
4300 {
4301 /* get all buffer-local options in a dict */
4302 dict_T *opts = get_winbuf_options(TRUE);
4303
4304 if (opts != NULL)
4305 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004306 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004307 done = TRUE;
4308 }
4309 }
4310 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4311 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004312 done = TRUE;
4313 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004314 else
4315 {
4316 /* Look up the variable. */
4317 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4318 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4319 'b', varname, FALSE);
4320 if (v != NULL)
4321 {
4322 copy_tv(&v->di_tv, rettv);
4323 done = TRUE;
4324 }
4325 }
4326
4327 /* restore previous notion of curbuf */
4328 curbuf = save_curbuf;
4329 }
4330
4331 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4332 /* use the default value */
4333 copy_tv(&argvars[2], rettv);
4334
4335 --emsg_off;
4336}
4337
4338/*
4339 * "getchar()" function
4340 */
4341 static void
4342f_getchar(typval_T *argvars, typval_T *rettv)
4343{
4344 varnumber_T n;
4345 int error = FALSE;
4346
4347 /* Position the cursor. Needed after a message that ends in a space. */
4348 windgoto(msg_row, msg_col);
4349
4350 ++no_mapping;
4351 ++allow_keys;
4352 for (;;)
4353 {
4354 if (argvars[0].v_type == VAR_UNKNOWN)
4355 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004356 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004357 else if (get_tv_number_chk(&argvars[0], &error) == 1)
4358 /* getchar(1): only check if char avail */
4359 n = vpeekc_any();
4360 else if (error || vpeekc_any() == NUL)
4361 /* illegal argument or getchar(0) and no char avail: return zero */
4362 n = 0;
4363 else
4364 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004365 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004366
4367 if (n == K_IGNORE)
4368 continue;
4369 break;
4370 }
4371 --no_mapping;
4372 --allow_keys;
4373
4374 set_vim_var_nr(VV_MOUSE_WIN, 0);
4375 set_vim_var_nr(VV_MOUSE_WINID, 0);
4376 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4377 set_vim_var_nr(VV_MOUSE_COL, 0);
4378
4379 rettv->vval.v_number = n;
4380 if (IS_SPECIAL(n) || mod_mask != 0)
4381 {
4382 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4383 int i = 0;
4384
4385 /* Turn a special key into three bytes, plus modifier. */
4386 if (mod_mask != 0)
4387 {
4388 temp[i++] = K_SPECIAL;
4389 temp[i++] = KS_MODIFIER;
4390 temp[i++] = mod_mask;
4391 }
4392 if (IS_SPECIAL(n))
4393 {
4394 temp[i++] = K_SPECIAL;
4395 temp[i++] = K_SECOND(n);
4396 temp[i++] = K_THIRD(n);
4397 }
4398#ifdef FEAT_MBYTE
4399 else if (has_mbyte)
4400 i += (*mb_char2bytes)(n, temp + i);
4401#endif
4402 else
4403 temp[i++] = n;
4404 temp[i++] = NUL;
4405 rettv->v_type = VAR_STRING;
4406 rettv->vval.v_string = vim_strsave(temp);
4407
4408#ifdef FEAT_MOUSE
4409 if (is_mouse_key(n))
4410 {
4411 int row = mouse_row;
4412 int col = mouse_col;
4413 win_T *win;
4414 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004415 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004416 int winnr = 1;
4417
4418 if (row >= 0 && col >= 0)
4419 {
4420 /* Find the window at the mouse coordinates and compute the
4421 * text position. */
4422 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004423 if (win == NULL)
4424 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004425 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004426 for (wp = firstwin; wp != win; wp = wp->w_next)
4427 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004428 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4429 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4430 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4431 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4432 }
4433 }
4434#endif
4435 }
4436}
4437
4438/*
4439 * "getcharmod()" function
4440 */
4441 static void
4442f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4443{
4444 rettv->vval.v_number = mod_mask;
4445}
4446
4447/*
4448 * "getcharsearch()" function
4449 */
4450 static void
4451f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4452{
4453 if (rettv_dict_alloc(rettv) != FAIL)
4454 {
4455 dict_T *dict = rettv->vval.v_dict;
4456
4457 dict_add_nr_str(dict, "char", 0L, last_csearch());
4458 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
4459 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
4460 }
4461}
4462
4463/*
4464 * "getcmdline()" function
4465 */
4466 static void
4467f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4468{
4469 rettv->v_type = VAR_STRING;
4470 rettv->vval.v_string = get_cmdline_str();
4471}
4472
4473/*
4474 * "getcmdpos()" function
4475 */
4476 static void
4477f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4478{
4479 rettv->vval.v_number = get_cmdline_pos() + 1;
4480}
4481
4482/*
4483 * "getcmdtype()" function
4484 */
4485 static void
4486f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4487{
4488 rettv->v_type = VAR_STRING;
4489 rettv->vval.v_string = alloc(2);
4490 if (rettv->vval.v_string != NULL)
4491 {
4492 rettv->vval.v_string[0] = get_cmdline_type();
4493 rettv->vval.v_string[1] = NUL;
4494 }
4495}
4496
4497/*
4498 * "getcmdwintype()" function
4499 */
4500 static void
4501f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4502{
4503 rettv->v_type = VAR_STRING;
4504 rettv->vval.v_string = NULL;
4505#ifdef FEAT_CMDWIN
4506 rettv->vval.v_string = alloc(2);
4507 if (rettv->vval.v_string != NULL)
4508 {
4509 rettv->vval.v_string[0] = cmdwin_type;
4510 rettv->vval.v_string[1] = NUL;
4511 }
4512#endif
4513}
4514
4515#if defined(FEAT_CMDL_COMPL)
4516/*
4517 * "getcompletion()" function
4518 */
4519 static void
4520f_getcompletion(typval_T *argvars, typval_T *rettv)
4521{
4522 char_u *pat;
4523 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004524 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004525 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
4526 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004527
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004528 if (argvars[2].v_type != VAR_UNKNOWN)
4529 filtered = get_tv_number_chk(&argvars[2], NULL);
4530
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004531 if (p_wic)
4532 options |= WILD_ICASE;
4533
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004534 /* For filtered results, 'wildignore' is used */
4535 if (!filtered)
4536 options |= WILD_KEEP_ALL;
4537
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004538 ExpandInit(&xpc);
4539 xpc.xp_pattern = get_tv_string(&argvars[0]);
4540 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4541 xpc.xp_context = cmdcomplete_str_to_type(get_tv_string(&argvars[1]));
4542 if (xpc.xp_context == EXPAND_NOTHING)
4543 {
4544 if (argvars[1].v_type == VAR_STRING)
4545 EMSG2(_(e_invarg2), argvars[1].vval.v_string);
4546 else
4547 EMSG(_(e_invarg));
4548 return;
4549 }
4550
4551# if defined(FEAT_MENU)
4552 if (xpc.xp_context == EXPAND_MENUS)
4553 {
4554 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4555 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4556 }
4557# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02004558#ifdef FEAT_CSCOPE
4559 if (xpc.xp_context == EXPAND_CSCOPE)
4560 {
4561 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4562 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4563 }
4564#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02004565#ifdef FEAT_SIGNS
4566 if (xpc.xp_context == EXPAND_SIGN)
4567 {
4568 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4569 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4570 }
4571#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004572
4573 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4574 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
4575 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004576 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004577
4578 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4579
4580 for (i = 0; i < xpc.xp_numfiles; i++)
4581 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4582 }
4583 vim_free(pat);
4584 ExpandCleanup(&xpc);
4585}
4586#endif
4587
4588/*
4589 * "getcwd()" function
4590 */
4591 static void
4592f_getcwd(typval_T *argvars, typval_T *rettv)
4593{
4594 win_T *wp = NULL;
4595 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01004596 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004597
4598 rettv->v_type = VAR_STRING;
4599 rettv->vval.v_string = NULL;
4600
Bram Moolenaar54591292018-02-09 20:53:59 +01004601 if (argvars[0].v_type == VAR_NUMBER && argvars[0].vval.v_number == -1)
4602 global = TRUE;
4603 else
4604 wp = find_tabwin(&argvars[0], &argvars[1]);
4605
4606 if (wp != NULL && wp->w_localdir != NULL)
4607 rettv->vval.v_string = vim_strsave(wp->w_localdir);
4608 else if (wp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004609 {
Bram Moolenaar54591292018-02-09 20:53:59 +01004610 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004611 rettv->vval.v_string = vim_strsave(globaldir);
4612 else
4613 {
4614 cwd = alloc(MAXPATHL);
4615 if (cwd != NULL)
4616 {
4617 if (mch_dirname(cwd, MAXPATHL) != FAIL)
4618 rettv->vval.v_string = vim_strsave(cwd);
4619 vim_free(cwd);
4620 }
4621 }
4622#ifdef BACKSLASH_IN_FILENAME
4623 if (rettv->vval.v_string != NULL)
4624 slash_adjust(rettv->vval.v_string);
4625#endif
4626 }
4627}
4628
4629/*
4630 * "getfontname()" function
4631 */
4632 static void
4633f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4634{
4635 rettv->v_type = VAR_STRING;
4636 rettv->vval.v_string = NULL;
4637#ifdef FEAT_GUI
4638 if (gui.in_use)
4639 {
4640 GuiFont font;
4641 char_u *name = NULL;
4642
4643 if (argvars[0].v_type == VAR_UNKNOWN)
4644 {
4645 /* Get the "Normal" font. Either the name saved by
4646 * hl_set_font_name() or from the font ID. */
4647 font = gui.norm_font;
4648 name = hl_get_font_name();
4649 }
4650 else
4651 {
4652 name = get_tv_string(&argvars[0]);
4653 if (STRCMP(name, "*") == 0) /* don't use font dialog */
4654 return;
4655 font = gui_mch_get_font(name, FALSE);
4656 if (font == NOFONT)
4657 return; /* Invalid font name, return empty string. */
4658 }
4659 rettv->vval.v_string = gui_mch_get_fontname(font, name);
4660 if (argvars[0].v_type != VAR_UNKNOWN)
4661 gui_mch_free_font(font);
4662 }
4663#endif
4664}
4665
4666/*
4667 * "getfperm({fname})" function
4668 */
4669 static void
4670f_getfperm(typval_T *argvars, typval_T *rettv)
4671{
4672 char_u *fname;
4673 stat_T st;
4674 char_u *perm = NULL;
4675 char_u flags[] = "rwx";
4676 int i;
4677
4678 fname = get_tv_string(&argvars[0]);
4679
4680 rettv->v_type = VAR_STRING;
4681 if (mch_stat((char *)fname, &st) >= 0)
4682 {
4683 perm = vim_strsave((char_u *)"---------");
4684 if (perm != NULL)
4685 {
4686 for (i = 0; i < 9; i++)
4687 {
4688 if (st.st_mode & (1 << (8 - i)))
4689 perm[i] = flags[i % 3];
4690 }
4691 }
4692 }
4693 rettv->vval.v_string = perm;
4694}
4695
4696/*
4697 * "getfsize({fname})" function
4698 */
4699 static void
4700f_getfsize(typval_T *argvars, typval_T *rettv)
4701{
4702 char_u *fname;
4703 stat_T st;
4704
4705 fname = get_tv_string(&argvars[0]);
4706
4707 rettv->v_type = VAR_NUMBER;
4708
4709 if (mch_stat((char *)fname, &st) >= 0)
4710 {
4711 if (mch_isdir(fname))
4712 rettv->vval.v_number = 0;
4713 else
4714 {
4715 rettv->vval.v_number = (varnumber_T)st.st_size;
4716
4717 /* non-perfect check for overflow */
4718 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
4719 rettv->vval.v_number = -2;
4720 }
4721 }
4722 else
4723 rettv->vval.v_number = -1;
4724}
4725
4726/*
4727 * "getftime({fname})" function
4728 */
4729 static void
4730f_getftime(typval_T *argvars, typval_T *rettv)
4731{
4732 char_u *fname;
4733 stat_T st;
4734
4735 fname = get_tv_string(&argvars[0]);
4736
4737 if (mch_stat((char *)fname, &st) >= 0)
4738 rettv->vval.v_number = (varnumber_T)st.st_mtime;
4739 else
4740 rettv->vval.v_number = -1;
4741}
4742
4743/*
4744 * "getftype({fname})" function
4745 */
4746 static void
4747f_getftype(typval_T *argvars, typval_T *rettv)
4748{
4749 char_u *fname;
4750 stat_T st;
4751 char_u *type = NULL;
4752 char *t;
4753
4754 fname = get_tv_string(&argvars[0]);
4755
4756 rettv->v_type = VAR_STRING;
4757 if (mch_lstat((char *)fname, &st) >= 0)
4758 {
4759#ifdef S_ISREG
4760 if (S_ISREG(st.st_mode))
4761 t = "file";
4762 else if (S_ISDIR(st.st_mode))
4763 t = "dir";
4764# ifdef S_ISLNK
4765 else if (S_ISLNK(st.st_mode))
4766 t = "link";
4767# endif
4768# ifdef S_ISBLK
4769 else if (S_ISBLK(st.st_mode))
4770 t = "bdev";
4771# endif
4772# ifdef S_ISCHR
4773 else if (S_ISCHR(st.st_mode))
4774 t = "cdev";
4775# endif
4776# ifdef S_ISFIFO
4777 else if (S_ISFIFO(st.st_mode))
4778 t = "fifo";
4779# endif
4780# ifdef S_ISSOCK
4781 else if (S_ISSOCK(st.st_mode))
4782 t = "fifo";
4783# endif
4784 else
4785 t = "other";
4786#else
4787# ifdef S_IFMT
4788 switch (st.st_mode & S_IFMT)
4789 {
4790 case S_IFREG: t = "file"; break;
4791 case S_IFDIR: t = "dir"; break;
4792# ifdef S_IFLNK
4793 case S_IFLNK: t = "link"; break;
4794# endif
4795# ifdef S_IFBLK
4796 case S_IFBLK: t = "bdev"; break;
4797# endif
4798# ifdef S_IFCHR
4799 case S_IFCHR: t = "cdev"; break;
4800# endif
4801# ifdef S_IFIFO
4802 case S_IFIFO: t = "fifo"; break;
4803# endif
4804# ifdef S_IFSOCK
4805 case S_IFSOCK: t = "socket"; break;
4806# endif
4807 default: t = "other";
4808 }
4809# else
4810 if (mch_isdir(fname))
4811 t = "dir";
4812 else
4813 t = "file";
4814# endif
4815#endif
4816 type = vim_strsave((char_u *)t);
4817 }
4818 rettv->vval.v_string = type;
4819}
4820
4821/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01004822 * "getjumplist()" function
4823 */
4824 static void
4825f_getjumplist(typval_T *argvars, typval_T *rettv)
4826{
4827#ifdef FEAT_JUMPLIST
4828 win_T *wp;
4829 int i;
4830 list_T *l;
4831 dict_T *d;
4832#endif
4833
4834 if (rettv_list_alloc(rettv) != OK)
4835 return;
4836
4837#ifdef FEAT_JUMPLIST
4838 wp = find_tabwin(&argvars[0], &argvars[1]);
4839 if (wp == NULL)
4840 return;
4841
4842 l = list_alloc();
4843 if (l == NULL)
4844 return;
4845
4846 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4847 return;
4848 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
4849
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004850 cleanup_jumplist(wp);
Bram Moolenaar4f505882018-02-10 21:06:32 +01004851 for (i = 0; i < wp->w_jumplistlen; ++i)
4852 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004853 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
4854 continue;
4855 if (wp->w_jumplist[i].fmark.fnum == 0)
4856 fname2fnum(&wp->w_jumplist[i]);
Bram Moolenaar4f505882018-02-10 21:06:32 +01004857 if ((d = dict_alloc()) == NULL)
4858 return;
4859 if (list_append_dict(l, d) == FAIL)
4860 return;
4861 dict_add_nr_str(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum,
4862 NULL);
4863 dict_add_nr_str(d, "col", (long)wp->w_jumplist[i].fmark.mark.col,
4864 NULL);
4865# ifdef FEAT_VIRTUALEDIT
4866 dict_add_nr_str(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd,
4867 NULL);
4868# endif
4869 dict_add_nr_str(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum, NULL);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01004870 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaar4f505882018-02-10 21:06:32 +01004871 dict_add_nr_str(d, "filename", 0L, wp->w_jumplist[i].fname);
4872 }
4873#endif
4874}
4875
4876/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004877 * "getline(lnum, [end])" function
4878 */
4879 static void
4880f_getline(typval_T *argvars, typval_T *rettv)
4881{
4882 linenr_T lnum;
4883 linenr_T end;
4884 int retlist;
4885
4886 lnum = get_tv_lnum(argvars);
4887 if (argvars[1].v_type == VAR_UNKNOWN)
4888 {
4889 end = 0;
4890 retlist = FALSE;
4891 }
4892 else
4893 {
4894 end = get_tv_lnum(&argvars[1]);
4895 retlist = TRUE;
4896 }
4897
4898 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
4899}
4900
Bram Moolenaar4ae20952016-08-13 15:29:14 +02004901#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02004902 static void
4903get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
4904{
Bram Moolenaard823fa92016-08-12 16:29:27 +02004905 if (what_arg->v_type == VAR_UNKNOWN)
4906 {
4907 if (rettv_list_alloc(rettv) == OK)
4908 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02004909 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004910 }
4911 else
4912 {
4913 if (rettv_dict_alloc(rettv) == OK)
4914 if (is_qf || (wp != NULL))
4915 {
4916 if (what_arg->v_type == VAR_DICT)
4917 {
4918 dict_T *d = what_arg->vval.v_dict;
4919
4920 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02004921 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02004922 }
4923 else
4924 EMSG(_(e_dictreq));
4925 }
4926 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004927}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02004928#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02004929
4930/*
4931 * "getloclist()" function
4932 */
4933 static void
4934f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4935{
4936#ifdef FEAT_QUICKFIX
4937 win_T *wp;
4938
4939 wp = find_win_by_nr(&argvars[0], NULL);
4940 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
4941#endif
4942}
4943
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004944/*
4945 * "getmatches()" function
4946 */
4947 static void
4948f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4949{
4950#ifdef FEAT_SEARCH_EXTRA
4951 dict_T *dict;
4952 matchitem_T *cur = curwin->w_match_head;
4953 int i;
4954
4955 if (rettv_list_alloc(rettv) == OK)
4956 {
4957 while (cur != NULL)
4958 {
4959 dict = dict_alloc();
4960 if (dict == NULL)
4961 return;
4962 if (cur->match.regprog == NULL)
4963 {
4964 /* match added with matchaddpos() */
4965 for (i = 0; i < MAXPOSMATCH; ++i)
4966 {
4967 llpos_T *llpos;
4968 char buf[6];
4969 list_T *l;
4970
4971 llpos = &cur->pos.pos[i];
4972 if (llpos->lnum == 0)
4973 break;
4974 l = list_alloc();
4975 if (l == NULL)
4976 break;
4977 list_append_number(l, (varnumber_T)llpos->lnum);
4978 if (llpos->col > 0)
4979 {
4980 list_append_number(l, (varnumber_T)llpos->col);
4981 list_append_number(l, (varnumber_T)llpos->len);
4982 }
4983 sprintf(buf, "pos%d", i + 1);
4984 dict_add_list(dict, buf, l);
4985 }
4986 }
4987 else
4988 {
4989 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
4990 }
4991 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
4992 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
4993 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
4994# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
4995 if (cur->conceal_char)
4996 {
4997 char_u buf[MB_MAXBYTES + 1];
4998
4999 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
5000 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
5001 }
5002# endif
5003 list_append_dict(rettv->vval.v_list, dict);
5004 cur = cur->next;
5005 }
5006 }
5007#endif
5008}
5009
5010/*
5011 * "getpid()" function
5012 */
5013 static void
5014f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5015{
5016 rettv->vval.v_number = mch_get_pid();
5017}
5018
5019 static void
5020getpos_both(
5021 typval_T *argvars,
5022 typval_T *rettv,
5023 int getcurpos)
5024{
5025 pos_T *fp;
5026 list_T *l;
5027 int fnum = -1;
5028
5029 if (rettv_list_alloc(rettv) == OK)
5030 {
5031 l = rettv->vval.v_list;
5032 if (getcurpos)
5033 fp = &curwin->w_cursor;
5034 else
5035 fp = var2fpos(&argvars[0], TRUE, &fnum);
5036 if (fnum != -1)
5037 list_append_number(l, (varnumber_T)fnum);
5038 else
5039 list_append_number(l, (varnumber_T)0);
5040 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5041 : (varnumber_T)0);
5042 list_append_number(l, (fp != NULL)
5043 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5044 : (varnumber_T)0);
5045 list_append_number(l,
5046#ifdef FEAT_VIRTUALEDIT
5047 (fp != NULL) ? (varnumber_T)fp->coladd :
5048#endif
5049 (varnumber_T)0);
5050 if (getcurpos)
5051 {
5052 update_curswant();
5053 list_append_number(l, curwin->w_curswant == MAXCOL ?
5054 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
5055 }
5056 }
5057 else
5058 rettv->vval.v_number = FALSE;
5059}
5060
5061
5062/*
5063 * "getcurpos()" function
5064 */
5065 static void
5066f_getcurpos(typval_T *argvars, typval_T *rettv)
5067{
5068 getpos_both(argvars, rettv, TRUE);
5069}
5070
5071/*
5072 * "getpos(string)" function
5073 */
5074 static void
5075f_getpos(typval_T *argvars, typval_T *rettv)
5076{
5077 getpos_both(argvars, rettv, FALSE);
5078}
5079
5080/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005081 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005082 */
5083 static void
5084f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5085{
5086#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005087 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005088#endif
5089}
5090
5091/*
5092 * "getreg()" function
5093 */
5094 static void
5095f_getreg(typval_T *argvars, typval_T *rettv)
5096{
5097 char_u *strregname;
5098 int regname;
5099 int arg2 = FALSE;
5100 int return_list = FALSE;
5101 int error = FALSE;
5102
5103 if (argvars[0].v_type != VAR_UNKNOWN)
5104 {
5105 strregname = get_tv_string_chk(&argvars[0]);
5106 error = strregname == NULL;
5107 if (argvars[1].v_type != VAR_UNKNOWN)
5108 {
5109 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
5110 if (!error && argvars[2].v_type != VAR_UNKNOWN)
5111 return_list = (int)get_tv_number_chk(&argvars[2], &error);
5112 }
5113 }
5114 else
5115 strregname = get_vim_var_str(VV_REG);
5116
5117 if (error)
5118 return;
5119
5120 regname = (strregname == NULL ? '"' : *strregname);
5121 if (regname == 0)
5122 regname = '"';
5123
5124 if (return_list)
5125 {
5126 rettv->v_type = VAR_LIST;
5127 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5128 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5129 if (rettv->vval.v_list == NULL)
5130 (void)rettv_list_alloc(rettv);
5131 else
5132 ++rettv->vval.v_list->lv_refcount;
5133 }
5134 else
5135 {
5136 rettv->v_type = VAR_STRING;
5137 rettv->vval.v_string = get_reg_contents(regname,
5138 arg2 ? GREG_EXPR_SRC : 0);
5139 }
5140}
5141
5142/*
5143 * "getregtype()" function
5144 */
5145 static void
5146f_getregtype(typval_T *argvars, typval_T *rettv)
5147{
5148 char_u *strregname;
5149 int regname;
5150 char_u buf[NUMBUFLEN + 2];
5151 long reglen = 0;
5152
5153 if (argvars[0].v_type != VAR_UNKNOWN)
5154 {
5155 strregname = get_tv_string_chk(&argvars[0]);
5156 if (strregname == NULL) /* type error; errmsg already given */
5157 {
5158 rettv->v_type = VAR_STRING;
5159 rettv->vval.v_string = NULL;
5160 return;
5161 }
5162 }
5163 else
5164 /* Default to v:register */
5165 strregname = get_vim_var_str(VV_REG);
5166
5167 regname = (strregname == NULL ? '"' : *strregname);
5168 if (regname == 0)
5169 regname = '"';
5170
5171 buf[0] = NUL;
5172 buf[1] = NUL;
5173 switch (get_reg_type(regname, &reglen))
5174 {
5175 case MLINE: buf[0] = 'V'; break;
5176 case MCHAR: buf[0] = 'v'; break;
5177 case MBLOCK:
5178 buf[0] = Ctrl_V;
5179 sprintf((char *)buf + 1, "%ld", reglen + 1);
5180 break;
5181 }
5182 rettv->v_type = VAR_STRING;
5183 rettv->vval.v_string = vim_strsave(buf);
5184}
5185
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005186/*
5187 * Returns information (variables, options, etc.) about a tab page
5188 * as a dictionary.
5189 */
5190 static dict_T *
5191get_tabpage_info(tabpage_T *tp, int tp_idx)
5192{
5193 win_T *wp;
5194 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005195 list_T *l;
5196
5197 dict = dict_alloc();
5198 if (dict == NULL)
5199 return NULL;
5200
Bram Moolenaar33928832016-08-18 21:22:04 +02005201 dict_add_nr_str(dict, "tabnr", tp_idx, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005202
5203 l = list_alloc();
5204 if (l != NULL)
5205 {
5206 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5207 wp; wp = wp->w_next)
5208 list_append_number(l, (varnumber_T)wp->w_id);
5209 dict_add_list(dict, "windows", l);
5210 }
5211
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005212 /* Make a reference to tabpage variables */
5213 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005214
5215 return dict;
5216}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005217
5218/*
5219 * "gettabinfo()" function
5220 */
5221 static void
5222f_gettabinfo(typval_T *argvars, typval_T *rettv)
5223{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005224 tabpage_T *tp, *tparg = NULL;
5225 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005226 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005227
5228 if (rettv_list_alloc(rettv) != OK)
5229 return;
5230
5231 if (argvars[0].v_type != VAR_UNKNOWN)
5232 {
5233 /* Information about one tab page */
5234 tparg = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5235 if (tparg == NULL)
5236 return;
5237 }
5238
5239 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005240 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005241 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005242 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005243 if (tparg != NULL && tp != tparg)
5244 continue;
5245 d = get_tabpage_info(tp, tpnr);
5246 if (d != NULL)
5247 list_append_dict(rettv->vval.v_list, d);
5248 if (tparg != NULL)
5249 return;
5250 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005251}
5252
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005253/*
5254 * "gettabvar()" function
5255 */
5256 static void
5257f_gettabvar(typval_T *argvars, typval_T *rettv)
5258{
5259 win_T *oldcurwin;
5260 tabpage_T *tp, *oldtabpage;
5261 dictitem_T *v;
5262 char_u *varname;
5263 int done = FALSE;
5264
5265 rettv->v_type = VAR_STRING;
5266 rettv->vval.v_string = NULL;
5267
5268 varname = get_tv_string_chk(&argvars[1]);
5269 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5270 if (tp != NULL && varname != NULL)
5271 {
5272 /* Set tp to be our tabpage, temporarily. Also set the window to the
5273 * first window in the tabpage, otherwise the window is not valid. */
5274 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005275 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5276 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005277 {
5278 /* look up the variable */
5279 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5280 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5281 if (v != NULL)
5282 {
5283 copy_tv(&v->di_tv, rettv);
5284 done = TRUE;
5285 }
5286 }
5287
5288 /* restore previous notion of curwin */
5289 restore_win(oldcurwin, oldtabpage, TRUE);
5290 }
5291
5292 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5293 copy_tv(&argvars[2], rettv);
5294}
5295
5296/*
5297 * "gettabwinvar()" function
5298 */
5299 static void
5300f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5301{
5302 getwinvar(argvars, rettv, 1);
5303}
5304
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005305/*
5306 * Returns information about a window as a dictionary.
5307 */
5308 static dict_T *
5309get_win_info(win_T *wp, short tpnr, short winnr)
5310{
5311 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005312
5313 dict = dict_alloc();
5314 if (dict == NULL)
5315 return NULL;
5316
Bram Moolenaar33928832016-08-18 21:22:04 +02005317 dict_add_nr_str(dict, "tabnr", tpnr, NULL);
5318 dict_add_nr_str(dict, "winnr", winnr, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005319 dict_add_nr_str(dict, "winid", wp->w_id, NULL);
5320 dict_add_nr_str(dict, "height", wp->w_height, NULL);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005321#ifdef FEAT_MENU
5322 dict_add_nr_str(dict, "winbar", wp->w_winbar_height, NULL);
5323#endif
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005324 dict_add_nr_str(dict, "width", wp->w_width, NULL);
Bram Moolenaar33928832016-08-18 21:22:04 +02005325 dict_add_nr_str(dict, "bufnr", wp->w_buffer->b_fnum, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005326
Bram Moolenaar69905d12017-08-13 18:14:47 +02005327#ifdef FEAT_TERMINAL
5328 dict_add_nr_str(dict, "terminal", bt_terminal(wp->w_buffer), NULL);
5329#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005330#ifdef FEAT_QUICKFIX
5331 dict_add_nr_str(dict, "quickfix", bt_quickfix(wp->w_buffer), NULL);
5332 dict_add_nr_str(dict, "loclist",
5333 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL), NULL);
5334#endif
5335
Bram Moolenaar30567352016-08-27 21:25:44 +02005336 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005337 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005338
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005339 return dict;
5340}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005341
5342/*
5343 * "getwininfo()" function
5344 */
5345 static void
5346f_getwininfo(typval_T *argvars, typval_T *rettv)
5347{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005348 tabpage_T *tp;
5349 win_T *wp = NULL, *wparg = NULL;
5350 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005351 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005352
5353 if (rettv_list_alloc(rettv) != OK)
5354 return;
5355
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005356 if (argvars[0].v_type != VAR_UNKNOWN)
5357 {
5358 wparg = win_id2wp(argvars);
5359 if (wparg == NULL)
5360 return;
5361 }
5362
5363 /* Collect information about either all the windows across all the tab
5364 * pages or one particular window.
5365 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005366 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005367 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005368 tabnr++;
5369 winnr = 0;
5370 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005371 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005372 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005373 if (wparg != NULL && wp != wparg)
5374 continue;
5375 d = get_win_info(wp, tabnr, winnr);
5376 if (d != NULL)
5377 list_append_dict(rettv->vval.v_list, d);
5378 if (wparg != NULL)
5379 /* found information about a specific window */
5380 return;
5381 }
5382 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005383}
5384
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005385/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005386 * "win_findbuf()" function
5387 */
5388 static void
5389f_win_findbuf(typval_T *argvars, typval_T *rettv)
5390{
5391 if (rettv_list_alloc(rettv) != FAIL)
5392 win_findbuf(argvars, rettv->vval.v_list);
5393}
5394
5395/*
5396 * "win_getid()" function
5397 */
5398 static void
5399f_win_getid(typval_T *argvars, typval_T *rettv)
5400{
5401 rettv->vval.v_number = win_getid(argvars);
5402}
5403
5404/*
5405 * "win_gotoid()" function
5406 */
5407 static void
5408f_win_gotoid(typval_T *argvars, typval_T *rettv)
5409{
5410 rettv->vval.v_number = win_gotoid(argvars);
5411}
5412
5413/*
5414 * "win_id2tabwin()" function
5415 */
5416 static void
5417f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5418{
5419 if (rettv_list_alloc(rettv) != FAIL)
5420 win_id2tabwin(argvars, rettv->vval.v_list);
5421}
5422
5423/*
5424 * "win_id2win()" function
5425 */
5426 static void
5427f_win_id2win(typval_T *argvars, typval_T *rettv)
5428{
5429 rettv->vval.v_number = win_id2win(argvars);
5430}
5431
5432/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005433 * "win_screenpos()" function
5434 */
5435 static void
5436f_win_screenpos(typval_T *argvars, typval_T *rettv)
5437{
5438 win_T *wp;
5439
5440 if (rettv_list_alloc(rettv) == FAIL)
5441 return;
5442
5443 wp = find_win_by_nr(&argvars[0], NULL);
5444 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5445 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5446}
5447
5448/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005449 * "getwinposx()" function
5450 */
5451 static void
5452f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5453{
5454 rettv->vval.v_number = -1;
5455#ifdef FEAT_GUI
5456 if (gui.in_use)
5457 {
5458 int x, y;
5459
5460 if (gui_mch_get_winpos(&x, &y) == OK)
5461 rettv->vval.v_number = x;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005462 return;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005463 }
5464#endif
5465#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5466 {
5467 int x, y;
5468
5469 if (term_get_winpos(&x, &y) == OK)
5470 rettv->vval.v_number = x;
5471 }
5472#endif
5473}
5474
5475/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005476 * "getwinposy()" function
5477 */
5478 static void
5479f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5480{
5481 rettv->vval.v_number = -1;
5482#ifdef FEAT_GUI
5483 if (gui.in_use)
5484 {
5485 int x, y;
5486
5487 if (gui_mch_get_winpos(&x, &y) == OK)
5488 rettv->vval.v_number = y;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005489 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005490 }
5491#endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005492#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5493 {
5494 int x, y;
5495
5496 if (term_get_winpos(&x, &y) == OK)
5497 rettv->vval.v_number = y;
5498 }
5499#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005500}
5501
5502/*
5503 * "getwinvar()" function
5504 */
5505 static void
5506f_getwinvar(typval_T *argvars, typval_T *rettv)
5507{
5508 getwinvar(argvars, rettv, 0);
5509}
5510
5511/*
5512 * "glob()" function
5513 */
5514 static void
5515f_glob(typval_T *argvars, typval_T *rettv)
5516{
5517 int options = WILD_SILENT|WILD_USE_NL;
5518 expand_T xpc;
5519 int error = FALSE;
5520
5521 /* When the optional second argument is non-zero, don't remove matches
5522 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5523 rettv->v_type = VAR_STRING;
5524 if (argvars[1].v_type != VAR_UNKNOWN)
5525 {
5526 if (get_tv_number_chk(&argvars[1], &error))
5527 options |= WILD_KEEP_ALL;
5528 if (argvars[2].v_type != VAR_UNKNOWN)
5529 {
5530 if (get_tv_number_chk(&argvars[2], &error))
5531 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005532 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005533 }
5534 if (argvars[3].v_type != VAR_UNKNOWN
5535 && get_tv_number_chk(&argvars[3], &error))
5536 options |= WILD_ALLLINKS;
5537 }
5538 }
5539 if (!error)
5540 {
5541 ExpandInit(&xpc);
5542 xpc.xp_context = EXPAND_FILES;
5543 if (p_wic)
5544 options += WILD_ICASE;
5545 if (rettv->v_type == VAR_STRING)
5546 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
5547 NULL, options, WILD_ALL);
5548 else if (rettv_list_alloc(rettv) != FAIL)
5549 {
5550 int i;
5551
5552 ExpandOne(&xpc, get_tv_string(&argvars[0]),
5553 NULL, options, WILD_ALL_KEEP);
5554 for (i = 0; i < xpc.xp_numfiles; i++)
5555 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5556
5557 ExpandCleanup(&xpc);
5558 }
5559 }
5560 else
5561 rettv->vval.v_string = NULL;
5562}
5563
5564/*
5565 * "globpath()" function
5566 */
5567 static void
5568f_globpath(typval_T *argvars, typval_T *rettv)
5569{
5570 int flags = 0;
5571 char_u buf1[NUMBUFLEN];
5572 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
5573 int error = FALSE;
5574 garray_T ga;
5575 int i;
5576
5577 /* When the optional second argument is non-zero, don't remove matches
5578 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5579 rettv->v_type = VAR_STRING;
5580 if (argvars[2].v_type != VAR_UNKNOWN)
5581 {
5582 if (get_tv_number_chk(&argvars[2], &error))
5583 flags |= WILD_KEEP_ALL;
5584 if (argvars[3].v_type != VAR_UNKNOWN)
5585 {
5586 if (get_tv_number_chk(&argvars[3], &error))
5587 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005588 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005589 }
5590 if (argvars[4].v_type != VAR_UNKNOWN
5591 && get_tv_number_chk(&argvars[4], &error))
5592 flags |= WILD_ALLLINKS;
5593 }
5594 }
5595 if (file != NULL && !error)
5596 {
5597 ga_init2(&ga, (int)sizeof(char_u *), 10);
5598 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
5599 if (rettv->v_type == VAR_STRING)
5600 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
5601 else if (rettv_list_alloc(rettv) != FAIL)
5602 for (i = 0; i < ga.ga_len; ++i)
5603 list_append_string(rettv->vval.v_list,
5604 ((char_u **)(ga.ga_data))[i], -1);
5605 ga_clear_strings(&ga);
5606 }
5607 else
5608 rettv->vval.v_string = NULL;
5609}
5610
5611/*
5612 * "glob2regpat()" function
5613 */
5614 static void
5615f_glob2regpat(typval_T *argvars, typval_T *rettv)
5616{
5617 char_u *pat = get_tv_string_chk(&argvars[0]);
5618
5619 rettv->v_type = VAR_STRING;
5620 rettv->vval.v_string = (pat == NULL)
5621 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
5622}
5623
5624/* for VIM_VERSION_ defines */
5625#include "version.h"
5626
5627/*
5628 * "has()" function
5629 */
5630 static void
5631f_has(typval_T *argvars, typval_T *rettv)
5632{
5633 int i;
5634 char_u *name;
5635 int n = FALSE;
5636 static char *(has_list[]) =
5637 {
5638#ifdef AMIGA
5639 "amiga",
5640# ifdef FEAT_ARP
5641 "arp",
5642# endif
5643#endif
5644#ifdef __BEOS__
5645 "beos",
5646#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02005647#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01005648 "mac", /* Mac OS X (and, once, Mac OS Classic) */
5649 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02005650# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01005651 "macunix", /* Mac OS X, with the darwin feature */
5652 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02005653# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005654#endif
5655#ifdef __QNX__
5656 "qnx",
5657#endif
5658#ifdef UNIX
5659 "unix",
5660#endif
5661#ifdef VMS
5662 "vms",
5663#endif
5664#ifdef WIN32
5665 "win32",
5666#endif
5667#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
5668 "win32unix",
5669#endif
5670#if defined(WIN64) || defined(_WIN64)
5671 "win64",
5672#endif
5673#ifdef EBCDIC
5674 "ebcdic",
5675#endif
5676#ifndef CASE_INSENSITIVE_FILENAME
5677 "fname_case",
5678#endif
5679#ifdef HAVE_ACL
5680 "acl",
5681#endif
5682#ifdef FEAT_ARABIC
5683 "arabic",
5684#endif
5685#ifdef FEAT_AUTOCMD
5686 "autocmd",
5687#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01005688#ifdef FEAT_AUTOSERVERNAME
5689 "autoservername",
5690#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01005691#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005692 "balloon_eval",
5693# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
5694 "balloon_multiline",
5695# endif
5696#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01005697#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01005698 "balloon_eval_term",
5699#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005700#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
5701 "builtin_terms",
5702# ifdef ALL_BUILTIN_TCAPS
5703 "all_builtin_terms",
5704# endif
5705#endif
5706#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
5707 || defined(FEAT_GUI_W32) \
5708 || defined(FEAT_GUI_MOTIF))
5709 "browsefilter",
5710#endif
5711#ifdef FEAT_BYTEOFF
5712 "byte_offset",
5713#endif
5714#ifdef FEAT_JOB_CHANNEL
5715 "channel",
5716#endif
5717#ifdef FEAT_CINDENT
5718 "cindent",
5719#endif
5720#ifdef FEAT_CLIENTSERVER
5721 "clientserver",
5722#endif
5723#ifdef FEAT_CLIPBOARD
5724 "clipboard",
5725#endif
5726#ifdef FEAT_CMDL_COMPL
5727 "cmdline_compl",
5728#endif
5729#ifdef FEAT_CMDHIST
5730 "cmdline_hist",
5731#endif
5732#ifdef FEAT_COMMENTS
5733 "comments",
5734#endif
5735#ifdef FEAT_CONCEAL
5736 "conceal",
5737#endif
5738#ifdef FEAT_CRYPT
5739 "cryptv",
5740 "crypt-blowfish",
5741 "crypt-blowfish2",
5742#endif
5743#ifdef FEAT_CSCOPE
5744 "cscope",
5745#endif
5746#ifdef FEAT_CURSORBIND
5747 "cursorbind",
5748#endif
5749#ifdef CURSOR_SHAPE
5750 "cursorshape",
5751#endif
5752#ifdef DEBUG
5753 "debug",
5754#endif
5755#ifdef FEAT_CON_DIALOG
5756 "dialog_con",
5757#endif
5758#ifdef FEAT_GUI_DIALOG
5759 "dialog_gui",
5760#endif
5761#ifdef FEAT_DIFF
5762 "diff",
5763#endif
5764#ifdef FEAT_DIGRAPHS
5765 "digraphs",
5766#endif
5767#ifdef FEAT_DIRECTX
5768 "directx",
5769#endif
5770#ifdef FEAT_DND
5771 "dnd",
5772#endif
5773#ifdef FEAT_EMACS_TAGS
5774 "emacs_tags",
5775#endif
5776 "eval", /* always present, of course! */
5777 "ex_extra", /* graduated feature */
5778#ifdef FEAT_SEARCH_EXTRA
5779 "extra_search",
5780#endif
5781#ifdef FEAT_FKMAP
5782 "farsi",
5783#endif
5784#ifdef FEAT_SEARCHPATH
5785 "file_in_path",
5786#endif
5787#ifdef FEAT_FILTERPIPE
5788 "filterpipe",
5789#endif
5790#ifdef FEAT_FIND_ID
5791 "find_in_path",
5792#endif
5793#ifdef FEAT_FLOAT
5794 "float",
5795#endif
5796#ifdef FEAT_FOLDING
5797 "folding",
5798#endif
5799#ifdef FEAT_FOOTER
5800 "footer",
5801#endif
5802#if !defined(USE_SYSTEM) && defined(UNIX)
5803 "fork",
5804#endif
5805#ifdef FEAT_GETTEXT
5806 "gettext",
5807#endif
5808#ifdef FEAT_GUI
5809 "gui",
5810#endif
5811#ifdef FEAT_GUI_ATHENA
5812# ifdef FEAT_GUI_NEXTAW
5813 "gui_neXtaw",
5814# else
5815 "gui_athena",
5816# endif
5817#endif
5818#ifdef FEAT_GUI_GTK
5819 "gui_gtk",
5820# ifdef USE_GTK3
5821 "gui_gtk3",
5822# else
5823 "gui_gtk2",
5824# endif
5825#endif
5826#ifdef FEAT_GUI_GNOME
5827 "gui_gnome",
5828#endif
5829#ifdef FEAT_GUI_MAC
5830 "gui_mac",
5831#endif
5832#ifdef FEAT_GUI_MOTIF
5833 "gui_motif",
5834#endif
5835#ifdef FEAT_GUI_PHOTON
5836 "gui_photon",
5837#endif
5838#ifdef FEAT_GUI_W32
5839 "gui_win32",
5840#endif
5841#ifdef FEAT_HANGULIN
5842 "hangul_input",
5843#endif
5844#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
5845 "iconv",
5846#endif
5847#ifdef FEAT_INS_EXPAND
5848 "insert_expand",
5849#endif
5850#ifdef FEAT_JOB_CHANNEL
5851 "job",
5852#endif
5853#ifdef FEAT_JUMPLIST
5854 "jumplist",
5855#endif
5856#ifdef FEAT_KEYMAP
5857 "keymap",
5858#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02005859 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005860#ifdef FEAT_LANGMAP
5861 "langmap",
5862#endif
5863#ifdef FEAT_LIBCALL
5864 "libcall",
5865#endif
5866#ifdef FEAT_LINEBREAK
5867 "linebreak",
5868#endif
5869#ifdef FEAT_LISP
5870 "lispindent",
5871#endif
5872#ifdef FEAT_LISTCMDS
5873 "listcmds",
5874#endif
5875#ifdef FEAT_LOCALMAP
5876 "localmap",
5877#endif
5878#ifdef FEAT_LUA
5879# ifndef DYNAMIC_LUA
5880 "lua",
5881# endif
5882#endif
5883#ifdef FEAT_MENU
5884 "menu",
5885#endif
5886#ifdef FEAT_SESSION
5887 "mksession",
5888#endif
5889#ifdef FEAT_MODIFY_FNAME
5890 "modify_fname",
5891#endif
5892#ifdef FEAT_MOUSE
5893 "mouse",
5894#endif
5895#ifdef FEAT_MOUSESHAPE
5896 "mouseshape",
5897#endif
5898#if defined(UNIX) || defined(VMS)
5899# ifdef FEAT_MOUSE_DEC
5900 "mouse_dec",
5901# endif
5902# ifdef FEAT_MOUSE_GPM
5903 "mouse_gpm",
5904# endif
5905# ifdef FEAT_MOUSE_JSB
5906 "mouse_jsbterm",
5907# endif
5908# ifdef FEAT_MOUSE_NET
5909 "mouse_netterm",
5910# endif
5911# ifdef FEAT_MOUSE_PTERM
5912 "mouse_pterm",
5913# endif
5914# ifdef FEAT_MOUSE_SGR
5915 "mouse_sgr",
5916# endif
5917# ifdef FEAT_SYSMOUSE
5918 "mouse_sysmouse",
5919# endif
5920# ifdef FEAT_MOUSE_URXVT
5921 "mouse_urxvt",
5922# endif
5923# ifdef FEAT_MOUSE_XTERM
5924 "mouse_xterm",
5925# endif
5926#endif
5927#ifdef FEAT_MBYTE
5928 "multi_byte",
5929#endif
5930#ifdef FEAT_MBYTE_IME
5931 "multi_byte_ime",
5932#endif
5933#ifdef FEAT_MULTI_LANG
5934 "multi_lang",
5935#endif
5936#ifdef FEAT_MZSCHEME
5937#ifndef DYNAMIC_MZSCHEME
5938 "mzscheme",
5939#endif
5940#endif
5941#ifdef FEAT_NUM64
5942 "num64",
5943#endif
5944#ifdef FEAT_OLE
5945 "ole",
5946#endif
5947 "packages",
5948#ifdef FEAT_PATH_EXTRA
5949 "path_extra",
5950#endif
5951#ifdef FEAT_PERL
5952#ifndef DYNAMIC_PERL
5953 "perl",
5954#endif
5955#endif
5956#ifdef FEAT_PERSISTENT_UNDO
5957 "persistent_undo",
5958#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01005959#if defined(FEAT_PYTHON)
5960 "python_compiled",
5961# if defined(DYNAMIC_PYTHON)
5962 "python_dynamic",
5963# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005964 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005965 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01005966# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005967#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01005968#if defined(FEAT_PYTHON3)
5969 "python3_compiled",
5970# if defined(DYNAMIC_PYTHON3)
5971 "python3_dynamic",
5972# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005973 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005974 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01005975# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005976#endif
5977#ifdef FEAT_POSTSCRIPT
5978 "postscript",
5979#endif
5980#ifdef FEAT_PRINTER
5981 "printer",
5982#endif
5983#ifdef FEAT_PROFILE
5984 "profile",
5985#endif
5986#ifdef FEAT_RELTIME
5987 "reltime",
5988#endif
5989#ifdef FEAT_QUICKFIX
5990 "quickfix",
5991#endif
5992#ifdef FEAT_RIGHTLEFT
5993 "rightleft",
5994#endif
5995#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
5996 "ruby",
5997#endif
5998#ifdef FEAT_SCROLLBIND
5999 "scrollbind",
6000#endif
6001#ifdef FEAT_CMDL_INFO
6002 "showcmd",
6003 "cmdline_info",
6004#endif
6005#ifdef FEAT_SIGNS
6006 "signs",
6007#endif
6008#ifdef FEAT_SMARTINDENT
6009 "smartindent",
6010#endif
6011#ifdef STARTUPTIME
6012 "startuptime",
6013#endif
6014#ifdef FEAT_STL_OPT
6015 "statusline",
6016#endif
6017#ifdef FEAT_SUN_WORKSHOP
6018 "sun_workshop",
6019#endif
6020#ifdef FEAT_NETBEANS_INTG
6021 "netbeans_intg",
6022#endif
6023#ifdef FEAT_SPELL
6024 "spell",
6025#endif
6026#ifdef FEAT_SYN_HL
6027 "syntax",
6028#endif
6029#if defined(USE_SYSTEM) || !defined(UNIX)
6030 "system",
6031#endif
6032#ifdef FEAT_TAG_BINS
6033 "tag_binary",
6034#endif
6035#ifdef FEAT_TAG_OLDSTATIC
6036 "tag_old_static",
6037#endif
6038#ifdef FEAT_TAG_ANYWHITE
6039 "tag_any_white",
6040#endif
6041#ifdef FEAT_TCL
6042# ifndef DYNAMIC_TCL
6043 "tcl",
6044# endif
6045#endif
6046#ifdef FEAT_TERMGUICOLORS
6047 "termguicolors",
6048#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006049#if defined(FEAT_TERMINAL) && !defined(WIN3264)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006050 "terminal",
6051#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006052#ifdef TERMINFO
6053 "terminfo",
6054#endif
6055#ifdef FEAT_TERMRESPONSE
6056 "termresponse",
6057#endif
6058#ifdef FEAT_TEXTOBJ
6059 "textobjects",
6060#endif
6061#ifdef HAVE_TGETENT
6062 "tgetent",
6063#endif
6064#ifdef FEAT_TIMERS
6065 "timers",
6066#endif
6067#ifdef FEAT_TITLE
6068 "title",
6069#endif
6070#ifdef FEAT_TOOLBAR
6071 "toolbar",
6072#endif
6073#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6074 "unnamedplus",
6075#endif
6076#ifdef FEAT_USR_CMDS
6077 "user-commands", /* was accidentally included in 5.4 */
6078 "user_commands",
6079#endif
6080#ifdef FEAT_VIMINFO
6081 "viminfo",
6082#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006083 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006084#ifdef FEAT_VIRTUALEDIT
6085 "virtualedit",
6086#endif
6087 "visual",
6088#ifdef FEAT_VISUALEXTRA
6089 "visualextra",
6090#endif
6091#ifdef FEAT_VREPLACE
6092 "vreplace",
6093#endif
6094#ifdef FEAT_WILDIGN
6095 "wildignore",
6096#endif
6097#ifdef FEAT_WILDMENU
6098 "wildmenu",
6099#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006100 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006101#ifdef FEAT_WAK
6102 "winaltkeys",
6103#endif
6104#ifdef FEAT_WRITEBACKUP
6105 "writebackup",
6106#endif
6107#ifdef FEAT_XIM
6108 "xim",
6109#endif
6110#ifdef FEAT_XFONTSET
6111 "xfontset",
6112#endif
6113#ifdef FEAT_XPM_W32
6114 "xpm",
6115 "xpm_w32", /* for backward compatibility */
6116#else
6117# if defined(HAVE_XPM)
6118 "xpm",
6119# endif
6120#endif
6121#ifdef USE_XSMP
6122 "xsmp",
6123#endif
6124#ifdef USE_XSMP_INTERACT
6125 "xsmp_interact",
6126#endif
6127#ifdef FEAT_XCLIPBOARD
6128 "xterm_clipboard",
6129#endif
6130#ifdef FEAT_XTERM_SAVE
6131 "xterm_save",
6132#endif
6133#if defined(UNIX) && defined(FEAT_X11)
6134 "X11",
6135#endif
6136 NULL
6137 };
6138
6139 name = get_tv_string(&argvars[0]);
6140 for (i = 0; has_list[i] != NULL; ++i)
6141 if (STRICMP(name, has_list[i]) == 0)
6142 {
6143 n = TRUE;
6144 break;
6145 }
6146
6147 if (n == FALSE)
6148 {
6149 if (STRNICMP(name, "patch", 5) == 0)
6150 {
6151 if (name[5] == '-'
6152 && STRLEN(name) >= 11
6153 && vim_isdigit(name[6])
6154 && vim_isdigit(name[8])
6155 && vim_isdigit(name[10]))
6156 {
6157 int major = atoi((char *)name + 6);
6158 int minor = atoi((char *)name + 8);
6159
6160 /* Expect "patch-9.9.01234". */
6161 n = (major < VIM_VERSION_MAJOR
6162 || (major == VIM_VERSION_MAJOR
6163 && (minor < VIM_VERSION_MINOR
6164 || (minor == VIM_VERSION_MINOR
6165 && has_patch(atoi((char *)name + 10))))));
6166 }
6167 else
6168 n = has_patch(atoi((char *)name + 5));
6169 }
6170 else if (STRICMP(name, "vim_starting") == 0)
6171 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006172 else if (STRICMP(name, "ttyin") == 0)
6173 n = mch_input_isatty();
6174 else if (STRICMP(name, "ttyout") == 0)
6175 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006176#ifdef FEAT_MBYTE
6177 else if (STRICMP(name, "multi_byte_encoding") == 0)
6178 n = has_mbyte;
6179#endif
6180#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
6181 else if (STRICMP(name, "balloon_multiline") == 0)
6182 n = multiline_balloon_available();
6183#endif
6184#ifdef DYNAMIC_TCL
6185 else if (STRICMP(name, "tcl") == 0)
6186 n = tcl_enabled(FALSE);
6187#endif
6188#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6189 else if (STRICMP(name, "iconv") == 0)
6190 n = iconv_enabled(FALSE);
6191#endif
6192#ifdef DYNAMIC_LUA
6193 else if (STRICMP(name, "lua") == 0)
6194 n = lua_enabled(FALSE);
6195#endif
6196#ifdef DYNAMIC_MZSCHEME
6197 else if (STRICMP(name, "mzscheme") == 0)
6198 n = mzscheme_enabled(FALSE);
6199#endif
6200#ifdef DYNAMIC_RUBY
6201 else if (STRICMP(name, "ruby") == 0)
6202 n = ruby_enabled(FALSE);
6203#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006204#ifdef DYNAMIC_PYTHON
6205 else if (STRICMP(name, "python") == 0)
6206 n = python_enabled(FALSE);
6207#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006208#ifdef DYNAMIC_PYTHON3
6209 else if (STRICMP(name, "python3") == 0)
6210 n = python3_enabled(FALSE);
6211#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006212#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6213 else if (STRICMP(name, "pythonx") == 0)
6214 {
6215# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6216 if (p_pyx == 0)
6217 n = python3_enabled(FALSE) || python_enabled(FALSE);
6218 else if (p_pyx == 3)
6219 n = python3_enabled(FALSE);
6220 else if (p_pyx == 2)
6221 n = python_enabled(FALSE);
6222# elif defined(DYNAMIC_PYTHON)
6223 n = python_enabled(FALSE);
6224# elif defined(DYNAMIC_PYTHON3)
6225 n = python3_enabled(FALSE);
6226# endif
6227 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006228#endif
6229#ifdef DYNAMIC_PERL
6230 else if (STRICMP(name, "perl") == 0)
6231 n = perl_enabled(FALSE);
6232#endif
6233#ifdef FEAT_GUI
6234 else if (STRICMP(name, "gui_running") == 0)
6235 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006236# ifdef FEAT_BROWSE
6237 else if (STRICMP(name, "browse") == 0)
6238 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6239# endif
6240#endif
6241#ifdef FEAT_SYN_HL
6242 else if (STRICMP(name, "syntax_items") == 0)
6243 n = syntax_present(curwin);
6244#endif
6245#if defined(WIN3264)
6246 else if (STRICMP(name, "win95") == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006247 n = FALSE; /* Win9x is no more supported. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006248#endif
6249#ifdef FEAT_NETBEANS_INTG
6250 else if (STRICMP(name, "netbeans_enabled") == 0)
6251 n = netbeans_active();
6252#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006253#if defined(FEAT_TERMINAL) && defined(WIN3264)
6254 else if (STRICMP(name, "terminal") == 0)
6255 n = terminal_enabled();
6256#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006257 }
6258
6259 rettv->vval.v_number = n;
6260}
6261
6262/*
6263 * "has_key()" function
6264 */
6265 static void
6266f_has_key(typval_T *argvars, typval_T *rettv)
6267{
6268 if (argvars[0].v_type != VAR_DICT)
6269 {
6270 EMSG(_(e_dictreq));
6271 return;
6272 }
6273 if (argvars[0].vval.v_dict == NULL)
6274 return;
6275
6276 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
6277 get_tv_string(&argvars[1]), -1) != NULL;
6278}
6279
6280/*
6281 * "haslocaldir()" function
6282 */
6283 static void
6284f_haslocaldir(typval_T *argvars, typval_T *rettv)
6285{
6286 win_T *wp = NULL;
6287
6288 wp = find_tabwin(&argvars[0], &argvars[1]);
6289 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6290}
6291
6292/*
6293 * "hasmapto()" function
6294 */
6295 static void
6296f_hasmapto(typval_T *argvars, typval_T *rettv)
6297{
6298 char_u *name;
6299 char_u *mode;
6300 char_u buf[NUMBUFLEN];
6301 int abbr = FALSE;
6302
6303 name = get_tv_string(&argvars[0]);
6304 if (argvars[1].v_type == VAR_UNKNOWN)
6305 mode = (char_u *)"nvo";
6306 else
6307 {
6308 mode = get_tv_string_buf(&argvars[1], buf);
6309 if (argvars[2].v_type != VAR_UNKNOWN)
6310 abbr = (int)get_tv_number(&argvars[2]);
6311 }
6312
6313 if (map_to_exists(name, mode, abbr))
6314 rettv->vval.v_number = TRUE;
6315 else
6316 rettv->vval.v_number = FALSE;
6317}
6318
6319/*
6320 * "histadd()" function
6321 */
6322 static void
6323f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6324{
6325#ifdef FEAT_CMDHIST
6326 int histype;
6327 char_u *str;
6328 char_u buf[NUMBUFLEN];
6329#endif
6330
6331 rettv->vval.v_number = FALSE;
6332 if (check_restricted() || check_secure())
6333 return;
6334#ifdef FEAT_CMDHIST
6335 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6336 histype = str != NULL ? get_histtype(str) : -1;
6337 if (histype >= 0)
6338 {
6339 str = get_tv_string_buf(&argvars[1], buf);
6340 if (*str != NUL)
6341 {
6342 init_history();
6343 add_to_history(histype, str, FALSE, NUL);
6344 rettv->vval.v_number = TRUE;
6345 return;
6346 }
6347 }
6348#endif
6349}
6350
6351/*
6352 * "histdel()" function
6353 */
6354 static void
6355f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6356{
6357#ifdef FEAT_CMDHIST
6358 int n;
6359 char_u buf[NUMBUFLEN];
6360 char_u *str;
6361
6362 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6363 if (str == NULL)
6364 n = 0;
6365 else if (argvars[1].v_type == VAR_UNKNOWN)
6366 /* only one argument: clear entire history */
6367 n = clr_history(get_histtype(str));
6368 else if (argvars[1].v_type == VAR_NUMBER)
6369 /* index given: remove that entry */
6370 n = del_history_idx(get_histtype(str),
6371 (int)get_tv_number(&argvars[1]));
6372 else
6373 /* string given: remove all matching entries */
6374 n = del_history_entry(get_histtype(str),
6375 get_tv_string_buf(&argvars[1], buf));
6376 rettv->vval.v_number = n;
6377#endif
6378}
6379
6380/*
6381 * "histget()" function
6382 */
6383 static void
6384f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6385{
6386#ifdef FEAT_CMDHIST
6387 int type;
6388 int idx;
6389 char_u *str;
6390
6391 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6392 if (str == NULL)
6393 rettv->vval.v_string = NULL;
6394 else
6395 {
6396 type = get_histtype(str);
6397 if (argvars[1].v_type == VAR_UNKNOWN)
6398 idx = get_history_idx(type);
6399 else
6400 idx = (int)get_tv_number_chk(&argvars[1], NULL);
6401 /* -1 on type error */
6402 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6403 }
6404#else
6405 rettv->vval.v_string = NULL;
6406#endif
6407 rettv->v_type = VAR_STRING;
6408}
6409
6410/*
6411 * "histnr()" function
6412 */
6413 static void
6414f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6415{
6416 int i;
6417
6418#ifdef FEAT_CMDHIST
6419 char_u *history = get_tv_string_chk(&argvars[0]);
6420
6421 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6422 if (i >= HIST_CMD && i < HIST_COUNT)
6423 i = get_history_idx(i);
6424 else
6425#endif
6426 i = -1;
6427 rettv->vval.v_number = i;
6428}
6429
6430/*
6431 * "highlightID(name)" function
6432 */
6433 static void
6434f_hlID(typval_T *argvars, typval_T *rettv)
6435{
6436 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
6437}
6438
6439/*
6440 * "highlight_exists()" function
6441 */
6442 static void
6443f_hlexists(typval_T *argvars, typval_T *rettv)
6444{
6445 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
6446}
6447
6448/*
6449 * "hostname()" function
6450 */
6451 static void
6452f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6453{
6454 char_u hostname[256];
6455
6456 mch_get_host_name(hostname, 256);
6457 rettv->v_type = VAR_STRING;
6458 rettv->vval.v_string = vim_strsave(hostname);
6459}
6460
6461/*
6462 * iconv() function
6463 */
6464 static void
6465f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6466{
6467#ifdef FEAT_MBYTE
6468 char_u buf1[NUMBUFLEN];
6469 char_u buf2[NUMBUFLEN];
6470 char_u *from, *to, *str;
6471 vimconv_T vimconv;
6472#endif
6473
6474 rettv->v_type = VAR_STRING;
6475 rettv->vval.v_string = NULL;
6476
6477#ifdef FEAT_MBYTE
6478 str = get_tv_string(&argvars[0]);
6479 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
6480 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
6481 vimconv.vc_type = CONV_NONE;
6482 convert_setup(&vimconv, from, to);
6483
6484 /* If the encodings are equal, no conversion needed. */
6485 if (vimconv.vc_type == CONV_NONE)
6486 rettv->vval.v_string = vim_strsave(str);
6487 else
6488 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6489
6490 convert_setup(&vimconv, NULL, NULL);
6491 vim_free(from);
6492 vim_free(to);
6493#endif
6494}
6495
6496/*
6497 * "indent()" function
6498 */
6499 static void
6500f_indent(typval_T *argvars, typval_T *rettv)
6501{
6502 linenr_T lnum;
6503
6504 lnum = get_tv_lnum(argvars);
6505 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6506 rettv->vval.v_number = get_indent_lnum(lnum);
6507 else
6508 rettv->vval.v_number = -1;
6509}
6510
6511/*
6512 * "index()" function
6513 */
6514 static void
6515f_index(typval_T *argvars, typval_T *rettv)
6516{
6517 list_T *l;
6518 listitem_T *item;
6519 long idx = 0;
6520 int ic = FALSE;
6521
6522 rettv->vval.v_number = -1;
6523 if (argvars[0].v_type != VAR_LIST)
6524 {
6525 EMSG(_(e_listreq));
6526 return;
6527 }
6528 l = argvars[0].vval.v_list;
6529 if (l != NULL)
6530 {
6531 item = l->lv_first;
6532 if (argvars[2].v_type != VAR_UNKNOWN)
6533 {
6534 int error = FALSE;
6535
6536 /* Start at specified item. Use the cached index that list_find()
6537 * sets, so that a negative number also works. */
6538 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
6539 idx = l->lv_idx;
6540 if (argvars[3].v_type != VAR_UNKNOWN)
6541 ic = (int)get_tv_number_chk(&argvars[3], &error);
6542 if (error)
6543 item = NULL;
6544 }
6545
6546 for ( ; item != NULL; item = item->li_next, ++idx)
6547 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6548 {
6549 rettv->vval.v_number = idx;
6550 break;
6551 }
6552 }
6553}
6554
6555static int inputsecret_flag = 0;
6556
6557/*
6558 * "input()" function
6559 * Also handles inputsecret() when inputsecret is set.
6560 */
6561 static void
6562f_input(typval_T *argvars, typval_T *rettv)
6563{
6564 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6565}
6566
6567/*
6568 * "inputdialog()" function
6569 */
6570 static void
6571f_inputdialog(typval_T *argvars, typval_T *rettv)
6572{
6573#if defined(FEAT_GUI_TEXTDIALOG)
6574 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
6575 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6576 {
6577 char_u *message;
6578 char_u buf[NUMBUFLEN];
6579 char_u *defstr = (char_u *)"";
6580
6581 message = get_tv_string_chk(&argvars[0]);
6582 if (argvars[1].v_type != VAR_UNKNOWN
6583 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
6584 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6585 else
6586 IObuff[0] = NUL;
6587 if (message != NULL && defstr != NULL
6588 && do_dialog(VIM_QUESTION, NULL, message,
6589 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6590 rettv->vval.v_string = vim_strsave(IObuff);
6591 else
6592 {
6593 if (message != NULL && defstr != NULL
6594 && argvars[1].v_type != VAR_UNKNOWN
6595 && argvars[2].v_type != VAR_UNKNOWN)
6596 rettv->vval.v_string = vim_strsave(
6597 get_tv_string_buf(&argvars[2], buf));
6598 else
6599 rettv->vval.v_string = NULL;
6600 }
6601 rettv->v_type = VAR_STRING;
6602 }
6603 else
6604#endif
6605 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6606}
6607
6608/*
6609 * "inputlist()" function
6610 */
6611 static void
6612f_inputlist(typval_T *argvars, typval_T *rettv)
6613{
6614 listitem_T *li;
6615 int selected;
6616 int mouse_used;
6617
6618#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02006619 /* While starting up, there is no place to enter text. When running tests
6620 * with --not-a-term we assume feedkeys() will be used. */
6621 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006622 return;
6623#endif
6624 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6625 {
6626 EMSG2(_(e_listarg), "inputlist()");
6627 return;
6628 }
6629
6630 msg_start();
6631 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
6632 lines_left = Rows; /* avoid more prompt */
6633 msg_scroll = TRUE;
6634 msg_clr_eos();
6635
6636 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
6637 {
6638 msg_puts(get_tv_string(&li->li_tv));
6639 msg_putchar('\n');
6640 }
6641
6642 /* Ask for choice. */
6643 selected = prompt_for_number(&mouse_used);
6644 if (mouse_used)
6645 selected -= lines_left;
6646
6647 rettv->vval.v_number = selected;
6648}
6649
6650
6651static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6652
6653/*
6654 * "inputrestore()" function
6655 */
6656 static void
6657f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6658{
6659 if (ga_userinput.ga_len > 0)
6660 {
6661 --ga_userinput.ga_len;
6662 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
6663 + ga_userinput.ga_len);
6664 /* default return is zero == OK */
6665 }
6666 else if (p_verbose > 1)
6667 {
6668 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
6669 rettv->vval.v_number = 1; /* Failed */
6670 }
6671}
6672
6673/*
6674 * "inputsave()" function
6675 */
6676 static void
6677f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
6678{
6679 /* Add an entry to the stack of typeahead storage. */
6680 if (ga_grow(&ga_userinput, 1) == OK)
6681 {
6682 save_typeahead((tasave_T *)(ga_userinput.ga_data)
6683 + ga_userinput.ga_len);
6684 ++ga_userinput.ga_len;
6685 /* default return is zero == OK */
6686 }
6687 else
6688 rettv->vval.v_number = 1; /* Failed */
6689}
6690
6691/*
6692 * "inputsecret()" function
6693 */
6694 static void
6695f_inputsecret(typval_T *argvars, typval_T *rettv)
6696{
6697 ++cmdline_star;
6698 ++inputsecret_flag;
6699 f_input(argvars, rettv);
6700 --cmdline_star;
6701 --inputsecret_flag;
6702}
6703
6704/*
6705 * "insert()" function
6706 */
6707 static void
6708f_insert(typval_T *argvars, typval_T *rettv)
6709{
6710 long before = 0;
6711 listitem_T *item;
6712 list_T *l;
6713 int error = FALSE;
6714
6715 if (argvars[0].v_type != VAR_LIST)
6716 EMSG2(_(e_listarg), "insert()");
6717 else if ((l = argvars[0].vval.v_list) != NULL
6718 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
6719 {
6720 if (argvars[2].v_type != VAR_UNKNOWN)
6721 before = (long)get_tv_number_chk(&argvars[2], &error);
6722 if (error)
6723 return; /* type error; errmsg already given */
6724
6725 if (before == l->lv_len)
6726 item = NULL;
6727 else
6728 {
6729 item = list_find(l, before);
6730 if (item == NULL)
6731 {
6732 EMSGN(_(e_listidx), before);
6733 l = NULL;
6734 }
6735 }
6736 if (l != NULL)
6737 {
6738 list_insert_tv(l, &argvars[1], item);
6739 copy_tv(&argvars[0], rettv);
6740 }
6741 }
6742}
6743
6744/*
6745 * "invert(expr)" function
6746 */
6747 static void
6748f_invert(typval_T *argvars, typval_T *rettv)
6749{
6750 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
6751}
6752
6753/*
6754 * "isdirectory()" function
6755 */
6756 static void
6757f_isdirectory(typval_T *argvars, typval_T *rettv)
6758{
6759 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
6760}
6761
6762/*
6763 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
6764 * or it refers to a List or Dictionary that is locked.
6765 */
6766 static int
6767tv_islocked(typval_T *tv)
6768{
6769 return (tv->v_lock & VAR_LOCKED)
6770 || (tv->v_type == VAR_LIST
6771 && tv->vval.v_list != NULL
6772 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
6773 || (tv->v_type == VAR_DICT
6774 && tv->vval.v_dict != NULL
6775 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
6776}
6777
6778/*
6779 * "islocked()" function
6780 */
6781 static void
6782f_islocked(typval_T *argvars, typval_T *rettv)
6783{
6784 lval_T lv;
6785 char_u *end;
6786 dictitem_T *di;
6787
6788 rettv->vval.v_number = -1;
6789 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01006790 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006791 if (end != NULL && lv.ll_name != NULL)
6792 {
6793 if (*end != NUL)
6794 EMSG(_(e_trailing));
6795 else
6796 {
6797 if (lv.ll_tv == NULL)
6798 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006799 di = find_var(lv.ll_name, NULL, TRUE);
6800 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006801 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006802 /* Consider a variable locked when:
6803 * 1. the variable itself is locked
6804 * 2. the value of the variable is locked.
6805 * 3. the List or Dict value is locked.
6806 */
6807 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
6808 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006809 }
6810 }
6811 else if (lv.ll_range)
6812 EMSG(_("E786: Range not allowed"));
6813 else if (lv.ll_newkey != NULL)
6814 EMSG2(_(e_dictkey), lv.ll_newkey);
6815 else if (lv.ll_list != NULL)
6816 /* List item. */
6817 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
6818 else
6819 /* Dictionary item. */
6820 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
6821 }
6822 }
6823
6824 clear_lval(&lv);
6825}
6826
6827#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
6828/*
6829 * "isnan()" function
6830 */
6831 static void
6832f_isnan(typval_T *argvars, typval_T *rettv)
6833{
6834 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
6835 && isnan(argvars[0].vval.v_float);
6836}
6837#endif
6838
6839/*
6840 * "items(dict)" function
6841 */
6842 static void
6843f_items(typval_T *argvars, typval_T *rettv)
6844{
6845 dict_list(argvars, rettv, 2);
6846}
6847
6848#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
6849/*
6850 * Get the job from the argument.
6851 * Returns NULL if the job is invalid.
6852 */
6853 static job_T *
6854get_job_arg(typval_T *tv)
6855{
6856 job_T *job;
6857
6858 if (tv->v_type != VAR_JOB)
6859 {
6860 EMSG2(_(e_invarg2), get_tv_string(tv));
6861 return NULL;
6862 }
6863 job = tv->vval.v_job;
6864
6865 if (job == NULL)
6866 EMSG(_("E916: not a valid job"));
6867 return job;
6868}
6869
6870/*
6871 * "job_getchannel()" function
6872 */
6873 static void
6874f_job_getchannel(typval_T *argvars, typval_T *rettv)
6875{
6876 job_T *job = get_job_arg(&argvars[0]);
6877
6878 if (job != NULL)
6879 {
6880 rettv->v_type = VAR_CHANNEL;
6881 rettv->vval.v_channel = job->jv_channel;
6882 if (job->jv_channel != NULL)
6883 ++job->jv_channel->ch_refcount;
6884 }
6885}
6886
6887/*
6888 * "job_info()" function
6889 */
6890 static void
6891f_job_info(typval_T *argvars, typval_T *rettv)
6892{
6893 job_T *job = get_job_arg(&argvars[0]);
6894
6895 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
6896 job_info(job, rettv->vval.v_dict);
6897}
6898
6899/*
6900 * "job_setoptions()" function
6901 */
6902 static void
6903f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
6904{
6905 job_T *job = get_job_arg(&argvars[0]);
6906 jobopt_T opt;
6907
6908 if (job == NULL)
6909 return;
6910 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02006911 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006912 job_set_options(job, &opt);
6913 free_job_options(&opt);
6914}
6915
6916/*
6917 * "job_start()" function
6918 */
6919 static void
6920f_job_start(typval_T *argvars, typval_T *rettv)
6921{
6922 rettv->v_type = VAR_JOB;
6923 if (check_restricted() || check_secure())
6924 return;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +02006925 rettv->vval.v_job = job_start(argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006926}
6927
6928/*
6929 * "job_status()" function
6930 */
6931 static void
6932f_job_status(typval_T *argvars, typval_T *rettv)
6933{
6934 job_T *job = get_job_arg(&argvars[0]);
6935
6936 if (job != NULL)
6937 {
6938 rettv->v_type = VAR_STRING;
6939 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
6940 }
6941}
6942
6943/*
6944 * "job_stop()" function
6945 */
6946 static void
6947f_job_stop(typval_T *argvars, typval_T *rettv)
6948{
6949 job_T *job = get_job_arg(&argvars[0]);
6950
6951 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02006952 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006953}
6954#endif
6955
6956/*
6957 * "join()" function
6958 */
6959 static void
6960f_join(typval_T *argvars, typval_T *rettv)
6961{
6962 garray_T ga;
6963 char_u *sep;
6964
6965 if (argvars[0].v_type != VAR_LIST)
6966 {
6967 EMSG(_(e_listreq));
6968 return;
6969 }
6970 if (argvars[0].vval.v_list == NULL)
6971 return;
6972 if (argvars[1].v_type == VAR_UNKNOWN)
6973 sep = (char_u *)" ";
6974 else
6975 sep = get_tv_string_chk(&argvars[1]);
6976
6977 rettv->v_type = VAR_STRING;
6978
6979 if (sep != NULL)
6980 {
6981 ga_init2(&ga, (int)sizeof(char), 80);
6982 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
6983 ga_append(&ga, NUL);
6984 rettv->vval.v_string = (char_u *)ga.ga_data;
6985 }
6986 else
6987 rettv->vval.v_string = NULL;
6988}
6989
6990/*
6991 * "js_decode()" function
6992 */
6993 static void
6994f_js_decode(typval_T *argvars, typval_T *rettv)
6995{
6996 js_read_T reader;
6997
6998 reader.js_buf = get_tv_string(&argvars[0]);
6999 reader.js_fill = NULL;
7000 reader.js_used = 0;
7001 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
7002 EMSG(_(e_invarg));
7003}
7004
7005/*
7006 * "js_encode()" function
7007 */
7008 static void
7009f_js_encode(typval_T *argvars, typval_T *rettv)
7010{
7011 rettv->v_type = VAR_STRING;
7012 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7013}
7014
7015/*
7016 * "json_decode()" function
7017 */
7018 static void
7019f_json_decode(typval_T *argvars, typval_T *rettv)
7020{
7021 js_read_T reader;
7022
7023 reader.js_buf = get_tv_string(&argvars[0]);
7024 reader.js_fill = NULL;
7025 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007026 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007027}
7028
7029/*
7030 * "json_encode()" function
7031 */
7032 static void
7033f_json_encode(typval_T *argvars, typval_T *rettv)
7034{
7035 rettv->v_type = VAR_STRING;
7036 rettv->vval.v_string = json_encode(&argvars[0], 0);
7037}
7038
7039/*
7040 * "keys()" function
7041 */
7042 static void
7043f_keys(typval_T *argvars, typval_T *rettv)
7044{
7045 dict_list(argvars, rettv, 0);
7046}
7047
7048/*
7049 * "last_buffer_nr()" function.
7050 */
7051 static void
7052f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7053{
7054 int n = 0;
7055 buf_T *buf;
7056
Bram Moolenaar29323592016-07-24 22:04:11 +02007057 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007058 if (n < buf->b_fnum)
7059 n = buf->b_fnum;
7060
7061 rettv->vval.v_number = n;
7062}
7063
7064/*
7065 * "len()" function
7066 */
7067 static void
7068f_len(typval_T *argvars, typval_T *rettv)
7069{
7070 switch (argvars[0].v_type)
7071 {
7072 case VAR_STRING:
7073 case VAR_NUMBER:
7074 rettv->vval.v_number = (varnumber_T)STRLEN(
7075 get_tv_string(&argvars[0]));
7076 break;
7077 case VAR_LIST:
7078 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7079 break;
7080 case VAR_DICT:
7081 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7082 break;
7083 case VAR_UNKNOWN:
7084 case VAR_SPECIAL:
7085 case VAR_FLOAT:
7086 case VAR_FUNC:
7087 case VAR_PARTIAL:
7088 case VAR_JOB:
7089 case VAR_CHANNEL:
7090 EMSG(_("E701: Invalid type for len()"));
7091 break;
7092 }
7093}
7094
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007095 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007096libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007097{
7098#ifdef FEAT_LIBCALL
7099 char_u *string_in;
7100 char_u **string_result;
7101 int nr_result;
7102#endif
7103
7104 rettv->v_type = type;
7105 if (type != VAR_NUMBER)
7106 rettv->vval.v_string = NULL;
7107
7108 if (check_restricted() || check_secure())
7109 return;
7110
7111#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007112 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007113 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7114 {
7115 string_in = NULL;
7116 if (argvars[2].v_type == VAR_STRING)
7117 string_in = argvars[2].vval.v_string;
7118 if (type == VAR_NUMBER)
7119 string_result = NULL;
7120 else
7121 string_result = &rettv->vval.v_string;
7122 if (mch_libcall(argvars[0].vval.v_string,
7123 argvars[1].vval.v_string,
7124 string_in,
7125 argvars[2].vval.v_number,
7126 string_result,
7127 &nr_result) == OK
7128 && type == VAR_NUMBER)
7129 rettv->vval.v_number = nr_result;
7130 }
7131#endif
7132}
7133
7134/*
7135 * "libcall()" function
7136 */
7137 static void
7138f_libcall(typval_T *argvars, typval_T *rettv)
7139{
7140 libcall_common(argvars, rettv, VAR_STRING);
7141}
7142
7143/*
7144 * "libcallnr()" function
7145 */
7146 static void
7147f_libcallnr(typval_T *argvars, typval_T *rettv)
7148{
7149 libcall_common(argvars, rettv, VAR_NUMBER);
7150}
7151
7152/*
7153 * "line(string)" function
7154 */
7155 static void
7156f_line(typval_T *argvars, typval_T *rettv)
7157{
7158 linenr_T lnum = 0;
7159 pos_T *fp;
7160 int fnum;
7161
7162 fp = var2fpos(&argvars[0], TRUE, &fnum);
7163 if (fp != NULL)
7164 lnum = fp->lnum;
7165 rettv->vval.v_number = lnum;
7166}
7167
7168/*
7169 * "line2byte(lnum)" function
7170 */
7171 static void
7172f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7173{
7174#ifndef FEAT_BYTEOFF
7175 rettv->vval.v_number = -1;
7176#else
7177 linenr_T lnum;
7178
7179 lnum = get_tv_lnum(argvars);
7180 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7181 rettv->vval.v_number = -1;
7182 else
7183 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7184 if (rettv->vval.v_number >= 0)
7185 ++rettv->vval.v_number;
7186#endif
7187}
7188
7189/*
7190 * "lispindent(lnum)" function
7191 */
7192 static void
7193f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7194{
7195#ifdef FEAT_LISP
7196 pos_T pos;
7197 linenr_T lnum;
7198
7199 pos = curwin->w_cursor;
7200 lnum = get_tv_lnum(argvars);
7201 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7202 {
7203 curwin->w_cursor.lnum = lnum;
7204 rettv->vval.v_number = get_lisp_indent();
7205 curwin->w_cursor = pos;
7206 }
7207 else
7208#endif
7209 rettv->vval.v_number = -1;
7210}
7211
7212/*
7213 * "localtime()" function
7214 */
7215 static void
7216f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7217{
7218 rettv->vval.v_number = (varnumber_T)time(NULL);
7219}
7220
7221static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
7222
7223 static void
7224get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7225{
7226 char_u *keys;
7227 char_u *which;
7228 char_u buf[NUMBUFLEN];
7229 char_u *keys_buf = NULL;
7230 char_u *rhs;
7231 int mode;
7232 int abbr = FALSE;
7233 int get_dict = FALSE;
7234 mapblock_T *mp;
7235 int buffer_local;
7236
7237 /* return empty string for failure */
7238 rettv->v_type = VAR_STRING;
7239 rettv->vval.v_string = NULL;
7240
7241 keys = get_tv_string(&argvars[0]);
7242 if (*keys == NUL)
7243 return;
7244
7245 if (argvars[1].v_type != VAR_UNKNOWN)
7246 {
7247 which = get_tv_string_buf_chk(&argvars[1], buf);
7248 if (argvars[2].v_type != VAR_UNKNOWN)
7249 {
7250 abbr = (int)get_tv_number(&argvars[2]);
7251 if (argvars[3].v_type != VAR_UNKNOWN)
7252 get_dict = (int)get_tv_number(&argvars[3]);
7253 }
7254 }
7255 else
7256 which = (char_u *)"";
7257 if (which == NULL)
7258 return;
7259
7260 mode = get_map_mode(&which, 0);
7261
7262 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7263 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7264 vim_free(keys_buf);
7265
7266 if (!get_dict)
7267 {
7268 /* Return a string. */
7269 if (rhs != NULL)
7270 rettv->vval.v_string = str2special_save(rhs, FALSE);
7271
7272 }
7273 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7274 {
7275 /* Return a dictionary. */
7276 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7277 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7278 dict_T *dict = rettv->vval.v_dict;
7279
7280 dict_add_nr_str(dict, "lhs", 0L, lhs);
7281 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
7282 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
7283 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
7284 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
7285 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
7286 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
7287 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
7288 dict_add_nr_str(dict, "mode", 0L, mapmode);
7289
7290 vim_free(lhs);
7291 vim_free(mapmode);
7292 }
7293}
7294
7295#ifdef FEAT_FLOAT
7296/*
7297 * "log()" function
7298 */
7299 static void
7300f_log(typval_T *argvars, typval_T *rettv)
7301{
7302 float_T f = 0.0;
7303
7304 rettv->v_type = VAR_FLOAT;
7305 if (get_float_arg(argvars, &f) == OK)
7306 rettv->vval.v_float = log(f);
7307 else
7308 rettv->vval.v_float = 0.0;
7309}
7310
7311/*
7312 * "log10()" function
7313 */
7314 static void
7315f_log10(typval_T *argvars, typval_T *rettv)
7316{
7317 float_T f = 0.0;
7318
7319 rettv->v_type = VAR_FLOAT;
7320 if (get_float_arg(argvars, &f) == OK)
7321 rettv->vval.v_float = log10(f);
7322 else
7323 rettv->vval.v_float = 0.0;
7324}
7325#endif
7326
7327#ifdef FEAT_LUA
7328/*
7329 * "luaeval()" function
7330 */
7331 static void
7332f_luaeval(typval_T *argvars, typval_T *rettv)
7333{
7334 char_u *str;
7335 char_u buf[NUMBUFLEN];
7336
7337 str = get_tv_string_buf(&argvars[0], buf);
7338 do_luaeval(str, argvars + 1, rettv);
7339}
7340#endif
7341
7342/*
7343 * "map()" function
7344 */
7345 static void
7346f_map(typval_T *argvars, typval_T *rettv)
7347{
7348 filter_map(argvars, rettv, TRUE);
7349}
7350
7351/*
7352 * "maparg()" function
7353 */
7354 static void
7355f_maparg(typval_T *argvars, typval_T *rettv)
7356{
7357 get_maparg(argvars, rettv, TRUE);
7358}
7359
7360/*
7361 * "mapcheck()" function
7362 */
7363 static void
7364f_mapcheck(typval_T *argvars, typval_T *rettv)
7365{
7366 get_maparg(argvars, rettv, FALSE);
7367}
7368
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007369typedef enum
7370{
7371 MATCH_END, /* matchend() */
7372 MATCH_MATCH, /* match() */
7373 MATCH_STR, /* matchstr() */
7374 MATCH_LIST, /* matchlist() */
7375 MATCH_POS /* matchstrpos() */
7376} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007377
7378 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007379find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007380{
7381 char_u *str = NULL;
7382 long len = 0;
7383 char_u *expr = NULL;
7384 char_u *pat;
7385 regmatch_T regmatch;
7386 char_u patbuf[NUMBUFLEN];
7387 char_u strbuf[NUMBUFLEN];
7388 char_u *save_cpo;
7389 long start = 0;
7390 long nth = 1;
7391 colnr_T startcol = 0;
7392 int match = 0;
7393 list_T *l = NULL;
7394 listitem_T *li = NULL;
7395 long idx = 0;
7396 char_u *tofree = NULL;
7397
7398 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7399 save_cpo = p_cpo;
7400 p_cpo = (char_u *)"";
7401
7402 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007403 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007404 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007405 /* type MATCH_LIST: return empty list when there are no matches.
7406 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007407 if (rettv_list_alloc(rettv) == FAIL)
7408 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007409 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007410 && (list_append_string(rettv->vval.v_list,
7411 (char_u *)"", 0) == FAIL
7412 || list_append_number(rettv->vval.v_list,
7413 (varnumber_T)-1) == FAIL
7414 || list_append_number(rettv->vval.v_list,
7415 (varnumber_T)-1) == FAIL
7416 || list_append_number(rettv->vval.v_list,
7417 (varnumber_T)-1) == FAIL))
7418 {
7419 list_free(rettv->vval.v_list);
7420 rettv->vval.v_list = NULL;
7421 goto theend;
7422 }
7423 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007424 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007425 {
7426 rettv->v_type = VAR_STRING;
7427 rettv->vval.v_string = NULL;
7428 }
7429
7430 if (argvars[0].v_type == VAR_LIST)
7431 {
7432 if ((l = argvars[0].vval.v_list) == NULL)
7433 goto theend;
7434 li = l->lv_first;
7435 }
7436 else
7437 {
7438 expr = str = get_tv_string(&argvars[0]);
7439 len = (long)STRLEN(str);
7440 }
7441
7442 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
7443 if (pat == NULL)
7444 goto theend;
7445
7446 if (argvars[2].v_type != VAR_UNKNOWN)
7447 {
7448 int error = FALSE;
7449
7450 start = (long)get_tv_number_chk(&argvars[2], &error);
7451 if (error)
7452 goto theend;
7453 if (l != NULL)
7454 {
7455 li = list_find(l, start);
7456 if (li == NULL)
7457 goto theend;
7458 idx = l->lv_idx; /* use the cached index */
7459 }
7460 else
7461 {
7462 if (start < 0)
7463 start = 0;
7464 if (start > len)
7465 goto theend;
7466 /* When "count" argument is there ignore matches before "start",
7467 * otherwise skip part of the string. Differs when pattern is "^"
7468 * or "\<". */
7469 if (argvars[3].v_type != VAR_UNKNOWN)
7470 startcol = start;
7471 else
7472 {
7473 str += start;
7474 len -= start;
7475 }
7476 }
7477
7478 if (argvars[3].v_type != VAR_UNKNOWN)
7479 nth = (long)get_tv_number_chk(&argvars[3], &error);
7480 if (error)
7481 goto theend;
7482 }
7483
7484 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7485 if (regmatch.regprog != NULL)
7486 {
7487 regmatch.rm_ic = p_ic;
7488
7489 for (;;)
7490 {
7491 if (l != NULL)
7492 {
7493 if (li == NULL)
7494 {
7495 match = FALSE;
7496 break;
7497 }
7498 vim_free(tofree);
7499 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7500 if (str == NULL)
7501 break;
7502 }
7503
7504 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7505
7506 if (match && --nth <= 0)
7507 break;
7508 if (l == NULL && !match)
7509 break;
7510
7511 /* Advance to just after the match. */
7512 if (l != NULL)
7513 {
7514 li = li->li_next;
7515 ++idx;
7516 }
7517 else
7518 {
7519#ifdef FEAT_MBYTE
7520 startcol = (colnr_T)(regmatch.startp[0]
7521 + (*mb_ptr2len)(regmatch.startp[0]) - str);
7522#else
7523 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
7524#endif
7525 if (startcol > (colnr_T)len
7526 || str + startcol <= regmatch.startp[0])
7527 {
7528 match = FALSE;
7529 break;
7530 }
7531 }
7532 }
7533
7534 if (match)
7535 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007536 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007537 {
7538 listitem_T *li1 = rettv->vval.v_list->lv_first;
7539 listitem_T *li2 = li1->li_next;
7540 listitem_T *li3 = li2->li_next;
7541 listitem_T *li4 = li3->li_next;
7542
7543 vim_free(li1->li_tv.vval.v_string);
7544 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7545 (int)(regmatch.endp[0] - regmatch.startp[0]));
7546 li3->li_tv.vval.v_number =
7547 (varnumber_T)(regmatch.startp[0] - expr);
7548 li4->li_tv.vval.v_number =
7549 (varnumber_T)(regmatch.endp[0] - expr);
7550 if (l != NULL)
7551 li2->li_tv.vval.v_number = (varnumber_T)idx;
7552 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007553 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007554 {
7555 int i;
7556
7557 /* return list with matched string and submatches */
7558 for (i = 0; i < NSUBEXP; ++i)
7559 {
7560 if (regmatch.endp[i] == NULL)
7561 {
7562 if (list_append_string(rettv->vval.v_list,
7563 (char_u *)"", 0) == FAIL)
7564 break;
7565 }
7566 else if (list_append_string(rettv->vval.v_list,
7567 regmatch.startp[i],
7568 (int)(regmatch.endp[i] - regmatch.startp[i]))
7569 == FAIL)
7570 break;
7571 }
7572 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007573 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007574 {
7575 /* return matched string */
7576 if (l != NULL)
7577 copy_tv(&li->li_tv, rettv);
7578 else
7579 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
7580 (int)(regmatch.endp[0] - regmatch.startp[0]));
7581 }
7582 else if (l != NULL)
7583 rettv->vval.v_number = idx;
7584 else
7585 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007586 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007587 rettv->vval.v_number =
7588 (varnumber_T)(regmatch.startp[0] - str);
7589 else
7590 rettv->vval.v_number =
7591 (varnumber_T)(regmatch.endp[0] - str);
7592 rettv->vval.v_number += (varnumber_T)(str - expr);
7593 }
7594 }
7595 vim_regfree(regmatch.regprog);
7596 }
7597
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007598theend:
7599 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007600 /* matchstrpos() without a list: drop the second item. */
7601 listitem_remove(rettv->vval.v_list,
7602 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007603 vim_free(tofree);
7604 p_cpo = save_cpo;
7605}
7606
7607/*
7608 * "match()" function
7609 */
7610 static void
7611f_match(typval_T *argvars, typval_T *rettv)
7612{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007613 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007614}
7615
7616/*
7617 * "matchadd()" function
7618 */
7619 static void
7620f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7621{
7622#ifdef FEAT_SEARCH_EXTRA
7623 char_u buf[NUMBUFLEN];
7624 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
7625 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
7626 int prio = 10; /* default priority */
7627 int id = -1;
7628 int error = FALSE;
7629 char_u *conceal_char = NULL;
7630
7631 rettv->vval.v_number = -1;
7632
7633 if (grp == NULL || pat == NULL)
7634 return;
7635 if (argvars[2].v_type != VAR_UNKNOWN)
7636 {
7637 prio = (int)get_tv_number_chk(&argvars[2], &error);
7638 if (argvars[3].v_type != VAR_UNKNOWN)
7639 {
7640 id = (int)get_tv_number_chk(&argvars[3], &error);
7641 if (argvars[4].v_type != VAR_UNKNOWN)
7642 {
7643 if (argvars[4].v_type != VAR_DICT)
7644 {
7645 EMSG(_(e_dictreq));
7646 return;
7647 }
7648 if (dict_find(argvars[4].vval.v_dict,
7649 (char_u *)"conceal", -1) != NULL)
7650 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7651 (char_u *)"conceal", FALSE);
7652 }
7653 }
7654 }
7655 if (error == TRUE)
7656 return;
7657 if (id >= 1 && id <= 3)
7658 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007659 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007660 return;
7661 }
7662
7663 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
7664 conceal_char);
7665#endif
7666}
7667
7668/*
7669 * "matchaddpos()" function
7670 */
7671 static void
7672f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7673{
7674#ifdef FEAT_SEARCH_EXTRA
7675 char_u buf[NUMBUFLEN];
7676 char_u *group;
7677 int prio = 10;
7678 int id = -1;
7679 int error = FALSE;
7680 list_T *l;
7681 char_u *conceal_char = NULL;
7682
7683 rettv->vval.v_number = -1;
7684
7685 group = get_tv_string_buf_chk(&argvars[0], buf);
7686 if (group == NULL)
7687 return;
7688
7689 if (argvars[1].v_type != VAR_LIST)
7690 {
7691 EMSG2(_(e_listarg), "matchaddpos()");
7692 return;
7693 }
7694 l = argvars[1].vval.v_list;
7695 if (l == NULL)
7696 return;
7697
7698 if (argvars[2].v_type != VAR_UNKNOWN)
7699 {
7700 prio = (int)get_tv_number_chk(&argvars[2], &error);
7701 if (argvars[3].v_type != VAR_UNKNOWN)
7702 {
7703 id = (int)get_tv_number_chk(&argvars[3], &error);
7704 if (argvars[4].v_type != VAR_UNKNOWN)
7705 {
7706 if (argvars[4].v_type != VAR_DICT)
7707 {
7708 EMSG(_(e_dictreq));
7709 return;
7710 }
7711 if (dict_find(argvars[4].vval.v_dict,
7712 (char_u *)"conceal", -1) != NULL)
7713 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7714 (char_u *)"conceal", FALSE);
7715 }
7716 }
7717 }
7718 if (error == TRUE)
7719 return;
7720
7721 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
7722 if (id == 1 || id == 2)
7723 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007724 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007725 return;
7726 }
7727
7728 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
7729 conceal_char);
7730#endif
7731}
7732
7733/*
7734 * "matcharg()" function
7735 */
7736 static void
7737f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
7738{
7739 if (rettv_list_alloc(rettv) == OK)
7740 {
7741#ifdef FEAT_SEARCH_EXTRA
7742 int id = (int)get_tv_number(&argvars[0]);
7743 matchitem_T *m;
7744
7745 if (id >= 1 && id <= 3)
7746 {
7747 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
7748 {
7749 list_append_string(rettv->vval.v_list,
7750 syn_id2name(m->hlg_id), -1);
7751 list_append_string(rettv->vval.v_list, m->pattern, -1);
7752 }
7753 else
7754 {
7755 list_append_string(rettv->vval.v_list, NULL, -1);
7756 list_append_string(rettv->vval.v_list, NULL, -1);
7757 }
7758 }
7759#endif
7760 }
7761}
7762
7763/*
7764 * "matchdelete()" function
7765 */
7766 static void
7767f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7768{
7769#ifdef FEAT_SEARCH_EXTRA
7770 rettv->vval.v_number = match_delete(curwin,
7771 (int)get_tv_number(&argvars[0]), TRUE);
7772#endif
7773}
7774
7775/*
7776 * "matchend()" function
7777 */
7778 static void
7779f_matchend(typval_T *argvars, typval_T *rettv)
7780{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007781 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007782}
7783
7784/*
7785 * "matchlist()" function
7786 */
7787 static void
7788f_matchlist(typval_T *argvars, typval_T *rettv)
7789{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007790 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007791}
7792
7793/*
7794 * "matchstr()" function
7795 */
7796 static void
7797f_matchstr(typval_T *argvars, typval_T *rettv)
7798{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007799 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007800}
7801
7802/*
7803 * "matchstrpos()" function
7804 */
7805 static void
7806f_matchstrpos(typval_T *argvars, typval_T *rettv)
7807{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007808 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007809}
7810
7811static void max_min(typval_T *argvars, typval_T *rettv, int domax);
7812
7813 static void
7814max_min(typval_T *argvars, typval_T *rettv, int domax)
7815{
7816 varnumber_T n = 0;
7817 varnumber_T i;
7818 int error = FALSE;
7819
7820 if (argvars[0].v_type == VAR_LIST)
7821 {
7822 list_T *l;
7823 listitem_T *li;
7824
7825 l = argvars[0].vval.v_list;
7826 if (l != NULL)
7827 {
7828 li = l->lv_first;
7829 if (li != NULL)
7830 {
7831 n = get_tv_number_chk(&li->li_tv, &error);
7832 for (;;)
7833 {
7834 li = li->li_next;
7835 if (li == NULL)
7836 break;
7837 i = get_tv_number_chk(&li->li_tv, &error);
7838 if (domax ? i > n : i < n)
7839 n = i;
7840 }
7841 }
7842 }
7843 }
7844 else if (argvars[0].v_type == VAR_DICT)
7845 {
7846 dict_T *d;
7847 int first = TRUE;
7848 hashitem_T *hi;
7849 int todo;
7850
7851 d = argvars[0].vval.v_dict;
7852 if (d != NULL)
7853 {
7854 todo = (int)d->dv_hashtab.ht_used;
7855 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
7856 {
7857 if (!HASHITEM_EMPTY(hi))
7858 {
7859 --todo;
7860 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
7861 if (first)
7862 {
7863 n = i;
7864 first = FALSE;
7865 }
7866 else if (domax ? i > n : i < n)
7867 n = i;
7868 }
7869 }
7870 }
7871 }
7872 else
Bram Moolenaar26b84332016-09-04 21:42:36 +02007873 EMSG2(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007874 rettv->vval.v_number = error ? 0 : n;
7875}
7876
7877/*
7878 * "max()" function
7879 */
7880 static void
7881f_max(typval_T *argvars, typval_T *rettv)
7882{
7883 max_min(argvars, rettv, TRUE);
7884}
7885
7886/*
7887 * "min()" function
7888 */
7889 static void
7890f_min(typval_T *argvars, typval_T *rettv)
7891{
7892 max_min(argvars, rettv, FALSE);
7893}
7894
7895static int mkdir_recurse(char_u *dir, int prot);
7896
7897/*
7898 * Create the directory in which "dir" is located, and higher levels when
7899 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02007900 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007901 */
7902 static int
7903mkdir_recurse(char_u *dir, int prot)
7904{
7905 char_u *p;
7906 char_u *updir;
7907 int r = FAIL;
7908
7909 /* Get end of directory name in "dir".
7910 * We're done when it's "/" or "c:/". */
7911 p = gettail_sep(dir);
7912 if (p <= get_past_head(dir))
7913 return OK;
7914
7915 /* If the directory exists we're done. Otherwise: create it.*/
7916 updir = vim_strnsave(dir, (int)(p - dir));
7917 if (updir == NULL)
7918 return FAIL;
7919 if (mch_isdir(updir))
7920 r = OK;
7921 else if (mkdir_recurse(updir, prot) == OK)
7922 r = vim_mkdir_emsg(updir, prot);
7923 vim_free(updir);
7924 return r;
7925}
7926
7927#ifdef vim_mkdir
7928/*
7929 * "mkdir()" function
7930 */
7931 static void
7932f_mkdir(typval_T *argvars, typval_T *rettv)
7933{
7934 char_u *dir;
7935 char_u buf[NUMBUFLEN];
7936 int prot = 0755;
7937
7938 rettv->vval.v_number = FAIL;
7939 if (check_restricted() || check_secure())
7940 return;
7941
7942 dir = get_tv_string_buf(&argvars[0], buf);
7943 if (*dir == NUL)
7944 rettv->vval.v_number = FAIL;
7945 else
7946 {
7947 if (*gettail(dir) == NUL)
7948 /* remove trailing slashes */
7949 *gettail_sep(dir) = NUL;
7950
7951 if (argvars[1].v_type != VAR_UNKNOWN)
7952 {
7953 if (argvars[2].v_type != VAR_UNKNOWN)
7954 prot = (int)get_tv_number_chk(&argvars[2], NULL);
7955 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
7956 mkdir_recurse(dir, prot);
7957 }
7958 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
7959 }
7960}
7961#endif
7962
7963/*
7964 * "mode()" function
7965 */
7966 static void
7967f_mode(typval_T *argvars, typval_T *rettv)
7968{
7969 char_u buf[3];
7970
7971 buf[1] = NUL;
7972 buf[2] = NUL;
7973
7974 if (time_for_testing == 93784)
7975 {
7976 /* Testing the two-character code. */
7977 buf[0] = 'x';
7978 buf[1] = '!';
7979 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02007980#ifdef FEAT_TERMINAL
7981 else if (term_use_loop())
7982 buf[0] = 't';
7983#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007984 else if (VIsual_active)
7985 {
7986 if (VIsual_select)
7987 buf[0] = VIsual_mode + 's' - 'v';
7988 else
7989 buf[0] = VIsual_mode;
7990 }
7991 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
7992 || State == CONFIRM)
7993 {
7994 buf[0] = 'r';
7995 if (State == ASKMORE)
7996 buf[1] = 'm';
7997 else if (State == CONFIRM)
7998 buf[1] = '?';
7999 }
8000 else if (State == EXTERNCMD)
8001 buf[0] = '!';
8002 else if (State & INSERT)
8003 {
8004#ifdef FEAT_VREPLACE
8005 if (State & VREPLACE_FLAG)
8006 {
8007 buf[0] = 'R';
8008 buf[1] = 'v';
8009 }
8010 else
8011#endif
Bram Moolenaare90858d2017-02-01 17:24:34 +01008012 {
8013 if (State & REPLACE_FLAG)
8014 buf[0] = 'R';
8015 else
8016 buf[0] = 'i';
8017#ifdef FEAT_INS_EXPAND
8018 if (ins_compl_active())
8019 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008020 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008021 buf[1] = 'x';
8022#endif
8023 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008024 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008025 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008026 {
8027 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008028 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008029 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008030 else if (exmode_active == EXMODE_NORMAL)
8031 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008032 }
8033 else
8034 {
8035 buf[0] = 'n';
8036 if (finish_op)
8037 buf[1] = 'o';
8038 }
8039
8040 /* Clear out the minor mode when the argument is not a non-zero number or
8041 * non-empty string. */
8042 if (!non_zero_arg(&argvars[0]))
8043 buf[1] = NUL;
8044
8045 rettv->vval.v_string = vim_strsave(buf);
8046 rettv->v_type = VAR_STRING;
8047}
8048
8049#if defined(FEAT_MZSCHEME) || defined(PROTO)
8050/*
8051 * "mzeval()" function
8052 */
8053 static void
8054f_mzeval(typval_T *argvars, typval_T *rettv)
8055{
8056 char_u *str;
8057 char_u buf[NUMBUFLEN];
8058
8059 str = get_tv_string_buf(&argvars[0], buf);
8060 do_mzeval(str, rettv);
8061}
8062
8063 void
8064mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8065{
8066 typval_T argvars[3];
8067
8068 argvars[0].v_type = VAR_STRING;
8069 argvars[0].vval.v_string = name;
8070 copy_tv(args, &argvars[1]);
8071 argvars[2].v_type = VAR_UNKNOWN;
8072 f_call(argvars, rettv);
8073 clear_tv(&argvars[1]);
8074}
8075#endif
8076
8077/*
8078 * "nextnonblank()" function
8079 */
8080 static void
8081f_nextnonblank(typval_T *argvars, typval_T *rettv)
8082{
8083 linenr_T lnum;
8084
8085 for (lnum = get_tv_lnum(argvars); ; ++lnum)
8086 {
8087 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8088 {
8089 lnum = 0;
8090 break;
8091 }
8092 if (*skipwhite(ml_get(lnum)) != NUL)
8093 break;
8094 }
8095 rettv->vval.v_number = lnum;
8096}
8097
8098/*
8099 * "nr2char()" function
8100 */
8101 static void
8102f_nr2char(typval_T *argvars, typval_T *rettv)
8103{
8104 char_u buf[NUMBUFLEN];
8105
8106#ifdef FEAT_MBYTE
8107 if (has_mbyte)
8108 {
8109 int utf8 = 0;
8110
8111 if (argvars[1].v_type != VAR_UNKNOWN)
8112 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
8113 if (utf8)
8114 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8115 else
8116 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8117 }
8118 else
8119#endif
8120 {
8121 buf[0] = (char_u)get_tv_number(&argvars[0]);
8122 buf[1] = NUL;
8123 }
8124 rettv->v_type = VAR_STRING;
8125 rettv->vval.v_string = vim_strsave(buf);
8126}
8127
8128/*
8129 * "or(expr, expr)" function
8130 */
8131 static void
8132f_or(typval_T *argvars, typval_T *rettv)
8133{
8134 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8135 | get_tv_number_chk(&argvars[1], NULL);
8136}
8137
8138/*
8139 * "pathshorten()" function
8140 */
8141 static void
8142f_pathshorten(typval_T *argvars, typval_T *rettv)
8143{
8144 char_u *p;
8145
8146 rettv->v_type = VAR_STRING;
8147 p = get_tv_string_chk(&argvars[0]);
8148 if (p == NULL)
8149 rettv->vval.v_string = NULL;
8150 else
8151 {
8152 p = vim_strsave(p);
8153 rettv->vval.v_string = p;
8154 if (p != NULL)
8155 shorten_dir(p);
8156 }
8157}
8158
8159#ifdef FEAT_PERL
8160/*
8161 * "perleval()" function
8162 */
8163 static void
8164f_perleval(typval_T *argvars, typval_T *rettv)
8165{
8166 char_u *str;
8167 char_u buf[NUMBUFLEN];
8168
8169 str = get_tv_string_buf(&argvars[0], buf);
8170 do_perleval(str, rettv);
8171}
8172#endif
8173
8174#ifdef FEAT_FLOAT
8175/*
8176 * "pow()" function
8177 */
8178 static void
8179f_pow(typval_T *argvars, typval_T *rettv)
8180{
8181 float_T fx = 0.0, fy = 0.0;
8182
8183 rettv->v_type = VAR_FLOAT;
8184 if (get_float_arg(argvars, &fx) == OK
8185 && get_float_arg(&argvars[1], &fy) == OK)
8186 rettv->vval.v_float = pow(fx, fy);
8187 else
8188 rettv->vval.v_float = 0.0;
8189}
8190#endif
8191
8192/*
8193 * "prevnonblank()" function
8194 */
8195 static void
8196f_prevnonblank(typval_T *argvars, typval_T *rettv)
8197{
8198 linenr_T lnum;
8199
8200 lnum = get_tv_lnum(argvars);
8201 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8202 lnum = 0;
8203 else
8204 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8205 --lnum;
8206 rettv->vval.v_number = lnum;
8207}
8208
8209/* This dummy va_list is here because:
8210 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8211 * - locally in the function results in a "used before set" warning
8212 * - using va_start() to initialize it gives "function with fixed args" error */
8213static va_list ap;
8214
8215/*
8216 * "printf()" function
8217 */
8218 static void
8219f_printf(typval_T *argvars, typval_T *rettv)
8220{
8221 char_u buf[NUMBUFLEN];
8222 int len;
8223 char_u *s;
8224 int saved_did_emsg = did_emsg;
8225 char *fmt;
8226
8227 rettv->v_type = VAR_STRING;
8228 rettv->vval.v_string = NULL;
8229
8230 /* Get the required length, allocate the buffer and do it for real. */
8231 did_emsg = FALSE;
8232 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008233 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008234 if (!did_emsg)
8235 {
8236 s = alloc(len + 1);
8237 if (s != NULL)
8238 {
8239 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008240 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8241 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008242 }
8243 }
8244 did_emsg |= saved_did_emsg;
8245}
8246
8247/*
8248 * "pumvisible()" function
8249 */
8250 static void
8251f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8252{
8253#ifdef FEAT_INS_EXPAND
8254 if (pum_visible())
8255 rettv->vval.v_number = 1;
8256#endif
8257}
8258
8259#ifdef FEAT_PYTHON3
8260/*
8261 * "py3eval()" function
8262 */
8263 static void
8264f_py3eval(typval_T *argvars, typval_T *rettv)
8265{
8266 char_u *str;
8267 char_u buf[NUMBUFLEN];
8268
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008269 if (p_pyx == 0)
8270 p_pyx = 3;
8271
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008272 str = get_tv_string_buf(&argvars[0], buf);
8273 do_py3eval(str, rettv);
8274}
8275#endif
8276
8277#ifdef FEAT_PYTHON
8278/*
8279 * "pyeval()" function
8280 */
8281 static void
8282f_pyeval(typval_T *argvars, typval_T *rettv)
8283{
8284 char_u *str;
8285 char_u buf[NUMBUFLEN];
8286
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008287 if (p_pyx == 0)
8288 p_pyx = 2;
8289
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008290 str = get_tv_string_buf(&argvars[0], buf);
8291 do_pyeval(str, rettv);
8292}
8293#endif
8294
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008295#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8296/*
8297 * "pyxeval()" function
8298 */
8299 static void
8300f_pyxeval(typval_T *argvars, typval_T *rettv)
8301{
8302# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8303 init_pyxversion();
8304 if (p_pyx == 2)
8305 f_pyeval(argvars, rettv);
8306 else
8307 f_py3eval(argvars, rettv);
8308# elif defined(FEAT_PYTHON)
8309 f_pyeval(argvars, rettv);
8310# elif defined(FEAT_PYTHON3)
8311 f_py3eval(argvars, rettv);
8312# endif
8313}
8314#endif
8315
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008316/*
8317 * "range()" function
8318 */
8319 static void
8320f_range(typval_T *argvars, typval_T *rettv)
8321{
8322 varnumber_T start;
8323 varnumber_T end;
8324 varnumber_T stride = 1;
8325 varnumber_T i;
8326 int error = FALSE;
8327
8328 start = get_tv_number_chk(&argvars[0], &error);
8329 if (argvars[1].v_type == VAR_UNKNOWN)
8330 {
8331 end = start - 1;
8332 start = 0;
8333 }
8334 else
8335 {
8336 end = get_tv_number_chk(&argvars[1], &error);
8337 if (argvars[2].v_type != VAR_UNKNOWN)
8338 stride = get_tv_number_chk(&argvars[2], &error);
8339 }
8340
8341 if (error)
8342 return; /* type error; errmsg already given */
8343 if (stride == 0)
8344 EMSG(_("E726: Stride is zero"));
8345 else if (stride > 0 ? end + 1 < start : end - 1 > start)
8346 EMSG(_("E727: Start past end"));
8347 else
8348 {
8349 if (rettv_list_alloc(rettv) == OK)
8350 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8351 if (list_append_number(rettv->vval.v_list,
8352 (varnumber_T)i) == FAIL)
8353 break;
8354 }
8355}
8356
8357/*
8358 * "readfile()" function
8359 */
8360 static void
8361f_readfile(typval_T *argvars, typval_T *rettv)
8362{
8363 int binary = FALSE;
8364 int failed = FALSE;
8365 char_u *fname;
8366 FILE *fd;
8367 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8368 int io_size = sizeof(buf);
8369 int readlen; /* size of last fread() */
8370 char_u *prev = NULL; /* previously read bytes, if any */
8371 long prevlen = 0; /* length of data in prev */
8372 long prevsize = 0; /* size of prev buffer */
8373 long maxline = MAXLNUM;
8374 long cnt = 0;
8375 char_u *p; /* position in buf */
8376 char_u *start; /* start of current line */
8377
8378 if (argvars[1].v_type != VAR_UNKNOWN)
8379 {
8380 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
8381 binary = TRUE;
8382 if (argvars[2].v_type != VAR_UNKNOWN)
8383 maxline = (long)get_tv_number(&argvars[2]);
8384 }
8385
8386 if (rettv_list_alloc(rettv) == FAIL)
8387 return;
8388
8389 /* Always open the file in binary mode, library functions have a mind of
8390 * their own about CR-LF conversion. */
8391 fname = get_tv_string(&argvars[0]);
8392 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8393 {
8394 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
8395 return;
8396 }
8397
8398 while (cnt < maxline || maxline < 0)
8399 {
8400 readlen = (int)fread(buf, 1, io_size, fd);
8401
8402 /* This for loop processes what was read, but is also entered at end
8403 * of file so that either:
8404 * - an incomplete line gets written
8405 * - a "binary" file gets an empty line at the end if it ends in a
8406 * newline. */
8407 for (p = buf, start = buf;
8408 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8409 ++p)
8410 {
8411 if (*p == '\n' || readlen <= 0)
8412 {
8413 listitem_T *li;
8414 char_u *s = NULL;
8415 long_u len = p - start;
8416
8417 /* Finished a line. Remove CRs before NL. */
8418 if (readlen > 0 && !binary)
8419 {
8420 while (len > 0 && start[len - 1] == '\r')
8421 --len;
8422 /* removal may cross back to the "prev" string */
8423 if (len == 0)
8424 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8425 --prevlen;
8426 }
8427 if (prevlen == 0)
8428 s = vim_strnsave(start, (int)len);
8429 else
8430 {
8431 /* Change "prev" buffer to be the right size. This way
8432 * the bytes are only copied once, and very long lines are
8433 * allocated only once. */
8434 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8435 {
8436 mch_memmove(s + prevlen, start, len);
8437 s[prevlen + len] = NUL;
8438 prev = NULL; /* the list will own the string */
8439 prevlen = prevsize = 0;
8440 }
8441 }
8442 if (s == NULL)
8443 {
8444 do_outofmem_msg((long_u) prevlen + len + 1);
8445 failed = TRUE;
8446 break;
8447 }
8448
8449 if ((li = listitem_alloc()) == NULL)
8450 {
8451 vim_free(s);
8452 failed = TRUE;
8453 break;
8454 }
8455 li->li_tv.v_type = VAR_STRING;
8456 li->li_tv.v_lock = 0;
8457 li->li_tv.vval.v_string = s;
8458 list_append(rettv->vval.v_list, li);
8459
8460 start = p + 1; /* step over newline */
8461 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
8462 break;
8463 }
8464 else if (*p == NUL)
8465 *p = '\n';
8466#ifdef FEAT_MBYTE
8467 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
8468 * when finding the BF and check the previous two bytes. */
8469 else if (*p == 0xbf && enc_utf8 && !binary)
8470 {
8471 /* Find the two bytes before the 0xbf. If p is at buf, or buf
8472 * + 1, these may be in the "prev" string. */
8473 char_u back1 = p >= buf + 1 ? p[-1]
8474 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
8475 char_u back2 = p >= buf + 2 ? p[-2]
8476 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
8477 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
8478
8479 if (back2 == 0xef && back1 == 0xbb)
8480 {
8481 char_u *dest = p - 2;
8482
8483 /* Usually a BOM is at the beginning of a file, and so at
8484 * the beginning of a line; then we can just step over it.
8485 */
8486 if (start == dest)
8487 start = p + 1;
8488 else
8489 {
8490 /* have to shuffle buf to close gap */
8491 int adjust_prevlen = 0;
8492
8493 if (dest < buf)
8494 {
8495 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
8496 dest = buf;
8497 }
8498 if (readlen > p - buf + 1)
8499 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
8500 readlen -= 3 - adjust_prevlen;
8501 prevlen -= adjust_prevlen;
8502 p = dest - 1;
8503 }
8504 }
8505 }
8506#endif
8507 } /* for */
8508
8509 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
8510 break;
8511 if (start < p)
8512 {
8513 /* There's part of a line in buf, store it in "prev". */
8514 if (p - start + prevlen >= prevsize)
8515 {
8516 /* need bigger "prev" buffer */
8517 char_u *newprev;
8518
8519 /* A common use case is ordinary text files and "prev" gets a
8520 * fragment of a line, so the first allocation is made
8521 * small, to avoid repeatedly 'allocing' large and
8522 * 'reallocing' small. */
8523 if (prevsize == 0)
8524 prevsize = (long)(p - start);
8525 else
8526 {
8527 long grow50pc = (prevsize * 3) / 2;
8528 long growmin = (long)((p - start) * 2 + prevlen);
8529 prevsize = grow50pc > growmin ? grow50pc : growmin;
8530 }
8531 newprev = prev == NULL ? alloc(prevsize)
8532 : vim_realloc(prev, prevsize);
8533 if (newprev == NULL)
8534 {
8535 do_outofmem_msg((long_u)prevsize);
8536 failed = TRUE;
8537 break;
8538 }
8539 prev = newprev;
8540 }
8541 /* Add the line part to end of "prev". */
8542 mch_memmove(prev + prevlen, start, p - start);
8543 prevlen += (long)(p - start);
8544 }
8545 } /* while */
8546
8547 /*
8548 * For a negative line count use only the lines at the end of the file,
8549 * free the rest.
8550 */
8551 if (!failed && maxline < 0)
8552 while (cnt > -maxline)
8553 {
8554 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
8555 --cnt;
8556 }
8557
8558 if (failed)
8559 {
8560 list_free(rettv->vval.v_list);
8561 /* readfile doc says an empty list is returned on error */
8562 rettv->vval.v_list = list_alloc();
8563 }
8564
8565 vim_free(prev);
8566 fclose(fd);
8567}
8568
8569#if defined(FEAT_RELTIME)
8570static int list2proftime(typval_T *arg, proftime_T *tm);
8571
8572/*
8573 * Convert a List to proftime_T.
8574 * Return FAIL when there is something wrong.
8575 */
8576 static int
8577list2proftime(typval_T *arg, proftime_T *tm)
8578{
8579 long n1, n2;
8580 int error = FALSE;
8581
8582 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
8583 || arg->vval.v_list->lv_len != 2)
8584 return FAIL;
8585 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
8586 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
8587# ifdef WIN3264
8588 tm->HighPart = n1;
8589 tm->LowPart = n2;
8590# else
8591 tm->tv_sec = n1;
8592 tm->tv_usec = n2;
8593# endif
8594 return error ? FAIL : OK;
8595}
8596#endif /* FEAT_RELTIME */
8597
8598/*
8599 * "reltime()" function
8600 */
8601 static void
8602f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8603{
8604#ifdef FEAT_RELTIME
8605 proftime_T res;
8606 proftime_T start;
8607
8608 if (argvars[0].v_type == VAR_UNKNOWN)
8609 {
8610 /* No arguments: get current time. */
8611 profile_start(&res);
8612 }
8613 else if (argvars[1].v_type == VAR_UNKNOWN)
8614 {
8615 if (list2proftime(&argvars[0], &res) == FAIL)
8616 return;
8617 profile_end(&res);
8618 }
8619 else
8620 {
8621 /* Two arguments: compute the difference. */
8622 if (list2proftime(&argvars[0], &start) == FAIL
8623 || list2proftime(&argvars[1], &res) == FAIL)
8624 return;
8625 profile_sub(&res, &start);
8626 }
8627
8628 if (rettv_list_alloc(rettv) == OK)
8629 {
8630 long n1, n2;
8631
8632# ifdef WIN3264
8633 n1 = res.HighPart;
8634 n2 = res.LowPart;
8635# else
8636 n1 = res.tv_sec;
8637 n2 = res.tv_usec;
8638# endif
8639 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
8640 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
8641 }
8642#endif
8643}
8644
8645#ifdef FEAT_FLOAT
8646/*
8647 * "reltimefloat()" function
8648 */
8649 static void
8650f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
8651{
8652# ifdef FEAT_RELTIME
8653 proftime_T tm;
8654# endif
8655
8656 rettv->v_type = VAR_FLOAT;
8657 rettv->vval.v_float = 0;
8658# ifdef FEAT_RELTIME
8659 if (list2proftime(&argvars[0], &tm) == OK)
8660 rettv->vval.v_float = profile_float(&tm);
8661# endif
8662}
8663#endif
8664
8665/*
8666 * "reltimestr()" function
8667 */
8668 static void
8669f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
8670{
8671#ifdef FEAT_RELTIME
8672 proftime_T tm;
8673#endif
8674
8675 rettv->v_type = VAR_STRING;
8676 rettv->vval.v_string = NULL;
8677#ifdef FEAT_RELTIME
8678 if (list2proftime(&argvars[0], &tm) == OK)
8679 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
8680#endif
8681}
8682
8683#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
8684static void make_connection(void);
8685static int check_connection(void);
8686
8687 static void
8688make_connection(void)
8689{
8690 if (X_DISPLAY == NULL
8691# ifdef FEAT_GUI
8692 && !gui.in_use
8693# endif
8694 )
8695 {
8696 x_force_connect = TRUE;
8697 setup_term_clip();
8698 x_force_connect = FALSE;
8699 }
8700}
8701
8702 static int
8703check_connection(void)
8704{
8705 make_connection();
8706 if (X_DISPLAY == NULL)
8707 {
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01008708 EMSG(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008709 return FAIL;
8710 }
8711 return OK;
8712}
8713#endif
8714
8715#ifdef FEAT_CLIENTSERVER
8716 static void
8717remote_common(typval_T *argvars, typval_T *rettv, int expr)
8718{
8719 char_u *server_name;
8720 char_u *keys;
8721 char_u *r = NULL;
8722 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008723 int timeout = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008724# ifdef WIN32
8725 HWND w;
8726# else
8727 Window w;
8728# endif
8729
8730 if (check_restricted() || check_secure())
8731 return;
8732
8733# ifdef FEAT_X11
8734 if (check_connection() == FAIL)
8735 return;
8736# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008737 if (argvars[2].v_type != VAR_UNKNOWN
8738 && argvars[3].v_type != VAR_UNKNOWN)
8739 timeout = get_tv_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008740
8741 server_name = get_tv_string_chk(&argvars[0]);
8742 if (server_name == NULL)
8743 return; /* type error; errmsg already given */
8744 keys = get_tv_string_buf(&argvars[1], buf);
8745# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008746 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008747# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008748 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
8749 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008750# endif
8751 {
8752 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008753 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008754 EMSG(r); /* sending worked but evaluation failed */
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008755 vim_free(r);
8756 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008757 else
8758 EMSG2(_("E241: Unable to send to %s"), server_name);
8759 return;
8760 }
8761
8762 rettv->vval.v_string = r;
8763
8764 if (argvars[2].v_type != VAR_UNKNOWN)
8765 {
8766 dictitem_T v;
8767 char_u str[30];
8768 char_u *idvar;
8769
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008770 idvar = get_tv_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008771 if (idvar != NULL && *idvar != NUL)
8772 {
8773 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
8774 v.di_tv.v_type = VAR_STRING;
8775 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008776 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008777 vim_free(v.di_tv.vval.v_string);
8778 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008779 }
8780}
8781#endif
8782
8783/*
8784 * "remote_expr()" function
8785 */
8786 static void
8787f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
8788{
8789 rettv->v_type = VAR_STRING;
8790 rettv->vval.v_string = NULL;
8791#ifdef FEAT_CLIENTSERVER
8792 remote_common(argvars, rettv, TRUE);
8793#endif
8794}
8795
8796/*
8797 * "remote_foreground()" function
8798 */
8799 static void
8800f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8801{
8802#ifdef FEAT_CLIENTSERVER
8803# ifdef WIN32
8804 /* On Win32 it's done in this application. */
8805 {
8806 char_u *server_name = get_tv_string_chk(&argvars[0]);
8807
8808 if (server_name != NULL)
8809 serverForeground(server_name);
8810 }
8811# else
8812 /* Send a foreground() expression to the server. */
8813 argvars[1].v_type = VAR_STRING;
8814 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
8815 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02008816 rettv->v_type = VAR_STRING;
8817 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008818 remote_common(argvars, rettv, TRUE);
8819 vim_free(argvars[1].vval.v_string);
8820# endif
8821#endif
8822}
8823
8824 static void
8825f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
8826{
8827#ifdef FEAT_CLIENTSERVER
8828 dictitem_T v;
8829 char_u *s = NULL;
8830# ifdef WIN32
8831 long_u n = 0;
8832# endif
8833 char_u *serverid;
8834
8835 if (check_restricted() || check_secure())
8836 {
8837 rettv->vval.v_number = -1;
8838 return;
8839 }
8840 serverid = get_tv_string_chk(&argvars[0]);
8841 if (serverid == NULL)
8842 {
8843 rettv->vval.v_number = -1;
8844 return; /* type error; errmsg already given */
8845 }
8846# ifdef WIN32
8847 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
8848 if (n == 0)
8849 rettv->vval.v_number = -1;
8850 else
8851 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008852 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008853 rettv->vval.v_number = (s != NULL);
8854 }
8855# else
8856 if (check_connection() == FAIL)
8857 return;
8858
8859 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
8860 serverStrToWin(serverid), &s);
8861# endif
8862
8863 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
8864 {
8865 char_u *retvar;
8866
8867 v.di_tv.v_type = VAR_STRING;
8868 v.di_tv.vval.v_string = vim_strsave(s);
8869 retvar = get_tv_string_chk(&argvars[1]);
8870 if (retvar != NULL)
8871 set_var(retvar, &v.di_tv, FALSE);
8872 vim_free(v.di_tv.vval.v_string);
8873 }
8874#else
8875 rettv->vval.v_number = -1;
8876#endif
8877}
8878
8879 static void
8880f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
8881{
8882 char_u *r = NULL;
8883
8884#ifdef FEAT_CLIENTSERVER
8885 char_u *serverid = get_tv_string_chk(&argvars[0]);
8886
8887 if (serverid != NULL && !check_restricted() && !check_secure())
8888 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008889 int timeout = 0;
Bram Moolenaar1662ce12017-03-19 21:47:50 +01008890# ifdef WIN32
8891 /* The server's HWND is encoded in the 'id' parameter */
8892 long_u n = 0;
8893# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008894
8895 if (argvars[1].v_type != VAR_UNKNOWN)
8896 timeout = get_tv_number(&argvars[1]);
8897
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008898# ifdef WIN32
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008899 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
8900 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008901 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008902 if (r == NULL)
8903# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01008904 if (check_connection() == FAIL
8905 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
8906 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008907# endif
8908 EMSG(_("E277: Unable to read a server reply"));
8909 }
8910#endif
8911 rettv->v_type = VAR_STRING;
8912 rettv->vval.v_string = r;
8913}
8914
8915/*
8916 * "remote_send()" function
8917 */
8918 static void
8919f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
8920{
8921 rettv->v_type = VAR_STRING;
8922 rettv->vval.v_string = NULL;
8923#ifdef FEAT_CLIENTSERVER
8924 remote_common(argvars, rettv, FALSE);
8925#endif
8926}
8927
8928/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01008929 * "remote_startserver()" function
8930 */
8931 static void
8932f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8933{
8934#ifdef FEAT_CLIENTSERVER
8935 char_u *server = get_tv_string_chk(&argvars[0]);
8936
8937 if (server == NULL)
8938 return; /* type error; errmsg already given */
8939 if (serverName != NULL)
8940 EMSG(_("E941: already started a server"));
8941 else
8942 {
8943# ifdef FEAT_X11
8944 if (check_connection() == OK)
8945 serverRegisterName(X_DISPLAY, server);
8946# else
8947 serverSetName(server);
8948# endif
8949 }
8950#else
8951 EMSG(_("E942: +clientserver feature not available"));
8952#endif
8953}
8954
8955/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008956 * "remove()" function
8957 */
8958 static void
8959f_remove(typval_T *argvars, typval_T *rettv)
8960{
8961 list_T *l;
8962 listitem_T *item, *item2;
8963 listitem_T *li;
8964 long idx;
8965 long end;
8966 char_u *key;
8967 dict_T *d;
8968 dictitem_T *di;
8969 char_u *arg_errmsg = (char_u *)N_("remove() argument");
8970
8971 if (argvars[0].v_type == VAR_DICT)
8972 {
8973 if (argvars[2].v_type != VAR_UNKNOWN)
8974 EMSG2(_(e_toomanyarg), "remove()");
8975 else if ((d = argvars[0].vval.v_dict) != NULL
8976 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
8977 {
8978 key = get_tv_string_chk(&argvars[1]);
8979 if (key != NULL)
8980 {
8981 di = dict_find(d, key, -1);
8982 if (di == NULL)
8983 EMSG2(_(e_dictkey), key);
8984 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
8985 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
8986 {
8987 *rettv = di->di_tv;
8988 init_tv(&di->di_tv);
8989 dictitem_remove(d, di);
8990 }
8991 }
8992 }
8993 }
8994 else if (argvars[0].v_type != VAR_LIST)
8995 EMSG2(_(e_listdictarg), "remove()");
8996 else if ((l = argvars[0].vval.v_list) != NULL
8997 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
8998 {
8999 int error = FALSE;
9000
9001 idx = (long)get_tv_number_chk(&argvars[1], &error);
9002 if (error)
9003 ; /* type error: do nothing, errmsg already given */
9004 else if ((item = list_find(l, idx)) == NULL)
9005 EMSGN(_(e_listidx), idx);
9006 else
9007 {
9008 if (argvars[2].v_type == VAR_UNKNOWN)
9009 {
9010 /* Remove one item, return its value. */
9011 vimlist_remove(l, item, item);
9012 *rettv = item->li_tv;
9013 vim_free(item);
9014 }
9015 else
9016 {
9017 /* Remove range of items, return list with values. */
9018 end = (long)get_tv_number_chk(&argvars[2], &error);
9019 if (error)
9020 ; /* type error: do nothing */
9021 else if ((item2 = list_find(l, end)) == NULL)
9022 EMSGN(_(e_listidx), end);
9023 else
9024 {
9025 int cnt = 0;
9026
9027 for (li = item; li != NULL; li = li->li_next)
9028 {
9029 ++cnt;
9030 if (li == item2)
9031 break;
9032 }
9033 if (li == NULL) /* didn't find "item2" after "item" */
9034 EMSG(_(e_invrange));
9035 else
9036 {
9037 vimlist_remove(l, item, item2);
9038 if (rettv_list_alloc(rettv) == OK)
9039 {
9040 l = rettv->vval.v_list;
9041 l->lv_first = item;
9042 l->lv_last = item2;
9043 item->li_prev = NULL;
9044 item2->li_next = NULL;
9045 l->lv_len = cnt;
9046 }
9047 }
9048 }
9049 }
9050 }
9051 }
9052}
9053
9054/*
9055 * "rename({from}, {to})" function
9056 */
9057 static void
9058f_rename(typval_T *argvars, typval_T *rettv)
9059{
9060 char_u buf[NUMBUFLEN];
9061
9062 if (check_restricted() || check_secure())
9063 rettv->vval.v_number = -1;
9064 else
9065 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
9066 get_tv_string_buf(&argvars[1], buf));
9067}
9068
9069/*
9070 * "repeat()" function
9071 */
9072 static void
9073f_repeat(typval_T *argvars, typval_T *rettv)
9074{
9075 char_u *p;
9076 int n;
9077 int slen;
9078 int len;
9079 char_u *r;
9080 int i;
9081
9082 n = (int)get_tv_number(&argvars[1]);
9083 if (argvars[0].v_type == VAR_LIST)
9084 {
9085 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9086 while (n-- > 0)
9087 if (list_extend(rettv->vval.v_list,
9088 argvars[0].vval.v_list, NULL) == FAIL)
9089 break;
9090 }
9091 else
9092 {
9093 p = get_tv_string(&argvars[0]);
9094 rettv->v_type = VAR_STRING;
9095 rettv->vval.v_string = NULL;
9096
9097 slen = (int)STRLEN(p);
9098 len = slen * n;
9099 if (len <= 0)
9100 return;
9101
9102 r = alloc(len + 1);
9103 if (r != NULL)
9104 {
9105 for (i = 0; i < n; i++)
9106 mch_memmove(r + i * slen, p, (size_t)slen);
9107 r[len] = NUL;
9108 }
9109
9110 rettv->vval.v_string = r;
9111 }
9112}
9113
9114/*
9115 * "resolve()" function
9116 */
9117 static void
9118f_resolve(typval_T *argvars, typval_T *rettv)
9119{
9120 char_u *p;
9121#ifdef HAVE_READLINK
9122 char_u *buf = NULL;
9123#endif
9124
9125 p = get_tv_string(&argvars[0]);
9126#ifdef FEAT_SHORTCUT
9127 {
9128 char_u *v = NULL;
9129
9130 v = mch_resolve_shortcut(p);
9131 if (v != NULL)
9132 rettv->vval.v_string = v;
9133 else
9134 rettv->vval.v_string = vim_strsave(p);
9135 }
9136#else
9137# ifdef HAVE_READLINK
9138 {
9139 char_u *cpy;
9140 int len;
9141 char_u *remain = NULL;
9142 char_u *q;
9143 int is_relative_to_current = FALSE;
9144 int has_trailing_pathsep = FALSE;
9145 int limit = 100;
9146
9147 p = vim_strsave(p);
9148
9149 if (p[0] == '.' && (vim_ispathsep(p[1])
9150 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9151 is_relative_to_current = TRUE;
9152
9153 len = STRLEN(p);
9154 if (len > 0 && after_pathsep(p, p + len))
9155 {
9156 has_trailing_pathsep = TRUE;
9157 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9158 }
9159
9160 q = getnextcomp(p);
9161 if (*q != NUL)
9162 {
9163 /* Separate the first path component in "p", and keep the
9164 * remainder (beginning with the path separator). */
9165 remain = vim_strsave(q - 1);
9166 q[-1] = NUL;
9167 }
9168
9169 buf = alloc(MAXPATHL + 1);
9170 if (buf == NULL)
9171 goto fail;
9172
9173 for (;;)
9174 {
9175 for (;;)
9176 {
9177 len = readlink((char *)p, (char *)buf, MAXPATHL);
9178 if (len <= 0)
9179 break;
9180 buf[len] = NUL;
9181
9182 if (limit-- == 0)
9183 {
9184 vim_free(p);
9185 vim_free(remain);
9186 EMSG(_("E655: Too many symbolic links (cycle?)"));
9187 rettv->vval.v_string = NULL;
9188 goto fail;
9189 }
9190
9191 /* Ensure that the result will have a trailing path separator
9192 * if the argument has one. */
9193 if (remain == NULL && has_trailing_pathsep)
9194 add_pathsep(buf);
9195
9196 /* Separate the first path component in the link value and
9197 * concatenate the remainders. */
9198 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9199 if (*q != NUL)
9200 {
9201 if (remain == NULL)
9202 remain = vim_strsave(q - 1);
9203 else
9204 {
9205 cpy = concat_str(q - 1, remain);
9206 if (cpy != NULL)
9207 {
9208 vim_free(remain);
9209 remain = cpy;
9210 }
9211 }
9212 q[-1] = NUL;
9213 }
9214
9215 q = gettail(p);
9216 if (q > p && *q == NUL)
9217 {
9218 /* Ignore trailing path separator. */
9219 q[-1] = NUL;
9220 q = gettail(p);
9221 }
9222 if (q > p && !mch_isFullName(buf))
9223 {
9224 /* symlink is relative to directory of argument */
9225 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
9226 if (cpy != NULL)
9227 {
9228 STRCPY(cpy, p);
9229 STRCPY(gettail(cpy), buf);
9230 vim_free(p);
9231 p = cpy;
9232 }
9233 }
9234 else
9235 {
9236 vim_free(p);
9237 p = vim_strsave(buf);
9238 }
9239 }
9240
9241 if (remain == NULL)
9242 break;
9243
9244 /* Append the first path component of "remain" to "p". */
9245 q = getnextcomp(remain + 1);
9246 len = q - remain - (*q != NUL);
9247 cpy = vim_strnsave(p, STRLEN(p) + len);
9248 if (cpy != NULL)
9249 {
9250 STRNCAT(cpy, remain, len);
9251 vim_free(p);
9252 p = cpy;
9253 }
9254 /* Shorten "remain". */
9255 if (*q != NUL)
9256 STRMOVE(remain, q - 1);
9257 else
Bram Moolenaard23a8232018-02-10 18:45:26 +01009258 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009259 }
9260
9261 /* If the result is a relative path name, make it explicitly relative to
9262 * the current directory if and only if the argument had this form. */
9263 if (!vim_ispathsep(*p))
9264 {
9265 if (is_relative_to_current
9266 && *p != NUL
9267 && !(p[0] == '.'
9268 && (p[1] == NUL
9269 || vim_ispathsep(p[1])
9270 || (p[1] == '.'
9271 && (p[2] == NUL
9272 || vim_ispathsep(p[2]))))))
9273 {
9274 /* Prepend "./". */
9275 cpy = concat_str((char_u *)"./", p);
9276 if (cpy != NULL)
9277 {
9278 vim_free(p);
9279 p = cpy;
9280 }
9281 }
9282 else if (!is_relative_to_current)
9283 {
9284 /* Strip leading "./". */
9285 q = p;
9286 while (q[0] == '.' && vim_ispathsep(q[1]))
9287 q += 2;
9288 if (q > p)
9289 STRMOVE(p, p + 2);
9290 }
9291 }
9292
9293 /* Ensure that the result will have no trailing path separator
9294 * if the argument had none. But keep "/" or "//". */
9295 if (!has_trailing_pathsep)
9296 {
9297 q = p + STRLEN(p);
9298 if (after_pathsep(p, q))
9299 *gettail_sep(p) = NUL;
9300 }
9301
9302 rettv->vval.v_string = p;
9303 }
9304# else
9305 rettv->vval.v_string = vim_strsave(p);
9306# endif
9307#endif
9308
9309 simplify_filename(rettv->vval.v_string);
9310
9311#ifdef HAVE_READLINK
9312fail:
9313 vim_free(buf);
9314#endif
9315 rettv->v_type = VAR_STRING;
9316}
9317
9318/*
9319 * "reverse({list})" function
9320 */
9321 static void
9322f_reverse(typval_T *argvars, typval_T *rettv)
9323{
9324 list_T *l;
9325 listitem_T *li, *ni;
9326
9327 if (argvars[0].v_type != VAR_LIST)
9328 EMSG2(_(e_listarg), "reverse()");
9329 else if ((l = argvars[0].vval.v_list) != NULL
9330 && !tv_check_lock(l->lv_lock,
9331 (char_u *)N_("reverse() argument"), TRUE))
9332 {
9333 li = l->lv_last;
9334 l->lv_first = l->lv_last = NULL;
9335 l->lv_len = 0;
9336 while (li != NULL)
9337 {
9338 ni = li->li_prev;
9339 list_append(l, li);
9340 li = ni;
9341 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009342 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009343 l->lv_idx = l->lv_len - l->lv_idx - 1;
9344 }
9345}
9346
9347#define SP_NOMOVE 0x01 /* don't move cursor */
9348#define SP_REPEAT 0x02 /* repeat to find outer pair */
9349#define SP_RETCOUNT 0x04 /* return matchcount */
9350#define SP_SETPCMARK 0x08 /* set previous context mark */
9351#define SP_START 0x10 /* accept match at start position */
9352#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9353#define SP_END 0x40 /* leave cursor at end of match */
9354#define SP_COLUMN 0x80 /* start at cursor column */
9355
9356static int get_search_arg(typval_T *varp, int *flagsp);
9357
9358/*
9359 * Get flags for a search function.
9360 * Possibly sets "p_ws".
9361 * Returns BACKWARD, FORWARD or zero (for an error).
9362 */
9363 static int
9364get_search_arg(typval_T *varp, int *flagsp)
9365{
9366 int dir = FORWARD;
9367 char_u *flags;
9368 char_u nbuf[NUMBUFLEN];
9369 int mask;
9370
9371 if (varp->v_type != VAR_UNKNOWN)
9372 {
9373 flags = get_tv_string_buf_chk(varp, nbuf);
9374 if (flags == NULL)
9375 return 0; /* type error; errmsg already given */
9376 while (*flags != NUL)
9377 {
9378 switch (*flags)
9379 {
9380 case 'b': dir = BACKWARD; break;
9381 case 'w': p_ws = TRUE; break;
9382 case 'W': p_ws = FALSE; break;
9383 default: mask = 0;
9384 if (flagsp != NULL)
9385 switch (*flags)
9386 {
9387 case 'c': mask = SP_START; break;
9388 case 'e': mask = SP_END; break;
9389 case 'm': mask = SP_RETCOUNT; break;
9390 case 'n': mask = SP_NOMOVE; break;
9391 case 'p': mask = SP_SUBPAT; break;
9392 case 'r': mask = SP_REPEAT; break;
9393 case 's': mask = SP_SETPCMARK; break;
9394 case 'z': mask = SP_COLUMN; break;
9395 }
9396 if (mask == 0)
9397 {
9398 EMSG2(_(e_invarg2), flags);
9399 dir = 0;
9400 }
9401 else
9402 *flagsp |= mask;
9403 }
9404 if (dir == 0)
9405 break;
9406 ++flags;
9407 }
9408 }
9409 return dir;
9410}
9411
9412/*
9413 * Shared by search() and searchpos() functions.
9414 */
9415 static int
9416search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9417{
9418 int flags;
9419 char_u *pat;
9420 pos_T pos;
9421 pos_T save_cursor;
9422 int save_p_ws = p_ws;
9423 int dir;
9424 int retval = 0; /* default: FAIL */
9425 long lnum_stop = 0;
9426 proftime_T tm;
9427#ifdef FEAT_RELTIME
9428 long time_limit = 0;
9429#endif
9430 int options = SEARCH_KEEP;
9431 int subpatnum;
9432
9433 pat = get_tv_string(&argvars[0]);
9434 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
9435 if (dir == 0)
9436 goto theend;
9437 flags = *flagsp;
9438 if (flags & SP_START)
9439 options |= SEARCH_START;
9440 if (flags & SP_END)
9441 options |= SEARCH_END;
9442 if (flags & SP_COLUMN)
9443 options |= SEARCH_COL;
9444
9445 /* Optional arguments: line number to stop searching and timeout. */
9446 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9447 {
9448 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
9449 if (lnum_stop < 0)
9450 goto theend;
9451#ifdef FEAT_RELTIME
9452 if (argvars[3].v_type != VAR_UNKNOWN)
9453 {
9454 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
9455 if (time_limit < 0)
9456 goto theend;
9457 }
9458#endif
9459 }
9460
9461#ifdef FEAT_RELTIME
9462 /* Set the time limit, if there is one. */
9463 profile_setlimit(time_limit, &tm);
9464#endif
9465
9466 /*
9467 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
9468 * Check to make sure only those flags are set.
9469 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
9470 * flags cannot be set. Check for that condition also.
9471 */
9472 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
9473 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9474 {
9475 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
9476 goto theend;
9477 }
9478
9479 pos = save_cursor = curwin->w_cursor;
9480 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009481 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009482 if (subpatnum != FAIL)
9483 {
9484 if (flags & SP_SUBPAT)
9485 retval = subpatnum;
9486 else
9487 retval = pos.lnum;
9488 if (flags & SP_SETPCMARK)
9489 setpcmark();
9490 curwin->w_cursor = pos;
9491 if (match_pos != NULL)
9492 {
9493 /* Store the match cursor position */
9494 match_pos->lnum = pos.lnum;
9495 match_pos->col = pos.col + 1;
9496 }
9497 /* "/$" will put the cursor after the end of the line, may need to
9498 * correct that here */
9499 check_cursor();
9500 }
9501
9502 /* If 'n' flag is used: restore cursor position. */
9503 if (flags & SP_NOMOVE)
9504 curwin->w_cursor = save_cursor;
9505 else
9506 curwin->w_set_curswant = TRUE;
9507theend:
9508 p_ws = save_p_ws;
9509
9510 return retval;
9511}
9512
9513#ifdef FEAT_FLOAT
9514
9515/*
9516 * round() is not in C90, use ceil() or floor() instead.
9517 */
9518 float_T
9519vim_round(float_T f)
9520{
9521 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
9522}
9523
9524/*
9525 * "round({float})" function
9526 */
9527 static void
9528f_round(typval_T *argvars, typval_T *rettv)
9529{
9530 float_T f = 0.0;
9531
9532 rettv->v_type = VAR_FLOAT;
9533 if (get_float_arg(argvars, &f) == OK)
9534 rettv->vval.v_float = vim_round(f);
9535 else
9536 rettv->vval.v_float = 0.0;
9537}
9538#endif
9539
9540/*
9541 * "screenattr()" function
9542 */
9543 static void
9544f_screenattr(typval_T *argvars, typval_T *rettv)
9545{
9546 int row;
9547 int col;
9548 int c;
9549
9550 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9551 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9552 if (row < 0 || row >= screen_Rows
9553 || col < 0 || col >= screen_Columns)
9554 c = -1;
9555 else
9556 c = ScreenAttrs[LineOffset[row] + col];
9557 rettv->vval.v_number = c;
9558}
9559
9560/*
9561 * "screenchar()" function
9562 */
9563 static void
9564f_screenchar(typval_T *argvars, typval_T *rettv)
9565{
9566 int row;
9567 int col;
9568 int off;
9569 int c;
9570
9571 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9572 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9573 if (row < 0 || row >= screen_Rows
9574 || col < 0 || col >= screen_Columns)
9575 c = -1;
9576 else
9577 {
9578 off = LineOffset[row] + col;
9579#ifdef FEAT_MBYTE
9580 if (enc_utf8 && ScreenLinesUC[off] != 0)
9581 c = ScreenLinesUC[off];
9582 else
9583#endif
9584 c = ScreenLines[off];
9585 }
9586 rettv->vval.v_number = c;
9587}
9588
9589/*
9590 * "screencol()" function
9591 *
9592 * First column is 1 to be consistent with virtcol().
9593 */
9594 static void
9595f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
9596{
9597 rettv->vval.v_number = screen_screencol() + 1;
9598}
9599
9600/*
9601 * "screenrow()" function
9602 */
9603 static void
9604f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
9605{
9606 rettv->vval.v_number = screen_screenrow() + 1;
9607}
9608
9609/*
9610 * "search()" function
9611 */
9612 static void
9613f_search(typval_T *argvars, typval_T *rettv)
9614{
9615 int flags = 0;
9616
9617 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
9618}
9619
9620/*
9621 * "searchdecl()" function
9622 */
9623 static void
9624f_searchdecl(typval_T *argvars, typval_T *rettv)
9625{
9626 int locally = 1;
9627 int thisblock = 0;
9628 int error = FALSE;
9629 char_u *name;
9630
9631 rettv->vval.v_number = 1; /* default: FAIL */
9632
9633 name = get_tv_string_chk(&argvars[0]);
9634 if (argvars[1].v_type != VAR_UNKNOWN)
9635 {
9636 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
9637 if (!error && argvars[2].v_type != VAR_UNKNOWN)
9638 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
9639 }
9640 if (!error && name != NULL)
9641 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
9642 locally, thisblock, SEARCH_KEEP) == FAIL;
9643}
9644
9645/*
9646 * Used by searchpair() and searchpairpos()
9647 */
9648 static int
9649searchpair_cmn(typval_T *argvars, pos_T *match_pos)
9650{
9651 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +01009652 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009653 int save_p_ws = p_ws;
9654 int dir;
9655 int flags = 0;
9656 char_u nbuf1[NUMBUFLEN];
9657 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009658 int retval = 0; /* default: FAIL */
9659 long lnum_stop = 0;
9660 long time_limit = 0;
9661
9662 /* Get the three pattern arguments: start, middle, end. */
9663 spat = get_tv_string_chk(&argvars[0]);
9664 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
9665 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
9666 if (spat == NULL || mpat == NULL || epat == NULL)
9667 goto theend; /* type error */
9668
9669 /* Handle the optional fourth argument: flags */
9670 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
9671 if (dir == 0)
9672 goto theend;
9673
9674 /* Don't accept SP_END or SP_SUBPAT.
9675 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
9676 */
9677 if ((flags & (SP_END | SP_SUBPAT)) != 0
9678 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9679 {
9680 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
9681 goto theend;
9682 }
9683
9684 /* Using 'r' implies 'W', otherwise it doesn't work. */
9685 if (flags & SP_REPEAT)
9686 p_ws = FALSE;
9687
9688 /* Optional fifth argument: skip expression */
9689 if (argvars[3].v_type == VAR_UNKNOWN
9690 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +01009691 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009692 else
9693 {
Bram Moolenaar48570482017-10-30 21:48:41 +01009694 skip = &argvars[4];
9695 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
9696 && skip->v_type != VAR_STRING)
9697 {
9698 /* Type error */
9699 goto theend;
9700 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009701 if (argvars[5].v_type != VAR_UNKNOWN)
9702 {
9703 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
9704 if (lnum_stop < 0)
9705 goto theend;
9706#ifdef FEAT_RELTIME
9707 if (argvars[6].v_type != VAR_UNKNOWN)
9708 {
9709 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
9710 if (time_limit < 0)
9711 goto theend;
9712 }
9713#endif
9714 }
9715 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009716
9717 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
9718 match_pos, lnum_stop, time_limit);
9719
9720theend:
9721 p_ws = save_p_ws;
9722
9723 return retval;
9724}
9725
9726/*
9727 * "searchpair()" function
9728 */
9729 static void
9730f_searchpair(typval_T *argvars, typval_T *rettv)
9731{
9732 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
9733}
9734
9735/*
9736 * "searchpairpos()" function
9737 */
9738 static void
9739f_searchpairpos(typval_T *argvars, typval_T *rettv)
9740{
9741 pos_T match_pos;
9742 int lnum = 0;
9743 int col = 0;
9744
9745 if (rettv_list_alloc(rettv) == FAIL)
9746 return;
9747
9748 if (searchpair_cmn(argvars, &match_pos) > 0)
9749 {
9750 lnum = match_pos.lnum;
9751 col = match_pos.col;
9752 }
9753
9754 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9755 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9756}
9757
9758/*
9759 * Search for a start/middle/end thing.
9760 * Used by searchpair(), see its documentation for the details.
9761 * Returns 0 or -1 for no match,
9762 */
9763 long
9764do_searchpair(
9765 char_u *spat, /* start pattern */
9766 char_u *mpat, /* middle pattern */
9767 char_u *epat, /* end pattern */
9768 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +01009769 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009770 int flags, /* SP_SETPCMARK and other SP_ values */
9771 pos_T *match_pos,
9772 linenr_T lnum_stop, /* stop at this line if not zero */
9773 long time_limit UNUSED) /* stop after this many msec */
9774{
9775 char_u *save_cpo;
9776 char_u *pat, *pat2 = NULL, *pat3 = NULL;
9777 long retval = 0;
9778 pos_T pos;
9779 pos_T firstpos;
9780 pos_T foundpos;
9781 pos_T save_cursor;
9782 pos_T save_pos;
9783 int n;
9784 int r;
9785 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +01009786 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009787 int err;
9788 int options = SEARCH_KEEP;
9789 proftime_T tm;
9790
9791 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
9792 save_cpo = p_cpo;
9793 p_cpo = empty_option;
9794
9795#ifdef FEAT_RELTIME
9796 /* Set the time limit, if there is one. */
9797 profile_setlimit(time_limit, &tm);
9798#endif
9799
9800 /* Make two search patterns: start/end (pat2, for in nested pairs) and
9801 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009802 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
9803 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009804 if (pat2 == NULL || pat3 == NULL)
9805 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009806 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009807 if (*mpat == NUL)
9808 STRCPY(pat3, pat2);
9809 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009810 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009811 spat, epat, mpat);
9812 if (flags & SP_START)
9813 options |= SEARCH_START;
9814
Bram Moolenaar48570482017-10-30 21:48:41 +01009815 if (skip != NULL)
9816 {
9817 /* Empty string means to not use the skip expression. */
9818 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
9819 use_skip = skip->vval.v_string != NULL
9820 && *skip->vval.v_string != NUL;
9821 }
9822
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009823 save_cursor = curwin->w_cursor;
9824 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009825 CLEAR_POS(&firstpos);
9826 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009827 pat = pat3;
9828 for (;;)
9829 {
9830 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009831 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009832 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009833 /* didn't find it or found the first match again: FAIL */
9834 break;
9835
9836 if (firstpos.lnum == 0)
9837 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01009838 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009839 {
9840 /* Found the same position again. Can happen with a pattern that
9841 * has "\zs" at the end and searching backwards. Advance one
9842 * character and try again. */
9843 if (dir == BACKWARD)
9844 decl(&pos);
9845 else
9846 incl(&pos);
9847 }
9848 foundpos = pos;
9849
9850 /* clear the start flag to avoid getting stuck here */
9851 options &= ~SEARCH_START;
9852
9853 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +01009854 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009855 {
9856 save_pos = curwin->w_cursor;
9857 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +01009858 err = FALSE;
9859 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009860 curwin->w_cursor = save_pos;
9861 if (err)
9862 {
9863 /* Evaluating {skip} caused an error, break here. */
9864 curwin->w_cursor = save_cursor;
9865 retval = -1;
9866 break;
9867 }
9868 if (r)
9869 continue;
9870 }
9871
9872 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
9873 {
9874 /* Found end when searching backwards or start when searching
9875 * forward: nested pair. */
9876 ++nest;
9877 pat = pat2; /* nested, don't search for middle */
9878 }
9879 else
9880 {
9881 /* Found end when searching forward or start when searching
9882 * backward: end of (nested) pair; or found middle in outer pair. */
9883 if (--nest == 1)
9884 pat = pat3; /* outer level, search for middle */
9885 }
9886
9887 if (nest == 0)
9888 {
9889 /* Found the match: return matchcount or line number. */
9890 if (flags & SP_RETCOUNT)
9891 ++retval;
9892 else
9893 retval = pos.lnum;
9894 if (flags & SP_SETPCMARK)
9895 setpcmark();
9896 curwin->w_cursor = pos;
9897 if (!(flags & SP_REPEAT))
9898 break;
9899 nest = 1; /* search for next unmatched */
9900 }
9901 }
9902
9903 if (match_pos != NULL)
9904 {
9905 /* Store the match cursor position */
9906 match_pos->lnum = curwin->w_cursor.lnum;
9907 match_pos->col = curwin->w_cursor.col + 1;
9908 }
9909
9910 /* If 'n' flag is used or search failed: restore cursor position. */
9911 if ((flags & SP_NOMOVE) || retval == 0)
9912 curwin->w_cursor = save_cursor;
9913
9914theend:
9915 vim_free(pat2);
9916 vim_free(pat3);
9917 if (p_cpo == empty_option)
9918 p_cpo = save_cpo;
9919 else
9920 /* Darn, evaluating the {skip} expression changed the value. */
9921 free_string_option(save_cpo);
9922
9923 return retval;
9924}
9925
9926/*
9927 * "searchpos()" function
9928 */
9929 static void
9930f_searchpos(typval_T *argvars, typval_T *rettv)
9931{
9932 pos_T match_pos;
9933 int lnum = 0;
9934 int col = 0;
9935 int n;
9936 int flags = 0;
9937
9938 if (rettv_list_alloc(rettv) == FAIL)
9939 return;
9940
9941 n = search_cmn(argvars, &match_pos, &flags);
9942 if (n > 0)
9943 {
9944 lnum = match_pos.lnum;
9945 col = match_pos.col;
9946 }
9947
9948 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9949 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9950 if (flags & SP_SUBPAT)
9951 list_append_number(rettv->vval.v_list, (varnumber_T)n);
9952}
9953
9954 static void
9955f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
9956{
9957#ifdef FEAT_CLIENTSERVER
9958 char_u buf[NUMBUFLEN];
9959 char_u *server = get_tv_string_chk(&argvars[0]);
9960 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
9961
9962 rettv->vval.v_number = -1;
9963 if (server == NULL || reply == NULL)
9964 return;
9965 if (check_restricted() || check_secure())
9966 return;
9967# ifdef FEAT_X11
9968 if (check_connection() == FAIL)
9969 return;
9970# endif
9971
9972 if (serverSendReply(server, reply) < 0)
9973 {
9974 EMSG(_("E258: Unable to send to client"));
9975 return;
9976 }
9977 rettv->vval.v_number = 0;
9978#else
9979 rettv->vval.v_number = -1;
9980#endif
9981}
9982
9983 static void
9984f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
9985{
9986 char_u *r = NULL;
9987
9988#ifdef FEAT_CLIENTSERVER
9989# ifdef WIN32
9990 r = serverGetVimNames();
9991# else
9992 make_connection();
9993 if (X_DISPLAY != NULL)
9994 r = serverGetVimNames(X_DISPLAY);
9995# endif
9996#endif
9997 rettv->v_type = VAR_STRING;
9998 rettv->vval.v_string = r;
9999}
10000
10001/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010002 * Set line or list of lines in buffer "buf".
10003 */
10004 static void
10005set_buffer_lines(buf_T *buf, linenr_T lnum, typval_T *lines, typval_T *rettv)
10006{
10007 char_u *line = NULL;
10008 list_T *l = NULL;
10009 listitem_T *li = NULL;
10010 long added = 0;
10011 linenr_T lcount;
Bram Moolenaar0c4dc882017-11-06 21:32:54 +010010012 buf_T *curbuf_save = NULL;
10013 win_T *curwin_save = NULL;
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010014 int is_curbuf = buf == curbuf;
10015
Bram Moolenaar9d954202017-09-04 20:34:19 +020010016 /* When using the current buffer ml_mfp will be set if needed. Useful when
10017 * setline() is used on startup. For other buffers the buffer must be
10018 * loaded. */
10019 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010020 {
10021 rettv->vval.v_number = 1; /* FAIL */
10022 return;
10023 }
10024
Bram Moolenaar0c4dc882017-11-06 21:32:54 +010010025 if (!is_curbuf)
10026 {
10027 wininfo_T *wip;
10028
10029 curbuf_save = curbuf;
10030 curwin_save = curwin;
10031 curbuf = buf;
10032 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
10033 {
10034 if (wip->wi_win != NULL)
10035 {
10036 curwin = wip->wi_win;
10037 break;
10038 }
10039 }
10040 }
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010041
10042 lcount = curbuf->b_ml.ml_line_count;
10043
10044 if (lines->v_type == VAR_LIST)
10045 {
10046 l = lines->vval.v_list;
10047 li = l->lv_first;
10048 }
10049 else
10050 line = get_tv_string_chk(lines);
10051
10052 /* default result is zero == OK */
10053 for (;;)
10054 {
10055 if (l != NULL)
10056 {
10057 /* list argument, get next string */
10058 if (li == NULL)
10059 break;
10060 line = get_tv_string_chk(&li->li_tv);
10061 li = li->li_next;
10062 }
10063
10064 rettv->vval.v_number = 1; /* FAIL */
10065 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
10066 break;
10067
10068 /* When coming here from Insert mode, sync undo, so that this can be
10069 * undone separately from what was previously inserted. */
10070 if (u_sync_once == 2)
10071 {
10072 u_sync_once = 1; /* notify that u_sync() was called */
10073 u_sync(TRUE);
10074 }
10075
10076 if (lnum <= curbuf->b_ml.ml_line_count)
10077 {
10078 /* existing line, replace it */
10079 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
10080 {
10081 changed_bytes(lnum, 0);
10082 if (is_curbuf && lnum == curwin->w_cursor.lnum)
10083 check_cursor_col();
10084 rettv->vval.v_number = 0; /* OK */
10085 }
10086 }
10087 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
10088 {
10089 /* lnum is one past the last line, append the line */
10090 ++added;
10091 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
10092 rettv->vval.v_number = 0; /* OK */
10093 }
10094
10095 if (l == NULL) /* only one string argument */
10096 break;
10097 ++lnum;
10098 }
10099
10100 if (added > 0)
10101 appended_lines_mark(lcount, added);
10102
Bram Moolenaar0c4dc882017-11-06 21:32:54 +010010103 if (!is_curbuf)
10104 {
10105 curbuf = curbuf_save;
10106 curwin = curwin_save;
10107 }
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010108}
10109
10110/*
10111 * "setbufline()" function
10112 */
10113 static void
10114f_setbufline(argvars, rettv)
10115 typval_T *argvars;
10116 typval_T *rettv;
10117{
10118 linenr_T lnum;
10119 buf_T *buf;
10120
10121 buf = get_buf_tv(&argvars[0], FALSE);
10122 if (buf == NULL)
10123 rettv->vval.v_number = 1; /* FAIL */
10124 else
10125 {
10126 lnum = get_tv_lnum_buf(&argvars[1], buf);
10127
10128 set_buffer_lines(buf, lnum, &argvars[2], rettv);
10129 }
10130}
10131
10132/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010133 * "setbufvar()" function
10134 */
10135 static void
10136f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10137{
10138 buf_T *buf;
10139 char_u *varname, *bufvarname;
10140 typval_T *varp;
10141 char_u nbuf[NUMBUFLEN];
10142
10143 if (check_restricted() || check_secure())
10144 return;
10145 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10146 varname = get_tv_string_chk(&argvars[1]);
10147 buf = get_buf_tv(&argvars[0], FALSE);
10148 varp = &argvars[2];
10149
10150 if (buf != NULL && varname != NULL && varp != NULL)
10151 {
10152 if (*varname == '&')
10153 {
10154 long numval;
10155 char_u *strval;
10156 int error = FALSE;
10157 aco_save_T aco;
10158
10159 /* set curbuf to be our buf, temporarily */
10160 aucmd_prepbuf(&aco, buf);
10161
10162 ++varname;
10163 numval = (long)get_tv_number_chk(varp, &error);
10164 strval = get_tv_string_buf_chk(varp, nbuf);
10165 if (!error && strval != NULL)
10166 set_option_value(varname, numval, strval, OPT_LOCAL);
10167
10168 /* reset notion of buffer */
10169 aucmd_restbuf(&aco);
10170 }
10171 else
10172 {
10173 buf_T *save_curbuf = curbuf;
10174
10175 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
10176 if (bufvarname != NULL)
10177 {
10178 curbuf = buf;
10179 STRCPY(bufvarname, "b:");
10180 STRCPY(bufvarname + 2, varname);
10181 set_var(bufvarname, varp, TRUE);
10182 vim_free(bufvarname);
10183 curbuf = save_curbuf;
10184 }
10185 }
10186 }
10187}
10188
10189 static void
10190f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10191{
10192 dict_T *d;
10193 dictitem_T *di;
10194 char_u *csearch;
10195
10196 if (argvars[0].v_type != VAR_DICT)
10197 {
10198 EMSG(_(e_dictreq));
10199 return;
10200 }
10201
10202 if ((d = argvars[0].vval.v_dict) != NULL)
10203 {
10204 csearch = get_dict_string(d, (char_u *)"char", FALSE);
10205 if (csearch != NULL)
10206 {
10207#ifdef FEAT_MBYTE
10208 if (enc_utf8)
10209 {
10210 int pcc[MAX_MCO];
10211 int c = utfc_ptr2char(csearch, pcc);
10212
10213 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10214 }
10215 else
10216#endif
10217 set_last_csearch(PTR2CHAR(csearch),
10218 csearch, MB_PTR2LEN(csearch));
10219 }
10220
10221 di = dict_find(d, (char_u *)"forward", -1);
10222 if (di != NULL)
10223 set_csearch_direction((int)get_tv_number(&di->di_tv)
10224 ? FORWARD : BACKWARD);
10225
10226 di = dict_find(d, (char_u *)"until", -1);
10227 if (di != NULL)
10228 set_csearch_until(!!get_tv_number(&di->di_tv));
10229 }
10230}
10231
10232/*
10233 * "setcmdpos()" function
10234 */
10235 static void
10236f_setcmdpos(typval_T *argvars, typval_T *rettv)
10237{
10238 int pos = (int)get_tv_number(&argvars[0]) - 1;
10239
10240 if (pos >= 0)
10241 rettv->vval.v_number = set_cmdline_pos(pos);
10242}
10243
10244/*
10245 * "setfperm({fname}, {mode})" function
10246 */
10247 static void
10248f_setfperm(typval_T *argvars, typval_T *rettv)
10249{
10250 char_u *fname;
10251 char_u modebuf[NUMBUFLEN];
10252 char_u *mode_str;
10253 int i;
10254 int mask;
10255 int mode = 0;
10256
10257 rettv->vval.v_number = 0;
10258 fname = get_tv_string_chk(&argvars[0]);
10259 if (fname == NULL)
10260 return;
10261 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
10262 if (mode_str == NULL)
10263 return;
10264 if (STRLEN(mode_str) != 9)
10265 {
10266 EMSG2(_(e_invarg2), mode_str);
10267 return;
10268 }
10269
10270 mask = 1;
10271 for (i = 8; i >= 0; --i)
10272 {
10273 if (mode_str[i] != '-')
10274 mode |= mask;
10275 mask = mask << 1;
10276 }
10277 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10278}
10279
10280/*
10281 * "setline()" function
10282 */
10283 static void
10284f_setline(typval_T *argvars, typval_T *rettv)
10285{
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010286 linenr_T lnum = get_tv_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010287
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010288 set_buffer_lines(curbuf, lnum, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010289}
10290
Bram Moolenaard823fa92016-08-12 16:29:27 +020010291static 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 +020010292
10293/*
10294 * Used by "setqflist()" and "setloclist()" functions
10295 */
10296 static void
10297set_qf_ll_list(
10298 win_T *wp UNUSED,
10299 typval_T *list_arg UNUSED,
10300 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010301 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010302 typval_T *rettv)
10303{
10304#ifdef FEAT_QUICKFIX
10305 static char *e_invact = N_("E927: Invalid action: '%s'");
10306 char_u *act;
10307 int action = 0;
10308#endif
10309
10310 rettv->vval.v_number = -1;
10311
10312#ifdef FEAT_QUICKFIX
10313 if (list_arg->v_type != VAR_LIST)
10314 EMSG(_(e_listreq));
10315 else
10316 {
10317 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010318 dict_T *d = NULL;
10319 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010320
10321 if (action_arg->v_type == VAR_STRING)
10322 {
10323 act = get_tv_string_chk(action_arg);
10324 if (act == NULL)
10325 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020010326 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
10327 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010328 action = *act;
10329 else
10330 EMSG2(_(e_invact), act);
10331 }
10332 else if (action_arg->v_type == VAR_UNKNOWN)
10333 action = ' ';
10334 else
10335 EMSG(_(e_stringreq));
10336
Bram Moolenaard823fa92016-08-12 16:29:27 +020010337 if (action_arg->v_type != VAR_UNKNOWN
10338 && what_arg->v_type != VAR_UNKNOWN)
10339 {
10340 if (what_arg->v_type == VAR_DICT)
10341 d = what_arg->vval.v_dict;
10342 else
10343 {
10344 EMSG(_(e_dictreq));
10345 valid_dict = FALSE;
10346 }
10347 }
10348
10349 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
10350 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()"), d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010351 rettv->vval.v_number = 0;
10352 }
10353#endif
10354}
10355
10356/*
10357 * "setloclist()" function
10358 */
10359 static void
10360f_setloclist(typval_T *argvars, typval_T *rettv)
10361{
10362 win_T *win;
10363
10364 rettv->vval.v_number = -1;
10365
10366 win = find_win_by_nr(&argvars[0], NULL);
10367 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020010368 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010369}
10370
10371/*
10372 * "setmatches()" function
10373 */
10374 static void
10375f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10376{
10377#ifdef FEAT_SEARCH_EXTRA
10378 list_T *l;
10379 listitem_T *li;
10380 dict_T *d;
10381 list_T *s = NULL;
10382
10383 rettv->vval.v_number = -1;
10384 if (argvars[0].v_type != VAR_LIST)
10385 {
10386 EMSG(_(e_listreq));
10387 return;
10388 }
10389 if ((l = argvars[0].vval.v_list) != NULL)
10390 {
10391
10392 /* To some extent make sure that we are dealing with a list from
10393 * "getmatches()". */
10394 li = l->lv_first;
10395 while (li != NULL)
10396 {
10397 if (li->li_tv.v_type != VAR_DICT
10398 || (d = li->li_tv.vval.v_dict) == NULL)
10399 {
10400 EMSG(_(e_invarg));
10401 return;
10402 }
10403 if (!(dict_find(d, (char_u *)"group", -1) != NULL
10404 && (dict_find(d, (char_u *)"pattern", -1) != NULL
10405 || dict_find(d, (char_u *)"pos1", -1) != NULL)
10406 && dict_find(d, (char_u *)"priority", -1) != NULL
10407 && dict_find(d, (char_u *)"id", -1) != NULL))
10408 {
10409 EMSG(_(e_invarg));
10410 return;
10411 }
10412 li = li->li_next;
10413 }
10414
10415 clear_matches(curwin);
10416 li = l->lv_first;
10417 while (li != NULL)
10418 {
10419 int i = 0;
10420 char_u buf[5];
10421 dictitem_T *di;
10422 char_u *group;
10423 int priority;
10424 int id;
10425 char_u *conceal;
10426
10427 d = li->li_tv.vval.v_dict;
10428 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
10429 {
10430 if (s == NULL)
10431 {
10432 s = list_alloc();
10433 if (s == NULL)
10434 return;
10435 }
10436
10437 /* match from matchaddpos() */
10438 for (i = 1; i < 9; i++)
10439 {
10440 sprintf((char *)buf, (char *)"pos%d", i);
10441 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
10442 {
10443 if (di->di_tv.v_type != VAR_LIST)
10444 return;
10445
10446 list_append_tv(s, &di->di_tv);
10447 s->lv_refcount++;
10448 }
10449 else
10450 break;
10451 }
10452 }
10453
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010454 group = get_dict_string(d, (char_u *)"group", TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010455 priority = (int)get_dict_number(d, (char_u *)"priority");
10456 id = (int)get_dict_number(d, (char_u *)"id");
10457 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010458 ? get_dict_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010459 : NULL;
10460 if (i == 0)
10461 {
10462 match_add(curwin, group,
10463 get_dict_string(d, (char_u *)"pattern", FALSE),
10464 priority, id, NULL, conceal);
10465 }
10466 else
10467 {
10468 match_add(curwin, group, NULL, priority, id, s, conceal);
10469 list_unref(s);
10470 s = NULL;
10471 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010472 vim_free(group);
10473 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010474
10475 li = li->li_next;
10476 }
10477 rettv->vval.v_number = 0;
10478 }
10479#endif
10480}
10481
10482/*
10483 * "setpos()" function
10484 */
10485 static void
10486f_setpos(typval_T *argvars, typval_T *rettv)
10487{
10488 pos_T pos;
10489 int fnum;
10490 char_u *name;
10491 colnr_T curswant = -1;
10492
10493 rettv->vval.v_number = -1;
10494 name = get_tv_string_chk(argvars);
10495 if (name != NULL)
10496 {
10497 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10498 {
10499 if (--pos.col < 0)
10500 pos.col = 0;
10501 if (name[0] == '.' && name[1] == NUL)
10502 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010503 /* set cursor; "fnum" is ignored */
10504 curwin->w_cursor = pos;
10505 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010506 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010507 curwin->w_curswant = curswant - 1;
10508 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010509 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010510 check_cursor();
10511 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010512 }
10513 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10514 {
10515 /* set mark */
10516 if (setmark_pos(name[1], &pos, fnum) == OK)
10517 rettv->vval.v_number = 0;
10518 }
10519 else
10520 EMSG(_(e_invarg));
10521 }
10522 }
10523}
10524
10525/*
10526 * "setqflist()" function
10527 */
10528 static void
10529f_setqflist(typval_T *argvars, typval_T *rettv)
10530{
Bram Moolenaard823fa92016-08-12 16:29:27 +020010531 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010532}
10533
10534/*
10535 * "setreg()" function
10536 */
10537 static void
10538f_setreg(typval_T *argvars, typval_T *rettv)
10539{
10540 int regname;
10541 char_u *strregname;
10542 char_u *stropt;
10543 char_u *strval;
10544 int append;
10545 char_u yank_type;
10546 long block_len;
10547
10548 block_len = -1;
10549 yank_type = MAUTO;
10550 append = FALSE;
10551
10552 strregname = get_tv_string_chk(argvars);
10553 rettv->vval.v_number = 1; /* FAIL is default */
10554
10555 if (strregname == NULL)
10556 return; /* type error; errmsg already given */
10557 regname = *strregname;
10558 if (regname == 0 || regname == '@')
10559 regname = '"';
10560
10561 if (argvars[2].v_type != VAR_UNKNOWN)
10562 {
10563 stropt = get_tv_string_chk(&argvars[2]);
10564 if (stropt == NULL)
10565 return; /* type error */
10566 for (; *stropt != NUL; ++stropt)
10567 switch (*stropt)
10568 {
10569 case 'a': case 'A': /* append */
10570 append = TRUE;
10571 break;
10572 case 'v': case 'c': /* character-wise selection */
10573 yank_type = MCHAR;
10574 break;
10575 case 'V': case 'l': /* line-wise selection */
10576 yank_type = MLINE;
10577 break;
10578 case 'b': case Ctrl_V: /* block-wise selection */
10579 yank_type = MBLOCK;
10580 if (VIM_ISDIGIT(stropt[1]))
10581 {
10582 ++stropt;
10583 block_len = getdigits(&stropt) - 1;
10584 --stropt;
10585 }
10586 break;
10587 }
10588 }
10589
10590 if (argvars[1].v_type == VAR_LIST)
10591 {
10592 char_u **lstval;
10593 char_u **allocval;
10594 char_u buf[NUMBUFLEN];
10595 char_u **curval;
10596 char_u **curallocval;
10597 list_T *ll = argvars[1].vval.v_list;
10598 listitem_T *li;
10599 int len;
10600
10601 /* If the list is NULL handle like an empty list. */
10602 len = ll == NULL ? 0 : ll->lv_len;
10603
10604 /* First half: use for pointers to result lines; second half: use for
10605 * pointers to allocated copies. */
10606 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
10607 if (lstval == NULL)
10608 return;
10609 curval = lstval;
10610 allocval = lstval + len + 2;
10611 curallocval = allocval;
10612
10613 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
10614 li = li->li_next)
10615 {
10616 strval = get_tv_string_buf_chk(&li->li_tv, buf);
10617 if (strval == NULL)
10618 goto free_lstval;
10619 if (strval == buf)
10620 {
10621 /* Need to make a copy, next get_tv_string_buf_chk() will
10622 * overwrite the string. */
10623 strval = vim_strsave(buf);
10624 if (strval == NULL)
10625 goto free_lstval;
10626 *curallocval++ = strval;
10627 }
10628 *curval++ = strval;
10629 }
10630 *curval++ = NULL;
10631
10632 write_reg_contents_lst(regname, lstval, -1,
10633 append, yank_type, block_len);
10634free_lstval:
10635 while (curallocval > allocval)
10636 vim_free(*--curallocval);
10637 vim_free(lstval);
10638 }
10639 else
10640 {
10641 strval = get_tv_string_chk(&argvars[1]);
10642 if (strval == NULL)
10643 return;
10644 write_reg_contents_ex(regname, strval, -1,
10645 append, yank_type, block_len);
10646 }
10647 rettv->vval.v_number = 0;
10648}
10649
10650/*
10651 * "settabvar()" function
10652 */
10653 static void
10654f_settabvar(typval_T *argvars, typval_T *rettv)
10655{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010656 tabpage_T *save_curtab;
10657 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010658 char_u *varname, *tabvarname;
10659 typval_T *varp;
10660
10661 rettv->vval.v_number = 0;
10662
10663 if (check_restricted() || check_secure())
10664 return;
10665
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010666 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010667 varname = get_tv_string_chk(&argvars[1]);
10668 varp = &argvars[2];
10669
Bram Moolenaar4033c552017-09-16 20:54:51 +020010670 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010671 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010672 save_curtab = curtab;
10673 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010674
10675 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
10676 if (tabvarname != NULL)
10677 {
10678 STRCPY(tabvarname, "t:");
10679 STRCPY(tabvarname + 2, varname);
10680 set_var(tabvarname, varp, TRUE);
10681 vim_free(tabvarname);
10682 }
10683
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010684 /* Restore current tabpage */
10685 if (valid_tabpage(save_curtab))
10686 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010687 }
10688}
10689
10690/*
10691 * "settabwinvar()" function
10692 */
10693 static void
10694f_settabwinvar(typval_T *argvars, typval_T *rettv)
10695{
10696 setwinvar(argvars, rettv, 1);
10697}
10698
10699/*
10700 * "setwinvar()" function
10701 */
10702 static void
10703f_setwinvar(typval_T *argvars, typval_T *rettv)
10704{
10705 setwinvar(argvars, rettv, 0);
10706}
10707
10708#ifdef FEAT_CRYPT
10709/*
10710 * "sha256({string})" function
10711 */
10712 static void
10713f_sha256(typval_T *argvars, typval_T *rettv)
10714{
10715 char_u *p;
10716
10717 p = get_tv_string(&argvars[0]);
10718 rettv->vval.v_string = vim_strsave(
10719 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
10720 rettv->v_type = VAR_STRING;
10721}
10722#endif /* FEAT_CRYPT */
10723
10724/*
10725 * "shellescape({string})" function
10726 */
10727 static void
10728f_shellescape(typval_T *argvars, typval_T *rettv)
10729{
Bram Moolenaar20615522017-06-05 18:46:26 +020010730 int do_special = non_zero_arg(&argvars[1]);
10731
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010732 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar20615522017-06-05 18:46:26 +020010733 get_tv_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010734 rettv->v_type = VAR_STRING;
10735}
10736
10737/*
10738 * shiftwidth() function
10739 */
10740 static void
10741f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
10742{
10743 rettv->vval.v_number = get_sw_value(curbuf);
10744}
10745
10746/*
10747 * "simplify()" function
10748 */
10749 static void
10750f_simplify(typval_T *argvars, typval_T *rettv)
10751{
10752 char_u *p;
10753
10754 p = get_tv_string(&argvars[0]);
10755 rettv->vval.v_string = vim_strsave(p);
10756 simplify_filename(rettv->vval.v_string); /* simplify in place */
10757 rettv->v_type = VAR_STRING;
10758}
10759
10760#ifdef FEAT_FLOAT
10761/*
10762 * "sin()" function
10763 */
10764 static void
10765f_sin(typval_T *argvars, typval_T *rettv)
10766{
10767 float_T f = 0.0;
10768
10769 rettv->v_type = VAR_FLOAT;
10770 if (get_float_arg(argvars, &f) == OK)
10771 rettv->vval.v_float = sin(f);
10772 else
10773 rettv->vval.v_float = 0.0;
10774}
10775
10776/*
10777 * "sinh()" function
10778 */
10779 static void
10780f_sinh(typval_T *argvars, typval_T *rettv)
10781{
10782 float_T f = 0.0;
10783
10784 rettv->v_type = VAR_FLOAT;
10785 if (get_float_arg(argvars, &f) == OK)
10786 rettv->vval.v_float = sinh(f);
10787 else
10788 rettv->vval.v_float = 0.0;
10789}
10790#endif
10791
10792static int
10793#ifdef __BORLANDC__
10794 _RTLENTRYF
10795#endif
10796 item_compare(const void *s1, const void *s2);
10797static int
10798#ifdef __BORLANDC__
10799 _RTLENTRYF
10800#endif
10801 item_compare2(const void *s1, const void *s2);
10802
10803/* struct used in the array that's given to qsort() */
10804typedef struct
10805{
10806 listitem_T *item;
10807 int idx;
10808} sortItem_T;
10809
10810/* struct storing information about current sort */
10811typedef struct
10812{
10813 int item_compare_ic;
10814 int item_compare_numeric;
10815 int item_compare_numbers;
10816#ifdef FEAT_FLOAT
10817 int item_compare_float;
10818#endif
10819 char_u *item_compare_func;
10820 partial_T *item_compare_partial;
10821 dict_T *item_compare_selfdict;
10822 int item_compare_func_err;
10823 int item_compare_keep_zero;
10824} sortinfo_T;
10825static sortinfo_T *sortinfo = NULL;
10826static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
10827#define ITEM_COMPARE_FAIL 999
10828
10829/*
10830 * Compare functions for f_sort() and f_uniq() below.
10831 */
10832 static int
10833#ifdef __BORLANDC__
10834_RTLENTRYF
10835#endif
10836item_compare(const void *s1, const void *s2)
10837{
10838 sortItem_T *si1, *si2;
10839 typval_T *tv1, *tv2;
10840 char_u *p1, *p2;
10841 char_u *tofree1 = NULL, *tofree2 = NULL;
10842 int res;
10843 char_u numbuf1[NUMBUFLEN];
10844 char_u numbuf2[NUMBUFLEN];
10845
10846 si1 = (sortItem_T *)s1;
10847 si2 = (sortItem_T *)s2;
10848 tv1 = &si1->item->li_tv;
10849 tv2 = &si2->item->li_tv;
10850
10851 if (sortinfo->item_compare_numbers)
10852 {
10853 varnumber_T v1 = get_tv_number(tv1);
10854 varnumber_T v2 = get_tv_number(tv2);
10855
10856 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10857 }
10858
10859#ifdef FEAT_FLOAT
10860 if (sortinfo->item_compare_float)
10861 {
10862 float_T v1 = get_tv_float(tv1);
10863 float_T v2 = get_tv_float(tv2);
10864
10865 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10866 }
10867#endif
10868
10869 /* tv2string() puts quotes around a string and allocates memory. Don't do
10870 * that for string variables. Use a single quote when comparing with a
10871 * non-string to do what the docs promise. */
10872 if (tv1->v_type == VAR_STRING)
10873 {
10874 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
10875 p1 = (char_u *)"'";
10876 else
10877 p1 = tv1->vval.v_string;
10878 }
10879 else
10880 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
10881 if (tv2->v_type == VAR_STRING)
10882 {
10883 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
10884 p2 = (char_u *)"'";
10885 else
10886 p2 = tv2->vval.v_string;
10887 }
10888 else
10889 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
10890 if (p1 == NULL)
10891 p1 = (char_u *)"";
10892 if (p2 == NULL)
10893 p2 = (char_u *)"";
10894 if (!sortinfo->item_compare_numeric)
10895 {
10896 if (sortinfo->item_compare_ic)
10897 res = STRICMP(p1, p2);
10898 else
10899 res = STRCMP(p1, p2);
10900 }
10901 else
10902 {
10903 double n1, n2;
10904 n1 = strtod((char *)p1, (char **)&p1);
10905 n2 = strtod((char *)p2, (char **)&p2);
10906 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
10907 }
10908
10909 /* When the result would be zero, compare the item indexes. Makes the
10910 * sort stable. */
10911 if (res == 0 && !sortinfo->item_compare_keep_zero)
10912 res = si1->idx > si2->idx ? 1 : -1;
10913
10914 vim_free(tofree1);
10915 vim_free(tofree2);
10916 return res;
10917}
10918
10919 static int
10920#ifdef __BORLANDC__
10921_RTLENTRYF
10922#endif
10923item_compare2(const void *s1, const void *s2)
10924{
10925 sortItem_T *si1, *si2;
10926 int res;
10927 typval_T rettv;
10928 typval_T argv[3];
10929 int dummy;
10930 char_u *func_name;
10931 partial_T *partial = sortinfo->item_compare_partial;
10932
10933 /* shortcut after failure in previous call; compare all items equal */
10934 if (sortinfo->item_compare_func_err)
10935 return 0;
10936
10937 si1 = (sortItem_T *)s1;
10938 si2 = (sortItem_T *)s2;
10939
10940 if (partial == NULL)
10941 func_name = sortinfo->item_compare_func;
10942 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020010943 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010944
10945 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
10946 * in the copy without changing the original list items. */
10947 copy_tv(&si1->item->li_tv, &argv[0]);
10948 copy_tv(&si2->item->li_tv, &argv[1]);
10949
10950 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
10951 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020010952 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010953 partial, sortinfo->item_compare_selfdict);
10954 clear_tv(&argv[0]);
10955 clear_tv(&argv[1]);
10956
10957 if (res == FAIL)
10958 res = ITEM_COMPARE_FAIL;
10959 else
10960 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
10961 if (sortinfo->item_compare_func_err)
10962 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
10963 clear_tv(&rettv);
10964
10965 /* When the result would be zero, compare the pointers themselves. Makes
10966 * the sort stable. */
10967 if (res == 0 && !sortinfo->item_compare_keep_zero)
10968 res = si1->idx > si2->idx ? 1 : -1;
10969
10970 return res;
10971}
10972
10973/*
10974 * "sort({list})" function
10975 */
10976 static void
10977do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
10978{
10979 list_T *l;
10980 listitem_T *li;
10981 sortItem_T *ptrs;
10982 sortinfo_T *old_sortinfo;
10983 sortinfo_T info;
10984 long len;
10985 long i;
10986
10987 /* Pointer to current info struct used in compare function. Save and
10988 * restore the current one for nested calls. */
10989 old_sortinfo = sortinfo;
10990 sortinfo = &info;
10991
10992 if (argvars[0].v_type != VAR_LIST)
10993 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
10994 else
10995 {
10996 l = argvars[0].vval.v_list;
10997 if (l == NULL || tv_check_lock(l->lv_lock,
10998 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
10999 TRUE))
11000 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011001 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011002
11003 len = list_len(l);
11004 if (len <= 1)
11005 goto theend; /* short list sorts pretty quickly */
11006
11007 info.item_compare_ic = FALSE;
11008 info.item_compare_numeric = FALSE;
11009 info.item_compare_numbers = FALSE;
11010#ifdef FEAT_FLOAT
11011 info.item_compare_float = FALSE;
11012#endif
11013 info.item_compare_func = NULL;
11014 info.item_compare_partial = NULL;
11015 info.item_compare_selfdict = NULL;
11016 if (argvars[1].v_type != VAR_UNKNOWN)
11017 {
11018 /* optional second argument: {func} */
11019 if (argvars[1].v_type == VAR_FUNC)
11020 info.item_compare_func = argvars[1].vval.v_string;
11021 else if (argvars[1].v_type == VAR_PARTIAL)
11022 info.item_compare_partial = argvars[1].vval.v_partial;
11023 else
11024 {
11025 int error = FALSE;
11026
11027 i = (long)get_tv_number_chk(&argvars[1], &error);
11028 if (error)
11029 goto theend; /* type error; errmsg already given */
11030 if (i == 1)
11031 info.item_compare_ic = TRUE;
11032 else if (argvars[1].v_type != VAR_NUMBER)
11033 info.item_compare_func = get_tv_string(&argvars[1]);
11034 else if (i != 0)
11035 {
11036 EMSG(_(e_invarg));
11037 goto theend;
11038 }
11039 if (info.item_compare_func != NULL)
11040 {
11041 if (*info.item_compare_func == NUL)
11042 {
11043 /* empty string means default sort */
11044 info.item_compare_func = NULL;
11045 }
11046 else if (STRCMP(info.item_compare_func, "n") == 0)
11047 {
11048 info.item_compare_func = NULL;
11049 info.item_compare_numeric = TRUE;
11050 }
11051 else if (STRCMP(info.item_compare_func, "N") == 0)
11052 {
11053 info.item_compare_func = NULL;
11054 info.item_compare_numbers = TRUE;
11055 }
11056#ifdef FEAT_FLOAT
11057 else if (STRCMP(info.item_compare_func, "f") == 0)
11058 {
11059 info.item_compare_func = NULL;
11060 info.item_compare_float = TRUE;
11061 }
11062#endif
11063 else if (STRCMP(info.item_compare_func, "i") == 0)
11064 {
11065 info.item_compare_func = NULL;
11066 info.item_compare_ic = TRUE;
11067 }
11068 }
11069 }
11070
11071 if (argvars[2].v_type != VAR_UNKNOWN)
11072 {
11073 /* optional third argument: {dict} */
11074 if (argvars[2].v_type != VAR_DICT)
11075 {
11076 EMSG(_(e_dictreq));
11077 goto theend;
11078 }
11079 info.item_compare_selfdict = argvars[2].vval.v_dict;
11080 }
11081 }
11082
11083 /* Make an array with each entry pointing to an item in the List. */
11084 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
11085 if (ptrs == NULL)
11086 goto theend;
11087
11088 i = 0;
11089 if (sort)
11090 {
11091 /* sort(): ptrs will be the list to sort */
11092 for (li = l->lv_first; li != NULL; li = li->li_next)
11093 {
11094 ptrs[i].item = li;
11095 ptrs[i].idx = i;
11096 ++i;
11097 }
11098
11099 info.item_compare_func_err = FALSE;
11100 info.item_compare_keep_zero = FALSE;
11101 /* test the compare function */
11102 if ((info.item_compare_func != NULL
11103 || info.item_compare_partial != NULL)
11104 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11105 == ITEM_COMPARE_FAIL)
11106 EMSG(_("E702: Sort compare function failed"));
11107 else
11108 {
11109 /* Sort the array with item pointers. */
11110 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
11111 info.item_compare_func == NULL
11112 && info.item_compare_partial == NULL
11113 ? item_compare : item_compare2);
11114
11115 if (!info.item_compare_func_err)
11116 {
11117 /* Clear the List and append the items in sorted order. */
11118 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
11119 l->lv_len = 0;
11120 for (i = 0; i < len; ++i)
11121 list_append(l, ptrs[i].item);
11122 }
11123 }
11124 }
11125 else
11126 {
11127 int (*item_compare_func_ptr)(const void *, const void *);
11128
11129 /* f_uniq(): ptrs will be a stack of items to remove */
11130 info.item_compare_func_err = FALSE;
11131 info.item_compare_keep_zero = TRUE;
11132 item_compare_func_ptr = info.item_compare_func != NULL
11133 || info.item_compare_partial != NULL
11134 ? item_compare2 : item_compare;
11135
11136 for (li = l->lv_first; li != NULL && li->li_next != NULL;
11137 li = li->li_next)
11138 {
11139 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
11140 == 0)
11141 ptrs[i++].item = li;
11142 if (info.item_compare_func_err)
11143 {
11144 EMSG(_("E882: Uniq compare function failed"));
11145 break;
11146 }
11147 }
11148
11149 if (!info.item_compare_func_err)
11150 {
11151 while (--i >= 0)
11152 {
11153 li = ptrs[i].item->li_next;
11154 ptrs[i].item->li_next = li->li_next;
11155 if (li->li_next != NULL)
11156 li->li_next->li_prev = ptrs[i].item;
11157 else
11158 l->lv_last = ptrs[i].item;
11159 list_fix_watch(l, li);
11160 listitem_free(li);
11161 l->lv_len--;
11162 }
11163 }
11164 }
11165
11166 vim_free(ptrs);
11167 }
11168theend:
11169 sortinfo = old_sortinfo;
11170}
11171
11172/*
11173 * "sort({list})" function
11174 */
11175 static void
11176f_sort(typval_T *argvars, typval_T *rettv)
11177{
11178 do_sort_uniq(argvars, rettv, TRUE);
11179}
11180
11181/*
11182 * "uniq({list})" function
11183 */
11184 static void
11185f_uniq(typval_T *argvars, typval_T *rettv)
11186{
11187 do_sort_uniq(argvars, rettv, FALSE);
11188}
11189
11190/*
11191 * "soundfold({word})" function
11192 */
11193 static void
11194f_soundfold(typval_T *argvars, typval_T *rettv)
11195{
11196 char_u *s;
11197
11198 rettv->v_type = VAR_STRING;
11199 s = get_tv_string(&argvars[0]);
11200#ifdef FEAT_SPELL
11201 rettv->vval.v_string = eval_soundfold(s);
11202#else
11203 rettv->vval.v_string = vim_strsave(s);
11204#endif
11205}
11206
11207/*
11208 * "spellbadword()" function
11209 */
11210 static void
11211f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
11212{
11213 char_u *word = (char_u *)"";
11214 hlf_T attr = HLF_COUNT;
11215 int len = 0;
11216
11217 if (rettv_list_alloc(rettv) == FAIL)
11218 return;
11219
11220#ifdef FEAT_SPELL
11221 if (argvars[0].v_type == VAR_UNKNOWN)
11222 {
11223 /* Find the start and length of the badly spelled word. */
11224 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
11225 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011226 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011227 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011228 curwin->w_set_curswant = TRUE;
11229 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011230 }
11231 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
11232 {
11233 char_u *str = get_tv_string_chk(&argvars[0]);
11234 int capcol = -1;
11235
11236 if (str != NULL)
11237 {
11238 /* Check the argument for spelling. */
11239 while (*str != NUL)
11240 {
11241 len = spell_check(curwin, str, &attr, &capcol, FALSE);
11242 if (attr != HLF_COUNT)
11243 {
11244 word = str;
11245 break;
11246 }
11247 str += len;
11248 }
11249 }
11250 }
11251#endif
11252
11253 list_append_string(rettv->vval.v_list, word, len);
11254 list_append_string(rettv->vval.v_list, (char_u *)(
11255 attr == HLF_SPB ? "bad" :
11256 attr == HLF_SPR ? "rare" :
11257 attr == HLF_SPL ? "local" :
11258 attr == HLF_SPC ? "caps" :
11259 ""), -1);
11260}
11261
11262/*
11263 * "spellsuggest()" function
11264 */
11265 static void
11266f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
11267{
11268#ifdef FEAT_SPELL
11269 char_u *str;
11270 int typeerr = FALSE;
11271 int maxcount;
11272 garray_T ga;
11273 int i;
11274 listitem_T *li;
11275 int need_capital = FALSE;
11276#endif
11277
11278 if (rettv_list_alloc(rettv) == FAIL)
11279 return;
11280
11281#ifdef FEAT_SPELL
11282 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
11283 {
11284 str = get_tv_string(&argvars[0]);
11285 if (argvars[1].v_type != VAR_UNKNOWN)
11286 {
11287 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
11288 if (maxcount <= 0)
11289 return;
11290 if (argvars[2].v_type != VAR_UNKNOWN)
11291 {
11292 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
11293 if (typeerr)
11294 return;
11295 }
11296 }
11297 else
11298 maxcount = 25;
11299
11300 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
11301
11302 for (i = 0; i < ga.ga_len; ++i)
11303 {
11304 str = ((char_u **)ga.ga_data)[i];
11305
11306 li = listitem_alloc();
11307 if (li == NULL)
11308 vim_free(str);
11309 else
11310 {
11311 li->li_tv.v_type = VAR_STRING;
11312 li->li_tv.v_lock = 0;
11313 li->li_tv.vval.v_string = str;
11314 list_append(rettv->vval.v_list, li);
11315 }
11316 }
11317 ga_clear(&ga);
11318 }
11319#endif
11320}
11321
11322 static void
11323f_split(typval_T *argvars, typval_T *rettv)
11324{
11325 char_u *str;
11326 char_u *end;
11327 char_u *pat = NULL;
11328 regmatch_T regmatch;
11329 char_u patbuf[NUMBUFLEN];
11330 char_u *save_cpo;
11331 int match;
11332 colnr_T col = 0;
11333 int keepempty = FALSE;
11334 int typeerr = FALSE;
11335
11336 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11337 save_cpo = p_cpo;
11338 p_cpo = (char_u *)"";
11339
11340 str = get_tv_string(&argvars[0]);
11341 if (argvars[1].v_type != VAR_UNKNOWN)
11342 {
11343 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11344 if (pat == NULL)
11345 typeerr = TRUE;
11346 if (argvars[2].v_type != VAR_UNKNOWN)
11347 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
11348 }
11349 if (pat == NULL || *pat == NUL)
11350 pat = (char_u *)"[\\x01- ]\\+";
11351
11352 if (rettv_list_alloc(rettv) == FAIL)
11353 return;
11354 if (typeerr)
11355 return;
11356
11357 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11358 if (regmatch.regprog != NULL)
11359 {
11360 regmatch.rm_ic = FALSE;
11361 while (*str != NUL || keepempty)
11362 {
11363 if (*str == NUL)
11364 match = FALSE; /* empty item at the end */
11365 else
11366 match = vim_regexec_nl(&regmatch, str, col);
11367 if (match)
11368 end = regmatch.startp[0];
11369 else
11370 end = str + STRLEN(str);
11371 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
11372 && *str != NUL && match && end < regmatch.endp[0]))
11373 {
11374 if (list_append_string(rettv->vval.v_list, str,
11375 (int)(end - str)) == FAIL)
11376 break;
11377 }
11378 if (!match)
11379 break;
11380 /* Advance to just after the match. */
11381 if (regmatch.endp[0] > str)
11382 col = 0;
11383 else
11384 {
11385 /* Don't get stuck at the same match. */
11386#ifdef FEAT_MBYTE
11387 col = (*mb_ptr2len)(regmatch.endp[0]);
11388#else
11389 col = 1;
11390#endif
11391 }
11392 str = regmatch.endp[0];
11393 }
11394
11395 vim_regfree(regmatch.regprog);
11396 }
11397
11398 p_cpo = save_cpo;
11399}
11400
11401#ifdef FEAT_FLOAT
11402/*
11403 * "sqrt()" function
11404 */
11405 static void
11406f_sqrt(typval_T *argvars, typval_T *rettv)
11407{
11408 float_T f = 0.0;
11409
11410 rettv->v_type = VAR_FLOAT;
11411 if (get_float_arg(argvars, &f) == OK)
11412 rettv->vval.v_float = sqrt(f);
11413 else
11414 rettv->vval.v_float = 0.0;
11415}
11416
11417/*
11418 * "str2float()" function
11419 */
11420 static void
11421f_str2float(typval_T *argvars, typval_T *rettv)
11422{
11423 char_u *p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011424 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011425
Bram Moolenaar08243d22017-01-10 16:12:29 +010011426 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011427 p = skipwhite(p + 1);
11428 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011429 if (isneg)
11430 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011431 rettv->v_type = VAR_FLOAT;
11432}
11433#endif
11434
11435/*
11436 * "str2nr()" function
11437 */
11438 static void
11439f_str2nr(typval_T *argvars, typval_T *rettv)
11440{
11441 int base = 10;
11442 char_u *p;
11443 varnumber_T n;
11444 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010011445 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011446
11447 if (argvars[1].v_type != VAR_UNKNOWN)
11448 {
11449 base = (int)get_tv_number(&argvars[1]);
11450 if (base != 2 && base != 8 && base != 10 && base != 16)
11451 {
11452 EMSG(_(e_invarg));
11453 return;
11454 }
11455 }
11456
11457 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011458 isneg = (*p == '-');
11459 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011460 p = skipwhite(p + 1);
11461 switch (base)
11462 {
11463 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
11464 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
11465 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
11466 default: what = 0;
11467 }
11468 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011469 if (isneg)
11470 rettv->vval.v_number = -n;
11471 else
11472 rettv->vval.v_number = n;
11473
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011474}
11475
11476#ifdef HAVE_STRFTIME
11477/*
11478 * "strftime({format}[, {time}])" function
11479 */
11480 static void
11481f_strftime(typval_T *argvars, typval_T *rettv)
11482{
11483 char_u result_buf[256];
11484 struct tm *curtime;
11485 time_t seconds;
11486 char_u *p;
11487
11488 rettv->v_type = VAR_STRING;
11489
11490 p = get_tv_string(&argvars[0]);
11491 if (argvars[1].v_type == VAR_UNKNOWN)
11492 seconds = time(NULL);
11493 else
11494 seconds = (time_t)get_tv_number(&argvars[1]);
11495 curtime = localtime(&seconds);
11496 /* MSVC returns NULL for an invalid value of seconds. */
11497 if (curtime == NULL)
11498 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
11499 else
11500 {
11501# ifdef FEAT_MBYTE
11502 vimconv_T conv;
11503 char_u *enc;
11504
11505 conv.vc_type = CONV_NONE;
11506 enc = enc_locale();
11507 convert_setup(&conv, p_enc, enc);
11508 if (conv.vc_type != CONV_NONE)
11509 p = string_convert(&conv, p, NULL);
11510# endif
11511 if (p != NULL)
11512 (void)strftime((char *)result_buf, sizeof(result_buf),
11513 (char *)p, curtime);
11514 else
11515 result_buf[0] = NUL;
11516
11517# ifdef FEAT_MBYTE
11518 if (conv.vc_type != CONV_NONE)
11519 vim_free(p);
11520 convert_setup(&conv, enc, p_enc);
11521 if (conv.vc_type != CONV_NONE)
11522 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
11523 else
11524# endif
11525 rettv->vval.v_string = vim_strsave(result_buf);
11526
11527# ifdef FEAT_MBYTE
11528 /* Release conversion descriptors */
11529 convert_setup(&conv, NULL, NULL);
11530 vim_free(enc);
11531# endif
11532 }
11533}
11534#endif
11535
11536/*
11537 * "strgetchar()" function
11538 */
11539 static void
11540f_strgetchar(typval_T *argvars, typval_T *rettv)
11541{
11542 char_u *str;
11543 int len;
11544 int error = FALSE;
11545 int charidx;
11546
11547 rettv->vval.v_number = -1;
11548 str = get_tv_string_chk(&argvars[0]);
11549 if (str == NULL)
11550 return;
11551 len = (int)STRLEN(str);
11552 charidx = (int)get_tv_number_chk(&argvars[1], &error);
11553 if (error)
11554 return;
11555#ifdef FEAT_MBYTE
11556 {
11557 int byteidx = 0;
11558
11559 while (charidx >= 0 && byteidx < len)
11560 {
11561 if (charidx == 0)
11562 {
11563 rettv->vval.v_number = mb_ptr2char(str + byteidx);
11564 break;
11565 }
11566 --charidx;
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011567 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011568 }
11569 }
11570#else
11571 if (charidx < len)
11572 rettv->vval.v_number = str[charidx];
11573#endif
11574}
11575
11576/*
11577 * "stridx()" function
11578 */
11579 static void
11580f_stridx(typval_T *argvars, typval_T *rettv)
11581{
11582 char_u buf[NUMBUFLEN];
11583 char_u *needle;
11584 char_u *haystack;
11585 char_u *save_haystack;
11586 char_u *pos;
11587 int start_idx;
11588
11589 needle = get_tv_string_chk(&argvars[1]);
11590 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
11591 rettv->vval.v_number = -1;
11592 if (needle == NULL || haystack == NULL)
11593 return; /* type error; errmsg already given */
11594
11595 if (argvars[2].v_type != VAR_UNKNOWN)
11596 {
11597 int error = FALSE;
11598
11599 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
11600 if (error || start_idx >= (int)STRLEN(haystack))
11601 return;
11602 if (start_idx >= 0)
11603 haystack += start_idx;
11604 }
11605
11606 pos = (char_u *)strstr((char *)haystack, (char *)needle);
11607 if (pos != NULL)
11608 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
11609}
11610
11611/*
11612 * "string()" function
11613 */
11614 static void
11615f_string(typval_T *argvars, typval_T *rettv)
11616{
11617 char_u *tofree;
11618 char_u numbuf[NUMBUFLEN];
11619
11620 rettv->v_type = VAR_STRING;
11621 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
11622 get_copyID());
11623 /* Make a copy if we have a value but it's not in allocated memory. */
11624 if (rettv->vval.v_string != NULL && tofree == NULL)
11625 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
11626}
11627
11628/*
11629 * "strlen()" function
11630 */
11631 static void
11632f_strlen(typval_T *argvars, typval_T *rettv)
11633{
11634 rettv->vval.v_number = (varnumber_T)(STRLEN(
11635 get_tv_string(&argvars[0])));
11636}
11637
11638/*
11639 * "strchars()" function
11640 */
11641 static void
11642f_strchars(typval_T *argvars, typval_T *rettv)
11643{
11644 char_u *s = get_tv_string(&argvars[0]);
11645 int skipcc = 0;
11646#ifdef FEAT_MBYTE
11647 varnumber_T len = 0;
11648 int (*func_mb_ptr2char_adv)(char_u **pp);
11649#endif
11650
11651 if (argvars[1].v_type != VAR_UNKNOWN)
11652 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
11653 if (skipcc < 0 || skipcc > 1)
11654 EMSG(_(e_invarg));
11655 else
11656 {
11657#ifdef FEAT_MBYTE
11658 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
11659 while (*s != NUL)
11660 {
11661 func_mb_ptr2char_adv(&s);
11662 ++len;
11663 }
11664 rettv->vval.v_number = len;
11665#else
11666 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
11667#endif
11668 }
11669}
11670
11671/*
11672 * "strdisplaywidth()" function
11673 */
11674 static void
11675f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
11676{
11677 char_u *s = get_tv_string(&argvars[0]);
11678 int col = 0;
11679
11680 if (argvars[1].v_type != VAR_UNKNOWN)
11681 col = (int)get_tv_number(&argvars[1]);
11682
11683 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
11684}
11685
11686/*
11687 * "strwidth()" function
11688 */
11689 static void
11690f_strwidth(typval_T *argvars, typval_T *rettv)
11691{
11692 char_u *s = get_tv_string(&argvars[0]);
11693
11694 rettv->vval.v_number = (varnumber_T)(
11695#ifdef FEAT_MBYTE
11696 mb_string2cells(s, -1)
11697#else
11698 STRLEN(s)
11699#endif
11700 );
11701}
11702
11703/*
11704 * "strcharpart()" function
11705 */
11706 static void
11707f_strcharpart(typval_T *argvars, typval_T *rettv)
11708{
11709#ifdef FEAT_MBYTE
11710 char_u *p;
11711 int nchar;
11712 int nbyte = 0;
11713 int charlen;
11714 int len = 0;
11715 int slen;
11716 int error = FALSE;
11717
11718 p = get_tv_string(&argvars[0]);
11719 slen = (int)STRLEN(p);
11720
11721 nchar = (int)get_tv_number_chk(&argvars[1], &error);
11722 if (!error)
11723 {
11724 if (nchar > 0)
11725 while (nchar > 0 && nbyte < slen)
11726 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011727 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011728 --nchar;
11729 }
11730 else
11731 nbyte = nchar;
11732 if (argvars[2].v_type != VAR_UNKNOWN)
11733 {
11734 charlen = (int)get_tv_number(&argvars[2]);
11735 while (charlen > 0 && nbyte + len < slen)
11736 {
11737 int off = nbyte + len;
11738
11739 if (off < 0)
11740 len += 1;
11741 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011742 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011743 --charlen;
11744 }
11745 }
11746 else
11747 len = slen - nbyte; /* default: all bytes that are available. */
11748 }
11749
11750 /*
11751 * Only return the overlap between the specified part and the actual
11752 * string.
11753 */
11754 if (nbyte < 0)
11755 {
11756 len += nbyte;
11757 nbyte = 0;
11758 }
11759 else if (nbyte > slen)
11760 nbyte = slen;
11761 if (len < 0)
11762 len = 0;
11763 else if (nbyte + len > slen)
11764 len = slen - nbyte;
11765
11766 rettv->v_type = VAR_STRING;
11767 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
11768#else
11769 f_strpart(argvars, rettv);
11770#endif
11771}
11772
11773/*
11774 * "strpart()" function
11775 */
11776 static void
11777f_strpart(typval_T *argvars, typval_T *rettv)
11778{
11779 char_u *p;
11780 int n;
11781 int len;
11782 int slen;
11783 int error = FALSE;
11784
11785 p = get_tv_string(&argvars[0]);
11786 slen = (int)STRLEN(p);
11787
11788 n = (int)get_tv_number_chk(&argvars[1], &error);
11789 if (error)
11790 len = 0;
11791 else if (argvars[2].v_type != VAR_UNKNOWN)
11792 len = (int)get_tv_number(&argvars[2]);
11793 else
11794 len = slen - n; /* default len: all bytes that are available. */
11795
11796 /*
11797 * Only return the overlap between the specified part and the actual
11798 * string.
11799 */
11800 if (n < 0)
11801 {
11802 len += n;
11803 n = 0;
11804 }
11805 else if (n > slen)
11806 n = slen;
11807 if (len < 0)
11808 len = 0;
11809 else if (n + len > slen)
11810 len = slen - n;
11811
11812 rettv->v_type = VAR_STRING;
11813 rettv->vval.v_string = vim_strnsave(p + n, len);
11814}
11815
11816/*
11817 * "strridx()" function
11818 */
11819 static void
11820f_strridx(typval_T *argvars, typval_T *rettv)
11821{
11822 char_u buf[NUMBUFLEN];
11823 char_u *needle;
11824 char_u *haystack;
11825 char_u *rest;
11826 char_u *lastmatch = NULL;
11827 int haystack_len, end_idx;
11828
11829 needle = get_tv_string_chk(&argvars[1]);
11830 haystack = get_tv_string_buf_chk(&argvars[0], buf);
11831
11832 rettv->vval.v_number = -1;
11833 if (needle == NULL || haystack == NULL)
11834 return; /* type error; errmsg already given */
11835
11836 haystack_len = (int)STRLEN(haystack);
11837 if (argvars[2].v_type != VAR_UNKNOWN)
11838 {
11839 /* Third argument: upper limit for index */
11840 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
11841 if (end_idx < 0)
11842 return; /* can never find a match */
11843 }
11844 else
11845 end_idx = haystack_len;
11846
11847 if (*needle == NUL)
11848 {
11849 /* Empty string matches past the end. */
11850 lastmatch = haystack + end_idx;
11851 }
11852 else
11853 {
11854 for (rest = haystack; *rest != '\0'; ++rest)
11855 {
11856 rest = (char_u *)strstr((char *)rest, (char *)needle);
11857 if (rest == NULL || rest > haystack + end_idx)
11858 break;
11859 lastmatch = rest;
11860 }
11861 }
11862
11863 if (lastmatch == NULL)
11864 rettv->vval.v_number = -1;
11865 else
11866 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
11867}
11868
11869/*
11870 * "strtrans()" function
11871 */
11872 static void
11873f_strtrans(typval_T *argvars, typval_T *rettv)
11874{
11875 rettv->v_type = VAR_STRING;
11876 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
11877}
11878
11879/*
11880 * "submatch()" function
11881 */
11882 static void
11883f_submatch(typval_T *argvars, typval_T *rettv)
11884{
11885 int error = FALSE;
11886 int no;
11887 int retList = 0;
11888
11889 no = (int)get_tv_number_chk(&argvars[0], &error);
11890 if (error)
11891 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020011892 if (no < 0 || no >= NSUBEXP)
11893 {
Bram Moolenaar79518e22017-02-17 16:31:35 +010011894 EMSGN(_("E935: invalid submatch number: %d"), no);
11895 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020011896 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011897 if (argvars[1].v_type != VAR_UNKNOWN)
11898 retList = (int)get_tv_number_chk(&argvars[1], &error);
11899 if (error)
11900 return;
11901
11902 if (retList == 0)
11903 {
11904 rettv->v_type = VAR_STRING;
11905 rettv->vval.v_string = reg_submatch(no);
11906 }
11907 else
11908 {
11909 rettv->v_type = VAR_LIST;
11910 rettv->vval.v_list = reg_submatch_list(no);
11911 }
11912}
11913
11914/*
11915 * "substitute()" function
11916 */
11917 static void
11918f_substitute(typval_T *argvars, typval_T *rettv)
11919{
11920 char_u patbuf[NUMBUFLEN];
11921 char_u subbuf[NUMBUFLEN];
11922 char_u flagsbuf[NUMBUFLEN];
11923
11924 char_u *str = get_tv_string_chk(&argvars[0]);
11925 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011926 char_u *sub = NULL;
11927 typval_T *expr = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011928 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
11929
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011930 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
11931 expr = &argvars[2];
11932 else
11933 sub = get_tv_string_buf_chk(&argvars[2], subbuf);
11934
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011935 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011936 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
11937 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011938 rettv->vval.v_string = NULL;
11939 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011940 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011941}
11942
11943/*
11944 * "synID(lnum, col, trans)" function
11945 */
11946 static void
11947f_synID(typval_T *argvars UNUSED, typval_T *rettv)
11948{
11949 int id = 0;
11950#ifdef FEAT_SYN_HL
11951 linenr_T lnum;
11952 colnr_T col;
11953 int trans;
11954 int transerr = FALSE;
11955
11956 lnum = get_tv_lnum(argvars); /* -1 on type error */
11957 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
11958 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
11959
11960 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11961 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
11962 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
11963#endif
11964
11965 rettv->vval.v_number = id;
11966}
11967
11968/*
11969 * "synIDattr(id, what [, mode])" function
11970 */
11971 static void
11972f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
11973{
11974 char_u *p = NULL;
11975#ifdef FEAT_SYN_HL
11976 int id;
11977 char_u *what;
11978 char_u *mode;
11979 char_u modebuf[NUMBUFLEN];
11980 int modec;
11981
11982 id = (int)get_tv_number(&argvars[0]);
11983 what = get_tv_string(&argvars[1]);
11984 if (argvars[2].v_type != VAR_UNKNOWN)
11985 {
11986 mode = get_tv_string_buf(&argvars[2], modebuf);
11987 modec = TOLOWER_ASC(mode[0]);
11988 if (modec != 't' && modec != 'c' && modec != 'g')
11989 modec = 0; /* replace invalid with current */
11990 }
11991 else
11992 {
11993#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
11994 if (USE_24BIT)
11995 modec = 'g';
11996 else
11997#endif
11998 if (t_colors > 1)
11999 modec = 'c';
12000 else
12001 modec = 't';
12002 }
12003
12004
12005 switch (TOLOWER_ASC(what[0]))
12006 {
12007 case 'b':
12008 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12009 p = highlight_color(id, what, modec);
12010 else /* bold */
12011 p = highlight_has_attr(id, HL_BOLD, modec);
12012 break;
12013
12014 case 'f': /* fg[#] or font */
12015 p = highlight_color(id, what, modec);
12016 break;
12017
12018 case 'i':
12019 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
12020 p = highlight_has_attr(id, HL_INVERSE, modec);
12021 else /* italic */
12022 p = highlight_has_attr(id, HL_ITALIC, modec);
12023 break;
12024
12025 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020012026 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012027 break;
12028
12029 case 'r': /* reverse */
12030 p = highlight_has_attr(id, HL_INVERSE, modec);
12031 break;
12032
12033 case 's':
12034 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
12035 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020012036 /* strikeout */
12037 else if (TOLOWER_ASC(what[1]) == 't' &&
12038 TOLOWER_ASC(what[2]) == 'r')
12039 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012040 else /* standout */
12041 p = highlight_has_attr(id, HL_STANDOUT, modec);
12042 break;
12043
12044 case 'u':
12045 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
12046 /* underline */
12047 p = highlight_has_attr(id, HL_UNDERLINE, modec);
12048 else
12049 /* undercurl */
12050 p = highlight_has_attr(id, HL_UNDERCURL, modec);
12051 break;
12052 }
12053
12054 if (p != NULL)
12055 p = vim_strsave(p);
12056#endif
12057 rettv->v_type = VAR_STRING;
12058 rettv->vval.v_string = p;
12059}
12060
12061/*
12062 * "synIDtrans(id)" function
12063 */
12064 static void
12065f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
12066{
12067 int id;
12068
12069#ifdef FEAT_SYN_HL
12070 id = (int)get_tv_number(&argvars[0]);
12071
12072 if (id > 0)
12073 id = syn_get_final_id(id);
12074 else
12075#endif
12076 id = 0;
12077
12078 rettv->vval.v_number = id;
12079}
12080
12081/*
12082 * "synconcealed(lnum, col)" function
12083 */
12084 static void
12085f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
12086{
12087#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12088 linenr_T lnum;
12089 colnr_T col;
12090 int syntax_flags = 0;
12091 int cchar;
12092 int matchid = 0;
12093 char_u str[NUMBUFLEN];
12094#endif
12095
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012096 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012097
12098#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12099 lnum = get_tv_lnum(argvars); /* -1 on type error */
12100 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12101
12102 vim_memset(str, NUL, sizeof(str));
12103
12104 if (rettv_list_alloc(rettv) != FAIL)
12105 {
12106 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12107 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12108 && curwin->w_p_cole > 0)
12109 {
12110 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
12111 syntax_flags = get_syntax_info(&matchid);
12112
12113 /* get the conceal character */
12114 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
12115 {
12116 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020012117 if (cchar == NUL && curwin->w_p_cole == 1)
12118 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012119 if (cchar != NUL)
12120 {
12121# ifdef FEAT_MBYTE
12122 if (has_mbyte)
12123 (*mb_char2bytes)(cchar, str);
12124 else
12125# endif
12126 str[0] = cchar;
12127 }
12128 }
12129 }
12130
12131 list_append_number(rettv->vval.v_list,
12132 (syntax_flags & HL_CONCEAL) != 0);
12133 /* -1 to auto-determine strlen */
12134 list_append_string(rettv->vval.v_list, str, -1);
12135 list_append_number(rettv->vval.v_list, matchid);
12136 }
12137#endif
12138}
12139
12140/*
12141 * "synstack(lnum, col)" function
12142 */
12143 static void
12144f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
12145{
12146#ifdef FEAT_SYN_HL
12147 linenr_T lnum;
12148 colnr_T col;
12149 int i;
12150 int id;
12151#endif
12152
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012153 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012154
12155#ifdef FEAT_SYN_HL
12156 lnum = get_tv_lnum(argvars); /* -1 on type error */
12157 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12158
12159 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12160 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12161 && rettv_list_alloc(rettv) != FAIL)
12162 {
12163 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
12164 for (i = 0; ; ++i)
12165 {
12166 id = syn_get_stack_item(i);
12167 if (id < 0)
12168 break;
12169 if (list_append_number(rettv->vval.v_list, id) == FAIL)
12170 break;
12171 }
12172 }
12173#endif
12174}
12175
12176 static void
12177get_cmd_output_as_rettv(
12178 typval_T *argvars,
12179 typval_T *rettv,
12180 int retlist)
12181{
12182 char_u *res = NULL;
12183 char_u *p;
12184 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012185 int err = FALSE;
12186 FILE *fd;
12187 list_T *list = NULL;
12188 int flags = SHELL_SILENT;
12189
12190 rettv->v_type = VAR_STRING;
12191 rettv->vval.v_string = NULL;
12192 if (check_restricted() || check_secure())
12193 goto errret;
12194
12195 if (argvars[1].v_type != VAR_UNKNOWN)
12196 {
12197 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010012198 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012199 * command.
12200 */
12201 if ((infile = vim_tempname('i', TRUE)) == NULL)
12202 {
12203 EMSG(_(e_notmp));
12204 goto errret;
12205 }
12206
12207 fd = mch_fopen((char *)infile, WRITEBIN);
12208 if (fd == NULL)
12209 {
12210 EMSG2(_(e_notopen), infile);
12211 goto errret;
12212 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010012213 if (argvars[1].v_type == VAR_NUMBER)
12214 {
12215 linenr_T lnum;
12216 buf_T *buf;
12217
12218 buf = buflist_findnr(argvars[1].vval.v_number);
12219 if (buf == NULL)
12220 {
12221 EMSGN(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010012222 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010012223 goto errret;
12224 }
12225
12226 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
12227 {
12228 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
12229 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
12230 {
12231 err = TRUE;
12232 break;
12233 }
12234 if (putc(NL, fd) == EOF)
12235 {
12236 err = TRUE;
12237 break;
12238 }
12239 }
12240 }
12241 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012242 {
12243 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
12244 err = TRUE;
12245 }
12246 else
12247 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010012248 size_t len;
12249 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012250
12251 p = get_tv_string_buf_chk(&argvars[1], buf);
12252 if (p == NULL)
12253 {
12254 fclose(fd);
12255 goto errret; /* type error; errmsg already given */
12256 }
12257 len = STRLEN(p);
12258 if (len > 0 && fwrite(p, len, 1, fd) != 1)
12259 err = TRUE;
12260 }
12261 if (fclose(fd) != 0)
12262 err = TRUE;
12263 if (err)
12264 {
12265 EMSG(_("E677: Error writing temp file"));
12266 goto errret;
12267 }
12268 }
12269
12270 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
12271 * echoes typeahead, that messes up the display. */
12272 if (!msg_silent)
12273 flags += SHELL_COOKED;
12274
12275 if (retlist)
12276 {
12277 int len;
12278 listitem_T *li;
12279 char_u *s = NULL;
12280 char_u *start;
12281 char_u *end;
12282 int i;
12283
12284 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
12285 if (res == NULL)
12286 goto errret;
12287
12288 list = list_alloc();
12289 if (list == NULL)
12290 goto errret;
12291
12292 for (i = 0; i < len; ++i)
12293 {
12294 start = res + i;
12295 while (i < len && res[i] != NL)
12296 ++i;
12297 end = res + i;
12298
12299 s = alloc((unsigned)(end - start + 1));
12300 if (s == NULL)
12301 goto errret;
12302
12303 for (p = s; start < end; ++p, ++start)
12304 *p = *start == NUL ? NL : *start;
12305 *p = NUL;
12306
12307 li = listitem_alloc();
12308 if (li == NULL)
12309 {
12310 vim_free(s);
12311 goto errret;
12312 }
12313 li->li_tv.v_type = VAR_STRING;
12314 li->li_tv.v_lock = 0;
12315 li->li_tv.vval.v_string = s;
12316 list_append(list, li);
12317 }
12318
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012319 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012320 list = NULL;
12321 }
12322 else
12323 {
12324 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
12325#ifdef USE_CR
12326 /* translate <CR> into <NL> */
12327 if (res != NULL)
12328 {
12329 char_u *s;
12330
12331 for (s = res; *s; ++s)
12332 {
12333 if (*s == CAR)
12334 *s = NL;
12335 }
12336 }
12337#else
12338# ifdef USE_CRNL
12339 /* translate <CR><NL> into <NL> */
12340 if (res != NULL)
12341 {
12342 char_u *s, *d;
12343
12344 d = res;
12345 for (s = res; *s; ++s)
12346 {
12347 if (s[0] == CAR && s[1] == NL)
12348 ++s;
12349 *d++ = *s;
12350 }
12351 *d = NUL;
12352 }
12353# endif
12354#endif
12355 rettv->vval.v_string = res;
12356 res = NULL;
12357 }
12358
12359errret:
12360 if (infile != NULL)
12361 {
12362 mch_remove(infile);
12363 vim_free(infile);
12364 }
12365 if (res != NULL)
12366 vim_free(res);
12367 if (list != NULL)
12368 list_free(list);
12369}
12370
12371/*
12372 * "system()" function
12373 */
12374 static void
12375f_system(typval_T *argvars, typval_T *rettv)
12376{
12377 get_cmd_output_as_rettv(argvars, rettv, FALSE);
12378}
12379
12380/*
12381 * "systemlist()" function
12382 */
12383 static void
12384f_systemlist(typval_T *argvars, typval_T *rettv)
12385{
12386 get_cmd_output_as_rettv(argvars, rettv, TRUE);
12387}
12388
12389/*
12390 * "tabpagebuflist()" function
12391 */
12392 static void
12393f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12394{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012395 tabpage_T *tp;
12396 win_T *wp = NULL;
12397
12398 if (argvars[0].v_type == VAR_UNKNOWN)
12399 wp = firstwin;
12400 else
12401 {
12402 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12403 if (tp != NULL)
12404 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12405 }
12406 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
12407 {
12408 for (; wp != NULL; wp = wp->w_next)
12409 if (list_append_number(rettv->vval.v_list,
12410 wp->w_buffer->b_fnum) == FAIL)
12411 break;
12412 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012413}
12414
12415
12416/*
12417 * "tabpagenr()" function
12418 */
12419 static void
12420f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
12421{
12422 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012423 char_u *arg;
12424
12425 if (argvars[0].v_type != VAR_UNKNOWN)
12426 {
12427 arg = get_tv_string_chk(&argvars[0]);
12428 nr = 0;
12429 if (arg != NULL)
12430 {
12431 if (STRCMP(arg, "$") == 0)
12432 nr = tabpage_index(NULL) - 1;
12433 else
12434 EMSG2(_(e_invexpr2), arg);
12435 }
12436 }
12437 else
12438 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012439 rettv->vval.v_number = nr;
12440}
12441
12442
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012443static int get_winnr(tabpage_T *tp, typval_T *argvar);
12444
12445/*
12446 * Common code for tabpagewinnr() and winnr().
12447 */
12448 static int
12449get_winnr(tabpage_T *tp, typval_T *argvar)
12450{
12451 win_T *twin;
12452 int nr = 1;
12453 win_T *wp;
12454 char_u *arg;
12455
12456 twin = (tp == curtab) ? curwin : tp->tp_curwin;
12457 if (argvar->v_type != VAR_UNKNOWN)
12458 {
12459 arg = get_tv_string_chk(argvar);
12460 if (arg == NULL)
12461 nr = 0; /* type error; errmsg already given */
12462 else if (STRCMP(arg, "$") == 0)
12463 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
12464 else if (STRCMP(arg, "#") == 0)
12465 {
12466 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
12467 if (twin == NULL)
12468 nr = 0;
12469 }
12470 else
12471 {
12472 EMSG2(_(e_invexpr2), arg);
12473 nr = 0;
12474 }
12475 }
12476
12477 if (nr > 0)
12478 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12479 wp != twin; wp = wp->w_next)
12480 {
12481 if (wp == NULL)
12482 {
12483 /* didn't find it in this tabpage */
12484 nr = 0;
12485 break;
12486 }
12487 ++nr;
12488 }
12489 return nr;
12490}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012491
12492/*
12493 * "tabpagewinnr()" function
12494 */
12495 static void
12496f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
12497{
12498 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012499 tabpage_T *tp;
12500
12501 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12502 if (tp == NULL)
12503 nr = 0;
12504 else
12505 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012506 rettv->vval.v_number = nr;
12507}
12508
12509
12510/*
12511 * "tagfiles()" function
12512 */
12513 static void
12514f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
12515{
12516 char_u *fname;
12517 tagname_T tn;
12518 int first;
12519
12520 if (rettv_list_alloc(rettv) == FAIL)
12521 return;
12522 fname = alloc(MAXPATHL);
12523 if (fname == NULL)
12524 return;
12525
12526 for (first = TRUE; ; first = FALSE)
12527 if (get_tagfname(&tn, first, fname) == FAIL
12528 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
12529 break;
12530 tagname_free(&tn);
12531 vim_free(fname);
12532}
12533
12534/*
12535 * "taglist()" function
12536 */
12537 static void
12538f_taglist(typval_T *argvars, typval_T *rettv)
12539{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012540 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012541 char_u *tag_pattern;
12542
12543 tag_pattern = get_tv_string(&argvars[0]);
12544
12545 rettv->vval.v_number = FALSE;
12546 if (*tag_pattern == NUL)
12547 return;
12548
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012549 if (argvars[1].v_type != VAR_UNKNOWN)
12550 fname = get_tv_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012551 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012552 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012553}
12554
12555/*
12556 * "tempname()" function
12557 */
12558 static void
12559f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
12560{
12561 static int x = 'A';
12562
12563 rettv->v_type = VAR_STRING;
12564 rettv->vval.v_string = vim_tempname(x, FALSE);
12565
12566 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
12567 * names. Skip 'I' and 'O', they are used for shell redirection. */
12568 do
12569 {
12570 if (x == 'Z')
12571 x = '0';
12572 else if (x == '9')
12573 x = 'A';
12574 else
12575 {
12576#ifdef EBCDIC
12577 if (x == 'I')
12578 x = 'J';
12579 else if (x == 'R')
12580 x = 'S';
12581 else
12582#endif
12583 ++x;
12584 }
12585 } while (x == 'I' || x == 'O');
12586}
12587
12588#ifdef FEAT_FLOAT
12589/*
12590 * "tan()" function
12591 */
12592 static void
12593f_tan(typval_T *argvars, typval_T *rettv)
12594{
12595 float_T f = 0.0;
12596
12597 rettv->v_type = VAR_FLOAT;
12598 if (get_float_arg(argvars, &f) == OK)
12599 rettv->vval.v_float = tan(f);
12600 else
12601 rettv->vval.v_float = 0.0;
12602}
12603
12604/*
12605 * "tanh()" function
12606 */
12607 static void
12608f_tanh(typval_T *argvars, typval_T *rettv)
12609{
12610 float_T f = 0.0;
12611
12612 rettv->v_type = VAR_FLOAT;
12613 if (get_float_arg(argvars, &f) == OK)
12614 rettv->vval.v_float = tanh(f);
12615 else
12616 rettv->vval.v_float = 0.0;
12617}
12618#endif
12619
12620/*
12621 * "test_alloc_fail(id, countdown, repeat)" function
12622 */
12623 static void
12624f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
12625{
12626 if (argvars[0].v_type != VAR_NUMBER
12627 || argvars[0].vval.v_number <= 0
12628 || argvars[1].v_type != VAR_NUMBER
12629 || argvars[1].vval.v_number < 0
12630 || argvars[2].v_type != VAR_NUMBER)
12631 EMSG(_(e_invarg));
12632 else
12633 {
12634 alloc_fail_id = argvars[0].vval.v_number;
12635 if (alloc_fail_id >= aid_last)
12636 EMSG(_(e_invarg));
12637 alloc_fail_countdown = argvars[1].vval.v_number;
12638 alloc_fail_repeat = argvars[2].vval.v_number;
12639 did_outofmem_msg = FALSE;
12640 }
12641}
12642
12643/*
12644 * "test_autochdir()"
12645 */
12646 static void
12647f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12648{
12649#if defined(FEAT_AUTOCHDIR)
12650 test_autochdir = TRUE;
12651#endif
12652}
12653
12654/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020012655 * "test_feedinput()"
12656 */
12657 static void
12658f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
12659{
12660#ifdef USE_INPUT_BUF
12661 char_u *val = get_tv_string_chk(&argvars[0]);
12662
12663 if (val != NULL)
12664 {
12665 trash_input_buf();
12666 add_to_input_buf_csi(val, (int)STRLEN(val));
12667 }
12668#endif
12669}
12670
12671/*
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012672 * "test_disable({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012673 */
12674 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012675f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012676{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012677 char_u *name = (char_u *)"";
12678 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012679 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012680
12681 if (argvars[0].v_type != VAR_STRING
12682 || (argvars[1].v_type) != VAR_NUMBER)
12683 EMSG(_(e_invarg));
12684 else
12685 {
12686 name = get_tv_string_chk(&argvars[0]);
12687 val = (int)get_tv_number(&argvars[1]);
12688
12689 if (STRCMP(name, (char_u *)"redraw") == 0)
12690 disable_redraw_for_testing = val;
12691 else if (STRCMP(name, (char_u *)"char_avail") == 0)
12692 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012693 else if (STRCMP(name, (char_u *)"starting") == 0)
12694 {
12695 if (val)
12696 {
12697 if (save_starting < 0)
12698 save_starting = starting;
12699 starting = 0;
12700 }
12701 else
12702 {
12703 starting = save_starting;
12704 save_starting = -1;
12705 }
12706 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012707 else if (STRCMP(name, (char_u *)"ALL") == 0)
12708 {
12709 disable_char_avail_for_testing = FALSE;
12710 disable_redraw_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020012711 if (save_starting >= 0)
12712 {
12713 starting = save_starting;
12714 save_starting = -1;
12715 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010012716 }
12717 else
12718 EMSG2(_(e_invarg2), name);
12719 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012720}
12721
12722/*
12723 * "test_garbagecollect_now()" function
12724 */
12725 static void
12726f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12727{
12728 /* This is dangerous, any Lists and Dicts used internally may be freed
12729 * while still in use. */
12730 garbage_collect(TRUE);
12731}
12732
Bram Moolenaare0c31f62017-03-01 15:07:05 +010012733/*
12734 * "test_ignore_error()" function
12735 */
12736 static void
12737f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
12738{
12739 ignore_error_for_testing(get_tv_string(&argvars[0]));
12740}
12741
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012742#ifdef FEAT_JOB_CHANNEL
12743 static void
12744f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
12745{
12746 rettv->v_type = VAR_CHANNEL;
12747 rettv->vval.v_channel = NULL;
12748}
12749#endif
12750
12751 static void
12752f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
12753{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012754 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012755}
12756
12757#ifdef FEAT_JOB_CHANNEL
12758 static void
12759f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
12760{
12761 rettv->v_type = VAR_JOB;
12762 rettv->vval.v_job = NULL;
12763}
12764#endif
12765
12766 static void
12767f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
12768{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012769 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012770}
12771
12772 static void
12773f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
12774{
12775 rettv->v_type = VAR_PARTIAL;
12776 rettv->vval.v_partial = NULL;
12777}
12778
12779 static void
12780f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
12781{
12782 rettv->v_type = VAR_STRING;
12783 rettv->vval.v_string = NULL;
12784}
12785
12786 static void
12787f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
12788{
12789 time_for_testing = (time_t)get_tv_number(&argvars[0]);
12790}
12791
12792#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
12793/*
12794 * Get a callback from "arg". It can be a Funcref or a function name.
12795 * When "arg" is zero return an empty string.
12796 * Return NULL for an invalid argument.
12797 */
12798 char_u *
12799get_callback(typval_T *arg, partial_T **pp)
12800{
12801 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
12802 {
12803 *pp = arg->vval.v_partial;
12804 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012805 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012806 }
12807 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012808 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012809 {
12810 func_ref(arg->vval.v_string);
12811 return arg->vval.v_string;
12812 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012813 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
12814 return (char_u *)"";
12815 EMSG(_("E921: Invalid callback argument"));
12816 return NULL;
12817}
12818
12819/*
12820 * Unref/free "callback" and "partial" retured by get_callback().
12821 */
12822 void
12823free_callback(char_u *callback, partial_T *partial)
12824{
12825 if (partial != NULL)
12826 partial_unref(partial);
12827 else if (callback != NULL)
12828 {
12829 func_unref(callback);
12830 vim_free(callback);
12831 }
12832}
12833#endif
12834
12835#ifdef FEAT_TIMERS
12836/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020012837 * "timer_info([timer])" function
12838 */
12839 static void
12840f_timer_info(typval_T *argvars, typval_T *rettv)
12841{
12842 timer_T *timer = NULL;
12843
12844 if (rettv_list_alloc(rettv) != OK)
12845 return;
12846 if (argvars[0].v_type != VAR_UNKNOWN)
12847 {
12848 if (argvars[0].v_type != VAR_NUMBER)
12849 EMSG(_(e_number_exp));
12850 else
12851 {
12852 timer = find_timer((int)get_tv_number(&argvars[0]));
12853 if (timer != NULL)
12854 add_timer_info(rettv, timer);
12855 }
12856 }
12857 else
12858 add_timer_info_all(rettv);
12859}
12860
12861/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012862 * "timer_pause(timer, paused)" function
12863 */
12864 static void
12865f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
12866{
12867 timer_T *timer = NULL;
12868 int paused = (int)get_tv_number(&argvars[1]);
12869
12870 if (argvars[0].v_type != VAR_NUMBER)
12871 EMSG(_(e_number_exp));
12872 else
12873 {
12874 timer = find_timer((int)get_tv_number(&argvars[0]));
12875 if (timer != NULL)
12876 timer->tr_paused = paused;
12877 }
12878}
12879
12880/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012881 * "timer_start(time, callback [, options])" function
12882 */
12883 static void
12884f_timer_start(typval_T *argvars, typval_T *rettv)
12885{
Bram Moolenaar75537a92016-09-05 22:45:28 +020012886 long msec = (long)get_tv_number(&argvars[0]);
12887 timer_T *timer;
12888 int repeat = 0;
12889 char_u *callback;
12890 dict_T *dict;
12891 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012892
Bram Moolenaar75537a92016-09-05 22:45:28 +020012893 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012894 if (check_secure())
12895 return;
12896 if (argvars[2].v_type != VAR_UNKNOWN)
12897 {
12898 if (argvars[2].v_type != VAR_DICT
12899 || (dict = argvars[2].vval.v_dict) == NULL)
12900 {
12901 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
12902 return;
12903 }
12904 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
12905 repeat = get_dict_number(dict, (char_u *)"repeat");
12906 }
12907
Bram Moolenaar75537a92016-09-05 22:45:28 +020012908 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012909 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020012910 return;
12911
12912 timer = create_timer(msec, repeat);
12913 if (timer == NULL)
12914 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012915 else
12916 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020012917 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020012918 timer->tr_callback = vim_strsave(callback);
12919 else
12920 /* pointer into the partial */
12921 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020012922 timer->tr_partial = partial;
12923 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012924 }
12925}
12926
12927/*
12928 * "timer_stop(timer)" function
12929 */
12930 static void
12931f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
12932{
12933 timer_T *timer;
12934
12935 if (argvars[0].v_type != VAR_NUMBER)
12936 {
12937 EMSG(_(e_number_exp));
12938 return;
12939 }
12940 timer = find_timer((int)get_tv_number(&argvars[0]));
12941 if (timer != NULL)
12942 stop_timer(timer);
12943}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012944
12945/*
12946 * "timer_stopall()" function
12947 */
12948 static void
12949f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12950{
12951 stop_all_timers();
12952}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012953#endif
12954
12955/*
12956 * "tolower(string)" function
12957 */
12958 static void
12959f_tolower(typval_T *argvars, typval_T *rettv)
12960{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012961 rettv->v_type = VAR_STRING;
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +010012962 rettv->vval.v_string = strlow_save(get_tv_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012963}
12964
12965/*
12966 * "toupper(string)" function
12967 */
12968 static void
12969f_toupper(typval_T *argvars, typval_T *rettv)
12970{
12971 rettv->v_type = VAR_STRING;
12972 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
12973}
12974
12975/*
12976 * "tr(string, fromstr, tostr)" function
12977 */
12978 static void
12979f_tr(typval_T *argvars, typval_T *rettv)
12980{
12981 char_u *in_str;
12982 char_u *fromstr;
12983 char_u *tostr;
12984 char_u *p;
12985#ifdef FEAT_MBYTE
12986 int inlen;
12987 int fromlen;
12988 int tolen;
12989 int idx;
12990 char_u *cpstr;
12991 int cplen;
12992 int first = TRUE;
12993#endif
12994 char_u buf[NUMBUFLEN];
12995 char_u buf2[NUMBUFLEN];
12996 garray_T ga;
12997
12998 in_str = get_tv_string(&argvars[0]);
12999 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
13000 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
13001
13002 /* Default return value: empty string. */
13003 rettv->v_type = VAR_STRING;
13004 rettv->vval.v_string = NULL;
13005 if (fromstr == NULL || tostr == NULL)
13006 return; /* type error; errmsg already given */
13007 ga_init2(&ga, (int)sizeof(char), 80);
13008
13009#ifdef FEAT_MBYTE
13010 if (!has_mbyte)
13011#endif
13012 /* not multi-byte: fromstr and tostr must be the same length */
13013 if (STRLEN(fromstr) != STRLEN(tostr))
13014 {
13015#ifdef FEAT_MBYTE
13016error:
13017#endif
13018 EMSG2(_(e_invarg2), fromstr);
13019 ga_clear(&ga);
13020 return;
13021 }
13022
13023 /* fromstr and tostr have to contain the same number of chars */
13024 while (*in_str != NUL)
13025 {
13026#ifdef FEAT_MBYTE
13027 if (has_mbyte)
13028 {
13029 inlen = (*mb_ptr2len)(in_str);
13030 cpstr = in_str;
13031 cplen = inlen;
13032 idx = 0;
13033 for (p = fromstr; *p != NUL; p += fromlen)
13034 {
13035 fromlen = (*mb_ptr2len)(p);
13036 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
13037 {
13038 for (p = tostr; *p != NUL; p += tolen)
13039 {
13040 tolen = (*mb_ptr2len)(p);
13041 if (idx-- == 0)
13042 {
13043 cplen = tolen;
13044 cpstr = p;
13045 break;
13046 }
13047 }
13048 if (*p == NUL) /* tostr is shorter than fromstr */
13049 goto error;
13050 break;
13051 }
13052 ++idx;
13053 }
13054
13055 if (first && cpstr == in_str)
13056 {
13057 /* Check that fromstr and tostr have the same number of
13058 * (multi-byte) characters. Done only once when a character
13059 * of in_str doesn't appear in fromstr. */
13060 first = FALSE;
13061 for (p = tostr; *p != NUL; p += tolen)
13062 {
13063 tolen = (*mb_ptr2len)(p);
13064 --idx;
13065 }
13066 if (idx != 0)
13067 goto error;
13068 }
13069
13070 (void)ga_grow(&ga, cplen);
13071 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
13072 ga.ga_len += cplen;
13073
13074 in_str += inlen;
13075 }
13076 else
13077#endif
13078 {
13079 /* When not using multi-byte chars we can do it faster. */
13080 p = vim_strchr(fromstr, *in_str);
13081 if (p != NULL)
13082 ga_append(&ga, tostr[p - fromstr]);
13083 else
13084 ga_append(&ga, *in_str);
13085 ++in_str;
13086 }
13087 }
13088
13089 /* add a terminating NUL */
13090 (void)ga_grow(&ga, 1);
13091 ga_append(&ga, NUL);
13092
13093 rettv->vval.v_string = ga.ga_data;
13094}
13095
13096#ifdef FEAT_FLOAT
13097/*
13098 * "trunc({float})" function
13099 */
13100 static void
13101f_trunc(typval_T *argvars, typval_T *rettv)
13102{
13103 float_T f = 0.0;
13104
13105 rettv->v_type = VAR_FLOAT;
13106 if (get_float_arg(argvars, &f) == OK)
13107 /* trunc() is not in C90, use floor() or ceil() instead. */
13108 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
13109 else
13110 rettv->vval.v_float = 0.0;
13111}
13112#endif
13113
13114/*
13115 * "type(expr)" function
13116 */
13117 static void
13118f_type(typval_T *argvars, typval_T *rettv)
13119{
13120 int n = -1;
13121
13122 switch (argvars[0].v_type)
13123 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020013124 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
13125 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013126 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020013127 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
13128 case VAR_LIST: n = VAR_TYPE_LIST; break;
13129 case VAR_DICT: n = VAR_TYPE_DICT; break;
13130 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013131 case VAR_SPECIAL:
13132 if (argvars[0].vval.v_number == VVAL_FALSE
13133 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020013134 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013135 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020013136 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013137 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020013138 case VAR_JOB: n = VAR_TYPE_JOB; break;
13139 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013140 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010013141 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013142 n = -1;
13143 break;
13144 }
13145 rettv->vval.v_number = n;
13146}
13147
13148/*
13149 * "undofile(name)" function
13150 */
13151 static void
13152f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
13153{
13154 rettv->v_type = VAR_STRING;
13155#ifdef FEAT_PERSISTENT_UNDO
13156 {
13157 char_u *fname = get_tv_string(&argvars[0]);
13158
13159 if (*fname == NUL)
13160 {
13161 /* If there is no file name there will be no undo file. */
13162 rettv->vval.v_string = NULL;
13163 }
13164 else
13165 {
13166 char_u *ffname = FullName_save(fname, FALSE);
13167
13168 if (ffname != NULL)
13169 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
13170 vim_free(ffname);
13171 }
13172 }
13173#else
13174 rettv->vval.v_string = NULL;
13175#endif
13176}
13177
13178/*
13179 * "undotree()" function
13180 */
13181 static void
13182f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
13183{
13184 if (rettv_dict_alloc(rettv) == OK)
13185 {
13186 dict_T *dict = rettv->vval.v_dict;
13187 list_T *list;
13188
13189 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
13190 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
13191 dict_add_nr_str(dict, "save_last",
13192 (long)curbuf->b_u_save_nr_last, NULL);
13193 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
13194 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
13195 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
13196
13197 list = list_alloc();
13198 if (list != NULL)
13199 {
13200 u_eval_tree(curbuf->b_u_oldhead, list);
13201 dict_add_list(dict, "entries", list);
13202 }
13203 }
13204}
13205
13206/*
13207 * "values(dict)" function
13208 */
13209 static void
13210f_values(typval_T *argvars, typval_T *rettv)
13211{
13212 dict_list(argvars, rettv, 1);
13213}
13214
13215/*
13216 * "virtcol(string)" function
13217 */
13218 static void
13219f_virtcol(typval_T *argvars, typval_T *rettv)
13220{
13221 colnr_T vcol = 0;
13222 pos_T *fp;
13223 int fnum = curbuf->b_fnum;
13224
13225 fp = var2fpos(&argvars[0], FALSE, &fnum);
13226 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
13227 && fnum == curbuf->b_fnum)
13228 {
13229 getvvcol(curwin, fp, NULL, NULL, &vcol);
13230 ++vcol;
13231 }
13232
13233 rettv->vval.v_number = vcol;
13234}
13235
13236/*
13237 * "visualmode()" function
13238 */
13239 static void
13240f_visualmode(typval_T *argvars, typval_T *rettv)
13241{
13242 char_u str[2];
13243
13244 rettv->v_type = VAR_STRING;
13245 str[0] = curbuf->b_visual_mode_eval;
13246 str[1] = NUL;
13247 rettv->vval.v_string = vim_strsave(str);
13248
13249 /* A non-zero number or non-empty string argument: reset mode. */
13250 if (non_zero_arg(&argvars[0]))
13251 curbuf->b_visual_mode_eval = NUL;
13252}
13253
13254/*
13255 * "wildmenumode()" function
13256 */
13257 static void
13258f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13259{
13260#ifdef FEAT_WILDMENU
13261 if (wild_menu_showing)
13262 rettv->vval.v_number = 1;
13263#endif
13264}
13265
13266/*
13267 * "winbufnr(nr)" function
13268 */
13269 static void
13270f_winbufnr(typval_T *argvars, typval_T *rettv)
13271{
13272 win_T *wp;
13273
13274 wp = find_win_by_nr(&argvars[0], NULL);
13275 if (wp == NULL)
13276 rettv->vval.v_number = -1;
13277 else
13278 rettv->vval.v_number = wp->w_buffer->b_fnum;
13279}
13280
13281/*
13282 * "wincol()" function
13283 */
13284 static void
13285f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
13286{
13287 validate_cursor();
13288 rettv->vval.v_number = curwin->w_wcol + 1;
13289}
13290
13291/*
13292 * "winheight(nr)" function
13293 */
13294 static void
13295f_winheight(typval_T *argvars, typval_T *rettv)
13296{
13297 win_T *wp;
13298
13299 wp = find_win_by_nr(&argvars[0], NULL);
13300 if (wp == NULL)
13301 rettv->vval.v_number = -1;
13302 else
13303 rettv->vval.v_number = wp->w_height;
13304}
13305
13306/*
13307 * "winline()" function
13308 */
13309 static void
13310f_winline(typval_T *argvars UNUSED, typval_T *rettv)
13311{
13312 validate_cursor();
13313 rettv->vval.v_number = curwin->w_wrow + 1;
13314}
13315
13316/*
13317 * "winnr()" function
13318 */
13319 static void
13320f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
13321{
13322 int nr = 1;
13323
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013324 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013325 rettv->vval.v_number = nr;
13326}
13327
13328/*
13329 * "winrestcmd()" function
13330 */
13331 static void
13332f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
13333{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013334 win_T *wp;
13335 int winnr = 1;
13336 garray_T ga;
13337 char_u buf[50];
13338
13339 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020013340 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013341 {
13342 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
13343 ga_concat(&ga, buf);
13344 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
13345 ga_concat(&ga, buf);
13346 ++winnr;
13347 }
13348 ga_append(&ga, NUL);
13349
13350 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013351 rettv->v_type = VAR_STRING;
13352}
13353
13354/*
13355 * "winrestview()" function
13356 */
13357 static void
13358f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
13359{
13360 dict_T *dict;
13361
13362 if (argvars[0].v_type != VAR_DICT
13363 || (dict = argvars[0].vval.v_dict) == NULL)
13364 EMSG(_(e_invarg));
13365 else
13366 {
13367 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
13368 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
13369 if (dict_find(dict, (char_u *)"col", -1) != NULL)
13370 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
13371#ifdef FEAT_VIRTUALEDIT
13372 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
13373 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
13374#endif
13375 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
13376 {
13377 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
13378 curwin->w_set_curswant = FALSE;
13379 }
13380
13381 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
13382 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
13383#ifdef FEAT_DIFF
13384 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
13385 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
13386#endif
13387 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
13388 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
13389 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
13390 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
13391
13392 check_cursor();
13393 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020013394 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013395 changed_window_setting();
13396
13397 if (curwin->w_topline <= 0)
13398 curwin->w_topline = 1;
13399 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
13400 curwin->w_topline = curbuf->b_ml.ml_line_count;
13401#ifdef FEAT_DIFF
13402 check_topfill(curwin, TRUE);
13403#endif
13404 }
13405}
13406
13407/*
13408 * "winsaveview()" function
13409 */
13410 static void
13411f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
13412{
13413 dict_T *dict;
13414
13415 if (rettv_dict_alloc(rettv) == FAIL)
13416 return;
13417 dict = rettv->vval.v_dict;
13418
13419 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
13420 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
13421#ifdef FEAT_VIRTUALEDIT
13422 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
13423#endif
13424 update_curswant();
13425 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
13426
13427 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
13428#ifdef FEAT_DIFF
13429 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
13430#endif
13431 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
13432 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
13433}
13434
13435/*
13436 * "winwidth(nr)" function
13437 */
13438 static void
13439f_winwidth(typval_T *argvars, typval_T *rettv)
13440{
13441 win_T *wp;
13442
13443 wp = find_win_by_nr(&argvars[0], NULL);
13444 if (wp == NULL)
13445 rettv->vval.v_number = -1;
13446 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013447 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013448}
13449
13450/*
13451 * "wordcount()" function
13452 */
13453 static void
13454f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
13455{
13456 if (rettv_dict_alloc(rettv) == FAIL)
13457 return;
13458 cursor_pos_info(rettv->vval.v_dict);
13459}
13460
13461/*
13462 * "writefile()" function
13463 */
13464 static void
13465f_writefile(typval_T *argvars, typval_T *rettv)
13466{
13467 int binary = FALSE;
13468 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013469#ifdef HAVE_FSYNC
13470 int do_fsync = p_fs;
13471#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013472 char_u *fname;
13473 FILE *fd;
13474 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013475 listitem_T *li;
13476 list_T *list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013477
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013478 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013479 if (check_restricted() || check_secure())
13480 return;
13481
13482 if (argvars[0].v_type != VAR_LIST)
13483 {
13484 EMSG2(_(e_listarg), "writefile()");
13485 return;
13486 }
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013487 list = argvars[0].vval.v_list;
13488 if (list == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013489 return;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013490 for (li = list->lv_first; li != NULL; li = li->li_next)
13491 if (get_tv_string_chk(&li->li_tv) == NULL)
13492 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013493
13494 if (argvars[2].v_type != VAR_UNKNOWN)
13495 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013496 char_u *arg2 = get_tv_string_chk(&argvars[2]);
13497
13498 if (arg2 == NULL)
13499 return;
13500 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013501 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013502 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013503 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013504#ifdef HAVE_FSYNC
13505 if (vim_strchr(arg2, 's') != NULL)
13506 do_fsync = TRUE;
13507 else if (vim_strchr(arg2, 'S') != NULL)
13508 do_fsync = FALSE;
13509#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013510 }
13511
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013512 fname = get_tv_string_chk(&argvars[1]);
13513 if (fname == NULL)
13514 return;
13515
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013516 /* Always open the file in binary mode, library functions have a mind of
13517 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013518 if (*fname == NUL || (fd = mch_fopen((char *)fname,
13519 append ? APPENDBIN : WRITEBIN)) == NULL)
13520 {
13521 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
13522 ret = -1;
13523 }
13524 else
13525 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020013526 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013527 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013528#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010013529 else if (do_fsync)
13530 /* Ignore the error, the user wouldn't know what to do about it.
13531 * May happen for a device. */
13532 ignored = fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010013533#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013534 fclose(fd);
13535 }
13536
13537 rettv->vval.v_number = ret;
13538}
13539
13540/*
13541 * "xor(expr, expr)" function
13542 */
13543 static void
13544f_xor(typval_T *argvars, typval_T *rettv)
13545{
13546 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
13547 ^ get_tv_number_chk(&argvars[1], NULL);
13548}
13549
13550
13551#endif /* FEAT_EVAL */