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