blob: 70b90ad175355c082fe96e8fb625aa0dafe5fc5e [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
27#ifdef MACOS
28# 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);
55static void f_assert_true(typval_T *argvars, typval_T *rettv);
56#ifdef FEAT_FLOAT
57static void f_asin(typval_T *argvars, typval_T *rettv);
58static void f_atan(typval_T *argvars, typval_T *rettv);
59static void f_atan2(typval_T *argvars, typval_T *rettv);
60#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010061#ifdef FEAT_BEVAL
62static void f_balloon_show(typval_T *argvars, typval_T *rettv);
63#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020064static void f_browse(typval_T *argvars, typval_T *rettv);
65static void f_browsedir(typval_T *argvars, typval_T *rettv);
66static void f_bufexists(typval_T *argvars, typval_T *rettv);
67static void f_buflisted(typval_T *argvars, typval_T *rettv);
68static void f_bufloaded(typval_T *argvars, typval_T *rettv);
69static void f_bufname(typval_T *argvars, typval_T *rettv);
70static void f_bufnr(typval_T *argvars, typval_T *rettv);
71static void f_bufwinid(typval_T *argvars, typval_T *rettv);
72static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
73static void f_byte2line(typval_T *argvars, typval_T *rettv);
74static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
75static void f_byteidx(typval_T *argvars, typval_T *rettv);
76static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
77static void f_call(typval_T *argvars, typval_T *rettv);
78#ifdef FEAT_FLOAT
79static void f_ceil(typval_T *argvars, typval_T *rettv);
80#endif
81#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +010082static void f_ch_canread(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020083static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0874a832016-09-01 15:11:51 +020084static void f_ch_close_in(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020085static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
86static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
87static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
88static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
89static void f_ch_info(typval_T *argvars, typval_T *rettv);
90static void f_ch_log(typval_T *argvars, typval_T *rettv);
91static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
92static void f_ch_open(typval_T *argvars, typval_T *rettv);
93static void f_ch_read(typval_T *argvars, typval_T *rettv);
94static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
95static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
96static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
97static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
98static void f_ch_status(typval_T *argvars, typval_T *rettv);
99#endif
100static void f_changenr(typval_T *argvars, typval_T *rettv);
101static void f_char2nr(typval_T *argvars, typval_T *rettv);
102static void f_cindent(typval_T *argvars, typval_T *rettv);
103static void f_clearmatches(typval_T *argvars, typval_T *rettv);
104static void f_col(typval_T *argvars, typval_T *rettv);
105#if defined(FEAT_INS_EXPAND)
106static void f_complete(typval_T *argvars, typval_T *rettv);
107static void f_complete_add(typval_T *argvars, typval_T *rettv);
108static void f_complete_check(typval_T *argvars, typval_T *rettv);
109#endif
110static void f_confirm(typval_T *argvars, typval_T *rettv);
111static void f_copy(typval_T *argvars, typval_T *rettv);
112#ifdef FEAT_FLOAT
113static void f_cos(typval_T *argvars, typval_T *rettv);
114static void f_cosh(typval_T *argvars, typval_T *rettv);
115#endif
116static void f_count(typval_T *argvars, typval_T *rettv);
117static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
118static void f_cursor(typval_T *argsvars, typval_T *rettv);
119static void f_deepcopy(typval_T *argvars, typval_T *rettv);
120static void f_delete(typval_T *argvars, typval_T *rettv);
121static void f_did_filetype(typval_T *argvars, typval_T *rettv);
122static void f_diff_filler(typval_T *argvars, typval_T *rettv);
123static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
124static void f_empty(typval_T *argvars, typval_T *rettv);
125static void f_escape(typval_T *argvars, typval_T *rettv);
126static void f_eval(typval_T *argvars, typval_T *rettv);
127static void f_eventhandler(typval_T *argvars, typval_T *rettv);
128static void f_executable(typval_T *argvars, typval_T *rettv);
129static void f_execute(typval_T *argvars, typval_T *rettv);
130static void f_exepath(typval_T *argvars, typval_T *rettv);
131static void f_exists(typval_T *argvars, typval_T *rettv);
132#ifdef FEAT_FLOAT
133static void f_exp(typval_T *argvars, typval_T *rettv);
134#endif
135static void f_expand(typval_T *argvars, typval_T *rettv);
136static void f_extend(typval_T *argvars, typval_T *rettv);
137static void f_feedkeys(typval_T *argvars, typval_T *rettv);
138static void f_filereadable(typval_T *argvars, typval_T *rettv);
139static void f_filewritable(typval_T *argvars, typval_T *rettv);
140static void f_filter(typval_T *argvars, typval_T *rettv);
141static void f_finddir(typval_T *argvars, typval_T *rettv);
142static void f_findfile(typval_T *argvars, typval_T *rettv);
143#ifdef FEAT_FLOAT
144static void f_float2nr(typval_T *argvars, typval_T *rettv);
145static void f_floor(typval_T *argvars, typval_T *rettv);
146static void f_fmod(typval_T *argvars, typval_T *rettv);
147#endif
148static void f_fnameescape(typval_T *argvars, typval_T *rettv);
149static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
150static void f_foldclosed(typval_T *argvars, typval_T *rettv);
151static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
152static void f_foldlevel(typval_T *argvars, typval_T *rettv);
153static void f_foldtext(typval_T *argvars, typval_T *rettv);
154static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
155static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200156static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200157static void f_function(typval_T *argvars, typval_T *rettv);
158static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
159static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200160static void f_getbufinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200161static void f_getbufline(typval_T *argvars, typval_T *rettv);
162static void f_getbufvar(typval_T *argvars, typval_T *rettv);
163static void f_getchar(typval_T *argvars, typval_T *rettv);
164static void f_getcharmod(typval_T *argvars, typval_T *rettv);
165static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
166static void f_getcmdline(typval_T *argvars, typval_T *rettv);
167#if defined(FEAT_CMDL_COMPL)
168static void f_getcompletion(typval_T *argvars, typval_T *rettv);
169#endif
170static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
171static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
172static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
173static void f_getcwd(typval_T *argvars, typval_T *rettv);
174static void f_getfontname(typval_T *argvars, typval_T *rettv);
175static void f_getfperm(typval_T *argvars, typval_T *rettv);
176static void f_getfsize(typval_T *argvars, typval_T *rettv);
177static void f_getftime(typval_T *argvars, typval_T *rettv);
178static void f_getftype(typval_T *argvars, typval_T *rettv);
179static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200180static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200181static void f_getmatches(typval_T *argvars, typval_T *rettv);
182static void f_getpid(typval_T *argvars, typval_T *rettv);
183static void f_getcurpos(typval_T *argvars, typval_T *rettv);
184static void f_getpos(typval_T *argvars, typval_T *rettv);
185static void f_getqflist(typval_T *argvars, typval_T *rettv);
186static void f_getreg(typval_T *argvars, typval_T *rettv);
187static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200188static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200189static void f_gettabvar(typval_T *argvars, typval_T *rettv);
190static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200191static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200192static void f_getwinposx(typval_T *argvars, typval_T *rettv);
193static void f_getwinposy(typval_T *argvars, typval_T *rettv);
194static void f_getwinvar(typval_T *argvars, typval_T *rettv);
195static void f_glob(typval_T *argvars, typval_T *rettv);
196static void f_globpath(typval_T *argvars, typval_T *rettv);
197static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
198static void f_has(typval_T *argvars, typval_T *rettv);
199static void f_has_key(typval_T *argvars, typval_T *rettv);
200static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
201static void f_hasmapto(typval_T *argvars, typval_T *rettv);
202static void f_histadd(typval_T *argvars, typval_T *rettv);
203static void f_histdel(typval_T *argvars, typval_T *rettv);
204static void f_histget(typval_T *argvars, typval_T *rettv);
205static void f_histnr(typval_T *argvars, typval_T *rettv);
206static void f_hlID(typval_T *argvars, typval_T *rettv);
207static void f_hlexists(typval_T *argvars, typval_T *rettv);
208static void f_hostname(typval_T *argvars, typval_T *rettv);
209static void f_iconv(typval_T *argvars, typval_T *rettv);
210static void f_indent(typval_T *argvars, typval_T *rettv);
211static void f_index(typval_T *argvars, typval_T *rettv);
212static void f_input(typval_T *argvars, typval_T *rettv);
213static void f_inputdialog(typval_T *argvars, typval_T *rettv);
214static void f_inputlist(typval_T *argvars, typval_T *rettv);
215static void f_inputrestore(typval_T *argvars, typval_T *rettv);
216static void f_inputsave(typval_T *argvars, typval_T *rettv);
217static void f_inputsecret(typval_T *argvars, typval_T *rettv);
218static void f_insert(typval_T *argvars, typval_T *rettv);
219static void f_invert(typval_T *argvars, typval_T *rettv);
220static void f_isdirectory(typval_T *argvars, typval_T *rettv);
221static void f_islocked(typval_T *argvars, typval_T *rettv);
222#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
223static void f_isnan(typval_T *argvars, typval_T *rettv);
224#endif
225static void f_items(typval_T *argvars, typval_T *rettv);
226#ifdef FEAT_JOB_CHANNEL
227static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
228static void f_job_info(typval_T *argvars, typval_T *rettv);
229static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
230static void f_job_start(typval_T *argvars, typval_T *rettv);
231static void f_job_stop(typval_T *argvars, typval_T *rettv);
232static void f_job_status(typval_T *argvars, typval_T *rettv);
233#endif
234static void f_join(typval_T *argvars, typval_T *rettv);
235static void f_js_decode(typval_T *argvars, typval_T *rettv);
236static void f_js_encode(typval_T *argvars, typval_T *rettv);
237static void f_json_decode(typval_T *argvars, typval_T *rettv);
238static void f_json_encode(typval_T *argvars, typval_T *rettv);
239static void f_keys(typval_T *argvars, typval_T *rettv);
240static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
241static void f_len(typval_T *argvars, typval_T *rettv);
242static void f_libcall(typval_T *argvars, typval_T *rettv);
243static void f_libcallnr(typval_T *argvars, typval_T *rettv);
244static void f_line(typval_T *argvars, typval_T *rettv);
245static void f_line2byte(typval_T *argvars, typval_T *rettv);
246static void f_lispindent(typval_T *argvars, typval_T *rettv);
247static void f_localtime(typval_T *argvars, typval_T *rettv);
248#ifdef FEAT_FLOAT
249static void f_log(typval_T *argvars, typval_T *rettv);
250static void f_log10(typval_T *argvars, typval_T *rettv);
251#endif
252#ifdef FEAT_LUA
253static void f_luaeval(typval_T *argvars, typval_T *rettv);
254#endif
255static void f_map(typval_T *argvars, typval_T *rettv);
256static void f_maparg(typval_T *argvars, typval_T *rettv);
257static void f_mapcheck(typval_T *argvars, typval_T *rettv);
258static void f_match(typval_T *argvars, typval_T *rettv);
259static void f_matchadd(typval_T *argvars, typval_T *rettv);
260static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
261static void f_matcharg(typval_T *argvars, typval_T *rettv);
262static void f_matchdelete(typval_T *argvars, typval_T *rettv);
263static void f_matchend(typval_T *argvars, typval_T *rettv);
264static void f_matchlist(typval_T *argvars, typval_T *rettv);
265static void f_matchstr(typval_T *argvars, typval_T *rettv);
266static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
267static void f_max(typval_T *argvars, typval_T *rettv);
268static void f_min(typval_T *argvars, typval_T *rettv);
269#ifdef vim_mkdir
270static void f_mkdir(typval_T *argvars, typval_T *rettv);
271#endif
272static void f_mode(typval_T *argvars, typval_T *rettv);
273#ifdef FEAT_MZSCHEME
274static void f_mzeval(typval_T *argvars, typval_T *rettv);
275#endif
276static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
277static void f_nr2char(typval_T *argvars, typval_T *rettv);
278static void f_or(typval_T *argvars, typval_T *rettv);
279static void f_pathshorten(typval_T *argvars, typval_T *rettv);
280#ifdef FEAT_PERL
281static void f_perleval(typval_T *argvars, typval_T *rettv);
282#endif
283#ifdef FEAT_FLOAT
284static void f_pow(typval_T *argvars, typval_T *rettv);
285#endif
286static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
287static void f_printf(typval_T *argvars, typval_T *rettv);
288static void f_pumvisible(typval_T *argvars, typval_T *rettv);
289#ifdef FEAT_PYTHON3
290static void f_py3eval(typval_T *argvars, typval_T *rettv);
291#endif
292#ifdef FEAT_PYTHON
293static void f_pyeval(typval_T *argvars, typval_T *rettv);
294#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100295#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
296static void f_pyxeval(typval_T *argvars, typval_T *rettv);
297#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200298static void f_range(typval_T *argvars, typval_T *rettv);
299static void f_readfile(typval_T *argvars, typval_T *rettv);
300static void f_reltime(typval_T *argvars, typval_T *rettv);
301#ifdef FEAT_FLOAT
302static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
303#endif
304static void f_reltimestr(typval_T *argvars, typval_T *rettv);
305static void f_remote_expr(typval_T *argvars, typval_T *rettv);
306static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
307static void f_remote_peek(typval_T *argvars, typval_T *rettv);
308static void f_remote_read(typval_T *argvars, typval_T *rettv);
309static void f_remote_send(typval_T *argvars, typval_T *rettv);
310static void f_remove(typval_T *argvars, typval_T *rettv);
311static void f_rename(typval_T *argvars, typval_T *rettv);
312static void f_repeat(typval_T *argvars, typval_T *rettv);
313static void f_resolve(typval_T *argvars, typval_T *rettv);
314static void f_reverse(typval_T *argvars, typval_T *rettv);
315#ifdef FEAT_FLOAT
316static void f_round(typval_T *argvars, typval_T *rettv);
317#endif
318static void f_screenattr(typval_T *argvars, typval_T *rettv);
319static void f_screenchar(typval_T *argvars, typval_T *rettv);
320static void f_screencol(typval_T *argvars, typval_T *rettv);
321static void f_screenrow(typval_T *argvars, typval_T *rettv);
322static void f_search(typval_T *argvars, typval_T *rettv);
323static void f_searchdecl(typval_T *argvars, typval_T *rettv);
324static void f_searchpair(typval_T *argvars, typval_T *rettv);
325static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
326static void f_searchpos(typval_T *argvars, typval_T *rettv);
327static void f_server2client(typval_T *argvars, typval_T *rettv);
328static void f_serverlist(typval_T *argvars, typval_T *rettv);
329static void f_setbufvar(typval_T *argvars, typval_T *rettv);
330static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
331static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
332static void f_setfperm(typval_T *argvars, typval_T *rettv);
333static void f_setline(typval_T *argvars, typval_T *rettv);
334static void f_setloclist(typval_T *argvars, typval_T *rettv);
335static void f_setmatches(typval_T *argvars, typval_T *rettv);
336static void f_setpos(typval_T *argvars, typval_T *rettv);
337static void f_setqflist(typval_T *argvars, typval_T *rettv);
338static void f_setreg(typval_T *argvars, typval_T *rettv);
339static void f_settabvar(typval_T *argvars, typval_T *rettv);
340static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
341static void f_setwinvar(typval_T *argvars, typval_T *rettv);
342#ifdef FEAT_CRYPT
343static void f_sha256(typval_T *argvars, typval_T *rettv);
344#endif /* FEAT_CRYPT */
345static void f_shellescape(typval_T *argvars, typval_T *rettv);
346static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
347static void f_simplify(typval_T *argvars, typval_T *rettv);
348#ifdef FEAT_FLOAT
349static void f_sin(typval_T *argvars, typval_T *rettv);
350static void f_sinh(typval_T *argvars, typval_T *rettv);
351#endif
352static void f_sort(typval_T *argvars, typval_T *rettv);
353static void f_soundfold(typval_T *argvars, typval_T *rettv);
354static void f_spellbadword(typval_T *argvars, typval_T *rettv);
355static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
356static void f_split(typval_T *argvars, typval_T *rettv);
357#ifdef FEAT_FLOAT
358static void f_sqrt(typval_T *argvars, typval_T *rettv);
359static void f_str2float(typval_T *argvars, typval_T *rettv);
360#endif
361static void f_str2nr(typval_T *argvars, typval_T *rettv);
362static void f_strchars(typval_T *argvars, typval_T *rettv);
363#ifdef HAVE_STRFTIME
364static void f_strftime(typval_T *argvars, typval_T *rettv);
365#endif
366static void f_strgetchar(typval_T *argvars, typval_T *rettv);
367static void f_stridx(typval_T *argvars, typval_T *rettv);
368static void f_string(typval_T *argvars, typval_T *rettv);
369static void f_strlen(typval_T *argvars, typval_T *rettv);
370static void f_strcharpart(typval_T *argvars, typval_T *rettv);
371static void f_strpart(typval_T *argvars, typval_T *rettv);
372static void f_strridx(typval_T *argvars, typval_T *rettv);
373static void f_strtrans(typval_T *argvars, typval_T *rettv);
374static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
375static void f_strwidth(typval_T *argvars, typval_T *rettv);
376static void f_submatch(typval_T *argvars, typval_T *rettv);
377static void f_substitute(typval_T *argvars, typval_T *rettv);
378static void f_synID(typval_T *argvars, typval_T *rettv);
379static void f_synIDattr(typval_T *argvars, typval_T *rettv);
380static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
381static void f_synstack(typval_T *argvars, typval_T *rettv);
382static void f_synconcealed(typval_T *argvars, typval_T *rettv);
383static void f_system(typval_T *argvars, typval_T *rettv);
384static void f_systemlist(typval_T *argvars, typval_T *rettv);
385static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
386static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
387static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
388static void f_taglist(typval_T *argvars, typval_T *rettv);
389static void f_tagfiles(typval_T *argvars, typval_T *rettv);
390static void f_tempname(typval_T *argvars, typval_T *rettv);
391static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
392static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
393static void f_test_disable_char_avail(typval_T *argvars, typval_T *rettv);
394static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100395static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200396#ifdef FEAT_JOB_CHANNEL
397static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
398#endif
399static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
400#ifdef FEAT_JOB_CHANNEL
401static void f_test_null_job(typval_T *argvars, typval_T *rettv);
402#endif
403static void f_test_null_list(typval_T *argvars, typval_T *rettv);
404static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
405static void f_test_null_string(typval_T *argvars, typval_T *rettv);
406static void f_test_settime(typval_T *argvars, typval_T *rettv);
407#ifdef FEAT_FLOAT
408static void f_tan(typval_T *argvars, typval_T *rettv);
409static void f_tanh(typval_T *argvars, typval_T *rettv);
410#endif
411#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200412static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200413static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200414static void f_timer_start(typval_T *argvars, typval_T *rettv);
415static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200416static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200417#endif
418static void f_tolower(typval_T *argvars, typval_T *rettv);
419static void f_toupper(typval_T *argvars, typval_T *rettv);
420static void f_tr(typval_T *argvars, typval_T *rettv);
421#ifdef FEAT_FLOAT
422static void f_trunc(typval_T *argvars, typval_T *rettv);
423#endif
424static void f_type(typval_T *argvars, typval_T *rettv);
425static void f_undofile(typval_T *argvars, typval_T *rettv);
426static void f_undotree(typval_T *argvars, typval_T *rettv);
427static void f_uniq(typval_T *argvars, typval_T *rettv);
428static void f_values(typval_T *argvars, typval_T *rettv);
429static void f_virtcol(typval_T *argvars, typval_T *rettv);
430static void f_visualmode(typval_T *argvars, typval_T *rettv);
431static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
432static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
433static void f_win_getid(typval_T *argvars, typval_T *rettv);
434static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
435static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
436static void f_win_id2win(typval_T *argvars, typval_T *rettv);
437static void f_winbufnr(typval_T *argvars, typval_T *rettv);
438static void f_wincol(typval_T *argvars, typval_T *rettv);
439static void f_winheight(typval_T *argvars, typval_T *rettv);
440static void f_winline(typval_T *argvars, typval_T *rettv);
441static void f_winnr(typval_T *argvars, typval_T *rettv);
442static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
443static void f_winrestview(typval_T *argvars, typval_T *rettv);
444static void f_winsaveview(typval_T *argvars, typval_T *rettv);
445static void f_winwidth(typval_T *argvars, typval_T *rettv);
446static void f_writefile(typval_T *argvars, typval_T *rettv);
447static void f_wordcount(typval_T *argvars, typval_T *rettv);
448static void f_xor(typval_T *argvars, typval_T *rettv);
449
450/*
451 * Array with names and number of arguments of all internal functions
452 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
453 */
454static struct fst
455{
456 char *f_name; /* function name */
457 char f_min_argc; /* minimal number of arguments */
458 char f_max_argc; /* maximal number of arguments */
459 void (*f_func)(typval_T *args, typval_T *rvar);
460 /* implementation of function */
461} functions[] =
462{
463#ifdef FEAT_FLOAT
464 {"abs", 1, 1, f_abs},
465 {"acos", 1, 1, f_acos}, /* WJMc */
466#endif
467 {"add", 2, 2, f_add},
468 {"and", 2, 2, f_and},
469 {"append", 2, 2, f_append},
470 {"argc", 0, 0, f_argc},
471 {"argidx", 0, 0, f_argidx},
472 {"arglistid", 0, 2, f_arglistid},
473 {"argv", 0, 1, f_argv},
474#ifdef FEAT_FLOAT
475 {"asin", 1, 1, f_asin}, /* WJMc */
476#endif
477 {"assert_equal", 2, 3, f_assert_equal},
478 {"assert_exception", 1, 2, f_assert_exception},
479 {"assert_fails", 1, 2, f_assert_fails},
480 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100481 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200482 {"assert_match", 2, 3, f_assert_match},
483 {"assert_notequal", 2, 3, f_assert_notequal},
484 {"assert_notmatch", 2, 3, f_assert_notmatch},
485 {"assert_true", 1, 2, f_assert_true},
486#ifdef FEAT_FLOAT
487 {"atan", 1, 1, f_atan},
488 {"atan2", 2, 2, f_atan2},
489#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100490#ifdef FEAT_BEVAL
491 {"balloon_show", 1, 1, f_balloon_show},
492#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200493 {"browse", 4, 4, f_browse},
494 {"browsedir", 2, 2, f_browsedir},
495 {"bufexists", 1, 1, f_bufexists},
496 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
497 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
498 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
499 {"buflisted", 1, 1, f_buflisted},
500 {"bufloaded", 1, 1, f_bufloaded},
501 {"bufname", 1, 1, f_bufname},
502 {"bufnr", 1, 2, f_bufnr},
503 {"bufwinid", 1, 1, f_bufwinid},
504 {"bufwinnr", 1, 1, f_bufwinnr},
505 {"byte2line", 1, 1, f_byte2line},
506 {"byteidx", 2, 2, f_byteidx},
507 {"byteidxcomp", 2, 2, f_byteidxcomp},
508 {"call", 2, 3, f_call},
509#ifdef FEAT_FLOAT
510 {"ceil", 1, 1, f_ceil},
511#endif
512#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100513 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200514 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200515 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200516 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
517 {"ch_evalraw", 2, 3, f_ch_evalraw},
518 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
519 {"ch_getjob", 1, 1, f_ch_getjob},
520 {"ch_info", 1, 1, f_ch_info},
521 {"ch_log", 1, 2, f_ch_log},
522 {"ch_logfile", 1, 2, f_ch_logfile},
523 {"ch_open", 1, 2, f_ch_open},
524 {"ch_read", 1, 2, f_ch_read},
525 {"ch_readraw", 1, 2, f_ch_readraw},
526 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
527 {"ch_sendraw", 2, 3, f_ch_sendraw},
528 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200529 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200530#endif
531 {"changenr", 0, 0, f_changenr},
532 {"char2nr", 1, 2, f_char2nr},
533 {"cindent", 1, 1, f_cindent},
534 {"clearmatches", 0, 0, f_clearmatches},
535 {"col", 1, 1, f_col},
536#if defined(FEAT_INS_EXPAND)
537 {"complete", 2, 2, f_complete},
538 {"complete_add", 1, 1, f_complete_add},
539 {"complete_check", 0, 0, f_complete_check},
540#endif
541 {"confirm", 1, 4, f_confirm},
542 {"copy", 1, 1, f_copy},
543#ifdef FEAT_FLOAT
544 {"cos", 1, 1, f_cos},
545 {"cosh", 1, 1, f_cosh},
546#endif
547 {"count", 2, 4, f_count},
548 {"cscope_connection",0,3, f_cscope_connection},
549 {"cursor", 1, 3, f_cursor},
550 {"deepcopy", 1, 2, f_deepcopy},
551 {"delete", 1, 2, f_delete},
552 {"did_filetype", 0, 0, f_did_filetype},
553 {"diff_filler", 1, 1, f_diff_filler},
554 {"diff_hlID", 2, 2, f_diff_hlID},
555 {"empty", 1, 1, f_empty},
556 {"escape", 2, 2, f_escape},
557 {"eval", 1, 1, f_eval},
558 {"eventhandler", 0, 0, f_eventhandler},
559 {"executable", 1, 1, f_executable},
560 {"execute", 1, 2, f_execute},
561 {"exepath", 1, 1, f_exepath},
562 {"exists", 1, 1, f_exists},
563#ifdef FEAT_FLOAT
564 {"exp", 1, 1, f_exp},
565#endif
566 {"expand", 1, 3, f_expand},
567 {"extend", 2, 3, f_extend},
568 {"feedkeys", 1, 2, f_feedkeys},
569 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
570 {"filereadable", 1, 1, f_filereadable},
571 {"filewritable", 1, 1, f_filewritable},
572 {"filter", 2, 2, f_filter},
573 {"finddir", 1, 3, f_finddir},
574 {"findfile", 1, 3, f_findfile},
575#ifdef FEAT_FLOAT
576 {"float2nr", 1, 1, f_float2nr},
577 {"floor", 1, 1, f_floor},
578 {"fmod", 2, 2, f_fmod},
579#endif
580 {"fnameescape", 1, 1, f_fnameescape},
581 {"fnamemodify", 2, 2, f_fnamemodify},
582 {"foldclosed", 1, 1, f_foldclosed},
583 {"foldclosedend", 1, 1, f_foldclosedend},
584 {"foldlevel", 1, 1, f_foldlevel},
585 {"foldtext", 0, 0, f_foldtext},
586 {"foldtextresult", 1, 1, f_foldtextresult},
587 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200588 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200589 {"function", 1, 3, f_function},
590 {"garbagecollect", 0, 1, f_garbagecollect},
591 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200592 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200593 {"getbufline", 2, 3, f_getbufline},
594 {"getbufvar", 2, 3, f_getbufvar},
595 {"getchar", 0, 1, f_getchar},
596 {"getcharmod", 0, 0, f_getcharmod},
597 {"getcharsearch", 0, 0, f_getcharsearch},
598 {"getcmdline", 0, 0, f_getcmdline},
599 {"getcmdpos", 0, 0, f_getcmdpos},
600 {"getcmdtype", 0, 0, f_getcmdtype},
601 {"getcmdwintype", 0, 0, f_getcmdwintype},
602#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200603 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200604#endif
605 {"getcurpos", 0, 0, f_getcurpos},
606 {"getcwd", 0, 2, f_getcwd},
607 {"getfontname", 0, 1, f_getfontname},
608 {"getfperm", 1, 1, f_getfperm},
609 {"getfsize", 1, 1, f_getfsize},
610 {"getftime", 1, 1, f_getftime},
611 {"getftype", 1, 1, f_getftype},
612 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200613 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200614 {"getmatches", 0, 0, f_getmatches},
615 {"getpid", 0, 0, f_getpid},
616 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200617 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200618 {"getreg", 0, 3, f_getreg},
619 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200620 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200621 {"gettabvar", 2, 3, f_gettabvar},
622 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200623 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200624 {"getwinposx", 0, 0, f_getwinposx},
625 {"getwinposy", 0, 0, f_getwinposy},
626 {"getwinvar", 2, 3, f_getwinvar},
627 {"glob", 1, 4, f_glob},
628 {"glob2regpat", 1, 1, f_glob2regpat},
629 {"globpath", 2, 5, f_globpath},
630 {"has", 1, 1, f_has},
631 {"has_key", 2, 2, f_has_key},
632 {"haslocaldir", 0, 2, f_haslocaldir},
633 {"hasmapto", 1, 3, f_hasmapto},
634 {"highlightID", 1, 1, f_hlID}, /* obsolete */
635 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
636 {"histadd", 2, 2, f_histadd},
637 {"histdel", 1, 2, f_histdel},
638 {"histget", 1, 2, f_histget},
639 {"histnr", 1, 1, f_histnr},
640 {"hlID", 1, 1, f_hlID},
641 {"hlexists", 1, 1, f_hlexists},
642 {"hostname", 0, 0, f_hostname},
643 {"iconv", 3, 3, f_iconv},
644 {"indent", 1, 1, f_indent},
645 {"index", 2, 4, f_index},
646 {"input", 1, 3, f_input},
647 {"inputdialog", 1, 3, f_inputdialog},
648 {"inputlist", 1, 1, f_inputlist},
649 {"inputrestore", 0, 0, f_inputrestore},
650 {"inputsave", 0, 0, f_inputsave},
651 {"inputsecret", 1, 2, f_inputsecret},
652 {"insert", 2, 3, f_insert},
653 {"invert", 1, 1, f_invert},
654 {"isdirectory", 1, 1, f_isdirectory},
655 {"islocked", 1, 1, f_islocked},
656#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
657 {"isnan", 1, 1, f_isnan},
658#endif
659 {"items", 1, 1, f_items},
660#ifdef FEAT_JOB_CHANNEL
661 {"job_getchannel", 1, 1, f_job_getchannel},
662 {"job_info", 1, 1, f_job_info},
663 {"job_setoptions", 2, 2, f_job_setoptions},
664 {"job_start", 1, 2, f_job_start},
665 {"job_status", 1, 1, f_job_status},
666 {"job_stop", 1, 2, f_job_stop},
667#endif
668 {"join", 1, 2, f_join},
669 {"js_decode", 1, 1, f_js_decode},
670 {"js_encode", 1, 1, f_js_encode},
671 {"json_decode", 1, 1, f_json_decode},
672 {"json_encode", 1, 1, f_json_encode},
673 {"keys", 1, 1, f_keys},
674 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
675 {"len", 1, 1, f_len},
676 {"libcall", 3, 3, f_libcall},
677 {"libcallnr", 3, 3, f_libcallnr},
678 {"line", 1, 1, f_line},
679 {"line2byte", 1, 1, f_line2byte},
680 {"lispindent", 1, 1, f_lispindent},
681 {"localtime", 0, 0, f_localtime},
682#ifdef FEAT_FLOAT
683 {"log", 1, 1, f_log},
684 {"log10", 1, 1, f_log10},
685#endif
686#ifdef FEAT_LUA
687 {"luaeval", 1, 2, f_luaeval},
688#endif
689 {"map", 2, 2, f_map},
690 {"maparg", 1, 4, f_maparg},
691 {"mapcheck", 1, 3, f_mapcheck},
692 {"match", 2, 4, f_match},
693 {"matchadd", 2, 5, f_matchadd},
694 {"matchaddpos", 2, 5, f_matchaddpos},
695 {"matcharg", 1, 1, f_matcharg},
696 {"matchdelete", 1, 1, f_matchdelete},
697 {"matchend", 2, 4, f_matchend},
698 {"matchlist", 2, 4, f_matchlist},
699 {"matchstr", 2, 4, f_matchstr},
700 {"matchstrpos", 2, 4, f_matchstrpos},
701 {"max", 1, 1, f_max},
702 {"min", 1, 1, f_min},
703#ifdef vim_mkdir
704 {"mkdir", 1, 3, f_mkdir},
705#endif
706 {"mode", 0, 1, f_mode},
707#ifdef FEAT_MZSCHEME
708 {"mzeval", 1, 1, f_mzeval},
709#endif
710 {"nextnonblank", 1, 1, f_nextnonblank},
711 {"nr2char", 1, 2, f_nr2char},
712 {"or", 2, 2, f_or},
713 {"pathshorten", 1, 1, f_pathshorten},
714#ifdef FEAT_PERL
715 {"perleval", 1, 1, f_perleval},
716#endif
717#ifdef FEAT_FLOAT
718 {"pow", 2, 2, f_pow},
719#endif
720 {"prevnonblank", 1, 1, f_prevnonblank},
721 {"printf", 2, 19, f_printf},
722 {"pumvisible", 0, 0, f_pumvisible},
723#ifdef FEAT_PYTHON3
724 {"py3eval", 1, 1, f_py3eval},
725#endif
726#ifdef FEAT_PYTHON
727 {"pyeval", 1, 1, f_pyeval},
728#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100729#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
730 {"pyxeval", 1, 1, f_pyxeval},
731#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200732 {"range", 1, 3, f_range},
733 {"readfile", 1, 3, f_readfile},
734 {"reltime", 0, 2, f_reltime},
735#ifdef FEAT_FLOAT
736 {"reltimefloat", 1, 1, f_reltimefloat},
737#endif
738 {"reltimestr", 1, 1, f_reltimestr},
739 {"remote_expr", 2, 3, f_remote_expr},
740 {"remote_foreground", 1, 1, f_remote_foreground},
741 {"remote_peek", 1, 2, f_remote_peek},
742 {"remote_read", 1, 1, f_remote_read},
743 {"remote_send", 2, 3, f_remote_send},
744 {"remove", 2, 3, f_remove},
745 {"rename", 2, 2, f_rename},
746 {"repeat", 2, 2, f_repeat},
747 {"resolve", 1, 1, f_resolve},
748 {"reverse", 1, 1, f_reverse},
749#ifdef FEAT_FLOAT
750 {"round", 1, 1, f_round},
751#endif
752 {"screenattr", 2, 2, f_screenattr},
753 {"screenchar", 2, 2, f_screenchar},
754 {"screencol", 0, 0, f_screencol},
755 {"screenrow", 0, 0, f_screenrow},
756 {"search", 1, 4, f_search},
757 {"searchdecl", 1, 3, f_searchdecl},
758 {"searchpair", 3, 7, f_searchpair},
759 {"searchpairpos", 3, 7, f_searchpairpos},
760 {"searchpos", 1, 4, f_searchpos},
761 {"server2client", 2, 2, f_server2client},
762 {"serverlist", 0, 0, f_serverlist},
763 {"setbufvar", 3, 3, f_setbufvar},
764 {"setcharsearch", 1, 1, f_setcharsearch},
765 {"setcmdpos", 1, 1, f_setcmdpos},
766 {"setfperm", 2, 2, f_setfperm},
767 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200768 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200769 {"setmatches", 1, 1, f_setmatches},
770 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200771 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200772 {"setreg", 2, 3, f_setreg},
773 {"settabvar", 3, 3, f_settabvar},
774 {"settabwinvar", 4, 4, f_settabwinvar},
775 {"setwinvar", 3, 3, f_setwinvar},
776#ifdef FEAT_CRYPT
777 {"sha256", 1, 1, f_sha256},
778#endif
779 {"shellescape", 1, 2, f_shellescape},
780 {"shiftwidth", 0, 0, f_shiftwidth},
781 {"simplify", 1, 1, f_simplify},
782#ifdef FEAT_FLOAT
783 {"sin", 1, 1, f_sin},
784 {"sinh", 1, 1, f_sinh},
785#endif
786 {"sort", 1, 3, f_sort},
787 {"soundfold", 1, 1, f_soundfold},
788 {"spellbadword", 0, 1, f_spellbadword},
789 {"spellsuggest", 1, 3, f_spellsuggest},
790 {"split", 1, 3, f_split},
791#ifdef FEAT_FLOAT
792 {"sqrt", 1, 1, f_sqrt},
793 {"str2float", 1, 1, f_str2float},
794#endif
795 {"str2nr", 1, 2, f_str2nr},
796 {"strcharpart", 2, 3, f_strcharpart},
797 {"strchars", 1, 2, f_strchars},
798 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
799#ifdef HAVE_STRFTIME
800 {"strftime", 1, 2, f_strftime},
801#endif
802 {"strgetchar", 2, 2, f_strgetchar},
803 {"stridx", 2, 3, f_stridx},
804 {"string", 1, 1, f_string},
805 {"strlen", 1, 1, f_strlen},
806 {"strpart", 2, 3, f_strpart},
807 {"strridx", 2, 3, f_strridx},
808 {"strtrans", 1, 1, f_strtrans},
809 {"strwidth", 1, 1, f_strwidth},
810 {"submatch", 1, 2, f_submatch},
811 {"substitute", 4, 4, f_substitute},
812 {"synID", 3, 3, f_synID},
813 {"synIDattr", 2, 3, f_synIDattr},
814 {"synIDtrans", 1, 1, f_synIDtrans},
815 {"synconcealed", 2, 2, f_synconcealed},
816 {"synstack", 2, 2, f_synstack},
817 {"system", 1, 2, f_system},
818 {"systemlist", 1, 2, f_systemlist},
819 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
820 {"tabpagenr", 0, 1, f_tabpagenr},
821 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
822 {"tagfiles", 0, 0, f_tagfiles},
823 {"taglist", 1, 1, f_taglist},
824#ifdef FEAT_FLOAT
825 {"tan", 1, 1, f_tan},
826 {"tanh", 1, 1, f_tanh},
827#endif
828 {"tempname", 0, 0, f_tempname},
829 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
830 {"test_autochdir", 0, 0, f_test_autochdir},
831 {"test_disable_char_avail", 1, 1, f_test_disable_char_avail},
832 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100833 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200834#ifdef FEAT_JOB_CHANNEL
835 {"test_null_channel", 0, 0, f_test_null_channel},
836#endif
837 {"test_null_dict", 0, 0, f_test_null_dict},
838#ifdef FEAT_JOB_CHANNEL
839 {"test_null_job", 0, 0, f_test_null_job},
840#endif
841 {"test_null_list", 0, 0, f_test_null_list},
842 {"test_null_partial", 0, 0, f_test_null_partial},
843 {"test_null_string", 0, 0, f_test_null_string},
844 {"test_settime", 1, 1, f_test_settime},
845#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200846 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200847 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200848 {"timer_start", 2, 3, f_timer_start},
849 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200850 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200851#endif
852 {"tolower", 1, 1, f_tolower},
853 {"toupper", 1, 1, f_toupper},
854 {"tr", 3, 3, f_tr},
855#ifdef FEAT_FLOAT
856 {"trunc", 1, 1, f_trunc},
857#endif
858 {"type", 1, 1, f_type},
859 {"undofile", 1, 1, f_undofile},
860 {"undotree", 0, 0, f_undotree},
861 {"uniq", 1, 3, f_uniq},
862 {"values", 1, 1, f_values},
863 {"virtcol", 1, 1, f_virtcol},
864 {"visualmode", 0, 1, f_visualmode},
865 {"wildmenumode", 0, 0, f_wildmenumode},
866 {"win_findbuf", 1, 1, f_win_findbuf},
867 {"win_getid", 0, 2, f_win_getid},
868 {"win_gotoid", 1, 1, f_win_gotoid},
869 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
870 {"win_id2win", 1, 1, f_win_id2win},
871 {"winbufnr", 1, 1, f_winbufnr},
872 {"wincol", 0, 0, f_wincol},
873 {"winheight", 1, 1, f_winheight},
874 {"winline", 0, 0, f_winline},
875 {"winnr", 0, 1, f_winnr},
876 {"winrestcmd", 0, 0, f_winrestcmd},
877 {"winrestview", 1, 1, f_winrestview},
878 {"winsaveview", 0, 0, f_winsaveview},
879 {"winwidth", 1, 1, f_winwidth},
880 {"wordcount", 0, 0, f_wordcount},
881 {"writefile", 2, 3, f_writefile},
882 {"xor", 2, 2, f_xor},
883};
884
885#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
886
887/*
888 * Function given to ExpandGeneric() to obtain the list of internal
889 * or user defined function names.
890 */
891 char_u *
892get_function_name(expand_T *xp, int idx)
893{
894 static int intidx = -1;
895 char_u *name;
896
897 if (idx == 0)
898 intidx = -1;
899 if (intidx < 0)
900 {
901 name = get_user_func_name(xp, idx);
902 if (name != NULL)
903 return name;
904 }
905 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
906 {
907 STRCPY(IObuff, functions[intidx].f_name);
908 STRCAT(IObuff, "(");
909 if (functions[intidx].f_max_argc == 0)
910 STRCAT(IObuff, ")");
911 return IObuff;
912 }
913
914 return NULL;
915}
916
917/*
918 * Function given to ExpandGeneric() to obtain the list of internal or
919 * user defined variable or function names.
920 */
921 char_u *
922get_expr_name(expand_T *xp, int idx)
923{
924 static int intidx = -1;
925 char_u *name;
926
927 if (idx == 0)
928 intidx = -1;
929 if (intidx < 0)
930 {
931 name = get_function_name(xp, idx);
932 if (name != NULL)
933 return name;
934 }
935 return get_user_var_name(xp, ++intidx);
936}
937
938#endif /* FEAT_CMDL_COMPL */
939
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200940/*
941 * Find internal function in table above.
942 * Return index, or -1 if not found
943 */
944 int
945find_internal_func(
946 char_u *name) /* name of the function */
947{
948 int first = 0;
949 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
950 int cmp;
951 int x;
952
953 /*
954 * Find the function name in the table. Binary search.
955 */
956 while (first <= last)
957 {
958 x = first + ((unsigned)(last - first) >> 1);
959 cmp = STRCMP(name, functions[x].f_name);
960 if (cmp < 0)
961 last = x - 1;
962 else if (cmp > 0)
963 first = x + 1;
964 else
965 return x;
966 }
967 return -1;
968}
969
970 int
971call_internal_func(
972 char_u *name,
973 int argcount,
974 typval_T *argvars,
975 typval_T *rettv)
976{
977 int i;
978
979 i = find_internal_func(name);
980 if (i < 0)
981 return ERROR_UNKNOWN;
982 if (argcount < functions[i].f_min_argc)
983 return ERROR_TOOFEW;
984 if (argcount > functions[i].f_max_argc)
985 return ERROR_TOOMANY;
986 argvars[argcount].v_type = VAR_UNKNOWN;
987 functions[i].f_func(argvars, rettv);
988 return ERROR_NONE;
989}
990
991/*
992 * Return TRUE for a non-zero Number and a non-empty String.
993 */
994 static int
995non_zero_arg(typval_T *argvars)
996{
997 return ((argvars[0].v_type == VAR_NUMBER
998 && argvars[0].vval.v_number != 0)
999 || (argvars[0].v_type == VAR_SPECIAL
1000 && argvars[0].vval.v_number == VVAL_TRUE)
1001 || (argvars[0].v_type == VAR_STRING
1002 && argvars[0].vval.v_string != NULL
1003 && *argvars[0].vval.v_string != NUL));
1004}
1005
1006/*
1007 * Get the lnum from the first argument.
1008 * Also accepts ".", "$", etc., but that only works for the current buffer.
1009 * Returns -1 on error.
1010 */
1011 static linenr_T
1012get_tv_lnum(typval_T *argvars)
1013{
1014 typval_T rettv;
1015 linenr_T lnum;
1016
1017 lnum = (linenr_T)get_tv_number_chk(&argvars[0], NULL);
1018 if (lnum == 0) /* no valid number, try using line() */
1019 {
1020 rettv.v_type = VAR_NUMBER;
1021 f_line(argvars, &rettv);
1022 lnum = (linenr_T)rettv.vval.v_number;
1023 clear_tv(&rettv);
1024 }
1025 return lnum;
1026}
1027
1028#ifdef FEAT_FLOAT
1029static int get_float_arg(typval_T *argvars, float_T *f);
1030
1031/*
1032 * Get the float value of "argvars[0]" into "f".
1033 * Returns FAIL when the argument is not a Number or Float.
1034 */
1035 static int
1036get_float_arg(typval_T *argvars, float_T *f)
1037{
1038 if (argvars[0].v_type == VAR_FLOAT)
1039 {
1040 *f = argvars[0].vval.v_float;
1041 return OK;
1042 }
1043 if (argvars[0].v_type == VAR_NUMBER)
1044 {
1045 *f = (float_T)argvars[0].vval.v_number;
1046 return OK;
1047 }
1048 EMSG(_("E808: Number or Float required"));
1049 return FAIL;
1050}
1051
1052/*
1053 * "abs(expr)" function
1054 */
1055 static void
1056f_abs(typval_T *argvars, typval_T *rettv)
1057{
1058 if (argvars[0].v_type == VAR_FLOAT)
1059 {
1060 rettv->v_type = VAR_FLOAT;
1061 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1062 }
1063 else
1064 {
1065 varnumber_T n;
1066 int error = FALSE;
1067
1068 n = get_tv_number_chk(&argvars[0], &error);
1069 if (error)
1070 rettv->vval.v_number = -1;
1071 else if (n > 0)
1072 rettv->vval.v_number = n;
1073 else
1074 rettv->vval.v_number = -n;
1075 }
1076}
1077
1078/*
1079 * "acos()" function
1080 */
1081 static void
1082f_acos(typval_T *argvars, typval_T *rettv)
1083{
1084 float_T f = 0.0;
1085
1086 rettv->v_type = VAR_FLOAT;
1087 if (get_float_arg(argvars, &f) == OK)
1088 rettv->vval.v_float = acos(f);
1089 else
1090 rettv->vval.v_float = 0.0;
1091}
1092#endif
1093
1094/*
1095 * "add(list, item)" function
1096 */
1097 static void
1098f_add(typval_T *argvars, typval_T *rettv)
1099{
1100 list_T *l;
1101
1102 rettv->vval.v_number = 1; /* Default: Failed */
1103 if (argvars[0].v_type == VAR_LIST)
1104 {
1105 if ((l = argvars[0].vval.v_list) != NULL
1106 && !tv_check_lock(l->lv_lock,
1107 (char_u *)N_("add() argument"), TRUE)
1108 && list_append_tv(l, &argvars[1]) == OK)
1109 copy_tv(&argvars[0], rettv);
1110 }
1111 else
1112 EMSG(_(e_listreq));
1113}
1114
1115/*
1116 * "and(expr, expr)" function
1117 */
1118 static void
1119f_and(typval_T *argvars, typval_T *rettv)
1120{
1121 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
1122 & get_tv_number_chk(&argvars[1], NULL);
1123}
1124
1125/*
1126 * "append(lnum, string/list)" function
1127 */
1128 static void
1129f_append(typval_T *argvars, typval_T *rettv)
1130{
1131 long lnum;
1132 char_u *line;
1133 list_T *l = NULL;
1134 listitem_T *li = NULL;
1135 typval_T *tv;
1136 long added = 0;
1137
1138 /* When coming here from Insert mode, sync undo, so that this can be
1139 * undone separately from what was previously inserted. */
1140 if (u_sync_once == 2)
1141 {
1142 u_sync_once = 1; /* notify that u_sync() was called */
1143 u_sync(TRUE);
1144 }
1145
1146 lnum = get_tv_lnum(argvars);
1147 if (lnum >= 0
1148 && lnum <= curbuf->b_ml.ml_line_count
1149 && u_save(lnum, lnum + 1) == OK)
1150 {
1151 if (argvars[1].v_type == VAR_LIST)
1152 {
1153 l = argvars[1].vval.v_list;
1154 if (l == NULL)
1155 return;
1156 li = l->lv_first;
1157 }
1158 for (;;)
1159 {
1160 if (l == NULL)
1161 tv = &argvars[1]; /* append a string */
1162 else if (li == NULL)
1163 break; /* end of list */
1164 else
1165 tv = &li->li_tv; /* append item from list */
1166 line = get_tv_string_chk(tv);
1167 if (line == NULL) /* type error */
1168 {
1169 rettv->vval.v_number = 1; /* Failed */
1170 break;
1171 }
1172 ml_append(lnum + added, line, (colnr_T)0, FALSE);
1173 ++added;
1174 if (l == NULL)
1175 break;
1176 li = li->li_next;
1177 }
1178
1179 appended_lines_mark(lnum, added);
1180 if (curwin->w_cursor.lnum > lnum)
1181 curwin->w_cursor.lnum += added;
1182 }
1183 else
1184 rettv->vval.v_number = 1; /* Failed */
1185}
1186
1187/*
1188 * "argc()" function
1189 */
1190 static void
1191f_argc(typval_T *argvars UNUSED, typval_T *rettv)
1192{
1193 rettv->vval.v_number = ARGCOUNT;
1194}
1195
1196/*
1197 * "argidx()" function
1198 */
1199 static void
1200f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1201{
1202 rettv->vval.v_number = curwin->w_arg_idx;
1203}
1204
1205/*
1206 * "arglistid()" function
1207 */
1208 static void
1209f_arglistid(typval_T *argvars, typval_T *rettv)
1210{
1211 win_T *wp;
1212
1213 rettv->vval.v_number = -1;
1214 wp = find_tabwin(&argvars[0], &argvars[1]);
1215 if (wp != NULL)
1216 rettv->vval.v_number = wp->w_alist->id;
1217}
1218
1219/*
1220 * "argv(nr)" function
1221 */
1222 static void
1223f_argv(typval_T *argvars, typval_T *rettv)
1224{
1225 int idx;
1226
1227 if (argvars[0].v_type != VAR_UNKNOWN)
1228 {
1229 idx = (int)get_tv_number_chk(&argvars[0], NULL);
1230 if (idx >= 0 && idx < ARGCOUNT)
1231 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
1232 else
1233 rettv->vval.v_string = NULL;
1234 rettv->v_type = VAR_STRING;
1235 }
1236 else if (rettv_list_alloc(rettv) == OK)
1237 for (idx = 0; idx < ARGCOUNT; ++idx)
1238 list_append_string(rettv->vval.v_list,
1239 alist_name(&ARGLIST[idx]), -1);
1240}
1241
1242/*
1243 * "assert_equal(expected, actual[, msg])" function
1244 */
1245 static void
1246f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
1247{
1248 assert_equal_common(argvars, ASSERT_EQUAL);
1249}
1250
1251/*
1252 * "assert_notequal(expected, actual[, msg])" function
1253 */
1254 static void
1255f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED)
1256{
1257 assert_equal_common(argvars, ASSERT_NOTEQUAL);
1258}
1259
1260/*
1261 * "assert_exception(string[, msg])" function
1262 */
1263 static void
1264f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
1265{
1266 assert_exception(argvars);
1267}
1268
1269/*
1270 * "assert_fails(cmd [, error])" function
1271 */
1272 static void
1273f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
1274{
1275 assert_fails(argvars);
1276}
1277
1278/*
1279 * "assert_false(actual[, msg])" function
1280 */
1281 static void
1282f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
1283{
1284 assert_bool(argvars, FALSE);
1285}
1286
1287/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001288 * "assert_inrange(lower, upper[, msg])" function
1289 */
1290 static void
1291f_assert_inrange(typval_T *argvars, typval_T *rettv UNUSED)
1292{
1293 assert_inrange(argvars);
1294}
1295
1296/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001297 * "assert_match(pattern, actual[, msg])" function
1298 */
1299 static void
1300f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
1301{
1302 assert_match_common(argvars, ASSERT_MATCH);
1303}
1304
1305/*
1306 * "assert_notmatch(pattern, actual[, msg])" function
1307 */
1308 static void
1309f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED)
1310{
1311 assert_match_common(argvars, ASSERT_NOTMATCH);
1312}
1313
1314/*
1315 * "assert_true(actual[, msg])" function
1316 */
1317 static void
1318f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
1319{
1320 assert_bool(argvars, TRUE);
1321}
1322
1323#ifdef FEAT_FLOAT
1324/*
1325 * "asin()" function
1326 */
1327 static void
1328f_asin(typval_T *argvars, typval_T *rettv)
1329{
1330 float_T f = 0.0;
1331
1332 rettv->v_type = VAR_FLOAT;
1333 if (get_float_arg(argvars, &f) == OK)
1334 rettv->vval.v_float = asin(f);
1335 else
1336 rettv->vval.v_float = 0.0;
1337}
1338
1339/*
1340 * "atan()" function
1341 */
1342 static void
1343f_atan(typval_T *argvars, typval_T *rettv)
1344{
1345 float_T f = 0.0;
1346
1347 rettv->v_type = VAR_FLOAT;
1348 if (get_float_arg(argvars, &f) == OK)
1349 rettv->vval.v_float = atan(f);
1350 else
1351 rettv->vval.v_float = 0.0;
1352}
1353
1354/*
1355 * "atan2()" function
1356 */
1357 static void
1358f_atan2(typval_T *argvars, typval_T *rettv)
1359{
1360 float_T fx = 0.0, fy = 0.0;
1361
1362 rettv->v_type = VAR_FLOAT;
1363 if (get_float_arg(argvars, &fx) == OK
1364 && get_float_arg(&argvars[1], &fy) == OK)
1365 rettv->vval.v_float = atan2(fx, fy);
1366 else
1367 rettv->vval.v_float = 0.0;
1368}
1369#endif
1370
1371/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001372 * "balloon_show()" function
1373 */
1374#ifdef FEAT_BEVAL
1375 static void
1376f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1377{
1378 gui_mch_post_balloon(balloonEval, get_tv_string_chk(&argvars[0]));
1379}
1380#endif
1381
1382/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001383 * "browse(save, title, initdir, default)" function
1384 */
1385 static void
1386f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1387{
1388#ifdef FEAT_BROWSE
1389 int save;
1390 char_u *title;
1391 char_u *initdir;
1392 char_u *defname;
1393 char_u buf[NUMBUFLEN];
1394 char_u buf2[NUMBUFLEN];
1395 int error = FALSE;
1396
1397 save = (int)get_tv_number_chk(&argvars[0], &error);
1398 title = get_tv_string_chk(&argvars[1]);
1399 initdir = get_tv_string_buf_chk(&argvars[2], buf);
1400 defname = get_tv_string_buf_chk(&argvars[3], buf2);
1401
1402 if (error || title == NULL || initdir == NULL || defname == NULL)
1403 rettv->vval.v_string = NULL;
1404 else
1405 rettv->vval.v_string =
1406 do_browse(save ? BROWSE_SAVE : 0,
1407 title, defname, NULL, initdir, NULL, curbuf);
1408#else
1409 rettv->vval.v_string = NULL;
1410#endif
1411 rettv->v_type = VAR_STRING;
1412}
1413
1414/*
1415 * "browsedir(title, initdir)" function
1416 */
1417 static void
1418f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1419{
1420#ifdef FEAT_BROWSE
1421 char_u *title;
1422 char_u *initdir;
1423 char_u buf[NUMBUFLEN];
1424
1425 title = get_tv_string_chk(&argvars[0]);
1426 initdir = get_tv_string_buf_chk(&argvars[1], buf);
1427
1428 if (title == NULL || initdir == NULL)
1429 rettv->vval.v_string = NULL;
1430 else
1431 rettv->vval.v_string = do_browse(BROWSE_DIR,
1432 title, NULL, NULL, initdir, NULL, curbuf);
1433#else
1434 rettv->vval.v_string = NULL;
1435#endif
1436 rettv->v_type = VAR_STRING;
1437}
1438
1439static buf_T *find_buffer(typval_T *avar);
1440
1441/*
1442 * Find a buffer by number or exact name.
1443 */
1444 static buf_T *
1445find_buffer(typval_T *avar)
1446{
1447 buf_T *buf = NULL;
1448
1449 if (avar->v_type == VAR_NUMBER)
1450 buf = buflist_findnr((int)avar->vval.v_number);
1451 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1452 {
1453 buf = buflist_findname_exp(avar->vval.v_string);
1454 if (buf == NULL)
1455 {
1456 /* No full path name match, try a match with a URL or a "nofile"
1457 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001458 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001459 if (buf->b_fname != NULL
1460 && (path_with_url(buf->b_fname)
1461#ifdef FEAT_QUICKFIX
1462 || bt_nofile(buf)
1463#endif
1464 )
1465 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1466 break;
1467 }
1468 }
1469 return buf;
1470}
1471
1472/*
1473 * "bufexists(expr)" function
1474 */
1475 static void
1476f_bufexists(typval_T *argvars, typval_T *rettv)
1477{
1478 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1479}
1480
1481/*
1482 * "buflisted(expr)" function
1483 */
1484 static void
1485f_buflisted(typval_T *argvars, typval_T *rettv)
1486{
1487 buf_T *buf;
1488
1489 buf = find_buffer(&argvars[0]);
1490 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1491}
1492
1493/*
1494 * "bufloaded(expr)" function
1495 */
1496 static void
1497f_bufloaded(typval_T *argvars, typval_T *rettv)
1498{
1499 buf_T *buf;
1500
1501 buf = find_buffer(&argvars[0]);
1502 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1503}
1504
1505 buf_T *
1506buflist_find_by_name(char_u *name, int curtab_only)
1507{
1508 int save_magic;
1509 char_u *save_cpo;
1510 buf_T *buf;
1511
1512 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1513 save_magic = p_magic;
1514 p_magic = TRUE;
1515 save_cpo = p_cpo;
1516 p_cpo = (char_u *)"";
1517
1518 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1519 TRUE, FALSE, curtab_only));
1520
1521 p_magic = save_magic;
1522 p_cpo = save_cpo;
1523 return buf;
1524}
1525
1526/*
1527 * Get buffer by number or pattern.
1528 */
1529 static buf_T *
1530get_buf_tv(typval_T *tv, int curtab_only)
1531{
1532 char_u *name = tv->vval.v_string;
1533 buf_T *buf;
1534
1535 if (tv->v_type == VAR_NUMBER)
1536 return buflist_findnr((int)tv->vval.v_number);
1537 if (tv->v_type != VAR_STRING)
1538 return NULL;
1539 if (name == NULL || *name == NUL)
1540 return curbuf;
1541 if (name[0] == '$' && name[1] == NUL)
1542 return lastbuf;
1543
1544 buf = buflist_find_by_name(name, curtab_only);
1545
1546 /* If not found, try expanding the name, like done for bufexists(). */
1547 if (buf == NULL)
1548 buf = find_buffer(tv);
1549
1550 return buf;
1551}
1552
1553/*
1554 * "bufname(expr)" function
1555 */
1556 static void
1557f_bufname(typval_T *argvars, typval_T *rettv)
1558{
1559 buf_T *buf;
1560
1561 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1562 ++emsg_off;
1563 buf = get_buf_tv(&argvars[0], FALSE);
1564 rettv->v_type = VAR_STRING;
1565 if (buf != NULL && buf->b_fname != NULL)
1566 rettv->vval.v_string = vim_strsave(buf->b_fname);
1567 else
1568 rettv->vval.v_string = NULL;
1569 --emsg_off;
1570}
1571
1572/*
1573 * "bufnr(expr)" function
1574 */
1575 static void
1576f_bufnr(typval_T *argvars, typval_T *rettv)
1577{
1578 buf_T *buf;
1579 int error = FALSE;
1580 char_u *name;
1581
1582 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1583 ++emsg_off;
1584 buf = get_buf_tv(&argvars[0], FALSE);
1585 --emsg_off;
1586
1587 /* If the buffer isn't found and the second argument is not zero create a
1588 * new buffer. */
1589 if (buf == NULL
1590 && argvars[1].v_type != VAR_UNKNOWN
1591 && get_tv_number_chk(&argvars[1], &error) != 0
1592 && !error
1593 && (name = get_tv_string_chk(&argvars[0])) != NULL
1594 && !error)
1595 buf = buflist_new(name, NULL, (linenr_T)1, 0);
1596
1597 if (buf != NULL)
1598 rettv->vval.v_number = buf->b_fnum;
1599 else
1600 rettv->vval.v_number = -1;
1601}
1602
1603 static void
1604buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
1605{
1606#ifdef FEAT_WINDOWS
1607 win_T *wp;
1608 int winnr = 0;
1609#endif
1610 buf_T *buf;
1611
1612 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1613 ++emsg_off;
1614 buf = get_buf_tv(&argvars[0], TRUE);
1615#ifdef FEAT_WINDOWS
Bram Moolenaar29323592016-07-24 22:04:11 +02001616 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001617 {
1618 ++winnr;
1619 if (wp->w_buffer == buf)
1620 break;
1621 }
1622 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
1623#else
1624 rettv->vval.v_number = (curwin->w_buffer == buf
1625 ? (get_nr ? 1 : curwin->w_id) : -1);
1626#endif
1627 --emsg_off;
1628}
1629
1630/*
1631 * "bufwinid(nr)" function
1632 */
1633 static void
1634f_bufwinid(typval_T *argvars, typval_T *rettv)
1635{
1636 buf_win_common(argvars, rettv, FALSE);
1637}
1638
1639/*
1640 * "bufwinnr(nr)" function
1641 */
1642 static void
1643f_bufwinnr(typval_T *argvars, typval_T *rettv)
1644{
1645 buf_win_common(argvars, rettv, TRUE);
1646}
1647
1648/*
1649 * "byte2line(byte)" function
1650 */
1651 static void
1652f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1653{
1654#ifndef FEAT_BYTEOFF
1655 rettv->vval.v_number = -1;
1656#else
1657 long boff = 0;
1658
1659 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
1660 if (boff < 0)
1661 rettv->vval.v_number = -1;
1662 else
1663 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
1664 (linenr_T)0, &boff);
1665#endif
1666}
1667
1668 static void
1669byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
1670{
1671#ifdef FEAT_MBYTE
1672 char_u *t;
1673#endif
1674 char_u *str;
1675 varnumber_T idx;
1676
1677 str = get_tv_string_chk(&argvars[0]);
1678 idx = get_tv_number_chk(&argvars[1], NULL);
1679 rettv->vval.v_number = -1;
1680 if (str == NULL || idx < 0)
1681 return;
1682
1683#ifdef FEAT_MBYTE
1684 t = str;
1685 for ( ; idx > 0; idx--)
1686 {
1687 if (*t == NUL) /* EOL reached */
1688 return;
1689 if (enc_utf8 && comp)
1690 t += utf_ptr2len(t);
1691 else
1692 t += (*mb_ptr2len)(t);
1693 }
1694 rettv->vval.v_number = (varnumber_T)(t - str);
1695#else
1696 if ((size_t)idx <= STRLEN(str))
1697 rettv->vval.v_number = idx;
1698#endif
1699}
1700
1701/*
1702 * "byteidx()" function
1703 */
1704 static void
1705f_byteidx(typval_T *argvars, typval_T *rettv)
1706{
1707 byteidx(argvars, rettv, FALSE);
1708}
1709
1710/*
1711 * "byteidxcomp()" function
1712 */
1713 static void
1714f_byteidxcomp(typval_T *argvars, typval_T *rettv)
1715{
1716 byteidx(argvars, rettv, TRUE);
1717}
1718
1719/*
1720 * "call(func, arglist [, dict])" function
1721 */
1722 static void
1723f_call(typval_T *argvars, typval_T *rettv)
1724{
1725 char_u *func;
1726 partial_T *partial = NULL;
1727 dict_T *selfdict = NULL;
1728
1729 if (argvars[1].v_type != VAR_LIST)
1730 {
1731 EMSG(_(e_listreq));
1732 return;
1733 }
1734 if (argvars[1].vval.v_list == NULL)
1735 return;
1736
1737 if (argvars[0].v_type == VAR_FUNC)
1738 func = argvars[0].vval.v_string;
1739 else if (argvars[0].v_type == VAR_PARTIAL)
1740 {
1741 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001742 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001743 }
1744 else
1745 func = get_tv_string(&argvars[0]);
1746 if (*func == NUL)
1747 return; /* type error or empty name */
1748
1749 if (argvars[2].v_type != VAR_UNKNOWN)
1750 {
1751 if (argvars[2].v_type != VAR_DICT)
1752 {
1753 EMSG(_(e_dictreq));
1754 return;
1755 }
1756 selfdict = argvars[2].vval.v_dict;
1757 }
1758
1759 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
1760}
1761
1762#ifdef FEAT_FLOAT
1763/*
1764 * "ceil({float})" function
1765 */
1766 static void
1767f_ceil(typval_T *argvars, typval_T *rettv)
1768{
1769 float_T f = 0.0;
1770
1771 rettv->v_type = VAR_FLOAT;
1772 if (get_float_arg(argvars, &f) == OK)
1773 rettv->vval.v_float = ceil(f);
1774 else
1775 rettv->vval.v_float = 0.0;
1776}
1777#endif
1778
1779#ifdef FEAT_JOB_CHANNEL
1780/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01001781 * "ch_canread()" function
1782 */
1783 static void
1784f_ch_canread(typval_T *argvars, typval_T *rettv)
1785{
Bram Moolenaar958dc692016-12-01 15:34:12 +01001786 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01001787
1788 rettv->vval.v_number = 0;
1789 if (channel != NULL)
1790 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
1791 || channel_has_readahead(channel, PART_OUT)
1792 || channel_has_readahead(channel, PART_ERR);
1793}
1794
1795/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001796 * "ch_close()" function
1797 */
1798 static void
1799f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
1800{
1801 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
1802
1803 if (channel != NULL)
1804 {
1805 channel_close(channel, FALSE);
1806 channel_clear(channel);
1807 }
1808}
1809
1810/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02001811 * "ch_close()" function
1812 */
1813 static void
1814f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
1815{
1816 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
1817
1818 if (channel != NULL)
1819 channel_close_in(channel);
1820}
1821
1822/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001823 * "ch_getbufnr()" function
1824 */
1825 static void
1826f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
1827{
1828 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1829
1830 rettv->vval.v_number = -1;
1831 if (channel != NULL)
1832 {
1833 char_u *what = get_tv_string(&argvars[1]);
1834 int part;
1835
1836 if (STRCMP(what, "err") == 0)
1837 part = PART_ERR;
1838 else if (STRCMP(what, "out") == 0)
1839 part = PART_OUT;
1840 else if (STRCMP(what, "in") == 0)
1841 part = PART_IN;
1842 else
1843 part = PART_SOCK;
1844 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
1845 rettv->vval.v_number =
1846 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
1847 }
1848}
1849
1850/*
1851 * "ch_getjob()" function
1852 */
1853 static void
1854f_ch_getjob(typval_T *argvars, typval_T *rettv)
1855{
1856 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1857
1858 if (channel != NULL)
1859 {
1860 rettv->v_type = VAR_JOB;
1861 rettv->vval.v_job = channel->ch_job;
1862 if (channel->ch_job != NULL)
1863 ++channel->ch_job->jv_refcount;
1864 }
1865}
1866
1867/*
1868 * "ch_info()" function
1869 */
1870 static void
1871f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
1872{
1873 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1874
1875 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
1876 channel_info(channel, rettv->vval.v_dict);
1877}
1878
1879/*
1880 * "ch_log()" function
1881 */
1882 static void
1883f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
1884{
1885 char_u *msg = get_tv_string(&argvars[0]);
1886 channel_T *channel = NULL;
1887
1888 if (argvars[1].v_type != VAR_UNKNOWN)
1889 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
1890
1891 ch_log(channel, (char *)msg);
1892}
1893
1894/*
1895 * "ch_logfile()" function
1896 */
1897 static void
1898f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
1899{
1900 char_u *fname;
1901 char_u *opt = (char_u *)"";
1902 char_u buf[NUMBUFLEN];
1903
1904 fname = get_tv_string(&argvars[0]);
1905 if (argvars[1].v_type == VAR_STRING)
1906 opt = get_tv_string_buf(&argvars[1], buf);
1907 ch_logfile(fname, opt);
1908}
1909
1910/*
1911 * "ch_open()" function
1912 */
1913 static void
1914f_ch_open(typval_T *argvars, typval_T *rettv)
1915{
1916 rettv->v_type = VAR_CHANNEL;
1917 if (check_restricted() || check_secure())
1918 return;
1919 rettv->vval.v_channel = channel_open_func(argvars);
1920}
1921
1922/*
1923 * "ch_read()" function
1924 */
1925 static void
1926f_ch_read(typval_T *argvars, typval_T *rettv)
1927{
1928 common_channel_read(argvars, rettv, FALSE);
1929}
1930
1931/*
1932 * "ch_readraw()" function
1933 */
1934 static void
1935f_ch_readraw(typval_T *argvars, typval_T *rettv)
1936{
1937 common_channel_read(argvars, rettv, TRUE);
1938}
1939
1940/*
1941 * "ch_evalexpr()" function
1942 */
1943 static void
1944f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
1945{
1946 ch_expr_common(argvars, rettv, TRUE);
1947}
1948
1949/*
1950 * "ch_sendexpr()" function
1951 */
1952 static void
1953f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
1954{
1955 ch_expr_common(argvars, rettv, FALSE);
1956}
1957
1958/*
1959 * "ch_evalraw()" function
1960 */
1961 static void
1962f_ch_evalraw(typval_T *argvars, typval_T *rettv)
1963{
1964 ch_raw_common(argvars, rettv, TRUE);
1965}
1966
1967/*
1968 * "ch_sendraw()" function
1969 */
1970 static void
1971f_ch_sendraw(typval_T *argvars, typval_T *rettv)
1972{
1973 ch_raw_common(argvars, rettv, FALSE);
1974}
1975
1976/*
1977 * "ch_setoptions()" function
1978 */
1979 static void
1980f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
1981{
1982 channel_T *channel;
1983 jobopt_T opt;
1984
1985 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
1986 if (channel == NULL)
1987 return;
1988 clear_job_options(&opt);
1989 if (get_job_options(&argvars[1], &opt,
1990 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == OK)
1991 channel_set_options(channel, &opt);
1992 free_job_options(&opt);
1993}
1994
1995/*
1996 * "ch_status()" function
1997 */
1998 static void
1999f_ch_status(typval_T *argvars, typval_T *rettv)
2000{
2001 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002002 jobopt_T opt;
2003 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002004
2005 /* return an empty string by default */
2006 rettv->v_type = VAR_STRING;
2007 rettv->vval.v_string = NULL;
2008
2009 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002010
2011 if (argvars[1].v_type != VAR_UNKNOWN)
2012 {
2013 clear_job_options(&opt);
2014 if (get_job_options(&argvars[1], &opt, JO_PART) == OK
2015 && (opt.jo_set & JO_PART))
2016 part = opt.jo_part;
2017 }
2018
2019 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002020}
2021#endif
2022
2023/*
2024 * "changenr()" function
2025 */
2026 static void
2027f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2028{
2029 rettv->vval.v_number = curbuf->b_u_seq_cur;
2030}
2031
2032/*
2033 * "char2nr(string)" function
2034 */
2035 static void
2036f_char2nr(typval_T *argvars, typval_T *rettv)
2037{
2038#ifdef FEAT_MBYTE
2039 if (has_mbyte)
2040 {
2041 int utf8 = 0;
2042
2043 if (argvars[1].v_type != VAR_UNKNOWN)
2044 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
2045
2046 if (utf8)
2047 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
2048 else
2049 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
2050 }
2051 else
2052#endif
2053 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
2054}
2055
2056/*
2057 * "cindent(lnum)" function
2058 */
2059 static void
2060f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2061{
2062#ifdef FEAT_CINDENT
2063 pos_T pos;
2064 linenr_T lnum;
2065
2066 pos = curwin->w_cursor;
2067 lnum = get_tv_lnum(argvars);
2068 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2069 {
2070 curwin->w_cursor.lnum = lnum;
2071 rettv->vval.v_number = get_c_indent();
2072 curwin->w_cursor = pos;
2073 }
2074 else
2075#endif
2076 rettv->vval.v_number = -1;
2077}
2078
2079/*
2080 * "clearmatches()" function
2081 */
2082 static void
2083f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2084{
2085#ifdef FEAT_SEARCH_EXTRA
2086 clear_matches(curwin);
2087#endif
2088}
2089
2090/*
2091 * "col(string)" function
2092 */
2093 static void
2094f_col(typval_T *argvars, typval_T *rettv)
2095{
2096 colnr_T col = 0;
2097 pos_T *fp;
2098 int fnum = curbuf->b_fnum;
2099
2100 fp = var2fpos(&argvars[0], FALSE, &fnum);
2101 if (fp != NULL && fnum == curbuf->b_fnum)
2102 {
2103 if (fp->col == MAXCOL)
2104 {
2105 /* '> can be MAXCOL, get the length of the line then */
2106 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2107 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2108 else
2109 col = MAXCOL;
2110 }
2111 else
2112 {
2113 col = fp->col + 1;
2114#ifdef FEAT_VIRTUALEDIT
2115 /* col(".") when the cursor is on the NUL at the end of the line
2116 * because of "coladd" can be seen as an extra column. */
2117 if (virtual_active() && fp == &curwin->w_cursor)
2118 {
2119 char_u *p = ml_get_cursor();
2120
2121 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2122 curwin->w_virtcol - curwin->w_cursor.coladd))
2123 {
2124# ifdef FEAT_MBYTE
2125 int l;
2126
2127 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2128 col += l;
2129# else
2130 if (*p != NUL && p[1] == NUL)
2131 ++col;
2132# endif
2133 }
2134 }
2135#endif
2136 }
2137 }
2138 rettv->vval.v_number = col;
2139}
2140
2141#if defined(FEAT_INS_EXPAND)
2142/*
2143 * "complete()" function
2144 */
2145 static void
2146f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2147{
2148 int startcol;
2149
2150 if ((State & INSERT) == 0)
2151 {
2152 EMSG(_("E785: complete() can only be used in Insert mode"));
2153 return;
2154 }
2155
2156 /* Check for undo allowed here, because if something was already inserted
2157 * the line was already saved for undo and this check isn't done. */
2158 if (!undo_allowed())
2159 return;
2160
2161 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2162 {
2163 EMSG(_(e_invarg));
2164 return;
2165 }
2166
2167 startcol = (int)get_tv_number_chk(&argvars[0], NULL);
2168 if (startcol <= 0)
2169 return;
2170
2171 set_completion(startcol - 1, argvars[1].vval.v_list);
2172}
2173
2174/*
2175 * "complete_add()" function
2176 */
2177 static void
2178f_complete_add(typval_T *argvars, typval_T *rettv)
2179{
2180 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2181}
2182
2183/*
2184 * "complete_check()" function
2185 */
2186 static void
2187f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2188{
2189 int saved = RedrawingDisabled;
2190
2191 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002192 ins_compl_check_keys(0, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002193 rettv->vval.v_number = compl_interrupted;
2194 RedrawingDisabled = saved;
2195}
2196#endif
2197
2198/*
2199 * "confirm(message, buttons[, default [, type]])" function
2200 */
2201 static void
2202f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2203{
2204#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2205 char_u *message;
2206 char_u *buttons = NULL;
2207 char_u buf[NUMBUFLEN];
2208 char_u buf2[NUMBUFLEN];
2209 int def = 1;
2210 int type = VIM_GENERIC;
2211 char_u *typestr;
2212 int error = FALSE;
2213
2214 message = get_tv_string_chk(&argvars[0]);
2215 if (message == NULL)
2216 error = TRUE;
2217 if (argvars[1].v_type != VAR_UNKNOWN)
2218 {
2219 buttons = get_tv_string_buf_chk(&argvars[1], buf);
2220 if (buttons == NULL)
2221 error = TRUE;
2222 if (argvars[2].v_type != VAR_UNKNOWN)
2223 {
2224 def = (int)get_tv_number_chk(&argvars[2], &error);
2225 if (argvars[3].v_type != VAR_UNKNOWN)
2226 {
2227 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
2228 if (typestr == NULL)
2229 error = TRUE;
2230 else
2231 {
2232 switch (TOUPPER_ASC(*typestr))
2233 {
2234 case 'E': type = VIM_ERROR; break;
2235 case 'Q': type = VIM_QUESTION; break;
2236 case 'I': type = VIM_INFO; break;
2237 case 'W': type = VIM_WARNING; break;
2238 case 'G': type = VIM_GENERIC; break;
2239 }
2240 }
2241 }
2242 }
2243 }
2244
2245 if (buttons == NULL || *buttons == NUL)
2246 buttons = (char_u *)_("&Ok");
2247
2248 if (!error)
2249 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2250 def, NULL, FALSE);
2251#endif
2252}
2253
2254/*
2255 * "copy()" function
2256 */
2257 static void
2258f_copy(typval_T *argvars, typval_T *rettv)
2259{
2260 item_copy(&argvars[0], rettv, FALSE, 0);
2261}
2262
2263#ifdef FEAT_FLOAT
2264/*
2265 * "cos()" function
2266 */
2267 static void
2268f_cos(typval_T *argvars, typval_T *rettv)
2269{
2270 float_T f = 0.0;
2271
2272 rettv->v_type = VAR_FLOAT;
2273 if (get_float_arg(argvars, &f) == OK)
2274 rettv->vval.v_float = cos(f);
2275 else
2276 rettv->vval.v_float = 0.0;
2277}
2278
2279/*
2280 * "cosh()" function
2281 */
2282 static void
2283f_cosh(typval_T *argvars, typval_T *rettv)
2284{
2285 float_T f = 0.0;
2286
2287 rettv->v_type = VAR_FLOAT;
2288 if (get_float_arg(argvars, &f) == OK)
2289 rettv->vval.v_float = cosh(f);
2290 else
2291 rettv->vval.v_float = 0.0;
2292}
2293#endif
2294
2295/*
2296 * "count()" function
2297 */
2298 static void
2299f_count(typval_T *argvars, typval_T *rettv)
2300{
2301 long n = 0;
2302 int ic = FALSE;
2303
2304 if (argvars[0].v_type == VAR_LIST)
2305 {
2306 listitem_T *li;
2307 list_T *l;
2308 long idx;
2309
2310 if ((l = argvars[0].vval.v_list) != NULL)
2311 {
2312 li = l->lv_first;
2313 if (argvars[2].v_type != VAR_UNKNOWN)
2314 {
2315 int error = FALSE;
2316
2317 ic = (int)get_tv_number_chk(&argvars[2], &error);
2318 if (argvars[3].v_type != VAR_UNKNOWN)
2319 {
2320 idx = (long)get_tv_number_chk(&argvars[3], &error);
2321 if (!error)
2322 {
2323 li = list_find(l, idx);
2324 if (li == NULL)
2325 EMSGN(_(e_listidx), idx);
2326 }
2327 }
2328 if (error)
2329 li = NULL;
2330 }
2331
2332 for ( ; li != NULL; li = li->li_next)
2333 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2334 ++n;
2335 }
2336 }
2337 else if (argvars[0].v_type == VAR_DICT)
2338 {
2339 int todo;
2340 dict_T *d;
2341 hashitem_T *hi;
2342
2343 if ((d = argvars[0].vval.v_dict) != NULL)
2344 {
2345 int error = FALSE;
2346
2347 if (argvars[2].v_type != VAR_UNKNOWN)
2348 {
2349 ic = (int)get_tv_number_chk(&argvars[2], &error);
2350 if (argvars[3].v_type != VAR_UNKNOWN)
2351 EMSG(_(e_invarg));
2352 }
2353
2354 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2355 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2356 {
2357 if (!HASHITEM_EMPTY(hi))
2358 {
2359 --todo;
2360 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2361 ++n;
2362 }
2363 }
2364 }
2365 }
2366 else
2367 EMSG2(_(e_listdictarg), "count()");
2368 rettv->vval.v_number = n;
2369}
2370
2371/*
2372 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2373 *
2374 * Checks the existence of a cscope connection.
2375 */
2376 static void
2377f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2378{
2379#ifdef FEAT_CSCOPE
2380 int num = 0;
2381 char_u *dbpath = NULL;
2382 char_u *prepend = NULL;
2383 char_u buf[NUMBUFLEN];
2384
2385 if (argvars[0].v_type != VAR_UNKNOWN
2386 && argvars[1].v_type != VAR_UNKNOWN)
2387 {
2388 num = (int)get_tv_number(&argvars[0]);
2389 dbpath = get_tv_string(&argvars[1]);
2390 if (argvars[2].v_type != VAR_UNKNOWN)
2391 prepend = get_tv_string_buf(&argvars[2], buf);
2392 }
2393
2394 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2395#endif
2396}
2397
2398/*
2399 * "cursor(lnum, col)" function, or
2400 * "cursor(list)"
2401 *
2402 * Moves the cursor to the specified line and column.
2403 * Returns 0 when the position could be set, -1 otherwise.
2404 */
2405 static void
2406f_cursor(typval_T *argvars, typval_T *rettv)
2407{
2408 long line, col;
2409#ifdef FEAT_VIRTUALEDIT
2410 long coladd = 0;
2411#endif
2412 int set_curswant = TRUE;
2413
2414 rettv->vval.v_number = -1;
2415 if (argvars[1].v_type == VAR_UNKNOWN)
2416 {
2417 pos_T pos;
2418 colnr_T curswant = -1;
2419
2420 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2421 {
2422 EMSG(_(e_invarg));
2423 return;
2424 }
2425 line = pos.lnum;
2426 col = pos.col;
2427#ifdef FEAT_VIRTUALEDIT
2428 coladd = pos.coladd;
2429#endif
2430 if (curswant >= 0)
2431 {
2432 curwin->w_curswant = curswant - 1;
2433 set_curswant = FALSE;
2434 }
2435 }
2436 else
2437 {
2438 line = get_tv_lnum(argvars);
2439 col = (long)get_tv_number_chk(&argvars[1], NULL);
2440#ifdef FEAT_VIRTUALEDIT
2441 if (argvars[2].v_type != VAR_UNKNOWN)
2442 coladd = (long)get_tv_number_chk(&argvars[2], NULL);
2443#endif
2444 }
2445 if (line < 0 || col < 0
2446#ifdef FEAT_VIRTUALEDIT
2447 || coladd < 0
2448#endif
2449 )
2450 return; /* type error; errmsg already given */
2451 if (line > 0)
2452 curwin->w_cursor.lnum = line;
2453 if (col > 0)
2454 curwin->w_cursor.col = col - 1;
2455#ifdef FEAT_VIRTUALEDIT
2456 curwin->w_cursor.coladd = coladd;
2457#endif
2458
2459 /* Make sure the cursor is in a valid position. */
2460 check_cursor();
2461#ifdef FEAT_MBYTE
2462 /* Correct cursor for multi-byte character. */
2463 if (has_mbyte)
2464 mb_adjust_cursor();
2465#endif
2466
2467 curwin->w_set_curswant = set_curswant;
2468 rettv->vval.v_number = 0;
2469}
2470
2471/*
2472 * "deepcopy()" function
2473 */
2474 static void
2475f_deepcopy(typval_T *argvars, typval_T *rettv)
2476{
2477 int noref = 0;
2478 int copyID;
2479
2480 if (argvars[1].v_type != VAR_UNKNOWN)
2481 noref = (int)get_tv_number_chk(&argvars[1], NULL);
2482 if (noref < 0 || noref > 1)
2483 EMSG(_(e_invarg));
2484 else
2485 {
2486 copyID = get_copyID();
2487 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2488 }
2489}
2490
2491/*
2492 * "delete()" function
2493 */
2494 static void
2495f_delete(typval_T *argvars, typval_T *rettv)
2496{
2497 char_u nbuf[NUMBUFLEN];
2498 char_u *name;
2499 char_u *flags;
2500
2501 rettv->vval.v_number = -1;
2502 if (check_restricted() || check_secure())
2503 return;
2504
2505 name = get_tv_string(&argvars[0]);
2506 if (name == NULL || *name == NUL)
2507 {
2508 EMSG(_(e_invarg));
2509 return;
2510 }
2511
2512 if (argvars[1].v_type != VAR_UNKNOWN)
2513 flags = get_tv_string_buf(&argvars[1], nbuf);
2514 else
2515 flags = (char_u *)"";
2516
2517 if (*flags == NUL)
2518 /* delete a file */
2519 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2520 else if (STRCMP(flags, "d") == 0)
2521 /* delete an empty directory */
2522 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2523 else if (STRCMP(flags, "rf") == 0)
2524 /* delete a directory recursively */
2525 rettv->vval.v_number = delete_recursive(name);
2526 else
2527 EMSG2(_(e_invexpr2), flags);
2528}
2529
2530/*
2531 * "did_filetype()" function
2532 */
2533 static void
2534f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2535{
2536#ifdef FEAT_AUTOCMD
2537 rettv->vval.v_number = did_filetype;
2538#endif
2539}
2540
2541/*
2542 * "diff_filler()" function
2543 */
2544 static void
2545f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2546{
2547#ifdef FEAT_DIFF
2548 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
2549#endif
2550}
2551
2552/*
2553 * "diff_hlID()" function
2554 */
2555 static void
2556f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2557{
2558#ifdef FEAT_DIFF
2559 linenr_T lnum = get_tv_lnum(argvars);
2560 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01002561 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002562 static int fnum = 0;
2563 static int change_start = 0;
2564 static int change_end = 0;
2565 static hlf_T hlID = (hlf_T)0;
2566 int filler_lines;
2567 int col;
2568
2569 if (lnum < 0) /* ignore type error in {lnum} arg */
2570 lnum = 0;
2571 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002572 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002573 || fnum != curbuf->b_fnum)
2574 {
2575 /* New line, buffer, change: need to get the values. */
2576 filler_lines = diff_check(curwin, lnum);
2577 if (filler_lines < 0)
2578 {
2579 if (filler_lines == -1)
2580 {
2581 change_start = MAXCOL;
2582 change_end = -1;
2583 if (diff_find_change(curwin, lnum, &change_start, &change_end))
2584 hlID = HLF_ADD; /* added line */
2585 else
2586 hlID = HLF_CHD; /* changed line */
2587 }
2588 else
2589 hlID = HLF_ADD; /* added line */
2590 }
2591 else
2592 hlID = (hlf_T)0;
2593 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002594 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002595 fnum = curbuf->b_fnum;
2596 }
2597
2598 if (hlID == HLF_CHD || hlID == HLF_TXD)
2599 {
2600 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
2601 if (col >= change_start && col <= change_end)
2602 hlID = HLF_TXD; /* changed text */
2603 else
2604 hlID = HLF_CHD; /* changed line */
2605 }
2606 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
2607#endif
2608}
2609
2610/*
2611 * "empty({expr})" function
2612 */
2613 static void
2614f_empty(typval_T *argvars, typval_T *rettv)
2615{
2616 int n = FALSE;
2617
2618 switch (argvars[0].v_type)
2619 {
2620 case VAR_STRING:
2621 case VAR_FUNC:
2622 n = argvars[0].vval.v_string == NULL
2623 || *argvars[0].vval.v_string == NUL;
2624 break;
2625 case VAR_PARTIAL:
2626 n = FALSE;
2627 break;
2628 case VAR_NUMBER:
2629 n = argvars[0].vval.v_number == 0;
2630 break;
2631 case VAR_FLOAT:
2632#ifdef FEAT_FLOAT
2633 n = argvars[0].vval.v_float == 0.0;
2634 break;
2635#endif
2636 case VAR_LIST:
2637 n = argvars[0].vval.v_list == NULL
2638 || argvars[0].vval.v_list->lv_first == NULL;
2639 break;
2640 case VAR_DICT:
2641 n = argvars[0].vval.v_dict == NULL
2642 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
2643 break;
2644 case VAR_SPECIAL:
2645 n = argvars[0].vval.v_number != VVAL_TRUE;
2646 break;
2647
2648 case VAR_JOB:
2649#ifdef FEAT_JOB_CHANNEL
2650 n = argvars[0].vval.v_job == NULL
2651 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
2652 break;
2653#endif
2654 case VAR_CHANNEL:
2655#ifdef FEAT_JOB_CHANNEL
2656 n = argvars[0].vval.v_channel == NULL
2657 || !channel_is_open(argvars[0].vval.v_channel);
2658 break;
2659#endif
2660 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01002661 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002662 n = TRUE;
2663 break;
2664 }
2665
2666 rettv->vval.v_number = n;
2667}
2668
2669/*
2670 * "escape({string}, {chars})" function
2671 */
2672 static void
2673f_escape(typval_T *argvars, typval_T *rettv)
2674{
2675 char_u buf[NUMBUFLEN];
2676
2677 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
2678 get_tv_string_buf(&argvars[1], buf));
2679 rettv->v_type = VAR_STRING;
2680}
2681
2682/*
2683 * "eval()" function
2684 */
2685 static void
2686f_eval(typval_T *argvars, typval_T *rettv)
2687{
2688 char_u *s, *p;
2689
2690 s = get_tv_string_chk(&argvars[0]);
2691 if (s != NULL)
2692 s = skipwhite(s);
2693
2694 p = s;
2695 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
2696 {
2697 if (p != NULL && !aborting())
2698 EMSG2(_(e_invexpr2), p);
2699 need_clr_eos = FALSE;
2700 rettv->v_type = VAR_NUMBER;
2701 rettv->vval.v_number = 0;
2702 }
2703 else if (*s != NUL)
2704 EMSG(_(e_trailing));
2705}
2706
2707/*
2708 * "eventhandler()" function
2709 */
2710 static void
2711f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
2712{
2713 rettv->vval.v_number = vgetc_busy;
2714}
2715
2716/*
2717 * "executable()" function
2718 */
2719 static void
2720f_executable(typval_T *argvars, typval_T *rettv)
2721{
2722 char_u *name = get_tv_string(&argvars[0]);
2723
2724 /* Check in $PATH and also check directly if there is a directory name. */
2725 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
2726 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
2727}
2728
2729static garray_T redir_execute_ga;
2730
2731/*
2732 * Append "value[value_len]" to the execute() output.
2733 */
2734 void
2735execute_redir_str(char_u *value, int value_len)
2736{
2737 int len;
2738
2739 if (value_len == -1)
2740 len = (int)STRLEN(value); /* Append the entire string */
2741 else
2742 len = value_len; /* Append only "value_len" characters */
2743 if (ga_grow(&redir_execute_ga, len) == OK)
2744 {
2745 mch_memmove((char *)redir_execute_ga.ga_data
2746 + redir_execute_ga.ga_len, value, len);
2747 redir_execute_ga.ga_len += len;
2748 }
2749}
2750
2751/*
2752 * Get next line from a list.
2753 * Called by do_cmdline() to get the next line.
2754 * Returns allocated string, or NULL for end of function.
2755 */
2756
2757 static char_u *
2758get_list_line(
2759 int c UNUSED,
2760 void *cookie,
2761 int indent UNUSED)
2762{
2763 listitem_T **p = (listitem_T **)cookie;
2764 listitem_T *item = *p;
2765 char_u buf[NUMBUFLEN];
2766 char_u *s;
2767
2768 if (item == NULL)
2769 return NULL;
2770 s = get_tv_string_buf_chk(&item->li_tv, buf);
2771 *p = item->li_next;
2772 return s == NULL ? NULL : vim_strsave(s);
2773}
2774
2775/*
2776 * "execute()" function
2777 */
2778 static void
2779f_execute(typval_T *argvars, typval_T *rettv)
2780{
2781 char_u *cmd = NULL;
2782 list_T *list = NULL;
2783 int save_msg_silent = msg_silent;
2784 int save_emsg_silent = emsg_silent;
2785 int save_emsg_noredir = emsg_noredir;
2786 int save_redir_execute = redir_execute;
2787 garray_T save_ga;
2788
2789 rettv->vval.v_string = NULL;
2790 rettv->v_type = VAR_STRING;
2791
2792 if (argvars[0].v_type == VAR_LIST)
2793 {
2794 list = argvars[0].vval.v_list;
2795 if (list == NULL || list->lv_first == NULL)
2796 /* empty list, no commands, empty output */
2797 return;
2798 ++list->lv_refcount;
2799 }
2800 else
2801 {
2802 cmd = get_tv_string_chk(&argvars[0]);
2803 if (cmd == NULL)
2804 return;
2805 }
2806
2807 if (argvars[1].v_type != VAR_UNKNOWN)
2808 {
2809 char_u buf[NUMBUFLEN];
2810 char_u *s = get_tv_string_buf_chk(&argvars[1], buf);
2811
2812 if (s == NULL)
2813 return;
2814 if (STRNCMP(s, "silent", 6) == 0)
2815 ++msg_silent;
2816 if (STRCMP(s, "silent!") == 0)
2817 {
2818 emsg_silent = TRUE;
2819 emsg_noredir = TRUE;
2820 }
2821 }
2822 else
2823 ++msg_silent;
2824
2825 if (redir_execute)
2826 save_ga = redir_execute_ga;
2827 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
2828 redir_execute = TRUE;
2829
2830 if (cmd != NULL)
2831 do_cmdline_cmd(cmd);
2832 else
2833 {
2834 listitem_T *item = list->lv_first;
2835
2836 do_cmdline(NULL, get_list_line, (void *)&item,
2837 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
2838 --list->lv_refcount;
2839 }
2840
Bram Moolenaard297f352017-01-29 20:31:21 +01002841 /* Need to append a NUL to the result. */
2842 if (ga_grow(&redir_execute_ga, 1) == OK)
2843 {
2844 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
2845 rettv->vval.v_string = redir_execute_ga.ga_data;
2846 }
2847 else
2848 {
2849 ga_clear(&redir_execute_ga);
2850 rettv->vval.v_string = NULL;
2851 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002852 msg_silent = save_msg_silent;
2853 emsg_silent = save_emsg_silent;
2854 emsg_noredir = save_emsg_noredir;
2855
2856 redir_execute = save_redir_execute;
2857 if (redir_execute)
2858 redir_execute_ga = save_ga;
2859
2860 /* "silent reg" or "silent echo x" leaves msg_col somewhere in the
2861 * line. Put it back in the first column. */
2862 msg_col = 0;
2863}
2864
2865/*
2866 * "exepath()" function
2867 */
2868 static void
2869f_exepath(typval_T *argvars, typval_T *rettv)
2870{
2871 char_u *p = NULL;
2872
2873 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
2874 rettv->v_type = VAR_STRING;
2875 rettv->vval.v_string = p;
2876}
2877
2878/*
2879 * "exists()" function
2880 */
2881 static void
2882f_exists(typval_T *argvars, typval_T *rettv)
2883{
2884 char_u *p;
2885 char_u *name;
2886 int n = FALSE;
2887 int len = 0;
2888
2889 p = get_tv_string(&argvars[0]);
2890 if (*p == '$') /* environment variable */
2891 {
2892 /* first try "normal" environment variables (fast) */
2893 if (mch_getenv(p + 1) != NULL)
2894 n = TRUE;
2895 else
2896 {
2897 /* try expanding things like $VIM and ${HOME} */
2898 p = expand_env_save(p);
2899 if (p != NULL && *p != '$')
2900 n = TRUE;
2901 vim_free(p);
2902 }
2903 }
2904 else if (*p == '&' || *p == '+') /* option */
2905 {
2906 n = (get_option_tv(&p, NULL, TRUE) == OK);
2907 if (*skipwhite(p) != NUL)
2908 n = FALSE; /* trailing garbage */
2909 }
2910 else if (*p == '*') /* internal or user defined function */
2911 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002912 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002913 }
2914 else if (*p == ':')
2915 {
2916 n = cmd_exists(p + 1);
2917 }
2918 else if (*p == '#')
2919 {
2920#ifdef FEAT_AUTOCMD
2921 if (p[1] == '#')
2922 n = autocmd_supported(p + 2);
2923 else
2924 n = au_exists(p + 1);
2925#endif
2926 }
2927 else /* internal variable */
2928 {
2929 char_u *tofree;
2930 typval_T tv;
2931
2932 /* get_name_len() takes care of expanding curly braces */
2933 name = p;
2934 len = get_name_len(&p, &tofree, TRUE, FALSE);
2935 if (len > 0)
2936 {
2937 if (tofree != NULL)
2938 name = tofree;
2939 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
2940 if (n)
2941 {
2942 /* handle d.key, l[idx], f(expr) */
2943 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
2944 if (n)
2945 clear_tv(&tv);
2946 }
2947 }
2948 if (*p != NUL)
2949 n = FALSE;
2950
2951 vim_free(tofree);
2952 }
2953
2954 rettv->vval.v_number = n;
2955}
2956
2957#ifdef FEAT_FLOAT
2958/*
2959 * "exp()" function
2960 */
2961 static void
2962f_exp(typval_T *argvars, typval_T *rettv)
2963{
2964 float_T f = 0.0;
2965
2966 rettv->v_type = VAR_FLOAT;
2967 if (get_float_arg(argvars, &f) == OK)
2968 rettv->vval.v_float = exp(f);
2969 else
2970 rettv->vval.v_float = 0.0;
2971}
2972#endif
2973
2974/*
2975 * "expand()" function
2976 */
2977 static void
2978f_expand(typval_T *argvars, typval_T *rettv)
2979{
2980 char_u *s;
2981 int len;
2982 char_u *errormsg;
2983 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
2984 expand_T xpc;
2985 int error = FALSE;
2986 char_u *result;
2987
2988 rettv->v_type = VAR_STRING;
2989 if (argvars[1].v_type != VAR_UNKNOWN
2990 && argvars[2].v_type != VAR_UNKNOWN
2991 && get_tv_number_chk(&argvars[2], &error)
2992 && !error)
2993 {
2994 rettv->v_type = VAR_LIST;
2995 rettv->vval.v_list = NULL;
2996 }
2997
2998 s = get_tv_string(&argvars[0]);
2999 if (*s == '%' || *s == '#' || *s == '<')
3000 {
3001 ++emsg_off;
3002 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3003 --emsg_off;
3004 if (rettv->v_type == VAR_LIST)
3005 {
3006 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3007 list_append_string(rettv->vval.v_list, result, -1);
3008 else
3009 vim_free(result);
3010 }
3011 else
3012 rettv->vval.v_string = result;
3013 }
3014 else
3015 {
3016 /* When the optional second argument is non-zero, don't remove matches
3017 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3018 if (argvars[1].v_type != VAR_UNKNOWN
3019 && get_tv_number_chk(&argvars[1], &error))
3020 options |= WILD_KEEP_ALL;
3021 if (!error)
3022 {
3023 ExpandInit(&xpc);
3024 xpc.xp_context = EXPAND_FILES;
3025 if (p_wic)
3026 options += WILD_ICASE;
3027 if (rettv->v_type == VAR_STRING)
3028 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3029 options, WILD_ALL);
3030 else if (rettv_list_alloc(rettv) != FAIL)
3031 {
3032 int i;
3033
3034 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3035 for (i = 0; i < xpc.xp_numfiles; i++)
3036 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3037 ExpandCleanup(&xpc);
3038 }
3039 }
3040 else
3041 rettv->vval.v_string = NULL;
3042 }
3043}
3044
3045/*
3046 * "extend(list, list [, idx])" function
3047 * "extend(dict, dict [, action])" function
3048 */
3049 static void
3050f_extend(typval_T *argvars, typval_T *rettv)
3051{
3052 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3053
3054 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3055 {
3056 list_T *l1, *l2;
3057 listitem_T *item;
3058 long before;
3059 int error = FALSE;
3060
3061 l1 = argvars[0].vval.v_list;
3062 l2 = argvars[1].vval.v_list;
3063 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
3064 && l2 != NULL)
3065 {
3066 if (argvars[2].v_type != VAR_UNKNOWN)
3067 {
3068 before = (long)get_tv_number_chk(&argvars[2], &error);
3069 if (error)
3070 return; /* type error; errmsg already given */
3071
3072 if (before == l1->lv_len)
3073 item = NULL;
3074 else
3075 {
3076 item = list_find(l1, before);
3077 if (item == NULL)
3078 {
3079 EMSGN(_(e_listidx), before);
3080 return;
3081 }
3082 }
3083 }
3084 else
3085 item = NULL;
3086 list_extend(l1, l2, item);
3087
3088 copy_tv(&argvars[0], rettv);
3089 }
3090 }
3091 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3092 {
3093 dict_T *d1, *d2;
3094 char_u *action;
3095 int i;
3096
3097 d1 = argvars[0].vval.v_dict;
3098 d2 = argvars[1].vval.v_dict;
3099 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
3100 && d2 != NULL)
3101 {
3102 /* Check the third argument. */
3103 if (argvars[2].v_type != VAR_UNKNOWN)
3104 {
3105 static char *(av[]) = {"keep", "force", "error"};
3106
3107 action = get_tv_string_chk(&argvars[2]);
3108 if (action == NULL)
3109 return; /* type error; errmsg already given */
3110 for (i = 0; i < 3; ++i)
3111 if (STRCMP(action, av[i]) == 0)
3112 break;
3113 if (i == 3)
3114 {
3115 EMSG2(_(e_invarg2), action);
3116 return;
3117 }
3118 }
3119 else
3120 action = (char_u *)"force";
3121
3122 dict_extend(d1, d2, action);
3123
3124 copy_tv(&argvars[0], rettv);
3125 }
3126 }
3127 else
3128 EMSG2(_(e_listdictarg), "extend()");
3129}
3130
3131/*
3132 * "feedkeys()" function
3133 */
3134 static void
3135f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3136{
3137 int remap = TRUE;
3138 int insert = FALSE;
3139 char_u *keys, *flags;
3140 char_u nbuf[NUMBUFLEN];
3141 int typed = FALSE;
3142 int execute = FALSE;
3143 int dangerous = FALSE;
3144 char_u *keys_esc;
3145
3146 /* This is not allowed in the sandbox. If the commands would still be
3147 * executed in the sandbox it would be OK, but it probably happens later,
3148 * when "sandbox" is no longer set. */
3149 if (check_secure())
3150 return;
3151
3152 keys = get_tv_string(&argvars[0]);
3153
3154 if (argvars[1].v_type != VAR_UNKNOWN)
3155 {
3156 flags = get_tv_string_buf(&argvars[1], nbuf);
3157 for ( ; *flags != NUL; ++flags)
3158 {
3159 switch (*flags)
3160 {
3161 case 'n': remap = FALSE; break;
3162 case 'm': remap = TRUE; break;
3163 case 't': typed = TRUE; break;
3164 case 'i': insert = TRUE; break;
3165 case 'x': execute = TRUE; break;
3166 case '!': dangerous = TRUE; break;
3167 }
3168 }
3169 }
3170
3171 if (*keys != NUL || execute)
3172 {
3173 /* Need to escape K_SPECIAL and CSI before putting the string in the
3174 * typeahead buffer. */
3175 keys_esc = vim_strsave_escape_csi(keys);
3176 if (keys_esc != NULL)
3177 {
3178 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
3179 insert ? 0 : typebuf.tb_len, !typed, FALSE);
3180 vim_free(keys_esc);
3181 if (vgetc_busy)
3182 typebuf_was_filled = TRUE;
3183 if (execute)
3184 {
3185 int save_msg_scroll = msg_scroll;
3186
3187 /* Avoid a 1 second delay when the keys start Insert mode. */
3188 msg_scroll = FALSE;
3189
3190 if (!dangerous)
3191 ++ex_normal_busy;
3192 exec_normal(TRUE);
3193 if (!dangerous)
3194 --ex_normal_busy;
3195 msg_scroll |= save_msg_scroll;
3196 }
3197 }
3198 }
3199}
3200
3201/*
3202 * "filereadable()" function
3203 */
3204 static void
3205f_filereadable(typval_T *argvars, typval_T *rettv)
3206{
3207 int fd;
3208 char_u *p;
3209 int n;
3210
3211#ifndef O_NONBLOCK
3212# define O_NONBLOCK 0
3213#endif
3214 p = get_tv_string(&argvars[0]);
3215 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3216 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3217 {
3218 n = TRUE;
3219 close(fd);
3220 }
3221 else
3222 n = FALSE;
3223
3224 rettv->vval.v_number = n;
3225}
3226
3227/*
3228 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3229 * rights to write into.
3230 */
3231 static void
3232f_filewritable(typval_T *argvars, typval_T *rettv)
3233{
3234 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
3235}
3236
3237 static void
3238findfilendir(
3239 typval_T *argvars UNUSED,
3240 typval_T *rettv,
3241 int find_what UNUSED)
3242{
3243#ifdef FEAT_SEARCHPATH
3244 char_u *fname;
3245 char_u *fresult = NULL;
3246 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3247 char_u *p;
3248 char_u pathbuf[NUMBUFLEN];
3249 int count = 1;
3250 int first = TRUE;
3251 int error = FALSE;
3252#endif
3253
3254 rettv->vval.v_string = NULL;
3255 rettv->v_type = VAR_STRING;
3256
3257#ifdef FEAT_SEARCHPATH
3258 fname = get_tv_string(&argvars[0]);
3259
3260 if (argvars[1].v_type != VAR_UNKNOWN)
3261 {
3262 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
3263 if (p == NULL)
3264 error = TRUE;
3265 else
3266 {
3267 if (*p != NUL)
3268 path = p;
3269
3270 if (argvars[2].v_type != VAR_UNKNOWN)
3271 count = (int)get_tv_number_chk(&argvars[2], &error);
3272 }
3273 }
3274
3275 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3276 error = TRUE;
3277
3278 if (*fname != NUL && !error)
3279 {
3280 do
3281 {
3282 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3283 vim_free(fresult);
3284 fresult = find_file_in_path_option(first ? fname : NULL,
3285 first ? (int)STRLEN(fname) : 0,
3286 0, first, path,
3287 find_what,
3288 curbuf->b_ffname,
3289 find_what == FINDFILE_DIR
3290 ? (char_u *)"" : curbuf->b_p_sua);
3291 first = FALSE;
3292
3293 if (fresult != NULL && rettv->v_type == VAR_LIST)
3294 list_append_string(rettv->vval.v_list, fresult, -1);
3295
3296 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3297 }
3298
3299 if (rettv->v_type == VAR_STRING)
3300 rettv->vval.v_string = fresult;
3301#endif
3302}
3303
3304/*
3305 * "filter()" function
3306 */
3307 static void
3308f_filter(typval_T *argvars, typval_T *rettv)
3309{
3310 filter_map(argvars, rettv, FALSE);
3311}
3312
3313/*
3314 * "finddir({fname}[, {path}[, {count}]])" function
3315 */
3316 static void
3317f_finddir(typval_T *argvars, typval_T *rettv)
3318{
3319 findfilendir(argvars, rettv, FINDFILE_DIR);
3320}
3321
3322/*
3323 * "findfile({fname}[, {path}[, {count}]])" function
3324 */
3325 static void
3326f_findfile(typval_T *argvars, typval_T *rettv)
3327{
3328 findfilendir(argvars, rettv, FINDFILE_FILE);
3329}
3330
3331#ifdef FEAT_FLOAT
3332/*
3333 * "float2nr({float})" function
3334 */
3335 static void
3336f_float2nr(typval_T *argvars, typval_T *rettv)
3337{
3338 float_T f = 0.0;
3339
3340 if (get_float_arg(argvars, &f) == OK)
3341 {
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003342 if (f < -VARNUM_MAX)
3343 rettv->vval.v_number = -VARNUM_MAX;
3344 else if (f > VARNUM_MAX)
3345 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003346 else
3347 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003348 }
3349}
3350
3351/*
3352 * "floor({float})" function
3353 */
3354 static void
3355f_floor(typval_T *argvars, typval_T *rettv)
3356{
3357 float_T f = 0.0;
3358
3359 rettv->v_type = VAR_FLOAT;
3360 if (get_float_arg(argvars, &f) == OK)
3361 rettv->vval.v_float = floor(f);
3362 else
3363 rettv->vval.v_float = 0.0;
3364}
3365
3366/*
3367 * "fmod()" function
3368 */
3369 static void
3370f_fmod(typval_T *argvars, typval_T *rettv)
3371{
3372 float_T fx = 0.0, fy = 0.0;
3373
3374 rettv->v_type = VAR_FLOAT;
3375 if (get_float_arg(argvars, &fx) == OK
3376 && get_float_arg(&argvars[1], &fy) == OK)
3377 rettv->vval.v_float = fmod(fx, fy);
3378 else
3379 rettv->vval.v_float = 0.0;
3380}
3381#endif
3382
3383/*
3384 * "fnameescape({string})" function
3385 */
3386 static void
3387f_fnameescape(typval_T *argvars, typval_T *rettv)
3388{
3389 rettv->vval.v_string = vim_strsave_fnameescape(
3390 get_tv_string(&argvars[0]), FALSE);
3391 rettv->v_type = VAR_STRING;
3392}
3393
3394/*
3395 * "fnamemodify({fname}, {mods})" function
3396 */
3397 static void
3398f_fnamemodify(typval_T *argvars, typval_T *rettv)
3399{
3400 char_u *fname;
3401 char_u *mods;
3402 int usedlen = 0;
3403 int len;
3404 char_u *fbuf = NULL;
3405 char_u buf[NUMBUFLEN];
3406
3407 fname = get_tv_string_chk(&argvars[0]);
3408 mods = get_tv_string_buf_chk(&argvars[1], buf);
3409 if (fname == NULL || mods == NULL)
3410 fname = NULL;
3411 else
3412 {
3413 len = (int)STRLEN(fname);
3414 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
3415 }
3416
3417 rettv->v_type = VAR_STRING;
3418 if (fname == NULL)
3419 rettv->vval.v_string = NULL;
3420 else
3421 rettv->vval.v_string = vim_strnsave(fname, len);
3422 vim_free(fbuf);
3423}
3424
3425static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
3426
3427/*
3428 * "foldclosed()" function
3429 */
3430 static void
3431foldclosed_both(
3432 typval_T *argvars UNUSED,
3433 typval_T *rettv,
3434 int end UNUSED)
3435{
3436#ifdef FEAT_FOLDING
3437 linenr_T lnum;
3438 linenr_T first, last;
3439
3440 lnum = get_tv_lnum(argvars);
3441 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3442 {
3443 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3444 {
3445 if (end)
3446 rettv->vval.v_number = (varnumber_T)last;
3447 else
3448 rettv->vval.v_number = (varnumber_T)first;
3449 return;
3450 }
3451 }
3452#endif
3453 rettv->vval.v_number = -1;
3454}
3455
3456/*
3457 * "foldclosed()" function
3458 */
3459 static void
3460f_foldclosed(typval_T *argvars, typval_T *rettv)
3461{
3462 foldclosed_both(argvars, rettv, FALSE);
3463}
3464
3465/*
3466 * "foldclosedend()" function
3467 */
3468 static void
3469f_foldclosedend(typval_T *argvars, typval_T *rettv)
3470{
3471 foldclosed_both(argvars, rettv, TRUE);
3472}
3473
3474/*
3475 * "foldlevel()" function
3476 */
3477 static void
3478f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3479{
3480#ifdef FEAT_FOLDING
3481 linenr_T lnum;
3482
3483 lnum = get_tv_lnum(argvars);
3484 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3485 rettv->vval.v_number = foldLevel(lnum);
3486#endif
3487}
3488
3489/*
3490 * "foldtext()" function
3491 */
3492 static void
3493f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
3494{
3495#ifdef FEAT_FOLDING
3496 linenr_T foldstart;
3497 linenr_T foldend;
3498 char_u *dashes;
3499 linenr_T lnum;
3500 char_u *s;
3501 char_u *r;
3502 int len;
3503 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003504 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003505#endif
3506
3507 rettv->v_type = VAR_STRING;
3508 rettv->vval.v_string = NULL;
3509#ifdef FEAT_FOLDING
3510 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
3511 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
3512 dashes = get_vim_var_str(VV_FOLDDASHES);
3513 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
3514 && dashes != NULL)
3515 {
3516 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02003517 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003518 if (!linewhite(lnum))
3519 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003520
3521 /* Find interesting text in this line. */
3522 s = skipwhite(ml_get(lnum));
3523 /* skip C comment-start */
3524 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
3525 {
3526 s = skipwhite(s + 2);
3527 if (*skipwhite(s) == NUL
3528 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
3529 {
3530 s = skipwhite(ml_get(lnum + 1));
3531 if (*s == '*')
3532 s = skipwhite(s + 1);
3533 }
3534 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02003535 count = (long)(foldend - foldstart + 1);
3536 txt = ngettext("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003537 r = alloc((unsigned)(STRLEN(txt)
3538 + STRLEN(dashes) /* for %s */
3539 + 20 /* for %3ld */
3540 + STRLEN(s))); /* concatenated */
3541 if (r != NULL)
3542 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02003543 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003544 len = (int)STRLEN(r);
3545 STRCAT(r, s);
3546 /* remove 'foldmarker' and 'commentstring' */
3547 foldtext_cleanup(r + len);
3548 rettv->vval.v_string = r;
3549 }
3550 }
3551#endif
3552}
3553
3554/*
3555 * "foldtextresult(lnum)" function
3556 */
3557 static void
3558f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
3559{
3560#ifdef FEAT_FOLDING
3561 linenr_T lnum;
3562 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003563 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003564 foldinfo_T foldinfo;
3565 int fold_count;
3566#endif
3567
3568 rettv->v_type = VAR_STRING;
3569 rettv->vval.v_string = NULL;
3570#ifdef FEAT_FOLDING
3571 lnum = get_tv_lnum(argvars);
3572 /* treat illegal types and illegal string values for {lnum} the same */
3573 if (lnum < 0)
3574 lnum = 0;
3575 fold_count = foldedCount(curwin, lnum, &foldinfo);
3576 if (fold_count > 0)
3577 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02003578 text = get_foldtext(curwin, lnum, lnum + fold_count - 1, &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003579 if (text == buf)
3580 text = vim_strsave(text);
3581 rettv->vval.v_string = text;
3582 }
3583#endif
3584}
3585
3586/*
3587 * "foreground()" function
3588 */
3589 static void
3590f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3591{
3592#ifdef FEAT_GUI
3593 if (gui.in_use)
3594 gui_mch_set_foreground();
3595#else
3596# ifdef WIN32
3597 win32_set_foreground();
3598# endif
3599#endif
3600}
3601
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003602 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003603common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003604{
3605 char_u *s;
3606 char_u *name;
3607 int use_string = FALSE;
3608 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003609 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003610
3611 if (argvars[0].v_type == VAR_FUNC)
3612 {
3613 /* function(MyFunc, [arg], dict) */
3614 s = argvars[0].vval.v_string;
3615 }
3616 else if (argvars[0].v_type == VAR_PARTIAL
3617 && argvars[0].vval.v_partial != NULL)
3618 {
3619 /* function(dict.MyFunc, [arg]) */
3620 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003621 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003622 }
3623 else
3624 {
3625 /* function('MyFunc', [arg], dict) */
3626 s = get_tv_string(&argvars[0]);
3627 use_string = TRUE;
3628 }
3629
Bram Moolenaar843b8842016-08-21 14:36:15 +02003630 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003631 {
3632 name = s;
3633 trans_name = trans_function_name(&name, FALSE,
3634 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
3635 if (*name != NUL)
3636 s = NULL;
3637 }
3638
Bram Moolenaar843b8842016-08-21 14:36:15 +02003639 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
3640 || (is_funcref && trans_name == NULL))
Bram Moolenaar5582ef12016-09-14 22:16:13 +02003641 EMSG2(_(e_invarg2), use_string ? get_tv_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003642 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003643 else if (trans_name != NULL && (is_funcref
3644 ? find_func(trans_name) == NULL
3645 : !translated_function_exists(trans_name)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003646 EMSG2(_("E700: Unknown function: %s"), s);
3647 else
3648 {
3649 int dict_idx = 0;
3650 int arg_idx = 0;
3651 list_T *list = NULL;
3652
3653 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
3654 {
3655 char sid_buf[25];
3656 int off = *s == 's' ? 2 : 5;
3657
3658 /* Expand s: and <SID> into <SNR>nr_, so that the function can
3659 * also be called from another script. Using trans_function_name()
3660 * would also work, but some plugins depend on the name being
3661 * printable text. */
3662 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
3663 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
3664 if (name != NULL)
3665 {
3666 STRCPY(name, sid_buf);
3667 STRCAT(name, s + off);
3668 }
3669 }
3670 else
3671 name = vim_strsave(s);
3672
3673 if (argvars[1].v_type != VAR_UNKNOWN)
3674 {
3675 if (argvars[2].v_type != VAR_UNKNOWN)
3676 {
3677 /* function(name, [args], dict) */
3678 arg_idx = 1;
3679 dict_idx = 2;
3680 }
3681 else if (argvars[1].v_type == VAR_DICT)
3682 /* function(name, dict) */
3683 dict_idx = 1;
3684 else
3685 /* function(name, [args]) */
3686 arg_idx = 1;
3687 if (dict_idx > 0)
3688 {
3689 if (argvars[dict_idx].v_type != VAR_DICT)
3690 {
3691 EMSG(_("E922: expected a dict"));
3692 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003693 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003694 }
3695 if (argvars[dict_idx].vval.v_dict == NULL)
3696 dict_idx = 0;
3697 }
3698 if (arg_idx > 0)
3699 {
3700 if (argvars[arg_idx].v_type != VAR_LIST)
3701 {
3702 EMSG(_("E923: Second argument of function() must be a list or a dict"));
3703 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003704 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003705 }
3706 list = argvars[arg_idx].vval.v_list;
3707 if (list == NULL || list->lv_len == 0)
3708 arg_idx = 0;
3709 }
3710 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003711 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003712 {
3713 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
3714
3715 /* result is a VAR_PARTIAL */
3716 if (pt == NULL)
3717 vim_free(name);
3718 else
3719 {
3720 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
3721 {
3722 listitem_T *li;
3723 int i = 0;
3724 int arg_len = 0;
3725 int lv_len = 0;
3726
3727 if (arg_pt != NULL)
3728 arg_len = arg_pt->pt_argc;
3729 if (list != NULL)
3730 lv_len = list->lv_len;
3731 pt->pt_argc = arg_len + lv_len;
3732 pt->pt_argv = (typval_T *)alloc(
3733 sizeof(typval_T) * pt->pt_argc);
3734 if (pt->pt_argv == NULL)
3735 {
3736 vim_free(pt);
3737 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003738 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003739 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003740 for (i = 0; i < arg_len; i++)
3741 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
3742 if (lv_len > 0)
3743 for (li = list->lv_first; li != NULL;
3744 li = li->li_next)
3745 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003746 }
3747
3748 /* For "function(dict.func, [], dict)" and "func" is a partial
3749 * use "dict". That is backwards compatible. */
3750 if (dict_idx > 0)
3751 {
3752 /* The dict is bound explicitly, pt_auto is FALSE. */
3753 pt->pt_dict = argvars[dict_idx].vval.v_dict;
3754 ++pt->pt_dict->dv_refcount;
3755 }
3756 else if (arg_pt != NULL)
3757 {
3758 /* If the dict was bound automatically the result is also
3759 * bound automatically. */
3760 pt->pt_dict = arg_pt->pt_dict;
3761 pt->pt_auto = arg_pt->pt_auto;
3762 if (pt->pt_dict != NULL)
3763 ++pt->pt_dict->dv_refcount;
3764 }
3765
3766 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003767 if (arg_pt != NULL && arg_pt->pt_func != NULL)
3768 {
3769 pt->pt_func = arg_pt->pt_func;
3770 func_ptr_ref(pt->pt_func);
3771 vim_free(name);
3772 }
3773 else if (is_funcref)
3774 {
3775 pt->pt_func = find_func(trans_name);
3776 func_ptr_ref(pt->pt_func);
3777 vim_free(name);
3778 }
3779 else
3780 {
3781 pt->pt_name = name;
3782 func_ref(name);
3783 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003784 }
3785 rettv->v_type = VAR_PARTIAL;
3786 rettv->vval.v_partial = pt;
3787 }
3788 else
3789 {
3790 /* result is a VAR_FUNC */
3791 rettv->v_type = VAR_FUNC;
3792 rettv->vval.v_string = name;
3793 func_ref(name);
3794 }
3795 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003796theend:
3797 vim_free(trans_name);
3798}
3799
3800/*
3801 * "funcref()" function
3802 */
3803 static void
3804f_funcref(typval_T *argvars, typval_T *rettv)
3805{
3806 common_function(argvars, rettv, TRUE);
3807}
3808
3809/*
3810 * "function()" function
3811 */
3812 static void
3813f_function(typval_T *argvars, typval_T *rettv)
3814{
3815 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003816}
3817
3818/*
3819 * "garbagecollect()" function
3820 */
3821 static void
3822f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
3823{
3824 /* This is postponed until we are back at the toplevel, because we may be
3825 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
3826 want_garbage_collect = TRUE;
3827
3828 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
3829 garbage_collect_at_exit = TRUE;
3830}
3831
3832/*
3833 * "get()" function
3834 */
3835 static void
3836f_get(typval_T *argvars, typval_T *rettv)
3837{
3838 listitem_T *li;
3839 list_T *l;
3840 dictitem_T *di;
3841 dict_T *d;
3842 typval_T *tv = NULL;
3843
3844 if (argvars[0].v_type == VAR_LIST)
3845 {
3846 if ((l = argvars[0].vval.v_list) != NULL)
3847 {
3848 int error = FALSE;
3849
3850 li = list_find(l, (long)get_tv_number_chk(&argvars[1], &error));
3851 if (!error && li != NULL)
3852 tv = &li->li_tv;
3853 }
3854 }
3855 else if (argvars[0].v_type == VAR_DICT)
3856 {
3857 if ((d = argvars[0].vval.v_dict) != NULL)
3858 {
3859 di = dict_find(d, get_tv_string(&argvars[1]), -1);
3860 if (di != NULL)
3861 tv = &di->di_tv;
3862 }
3863 }
3864 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
3865 {
3866 partial_T *pt;
3867 partial_T fref_pt;
3868
3869 if (argvars[0].v_type == VAR_PARTIAL)
3870 pt = argvars[0].vval.v_partial;
3871 else
3872 {
3873 vim_memset(&fref_pt, 0, sizeof(fref_pt));
3874 fref_pt.pt_name = argvars[0].vval.v_string;
3875 pt = &fref_pt;
3876 }
3877
3878 if (pt != NULL)
3879 {
3880 char_u *what = get_tv_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003881 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003882
3883 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
3884 {
3885 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003886 n = partial_name(pt);
3887 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003888 rettv->vval.v_string = NULL;
3889 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003890 {
3891 rettv->vval.v_string = vim_strsave(n);
3892 if (rettv->v_type == VAR_FUNC)
3893 func_ref(rettv->vval.v_string);
3894 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003895 }
3896 else if (STRCMP(what, "dict") == 0)
3897 {
3898 rettv->v_type = VAR_DICT;
3899 rettv->vval.v_dict = pt->pt_dict;
3900 if (pt->pt_dict != NULL)
3901 ++pt->pt_dict->dv_refcount;
3902 }
3903 else if (STRCMP(what, "args") == 0)
3904 {
3905 rettv->v_type = VAR_LIST;
3906 if (rettv_list_alloc(rettv) == OK)
3907 {
3908 int i;
3909
3910 for (i = 0; i < pt->pt_argc; ++i)
3911 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
3912 }
3913 }
3914 else
3915 EMSG2(_(e_invarg2), what);
3916 return;
3917 }
3918 }
3919 else
3920 EMSG2(_(e_listdictarg), "get()");
3921
3922 if (tv == NULL)
3923 {
3924 if (argvars[2].v_type != VAR_UNKNOWN)
3925 copy_tv(&argvars[2], rettv);
3926 }
3927 else
3928 copy_tv(tv, rettv);
3929}
3930
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003931#ifdef FEAT_SIGNS
3932/*
3933 * Returns information about signs placed in a buffer as list of dicts.
3934 */
3935 static void
3936get_buffer_signs(buf_T *buf, list_T *l)
3937{
3938 signlist_T *sign;
3939
3940 for (sign = buf->b_signlist; sign; sign = sign->next)
3941 {
3942 dict_T *d = dict_alloc();
3943
3944 if (d != NULL)
3945 {
3946 dict_add_nr_str(d, "id", sign->id, NULL);
3947 dict_add_nr_str(d, "lnum", sign->lnum, NULL);
Bram Moolenaar6a402ed2016-08-28 14:11:24 +02003948 dict_add_nr_str(d, "name", 0L, sign_typenr2name(sign->typenr));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003949
3950 list_append_dict(l, d);
3951 }
3952 }
3953}
3954#endif
3955
3956/*
3957 * Returns buffer options, variables and other attributes in a dictionary.
3958 */
3959 static dict_T *
3960get_buffer_info(buf_T *buf)
3961{
3962 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003963 tabpage_T *tp;
3964 win_T *wp;
3965 list_T *windows;
3966
3967 dict = dict_alloc();
3968 if (dict == NULL)
3969 return NULL;
3970
Bram Moolenaar33928832016-08-18 21:22:04 +02003971 dict_add_nr_str(dict, "bufnr", buf->b_fnum, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003972 dict_add_nr_str(dict, "name", 0L,
3973 buf->b_ffname != NULL ? buf->b_ffname : (char_u *)"");
Bram Moolenaarf845b872017-01-06 14:04:54 +01003974 dict_add_nr_str(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
3975 : buflist_findlnum(buf), NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003976 dict_add_nr_str(dict, "loaded", buf->b_ml.ml_mfp != NULL, NULL);
3977 dict_add_nr_str(dict, "listed", buf->b_p_bl, NULL);
3978 dict_add_nr_str(dict, "changed", bufIsChanged(buf), NULL);
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003979 dict_add_nr_str(dict, "changedtick", CHANGEDTICK(buf), NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003980 dict_add_nr_str(dict, "hidden",
3981 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0,
3982 NULL);
3983
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02003984 /* Get a reference to buffer variables */
3985 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003986
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003987 /* List of windows displaying this buffer */
3988 windows = list_alloc();
3989 if (windows != NULL)
3990 {
3991 FOR_ALL_TAB_WINDOWS(tp, wp)
3992 if (wp->w_buffer == buf)
3993 list_append_number(windows, (varnumber_T)wp->w_id);
3994 dict_add_list(dict, "windows", windows);
3995 }
3996
3997#ifdef FEAT_SIGNS
3998 if (buf->b_signlist != NULL)
3999 {
4000 /* List of signs placed in this buffer */
4001 list_T *signs = list_alloc();
4002 if (signs != NULL)
4003 {
4004 get_buffer_signs(buf, signs);
4005 dict_add_list(dict, "signs", signs);
4006 }
4007 }
4008#endif
4009
4010 return dict;
4011}
4012
4013/*
4014 * "getbufinfo()" function
4015 */
4016 static void
4017f_getbufinfo(typval_T *argvars, typval_T *rettv)
4018{
4019 buf_T *buf = NULL;
4020 buf_T *argbuf = NULL;
4021 dict_T *d;
4022 int filtered = FALSE;
4023 int sel_buflisted = FALSE;
4024 int sel_bufloaded = FALSE;
4025
4026 if (rettv_list_alloc(rettv) != OK)
4027 return;
4028
4029 /* List of all the buffers or selected buffers */
4030 if (argvars[0].v_type == VAR_DICT)
4031 {
4032 dict_T *sel_d = argvars[0].vval.v_dict;
4033
4034 if (sel_d != NULL)
4035 {
4036 dictitem_T *di;
4037
4038 filtered = TRUE;
4039
4040 di = dict_find(sel_d, (char_u *)"buflisted", -1);
4041 if (di != NULL && get_tv_number(&di->di_tv))
4042 sel_buflisted = TRUE;
4043
4044 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
4045 if (di != NULL && get_tv_number(&di->di_tv))
4046 sel_bufloaded = TRUE;
4047 }
4048 }
4049 else if (argvars[0].v_type != VAR_UNKNOWN)
4050 {
4051 /* Information about one buffer. Argument specifies the buffer */
4052 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4053 ++emsg_off;
4054 argbuf = get_buf_tv(&argvars[0], FALSE);
4055 --emsg_off;
4056 if (argbuf == NULL)
4057 return;
4058 }
4059
4060 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004061 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004062 {
4063 if (argbuf != NULL && argbuf != buf)
4064 continue;
4065 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
4066 || (sel_buflisted && !buf->b_p_bl)))
4067 continue;
4068
4069 d = get_buffer_info(buf);
4070 if (d != NULL)
4071 list_append_dict(rettv->vval.v_list, d);
4072 if (argbuf != NULL)
4073 return;
4074 }
4075}
4076
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004077static void get_buffer_lines(buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv);
4078
4079/*
4080 * Get line or list of lines from buffer "buf" into "rettv".
4081 * Return a range (from start to end) of lines in rettv from the specified
4082 * buffer.
4083 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4084 */
4085 static void
4086get_buffer_lines(
4087 buf_T *buf,
4088 linenr_T start,
4089 linenr_T end,
4090 int retlist,
4091 typval_T *rettv)
4092{
4093 char_u *p;
4094
4095 rettv->v_type = VAR_STRING;
4096 rettv->vval.v_string = NULL;
4097 if (retlist && rettv_list_alloc(rettv) == FAIL)
4098 return;
4099
4100 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4101 return;
4102
4103 if (!retlist)
4104 {
4105 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4106 p = ml_get_buf(buf, start, FALSE);
4107 else
4108 p = (char_u *)"";
4109 rettv->vval.v_string = vim_strsave(p);
4110 }
4111 else
4112 {
4113 if (end < start)
4114 return;
4115
4116 if (start < 1)
4117 start = 1;
4118 if (end > buf->b_ml.ml_line_count)
4119 end = buf->b_ml.ml_line_count;
4120 while (start <= end)
4121 if (list_append_string(rettv->vval.v_list,
4122 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4123 break;
4124 }
4125}
4126
4127/*
4128 * Get the lnum from the first argument.
4129 * Also accepts "$", then "buf" is used.
4130 * Returns 0 on error.
4131 */
4132 static linenr_T
4133get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
4134{
4135 if (argvars[0].v_type == VAR_STRING
4136 && argvars[0].vval.v_string != NULL
4137 && argvars[0].vval.v_string[0] == '$'
4138 && buf != NULL)
4139 return buf->b_ml.ml_line_count;
4140 return (linenr_T)get_tv_number_chk(&argvars[0], NULL);
4141}
4142
4143/*
4144 * "getbufline()" function
4145 */
4146 static void
4147f_getbufline(typval_T *argvars, typval_T *rettv)
4148{
4149 linenr_T lnum;
4150 linenr_T end;
4151 buf_T *buf;
4152
4153 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4154 ++emsg_off;
4155 buf = get_buf_tv(&argvars[0], FALSE);
4156 --emsg_off;
4157
4158 lnum = get_tv_lnum_buf(&argvars[1], buf);
4159 if (argvars[2].v_type == VAR_UNKNOWN)
4160 end = lnum;
4161 else
4162 end = get_tv_lnum_buf(&argvars[2], buf);
4163
4164 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4165}
4166
4167/*
4168 * "getbufvar()" function
4169 */
4170 static void
4171f_getbufvar(typval_T *argvars, typval_T *rettv)
4172{
4173 buf_T *buf;
4174 buf_T *save_curbuf;
4175 char_u *varname;
4176 dictitem_T *v;
4177 int done = FALSE;
4178
4179 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4180 varname = get_tv_string_chk(&argvars[1]);
4181 ++emsg_off;
4182 buf = get_buf_tv(&argvars[0], FALSE);
4183
4184 rettv->v_type = VAR_STRING;
4185 rettv->vval.v_string = NULL;
4186
4187 if (buf != NULL && varname != NULL)
4188 {
4189 /* set curbuf to be our buf, temporarily */
4190 save_curbuf = curbuf;
4191 curbuf = buf;
4192
Bram Moolenaar30567352016-08-27 21:25:44 +02004193 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004194 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004195 if (varname[1] == NUL)
4196 {
4197 /* get all buffer-local options in a dict */
4198 dict_T *opts = get_winbuf_options(TRUE);
4199
4200 if (opts != NULL)
4201 {
4202 rettv->v_type = VAR_DICT;
4203 rettv->vval.v_dict = opts;
4204 ++opts->dv_refcount;
4205 done = TRUE;
4206 }
4207 }
4208 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4209 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004210 done = TRUE;
4211 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004212 else
4213 {
4214 /* Look up the variable. */
4215 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4216 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4217 'b', varname, FALSE);
4218 if (v != NULL)
4219 {
4220 copy_tv(&v->di_tv, rettv);
4221 done = TRUE;
4222 }
4223 }
4224
4225 /* restore previous notion of curbuf */
4226 curbuf = save_curbuf;
4227 }
4228
4229 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4230 /* use the default value */
4231 copy_tv(&argvars[2], rettv);
4232
4233 --emsg_off;
4234}
4235
4236/*
4237 * "getchar()" function
4238 */
4239 static void
4240f_getchar(typval_T *argvars, typval_T *rettv)
4241{
4242 varnumber_T n;
4243 int error = FALSE;
4244
4245 /* Position the cursor. Needed after a message that ends in a space. */
4246 windgoto(msg_row, msg_col);
4247
4248 ++no_mapping;
4249 ++allow_keys;
4250 for (;;)
4251 {
4252 if (argvars[0].v_type == VAR_UNKNOWN)
4253 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004254 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004255 else if (get_tv_number_chk(&argvars[0], &error) == 1)
4256 /* getchar(1): only check if char avail */
4257 n = vpeekc_any();
4258 else if (error || vpeekc_any() == NUL)
4259 /* illegal argument or getchar(0) and no char avail: return zero */
4260 n = 0;
4261 else
4262 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004263 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004264
4265 if (n == K_IGNORE)
4266 continue;
4267 break;
4268 }
4269 --no_mapping;
4270 --allow_keys;
4271
4272 set_vim_var_nr(VV_MOUSE_WIN, 0);
4273 set_vim_var_nr(VV_MOUSE_WINID, 0);
4274 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4275 set_vim_var_nr(VV_MOUSE_COL, 0);
4276
4277 rettv->vval.v_number = n;
4278 if (IS_SPECIAL(n) || mod_mask != 0)
4279 {
4280 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4281 int i = 0;
4282
4283 /* Turn a special key into three bytes, plus modifier. */
4284 if (mod_mask != 0)
4285 {
4286 temp[i++] = K_SPECIAL;
4287 temp[i++] = KS_MODIFIER;
4288 temp[i++] = mod_mask;
4289 }
4290 if (IS_SPECIAL(n))
4291 {
4292 temp[i++] = K_SPECIAL;
4293 temp[i++] = K_SECOND(n);
4294 temp[i++] = K_THIRD(n);
4295 }
4296#ifdef FEAT_MBYTE
4297 else if (has_mbyte)
4298 i += (*mb_char2bytes)(n, temp + i);
4299#endif
4300 else
4301 temp[i++] = n;
4302 temp[i++] = NUL;
4303 rettv->v_type = VAR_STRING;
4304 rettv->vval.v_string = vim_strsave(temp);
4305
4306#ifdef FEAT_MOUSE
4307 if (is_mouse_key(n))
4308 {
4309 int row = mouse_row;
4310 int col = mouse_col;
4311 win_T *win;
4312 linenr_T lnum;
4313# ifdef FEAT_WINDOWS
4314 win_T *wp;
4315# endif
4316 int winnr = 1;
4317
4318 if (row >= 0 && col >= 0)
4319 {
4320 /* Find the window at the mouse coordinates and compute the
4321 * text position. */
4322 win = mouse_find_win(&row, &col);
4323 (void)mouse_comp_pos(win, &row, &col, &lnum);
4324# ifdef FEAT_WINDOWS
4325 for (wp = firstwin; wp != win; wp = wp->w_next)
4326 ++winnr;
4327# endif
4328 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4329 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4330 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4331 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4332 }
4333 }
4334#endif
4335 }
4336}
4337
4338/*
4339 * "getcharmod()" function
4340 */
4341 static void
4342f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4343{
4344 rettv->vval.v_number = mod_mask;
4345}
4346
4347/*
4348 * "getcharsearch()" function
4349 */
4350 static void
4351f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4352{
4353 if (rettv_dict_alloc(rettv) != FAIL)
4354 {
4355 dict_T *dict = rettv->vval.v_dict;
4356
4357 dict_add_nr_str(dict, "char", 0L, last_csearch());
4358 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
4359 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
4360 }
4361}
4362
4363/*
4364 * "getcmdline()" function
4365 */
4366 static void
4367f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4368{
4369 rettv->v_type = VAR_STRING;
4370 rettv->vval.v_string = get_cmdline_str();
4371}
4372
4373/*
4374 * "getcmdpos()" function
4375 */
4376 static void
4377f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4378{
4379 rettv->vval.v_number = get_cmdline_pos() + 1;
4380}
4381
4382/*
4383 * "getcmdtype()" function
4384 */
4385 static void
4386f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4387{
4388 rettv->v_type = VAR_STRING;
4389 rettv->vval.v_string = alloc(2);
4390 if (rettv->vval.v_string != NULL)
4391 {
4392 rettv->vval.v_string[0] = get_cmdline_type();
4393 rettv->vval.v_string[1] = NUL;
4394 }
4395}
4396
4397/*
4398 * "getcmdwintype()" function
4399 */
4400 static void
4401f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4402{
4403 rettv->v_type = VAR_STRING;
4404 rettv->vval.v_string = NULL;
4405#ifdef FEAT_CMDWIN
4406 rettv->vval.v_string = alloc(2);
4407 if (rettv->vval.v_string != NULL)
4408 {
4409 rettv->vval.v_string[0] = cmdwin_type;
4410 rettv->vval.v_string[1] = NUL;
4411 }
4412#endif
4413}
4414
4415#if defined(FEAT_CMDL_COMPL)
4416/*
4417 * "getcompletion()" function
4418 */
4419 static void
4420f_getcompletion(typval_T *argvars, typval_T *rettv)
4421{
4422 char_u *pat;
4423 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004424 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004425 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
4426 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004427
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004428 if (argvars[2].v_type != VAR_UNKNOWN)
4429 filtered = get_tv_number_chk(&argvars[2], NULL);
4430
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004431 if (p_wic)
4432 options |= WILD_ICASE;
4433
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004434 /* For filtered results, 'wildignore' is used */
4435 if (!filtered)
4436 options |= WILD_KEEP_ALL;
4437
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004438 ExpandInit(&xpc);
4439 xpc.xp_pattern = get_tv_string(&argvars[0]);
4440 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4441 xpc.xp_context = cmdcomplete_str_to_type(get_tv_string(&argvars[1]));
4442 if (xpc.xp_context == EXPAND_NOTHING)
4443 {
4444 if (argvars[1].v_type == VAR_STRING)
4445 EMSG2(_(e_invarg2), argvars[1].vval.v_string);
4446 else
4447 EMSG(_(e_invarg));
4448 return;
4449 }
4450
4451# if defined(FEAT_MENU)
4452 if (xpc.xp_context == EXPAND_MENUS)
4453 {
4454 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4455 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4456 }
4457# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02004458#ifdef FEAT_CSCOPE
4459 if (xpc.xp_context == EXPAND_CSCOPE)
4460 {
4461 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4462 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4463 }
4464#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02004465#ifdef FEAT_SIGNS
4466 if (xpc.xp_context == EXPAND_SIGN)
4467 {
4468 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4469 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4470 }
4471#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004472
4473 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4474 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
4475 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004476 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004477
4478 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4479
4480 for (i = 0; i < xpc.xp_numfiles; i++)
4481 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4482 }
4483 vim_free(pat);
4484 ExpandCleanup(&xpc);
4485}
4486#endif
4487
4488/*
4489 * "getcwd()" function
4490 */
4491 static void
4492f_getcwd(typval_T *argvars, typval_T *rettv)
4493{
4494 win_T *wp = NULL;
4495 char_u *cwd;
4496
4497 rettv->v_type = VAR_STRING;
4498 rettv->vval.v_string = NULL;
4499
4500 wp = find_tabwin(&argvars[0], &argvars[1]);
4501 if (wp != NULL)
4502 {
4503 if (wp->w_localdir != NULL)
4504 rettv->vval.v_string = vim_strsave(wp->w_localdir);
4505 else if (globaldir != NULL)
4506 rettv->vval.v_string = vim_strsave(globaldir);
4507 else
4508 {
4509 cwd = alloc(MAXPATHL);
4510 if (cwd != NULL)
4511 {
4512 if (mch_dirname(cwd, MAXPATHL) != FAIL)
4513 rettv->vval.v_string = vim_strsave(cwd);
4514 vim_free(cwd);
4515 }
4516 }
4517#ifdef BACKSLASH_IN_FILENAME
4518 if (rettv->vval.v_string != NULL)
4519 slash_adjust(rettv->vval.v_string);
4520#endif
4521 }
4522}
4523
4524/*
4525 * "getfontname()" function
4526 */
4527 static void
4528f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4529{
4530 rettv->v_type = VAR_STRING;
4531 rettv->vval.v_string = NULL;
4532#ifdef FEAT_GUI
4533 if (gui.in_use)
4534 {
4535 GuiFont font;
4536 char_u *name = NULL;
4537
4538 if (argvars[0].v_type == VAR_UNKNOWN)
4539 {
4540 /* Get the "Normal" font. Either the name saved by
4541 * hl_set_font_name() or from the font ID. */
4542 font = gui.norm_font;
4543 name = hl_get_font_name();
4544 }
4545 else
4546 {
4547 name = get_tv_string(&argvars[0]);
4548 if (STRCMP(name, "*") == 0) /* don't use font dialog */
4549 return;
4550 font = gui_mch_get_font(name, FALSE);
4551 if (font == NOFONT)
4552 return; /* Invalid font name, return empty string. */
4553 }
4554 rettv->vval.v_string = gui_mch_get_fontname(font, name);
4555 if (argvars[0].v_type != VAR_UNKNOWN)
4556 gui_mch_free_font(font);
4557 }
4558#endif
4559}
4560
4561/*
4562 * "getfperm({fname})" function
4563 */
4564 static void
4565f_getfperm(typval_T *argvars, typval_T *rettv)
4566{
4567 char_u *fname;
4568 stat_T st;
4569 char_u *perm = NULL;
4570 char_u flags[] = "rwx";
4571 int i;
4572
4573 fname = get_tv_string(&argvars[0]);
4574
4575 rettv->v_type = VAR_STRING;
4576 if (mch_stat((char *)fname, &st) >= 0)
4577 {
4578 perm = vim_strsave((char_u *)"---------");
4579 if (perm != NULL)
4580 {
4581 for (i = 0; i < 9; i++)
4582 {
4583 if (st.st_mode & (1 << (8 - i)))
4584 perm[i] = flags[i % 3];
4585 }
4586 }
4587 }
4588 rettv->vval.v_string = perm;
4589}
4590
4591/*
4592 * "getfsize({fname})" function
4593 */
4594 static void
4595f_getfsize(typval_T *argvars, typval_T *rettv)
4596{
4597 char_u *fname;
4598 stat_T st;
4599
4600 fname = get_tv_string(&argvars[0]);
4601
4602 rettv->v_type = VAR_NUMBER;
4603
4604 if (mch_stat((char *)fname, &st) >= 0)
4605 {
4606 if (mch_isdir(fname))
4607 rettv->vval.v_number = 0;
4608 else
4609 {
4610 rettv->vval.v_number = (varnumber_T)st.st_size;
4611
4612 /* non-perfect check for overflow */
4613 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
4614 rettv->vval.v_number = -2;
4615 }
4616 }
4617 else
4618 rettv->vval.v_number = -1;
4619}
4620
4621/*
4622 * "getftime({fname})" function
4623 */
4624 static void
4625f_getftime(typval_T *argvars, typval_T *rettv)
4626{
4627 char_u *fname;
4628 stat_T st;
4629
4630 fname = get_tv_string(&argvars[0]);
4631
4632 if (mch_stat((char *)fname, &st) >= 0)
4633 rettv->vval.v_number = (varnumber_T)st.st_mtime;
4634 else
4635 rettv->vval.v_number = -1;
4636}
4637
4638/*
4639 * "getftype({fname})" function
4640 */
4641 static void
4642f_getftype(typval_T *argvars, typval_T *rettv)
4643{
4644 char_u *fname;
4645 stat_T st;
4646 char_u *type = NULL;
4647 char *t;
4648
4649 fname = get_tv_string(&argvars[0]);
4650
4651 rettv->v_type = VAR_STRING;
4652 if (mch_lstat((char *)fname, &st) >= 0)
4653 {
4654#ifdef S_ISREG
4655 if (S_ISREG(st.st_mode))
4656 t = "file";
4657 else if (S_ISDIR(st.st_mode))
4658 t = "dir";
4659# ifdef S_ISLNK
4660 else if (S_ISLNK(st.st_mode))
4661 t = "link";
4662# endif
4663# ifdef S_ISBLK
4664 else if (S_ISBLK(st.st_mode))
4665 t = "bdev";
4666# endif
4667# ifdef S_ISCHR
4668 else if (S_ISCHR(st.st_mode))
4669 t = "cdev";
4670# endif
4671# ifdef S_ISFIFO
4672 else if (S_ISFIFO(st.st_mode))
4673 t = "fifo";
4674# endif
4675# ifdef S_ISSOCK
4676 else if (S_ISSOCK(st.st_mode))
4677 t = "fifo";
4678# endif
4679 else
4680 t = "other";
4681#else
4682# ifdef S_IFMT
4683 switch (st.st_mode & S_IFMT)
4684 {
4685 case S_IFREG: t = "file"; break;
4686 case S_IFDIR: t = "dir"; break;
4687# ifdef S_IFLNK
4688 case S_IFLNK: t = "link"; break;
4689# endif
4690# ifdef S_IFBLK
4691 case S_IFBLK: t = "bdev"; break;
4692# endif
4693# ifdef S_IFCHR
4694 case S_IFCHR: t = "cdev"; break;
4695# endif
4696# ifdef S_IFIFO
4697 case S_IFIFO: t = "fifo"; break;
4698# endif
4699# ifdef S_IFSOCK
4700 case S_IFSOCK: t = "socket"; break;
4701# endif
4702 default: t = "other";
4703 }
4704# else
4705 if (mch_isdir(fname))
4706 t = "dir";
4707 else
4708 t = "file";
4709# endif
4710#endif
4711 type = vim_strsave((char_u *)t);
4712 }
4713 rettv->vval.v_string = type;
4714}
4715
4716/*
4717 * "getline(lnum, [end])" function
4718 */
4719 static void
4720f_getline(typval_T *argvars, typval_T *rettv)
4721{
4722 linenr_T lnum;
4723 linenr_T end;
4724 int retlist;
4725
4726 lnum = get_tv_lnum(argvars);
4727 if (argvars[1].v_type == VAR_UNKNOWN)
4728 {
4729 end = 0;
4730 retlist = FALSE;
4731 }
4732 else
4733 {
4734 end = get_tv_lnum(&argvars[1]);
4735 retlist = TRUE;
4736 }
4737
4738 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
4739}
4740
Bram Moolenaar4ae20952016-08-13 15:29:14 +02004741#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02004742 static void
4743get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
4744{
Bram Moolenaard823fa92016-08-12 16:29:27 +02004745 if (what_arg->v_type == VAR_UNKNOWN)
4746 {
4747 if (rettv_list_alloc(rettv) == OK)
4748 if (is_qf || wp != NULL)
4749 (void)get_errorlist(wp, -1, rettv->vval.v_list);
4750 }
4751 else
4752 {
4753 if (rettv_dict_alloc(rettv) == OK)
4754 if (is_qf || (wp != NULL))
4755 {
4756 if (what_arg->v_type == VAR_DICT)
4757 {
4758 dict_T *d = what_arg->vval.v_dict;
4759
4760 if (d != NULL)
4761 get_errorlist_properties(wp, d, rettv->vval.v_dict);
4762 }
4763 else
4764 EMSG(_(e_dictreq));
4765 }
4766 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004767}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02004768#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02004769
4770/*
4771 * "getloclist()" function
4772 */
4773 static void
4774f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4775{
4776#ifdef FEAT_QUICKFIX
4777 win_T *wp;
4778
4779 wp = find_win_by_nr(&argvars[0], NULL);
4780 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
4781#endif
4782}
4783
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004784/*
4785 * "getmatches()" function
4786 */
4787 static void
4788f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4789{
4790#ifdef FEAT_SEARCH_EXTRA
4791 dict_T *dict;
4792 matchitem_T *cur = curwin->w_match_head;
4793 int i;
4794
4795 if (rettv_list_alloc(rettv) == OK)
4796 {
4797 while (cur != NULL)
4798 {
4799 dict = dict_alloc();
4800 if (dict == NULL)
4801 return;
4802 if (cur->match.regprog == NULL)
4803 {
4804 /* match added with matchaddpos() */
4805 for (i = 0; i < MAXPOSMATCH; ++i)
4806 {
4807 llpos_T *llpos;
4808 char buf[6];
4809 list_T *l;
4810
4811 llpos = &cur->pos.pos[i];
4812 if (llpos->lnum == 0)
4813 break;
4814 l = list_alloc();
4815 if (l == NULL)
4816 break;
4817 list_append_number(l, (varnumber_T)llpos->lnum);
4818 if (llpos->col > 0)
4819 {
4820 list_append_number(l, (varnumber_T)llpos->col);
4821 list_append_number(l, (varnumber_T)llpos->len);
4822 }
4823 sprintf(buf, "pos%d", i + 1);
4824 dict_add_list(dict, buf, l);
4825 }
4826 }
4827 else
4828 {
4829 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
4830 }
4831 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
4832 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
4833 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
4834# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
4835 if (cur->conceal_char)
4836 {
4837 char_u buf[MB_MAXBYTES + 1];
4838
4839 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
4840 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
4841 }
4842# endif
4843 list_append_dict(rettv->vval.v_list, dict);
4844 cur = cur->next;
4845 }
4846 }
4847#endif
4848}
4849
4850/*
4851 * "getpid()" function
4852 */
4853 static void
4854f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
4855{
4856 rettv->vval.v_number = mch_get_pid();
4857}
4858
4859 static void
4860getpos_both(
4861 typval_T *argvars,
4862 typval_T *rettv,
4863 int getcurpos)
4864{
4865 pos_T *fp;
4866 list_T *l;
4867 int fnum = -1;
4868
4869 if (rettv_list_alloc(rettv) == OK)
4870 {
4871 l = rettv->vval.v_list;
4872 if (getcurpos)
4873 fp = &curwin->w_cursor;
4874 else
4875 fp = var2fpos(&argvars[0], TRUE, &fnum);
4876 if (fnum != -1)
4877 list_append_number(l, (varnumber_T)fnum);
4878 else
4879 list_append_number(l, (varnumber_T)0);
4880 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
4881 : (varnumber_T)0);
4882 list_append_number(l, (fp != NULL)
4883 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
4884 : (varnumber_T)0);
4885 list_append_number(l,
4886#ifdef FEAT_VIRTUALEDIT
4887 (fp != NULL) ? (varnumber_T)fp->coladd :
4888#endif
4889 (varnumber_T)0);
4890 if (getcurpos)
4891 {
4892 update_curswant();
4893 list_append_number(l, curwin->w_curswant == MAXCOL ?
4894 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
4895 }
4896 }
4897 else
4898 rettv->vval.v_number = FALSE;
4899}
4900
4901
4902/*
4903 * "getcurpos()" function
4904 */
4905 static void
4906f_getcurpos(typval_T *argvars, typval_T *rettv)
4907{
4908 getpos_both(argvars, rettv, TRUE);
4909}
4910
4911/*
4912 * "getpos(string)" function
4913 */
4914 static void
4915f_getpos(typval_T *argvars, typval_T *rettv)
4916{
4917 getpos_both(argvars, rettv, FALSE);
4918}
4919
4920/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004921 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004922 */
4923 static void
4924f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4925{
4926#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02004927 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004928#endif
4929}
4930
4931/*
4932 * "getreg()" function
4933 */
4934 static void
4935f_getreg(typval_T *argvars, typval_T *rettv)
4936{
4937 char_u *strregname;
4938 int regname;
4939 int arg2 = FALSE;
4940 int return_list = FALSE;
4941 int error = FALSE;
4942
4943 if (argvars[0].v_type != VAR_UNKNOWN)
4944 {
4945 strregname = get_tv_string_chk(&argvars[0]);
4946 error = strregname == NULL;
4947 if (argvars[1].v_type != VAR_UNKNOWN)
4948 {
4949 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
4950 if (!error && argvars[2].v_type != VAR_UNKNOWN)
4951 return_list = (int)get_tv_number_chk(&argvars[2], &error);
4952 }
4953 }
4954 else
4955 strregname = get_vim_var_str(VV_REG);
4956
4957 if (error)
4958 return;
4959
4960 regname = (strregname == NULL ? '"' : *strregname);
4961 if (regname == 0)
4962 regname = '"';
4963
4964 if (return_list)
4965 {
4966 rettv->v_type = VAR_LIST;
4967 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
4968 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
4969 if (rettv->vval.v_list == NULL)
4970 (void)rettv_list_alloc(rettv);
4971 else
4972 ++rettv->vval.v_list->lv_refcount;
4973 }
4974 else
4975 {
4976 rettv->v_type = VAR_STRING;
4977 rettv->vval.v_string = get_reg_contents(regname,
4978 arg2 ? GREG_EXPR_SRC : 0);
4979 }
4980}
4981
4982/*
4983 * "getregtype()" function
4984 */
4985 static void
4986f_getregtype(typval_T *argvars, typval_T *rettv)
4987{
4988 char_u *strregname;
4989 int regname;
4990 char_u buf[NUMBUFLEN + 2];
4991 long reglen = 0;
4992
4993 if (argvars[0].v_type != VAR_UNKNOWN)
4994 {
4995 strregname = get_tv_string_chk(&argvars[0]);
4996 if (strregname == NULL) /* type error; errmsg already given */
4997 {
4998 rettv->v_type = VAR_STRING;
4999 rettv->vval.v_string = NULL;
5000 return;
5001 }
5002 }
5003 else
5004 /* Default to v:register */
5005 strregname = get_vim_var_str(VV_REG);
5006
5007 regname = (strregname == NULL ? '"' : *strregname);
5008 if (regname == 0)
5009 regname = '"';
5010
5011 buf[0] = NUL;
5012 buf[1] = NUL;
5013 switch (get_reg_type(regname, &reglen))
5014 {
5015 case MLINE: buf[0] = 'V'; break;
5016 case MCHAR: buf[0] = 'v'; break;
5017 case MBLOCK:
5018 buf[0] = Ctrl_V;
5019 sprintf((char *)buf + 1, "%ld", reglen + 1);
5020 break;
5021 }
5022 rettv->v_type = VAR_STRING;
5023 rettv->vval.v_string = vim_strsave(buf);
5024}
5025
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005026#ifdef FEAT_WINDOWS
5027/*
5028 * Returns information (variables, options, etc.) about a tab page
5029 * as a dictionary.
5030 */
5031 static dict_T *
5032get_tabpage_info(tabpage_T *tp, int tp_idx)
5033{
5034 win_T *wp;
5035 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005036 list_T *l;
5037
5038 dict = dict_alloc();
5039 if (dict == NULL)
5040 return NULL;
5041
Bram Moolenaar33928832016-08-18 21:22:04 +02005042 dict_add_nr_str(dict, "tabnr", tp_idx, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005043
5044 l = list_alloc();
5045 if (l != NULL)
5046 {
5047 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5048 wp; wp = wp->w_next)
5049 list_append_number(l, (varnumber_T)wp->w_id);
5050 dict_add_list(dict, "windows", l);
5051 }
5052
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005053 /* Make a reference to tabpage variables */
5054 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005055
5056 return dict;
5057}
5058#endif
5059
5060/*
5061 * "gettabinfo()" function
5062 */
5063 static void
5064f_gettabinfo(typval_T *argvars, typval_T *rettv)
5065{
5066#ifdef FEAT_WINDOWS
5067 tabpage_T *tp, *tparg = NULL;
5068 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005069 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005070
5071 if (rettv_list_alloc(rettv) != OK)
5072 return;
5073
5074 if (argvars[0].v_type != VAR_UNKNOWN)
5075 {
5076 /* Information about one tab page */
5077 tparg = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5078 if (tparg == NULL)
5079 return;
5080 }
5081
5082 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005083 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005084 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005085 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005086 if (tparg != NULL && tp != tparg)
5087 continue;
5088 d = get_tabpage_info(tp, tpnr);
5089 if (d != NULL)
5090 list_append_dict(rettv->vval.v_list, d);
5091 if (tparg != NULL)
5092 return;
5093 }
5094#endif
5095}
5096
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005097/*
5098 * "gettabvar()" function
5099 */
5100 static void
5101f_gettabvar(typval_T *argvars, typval_T *rettv)
5102{
5103 win_T *oldcurwin;
5104 tabpage_T *tp, *oldtabpage;
5105 dictitem_T *v;
5106 char_u *varname;
5107 int done = FALSE;
5108
5109 rettv->v_type = VAR_STRING;
5110 rettv->vval.v_string = NULL;
5111
5112 varname = get_tv_string_chk(&argvars[1]);
5113 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5114 if (tp != NULL && varname != NULL)
5115 {
5116 /* Set tp to be our tabpage, temporarily. Also set the window to the
5117 * first window in the tabpage, otherwise the window is not valid. */
5118 if (switch_win(&oldcurwin, &oldtabpage,
5119 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
5120 == OK)
5121 {
5122 /* look up the variable */
5123 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5124 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5125 if (v != NULL)
5126 {
5127 copy_tv(&v->di_tv, rettv);
5128 done = TRUE;
5129 }
5130 }
5131
5132 /* restore previous notion of curwin */
5133 restore_win(oldcurwin, oldtabpage, TRUE);
5134 }
5135
5136 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5137 copy_tv(&argvars[2], rettv);
5138}
5139
5140/*
5141 * "gettabwinvar()" function
5142 */
5143 static void
5144f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5145{
5146 getwinvar(argvars, rettv, 1);
5147}
5148
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005149#ifdef FEAT_WINDOWS
5150/*
5151 * Returns information about a window as a dictionary.
5152 */
5153 static dict_T *
5154get_win_info(win_T *wp, short tpnr, short winnr)
5155{
5156 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005157
5158 dict = dict_alloc();
5159 if (dict == NULL)
5160 return NULL;
5161
Bram Moolenaar33928832016-08-18 21:22:04 +02005162 dict_add_nr_str(dict, "tabnr", tpnr, NULL);
5163 dict_add_nr_str(dict, "winnr", winnr, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005164 dict_add_nr_str(dict, "winid", wp->w_id, NULL);
5165 dict_add_nr_str(dict, "height", wp->w_height, NULL);
5166 dict_add_nr_str(dict, "width", wp->w_width, NULL);
Bram Moolenaar33928832016-08-18 21:22:04 +02005167 dict_add_nr_str(dict, "bufnr", wp->w_buffer->b_fnum, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005168
Bram Moolenaar386600f2016-08-15 22:16:25 +02005169#ifdef FEAT_QUICKFIX
5170 dict_add_nr_str(dict, "quickfix", bt_quickfix(wp->w_buffer), NULL);
5171 dict_add_nr_str(dict, "loclist",
5172 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL), NULL);
5173#endif
5174
Bram Moolenaar30567352016-08-27 21:25:44 +02005175 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005176 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005177
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005178 return dict;
5179}
5180#endif
5181
5182/*
5183 * "getwininfo()" function
5184 */
5185 static void
5186f_getwininfo(typval_T *argvars, typval_T *rettv)
5187{
5188#ifdef FEAT_WINDOWS
5189 tabpage_T *tp;
5190 win_T *wp = NULL, *wparg = NULL;
5191 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005192 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005193#endif
5194
5195 if (rettv_list_alloc(rettv) != OK)
5196 return;
5197
5198#ifdef FEAT_WINDOWS
5199 if (argvars[0].v_type != VAR_UNKNOWN)
5200 {
5201 wparg = win_id2wp(argvars);
5202 if (wparg == NULL)
5203 return;
5204 }
5205
5206 /* Collect information about either all the windows across all the tab
5207 * pages or one particular window.
5208 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005209 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005210 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005211 tabnr++;
5212 winnr = 0;
5213 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005214 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005215 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005216 if (wparg != NULL && wp != wparg)
5217 continue;
5218 d = get_win_info(wp, tabnr, winnr);
5219 if (d != NULL)
5220 list_append_dict(rettv->vval.v_list, d);
5221 if (wparg != NULL)
5222 /* found information about a specific window */
5223 return;
5224 }
5225 }
5226#endif
5227}
5228
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005229/*
5230 * "getwinposx()" function
5231 */
5232 static void
5233f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5234{
5235 rettv->vval.v_number = -1;
5236#ifdef FEAT_GUI
5237 if (gui.in_use)
5238 {
5239 int x, y;
5240
5241 if (gui_mch_get_winpos(&x, &y) == OK)
5242 rettv->vval.v_number = x;
5243 }
5244#endif
5245}
5246
5247/*
5248 * "win_findbuf()" function
5249 */
5250 static void
5251f_win_findbuf(typval_T *argvars, typval_T *rettv)
5252{
5253 if (rettv_list_alloc(rettv) != FAIL)
5254 win_findbuf(argvars, rettv->vval.v_list);
5255}
5256
5257/*
5258 * "win_getid()" function
5259 */
5260 static void
5261f_win_getid(typval_T *argvars, typval_T *rettv)
5262{
5263 rettv->vval.v_number = win_getid(argvars);
5264}
5265
5266/*
5267 * "win_gotoid()" function
5268 */
5269 static void
5270f_win_gotoid(typval_T *argvars, typval_T *rettv)
5271{
5272 rettv->vval.v_number = win_gotoid(argvars);
5273}
5274
5275/*
5276 * "win_id2tabwin()" function
5277 */
5278 static void
5279f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5280{
5281 if (rettv_list_alloc(rettv) != FAIL)
5282 win_id2tabwin(argvars, rettv->vval.v_list);
5283}
5284
5285/*
5286 * "win_id2win()" function
5287 */
5288 static void
5289f_win_id2win(typval_T *argvars, typval_T *rettv)
5290{
5291 rettv->vval.v_number = win_id2win(argvars);
5292}
5293
5294/*
5295 * "getwinposy()" function
5296 */
5297 static void
5298f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5299{
5300 rettv->vval.v_number = -1;
5301#ifdef FEAT_GUI
5302 if (gui.in_use)
5303 {
5304 int x, y;
5305
5306 if (gui_mch_get_winpos(&x, &y) == OK)
5307 rettv->vval.v_number = y;
5308 }
5309#endif
5310}
5311
5312/*
5313 * "getwinvar()" function
5314 */
5315 static void
5316f_getwinvar(typval_T *argvars, typval_T *rettv)
5317{
5318 getwinvar(argvars, rettv, 0);
5319}
5320
5321/*
5322 * "glob()" function
5323 */
5324 static void
5325f_glob(typval_T *argvars, typval_T *rettv)
5326{
5327 int options = WILD_SILENT|WILD_USE_NL;
5328 expand_T xpc;
5329 int error = FALSE;
5330
5331 /* When the optional second argument is non-zero, don't remove matches
5332 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5333 rettv->v_type = VAR_STRING;
5334 if (argvars[1].v_type != VAR_UNKNOWN)
5335 {
5336 if (get_tv_number_chk(&argvars[1], &error))
5337 options |= WILD_KEEP_ALL;
5338 if (argvars[2].v_type != VAR_UNKNOWN)
5339 {
5340 if (get_tv_number_chk(&argvars[2], &error))
5341 {
5342 rettv->v_type = VAR_LIST;
5343 rettv->vval.v_list = NULL;
5344 }
5345 if (argvars[3].v_type != VAR_UNKNOWN
5346 && get_tv_number_chk(&argvars[3], &error))
5347 options |= WILD_ALLLINKS;
5348 }
5349 }
5350 if (!error)
5351 {
5352 ExpandInit(&xpc);
5353 xpc.xp_context = EXPAND_FILES;
5354 if (p_wic)
5355 options += WILD_ICASE;
5356 if (rettv->v_type == VAR_STRING)
5357 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
5358 NULL, options, WILD_ALL);
5359 else if (rettv_list_alloc(rettv) != FAIL)
5360 {
5361 int i;
5362
5363 ExpandOne(&xpc, get_tv_string(&argvars[0]),
5364 NULL, options, WILD_ALL_KEEP);
5365 for (i = 0; i < xpc.xp_numfiles; i++)
5366 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5367
5368 ExpandCleanup(&xpc);
5369 }
5370 }
5371 else
5372 rettv->vval.v_string = NULL;
5373}
5374
5375/*
5376 * "globpath()" function
5377 */
5378 static void
5379f_globpath(typval_T *argvars, typval_T *rettv)
5380{
5381 int flags = 0;
5382 char_u buf1[NUMBUFLEN];
5383 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
5384 int error = FALSE;
5385 garray_T ga;
5386 int i;
5387
5388 /* When the optional second argument is non-zero, don't remove matches
5389 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5390 rettv->v_type = VAR_STRING;
5391 if (argvars[2].v_type != VAR_UNKNOWN)
5392 {
5393 if (get_tv_number_chk(&argvars[2], &error))
5394 flags |= WILD_KEEP_ALL;
5395 if (argvars[3].v_type != VAR_UNKNOWN)
5396 {
5397 if (get_tv_number_chk(&argvars[3], &error))
5398 {
5399 rettv->v_type = VAR_LIST;
5400 rettv->vval.v_list = NULL;
5401 }
5402 if (argvars[4].v_type != VAR_UNKNOWN
5403 && get_tv_number_chk(&argvars[4], &error))
5404 flags |= WILD_ALLLINKS;
5405 }
5406 }
5407 if (file != NULL && !error)
5408 {
5409 ga_init2(&ga, (int)sizeof(char_u *), 10);
5410 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
5411 if (rettv->v_type == VAR_STRING)
5412 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
5413 else if (rettv_list_alloc(rettv) != FAIL)
5414 for (i = 0; i < ga.ga_len; ++i)
5415 list_append_string(rettv->vval.v_list,
5416 ((char_u **)(ga.ga_data))[i], -1);
5417 ga_clear_strings(&ga);
5418 }
5419 else
5420 rettv->vval.v_string = NULL;
5421}
5422
5423/*
5424 * "glob2regpat()" function
5425 */
5426 static void
5427f_glob2regpat(typval_T *argvars, typval_T *rettv)
5428{
5429 char_u *pat = get_tv_string_chk(&argvars[0]);
5430
5431 rettv->v_type = VAR_STRING;
5432 rettv->vval.v_string = (pat == NULL)
5433 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
5434}
5435
5436/* for VIM_VERSION_ defines */
5437#include "version.h"
5438
5439/*
5440 * "has()" function
5441 */
5442 static void
5443f_has(typval_T *argvars, typval_T *rettv)
5444{
5445 int i;
5446 char_u *name;
5447 int n = FALSE;
5448 static char *(has_list[]) =
5449 {
5450#ifdef AMIGA
5451 "amiga",
5452# ifdef FEAT_ARP
5453 "arp",
5454# endif
5455#endif
5456#ifdef __BEOS__
5457 "beos",
5458#endif
5459#ifdef MACOS
5460 "mac",
5461#endif
5462#if defined(MACOS_X_UNIX)
5463 "macunix", /* built with 'darwin' enabled */
5464#endif
5465#if defined(__APPLE__) && __APPLE__ == 1
5466 "osx", /* built with or without 'darwin' enabled */
5467#endif
5468#ifdef __QNX__
5469 "qnx",
5470#endif
5471#ifdef UNIX
5472 "unix",
5473#endif
5474#ifdef VMS
5475 "vms",
5476#endif
5477#ifdef WIN32
5478 "win32",
5479#endif
5480#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
5481 "win32unix",
5482#endif
5483#if defined(WIN64) || defined(_WIN64)
5484 "win64",
5485#endif
5486#ifdef EBCDIC
5487 "ebcdic",
5488#endif
5489#ifndef CASE_INSENSITIVE_FILENAME
5490 "fname_case",
5491#endif
5492#ifdef HAVE_ACL
5493 "acl",
5494#endif
5495#ifdef FEAT_ARABIC
5496 "arabic",
5497#endif
5498#ifdef FEAT_AUTOCMD
5499 "autocmd",
5500#endif
5501#ifdef FEAT_BEVAL
5502 "balloon_eval",
5503# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
5504 "balloon_multiline",
5505# endif
5506#endif
5507#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
5508 "builtin_terms",
5509# ifdef ALL_BUILTIN_TCAPS
5510 "all_builtin_terms",
5511# endif
5512#endif
5513#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
5514 || defined(FEAT_GUI_W32) \
5515 || defined(FEAT_GUI_MOTIF))
5516 "browsefilter",
5517#endif
5518#ifdef FEAT_BYTEOFF
5519 "byte_offset",
5520#endif
5521#ifdef FEAT_JOB_CHANNEL
5522 "channel",
5523#endif
5524#ifdef FEAT_CINDENT
5525 "cindent",
5526#endif
5527#ifdef FEAT_CLIENTSERVER
5528 "clientserver",
5529#endif
5530#ifdef FEAT_CLIPBOARD
5531 "clipboard",
5532#endif
5533#ifdef FEAT_CMDL_COMPL
5534 "cmdline_compl",
5535#endif
5536#ifdef FEAT_CMDHIST
5537 "cmdline_hist",
5538#endif
5539#ifdef FEAT_COMMENTS
5540 "comments",
5541#endif
5542#ifdef FEAT_CONCEAL
5543 "conceal",
5544#endif
5545#ifdef FEAT_CRYPT
5546 "cryptv",
5547 "crypt-blowfish",
5548 "crypt-blowfish2",
5549#endif
5550#ifdef FEAT_CSCOPE
5551 "cscope",
5552#endif
5553#ifdef FEAT_CURSORBIND
5554 "cursorbind",
5555#endif
5556#ifdef CURSOR_SHAPE
5557 "cursorshape",
5558#endif
5559#ifdef DEBUG
5560 "debug",
5561#endif
5562#ifdef FEAT_CON_DIALOG
5563 "dialog_con",
5564#endif
5565#ifdef FEAT_GUI_DIALOG
5566 "dialog_gui",
5567#endif
5568#ifdef FEAT_DIFF
5569 "diff",
5570#endif
5571#ifdef FEAT_DIGRAPHS
5572 "digraphs",
5573#endif
5574#ifdef FEAT_DIRECTX
5575 "directx",
5576#endif
5577#ifdef FEAT_DND
5578 "dnd",
5579#endif
5580#ifdef FEAT_EMACS_TAGS
5581 "emacs_tags",
5582#endif
5583 "eval", /* always present, of course! */
5584 "ex_extra", /* graduated feature */
5585#ifdef FEAT_SEARCH_EXTRA
5586 "extra_search",
5587#endif
5588#ifdef FEAT_FKMAP
5589 "farsi",
5590#endif
5591#ifdef FEAT_SEARCHPATH
5592 "file_in_path",
5593#endif
5594#ifdef FEAT_FILTERPIPE
5595 "filterpipe",
5596#endif
5597#ifdef FEAT_FIND_ID
5598 "find_in_path",
5599#endif
5600#ifdef FEAT_FLOAT
5601 "float",
5602#endif
5603#ifdef FEAT_FOLDING
5604 "folding",
5605#endif
5606#ifdef FEAT_FOOTER
5607 "footer",
5608#endif
5609#if !defined(USE_SYSTEM) && defined(UNIX)
5610 "fork",
5611#endif
5612#ifdef FEAT_GETTEXT
5613 "gettext",
5614#endif
5615#ifdef FEAT_GUI
5616 "gui",
5617#endif
5618#ifdef FEAT_GUI_ATHENA
5619# ifdef FEAT_GUI_NEXTAW
5620 "gui_neXtaw",
5621# else
5622 "gui_athena",
5623# endif
5624#endif
5625#ifdef FEAT_GUI_GTK
5626 "gui_gtk",
5627# ifdef USE_GTK3
5628 "gui_gtk3",
5629# else
5630 "gui_gtk2",
5631# endif
5632#endif
5633#ifdef FEAT_GUI_GNOME
5634 "gui_gnome",
5635#endif
5636#ifdef FEAT_GUI_MAC
5637 "gui_mac",
5638#endif
5639#ifdef FEAT_GUI_MOTIF
5640 "gui_motif",
5641#endif
5642#ifdef FEAT_GUI_PHOTON
5643 "gui_photon",
5644#endif
5645#ifdef FEAT_GUI_W32
5646 "gui_win32",
5647#endif
5648#ifdef FEAT_HANGULIN
5649 "hangul_input",
5650#endif
5651#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
5652 "iconv",
5653#endif
5654#ifdef FEAT_INS_EXPAND
5655 "insert_expand",
5656#endif
5657#ifdef FEAT_JOB_CHANNEL
5658 "job",
5659#endif
5660#ifdef FEAT_JUMPLIST
5661 "jumplist",
5662#endif
5663#ifdef FEAT_KEYMAP
5664 "keymap",
5665#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02005666 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005667#ifdef FEAT_LANGMAP
5668 "langmap",
5669#endif
5670#ifdef FEAT_LIBCALL
5671 "libcall",
5672#endif
5673#ifdef FEAT_LINEBREAK
5674 "linebreak",
5675#endif
5676#ifdef FEAT_LISP
5677 "lispindent",
5678#endif
5679#ifdef FEAT_LISTCMDS
5680 "listcmds",
5681#endif
5682#ifdef FEAT_LOCALMAP
5683 "localmap",
5684#endif
5685#ifdef FEAT_LUA
5686# ifndef DYNAMIC_LUA
5687 "lua",
5688# endif
5689#endif
5690#ifdef FEAT_MENU
5691 "menu",
5692#endif
5693#ifdef FEAT_SESSION
5694 "mksession",
5695#endif
5696#ifdef FEAT_MODIFY_FNAME
5697 "modify_fname",
5698#endif
5699#ifdef FEAT_MOUSE
5700 "mouse",
5701#endif
5702#ifdef FEAT_MOUSESHAPE
5703 "mouseshape",
5704#endif
5705#if defined(UNIX) || defined(VMS)
5706# ifdef FEAT_MOUSE_DEC
5707 "mouse_dec",
5708# endif
5709# ifdef FEAT_MOUSE_GPM
5710 "mouse_gpm",
5711# endif
5712# ifdef FEAT_MOUSE_JSB
5713 "mouse_jsbterm",
5714# endif
5715# ifdef FEAT_MOUSE_NET
5716 "mouse_netterm",
5717# endif
5718# ifdef FEAT_MOUSE_PTERM
5719 "mouse_pterm",
5720# endif
5721# ifdef FEAT_MOUSE_SGR
5722 "mouse_sgr",
5723# endif
5724# ifdef FEAT_SYSMOUSE
5725 "mouse_sysmouse",
5726# endif
5727# ifdef FEAT_MOUSE_URXVT
5728 "mouse_urxvt",
5729# endif
5730# ifdef FEAT_MOUSE_XTERM
5731 "mouse_xterm",
5732# endif
5733#endif
5734#ifdef FEAT_MBYTE
5735 "multi_byte",
5736#endif
5737#ifdef FEAT_MBYTE_IME
5738 "multi_byte_ime",
5739#endif
5740#ifdef FEAT_MULTI_LANG
5741 "multi_lang",
5742#endif
5743#ifdef FEAT_MZSCHEME
5744#ifndef DYNAMIC_MZSCHEME
5745 "mzscheme",
5746#endif
5747#endif
5748#ifdef FEAT_NUM64
5749 "num64",
5750#endif
5751#ifdef FEAT_OLE
5752 "ole",
5753#endif
5754 "packages",
5755#ifdef FEAT_PATH_EXTRA
5756 "path_extra",
5757#endif
5758#ifdef FEAT_PERL
5759#ifndef DYNAMIC_PERL
5760 "perl",
5761#endif
5762#endif
5763#ifdef FEAT_PERSISTENT_UNDO
5764 "persistent_undo",
5765#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005766#if defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005767 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005768 "pythonx",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005769#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005770#if defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005771 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005772 "pythonx",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005773#endif
5774#ifdef FEAT_POSTSCRIPT
5775 "postscript",
5776#endif
5777#ifdef FEAT_PRINTER
5778 "printer",
5779#endif
5780#ifdef FEAT_PROFILE
5781 "profile",
5782#endif
5783#ifdef FEAT_RELTIME
5784 "reltime",
5785#endif
5786#ifdef FEAT_QUICKFIX
5787 "quickfix",
5788#endif
5789#ifdef FEAT_RIGHTLEFT
5790 "rightleft",
5791#endif
5792#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
5793 "ruby",
5794#endif
5795#ifdef FEAT_SCROLLBIND
5796 "scrollbind",
5797#endif
5798#ifdef FEAT_CMDL_INFO
5799 "showcmd",
5800 "cmdline_info",
5801#endif
5802#ifdef FEAT_SIGNS
5803 "signs",
5804#endif
5805#ifdef FEAT_SMARTINDENT
5806 "smartindent",
5807#endif
5808#ifdef STARTUPTIME
5809 "startuptime",
5810#endif
5811#ifdef FEAT_STL_OPT
5812 "statusline",
5813#endif
5814#ifdef FEAT_SUN_WORKSHOP
5815 "sun_workshop",
5816#endif
5817#ifdef FEAT_NETBEANS_INTG
5818 "netbeans_intg",
5819#endif
5820#ifdef FEAT_SPELL
5821 "spell",
5822#endif
5823#ifdef FEAT_SYN_HL
5824 "syntax",
5825#endif
5826#if defined(USE_SYSTEM) || !defined(UNIX)
5827 "system",
5828#endif
5829#ifdef FEAT_TAG_BINS
5830 "tag_binary",
5831#endif
5832#ifdef FEAT_TAG_OLDSTATIC
5833 "tag_old_static",
5834#endif
5835#ifdef FEAT_TAG_ANYWHITE
5836 "tag_any_white",
5837#endif
5838#ifdef FEAT_TCL
5839# ifndef DYNAMIC_TCL
5840 "tcl",
5841# endif
5842#endif
5843#ifdef FEAT_TERMGUICOLORS
5844 "termguicolors",
5845#endif
5846#ifdef TERMINFO
5847 "terminfo",
5848#endif
5849#ifdef FEAT_TERMRESPONSE
5850 "termresponse",
5851#endif
5852#ifdef FEAT_TEXTOBJ
5853 "textobjects",
5854#endif
5855#ifdef HAVE_TGETENT
5856 "tgetent",
5857#endif
5858#ifdef FEAT_TIMERS
5859 "timers",
5860#endif
5861#ifdef FEAT_TITLE
5862 "title",
5863#endif
5864#ifdef FEAT_TOOLBAR
5865 "toolbar",
5866#endif
5867#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
5868 "unnamedplus",
5869#endif
5870#ifdef FEAT_USR_CMDS
5871 "user-commands", /* was accidentally included in 5.4 */
5872 "user_commands",
5873#endif
5874#ifdef FEAT_VIMINFO
5875 "viminfo",
5876#endif
5877#ifdef FEAT_WINDOWS
5878 "vertsplit",
5879#endif
5880#ifdef FEAT_VIRTUALEDIT
5881 "virtualedit",
5882#endif
5883 "visual",
5884#ifdef FEAT_VISUALEXTRA
5885 "visualextra",
5886#endif
5887#ifdef FEAT_VREPLACE
5888 "vreplace",
5889#endif
5890#ifdef FEAT_WILDIGN
5891 "wildignore",
5892#endif
5893#ifdef FEAT_WILDMENU
5894 "wildmenu",
5895#endif
5896#ifdef FEAT_WINDOWS
5897 "windows",
5898#endif
5899#ifdef FEAT_WAK
5900 "winaltkeys",
5901#endif
5902#ifdef FEAT_WRITEBACKUP
5903 "writebackup",
5904#endif
5905#ifdef FEAT_XIM
5906 "xim",
5907#endif
5908#ifdef FEAT_XFONTSET
5909 "xfontset",
5910#endif
5911#ifdef FEAT_XPM_W32
5912 "xpm",
5913 "xpm_w32", /* for backward compatibility */
5914#else
5915# if defined(HAVE_XPM)
5916 "xpm",
5917# endif
5918#endif
5919#ifdef USE_XSMP
5920 "xsmp",
5921#endif
5922#ifdef USE_XSMP_INTERACT
5923 "xsmp_interact",
5924#endif
5925#ifdef FEAT_XCLIPBOARD
5926 "xterm_clipboard",
5927#endif
5928#ifdef FEAT_XTERM_SAVE
5929 "xterm_save",
5930#endif
5931#if defined(UNIX) && defined(FEAT_X11)
5932 "X11",
5933#endif
5934 NULL
5935 };
5936
5937 name = get_tv_string(&argvars[0]);
5938 for (i = 0; has_list[i] != NULL; ++i)
5939 if (STRICMP(name, has_list[i]) == 0)
5940 {
5941 n = TRUE;
5942 break;
5943 }
5944
5945 if (n == FALSE)
5946 {
5947 if (STRNICMP(name, "patch", 5) == 0)
5948 {
5949 if (name[5] == '-'
5950 && STRLEN(name) >= 11
5951 && vim_isdigit(name[6])
5952 && vim_isdigit(name[8])
5953 && vim_isdigit(name[10]))
5954 {
5955 int major = atoi((char *)name + 6);
5956 int minor = atoi((char *)name + 8);
5957
5958 /* Expect "patch-9.9.01234". */
5959 n = (major < VIM_VERSION_MAJOR
5960 || (major == VIM_VERSION_MAJOR
5961 && (minor < VIM_VERSION_MINOR
5962 || (minor == VIM_VERSION_MINOR
5963 && has_patch(atoi((char *)name + 10))))));
5964 }
5965 else
5966 n = has_patch(atoi((char *)name + 5));
5967 }
5968 else if (STRICMP(name, "vim_starting") == 0)
5969 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01005970 else if (STRICMP(name, "ttyin") == 0)
5971 n = mch_input_isatty();
5972 else if (STRICMP(name, "ttyout") == 0)
5973 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005974#ifdef FEAT_MBYTE
5975 else if (STRICMP(name, "multi_byte_encoding") == 0)
5976 n = has_mbyte;
5977#endif
5978#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
5979 else if (STRICMP(name, "balloon_multiline") == 0)
5980 n = multiline_balloon_available();
5981#endif
5982#ifdef DYNAMIC_TCL
5983 else if (STRICMP(name, "tcl") == 0)
5984 n = tcl_enabled(FALSE);
5985#endif
5986#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5987 else if (STRICMP(name, "iconv") == 0)
5988 n = iconv_enabled(FALSE);
5989#endif
5990#ifdef DYNAMIC_LUA
5991 else if (STRICMP(name, "lua") == 0)
5992 n = lua_enabled(FALSE);
5993#endif
5994#ifdef DYNAMIC_MZSCHEME
5995 else if (STRICMP(name, "mzscheme") == 0)
5996 n = mzscheme_enabled(FALSE);
5997#endif
5998#ifdef DYNAMIC_RUBY
5999 else if (STRICMP(name, "ruby") == 0)
6000 n = ruby_enabled(FALSE);
6001#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006002#ifdef DYNAMIC_PYTHON
6003 else if (STRICMP(name, "python") == 0)
6004 n = python_enabled(FALSE);
6005#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006006#ifdef DYNAMIC_PYTHON3
6007 else if (STRICMP(name, "python3") == 0)
6008 n = python3_enabled(FALSE);
6009#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006010#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6011 else if (STRICMP(name, "pythonx") == 0)
6012 {
6013# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6014 if (p_pyx == 0)
6015 n = python3_enabled(FALSE) || python_enabled(FALSE);
6016 else if (p_pyx == 3)
6017 n = python3_enabled(FALSE);
6018 else if (p_pyx == 2)
6019 n = python_enabled(FALSE);
6020# elif defined(DYNAMIC_PYTHON)
6021 n = python_enabled(FALSE);
6022# elif defined(DYNAMIC_PYTHON3)
6023 n = python3_enabled(FALSE);
6024# endif
6025 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006026#endif
6027#ifdef DYNAMIC_PERL
6028 else if (STRICMP(name, "perl") == 0)
6029 n = perl_enabled(FALSE);
6030#endif
6031#ifdef FEAT_GUI
6032 else if (STRICMP(name, "gui_running") == 0)
6033 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006034# ifdef FEAT_BROWSE
6035 else if (STRICMP(name, "browse") == 0)
6036 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6037# endif
6038#endif
6039#ifdef FEAT_SYN_HL
6040 else if (STRICMP(name, "syntax_items") == 0)
6041 n = syntax_present(curwin);
6042#endif
6043#if defined(WIN3264)
6044 else if (STRICMP(name, "win95") == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006045 n = FALSE; /* Win9x is no more supported. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006046#endif
6047#ifdef FEAT_NETBEANS_INTG
6048 else if (STRICMP(name, "netbeans_enabled") == 0)
6049 n = netbeans_active();
6050#endif
6051 }
6052
6053 rettv->vval.v_number = n;
6054}
6055
6056/*
6057 * "has_key()" function
6058 */
6059 static void
6060f_has_key(typval_T *argvars, typval_T *rettv)
6061{
6062 if (argvars[0].v_type != VAR_DICT)
6063 {
6064 EMSG(_(e_dictreq));
6065 return;
6066 }
6067 if (argvars[0].vval.v_dict == NULL)
6068 return;
6069
6070 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
6071 get_tv_string(&argvars[1]), -1) != NULL;
6072}
6073
6074/*
6075 * "haslocaldir()" function
6076 */
6077 static void
6078f_haslocaldir(typval_T *argvars, typval_T *rettv)
6079{
6080 win_T *wp = NULL;
6081
6082 wp = find_tabwin(&argvars[0], &argvars[1]);
6083 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6084}
6085
6086/*
6087 * "hasmapto()" function
6088 */
6089 static void
6090f_hasmapto(typval_T *argvars, typval_T *rettv)
6091{
6092 char_u *name;
6093 char_u *mode;
6094 char_u buf[NUMBUFLEN];
6095 int abbr = FALSE;
6096
6097 name = get_tv_string(&argvars[0]);
6098 if (argvars[1].v_type == VAR_UNKNOWN)
6099 mode = (char_u *)"nvo";
6100 else
6101 {
6102 mode = get_tv_string_buf(&argvars[1], buf);
6103 if (argvars[2].v_type != VAR_UNKNOWN)
6104 abbr = (int)get_tv_number(&argvars[2]);
6105 }
6106
6107 if (map_to_exists(name, mode, abbr))
6108 rettv->vval.v_number = TRUE;
6109 else
6110 rettv->vval.v_number = FALSE;
6111}
6112
6113/*
6114 * "histadd()" function
6115 */
6116 static void
6117f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6118{
6119#ifdef FEAT_CMDHIST
6120 int histype;
6121 char_u *str;
6122 char_u buf[NUMBUFLEN];
6123#endif
6124
6125 rettv->vval.v_number = FALSE;
6126 if (check_restricted() || check_secure())
6127 return;
6128#ifdef FEAT_CMDHIST
6129 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6130 histype = str != NULL ? get_histtype(str) : -1;
6131 if (histype >= 0)
6132 {
6133 str = get_tv_string_buf(&argvars[1], buf);
6134 if (*str != NUL)
6135 {
6136 init_history();
6137 add_to_history(histype, str, FALSE, NUL);
6138 rettv->vval.v_number = TRUE;
6139 return;
6140 }
6141 }
6142#endif
6143}
6144
6145/*
6146 * "histdel()" function
6147 */
6148 static void
6149f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6150{
6151#ifdef FEAT_CMDHIST
6152 int n;
6153 char_u buf[NUMBUFLEN];
6154 char_u *str;
6155
6156 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6157 if (str == NULL)
6158 n = 0;
6159 else if (argvars[1].v_type == VAR_UNKNOWN)
6160 /* only one argument: clear entire history */
6161 n = clr_history(get_histtype(str));
6162 else if (argvars[1].v_type == VAR_NUMBER)
6163 /* index given: remove that entry */
6164 n = del_history_idx(get_histtype(str),
6165 (int)get_tv_number(&argvars[1]));
6166 else
6167 /* string given: remove all matching entries */
6168 n = del_history_entry(get_histtype(str),
6169 get_tv_string_buf(&argvars[1], buf));
6170 rettv->vval.v_number = n;
6171#endif
6172}
6173
6174/*
6175 * "histget()" function
6176 */
6177 static void
6178f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6179{
6180#ifdef FEAT_CMDHIST
6181 int type;
6182 int idx;
6183 char_u *str;
6184
6185 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6186 if (str == NULL)
6187 rettv->vval.v_string = NULL;
6188 else
6189 {
6190 type = get_histtype(str);
6191 if (argvars[1].v_type == VAR_UNKNOWN)
6192 idx = get_history_idx(type);
6193 else
6194 idx = (int)get_tv_number_chk(&argvars[1], NULL);
6195 /* -1 on type error */
6196 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6197 }
6198#else
6199 rettv->vval.v_string = NULL;
6200#endif
6201 rettv->v_type = VAR_STRING;
6202}
6203
6204/*
6205 * "histnr()" function
6206 */
6207 static void
6208f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6209{
6210 int i;
6211
6212#ifdef FEAT_CMDHIST
6213 char_u *history = get_tv_string_chk(&argvars[0]);
6214
6215 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6216 if (i >= HIST_CMD && i < HIST_COUNT)
6217 i = get_history_idx(i);
6218 else
6219#endif
6220 i = -1;
6221 rettv->vval.v_number = i;
6222}
6223
6224/*
6225 * "highlightID(name)" function
6226 */
6227 static void
6228f_hlID(typval_T *argvars, typval_T *rettv)
6229{
6230 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
6231}
6232
6233/*
6234 * "highlight_exists()" function
6235 */
6236 static void
6237f_hlexists(typval_T *argvars, typval_T *rettv)
6238{
6239 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
6240}
6241
6242/*
6243 * "hostname()" function
6244 */
6245 static void
6246f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6247{
6248 char_u hostname[256];
6249
6250 mch_get_host_name(hostname, 256);
6251 rettv->v_type = VAR_STRING;
6252 rettv->vval.v_string = vim_strsave(hostname);
6253}
6254
6255/*
6256 * iconv() function
6257 */
6258 static void
6259f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6260{
6261#ifdef FEAT_MBYTE
6262 char_u buf1[NUMBUFLEN];
6263 char_u buf2[NUMBUFLEN];
6264 char_u *from, *to, *str;
6265 vimconv_T vimconv;
6266#endif
6267
6268 rettv->v_type = VAR_STRING;
6269 rettv->vval.v_string = NULL;
6270
6271#ifdef FEAT_MBYTE
6272 str = get_tv_string(&argvars[0]);
6273 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
6274 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
6275 vimconv.vc_type = CONV_NONE;
6276 convert_setup(&vimconv, from, to);
6277
6278 /* If the encodings are equal, no conversion needed. */
6279 if (vimconv.vc_type == CONV_NONE)
6280 rettv->vval.v_string = vim_strsave(str);
6281 else
6282 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6283
6284 convert_setup(&vimconv, NULL, NULL);
6285 vim_free(from);
6286 vim_free(to);
6287#endif
6288}
6289
6290/*
6291 * "indent()" function
6292 */
6293 static void
6294f_indent(typval_T *argvars, typval_T *rettv)
6295{
6296 linenr_T lnum;
6297
6298 lnum = get_tv_lnum(argvars);
6299 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6300 rettv->vval.v_number = get_indent_lnum(lnum);
6301 else
6302 rettv->vval.v_number = -1;
6303}
6304
6305/*
6306 * "index()" function
6307 */
6308 static void
6309f_index(typval_T *argvars, typval_T *rettv)
6310{
6311 list_T *l;
6312 listitem_T *item;
6313 long idx = 0;
6314 int ic = FALSE;
6315
6316 rettv->vval.v_number = -1;
6317 if (argvars[0].v_type != VAR_LIST)
6318 {
6319 EMSG(_(e_listreq));
6320 return;
6321 }
6322 l = argvars[0].vval.v_list;
6323 if (l != NULL)
6324 {
6325 item = l->lv_first;
6326 if (argvars[2].v_type != VAR_UNKNOWN)
6327 {
6328 int error = FALSE;
6329
6330 /* Start at specified item. Use the cached index that list_find()
6331 * sets, so that a negative number also works. */
6332 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
6333 idx = l->lv_idx;
6334 if (argvars[3].v_type != VAR_UNKNOWN)
6335 ic = (int)get_tv_number_chk(&argvars[3], &error);
6336 if (error)
6337 item = NULL;
6338 }
6339
6340 for ( ; item != NULL; item = item->li_next, ++idx)
6341 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6342 {
6343 rettv->vval.v_number = idx;
6344 break;
6345 }
6346 }
6347}
6348
6349static int inputsecret_flag = 0;
6350
6351/*
6352 * "input()" function
6353 * Also handles inputsecret() when inputsecret is set.
6354 */
6355 static void
6356f_input(typval_T *argvars, typval_T *rettv)
6357{
6358 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6359}
6360
6361/*
6362 * "inputdialog()" function
6363 */
6364 static void
6365f_inputdialog(typval_T *argvars, typval_T *rettv)
6366{
6367#if defined(FEAT_GUI_TEXTDIALOG)
6368 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
6369 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6370 {
6371 char_u *message;
6372 char_u buf[NUMBUFLEN];
6373 char_u *defstr = (char_u *)"";
6374
6375 message = get_tv_string_chk(&argvars[0]);
6376 if (argvars[1].v_type != VAR_UNKNOWN
6377 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
6378 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6379 else
6380 IObuff[0] = NUL;
6381 if (message != NULL && defstr != NULL
6382 && do_dialog(VIM_QUESTION, NULL, message,
6383 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6384 rettv->vval.v_string = vim_strsave(IObuff);
6385 else
6386 {
6387 if (message != NULL && defstr != NULL
6388 && argvars[1].v_type != VAR_UNKNOWN
6389 && argvars[2].v_type != VAR_UNKNOWN)
6390 rettv->vval.v_string = vim_strsave(
6391 get_tv_string_buf(&argvars[2], buf));
6392 else
6393 rettv->vval.v_string = NULL;
6394 }
6395 rettv->v_type = VAR_STRING;
6396 }
6397 else
6398#endif
6399 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6400}
6401
6402/*
6403 * "inputlist()" function
6404 */
6405 static void
6406f_inputlist(typval_T *argvars, typval_T *rettv)
6407{
6408 listitem_T *li;
6409 int selected;
6410 int mouse_used;
6411
6412#ifdef NO_CONSOLE_INPUT
6413 /* While starting up, there is no place to enter text. */
6414 if (no_console_input())
6415 return;
6416#endif
6417 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6418 {
6419 EMSG2(_(e_listarg), "inputlist()");
6420 return;
6421 }
6422
6423 msg_start();
6424 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
6425 lines_left = Rows; /* avoid more prompt */
6426 msg_scroll = TRUE;
6427 msg_clr_eos();
6428
6429 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
6430 {
6431 msg_puts(get_tv_string(&li->li_tv));
6432 msg_putchar('\n');
6433 }
6434
6435 /* Ask for choice. */
6436 selected = prompt_for_number(&mouse_used);
6437 if (mouse_used)
6438 selected -= lines_left;
6439
6440 rettv->vval.v_number = selected;
6441}
6442
6443
6444static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6445
6446/*
6447 * "inputrestore()" function
6448 */
6449 static void
6450f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6451{
6452 if (ga_userinput.ga_len > 0)
6453 {
6454 --ga_userinput.ga_len;
6455 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
6456 + ga_userinput.ga_len);
6457 /* default return is zero == OK */
6458 }
6459 else if (p_verbose > 1)
6460 {
6461 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
6462 rettv->vval.v_number = 1; /* Failed */
6463 }
6464}
6465
6466/*
6467 * "inputsave()" function
6468 */
6469 static void
6470f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
6471{
6472 /* Add an entry to the stack of typeahead storage. */
6473 if (ga_grow(&ga_userinput, 1) == OK)
6474 {
6475 save_typeahead((tasave_T *)(ga_userinput.ga_data)
6476 + ga_userinput.ga_len);
6477 ++ga_userinput.ga_len;
6478 /* default return is zero == OK */
6479 }
6480 else
6481 rettv->vval.v_number = 1; /* Failed */
6482}
6483
6484/*
6485 * "inputsecret()" function
6486 */
6487 static void
6488f_inputsecret(typval_T *argvars, typval_T *rettv)
6489{
6490 ++cmdline_star;
6491 ++inputsecret_flag;
6492 f_input(argvars, rettv);
6493 --cmdline_star;
6494 --inputsecret_flag;
6495}
6496
6497/*
6498 * "insert()" function
6499 */
6500 static void
6501f_insert(typval_T *argvars, typval_T *rettv)
6502{
6503 long before = 0;
6504 listitem_T *item;
6505 list_T *l;
6506 int error = FALSE;
6507
6508 if (argvars[0].v_type != VAR_LIST)
6509 EMSG2(_(e_listarg), "insert()");
6510 else if ((l = argvars[0].vval.v_list) != NULL
6511 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
6512 {
6513 if (argvars[2].v_type != VAR_UNKNOWN)
6514 before = (long)get_tv_number_chk(&argvars[2], &error);
6515 if (error)
6516 return; /* type error; errmsg already given */
6517
6518 if (before == l->lv_len)
6519 item = NULL;
6520 else
6521 {
6522 item = list_find(l, before);
6523 if (item == NULL)
6524 {
6525 EMSGN(_(e_listidx), before);
6526 l = NULL;
6527 }
6528 }
6529 if (l != NULL)
6530 {
6531 list_insert_tv(l, &argvars[1], item);
6532 copy_tv(&argvars[0], rettv);
6533 }
6534 }
6535}
6536
6537/*
6538 * "invert(expr)" function
6539 */
6540 static void
6541f_invert(typval_T *argvars, typval_T *rettv)
6542{
6543 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
6544}
6545
6546/*
6547 * "isdirectory()" function
6548 */
6549 static void
6550f_isdirectory(typval_T *argvars, typval_T *rettv)
6551{
6552 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
6553}
6554
6555/*
6556 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
6557 * or it refers to a List or Dictionary that is locked.
6558 */
6559 static int
6560tv_islocked(typval_T *tv)
6561{
6562 return (tv->v_lock & VAR_LOCKED)
6563 || (tv->v_type == VAR_LIST
6564 && tv->vval.v_list != NULL
6565 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
6566 || (tv->v_type == VAR_DICT
6567 && tv->vval.v_dict != NULL
6568 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
6569}
6570
6571/*
6572 * "islocked()" function
6573 */
6574 static void
6575f_islocked(typval_T *argvars, typval_T *rettv)
6576{
6577 lval_T lv;
6578 char_u *end;
6579 dictitem_T *di;
6580
6581 rettv->vval.v_number = -1;
6582 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01006583 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006584 if (end != NULL && lv.ll_name != NULL)
6585 {
6586 if (*end != NUL)
6587 EMSG(_(e_trailing));
6588 else
6589 {
6590 if (lv.ll_tv == NULL)
6591 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006592 di = find_var(lv.ll_name, NULL, TRUE);
6593 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006594 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006595 /* Consider a variable locked when:
6596 * 1. the variable itself is locked
6597 * 2. the value of the variable is locked.
6598 * 3. the List or Dict value is locked.
6599 */
6600 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
6601 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006602 }
6603 }
6604 else if (lv.ll_range)
6605 EMSG(_("E786: Range not allowed"));
6606 else if (lv.ll_newkey != NULL)
6607 EMSG2(_(e_dictkey), lv.ll_newkey);
6608 else if (lv.ll_list != NULL)
6609 /* List item. */
6610 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
6611 else
6612 /* Dictionary item. */
6613 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
6614 }
6615 }
6616
6617 clear_lval(&lv);
6618}
6619
6620#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
6621/*
6622 * "isnan()" function
6623 */
6624 static void
6625f_isnan(typval_T *argvars, typval_T *rettv)
6626{
6627 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
6628 && isnan(argvars[0].vval.v_float);
6629}
6630#endif
6631
6632/*
6633 * "items(dict)" function
6634 */
6635 static void
6636f_items(typval_T *argvars, typval_T *rettv)
6637{
6638 dict_list(argvars, rettv, 2);
6639}
6640
6641#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
6642/*
6643 * Get the job from the argument.
6644 * Returns NULL if the job is invalid.
6645 */
6646 static job_T *
6647get_job_arg(typval_T *tv)
6648{
6649 job_T *job;
6650
6651 if (tv->v_type != VAR_JOB)
6652 {
6653 EMSG2(_(e_invarg2), get_tv_string(tv));
6654 return NULL;
6655 }
6656 job = tv->vval.v_job;
6657
6658 if (job == NULL)
6659 EMSG(_("E916: not a valid job"));
6660 return job;
6661}
6662
6663/*
6664 * "job_getchannel()" function
6665 */
6666 static void
6667f_job_getchannel(typval_T *argvars, typval_T *rettv)
6668{
6669 job_T *job = get_job_arg(&argvars[0]);
6670
6671 if (job != NULL)
6672 {
6673 rettv->v_type = VAR_CHANNEL;
6674 rettv->vval.v_channel = job->jv_channel;
6675 if (job->jv_channel != NULL)
6676 ++job->jv_channel->ch_refcount;
6677 }
6678}
6679
6680/*
6681 * "job_info()" function
6682 */
6683 static void
6684f_job_info(typval_T *argvars, typval_T *rettv)
6685{
6686 job_T *job = get_job_arg(&argvars[0]);
6687
6688 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
6689 job_info(job, rettv->vval.v_dict);
6690}
6691
6692/*
6693 * "job_setoptions()" function
6694 */
6695 static void
6696f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
6697{
6698 job_T *job = get_job_arg(&argvars[0]);
6699 jobopt_T opt;
6700
6701 if (job == NULL)
6702 return;
6703 clear_job_options(&opt);
6704 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == OK)
6705 job_set_options(job, &opt);
6706 free_job_options(&opt);
6707}
6708
6709/*
6710 * "job_start()" function
6711 */
6712 static void
6713f_job_start(typval_T *argvars, typval_T *rettv)
6714{
6715 rettv->v_type = VAR_JOB;
6716 if (check_restricted() || check_secure())
6717 return;
6718 rettv->vval.v_job = job_start(argvars);
6719}
6720
6721/*
6722 * "job_status()" function
6723 */
6724 static void
6725f_job_status(typval_T *argvars, typval_T *rettv)
6726{
6727 job_T *job = get_job_arg(&argvars[0]);
6728
6729 if (job != NULL)
6730 {
6731 rettv->v_type = VAR_STRING;
6732 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
6733 }
6734}
6735
6736/*
6737 * "job_stop()" function
6738 */
6739 static void
6740f_job_stop(typval_T *argvars, typval_T *rettv)
6741{
6742 job_T *job = get_job_arg(&argvars[0]);
6743
6744 if (job != NULL)
6745 rettv->vval.v_number = job_stop(job, argvars);
6746}
6747#endif
6748
6749/*
6750 * "join()" function
6751 */
6752 static void
6753f_join(typval_T *argvars, typval_T *rettv)
6754{
6755 garray_T ga;
6756 char_u *sep;
6757
6758 if (argvars[0].v_type != VAR_LIST)
6759 {
6760 EMSG(_(e_listreq));
6761 return;
6762 }
6763 if (argvars[0].vval.v_list == NULL)
6764 return;
6765 if (argvars[1].v_type == VAR_UNKNOWN)
6766 sep = (char_u *)" ";
6767 else
6768 sep = get_tv_string_chk(&argvars[1]);
6769
6770 rettv->v_type = VAR_STRING;
6771
6772 if (sep != NULL)
6773 {
6774 ga_init2(&ga, (int)sizeof(char), 80);
6775 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
6776 ga_append(&ga, NUL);
6777 rettv->vval.v_string = (char_u *)ga.ga_data;
6778 }
6779 else
6780 rettv->vval.v_string = NULL;
6781}
6782
6783/*
6784 * "js_decode()" function
6785 */
6786 static void
6787f_js_decode(typval_T *argvars, typval_T *rettv)
6788{
6789 js_read_T reader;
6790
6791 reader.js_buf = get_tv_string(&argvars[0]);
6792 reader.js_fill = NULL;
6793 reader.js_used = 0;
6794 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
6795 EMSG(_(e_invarg));
6796}
6797
6798/*
6799 * "js_encode()" function
6800 */
6801 static void
6802f_js_encode(typval_T *argvars, typval_T *rettv)
6803{
6804 rettv->v_type = VAR_STRING;
6805 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
6806}
6807
6808/*
6809 * "json_decode()" function
6810 */
6811 static void
6812f_json_decode(typval_T *argvars, typval_T *rettv)
6813{
6814 js_read_T reader;
6815
6816 reader.js_buf = get_tv_string(&argvars[0]);
6817 reader.js_fill = NULL;
6818 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01006819 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006820}
6821
6822/*
6823 * "json_encode()" function
6824 */
6825 static void
6826f_json_encode(typval_T *argvars, typval_T *rettv)
6827{
6828 rettv->v_type = VAR_STRING;
6829 rettv->vval.v_string = json_encode(&argvars[0], 0);
6830}
6831
6832/*
6833 * "keys()" function
6834 */
6835 static void
6836f_keys(typval_T *argvars, typval_T *rettv)
6837{
6838 dict_list(argvars, rettv, 0);
6839}
6840
6841/*
6842 * "last_buffer_nr()" function.
6843 */
6844 static void
6845f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
6846{
6847 int n = 0;
6848 buf_T *buf;
6849
Bram Moolenaar29323592016-07-24 22:04:11 +02006850 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006851 if (n < buf->b_fnum)
6852 n = buf->b_fnum;
6853
6854 rettv->vval.v_number = n;
6855}
6856
6857/*
6858 * "len()" function
6859 */
6860 static void
6861f_len(typval_T *argvars, typval_T *rettv)
6862{
6863 switch (argvars[0].v_type)
6864 {
6865 case VAR_STRING:
6866 case VAR_NUMBER:
6867 rettv->vval.v_number = (varnumber_T)STRLEN(
6868 get_tv_string(&argvars[0]));
6869 break;
6870 case VAR_LIST:
6871 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
6872 break;
6873 case VAR_DICT:
6874 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
6875 break;
6876 case VAR_UNKNOWN:
6877 case VAR_SPECIAL:
6878 case VAR_FLOAT:
6879 case VAR_FUNC:
6880 case VAR_PARTIAL:
6881 case VAR_JOB:
6882 case VAR_CHANNEL:
6883 EMSG(_("E701: Invalid type for len()"));
6884 break;
6885 }
6886}
6887
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006888 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01006889libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006890{
6891#ifdef FEAT_LIBCALL
6892 char_u *string_in;
6893 char_u **string_result;
6894 int nr_result;
6895#endif
6896
6897 rettv->v_type = type;
6898 if (type != VAR_NUMBER)
6899 rettv->vval.v_string = NULL;
6900
6901 if (check_restricted() || check_secure())
6902 return;
6903
6904#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02006905 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006906 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
6907 {
6908 string_in = NULL;
6909 if (argvars[2].v_type == VAR_STRING)
6910 string_in = argvars[2].vval.v_string;
6911 if (type == VAR_NUMBER)
6912 string_result = NULL;
6913 else
6914 string_result = &rettv->vval.v_string;
6915 if (mch_libcall(argvars[0].vval.v_string,
6916 argvars[1].vval.v_string,
6917 string_in,
6918 argvars[2].vval.v_number,
6919 string_result,
6920 &nr_result) == OK
6921 && type == VAR_NUMBER)
6922 rettv->vval.v_number = nr_result;
6923 }
6924#endif
6925}
6926
6927/*
6928 * "libcall()" function
6929 */
6930 static void
6931f_libcall(typval_T *argvars, typval_T *rettv)
6932{
6933 libcall_common(argvars, rettv, VAR_STRING);
6934}
6935
6936/*
6937 * "libcallnr()" function
6938 */
6939 static void
6940f_libcallnr(typval_T *argvars, typval_T *rettv)
6941{
6942 libcall_common(argvars, rettv, VAR_NUMBER);
6943}
6944
6945/*
6946 * "line(string)" function
6947 */
6948 static void
6949f_line(typval_T *argvars, typval_T *rettv)
6950{
6951 linenr_T lnum = 0;
6952 pos_T *fp;
6953 int fnum;
6954
6955 fp = var2fpos(&argvars[0], TRUE, &fnum);
6956 if (fp != NULL)
6957 lnum = fp->lnum;
6958 rettv->vval.v_number = lnum;
6959}
6960
6961/*
6962 * "line2byte(lnum)" function
6963 */
6964 static void
6965f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
6966{
6967#ifndef FEAT_BYTEOFF
6968 rettv->vval.v_number = -1;
6969#else
6970 linenr_T lnum;
6971
6972 lnum = get_tv_lnum(argvars);
6973 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
6974 rettv->vval.v_number = -1;
6975 else
6976 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
6977 if (rettv->vval.v_number >= 0)
6978 ++rettv->vval.v_number;
6979#endif
6980}
6981
6982/*
6983 * "lispindent(lnum)" function
6984 */
6985 static void
6986f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
6987{
6988#ifdef FEAT_LISP
6989 pos_T pos;
6990 linenr_T lnum;
6991
6992 pos = curwin->w_cursor;
6993 lnum = get_tv_lnum(argvars);
6994 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6995 {
6996 curwin->w_cursor.lnum = lnum;
6997 rettv->vval.v_number = get_lisp_indent();
6998 curwin->w_cursor = pos;
6999 }
7000 else
7001#endif
7002 rettv->vval.v_number = -1;
7003}
7004
7005/*
7006 * "localtime()" function
7007 */
7008 static void
7009f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7010{
7011 rettv->vval.v_number = (varnumber_T)time(NULL);
7012}
7013
7014static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
7015
7016 static void
7017get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7018{
7019 char_u *keys;
7020 char_u *which;
7021 char_u buf[NUMBUFLEN];
7022 char_u *keys_buf = NULL;
7023 char_u *rhs;
7024 int mode;
7025 int abbr = FALSE;
7026 int get_dict = FALSE;
7027 mapblock_T *mp;
7028 int buffer_local;
7029
7030 /* return empty string for failure */
7031 rettv->v_type = VAR_STRING;
7032 rettv->vval.v_string = NULL;
7033
7034 keys = get_tv_string(&argvars[0]);
7035 if (*keys == NUL)
7036 return;
7037
7038 if (argvars[1].v_type != VAR_UNKNOWN)
7039 {
7040 which = get_tv_string_buf_chk(&argvars[1], buf);
7041 if (argvars[2].v_type != VAR_UNKNOWN)
7042 {
7043 abbr = (int)get_tv_number(&argvars[2]);
7044 if (argvars[3].v_type != VAR_UNKNOWN)
7045 get_dict = (int)get_tv_number(&argvars[3]);
7046 }
7047 }
7048 else
7049 which = (char_u *)"";
7050 if (which == NULL)
7051 return;
7052
7053 mode = get_map_mode(&which, 0);
7054
7055 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7056 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7057 vim_free(keys_buf);
7058
7059 if (!get_dict)
7060 {
7061 /* Return a string. */
7062 if (rhs != NULL)
7063 rettv->vval.v_string = str2special_save(rhs, FALSE);
7064
7065 }
7066 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7067 {
7068 /* Return a dictionary. */
7069 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7070 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7071 dict_T *dict = rettv->vval.v_dict;
7072
7073 dict_add_nr_str(dict, "lhs", 0L, lhs);
7074 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
7075 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
7076 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
7077 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
7078 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
7079 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
7080 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
7081 dict_add_nr_str(dict, "mode", 0L, mapmode);
7082
7083 vim_free(lhs);
7084 vim_free(mapmode);
7085 }
7086}
7087
7088#ifdef FEAT_FLOAT
7089/*
7090 * "log()" function
7091 */
7092 static void
7093f_log(typval_T *argvars, typval_T *rettv)
7094{
7095 float_T f = 0.0;
7096
7097 rettv->v_type = VAR_FLOAT;
7098 if (get_float_arg(argvars, &f) == OK)
7099 rettv->vval.v_float = log(f);
7100 else
7101 rettv->vval.v_float = 0.0;
7102}
7103
7104/*
7105 * "log10()" function
7106 */
7107 static void
7108f_log10(typval_T *argvars, typval_T *rettv)
7109{
7110 float_T f = 0.0;
7111
7112 rettv->v_type = VAR_FLOAT;
7113 if (get_float_arg(argvars, &f) == OK)
7114 rettv->vval.v_float = log10(f);
7115 else
7116 rettv->vval.v_float = 0.0;
7117}
7118#endif
7119
7120#ifdef FEAT_LUA
7121/*
7122 * "luaeval()" function
7123 */
7124 static void
7125f_luaeval(typval_T *argvars, typval_T *rettv)
7126{
7127 char_u *str;
7128 char_u buf[NUMBUFLEN];
7129
7130 str = get_tv_string_buf(&argvars[0], buf);
7131 do_luaeval(str, argvars + 1, rettv);
7132}
7133#endif
7134
7135/*
7136 * "map()" function
7137 */
7138 static void
7139f_map(typval_T *argvars, typval_T *rettv)
7140{
7141 filter_map(argvars, rettv, TRUE);
7142}
7143
7144/*
7145 * "maparg()" function
7146 */
7147 static void
7148f_maparg(typval_T *argvars, typval_T *rettv)
7149{
7150 get_maparg(argvars, rettv, TRUE);
7151}
7152
7153/*
7154 * "mapcheck()" function
7155 */
7156 static void
7157f_mapcheck(typval_T *argvars, typval_T *rettv)
7158{
7159 get_maparg(argvars, rettv, FALSE);
7160}
7161
7162static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
7163
7164 static void
7165find_some_match(typval_T *argvars, typval_T *rettv, int type)
7166{
7167 char_u *str = NULL;
7168 long len = 0;
7169 char_u *expr = NULL;
7170 char_u *pat;
7171 regmatch_T regmatch;
7172 char_u patbuf[NUMBUFLEN];
7173 char_u strbuf[NUMBUFLEN];
7174 char_u *save_cpo;
7175 long start = 0;
7176 long nth = 1;
7177 colnr_T startcol = 0;
7178 int match = 0;
7179 list_T *l = NULL;
7180 listitem_T *li = NULL;
7181 long idx = 0;
7182 char_u *tofree = NULL;
7183
7184 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7185 save_cpo = p_cpo;
7186 p_cpo = (char_u *)"";
7187
7188 rettv->vval.v_number = -1;
7189 if (type == 3 || type == 4)
7190 {
7191 /* type 3: return empty list when there are no matches.
7192 * type 4: return ["", -1, -1, -1] */
7193 if (rettv_list_alloc(rettv) == FAIL)
7194 goto theend;
7195 if (type == 4
7196 && (list_append_string(rettv->vval.v_list,
7197 (char_u *)"", 0) == FAIL
7198 || list_append_number(rettv->vval.v_list,
7199 (varnumber_T)-1) == FAIL
7200 || list_append_number(rettv->vval.v_list,
7201 (varnumber_T)-1) == FAIL
7202 || list_append_number(rettv->vval.v_list,
7203 (varnumber_T)-1) == FAIL))
7204 {
7205 list_free(rettv->vval.v_list);
7206 rettv->vval.v_list = NULL;
7207 goto theend;
7208 }
7209 }
7210 else if (type == 2)
7211 {
7212 rettv->v_type = VAR_STRING;
7213 rettv->vval.v_string = NULL;
7214 }
7215
7216 if (argvars[0].v_type == VAR_LIST)
7217 {
7218 if ((l = argvars[0].vval.v_list) == NULL)
7219 goto theend;
7220 li = l->lv_first;
7221 }
7222 else
7223 {
7224 expr = str = get_tv_string(&argvars[0]);
7225 len = (long)STRLEN(str);
7226 }
7227
7228 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
7229 if (pat == NULL)
7230 goto theend;
7231
7232 if (argvars[2].v_type != VAR_UNKNOWN)
7233 {
7234 int error = FALSE;
7235
7236 start = (long)get_tv_number_chk(&argvars[2], &error);
7237 if (error)
7238 goto theend;
7239 if (l != NULL)
7240 {
7241 li = list_find(l, start);
7242 if (li == NULL)
7243 goto theend;
7244 idx = l->lv_idx; /* use the cached index */
7245 }
7246 else
7247 {
7248 if (start < 0)
7249 start = 0;
7250 if (start > len)
7251 goto theend;
7252 /* When "count" argument is there ignore matches before "start",
7253 * otherwise skip part of the string. Differs when pattern is "^"
7254 * or "\<". */
7255 if (argvars[3].v_type != VAR_UNKNOWN)
7256 startcol = start;
7257 else
7258 {
7259 str += start;
7260 len -= start;
7261 }
7262 }
7263
7264 if (argvars[3].v_type != VAR_UNKNOWN)
7265 nth = (long)get_tv_number_chk(&argvars[3], &error);
7266 if (error)
7267 goto theend;
7268 }
7269
7270 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7271 if (regmatch.regprog != NULL)
7272 {
7273 regmatch.rm_ic = p_ic;
7274
7275 for (;;)
7276 {
7277 if (l != NULL)
7278 {
7279 if (li == NULL)
7280 {
7281 match = FALSE;
7282 break;
7283 }
7284 vim_free(tofree);
7285 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7286 if (str == NULL)
7287 break;
7288 }
7289
7290 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7291
7292 if (match && --nth <= 0)
7293 break;
7294 if (l == NULL && !match)
7295 break;
7296
7297 /* Advance to just after the match. */
7298 if (l != NULL)
7299 {
7300 li = li->li_next;
7301 ++idx;
7302 }
7303 else
7304 {
7305#ifdef FEAT_MBYTE
7306 startcol = (colnr_T)(regmatch.startp[0]
7307 + (*mb_ptr2len)(regmatch.startp[0]) - str);
7308#else
7309 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
7310#endif
7311 if (startcol > (colnr_T)len
7312 || str + startcol <= regmatch.startp[0])
7313 {
7314 match = FALSE;
7315 break;
7316 }
7317 }
7318 }
7319
7320 if (match)
7321 {
7322 if (type == 4)
7323 {
7324 listitem_T *li1 = rettv->vval.v_list->lv_first;
7325 listitem_T *li2 = li1->li_next;
7326 listitem_T *li3 = li2->li_next;
7327 listitem_T *li4 = li3->li_next;
7328
7329 vim_free(li1->li_tv.vval.v_string);
7330 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7331 (int)(regmatch.endp[0] - regmatch.startp[0]));
7332 li3->li_tv.vval.v_number =
7333 (varnumber_T)(regmatch.startp[0] - expr);
7334 li4->li_tv.vval.v_number =
7335 (varnumber_T)(regmatch.endp[0] - expr);
7336 if (l != NULL)
7337 li2->li_tv.vval.v_number = (varnumber_T)idx;
7338 }
7339 else if (type == 3)
7340 {
7341 int i;
7342
7343 /* return list with matched string and submatches */
7344 for (i = 0; i < NSUBEXP; ++i)
7345 {
7346 if (regmatch.endp[i] == NULL)
7347 {
7348 if (list_append_string(rettv->vval.v_list,
7349 (char_u *)"", 0) == FAIL)
7350 break;
7351 }
7352 else if (list_append_string(rettv->vval.v_list,
7353 regmatch.startp[i],
7354 (int)(regmatch.endp[i] - regmatch.startp[i]))
7355 == FAIL)
7356 break;
7357 }
7358 }
7359 else if (type == 2)
7360 {
7361 /* return matched string */
7362 if (l != NULL)
7363 copy_tv(&li->li_tv, rettv);
7364 else
7365 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
7366 (int)(regmatch.endp[0] - regmatch.startp[0]));
7367 }
7368 else if (l != NULL)
7369 rettv->vval.v_number = idx;
7370 else
7371 {
7372 if (type != 0)
7373 rettv->vval.v_number =
7374 (varnumber_T)(regmatch.startp[0] - str);
7375 else
7376 rettv->vval.v_number =
7377 (varnumber_T)(regmatch.endp[0] - str);
7378 rettv->vval.v_number += (varnumber_T)(str - expr);
7379 }
7380 }
7381 vim_regfree(regmatch.regprog);
7382 }
7383
7384 if (type == 4 && l == NULL)
7385 /* matchstrpos() without a list: drop the second item. */
7386 listitem_remove(rettv->vval.v_list,
7387 rettv->vval.v_list->lv_first->li_next);
7388
7389theend:
7390 vim_free(tofree);
7391 p_cpo = save_cpo;
7392}
7393
7394/*
7395 * "match()" function
7396 */
7397 static void
7398f_match(typval_T *argvars, typval_T *rettv)
7399{
7400 find_some_match(argvars, rettv, 1);
7401}
7402
7403/*
7404 * "matchadd()" function
7405 */
7406 static void
7407f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7408{
7409#ifdef FEAT_SEARCH_EXTRA
7410 char_u buf[NUMBUFLEN];
7411 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
7412 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
7413 int prio = 10; /* default priority */
7414 int id = -1;
7415 int error = FALSE;
7416 char_u *conceal_char = NULL;
7417
7418 rettv->vval.v_number = -1;
7419
7420 if (grp == NULL || pat == NULL)
7421 return;
7422 if (argvars[2].v_type != VAR_UNKNOWN)
7423 {
7424 prio = (int)get_tv_number_chk(&argvars[2], &error);
7425 if (argvars[3].v_type != VAR_UNKNOWN)
7426 {
7427 id = (int)get_tv_number_chk(&argvars[3], &error);
7428 if (argvars[4].v_type != VAR_UNKNOWN)
7429 {
7430 if (argvars[4].v_type != VAR_DICT)
7431 {
7432 EMSG(_(e_dictreq));
7433 return;
7434 }
7435 if (dict_find(argvars[4].vval.v_dict,
7436 (char_u *)"conceal", -1) != NULL)
7437 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7438 (char_u *)"conceal", FALSE);
7439 }
7440 }
7441 }
7442 if (error == TRUE)
7443 return;
7444 if (id >= 1 && id <= 3)
7445 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007446 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007447 return;
7448 }
7449
7450 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
7451 conceal_char);
7452#endif
7453}
7454
7455/*
7456 * "matchaddpos()" function
7457 */
7458 static void
7459f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7460{
7461#ifdef FEAT_SEARCH_EXTRA
7462 char_u buf[NUMBUFLEN];
7463 char_u *group;
7464 int prio = 10;
7465 int id = -1;
7466 int error = FALSE;
7467 list_T *l;
7468 char_u *conceal_char = NULL;
7469
7470 rettv->vval.v_number = -1;
7471
7472 group = get_tv_string_buf_chk(&argvars[0], buf);
7473 if (group == NULL)
7474 return;
7475
7476 if (argvars[1].v_type != VAR_LIST)
7477 {
7478 EMSG2(_(e_listarg), "matchaddpos()");
7479 return;
7480 }
7481 l = argvars[1].vval.v_list;
7482 if (l == NULL)
7483 return;
7484
7485 if (argvars[2].v_type != VAR_UNKNOWN)
7486 {
7487 prio = (int)get_tv_number_chk(&argvars[2], &error);
7488 if (argvars[3].v_type != VAR_UNKNOWN)
7489 {
7490 id = (int)get_tv_number_chk(&argvars[3], &error);
7491 if (argvars[4].v_type != VAR_UNKNOWN)
7492 {
7493 if (argvars[4].v_type != VAR_DICT)
7494 {
7495 EMSG(_(e_dictreq));
7496 return;
7497 }
7498 if (dict_find(argvars[4].vval.v_dict,
7499 (char_u *)"conceal", -1) != NULL)
7500 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7501 (char_u *)"conceal", FALSE);
7502 }
7503 }
7504 }
7505 if (error == TRUE)
7506 return;
7507
7508 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
7509 if (id == 1 || id == 2)
7510 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007511 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007512 return;
7513 }
7514
7515 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
7516 conceal_char);
7517#endif
7518}
7519
7520/*
7521 * "matcharg()" function
7522 */
7523 static void
7524f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
7525{
7526 if (rettv_list_alloc(rettv) == OK)
7527 {
7528#ifdef FEAT_SEARCH_EXTRA
7529 int id = (int)get_tv_number(&argvars[0]);
7530 matchitem_T *m;
7531
7532 if (id >= 1 && id <= 3)
7533 {
7534 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
7535 {
7536 list_append_string(rettv->vval.v_list,
7537 syn_id2name(m->hlg_id), -1);
7538 list_append_string(rettv->vval.v_list, m->pattern, -1);
7539 }
7540 else
7541 {
7542 list_append_string(rettv->vval.v_list, NULL, -1);
7543 list_append_string(rettv->vval.v_list, NULL, -1);
7544 }
7545 }
7546#endif
7547 }
7548}
7549
7550/*
7551 * "matchdelete()" function
7552 */
7553 static void
7554f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7555{
7556#ifdef FEAT_SEARCH_EXTRA
7557 rettv->vval.v_number = match_delete(curwin,
7558 (int)get_tv_number(&argvars[0]), TRUE);
7559#endif
7560}
7561
7562/*
7563 * "matchend()" function
7564 */
7565 static void
7566f_matchend(typval_T *argvars, typval_T *rettv)
7567{
7568 find_some_match(argvars, rettv, 0);
7569}
7570
7571/*
7572 * "matchlist()" function
7573 */
7574 static void
7575f_matchlist(typval_T *argvars, typval_T *rettv)
7576{
7577 find_some_match(argvars, rettv, 3);
7578}
7579
7580/*
7581 * "matchstr()" function
7582 */
7583 static void
7584f_matchstr(typval_T *argvars, typval_T *rettv)
7585{
7586 find_some_match(argvars, rettv, 2);
7587}
7588
7589/*
7590 * "matchstrpos()" function
7591 */
7592 static void
7593f_matchstrpos(typval_T *argvars, typval_T *rettv)
7594{
7595 find_some_match(argvars, rettv, 4);
7596}
7597
7598static void max_min(typval_T *argvars, typval_T *rettv, int domax);
7599
7600 static void
7601max_min(typval_T *argvars, typval_T *rettv, int domax)
7602{
7603 varnumber_T n = 0;
7604 varnumber_T i;
7605 int error = FALSE;
7606
7607 if (argvars[0].v_type == VAR_LIST)
7608 {
7609 list_T *l;
7610 listitem_T *li;
7611
7612 l = argvars[0].vval.v_list;
7613 if (l != NULL)
7614 {
7615 li = l->lv_first;
7616 if (li != NULL)
7617 {
7618 n = get_tv_number_chk(&li->li_tv, &error);
7619 for (;;)
7620 {
7621 li = li->li_next;
7622 if (li == NULL)
7623 break;
7624 i = get_tv_number_chk(&li->li_tv, &error);
7625 if (domax ? i > n : i < n)
7626 n = i;
7627 }
7628 }
7629 }
7630 }
7631 else if (argvars[0].v_type == VAR_DICT)
7632 {
7633 dict_T *d;
7634 int first = TRUE;
7635 hashitem_T *hi;
7636 int todo;
7637
7638 d = argvars[0].vval.v_dict;
7639 if (d != NULL)
7640 {
7641 todo = (int)d->dv_hashtab.ht_used;
7642 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
7643 {
7644 if (!HASHITEM_EMPTY(hi))
7645 {
7646 --todo;
7647 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
7648 if (first)
7649 {
7650 n = i;
7651 first = FALSE;
7652 }
7653 else if (domax ? i > n : i < n)
7654 n = i;
7655 }
7656 }
7657 }
7658 }
7659 else
Bram Moolenaar26b84332016-09-04 21:42:36 +02007660 EMSG2(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007661 rettv->vval.v_number = error ? 0 : n;
7662}
7663
7664/*
7665 * "max()" function
7666 */
7667 static void
7668f_max(typval_T *argvars, typval_T *rettv)
7669{
7670 max_min(argvars, rettv, TRUE);
7671}
7672
7673/*
7674 * "min()" function
7675 */
7676 static void
7677f_min(typval_T *argvars, typval_T *rettv)
7678{
7679 max_min(argvars, rettv, FALSE);
7680}
7681
7682static int mkdir_recurse(char_u *dir, int prot);
7683
7684/*
7685 * Create the directory in which "dir" is located, and higher levels when
7686 * needed.
7687 */
7688 static int
7689mkdir_recurse(char_u *dir, int prot)
7690{
7691 char_u *p;
7692 char_u *updir;
7693 int r = FAIL;
7694
7695 /* Get end of directory name in "dir".
7696 * We're done when it's "/" or "c:/". */
7697 p = gettail_sep(dir);
7698 if (p <= get_past_head(dir))
7699 return OK;
7700
7701 /* If the directory exists we're done. Otherwise: create it.*/
7702 updir = vim_strnsave(dir, (int)(p - dir));
7703 if (updir == NULL)
7704 return FAIL;
7705 if (mch_isdir(updir))
7706 r = OK;
7707 else if (mkdir_recurse(updir, prot) == OK)
7708 r = vim_mkdir_emsg(updir, prot);
7709 vim_free(updir);
7710 return r;
7711}
7712
7713#ifdef vim_mkdir
7714/*
7715 * "mkdir()" function
7716 */
7717 static void
7718f_mkdir(typval_T *argvars, typval_T *rettv)
7719{
7720 char_u *dir;
7721 char_u buf[NUMBUFLEN];
7722 int prot = 0755;
7723
7724 rettv->vval.v_number = FAIL;
7725 if (check_restricted() || check_secure())
7726 return;
7727
7728 dir = get_tv_string_buf(&argvars[0], buf);
7729 if (*dir == NUL)
7730 rettv->vval.v_number = FAIL;
7731 else
7732 {
7733 if (*gettail(dir) == NUL)
7734 /* remove trailing slashes */
7735 *gettail_sep(dir) = NUL;
7736
7737 if (argvars[1].v_type != VAR_UNKNOWN)
7738 {
7739 if (argvars[2].v_type != VAR_UNKNOWN)
7740 prot = (int)get_tv_number_chk(&argvars[2], NULL);
7741 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
7742 mkdir_recurse(dir, prot);
7743 }
7744 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
7745 }
7746}
7747#endif
7748
7749/*
7750 * "mode()" function
7751 */
7752 static void
7753f_mode(typval_T *argvars, typval_T *rettv)
7754{
7755 char_u buf[3];
7756
7757 buf[1] = NUL;
7758 buf[2] = NUL;
7759
7760 if (time_for_testing == 93784)
7761 {
7762 /* Testing the two-character code. */
7763 buf[0] = 'x';
7764 buf[1] = '!';
7765 }
7766 else if (VIsual_active)
7767 {
7768 if (VIsual_select)
7769 buf[0] = VIsual_mode + 's' - 'v';
7770 else
7771 buf[0] = VIsual_mode;
7772 }
7773 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
7774 || State == CONFIRM)
7775 {
7776 buf[0] = 'r';
7777 if (State == ASKMORE)
7778 buf[1] = 'm';
7779 else if (State == CONFIRM)
7780 buf[1] = '?';
7781 }
7782 else if (State == EXTERNCMD)
7783 buf[0] = '!';
7784 else if (State & INSERT)
7785 {
7786#ifdef FEAT_VREPLACE
7787 if (State & VREPLACE_FLAG)
7788 {
7789 buf[0] = 'R';
7790 buf[1] = 'v';
7791 }
7792 else
7793#endif
Bram Moolenaare90858d2017-02-01 17:24:34 +01007794 {
7795 if (State & REPLACE_FLAG)
7796 buf[0] = 'R';
7797 else
7798 buf[0] = 'i';
7799#ifdef FEAT_INS_EXPAND
7800 if (ins_compl_active())
7801 buf[1] = 'c';
7802 else if (ctrl_x_mode == 1)
7803 buf[1] = 'x';
7804#endif
7805 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007806 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01007807 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007808 {
7809 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01007810 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007811 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01007812 else if (exmode_active == EXMODE_NORMAL)
7813 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007814 }
7815 else
7816 {
7817 buf[0] = 'n';
7818 if (finish_op)
7819 buf[1] = 'o';
7820 }
7821
7822 /* Clear out the minor mode when the argument is not a non-zero number or
7823 * non-empty string. */
7824 if (!non_zero_arg(&argvars[0]))
7825 buf[1] = NUL;
7826
7827 rettv->vval.v_string = vim_strsave(buf);
7828 rettv->v_type = VAR_STRING;
7829}
7830
7831#if defined(FEAT_MZSCHEME) || defined(PROTO)
7832/*
7833 * "mzeval()" function
7834 */
7835 static void
7836f_mzeval(typval_T *argvars, typval_T *rettv)
7837{
7838 char_u *str;
7839 char_u buf[NUMBUFLEN];
7840
7841 str = get_tv_string_buf(&argvars[0], buf);
7842 do_mzeval(str, rettv);
7843}
7844
7845 void
7846mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
7847{
7848 typval_T argvars[3];
7849
7850 argvars[0].v_type = VAR_STRING;
7851 argvars[0].vval.v_string = name;
7852 copy_tv(args, &argvars[1]);
7853 argvars[2].v_type = VAR_UNKNOWN;
7854 f_call(argvars, rettv);
7855 clear_tv(&argvars[1]);
7856}
7857#endif
7858
7859/*
7860 * "nextnonblank()" function
7861 */
7862 static void
7863f_nextnonblank(typval_T *argvars, typval_T *rettv)
7864{
7865 linenr_T lnum;
7866
7867 for (lnum = get_tv_lnum(argvars); ; ++lnum)
7868 {
7869 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
7870 {
7871 lnum = 0;
7872 break;
7873 }
7874 if (*skipwhite(ml_get(lnum)) != NUL)
7875 break;
7876 }
7877 rettv->vval.v_number = lnum;
7878}
7879
7880/*
7881 * "nr2char()" function
7882 */
7883 static void
7884f_nr2char(typval_T *argvars, typval_T *rettv)
7885{
7886 char_u buf[NUMBUFLEN];
7887
7888#ifdef FEAT_MBYTE
7889 if (has_mbyte)
7890 {
7891 int utf8 = 0;
7892
7893 if (argvars[1].v_type != VAR_UNKNOWN)
7894 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
7895 if (utf8)
7896 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
7897 else
7898 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
7899 }
7900 else
7901#endif
7902 {
7903 buf[0] = (char_u)get_tv_number(&argvars[0]);
7904 buf[1] = NUL;
7905 }
7906 rettv->v_type = VAR_STRING;
7907 rettv->vval.v_string = vim_strsave(buf);
7908}
7909
7910/*
7911 * "or(expr, expr)" function
7912 */
7913 static void
7914f_or(typval_T *argvars, typval_T *rettv)
7915{
7916 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
7917 | get_tv_number_chk(&argvars[1], NULL);
7918}
7919
7920/*
7921 * "pathshorten()" function
7922 */
7923 static void
7924f_pathshorten(typval_T *argvars, typval_T *rettv)
7925{
7926 char_u *p;
7927
7928 rettv->v_type = VAR_STRING;
7929 p = get_tv_string_chk(&argvars[0]);
7930 if (p == NULL)
7931 rettv->vval.v_string = NULL;
7932 else
7933 {
7934 p = vim_strsave(p);
7935 rettv->vval.v_string = p;
7936 if (p != NULL)
7937 shorten_dir(p);
7938 }
7939}
7940
7941#ifdef FEAT_PERL
7942/*
7943 * "perleval()" function
7944 */
7945 static void
7946f_perleval(typval_T *argvars, typval_T *rettv)
7947{
7948 char_u *str;
7949 char_u buf[NUMBUFLEN];
7950
7951 str = get_tv_string_buf(&argvars[0], buf);
7952 do_perleval(str, rettv);
7953}
7954#endif
7955
7956#ifdef FEAT_FLOAT
7957/*
7958 * "pow()" function
7959 */
7960 static void
7961f_pow(typval_T *argvars, typval_T *rettv)
7962{
7963 float_T fx = 0.0, fy = 0.0;
7964
7965 rettv->v_type = VAR_FLOAT;
7966 if (get_float_arg(argvars, &fx) == OK
7967 && get_float_arg(&argvars[1], &fy) == OK)
7968 rettv->vval.v_float = pow(fx, fy);
7969 else
7970 rettv->vval.v_float = 0.0;
7971}
7972#endif
7973
7974/*
7975 * "prevnonblank()" function
7976 */
7977 static void
7978f_prevnonblank(typval_T *argvars, typval_T *rettv)
7979{
7980 linenr_T lnum;
7981
7982 lnum = get_tv_lnum(argvars);
7983 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
7984 lnum = 0;
7985 else
7986 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
7987 --lnum;
7988 rettv->vval.v_number = lnum;
7989}
7990
7991/* This dummy va_list is here because:
7992 * - passing a NULL pointer doesn't work when va_list isn't a pointer
7993 * - locally in the function results in a "used before set" warning
7994 * - using va_start() to initialize it gives "function with fixed args" error */
7995static va_list ap;
7996
7997/*
7998 * "printf()" function
7999 */
8000 static void
8001f_printf(typval_T *argvars, typval_T *rettv)
8002{
8003 char_u buf[NUMBUFLEN];
8004 int len;
8005 char_u *s;
8006 int saved_did_emsg = did_emsg;
8007 char *fmt;
8008
8009 rettv->v_type = VAR_STRING;
8010 rettv->vval.v_string = NULL;
8011
8012 /* Get the required length, allocate the buffer and do it for real. */
8013 did_emsg = FALSE;
8014 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
8015 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
8016 if (!did_emsg)
8017 {
8018 s = alloc(len + 1);
8019 if (s != NULL)
8020 {
8021 rettv->vval.v_string = s;
8022 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
8023 }
8024 }
8025 did_emsg |= saved_did_emsg;
8026}
8027
8028/*
8029 * "pumvisible()" function
8030 */
8031 static void
8032f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8033{
8034#ifdef FEAT_INS_EXPAND
8035 if (pum_visible())
8036 rettv->vval.v_number = 1;
8037#endif
8038}
8039
8040#ifdef FEAT_PYTHON3
8041/*
8042 * "py3eval()" function
8043 */
8044 static void
8045f_py3eval(typval_T *argvars, typval_T *rettv)
8046{
8047 char_u *str;
8048 char_u buf[NUMBUFLEN];
8049
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008050 if (p_pyx == 0)
8051 p_pyx = 3;
8052
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008053 str = get_tv_string_buf(&argvars[0], buf);
8054 do_py3eval(str, rettv);
8055}
8056#endif
8057
8058#ifdef FEAT_PYTHON
8059/*
8060 * "pyeval()" function
8061 */
8062 static void
8063f_pyeval(typval_T *argvars, typval_T *rettv)
8064{
8065 char_u *str;
8066 char_u buf[NUMBUFLEN];
8067
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008068 if (p_pyx == 0)
8069 p_pyx = 2;
8070
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008071 str = get_tv_string_buf(&argvars[0], buf);
8072 do_pyeval(str, rettv);
8073}
8074#endif
8075
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008076#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8077/*
8078 * "pyxeval()" function
8079 */
8080 static void
8081f_pyxeval(typval_T *argvars, typval_T *rettv)
8082{
8083# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8084 init_pyxversion();
8085 if (p_pyx == 2)
8086 f_pyeval(argvars, rettv);
8087 else
8088 f_py3eval(argvars, rettv);
8089# elif defined(FEAT_PYTHON)
8090 f_pyeval(argvars, rettv);
8091# elif defined(FEAT_PYTHON3)
8092 f_py3eval(argvars, rettv);
8093# endif
8094}
8095#endif
8096
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008097/*
8098 * "range()" function
8099 */
8100 static void
8101f_range(typval_T *argvars, typval_T *rettv)
8102{
8103 varnumber_T start;
8104 varnumber_T end;
8105 varnumber_T stride = 1;
8106 varnumber_T i;
8107 int error = FALSE;
8108
8109 start = get_tv_number_chk(&argvars[0], &error);
8110 if (argvars[1].v_type == VAR_UNKNOWN)
8111 {
8112 end = start - 1;
8113 start = 0;
8114 }
8115 else
8116 {
8117 end = get_tv_number_chk(&argvars[1], &error);
8118 if (argvars[2].v_type != VAR_UNKNOWN)
8119 stride = get_tv_number_chk(&argvars[2], &error);
8120 }
8121
8122 if (error)
8123 return; /* type error; errmsg already given */
8124 if (stride == 0)
8125 EMSG(_("E726: Stride is zero"));
8126 else if (stride > 0 ? end + 1 < start : end - 1 > start)
8127 EMSG(_("E727: Start past end"));
8128 else
8129 {
8130 if (rettv_list_alloc(rettv) == OK)
8131 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8132 if (list_append_number(rettv->vval.v_list,
8133 (varnumber_T)i) == FAIL)
8134 break;
8135 }
8136}
8137
8138/*
8139 * "readfile()" function
8140 */
8141 static void
8142f_readfile(typval_T *argvars, typval_T *rettv)
8143{
8144 int binary = FALSE;
8145 int failed = FALSE;
8146 char_u *fname;
8147 FILE *fd;
8148 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8149 int io_size = sizeof(buf);
8150 int readlen; /* size of last fread() */
8151 char_u *prev = NULL; /* previously read bytes, if any */
8152 long prevlen = 0; /* length of data in prev */
8153 long prevsize = 0; /* size of prev buffer */
8154 long maxline = MAXLNUM;
8155 long cnt = 0;
8156 char_u *p; /* position in buf */
8157 char_u *start; /* start of current line */
8158
8159 if (argvars[1].v_type != VAR_UNKNOWN)
8160 {
8161 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
8162 binary = TRUE;
8163 if (argvars[2].v_type != VAR_UNKNOWN)
8164 maxline = (long)get_tv_number(&argvars[2]);
8165 }
8166
8167 if (rettv_list_alloc(rettv) == FAIL)
8168 return;
8169
8170 /* Always open the file in binary mode, library functions have a mind of
8171 * their own about CR-LF conversion. */
8172 fname = get_tv_string(&argvars[0]);
8173 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8174 {
8175 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
8176 return;
8177 }
8178
8179 while (cnt < maxline || maxline < 0)
8180 {
8181 readlen = (int)fread(buf, 1, io_size, fd);
8182
8183 /* This for loop processes what was read, but is also entered at end
8184 * of file so that either:
8185 * - an incomplete line gets written
8186 * - a "binary" file gets an empty line at the end if it ends in a
8187 * newline. */
8188 for (p = buf, start = buf;
8189 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8190 ++p)
8191 {
8192 if (*p == '\n' || readlen <= 0)
8193 {
8194 listitem_T *li;
8195 char_u *s = NULL;
8196 long_u len = p - start;
8197
8198 /* Finished a line. Remove CRs before NL. */
8199 if (readlen > 0 && !binary)
8200 {
8201 while (len > 0 && start[len - 1] == '\r')
8202 --len;
8203 /* removal may cross back to the "prev" string */
8204 if (len == 0)
8205 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8206 --prevlen;
8207 }
8208 if (prevlen == 0)
8209 s = vim_strnsave(start, (int)len);
8210 else
8211 {
8212 /* Change "prev" buffer to be the right size. This way
8213 * the bytes are only copied once, and very long lines are
8214 * allocated only once. */
8215 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8216 {
8217 mch_memmove(s + prevlen, start, len);
8218 s[prevlen + len] = NUL;
8219 prev = NULL; /* the list will own the string */
8220 prevlen = prevsize = 0;
8221 }
8222 }
8223 if (s == NULL)
8224 {
8225 do_outofmem_msg((long_u) prevlen + len + 1);
8226 failed = TRUE;
8227 break;
8228 }
8229
8230 if ((li = listitem_alloc()) == NULL)
8231 {
8232 vim_free(s);
8233 failed = TRUE;
8234 break;
8235 }
8236 li->li_tv.v_type = VAR_STRING;
8237 li->li_tv.v_lock = 0;
8238 li->li_tv.vval.v_string = s;
8239 list_append(rettv->vval.v_list, li);
8240
8241 start = p + 1; /* step over newline */
8242 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
8243 break;
8244 }
8245 else if (*p == NUL)
8246 *p = '\n';
8247#ifdef FEAT_MBYTE
8248 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
8249 * when finding the BF and check the previous two bytes. */
8250 else if (*p == 0xbf && enc_utf8 && !binary)
8251 {
8252 /* Find the two bytes before the 0xbf. If p is at buf, or buf
8253 * + 1, these may be in the "prev" string. */
8254 char_u back1 = p >= buf + 1 ? p[-1]
8255 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
8256 char_u back2 = p >= buf + 2 ? p[-2]
8257 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
8258 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
8259
8260 if (back2 == 0xef && back1 == 0xbb)
8261 {
8262 char_u *dest = p - 2;
8263
8264 /* Usually a BOM is at the beginning of a file, and so at
8265 * the beginning of a line; then we can just step over it.
8266 */
8267 if (start == dest)
8268 start = p + 1;
8269 else
8270 {
8271 /* have to shuffle buf to close gap */
8272 int adjust_prevlen = 0;
8273
8274 if (dest < buf)
8275 {
8276 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
8277 dest = buf;
8278 }
8279 if (readlen > p - buf + 1)
8280 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
8281 readlen -= 3 - adjust_prevlen;
8282 prevlen -= adjust_prevlen;
8283 p = dest - 1;
8284 }
8285 }
8286 }
8287#endif
8288 } /* for */
8289
8290 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
8291 break;
8292 if (start < p)
8293 {
8294 /* There's part of a line in buf, store it in "prev". */
8295 if (p - start + prevlen >= prevsize)
8296 {
8297 /* need bigger "prev" buffer */
8298 char_u *newprev;
8299
8300 /* A common use case is ordinary text files and "prev" gets a
8301 * fragment of a line, so the first allocation is made
8302 * small, to avoid repeatedly 'allocing' large and
8303 * 'reallocing' small. */
8304 if (prevsize == 0)
8305 prevsize = (long)(p - start);
8306 else
8307 {
8308 long grow50pc = (prevsize * 3) / 2;
8309 long growmin = (long)((p - start) * 2 + prevlen);
8310 prevsize = grow50pc > growmin ? grow50pc : growmin;
8311 }
8312 newprev = prev == NULL ? alloc(prevsize)
8313 : vim_realloc(prev, prevsize);
8314 if (newprev == NULL)
8315 {
8316 do_outofmem_msg((long_u)prevsize);
8317 failed = TRUE;
8318 break;
8319 }
8320 prev = newprev;
8321 }
8322 /* Add the line part to end of "prev". */
8323 mch_memmove(prev + prevlen, start, p - start);
8324 prevlen += (long)(p - start);
8325 }
8326 } /* while */
8327
8328 /*
8329 * For a negative line count use only the lines at the end of the file,
8330 * free the rest.
8331 */
8332 if (!failed && maxline < 0)
8333 while (cnt > -maxline)
8334 {
8335 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
8336 --cnt;
8337 }
8338
8339 if (failed)
8340 {
8341 list_free(rettv->vval.v_list);
8342 /* readfile doc says an empty list is returned on error */
8343 rettv->vval.v_list = list_alloc();
8344 }
8345
8346 vim_free(prev);
8347 fclose(fd);
8348}
8349
8350#if defined(FEAT_RELTIME)
8351static int list2proftime(typval_T *arg, proftime_T *tm);
8352
8353/*
8354 * Convert a List to proftime_T.
8355 * Return FAIL when there is something wrong.
8356 */
8357 static int
8358list2proftime(typval_T *arg, proftime_T *tm)
8359{
8360 long n1, n2;
8361 int error = FALSE;
8362
8363 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
8364 || arg->vval.v_list->lv_len != 2)
8365 return FAIL;
8366 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
8367 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
8368# ifdef WIN3264
8369 tm->HighPart = n1;
8370 tm->LowPart = n2;
8371# else
8372 tm->tv_sec = n1;
8373 tm->tv_usec = n2;
8374# endif
8375 return error ? FAIL : OK;
8376}
8377#endif /* FEAT_RELTIME */
8378
8379/*
8380 * "reltime()" function
8381 */
8382 static void
8383f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8384{
8385#ifdef FEAT_RELTIME
8386 proftime_T res;
8387 proftime_T start;
8388
8389 if (argvars[0].v_type == VAR_UNKNOWN)
8390 {
8391 /* No arguments: get current time. */
8392 profile_start(&res);
8393 }
8394 else if (argvars[1].v_type == VAR_UNKNOWN)
8395 {
8396 if (list2proftime(&argvars[0], &res) == FAIL)
8397 return;
8398 profile_end(&res);
8399 }
8400 else
8401 {
8402 /* Two arguments: compute the difference. */
8403 if (list2proftime(&argvars[0], &start) == FAIL
8404 || list2proftime(&argvars[1], &res) == FAIL)
8405 return;
8406 profile_sub(&res, &start);
8407 }
8408
8409 if (rettv_list_alloc(rettv) == OK)
8410 {
8411 long n1, n2;
8412
8413# ifdef WIN3264
8414 n1 = res.HighPart;
8415 n2 = res.LowPart;
8416# else
8417 n1 = res.tv_sec;
8418 n2 = res.tv_usec;
8419# endif
8420 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
8421 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
8422 }
8423#endif
8424}
8425
8426#ifdef FEAT_FLOAT
8427/*
8428 * "reltimefloat()" function
8429 */
8430 static void
8431f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
8432{
8433# ifdef FEAT_RELTIME
8434 proftime_T tm;
8435# endif
8436
8437 rettv->v_type = VAR_FLOAT;
8438 rettv->vval.v_float = 0;
8439# ifdef FEAT_RELTIME
8440 if (list2proftime(&argvars[0], &tm) == OK)
8441 rettv->vval.v_float = profile_float(&tm);
8442# endif
8443}
8444#endif
8445
8446/*
8447 * "reltimestr()" function
8448 */
8449 static void
8450f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
8451{
8452#ifdef FEAT_RELTIME
8453 proftime_T tm;
8454#endif
8455
8456 rettv->v_type = VAR_STRING;
8457 rettv->vval.v_string = NULL;
8458#ifdef FEAT_RELTIME
8459 if (list2proftime(&argvars[0], &tm) == OK)
8460 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
8461#endif
8462}
8463
8464#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
8465static void make_connection(void);
8466static int check_connection(void);
8467
8468 static void
8469make_connection(void)
8470{
8471 if (X_DISPLAY == NULL
8472# ifdef FEAT_GUI
8473 && !gui.in_use
8474# endif
8475 )
8476 {
8477 x_force_connect = TRUE;
8478 setup_term_clip();
8479 x_force_connect = FALSE;
8480 }
8481}
8482
8483 static int
8484check_connection(void)
8485{
8486 make_connection();
8487 if (X_DISPLAY == NULL)
8488 {
8489 EMSG(_("E240: No connection to Vim server"));
8490 return FAIL;
8491 }
8492 return OK;
8493}
8494#endif
8495
8496#ifdef FEAT_CLIENTSERVER
8497 static void
8498remote_common(typval_T *argvars, typval_T *rettv, int expr)
8499{
8500 char_u *server_name;
8501 char_u *keys;
8502 char_u *r = NULL;
8503 char_u buf[NUMBUFLEN];
8504# ifdef WIN32
8505 HWND w;
8506# else
8507 Window w;
8508# endif
8509
8510 if (check_restricted() || check_secure())
8511 return;
8512
8513# ifdef FEAT_X11
8514 if (check_connection() == FAIL)
8515 return;
8516# endif
8517
8518 server_name = get_tv_string_chk(&argvars[0]);
8519 if (server_name == NULL)
8520 return; /* type error; errmsg already given */
8521 keys = get_tv_string_buf(&argvars[1], buf);
8522# ifdef WIN32
8523 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
8524# else
8525 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
8526 < 0)
8527# endif
8528 {
8529 if (r != NULL)
8530 EMSG(r); /* sending worked but evaluation failed */
8531 else
8532 EMSG2(_("E241: Unable to send to %s"), server_name);
8533 return;
8534 }
8535
8536 rettv->vval.v_string = r;
8537
8538 if (argvars[2].v_type != VAR_UNKNOWN)
8539 {
8540 dictitem_T v;
8541 char_u str[30];
8542 char_u *idvar;
8543
8544 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
8545 v.di_tv.v_type = VAR_STRING;
8546 v.di_tv.vval.v_string = vim_strsave(str);
8547 idvar = get_tv_string_chk(&argvars[2]);
8548 if (idvar != NULL)
8549 set_var(idvar, &v.di_tv, FALSE);
8550 vim_free(v.di_tv.vval.v_string);
8551 }
8552}
8553#endif
8554
8555/*
8556 * "remote_expr()" function
8557 */
8558 static void
8559f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
8560{
8561 rettv->v_type = VAR_STRING;
8562 rettv->vval.v_string = NULL;
8563#ifdef FEAT_CLIENTSERVER
8564 remote_common(argvars, rettv, TRUE);
8565#endif
8566}
8567
8568/*
8569 * "remote_foreground()" function
8570 */
8571 static void
8572f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8573{
8574#ifdef FEAT_CLIENTSERVER
8575# ifdef WIN32
8576 /* On Win32 it's done in this application. */
8577 {
8578 char_u *server_name = get_tv_string_chk(&argvars[0]);
8579
8580 if (server_name != NULL)
8581 serverForeground(server_name);
8582 }
8583# else
8584 /* Send a foreground() expression to the server. */
8585 argvars[1].v_type = VAR_STRING;
8586 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
8587 argvars[2].v_type = VAR_UNKNOWN;
8588 remote_common(argvars, rettv, TRUE);
8589 vim_free(argvars[1].vval.v_string);
8590# endif
8591#endif
8592}
8593
8594 static void
8595f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
8596{
8597#ifdef FEAT_CLIENTSERVER
8598 dictitem_T v;
8599 char_u *s = NULL;
8600# ifdef WIN32
8601 long_u n = 0;
8602# endif
8603 char_u *serverid;
8604
8605 if (check_restricted() || check_secure())
8606 {
8607 rettv->vval.v_number = -1;
8608 return;
8609 }
8610 serverid = get_tv_string_chk(&argvars[0]);
8611 if (serverid == NULL)
8612 {
8613 rettv->vval.v_number = -1;
8614 return; /* type error; errmsg already given */
8615 }
8616# ifdef WIN32
8617 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
8618 if (n == 0)
8619 rettv->vval.v_number = -1;
8620 else
8621 {
8622 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
8623 rettv->vval.v_number = (s != NULL);
8624 }
8625# else
8626 if (check_connection() == FAIL)
8627 return;
8628
8629 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
8630 serverStrToWin(serverid), &s);
8631# endif
8632
8633 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
8634 {
8635 char_u *retvar;
8636
8637 v.di_tv.v_type = VAR_STRING;
8638 v.di_tv.vval.v_string = vim_strsave(s);
8639 retvar = get_tv_string_chk(&argvars[1]);
8640 if (retvar != NULL)
8641 set_var(retvar, &v.di_tv, FALSE);
8642 vim_free(v.di_tv.vval.v_string);
8643 }
8644#else
8645 rettv->vval.v_number = -1;
8646#endif
8647}
8648
8649 static void
8650f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
8651{
8652 char_u *r = NULL;
8653
8654#ifdef FEAT_CLIENTSERVER
8655 char_u *serverid = get_tv_string_chk(&argvars[0]);
8656
8657 if (serverid != NULL && !check_restricted() && !check_secure())
8658 {
8659# ifdef WIN32
8660 /* The server's HWND is encoded in the 'id' parameter */
8661 long_u n = 0;
8662
8663 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
8664 if (n != 0)
8665 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
8666 if (r == NULL)
8667# else
8668 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
8669 serverStrToWin(serverid), &r, FALSE) < 0)
8670# endif
8671 EMSG(_("E277: Unable to read a server reply"));
8672 }
8673#endif
8674 rettv->v_type = VAR_STRING;
8675 rettv->vval.v_string = r;
8676}
8677
8678/*
8679 * "remote_send()" function
8680 */
8681 static void
8682f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
8683{
8684 rettv->v_type = VAR_STRING;
8685 rettv->vval.v_string = NULL;
8686#ifdef FEAT_CLIENTSERVER
8687 remote_common(argvars, rettv, FALSE);
8688#endif
8689}
8690
8691/*
8692 * "remove()" function
8693 */
8694 static void
8695f_remove(typval_T *argvars, typval_T *rettv)
8696{
8697 list_T *l;
8698 listitem_T *item, *item2;
8699 listitem_T *li;
8700 long idx;
8701 long end;
8702 char_u *key;
8703 dict_T *d;
8704 dictitem_T *di;
8705 char_u *arg_errmsg = (char_u *)N_("remove() argument");
8706
8707 if (argvars[0].v_type == VAR_DICT)
8708 {
8709 if (argvars[2].v_type != VAR_UNKNOWN)
8710 EMSG2(_(e_toomanyarg), "remove()");
8711 else if ((d = argvars[0].vval.v_dict) != NULL
8712 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
8713 {
8714 key = get_tv_string_chk(&argvars[1]);
8715 if (key != NULL)
8716 {
8717 di = dict_find(d, key, -1);
8718 if (di == NULL)
8719 EMSG2(_(e_dictkey), key);
8720 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
8721 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
8722 {
8723 *rettv = di->di_tv;
8724 init_tv(&di->di_tv);
8725 dictitem_remove(d, di);
8726 }
8727 }
8728 }
8729 }
8730 else if (argvars[0].v_type != VAR_LIST)
8731 EMSG2(_(e_listdictarg), "remove()");
8732 else if ((l = argvars[0].vval.v_list) != NULL
8733 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
8734 {
8735 int error = FALSE;
8736
8737 idx = (long)get_tv_number_chk(&argvars[1], &error);
8738 if (error)
8739 ; /* type error: do nothing, errmsg already given */
8740 else if ((item = list_find(l, idx)) == NULL)
8741 EMSGN(_(e_listidx), idx);
8742 else
8743 {
8744 if (argvars[2].v_type == VAR_UNKNOWN)
8745 {
8746 /* Remove one item, return its value. */
8747 vimlist_remove(l, item, item);
8748 *rettv = item->li_tv;
8749 vim_free(item);
8750 }
8751 else
8752 {
8753 /* Remove range of items, return list with values. */
8754 end = (long)get_tv_number_chk(&argvars[2], &error);
8755 if (error)
8756 ; /* type error: do nothing */
8757 else if ((item2 = list_find(l, end)) == NULL)
8758 EMSGN(_(e_listidx), end);
8759 else
8760 {
8761 int cnt = 0;
8762
8763 for (li = item; li != NULL; li = li->li_next)
8764 {
8765 ++cnt;
8766 if (li == item2)
8767 break;
8768 }
8769 if (li == NULL) /* didn't find "item2" after "item" */
8770 EMSG(_(e_invrange));
8771 else
8772 {
8773 vimlist_remove(l, item, item2);
8774 if (rettv_list_alloc(rettv) == OK)
8775 {
8776 l = rettv->vval.v_list;
8777 l->lv_first = item;
8778 l->lv_last = item2;
8779 item->li_prev = NULL;
8780 item2->li_next = NULL;
8781 l->lv_len = cnt;
8782 }
8783 }
8784 }
8785 }
8786 }
8787 }
8788}
8789
8790/*
8791 * "rename({from}, {to})" function
8792 */
8793 static void
8794f_rename(typval_T *argvars, typval_T *rettv)
8795{
8796 char_u buf[NUMBUFLEN];
8797
8798 if (check_restricted() || check_secure())
8799 rettv->vval.v_number = -1;
8800 else
8801 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
8802 get_tv_string_buf(&argvars[1], buf));
8803}
8804
8805/*
8806 * "repeat()" function
8807 */
8808 static void
8809f_repeat(typval_T *argvars, typval_T *rettv)
8810{
8811 char_u *p;
8812 int n;
8813 int slen;
8814 int len;
8815 char_u *r;
8816 int i;
8817
8818 n = (int)get_tv_number(&argvars[1]);
8819 if (argvars[0].v_type == VAR_LIST)
8820 {
8821 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
8822 while (n-- > 0)
8823 if (list_extend(rettv->vval.v_list,
8824 argvars[0].vval.v_list, NULL) == FAIL)
8825 break;
8826 }
8827 else
8828 {
8829 p = get_tv_string(&argvars[0]);
8830 rettv->v_type = VAR_STRING;
8831 rettv->vval.v_string = NULL;
8832
8833 slen = (int)STRLEN(p);
8834 len = slen * n;
8835 if (len <= 0)
8836 return;
8837
8838 r = alloc(len + 1);
8839 if (r != NULL)
8840 {
8841 for (i = 0; i < n; i++)
8842 mch_memmove(r + i * slen, p, (size_t)slen);
8843 r[len] = NUL;
8844 }
8845
8846 rettv->vval.v_string = r;
8847 }
8848}
8849
8850/*
8851 * "resolve()" function
8852 */
8853 static void
8854f_resolve(typval_T *argvars, typval_T *rettv)
8855{
8856 char_u *p;
8857#ifdef HAVE_READLINK
8858 char_u *buf = NULL;
8859#endif
8860
8861 p = get_tv_string(&argvars[0]);
8862#ifdef FEAT_SHORTCUT
8863 {
8864 char_u *v = NULL;
8865
8866 v = mch_resolve_shortcut(p);
8867 if (v != NULL)
8868 rettv->vval.v_string = v;
8869 else
8870 rettv->vval.v_string = vim_strsave(p);
8871 }
8872#else
8873# ifdef HAVE_READLINK
8874 {
8875 char_u *cpy;
8876 int len;
8877 char_u *remain = NULL;
8878 char_u *q;
8879 int is_relative_to_current = FALSE;
8880 int has_trailing_pathsep = FALSE;
8881 int limit = 100;
8882
8883 p = vim_strsave(p);
8884
8885 if (p[0] == '.' && (vim_ispathsep(p[1])
8886 || (p[1] == '.' && (vim_ispathsep(p[2])))))
8887 is_relative_to_current = TRUE;
8888
8889 len = STRLEN(p);
8890 if (len > 0 && after_pathsep(p, p + len))
8891 {
8892 has_trailing_pathsep = TRUE;
8893 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
8894 }
8895
8896 q = getnextcomp(p);
8897 if (*q != NUL)
8898 {
8899 /* Separate the first path component in "p", and keep the
8900 * remainder (beginning with the path separator). */
8901 remain = vim_strsave(q - 1);
8902 q[-1] = NUL;
8903 }
8904
8905 buf = alloc(MAXPATHL + 1);
8906 if (buf == NULL)
8907 goto fail;
8908
8909 for (;;)
8910 {
8911 for (;;)
8912 {
8913 len = readlink((char *)p, (char *)buf, MAXPATHL);
8914 if (len <= 0)
8915 break;
8916 buf[len] = NUL;
8917
8918 if (limit-- == 0)
8919 {
8920 vim_free(p);
8921 vim_free(remain);
8922 EMSG(_("E655: Too many symbolic links (cycle?)"));
8923 rettv->vval.v_string = NULL;
8924 goto fail;
8925 }
8926
8927 /* Ensure that the result will have a trailing path separator
8928 * if the argument has one. */
8929 if (remain == NULL && has_trailing_pathsep)
8930 add_pathsep(buf);
8931
8932 /* Separate the first path component in the link value and
8933 * concatenate the remainders. */
8934 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
8935 if (*q != NUL)
8936 {
8937 if (remain == NULL)
8938 remain = vim_strsave(q - 1);
8939 else
8940 {
8941 cpy = concat_str(q - 1, remain);
8942 if (cpy != NULL)
8943 {
8944 vim_free(remain);
8945 remain = cpy;
8946 }
8947 }
8948 q[-1] = NUL;
8949 }
8950
8951 q = gettail(p);
8952 if (q > p && *q == NUL)
8953 {
8954 /* Ignore trailing path separator. */
8955 q[-1] = NUL;
8956 q = gettail(p);
8957 }
8958 if (q > p && !mch_isFullName(buf))
8959 {
8960 /* symlink is relative to directory of argument */
8961 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
8962 if (cpy != NULL)
8963 {
8964 STRCPY(cpy, p);
8965 STRCPY(gettail(cpy), buf);
8966 vim_free(p);
8967 p = cpy;
8968 }
8969 }
8970 else
8971 {
8972 vim_free(p);
8973 p = vim_strsave(buf);
8974 }
8975 }
8976
8977 if (remain == NULL)
8978 break;
8979
8980 /* Append the first path component of "remain" to "p". */
8981 q = getnextcomp(remain + 1);
8982 len = q - remain - (*q != NUL);
8983 cpy = vim_strnsave(p, STRLEN(p) + len);
8984 if (cpy != NULL)
8985 {
8986 STRNCAT(cpy, remain, len);
8987 vim_free(p);
8988 p = cpy;
8989 }
8990 /* Shorten "remain". */
8991 if (*q != NUL)
8992 STRMOVE(remain, q - 1);
8993 else
8994 {
8995 vim_free(remain);
8996 remain = NULL;
8997 }
8998 }
8999
9000 /* If the result is a relative path name, make it explicitly relative to
9001 * the current directory if and only if the argument had this form. */
9002 if (!vim_ispathsep(*p))
9003 {
9004 if (is_relative_to_current
9005 && *p != NUL
9006 && !(p[0] == '.'
9007 && (p[1] == NUL
9008 || vim_ispathsep(p[1])
9009 || (p[1] == '.'
9010 && (p[2] == NUL
9011 || vim_ispathsep(p[2]))))))
9012 {
9013 /* Prepend "./". */
9014 cpy = concat_str((char_u *)"./", p);
9015 if (cpy != NULL)
9016 {
9017 vim_free(p);
9018 p = cpy;
9019 }
9020 }
9021 else if (!is_relative_to_current)
9022 {
9023 /* Strip leading "./". */
9024 q = p;
9025 while (q[0] == '.' && vim_ispathsep(q[1]))
9026 q += 2;
9027 if (q > p)
9028 STRMOVE(p, p + 2);
9029 }
9030 }
9031
9032 /* Ensure that the result will have no trailing path separator
9033 * if the argument had none. But keep "/" or "//". */
9034 if (!has_trailing_pathsep)
9035 {
9036 q = p + STRLEN(p);
9037 if (after_pathsep(p, q))
9038 *gettail_sep(p) = NUL;
9039 }
9040
9041 rettv->vval.v_string = p;
9042 }
9043# else
9044 rettv->vval.v_string = vim_strsave(p);
9045# endif
9046#endif
9047
9048 simplify_filename(rettv->vval.v_string);
9049
9050#ifdef HAVE_READLINK
9051fail:
9052 vim_free(buf);
9053#endif
9054 rettv->v_type = VAR_STRING;
9055}
9056
9057/*
9058 * "reverse({list})" function
9059 */
9060 static void
9061f_reverse(typval_T *argvars, typval_T *rettv)
9062{
9063 list_T *l;
9064 listitem_T *li, *ni;
9065
9066 if (argvars[0].v_type != VAR_LIST)
9067 EMSG2(_(e_listarg), "reverse()");
9068 else if ((l = argvars[0].vval.v_list) != NULL
9069 && !tv_check_lock(l->lv_lock,
9070 (char_u *)N_("reverse() argument"), TRUE))
9071 {
9072 li = l->lv_last;
9073 l->lv_first = l->lv_last = NULL;
9074 l->lv_len = 0;
9075 while (li != NULL)
9076 {
9077 ni = li->li_prev;
9078 list_append(l, li);
9079 li = ni;
9080 }
9081 rettv->vval.v_list = l;
9082 rettv->v_type = VAR_LIST;
9083 ++l->lv_refcount;
9084 l->lv_idx = l->lv_len - l->lv_idx - 1;
9085 }
9086}
9087
9088#define SP_NOMOVE 0x01 /* don't move cursor */
9089#define SP_REPEAT 0x02 /* repeat to find outer pair */
9090#define SP_RETCOUNT 0x04 /* return matchcount */
9091#define SP_SETPCMARK 0x08 /* set previous context mark */
9092#define SP_START 0x10 /* accept match at start position */
9093#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9094#define SP_END 0x40 /* leave cursor at end of match */
9095#define SP_COLUMN 0x80 /* start at cursor column */
9096
9097static int get_search_arg(typval_T *varp, int *flagsp);
9098
9099/*
9100 * Get flags for a search function.
9101 * Possibly sets "p_ws".
9102 * Returns BACKWARD, FORWARD or zero (for an error).
9103 */
9104 static int
9105get_search_arg(typval_T *varp, int *flagsp)
9106{
9107 int dir = FORWARD;
9108 char_u *flags;
9109 char_u nbuf[NUMBUFLEN];
9110 int mask;
9111
9112 if (varp->v_type != VAR_UNKNOWN)
9113 {
9114 flags = get_tv_string_buf_chk(varp, nbuf);
9115 if (flags == NULL)
9116 return 0; /* type error; errmsg already given */
9117 while (*flags != NUL)
9118 {
9119 switch (*flags)
9120 {
9121 case 'b': dir = BACKWARD; break;
9122 case 'w': p_ws = TRUE; break;
9123 case 'W': p_ws = FALSE; break;
9124 default: mask = 0;
9125 if (flagsp != NULL)
9126 switch (*flags)
9127 {
9128 case 'c': mask = SP_START; break;
9129 case 'e': mask = SP_END; break;
9130 case 'm': mask = SP_RETCOUNT; break;
9131 case 'n': mask = SP_NOMOVE; break;
9132 case 'p': mask = SP_SUBPAT; break;
9133 case 'r': mask = SP_REPEAT; break;
9134 case 's': mask = SP_SETPCMARK; break;
9135 case 'z': mask = SP_COLUMN; break;
9136 }
9137 if (mask == 0)
9138 {
9139 EMSG2(_(e_invarg2), flags);
9140 dir = 0;
9141 }
9142 else
9143 *flagsp |= mask;
9144 }
9145 if (dir == 0)
9146 break;
9147 ++flags;
9148 }
9149 }
9150 return dir;
9151}
9152
9153/*
9154 * Shared by search() and searchpos() functions.
9155 */
9156 static int
9157search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9158{
9159 int flags;
9160 char_u *pat;
9161 pos_T pos;
9162 pos_T save_cursor;
9163 int save_p_ws = p_ws;
9164 int dir;
9165 int retval = 0; /* default: FAIL */
9166 long lnum_stop = 0;
9167 proftime_T tm;
9168#ifdef FEAT_RELTIME
9169 long time_limit = 0;
9170#endif
9171 int options = SEARCH_KEEP;
9172 int subpatnum;
9173
9174 pat = get_tv_string(&argvars[0]);
9175 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
9176 if (dir == 0)
9177 goto theend;
9178 flags = *flagsp;
9179 if (flags & SP_START)
9180 options |= SEARCH_START;
9181 if (flags & SP_END)
9182 options |= SEARCH_END;
9183 if (flags & SP_COLUMN)
9184 options |= SEARCH_COL;
9185
9186 /* Optional arguments: line number to stop searching and timeout. */
9187 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9188 {
9189 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
9190 if (lnum_stop < 0)
9191 goto theend;
9192#ifdef FEAT_RELTIME
9193 if (argvars[3].v_type != VAR_UNKNOWN)
9194 {
9195 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
9196 if (time_limit < 0)
9197 goto theend;
9198 }
9199#endif
9200 }
9201
9202#ifdef FEAT_RELTIME
9203 /* Set the time limit, if there is one. */
9204 profile_setlimit(time_limit, &tm);
9205#endif
9206
9207 /*
9208 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
9209 * Check to make sure only those flags are set.
9210 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
9211 * flags cannot be set. Check for that condition also.
9212 */
9213 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
9214 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9215 {
9216 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
9217 goto theend;
9218 }
9219
9220 pos = save_cursor = curwin->w_cursor;
9221 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
9222 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
9223 if (subpatnum != FAIL)
9224 {
9225 if (flags & SP_SUBPAT)
9226 retval = subpatnum;
9227 else
9228 retval = pos.lnum;
9229 if (flags & SP_SETPCMARK)
9230 setpcmark();
9231 curwin->w_cursor = pos;
9232 if (match_pos != NULL)
9233 {
9234 /* Store the match cursor position */
9235 match_pos->lnum = pos.lnum;
9236 match_pos->col = pos.col + 1;
9237 }
9238 /* "/$" will put the cursor after the end of the line, may need to
9239 * correct that here */
9240 check_cursor();
9241 }
9242
9243 /* If 'n' flag is used: restore cursor position. */
9244 if (flags & SP_NOMOVE)
9245 curwin->w_cursor = save_cursor;
9246 else
9247 curwin->w_set_curswant = TRUE;
9248theend:
9249 p_ws = save_p_ws;
9250
9251 return retval;
9252}
9253
9254#ifdef FEAT_FLOAT
9255
9256/*
9257 * round() is not in C90, use ceil() or floor() instead.
9258 */
9259 float_T
9260vim_round(float_T f)
9261{
9262 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
9263}
9264
9265/*
9266 * "round({float})" function
9267 */
9268 static void
9269f_round(typval_T *argvars, typval_T *rettv)
9270{
9271 float_T f = 0.0;
9272
9273 rettv->v_type = VAR_FLOAT;
9274 if (get_float_arg(argvars, &f) == OK)
9275 rettv->vval.v_float = vim_round(f);
9276 else
9277 rettv->vval.v_float = 0.0;
9278}
9279#endif
9280
9281/*
9282 * "screenattr()" function
9283 */
9284 static void
9285f_screenattr(typval_T *argvars, typval_T *rettv)
9286{
9287 int row;
9288 int col;
9289 int c;
9290
9291 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9292 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9293 if (row < 0 || row >= screen_Rows
9294 || col < 0 || col >= screen_Columns)
9295 c = -1;
9296 else
9297 c = ScreenAttrs[LineOffset[row] + col];
9298 rettv->vval.v_number = c;
9299}
9300
9301/*
9302 * "screenchar()" function
9303 */
9304 static void
9305f_screenchar(typval_T *argvars, typval_T *rettv)
9306{
9307 int row;
9308 int col;
9309 int off;
9310 int c;
9311
9312 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9313 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9314 if (row < 0 || row >= screen_Rows
9315 || col < 0 || col >= screen_Columns)
9316 c = -1;
9317 else
9318 {
9319 off = LineOffset[row] + col;
9320#ifdef FEAT_MBYTE
9321 if (enc_utf8 && ScreenLinesUC[off] != 0)
9322 c = ScreenLinesUC[off];
9323 else
9324#endif
9325 c = ScreenLines[off];
9326 }
9327 rettv->vval.v_number = c;
9328}
9329
9330/*
9331 * "screencol()" function
9332 *
9333 * First column is 1 to be consistent with virtcol().
9334 */
9335 static void
9336f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
9337{
9338 rettv->vval.v_number = screen_screencol() + 1;
9339}
9340
9341/*
9342 * "screenrow()" function
9343 */
9344 static void
9345f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
9346{
9347 rettv->vval.v_number = screen_screenrow() + 1;
9348}
9349
9350/*
9351 * "search()" function
9352 */
9353 static void
9354f_search(typval_T *argvars, typval_T *rettv)
9355{
9356 int flags = 0;
9357
9358 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
9359}
9360
9361/*
9362 * "searchdecl()" function
9363 */
9364 static void
9365f_searchdecl(typval_T *argvars, typval_T *rettv)
9366{
9367 int locally = 1;
9368 int thisblock = 0;
9369 int error = FALSE;
9370 char_u *name;
9371
9372 rettv->vval.v_number = 1; /* default: FAIL */
9373
9374 name = get_tv_string_chk(&argvars[0]);
9375 if (argvars[1].v_type != VAR_UNKNOWN)
9376 {
9377 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
9378 if (!error && argvars[2].v_type != VAR_UNKNOWN)
9379 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
9380 }
9381 if (!error && name != NULL)
9382 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
9383 locally, thisblock, SEARCH_KEEP) == FAIL;
9384}
9385
9386/*
9387 * Used by searchpair() and searchpairpos()
9388 */
9389 static int
9390searchpair_cmn(typval_T *argvars, pos_T *match_pos)
9391{
9392 char_u *spat, *mpat, *epat;
9393 char_u *skip;
9394 int save_p_ws = p_ws;
9395 int dir;
9396 int flags = 0;
9397 char_u nbuf1[NUMBUFLEN];
9398 char_u nbuf2[NUMBUFLEN];
9399 char_u nbuf3[NUMBUFLEN];
9400 int retval = 0; /* default: FAIL */
9401 long lnum_stop = 0;
9402 long time_limit = 0;
9403
9404 /* Get the three pattern arguments: start, middle, end. */
9405 spat = get_tv_string_chk(&argvars[0]);
9406 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
9407 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
9408 if (spat == NULL || mpat == NULL || epat == NULL)
9409 goto theend; /* type error */
9410
9411 /* Handle the optional fourth argument: flags */
9412 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
9413 if (dir == 0)
9414 goto theend;
9415
9416 /* Don't accept SP_END or SP_SUBPAT.
9417 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
9418 */
9419 if ((flags & (SP_END | SP_SUBPAT)) != 0
9420 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9421 {
9422 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
9423 goto theend;
9424 }
9425
9426 /* Using 'r' implies 'W', otherwise it doesn't work. */
9427 if (flags & SP_REPEAT)
9428 p_ws = FALSE;
9429
9430 /* Optional fifth argument: skip expression */
9431 if (argvars[3].v_type == VAR_UNKNOWN
9432 || argvars[4].v_type == VAR_UNKNOWN)
9433 skip = (char_u *)"";
9434 else
9435 {
9436 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
9437 if (argvars[5].v_type != VAR_UNKNOWN)
9438 {
9439 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
9440 if (lnum_stop < 0)
9441 goto theend;
9442#ifdef FEAT_RELTIME
9443 if (argvars[6].v_type != VAR_UNKNOWN)
9444 {
9445 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
9446 if (time_limit < 0)
9447 goto theend;
9448 }
9449#endif
9450 }
9451 }
9452 if (skip == NULL)
9453 goto theend; /* type error */
9454
9455 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
9456 match_pos, lnum_stop, time_limit);
9457
9458theend:
9459 p_ws = save_p_ws;
9460
9461 return retval;
9462}
9463
9464/*
9465 * "searchpair()" function
9466 */
9467 static void
9468f_searchpair(typval_T *argvars, typval_T *rettv)
9469{
9470 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
9471}
9472
9473/*
9474 * "searchpairpos()" function
9475 */
9476 static void
9477f_searchpairpos(typval_T *argvars, typval_T *rettv)
9478{
9479 pos_T match_pos;
9480 int lnum = 0;
9481 int col = 0;
9482
9483 if (rettv_list_alloc(rettv) == FAIL)
9484 return;
9485
9486 if (searchpair_cmn(argvars, &match_pos) > 0)
9487 {
9488 lnum = match_pos.lnum;
9489 col = match_pos.col;
9490 }
9491
9492 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9493 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9494}
9495
9496/*
9497 * Search for a start/middle/end thing.
9498 * Used by searchpair(), see its documentation for the details.
9499 * Returns 0 or -1 for no match,
9500 */
9501 long
9502do_searchpair(
9503 char_u *spat, /* start pattern */
9504 char_u *mpat, /* middle pattern */
9505 char_u *epat, /* end pattern */
9506 int dir, /* BACKWARD or FORWARD */
9507 char_u *skip, /* skip expression */
9508 int flags, /* SP_SETPCMARK and other SP_ values */
9509 pos_T *match_pos,
9510 linenr_T lnum_stop, /* stop at this line if not zero */
9511 long time_limit UNUSED) /* stop after this many msec */
9512{
9513 char_u *save_cpo;
9514 char_u *pat, *pat2 = NULL, *pat3 = NULL;
9515 long retval = 0;
9516 pos_T pos;
9517 pos_T firstpos;
9518 pos_T foundpos;
9519 pos_T save_cursor;
9520 pos_T save_pos;
9521 int n;
9522 int r;
9523 int nest = 1;
9524 int err;
9525 int options = SEARCH_KEEP;
9526 proftime_T tm;
9527
9528 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
9529 save_cpo = p_cpo;
9530 p_cpo = empty_option;
9531
9532#ifdef FEAT_RELTIME
9533 /* Set the time limit, if there is one. */
9534 profile_setlimit(time_limit, &tm);
9535#endif
9536
9537 /* Make two search patterns: start/end (pat2, for in nested pairs) and
9538 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009539 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
9540 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009541 if (pat2 == NULL || pat3 == NULL)
9542 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009543 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009544 if (*mpat == NUL)
9545 STRCPY(pat3, pat2);
9546 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009547 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009548 spat, epat, mpat);
9549 if (flags & SP_START)
9550 options |= SEARCH_START;
9551
9552 save_cursor = curwin->w_cursor;
9553 pos = curwin->w_cursor;
9554 clearpos(&firstpos);
9555 clearpos(&foundpos);
9556 pat = pat3;
9557 for (;;)
9558 {
9559 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
9560 options, RE_SEARCH, lnum_stop, &tm);
9561 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
9562 /* didn't find it or found the first match again: FAIL */
9563 break;
9564
9565 if (firstpos.lnum == 0)
9566 firstpos = pos;
9567 if (equalpos(pos, foundpos))
9568 {
9569 /* Found the same position again. Can happen with a pattern that
9570 * has "\zs" at the end and searching backwards. Advance one
9571 * character and try again. */
9572 if (dir == BACKWARD)
9573 decl(&pos);
9574 else
9575 incl(&pos);
9576 }
9577 foundpos = pos;
9578
9579 /* clear the start flag to avoid getting stuck here */
9580 options &= ~SEARCH_START;
9581
9582 /* If the skip pattern matches, ignore this match. */
9583 if (*skip != NUL)
9584 {
9585 save_pos = curwin->w_cursor;
9586 curwin->w_cursor = pos;
9587 r = eval_to_bool(skip, &err, NULL, FALSE);
9588 curwin->w_cursor = save_pos;
9589 if (err)
9590 {
9591 /* Evaluating {skip} caused an error, break here. */
9592 curwin->w_cursor = save_cursor;
9593 retval = -1;
9594 break;
9595 }
9596 if (r)
9597 continue;
9598 }
9599
9600 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
9601 {
9602 /* Found end when searching backwards or start when searching
9603 * forward: nested pair. */
9604 ++nest;
9605 pat = pat2; /* nested, don't search for middle */
9606 }
9607 else
9608 {
9609 /* Found end when searching forward or start when searching
9610 * backward: end of (nested) pair; or found middle in outer pair. */
9611 if (--nest == 1)
9612 pat = pat3; /* outer level, search for middle */
9613 }
9614
9615 if (nest == 0)
9616 {
9617 /* Found the match: return matchcount or line number. */
9618 if (flags & SP_RETCOUNT)
9619 ++retval;
9620 else
9621 retval = pos.lnum;
9622 if (flags & SP_SETPCMARK)
9623 setpcmark();
9624 curwin->w_cursor = pos;
9625 if (!(flags & SP_REPEAT))
9626 break;
9627 nest = 1; /* search for next unmatched */
9628 }
9629 }
9630
9631 if (match_pos != NULL)
9632 {
9633 /* Store the match cursor position */
9634 match_pos->lnum = curwin->w_cursor.lnum;
9635 match_pos->col = curwin->w_cursor.col + 1;
9636 }
9637
9638 /* If 'n' flag is used or search failed: restore cursor position. */
9639 if ((flags & SP_NOMOVE) || retval == 0)
9640 curwin->w_cursor = save_cursor;
9641
9642theend:
9643 vim_free(pat2);
9644 vim_free(pat3);
9645 if (p_cpo == empty_option)
9646 p_cpo = save_cpo;
9647 else
9648 /* Darn, evaluating the {skip} expression changed the value. */
9649 free_string_option(save_cpo);
9650
9651 return retval;
9652}
9653
9654/*
9655 * "searchpos()" function
9656 */
9657 static void
9658f_searchpos(typval_T *argvars, typval_T *rettv)
9659{
9660 pos_T match_pos;
9661 int lnum = 0;
9662 int col = 0;
9663 int n;
9664 int flags = 0;
9665
9666 if (rettv_list_alloc(rettv) == FAIL)
9667 return;
9668
9669 n = search_cmn(argvars, &match_pos, &flags);
9670 if (n > 0)
9671 {
9672 lnum = match_pos.lnum;
9673 col = match_pos.col;
9674 }
9675
9676 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9677 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9678 if (flags & SP_SUBPAT)
9679 list_append_number(rettv->vval.v_list, (varnumber_T)n);
9680}
9681
9682 static void
9683f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
9684{
9685#ifdef FEAT_CLIENTSERVER
9686 char_u buf[NUMBUFLEN];
9687 char_u *server = get_tv_string_chk(&argvars[0]);
9688 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
9689
9690 rettv->vval.v_number = -1;
9691 if (server == NULL || reply == NULL)
9692 return;
9693 if (check_restricted() || check_secure())
9694 return;
9695# ifdef FEAT_X11
9696 if (check_connection() == FAIL)
9697 return;
9698# endif
9699
9700 if (serverSendReply(server, reply) < 0)
9701 {
9702 EMSG(_("E258: Unable to send to client"));
9703 return;
9704 }
9705 rettv->vval.v_number = 0;
9706#else
9707 rettv->vval.v_number = -1;
9708#endif
9709}
9710
9711 static void
9712f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
9713{
9714 char_u *r = NULL;
9715
9716#ifdef FEAT_CLIENTSERVER
9717# ifdef WIN32
9718 r = serverGetVimNames();
9719# else
9720 make_connection();
9721 if (X_DISPLAY != NULL)
9722 r = serverGetVimNames(X_DISPLAY);
9723# endif
9724#endif
9725 rettv->v_type = VAR_STRING;
9726 rettv->vval.v_string = r;
9727}
9728
9729/*
9730 * "setbufvar()" function
9731 */
9732 static void
9733f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
9734{
9735 buf_T *buf;
9736 char_u *varname, *bufvarname;
9737 typval_T *varp;
9738 char_u nbuf[NUMBUFLEN];
9739
9740 if (check_restricted() || check_secure())
9741 return;
9742 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9743 varname = get_tv_string_chk(&argvars[1]);
9744 buf = get_buf_tv(&argvars[0], FALSE);
9745 varp = &argvars[2];
9746
9747 if (buf != NULL && varname != NULL && varp != NULL)
9748 {
9749 if (*varname == '&')
9750 {
9751 long numval;
9752 char_u *strval;
9753 int error = FALSE;
9754 aco_save_T aco;
9755
9756 /* set curbuf to be our buf, temporarily */
9757 aucmd_prepbuf(&aco, buf);
9758
9759 ++varname;
9760 numval = (long)get_tv_number_chk(varp, &error);
9761 strval = get_tv_string_buf_chk(varp, nbuf);
9762 if (!error && strval != NULL)
9763 set_option_value(varname, numval, strval, OPT_LOCAL);
9764
9765 /* reset notion of buffer */
9766 aucmd_restbuf(&aco);
9767 }
9768 else
9769 {
9770 buf_T *save_curbuf = curbuf;
9771
9772 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
9773 if (bufvarname != NULL)
9774 {
9775 curbuf = buf;
9776 STRCPY(bufvarname, "b:");
9777 STRCPY(bufvarname + 2, varname);
9778 set_var(bufvarname, varp, TRUE);
9779 vim_free(bufvarname);
9780 curbuf = save_curbuf;
9781 }
9782 }
9783 }
9784}
9785
9786 static void
9787f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
9788{
9789 dict_T *d;
9790 dictitem_T *di;
9791 char_u *csearch;
9792
9793 if (argvars[0].v_type != VAR_DICT)
9794 {
9795 EMSG(_(e_dictreq));
9796 return;
9797 }
9798
9799 if ((d = argvars[0].vval.v_dict) != NULL)
9800 {
9801 csearch = get_dict_string(d, (char_u *)"char", FALSE);
9802 if (csearch != NULL)
9803 {
9804#ifdef FEAT_MBYTE
9805 if (enc_utf8)
9806 {
9807 int pcc[MAX_MCO];
9808 int c = utfc_ptr2char(csearch, pcc);
9809
9810 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
9811 }
9812 else
9813#endif
9814 set_last_csearch(PTR2CHAR(csearch),
9815 csearch, MB_PTR2LEN(csearch));
9816 }
9817
9818 di = dict_find(d, (char_u *)"forward", -1);
9819 if (di != NULL)
9820 set_csearch_direction((int)get_tv_number(&di->di_tv)
9821 ? FORWARD : BACKWARD);
9822
9823 di = dict_find(d, (char_u *)"until", -1);
9824 if (di != NULL)
9825 set_csearch_until(!!get_tv_number(&di->di_tv));
9826 }
9827}
9828
9829/*
9830 * "setcmdpos()" function
9831 */
9832 static void
9833f_setcmdpos(typval_T *argvars, typval_T *rettv)
9834{
9835 int pos = (int)get_tv_number(&argvars[0]) - 1;
9836
9837 if (pos >= 0)
9838 rettv->vval.v_number = set_cmdline_pos(pos);
9839}
9840
9841/*
9842 * "setfperm({fname}, {mode})" function
9843 */
9844 static void
9845f_setfperm(typval_T *argvars, typval_T *rettv)
9846{
9847 char_u *fname;
9848 char_u modebuf[NUMBUFLEN];
9849 char_u *mode_str;
9850 int i;
9851 int mask;
9852 int mode = 0;
9853
9854 rettv->vval.v_number = 0;
9855 fname = get_tv_string_chk(&argvars[0]);
9856 if (fname == NULL)
9857 return;
9858 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
9859 if (mode_str == NULL)
9860 return;
9861 if (STRLEN(mode_str) != 9)
9862 {
9863 EMSG2(_(e_invarg2), mode_str);
9864 return;
9865 }
9866
9867 mask = 1;
9868 for (i = 8; i >= 0; --i)
9869 {
9870 if (mode_str[i] != '-')
9871 mode |= mask;
9872 mask = mask << 1;
9873 }
9874 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
9875}
9876
9877/*
9878 * "setline()" function
9879 */
9880 static void
9881f_setline(typval_T *argvars, typval_T *rettv)
9882{
9883 linenr_T lnum;
9884 char_u *line = NULL;
9885 list_T *l = NULL;
9886 listitem_T *li = NULL;
9887 long added = 0;
9888 linenr_T lcount = curbuf->b_ml.ml_line_count;
9889
9890 lnum = get_tv_lnum(&argvars[0]);
9891 if (argvars[1].v_type == VAR_LIST)
9892 {
9893 l = argvars[1].vval.v_list;
9894 li = l->lv_first;
9895 }
9896 else
9897 line = get_tv_string_chk(&argvars[1]);
9898
9899 /* default result is zero == OK */
9900 for (;;)
9901 {
9902 if (l != NULL)
9903 {
9904 /* list argument, get next string */
9905 if (li == NULL)
9906 break;
9907 line = get_tv_string_chk(&li->li_tv);
9908 li = li->li_next;
9909 }
9910
9911 rettv->vval.v_number = 1; /* FAIL */
9912 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
9913 break;
9914
9915 /* When coming here from Insert mode, sync undo, so that this can be
9916 * undone separately from what was previously inserted. */
9917 if (u_sync_once == 2)
9918 {
9919 u_sync_once = 1; /* notify that u_sync() was called */
9920 u_sync(TRUE);
9921 }
9922
9923 if (lnum <= curbuf->b_ml.ml_line_count)
9924 {
9925 /* existing line, replace it */
9926 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
9927 {
9928 changed_bytes(lnum, 0);
9929 if (lnum == curwin->w_cursor.lnum)
9930 check_cursor_col();
9931 rettv->vval.v_number = 0; /* OK */
9932 }
9933 }
9934 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
9935 {
9936 /* lnum is one past the last line, append the line */
9937 ++added;
9938 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
9939 rettv->vval.v_number = 0; /* OK */
9940 }
9941
9942 if (l == NULL) /* only one string argument */
9943 break;
9944 ++lnum;
9945 }
9946
9947 if (added > 0)
9948 appended_lines_mark(lcount, added);
9949}
9950
Bram Moolenaard823fa92016-08-12 16:29:27 +02009951static 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 +02009952
9953/*
9954 * Used by "setqflist()" and "setloclist()" functions
9955 */
9956 static void
9957set_qf_ll_list(
9958 win_T *wp UNUSED,
9959 typval_T *list_arg UNUSED,
9960 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +02009961 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009962 typval_T *rettv)
9963{
9964#ifdef FEAT_QUICKFIX
9965 static char *e_invact = N_("E927: Invalid action: '%s'");
9966 char_u *act;
9967 int action = 0;
9968#endif
9969
9970 rettv->vval.v_number = -1;
9971
9972#ifdef FEAT_QUICKFIX
9973 if (list_arg->v_type != VAR_LIST)
9974 EMSG(_(e_listreq));
9975 else
9976 {
9977 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +02009978 dict_T *d = NULL;
9979 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009980
9981 if (action_arg->v_type == VAR_STRING)
9982 {
9983 act = get_tv_string_chk(action_arg);
9984 if (act == NULL)
9985 return; /* type error; errmsg already given */
9986 if ((*act == 'a' || *act == 'r' || *act == ' ') && act[1] == NUL)
9987 action = *act;
9988 else
9989 EMSG2(_(e_invact), act);
9990 }
9991 else if (action_arg->v_type == VAR_UNKNOWN)
9992 action = ' ';
9993 else
9994 EMSG(_(e_stringreq));
9995
Bram Moolenaard823fa92016-08-12 16:29:27 +02009996 if (action_arg->v_type != VAR_UNKNOWN
9997 && what_arg->v_type != VAR_UNKNOWN)
9998 {
9999 if (what_arg->v_type == VAR_DICT)
10000 d = what_arg->vval.v_dict;
10001 else
10002 {
10003 EMSG(_(e_dictreq));
10004 valid_dict = FALSE;
10005 }
10006 }
10007
10008 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
10009 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()"), d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010010 rettv->vval.v_number = 0;
10011 }
10012#endif
10013}
10014
10015/*
10016 * "setloclist()" function
10017 */
10018 static void
10019f_setloclist(typval_T *argvars, typval_T *rettv)
10020{
10021 win_T *win;
10022
10023 rettv->vval.v_number = -1;
10024
10025 win = find_win_by_nr(&argvars[0], NULL);
10026 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020010027 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010028}
10029
10030/*
10031 * "setmatches()" function
10032 */
10033 static void
10034f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10035{
10036#ifdef FEAT_SEARCH_EXTRA
10037 list_T *l;
10038 listitem_T *li;
10039 dict_T *d;
10040 list_T *s = NULL;
10041
10042 rettv->vval.v_number = -1;
10043 if (argvars[0].v_type != VAR_LIST)
10044 {
10045 EMSG(_(e_listreq));
10046 return;
10047 }
10048 if ((l = argvars[0].vval.v_list) != NULL)
10049 {
10050
10051 /* To some extent make sure that we are dealing with a list from
10052 * "getmatches()". */
10053 li = l->lv_first;
10054 while (li != NULL)
10055 {
10056 if (li->li_tv.v_type != VAR_DICT
10057 || (d = li->li_tv.vval.v_dict) == NULL)
10058 {
10059 EMSG(_(e_invarg));
10060 return;
10061 }
10062 if (!(dict_find(d, (char_u *)"group", -1) != NULL
10063 && (dict_find(d, (char_u *)"pattern", -1) != NULL
10064 || dict_find(d, (char_u *)"pos1", -1) != NULL)
10065 && dict_find(d, (char_u *)"priority", -1) != NULL
10066 && dict_find(d, (char_u *)"id", -1) != NULL))
10067 {
10068 EMSG(_(e_invarg));
10069 return;
10070 }
10071 li = li->li_next;
10072 }
10073
10074 clear_matches(curwin);
10075 li = l->lv_first;
10076 while (li != NULL)
10077 {
10078 int i = 0;
10079 char_u buf[5];
10080 dictitem_T *di;
10081 char_u *group;
10082 int priority;
10083 int id;
10084 char_u *conceal;
10085
10086 d = li->li_tv.vval.v_dict;
10087 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
10088 {
10089 if (s == NULL)
10090 {
10091 s = list_alloc();
10092 if (s == NULL)
10093 return;
10094 }
10095
10096 /* match from matchaddpos() */
10097 for (i = 1; i < 9; i++)
10098 {
10099 sprintf((char *)buf, (char *)"pos%d", i);
10100 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
10101 {
10102 if (di->di_tv.v_type != VAR_LIST)
10103 return;
10104
10105 list_append_tv(s, &di->di_tv);
10106 s->lv_refcount++;
10107 }
10108 else
10109 break;
10110 }
10111 }
10112
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010113 group = get_dict_string(d, (char_u *)"group", TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010114 priority = (int)get_dict_number(d, (char_u *)"priority");
10115 id = (int)get_dict_number(d, (char_u *)"id");
10116 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010117 ? get_dict_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010118 : NULL;
10119 if (i == 0)
10120 {
10121 match_add(curwin, group,
10122 get_dict_string(d, (char_u *)"pattern", FALSE),
10123 priority, id, NULL, conceal);
10124 }
10125 else
10126 {
10127 match_add(curwin, group, NULL, priority, id, s, conceal);
10128 list_unref(s);
10129 s = NULL;
10130 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010131 vim_free(group);
10132 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010133
10134 li = li->li_next;
10135 }
10136 rettv->vval.v_number = 0;
10137 }
10138#endif
10139}
10140
10141/*
10142 * "setpos()" function
10143 */
10144 static void
10145f_setpos(typval_T *argvars, typval_T *rettv)
10146{
10147 pos_T pos;
10148 int fnum;
10149 char_u *name;
10150 colnr_T curswant = -1;
10151
10152 rettv->vval.v_number = -1;
10153 name = get_tv_string_chk(argvars);
10154 if (name != NULL)
10155 {
10156 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10157 {
10158 if (--pos.col < 0)
10159 pos.col = 0;
10160 if (name[0] == '.' && name[1] == NUL)
10161 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010162 /* set cursor; "fnum" is ignored */
10163 curwin->w_cursor = pos;
10164 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010165 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010166 curwin->w_curswant = curswant - 1;
10167 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010168 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010169 check_cursor();
10170 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010171 }
10172 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10173 {
10174 /* set mark */
10175 if (setmark_pos(name[1], &pos, fnum) == OK)
10176 rettv->vval.v_number = 0;
10177 }
10178 else
10179 EMSG(_(e_invarg));
10180 }
10181 }
10182}
10183
10184/*
10185 * "setqflist()" function
10186 */
10187 static void
10188f_setqflist(typval_T *argvars, typval_T *rettv)
10189{
Bram Moolenaard823fa92016-08-12 16:29:27 +020010190 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010191}
10192
10193/*
10194 * "setreg()" function
10195 */
10196 static void
10197f_setreg(typval_T *argvars, typval_T *rettv)
10198{
10199 int regname;
10200 char_u *strregname;
10201 char_u *stropt;
10202 char_u *strval;
10203 int append;
10204 char_u yank_type;
10205 long block_len;
10206
10207 block_len = -1;
10208 yank_type = MAUTO;
10209 append = FALSE;
10210
10211 strregname = get_tv_string_chk(argvars);
10212 rettv->vval.v_number = 1; /* FAIL is default */
10213
10214 if (strregname == NULL)
10215 return; /* type error; errmsg already given */
10216 regname = *strregname;
10217 if (regname == 0 || regname == '@')
10218 regname = '"';
10219
10220 if (argvars[2].v_type != VAR_UNKNOWN)
10221 {
10222 stropt = get_tv_string_chk(&argvars[2]);
10223 if (stropt == NULL)
10224 return; /* type error */
10225 for (; *stropt != NUL; ++stropt)
10226 switch (*stropt)
10227 {
10228 case 'a': case 'A': /* append */
10229 append = TRUE;
10230 break;
10231 case 'v': case 'c': /* character-wise selection */
10232 yank_type = MCHAR;
10233 break;
10234 case 'V': case 'l': /* line-wise selection */
10235 yank_type = MLINE;
10236 break;
10237 case 'b': case Ctrl_V: /* block-wise selection */
10238 yank_type = MBLOCK;
10239 if (VIM_ISDIGIT(stropt[1]))
10240 {
10241 ++stropt;
10242 block_len = getdigits(&stropt) - 1;
10243 --stropt;
10244 }
10245 break;
10246 }
10247 }
10248
10249 if (argvars[1].v_type == VAR_LIST)
10250 {
10251 char_u **lstval;
10252 char_u **allocval;
10253 char_u buf[NUMBUFLEN];
10254 char_u **curval;
10255 char_u **curallocval;
10256 list_T *ll = argvars[1].vval.v_list;
10257 listitem_T *li;
10258 int len;
10259
10260 /* If the list is NULL handle like an empty list. */
10261 len = ll == NULL ? 0 : ll->lv_len;
10262
10263 /* First half: use for pointers to result lines; second half: use for
10264 * pointers to allocated copies. */
10265 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
10266 if (lstval == NULL)
10267 return;
10268 curval = lstval;
10269 allocval = lstval + len + 2;
10270 curallocval = allocval;
10271
10272 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
10273 li = li->li_next)
10274 {
10275 strval = get_tv_string_buf_chk(&li->li_tv, buf);
10276 if (strval == NULL)
10277 goto free_lstval;
10278 if (strval == buf)
10279 {
10280 /* Need to make a copy, next get_tv_string_buf_chk() will
10281 * overwrite the string. */
10282 strval = vim_strsave(buf);
10283 if (strval == NULL)
10284 goto free_lstval;
10285 *curallocval++ = strval;
10286 }
10287 *curval++ = strval;
10288 }
10289 *curval++ = NULL;
10290
10291 write_reg_contents_lst(regname, lstval, -1,
10292 append, yank_type, block_len);
10293free_lstval:
10294 while (curallocval > allocval)
10295 vim_free(*--curallocval);
10296 vim_free(lstval);
10297 }
10298 else
10299 {
10300 strval = get_tv_string_chk(&argvars[1]);
10301 if (strval == NULL)
10302 return;
10303 write_reg_contents_ex(regname, strval, -1,
10304 append, yank_type, block_len);
10305 }
10306 rettv->vval.v_number = 0;
10307}
10308
10309/*
10310 * "settabvar()" function
10311 */
10312 static void
10313f_settabvar(typval_T *argvars, typval_T *rettv)
10314{
10315#ifdef FEAT_WINDOWS
10316 tabpage_T *save_curtab;
10317 tabpage_T *tp;
10318#endif
10319 char_u *varname, *tabvarname;
10320 typval_T *varp;
10321
10322 rettv->vval.v_number = 0;
10323
10324 if (check_restricted() || check_secure())
10325 return;
10326
10327#ifdef FEAT_WINDOWS
10328 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
10329#endif
10330 varname = get_tv_string_chk(&argvars[1]);
10331 varp = &argvars[2];
10332
10333 if (varname != NULL && varp != NULL
10334#ifdef FEAT_WINDOWS
10335 && tp != NULL
10336#endif
10337 )
10338 {
10339#ifdef FEAT_WINDOWS
10340 save_curtab = curtab;
10341 goto_tabpage_tp(tp, FALSE, FALSE);
10342#endif
10343
10344 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
10345 if (tabvarname != NULL)
10346 {
10347 STRCPY(tabvarname, "t:");
10348 STRCPY(tabvarname + 2, varname);
10349 set_var(tabvarname, varp, TRUE);
10350 vim_free(tabvarname);
10351 }
10352
10353#ifdef FEAT_WINDOWS
10354 /* Restore current tabpage */
10355 if (valid_tabpage(save_curtab))
10356 goto_tabpage_tp(save_curtab, FALSE, FALSE);
10357#endif
10358 }
10359}
10360
10361/*
10362 * "settabwinvar()" function
10363 */
10364 static void
10365f_settabwinvar(typval_T *argvars, typval_T *rettv)
10366{
10367 setwinvar(argvars, rettv, 1);
10368}
10369
10370/*
10371 * "setwinvar()" function
10372 */
10373 static void
10374f_setwinvar(typval_T *argvars, typval_T *rettv)
10375{
10376 setwinvar(argvars, rettv, 0);
10377}
10378
10379#ifdef FEAT_CRYPT
10380/*
10381 * "sha256({string})" function
10382 */
10383 static void
10384f_sha256(typval_T *argvars, typval_T *rettv)
10385{
10386 char_u *p;
10387
10388 p = get_tv_string(&argvars[0]);
10389 rettv->vval.v_string = vim_strsave(
10390 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
10391 rettv->v_type = VAR_STRING;
10392}
10393#endif /* FEAT_CRYPT */
10394
10395/*
10396 * "shellescape({string})" function
10397 */
10398 static void
10399f_shellescape(typval_T *argvars, typval_T *rettv)
10400{
10401 rettv->vval.v_string = vim_strsave_shellescape(
10402 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
10403 rettv->v_type = VAR_STRING;
10404}
10405
10406/*
10407 * shiftwidth() function
10408 */
10409 static void
10410f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
10411{
10412 rettv->vval.v_number = get_sw_value(curbuf);
10413}
10414
10415/*
10416 * "simplify()" function
10417 */
10418 static void
10419f_simplify(typval_T *argvars, typval_T *rettv)
10420{
10421 char_u *p;
10422
10423 p = get_tv_string(&argvars[0]);
10424 rettv->vval.v_string = vim_strsave(p);
10425 simplify_filename(rettv->vval.v_string); /* simplify in place */
10426 rettv->v_type = VAR_STRING;
10427}
10428
10429#ifdef FEAT_FLOAT
10430/*
10431 * "sin()" function
10432 */
10433 static void
10434f_sin(typval_T *argvars, typval_T *rettv)
10435{
10436 float_T f = 0.0;
10437
10438 rettv->v_type = VAR_FLOAT;
10439 if (get_float_arg(argvars, &f) == OK)
10440 rettv->vval.v_float = sin(f);
10441 else
10442 rettv->vval.v_float = 0.0;
10443}
10444
10445/*
10446 * "sinh()" function
10447 */
10448 static void
10449f_sinh(typval_T *argvars, typval_T *rettv)
10450{
10451 float_T f = 0.0;
10452
10453 rettv->v_type = VAR_FLOAT;
10454 if (get_float_arg(argvars, &f) == OK)
10455 rettv->vval.v_float = sinh(f);
10456 else
10457 rettv->vval.v_float = 0.0;
10458}
10459#endif
10460
10461static int
10462#ifdef __BORLANDC__
10463 _RTLENTRYF
10464#endif
10465 item_compare(const void *s1, const void *s2);
10466static int
10467#ifdef __BORLANDC__
10468 _RTLENTRYF
10469#endif
10470 item_compare2(const void *s1, const void *s2);
10471
10472/* struct used in the array that's given to qsort() */
10473typedef struct
10474{
10475 listitem_T *item;
10476 int idx;
10477} sortItem_T;
10478
10479/* struct storing information about current sort */
10480typedef struct
10481{
10482 int item_compare_ic;
10483 int item_compare_numeric;
10484 int item_compare_numbers;
10485#ifdef FEAT_FLOAT
10486 int item_compare_float;
10487#endif
10488 char_u *item_compare_func;
10489 partial_T *item_compare_partial;
10490 dict_T *item_compare_selfdict;
10491 int item_compare_func_err;
10492 int item_compare_keep_zero;
10493} sortinfo_T;
10494static sortinfo_T *sortinfo = NULL;
10495static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
10496#define ITEM_COMPARE_FAIL 999
10497
10498/*
10499 * Compare functions for f_sort() and f_uniq() below.
10500 */
10501 static int
10502#ifdef __BORLANDC__
10503_RTLENTRYF
10504#endif
10505item_compare(const void *s1, const void *s2)
10506{
10507 sortItem_T *si1, *si2;
10508 typval_T *tv1, *tv2;
10509 char_u *p1, *p2;
10510 char_u *tofree1 = NULL, *tofree2 = NULL;
10511 int res;
10512 char_u numbuf1[NUMBUFLEN];
10513 char_u numbuf2[NUMBUFLEN];
10514
10515 si1 = (sortItem_T *)s1;
10516 si2 = (sortItem_T *)s2;
10517 tv1 = &si1->item->li_tv;
10518 tv2 = &si2->item->li_tv;
10519
10520 if (sortinfo->item_compare_numbers)
10521 {
10522 varnumber_T v1 = get_tv_number(tv1);
10523 varnumber_T v2 = get_tv_number(tv2);
10524
10525 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10526 }
10527
10528#ifdef FEAT_FLOAT
10529 if (sortinfo->item_compare_float)
10530 {
10531 float_T v1 = get_tv_float(tv1);
10532 float_T v2 = get_tv_float(tv2);
10533
10534 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10535 }
10536#endif
10537
10538 /* tv2string() puts quotes around a string and allocates memory. Don't do
10539 * that for string variables. Use a single quote when comparing with a
10540 * non-string to do what the docs promise. */
10541 if (tv1->v_type == VAR_STRING)
10542 {
10543 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
10544 p1 = (char_u *)"'";
10545 else
10546 p1 = tv1->vval.v_string;
10547 }
10548 else
10549 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
10550 if (tv2->v_type == VAR_STRING)
10551 {
10552 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
10553 p2 = (char_u *)"'";
10554 else
10555 p2 = tv2->vval.v_string;
10556 }
10557 else
10558 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
10559 if (p1 == NULL)
10560 p1 = (char_u *)"";
10561 if (p2 == NULL)
10562 p2 = (char_u *)"";
10563 if (!sortinfo->item_compare_numeric)
10564 {
10565 if (sortinfo->item_compare_ic)
10566 res = STRICMP(p1, p2);
10567 else
10568 res = STRCMP(p1, p2);
10569 }
10570 else
10571 {
10572 double n1, n2;
10573 n1 = strtod((char *)p1, (char **)&p1);
10574 n2 = strtod((char *)p2, (char **)&p2);
10575 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
10576 }
10577
10578 /* When the result would be zero, compare the item indexes. Makes the
10579 * sort stable. */
10580 if (res == 0 && !sortinfo->item_compare_keep_zero)
10581 res = si1->idx > si2->idx ? 1 : -1;
10582
10583 vim_free(tofree1);
10584 vim_free(tofree2);
10585 return res;
10586}
10587
10588 static int
10589#ifdef __BORLANDC__
10590_RTLENTRYF
10591#endif
10592item_compare2(const void *s1, const void *s2)
10593{
10594 sortItem_T *si1, *si2;
10595 int res;
10596 typval_T rettv;
10597 typval_T argv[3];
10598 int dummy;
10599 char_u *func_name;
10600 partial_T *partial = sortinfo->item_compare_partial;
10601
10602 /* shortcut after failure in previous call; compare all items equal */
10603 if (sortinfo->item_compare_func_err)
10604 return 0;
10605
10606 si1 = (sortItem_T *)s1;
10607 si2 = (sortItem_T *)s2;
10608
10609 if (partial == NULL)
10610 func_name = sortinfo->item_compare_func;
10611 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020010612 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010613
10614 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
10615 * in the copy without changing the original list items. */
10616 copy_tv(&si1->item->li_tv, &argv[0]);
10617 copy_tv(&si2->item->li_tv, &argv[1]);
10618
10619 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
10620 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020010621 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010622 partial, sortinfo->item_compare_selfdict);
10623 clear_tv(&argv[0]);
10624 clear_tv(&argv[1]);
10625
10626 if (res == FAIL)
10627 res = ITEM_COMPARE_FAIL;
10628 else
10629 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
10630 if (sortinfo->item_compare_func_err)
10631 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
10632 clear_tv(&rettv);
10633
10634 /* When the result would be zero, compare the pointers themselves. Makes
10635 * the sort stable. */
10636 if (res == 0 && !sortinfo->item_compare_keep_zero)
10637 res = si1->idx > si2->idx ? 1 : -1;
10638
10639 return res;
10640}
10641
10642/*
10643 * "sort({list})" function
10644 */
10645 static void
10646do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
10647{
10648 list_T *l;
10649 listitem_T *li;
10650 sortItem_T *ptrs;
10651 sortinfo_T *old_sortinfo;
10652 sortinfo_T info;
10653 long len;
10654 long i;
10655
10656 /* Pointer to current info struct used in compare function. Save and
10657 * restore the current one for nested calls. */
10658 old_sortinfo = sortinfo;
10659 sortinfo = &info;
10660
10661 if (argvars[0].v_type != VAR_LIST)
10662 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
10663 else
10664 {
10665 l = argvars[0].vval.v_list;
10666 if (l == NULL || tv_check_lock(l->lv_lock,
10667 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
10668 TRUE))
10669 goto theend;
10670 rettv->vval.v_list = l;
10671 rettv->v_type = VAR_LIST;
10672 ++l->lv_refcount;
10673
10674 len = list_len(l);
10675 if (len <= 1)
10676 goto theend; /* short list sorts pretty quickly */
10677
10678 info.item_compare_ic = FALSE;
10679 info.item_compare_numeric = FALSE;
10680 info.item_compare_numbers = FALSE;
10681#ifdef FEAT_FLOAT
10682 info.item_compare_float = FALSE;
10683#endif
10684 info.item_compare_func = NULL;
10685 info.item_compare_partial = NULL;
10686 info.item_compare_selfdict = NULL;
10687 if (argvars[1].v_type != VAR_UNKNOWN)
10688 {
10689 /* optional second argument: {func} */
10690 if (argvars[1].v_type == VAR_FUNC)
10691 info.item_compare_func = argvars[1].vval.v_string;
10692 else if (argvars[1].v_type == VAR_PARTIAL)
10693 info.item_compare_partial = argvars[1].vval.v_partial;
10694 else
10695 {
10696 int error = FALSE;
10697
10698 i = (long)get_tv_number_chk(&argvars[1], &error);
10699 if (error)
10700 goto theend; /* type error; errmsg already given */
10701 if (i == 1)
10702 info.item_compare_ic = TRUE;
10703 else if (argvars[1].v_type != VAR_NUMBER)
10704 info.item_compare_func = get_tv_string(&argvars[1]);
10705 else if (i != 0)
10706 {
10707 EMSG(_(e_invarg));
10708 goto theend;
10709 }
10710 if (info.item_compare_func != NULL)
10711 {
10712 if (*info.item_compare_func == NUL)
10713 {
10714 /* empty string means default sort */
10715 info.item_compare_func = NULL;
10716 }
10717 else if (STRCMP(info.item_compare_func, "n") == 0)
10718 {
10719 info.item_compare_func = NULL;
10720 info.item_compare_numeric = TRUE;
10721 }
10722 else if (STRCMP(info.item_compare_func, "N") == 0)
10723 {
10724 info.item_compare_func = NULL;
10725 info.item_compare_numbers = TRUE;
10726 }
10727#ifdef FEAT_FLOAT
10728 else if (STRCMP(info.item_compare_func, "f") == 0)
10729 {
10730 info.item_compare_func = NULL;
10731 info.item_compare_float = TRUE;
10732 }
10733#endif
10734 else if (STRCMP(info.item_compare_func, "i") == 0)
10735 {
10736 info.item_compare_func = NULL;
10737 info.item_compare_ic = TRUE;
10738 }
10739 }
10740 }
10741
10742 if (argvars[2].v_type != VAR_UNKNOWN)
10743 {
10744 /* optional third argument: {dict} */
10745 if (argvars[2].v_type != VAR_DICT)
10746 {
10747 EMSG(_(e_dictreq));
10748 goto theend;
10749 }
10750 info.item_compare_selfdict = argvars[2].vval.v_dict;
10751 }
10752 }
10753
10754 /* Make an array with each entry pointing to an item in the List. */
10755 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
10756 if (ptrs == NULL)
10757 goto theend;
10758
10759 i = 0;
10760 if (sort)
10761 {
10762 /* sort(): ptrs will be the list to sort */
10763 for (li = l->lv_first; li != NULL; li = li->li_next)
10764 {
10765 ptrs[i].item = li;
10766 ptrs[i].idx = i;
10767 ++i;
10768 }
10769
10770 info.item_compare_func_err = FALSE;
10771 info.item_compare_keep_zero = FALSE;
10772 /* test the compare function */
10773 if ((info.item_compare_func != NULL
10774 || info.item_compare_partial != NULL)
10775 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
10776 == ITEM_COMPARE_FAIL)
10777 EMSG(_("E702: Sort compare function failed"));
10778 else
10779 {
10780 /* Sort the array with item pointers. */
10781 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
10782 info.item_compare_func == NULL
10783 && info.item_compare_partial == NULL
10784 ? item_compare : item_compare2);
10785
10786 if (!info.item_compare_func_err)
10787 {
10788 /* Clear the List and append the items in sorted order. */
10789 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
10790 l->lv_len = 0;
10791 for (i = 0; i < len; ++i)
10792 list_append(l, ptrs[i].item);
10793 }
10794 }
10795 }
10796 else
10797 {
10798 int (*item_compare_func_ptr)(const void *, const void *);
10799
10800 /* f_uniq(): ptrs will be a stack of items to remove */
10801 info.item_compare_func_err = FALSE;
10802 info.item_compare_keep_zero = TRUE;
10803 item_compare_func_ptr = info.item_compare_func != NULL
10804 || info.item_compare_partial != NULL
10805 ? item_compare2 : item_compare;
10806
10807 for (li = l->lv_first; li != NULL && li->li_next != NULL;
10808 li = li->li_next)
10809 {
10810 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
10811 == 0)
10812 ptrs[i++].item = li;
10813 if (info.item_compare_func_err)
10814 {
10815 EMSG(_("E882: Uniq compare function failed"));
10816 break;
10817 }
10818 }
10819
10820 if (!info.item_compare_func_err)
10821 {
10822 while (--i >= 0)
10823 {
10824 li = ptrs[i].item->li_next;
10825 ptrs[i].item->li_next = li->li_next;
10826 if (li->li_next != NULL)
10827 li->li_next->li_prev = ptrs[i].item;
10828 else
10829 l->lv_last = ptrs[i].item;
10830 list_fix_watch(l, li);
10831 listitem_free(li);
10832 l->lv_len--;
10833 }
10834 }
10835 }
10836
10837 vim_free(ptrs);
10838 }
10839theend:
10840 sortinfo = old_sortinfo;
10841}
10842
10843/*
10844 * "sort({list})" function
10845 */
10846 static void
10847f_sort(typval_T *argvars, typval_T *rettv)
10848{
10849 do_sort_uniq(argvars, rettv, TRUE);
10850}
10851
10852/*
10853 * "uniq({list})" function
10854 */
10855 static void
10856f_uniq(typval_T *argvars, typval_T *rettv)
10857{
10858 do_sort_uniq(argvars, rettv, FALSE);
10859}
10860
10861/*
10862 * "soundfold({word})" function
10863 */
10864 static void
10865f_soundfold(typval_T *argvars, typval_T *rettv)
10866{
10867 char_u *s;
10868
10869 rettv->v_type = VAR_STRING;
10870 s = get_tv_string(&argvars[0]);
10871#ifdef FEAT_SPELL
10872 rettv->vval.v_string = eval_soundfold(s);
10873#else
10874 rettv->vval.v_string = vim_strsave(s);
10875#endif
10876}
10877
10878/*
10879 * "spellbadword()" function
10880 */
10881 static void
10882f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
10883{
10884 char_u *word = (char_u *)"";
10885 hlf_T attr = HLF_COUNT;
10886 int len = 0;
10887
10888 if (rettv_list_alloc(rettv) == FAIL)
10889 return;
10890
10891#ifdef FEAT_SPELL
10892 if (argvars[0].v_type == VAR_UNKNOWN)
10893 {
10894 /* Find the start and length of the badly spelled word. */
10895 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
10896 if (len != 0)
10897 word = ml_get_cursor();
10898 }
10899 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
10900 {
10901 char_u *str = get_tv_string_chk(&argvars[0]);
10902 int capcol = -1;
10903
10904 if (str != NULL)
10905 {
10906 /* Check the argument for spelling. */
10907 while (*str != NUL)
10908 {
10909 len = spell_check(curwin, str, &attr, &capcol, FALSE);
10910 if (attr != HLF_COUNT)
10911 {
10912 word = str;
10913 break;
10914 }
10915 str += len;
10916 }
10917 }
10918 }
10919#endif
10920
10921 list_append_string(rettv->vval.v_list, word, len);
10922 list_append_string(rettv->vval.v_list, (char_u *)(
10923 attr == HLF_SPB ? "bad" :
10924 attr == HLF_SPR ? "rare" :
10925 attr == HLF_SPL ? "local" :
10926 attr == HLF_SPC ? "caps" :
10927 ""), -1);
10928}
10929
10930/*
10931 * "spellsuggest()" function
10932 */
10933 static void
10934f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
10935{
10936#ifdef FEAT_SPELL
10937 char_u *str;
10938 int typeerr = FALSE;
10939 int maxcount;
10940 garray_T ga;
10941 int i;
10942 listitem_T *li;
10943 int need_capital = FALSE;
10944#endif
10945
10946 if (rettv_list_alloc(rettv) == FAIL)
10947 return;
10948
10949#ifdef FEAT_SPELL
10950 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
10951 {
10952 str = get_tv_string(&argvars[0]);
10953 if (argvars[1].v_type != VAR_UNKNOWN)
10954 {
10955 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
10956 if (maxcount <= 0)
10957 return;
10958 if (argvars[2].v_type != VAR_UNKNOWN)
10959 {
10960 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
10961 if (typeerr)
10962 return;
10963 }
10964 }
10965 else
10966 maxcount = 25;
10967
10968 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
10969
10970 for (i = 0; i < ga.ga_len; ++i)
10971 {
10972 str = ((char_u **)ga.ga_data)[i];
10973
10974 li = listitem_alloc();
10975 if (li == NULL)
10976 vim_free(str);
10977 else
10978 {
10979 li->li_tv.v_type = VAR_STRING;
10980 li->li_tv.v_lock = 0;
10981 li->li_tv.vval.v_string = str;
10982 list_append(rettv->vval.v_list, li);
10983 }
10984 }
10985 ga_clear(&ga);
10986 }
10987#endif
10988}
10989
10990 static void
10991f_split(typval_T *argvars, typval_T *rettv)
10992{
10993 char_u *str;
10994 char_u *end;
10995 char_u *pat = NULL;
10996 regmatch_T regmatch;
10997 char_u patbuf[NUMBUFLEN];
10998 char_u *save_cpo;
10999 int match;
11000 colnr_T col = 0;
11001 int keepempty = FALSE;
11002 int typeerr = FALSE;
11003
11004 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11005 save_cpo = p_cpo;
11006 p_cpo = (char_u *)"";
11007
11008 str = get_tv_string(&argvars[0]);
11009 if (argvars[1].v_type != VAR_UNKNOWN)
11010 {
11011 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11012 if (pat == NULL)
11013 typeerr = TRUE;
11014 if (argvars[2].v_type != VAR_UNKNOWN)
11015 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
11016 }
11017 if (pat == NULL || *pat == NUL)
11018 pat = (char_u *)"[\\x01- ]\\+";
11019
11020 if (rettv_list_alloc(rettv) == FAIL)
11021 return;
11022 if (typeerr)
11023 return;
11024
11025 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11026 if (regmatch.regprog != NULL)
11027 {
11028 regmatch.rm_ic = FALSE;
11029 while (*str != NUL || keepempty)
11030 {
11031 if (*str == NUL)
11032 match = FALSE; /* empty item at the end */
11033 else
11034 match = vim_regexec_nl(&regmatch, str, col);
11035 if (match)
11036 end = regmatch.startp[0];
11037 else
11038 end = str + STRLEN(str);
11039 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
11040 && *str != NUL && match && end < regmatch.endp[0]))
11041 {
11042 if (list_append_string(rettv->vval.v_list, str,
11043 (int)(end - str)) == FAIL)
11044 break;
11045 }
11046 if (!match)
11047 break;
11048 /* Advance to just after the match. */
11049 if (regmatch.endp[0] > str)
11050 col = 0;
11051 else
11052 {
11053 /* Don't get stuck at the same match. */
11054#ifdef FEAT_MBYTE
11055 col = (*mb_ptr2len)(regmatch.endp[0]);
11056#else
11057 col = 1;
11058#endif
11059 }
11060 str = regmatch.endp[0];
11061 }
11062
11063 vim_regfree(regmatch.regprog);
11064 }
11065
11066 p_cpo = save_cpo;
11067}
11068
11069#ifdef FEAT_FLOAT
11070/*
11071 * "sqrt()" function
11072 */
11073 static void
11074f_sqrt(typval_T *argvars, typval_T *rettv)
11075{
11076 float_T f = 0.0;
11077
11078 rettv->v_type = VAR_FLOAT;
11079 if (get_float_arg(argvars, &f) == OK)
11080 rettv->vval.v_float = sqrt(f);
11081 else
11082 rettv->vval.v_float = 0.0;
11083}
11084
11085/*
11086 * "str2float()" function
11087 */
11088 static void
11089f_str2float(typval_T *argvars, typval_T *rettv)
11090{
11091 char_u *p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011092 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011093
Bram Moolenaar08243d22017-01-10 16:12:29 +010011094 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011095 p = skipwhite(p + 1);
11096 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011097 if (isneg)
11098 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011099 rettv->v_type = VAR_FLOAT;
11100}
11101#endif
11102
11103/*
11104 * "str2nr()" function
11105 */
11106 static void
11107f_str2nr(typval_T *argvars, typval_T *rettv)
11108{
11109 int base = 10;
11110 char_u *p;
11111 varnumber_T n;
11112 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010011113 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011114
11115 if (argvars[1].v_type != VAR_UNKNOWN)
11116 {
11117 base = (int)get_tv_number(&argvars[1]);
11118 if (base != 2 && base != 8 && base != 10 && base != 16)
11119 {
11120 EMSG(_(e_invarg));
11121 return;
11122 }
11123 }
11124
11125 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011126 isneg = (*p == '-');
11127 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011128 p = skipwhite(p + 1);
11129 switch (base)
11130 {
11131 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
11132 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
11133 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
11134 default: what = 0;
11135 }
11136 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011137 if (isneg)
11138 rettv->vval.v_number = -n;
11139 else
11140 rettv->vval.v_number = n;
11141
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011142}
11143
11144#ifdef HAVE_STRFTIME
11145/*
11146 * "strftime({format}[, {time}])" function
11147 */
11148 static void
11149f_strftime(typval_T *argvars, typval_T *rettv)
11150{
11151 char_u result_buf[256];
11152 struct tm *curtime;
11153 time_t seconds;
11154 char_u *p;
11155
11156 rettv->v_type = VAR_STRING;
11157
11158 p = get_tv_string(&argvars[0]);
11159 if (argvars[1].v_type == VAR_UNKNOWN)
11160 seconds = time(NULL);
11161 else
11162 seconds = (time_t)get_tv_number(&argvars[1]);
11163 curtime = localtime(&seconds);
11164 /* MSVC returns NULL for an invalid value of seconds. */
11165 if (curtime == NULL)
11166 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
11167 else
11168 {
11169# ifdef FEAT_MBYTE
11170 vimconv_T conv;
11171 char_u *enc;
11172
11173 conv.vc_type = CONV_NONE;
11174 enc = enc_locale();
11175 convert_setup(&conv, p_enc, enc);
11176 if (conv.vc_type != CONV_NONE)
11177 p = string_convert(&conv, p, NULL);
11178# endif
11179 if (p != NULL)
11180 (void)strftime((char *)result_buf, sizeof(result_buf),
11181 (char *)p, curtime);
11182 else
11183 result_buf[0] = NUL;
11184
11185# ifdef FEAT_MBYTE
11186 if (conv.vc_type != CONV_NONE)
11187 vim_free(p);
11188 convert_setup(&conv, enc, p_enc);
11189 if (conv.vc_type != CONV_NONE)
11190 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
11191 else
11192# endif
11193 rettv->vval.v_string = vim_strsave(result_buf);
11194
11195# ifdef FEAT_MBYTE
11196 /* Release conversion descriptors */
11197 convert_setup(&conv, NULL, NULL);
11198 vim_free(enc);
11199# endif
11200 }
11201}
11202#endif
11203
11204/*
11205 * "strgetchar()" function
11206 */
11207 static void
11208f_strgetchar(typval_T *argvars, typval_T *rettv)
11209{
11210 char_u *str;
11211 int len;
11212 int error = FALSE;
11213 int charidx;
11214
11215 rettv->vval.v_number = -1;
11216 str = get_tv_string_chk(&argvars[0]);
11217 if (str == NULL)
11218 return;
11219 len = (int)STRLEN(str);
11220 charidx = (int)get_tv_number_chk(&argvars[1], &error);
11221 if (error)
11222 return;
11223#ifdef FEAT_MBYTE
11224 {
11225 int byteidx = 0;
11226
11227 while (charidx >= 0 && byteidx < len)
11228 {
11229 if (charidx == 0)
11230 {
11231 rettv->vval.v_number = mb_ptr2char(str + byteidx);
11232 break;
11233 }
11234 --charidx;
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011235 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011236 }
11237 }
11238#else
11239 if (charidx < len)
11240 rettv->vval.v_number = str[charidx];
11241#endif
11242}
11243
11244/*
11245 * "stridx()" function
11246 */
11247 static void
11248f_stridx(typval_T *argvars, typval_T *rettv)
11249{
11250 char_u buf[NUMBUFLEN];
11251 char_u *needle;
11252 char_u *haystack;
11253 char_u *save_haystack;
11254 char_u *pos;
11255 int start_idx;
11256
11257 needle = get_tv_string_chk(&argvars[1]);
11258 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
11259 rettv->vval.v_number = -1;
11260 if (needle == NULL || haystack == NULL)
11261 return; /* type error; errmsg already given */
11262
11263 if (argvars[2].v_type != VAR_UNKNOWN)
11264 {
11265 int error = FALSE;
11266
11267 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
11268 if (error || start_idx >= (int)STRLEN(haystack))
11269 return;
11270 if (start_idx >= 0)
11271 haystack += start_idx;
11272 }
11273
11274 pos = (char_u *)strstr((char *)haystack, (char *)needle);
11275 if (pos != NULL)
11276 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
11277}
11278
11279/*
11280 * "string()" function
11281 */
11282 static void
11283f_string(typval_T *argvars, typval_T *rettv)
11284{
11285 char_u *tofree;
11286 char_u numbuf[NUMBUFLEN];
11287
11288 rettv->v_type = VAR_STRING;
11289 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
11290 get_copyID());
11291 /* Make a copy if we have a value but it's not in allocated memory. */
11292 if (rettv->vval.v_string != NULL && tofree == NULL)
11293 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
11294}
11295
11296/*
11297 * "strlen()" function
11298 */
11299 static void
11300f_strlen(typval_T *argvars, typval_T *rettv)
11301{
11302 rettv->vval.v_number = (varnumber_T)(STRLEN(
11303 get_tv_string(&argvars[0])));
11304}
11305
11306/*
11307 * "strchars()" function
11308 */
11309 static void
11310f_strchars(typval_T *argvars, typval_T *rettv)
11311{
11312 char_u *s = get_tv_string(&argvars[0]);
11313 int skipcc = 0;
11314#ifdef FEAT_MBYTE
11315 varnumber_T len = 0;
11316 int (*func_mb_ptr2char_adv)(char_u **pp);
11317#endif
11318
11319 if (argvars[1].v_type != VAR_UNKNOWN)
11320 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
11321 if (skipcc < 0 || skipcc > 1)
11322 EMSG(_(e_invarg));
11323 else
11324 {
11325#ifdef FEAT_MBYTE
11326 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
11327 while (*s != NUL)
11328 {
11329 func_mb_ptr2char_adv(&s);
11330 ++len;
11331 }
11332 rettv->vval.v_number = len;
11333#else
11334 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
11335#endif
11336 }
11337}
11338
11339/*
11340 * "strdisplaywidth()" function
11341 */
11342 static void
11343f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
11344{
11345 char_u *s = get_tv_string(&argvars[0]);
11346 int col = 0;
11347
11348 if (argvars[1].v_type != VAR_UNKNOWN)
11349 col = (int)get_tv_number(&argvars[1]);
11350
11351 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
11352}
11353
11354/*
11355 * "strwidth()" function
11356 */
11357 static void
11358f_strwidth(typval_T *argvars, typval_T *rettv)
11359{
11360 char_u *s = get_tv_string(&argvars[0]);
11361
11362 rettv->vval.v_number = (varnumber_T)(
11363#ifdef FEAT_MBYTE
11364 mb_string2cells(s, -1)
11365#else
11366 STRLEN(s)
11367#endif
11368 );
11369}
11370
11371/*
11372 * "strcharpart()" function
11373 */
11374 static void
11375f_strcharpart(typval_T *argvars, typval_T *rettv)
11376{
11377#ifdef FEAT_MBYTE
11378 char_u *p;
11379 int nchar;
11380 int nbyte = 0;
11381 int charlen;
11382 int len = 0;
11383 int slen;
11384 int error = FALSE;
11385
11386 p = get_tv_string(&argvars[0]);
11387 slen = (int)STRLEN(p);
11388
11389 nchar = (int)get_tv_number_chk(&argvars[1], &error);
11390 if (!error)
11391 {
11392 if (nchar > 0)
11393 while (nchar > 0 && nbyte < slen)
11394 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011395 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011396 --nchar;
11397 }
11398 else
11399 nbyte = nchar;
11400 if (argvars[2].v_type != VAR_UNKNOWN)
11401 {
11402 charlen = (int)get_tv_number(&argvars[2]);
11403 while (charlen > 0 && nbyte + len < slen)
11404 {
11405 int off = nbyte + len;
11406
11407 if (off < 0)
11408 len += 1;
11409 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011410 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011411 --charlen;
11412 }
11413 }
11414 else
11415 len = slen - nbyte; /* default: all bytes that are available. */
11416 }
11417
11418 /*
11419 * Only return the overlap between the specified part and the actual
11420 * string.
11421 */
11422 if (nbyte < 0)
11423 {
11424 len += nbyte;
11425 nbyte = 0;
11426 }
11427 else if (nbyte > slen)
11428 nbyte = slen;
11429 if (len < 0)
11430 len = 0;
11431 else if (nbyte + len > slen)
11432 len = slen - nbyte;
11433
11434 rettv->v_type = VAR_STRING;
11435 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
11436#else
11437 f_strpart(argvars, rettv);
11438#endif
11439}
11440
11441/*
11442 * "strpart()" function
11443 */
11444 static void
11445f_strpart(typval_T *argvars, typval_T *rettv)
11446{
11447 char_u *p;
11448 int n;
11449 int len;
11450 int slen;
11451 int error = FALSE;
11452
11453 p = get_tv_string(&argvars[0]);
11454 slen = (int)STRLEN(p);
11455
11456 n = (int)get_tv_number_chk(&argvars[1], &error);
11457 if (error)
11458 len = 0;
11459 else if (argvars[2].v_type != VAR_UNKNOWN)
11460 len = (int)get_tv_number(&argvars[2]);
11461 else
11462 len = slen - n; /* default len: all bytes that are available. */
11463
11464 /*
11465 * Only return the overlap between the specified part and the actual
11466 * string.
11467 */
11468 if (n < 0)
11469 {
11470 len += n;
11471 n = 0;
11472 }
11473 else if (n > slen)
11474 n = slen;
11475 if (len < 0)
11476 len = 0;
11477 else if (n + len > slen)
11478 len = slen - n;
11479
11480 rettv->v_type = VAR_STRING;
11481 rettv->vval.v_string = vim_strnsave(p + n, len);
11482}
11483
11484/*
11485 * "strridx()" function
11486 */
11487 static void
11488f_strridx(typval_T *argvars, typval_T *rettv)
11489{
11490 char_u buf[NUMBUFLEN];
11491 char_u *needle;
11492 char_u *haystack;
11493 char_u *rest;
11494 char_u *lastmatch = NULL;
11495 int haystack_len, end_idx;
11496
11497 needle = get_tv_string_chk(&argvars[1]);
11498 haystack = get_tv_string_buf_chk(&argvars[0], buf);
11499
11500 rettv->vval.v_number = -1;
11501 if (needle == NULL || haystack == NULL)
11502 return; /* type error; errmsg already given */
11503
11504 haystack_len = (int)STRLEN(haystack);
11505 if (argvars[2].v_type != VAR_UNKNOWN)
11506 {
11507 /* Third argument: upper limit for index */
11508 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
11509 if (end_idx < 0)
11510 return; /* can never find a match */
11511 }
11512 else
11513 end_idx = haystack_len;
11514
11515 if (*needle == NUL)
11516 {
11517 /* Empty string matches past the end. */
11518 lastmatch = haystack + end_idx;
11519 }
11520 else
11521 {
11522 for (rest = haystack; *rest != '\0'; ++rest)
11523 {
11524 rest = (char_u *)strstr((char *)rest, (char *)needle);
11525 if (rest == NULL || rest > haystack + end_idx)
11526 break;
11527 lastmatch = rest;
11528 }
11529 }
11530
11531 if (lastmatch == NULL)
11532 rettv->vval.v_number = -1;
11533 else
11534 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
11535}
11536
11537/*
11538 * "strtrans()" function
11539 */
11540 static void
11541f_strtrans(typval_T *argvars, typval_T *rettv)
11542{
11543 rettv->v_type = VAR_STRING;
11544 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
11545}
11546
11547/*
11548 * "submatch()" function
11549 */
11550 static void
11551f_submatch(typval_T *argvars, typval_T *rettv)
11552{
11553 int error = FALSE;
11554 int no;
11555 int retList = 0;
11556
11557 no = (int)get_tv_number_chk(&argvars[0], &error);
11558 if (error)
11559 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020011560 if (no < 0 || no >= NSUBEXP)
11561 {
Bram Moolenaar79518e22017-02-17 16:31:35 +010011562 EMSGN(_("E935: invalid submatch number: %d"), no);
11563 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020011564 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011565 if (argvars[1].v_type != VAR_UNKNOWN)
11566 retList = (int)get_tv_number_chk(&argvars[1], &error);
11567 if (error)
11568 return;
11569
11570 if (retList == 0)
11571 {
11572 rettv->v_type = VAR_STRING;
11573 rettv->vval.v_string = reg_submatch(no);
11574 }
11575 else
11576 {
11577 rettv->v_type = VAR_LIST;
11578 rettv->vval.v_list = reg_submatch_list(no);
11579 }
11580}
11581
11582/*
11583 * "substitute()" function
11584 */
11585 static void
11586f_substitute(typval_T *argvars, typval_T *rettv)
11587{
11588 char_u patbuf[NUMBUFLEN];
11589 char_u subbuf[NUMBUFLEN];
11590 char_u flagsbuf[NUMBUFLEN];
11591
11592 char_u *str = get_tv_string_chk(&argvars[0]);
11593 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011594 char_u *sub = NULL;
11595 typval_T *expr = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011596 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
11597
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011598 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
11599 expr = &argvars[2];
11600 else
11601 sub = get_tv_string_buf_chk(&argvars[2], subbuf);
11602
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011603 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011604 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
11605 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011606 rettv->vval.v_string = NULL;
11607 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011608 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011609}
11610
11611/*
11612 * "synID(lnum, col, trans)" function
11613 */
11614 static void
11615f_synID(typval_T *argvars UNUSED, typval_T *rettv)
11616{
11617 int id = 0;
11618#ifdef FEAT_SYN_HL
11619 linenr_T lnum;
11620 colnr_T col;
11621 int trans;
11622 int transerr = FALSE;
11623
11624 lnum = get_tv_lnum(argvars); /* -1 on type error */
11625 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
11626 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
11627
11628 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11629 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
11630 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
11631#endif
11632
11633 rettv->vval.v_number = id;
11634}
11635
11636/*
11637 * "synIDattr(id, what [, mode])" function
11638 */
11639 static void
11640f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
11641{
11642 char_u *p = NULL;
11643#ifdef FEAT_SYN_HL
11644 int id;
11645 char_u *what;
11646 char_u *mode;
11647 char_u modebuf[NUMBUFLEN];
11648 int modec;
11649
11650 id = (int)get_tv_number(&argvars[0]);
11651 what = get_tv_string(&argvars[1]);
11652 if (argvars[2].v_type != VAR_UNKNOWN)
11653 {
11654 mode = get_tv_string_buf(&argvars[2], modebuf);
11655 modec = TOLOWER_ASC(mode[0]);
11656 if (modec != 't' && modec != 'c' && modec != 'g')
11657 modec = 0; /* replace invalid with current */
11658 }
11659 else
11660 {
11661#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
11662 if (USE_24BIT)
11663 modec = 'g';
11664 else
11665#endif
11666 if (t_colors > 1)
11667 modec = 'c';
11668 else
11669 modec = 't';
11670 }
11671
11672
11673 switch (TOLOWER_ASC(what[0]))
11674 {
11675 case 'b':
11676 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
11677 p = highlight_color(id, what, modec);
11678 else /* bold */
11679 p = highlight_has_attr(id, HL_BOLD, modec);
11680 break;
11681
11682 case 'f': /* fg[#] or font */
11683 p = highlight_color(id, what, modec);
11684 break;
11685
11686 case 'i':
11687 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
11688 p = highlight_has_attr(id, HL_INVERSE, modec);
11689 else /* italic */
11690 p = highlight_has_attr(id, HL_ITALIC, modec);
11691 break;
11692
11693 case 'n': /* name */
11694 p = get_highlight_name(NULL, id - 1);
11695 break;
11696
11697 case 'r': /* reverse */
11698 p = highlight_has_attr(id, HL_INVERSE, modec);
11699 break;
11700
11701 case 's':
11702 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
11703 p = highlight_color(id, what, modec);
11704 else /* standout */
11705 p = highlight_has_attr(id, HL_STANDOUT, modec);
11706 break;
11707
11708 case 'u':
11709 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
11710 /* underline */
11711 p = highlight_has_attr(id, HL_UNDERLINE, modec);
11712 else
11713 /* undercurl */
11714 p = highlight_has_attr(id, HL_UNDERCURL, modec);
11715 break;
11716 }
11717
11718 if (p != NULL)
11719 p = vim_strsave(p);
11720#endif
11721 rettv->v_type = VAR_STRING;
11722 rettv->vval.v_string = p;
11723}
11724
11725/*
11726 * "synIDtrans(id)" function
11727 */
11728 static void
11729f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
11730{
11731 int id;
11732
11733#ifdef FEAT_SYN_HL
11734 id = (int)get_tv_number(&argvars[0]);
11735
11736 if (id > 0)
11737 id = syn_get_final_id(id);
11738 else
11739#endif
11740 id = 0;
11741
11742 rettv->vval.v_number = id;
11743}
11744
11745/*
11746 * "synconcealed(lnum, col)" function
11747 */
11748 static void
11749f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
11750{
11751#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
11752 linenr_T lnum;
11753 colnr_T col;
11754 int syntax_flags = 0;
11755 int cchar;
11756 int matchid = 0;
11757 char_u str[NUMBUFLEN];
11758#endif
11759
11760 rettv->v_type = VAR_LIST;
11761 rettv->vval.v_list = NULL;
11762
11763#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
11764 lnum = get_tv_lnum(argvars); /* -1 on type error */
11765 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
11766
11767 vim_memset(str, NUL, sizeof(str));
11768
11769 if (rettv_list_alloc(rettv) != FAIL)
11770 {
11771 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11772 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
11773 && curwin->w_p_cole > 0)
11774 {
11775 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
11776 syntax_flags = get_syntax_info(&matchid);
11777
11778 /* get the conceal character */
11779 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
11780 {
11781 cchar = syn_get_sub_char();
11782 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
11783 cchar = lcs_conceal;
11784 if (cchar != NUL)
11785 {
11786# ifdef FEAT_MBYTE
11787 if (has_mbyte)
11788 (*mb_char2bytes)(cchar, str);
11789 else
11790# endif
11791 str[0] = cchar;
11792 }
11793 }
11794 }
11795
11796 list_append_number(rettv->vval.v_list,
11797 (syntax_flags & HL_CONCEAL) != 0);
11798 /* -1 to auto-determine strlen */
11799 list_append_string(rettv->vval.v_list, str, -1);
11800 list_append_number(rettv->vval.v_list, matchid);
11801 }
11802#endif
11803}
11804
11805/*
11806 * "synstack(lnum, col)" function
11807 */
11808 static void
11809f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
11810{
11811#ifdef FEAT_SYN_HL
11812 linenr_T lnum;
11813 colnr_T col;
11814 int i;
11815 int id;
11816#endif
11817
11818 rettv->v_type = VAR_LIST;
11819 rettv->vval.v_list = NULL;
11820
11821#ifdef FEAT_SYN_HL
11822 lnum = get_tv_lnum(argvars); /* -1 on type error */
11823 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
11824
11825 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11826 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
11827 && rettv_list_alloc(rettv) != FAIL)
11828 {
11829 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
11830 for (i = 0; ; ++i)
11831 {
11832 id = syn_get_stack_item(i);
11833 if (id < 0)
11834 break;
11835 if (list_append_number(rettv->vval.v_list, id) == FAIL)
11836 break;
11837 }
11838 }
11839#endif
11840}
11841
11842 static void
11843get_cmd_output_as_rettv(
11844 typval_T *argvars,
11845 typval_T *rettv,
11846 int retlist)
11847{
11848 char_u *res = NULL;
11849 char_u *p;
11850 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011851 int err = FALSE;
11852 FILE *fd;
11853 list_T *list = NULL;
11854 int flags = SHELL_SILENT;
11855
11856 rettv->v_type = VAR_STRING;
11857 rettv->vval.v_string = NULL;
11858 if (check_restricted() || check_secure())
11859 goto errret;
11860
11861 if (argvars[1].v_type != VAR_UNKNOWN)
11862 {
11863 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010011864 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011865 * command.
11866 */
11867 if ((infile = vim_tempname('i', TRUE)) == NULL)
11868 {
11869 EMSG(_(e_notmp));
11870 goto errret;
11871 }
11872
11873 fd = mch_fopen((char *)infile, WRITEBIN);
11874 if (fd == NULL)
11875 {
11876 EMSG2(_(e_notopen), infile);
11877 goto errret;
11878 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010011879 if (argvars[1].v_type == VAR_NUMBER)
11880 {
11881 linenr_T lnum;
11882 buf_T *buf;
11883
11884 buf = buflist_findnr(argvars[1].vval.v_number);
11885 if (buf == NULL)
11886 {
11887 EMSGN(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010011888 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010011889 goto errret;
11890 }
11891
11892 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
11893 {
11894 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
11895 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
11896 {
11897 err = TRUE;
11898 break;
11899 }
11900 if (putc(NL, fd) == EOF)
11901 {
11902 err = TRUE;
11903 break;
11904 }
11905 }
11906 }
11907 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011908 {
11909 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
11910 err = TRUE;
11911 }
11912 else
11913 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010011914 size_t len;
11915 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011916
11917 p = get_tv_string_buf_chk(&argvars[1], buf);
11918 if (p == NULL)
11919 {
11920 fclose(fd);
11921 goto errret; /* type error; errmsg already given */
11922 }
11923 len = STRLEN(p);
11924 if (len > 0 && fwrite(p, len, 1, fd) != 1)
11925 err = TRUE;
11926 }
11927 if (fclose(fd) != 0)
11928 err = TRUE;
11929 if (err)
11930 {
11931 EMSG(_("E677: Error writing temp file"));
11932 goto errret;
11933 }
11934 }
11935
11936 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
11937 * echoes typeahead, that messes up the display. */
11938 if (!msg_silent)
11939 flags += SHELL_COOKED;
11940
11941 if (retlist)
11942 {
11943 int len;
11944 listitem_T *li;
11945 char_u *s = NULL;
11946 char_u *start;
11947 char_u *end;
11948 int i;
11949
11950 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
11951 if (res == NULL)
11952 goto errret;
11953
11954 list = list_alloc();
11955 if (list == NULL)
11956 goto errret;
11957
11958 for (i = 0; i < len; ++i)
11959 {
11960 start = res + i;
11961 while (i < len && res[i] != NL)
11962 ++i;
11963 end = res + i;
11964
11965 s = alloc((unsigned)(end - start + 1));
11966 if (s == NULL)
11967 goto errret;
11968
11969 for (p = s; start < end; ++p, ++start)
11970 *p = *start == NUL ? NL : *start;
11971 *p = NUL;
11972
11973 li = listitem_alloc();
11974 if (li == NULL)
11975 {
11976 vim_free(s);
11977 goto errret;
11978 }
11979 li->li_tv.v_type = VAR_STRING;
11980 li->li_tv.v_lock = 0;
11981 li->li_tv.vval.v_string = s;
11982 list_append(list, li);
11983 }
11984
11985 ++list->lv_refcount;
11986 rettv->v_type = VAR_LIST;
11987 rettv->vval.v_list = list;
11988 list = NULL;
11989 }
11990 else
11991 {
11992 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
11993#ifdef USE_CR
11994 /* translate <CR> into <NL> */
11995 if (res != NULL)
11996 {
11997 char_u *s;
11998
11999 for (s = res; *s; ++s)
12000 {
12001 if (*s == CAR)
12002 *s = NL;
12003 }
12004 }
12005#else
12006# ifdef USE_CRNL
12007 /* translate <CR><NL> into <NL> */
12008 if (res != NULL)
12009 {
12010 char_u *s, *d;
12011
12012 d = res;
12013 for (s = res; *s; ++s)
12014 {
12015 if (s[0] == CAR && s[1] == NL)
12016 ++s;
12017 *d++ = *s;
12018 }
12019 *d = NUL;
12020 }
12021# endif
12022#endif
12023 rettv->vval.v_string = res;
12024 res = NULL;
12025 }
12026
12027errret:
12028 if (infile != NULL)
12029 {
12030 mch_remove(infile);
12031 vim_free(infile);
12032 }
12033 if (res != NULL)
12034 vim_free(res);
12035 if (list != NULL)
12036 list_free(list);
12037}
12038
12039/*
12040 * "system()" function
12041 */
12042 static void
12043f_system(typval_T *argvars, typval_T *rettv)
12044{
12045 get_cmd_output_as_rettv(argvars, rettv, FALSE);
12046}
12047
12048/*
12049 * "systemlist()" function
12050 */
12051 static void
12052f_systemlist(typval_T *argvars, typval_T *rettv)
12053{
12054 get_cmd_output_as_rettv(argvars, rettv, TRUE);
12055}
12056
12057/*
12058 * "tabpagebuflist()" function
12059 */
12060 static void
12061f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12062{
12063#ifdef FEAT_WINDOWS
12064 tabpage_T *tp;
12065 win_T *wp = NULL;
12066
12067 if (argvars[0].v_type == VAR_UNKNOWN)
12068 wp = firstwin;
12069 else
12070 {
12071 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12072 if (tp != NULL)
12073 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12074 }
12075 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
12076 {
12077 for (; wp != NULL; wp = wp->w_next)
12078 if (list_append_number(rettv->vval.v_list,
12079 wp->w_buffer->b_fnum) == FAIL)
12080 break;
12081 }
12082#endif
12083}
12084
12085
12086/*
12087 * "tabpagenr()" function
12088 */
12089 static void
12090f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
12091{
12092 int nr = 1;
12093#ifdef FEAT_WINDOWS
12094 char_u *arg;
12095
12096 if (argvars[0].v_type != VAR_UNKNOWN)
12097 {
12098 arg = get_tv_string_chk(&argvars[0]);
12099 nr = 0;
12100 if (arg != NULL)
12101 {
12102 if (STRCMP(arg, "$") == 0)
12103 nr = tabpage_index(NULL) - 1;
12104 else
12105 EMSG2(_(e_invexpr2), arg);
12106 }
12107 }
12108 else
12109 nr = tabpage_index(curtab);
12110#endif
12111 rettv->vval.v_number = nr;
12112}
12113
12114
12115#ifdef FEAT_WINDOWS
12116static int get_winnr(tabpage_T *tp, typval_T *argvar);
12117
12118/*
12119 * Common code for tabpagewinnr() and winnr().
12120 */
12121 static int
12122get_winnr(tabpage_T *tp, typval_T *argvar)
12123{
12124 win_T *twin;
12125 int nr = 1;
12126 win_T *wp;
12127 char_u *arg;
12128
12129 twin = (tp == curtab) ? curwin : tp->tp_curwin;
12130 if (argvar->v_type != VAR_UNKNOWN)
12131 {
12132 arg = get_tv_string_chk(argvar);
12133 if (arg == NULL)
12134 nr = 0; /* type error; errmsg already given */
12135 else if (STRCMP(arg, "$") == 0)
12136 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
12137 else if (STRCMP(arg, "#") == 0)
12138 {
12139 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
12140 if (twin == NULL)
12141 nr = 0;
12142 }
12143 else
12144 {
12145 EMSG2(_(e_invexpr2), arg);
12146 nr = 0;
12147 }
12148 }
12149
12150 if (nr > 0)
12151 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12152 wp != twin; wp = wp->w_next)
12153 {
12154 if (wp == NULL)
12155 {
12156 /* didn't find it in this tabpage */
12157 nr = 0;
12158 break;
12159 }
12160 ++nr;
12161 }
12162 return nr;
12163}
12164#endif
12165
12166/*
12167 * "tabpagewinnr()" function
12168 */
12169 static void
12170f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
12171{
12172 int nr = 1;
12173#ifdef FEAT_WINDOWS
12174 tabpage_T *tp;
12175
12176 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12177 if (tp == NULL)
12178 nr = 0;
12179 else
12180 nr = get_winnr(tp, &argvars[1]);
12181#endif
12182 rettv->vval.v_number = nr;
12183}
12184
12185
12186/*
12187 * "tagfiles()" function
12188 */
12189 static void
12190f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
12191{
12192 char_u *fname;
12193 tagname_T tn;
12194 int first;
12195
12196 if (rettv_list_alloc(rettv) == FAIL)
12197 return;
12198 fname = alloc(MAXPATHL);
12199 if (fname == NULL)
12200 return;
12201
12202 for (first = TRUE; ; first = FALSE)
12203 if (get_tagfname(&tn, first, fname) == FAIL
12204 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
12205 break;
12206 tagname_free(&tn);
12207 vim_free(fname);
12208}
12209
12210/*
12211 * "taglist()" function
12212 */
12213 static void
12214f_taglist(typval_T *argvars, typval_T *rettv)
12215{
12216 char_u *tag_pattern;
12217
12218 tag_pattern = get_tv_string(&argvars[0]);
12219
12220 rettv->vval.v_number = FALSE;
12221 if (*tag_pattern == NUL)
12222 return;
12223
12224 if (rettv_list_alloc(rettv) == OK)
12225 (void)get_tags(rettv->vval.v_list, tag_pattern);
12226}
12227
12228/*
12229 * "tempname()" function
12230 */
12231 static void
12232f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
12233{
12234 static int x = 'A';
12235
12236 rettv->v_type = VAR_STRING;
12237 rettv->vval.v_string = vim_tempname(x, FALSE);
12238
12239 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
12240 * names. Skip 'I' and 'O', they are used for shell redirection. */
12241 do
12242 {
12243 if (x == 'Z')
12244 x = '0';
12245 else if (x == '9')
12246 x = 'A';
12247 else
12248 {
12249#ifdef EBCDIC
12250 if (x == 'I')
12251 x = 'J';
12252 else if (x == 'R')
12253 x = 'S';
12254 else
12255#endif
12256 ++x;
12257 }
12258 } while (x == 'I' || x == 'O');
12259}
12260
12261#ifdef FEAT_FLOAT
12262/*
12263 * "tan()" function
12264 */
12265 static void
12266f_tan(typval_T *argvars, typval_T *rettv)
12267{
12268 float_T f = 0.0;
12269
12270 rettv->v_type = VAR_FLOAT;
12271 if (get_float_arg(argvars, &f) == OK)
12272 rettv->vval.v_float = tan(f);
12273 else
12274 rettv->vval.v_float = 0.0;
12275}
12276
12277/*
12278 * "tanh()" function
12279 */
12280 static void
12281f_tanh(typval_T *argvars, typval_T *rettv)
12282{
12283 float_T f = 0.0;
12284
12285 rettv->v_type = VAR_FLOAT;
12286 if (get_float_arg(argvars, &f) == OK)
12287 rettv->vval.v_float = tanh(f);
12288 else
12289 rettv->vval.v_float = 0.0;
12290}
12291#endif
12292
12293/*
12294 * "test_alloc_fail(id, countdown, repeat)" function
12295 */
12296 static void
12297f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
12298{
12299 if (argvars[0].v_type != VAR_NUMBER
12300 || argvars[0].vval.v_number <= 0
12301 || argvars[1].v_type != VAR_NUMBER
12302 || argvars[1].vval.v_number < 0
12303 || argvars[2].v_type != VAR_NUMBER)
12304 EMSG(_(e_invarg));
12305 else
12306 {
12307 alloc_fail_id = argvars[0].vval.v_number;
12308 if (alloc_fail_id >= aid_last)
12309 EMSG(_(e_invarg));
12310 alloc_fail_countdown = argvars[1].vval.v_number;
12311 alloc_fail_repeat = argvars[2].vval.v_number;
12312 did_outofmem_msg = FALSE;
12313 }
12314}
12315
12316/*
12317 * "test_autochdir()"
12318 */
12319 static void
12320f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12321{
12322#if defined(FEAT_AUTOCHDIR)
12323 test_autochdir = TRUE;
12324#endif
12325}
12326
12327/*
12328 * "test_disable_char_avail({expr})" function
12329 */
12330 static void
12331f_test_disable_char_avail(typval_T *argvars, typval_T *rettv UNUSED)
12332{
12333 disable_char_avail_for_testing = (int)get_tv_number(&argvars[0]);
12334}
12335
12336/*
12337 * "test_garbagecollect_now()" function
12338 */
12339 static void
12340f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12341{
12342 /* This is dangerous, any Lists and Dicts used internally may be freed
12343 * while still in use. */
12344 garbage_collect(TRUE);
12345}
12346
Bram Moolenaare0c31f62017-03-01 15:07:05 +010012347/*
12348 * "test_ignore_error()" function
12349 */
12350 static void
12351f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
12352{
12353 ignore_error_for_testing(get_tv_string(&argvars[0]));
12354}
12355
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012356#ifdef FEAT_JOB_CHANNEL
12357 static void
12358f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
12359{
12360 rettv->v_type = VAR_CHANNEL;
12361 rettv->vval.v_channel = NULL;
12362}
12363#endif
12364
12365 static void
12366f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
12367{
12368 rettv->v_type = VAR_DICT;
12369 rettv->vval.v_dict = NULL;
12370}
12371
12372#ifdef FEAT_JOB_CHANNEL
12373 static void
12374f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
12375{
12376 rettv->v_type = VAR_JOB;
12377 rettv->vval.v_job = NULL;
12378}
12379#endif
12380
12381 static void
12382f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
12383{
12384 rettv->v_type = VAR_LIST;
12385 rettv->vval.v_list = NULL;
12386}
12387
12388 static void
12389f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
12390{
12391 rettv->v_type = VAR_PARTIAL;
12392 rettv->vval.v_partial = NULL;
12393}
12394
12395 static void
12396f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
12397{
12398 rettv->v_type = VAR_STRING;
12399 rettv->vval.v_string = NULL;
12400}
12401
12402 static void
12403f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
12404{
12405 time_for_testing = (time_t)get_tv_number(&argvars[0]);
12406}
12407
12408#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
12409/*
12410 * Get a callback from "arg". It can be a Funcref or a function name.
12411 * When "arg" is zero return an empty string.
12412 * Return NULL for an invalid argument.
12413 */
12414 char_u *
12415get_callback(typval_T *arg, partial_T **pp)
12416{
12417 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
12418 {
12419 *pp = arg->vval.v_partial;
12420 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012421 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012422 }
12423 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012424 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012425 {
12426 func_ref(arg->vval.v_string);
12427 return arg->vval.v_string;
12428 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012429 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
12430 return (char_u *)"";
12431 EMSG(_("E921: Invalid callback argument"));
12432 return NULL;
12433}
12434
12435/*
12436 * Unref/free "callback" and "partial" retured by get_callback().
12437 */
12438 void
12439free_callback(char_u *callback, partial_T *partial)
12440{
12441 if (partial != NULL)
12442 partial_unref(partial);
12443 else if (callback != NULL)
12444 {
12445 func_unref(callback);
12446 vim_free(callback);
12447 }
12448}
12449#endif
12450
12451#ifdef FEAT_TIMERS
12452/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020012453 * "timer_info([timer])" function
12454 */
12455 static void
12456f_timer_info(typval_T *argvars, typval_T *rettv)
12457{
12458 timer_T *timer = NULL;
12459
12460 if (rettv_list_alloc(rettv) != OK)
12461 return;
12462 if (argvars[0].v_type != VAR_UNKNOWN)
12463 {
12464 if (argvars[0].v_type != VAR_NUMBER)
12465 EMSG(_(e_number_exp));
12466 else
12467 {
12468 timer = find_timer((int)get_tv_number(&argvars[0]));
12469 if (timer != NULL)
12470 add_timer_info(rettv, timer);
12471 }
12472 }
12473 else
12474 add_timer_info_all(rettv);
12475}
12476
12477/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012478 * "timer_pause(timer, paused)" function
12479 */
12480 static void
12481f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
12482{
12483 timer_T *timer = NULL;
12484 int paused = (int)get_tv_number(&argvars[1]);
12485
12486 if (argvars[0].v_type != VAR_NUMBER)
12487 EMSG(_(e_number_exp));
12488 else
12489 {
12490 timer = find_timer((int)get_tv_number(&argvars[0]));
12491 if (timer != NULL)
12492 timer->tr_paused = paused;
12493 }
12494}
12495
12496/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012497 * "timer_start(time, callback [, options])" function
12498 */
12499 static void
12500f_timer_start(typval_T *argvars, typval_T *rettv)
12501{
Bram Moolenaar75537a92016-09-05 22:45:28 +020012502 long msec = (long)get_tv_number(&argvars[0]);
12503 timer_T *timer;
12504 int repeat = 0;
12505 char_u *callback;
12506 dict_T *dict;
12507 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012508
Bram Moolenaar75537a92016-09-05 22:45:28 +020012509 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012510 if (check_secure())
12511 return;
12512 if (argvars[2].v_type != VAR_UNKNOWN)
12513 {
12514 if (argvars[2].v_type != VAR_DICT
12515 || (dict = argvars[2].vval.v_dict) == NULL)
12516 {
12517 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
12518 return;
12519 }
12520 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
12521 repeat = get_dict_number(dict, (char_u *)"repeat");
12522 }
12523
Bram Moolenaar75537a92016-09-05 22:45:28 +020012524 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012525 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020012526 return;
12527
12528 timer = create_timer(msec, repeat);
12529 if (timer == NULL)
12530 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012531 else
12532 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020012533 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020012534 timer->tr_callback = vim_strsave(callback);
12535 else
12536 /* pointer into the partial */
12537 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020012538 timer->tr_partial = partial;
12539 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012540 }
12541}
12542
12543/*
12544 * "timer_stop(timer)" function
12545 */
12546 static void
12547f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
12548{
12549 timer_T *timer;
12550
12551 if (argvars[0].v_type != VAR_NUMBER)
12552 {
12553 EMSG(_(e_number_exp));
12554 return;
12555 }
12556 timer = find_timer((int)get_tv_number(&argvars[0]));
12557 if (timer != NULL)
12558 stop_timer(timer);
12559}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012560
12561/*
12562 * "timer_stopall()" function
12563 */
12564 static void
12565f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12566{
12567 stop_all_timers();
12568}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012569#endif
12570
12571/*
12572 * "tolower(string)" function
12573 */
12574 static void
12575f_tolower(typval_T *argvars, typval_T *rettv)
12576{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012577 rettv->v_type = VAR_STRING;
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +010012578 rettv->vval.v_string = strlow_save(get_tv_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012579}
12580
12581/*
12582 * "toupper(string)" function
12583 */
12584 static void
12585f_toupper(typval_T *argvars, typval_T *rettv)
12586{
12587 rettv->v_type = VAR_STRING;
12588 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
12589}
12590
12591/*
12592 * "tr(string, fromstr, tostr)" function
12593 */
12594 static void
12595f_tr(typval_T *argvars, typval_T *rettv)
12596{
12597 char_u *in_str;
12598 char_u *fromstr;
12599 char_u *tostr;
12600 char_u *p;
12601#ifdef FEAT_MBYTE
12602 int inlen;
12603 int fromlen;
12604 int tolen;
12605 int idx;
12606 char_u *cpstr;
12607 int cplen;
12608 int first = TRUE;
12609#endif
12610 char_u buf[NUMBUFLEN];
12611 char_u buf2[NUMBUFLEN];
12612 garray_T ga;
12613
12614 in_str = get_tv_string(&argvars[0]);
12615 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
12616 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
12617
12618 /* Default return value: empty string. */
12619 rettv->v_type = VAR_STRING;
12620 rettv->vval.v_string = NULL;
12621 if (fromstr == NULL || tostr == NULL)
12622 return; /* type error; errmsg already given */
12623 ga_init2(&ga, (int)sizeof(char), 80);
12624
12625#ifdef FEAT_MBYTE
12626 if (!has_mbyte)
12627#endif
12628 /* not multi-byte: fromstr and tostr must be the same length */
12629 if (STRLEN(fromstr) != STRLEN(tostr))
12630 {
12631#ifdef FEAT_MBYTE
12632error:
12633#endif
12634 EMSG2(_(e_invarg2), fromstr);
12635 ga_clear(&ga);
12636 return;
12637 }
12638
12639 /* fromstr and tostr have to contain the same number of chars */
12640 while (*in_str != NUL)
12641 {
12642#ifdef FEAT_MBYTE
12643 if (has_mbyte)
12644 {
12645 inlen = (*mb_ptr2len)(in_str);
12646 cpstr = in_str;
12647 cplen = inlen;
12648 idx = 0;
12649 for (p = fromstr; *p != NUL; p += fromlen)
12650 {
12651 fromlen = (*mb_ptr2len)(p);
12652 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
12653 {
12654 for (p = tostr; *p != NUL; p += tolen)
12655 {
12656 tolen = (*mb_ptr2len)(p);
12657 if (idx-- == 0)
12658 {
12659 cplen = tolen;
12660 cpstr = p;
12661 break;
12662 }
12663 }
12664 if (*p == NUL) /* tostr is shorter than fromstr */
12665 goto error;
12666 break;
12667 }
12668 ++idx;
12669 }
12670
12671 if (first && cpstr == in_str)
12672 {
12673 /* Check that fromstr and tostr have the same number of
12674 * (multi-byte) characters. Done only once when a character
12675 * of in_str doesn't appear in fromstr. */
12676 first = FALSE;
12677 for (p = tostr; *p != NUL; p += tolen)
12678 {
12679 tolen = (*mb_ptr2len)(p);
12680 --idx;
12681 }
12682 if (idx != 0)
12683 goto error;
12684 }
12685
12686 (void)ga_grow(&ga, cplen);
12687 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
12688 ga.ga_len += cplen;
12689
12690 in_str += inlen;
12691 }
12692 else
12693#endif
12694 {
12695 /* When not using multi-byte chars we can do it faster. */
12696 p = vim_strchr(fromstr, *in_str);
12697 if (p != NULL)
12698 ga_append(&ga, tostr[p - fromstr]);
12699 else
12700 ga_append(&ga, *in_str);
12701 ++in_str;
12702 }
12703 }
12704
12705 /* add a terminating NUL */
12706 (void)ga_grow(&ga, 1);
12707 ga_append(&ga, NUL);
12708
12709 rettv->vval.v_string = ga.ga_data;
12710}
12711
12712#ifdef FEAT_FLOAT
12713/*
12714 * "trunc({float})" function
12715 */
12716 static void
12717f_trunc(typval_T *argvars, typval_T *rettv)
12718{
12719 float_T f = 0.0;
12720
12721 rettv->v_type = VAR_FLOAT;
12722 if (get_float_arg(argvars, &f) == OK)
12723 /* trunc() is not in C90, use floor() or ceil() instead. */
12724 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
12725 else
12726 rettv->vval.v_float = 0.0;
12727}
12728#endif
12729
12730/*
12731 * "type(expr)" function
12732 */
12733 static void
12734f_type(typval_T *argvars, typval_T *rettv)
12735{
12736 int n = -1;
12737
12738 switch (argvars[0].v_type)
12739 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020012740 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
12741 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012742 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020012743 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
12744 case VAR_LIST: n = VAR_TYPE_LIST; break;
12745 case VAR_DICT: n = VAR_TYPE_DICT; break;
12746 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012747 case VAR_SPECIAL:
12748 if (argvars[0].vval.v_number == VVAL_FALSE
12749 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020012750 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012751 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020012752 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012753 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020012754 case VAR_JOB: n = VAR_TYPE_JOB; break;
12755 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012756 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010012757 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012758 n = -1;
12759 break;
12760 }
12761 rettv->vval.v_number = n;
12762}
12763
12764/*
12765 * "undofile(name)" function
12766 */
12767 static void
12768f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
12769{
12770 rettv->v_type = VAR_STRING;
12771#ifdef FEAT_PERSISTENT_UNDO
12772 {
12773 char_u *fname = get_tv_string(&argvars[0]);
12774
12775 if (*fname == NUL)
12776 {
12777 /* If there is no file name there will be no undo file. */
12778 rettv->vval.v_string = NULL;
12779 }
12780 else
12781 {
12782 char_u *ffname = FullName_save(fname, FALSE);
12783
12784 if (ffname != NULL)
12785 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
12786 vim_free(ffname);
12787 }
12788 }
12789#else
12790 rettv->vval.v_string = NULL;
12791#endif
12792}
12793
12794/*
12795 * "undotree()" function
12796 */
12797 static void
12798f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
12799{
12800 if (rettv_dict_alloc(rettv) == OK)
12801 {
12802 dict_T *dict = rettv->vval.v_dict;
12803 list_T *list;
12804
12805 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
12806 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
12807 dict_add_nr_str(dict, "save_last",
12808 (long)curbuf->b_u_save_nr_last, NULL);
12809 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
12810 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
12811 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
12812
12813 list = list_alloc();
12814 if (list != NULL)
12815 {
12816 u_eval_tree(curbuf->b_u_oldhead, list);
12817 dict_add_list(dict, "entries", list);
12818 }
12819 }
12820}
12821
12822/*
12823 * "values(dict)" function
12824 */
12825 static void
12826f_values(typval_T *argvars, typval_T *rettv)
12827{
12828 dict_list(argvars, rettv, 1);
12829}
12830
12831/*
12832 * "virtcol(string)" function
12833 */
12834 static void
12835f_virtcol(typval_T *argvars, typval_T *rettv)
12836{
12837 colnr_T vcol = 0;
12838 pos_T *fp;
12839 int fnum = curbuf->b_fnum;
12840
12841 fp = var2fpos(&argvars[0], FALSE, &fnum);
12842 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
12843 && fnum == curbuf->b_fnum)
12844 {
12845 getvvcol(curwin, fp, NULL, NULL, &vcol);
12846 ++vcol;
12847 }
12848
12849 rettv->vval.v_number = vcol;
12850}
12851
12852/*
12853 * "visualmode()" function
12854 */
12855 static void
12856f_visualmode(typval_T *argvars, typval_T *rettv)
12857{
12858 char_u str[2];
12859
12860 rettv->v_type = VAR_STRING;
12861 str[0] = curbuf->b_visual_mode_eval;
12862 str[1] = NUL;
12863 rettv->vval.v_string = vim_strsave(str);
12864
12865 /* A non-zero number or non-empty string argument: reset mode. */
12866 if (non_zero_arg(&argvars[0]))
12867 curbuf->b_visual_mode_eval = NUL;
12868}
12869
12870/*
12871 * "wildmenumode()" function
12872 */
12873 static void
12874f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12875{
12876#ifdef FEAT_WILDMENU
12877 if (wild_menu_showing)
12878 rettv->vval.v_number = 1;
12879#endif
12880}
12881
12882/*
12883 * "winbufnr(nr)" function
12884 */
12885 static void
12886f_winbufnr(typval_T *argvars, typval_T *rettv)
12887{
12888 win_T *wp;
12889
12890 wp = find_win_by_nr(&argvars[0], NULL);
12891 if (wp == NULL)
12892 rettv->vval.v_number = -1;
12893 else
12894 rettv->vval.v_number = wp->w_buffer->b_fnum;
12895}
12896
12897/*
12898 * "wincol()" function
12899 */
12900 static void
12901f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
12902{
12903 validate_cursor();
12904 rettv->vval.v_number = curwin->w_wcol + 1;
12905}
12906
12907/*
12908 * "winheight(nr)" function
12909 */
12910 static void
12911f_winheight(typval_T *argvars, typval_T *rettv)
12912{
12913 win_T *wp;
12914
12915 wp = find_win_by_nr(&argvars[0], NULL);
12916 if (wp == NULL)
12917 rettv->vval.v_number = -1;
12918 else
12919 rettv->vval.v_number = wp->w_height;
12920}
12921
12922/*
12923 * "winline()" function
12924 */
12925 static void
12926f_winline(typval_T *argvars UNUSED, typval_T *rettv)
12927{
12928 validate_cursor();
12929 rettv->vval.v_number = curwin->w_wrow + 1;
12930}
12931
12932/*
12933 * "winnr()" function
12934 */
12935 static void
12936f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
12937{
12938 int nr = 1;
12939
12940#ifdef FEAT_WINDOWS
12941 nr = get_winnr(curtab, &argvars[0]);
12942#endif
12943 rettv->vval.v_number = nr;
12944}
12945
12946/*
12947 * "winrestcmd()" function
12948 */
12949 static void
12950f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
12951{
12952#ifdef FEAT_WINDOWS
12953 win_T *wp;
12954 int winnr = 1;
12955 garray_T ga;
12956 char_u buf[50];
12957
12958 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020012959 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012960 {
12961 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
12962 ga_concat(&ga, buf);
12963 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
12964 ga_concat(&ga, buf);
12965 ++winnr;
12966 }
12967 ga_append(&ga, NUL);
12968
12969 rettv->vval.v_string = ga.ga_data;
12970#else
12971 rettv->vval.v_string = NULL;
12972#endif
12973 rettv->v_type = VAR_STRING;
12974}
12975
12976/*
12977 * "winrestview()" function
12978 */
12979 static void
12980f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
12981{
12982 dict_T *dict;
12983
12984 if (argvars[0].v_type != VAR_DICT
12985 || (dict = argvars[0].vval.v_dict) == NULL)
12986 EMSG(_(e_invarg));
12987 else
12988 {
12989 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
12990 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
12991 if (dict_find(dict, (char_u *)"col", -1) != NULL)
12992 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
12993#ifdef FEAT_VIRTUALEDIT
12994 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
12995 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
12996#endif
12997 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
12998 {
12999 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
13000 curwin->w_set_curswant = FALSE;
13001 }
13002
13003 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
13004 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
13005#ifdef FEAT_DIFF
13006 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
13007 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
13008#endif
13009 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
13010 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
13011 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
13012 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
13013
13014 check_cursor();
13015 win_new_height(curwin, curwin->w_height);
13016# ifdef FEAT_WINDOWS
13017 win_new_width(curwin, W_WIDTH(curwin));
13018# endif
13019 changed_window_setting();
13020
13021 if (curwin->w_topline <= 0)
13022 curwin->w_topline = 1;
13023 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
13024 curwin->w_topline = curbuf->b_ml.ml_line_count;
13025#ifdef FEAT_DIFF
13026 check_topfill(curwin, TRUE);
13027#endif
13028 }
13029}
13030
13031/*
13032 * "winsaveview()" function
13033 */
13034 static void
13035f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
13036{
13037 dict_T *dict;
13038
13039 if (rettv_dict_alloc(rettv) == FAIL)
13040 return;
13041 dict = rettv->vval.v_dict;
13042
13043 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
13044 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
13045#ifdef FEAT_VIRTUALEDIT
13046 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
13047#endif
13048 update_curswant();
13049 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
13050
13051 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
13052#ifdef FEAT_DIFF
13053 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
13054#endif
13055 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
13056 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
13057}
13058
13059/*
13060 * "winwidth(nr)" function
13061 */
13062 static void
13063f_winwidth(typval_T *argvars, typval_T *rettv)
13064{
13065 win_T *wp;
13066
13067 wp = find_win_by_nr(&argvars[0], NULL);
13068 if (wp == NULL)
13069 rettv->vval.v_number = -1;
13070 else
13071#ifdef FEAT_WINDOWS
13072 rettv->vval.v_number = wp->w_width;
13073#else
13074 rettv->vval.v_number = Columns;
13075#endif
13076}
13077
13078/*
13079 * "wordcount()" function
13080 */
13081 static void
13082f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
13083{
13084 if (rettv_dict_alloc(rettv) == FAIL)
13085 return;
13086 cursor_pos_info(rettv->vval.v_dict);
13087}
13088
13089/*
13090 * "writefile()" function
13091 */
13092 static void
13093f_writefile(typval_T *argvars, typval_T *rettv)
13094{
13095 int binary = FALSE;
13096 int append = FALSE;
13097 char_u *fname;
13098 FILE *fd;
13099 int ret = 0;
13100
13101 if (check_restricted() || check_secure())
13102 return;
13103
13104 if (argvars[0].v_type != VAR_LIST)
13105 {
13106 EMSG2(_(e_listarg), "writefile()");
13107 return;
13108 }
13109 if (argvars[0].vval.v_list == NULL)
13110 return;
13111
13112 if (argvars[2].v_type != VAR_UNKNOWN)
13113 {
13114 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
13115 binary = TRUE;
13116 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
13117 append = TRUE;
13118 }
13119
13120 /* Always open the file in binary mode, library functions have a mind of
13121 * their own about CR-LF conversion. */
13122 fname = get_tv_string(&argvars[1]);
13123 if (*fname == NUL || (fd = mch_fopen((char *)fname,
13124 append ? APPENDBIN : WRITEBIN)) == NULL)
13125 {
13126 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
13127 ret = -1;
13128 }
13129 else
13130 {
13131 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
13132 ret = -1;
13133 fclose(fd);
13134 }
13135
13136 rettv->vval.v_number = ret;
13137}
13138
13139/*
13140 * "xor(expr, expr)" function
13141 */
13142 static void
13143f_xor(typval_T *argvars, typval_T *rettv)
13144{
13145 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
13146 ^ get_tv_number_chk(&argvars[1], NULL);
13147}
13148
13149
13150#endif /* FEAT_EVAL */