blob: 7f433a5a6fe47a0180a5244c8894c5b137073985 [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;
2542 static int changedtick = 0;
2543 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
2553 || changedtick != curbuf->b_changedtick
2554 || 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;
2575 changedtick = curbuf->b_changedtick;
2576 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);
3960 dict_add_nr_str(dict, "changedtick", buf->b_changedtick, NULL);
3961 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 }
4193 else if (STRCMP(varname, "changedtick") == 0)
4194 {
4195 rettv->v_type = VAR_NUMBER;
4196 rettv->vval.v_number = curbuf->b_changedtick;
4197 done = TRUE;
4198 }
4199 else
4200 {
4201 /* Look up the variable. */
4202 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4203 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4204 'b', varname, FALSE);
4205 if (v != NULL)
4206 {
4207 copy_tv(&v->di_tv, rettv);
4208 done = TRUE;
4209 }
4210 }
4211
4212 /* restore previous notion of curbuf */
4213 curbuf = save_curbuf;
4214 }
4215
4216 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4217 /* use the default value */
4218 copy_tv(&argvars[2], rettv);
4219
4220 --emsg_off;
4221}
4222
4223/*
4224 * "getchar()" function
4225 */
4226 static void
4227f_getchar(typval_T *argvars, typval_T *rettv)
4228{
4229 varnumber_T n;
4230 int error = FALSE;
4231
4232 /* Position the cursor. Needed after a message that ends in a space. */
4233 windgoto(msg_row, msg_col);
4234
4235 ++no_mapping;
4236 ++allow_keys;
4237 for (;;)
4238 {
4239 if (argvars[0].v_type == VAR_UNKNOWN)
4240 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004241 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004242 else if (get_tv_number_chk(&argvars[0], &error) == 1)
4243 /* getchar(1): only check if char avail */
4244 n = vpeekc_any();
4245 else if (error || vpeekc_any() == NUL)
4246 /* illegal argument or getchar(0) and no char avail: return zero */
4247 n = 0;
4248 else
4249 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004250 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004251
4252 if (n == K_IGNORE)
4253 continue;
4254 break;
4255 }
4256 --no_mapping;
4257 --allow_keys;
4258
4259 set_vim_var_nr(VV_MOUSE_WIN, 0);
4260 set_vim_var_nr(VV_MOUSE_WINID, 0);
4261 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4262 set_vim_var_nr(VV_MOUSE_COL, 0);
4263
4264 rettv->vval.v_number = n;
4265 if (IS_SPECIAL(n) || mod_mask != 0)
4266 {
4267 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4268 int i = 0;
4269
4270 /* Turn a special key into three bytes, plus modifier. */
4271 if (mod_mask != 0)
4272 {
4273 temp[i++] = K_SPECIAL;
4274 temp[i++] = KS_MODIFIER;
4275 temp[i++] = mod_mask;
4276 }
4277 if (IS_SPECIAL(n))
4278 {
4279 temp[i++] = K_SPECIAL;
4280 temp[i++] = K_SECOND(n);
4281 temp[i++] = K_THIRD(n);
4282 }
4283#ifdef FEAT_MBYTE
4284 else if (has_mbyte)
4285 i += (*mb_char2bytes)(n, temp + i);
4286#endif
4287 else
4288 temp[i++] = n;
4289 temp[i++] = NUL;
4290 rettv->v_type = VAR_STRING;
4291 rettv->vval.v_string = vim_strsave(temp);
4292
4293#ifdef FEAT_MOUSE
4294 if (is_mouse_key(n))
4295 {
4296 int row = mouse_row;
4297 int col = mouse_col;
4298 win_T *win;
4299 linenr_T lnum;
4300# ifdef FEAT_WINDOWS
4301 win_T *wp;
4302# endif
4303 int winnr = 1;
4304
4305 if (row >= 0 && col >= 0)
4306 {
4307 /* Find the window at the mouse coordinates and compute the
4308 * text position. */
4309 win = mouse_find_win(&row, &col);
4310 (void)mouse_comp_pos(win, &row, &col, &lnum);
4311# ifdef FEAT_WINDOWS
4312 for (wp = firstwin; wp != win; wp = wp->w_next)
4313 ++winnr;
4314# endif
4315 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4316 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4317 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4318 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4319 }
4320 }
4321#endif
4322 }
4323}
4324
4325/*
4326 * "getcharmod()" function
4327 */
4328 static void
4329f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4330{
4331 rettv->vval.v_number = mod_mask;
4332}
4333
4334/*
4335 * "getcharsearch()" function
4336 */
4337 static void
4338f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4339{
4340 if (rettv_dict_alloc(rettv) != FAIL)
4341 {
4342 dict_T *dict = rettv->vval.v_dict;
4343
4344 dict_add_nr_str(dict, "char", 0L, last_csearch());
4345 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
4346 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
4347 }
4348}
4349
4350/*
4351 * "getcmdline()" function
4352 */
4353 static void
4354f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4355{
4356 rettv->v_type = VAR_STRING;
4357 rettv->vval.v_string = get_cmdline_str();
4358}
4359
4360/*
4361 * "getcmdpos()" function
4362 */
4363 static void
4364f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4365{
4366 rettv->vval.v_number = get_cmdline_pos() + 1;
4367}
4368
4369/*
4370 * "getcmdtype()" function
4371 */
4372 static void
4373f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4374{
4375 rettv->v_type = VAR_STRING;
4376 rettv->vval.v_string = alloc(2);
4377 if (rettv->vval.v_string != NULL)
4378 {
4379 rettv->vval.v_string[0] = get_cmdline_type();
4380 rettv->vval.v_string[1] = NUL;
4381 }
4382}
4383
4384/*
4385 * "getcmdwintype()" function
4386 */
4387 static void
4388f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4389{
4390 rettv->v_type = VAR_STRING;
4391 rettv->vval.v_string = NULL;
4392#ifdef FEAT_CMDWIN
4393 rettv->vval.v_string = alloc(2);
4394 if (rettv->vval.v_string != NULL)
4395 {
4396 rettv->vval.v_string[0] = cmdwin_type;
4397 rettv->vval.v_string[1] = NUL;
4398 }
4399#endif
4400}
4401
4402#if defined(FEAT_CMDL_COMPL)
4403/*
4404 * "getcompletion()" function
4405 */
4406 static void
4407f_getcompletion(typval_T *argvars, typval_T *rettv)
4408{
4409 char_u *pat;
4410 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004411 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004412 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
4413 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004414
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004415 if (argvars[2].v_type != VAR_UNKNOWN)
4416 filtered = get_tv_number_chk(&argvars[2], NULL);
4417
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004418 if (p_wic)
4419 options |= WILD_ICASE;
4420
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004421 /* For filtered results, 'wildignore' is used */
4422 if (!filtered)
4423 options |= WILD_KEEP_ALL;
4424
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004425 ExpandInit(&xpc);
4426 xpc.xp_pattern = get_tv_string(&argvars[0]);
4427 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4428 xpc.xp_context = cmdcomplete_str_to_type(get_tv_string(&argvars[1]));
4429 if (xpc.xp_context == EXPAND_NOTHING)
4430 {
4431 if (argvars[1].v_type == VAR_STRING)
4432 EMSG2(_(e_invarg2), argvars[1].vval.v_string);
4433 else
4434 EMSG(_(e_invarg));
4435 return;
4436 }
4437
4438# if defined(FEAT_MENU)
4439 if (xpc.xp_context == EXPAND_MENUS)
4440 {
4441 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4442 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4443 }
4444# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02004445#ifdef FEAT_CSCOPE
4446 if (xpc.xp_context == EXPAND_CSCOPE)
4447 {
4448 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4449 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4450 }
4451#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02004452#ifdef FEAT_SIGNS
4453 if (xpc.xp_context == EXPAND_SIGN)
4454 {
4455 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4456 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4457 }
4458#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004459
4460 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4461 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
4462 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004463 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004464
4465 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4466
4467 for (i = 0; i < xpc.xp_numfiles; i++)
4468 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4469 }
4470 vim_free(pat);
4471 ExpandCleanup(&xpc);
4472}
4473#endif
4474
4475/*
4476 * "getcwd()" function
4477 */
4478 static void
4479f_getcwd(typval_T *argvars, typval_T *rettv)
4480{
4481 win_T *wp = NULL;
4482 char_u *cwd;
4483
4484 rettv->v_type = VAR_STRING;
4485 rettv->vval.v_string = NULL;
4486
4487 wp = find_tabwin(&argvars[0], &argvars[1]);
4488 if (wp != NULL)
4489 {
4490 if (wp->w_localdir != NULL)
4491 rettv->vval.v_string = vim_strsave(wp->w_localdir);
4492 else if (globaldir != NULL)
4493 rettv->vval.v_string = vim_strsave(globaldir);
4494 else
4495 {
4496 cwd = alloc(MAXPATHL);
4497 if (cwd != NULL)
4498 {
4499 if (mch_dirname(cwd, MAXPATHL) != FAIL)
4500 rettv->vval.v_string = vim_strsave(cwd);
4501 vim_free(cwd);
4502 }
4503 }
4504#ifdef BACKSLASH_IN_FILENAME
4505 if (rettv->vval.v_string != NULL)
4506 slash_adjust(rettv->vval.v_string);
4507#endif
4508 }
4509}
4510
4511/*
4512 * "getfontname()" function
4513 */
4514 static void
4515f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4516{
4517 rettv->v_type = VAR_STRING;
4518 rettv->vval.v_string = NULL;
4519#ifdef FEAT_GUI
4520 if (gui.in_use)
4521 {
4522 GuiFont font;
4523 char_u *name = NULL;
4524
4525 if (argvars[0].v_type == VAR_UNKNOWN)
4526 {
4527 /* Get the "Normal" font. Either the name saved by
4528 * hl_set_font_name() or from the font ID. */
4529 font = gui.norm_font;
4530 name = hl_get_font_name();
4531 }
4532 else
4533 {
4534 name = get_tv_string(&argvars[0]);
4535 if (STRCMP(name, "*") == 0) /* don't use font dialog */
4536 return;
4537 font = gui_mch_get_font(name, FALSE);
4538 if (font == NOFONT)
4539 return; /* Invalid font name, return empty string. */
4540 }
4541 rettv->vval.v_string = gui_mch_get_fontname(font, name);
4542 if (argvars[0].v_type != VAR_UNKNOWN)
4543 gui_mch_free_font(font);
4544 }
4545#endif
4546}
4547
4548/*
4549 * "getfperm({fname})" function
4550 */
4551 static void
4552f_getfperm(typval_T *argvars, typval_T *rettv)
4553{
4554 char_u *fname;
4555 stat_T st;
4556 char_u *perm = NULL;
4557 char_u flags[] = "rwx";
4558 int i;
4559
4560 fname = get_tv_string(&argvars[0]);
4561
4562 rettv->v_type = VAR_STRING;
4563 if (mch_stat((char *)fname, &st) >= 0)
4564 {
4565 perm = vim_strsave((char_u *)"---------");
4566 if (perm != NULL)
4567 {
4568 for (i = 0; i < 9; i++)
4569 {
4570 if (st.st_mode & (1 << (8 - i)))
4571 perm[i] = flags[i % 3];
4572 }
4573 }
4574 }
4575 rettv->vval.v_string = perm;
4576}
4577
4578/*
4579 * "getfsize({fname})" function
4580 */
4581 static void
4582f_getfsize(typval_T *argvars, typval_T *rettv)
4583{
4584 char_u *fname;
4585 stat_T st;
4586
4587 fname = get_tv_string(&argvars[0]);
4588
4589 rettv->v_type = VAR_NUMBER;
4590
4591 if (mch_stat((char *)fname, &st) >= 0)
4592 {
4593 if (mch_isdir(fname))
4594 rettv->vval.v_number = 0;
4595 else
4596 {
4597 rettv->vval.v_number = (varnumber_T)st.st_size;
4598
4599 /* non-perfect check for overflow */
4600 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
4601 rettv->vval.v_number = -2;
4602 }
4603 }
4604 else
4605 rettv->vval.v_number = -1;
4606}
4607
4608/*
4609 * "getftime({fname})" function
4610 */
4611 static void
4612f_getftime(typval_T *argvars, typval_T *rettv)
4613{
4614 char_u *fname;
4615 stat_T st;
4616
4617 fname = get_tv_string(&argvars[0]);
4618
4619 if (mch_stat((char *)fname, &st) >= 0)
4620 rettv->vval.v_number = (varnumber_T)st.st_mtime;
4621 else
4622 rettv->vval.v_number = -1;
4623}
4624
4625/*
4626 * "getftype({fname})" function
4627 */
4628 static void
4629f_getftype(typval_T *argvars, typval_T *rettv)
4630{
4631 char_u *fname;
4632 stat_T st;
4633 char_u *type = NULL;
4634 char *t;
4635
4636 fname = get_tv_string(&argvars[0]);
4637
4638 rettv->v_type = VAR_STRING;
4639 if (mch_lstat((char *)fname, &st) >= 0)
4640 {
4641#ifdef S_ISREG
4642 if (S_ISREG(st.st_mode))
4643 t = "file";
4644 else if (S_ISDIR(st.st_mode))
4645 t = "dir";
4646# ifdef S_ISLNK
4647 else if (S_ISLNK(st.st_mode))
4648 t = "link";
4649# endif
4650# ifdef S_ISBLK
4651 else if (S_ISBLK(st.st_mode))
4652 t = "bdev";
4653# endif
4654# ifdef S_ISCHR
4655 else if (S_ISCHR(st.st_mode))
4656 t = "cdev";
4657# endif
4658# ifdef S_ISFIFO
4659 else if (S_ISFIFO(st.st_mode))
4660 t = "fifo";
4661# endif
4662# ifdef S_ISSOCK
4663 else if (S_ISSOCK(st.st_mode))
4664 t = "fifo";
4665# endif
4666 else
4667 t = "other";
4668#else
4669# ifdef S_IFMT
4670 switch (st.st_mode & S_IFMT)
4671 {
4672 case S_IFREG: t = "file"; break;
4673 case S_IFDIR: t = "dir"; break;
4674# ifdef S_IFLNK
4675 case S_IFLNK: t = "link"; break;
4676# endif
4677# ifdef S_IFBLK
4678 case S_IFBLK: t = "bdev"; break;
4679# endif
4680# ifdef S_IFCHR
4681 case S_IFCHR: t = "cdev"; break;
4682# endif
4683# ifdef S_IFIFO
4684 case S_IFIFO: t = "fifo"; break;
4685# endif
4686# ifdef S_IFSOCK
4687 case S_IFSOCK: t = "socket"; break;
4688# endif
4689 default: t = "other";
4690 }
4691# else
4692 if (mch_isdir(fname))
4693 t = "dir";
4694 else
4695 t = "file";
4696# endif
4697#endif
4698 type = vim_strsave((char_u *)t);
4699 }
4700 rettv->vval.v_string = type;
4701}
4702
4703/*
4704 * "getline(lnum, [end])" function
4705 */
4706 static void
4707f_getline(typval_T *argvars, typval_T *rettv)
4708{
4709 linenr_T lnum;
4710 linenr_T end;
4711 int retlist;
4712
4713 lnum = get_tv_lnum(argvars);
4714 if (argvars[1].v_type == VAR_UNKNOWN)
4715 {
4716 end = 0;
4717 retlist = FALSE;
4718 }
4719 else
4720 {
4721 end = get_tv_lnum(&argvars[1]);
4722 retlist = TRUE;
4723 }
4724
4725 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
4726}
4727
Bram Moolenaar4ae20952016-08-13 15:29:14 +02004728#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02004729 static void
4730get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
4731{
Bram Moolenaard823fa92016-08-12 16:29:27 +02004732 if (what_arg->v_type == VAR_UNKNOWN)
4733 {
4734 if (rettv_list_alloc(rettv) == OK)
4735 if (is_qf || wp != NULL)
4736 (void)get_errorlist(wp, -1, rettv->vval.v_list);
4737 }
4738 else
4739 {
4740 if (rettv_dict_alloc(rettv) == OK)
4741 if (is_qf || (wp != NULL))
4742 {
4743 if (what_arg->v_type == VAR_DICT)
4744 {
4745 dict_T *d = what_arg->vval.v_dict;
4746
4747 if (d != NULL)
4748 get_errorlist_properties(wp, d, rettv->vval.v_dict);
4749 }
4750 else
4751 EMSG(_(e_dictreq));
4752 }
4753 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004754}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02004755#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02004756
4757/*
4758 * "getloclist()" function
4759 */
4760 static void
4761f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4762{
4763#ifdef FEAT_QUICKFIX
4764 win_T *wp;
4765
4766 wp = find_win_by_nr(&argvars[0], NULL);
4767 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
4768#endif
4769}
4770
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004771/*
4772 * "getmatches()" function
4773 */
4774 static void
4775f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4776{
4777#ifdef FEAT_SEARCH_EXTRA
4778 dict_T *dict;
4779 matchitem_T *cur = curwin->w_match_head;
4780 int i;
4781
4782 if (rettv_list_alloc(rettv) == OK)
4783 {
4784 while (cur != NULL)
4785 {
4786 dict = dict_alloc();
4787 if (dict == NULL)
4788 return;
4789 if (cur->match.regprog == NULL)
4790 {
4791 /* match added with matchaddpos() */
4792 for (i = 0; i < MAXPOSMATCH; ++i)
4793 {
4794 llpos_T *llpos;
4795 char buf[6];
4796 list_T *l;
4797
4798 llpos = &cur->pos.pos[i];
4799 if (llpos->lnum == 0)
4800 break;
4801 l = list_alloc();
4802 if (l == NULL)
4803 break;
4804 list_append_number(l, (varnumber_T)llpos->lnum);
4805 if (llpos->col > 0)
4806 {
4807 list_append_number(l, (varnumber_T)llpos->col);
4808 list_append_number(l, (varnumber_T)llpos->len);
4809 }
4810 sprintf(buf, "pos%d", i + 1);
4811 dict_add_list(dict, buf, l);
4812 }
4813 }
4814 else
4815 {
4816 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
4817 }
4818 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
4819 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
4820 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
4821# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
4822 if (cur->conceal_char)
4823 {
4824 char_u buf[MB_MAXBYTES + 1];
4825
4826 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
4827 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
4828 }
4829# endif
4830 list_append_dict(rettv->vval.v_list, dict);
4831 cur = cur->next;
4832 }
4833 }
4834#endif
4835}
4836
4837/*
4838 * "getpid()" function
4839 */
4840 static void
4841f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
4842{
4843 rettv->vval.v_number = mch_get_pid();
4844}
4845
4846 static void
4847getpos_both(
4848 typval_T *argvars,
4849 typval_T *rettv,
4850 int getcurpos)
4851{
4852 pos_T *fp;
4853 list_T *l;
4854 int fnum = -1;
4855
4856 if (rettv_list_alloc(rettv) == OK)
4857 {
4858 l = rettv->vval.v_list;
4859 if (getcurpos)
4860 fp = &curwin->w_cursor;
4861 else
4862 fp = var2fpos(&argvars[0], TRUE, &fnum);
4863 if (fnum != -1)
4864 list_append_number(l, (varnumber_T)fnum);
4865 else
4866 list_append_number(l, (varnumber_T)0);
4867 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
4868 : (varnumber_T)0);
4869 list_append_number(l, (fp != NULL)
4870 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
4871 : (varnumber_T)0);
4872 list_append_number(l,
4873#ifdef FEAT_VIRTUALEDIT
4874 (fp != NULL) ? (varnumber_T)fp->coladd :
4875#endif
4876 (varnumber_T)0);
4877 if (getcurpos)
4878 {
4879 update_curswant();
4880 list_append_number(l, curwin->w_curswant == MAXCOL ?
4881 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
4882 }
4883 }
4884 else
4885 rettv->vval.v_number = FALSE;
4886}
4887
4888
4889/*
4890 * "getcurpos()" function
4891 */
4892 static void
4893f_getcurpos(typval_T *argvars, typval_T *rettv)
4894{
4895 getpos_both(argvars, rettv, TRUE);
4896}
4897
4898/*
4899 * "getpos(string)" function
4900 */
4901 static void
4902f_getpos(typval_T *argvars, typval_T *rettv)
4903{
4904 getpos_both(argvars, rettv, FALSE);
4905}
4906
4907/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004908 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004909 */
4910 static void
4911f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4912{
4913#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02004914 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004915#endif
4916}
4917
4918/*
4919 * "getreg()" function
4920 */
4921 static void
4922f_getreg(typval_T *argvars, typval_T *rettv)
4923{
4924 char_u *strregname;
4925 int regname;
4926 int arg2 = FALSE;
4927 int return_list = FALSE;
4928 int error = FALSE;
4929
4930 if (argvars[0].v_type != VAR_UNKNOWN)
4931 {
4932 strregname = get_tv_string_chk(&argvars[0]);
4933 error = strregname == NULL;
4934 if (argvars[1].v_type != VAR_UNKNOWN)
4935 {
4936 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
4937 if (!error && argvars[2].v_type != VAR_UNKNOWN)
4938 return_list = (int)get_tv_number_chk(&argvars[2], &error);
4939 }
4940 }
4941 else
4942 strregname = get_vim_var_str(VV_REG);
4943
4944 if (error)
4945 return;
4946
4947 regname = (strregname == NULL ? '"' : *strregname);
4948 if (regname == 0)
4949 regname = '"';
4950
4951 if (return_list)
4952 {
4953 rettv->v_type = VAR_LIST;
4954 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
4955 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
4956 if (rettv->vval.v_list == NULL)
4957 (void)rettv_list_alloc(rettv);
4958 else
4959 ++rettv->vval.v_list->lv_refcount;
4960 }
4961 else
4962 {
4963 rettv->v_type = VAR_STRING;
4964 rettv->vval.v_string = get_reg_contents(regname,
4965 arg2 ? GREG_EXPR_SRC : 0);
4966 }
4967}
4968
4969/*
4970 * "getregtype()" function
4971 */
4972 static void
4973f_getregtype(typval_T *argvars, typval_T *rettv)
4974{
4975 char_u *strregname;
4976 int regname;
4977 char_u buf[NUMBUFLEN + 2];
4978 long reglen = 0;
4979
4980 if (argvars[0].v_type != VAR_UNKNOWN)
4981 {
4982 strregname = get_tv_string_chk(&argvars[0]);
4983 if (strregname == NULL) /* type error; errmsg already given */
4984 {
4985 rettv->v_type = VAR_STRING;
4986 rettv->vval.v_string = NULL;
4987 return;
4988 }
4989 }
4990 else
4991 /* Default to v:register */
4992 strregname = get_vim_var_str(VV_REG);
4993
4994 regname = (strregname == NULL ? '"' : *strregname);
4995 if (regname == 0)
4996 regname = '"';
4997
4998 buf[0] = NUL;
4999 buf[1] = NUL;
5000 switch (get_reg_type(regname, &reglen))
5001 {
5002 case MLINE: buf[0] = 'V'; break;
5003 case MCHAR: buf[0] = 'v'; break;
5004 case MBLOCK:
5005 buf[0] = Ctrl_V;
5006 sprintf((char *)buf + 1, "%ld", reglen + 1);
5007 break;
5008 }
5009 rettv->v_type = VAR_STRING;
5010 rettv->vval.v_string = vim_strsave(buf);
5011}
5012
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005013#ifdef FEAT_WINDOWS
5014/*
5015 * Returns information (variables, options, etc.) about a tab page
5016 * as a dictionary.
5017 */
5018 static dict_T *
5019get_tabpage_info(tabpage_T *tp, int tp_idx)
5020{
5021 win_T *wp;
5022 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005023 list_T *l;
5024
5025 dict = dict_alloc();
5026 if (dict == NULL)
5027 return NULL;
5028
Bram Moolenaar33928832016-08-18 21:22:04 +02005029 dict_add_nr_str(dict, "tabnr", tp_idx, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005030
5031 l = list_alloc();
5032 if (l != NULL)
5033 {
5034 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5035 wp; wp = wp->w_next)
5036 list_append_number(l, (varnumber_T)wp->w_id);
5037 dict_add_list(dict, "windows", l);
5038 }
5039
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005040 /* Make a reference to tabpage variables */
5041 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005042
5043 return dict;
5044}
5045#endif
5046
5047/*
5048 * "gettabinfo()" function
5049 */
5050 static void
5051f_gettabinfo(typval_T *argvars, typval_T *rettv)
5052{
5053#ifdef FEAT_WINDOWS
5054 tabpage_T *tp, *tparg = NULL;
5055 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005056 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005057
5058 if (rettv_list_alloc(rettv) != OK)
5059 return;
5060
5061 if (argvars[0].v_type != VAR_UNKNOWN)
5062 {
5063 /* Information about one tab page */
5064 tparg = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5065 if (tparg == NULL)
5066 return;
5067 }
5068
5069 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005070 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005071 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005072 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005073 if (tparg != NULL && tp != tparg)
5074 continue;
5075 d = get_tabpage_info(tp, tpnr);
5076 if (d != NULL)
5077 list_append_dict(rettv->vval.v_list, d);
5078 if (tparg != NULL)
5079 return;
5080 }
5081#endif
5082}
5083
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005084/*
5085 * "gettabvar()" function
5086 */
5087 static void
5088f_gettabvar(typval_T *argvars, typval_T *rettv)
5089{
5090 win_T *oldcurwin;
5091 tabpage_T *tp, *oldtabpage;
5092 dictitem_T *v;
5093 char_u *varname;
5094 int done = FALSE;
5095
5096 rettv->v_type = VAR_STRING;
5097 rettv->vval.v_string = NULL;
5098
5099 varname = get_tv_string_chk(&argvars[1]);
5100 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5101 if (tp != NULL && varname != NULL)
5102 {
5103 /* Set tp to be our tabpage, temporarily. Also set the window to the
5104 * first window in the tabpage, otherwise the window is not valid. */
5105 if (switch_win(&oldcurwin, &oldtabpage,
5106 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
5107 == OK)
5108 {
5109 /* look up the variable */
5110 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5111 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5112 if (v != NULL)
5113 {
5114 copy_tv(&v->di_tv, rettv);
5115 done = TRUE;
5116 }
5117 }
5118
5119 /* restore previous notion of curwin */
5120 restore_win(oldcurwin, oldtabpage, TRUE);
5121 }
5122
5123 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5124 copy_tv(&argvars[2], rettv);
5125}
5126
5127/*
5128 * "gettabwinvar()" function
5129 */
5130 static void
5131f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5132{
5133 getwinvar(argvars, rettv, 1);
5134}
5135
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005136#ifdef FEAT_WINDOWS
5137/*
5138 * Returns information about a window as a dictionary.
5139 */
5140 static dict_T *
5141get_win_info(win_T *wp, short tpnr, short winnr)
5142{
5143 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005144
5145 dict = dict_alloc();
5146 if (dict == NULL)
5147 return NULL;
5148
Bram Moolenaar33928832016-08-18 21:22:04 +02005149 dict_add_nr_str(dict, "tabnr", tpnr, NULL);
5150 dict_add_nr_str(dict, "winnr", winnr, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005151 dict_add_nr_str(dict, "winid", wp->w_id, NULL);
5152 dict_add_nr_str(dict, "height", wp->w_height, NULL);
5153 dict_add_nr_str(dict, "width", wp->w_width, NULL);
Bram Moolenaar33928832016-08-18 21:22:04 +02005154 dict_add_nr_str(dict, "bufnr", wp->w_buffer->b_fnum, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005155
Bram Moolenaar386600f2016-08-15 22:16:25 +02005156#ifdef FEAT_QUICKFIX
5157 dict_add_nr_str(dict, "quickfix", bt_quickfix(wp->w_buffer), NULL);
5158 dict_add_nr_str(dict, "loclist",
5159 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL), NULL);
5160#endif
5161
Bram Moolenaar30567352016-08-27 21:25:44 +02005162 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005163 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005164
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005165 return dict;
5166}
5167#endif
5168
5169/*
5170 * "getwininfo()" function
5171 */
5172 static void
5173f_getwininfo(typval_T *argvars, typval_T *rettv)
5174{
5175#ifdef FEAT_WINDOWS
5176 tabpage_T *tp;
5177 win_T *wp = NULL, *wparg = NULL;
5178 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005179 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005180#endif
5181
5182 if (rettv_list_alloc(rettv) != OK)
5183 return;
5184
5185#ifdef FEAT_WINDOWS
5186 if (argvars[0].v_type != VAR_UNKNOWN)
5187 {
5188 wparg = win_id2wp(argvars);
5189 if (wparg == NULL)
5190 return;
5191 }
5192
5193 /* Collect information about either all the windows across all the tab
5194 * pages or one particular window.
5195 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005196 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005197 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005198 tabnr++;
5199 winnr = 0;
5200 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005201 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005202 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005203 if (wparg != NULL && wp != wparg)
5204 continue;
5205 d = get_win_info(wp, tabnr, winnr);
5206 if (d != NULL)
5207 list_append_dict(rettv->vval.v_list, d);
5208 if (wparg != NULL)
5209 /* found information about a specific window */
5210 return;
5211 }
5212 }
5213#endif
5214}
5215
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005216/*
5217 * "getwinposx()" function
5218 */
5219 static void
5220f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5221{
5222 rettv->vval.v_number = -1;
5223#ifdef FEAT_GUI
5224 if (gui.in_use)
5225 {
5226 int x, y;
5227
5228 if (gui_mch_get_winpos(&x, &y) == OK)
5229 rettv->vval.v_number = x;
5230 }
5231#endif
5232}
5233
5234/*
5235 * "win_findbuf()" function
5236 */
5237 static void
5238f_win_findbuf(typval_T *argvars, typval_T *rettv)
5239{
5240 if (rettv_list_alloc(rettv) != FAIL)
5241 win_findbuf(argvars, rettv->vval.v_list);
5242}
5243
5244/*
5245 * "win_getid()" function
5246 */
5247 static void
5248f_win_getid(typval_T *argvars, typval_T *rettv)
5249{
5250 rettv->vval.v_number = win_getid(argvars);
5251}
5252
5253/*
5254 * "win_gotoid()" function
5255 */
5256 static void
5257f_win_gotoid(typval_T *argvars, typval_T *rettv)
5258{
5259 rettv->vval.v_number = win_gotoid(argvars);
5260}
5261
5262/*
5263 * "win_id2tabwin()" function
5264 */
5265 static void
5266f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5267{
5268 if (rettv_list_alloc(rettv) != FAIL)
5269 win_id2tabwin(argvars, rettv->vval.v_list);
5270}
5271
5272/*
5273 * "win_id2win()" function
5274 */
5275 static void
5276f_win_id2win(typval_T *argvars, typval_T *rettv)
5277{
5278 rettv->vval.v_number = win_id2win(argvars);
5279}
5280
5281/*
5282 * "getwinposy()" function
5283 */
5284 static void
5285f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5286{
5287 rettv->vval.v_number = -1;
5288#ifdef FEAT_GUI
5289 if (gui.in_use)
5290 {
5291 int x, y;
5292
5293 if (gui_mch_get_winpos(&x, &y) == OK)
5294 rettv->vval.v_number = y;
5295 }
5296#endif
5297}
5298
5299/*
5300 * "getwinvar()" function
5301 */
5302 static void
5303f_getwinvar(typval_T *argvars, typval_T *rettv)
5304{
5305 getwinvar(argvars, rettv, 0);
5306}
5307
5308/*
5309 * "glob()" function
5310 */
5311 static void
5312f_glob(typval_T *argvars, typval_T *rettv)
5313{
5314 int options = WILD_SILENT|WILD_USE_NL;
5315 expand_T xpc;
5316 int error = FALSE;
5317
5318 /* When the optional second argument is non-zero, don't remove matches
5319 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5320 rettv->v_type = VAR_STRING;
5321 if (argvars[1].v_type != VAR_UNKNOWN)
5322 {
5323 if (get_tv_number_chk(&argvars[1], &error))
5324 options |= WILD_KEEP_ALL;
5325 if (argvars[2].v_type != VAR_UNKNOWN)
5326 {
5327 if (get_tv_number_chk(&argvars[2], &error))
5328 {
5329 rettv->v_type = VAR_LIST;
5330 rettv->vval.v_list = NULL;
5331 }
5332 if (argvars[3].v_type != VAR_UNKNOWN
5333 && get_tv_number_chk(&argvars[3], &error))
5334 options |= WILD_ALLLINKS;
5335 }
5336 }
5337 if (!error)
5338 {
5339 ExpandInit(&xpc);
5340 xpc.xp_context = EXPAND_FILES;
5341 if (p_wic)
5342 options += WILD_ICASE;
5343 if (rettv->v_type == VAR_STRING)
5344 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
5345 NULL, options, WILD_ALL);
5346 else if (rettv_list_alloc(rettv) != FAIL)
5347 {
5348 int i;
5349
5350 ExpandOne(&xpc, get_tv_string(&argvars[0]),
5351 NULL, options, WILD_ALL_KEEP);
5352 for (i = 0; i < xpc.xp_numfiles; i++)
5353 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5354
5355 ExpandCleanup(&xpc);
5356 }
5357 }
5358 else
5359 rettv->vval.v_string = NULL;
5360}
5361
5362/*
5363 * "globpath()" function
5364 */
5365 static void
5366f_globpath(typval_T *argvars, typval_T *rettv)
5367{
5368 int flags = 0;
5369 char_u buf1[NUMBUFLEN];
5370 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
5371 int error = FALSE;
5372 garray_T ga;
5373 int i;
5374
5375 /* When the optional second argument is non-zero, don't remove matches
5376 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5377 rettv->v_type = VAR_STRING;
5378 if (argvars[2].v_type != VAR_UNKNOWN)
5379 {
5380 if (get_tv_number_chk(&argvars[2], &error))
5381 flags |= WILD_KEEP_ALL;
5382 if (argvars[3].v_type != VAR_UNKNOWN)
5383 {
5384 if (get_tv_number_chk(&argvars[3], &error))
5385 {
5386 rettv->v_type = VAR_LIST;
5387 rettv->vval.v_list = NULL;
5388 }
5389 if (argvars[4].v_type != VAR_UNKNOWN
5390 && get_tv_number_chk(&argvars[4], &error))
5391 flags |= WILD_ALLLINKS;
5392 }
5393 }
5394 if (file != NULL && !error)
5395 {
5396 ga_init2(&ga, (int)sizeof(char_u *), 10);
5397 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
5398 if (rettv->v_type == VAR_STRING)
5399 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
5400 else if (rettv_list_alloc(rettv) != FAIL)
5401 for (i = 0; i < ga.ga_len; ++i)
5402 list_append_string(rettv->vval.v_list,
5403 ((char_u **)(ga.ga_data))[i], -1);
5404 ga_clear_strings(&ga);
5405 }
5406 else
5407 rettv->vval.v_string = NULL;
5408}
5409
5410/*
5411 * "glob2regpat()" function
5412 */
5413 static void
5414f_glob2regpat(typval_T *argvars, typval_T *rettv)
5415{
5416 char_u *pat = get_tv_string_chk(&argvars[0]);
5417
5418 rettv->v_type = VAR_STRING;
5419 rettv->vval.v_string = (pat == NULL)
5420 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
5421}
5422
5423/* for VIM_VERSION_ defines */
5424#include "version.h"
5425
5426/*
5427 * "has()" function
5428 */
5429 static void
5430f_has(typval_T *argvars, typval_T *rettv)
5431{
5432 int i;
5433 char_u *name;
5434 int n = FALSE;
5435 static char *(has_list[]) =
5436 {
5437#ifdef AMIGA
5438 "amiga",
5439# ifdef FEAT_ARP
5440 "arp",
5441# endif
5442#endif
5443#ifdef __BEOS__
5444 "beos",
5445#endif
5446#ifdef MACOS
5447 "mac",
5448#endif
5449#if defined(MACOS_X_UNIX)
5450 "macunix", /* built with 'darwin' enabled */
5451#endif
5452#if defined(__APPLE__) && __APPLE__ == 1
5453 "osx", /* built with or without 'darwin' enabled */
5454#endif
5455#ifdef __QNX__
5456 "qnx",
5457#endif
5458#ifdef UNIX
5459 "unix",
5460#endif
5461#ifdef VMS
5462 "vms",
5463#endif
5464#ifdef WIN32
5465 "win32",
5466#endif
5467#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
5468 "win32unix",
5469#endif
5470#if defined(WIN64) || defined(_WIN64)
5471 "win64",
5472#endif
5473#ifdef EBCDIC
5474 "ebcdic",
5475#endif
5476#ifndef CASE_INSENSITIVE_FILENAME
5477 "fname_case",
5478#endif
5479#ifdef HAVE_ACL
5480 "acl",
5481#endif
5482#ifdef FEAT_ARABIC
5483 "arabic",
5484#endif
5485#ifdef FEAT_AUTOCMD
5486 "autocmd",
5487#endif
5488#ifdef FEAT_BEVAL
5489 "balloon_eval",
5490# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
5491 "balloon_multiline",
5492# endif
5493#endif
5494#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
5495 "builtin_terms",
5496# ifdef ALL_BUILTIN_TCAPS
5497 "all_builtin_terms",
5498# endif
5499#endif
5500#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
5501 || defined(FEAT_GUI_W32) \
5502 || defined(FEAT_GUI_MOTIF))
5503 "browsefilter",
5504#endif
5505#ifdef FEAT_BYTEOFF
5506 "byte_offset",
5507#endif
5508#ifdef FEAT_JOB_CHANNEL
5509 "channel",
5510#endif
5511#ifdef FEAT_CINDENT
5512 "cindent",
5513#endif
5514#ifdef FEAT_CLIENTSERVER
5515 "clientserver",
5516#endif
5517#ifdef FEAT_CLIPBOARD
5518 "clipboard",
5519#endif
5520#ifdef FEAT_CMDL_COMPL
5521 "cmdline_compl",
5522#endif
5523#ifdef FEAT_CMDHIST
5524 "cmdline_hist",
5525#endif
5526#ifdef FEAT_COMMENTS
5527 "comments",
5528#endif
5529#ifdef FEAT_CONCEAL
5530 "conceal",
5531#endif
5532#ifdef FEAT_CRYPT
5533 "cryptv",
5534 "crypt-blowfish",
5535 "crypt-blowfish2",
5536#endif
5537#ifdef FEAT_CSCOPE
5538 "cscope",
5539#endif
5540#ifdef FEAT_CURSORBIND
5541 "cursorbind",
5542#endif
5543#ifdef CURSOR_SHAPE
5544 "cursorshape",
5545#endif
5546#ifdef DEBUG
5547 "debug",
5548#endif
5549#ifdef FEAT_CON_DIALOG
5550 "dialog_con",
5551#endif
5552#ifdef FEAT_GUI_DIALOG
5553 "dialog_gui",
5554#endif
5555#ifdef FEAT_DIFF
5556 "diff",
5557#endif
5558#ifdef FEAT_DIGRAPHS
5559 "digraphs",
5560#endif
5561#ifdef FEAT_DIRECTX
5562 "directx",
5563#endif
5564#ifdef FEAT_DND
5565 "dnd",
5566#endif
5567#ifdef FEAT_EMACS_TAGS
5568 "emacs_tags",
5569#endif
5570 "eval", /* always present, of course! */
5571 "ex_extra", /* graduated feature */
5572#ifdef FEAT_SEARCH_EXTRA
5573 "extra_search",
5574#endif
5575#ifdef FEAT_FKMAP
5576 "farsi",
5577#endif
5578#ifdef FEAT_SEARCHPATH
5579 "file_in_path",
5580#endif
5581#ifdef FEAT_FILTERPIPE
5582 "filterpipe",
5583#endif
5584#ifdef FEAT_FIND_ID
5585 "find_in_path",
5586#endif
5587#ifdef FEAT_FLOAT
5588 "float",
5589#endif
5590#ifdef FEAT_FOLDING
5591 "folding",
5592#endif
5593#ifdef FEAT_FOOTER
5594 "footer",
5595#endif
5596#if !defined(USE_SYSTEM) && defined(UNIX)
5597 "fork",
5598#endif
5599#ifdef FEAT_GETTEXT
5600 "gettext",
5601#endif
5602#ifdef FEAT_GUI
5603 "gui",
5604#endif
5605#ifdef FEAT_GUI_ATHENA
5606# ifdef FEAT_GUI_NEXTAW
5607 "gui_neXtaw",
5608# else
5609 "gui_athena",
5610# endif
5611#endif
5612#ifdef FEAT_GUI_GTK
5613 "gui_gtk",
5614# ifdef USE_GTK3
5615 "gui_gtk3",
5616# else
5617 "gui_gtk2",
5618# endif
5619#endif
5620#ifdef FEAT_GUI_GNOME
5621 "gui_gnome",
5622#endif
5623#ifdef FEAT_GUI_MAC
5624 "gui_mac",
5625#endif
5626#ifdef FEAT_GUI_MOTIF
5627 "gui_motif",
5628#endif
5629#ifdef FEAT_GUI_PHOTON
5630 "gui_photon",
5631#endif
5632#ifdef FEAT_GUI_W32
5633 "gui_win32",
5634#endif
5635#ifdef FEAT_HANGULIN
5636 "hangul_input",
5637#endif
5638#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
5639 "iconv",
5640#endif
5641#ifdef FEAT_INS_EXPAND
5642 "insert_expand",
5643#endif
5644#ifdef FEAT_JOB_CHANNEL
5645 "job",
5646#endif
5647#ifdef FEAT_JUMPLIST
5648 "jumplist",
5649#endif
5650#ifdef FEAT_KEYMAP
5651 "keymap",
5652#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02005653 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005654#ifdef FEAT_LANGMAP
5655 "langmap",
5656#endif
5657#ifdef FEAT_LIBCALL
5658 "libcall",
5659#endif
5660#ifdef FEAT_LINEBREAK
5661 "linebreak",
5662#endif
5663#ifdef FEAT_LISP
5664 "lispindent",
5665#endif
5666#ifdef FEAT_LISTCMDS
5667 "listcmds",
5668#endif
5669#ifdef FEAT_LOCALMAP
5670 "localmap",
5671#endif
5672#ifdef FEAT_LUA
5673# ifndef DYNAMIC_LUA
5674 "lua",
5675# endif
5676#endif
5677#ifdef FEAT_MENU
5678 "menu",
5679#endif
5680#ifdef FEAT_SESSION
5681 "mksession",
5682#endif
5683#ifdef FEAT_MODIFY_FNAME
5684 "modify_fname",
5685#endif
5686#ifdef FEAT_MOUSE
5687 "mouse",
5688#endif
5689#ifdef FEAT_MOUSESHAPE
5690 "mouseshape",
5691#endif
5692#if defined(UNIX) || defined(VMS)
5693# ifdef FEAT_MOUSE_DEC
5694 "mouse_dec",
5695# endif
5696# ifdef FEAT_MOUSE_GPM
5697 "mouse_gpm",
5698# endif
5699# ifdef FEAT_MOUSE_JSB
5700 "mouse_jsbterm",
5701# endif
5702# ifdef FEAT_MOUSE_NET
5703 "mouse_netterm",
5704# endif
5705# ifdef FEAT_MOUSE_PTERM
5706 "mouse_pterm",
5707# endif
5708# ifdef FEAT_MOUSE_SGR
5709 "mouse_sgr",
5710# endif
5711# ifdef FEAT_SYSMOUSE
5712 "mouse_sysmouse",
5713# endif
5714# ifdef FEAT_MOUSE_URXVT
5715 "mouse_urxvt",
5716# endif
5717# ifdef FEAT_MOUSE_XTERM
5718 "mouse_xterm",
5719# endif
5720#endif
5721#ifdef FEAT_MBYTE
5722 "multi_byte",
5723#endif
5724#ifdef FEAT_MBYTE_IME
5725 "multi_byte_ime",
5726#endif
5727#ifdef FEAT_MULTI_LANG
5728 "multi_lang",
5729#endif
5730#ifdef FEAT_MZSCHEME
5731#ifndef DYNAMIC_MZSCHEME
5732 "mzscheme",
5733#endif
5734#endif
5735#ifdef FEAT_NUM64
5736 "num64",
5737#endif
5738#ifdef FEAT_OLE
5739 "ole",
5740#endif
5741 "packages",
5742#ifdef FEAT_PATH_EXTRA
5743 "path_extra",
5744#endif
5745#ifdef FEAT_PERL
5746#ifndef DYNAMIC_PERL
5747 "perl",
5748#endif
5749#endif
5750#ifdef FEAT_PERSISTENT_UNDO
5751 "persistent_undo",
5752#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005753#if defined(FEAT_PYTHON) && !defined(DYNAMIC_PYTHON)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005754 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005755 "pythonx",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005756#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005757#if defined(FEAT_PYTHON3) && !defined(DYNAMIC_PYTHON3)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005758 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005759 "pythonx",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005760#endif
5761#ifdef FEAT_POSTSCRIPT
5762 "postscript",
5763#endif
5764#ifdef FEAT_PRINTER
5765 "printer",
5766#endif
5767#ifdef FEAT_PROFILE
5768 "profile",
5769#endif
5770#ifdef FEAT_RELTIME
5771 "reltime",
5772#endif
5773#ifdef FEAT_QUICKFIX
5774 "quickfix",
5775#endif
5776#ifdef FEAT_RIGHTLEFT
5777 "rightleft",
5778#endif
5779#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
5780 "ruby",
5781#endif
5782#ifdef FEAT_SCROLLBIND
5783 "scrollbind",
5784#endif
5785#ifdef FEAT_CMDL_INFO
5786 "showcmd",
5787 "cmdline_info",
5788#endif
5789#ifdef FEAT_SIGNS
5790 "signs",
5791#endif
5792#ifdef FEAT_SMARTINDENT
5793 "smartindent",
5794#endif
5795#ifdef STARTUPTIME
5796 "startuptime",
5797#endif
5798#ifdef FEAT_STL_OPT
5799 "statusline",
5800#endif
5801#ifdef FEAT_SUN_WORKSHOP
5802 "sun_workshop",
5803#endif
5804#ifdef FEAT_NETBEANS_INTG
5805 "netbeans_intg",
5806#endif
5807#ifdef FEAT_SPELL
5808 "spell",
5809#endif
5810#ifdef FEAT_SYN_HL
5811 "syntax",
5812#endif
5813#if defined(USE_SYSTEM) || !defined(UNIX)
5814 "system",
5815#endif
5816#ifdef FEAT_TAG_BINS
5817 "tag_binary",
5818#endif
5819#ifdef FEAT_TAG_OLDSTATIC
5820 "tag_old_static",
5821#endif
5822#ifdef FEAT_TAG_ANYWHITE
5823 "tag_any_white",
5824#endif
5825#ifdef FEAT_TCL
5826# ifndef DYNAMIC_TCL
5827 "tcl",
5828# endif
5829#endif
5830#ifdef FEAT_TERMGUICOLORS
5831 "termguicolors",
5832#endif
5833#ifdef TERMINFO
5834 "terminfo",
5835#endif
5836#ifdef FEAT_TERMRESPONSE
5837 "termresponse",
5838#endif
5839#ifdef FEAT_TEXTOBJ
5840 "textobjects",
5841#endif
5842#ifdef HAVE_TGETENT
5843 "tgetent",
5844#endif
5845#ifdef FEAT_TIMERS
5846 "timers",
5847#endif
5848#ifdef FEAT_TITLE
5849 "title",
5850#endif
5851#ifdef FEAT_TOOLBAR
5852 "toolbar",
5853#endif
5854#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
5855 "unnamedplus",
5856#endif
5857#ifdef FEAT_USR_CMDS
5858 "user-commands", /* was accidentally included in 5.4 */
5859 "user_commands",
5860#endif
5861#ifdef FEAT_VIMINFO
5862 "viminfo",
5863#endif
5864#ifdef FEAT_WINDOWS
5865 "vertsplit",
5866#endif
5867#ifdef FEAT_VIRTUALEDIT
5868 "virtualedit",
5869#endif
5870 "visual",
5871#ifdef FEAT_VISUALEXTRA
5872 "visualextra",
5873#endif
5874#ifdef FEAT_VREPLACE
5875 "vreplace",
5876#endif
5877#ifdef FEAT_WILDIGN
5878 "wildignore",
5879#endif
5880#ifdef FEAT_WILDMENU
5881 "wildmenu",
5882#endif
5883#ifdef FEAT_WINDOWS
5884 "windows",
5885#endif
5886#ifdef FEAT_WAK
5887 "winaltkeys",
5888#endif
5889#ifdef FEAT_WRITEBACKUP
5890 "writebackup",
5891#endif
5892#ifdef FEAT_XIM
5893 "xim",
5894#endif
5895#ifdef FEAT_XFONTSET
5896 "xfontset",
5897#endif
5898#ifdef FEAT_XPM_W32
5899 "xpm",
5900 "xpm_w32", /* for backward compatibility */
5901#else
5902# if defined(HAVE_XPM)
5903 "xpm",
5904# endif
5905#endif
5906#ifdef USE_XSMP
5907 "xsmp",
5908#endif
5909#ifdef USE_XSMP_INTERACT
5910 "xsmp_interact",
5911#endif
5912#ifdef FEAT_XCLIPBOARD
5913 "xterm_clipboard",
5914#endif
5915#ifdef FEAT_XTERM_SAVE
5916 "xterm_save",
5917#endif
5918#if defined(UNIX) && defined(FEAT_X11)
5919 "X11",
5920#endif
5921 NULL
5922 };
5923
5924 name = get_tv_string(&argvars[0]);
5925 for (i = 0; has_list[i] != NULL; ++i)
5926 if (STRICMP(name, has_list[i]) == 0)
5927 {
5928 n = TRUE;
5929 break;
5930 }
5931
5932 if (n == FALSE)
5933 {
5934 if (STRNICMP(name, "patch", 5) == 0)
5935 {
5936 if (name[5] == '-'
5937 && STRLEN(name) >= 11
5938 && vim_isdigit(name[6])
5939 && vim_isdigit(name[8])
5940 && vim_isdigit(name[10]))
5941 {
5942 int major = atoi((char *)name + 6);
5943 int minor = atoi((char *)name + 8);
5944
5945 /* Expect "patch-9.9.01234". */
5946 n = (major < VIM_VERSION_MAJOR
5947 || (major == VIM_VERSION_MAJOR
5948 && (minor < VIM_VERSION_MINOR
5949 || (minor == VIM_VERSION_MINOR
5950 && has_patch(atoi((char *)name + 10))))));
5951 }
5952 else
5953 n = has_patch(atoi((char *)name + 5));
5954 }
5955 else if (STRICMP(name, "vim_starting") == 0)
5956 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01005957 else if (STRICMP(name, "ttyin") == 0)
5958 n = mch_input_isatty();
5959 else if (STRICMP(name, "ttyout") == 0)
5960 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005961#ifdef FEAT_MBYTE
5962 else if (STRICMP(name, "multi_byte_encoding") == 0)
5963 n = has_mbyte;
5964#endif
5965#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
5966 else if (STRICMP(name, "balloon_multiline") == 0)
5967 n = multiline_balloon_available();
5968#endif
5969#ifdef DYNAMIC_TCL
5970 else if (STRICMP(name, "tcl") == 0)
5971 n = tcl_enabled(FALSE);
5972#endif
5973#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5974 else if (STRICMP(name, "iconv") == 0)
5975 n = iconv_enabled(FALSE);
5976#endif
5977#ifdef DYNAMIC_LUA
5978 else if (STRICMP(name, "lua") == 0)
5979 n = lua_enabled(FALSE);
5980#endif
5981#ifdef DYNAMIC_MZSCHEME
5982 else if (STRICMP(name, "mzscheme") == 0)
5983 n = mzscheme_enabled(FALSE);
5984#endif
5985#ifdef DYNAMIC_RUBY
5986 else if (STRICMP(name, "ruby") == 0)
5987 n = ruby_enabled(FALSE);
5988#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005989#ifdef DYNAMIC_PYTHON
5990 else if (STRICMP(name, "python") == 0)
5991 n = python_enabled(FALSE);
5992#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005993#ifdef DYNAMIC_PYTHON3
5994 else if (STRICMP(name, "python3") == 0)
5995 n = python3_enabled(FALSE);
5996#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01005997#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
5998 else if (STRICMP(name, "pythonx") == 0)
5999 {
6000# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6001 if (p_pyx == 0)
6002 n = python3_enabled(FALSE) || python_enabled(FALSE);
6003 else if (p_pyx == 3)
6004 n = python3_enabled(FALSE);
6005 else if (p_pyx == 2)
6006 n = python_enabled(FALSE);
6007# elif defined(DYNAMIC_PYTHON)
6008 n = python_enabled(FALSE);
6009# elif defined(DYNAMIC_PYTHON3)
6010 n = python3_enabled(FALSE);
6011# endif
6012 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006013#endif
6014#ifdef DYNAMIC_PERL
6015 else if (STRICMP(name, "perl") == 0)
6016 n = perl_enabled(FALSE);
6017#endif
6018#ifdef FEAT_GUI
6019 else if (STRICMP(name, "gui_running") == 0)
6020 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006021# ifdef FEAT_BROWSE
6022 else if (STRICMP(name, "browse") == 0)
6023 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6024# endif
6025#endif
6026#ifdef FEAT_SYN_HL
6027 else if (STRICMP(name, "syntax_items") == 0)
6028 n = syntax_present(curwin);
6029#endif
6030#if defined(WIN3264)
6031 else if (STRICMP(name, "win95") == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006032 n = FALSE; /* Win9x is no more supported. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006033#endif
6034#ifdef FEAT_NETBEANS_INTG
6035 else if (STRICMP(name, "netbeans_enabled") == 0)
6036 n = netbeans_active();
6037#endif
6038 }
6039
6040 rettv->vval.v_number = n;
6041}
6042
6043/*
6044 * "has_key()" function
6045 */
6046 static void
6047f_has_key(typval_T *argvars, typval_T *rettv)
6048{
6049 if (argvars[0].v_type != VAR_DICT)
6050 {
6051 EMSG(_(e_dictreq));
6052 return;
6053 }
6054 if (argvars[0].vval.v_dict == NULL)
6055 return;
6056
6057 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
6058 get_tv_string(&argvars[1]), -1) != NULL;
6059}
6060
6061/*
6062 * "haslocaldir()" function
6063 */
6064 static void
6065f_haslocaldir(typval_T *argvars, typval_T *rettv)
6066{
6067 win_T *wp = NULL;
6068
6069 wp = find_tabwin(&argvars[0], &argvars[1]);
6070 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6071}
6072
6073/*
6074 * "hasmapto()" function
6075 */
6076 static void
6077f_hasmapto(typval_T *argvars, typval_T *rettv)
6078{
6079 char_u *name;
6080 char_u *mode;
6081 char_u buf[NUMBUFLEN];
6082 int abbr = FALSE;
6083
6084 name = get_tv_string(&argvars[0]);
6085 if (argvars[1].v_type == VAR_UNKNOWN)
6086 mode = (char_u *)"nvo";
6087 else
6088 {
6089 mode = get_tv_string_buf(&argvars[1], buf);
6090 if (argvars[2].v_type != VAR_UNKNOWN)
6091 abbr = (int)get_tv_number(&argvars[2]);
6092 }
6093
6094 if (map_to_exists(name, mode, abbr))
6095 rettv->vval.v_number = TRUE;
6096 else
6097 rettv->vval.v_number = FALSE;
6098}
6099
6100/*
6101 * "histadd()" function
6102 */
6103 static void
6104f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6105{
6106#ifdef FEAT_CMDHIST
6107 int histype;
6108 char_u *str;
6109 char_u buf[NUMBUFLEN];
6110#endif
6111
6112 rettv->vval.v_number = FALSE;
6113 if (check_restricted() || check_secure())
6114 return;
6115#ifdef FEAT_CMDHIST
6116 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6117 histype = str != NULL ? get_histtype(str) : -1;
6118 if (histype >= 0)
6119 {
6120 str = get_tv_string_buf(&argvars[1], buf);
6121 if (*str != NUL)
6122 {
6123 init_history();
6124 add_to_history(histype, str, FALSE, NUL);
6125 rettv->vval.v_number = TRUE;
6126 return;
6127 }
6128 }
6129#endif
6130}
6131
6132/*
6133 * "histdel()" function
6134 */
6135 static void
6136f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6137{
6138#ifdef FEAT_CMDHIST
6139 int n;
6140 char_u buf[NUMBUFLEN];
6141 char_u *str;
6142
6143 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6144 if (str == NULL)
6145 n = 0;
6146 else if (argvars[1].v_type == VAR_UNKNOWN)
6147 /* only one argument: clear entire history */
6148 n = clr_history(get_histtype(str));
6149 else if (argvars[1].v_type == VAR_NUMBER)
6150 /* index given: remove that entry */
6151 n = del_history_idx(get_histtype(str),
6152 (int)get_tv_number(&argvars[1]));
6153 else
6154 /* string given: remove all matching entries */
6155 n = del_history_entry(get_histtype(str),
6156 get_tv_string_buf(&argvars[1], buf));
6157 rettv->vval.v_number = n;
6158#endif
6159}
6160
6161/*
6162 * "histget()" function
6163 */
6164 static void
6165f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6166{
6167#ifdef FEAT_CMDHIST
6168 int type;
6169 int idx;
6170 char_u *str;
6171
6172 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6173 if (str == NULL)
6174 rettv->vval.v_string = NULL;
6175 else
6176 {
6177 type = get_histtype(str);
6178 if (argvars[1].v_type == VAR_UNKNOWN)
6179 idx = get_history_idx(type);
6180 else
6181 idx = (int)get_tv_number_chk(&argvars[1], NULL);
6182 /* -1 on type error */
6183 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6184 }
6185#else
6186 rettv->vval.v_string = NULL;
6187#endif
6188 rettv->v_type = VAR_STRING;
6189}
6190
6191/*
6192 * "histnr()" function
6193 */
6194 static void
6195f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6196{
6197 int i;
6198
6199#ifdef FEAT_CMDHIST
6200 char_u *history = get_tv_string_chk(&argvars[0]);
6201
6202 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6203 if (i >= HIST_CMD && i < HIST_COUNT)
6204 i = get_history_idx(i);
6205 else
6206#endif
6207 i = -1;
6208 rettv->vval.v_number = i;
6209}
6210
6211/*
6212 * "highlightID(name)" function
6213 */
6214 static void
6215f_hlID(typval_T *argvars, typval_T *rettv)
6216{
6217 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
6218}
6219
6220/*
6221 * "highlight_exists()" function
6222 */
6223 static void
6224f_hlexists(typval_T *argvars, typval_T *rettv)
6225{
6226 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
6227}
6228
6229/*
6230 * "hostname()" function
6231 */
6232 static void
6233f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6234{
6235 char_u hostname[256];
6236
6237 mch_get_host_name(hostname, 256);
6238 rettv->v_type = VAR_STRING;
6239 rettv->vval.v_string = vim_strsave(hostname);
6240}
6241
6242/*
6243 * iconv() function
6244 */
6245 static void
6246f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6247{
6248#ifdef FEAT_MBYTE
6249 char_u buf1[NUMBUFLEN];
6250 char_u buf2[NUMBUFLEN];
6251 char_u *from, *to, *str;
6252 vimconv_T vimconv;
6253#endif
6254
6255 rettv->v_type = VAR_STRING;
6256 rettv->vval.v_string = NULL;
6257
6258#ifdef FEAT_MBYTE
6259 str = get_tv_string(&argvars[0]);
6260 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
6261 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
6262 vimconv.vc_type = CONV_NONE;
6263 convert_setup(&vimconv, from, to);
6264
6265 /* If the encodings are equal, no conversion needed. */
6266 if (vimconv.vc_type == CONV_NONE)
6267 rettv->vval.v_string = vim_strsave(str);
6268 else
6269 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6270
6271 convert_setup(&vimconv, NULL, NULL);
6272 vim_free(from);
6273 vim_free(to);
6274#endif
6275}
6276
6277/*
6278 * "indent()" function
6279 */
6280 static void
6281f_indent(typval_T *argvars, typval_T *rettv)
6282{
6283 linenr_T lnum;
6284
6285 lnum = get_tv_lnum(argvars);
6286 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6287 rettv->vval.v_number = get_indent_lnum(lnum);
6288 else
6289 rettv->vval.v_number = -1;
6290}
6291
6292/*
6293 * "index()" function
6294 */
6295 static void
6296f_index(typval_T *argvars, typval_T *rettv)
6297{
6298 list_T *l;
6299 listitem_T *item;
6300 long idx = 0;
6301 int ic = FALSE;
6302
6303 rettv->vval.v_number = -1;
6304 if (argvars[0].v_type != VAR_LIST)
6305 {
6306 EMSG(_(e_listreq));
6307 return;
6308 }
6309 l = argvars[0].vval.v_list;
6310 if (l != NULL)
6311 {
6312 item = l->lv_first;
6313 if (argvars[2].v_type != VAR_UNKNOWN)
6314 {
6315 int error = FALSE;
6316
6317 /* Start at specified item. Use the cached index that list_find()
6318 * sets, so that a negative number also works. */
6319 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
6320 idx = l->lv_idx;
6321 if (argvars[3].v_type != VAR_UNKNOWN)
6322 ic = (int)get_tv_number_chk(&argvars[3], &error);
6323 if (error)
6324 item = NULL;
6325 }
6326
6327 for ( ; item != NULL; item = item->li_next, ++idx)
6328 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6329 {
6330 rettv->vval.v_number = idx;
6331 break;
6332 }
6333 }
6334}
6335
6336static int inputsecret_flag = 0;
6337
6338/*
6339 * "input()" function
6340 * Also handles inputsecret() when inputsecret is set.
6341 */
6342 static void
6343f_input(typval_T *argvars, typval_T *rettv)
6344{
6345 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6346}
6347
6348/*
6349 * "inputdialog()" function
6350 */
6351 static void
6352f_inputdialog(typval_T *argvars, typval_T *rettv)
6353{
6354#if defined(FEAT_GUI_TEXTDIALOG)
6355 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
6356 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6357 {
6358 char_u *message;
6359 char_u buf[NUMBUFLEN];
6360 char_u *defstr = (char_u *)"";
6361
6362 message = get_tv_string_chk(&argvars[0]);
6363 if (argvars[1].v_type != VAR_UNKNOWN
6364 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
6365 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6366 else
6367 IObuff[0] = NUL;
6368 if (message != NULL && defstr != NULL
6369 && do_dialog(VIM_QUESTION, NULL, message,
6370 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6371 rettv->vval.v_string = vim_strsave(IObuff);
6372 else
6373 {
6374 if (message != NULL && defstr != NULL
6375 && argvars[1].v_type != VAR_UNKNOWN
6376 && argvars[2].v_type != VAR_UNKNOWN)
6377 rettv->vval.v_string = vim_strsave(
6378 get_tv_string_buf(&argvars[2], buf));
6379 else
6380 rettv->vval.v_string = NULL;
6381 }
6382 rettv->v_type = VAR_STRING;
6383 }
6384 else
6385#endif
6386 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6387}
6388
6389/*
6390 * "inputlist()" function
6391 */
6392 static void
6393f_inputlist(typval_T *argvars, typval_T *rettv)
6394{
6395 listitem_T *li;
6396 int selected;
6397 int mouse_used;
6398
6399#ifdef NO_CONSOLE_INPUT
6400 /* While starting up, there is no place to enter text. */
6401 if (no_console_input())
6402 return;
6403#endif
6404 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6405 {
6406 EMSG2(_(e_listarg), "inputlist()");
6407 return;
6408 }
6409
6410 msg_start();
6411 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
6412 lines_left = Rows; /* avoid more prompt */
6413 msg_scroll = TRUE;
6414 msg_clr_eos();
6415
6416 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
6417 {
6418 msg_puts(get_tv_string(&li->li_tv));
6419 msg_putchar('\n');
6420 }
6421
6422 /* Ask for choice. */
6423 selected = prompt_for_number(&mouse_used);
6424 if (mouse_used)
6425 selected -= lines_left;
6426
6427 rettv->vval.v_number = selected;
6428}
6429
6430
6431static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6432
6433/*
6434 * "inputrestore()" function
6435 */
6436 static void
6437f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6438{
6439 if (ga_userinput.ga_len > 0)
6440 {
6441 --ga_userinput.ga_len;
6442 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
6443 + ga_userinput.ga_len);
6444 /* default return is zero == OK */
6445 }
6446 else if (p_verbose > 1)
6447 {
6448 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
6449 rettv->vval.v_number = 1; /* Failed */
6450 }
6451}
6452
6453/*
6454 * "inputsave()" function
6455 */
6456 static void
6457f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
6458{
6459 /* Add an entry to the stack of typeahead storage. */
6460 if (ga_grow(&ga_userinput, 1) == OK)
6461 {
6462 save_typeahead((tasave_T *)(ga_userinput.ga_data)
6463 + ga_userinput.ga_len);
6464 ++ga_userinput.ga_len;
6465 /* default return is zero == OK */
6466 }
6467 else
6468 rettv->vval.v_number = 1; /* Failed */
6469}
6470
6471/*
6472 * "inputsecret()" function
6473 */
6474 static void
6475f_inputsecret(typval_T *argvars, typval_T *rettv)
6476{
6477 ++cmdline_star;
6478 ++inputsecret_flag;
6479 f_input(argvars, rettv);
6480 --cmdline_star;
6481 --inputsecret_flag;
6482}
6483
6484/*
6485 * "insert()" function
6486 */
6487 static void
6488f_insert(typval_T *argvars, typval_T *rettv)
6489{
6490 long before = 0;
6491 listitem_T *item;
6492 list_T *l;
6493 int error = FALSE;
6494
6495 if (argvars[0].v_type != VAR_LIST)
6496 EMSG2(_(e_listarg), "insert()");
6497 else if ((l = argvars[0].vval.v_list) != NULL
6498 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
6499 {
6500 if (argvars[2].v_type != VAR_UNKNOWN)
6501 before = (long)get_tv_number_chk(&argvars[2], &error);
6502 if (error)
6503 return; /* type error; errmsg already given */
6504
6505 if (before == l->lv_len)
6506 item = NULL;
6507 else
6508 {
6509 item = list_find(l, before);
6510 if (item == NULL)
6511 {
6512 EMSGN(_(e_listidx), before);
6513 l = NULL;
6514 }
6515 }
6516 if (l != NULL)
6517 {
6518 list_insert_tv(l, &argvars[1], item);
6519 copy_tv(&argvars[0], rettv);
6520 }
6521 }
6522}
6523
6524/*
6525 * "invert(expr)" function
6526 */
6527 static void
6528f_invert(typval_T *argvars, typval_T *rettv)
6529{
6530 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
6531}
6532
6533/*
6534 * "isdirectory()" function
6535 */
6536 static void
6537f_isdirectory(typval_T *argvars, typval_T *rettv)
6538{
6539 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
6540}
6541
6542/*
6543 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
6544 * or it refers to a List or Dictionary that is locked.
6545 */
6546 static int
6547tv_islocked(typval_T *tv)
6548{
6549 return (tv->v_lock & VAR_LOCKED)
6550 || (tv->v_type == VAR_LIST
6551 && tv->vval.v_list != NULL
6552 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
6553 || (tv->v_type == VAR_DICT
6554 && tv->vval.v_dict != NULL
6555 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
6556}
6557
6558/*
6559 * "islocked()" function
6560 */
6561 static void
6562f_islocked(typval_T *argvars, typval_T *rettv)
6563{
6564 lval_T lv;
6565 char_u *end;
6566 dictitem_T *di;
6567
6568 rettv->vval.v_number = -1;
6569 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
6570 GLV_NO_AUTOLOAD, FNE_CHECK_START);
6571 if (end != NULL && lv.ll_name != NULL)
6572 {
6573 if (*end != NUL)
6574 EMSG(_(e_trailing));
6575 else
6576 {
6577 if (lv.ll_tv == NULL)
6578 {
6579 if (check_changedtick(lv.ll_name))
6580 rettv->vval.v_number = 1; /* always locked */
6581 else
6582 {
6583 di = find_var(lv.ll_name, NULL, TRUE);
6584 if (di != NULL)
6585 {
6586 /* Consider a variable locked when:
6587 * 1. the variable itself is locked
6588 * 2. the value of the variable is locked.
6589 * 3. the List or Dict value is locked.
6590 */
6591 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
6592 || tv_islocked(&di->di_tv));
6593 }
6594 }
6595 }
6596 else if (lv.ll_range)
6597 EMSG(_("E786: Range not allowed"));
6598 else if (lv.ll_newkey != NULL)
6599 EMSG2(_(e_dictkey), lv.ll_newkey);
6600 else if (lv.ll_list != NULL)
6601 /* List item. */
6602 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
6603 else
6604 /* Dictionary item. */
6605 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
6606 }
6607 }
6608
6609 clear_lval(&lv);
6610}
6611
6612#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
6613/*
6614 * "isnan()" function
6615 */
6616 static void
6617f_isnan(typval_T *argvars, typval_T *rettv)
6618{
6619 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
6620 && isnan(argvars[0].vval.v_float);
6621}
6622#endif
6623
6624/*
6625 * "items(dict)" function
6626 */
6627 static void
6628f_items(typval_T *argvars, typval_T *rettv)
6629{
6630 dict_list(argvars, rettv, 2);
6631}
6632
6633#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
6634/*
6635 * Get the job from the argument.
6636 * Returns NULL if the job is invalid.
6637 */
6638 static job_T *
6639get_job_arg(typval_T *tv)
6640{
6641 job_T *job;
6642
6643 if (tv->v_type != VAR_JOB)
6644 {
6645 EMSG2(_(e_invarg2), get_tv_string(tv));
6646 return NULL;
6647 }
6648 job = tv->vval.v_job;
6649
6650 if (job == NULL)
6651 EMSG(_("E916: not a valid job"));
6652 return job;
6653}
6654
6655/*
6656 * "job_getchannel()" function
6657 */
6658 static void
6659f_job_getchannel(typval_T *argvars, typval_T *rettv)
6660{
6661 job_T *job = get_job_arg(&argvars[0]);
6662
6663 if (job != NULL)
6664 {
6665 rettv->v_type = VAR_CHANNEL;
6666 rettv->vval.v_channel = job->jv_channel;
6667 if (job->jv_channel != NULL)
6668 ++job->jv_channel->ch_refcount;
6669 }
6670}
6671
6672/*
6673 * "job_info()" function
6674 */
6675 static void
6676f_job_info(typval_T *argvars, typval_T *rettv)
6677{
6678 job_T *job = get_job_arg(&argvars[0]);
6679
6680 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
6681 job_info(job, rettv->vval.v_dict);
6682}
6683
6684/*
6685 * "job_setoptions()" function
6686 */
6687 static void
6688f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
6689{
6690 job_T *job = get_job_arg(&argvars[0]);
6691 jobopt_T opt;
6692
6693 if (job == NULL)
6694 return;
6695 clear_job_options(&opt);
6696 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == OK)
6697 job_set_options(job, &opt);
6698 free_job_options(&opt);
6699}
6700
6701/*
6702 * "job_start()" function
6703 */
6704 static void
6705f_job_start(typval_T *argvars, typval_T *rettv)
6706{
6707 rettv->v_type = VAR_JOB;
6708 if (check_restricted() || check_secure())
6709 return;
6710 rettv->vval.v_job = job_start(argvars);
6711}
6712
6713/*
6714 * "job_status()" function
6715 */
6716 static void
6717f_job_status(typval_T *argvars, typval_T *rettv)
6718{
6719 job_T *job = get_job_arg(&argvars[0]);
6720
6721 if (job != NULL)
6722 {
6723 rettv->v_type = VAR_STRING;
6724 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
6725 }
6726}
6727
6728/*
6729 * "job_stop()" function
6730 */
6731 static void
6732f_job_stop(typval_T *argvars, typval_T *rettv)
6733{
6734 job_T *job = get_job_arg(&argvars[0]);
6735
6736 if (job != NULL)
6737 rettv->vval.v_number = job_stop(job, argvars);
6738}
6739#endif
6740
6741/*
6742 * "join()" function
6743 */
6744 static void
6745f_join(typval_T *argvars, typval_T *rettv)
6746{
6747 garray_T ga;
6748 char_u *sep;
6749
6750 if (argvars[0].v_type != VAR_LIST)
6751 {
6752 EMSG(_(e_listreq));
6753 return;
6754 }
6755 if (argvars[0].vval.v_list == NULL)
6756 return;
6757 if (argvars[1].v_type == VAR_UNKNOWN)
6758 sep = (char_u *)" ";
6759 else
6760 sep = get_tv_string_chk(&argvars[1]);
6761
6762 rettv->v_type = VAR_STRING;
6763
6764 if (sep != NULL)
6765 {
6766 ga_init2(&ga, (int)sizeof(char), 80);
6767 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
6768 ga_append(&ga, NUL);
6769 rettv->vval.v_string = (char_u *)ga.ga_data;
6770 }
6771 else
6772 rettv->vval.v_string = NULL;
6773}
6774
6775/*
6776 * "js_decode()" function
6777 */
6778 static void
6779f_js_decode(typval_T *argvars, typval_T *rettv)
6780{
6781 js_read_T reader;
6782
6783 reader.js_buf = get_tv_string(&argvars[0]);
6784 reader.js_fill = NULL;
6785 reader.js_used = 0;
6786 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
6787 EMSG(_(e_invarg));
6788}
6789
6790/*
6791 * "js_encode()" function
6792 */
6793 static void
6794f_js_encode(typval_T *argvars, typval_T *rettv)
6795{
6796 rettv->v_type = VAR_STRING;
6797 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
6798}
6799
6800/*
6801 * "json_decode()" function
6802 */
6803 static void
6804f_json_decode(typval_T *argvars, typval_T *rettv)
6805{
6806 js_read_T reader;
6807
6808 reader.js_buf = get_tv_string(&argvars[0]);
6809 reader.js_fill = NULL;
6810 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01006811 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006812}
6813
6814/*
6815 * "json_encode()" function
6816 */
6817 static void
6818f_json_encode(typval_T *argvars, typval_T *rettv)
6819{
6820 rettv->v_type = VAR_STRING;
6821 rettv->vval.v_string = json_encode(&argvars[0], 0);
6822}
6823
6824/*
6825 * "keys()" function
6826 */
6827 static void
6828f_keys(typval_T *argvars, typval_T *rettv)
6829{
6830 dict_list(argvars, rettv, 0);
6831}
6832
6833/*
6834 * "last_buffer_nr()" function.
6835 */
6836 static void
6837f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
6838{
6839 int n = 0;
6840 buf_T *buf;
6841
Bram Moolenaar29323592016-07-24 22:04:11 +02006842 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006843 if (n < buf->b_fnum)
6844 n = buf->b_fnum;
6845
6846 rettv->vval.v_number = n;
6847}
6848
6849/*
6850 * "len()" function
6851 */
6852 static void
6853f_len(typval_T *argvars, typval_T *rettv)
6854{
6855 switch (argvars[0].v_type)
6856 {
6857 case VAR_STRING:
6858 case VAR_NUMBER:
6859 rettv->vval.v_number = (varnumber_T)STRLEN(
6860 get_tv_string(&argvars[0]));
6861 break;
6862 case VAR_LIST:
6863 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
6864 break;
6865 case VAR_DICT:
6866 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
6867 break;
6868 case VAR_UNKNOWN:
6869 case VAR_SPECIAL:
6870 case VAR_FLOAT:
6871 case VAR_FUNC:
6872 case VAR_PARTIAL:
6873 case VAR_JOB:
6874 case VAR_CHANNEL:
6875 EMSG(_("E701: Invalid type for len()"));
6876 break;
6877 }
6878}
6879
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006880 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01006881libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006882{
6883#ifdef FEAT_LIBCALL
6884 char_u *string_in;
6885 char_u **string_result;
6886 int nr_result;
6887#endif
6888
6889 rettv->v_type = type;
6890 if (type != VAR_NUMBER)
6891 rettv->vval.v_string = NULL;
6892
6893 if (check_restricted() || check_secure())
6894 return;
6895
6896#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02006897 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006898 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
6899 {
6900 string_in = NULL;
6901 if (argvars[2].v_type == VAR_STRING)
6902 string_in = argvars[2].vval.v_string;
6903 if (type == VAR_NUMBER)
6904 string_result = NULL;
6905 else
6906 string_result = &rettv->vval.v_string;
6907 if (mch_libcall(argvars[0].vval.v_string,
6908 argvars[1].vval.v_string,
6909 string_in,
6910 argvars[2].vval.v_number,
6911 string_result,
6912 &nr_result) == OK
6913 && type == VAR_NUMBER)
6914 rettv->vval.v_number = nr_result;
6915 }
6916#endif
6917}
6918
6919/*
6920 * "libcall()" function
6921 */
6922 static void
6923f_libcall(typval_T *argvars, typval_T *rettv)
6924{
6925 libcall_common(argvars, rettv, VAR_STRING);
6926}
6927
6928/*
6929 * "libcallnr()" function
6930 */
6931 static void
6932f_libcallnr(typval_T *argvars, typval_T *rettv)
6933{
6934 libcall_common(argvars, rettv, VAR_NUMBER);
6935}
6936
6937/*
6938 * "line(string)" function
6939 */
6940 static void
6941f_line(typval_T *argvars, typval_T *rettv)
6942{
6943 linenr_T lnum = 0;
6944 pos_T *fp;
6945 int fnum;
6946
6947 fp = var2fpos(&argvars[0], TRUE, &fnum);
6948 if (fp != NULL)
6949 lnum = fp->lnum;
6950 rettv->vval.v_number = lnum;
6951}
6952
6953/*
6954 * "line2byte(lnum)" function
6955 */
6956 static void
6957f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
6958{
6959#ifndef FEAT_BYTEOFF
6960 rettv->vval.v_number = -1;
6961#else
6962 linenr_T lnum;
6963
6964 lnum = get_tv_lnum(argvars);
6965 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
6966 rettv->vval.v_number = -1;
6967 else
6968 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
6969 if (rettv->vval.v_number >= 0)
6970 ++rettv->vval.v_number;
6971#endif
6972}
6973
6974/*
6975 * "lispindent(lnum)" function
6976 */
6977 static void
6978f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
6979{
6980#ifdef FEAT_LISP
6981 pos_T pos;
6982 linenr_T lnum;
6983
6984 pos = curwin->w_cursor;
6985 lnum = get_tv_lnum(argvars);
6986 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6987 {
6988 curwin->w_cursor.lnum = lnum;
6989 rettv->vval.v_number = get_lisp_indent();
6990 curwin->w_cursor = pos;
6991 }
6992 else
6993#endif
6994 rettv->vval.v_number = -1;
6995}
6996
6997/*
6998 * "localtime()" function
6999 */
7000 static void
7001f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7002{
7003 rettv->vval.v_number = (varnumber_T)time(NULL);
7004}
7005
7006static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
7007
7008 static void
7009get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7010{
7011 char_u *keys;
7012 char_u *which;
7013 char_u buf[NUMBUFLEN];
7014 char_u *keys_buf = NULL;
7015 char_u *rhs;
7016 int mode;
7017 int abbr = FALSE;
7018 int get_dict = FALSE;
7019 mapblock_T *mp;
7020 int buffer_local;
7021
7022 /* return empty string for failure */
7023 rettv->v_type = VAR_STRING;
7024 rettv->vval.v_string = NULL;
7025
7026 keys = get_tv_string(&argvars[0]);
7027 if (*keys == NUL)
7028 return;
7029
7030 if (argvars[1].v_type != VAR_UNKNOWN)
7031 {
7032 which = get_tv_string_buf_chk(&argvars[1], buf);
7033 if (argvars[2].v_type != VAR_UNKNOWN)
7034 {
7035 abbr = (int)get_tv_number(&argvars[2]);
7036 if (argvars[3].v_type != VAR_UNKNOWN)
7037 get_dict = (int)get_tv_number(&argvars[3]);
7038 }
7039 }
7040 else
7041 which = (char_u *)"";
7042 if (which == NULL)
7043 return;
7044
7045 mode = get_map_mode(&which, 0);
7046
7047 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7048 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7049 vim_free(keys_buf);
7050
7051 if (!get_dict)
7052 {
7053 /* Return a string. */
7054 if (rhs != NULL)
7055 rettv->vval.v_string = str2special_save(rhs, FALSE);
7056
7057 }
7058 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7059 {
7060 /* Return a dictionary. */
7061 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7062 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7063 dict_T *dict = rettv->vval.v_dict;
7064
7065 dict_add_nr_str(dict, "lhs", 0L, lhs);
7066 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
7067 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
7068 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
7069 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
7070 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
7071 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
7072 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
7073 dict_add_nr_str(dict, "mode", 0L, mapmode);
7074
7075 vim_free(lhs);
7076 vim_free(mapmode);
7077 }
7078}
7079
7080#ifdef FEAT_FLOAT
7081/*
7082 * "log()" function
7083 */
7084 static void
7085f_log(typval_T *argvars, typval_T *rettv)
7086{
7087 float_T f = 0.0;
7088
7089 rettv->v_type = VAR_FLOAT;
7090 if (get_float_arg(argvars, &f) == OK)
7091 rettv->vval.v_float = log(f);
7092 else
7093 rettv->vval.v_float = 0.0;
7094}
7095
7096/*
7097 * "log10()" function
7098 */
7099 static void
7100f_log10(typval_T *argvars, typval_T *rettv)
7101{
7102 float_T f = 0.0;
7103
7104 rettv->v_type = VAR_FLOAT;
7105 if (get_float_arg(argvars, &f) == OK)
7106 rettv->vval.v_float = log10(f);
7107 else
7108 rettv->vval.v_float = 0.0;
7109}
7110#endif
7111
7112#ifdef FEAT_LUA
7113/*
7114 * "luaeval()" function
7115 */
7116 static void
7117f_luaeval(typval_T *argvars, typval_T *rettv)
7118{
7119 char_u *str;
7120 char_u buf[NUMBUFLEN];
7121
7122 str = get_tv_string_buf(&argvars[0], buf);
7123 do_luaeval(str, argvars + 1, rettv);
7124}
7125#endif
7126
7127/*
7128 * "map()" function
7129 */
7130 static void
7131f_map(typval_T *argvars, typval_T *rettv)
7132{
7133 filter_map(argvars, rettv, TRUE);
7134}
7135
7136/*
7137 * "maparg()" function
7138 */
7139 static void
7140f_maparg(typval_T *argvars, typval_T *rettv)
7141{
7142 get_maparg(argvars, rettv, TRUE);
7143}
7144
7145/*
7146 * "mapcheck()" function
7147 */
7148 static void
7149f_mapcheck(typval_T *argvars, typval_T *rettv)
7150{
7151 get_maparg(argvars, rettv, FALSE);
7152}
7153
7154static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
7155
7156 static void
7157find_some_match(typval_T *argvars, typval_T *rettv, int type)
7158{
7159 char_u *str = NULL;
7160 long len = 0;
7161 char_u *expr = NULL;
7162 char_u *pat;
7163 regmatch_T regmatch;
7164 char_u patbuf[NUMBUFLEN];
7165 char_u strbuf[NUMBUFLEN];
7166 char_u *save_cpo;
7167 long start = 0;
7168 long nth = 1;
7169 colnr_T startcol = 0;
7170 int match = 0;
7171 list_T *l = NULL;
7172 listitem_T *li = NULL;
7173 long idx = 0;
7174 char_u *tofree = NULL;
7175
7176 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7177 save_cpo = p_cpo;
7178 p_cpo = (char_u *)"";
7179
7180 rettv->vval.v_number = -1;
7181 if (type == 3 || type == 4)
7182 {
7183 /* type 3: return empty list when there are no matches.
7184 * type 4: return ["", -1, -1, -1] */
7185 if (rettv_list_alloc(rettv) == FAIL)
7186 goto theend;
7187 if (type == 4
7188 && (list_append_string(rettv->vval.v_list,
7189 (char_u *)"", 0) == FAIL
7190 || list_append_number(rettv->vval.v_list,
7191 (varnumber_T)-1) == FAIL
7192 || list_append_number(rettv->vval.v_list,
7193 (varnumber_T)-1) == FAIL
7194 || list_append_number(rettv->vval.v_list,
7195 (varnumber_T)-1) == FAIL))
7196 {
7197 list_free(rettv->vval.v_list);
7198 rettv->vval.v_list = NULL;
7199 goto theend;
7200 }
7201 }
7202 else if (type == 2)
7203 {
7204 rettv->v_type = VAR_STRING;
7205 rettv->vval.v_string = NULL;
7206 }
7207
7208 if (argvars[0].v_type == VAR_LIST)
7209 {
7210 if ((l = argvars[0].vval.v_list) == NULL)
7211 goto theend;
7212 li = l->lv_first;
7213 }
7214 else
7215 {
7216 expr = str = get_tv_string(&argvars[0]);
7217 len = (long)STRLEN(str);
7218 }
7219
7220 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
7221 if (pat == NULL)
7222 goto theend;
7223
7224 if (argvars[2].v_type != VAR_UNKNOWN)
7225 {
7226 int error = FALSE;
7227
7228 start = (long)get_tv_number_chk(&argvars[2], &error);
7229 if (error)
7230 goto theend;
7231 if (l != NULL)
7232 {
7233 li = list_find(l, start);
7234 if (li == NULL)
7235 goto theend;
7236 idx = l->lv_idx; /* use the cached index */
7237 }
7238 else
7239 {
7240 if (start < 0)
7241 start = 0;
7242 if (start > len)
7243 goto theend;
7244 /* When "count" argument is there ignore matches before "start",
7245 * otherwise skip part of the string. Differs when pattern is "^"
7246 * or "\<". */
7247 if (argvars[3].v_type != VAR_UNKNOWN)
7248 startcol = start;
7249 else
7250 {
7251 str += start;
7252 len -= start;
7253 }
7254 }
7255
7256 if (argvars[3].v_type != VAR_UNKNOWN)
7257 nth = (long)get_tv_number_chk(&argvars[3], &error);
7258 if (error)
7259 goto theend;
7260 }
7261
7262 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7263 if (regmatch.regprog != NULL)
7264 {
7265 regmatch.rm_ic = p_ic;
7266
7267 for (;;)
7268 {
7269 if (l != NULL)
7270 {
7271 if (li == NULL)
7272 {
7273 match = FALSE;
7274 break;
7275 }
7276 vim_free(tofree);
7277 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7278 if (str == NULL)
7279 break;
7280 }
7281
7282 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7283
7284 if (match && --nth <= 0)
7285 break;
7286 if (l == NULL && !match)
7287 break;
7288
7289 /* Advance to just after the match. */
7290 if (l != NULL)
7291 {
7292 li = li->li_next;
7293 ++idx;
7294 }
7295 else
7296 {
7297#ifdef FEAT_MBYTE
7298 startcol = (colnr_T)(regmatch.startp[0]
7299 + (*mb_ptr2len)(regmatch.startp[0]) - str);
7300#else
7301 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
7302#endif
7303 if (startcol > (colnr_T)len
7304 || str + startcol <= regmatch.startp[0])
7305 {
7306 match = FALSE;
7307 break;
7308 }
7309 }
7310 }
7311
7312 if (match)
7313 {
7314 if (type == 4)
7315 {
7316 listitem_T *li1 = rettv->vval.v_list->lv_first;
7317 listitem_T *li2 = li1->li_next;
7318 listitem_T *li3 = li2->li_next;
7319 listitem_T *li4 = li3->li_next;
7320
7321 vim_free(li1->li_tv.vval.v_string);
7322 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7323 (int)(regmatch.endp[0] - regmatch.startp[0]));
7324 li3->li_tv.vval.v_number =
7325 (varnumber_T)(regmatch.startp[0] - expr);
7326 li4->li_tv.vval.v_number =
7327 (varnumber_T)(regmatch.endp[0] - expr);
7328 if (l != NULL)
7329 li2->li_tv.vval.v_number = (varnumber_T)idx;
7330 }
7331 else if (type == 3)
7332 {
7333 int i;
7334
7335 /* return list with matched string and submatches */
7336 for (i = 0; i < NSUBEXP; ++i)
7337 {
7338 if (regmatch.endp[i] == NULL)
7339 {
7340 if (list_append_string(rettv->vval.v_list,
7341 (char_u *)"", 0) == FAIL)
7342 break;
7343 }
7344 else if (list_append_string(rettv->vval.v_list,
7345 regmatch.startp[i],
7346 (int)(regmatch.endp[i] - regmatch.startp[i]))
7347 == FAIL)
7348 break;
7349 }
7350 }
7351 else if (type == 2)
7352 {
7353 /* return matched string */
7354 if (l != NULL)
7355 copy_tv(&li->li_tv, rettv);
7356 else
7357 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
7358 (int)(regmatch.endp[0] - regmatch.startp[0]));
7359 }
7360 else if (l != NULL)
7361 rettv->vval.v_number = idx;
7362 else
7363 {
7364 if (type != 0)
7365 rettv->vval.v_number =
7366 (varnumber_T)(regmatch.startp[0] - str);
7367 else
7368 rettv->vval.v_number =
7369 (varnumber_T)(regmatch.endp[0] - str);
7370 rettv->vval.v_number += (varnumber_T)(str - expr);
7371 }
7372 }
7373 vim_regfree(regmatch.regprog);
7374 }
7375
7376 if (type == 4 && l == NULL)
7377 /* matchstrpos() without a list: drop the second item. */
7378 listitem_remove(rettv->vval.v_list,
7379 rettv->vval.v_list->lv_first->li_next);
7380
7381theend:
7382 vim_free(tofree);
7383 p_cpo = save_cpo;
7384}
7385
7386/*
7387 * "match()" function
7388 */
7389 static void
7390f_match(typval_T *argvars, typval_T *rettv)
7391{
7392 find_some_match(argvars, rettv, 1);
7393}
7394
7395/*
7396 * "matchadd()" function
7397 */
7398 static void
7399f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7400{
7401#ifdef FEAT_SEARCH_EXTRA
7402 char_u buf[NUMBUFLEN];
7403 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
7404 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
7405 int prio = 10; /* default priority */
7406 int id = -1;
7407 int error = FALSE;
7408 char_u *conceal_char = NULL;
7409
7410 rettv->vval.v_number = -1;
7411
7412 if (grp == NULL || pat == NULL)
7413 return;
7414 if (argvars[2].v_type != VAR_UNKNOWN)
7415 {
7416 prio = (int)get_tv_number_chk(&argvars[2], &error);
7417 if (argvars[3].v_type != VAR_UNKNOWN)
7418 {
7419 id = (int)get_tv_number_chk(&argvars[3], &error);
7420 if (argvars[4].v_type != VAR_UNKNOWN)
7421 {
7422 if (argvars[4].v_type != VAR_DICT)
7423 {
7424 EMSG(_(e_dictreq));
7425 return;
7426 }
7427 if (dict_find(argvars[4].vval.v_dict,
7428 (char_u *)"conceal", -1) != NULL)
7429 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7430 (char_u *)"conceal", FALSE);
7431 }
7432 }
7433 }
7434 if (error == TRUE)
7435 return;
7436 if (id >= 1 && id <= 3)
7437 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007438 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007439 return;
7440 }
7441
7442 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
7443 conceal_char);
7444#endif
7445}
7446
7447/*
7448 * "matchaddpos()" function
7449 */
7450 static void
7451f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7452{
7453#ifdef FEAT_SEARCH_EXTRA
7454 char_u buf[NUMBUFLEN];
7455 char_u *group;
7456 int prio = 10;
7457 int id = -1;
7458 int error = FALSE;
7459 list_T *l;
7460 char_u *conceal_char = NULL;
7461
7462 rettv->vval.v_number = -1;
7463
7464 group = get_tv_string_buf_chk(&argvars[0], buf);
7465 if (group == NULL)
7466 return;
7467
7468 if (argvars[1].v_type != VAR_LIST)
7469 {
7470 EMSG2(_(e_listarg), "matchaddpos()");
7471 return;
7472 }
7473 l = argvars[1].vval.v_list;
7474 if (l == NULL)
7475 return;
7476
7477 if (argvars[2].v_type != VAR_UNKNOWN)
7478 {
7479 prio = (int)get_tv_number_chk(&argvars[2], &error);
7480 if (argvars[3].v_type != VAR_UNKNOWN)
7481 {
7482 id = (int)get_tv_number_chk(&argvars[3], &error);
7483 if (argvars[4].v_type != VAR_UNKNOWN)
7484 {
7485 if (argvars[4].v_type != VAR_DICT)
7486 {
7487 EMSG(_(e_dictreq));
7488 return;
7489 }
7490 if (dict_find(argvars[4].vval.v_dict,
7491 (char_u *)"conceal", -1) != NULL)
7492 conceal_char = get_dict_string(argvars[4].vval.v_dict,
7493 (char_u *)"conceal", FALSE);
7494 }
7495 }
7496 }
7497 if (error == TRUE)
7498 return;
7499
7500 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
7501 if (id == 1 || id == 2)
7502 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02007503 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007504 return;
7505 }
7506
7507 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
7508 conceal_char);
7509#endif
7510}
7511
7512/*
7513 * "matcharg()" function
7514 */
7515 static void
7516f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
7517{
7518 if (rettv_list_alloc(rettv) == OK)
7519 {
7520#ifdef FEAT_SEARCH_EXTRA
7521 int id = (int)get_tv_number(&argvars[0]);
7522 matchitem_T *m;
7523
7524 if (id >= 1 && id <= 3)
7525 {
7526 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
7527 {
7528 list_append_string(rettv->vval.v_list,
7529 syn_id2name(m->hlg_id), -1);
7530 list_append_string(rettv->vval.v_list, m->pattern, -1);
7531 }
7532 else
7533 {
7534 list_append_string(rettv->vval.v_list, NULL, -1);
7535 list_append_string(rettv->vval.v_list, NULL, -1);
7536 }
7537 }
7538#endif
7539 }
7540}
7541
7542/*
7543 * "matchdelete()" function
7544 */
7545 static void
7546f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7547{
7548#ifdef FEAT_SEARCH_EXTRA
7549 rettv->vval.v_number = match_delete(curwin,
7550 (int)get_tv_number(&argvars[0]), TRUE);
7551#endif
7552}
7553
7554/*
7555 * "matchend()" function
7556 */
7557 static void
7558f_matchend(typval_T *argvars, typval_T *rettv)
7559{
7560 find_some_match(argvars, rettv, 0);
7561}
7562
7563/*
7564 * "matchlist()" function
7565 */
7566 static void
7567f_matchlist(typval_T *argvars, typval_T *rettv)
7568{
7569 find_some_match(argvars, rettv, 3);
7570}
7571
7572/*
7573 * "matchstr()" function
7574 */
7575 static void
7576f_matchstr(typval_T *argvars, typval_T *rettv)
7577{
7578 find_some_match(argvars, rettv, 2);
7579}
7580
7581/*
7582 * "matchstrpos()" function
7583 */
7584 static void
7585f_matchstrpos(typval_T *argvars, typval_T *rettv)
7586{
7587 find_some_match(argvars, rettv, 4);
7588}
7589
7590static void max_min(typval_T *argvars, typval_T *rettv, int domax);
7591
7592 static void
7593max_min(typval_T *argvars, typval_T *rettv, int domax)
7594{
7595 varnumber_T n = 0;
7596 varnumber_T i;
7597 int error = FALSE;
7598
7599 if (argvars[0].v_type == VAR_LIST)
7600 {
7601 list_T *l;
7602 listitem_T *li;
7603
7604 l = argvars[0].vval.v_list;
7605 if (l != NULL)
7606 {
7607 li = l->lv_first;
7608 if (li != NULL)
7609 {
7610 n = get_tv_number_chk(&li->li_tv, &error);
7611 for (;;)
7612 {
7613 li = li->li_next;
7614 if (li == NULL)
7615 break;
7616 i = get_tv_number_chk(&li->li_tv, &error);
7617 if (domax ? i > n : i < n)
7618 n = i;
7619 }
7620 }
7621 }
7622 }
7623 else if (argvars[0].v_type == VAR_DICT)
7624 {
7625 dict_T *d;
7626 int first = TRUE;
7627 hashitem_T *hi;
7628 int todo;
7629
7630 d = argvars[0].vval.v_dict;
7631 if (d != NULL)
7632 {
7633 todo = (int)d->dv_hashtab.ht_used;
7634 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
7635 {
7636 if (!HASHITEM_EMPTY(hi))
7637 {
7638 --todo;
7639 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
7640 if (first)
7641 {
7642 n = i;
7643 first = FALSE;
7644 }
7645 else if (domax ? i > n : i < n)
7646 n = i;
7647 }
7648 }
7649 }
7650 }
7651 else
Bram Moolenaar26b84332016-09-04 21:42:36 +02007652 EMSG2(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007653 rettv->vval.v_number = error ? 0 : n;
7654}
7655
7656/*
7657 * "max()" function
7658 */
7659 static void
7660f_max(typval_T *argvars, typval_T *rettv)
7661{
7662 max_min(argvars, rettv, TRUE);
7663}
7664
7665/*
7666 * "min()" function
7667 */
7668 static void
7669f_min(typval_T *argvars, typval_T *rettv)
7670{
7671 max_min(argvars, rettv, FALSE);
7672}
7673
7674static int mkdir_recurse(char_u *dir, int prot);
7675
7676/*
7677 * Create the directory in which "dir" is located, and higher levels when
7678 * needed.
7679 */
7680 static int
7681mkdir_recurse(char_u *dir, int prot)
7682{
7683 char_u *p;
7684 char_u *updir;
7685 int r = FAIL;
7686
7687 /* Get end of directory name in "dir".
7688 * We're done when it's "/" or "c:/". */
7689 p = gettail_sep(dir);
7690 if (p <= get_past_head(dir))
7691 return OK;
7692
7693 /* If the directory exists we're done. Otherwise: create it.*/
7694 updir = vim_strnsave(dir, (int)(p - dir));
7695 if (updir == NULL)
7696 return FAIL;
7697 if (mch_isdir(updir))
7698 r = OK;
7699 else if (mkdir_recurse(updir, prot) == OK)
7700 r = vim_mkdir_emsg(updir, prot);
7701 vim_free(updir);
7702 return r;
7703}
7704
7705#ifdef vim_mkdir
7706/*
7707 * "mkdir()" function
7708 */
7709 static void
7710f_mkdir(typval_T *argvars, typval_T *rettv)
7711{
7712 char_u *dir;
7713 char_u buf[NUMBUFLEN];
7714 int prot = 0755;
7715
7716 rettv->vval.v_number = FAIL;
7717 if (check_restricted() || check_secure())
7718 return;
7719
7720 dir = get_tv_string_buf(&argvars[0], buf);
7721 if (*dir == NUL)
7722 rettv->vval.v_number = FAIL;
7723 else
7724 {
7725 if (*gettail(dir) == NUL)
7726 /* remove trailing slashes */
7727 *gettail_sep(dir) = NUL;
7728
7729 if (argvars[1].v_type != VAR_UNKNOWN)
7730 {
7731 if (argvars[2].v_type != VAR_UNKNOWN)
7732 prot = (int)get_tv_number_chk(&argvars[2], NULL);
7733 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
7734 mkdir_recurse(dir, prot);
7735 }
7736 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
7737 }
7738}
7739#endif
7740
7741/*
7742 * "mode()" function
7743 */
7744 static void
7745f_mode(typval_T *argvars, typval_T *rettv)
7746{
7747 char_u buf[3];
7748
7749 buf[1] = NUL;
7750 buf[2] = NUL;
7751
7752 if (time_for_testing == 93784)
7753 {
7754 /* Testing the two-character code. */
7755 buf[0] = 'x';
7756 buf[1] = '!';
7757 }
7758 else if (VIsual_active)
7759 {
7760 if (VIsual_select)
7761 buf[0] = VIsual_mode + 's' - 'v';
7762 else
7763 buf[0] = VIsual_mode;
7764 }
7765 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
7766 || State == CONFIRM)
7767 {
7768 buf[0] = 'r';
7769 if (State == ASKMORE)
7770 buf[1] = 'm';
7771 else if (State == CONFIRM)
7772 buf[1] = '?';
7773 }
7774 else if (State == EXTERNCMD)
7775 buf[0] = '!';
7776 else if (State & INSERT)
7777 {
7778#ifdef FEAT_VREPLACE
7779 if (State & VREPLACE_FLAG)
7780 {
7781 buf[0] = 'R';
7782 buf[1] = 'v';
7783 }
7784 else
7785#endif
7786 if (State & REPLACE_FLAG)
7787 buf[0] = 'R';
7788 else
7789 buf[0] = 'i';
7790 }
7791 else if (State & CMDLINE)
7792 {
7793 buf[0] = 'c';
7794 if (exmode_active)
7795 buf[1] = 'v';
7796 }
7797 else if (exmode_active)
7798 {
7799 buf[0] = 'c';
7800 buf[1] = 'e';
7801 }
7802 else
7803 {
7804 buf[0] = 'n';
7805 if (finish_op)
7806 buf[1] = 'o';
7807 }
7808
7809 /* Clear out the minor mode when the argument is not a non-zero number or
7810 * non-empty string. */
7811 if (!non_zero_arg(&argvars[0]))
7812 buf[1] = NUL;
7813
7814 rettv->vval.v_string = vim_strsave(buf);
7815 rettv->v_type = VAR_STRING;
7816}
7817
7818#if defined(FEAT_MZSCHEME) || defined(PROTO)
7819/*
7820 * "mzeval()" function
7821 */
7822 static void
7823f_mzeval(typval_T *argvars, typval_T *rettv)
7824{
7825 char_u *str;
7826 char_u buf[NUMBUFLEN];
7827
7828 str = get_tv_string_buf(&argvars[0], buf);
7829 do_mzeval(str, rettv);
7830}
7831
7832 void
7833mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
7834{
7835 typval_T argvars[3];
7836
7837 argvars[0].v_type = VAR_STRING;
7838 argvars[0].vval.v_string = name;
7839 copy_tv(args, &argvars[1]);
7840 argvars[2].v_type = VAR_UNKNOWN;
7841 f_call(argvars, rettv);
7842 clear_tv(&argvars[1]);
7843}
7844#endif
7845
7846/*
7847 * "nextnonblank()" function
7848 */
7849 static void
7850f_nextnonblank(typval_T *argvars, typval_T *rettv)
7851{
7852 linenr_T lnum;
7853
7854 for (lnum = get_tv_lnum(argvars); ; ++lnum)
7855 {
7856 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
7857 {
7858 lnum = 0;
7859 break;
7860 }
7861 if (*skipwhite(ml_get(lnum)) != NUL)
7862 break;
7863 }
7864 rettv->vval.v_number = lnum;
7865}
7866
7867/*
7868 * "nr2char()" function
7869 */
7870 static void
7871f_nr2char(typval_T *argvars, typval_T *rettv)
7872{
7873 char_u buf[NUMBUFLEN];
7874
7875#ifdef FEAT_MBYTE
7876 if (has_mbyte)
7877 {
7878 int utf8 = 0;
7879
7880 if (argvars[1].v_type != VAR_UNKNOWN)
7881 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
7882 if (utf8)
7883 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
7884 else
7885 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
7886 }
7887 else
7888#endif
7889 {
7890 buf[0] = (char_u)get_tv_number(&argvars[0]);
7891 buf[1] = NUL;
7892 }
7893 rettv->v_type = VAR_STRING;
7894 rettv->vval.v_string = vim_strsave(buf);
7895}
7896
7897/*
7898 * "or(expr, expr)" function
7899 */
7900 static void
7901f_or(typval_T *argvars, typval_T *rettv)
7902{
7903 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
7904 | get_tv_number_chk(&argvars[1], NULL);
7905}
7906
7907/*
7908 * "pathshorten()" function
7909 */
7910 static void
7911f_pathshorten(typval_T *argvars, typval_T *rettv)
7912{
7913 char_u *p;
7914
7915 rettv->v_type = VAR_STRING;
7916 p = get_tv_string_chk(&argvars[0]);
7917 if (p == NULL)
7918 rettv->vval.v_string = NULL;
7919 else
7920 {
7921 p = vim_strsave(p);
7922 rettv->vval.v_string = p;
7923 if (p != NULL)
7924 shorten_dir(p);
7925 }
7926}
7927
7928#ifdef FEAT_PERL
7929/*
7930 * "perleval()" function
7931 */
7932 static void
7933f_perleval(typval_T *argvars, typval_T *rettv)
7934{
7935 char_u *str;
7936 char_u buf[NUMBUFLEN];
7937
7938 str = get_tv_string_buf(&argvars[0], buf);
7939 do_perleval(str, rettv);
7940}
7941#endif
7942
7943#ifdef FEAT_FLOAT
7944/*
7945 * "pow()" function
7946 */
7947 static void
7948f_pow(typval_T *argvars, typval_T *rettv)
7949{
7950 float_T fx = 0.0, fy = 0.0;
7951
7952 rettv->v_type = VAR_FLOAT;
7953 if (get_float_arg(argvars, &fx) == OK
7954 && get_float_arg(&argvars[1], &fy) == OK)
7955 rettv->vval.v_float = pow(fx, fy);
7956 else
7957 rettv->vval.v_float = 0.0;
7958}
7959#endif
7960
7961/*
7962 * "prevnonblank()" function
7963 */
7964 static void
7965f_prevnonblank(typval_T *argvars, typval_T *rettv)
7966{
7967 linenr_T lnum;
7968
7969 lnum = get_tv_lnum(argvars);
7970 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
7971 lnum = 0;
7972 else
7973 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
7974 --lnum;
7975 rettv->vval.v_number = lnum;
7976}
7977
7978/* This dummy va_list is here because:
7979 * - passing a NULL pointer doesn't work when va_list isn't a pointer
7980 * - locally in the function results in a "used before set" warning
7981 * - using va_start() to initialize it gives "function with fixed args" error */
7982static va_list ap;
7983
7984/*
7985 * "printf()" function
7986 */
7987 static void
7988f_printf(typval_T *argvars, typval_T *rettv)
7989{
7990 char_u buf[NUMBUFLEN];
7991 int len;
7992 char_u *s;
7993 int saved_did_emsg = did_emsg;
7994 char *fmt;
7995
7996 rettv->v_type = VAR_STRING;
7997 rettv->vval.v_string = NULL;
7998
7999 /* Get the required length, allocate the buffer and do it for real. */
8000 did_emsg = FALSE;
8001 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
8002 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
8003 if (!did_emsg)
8004 {
8005 s = alloc(len + 1);
8006 if (s != NULL)
8007 {
8008 rettv->vval.v_string = s;
8009 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
8010 }
8011 }
8012 did_emsg |= saved_did_emsg;
8013}
8014
8015/*
8016 * "pumvisible()" function
8017 */
8018 static void
8019f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8020{
8021#ifdef FEAT_INS_EXPAND
8022 if (pum_visible())
8023 rettv->vval.v_number = 1;
8024#endif
8025}
8026
8027#ifdef FEAT_PYTHON3
8028/*
8029 * "py3eval()" function
8030 */
8031 static void
8032f_py3eval(typval_T *argvars, typval_T *rettv)
8033{
8034 char_u *str;
8035 char_u buf[NUMBUFLEN];
8036
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008037 if (p_pyx == 0)
8038 p_pyx = 3;
8039
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008040 str = get_tv_string_buf(&argvars[0], buf);
8041 do_py3eval(str, rettv);
8042}
8043#endif
8044
8045#ifdef FEAT_PYTHON
8046/*
8047 * "pyeval()" function
8048 */
8049 static void
8050f_pyeval(typval_T *argvars, typval_T *rettv)
8051{
8052 char_u *str;
8053 char_u buf[NUMBUFLEN];
8054
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008055 if (p_pyx == 0)
8056 p_pyx = 2;
8057
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008058 str = get_tv_string_buf(&argvars[0], buf);
8059 do_pyeval(str, rettv);
8060}
8061#endif
8062
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008063#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8064/*
8065 * "pyxeval()" function
8066 */
8067 static void
8068f_pyxeval(typval_T *argvars, typval_T *rettv)
8069{
8070# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8071 init_pyxversion();
8072 if (p_pyx == 2)
8073 f_pyeval(argvars, rettv);
8074 else
8075 f_py3eval(argvars, rettv);
8076# elif defined(FEAT_PYTHON)
8077 f_pyeval(argvars, rettv);
8078# elif defined(FEAT_PYTHON3)
8079 f_py3eval(argvars, rettv);
8080# endif
8081}
8082#endif
8083
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008084/*
8085 * "range()" function
8086 */
8087 static void
8088f_range(typval_T *argvars, typval_T *rettv)
8089{
8090 varnumber_T start;
8091 varnumber_T end;
8092 varnumber_T stride = 1;
8093 varnumber_T i;
8094 int error = FALSE;
8095
8096 start = get_tv_number_chk(&argvars[0], &error);
8097 if (argvars[1].v_type == VAR_UNKNOWN)
8098 {
8099 end = start - 1;
8100 start = 0;
8101 }
8102 else
8103 {
8104 end = get_tv_number_chk(&argvars[1], &error);
8105 if (argvars[2].v_type != VAR_UNKNOWN)
8106 stride = get_tv_number_chk(&argvars[2], &error);
8107 }
8108
8109 if (error)
8110 return; /* type error; errmsg already given */
8111 if (stride == 0)
8112 EMSG(_("E726: Stride is zero"));
8113 else if (stride > 0 ? end + 1 < start : end - 1 > start)
8114 EMSG(_("E727: Start past end"));
8115 else
8116 {
8117 if (rettv_list_alloc(rettv) == OK)
8118 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8119 if (list_append_number(rettv->vval.v_list,
8120 (varnumber_T)i) == FAIL)
8121 break;
8122 }
8123}
8124
8125/*
8126 * "readfile()" function
8127 */
8128 static void
8129f_readfile(typval_T *argvars, typval_T *rettv)
8130{
8131 int binary = FALSE;
8132 int failed = FALSE;
8133 char_u *fname;
8134 FILE *fd;
8135 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8136 int io_size = sizeof(buf);
8137 int readlen; /* size of last fread() */
8138 char_u *prev = NULL; /* previously read bytes, if any */
8139 long prevlen = 0; /* length of data in prev */
8140 long prevsize = 0; /* size of prev buffer */
8141 long maxline = MAXLNUM;
8142 long cnt = 0;
8143 char_u *p; /* position in buf */
8144 char_u *start; /* start of current line */
8145
8146 if (argvars[1].v_type != VAR_UNKNOWN)
8147 {
8148 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
8149 binary = TRUE;
8150 if (argvars[2].v_type != VAR_UNKNOWN)
8151 maxline = (long)get_tv_number(&argvars[2]);
8152 }
8153
8154 if (rettv_list_alloc(rettv) == FAIL)
8155 return;
8156
8157 /* Always open the file in binary mode, library functions have a mind of
8158 * their own about CR-LF conversion. */
8159 fname = get_tv_string(&argvars[0]);
8160 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8161 {
8162 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
8163 return;
8164 }
8165
8166 while (cnt < maxline || maxline < 0)
8167 {
8168 readlen = (int)fread(buf, 1, io_size, fd);
8169
8170 /* This for loop processes what was read, but is also entered at end
8171 * of file so that either:
8172 * - an incomplete line gets written
8173 * - a "binary" file gets an empty line at the end if it ends in a
8174 * newline. */
8175 for (p = buf, start = buf;
8176 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8177 ++p)
8178 {
8179 if (*p == '\n' || readlen <= 0)
8180 {
8181 listitem_T *li;
8182 char_u *s = NULL;
8183 long_u len = p - start;
8184
8185 /* Finished a line. Remove CRs before NL. */
8186 if (readlen > 0 && !binary)
8187 {
8188 while (len > 0 && start[len - 1] == '\r')
8189 --len;
8190 /* removal may cross back to the "prev" string */
8191 if (len == 0)
8192 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8193 --prevlen;
8194 }
8195 if (prevlen == 0)
8196 s = vim_strnsave(start, (int)len);
8197 else
8198 {
8199 /* Change "prev" buffer to be the right size. This way
8200 * the bytes are only copied once, and very long lines are
8201 * allocated only once. */
8202 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8203 {
8204 mch_memmove(s + prevlen, start, len);
8205 s[prevlen + len] = NUL;
8206 prev = NULL; /* the list will own the string */
8207 prevlen = prevsize = 0;
8208 }
8209 }
8210 if (s == NULL)
8211 {
8212 do_outofmem_msg((long_u) prevlen + len + 1);
8213 failed = TRUE;
8214 break;
8215 }
8216
8217 if ((li = listitem_alloc()) == NULL)
8218 {
8219 vim_free(s);
8220 failed = TRUE;
8221 break;
8222 }
8223 li->li_tv.v_type = VAR_STRING;
8224 li->li_tv.v_lock = 0;
8225 li->li_tv.vval.v_string = s;
8226 list_append(rettv->vval.v_list, li);
8227
8228 start = p + 1; /* step over newline */
8229 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
8230 break;
8231 }
8232 else if (*p == NUL)
8233 *p = '\n';
8234#ifdef FEAT_MBYTE
8235 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
8236 * when finding the BF and check the previous two bytes. */
8237 else if (*p == 0xbf && enc_utf8 && !binary)
8238 {
8239 /* Find the two bytes before the 0xbf. If p is at buf, or buf
8240 * + 1, these may be in the "prev" string. */
8241 char_u back1 = p >= buf + 1 ? p[-1]
8242 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
8243 char_u back2 = p >= buf + 2 ? p[-2]
8244 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
8245 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
8246
8247 if (back2 == 0xef && back1 == 0xbb)
8248 {
8249 char_u *dest = p - 2;
8250
8251 /* Usually a BOM is at the beginning of a file, and so at
8252 * the beginning of a line; then we can just step over it.
8253 */
8254 if (start == dest)
8255 start = p + 1;
8256 else
8257 {
8258 /* have to shuffle buf to close gap */
8259 int adjust_prevlen = 0;
8260
8261 if (dest < buf)
8262 {
8263 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
8264 dest = buf;
8265 }
8266 if (readlen > p - buf + 1)
8267 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
8268 readlen -= 3 - adjust_prevlen;
8269 prevlen -= adjust_prevlen;
8270 p = dest - 1;
8271 }
8272 }
8273 }
8274#endif
8275 } /* for */
8276
8277 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
8278 break;
8279 if (start < p)
8280 {
8281 /* There's part of a line in buf, store it in "prev". */
8282 if (p - start + prevlen >= prevsize)
8283 {
8284 /* need bigger "prev" buffer */
8285 char_u *newprev;
8286
8287 /* A common use case is ordinary text files and "prev" gets a
8288 * fragment of a line, so the first allocation is made
8289 * small, to avoid repeatedly 'allocing' large and
8290 * 'reallocing' small. */
8291 if (prevsize == 0)
8292 prevsize = (long)(p - start);
8293 else
8294 {
8295 long grow50pc = (prevsize * 3) / 2;
8296 long growmin = (long)((p - start) * 2 + prevlen);
8297 prevsize = grow50pc > growmin ? grow50pc : growmin;
8298 }
8299 newprev = prev == NULL ? alloc(prevsize)
8300 : vim_realloc(prev, prevsize);
8301 if (newprev == NULL)
8302 {
8303 do_outofmem_msg((long_u)prevsize);
8304 failed = TRUE;
8305 break;
8306 }
8307 prev = newprev;
8308 }
8309 /* Add the line part to end of "prev". */
8310 mch_memmove(prev + prevlen, start, p - start);
8311 prevlen += (long)(p - start);
8312 }
8313 } /* while */
8314
8315 /*
8316 * For a negative line count use only the lines at the end of the file,
8317 * free the rest.
8318 */
8319 if (!failed && maxline < 0)
8320 while (cnt > -maxline)
8321 {
8322 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
8323 --cnt;
8324 }
8325
8326 if (failed)
8327 {
8328 list_free(rettv->vval.v_list);
8329 /* readfile doc says an empty list is returned on error */
8330 rettv->vval.v_list = list_alloc();
8331 }
8332
8333 vim_free(prev);
8334 fclose(fd);
8335}
8336
8337#if defined(FEAT_RELTIME)
8338static int list2proftime(typval_T *arg, proftime_T *tm);
8339
8340/*
8341 * Convert a List to proftime_T.
8342 * Return FAIL when there is something wrong.
8343 */
8344 static int
8345list2proftime(typval_T *arg, proftime_T *tm)
8346{
8347 long n1, n2;
8348 int error = FALSE;
8349
8350 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
8351 || arg->vval.v_list->lv_len != 2)
8352 return FAIL;
8353 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
8354 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
8355# ifdef WIN3264
8356 tm->HighPart = n1;
8357 tm->LowPart = n2;
8358# else
8359 tm->tv_sec = n1;
8360 tm->tv_usec = n2;
8361# endif
8362 return error ? FAIL : OK;
8363}
8364#endif /* FEAT_RELTIME */
8365
8366/*
8367 * "reltime()" function
8368 */
8369 static void
8370f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8371{
8372#ifdef FEAT_RELTIME
8373 proftime_T res;
8374 proftime_T start;
8375
8376 if (argvars[0].v_type == VAR_UNKNOWN)
8377 {
8378 /* No arguments: get current time. */
8379 profile_start(&res);
8380 }
8381 else if (argvars[1].v_type == VAR_UNKNOWN)
8382 {
8383 if (list2proftime(&argvars[0], &res) == FAIL)
8384 return;
8385 profile_end(&res);
8386 }
8387 else
8388 {
8389 /* Two arguments: compute the difference. */
8390 if (list2proftime(&argvars[0], &start) == FAIL
8391 || list2proftime(&argvars[1], &res) == FAIL)
8392 return;
8393 profile_sub(&res, &start);
8394 }
8395
8396 if (rettv_list_alloc(rettv) == OK)
8397 {
8398 long n1, n2;
8399
8400# ifdef WIN3264
8401 n1 = res.HighPart;
8402 n2 = res.LowPart;
8403# else
8404 n1 = res.tv_sec;
8405 n2 = res.tv_usec;
8406# endif
8407 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
8408 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
8409 }
8410#endif
8411}
8412
8413#ifdef FEAT_FLOAT
8414/*
8415 * "reltimefloat()" function
8416 */
8417 static void
8418f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
8419{
8420# ifdef FEAT_RELTIME
8421 proftime_T tm;
8422# endif
8423
8424 rettv->v_type = VAR_FLOAT;
8425 rettv->vval.v_float = 0;
8426# ifdef FEAT_RELTIME
8427 if (list2proftime(&argvars[0], &tm) == OK)
8428 rettv->vval.v_float = profile_float(&tm);
8429# endif
8430}
8431#endif
8432
8433/*
8434 * "reltimestr()" function
8435 */
8436 static void
8437f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
8438{
8439#ifdef FEAT_RELTIME
8440 proftime_T tm;
8441#endif
8442
8443 rettv->v_type = VAR_STRING;
8444 rettv->vval.v_string = NULL;
8445#ifdef FEAT_RELTIME
8446 if (list2proftime(&argvars[0], &tm) == OK)
8447 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
8448#endif
8449}
8450
8451#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
8452static void make_connection(void);
8453static int check_connection(void);
8454
8455 static void
8456make_connection(void)
8457{
8458 if (X_DISPLAY == NULL
8459# ifdef FEAT_GUI
8460 && !gui.in_use
8461# endif
8462 )
8463 {
8464 x_force_connect = TRUE;
8465 setup_term_clip();
8466 x_force_connect = FALSE;
8467 }
8468}
8469
8470 static int
8471check_connection(void)
8472{
8473 make_connection();
8474 if (X_DISPLAY == NULL)
8475 {
8476 EMSG(_("E240: No connection to Vim server"));
8477 return FAIL;
8478 }
8479 return OK;
8480}
8481#endif
8482
8483#ifdef FEAT_CLIENTSERVER
8484 static void
8485remote_common(typval_T *argvars, typval_T *rettv, int expr)
8486{
8487 char_u *server_name;
8488 char_u *keys;
8489 char_u *r = NULL;
8490 char_u buf[NUMBUFLEN];
8491# ifdef WIN32
8492 HWND w;
8493# else
8494 Window w;
8495# endif
8496
8497 if (check_restricted() || check_secure())
8498 return;
8499
8500# ifdef FEAT_X11
8501 if (check_connection() == FAIL)
8502 return;
8503# endif
8504
8505 server_name = get_tv_string_chk(&argvars[0]);
8506 if (server_name == NULL)
8507 return; /* type error; errmsg already given */
8508 keys = get_tv_string_buf(&argvars[1], buf);
8509# ifdef WIN32
8510 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
8511# else
8512 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
8513 < 0)
8514# endif
8515 {
8516 if (r != NULL)
8517 EMSG(r); /* sending worked but evaluation failed */
8518 else
8519 EMSG2(_("E241: Unable to send to %s"), server_name);
8520 return;
8521 }
8522
8523 rettv->vval.v_string = r;
8524
8525 if (argvars[2].v_type != VAR_UNKNOWN)
8526 {
8527 dictitem_T v;
8528 char_u str[30];
8529 char_u *idvar;
8530
8531 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
8532 v.di_tv.v_type = VAR_STRING;
8533 v.di_tv.vval.v_string = vim_strsave(str);
8534 idvar = get_tv_string_chk(&argvars[2]);
8535 if (idvar != NULL)
8536 set_var(idvar, &v.di_tv, FALSE);
8537 vim_free(v.di_tv.vval.v_string);
8538 }
8539}
8540#endif
8541
8542/*
8543 * "remote_expr()" function
8544 */
8545 static void
8546f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
8547{
8548 rettv->v_type = VAR_STRING;
8549 rettv->vval.v_string = NULL;
8550#ifdef FEAT_CLIENTSERVER
8551 remote_common(argvars, rettv, TRUE);
8552#endif
8553}
8554
8555/*
8556 * "remote_foreground()" function
8557 */
8558 static void
8559f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8560{
8561#ifdef FEAT_CLIENTSERVER
8562# ifdef WIN32
8563 /* On Win32 it's done in this application. */
8564 {
8565 char_u *server_name = get_tv_string_chk(&argvars[0]);
8566
8567 if (server_name != NULL)
8568 serverForeground(server_name);
8569 }
8570# else
8571 /* Send a foreground() expression to the server. */
8572 argvars[1].v_type = VAR_STRING;
8573 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
8574 argvars[2].v_type = VAR_UNKNOWN;
8575 remote_common(argvars, rettv, TRUE);
8576 vim_free(argvars[1].vval.v_string);
8577# endif
8578#endif
8579}
8580
8581 static void
8582f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
8583{
8584#ifdef FEAT_CLIENTSERVER
8585 dictitem_T v;
8586 char_u *s = NULL;
8587# ifdef WIN32
8588 long_u n = 0;
8589# endif
8590 char_u *serverid;
8591
8592 if (check_restricted() || check_secure())
8593 {
8594 rettv->vval.v_number = -1;
8595 return;
8596 }
8597 serverid = get_tv_string_chk(&argvars[0]);
8598 if (serverid == NULL)
8599 {
8600 rettv->vval.v_number = -1;
8601 return; /* type error; errmsg already given */
8602 }
8603# ifdef WIN32
8604 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
8605 if (n == 0)
8606 rettv->vval.v_number = -1;
8607 else
8608 {
8609 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
8610 rettv->vval.v_number = (s != NULL);
8611 }
8612# else
8613 if (check_connection() == FAIL)
8614 return;
8615
8616 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
8617 serverStrToWin(serverid), &s);
8618# endif
8619
8620 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
8621 {
8622 char_u *retvar;
8623
8624 v.di_tv.v_type = VAR_STRING;
8625 v.di_tv.vval.v_string = vim_strsave(s);
8626 retvar = get_tv_string_chk(&argvars[1]);
8627 if (retvar != NULL)
8628 set_var(retvar, &v.di_tv, FALSE);
8629 vim_free(v.di_tv.vval.v_string);
8630 }
8631#else
8632 rettv->vval.v_number = -1;
8633#endif
8634}
8635
8636 static void
8637f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
8638{
8639 char_u *r = NULL;
8640
8641#ifdef FEAT_CLIENTSERVER
8642 char_u *serverid = get_tv_string_chk(&argvars[0]);
8643
8644 if (serverid != NULL && !check_restricted() && !check_secure())
8645 {
8646# ifdef WIN32
8647 /* The server's HWND is encoded in the 'id' parameter */
8648 long_u n = 0;
8649
8650 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
8651 if (n != 0)
8652 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
8653 if (r == NULL)
8654# else
8655 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
8656 serverStrToWin(serverid), &r, FALSE) < 0)
8657# endif
8658 EMSG(_("E277: Unable to read a server reply"));
8659 }
8660#endif
8661 rettv->v_type = VAR_STRING;
8662 rettv->vval.v_string = r;
8663}
8664
8665/*
8666 * "remote_send()" function
8667 */
8668 static void
8669f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
8670{
8671 rettv->v_type = VAR_STRING;
8672 rettv->vval.v_string = NULL;
8673#ifdef FEAT_CLIENTSERVER
8674 remote_common(argvars, rettv, FALSE);
8675#endif
8676}
8677
8678/*
8679 * "remove()" function
8680 */
8681 static void
8682f_remove(typval_T *argvars, typval_T *rettv)
8683{
8684 list_T *l;
8685 listitem_T *item, *item2;
8686 listitem_T *li;
8687 long idx;
8688 long end;
8689 char_u *key;
8690 dict_T *d;
8691 dictitem_T *di;
8692 char_u *arg_errmsg = (char_u *)N_("remove() argument");
8693
8694 if (argvars[0].v_type == VAR_DICT)
8695 {
8696 if (argvars[2].v_type != VAR_UNKNOWN)
8697 EMSG2(_(e_toomanyarg), "remove()");
8698 else if ((d = argvars[0].vval.v_dict) != NULL
8699 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
8700 {
8701 key = get_tv_string_chk(&argvars[1]);
8702 if (key != NULL)
8703 {
8704 di = dict_find(d, key, -1);
8705 if (di == NULL)
8706 EMSG2(_(e_dictkey), key);
8707 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
8708 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
8709 {
8710 *rettv = di->di_tv;
8711 init_tv(&di->di_tv);
8712 dictitem_remove(d, di);
8713 }
8714 }
8715 }
8716 }
8717 else if (argvars[0].v_type != VAR_LIST)
8718 EMSG2(_(e_listdictarg), "remove()");
8719 else if ((l = argvars[0].vval.v_list) != NULL
8720 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
8721 {
8722 int error = FALSE;
8723
8724 idx = (long)get_tv_number_chk(&argvars[1], &error);
8725 if (error)
8726 ; /* type error: do nothing, errmsg already given */
8727 else if ((item = list_find(l, idx)) == NULL)
8728 EMSGN(_(e_listidx), idx);
8729 else
8730 {
8731 if (argvars[2].v_type == VAR_UNKNOWN)
8732 {
8733 /* Remove one item, return its value. */
8734 vimlist_remove(l, item, item);
8735 *rettv = item->li_tv;
8736 vim_free(item);
8737 }
8738 else
8739 {
8740 /* Remove range of items, return list with values. */
8741 end = (long)get_tv_number_chk(&argvars[2], &error);
8742 if (error)
8743 ; /* type error: do nothing */
8744 else if ((item2 = list_find(l, end)) == NULL)
8745 EMSGN(_(e_listidx), end);
8746 else
8747 {
8748 int cnt = 0;
8749
8750 for (li = item; li != NULL; li = li->li_next)
8751 {
8752 ++cnt;
8753 if (li == item2)
8754 break;
8755 }
8756 if (li == NULL) /* didn't find "item2" after "item" */
8757 EMSG(_(e_invrange));
8758 else
8759 {
8760 vimlist_remove(l, item, item2);
8761 if (rettv_list_alloc(rettv) == OK)
8762 {
8763 l = rettv->vval.v_list;
8764 l->lv_first = item;
8765 l->lv_last = item2;
8766 item->li_prev = NULL;
8767 item2->li_next = NULL;
8768 l->lv_len = cnt;
8769 }
8770 }
8771 }
8772 }
8773 }
8774 }
8775}
8776
8777/*
8778 * "rename({from}, {to})" function
8779 */
8780 static void
8781f_rename(typval_T *argvars, typval_T *rettv)
8782{
8783 char_u buf[NUMBUFLEN];
8784
8785 if (check_restricted() || check_secure())
8786 rettv->vval.v_number = -1;
8787 else
8788 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
8789 get_tv_string_buf(&argvars[1], buf));
8790}
8791
8792/*
8793 * "repeat()" function
8794 */
8795 static void
8796f_repeat(typval_T *argvars, typval_T *rettv)
8797{
8798 char_u *p;
8799 int n;
8800 int slen;
8801 int len;
8802 char_u *r;
8803 int i;
8804
8805 n = (int)get_tv_number(&argvars[1]);
8806 if (argvars[0].v_type == VAR_LIST)
8807 {
8808 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
8809 while (n-- > 0)
8810 if (list_extend(rettv->vval.v_list,
8811 argvars[0].vval.v_list, NULL) == FAIL)
8812 break;
8813 }
8814 else
8815 {
8816 p = get_tv_string(&argvars[0]);
8817 rettv->v_type = VAR_STRING;
8818 rettv->vval.v_string = NULL;
8819
8820 slen = (int)STRLEN(p);
8821 len = slen * n;
8822 if (len <= 0)
8823 return;
8824
8825 r = alloc(len + 1);
8826 if (r != NULL)
8827 {
8828 for (i = 0; i < n; i++)
8829 mch_memmove(r + i * slen, p, (size_t)slen);
8830 r[len] = NUL;
8831 }
8832
8833 rettv->vval.v_string = r;
8834 }
8835}
8836
8837/*
8838 * "resolve()" function
8839 */
8840 static void
8841f_resolve(typval_T *argvars, typval_T *rettv)
8842{
8843 char_u *p;
8844#ifdef HAVE_READLINK
8845 char_u *buf = NULL;
8846#endif
8847
8848 p = get_tv_string(&argvars[0]);
8849#ifdef FEAT_SHORTCUT
8850 {
8851 char_u *v = NULL;
8852
8853 v = mch_resolve_shortcut(p);
8854 if (v != NULL)
8855 rettv->vval.v_string = v;
8856 else
8857 rettv->vval.v_string = vim_strsave(p);
8858 }
8859#else
8860# ifdef HAVE_READLINK
8861 {
8862 char_u *cpy;
8863 int len;
8864 char_u *remain = NULL;
8865 char_u *q;
8866 int is_relative_to_current = FALSE;
8867 int has_trailing_pathsep = FALSE;
8868 int limit = 100;
8869
8870 p = vim_strsave(p);
8871
8872 if (p[0] == '.' && (vim_ispathsep(p[1])
8873 || (p[1] == '.' && (vim_ispathsep(p[2])))))
8874 is_relative_to_current = TRUE;
8875
8876 len = STRLEN(p);
8877 if (len > 0 && after_pathsep(p, p + len))
8878 {
8879 has_trailing_pathsep = TRUE;
8880 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
8881 }
8882
8883 q = getnextcomp(p);
8884 if (*q != NUL)
8885 {
8886 /* Separate the first path component in "p", and keep the
8887 * remainder (beginning with the path separator). */
8888 remain = vim_strsave(q - 1);
8889 q[-1] = NUL;
8890 }
8891
8892 buf = alloc(MAXPATHL + 1);
8893 if (buf == NULL)
8894 goto fail;
8895
8896 for (;;)
8897 {
8898 for (;;)
8899 {
8900 len = readlink((char *)p, (char *)buf, MAXPATHL);
8901 if (len <= 0)
8902 break;
8903 buf[len] = NUL;
8904
8905 if (limit-- == 0)
8906 {
8907 vim_free(p);
8908 vim_free(remain);
8909 EMSG(_("E655: Too many symbolic links (cycle?)"));
8910 rettv->vval.v_string = NULL;
8911 goto fail;
8912 }
8913
8914 /* Ensure that the result will have a trailing path separator
8915 * if the argument has one. */
8916 if (remain == NULL && has_trailing_pathsep)
8917 add_pathsep(buf);
8918
8919 /* Separate the first path component in the link value and
8920 * concatenate the remainders. */
8921 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
8922 if (*q != NUL)
8923 {
8924 if (remain == NULL)
8925 remain = vim_strsave(q - 1);
8926 else
8927 {
8928 cpy = concat_str(q - 1, remain);
8929 if (cpy != NULL)
8930 {
8931 vim_free(remain);
8932 remain = cpy;
8933 }
8934 }
8935 q[-1] = NUL;
8936 }
8937
8938 q = gettail(p);
8939 if (q > p && *q == NUL)
8940 {
8941 /* Ignore trailing path separator. */
8942 q[-1] = NUL;
8943 q = gettail(p);
8944 }
8945 if (q > p && !mch_isFullName(buf))
8946 {
8947 /* symlink is relative to directory of argument */
8948 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
8949 if (cpy != NULL)
8950 {
8951 STRCPY(cpy, p);
8952 STRCPY(gettail(cpy), buf);
8953 vim_free(p);
8954 p = cpy;
8955 }
8956 }
8957 else
8958 {
8959 vim_free(p);
8960 p = vim_strsave(buf);
8961 }
8962 }
8963
8964 if (remain == NULL)
8965 break;
8966
8967 /* Append the first path component of "remain" to "p". */
8968 q = getnextcomp(remain + 1);
8969 len = q - remain - (*q != NUL);
8970 cpy = vim_strnsave(p, STRLEN(p) + len);
8971 if (cpy != NULL)
8972 {
8973 STRNCAT(cpy, remain, len);
8974 vim_free(p);
8975 p = cpy;
8976 }
8977 /* Shorten "remain". */
8978 if (*q != NUL)
8979 STRMOVE(remain, q - 1);
8980 else
8981 {
8982 vim_free(remain);
8983 remain = NULL;
8984 }
8985 }
8986
8987 /* If the result is a relative path name, make it explicitly relative to
8988 * the current directory if and only if the argument had this form. */
8989 if (!vim_ispathsep(*p))
8990 {
8991 if (is_relative_to_current
8992 && *p != NUL
8993 && !(p[0] == '.'
8994 && (p[1] == NUL
8995 || vim_ispathsep(p[1])
8996 || (p[1] == '.'
8997 && (p[2] == NUL
8998 || vim_ispathsep(p[2]))))))
8999 {
9000 /* Prepend "./". */
9001 cpy = concat_str((char_u *)"./", p);
9002 if (cpy != NULL)
9003 {
9004 vim_free(p);
9005 p = cpy;
9006 }
9007 }
9008 else if (!is_relative_to_current)
9009 {
9010 /* Strip leading "./". */
9011 q = p;
9012 while (q[0] == '.' && vim_ispathsep(q[1]))
9013 q += 2;
9014 if (q > p)
9015 STRMOVE(p, p + 2);
9016 }
9017 }
9018
9019 /* Ensure that the result will have no trailing path separator
9020 * if the argument had none. But keep "/" or "//". */
9021 if (!has_trailing_pathsep)
9022 {
9023 q = p + STRLEN(p);
9024 if (after_pathsep(p, q))
9025 *gettail_sep(p) = NUL;
9026 }
9027
9028 rettv->vval.v_string = p;
9029 }
9030# else
9031 rettv->vval.v_string = vim_strsave(p);
9032# endif
9033#endif
9034
9035 simplify_filename(rettv->vval.v_string);
9036
9037#ifdef HAVE_READLINK
9038fail:
9039 vim_free(buf);
9040#endif
9041 rettv->v_type = VAR_STRING;
9042}
9043
9044/*
9045 * "reverse({list})" function
9046 */
9047 static void
9048f_reverse(typval_T *argvars, typval_T *rettv)
9049{
9050 list_T *l;
9051 listitem_T *li, *ni;
9052
9053 if (argvars[0].v_type != VAR_LIST)
9054 EMSG2(_(e_listarg), "reverse()");
9055 else if ((l = argvars[0].vval.v_list) != NULL
9056 && !tv_check_lock(l->lv_lock,
9057 (char_u *)N_("reverse() argument"), TRUE))
9058 {
9059 li = l->lv_last;
9060 l->lv_first = l->lv_last = NULL;
9061 l->lv_len = 0;
9062 while (li != NULL)
9063 {
9064 ni = li->li_prev;
9065 list_append(l, li);
9066 li = ni;
9067 }
9068 rettv->vval.v_list = l;
9069 rettv->v_type = VAR_LIST;
9070 ++l->lv_refcount;
9071 l->lv_idx = l->lv_len - l->lv_idx - 1;
9072 }
9073}
9074
9075#define SP_NOMOVE 0x01 /* don't move cursor */
9076#define SP_REPEAT 0x02 /* repeat to find outer pair */
9077#define SP_RETCOUNT 0x04 /* return matchcount */
9078#define SP_SETPCMARK 0x08 /* set previous context mark */
9079#define SP_START 0x10 /* accept match at start position */
9080#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9081#define SP_END 0x40 /* leave cursor at end of match */
9082#define SP_COLUMN 0x80 /* start at cursor column */
9083
9084static int get_search_arg(typval_T *varp, int *flagsp);
9085
9086/*
9087 * Get flags for a search function.
9088 * Possibly sets "p_ws".
9089 * Returns BACKWARD, FORWARD or zero (for an error).
9090 */
9091 static int
9092get_search_arg(typval_T *varp, int *flagsp)
9093{
9094 int dir = FORWARD;
9095 char_u *flags;
9096 char_u nbuf[NUMBUFLEN];
9097 int mask;
9098
9099 if (varp->v_type != VAR_UNKNOWN)
9100 {
9101 flags = get_tv_string_buf_chk(varp, nbuf);
9102 if (flags == NULL)
9103 return 0; /* type error; errmsg already given */
9104 while (*flags != NUL)
9105 {
9106 switch (*flags)
9107 {
9108 case 'b': dir = BACKWARD; break;
9109 case 'w': p_ws = TRUE; break;
9110 case 'W': p_ws = FALSE; break;
9111 default: mask = 0;
9112 if (flagsp != NULL)
9113 switch (*flags)
9114 {
9115 case 'c': mask = SP_START; break;
9116 case 'e': mask = SP_END; break;
9117 case 'm': mask = SP_RETCOUNT; break;
9118 case 'n': mask = SP_NOMOVE; break;
9119 case 'p': mask = SP_SUBPAT; break;
9120 case 'r': mask = SP_REPEAT; break;
9121 case 's': mask = SP_SETPCMARK; break;
9122 case 'z': mask = SP_COLUMN; break;
9123 }
9124 if (mask == 0)
9125 {
9126 EMSG2(_(e_invarg2), flags);
9127 dir = 0;
9128 }
9129 else
9130 *flagsp |= mask;
9131 }
9132 if (dir == 0)
9133 break;
9134 ++flags;
9135 }
9136 }
9137 return dir;
9138}
9139
9140/*
9141 * Shared by search() and searchpos() functions.
9142 */
9143 static int
9144search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9145{
9146 int flags;
9147 char_u *pat;
9148 pos_T pos;
9149 pos_T save_cursor;
9150 int save_p_ws = p_ws;
9151 int dir;
9152 int retval = 0; /* default: FAIL */
9153 long lnum_stop = 0;
9154 proftime_T tm;
9155#ifdef FEAT_RELTIME
9156 long time_limit = 0;
9157#endif
9158 int options = SEARCH_KEEP;
9159 int subpatnum;
9160
9161 pat = get_tv_string(&argvars[0]);
9162 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
9163 if (dir == 0)
9164 goto theend;
9165 flags = *flagsp;
9166 if (flags & SP_START)
9167 options |= SEARCH_START;
9168 if (flags & SP_END)
9169 options |= SEARCH_END;
9170 if (flags & SP_COLUMN)
9171 options |= SEARCH_COL;
9172
9173 /* Optional arguments: line number to stop searching and timeout. */
9174 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9175 {
9176 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
9177 if (lnum_stop < 0)
9178 goto theend;
9179#ifdef FEAT_RELTIME
9180 if (argvars[3].v_type != VAR_UNKNOWN)
9181 {
9182 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
9183 if (time_limit < 0)
9184 goto theend;
9185 }
9186#endif
9187 }
9188
9189#ifdef FEAT_RELTIME
9190 /* Set the time limit, if there is one. */
9191 profile_setlimit(time_limit, &tm);
9192#endif
9193
9194 /*
9195 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
9196 * Check to make sure only those flags are set.
9197 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
9198 * flags cannot be set. Check for that condition also.
9199 */
9200 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
9201 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9202 {
9203 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
9204 goto theend;
9205 }
9206
9207 pos = save_cursor = curwin->w_cursor;
9208 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
9209 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
9210 if (subpatnum != FAIL)
9211 {
9212 if (flags & SP_SUBPAT)
9213 retval = subpatnum;
9214 else
9215 retval = pos.lnum;
9216 if (flags & SP_SETPCMARK)
9217 setpcmark();
9218 curwin->w_cursor = pos;
9219 if (match_pos != NULL)
9220 {
9221 /* Store the match cursor position */
9222 match_pos->lnum = pos.lnum;
9223 match_pos->col = pos.col + 1;
9224 }
9225 /* "/$" will put the cursor after the end of the line, may need to
9226 * correct that here */
9227 check_cursor();
9228 }
9229
9230 /* If 'n' flag is used: restore cursor position. */
9231 if (flags & SP_NOMOVE)
9232 curwin->w_cursor = save_cursor;
9233 else
9234 curwin->w_set_curswant = TRUE;
9235theend:
9236 p_ws = save_p_ws;
9237
9238 return retval;
9239}
9240
9241#ifdef FEAT_FLOAT
9242
9243/*
9244 * round() is not in C90, use ceil() or floor() instead.
9245 */
9246 float_T
9247vim_round(float_T f)
9248{
9249 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
9250}
9251
9252/*
9253 * "round({float})" function
9254 */
9255 static void
9256f_round(typval_T *argvars, typval_T *rettv)
9257{
9258 float_T f = 0.0;
9259
9260 rettv->v_type = VAR_FLOAT;
9261 if (get_float_arg(argvars, &f) == OK)
9262 rettv->vval.v_float = vim_round(f);
9263 else
9264 rettv->vval.v_float = 0.0;
9265}
9266#endif
9267
9268/*
9269 * "screenattr()" function
9270 */
9271 static void
9272f_screenattr(typval_T *argvars, typval_T *rettv)
9273{
9274 int row;
9275 int col;
9276 int c;
9277
9278 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9279 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9280 if (row < 0 || row >= screen_Rows
9281 || col < 0 || col >= screen_Columns)
9282 c = -1;
9283 else
9284 c = ScreenAttrs[LineOffset[row] + col];
9285 rettv->vval.v_number = c;
9286}
9287
9288/*
9289 * "screenchar()" function
9290 */
9291 static void
9292f_screenchar(typval_T *argvars, typval_T *rettv)
9293{
9294 int row;
9295 int col;
9296 int off;
9297 int c;
9298
9299 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
9300 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
9301 if (row < 0 || row >= screen_Rows
9302 || col < 0 || col >= screen_Columns)
9303 c = -1;
9304 else
9305 {
9306 off = LineOffset[row] + col;
9307#ifdef FEAT_MBYTE
9308 if (enc_utf8 && ScreenLinesUC[off] != 0)
9309 c = ScreenLinesUC[off];
9310 else
9311#endif
9312 c = ScreenLines[off];
9313 }
9314 rettv->vval.v_number = c;
9315}
9316
9317/*
9318 * "screencol()" function
9319 *
9320 * First column is 1 to be consistent with virtcol().
9321 */
9322 static void
9323f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
9324{
9325 rettv->vval.v_number = screen_screencol() + 1;
9326}
9327
9328/*
9329 * "screenrow()" function
9330 */
9331 static void
9332f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
9333{
9334 rettv->vval.v_number = screen_screenrow() + 1;
9335}
9336
9337/*
9338 * "search()" function
9339 */
9340 static void
9341f_search(typval_T *argvars, typval_T *rettv)
9342{
9343 int flags = 0;
9344
9345 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
9346}
9347
9348/*
9349 * "searchdecl()" function
9350 */
9351 static void
9352f_searchdecl(typval_T *argvars, typval_T *rettv)
9353{
9354 int locally = 1;
9355 int thisblock = 0;
9356 int error = FALSE;
9357 char_u *name;
9358
9359 rettv->vval.v_number = 1; /* default: FAIL */
9360
9361 name = get_tv_string_chk(&argvars[0]);
9362 if (argvars[1].v_type != VAR_UNKNOWN)
9363 {
9364 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
9365 if (!error && argvars[2].v_type != VAR_UNKNOWN)
9366 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
9367 }
9368 if (!error && name != NULL)
9369 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
9370 locally, thisblock, SEARCH_KEEP) == FAIL;
9371}
9372
9373/*
9374 * Used by searchpair() and searchpairpos()
9375 */
9376 static int
9377searchpair_cmn(typval_T *argvars, pos_T *match_pos)
9378{
9379 char_u *spat, *mpat, *epat;
9380 char_u *skip;
9381 int save_p_ws = p_ws;
9382 int dir;
9383 int flags = 0;
9384 char_u nbuf1[NUMBUFLEN];
9385 char_u nbuf2[NUMBUFLEN];
9386 char_u nbuf3[NUMBUFLEN];
9387 int retval = 0; /* default: FAIL */
9388 long lnum_stop = 0;
9389 long time_limit = 0;
9390
9391 /* Get the three pattern arguments: start, middle, end. */
9392 spat = get_tv_string_chk(&argvars[0]);
9393 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
9394 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
9395 if (spat == NULL || mpat == NULL || epat == NULL)
9396 goto theend; /* type error */
9397
9398 /* Handle the optional fourth argument: flags */
9399 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
9400 if (dir == 0)
9401 goto theend;
9402
9403 /* Don't accept SP_END or SP_SUBPAT.
9404 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
9405 */
9406 if ((flags & (SP_END | SP_SUBPAT)) != 0
9407 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9408 {
9409 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
9410 goto theend;
9411 }
9412
9413 /* Using 'r' implies 'W', otherwise it doesn't work. */
9414 if (flags & SP_REPEAT)
9415 p_ws = FALSE;
9416
9417 /* Optional fifth argument: skip expression */
9418 if (argvars[3].v_type == VAR_UNKNOWN
9419 || argvars[4].v_type == VAR_UNKNOWN)
9420 skip = (char_u *)"";
9421 else
9422 {
9423 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
9424 if (argvars[5].v_type != VAR_UNKNOWN)
9425 {
9426 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
9427 if (lnum_stop < 0)
9428 goto theend;
9429#ifdef FEAT_RELTIME
9430 if (argvars[6].v_type != VAR_UNKNOWN)
9431 {
9432 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
9433 if (time_limit < 0)
9434 goto theend;
9435 }
9436#endif
9437 }
9438 }
9439 if (skip == NULL)
9440 goto theend; /* type error */
9441
9442 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
9443 match_pos, lnum_stop, time_limit);
9444
9445theend:
9446 p_ws = save_p_ws;
9447
9448 return retval;
9449}
9450
9451/*
9452 * "searchpair()" function
9453 */
9454 static void
9455f_searchpair(typval_T *argvars, typval_T *rettv)
9456{
9457 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
9458}
9459
9460/*
9461 * "searchpairpos()" function
9462 */
9463 static void
9464f_searchpairpos(typval_T *argvars, typval_T *rettv)
9465{
9466 pos_T match_pos;
9467 int lnum = 0;
9468 int col = 0;
9469
9470 if (rettv_list_alloc(rettv) == FAIL)
9471 return;
9472
9473 if (searchpair_cmn(argvars, &match_pos) > 0)
9474 {
9475 lnum = match_pos.lnum;
9476 col = match_pos.col;
9477 }
9478
9479 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9480 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9481}
9482
9483/*
9484 * Search for a start/middle/end thing.
9485 * Used by searchpair(), see its documentation for the details.
9486 * Returns 0 or -1 for no match,
9487 */
9488 long
9489do_searchpair(
9490 char_u *spat, /* start pattern */
9491 char_u *mpat, /* middle pattern */
9492 char_u *epat, /* end pattern */
9493 int dir, /* BACKWARD or FORWARD */
9494 char_u *skip, /* skip expression */
9495 int flags, /* SP_SETPCMARK and other SP_ values */
9496 pos_T *match_pos,
9497 linenr_T lnum_stop, /* stop at this line if not zero */
9498 long time_limit UNUSED) /* stop after this many msec */
9499{
9500 char_u *save_cpo;
9501 char_u *pat, *pat2 = NULL, *pat3 = NULL;
9502 long retval = 0;
9503 pos_T pos;
9504 pos_T firstpos;
9505 pos_T foundpos;
9506 pos_T save_cursor;
9507 pos_T save_pos;
9508 int n;
9509 int r;
9510 int nest = 1;
9511 int err;
9512 int options = SEARCH_KEEP;
9513 proftime_T tm;
9514
9515 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
9516 save_cpo = p_cpo;
9517 p_cpo = empty_option;
9518
9519#ifdef FEAT_RELTIME
9520 /* Set the time limit, if there is one. */
9521 profile_setlimit(time_limit, &tm);
9522#endif
9523
9524 /* Make two search patterns: start/end (pat2, for in nested pairs) and
9525 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009526 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
9527 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009528 if (pat2 == NULL || pat3 == NULL)
9529 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009530 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009531 if (*mpat == NUL)
9532 STRCPY(pat3, pat2);
9533 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009534 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009535 spat, epat, mpat);
9536 if (flags & SP_START)
9537 options |= SEARCH_START;
9538
9539 save_cursor = curwin->w_cursor;
9540 pos = curwin->w_cursor;
9541 clearpos(&firstpos);
9542 clearpos(&foundpos);
9543 pat = pat3;
9544 for (;;)
9545 {
9546 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
9547 options, RE_SEARCH, lnum_stop, &tm);
9548 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
9549 /* didn't find it or found the first match again: FAIL */
9550 break;
9551
9552 if (firstpos.lnum == 0)
9553 firstpos = pos;
9554 if (equalpos(pos, foundpos))
9555 {
9556 /* Found the same position again. Can happen with a pattern that
9557 * has "\zs" at the end and searching backwards. Advance one
9558 * character and try again. */
9559 if (dir == BACKWARD)
9560 decl(&pos);
9561 else
9562 incl(&pos);
9563 }
9564 foundpos = pos;
9565
9566 /* clear the start flag to avoid getting stuck here */
9567 options &= ~SEARCH_START;
9568
9569 /* If the skip pattern matches, ignore this match. */
9570 if (*skip != NUL)
9571 {
9572 save_pos = curwin->w_cursor;
9573 curwin->w_cursor = pos;
9574 r = eval_to_bool(skip, &err, NULL, FALSE);
9575 curwin->w_cursor = save_pos;
9576 if (err)
9577 {
9578 /* Evaluating {skip} caused an error, break here. */
9579 curwin->w_cursor = save_cursor;
9580 retval = -1;
9581 break;
9582 }
9583 if (r)
9584 continue;
9585 }
9586
9587 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
9588 {
9589 /* Found end when searching backwards or start when searching
9590 * forward: nested pair. */
9591 ++nest;
9592 pat = pat2; /* nested, don't search for middle */
9593 }
9594 else
9595 {
9596 /* Found end when searching forward or start when searching
9597 * backward: end of (nested) pair; or found middle in outer pair. */
9598 if (--nest == 1)
9599 pat = pat3; /* outer level, search for middle */
9600 }
9601
9602 if (nest == 0)
9603 {
9604 /* Found the match: return matchcount or line number. */
9605 if (flags & SP_RETCOUNT)
9606 ++retval;
9607 else
9608 retval = pos.lnum;
9609 if (flags & SP_SETPCMARK)
9610 setpcmark();
9611 curwin->w_cursor = pos;
9612 if (!(flags & SP_REPEAT))
9613 break;
9614 nest = 1; /* search for next unmatched */
9615 }
9616 }
9617
9618 if (match_pos != NULL)
9619 {
9620 /* Store the match cursor position */
9621 match_pos->lnum = curwin->w_cursor.lnum;
9622 match_pos->col = curwin->w_cursor.col + 1;
9623 }
9624
9625 /* If 'n' flag is used or search failed: restore cursor position. */
9626 if ((flags & SP_NOMOVE) || retval == 0)
9627 curwin->w_cursor = save_cursor;
9628
9629theend:
9630 vim_free(pat2);
9631 vim_free(pat3);
9632 if (p_cpo == empty_option)
9633 p_cpo = save_cpo;
9634 else
9635 /* Darn, evaluating the {skip} expression changed the value. */
9636 free_string_option(save_cpo);
9637
9638 return retval;
9639}
9640
9641/*
9642 * "searchpos()" function
9643 */
9644 static void
9645f_searchpos(typval_T *argvars, typval_T *rettv)
9646{
9647 pos_T match_pos;
9648 int lnum = 0;
9649 int col = 0;
9650 int n;
9651 int flags = 0;
9652
9653 if (rettv_list_alloc(rettv) == FAIL)
9654 return;
9655
9656 n = search_cmn(argvars, &match_pos, &flags);
9657 if (n > 0)
9658 {
9659 lnum = match_pos.lnum;
9660 col = match_pos.col;
9661 }
9662
9663 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
9664 list_append_number(rettv->vval.v_list, (varnumber_T)col);
9665 if (flags & SP_SUBPAT)
9666 list_append_number(rettv->vval.v_list, (varnumber_T)n);
9667}
9668
9669 static void
9670f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
9671{
9672#ifdef FEAT_CLIENTSERVER
9673 char_u buf[NUMBUFLEN];
9674 char_u *server = get_tv_string_chk(&argvars[0]);
9675 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
9676
9677 rettv->vval.v_number = -1;
9678 if (server == NULL || reply == NULL)
9679 return;
9680 if (check_restricted() || check_secure())
9681 return;
9682# ifdef FEAT_X11
9683 if (check_connection() == FAIL)
9684 return;
9685# endif
9686
9687 if (serverSendReply(server, reply) < 0)
9688 {
9689 EMSG(_("E258: Unable to send to client"));
9690 return;
9691 }
9692 rettv->vval.v_number = 0;
9693#else
9694 rettv->vval.v_number = -1;
9695#endif
9696}
9697
9698 static void
9699f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
9700{
9701 char_u *r = NULL;
9702
9703#ifdef FEAT_CLIENTSERVER
9704# ifdef WIN32
9705 r = serverGetVimNames();
9706# else
9707 make_connection();
9708 if (X_DISPLAY != NULL)
9709 r = serverGetVimNames(X_DISPLAY);
9710# endif
9711#endif
9712 rettv->v_type = VAR_STRING;
9713 rettv->vval.v_string = r;
9714}
9715
9716/*
9717 * "setbufvar()" function
9718 */
9719 static void
9720f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
9721{
9722 buf_T *buf;
9723 char_u *varname, *bufvarname;
9724 typval_T *varp;
9725 char_u nbuf[NUMBUFLEN];
9726
9727 if (check_restricted() || check_secure())
9728 return;
9729 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9730 varname = get_tv_string_chk(&argvars[1]);
9731 buf = get_buf_tv(&argvars[0], FALSE);
9732 varp = &argvars[2];
9733
9734 if (buf != NULL && varname != NULL && varp != NULL)
9735 {
9736 if (*varname == '&')
9737 {
9738 long numval;
9739 char_u *strval;
9740 int error = FALSE;
9741 aco_save_T aco;
9742
9743 /* set curbuf to be our buf, temporarily */
9744 aucmd_prepbuf(&aco, buf);
9745
9746 ++varname;
9747 numval = (long)get_tv_number_chk(varp, &error);
9748 strval = get_tv_string_buf_chk(varp, nbuf);
9749 if (!error && strval != NULL)
9750 set_option_value(varname, numval, strval, OPT_LOCAL);
9751
9752 /* reset notion of buffer */
9753 aucmd_restbuf(&aco);
9754 }
9755 else
9756 {
9757 buf_T *save_curbuf = curbuf;
9758
9759 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
9760 if (bufvarname != NULL)
9761 {
9762 curbuf = buf;
9763 STRCPY(bufvarname, "b:");
9764 STRCPY(bufvarname + 2, varname);
9765 set_var(bufvarname, varp, TRUE);
9766 vim_free(bufvarname);
9767 curbuf = save_curbuf;
9768 }
9769 }
9770 }
9771}
9772
9773 static void
9774f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
9775{
9776 dict_T *d;
9777 dictitem_T *di;
9778 char_u *csearch;
9779
9780 if (argvars[0].v_type != VAR_DICT)
9781 {
9782 EMSG(_(e_dictreq));
9783 return;
9784 }
9785
9786 if ((d = argvars[0].vval.v_dict) != NULL)
9787 {
9788 csearch = get_dict_string(d, (char_u *)"char", FALSE);
9789 if (csearch != NULL)
9790 {
9791#ifdef FEAT_MBYTE
9792 if (enc_utf8)
9793 {
9794 int pcc[MAX_MCO];
9795 int c = utfc_ptr2char(csearch, pcc);
9796
9797 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
9798 }
9799 else
9800#endif
9801 set_last_csearch(PTR2CHAR(csearch),
9802 csearch, MB_PTR2LEN(csearch));
9803 }
9804
9805 di = dict_find(d, (char_u *)"forward", -1);
9806 if (di != NULL)
9807 set_csearch_direction((int)get_tv_number(&di->di_tv)
9808 ? FORWARD : BACKWARD);
9809
9810 di = dict_find(d, (char_u *)"until", -1);
9811 if (di != NULL)
9812 set_csearch_until(!!get_tv_number(&di->di_tv));
9813 }
9814}
9815
9816/*
9817 * "setcmdpos()" function
9818 */
9819 static void
9820f_setcmdpos(typval_T *argvars, typval_T *rettv)
9821{
9822 int pos = (int)get_tv_number(&argvars[0]) - 1;
9823
9824 if (pos >= 0)
9825 rettv->vval.v_number = set_cmdline_pos(pos);
9826}
9827
9828/*
9829 * "setfperm({fname}, {mode})" function
9830 */
9831 static void
9832f_setfperm(typval_T *argvars, typval_T *rettv)
9833{
9834 char_u *fname;
9835 char_u modebuf[NUMBUFLEN];
9836 char_u *mode_str;
9837 int i;
9838 int mask;
9839 int mode = 0;
9840
9841 rettv->vval.v_number = 0;
9842 fname = get_tv_string_chk(&argvars[0]);
9843 if (fname == NULL)
9844 return;
9845 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
9846 if (mode_str == NULL)
9847 return;
9848 if (STRLEN(mode_str) != 9)
9849 {
9850 EMSG2(_(e_invarg2), mode_str);
9851 return;
9852 }
9853
9854 mask = 1;
9855 for (i = 8; i >= 0; --i)
9856 {
9857 if (mode_str[i] != '-')
9858 mode |= mask;
9859 mask = mask << 1;
9860 }
9861 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
9862}
9863
9864/*
9865 * "setline()" function
9866 */
9867 static void
9868f_setline(typval_T *argvars, typval_T *rettv)
9869{
9870 linenr_T lnum;
9871 char_u *line = NULL;
9872 list_T *l = NULL;
9873 listitem_T *li = NULL;
9874 long added = 0;
9875 linenr_T lcount = curbuf->b_ml.ml_line_count;
9876
9877 lnum = get_tv_lnum(&argvars[0]);
9878 if (argvars[1].v_type == VAR_LIST)
9879 {
9880 l = argvars[1].vval.v_list;
9881 li = l->lv_first;
9882 }
9883 else
9884 line = get_tv_string_chk(&argvars[1]);
9885
9886 /* default result is zero == OK */
9887 for (;;)
9888 {
9889 if (l != NULL)
9890 {
9891 /* list argument, get next string */
9892 if (li == NULL)
9893 break;
9894 line = get_tv_string_chk(&li->li_tv);
9895 li = li->li_next;
9896 }
9897
9898 rettv->vval.v_number = 1; /* FAIL */
9899 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
9900 break;
9901
9902 /* When coming here from Insert mode, sync undo, so that this can be
9903 * undone separately from what was previously inserted. */
9904 if (u_sync_once == 2)
9905 {
9906 u_sync_once = 1; /* notify that u_sync() was called */
9907 u_sync(TRUE);
9908 }
9909
9910 if (lnum <= curbuf->b_ml.ml_line_count)
9911 {
9912 /* existing line, replace it */
9913 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
9914 {
9915 changed_bytes(lnum, 0);
9916 if (lnum == curwin->w_cursor.lnum)
9917 check_cursor_col();
9918 rettv->vval.v_number = 0; /* OK */
9919 }
9920 }
9921 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
9922 {
9923 /* lnum is one past the last line, append the line */
9924 ++added;
9925 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
9926 rettv->vval.v_number = 0; /* OK */
9927 }
9928
9929 if (l == NULL) /* only one string argument */
9930 break;
9931 ++lnum;
9932 }
9933
9934 if (added > 0)
9935 appended_lines_mark(lcount, added);
9936}
9937
Bram Moolenaard823fa92016-08-12 16:29:27 +02009938static 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 +02009939
9940/*
9941 * Used by "setqflist()" and "setloclist()" functions
9942 */
9943 static void
9944set_qf_ll_list(
9945 win_T *wp UNUSED,
9946 typval_T *list_arg UNUSED,
9947 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +02009948 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009949 typval_T *rettv)
9950{
9951#ifdef FEAT_QUICKFIX
9952 static char *e_invact = N_("E927: Invalid action: '%s'");
9953 char_u *act;
9954 int action = 0;
9955#endif
9956
9957 rettv->vval.v_number = -1;
9958
9959#ifdef FEAT_QUICKFIX
9960 if (list_arg->v_type != VAR_LIST)
9961 EMSG(_(e_listreq));
9962 else
9963 {
9964 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +02009965 dict_T *d = NULL;
9966 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009967
9968 if (action_arg->v_type == VAR_STRING)
9969 {
9970 act = get_tv_string_chk(action_arg);
9971 if (act == NULL)
9972 return; /* type error; errmsg already given */
9973 if ((*act == 'a' || *act == 'r' || *act == ' ') && act[1] == NUL)
9974 action = *act;
9975 else
9976 EMSG2(_(e_invact), act);
9977 }
9978 else if (action_arg->v_type == VAR_UNKNOWN)
9979 action = ' ';
9980 else
9981 EMSG(_(e_stringreq));
9982
Bram Moolenaard823fa92016-08-12 16:29:27 +02009983 if (action_arg->v_type != VAR_UNKNOWN
9984 && what_arg->v_type != VAR_UNKNOWN)
9985 {
9986 if (what_arg->v_type == VAR_DICT)
9987 d = what_arg->vval.v_dict;
9988 else
9989 {
9990 EMSG(_(e_dictreq));
9991 valid_dict = FALSE;
9992 }
9993 }
9994
9995 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
9996 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()"), d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009997 rettv->vval.v_number = 0;
9998 }
9999#endif
10000}
10001
10002/*
10003 * "setloclist()" function
10004 */
10005 static void
10006f_setloclist(typval_T *argvars, typval_T *rettv)
10007{
10008 win_T *win;
10009
10010 rettv->vval.v_number = -1;
10011
10012 win = find_win_by_nr(&argvars[0], NULL);
10013 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020010014 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010015}
10016
10017/*
10018 * "setmatches()" function
10019 */
10020 static void
10021f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10022{
10023#ifdef FEAT_SEARCH_EXTRA
10024 list_T *l;
10025 listitem_T *li;
10026 dict_T *d;
10027 list_T *s = NULL;
10028
10029 rettv->vval.v_number = -1;
10030 if (argvars[0].v_type != VAR_LIST)
10031 {
10032 EMSG(_(e_listreq));
10033 return;
10034 }
10035 if ((l = argvars[0].vval.v_list) != NULL)
10036 {
10037
10038 /* To some extent make sure that we are dealing with a list from
10039 * "getmatches()". */
10040 li = l->lv_first;
10041 while (li != NULL)
10042 {
10043 if (li->li_tv.v_type != VAR_DICT
10044 || (d = li->li_tv.vval.v_dict) == NULL)
10045 {
10046 EMSG(_(e_invarg));
10047 return;
10048 }
10049 if (!(dict_find(d, (char_u *)"group", -1) != NULL
10050 && (dict_find(d, (char_u *)"pattern", -1) != NULL
10051 || dict_find(d, (char_u *)"pos1", -1) != NULL)
10052 && dict_find(d, (char_u *)"priority", -1) != NULL
10053 && dict_find(d, (char_u *)"id", -1) != NULL))
10054 {
10055 EMSG(_(e_invarg));
10056 return;
10057 }
10058 li = li->li_next;
10059 }
10060
10061 clear_matches(curwin);
10062 li = l->lv_first;
10063 while (li != NULL)
10064 {
10065 int i = 0;
10066 char_u buf[5];
10067 dictitem_T *di;
10068 char_u *group;
10069 int priority;
10070 int id;
10071 char_u *conceal;
10072
10073 d = li->li_tv.vval.v_dict;
10074 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
10075 {
10076 if (s == NULL)
10077 {
10078 s = list_alloc();
10079 if (s == NULL)
10080 return;
10081 }
10082
10083 /* match from matchaddpos() */
10084 for (i = 1; i < 9; i++)
10085 {
10086 sprintf((char *)buf, (char *)"pos%d", i);
10087 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
10088 {
10089 if (di->di_tv.v_type != VAR_LIST)
10090 return;
10091
10092 list_append_tv(s, &di->di_tv);
10093 s->lv_refcount++;
10094 }
10095 else
10096 break;
10097 }
10098 }
10099
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010100 group = get_dict_string(d, (char_u *)"group", TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010101 priority = (int)get_dict_number(d, (char_u *)"priority");
10102 id = (int)get_dict_number(d, (char_u *)"id");
10103 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010104 ? get_dict_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010105 : NULL;
10106 if (i == 0)
10107 {
10108 match_add(curwin, group,
10109 get_dict_string(d, (char_u *)"pattern", FALSE),
10110 priority, id, NULL, conceal);
10111 }
10112 else
10113 {
10114 match_add(curwin, group, NULL, priority, id, s, conceal);
10115 list_unref(s);
10116 s = NULL;
10117 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010118 vim_free(group);
10119 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010120
10121 li = li->li_next;
10122 }
10123 rettv->vval.v_number = 0;
10124 }
10125#endif
10126}
10127
10128/*
10129 * "setpos()" function
10130 */
10131 static void
10132f_setpos(typval_T *argvars, typval_T *rettv)
10133{
10134 pos_T pos;
10135 int fnum;
10136 char_u *name;
10137 colnr_T curswant = -1;
10138
10139 rettv->vval.v_number = -1;
10140 name = get_tv_string_chk(argvars);
10141 if (name != NULL)
10142 {
10143 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10144 {
10145 if (--pos.col < 0)
10146 pos.col = 0;
10147 if (name[0] == '.' && name[1] == NUL)
10148 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010149 /* set cursor; "fnum" is ignored */
10150 curwin->w_cursor = pos;
10151 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010152 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010153 curwin->w_curswant = curswant - 1;
10154 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010155 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010156 check_cursor();
10157 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010158 }
10159 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10160 {
10161 /* set mark */
10162 if (setmark_pos(name[1], &pos, fnum) == OK)
10163 rettv->vval.v_number = 0;
10164 }
10165 else
10166 EMSG(_(e_invarg));
10167 }
10168 }
10169}
10170
10171/*
10172 * "setqflist()" function
10173 */
10174 static void
10175f_setqflist(typval_T *argvars, typval_T *rettv)
10176{
Bram Moolenaard823fa92016-08-12 16:29:27 +020010177 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010178}
10179
10180/*
10181 * "setreg()" function
10182 */
10183 static void
10184f_setreg(typval_T *argvars, typval_T *rettv)
10185{
10186 int regname;
10187 char_u *strregname;
10188 char_u *stropt;
10189 char_u *strval;
10190 int append;
10191 char_u yank_type;
10192 long block_len;
10193
10194 block_len = -1;
10195 yank_type = MAUTO;
10196 append = FALSE;
10197
10198 strregname = get_tv_string_chk(argvars);
10199 rettv->vval.v_number = 1; /* FAIL is default */
10200
10201 if (strregname == NULL)
10202 return; /* type error; errmsg already given */
10203 regname = *strregname;
10204 if (regname == 0 || regname == '@')
10205 regname = '"';
10206
10207 if (argvars[2].v_type != VAR_UNKNOWN)
10208 {
10209 stropt = get_tv_string_chk(&argvars[2]);
10210 if (stropt == NULL)
10211 return; /* type error */
10212 for (; *stropt != NUL; ++stropt)
10213 switch (*stropt)
10214 {
10215 case 'a': case 'A': /* append */
10216 append = TRUE;
10217 break;
10218 case 'v': case 'c': /* character-wise selection */
10219 yank_type = MCHAR;
10220 break;
10221 case 'V': case 'l': /* line-wise selection */
10222 yank_type = MLINE;
10223 break;
10224 case 'b': case Ctrl_V: /* block-wise selection */
10225 yank_type = MBLOCK;
10226 if (VIM_ISDIGIT(stropt[1]))
10227 {
10228 ++stropt;
10229 block_len = getdigits(&stropt) - 1;
10230 --stropt;
10231 }
10232 break;
10233 }
10234 }
10235
10236 if (argvars[1].v_type == VAR_LIST)
10237 {
10238 char_u **lstval;
10239 char_u **allocval;
10240 char_u buf[NUMBUFLEN];
10241 char_u **curval;
10242 char_u **curallocval;
10243 list_T *ll = argvars[1].vval.v_list;
10244 listitem_T *li;
10245 int len;
10246
10247 /* If the list is NULL handle like an empty list. */
10248 len = ll == NULL ? 0 : ll->lv_len;
10249
10250 /* First half: use for pointers to result lines; second half: use for
10251 * pointers to allocated copies. */
10252 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
10253 if (lstval == NULL)
10254 return;
10255 curval = lstval;
10256 allocval = lstval + len + 2;
10257 curallocval = allocval;
10258
10259 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
10260 li = li->li_next)
10261 {
10262 strval = get_tv_string_buf_chk(&li->li_tv, buf);
10263 if (strval == NULL)
10264 goto free_lstval;
10265 if (strval == buf)
10266 {
10267 /* Need to make a copy, next get_tv_string_buf_chk() will
10268 * overwrite the string. */
10269 strval = vim_strsave(buf);
10270 if (strval == NULL)
10271 goto free_lstval;
10272 *curallocval++ = strval;
10273 }
10274 *curval++ = strval;
10275 }
10276 *curval++ = NULL;
10277
10278 write_reg_contents_lst(regname, lstval, -1,
10279 append, yank_type, block_len);
10280free_lstval:
10281 while (curallocval > allocval)
10282 vim_free(*--curallocval);
10283 vim_free(lstval);
10284 }
10285 else
10286 {
10287 strval = get_tv_string_chk(&argvars[1]);
10288 if (strval == NULL)
10289 return;
10290 write_reg_contents_ex(regname, strval, -1,
10291 append, yank_type, block_len);
10292 }
10293 rettv->vval.v_number = 0;
10294}
10295
10296/*
10297 * "settabvar()" function
10298 */
10299 static void
10300f_settabvar(typval_T *argvars, typval_T *rettv)
10301{
10302#ifdef FEAT_WINDOWS
10303 tabpage_T *save_curtab;
10304 tabpage_T *tp;
10305#endif
10306 char_u *varname, *tabvarname;
10307 typval_T *varp;
10308
10309 rettv->vval.v_number = 0;
10310
10311 if (check_restricted() || check_secure())
10312 return;
10313
10314#ifdef FEAT_WINDOWS
10315 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
10316#endif
10317 varname = get_tv_string_chk(&argvars[1]);
10318 varp = &argvars[2];
10319
10320 if (varname != NULL && varp != NULL
10321#ifdef FEAT_WINDOWS
10322 && tp != NULL
10323#endif
10324 )
10325 {
10326#ifdef FEAT_WINDOWS
10327 save_curtab = curtab;
10328 goto_tabpage_tp(tp, FALSE, FALSE);
10329#endif
10330
10331 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
10332 if (tabvarname != NULL)
10333 {
10334 STRCPY(tabvarname, "t:");
10335 STRCPY(tabvarname + 2, varname);
10336 set_var(tabvarname, varp, TRUE);
10337 vim_free(tabvarname);
10338 }
10339
10340#ifdef FEAT_WINDOWS
10341 /* Restore current tabpage */
10342 if (valid_tabpage(save_curtab))
10343 goto_tabpage_tp(save_curtab, FALSE, FALSE);
10344#endif
10345 }
10346}
10347
10348/*
10349 * "settabwinvar()" function
10350 */
10351 static void
10352f_settabwinvar(typval_T *argvars, typval_T *rettv)
10353{
10354 setwinvar(argvars, rettv, 1);
10355}
10356
10357/*
10358 * "setwinvar()" function
10359 */
10360 static void
10361f_setwinvar(typval_T *argvars, typval_T *rettv)
10362{
10363 setwinvar(argvars, rettv, 0);
10364}
10365
10366#ifdef FEAT_CRYPT
10367/*
10368 * "sha256({string})" function
10369 */
10370 static void
10371f_sha256(typval_T *argvars, typval_T *rettv)
10372{
10373 char_u *p;
10374
10375 p = get_tv_string(&argvars[0]);
10376 rettv->vval.v_string = vim_strsave(
10377 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
10378 rettv->v_type = VAR_STRING;
10379}
10380#endif /* FEAT_CRYPT */
10381
10382/*
10383 * "shellescape({string})" function
10384 */
10385 static void
10386f_shellescape(typval_T *argvars, typval_T *rettv)
10387{
10388 rettv->vval.v_string = vim_strsave_shellescape(
10389 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
10390 rettv->v_type = VAR_STRING;
10391}
10392
10393/*
10394 * shiftwidth() function
10395 */
10396 static void
10397f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
10398{
10399 rettv->vval.v_number = get_sw_value(curbuf);
10400}
10401
10402/*
10403 * "simplify()" function
10404 */
10405 static void
10406f_simplify(typval_T *argvars, typval_T *rettv)
10407{
10408 char_u *p;
10409
10410 p = get_tv_string(&argvars[0]);
10411 rettv->vval.v_string = vim_strsave(p);
10412 simplify_filename(rettv->vval.v_string); /* simplify in place */
10413 rettv->v_type = VAR_STRING;
10414}
10415
10416#ifdef FEAT_FLOAT
10417/*
10418 * "sin()" function
10419 */
10420 static void
10421f_sin(typval_T *argvars, typval_T *rettv)
10422{
10423 float_T f = 0.0;
10424
10425 rettv->v_type = VAR_FLOAT;
10426 if (get_float_arg(argvars, &f) == OK)
10427 rettv->vval.v_float = sin(f);
10428 else
10429 rettv->vval.v_float = 0.0;
10430}
10431
10432/*
10433 * "sinh()" function
10434 */
10435 static void
10436f_sinh(typval_T *argvars, typval_T *rettv)
10437{
10438 float_T f = 0.0;
10439
10440 rettv->v_type = VAR_FLOAT;
10441 if (get_float_arg(argvars, &f) == OK)
10442 rettv->vval.v_float = sinh(f);
10443 else
10444 rettv->vval.v_float = 0.0;
10445}
10446#endif
10447
10448static int
10449#ifdef __BORLANDC__
10450 _RTLENTRYF
10451#endif
10452 item_compare(const void *s1, const void *s2);
10453static int
10454#ifdef __BORLANDC__
10455 _RTLENTRYF
10456#endif
10457 item_compare2(const void *s1, const void *s2);
10458
10459/* struct used in the array that's given to qsort() */
10460typedef struct
10461{
10462 listitem_T *item;
10463 int idx;
10464} sortItem_T;
10465
10466/* struct storing information about current sort */
10467typedef struct
10468{
10469 int item_compare_ic;
10470 int item_compare_numeric;
10471 int item_compare_numbers;
10472#ifdef FEAT_FLOAT
10473 int item_compare_float;
10474#endif
10475 char_u *item_compare_func;
10476 partial_T *item_compare_partial;
10477 dict_T *item_compare_selfdict;
10478 int item_compare_func_err;
10479 int item_compare_keep_zero;
10480} sortinfo_T;
10481static sortinfo_T *sortinfo = NULL;
10482static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
10483#define ITEM_COMPARE_FAIL 999
10484
10485/*
10486 * Compare functions for f_sort() and f_uniq() below.
10487 */
10488 static int
10489#ifdef __BORLANDC__
10490_RTLENTRYF
10491#endif
10492item_compare(const void *s1, const void *s2)
10493{
10494 sortItem_T *si1, *si2;
10495 typval_T *tv1, *tv2;
10496 char_u *p1, *p2;
10497 char_u *tofree1 = NULL, *tofree2 = NULL;
10498 int res;
10499 char_u numbuf1[NUMBUFLEN];
10500 char_u numbuf2[NUMBUFLEN];
10501
10502 si1 = (sortItem_T *)s1;
10503 si2 = (sortItem_T *)s2;
10504 tv1 = &si1->item->li_tv;
10505 tv2 = &si2->item->li_tv;
10506
10507 if (sortinfo->item_compare_numbers)
10508 {
10509 varnumber_T v1 = get_tv_number(tv1);
10510 varnumber_T v2 = get_tv_number(tv2);
10511
10512 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10513 }
10514
10515#ifdef FEAT_FLOAT
10516 if (sortinfo->item_compare_float)
10517 {
10518 float_T v1 = get_tv_float(tv1);
10519 float_T v2 = get_tv_float(tv2);
10520
10521 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
10522 }
10523#endif
10524
10525 /* tv2string() puts quotes around a string and allocates memory. Don't do
10526 * that for string variables. Use a single quote when comparing with a
10527 * non-string to do what the docs promise. */
10528 if (tv1->v_type == VAR_STRING)
10529 {
10530 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
10531 p1 = (char_u *)"'";
10532 else
10533 p1 = tv1->vval.v_string;
10534 }
10535 else
10536 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
10537 if (tv2->v_type == VAR_STRING)
10538 {
10539 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
10540 p2 = (char_u *)"'";
10541 else
10542 p2 = tv2->vval.v_string;
10543 }
10544 else
10545 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
10546 if (p1 == NULL)
10547 p1 = (char_u *)"";
10548 if (p2 == NULL)
10549 p2 = (char_u *)"";
10550 if (!sortinfo->item_compare_numeric)
10551 {
10552 if (sortinfo->item_compare_ic)
10553 res = STRICMP(p1, p2);
10554 else
10555 res = STRCMP(p1, p2);
10556 }
10557 else
10558 {
10559 double n1, n2;
10560 n1 = strtod((char *)p1, (char **)&p1);
10561 n2 = strtod((char *)p2, (char **)&p2);
10562 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
10563 }
10564
10565 /* When the result would be zero, compare the item indexes. Makes the
10566 * sort stable. */
10567 if (res == 0 && !sortinfo->item_compare_keep_zero)
10568 res = si1->idx > si2->idx ? 1 : -1;
10569
10570 vim_free(tofree1);
10571 vim_free(tofree2);
10572 return res;
10573}
10574
10575 static int
10576#ifdef __BORLANDC__
10577_RTLENTRYF
10578#endif
10579item_compare2(const void *s1, const void *s2)
10580{
10581 sortItem_T *si1, *si2;
10582 int res;
10583 typval_T rettv;
10584 typval_T argv[3];
10585 int dummy;
10586 char_u *func_name;
10587 partial_T *partial = sortinfo->item_compare_partial;
10588
10589 /* shortcut after failure in previous call; compare all items equal */
10590 if (sortinfo->item_compare_func_err)
10591 return 0;
10592
10593 si1 = (sortItem_T *)s1;
10594 si2 = (sortItem_T *)s2;
10595
10596 if (partial == NULL)
10597 func_name = sortinfo->item_compare_func;
10598 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020010599 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010600
10601 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
10602 * in the copy without changing the original list items. */
10603 copy_tv(&si1->item->li_tv, &argv[0]);
10604 copy_tv(&si2->item->li_tv, &argv[1]);
10605
10606 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
10607 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020010608 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010609 partial, sortinfo->item_compare_selfdict);
10610 clear_tv(&argv[0]);
10611 clear_tv(&argv[1]);
10612
10613 if (res == FAIL)
10614 res = ITEM_COMPARE_FAIL;
10615 else
10616 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
10617 if (sortinfo->item_compare_func_err)
10618 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
10619 clear_tv(&rettv);
10620
10621 /* When the result would be zero, compare the pointers themselves. Makes
10622 * the sort stable. */
10623 if (res == 0 && !sortinfo->item_compare_keep_zero)
10624 res = si1->idx > si2->idx ? 1 : -1;
10625
10626 return res;
10627}
10628
10629/*
10630 * "sort({list})" function
10631 */
10632 static void
10633do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
10634{
10635 list_T *l;
10636 listitem_T *li;
10637 sortItem_T *ptrs;
10638 sortinfo_T *old_sortinfo;
10639 sortinfo_T info;
10640 long len;
10641 long i;
10642
10643 /* Pointer to current info struct used in compare function. Save and
10644 * restore the current one for nested calls. */
10645 old_sortinfo = sortinfo;
10646 sortinfo = &info;
10647
10648 if (argvars[0].v_type != VAR_LIST)
10649 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
10650 else
10651 {
10652 l = argvars[0].vval.v_list;
10653 if (l == NULL || tv_check_lock(l->lv_lock,
10654 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
10655 TRUE))
10656 goto theend;
10657 rettv->vval.v_list = l;
10658 rettv->v_type = VAR_LIST;
10659 ++l->lv_refcount;
10660
10661 len = list_len(l);
10662 if (len <= 1)
10663 goto theend; /* short list sorts pretty quickly */
10664
10665 info.item_compare_ic = FALSE;
10666 info.item_compare_numeric = FALSE;
10667 info.item_compare_numbers = FALSE;
10668#ifdef FEAT_FLOAT
10669 info.item_compare_float = FALSE;
10670#endif
10671 info.item_compare_func = NULL;
10672 info.item_compare_partial = NULL;
10673 info.item_compare_selfdict = NULL;
10674 if (argvars[1].v_type != VAR_UNKNOWN)
10675 {
10676 /* optional second argument: {func} */
10677 if (argvars[1].v_type == VAR_FUNC)
10678 info.item_compare_func = argvars[1].vval.v_string;
10679 else if (argvars[1].v_type == VAR_PARTIAL)
10680 info.item_compare_partial = argvars[1].vval.v_partial;
10681 else
10682 {
10683 int error = FALSE;
10684
10685 i = (long)get_tv_number_chk(&argvars[1], &error);
10686 if (error)
10687 goto theend; /* type error; errmsg already given */
10688 if (i == 1)
10689 info.item_compare_ic = TRUE;
10690 else if (argvars[1].v_type != VAR_NUMBER)
10691 info.item_compare_func = get_tv_string(&argvars[1]);
10692 else if (i != 0)
10693 {
10694 EMSG(_(e_invarg));
10695 goto theend;
10696 }
10697 if (info.item_compare_func != NULL)
10698 {
10699 if (*info.item_compare_func == NUL)
10700 {
10701 /* empty string means default sort */
10702 info.item_compare_func = NULL;
10703 }
10704 else if (STRCMP(info.item_compare_func, "n") == 0)
10705 {
10706 info.item_compare_func = NULL;
10707 info.item_compare_numeric = TRUE;
10708 }
10709 else if (STRCMP(info.item_compare_func, "N") == 0)
10710 {
10711 info.item_compare_func = NULL;
10712 info.item_compare_numbers = TRUE;
10713 }
10714#ifdef FEAT_FLOAT
10715 else if (STRCMP(info.item_compare_func, "f") == 0)
10716 {
10717 info.item_compare_func = NULL;
10718 info.item_compare_float = TRUE;
10719 }
10720#endif
10721 else if (STRCMP(info.item_compare_func, "i") == 0)
10722 {
10723 info.item_compare_func = NULL;
10724 info.item_compare_ic = TRUE;
10725 }
10726 }
10727 }
10728
10729 if (argvars[2].v_type != VAR_UNKNOWN)
10730 {
10731 /* optional third argument: {dict} */
10732 if (argvars[2].v_type != VAR_DICT)
10733 {
10734 EMSG(_(e_dictreq));
10735 goto theend;
10736 }
10737 info.item_compare_selfdict = argvars[2].vval.v_dict;
10738 }
10739 }
10740
10741 /* Make an array with each entry pointing to an item in the List. */
10742 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
10743 if (ptrs == NULL)
10744 goto theend;
10745
10746 i = 0;
10747 if (sort)
10748 {
10749 /* sort(): ptrs will be the list to sort */
10750 for (li = l->lv_first; li != NULL; li = li->li_next)
10751 {
10752 ptrs[i].item = li;
10753 ptrs[i].idx = i;
10754 ++i;
10755 }
10756
10757 info.item_compare_func_err = FALSE;
10758 info.item_compare_keep_zero = FALSE;
10759 /* test the compare function */
10760 if ((info.item_compare_func != NULL
10761 || info.item_compare_partial != NULL)
10762 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
10763 == ITEM_COMPARE_FAIL)
10764 EMSG(_("E702: Sort compare function failed"));
10765 else
10766 {
10767 /* Sort the array with item pointers. */
10768 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
10769 info.item_compare_func == NULL
10770 && info.item_compare_partial == NULL
10771 ? item_compare : item_compare2);
10772
10773 if (!info.item_compare_func_err)
10774 {
10775 /* Clear the List and append the items in sorted order. */
10776 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
10777 l->lv_len = 0;
10778 for (i = 0; i < len; ++i)
10779 list_append(l, ptrs[i].item);
10780 }
10781 }
10782 }
10783 else
10784 {
10785 int (*item_compare_func_ptr)(const void *, const void *);
10786
10787 /* f_uniq(): ptrs will be a stack of items to remove */
10788 info.item_compare_func_err = FALSE;
10789 info.item_compare_keep_zero = TRUE;
10790 item_compare_func_ptr = info.item_compare_func != NULL
10791 || info.item_compare_partial != NULL
10792 ? item_compare2 : item_compare;
10793
10794 for (li = l->lv_first; li != NULL && li->li_next != NULL;
10795 li = li->li_next)
10796 {
10797 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
10798 == 0)
10799 ptrs[i++].item = li;
10800 if (info.item_compare_func_err)
10801 {
10802 EMSG(_("E882: Uniq compare function failed"));
10803 break;
10804 }
10805 }
10806
10807 if (!info.item_compare_func_err)
10808 {
10809 while (--i >= 0)
10810 {
10811 li = ptrs[i].item->li_next;
10812 ptrs[i].item->li_next = li->li_next;
10813 if (li->li_next != NULL)
10814 li->li_next->li_prev = ptrs[i].item;
10815 else
10816 l->lv_last = ptrs[i].item;
10817 list_fix_watch(l, li);
10818 listitem_free(li);
10819 l->lv_len--;
10820 }
10821 }
10822 }
10823
10824 vim_free(ptrs);
10825 }
10826theend:
10827 sortinfo = old_sortinfo;
10828}
10829
10830/*
10831 * "sort({list})" function
10832 */
10833 static void
10834f_sort(typval_T *argvars, typval_T *rettv)
10835{
10836 do_sort_uniq(argvars, rettv, TRUE);
10837}
10838
10839/*
10840 * "uniq({list})" function
10841 */
10842 static void
10843f_uniq(typval_T *argvars, typval_T *rettv)
10844{
10845 do_sort_uniq(argvars, rettv, FALSE);
10846}
10847
10848/*
10849 * "soundfold({word})" function
10850 */
10851 static void
10852f_soundfold(typval_T *argvars, typval_T *rettv)
10853{
10854 char_u *s;
10855
10856 rettv->v_type = VAR_STRING;
10857 s = get_tv_string(&argvars[0]);
10858#ifdef FEAT_SPELL
10859 rettv->vval.v_string = eval_soundfold(s);
10860#else
10861 rettv->vval.v_string = vim_strsave(s);
10862#endif
10863}
10864
10865/*
10866 * "spellbadword()" function
10867 */
10868 static void
10869f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
10870{
10871 char_u *word = (char_u *)"";
10872 hlf_T attr = HLF_COUNT;
10873 int len = 0;
10874
10875 if (rettv_list_alloc(rettv) == FAIL)
10876 return;
10877
10878#ifdef FEAT_SPELL
10879 if (argvars[0].v_type == VAR_UNKNOWN)
10880 {
10881 /* Find the start and length of the badly spelled word. */
10882 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
10883 if (len != 0)
10884 word = ml_get_cursor();
10885 }
10886 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
10887 {
10888 char_u *str = get_tv_string_chk(&argvars[0]);
10889 int capcol = -1;
10890
10891 if (str != NULL)
10892 {
10893 /* Check the argument for spelling. */
10894 while (*str != NUL)
10895 {
10896 len = spell_check(curwin, str, &attr, &capcol, FALSE);
10897 if (attr != HLF_COUNT)
10898 {
10899 word = str;
10900 break;
10901 }
10902 str += len;
10903 }
10904 }
10905 }
10906#endif
10907
10908 list_append_string(rettv->vval.v_list, word, len);
10909 list_append_string(rettv->vval.v_list, (char_u *)(
10910 attr == HLF_SPB ? "bad" :
10911 attr == HLF_SPR ? "rare" :
10912 attr == HLF_SPL ? "local" :
10913 attr == HLF_SPC ? "caps" :
10914 ""), -1);
10915}
10916
10917/*
10918 * "spellsuggest()" function
10919 */
10920 static void
10921f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
10922{
10923#ifdef FEAT_SPELL
10924 char_u *str;
10925 int typeerr = FALSE;
10926 int maxcount;
10927 garray_T ga;
10928 int i;
10929 listitem_T *li;
10930 int need_capital = FALSE;
10931#endif
10932
10933 if (rettv_list_alloc(rettv) == FAIL)
10934 return;
10935
10936#ifdef FEAT_SPELL
10937 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
10938 {
10939 str = get_tv_string(&argvars[0]);
10940 if (argvars[1].v_type != VAR_UNKNOWN)
10941 {
10942 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
10943 if (maxcount <= 0)
10944 return;
10945 if (argvars[2].v_type != VAR_UNKNOWN)
10946 {
10947 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
10948 if (typeerr)
10949 return;
10950 }
10951 }
10952 else
10953 maxcount = 25;
10954
10955 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
10956
10957 for (i = 0; i < ga.ga_len; ++i)
10958 {
10959 str = ((char_u **)ga.ga_data)[i];
10960
10961 li = listitem_alloc();
10962 if (li == NULL)
10963 vim_free(str);
10964 else
10965 {
10966 li->li_tv.v_type = VAR_STRING;
10967 li->li_tv.v_lock = 0;
10968 li->li_tv.vval.v_string = str;
10969 list_append(rettv->vval.v_list, li);
10970 }
10971 }
10972 ga_clear(&ga);
10973 }
10974#endif
10975}
10976
10977 static void
10978f_split(typval_T *argvars, typval_T *rettv)
10979{
10980 char_u *str;
10981 char_u *end;
10982 char_u *pat = NULL;
10983 regmatch_T regmatch;
10984 char_u patbuf[NUMBUFLEN];
10985 char_u *save_cpo;
10986 int match;
10987 colnr_T col = 0;
10988 int keepempty = FALSE;
10989 int typeerr = FALSE;
10990
10991 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10992 save_cpo = p_cpo;
10993 p_cpo = (char_u *)"";
10994
10995 str = get_tv_string(&argvars[0]);
10996 if (argvars[1].v_type != VAR_UNKNOWN)
10997 {
10998 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
10999 if (pat == NULL)
11000 typeerr = TRUE;
11001 if (argvars[2].v_type != VAR_UNKNOWN)
11002 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
11003 }
11004 if (pat == NULL || *pat == NUL)
11005 pat = (char_u *)"[\\x01- ]\\+";
11006
11007 if (rettv_list_alloc(rettv) == FAIL)
11008 return;
11009 if (typeerr)
11010 return;
11011
11012 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11013 if (regmatch.regprog != NULL)
11014 {
11015 regmatch.rm_ic = FALSE;
11016 while (*str != NUL || keepempty)
11017 {
11018 if (*str == NUL)
11019 match = FALSE; /* empty item at the end */
11020 else
11021 match = vim_regexec_nl(&regmatch, str, col);
11022 if (match)
11023 end = regmatch.startp[0];
11024 else
11025 end = str + STRLEN(str);
11026 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
11027 && *str != NUL && match && end < regmatch.endp[0]))
11028 {
11029 if (list_append_string(rettv->vval.v_list, str,
11030 (int)(end - str)) == FAIL)
11031 break;
11032 }
11033 if (!match)
11034 break;
11035 /* Advance to just after the match. */
11036 if (regmatch.endp[0] > str)
11037 col = 0;
11038 else
11039 {
11040 /* Don't get stuck at the same match. */
11041#ifdef FEAT_MBYTE
11042 col = (*mb_ptr2len)(regmatch.endp[0]);
11043#else
11044 col = 1;
11045#endif
11046 }
11047 str = regmatch.endp[0];
11048 }
11049
11050 vim_regfree(regmatch.regprog);
11051 }
11052
11053 p_cpo = save_cpo;
11054}
11055
11056#ifdef FEAT_FLOAT
11057/*
11058 * "sqrt()" function
11059 */
11060 static void
11061f_sqrt(typval_T *argvars, typval_T *rettv)
11062{
11063 float_T f = 0.0;
11064
11065 rettv->v_type = VAR_FLOAT;
11066 if (get_float_arg(argvars, &f) == OK)
11067 rettv->vval.v_float = sqrt(f);
11068 else
11069 rettv->vval.v_float = 0.0;
11070}
11071
11072/*
11073 * "str2float()" function
11074 */
11075 static void
11076f_str2float(typval_T *argvars, typval_T *rettv)
11077{
11078 char_u *p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011079 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011080
Bram Moolenaar08243d22017-01-10 16:12:29 +010011081 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011082 p = skipwhite(p + 1);
11083 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011084 if (isneg)
11085 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011086 rettv->v_type = VAR_FLOAT;
11087}
11088#endif
11089
11090/*
11091 * "str2nr()" function
11092 */
11093 static void
11094f_str2nr(typval_T *argvars, typval_T *rettv)
11095{
11096 int base = 10;
11097 char_u *p;
11098 varnumber_T n;
11099 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010011100 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011101
11102 if (argvars[1].v_type != VAR_UNKNOWN)
11103 {
11104 base = (int)get_tv_number(&argvars[1]);
11105 if (base != 2 && base != 8 && base != 10 && base != 16)
11106 {
11107 EMSG(_(e_invarg));
11108 return;
11109 }
11110 }
11111
11112 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011113 isneg = (*p == '-');
11114 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011115 p = skipwhite(p + 1);
11116 switch (base)
11117 {
11118 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
11119 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
11120 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
11121 default: what = 0;
11122 }
11123 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011124 if (isneg)
11125 rettv->vval.v_number = -n;
11126 else
11127 rettv->vval.v_number = n;
11128
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011129}
11130
11131#ifdef HAVE_STRFTIME
11132/*
11133 * "strftime({format}[, {time}])" function
11134 */
11135 static void
11136f_strftime(typval_T *argvars, typval_T *rettv)
11137{
11138 char_u result_buf[256];
11139 struct tm *curtime;
11140 time_t seconds;
11141 char_u *p;
11142
11143 rettv->v_type = VAR_STRING;
11144
11145 p = get_tv_string(&argvars[0]);
11146 if (argvars[1].v_type == VAR_UNKNOWN)
11147 seconds = time(NULL);
11148 else
11149 seconds = (time_t)get_tv_number(&argvars[1]);
11150 curtime = localtime(&seconds);
11151 /* MSVC returns NULL for an invalid value of seconds. */
11152 if (curtime == NULL)
11153 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
11154 else
11155 {
11156# ifdef FEAT_MBYTE
11157 vimconv_T conv;
11158 char_u *enc;
11159
11160 conv.vc_type = CONV_NONE;
11161 enc = enc_locale();
11162 convert_setup(&conv, p_enc, enc);
11163 if (conv.vc_type != CONV_NONE)
11164 p = string_convert(&conv, p, NULL);
11165# endif
11166 if (p != NULL)
11167 (void)strftime((char *)result_buf, sizeof(result_buf),
11168 (char *)p, curtime);
11169 else
11170 result_buf[0] = NUL;
11171
11172# ifdef FEAT_MBYTE
11173 if (conv.vc_type != CONV_NONE)
11174 vim_free(p);
11175 convert_setup(&conv, enc, p_enc);
11176 if (conv.vc_type != CONV_NONE)
11177 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
11178 else
11179# endif
11180 rettv->vval.v_string = vim_strsave(result_buf);
11181
11182# ifdef FEAT_MBYTE
11183 /* Release conversion descriptors */
11184 convert_setup(&conv, NULL, NULL);
11185 vim_free(enc);
11186# endif
11187 }
11188}
11189#endif
11190
11191/*
11192 * "strgetchar()" function
11193 */
11194 static void
11195f_strgetchar(typval_T *argvars, typval_T *rettv)
11196{
11197 char_u *str;
11198 int len;
11199 int error = FALSE;
11200 int charidx;
11201
11202 rettv->vval.v_number = -1;
11203 str = get_tv_string_chk(&argvars[0]);
11204 if (str == NULL)
11205 return;
11206 len = (int)STRLEN(str);
11207 charidx = (int)get_tv_number_chk(&argvars[1], &error);
11208 if (error)
11209 return;
11210#ifdef FEAT_MBYTE
11211 {
11212 int byteidx = 0;
11213
11214 while (charidx >= 0 && byteidx < len)
11215 {
11216 if (charidx == 0)
11217 {
11218 rettv->vval.v_number = mb_ptr2char(str + byteidx);
11219 break;
11220 }
11221 --charidx;
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011222 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011223 }
11224 }
11225#else
11226 if (charidx < len)
11227 rettv->vval.v_number = str[charidx];
11228#endif
11229}
11230
11231/*
11232 * "stridx()" function
11233 */
11234 static void
11235f_stridx(typval_T *argvars, typval_T *rettv)
11236{
11237 char_u buf[NUMBUFLEN];
11238 char_u *needle;
11239 char_u *haystack;
11240 char_u *save_haystack;
11241 char_u *pos;
11242 int start_idx;
11243
11244 needle = get_tv_string_chk(&argvars[1]);
11245 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
11246 rettv->vval.v_number = -1;
11247 if (needle == NULL || haystack == NULL)
11248 return; /* type error; errmsg already given */
11249
11250 if (argvars[2].v_type != VAR_UNKNOWN)
11251 {
11252 int error = FALSE;
11253
11254 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
11255 if (error || start_idx >= (int)STRLEN(haystack))
11256 return;
11257 if (start_idx >= 0)
11258 haystack += start_idx;
11259 }
11260
11261 pos = (char_u *)strstr((char *)haystack, (char *)needle);
11262 if (pos != NULL)
11263 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
11264}
11265
11266/*
11267 * "string()" function
11268 */
11269 static void
11270f_string(typval_T *argvars, typval_T *rettv)
11271{
11272 char_u *tofree;
11273 char_u numbuf[NUMBUFLEN];
11274
11275 rettv->v_type = VAR_STRING;
11276 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
11277 get_copyID());
11278 /* Make a copy if we have a value but it's not in allocated memory. */
11279 if (rettv->vval.v_string != NULL && tofree == NULL)
11280 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
11281}
11282
11283/*
11284 * "strlen()" function
11285 */
11286 static void
11287f_strlen(typval_T *argvars, typval_T *rettv)
11288{
11289 rettv->vval.v_number = (varnumber_T)(STRLEN(
11290 get_tv_string(&argvars[0])));
11291}
11292
11293/*
11294 * "strchars()" function
11295 */
11296 static void
11297f_strchars(typval_T *argvars, typval_T *rettv)
11298{
11299 char_u *s = get_tv_string(&argvars[0]);
11300 int skipcc = 0;
11301#ifdef FEAT_MBYTE
11302 varnumber_T len = 0;
11303 int (*func_mb_ptr2char_adv)(char_u **pp);
11304#endif
11305
11306 if (argvars[1].v_type != VAR_UNKNOWN)
11307 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
11308 if (skipcc < 0 || skipcc > 1)
11309 EMSG(_(e_invarg));
11310 else
11311 {
11312#ifdef FEAT_MBYTE
11313 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
11314 while (*s != NUL)
11315 {
11316 func_mb_ptr2char_adv(&s);
11317 ++len;
11318 }
11319 rettv->vval.v_number = len;
11320#else
11321 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
11322#endif
11323 }
11324}
11325
11326/*
11327 * "strdisplaywidth()" function
11328 */
11329 static void
11330f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
11331{
11332 char_u *s = get_tv_string(&argvars[0]);
11333 int col = 0;
11334
11335 if (argvars[1].v_type != VAR_UNKNOWN)
11336 col = (int)get_tv_number(&argvars[1]);
11337
11338 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
11339}
11340
11341/*
11342 * "strwidth()" function
11343 */
11344 static void
11345f_strwidth(typval_T *argvars, typval_T *rettv)
11346{
11347 char_u *s = get_tv_string(&argvars[0]);
11348
11349 rettv->vval.v_number = (varnumber_T)(
11350#ifdef FEAT_MBYTE
11351 mb_string2cells(s, -1)
11352#else
11353 STRLEN(s)
11354#endif
11355 );
11356}
11357
11358/*
11359 * "strcharpart()" function
11360 */
11361 static void
11362f_strcharpart(typval_T *argvars, typval_T *rettv)
11363{
11364#ifdef FEAT_MBYTE
11365 char_u *p;
11366 int nchar;
11367 int nbyte = 0;
11368 int charlen;
11369 int len = 0;
11370 int slen;
11371 int error = FALSE;
11372
11373 p = get_tv_string(&argvars[0]);
11374 slen = (int)STRLEN(p);
11375
11376 nchar = (int)get_tv_number_chk(&argvars[1], &error);
11377 if (!error)
11378 {
11379 if (nchar > 0)
11380 while (nchar > 0 && nbyte < slen)
11381 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011382 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011383 --nchar;
11384 }
11385 else
11386 nbyte = nchar;
11387 if (argvars[2].v_type != VAR_UNKNOWN)
11388 {
11389 charlen = (int)get_tv_number(&argvars[2]);
11390 while (charlen > 0 && nbyte + len < slen)
11391 {
11392 int off = nbyte + len;
11393
11394 if (off < 0)
11395 len += 1;
11396 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011397 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011398 --charlen;
11399 }
11400 }
11401 else
11402 len = slen - nbyte; /* default: all bytes that are available. */
11403 }
11404
11405 /*
11406 * Only return the overlap between the specified part and the actual
11407 * string.
11408 */
11409 if (nbyte < 0)
11410 {
11411 len += nbyte;
11412 nbyte = 0;
11413 }
11414 else if (nbyte > slen)
11415 nbyte = slen;
11416 if (len < 0)
11417 len = 0;
11418 else if (nbyte + len > slen)
11419 len = slen - nbyte;
11420
11421 rettv->v_type = VAR_STRING;
11422 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
11423#else
11424 f_strpart(argvars, rettv);
11425#endif
11426}
11427
11428/*
11429 * "strpart()" function
11430 */
11431 static void
11432f_strpart(typval_T *argvars, typval_T *rettv)
11433{
11434 char_u *p;
11435 int n;
11436 int len;
11437 int slen;
11438 int error = FALSE;
11439
11440 p = get_tv_string(&argvars[0]);
11441 slen = (int)STRLEN(p);
11442
11443 n = (int)get_tv_number_chk(&argvars[1], &error);
11444 if (error)
11445 len = 0;
11446 else if (argvars[2].v_type != VAR_UNKNOWN)
11447 len = (int)get_tv_number(&argvars[2]);
11448 else
11449 len = slen - n; /* default len: all bytes that are available. */
11450
11451 /*
11452 * Only return the overlap between the specified part and the actual
11453 * string.
11454 */
11455 if (n < 0)
11456 {
11457 len += n;
11458 n = 0;
11459 }
11460 else if (n > slen)
11461 n = slen;
11462 if (len < 0)
11463 len = 0;
11464 else if (n + len > slen)
11465 len = slen - n;
11466
11467 rettv->v_type = VAR_STRING;
11468 rettv->vval.v_string = vim_strnsave(p + n, len);
11469}
11470
11471/*
11472 * "strridx()" function
11473 */
11474 static void
11475f_strridx(typval_T *argvars, typval_T *rettv)
11476{
11477 char_u buf[NUMBUFLEN];
11478 char_u *needle;
11479 char_u *haystack;
11480 char_u *rest;
11481 char_u *lastmatch = NULL;
11482 int haystack_len, end_idx;
11483
11484 needle = get_tv_string_chk(&argvars[1]);
11485 haystack = get_tv_string_buf_chk(&argvars[0], buf);
11486
11487 rettv->vval.v_number = -1;
11488 if (needle == NULL || haystack == NULL)
11489 return; /* type error; errmsg already given */
11490
11491 haystack_len = (int)STRLEN(haystack);
11492 if (argvars[2].v_type != VAR_UNKNOWN)
11493 {
11494 /* Third argument: upper limit for index */
11495 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
11496 if (end_idx < 0)
11497 return; /* can never find a match */
11498 }
11499 else
11500 end_idx = haystack_len;
11501
11502 if (*needle == NUL)
11503 {
11504 /* Empty string matches past the end. */
11505 lastmatch = haystack + end_idx;
11506 }
11507 else
11508 {
11509 for (rest = haystack; *rest != '\0'; ++rest)
11510 {
11511 rest = (char_u *)strstr((char *)rest, (char *)needle);
11512 if (rest == NULL || rest > haystack + end_idx)
11513 break;
11514 lastmatch = rest;
11515 }
11516 }
11517
11518 if (lastmatch == NULL)
11519 rettv->vval.v_number = -1;
11520 else
11521 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
11522}
11523
11524/*
11525 * "strtrans()" function
11526 */
11527 static void
11528f_strtrans(typval_T *argvars, typval_T *rettv)
11529{
11530 rettv->v_type = VAR_STRING;
11531 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
11532}
11533
11534/*
11535 * "submatch()" function
11536 */
11537 static void
11538f_submatch(typval_T *argvars, typval_T *rettv)
11539{
11540 int error = FALSE;
11541 int no;
11542 int retList = 0;
11543
11544 no = (int)get_tv_number_chk(&argvars[0], &error);
11545 if (error)
11546 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020011547 if (no < 0 || no >= NSUBEXP)
11548 {
11549 EMSGN(_("E935: invalid submatch number: %d"), no);
11550 return;
11551 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011552 if (argvars[1].v_type != VAR_UNKNOWN)
11553 retList = (int)get_tv_number_chk(&argvars[1], &error);
11554 if (error)
11555 return;
11556
11557 if (retList == 0)
11558 {
11559 rettv->v_type = VAR_STRING;
11560 rettv->vval.v_string = reg_submatch(no);
11561 }
11562 else
11563 {
11564 rettv->v_type = VAR_LIST;
11565 rettv->vval.v_list = reg_submatch_list(no);
11566 }
11567}
11568
11569/*
11570 * "substitute()" function
11571 */
11572 static void
11573f_substitute(typval_T *argvars, typval_T *rettv)
11574{
11575 char_u patbuf[NUMBUFLEN];
11576 char_u subbuf[NUMBUFLEN];
11577 char_u flagsbuf[NUMBUFLEN];
11578
11579 char_u *str = get_tv_string_chk(&argvars[0]);
11580 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011581 char_u *sub = NULL;
11582 typval_T *expr = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011583 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
11584
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011585 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
11586 expr = &argvars[2];
11587 else
11588 sub = get_tv_string_buf_chk(&argvars[2], subbuf);
11589
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011590 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011591 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
11592 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011593 rettv->vval.v_string = NULL;
11594 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020011595 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011596}
11597
11598/*
11599 * "synID(lnum, col, trans)" function
11600 */
11601 static void
11602f_synID(typval_T *argvars UNUSED, typval_T *rettv)
11603{
11604 int id = 0;
11605#ifdef FEAT_SYN_HL
11606 linenr_T lnum;
11607 colnr_T col;
11608 int trans;
11609 int transerr = FALSE;
11610
11611 lnum = get_tv_lnum(argvars); /* -1 on type error */
11612 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
11613 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
11614
11615 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11616 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
11617 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
11618#endif
11619
11620 rettv->vval.v_number = id;
11621}
11622
11623/*
11624 * "synIDattr(id, what [, mode])" function
11625 */
11626 static void
11627f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
11628{
11629 char_u *p = NULL;
11630#ifdef FEAT_SYN_HL
11631 int id;
11632 char_u *what;
11633 char_u *mode;
11634 char_u modebuf[NUMBUFLEN];
11635 int modec;
11636
11637 id = (int)get_tv_number(&argvars[0]);
11638 what = get_tv_string(&argvars[1]);
11639 if (argvars[2].v_type != VAR_UNKNOWN)
11640 {
11641 mode = get_tv_string_buf(&argvars[2], modebuf);
11642 modec = TOLOWER_ASC(mode[0]);
11643 if (modec != 't' && modec != 'c' && modec != 'g')
11644 modec = 0; /* replace invalid with current */
11645 }
11646 else
11647 {
11648#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
11649 if (USE_24BIT)
11650 modec = 'g';
11651 else
11652#endif
11653 if (t_colors > 1)
11654 modec = 'c';
11655 else
11656 modec = 't';
11657 }
11658
11659
11660 switch (TOLOWER_ASC(what[0]))
11661 {
11662 case 'b':
11663 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
11664 p = highlight_color(id, what, modec);
11665 else /* bold */
11666 p = highlight_has_attr(id, HL_BOLD, modec);
11667 break;
11668
11669 case 'f': /* fg[#] or font */
11670 p = highlight_color(id, what, modec);
11671 break;
11672
11673 case 'i':
11674 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
11675 p = highlight_has_attr(id, HL_INVERSE, modec);
11676 else /* italic */
11677 p = highlight_has_attr(id, HL_ITALIC, modec);
11678 break;
11679
11680 case 'n': /* name */
11681 p = get_highlight_name(NULL, id - 1);
11682 break;
11683
11684 case 'r': /* reverse */
11685 p = highlight_has_attr(id, HL_INVERSE, modec);
11686 break;
11687
11688 case 's':
11689 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
11690 p = highlight_color(id, what, modec);
11691 else /* standout */
11692 p = highlight_has_attr(id, HL_STANDOUT, modec);
11693 break;
11694
11695 case 'u':
11696 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
11697 /* underline */
11698 p = highlight_has_attr(id, HL_UNDERLINE, modec);
11699 else
11700 /* undercurl */
11701 p = highlight_has_attr(id, HL_UNDERCURL, modec);
11702 break;
11703 }
11704
11705 if (p != NULL)
11706 p = vim_strsave(p);
11707#endif
11708 rettv->v_type = VAR_STRING;
11709 rettv->vval.v_string = p;
11710}
11711
11712/*
11713 * "synIDtrans(id)" function
11714 */
11715 static void
11716f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
11717{
11718 int id;
11719
11720#ifdef FEAT_SYN_HL
11721 id = (int)get_tv_number(&argvars[0]);
11722
11723 if (id > 0)
11724 id = syn_get_final_id(id);
11725 else
11726#endif
11727 id = 0;
11728
11729 rettv->vval.v_number = id;
11730}
11731
11732/*
11733 * "synconcealed(lnum, col)" function
11734 */
11735 static void
11736f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
11737{
11738#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
11739 linenr_T lnum;
11740 colnr_T col;
11741 int syntax_flags = 0;
11742 int cchar;
11743 int matchid = 0;
11744 char_u str[NUMBUFLEN];
11745#endif
11746
11747 rettv->v_type = VAR_LIST;
11748 rettv->vval.v_list = NULL;
11749
11750#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
11751 lnum = get_tv_lnum(argvars); /* -1 on type error */
11752 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
11753
11754 vim_memset(str, NUL, sizeof(str));
11755
11756 if (rettv_list_alloc(rettv) != FAIL)
11757 {
11758 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11759 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
11760 && curwin->w_p_cole > 0)
11761 {
11762 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
11763 syntax_flags = get_syntax_info(&matchid);
11764
11765 /* get the conceal character */
11766 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
11767 {
11768 cchar = syn_get_sub_char();
11769 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
11770 cchar = lcs_conceal;
11771 if (cchar != NUL)
11772 {
11773# ifdef FEAT_MBYTE
11774 if (has_mbyte)
11775 (*mb_char2bytes)(cchar, str);
11776 else
11777# endif
11778 str[0] = cchar;
11779 }
11780 }
11781 }
11782
11783 list_append_number(rettv->vval.v_list,
11784 (syntax_flags & HL_CONCEAL) != 0);
11785 /* -1 to auto-determine strlen */
11786 list_append_string(rettv->vval.v_list, str, -1);
11787 list_append_number(rettv->vval.v_list, matchid);
11788 }
11789#endif
11790}
11791
11792/*
11793 * "synstack(lnum, col)" function
11794 */
11795 static void
11796f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
11797{
11798#ifdef FEAT_SYN_HL
11799 linenr_T lnum;
11800 colnr_T col;
11801 int i;
11802 int id;
11803#endif
11804
11805 rettv->v_type = VAR_LIST;
11806 rettv->vval.v_list = NULL;
11807
11808#ifdef FEAT_SYN_HL
11809 lnum = get_tv_lnum(argvars); /* -1 on type error */
11810 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
11811
11812 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
11813 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
11814 && rettv_list_alloc(rettv) != FAIL)
11815 {
11816 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
11817 for (i = 0; ; ++i)
11818 {
11819 id = syn_get_stack_item(i);
11820 if (id < 0)
11821 break;
11822 if (list_append_number(rettv->vval.v_list, id) == FAIL)
11823 break;
11824 }
11825 }
11826#endif
11827}
11828
11829 static void
11830get_cmd_output_as_rettv(
11831 typval_T *argvars,
11832 typval_T *rettv,
11833 int retlist)
11834{
11835 char_u *res = NULL;
11836 char_u *p;
11837 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011838 int err = FALSE;
11839 FILE *fd;
11840 list_T *list = NULL;
11841 int flags = SHELL_SILENT;
11842
11843 rettv->v_type = VAR_STRING;
11844 rettv->vval.v_string = NULL;
11845 if (check_restricted() || check_secure())
11846 goto errret;
11847
11848 if (argvars[1].v_type != VAR_UNKNOWN)
11849 {
11850 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010011851 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011852 * command.
11853 */
11854 if ((infile = vim_tempname('i', TRUE)) == NULL)
11855 {
11856 EMSG(_(e_notmp));
11857 goto errret;
11858 }
11859
11860 fd = mch_fopen((char *)infile, WRITEBIN);
11861 if (fd == NULL)
11862 {
11863 EMSG2(_(e_notopen), infile);
11864 goto errret;
11865 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010011866 if (argvars[1].v_type == VAR_NUMBER)
11867 {
11868 linenr_T lnum;
11869 buf_T *buf;
11870
11871 buf = buflist_findnr(argvars[1].vval.v_number);
11872 if (buf == NULL)
11873 {
11874 EMSGN(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010011875 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010011876 goto errret;
11877 }
11878
11879 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
11880 {
11881 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
11882 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
11883 {
11884 err = TRUE;
11885 break;
11886 }
11887 if (putc(NL, fd) == EOF)
11888 {
11889 err = TRUE;
11890 break;
11891 }
11892 }
11893 }
11894 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011895 {
11896 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
11897 err = TRUE;
11898 }
11899 else
11900 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010011901 size_t len;
11902 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011903
11904 p = get_tv_string_buf_chk(&argvars[1], buf);
11905 if (p == NULL)
11906 {
11907 fclose(fd);
11908 goto errret; /* type error; errmsg already given */
11909 }
11910 len = STRLEN(p);
11911 if (len > 0 && fwrite(p, len, 1, fd) != 1)
11912 err = TRUE;
11913 }
11914 if (fclose(fd) != 0)
11915 err = TRUE;
11916 if (err)
11917 {
11918 EMSG(_("E677: Error writing temp file"));
11919 goto errret;
11920 }
11921 }
11922
11923 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
11924 * echoes typeahead, that messes up the display. */
11925 if (!msg_silent)
11926 flags += SHELL_COOKED;
11927
11928 if (retlist)
11929 {
11930 int len;
11931 listitem_T *li;
11932 char_u *s = NULL;
11933 char_u *start;
11934 char_u *end;
11935 int i;
11936
11937 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
11938 if (res == NULL)
11939 goto errret;
11940
11941 list = list_alloc();
11942 if (list == NULL)
11943 goto errret;
11944
11945 for (i = 0; i < len; ++i)
11946 {
11947 start = res + i;
11948 while (i < len && res[i] != NL)
11949 ++i;
11950 end = res + i;
11951
11952 s = alloc((unsigned)(end - start + 1));
11953 if (s == NULL)
11954 goto errret;
11955
11956 for (p = s; start < end; ++p, ++start)
11957 *p = *start == NUL ? NL : *start;
11958 *p = NUL;
11959
11960 li = listitem_alloc();
11961 if (li == NULL)
11962 {
11963 vim_free(s);
11964 goto errret;
11965 }
11966 li->li_tv.v_type = VAR_STRING;
11967 li->li_tv.v_lock = 0;
11968 li->li_tv.vval.v_string = s;
11969 list_append(list, li);
11970 }
11971
11972 ++list->lv_refcount;
11973 rettv->v_type = VAR_LIST;
11974 rettv->vval.v_list = list;
11975 list = NULL;
11976 }
11977 else
11978 {
11979 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
11980#ifdef USE_CR
11981 /* translate <CR> into <NL> */
11982 if (res != NULL)
11983 {
11984 char_u *s;
11985
11986 for (s = res; *s; ++s)
11987 {
11988 if (*s == CAR)
11989 *s = NL;
11990 }
11991 }
11992#else
11993# ifdef USE_CRNL
11994 /* translate <CR><NL> into <NL> */
11995 if (res != NULL)
11996 {
11997 char_u *s, *d;
11998
11999 d = res;
12000 for (s = res; *s; ++s)
12001 {
12002 if (s[0] == CAR && s[1] == NL)
12003 ++s;
12004 *d++ = *s;
12005 }
12006 *d = NUL;
12007 }
12008# endif
12009#endif
12010 rettv->vval.v_string = res;
12011 res = NULL;
12012 }
12013
12014errret:
12015 if (infile != NULL)
12016 {
12017 mch_remove(infile);
12018 vim_free(infile);
12019 }
12020 if (res != NULL)
12021 vim_free(res);
12022 if (list != NULL)
12023 list_free(list);
12024}
12025
12026/*
12027 * "system()" function
12028 */
12029 static void
12030f_system(typval_T *argvars, typval_T *rettv)
12031{
12032 get_cmd_output_as_rettv(argvars, rettv, FALSE);
12033}
12034
12035/*
12036 * "systemlist()" function
12037 */
12038 static void
12039f_systemlist(typval_T *argvars, typval_T *rettv)
12040{
12041 get_cmd_output_as_rettv(argvars, rettv, TRUE);
12042}
12043
12044/*
12045 * "tabpagebuflist()" function
12046 */
12047 static void
12048f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12049{
12050#ifdef FEAT_WINDOWS
12051 tabpage_T *tp;
12052 win_T *wp = NULL;
12053
12054 if (argvars[0].v_type == VAR_UNKNOWN)
12055 wp = firstwin;
12056 else
12057 {
12058 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12059 if (tp != NULL)
12060 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12061 }
12062 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
12063 {
12064 for (; wp != NULL; wp = wp->w_next)
12065 if (list_append_number(rettv->vval.v_list,
12066 wp->w_buffer->b_fnum) == FAIL)
12067 break;
12068 }
12069#endif
12070}
12071
12072
12073/*
12074 * "tabpagenr()" function
12075 */
12076 static void
12077f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
12078{
12079 int nr = 1;
12080#ifdef FEAT_WINDOWS
12081 char_u *arg;
12082
12083 if (argvars[0].v_type != VAR_UNKNOWN)
12084 {
12085 arg = get_tv_string_chk(&argvars[0]);
12086 nr = 0;
12087 if (arg != NULL)
12088 {
12089 if (STRCMP(arg, "$") == 0)
12090 nr = tabpage_index(NULL) - 1;
12091 else
12092 EMSG2(_(e_invexpr2), arg);
12093 }
12094 }
12095 else
12096 nr = tabpage_index(curtab);
12097#endif
12098 rettv->vval.v_number = nr;
12099}
12100
12101
12102#ifdef FEAT_WINDOWS
12103static int get_winnr(tabpage_T *tp, typval_T *argvar);
12104
12105/*
12106 * Common code for tabpagewinnr() and winnr().
12107 */
12108 static int
12109get_winnr(tabpage_T *tp, typval_T *argvar)
12110{
12111 win_T *twin;
12112 int nr = 1;
12113 win_T *wp;
12114 char_u *arg;
12115
12116 twin = (tp == curtab) ? curwin : tp->tp_curwin;
12117 if (argvar->v_type != VAR_UNKNOWN)
12118 {
12119 arg = get_tv_string_chk(argvar);
12120 if (arg == NULL)
12121 nr = 0; /* type error; errmsg already given */
12122 else if (STRCMP(arg, "$") == 0)
12123 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
12124 else if (STRCMP(arg, "#") == 0)
12125 {
12126 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
12127 if (twin == NULL)
12128 nr = 0;
12129 }
12130 else
12131 {
12132 EMSG2(_(e_invexpr2), arg);
12133 nr = 0;
12134 }
12135 }
12136
12137 if (nr > 0)
12138 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12139 wp != twin; wp = wp->w_next)
12140 {
12141 if (wp == NULL)
12142 {
12143 /* didn't find it in this tabpage */
12144 nr = 0;
12145 break;
12146 }
12147 ++nr;
12148 }
12149 return nr;
12150}
12151#endif
12152
12153/*
12154 * "tabpagewinnr()" function
12155 */
12156 static void
12157f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
12158{
12159 int nr = 1;
12160#ifdef FEAT_WINDOWS
12161 tabpage_T *tp;
12162
12163 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12164 if (tp == NULL)
12165 nr = 0;
12166 else
12167 nr = get_winnr(tp, &argvars[1]);
12168#endif
12169 rettv->vval.v_number = nr;
12170}
12171
12172
12173/*
12174 * "tagfiles()" function
12175 */
12176 static void
12177f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
12178{
12179 char_u *fname;
12180 tagname_T tn;
12181 int first;
12182
12183 if (rettv_list_alloc(rettv) == FAIL)
12184 return;
12185 fname = alloc(MAXPATHL);
12186 if (fname == NULL)
12187 return;
12188
12189 for (first = TRUE; ; first = FALSE)
12190 if (get_tagfname(&tn, first, fname) == FAIL
12191 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
12192 break;
12193 tagname_free(&tn);
12194 vim_free(fname);
12195}
12196
12197/*
12198 * "taglist()" function
12199 */
12200 static void
12201f_taglist(typval_T *argvars, typval_T *rettv)
12202{
12203 char_u *tag_pattern;
12204
12205 tag_pattern = get_tv_string(&argvars[0]);
12206
12207 rettv->vval.v_number = FALSE;
12208 if (*tag_pattern == NUL)
12209 return;
12210
12211 if (rettv_list_alloc(rettv) == OK)
12212 (void)get_tags(rettv->vval.v_list, tag_pattern);
12213}
12214
12215/*
12216 * "tempname()" function
12217 */
12218 static void
12219f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
12220{
12221 static int x = 'A';
12222
12223 rettv->v_type = VAR_STRING;
12224 rettv->vval.v_string = vim_tempname(x, FALSE);
12225
12226 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
12227 * names. Skip 'I' and 'O', they are used for shell redirection. */
12228 do
12229 {
12230 if (x == 'Z')
12231 x = '0';
12232 else if (x == '9')
12233 x = 'A';
12234 else
12235 {
12236#ifdef EBCDIC
12237 if (x == 'I')
12238 x = 'J';
12239 else if (x == 'R')
12240 x = 'S';
12241 else
12242#endif
12243 ++x;
12244 }
12245 } while (x == 'I' || x == 'O');
12246}
12247
12248#ifdef FEAT_FLOAT
12249/*
12250 * "tan()" function
12251 */
12252 static void
12253f_tan(typval_T *argvars, typval_T *rettv)
12254{
12255 float_T f = 0.0;
12256
12257 rettv->v_type = VAR_FLOAT;
12258 if (get_float_arg(argvars, &f) == OK)
12259 rettv->vval.v_float = tan(f);
12260 else
12261 rettv->vval.v_float = 0.0;
12262}
12263
12264/*
12265 * "tanh()" function
12266 */
12267 static void
12268f_tanh(typval_T *argvars, typval_T *rettv)
12269{
12270 float_T f = 0.0;
12271
12272 rettv->v_type = VAR_FLOAT;
12273 if (get_float_arg(argvars, &f) == OK)
12274 rettv->vval.v_float = tanh(f);
12275 else
12276 rettv->vval.v_float = 0.0;
12277}
12278#endif
12279
12280/*
12281 * "test_alloc_fail(id, countdown, repeat)" function
12282 */
12283 static void
12284f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
12285{
12286 if (argvars[0].v_type != VAR_NUMBER
12287 || argvars[0].vval.v_number <= 0
12288 || argvars[1].v_type != VAR_NUMBER
12289 || argvars[1].vval.v_number < 0
12290 || argvars[2].v_type != VAR_NUMBER)
12291 EMSG(_(e_invarg));
12292 else
12293 {
12294 alloc_fail_id = argvars[0].vval.v_number;
12295 if (alloc_fail_id >= aid_last)
12296 EMSG(_(e_invarg));
12297 alloc_fail_countdown = argvars[1].vval.v_number;
12298 alloc_fail_repeat = argvars[2].vval.v_number;
12299 did_outofmem_msg = FALSE;
12300 }
12301}
12302
12303/*
12304 * "test_autochdir()"
12305 */
12306 static void
12307f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12308{
12309#if defined(FEAT_AUTOCHDIR)
12310 test_autochdir = TRUE;
12311#endif
12312}
12313
12314/*
12315 * "test_disable_char_avail({expr})" function
12316 */
12317 static void
12318f_test_disable_char_avail(typval_T *argvars, typval_T *rettv UNUSED)
12319{
12320 disable_char_avail_for_testing = (int)get_tv_number(&argvars[0]);
12321}
12322
12323/*
12324 * "test_garbagecollect_now()" function
12325 */
12326 static void
12327f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12328{
12329 /* This is dangerous, any Lists and Dicts used internally may be freed
12330 * while still in use. */
12331 garbage_collect(TRUE);
12332}
12333
12334#ifdef FEAT_JOB_CHANNEL
12335 static void
12336f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
12337{
12338 rettv->v_type = VAR_CHANNEL;
12339 rettv->vval.v_channel = NULL;
12340}
12341#endif
12342
12343 static void
12344f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
12345{
12346 rettv->v_type = VAR_DICT;
12347 rettv->vval.v_dict = NULL;
12348}
12349
12350#ifdef FEAT_JOB_CHANNEL
12351 static void
12352f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
12353{
12354 rettv->v_type = VAR_JOB;
12355 rettv->vval.v_job = NULL;
12356}
12357#endif
12358
12359 static void
12360f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
12361{
12362 rettv->v_type = VAR_LIST;
12363 rettv->vval.v_list = NULL;
12364}
12365
12366 static void
12367f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
12368{
12369 rettv->v_type = VAR_PARTIAL;
12370 rettv->vval.v_partial = NULL;
12371}
12372
12373 static void
12374f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
12375{
12376 rettv->v_type = VAR_STRING;
12377 rettv->vval.v_string = NULL;
12378}
12379
12380 static void
12381f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
12382{
12383 time_for_testing = (time_t)get_tv_number(&argvars[0]);
12384}
12385
12386#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
12387/*
12388 * Get a callback from "arg". It can be a Funcref or a function name.
12389 * When "arg" is zero return an empty string.
12390 * Return NULL for an invalid argument.
12391 */
12392 char_u *
12393get_callback(typval_T *arg, partial_T **pp)
12394{
12395 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
12396 {
12397 *pp = arg->vval.v_partial;
12398 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012399 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012400 }
12401 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012402 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012403 {
12404 func_ref(arg->vval.v_string);
12405 return arg->vval.v_string;
12406 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012407 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
12408 return (char_u *)"";
12409 EMSG(_("E921: Invalid callback argument"));
12410 return NULL;
12411}
12412
12413/*
12414 * Unref/free "callback" and "partial" retured by get_callback().
12415 */
12416 void
12417free_callback(char_u *callback, partial_T *partial)
12418{
12419 if (partial != NULL)
12420 partial_unref(partial);
12421 else if (callback != NULL)
12422 {
12423 func_unref(callback);
12424 vim_free(callback);
12425 }
12426}
12427#endif
12428
12429#ifdef FEAT_TIMERS
12430/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020012431 * "timer_info([timer])" function
12432 */
12433 static void
12434f_timer_info(typval_T *argvars, typval_T *rettv)
12435{
12436 timer_T *timer = NULL;
12437
12438 if (rettv_list_alloc(rettv) != OK)
12439 return;
12440 if (argvars[0].v_type != VAR_UNKNOWN)
12441 {
12442 if (argvars[0].v_type != VAR_NUMBER)
12443 EMSG(_(e_number_exp));
12444 else
12445 {
12446 timer = find_timer((int)get_tv_number(&argvars[0]));
12447 if (timer != NULL)
12448 add_timer_info(rettv, timer);
12449 }
12450 }
12451 else
12452 add_timer_info_all(rettv);
12453}
12454
12455/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012456 * "timer_pause(timer, paused)" function
12457 */
12458 static void
12459f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
12460{
12461 timer_T *timer = NULL;
12462 int paused = (int)get_tv_number(&argvars[1]);
12463
12464 if (argvars[0].v_type != VAR_NUMBER)
12465 EMSG(_(e_number_exp));
12466 else
12467 {
12468 timer = find_timer((int)get_tv_number(&argvars[0]));
12469 if (timer != NULL)
12470 timer->tr_paused = paused;
12471 }
12472}
12473
12474/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012475 * "timer_start(time, callback [, options])" function
12476 */
12477 static void
12478f_timer_start(typval_T *argvars, typval_T *rettv)
12479{
Bram Moolenaar75537a92016-09-05 22:45:28 +020012480 long msec = (long)get_tv_number(&argvars[0]);
12481 timer_T *timer;
12482 int repeat = 0;
12483 char_u *callback;
12484 dict_T *dict;
12485 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012486
Bram Moolenaar75537a92016-09-05 22:45:28 +020012487 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012488 if (check_secure())
12489 return;
12490 if (argvars[2].v_type != VAR_UNKNOWN)
12491 {
12492 if (argvars[2].v_type != VAR_DICT
12493 || (dict = argvars[2].vval.v_dict) == NULL)
12494 {
12495 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
12496 return;
12497 }
12498 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
12499 repeat = get_dict_number(dict, (char_u *)"repeat");
12500 }
12501
Bram Moolenaar75537a92016-09-05 22:45:28 +020012502 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012503 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020012504 return;
12505
12506 timer = create_timer(msec, repeat);
12507 if (timer == NULL)
12508 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012509 else
12510 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020012511 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020012512 timer->tr_callback = vim_strsave(callback);
12513 else
12514 /* pointer into the partial */
12515 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020012516 timer->tr_partial = partial;
12517 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012518 }
12519}
12520
12521/*
12522 * "timer_stop(timer)" function
12523 */
12524 static void
12525f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
12526{
12527 timer_T *timer;
12528
12529 if (argvars[0].v_type != VAR_NUMBER)
12530 {
12531 EMSG(_(e_number_exp));
12532 return;
12533 }
12534 timer = find_timer((int)get_tv_number(&argvars[0]));
12535 if (timer != NULL)
12536 stop_timer(timer);
12537}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012538
12539/*
12540 * "timer_stopall()" function
12541 */
12542 static void
12543f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12544{
12545 stop_all_timers();
12546}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012547#endif
12548
12549/*
12550 * "tolower(string)" function
12551 */
12552 static void
12553f_tolower(typval_T *argvars, typval_T *rettv)
12554{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012555 rettv->v_type = VAR_STRING;
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +010012556 rettv->vval.v_string = strlow_save(get_tv_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012557}
12558
12559/*
12560 * "toupper(string)" function
12561 */
12562 static void
12563f_toupper(typval_T *argvars, typval_T *rettv)
12564{
12565 rettv->v_type = VAR_STRING;
12566 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
12567}
12568
12569/*
12570 * "tr(string, fromstr, tostr)" function
12571 */
12572 static void
12573f_tr(typval_T *argvars, typval_T *rettv)
12574{
12575 char_u *in_str;
12576 char_u *fromstr;
12577 char_u *tostr;
12578 char_u *p;
12579#ifdef FEAT_MBYTE
12580 int inlen;
12581 int fromlen;
12582 int tolen;
12583 int idx;
12584 char_u *cpstr;
12585 int cplen;
12586 int first = TRUE;
12587#endif
12588 char_u buf[NUMBUFLEN];
12589 char_u buf2[NUMBUFLEN];
12590 garray_T ga;
12591
12592 in_str = get_tv_string(&argvars[0]);
12593 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
12594 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
12595
12596 /* Default return value: empty string. */
12597 rettv->v_type = VAR_STRING;
12598 rettv->vval.v_string = NULL;
12599 if (fromstr == NULL || tostr == NULL)
12600 return; /* type error; errmsg already given */
12601 ga_init2(&ga, (int)sizeof(char), 80);
12602
12603#ifdef FEAT_MBYTE
12604 if (!has_mbyte)
12605#endif
12606 /* not multi-byte: fromstr and tostr must be the same length */
12607 if (STRLEN(fromstr) != STRLEN(tostr))
12608 {
12609#ifdef FEAT_MBYTE
12610error:
12611#endif
12612 EMSG2(_(e_invarg2), fromstr);
12613 ga_clear(&ga);
12614 return;
12615 }
12616
12617 /* fromstr and tostr have to contain the same number of chars */
12618 while (*in_str != NUL)
12619 {
12620#ifdef FEAT_MBYTE
12621 if (has_mbyte)
12622 {
12623 inlen = (*mb_ptr2len)(in_str);
12624 cpstr = in_str;
12625 cplen = inlen;
12626 idx = 0;
12627 for (p = fromstr; *p != NUL; p += fromlen)
12628 {
12629 fromlen = (*mb_ptr2len)(p);
12630 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
12631 {
12632 for (p = tostr; *p != NUL; p += tolen)
12633 {
12634 tolen = (*mb_ptr2len)(p);
12635 if (idx-- == 0)
12636 {
12637 cplen = tolen;
12638 cpstr = p;
12639 break;
12640 }
12641 }
12642 if (*p == NUL) /* tostr is shorter than fromstr */
12643 goto error;
12644 break;
12645 }
12646 ++idx;
12647 }
12648
12649 if (first && cpstr == in_str)
12650 {
12651 /* Check that fromstr and tostr have the same number of
12652 * (multi-byte) characters. Done only once when a character
12653 * of in_str doesn't appear in fromstr. */
12654 first = FALSE;
12655 for (p = tostr; *p != NUL; p += tolen)
12656 {
12657 tolen = (*mb_ptr2len)(p);
12658 --idx;
12659 }
12660 if (idx != 0)
12661 goto error;
12662 }
12663
12664 (void)ga_grow(&ga, cplen);
12665 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
12666 ga.ga_len += cplen;
12667
12668 in_str += inlen;
12669 }
12670 else
12671#endif
12672 {
12673 /* When not using multi-byte chars we can do it faster. */
12674 p = vim_strchr(fromstr, *in_str);
12675 if (p != NULL)
12676 ga_append(&ga, tostr[p - fromstr]);
12677 else
12678 ga_append(&ga, *in_str);
12679 ++in_str;
12680 }
12681 }
12682
12683 /* add a terminating NUL */
12684 (void)ga_grow(&ga, 1);
12685 ga_append(&ga, NUL);
12686
12687 rettv->vval.v_string = ga.ga_data;
12688}
12689
12690#ifdef FEAT_FLOAT
12691/*
12692 * "trunc({float})" function
12693 */
12694 static void
12695f_trunc(typval_T *argvars, typval_T *rettv)
12696{
12697 float_T f = 0.0;
12698
12699 rettv->v_type = VAR_FLOAT;
12700 if (get_float_arg(argvars, &f) == OK)
12701 /* trunc() is not in C90, use floor() or ceil() instead. */
12702 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
12703 else
12704 rettv->vval.v_float = 0.0;
12705}
12706#endif
12707
12708/*
12709 * "type(expr)" function
12710 */
12711 static void
12712f_type(typval_T *argvars, typval_T *rettv)
12713{
12714 int n = -1;
12715
12716 switch (argvars[0].v_type)
12717 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020012718 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
12719 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012720 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020012721 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
12722 case VAR_LIST: n = VAR_TYPE_LIST; break;
12723 case VAR_DICT: n = VAR_TYPE_DICT; break;
12724 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012725 case VAR_SPECIAL:
12726 if (argvars[0].vval.v_number == VVAL_FALSE
12727 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020012728 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012729 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020012730 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012731 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020012732 case VAR_JOB: n = VAR_TYPE_JOB; break;
12733 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012734 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010012735 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012736 n = -1;
12737 break;
12738 }
12739 rettv->vval.v_number = n;
12740}
12741
12742/*
12743 * "undofile(name)" function
12744 */
12745 static void
12746f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
12747{
12748 rettv->v_type = VAR_STRING;
12749#ifdef FEAT_PERSISTENT_UNDO
12750 {
12751 char_u *fname = get_tv_string(&argvars[0]);
12752
12753 if (*fname == NUL)
12754 {
12755 /* If there is no file name there will be no undo file. */
12756 rettv->vval.v_string = NULL;
12757 }
12758 else
12759 {
12760 char_u *ffname = FullName_save(fname, FALSE);
12761
12762 if (ffname != NULL)
12763 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
12764 vim_free(ffname);
12765 }
12766 }
12767#else
12768 rettv->vval.v_string = NULL;
12769#endif
12770}
12771
12772/*
12773 * "undotree()" function
12774 */
12775 static void
12776f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
12777{
12778 if (rettv_dict_alloc(rettv) == OK)
12779 {
12780 dict_T *dict = rettv->vval.v_dict;
12781 list_T *list;
12782
12783 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
12784 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
12785 dict_add_nr_str(dict, "save_last",
12786 (long)curbuf->b_u_save_nr_last, NULL);
12787 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
12788 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
12789 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
12790
12791 list = list_alloc();
12792 if (list != NULL)
12793 {
12794 u_eval_tree(curbuf->b_u_oldhead, list);
12795 dict_add_list(dict, "entries", list);
12796 }
12797 }
12798}
12799
12800/*
12801 * "values(dict)" function
12802 */
12803 static void
12804f_values(typval_T *argvars, typval_T *rettv)
12805{
12806 dict_list(argvars, rettv, 1);
12807}
12808
12809/*
12810 * "virtcol(string)" function
12811 */
12812 static void
12813f_virtcol(typval_T *argvars, typval_T *rettv)
12814{
12815 colnr_T vcol = 0;
12816 pos_T *fp;
12817 int fnum = curbuf->b_fnum;
12818
12819 fp = var2fpos(&argvars[0], FALSE, &fnum);
12820 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
12821 && fnum == curbuf->b_fnum)
12822 {
12823 getvvcol(curwin, fp, NULL, NULL, &vcol);
12824 ++vcol;
12825 }
12826
12827 rettv->vval.v_number = vcol;
12828}
12829
12830/*
12831 * "visualmode()" function
12832 */
12833 static void
12834f_visualmode(typval_T *argvars, typval_T *rettv)
12835{
12836 char_u str[2];
12837
12838 rettv->v_type = VAR_STRING;
12839 str[0] = curbuf->b_visual_mode_eval;
12840 str[1] = NUL;
12841 rettv->vval.v_string = vim_strsave(str);
12842
12843 /* A non-zero number or non-empty string argument: reset mode. */
12844 if (non_zero_arg(&argvars[0]))
12845 curbuf->b_visual_mode_eval = NUL;
12846}
12847
12848/*
12849 * "wildmenumode()" function
12850 */
12851 static void
12852f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12853{
12854#ifdef FEAT_WILDMENU
12855 if (wild_menu_showing)
12856 rettv->vval.v_number = 1;
12857#endif
12858}
12859
12860/*
12861 * "winbufnr(nr)" function
12862 */
12863 static void
12864f_winbufnr(typval_T *argvars, typval_T *rettv)
12865{
12866 win_T *wp;
12867
12868 wp = find_win_by_nr(&argvars[0], NULL);
12869 if (wp == NULL)
12870 rettv->vval.v_number = -1;
12871 else
12872 rettv->vval.v_number = wp->w_buffer->b_fnum;
12873}
12874
12875/*
12876 * "wincol()" function
12877 */
12878 static void
12879f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
12880{
12881 validate_cursor();
12882 rettv->vval.v_number = curwin->w_wcol + 1;
12883}
12884
12885/*
12886 * "winheight(nr)" function
12887 */
12888 static void
12889f_winheight(typval_T *argvars, typval_T *rettv)
12890{
12891 win_T *wp;
12892
12893 wp = find_win_by_nr(&argvars[0], NULL);
12894 if (wp == NULL)
12895 rettv->vval.v_number = -1;
12896 else
12897 rettv->vval.v_number = wp->w_height;
12898}
12899
12900/*
12901 * "winline()" function
12902 */
12903 static void
12904f_winline(typval_T *argvars UNUSED, typval_T *rettv)
12905{
12906 validate_cursor();
12907 rettv->vval.v_number = curwin->w_wrow + 1;
12908}
12909
12910/*
12911 * "winnr()" function
12912 */
12913 static void
12914f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
12915{
12916 int nr = 1;
12917
12918#ifdef FEAT_WINDOWS
12919 nr = get_winnr(curtab, &argvars[0]);
12920#endif
12921 rettv->vval.v_number = nr;
12922}
12923
12924/*
12925 * "winrestcmd()" function
12926 */
12927 static void
12928f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
12929{
12930#ifdef FEAT_WINDOWS
12931 win_T *wp;
12932 int winnr = 1;
12933 garray_T ga;
12934 char_u buf[50];
12935
12936 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020012937 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012938 {
12939 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
12940 ga_concat(&ga, buf);
12941 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
12942 ga_concat(&ga, buf);
12943 ++winnr;
12944 }
12945 ga_append(&ga, NUL);
12946
12947 rettv->vval.v_string = ga.ga_data;
12948#else
12949 rettv->vval.v_string = NULL;
12950#endif
12951 rettv->v_type = VAR_STRING;
12952}
12953
12954/*
12955 * "winrestview()" function
12956 */
12957 static void
12958f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
12959{
12960 dict_T *dict;
12961
12962 if (argvars[0].v_type != VAR_DICT
12963 || (dict = argvars[0].vval.v_dict) == NULL)
12964 EMSG(_(e_invarg));
12965 else
12966 {
12967 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
12968 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
12969 if (dict_find(dict, (char_u *)"col", -1) != NULL)
12970 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
12971#ifdef FEAT_VIRTUALEDIT
12972 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
12973 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
12974#endif
12975 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
12976 {
12977 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
12978 curwin->w_set_curswant = FALSE;
12979 }
12980
12981 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
12982 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
12983#ifdef FEAT_DIFF
12984 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
12985 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
12986#endif
12987 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
12988 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
12989 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
12990 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
12991
12992 check_cursor();
12993 win_new_height(curwin, curwin->w_height);
12994# ifdef FEAT_WINDOWS
12995 win_new_width(curwin, W_WIDTH(curwin));
12996# endif
12997 changed_window_setting();
12998
12999 if (curwin->w_topline <= 0)
13000 curwin->w_topline = 1;
13001 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
13002 curwin->w_topline = curbuf->b_ml.ml_line_count;
13003#ifdef FEAT_DIFF
13004 check_topfill(curwin, TRUE);
13005#endif
13006 }
13007}
13008
13009/*
13010 * "winsaveview()" function
13011 */
13012 static void
13013f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
13014{
13015 dict_T *dict;
13016
13017 if (rettv_dict_alloc(rettv) == FAIL)
13018 return;
13019 dict = rettv->vval.v_dict;
13020
13021 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
13022 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
13023#ifdef FEAT_VIRTUALEDIT
13024 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
13025#endif
13026 update_curswant();
13027 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
13028
13029 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
13030#ifdef FEAT_DIFF
13031 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
13032#endif
13033 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
13034 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
13035}
13036
13037/*
13038 * "winwidth(nr)" function
13039 */
13040 static void
13041f_winwidth(typval_T *argvars, typval_T *rettv)
13042{
13043 win_T *wp;
13044
13045 wp = find_win_by_nr(&argvars[0], NULL);
13046 if (wp == NULL)
13047 rettv->vval.v_number = -1;
13048 else
13049#ifdef FEAT_WINDOWS
13050 rettv->vval.v_number = wp->w_width;
13051#else
13052 rettv->vval.v_number = Columns;
13053#endif
13054}
13055
13056/*
13057 * "wordcount()" function
13058 */
13059 static void
13060f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
13061{
13062 if (rettv_dict_alloc(rettv) == FAIL)
13063 return;
13064 cursor_pos_info(rettv->vval.v_dict);
13065}
13066
13067/*
13068 * "writefile()" function
13069 */
13070 static void
13071f_writefile(typval_T *argvars, typval_T *rettv)
13072{
13073 int binary = FALSE;
13074 int append = FALSE;
13075 char_u *fname;
13076 FILE *fd;
13077 int ret = 0;
13078
13079 if (check_restricted() || check_secure())
13080 return;
13081
13082 if (argvars[0].v_type != VAR_LIST)
13083 {
13084 EMSG2(_(e_listarg), "writefile()");
13085 return;
13086 }
13087 if (argvars[0].vval.v_list == NULL)
13088 return;
13089
13090 if (argvars[2].v_type != VAR_UNKNOWN)
13091 {
13092 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
13093 binary = TRUE;
13094 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
13095 append = TRUE;
13096 }
13097
13098 /* Always open the file in binary mode, library functions have a mind of
13099 * their own about CR-LF conversion. */
13100 fname = get_tv_string(&argvars[1]);
13101 if (*fname == NUL || (fd = mch_fopen((char *)fname,
13102 append ? APPENDBIN : WRITEBIN)) == NULL)
13103 {
13104 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
13105 ret = -1;
13106 }
13107 else
13108 {
13109 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
13110 ret = -1;
13111 fclose(fd);
13112 }
13113
13114 rettv->vval.v_number = ret;
13115}
13116
13117/*
13118 * "xor(expr, expr)" function
13119 */
13120 static void
13121f_xor(typval_T *argvars, typval_T *rettv)
13122{
13123 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
13124 ^ get_tv_number_chk(&argvars[1], NULL);
13125}
13126
13127
13128#endif /* FEAT_EVAL */