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