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