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