blob: 74676c2062ec7059a77d451a92403394426b89d1 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalfunc.c: Builtin functions
12 */
13#define USING_FLOAT_STUFF
14
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef VMS
24# include <float.h>
25#endif
26
27#ifdef MACOS
28# include <time.h> /* for time_t */
29#endif
30
31static char *e_listarg = N_("E686: Argument of %s must be a List");
32#ifdef FEAT_QUICKFIX
33static char *e_stringreq = N_("E928: String required");
34#endif
35
36#ifdef FEAT_FLOAT
37static void f_abs(typval_T *argvars, typval_T *rettv);
38static void f_acos(typval_T *argvars, typval_T *rettv);
39#endif
40static void f_add(typval_T *argvars, typval_T *rettv);
41static void f_and(typval_T *argvars, typval_T *rettv);
42static void f_append(typval_T *argvars, typval_T *rettv);
43static void f_argc(typval_T *argvars, typval_T *rettv);
44static void f_argidx(typval_T *argvars, typval_T *rettv);
45static void f_arglistid(typval_T *argvars, typval_T *rettv);
46static void f_argv(typval_T *argvars, typval_T *rettv);
47static void f_assert_equal(typval_T *argvars, typval_T *rettv);
48static void f_assert_exception(typval_T *argvars, typval_T *rettv);
49static void f_assert_fails(typval_T *argvars, typval_T *rettv);
50static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaar61c04492016-07-23 15:35:35 +020051static void f_assert_inrange(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020052static void f_assert_match(typval_T *argvars, typval_T *rettv);
53static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
54static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
55static void f_assert_true(typval_T *argvars, typval_T *rettv);
56#ifdef FEAT_FLOAT
57static void f_asin(typval_T *argvars, typval_T *rettv);
58static void f_atan(typval_T *argvars, typval_T *rettv);
59static void f_atan2(typval_T *argvars, typval_T *rettv);
60#endif
61static void f_browse(typval_T *argvars, typval_T *rettv);
62static void f_browsedir(typval_T *argvars, typval_T *rettv);
63static void f_bufexists(typval_T *argvars, typval_T *rettv);
64static void f_buflisted(typval_T *argvars, typval_T *rettv);
65static void f_bufloaded(typval_T *argvars, typval_T *rettv);
66static void f_bufname(typval_T *argvars, typval_T *rettv);
67static void f_bufnr(typval_T *argvars, typval_T *rettv);
68static void f_bufwinid(typval_T *argvars, typval_T *rettv);
69static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
70static void f_byte2line(typval_T *argvars, typval_T *rettv);
71static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
72static void f_byteidx(typval_T *argvars, typval_T *rettv);
73static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
74static void f_call(typval_T *argvars, typval_T *rettv);
75#ifdef FEAT_FLOAT
76static void f_ceil(typval_T *argvars, typval_T *rettv);
77#endif
78#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +010079static void f_ch_canread(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020080static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0874a832016-09-01 15:11:51 +020081static void f_ch_close_in(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020082static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
83static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
84static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
85static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
86static void f_ch_info(typval_T *argvars, typval_T *rettv);
87static void f_ch_log(typval_T *argvars, typval_T *rettv);
88static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
89static void f_ch_open(typval_T *argvars, typval_T *rettv);
90static void f_ch_read(typval_T *argvars, typval_T *rettv);
91static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
92static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
93static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
94static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
95static void f_ch_status(typval_T *argvars, typval_T *rettv);
96#endif
97static void f_changenr(typval_T *argvars, typval_T *rettv);
98static void f_char2nr(typval_T *argvars, typval_T *rettv);
99static void f_cindent(typval_T *argvars, typval_T *rettv);
100static void f_clearmatches(typval_T *argvars, typval_T *rettv);
101static void f_col(typval_T *argvars, typval_T *rettv);
102#if defined(FEAT_INS_EXPAND)
103static void f_complete(typval_T *argvars, typval_T *rettv);
104static void f_complete_add(typval_T *argvars, typval_T *rettv);
105static void f_complete_check(typval_T *argvars, typval_T *rettv);
106#endif
107static void f_confirm(typval_T *argvars, typval_T *rettv);
108static void f_copy(typval_T *argvars, typval_T *rettv);
109#ifdef FEAT_FLOAT
110static void f_cos(typval_T *argvars, typval_T *rettv);
111static void f_cosh(typval_T *argvars, typval_T *rettv);
112#endif
113static void f_count(typval_T *argvars, typval_T *rettv);
114static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
115static void f_cursor(typval_T *argsvars, typval_T *rettv);
116static void f_deepcopy(typval_T *argvars, typval_T *rettv);
117static void f_delete(typval_T *argvars, typval_T *rettv);
118static void f_did_filetype(typval_T *argvars, typval_T *rettv);
119static void f_diff_filler(typval_T *argvars, typval_T *rettv);
120static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
121static void f_empty(typval_T *argvars, typval_T *rettv);
122static void f_escape(typval_T *argvars, typval_T *rettv);
123static void f_eval(typval_T *argvars, typval_T *rettv);
124static void f_eventhandler(typval_T *argvars, typval_T *rettv);
125static void f_executable(typval_T *argvars, typval_T *rettv);
126static void f_execute(typval_T *argvars, typval_T *rettv);
127static void f_exepath(typval_T *argvars, typval_T *rettv);
128static void f_exists(typval_T *argvars, typval_T *rettv);
129#ifdef FEAT_FLOAT
130static void f_exp(typval_T *argvars, typval_T *rettv);
131#endif
132static void f_expand(typval_T *argvars, typval_T *rettv);
133static void f_extend(typval_T *argvars, typval_T *rettv);
134static void f_feedkeys(typval_T *argvars, typval_T *rettv);
135static void f_filereadable(typval_T *argvars, typval_T *rettv);
136static void f_filewritable(typval_T *argvars, typval_T *rettv);
137static void f_filter(typval_T *argvars, typval_T *rettv);
138static void f_finddir(typval_T *argvars, typval_T *rettv);
139static void f_findfile(typval_T *argvars, typval_T *rettv);
140#ifdef FEAT_FLOAT
141static void f_float2nr(typval_T *argvars, typval_T *rettv);
142static void f_floor(typval_T *argvars, typval_T *rettv);
143static void f_fmod(typval_T *argvars, typval_T *rettv);
144#endif
145static void f_fnameescape(typval_T *argvars, typval_T *rettv);
146static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
147static void f_foldclosed(typval_T *argvars, typval_T *rettv);
148static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
149static void f_foldlevel(typval_T *argvars, typval_T *rettv);
150static void f_foldtext(typval_T *argvars, typval_T *rettv);
151static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
152static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200153static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200154static void f_function(typval_T *argvars, typval_T *rettv);
155static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
156static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200157static void f_getbufinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200158static void f_getbufline(typval_T *argvars, typval_T *rettv);
159static void f_getbufvar(typval_T *argvars, typval_T *rettv);
160static void f_getchar(typval_T *argvars, typval_T *rettv);
161static void f_getcharmod(typval_T *argvars, typval_T *rettv);
162static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
163static void f_getcmdline(typval_T *argvars, typval_T *rettv);
164#if defined(FEAT_CMDL_COMPL)
165static void f_getcompletion(typval_T *argvars, typval_T *rettv);
166#endif
167static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
168static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
169static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
170static void f_getcwd(typval_T *argvars, typval_T *rettv);
171static void f_getfontname(typval_T *argvars, typval_T *rettv);
172static void f_getfperm(typval_T *argvars, typval_T *rettv);
173static void f_getfsize(typval_T *argvars, typval_T *rettv);
174static void f_getftime(typval_T *argvars, typval_T *rettv);
175static void f_getftype(typval_T *argvars, typval_T *rettv);
176static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200177static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200178static void f_getmatches(typval_T *argvars, typval_T *rettv);
179static void f_getpid(typval_T *argvars, typval_T *rettv);
180static void f_getcurpos(typval_T *argvars, typval_T *rettv);
181static void f_getpos(typval_T *argvars, typval_T *rettv);
182static void f_getqflist(typval_T *argvars, typval_T *rettv);
183static void f_getreg(typval_T *argvars, typval_T *rettv);
184static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200185static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200186static void f_gettabvar(typval_T *argvars, typval_T *rettv);
187static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200188static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200189static void f_getwinposx(typval_T *argvars, typval_T *rettv);
190static void f_getwinposy(typval_T *argvars, typval_T *rettv);
191static void f_getwinvar(typval_T *argvars, typval_T *rettv);
192static void f_glob(typval_T *argvars, typval_T *rettv);
193static void f_globpath(typval_T *argvars, typval_T *rettv);
194static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
195static void f_has(typval_T *argvars, typval_T *rettv);
196static void f_has_key(typval_T *argvars, typval_T *rettv);
197static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
198static void f_hasmapto(typval_T *argvars, typval_T *rettv);
199static void f_histadd(typval_T *argvars, typval_T *rettv);
200static void f_histdel(typval_T *argvars, typval_T *rettv);
201static void f_histget(typval_T *argvars, typval_T *rettv);
202static void f_histnr(typval_T *argvars, typval_T *rettv);
203static void f_hlID(typval_T *argvars, typval_T *rettv);
204static void f_hlexists(typval_T *argvars, typval_T *rettv);
205static void f_hostname(typval_T *argvars, typval_T *rettv);
206static void f_iconv(typval_T *argvars, typval_T *rettv);
207static void f_indent(typval_T *argvars, typval_T *rettv);
208static void f_index(typval_T *argvars, typval_T *rettv);
209static void f_input(typval_T *argvars, typval_T *rettv);
210static void f_inputdialog(typval_T *argvars, typval_T *rettv);
211static void f_inputlist(typval_T *argvars, typval_T *rettv);
212static void f_inputrestore(typval_T *argvars, typval_T *rettv);
213static void f_inputsave(typval_T *argvars, typval_T *rettv);
214static void f_inputsecret(typval_T *argvars, typval_T *rettv);
215static void f_insert(typval_T *argvars, typval_T *rettv);
216static void f_invert(typval_T *argvars, typval_T *rettv);
217static void f_isdirectory(typval_T *argvars, typval_T *rettv);
218static void f_islocked(typval_T *argvars, typval_T *rettv);
219#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
220static void f_isnan(typval_T *argvars, typval_T *rettv);
221#endif
222static void f_items(typval_T *argvars, typval_T *rettv);
223#ifdef FEAT_JOB_CHANNEL
224static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
225static void f_job_info(typval_T *argvars, typval_T *rettv);
226static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
227static void f_job_start(typval_T *argvars, typval_T *rettv);
228static void f_job_stop(typval_T *argvars, typval_T *rettv);
229static void f_job_status(typval_T *argvars, typval_T *rettv);
230#endif
231static void f_join(typval_T *argvars, typval_T *rettv);
232static void f_js_decode(typval_T *argvars, typval_T *rettv);
233static void f_js_encode(typval_T *argvars, typval_T *rettv);
234static void f_json_decode(typval_T *argvars, typval_T *rettv);
235static void f_json_encode(typval_T *argvars, typval_T *rettv);
236static void f_keys(typval_T *argvars, typval_T *rettv);
237static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
238static void f_len(typval_T *argvars, typval_T *rettv);
239static void f_libcall(typval_T *argvars, typval_T *rettv);
240static void f_libcallnr(typval_T *argvars, typval_T *rettv);
241static void f_line(typval_T *argvars, typval_T *rettv);
242static void f_line2byte(typval_T *argvars, typval_T *rettv);
243static void f_lispindent(typval_T *argvars, typval_T *rettv);
244static void f_localtime(typval_T *argvars, typval_T *rettv);
245#ifdef FEAT_FLOAT
246static void f_log(typval_T *argvars, typval_T *rettv);
247static void f_log10(typval_T *argvars, typval_T *rettv);
248#endif
249#ifdef FEAT_LUA
250static void f_luaeval(typval_T *argvars, typval_T *rettv);
251#endif
252static void f_map(typval_T *argvars, typval_T *rettv);
253static void f_maparg(typval_T *argvars, typval_T *rettv);
254static void f_mapcheck(typval_T *argvars, typval_T *rettv);
255static void f_match(typval_T *argvars, typval_T *rettv);
256static void f_matchadd(typval_T *argvars, typval_T *rettv);
257static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
258static void f_matcharg(typval_T *argvars, typval_T *rettv);
259static void f_matchdelete(typval_T *argvars, typval_T *rettv);
260static void f_matchend(typval_T *argvars, typval_T *rettv);
261static void f_matchlist(typval_T *argvars, typval_T *rettv);
262static void f_matchstr(typval_T *argvars, typval_T *rettv);
263static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
264static void f_max(typval_T *argvars, typval_T *rettv);
265static void f_min(typval_T *argvars, typval_T *rettv);
266#ifdef vim_mkdir
267static void f_mkdir(typval_T *argvars, typval_T *rettv);
268#endif
269static void f_mode(typval_T *argvars, typval_T *rettv);
270#ifdef FEAT_MZSCHEME
271static void f_mzeval(typval_T *argvars, typval_T *rettv);
272#endif
273static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
274static void f_nr2char(typval_T *argvars, typval_T *rettv);
275static void f_or(typval_T *argvars, typval_T *rettv);
276static void f_pathshorten(typval_T *argvars, typval_T *rettv);
277#ifdef FEAT_PERL
278static void f_perleval(typval_T *argvars, typval_T *rettv);
279#endif
280#ifdef FEAT_FLOAT
281static void f_pow(typval_T *argvars, typval_T *rettv);
282#endif
283static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
284static void f_printf(typval_T *argvars, typval_T *rettv);
285static void f_pumvisible(typval_T *argvars, typval_T *rettv);
286#ifdef FEAT_PYTHON3
287static void f_py3eval(typval_T *argvars, typval_T *rettv);
288#endif
289#ifdef FEAT_PYTHON
290static void f_pyeval(typval_T *argvars, typval_T *rettv);
291#endif
292static void f_range(typval_T *argvars, typval_T *rettv);
293static void f_readfile(typval_T *argvars, typval_T *rettv);
294static void f_reltime(typval_T *argvars, typval_T *rettv);
295#ifdef FEAT_FLOAT
296static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
297#endif
298static void f_reltimestr(typval_T *argvars, typval_T *rettv);
299static void f_remote_expr(typval_T *argvars, typval_T *rettv);
300static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
301static void f_remote_peek(typval_T *argvars, typval_T *rettv);
302static void f_remote_read(typval_T *argvars, typval_T *rettv);
303static void f_remote_send(typval_T *argvars, typval_T *rettv);
304static void f_remove(typval_T *argvars, typval_T *rettv);
305static void f_rename(typval_T *argvars, typval_T *rettv);
306static void f_repeat(typval_T *argvars, typval_T *rettv);
307static void f_resolve(typval_T *argvars, typval_T *rettv);
308static void f_reverse(typval_T *argvars, typval_T *rettv);
309#ifdef FEAT_FLOAT
310static void f_round(typval_T *argvars, typval_T *rettv);
311#endif
312static void f_screenattr(typval_T *argvars, typval_T *rettv);
313static void f_screenchar(typval_T *argvars, typval_T *rettv);
314static void f_screencol(typval_T *argvars, typval_T *rettv);
315static void f_screenrow(typval_T *argvars, typval_T *rettv);
316static void f_search(typval_T *argvars, typval_T *rettv);
317static void f_searchdecl(typval_T *argvars, typval_T *rettv);
318static void f_searchpair(typval_T *argvars, typval_T *rettv);
319static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
320static void f_searchpos(typval_T *argvars, typval_T *rettv);
321static void f_server2client(typval_T *argvars, typval_T *rettv);
322static void f_serverlist(typval_T *argvars, typval_T *rettv);
323static void f_setbufvar(typval_T *argvars, typval_T *rettv);
324static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
325static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
326static void f_setfperm(typval_T *argvars, typval_T *rettv);
327static void f_setline(typval_T *argvars, typval_T *rettv);
328static void f_setloclist(typval_T *argvars, typval_T *rettv);
329static void f_setmatches(typval_T *argvars, typval_T *rettv);
330static void f_setpos(typval_T *argvars, typval_T *rettv);
331static void f_setqflist(typval_T *argvars, typval_T *rettv);
332static void f_setreg(typval_T *argvars, typval_T *rettv);
333static void f_settabvar(typval_T *argvars, typval_T *rettv);
334static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
335static void f_setwinvar(typval_T *argvars, typval_T *rettv);
336#ifdef FEAT_CRYPT
337static void f_sha256(typval_T *argvars, typval_T *rettv);
338#endif /* FEAT_CRYPT */
339static void f_shellescape(typval_T *argvars, typval_T *rettv);
340static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
341static void f_simplify(typval_T *argvars, typval_T *rettv);
342#ifdef FEAT_FLOAT
343static void f_sin(typval_T *argvars, typval_T *rettv);
344static void f_sinh(typval_T *argvars, typval_T *rettv);
345#endif
346static void f_sort(typval_T *argvars, typval_T *rettv);
347static void f_soundfold(typval_T *argvars, typval_T *rettv);
348static void f_spellbadword(typval_T *argvars, typval_T *rettv);
349static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
350static void f_split(typval_T *argvars, typval_T *rettv);
351#ifdef FEAT_FLOAT
352static void f_sqrt(typval_T *argvars, typval_T *rettv);
353static void f_str2float(typval_T *argvars, typval_T *rettv);
354#endif
355static void f_str2nr(typval_T *argvars, typval_T *rettv);
356static void f_strchars(typval_T *argvars, typval_T *rettv);
357#ifdef HAVE_STRFTIME
358static void f_strftime(typval_T *argvars, typval_T *rettv);
359#endif
360static void f_strgetchar(typval_T *argvars, typval_T *rettv);
361static void f_stridx(typval_T *argvars, typval_T *rettv);
362static void f_string(typval_T *argvars, typval_T *rettv);
363static void f_strlen(typval_T *argvars, typval_T *rettv);
364static void f_strcharpart(typval_T *argvars, typval_T *rettv);
365static void f_strpart(typval_T *argvars, typval_T *rettv);
366static void f_strridx(typval_T *argvars, typval_T *rettv);
367static void f_strtrans(typval_T *argvars, typval_T *rettv);
368static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
369static void f_strwidth(typval_T *argvars, typval_T *rettv);
370static void f_submatch(typval_T *argvars, typval_T *rettv);
371static void f_substitute(typval_T *argvars, typval_T *rettv);
372static void f_synID(typval_T *argvars, typval_T *rettv);
373static void f_synIDattr(typval_T *argvars, typval_T *rettv);
374static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
375static void f_synstack(typval_T *argvars, typval_T *rettv);
376static void f_synconcealed(typval_T *argvars, typval_T *rettv);
377static void f_system(typval_T *argvars, typval_T *rettv);
378static void f_systemlist(typval_T *argvars, typval_T *rettv);
379static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
380static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
381static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
382static void f_taglist(typval_T *argvars, typval_T *rettv);
383static void f_tagfiles(typval_T *argvars, typval_T *rettv);
384static void f_tempname(typval_T *argvars, typval_T *rettv);
385static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
386static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
387static void f_test_disable_char_avail(typval_T *argvars, typval_T *rettv);
388static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
389#ifdef FEAT_JOB_CHANNEL
390static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
391#endif
392static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
393#ifdef FEAT_JOB_CHANNEL
394static void f_test_null_job(typval_T *argvars, typval_T *rettv);
395#endif
396static void f_test_null_list(typval_T *argvars, typval_T *rettv);
397static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
398static void f_test_null_string(typval_T *argvars, typval_T *rettv);
399static void f_test_settime(typval_T *argvars, typval_T *rettv);
400#ifdef FEAT_FLOAT
401static void f_tan(typval_T *argvars, typval_T *rettv);
402static void f_tanh(typval_T *argvars, typval_T *rettv);
403#endif
404#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200405static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200406static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200407static void f_timer_start(typval_T *argvars, typval_T *rettv);
408static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200409static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200410#endif
411static void f_tolower(typval_T *argvars, typval_T *rettv);
412static void f_toupper(typval_T *argvars, typval_T *rettv);
413static void f_tr(typval_T *argvars, typval_T *rettv);
414#ifdef FEAT_FLOAT
415static void f_trunc(typval_T *argvars, typval_T *rettv);
416#endif
417static void f_type(typval_T *argvars, typval_T *rettv);
418static void f_undofile(typval_T *argvars, typval_T *rettv);
419static void f_undotree(typval_T *argvars, typval_T *rettv);
420static void f_uniq(typval_T *argvars, typval_T *rettv);
421static void f_values(typval_T *argvars, typval_T *rettv);
422static void f_virtcol(typval_T *argvars, typval_T *rettv);
423static void f_visualmode(typval_T *argvars, typval_T *rettv);
424static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
425static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
426static void f_win_getid(typval_T *argvars, typval_T *rettv);
427static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
428static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
429static void f_win_id2win(typval_T *argvars, typval_T *rettv);
430static void f_winbufnr(typval_T *argvars, typval_T *rettv);
431static void f_wincol(typval_T *argvars, typval_T *rettv);
432static void f_winheight(typval_T *argvars, typval_T *rettv);
433static void f_winline(typval_T *argvars, typval_T *rettv);
434static void f_winnr(typval_T *argvars, typval_T *rettv);
435static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
436static void f_winrestview(typval_T *argvars, typval_T *rettv);
437static void f_winsaveview(typval_T *argvars, typval_T *rettv);
438static void f_winwidth(typval_T *argvars, typval_T *rettv);
439static void f_writefile(typval_T *argvars, typval_T *rettv);
440static void f_wordcount(typval_T *argvars, typval_T *rettv);
441static void f_xor(typval_T *argvars, typval_T *rettv);
442
443/*
444 * Array with names and number of arguments of all internal functions
445 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
446 */
447static struct fst
448{
449 char *f_name; /* function name */
450 char f_min_argc; /* minimal number of arguments */
451 char f_max_argc; /* maximal number of arguments */
452 void (*f_func)(typval_T *args, typval_T *rvar);
453 /* implementation of function */
454} functions[] =
455{
456#ifdef FEAT_FLOAT
457 {"abs", 1, 1, f_abs},
458 {"acos", 1, 1, f_acos}, /* WJMc */
459#endif
460 {"add", 2, 2, f_add},
461 {"and", 2, 2, f_and},
462 {"append", 2, 2, f_append},
463 {"argc", 0, 0, f_argc},
464 {"argidx", 0, 0, f_argidx},
465 {"arglistid", 0, 2, f_arglistid},
466 {"argv", 0, 1, f_argv},
467#ifdef FEAT_FLOAT
468 {"asin", 1, 1, f_asin}, /* WJMc */
469#endif
470 {"assert_equal", 2, 3, f_assert_equal},
471 {"assert_exception", 1, 2, f_assert_exception},
472 {"assert_fails", 1, 2, f_assert_fails},
473 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100474 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200475 {"assert_match", 2, 3, f_assert_match},
476 {"assert_notequal", 2, 3, f_assert_notequal},
477 {"assert_notmatch", 2, 3, f_assert_notmatch},
478 {"assert_true", 1, 2, f_assert_true},
479#ifdef FEAT_FLOAT
480 {"atan", 1, 1, f_atan},
481 {"atan2", 2, 2, f_atan2},
482#endif
483 {"browse", 4, 4, f_browse},
484 {"browsedir", 2, 2, f_browsedir},
485 {"bufexists", 1, 1, f_bufexists},
486 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
487 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
488 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
489 {"buflisted", 1, 1, f_buflisted},
490 {"bufloaded", 1, 1, f_bufloaded},
491 {"bufname", 1, 1, f_bufname},
492 {"bufnr", 1, 2, f_bufnr},
493 {"bufwinid", 1, 1, f_bufwinid},
494 {"bufwinnr", 1, 1, f_bufwinnr},
495 {"byte2line", 1, 1, f_byte2line},
496 {"byteidx", 2, 2, f_byteidx},
497 {"byteidxcomp", 2, 2, f_byteidxcomp},
498 {"call", 2, 3, f_call},
499#ifdef FEAT_FLOAT
500 {"ceil", 1, 1, f_ceil},
501#endif
502#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100503 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200504 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200505 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200506 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
507 {"ch_evalraw", 2, 3, f_ch_evalraw},
508 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
509 {"ch_getjob", 1, 1, f_ch_getjob},
510 {"ch_info", 1, 1, f_ch_info},
511 {"ch_log", 1, 2, f_ch_log},
512 {"ch_logfile", 1, 2, f_ch_logfile},
513 {"ch_open", 1, 2, f_ch_open},
514 {"ch_read", 1, 2, f_ch_read},
515 {"ch_readraw", 1, 2, f_ch_readraw},
516 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
517 {"ch_sendraw", 2, 3, f_ch_sendraw},
518 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200519 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200520#endif
521 {"changenr", 0, 0, f_changenr},
522 {"char2nr", 1, 2, f_char2nr},
523 {"cindent", 1, 1, f_cindent},
524 {"clearmatches", 0, 0, f_clearmatches},
525 {"col", 1, 1, f_col},
526#if defined(FEAT_INS_EXPAND)
527 {"complete", 2, 2, f_complete},
528 {"complete_add", 1, 1, f_complete_add},
529 {"complete_check", 0, 0, f_complete_check},
530#endif
531 {"confirm", 1, 4, f_confirm},
532 {"copy", 1, 1, f_copy},
533#ifdef FEAT_FLOAT
534 {"cos", 1, 1, f_cos},
535 {"cosh", 1, 1, f_cosh},
536#endif
537 {"count", 2, 4, f_count},
538 {"cscope_connection",0,3, f_cscope_connection},
539 {"cursor", 1, 3, f_cursor},
540 {"deepcopy", 1, 2, f_deepcopy},
541 {"delete", 1, 2, f_delete},
542 {"did_filetype", 0, 0, f_did_filetype},
543 {"diff_filler", 1, 1, f_diff_filler},
544 {"diff_hlID", 2, 2, f_diff_hlID},
545 {"empty", 1, 1, f_empty},
546 {"escape", 2, 2, f_escape},
547 {"eval", 1, 1, f_eval},
548 {"eventhandler", 0, 0, f_eventhandler},
549 {"executable", 1, 1, f_executable},
550 {"execute", 1, 2, f_execute},
551 {"exepath", 1, 1, f_exepath},
552 {"exists", 1, 1, f_exists},
553#ifdef FEAT_FLOAT
554 {"exp", 1, 1, f_exp},
555#endif
556 {"expand", 1, 3, f_expand},
557 {"extend", 2, 3, f_extend},
558 {"feedkeys", 1, 2, f_feedkeys},
559 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
560 {"filereadable", 1, 1, f_filereadable},
561 {"filewritable", 1, 1, f_filewritable},
562 {"filter", 2, 2, f_filter},
563 {"finddir", 1, 3, f_finddir},
564 {"findfile", 1, 3, f_findfile},
565#ifdef FEAT_FLOAT
566 {"float2nr", 1, 1, f_float2nr},
567 {"floor", 1, 1, f_floor},
568 {"fmod", 2, 2, f_fmod},
569#endif
570 {"fnameescape", 1, 1, f_fnameescape},
571 {"fnamemodify", 2, 2, f_fnamemodify},
572 {"foldclosed", 1, 1, f_foldclosed},
573 {"foldclosedend", 1, 1, f_foldclosedend},
574 {"foldlevel", 1, 1, f_foldlevel},
575 {"foldtext", 0, 0, f_foldtext},
576 {"foldtextresult", 1, 1, f_foldtextresult},
577 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200578 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200579 {"function", 1, 3, f_function},
580 {"garbagecollect", 0, 1, f_garbagecollect},
581 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200582 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200583 {"getbufline", 2, 3, f_getbufline},
584 {"getbufvar", 2, 3, f_getbufvar},
585 {"getchar", 0, 1, f_getchar},
586 {"getcharmod", 0, 0, f_getcharmod},
587 {"getcharsearch", 0, 0, f_getcharsearch},
588 {"getcmdline", 0, 0, f_getcmdline},
589 {"getcmdpos", 0, 0, f_getcmdpos},
590 {"getcmdtype", 0, 0, f_getcmdtype},
591 {"getcmdwintype", 0, 0, f_getcmdwintype},
592#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200593 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200594#endif
595 {"getcurpos", 0, 0, f_getcurpos},
596 {"getcwd", 0, 2, f_getcwd},
597 {"getfontname", 0, 1, f_getfontname},
598 {"getfperm", 1, 1, f_getfperm},
599 {"getfsize", 1, 1, f_getfsize},
600 {"getftime", 1, 1, f_getftime},
601 {"getftype", 1, 1, f_getftype},
602 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200603 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200604 {"getmatches", 0, 0, f_getmatches},
605 {"getpid", 0, 0, f_getpid},
606 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200607 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200608 {"getreg", 0, 3, f_getreg},
609 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200610 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200611 {"gettabvar", 2, 3, f_gettabvar},
612 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200613 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200614 {"getwinposx", 0, 0, f_getwinposx},
615 {"getwinposy", 0, 0, f_getwinposy},
616 {"getwinvar", 2, 3, f_getwinvar},
617 {"glob", 1, 4, f_glob},
618 {"glob2regpat", 1, 1, f_glob2regpat},
619 {"globpath", 2, 5, f_globpath},
620 {"has", 1, 1, f_has},
621 {"has_key", 2, 2, f_has_key},
622 {"haslocaldir", 0, 2, f_haslocaldir},
623 {"hasmapto", 1, 3, f_hasmapto},
624 {"highlightID", 1, 1, f_hlID}, /* obsolete */
625 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
626 {"histadd", 2, 2, f_histadd},
627 {"histdel", 1, 2, f_histdel},
628 {"histget", 1, 2, f_histget},
629 {"histnr", 1, 1, f_histnr},
630 {"hlID", 1, 1, f_hlID},
631 {"hlexists", 1, 1, f_hlexists},
632 {"hostname", 0, 0, f_hostname},
633 {"iconv", 3, 3, f_iconv},
634 {"indent", 1, 1, f_indent},
635 {"index", 2, 4, f_index},
636 {"input", 1, 3, f_input},
637 {"inputdialog", 1, 3, f_inputdialog},
638 {"inputlist", 1, 1, f_inputlist},
639 {"inputrestore", 0, 0, f_inputrestore},
640 {"inputsave", 0, 0, f_inputsave},
641 {"inputsecret", 1, 2, f_inputsecret},
642 {"insert", 2, 3, f_insert},
643 {"invert", 1, 1, f_invert},
644 {"isdirectory", 1, 1, f_isdirectory},
645 {"islocked", 1, 1, f_islocked},
646#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
647 {"isnan", 1, 1, f_isnan},
648#endif
649 {"items", 1, 1, f_items},
650#ifdef FEAT_JOB_CHANNEL
651 {"job_getchannel", 1, 1, f_job_getchannel},
652 {"job_info", 1, 1, f_job_info},
653 {"job_setoptions", 2, 2, f_job_setoptions},
654 {"job_start", 1, 2, f_job_start},
655 {"job_status", 1, 1, f_job_status},
656 {"job_stop", 1, 2, f_job_stop},
657#endif
658 {"join", 1, 2, f_join},
659 {"js_decode", 1, 1, f_js_decode},
660 {"js_encode", 1, 1, f_js_encode},
661 {"json_decode", 1, 1, f_json_decode},
662 {"json_encode", 1, 1, f_json_encode},
663 {"keys", 1, 1, f_keys},
664 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
665 {"len", 1, 1, f_len},
666 {"libcall", 3, 3, f_libcall},
667 {"libcallnr", 3, 3, f_libcallnr},
668 {"line", 1, 1, f_line},
669 {"line2byte", 1, 1, f_line2byte},
670 {"lispindent", 1, 1, f_lispindent},
671 {"localtime", 0, 0, f_localtime},
672#ifdef FEAT_FLOAT
673 {"log", 1, 1, f_log},
674 {"log10", 1, 1, f_log10},
675#endif
676#ifdef FEAT_LUA
677 {"luaeval", 1, 2, f_luaeval},
678#endif
679 {"map", 2, 2, f_map},
680 {"maparg", 1, 4, f_maparg},
681 {"mapcheck", 1, 3, f_mapcheck},
682 {"match", 2, 4, f_match},
683 {"matchadd", 2, 5, f_matchadd},
684 {"matchaddpos", 2, 5, f_matchaddpos},
685 {"matcharg", 1, 1, f_matcharg},
686 {"matchdelete", 1, 1, f_matchdelete},
687 {"matchend", 2, 4, f_matchend},
688 {"matchlist", 2, 4, f_matchlist},
689 {"matchstr", 2, 4, f_matchstr},
690 {"matchstrpos", 2, 4, f_matchstrpos},
691 {"max", 1, 1, f_max},
692 {"min", 1, 1, f_min},
693#ifdef vim_mkdir
694 {"mkdir", 1, 3, f_mkdir},
695#endif
696 {"mode", 0, 1, f_mode},
697#ifdef FEAT_MZSCHEME
698 {"mzeval", 1, 1, f_mzeval},
699#endif
700 {"nextnonblank", 1, 1, f_nextnonblank},
701 {"nr2char", 1, 2, f_nr2char},
702 {"or", 2, 2, f_or},
703 {"pathshorten", 1, 1, f_pathshorten},
704#ifdef FEAT_PERL
705 {"perleval", 1, 1, f_perleval},
706#endif
707#ifdef FEAT_FLOAT
708 {"pow", 2, 2, f_pow},
709#endif
710 {"prevnonblank", 1, 1, f_prevnonblank},
711 {"printf", 2, 19, f_printf},
712 {"pumvisible", 0, 0, f_pumvisible},
713#ifdef FEAT_PYTHON3
714 {"py3eval", 1, 1, f_py3eval},
715#endif
716#ifdef FEAT_PYTHON
717 {"pyeval", 1, 1, f_pyeval},
718#endif
719 {"range", 1, 3, f_range},
720 {"readfile", 1, 3, f_readfile},
721 {"reltime", 0, 2, f_reltime},
722#ifdef FEAT_FLOAT
723 {"reltimefloat", 1, 1, f_reltimefloat},
724#endif
725 {"reltimestr", 1, 1, f_reltimestr},
726 {"remote_expr", 2, 3, f_remote_expr},
727 {"remote_foreground", 1, 1, f_remote_foreground},
728 {"remote_peek", 1, 2, f_remote_peek},
729 {"remote_read", 1, 1, f_remote_read},
730 {"remote_send", 2, 3, f_remote_send},
731 {"remove", 2, 3, f_remove},
732 {"rename", 2, 2, f_rename},
733 {"repeat", 2, 2, f_repeat},
734 {"resolve", 1, 1, f_resolve},
735 {"reverse", 1, 1, f_reverse},
736#ifdef FEAT_FLOAT
737 {"round", 1, 1, f_round},
738#endif
739 {"screenattr", 2, 2, f_screenattr},
740 {"screenchar", 2, 2, f_screenchar},
741 {"screencol", 0, 0, f_screencol},
742 {"screenrow", 0, 0, f_screenrow},
743 {"search", 1, 4, f_search},
744 {"searchdecl", 1, 3, f_searchdecl},
745 {"searchpair", 3, 7, f_searchpair},
746 {"searchpairpos", 3, 7, f_searchpairpos},
747 {"searchpos", 1, 4, f_searchpos},
748 {"server2client", 2, 2, f_server2client},
749 {"serverlist", 0, 0, f_serverlist},
750 {"setbufvar", 3, 3, f_setbufvar},
751 {"setcharsearch", 1, 1, f_setcharsearch},
752 {"setcmdpos", 1, 1, f_setcmdpos},
753 {"setfperm", 2, 2, f_setfperm},
754 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200755 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200756 {"setmatches", 1, 1, f_setmatches},
757 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200758 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200759 {"setreg", 2, 3, f_setreg},
760 {"settabvar", 3, 3, f_settabvar},
761 {"settabwinvar", 4, 4, f_settabwinvar},
762 {"setwinvar", 3, 3, f_setwinvar},
763#ifdef FEAT_CRYPT
764 {"sha256", 1, 1, f_sha256},
765#endif
766 {"shellescape", 1, 2, f_shellescape},
767 {"shiftwidth", 0, 0, f_shiftwidth},
768 {"simplify", 1, 1, f_simplify},
769#ifdef FEAT_FLOAT
770 {"sin", 1, 1, f_sin},
771 {"sinh", 1, 1, f_sinh},
772#endif
773 {"sort", 1, 3, f_sort},
774 {"soundfold", 1, 1, f_soundfold},
775 {"spellbadword", 0, 1, f_spellbadword},
776 {"spellsuggest", 1, 3, f_spellsuggest},
777 {"split", 1, 3, f_split},
778#ifdef FEAT_FLOAT
779 {"sqrt", 1, 1, f_sqrt},
780 {"str2float", 1, 1, f_str2float},
781#endif
782 {"str2nr", 1, 2, f_str2nr},
783 {"strcharpart", 2, 3, f_strcharpart},
784 {"strchars", 1, 2, f_strchars},
785 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
786#ifdef HAVE_STRFTIME
787 {"strftime", 1, 2, f_strftime},
788#endif
789 {"strgetchar", 2, 2, f_strgetchar},
790 {"stridx", 2, 3, f_stridx},
791 {"string", 1, 1, f_string},
792 {"strlen", 1, 1, f_strlen},
793 {"strpart", 2, 3, f_strpart},
794 {"strridx", 2, 3, f_strridx},
795 {"strtrans", 1, 1, f_strtrans},
796 {"strwidth", 1, 1, f_strwidth},
797 {"submatch", 1, 2, f_submatch},
798 {"substitute", 4, 4, f_substitute},
799 {"synID", 3, 3, f_synID},
800 {"synIDattr", 2, 3, f_synIDattr},
801 {"synIDtrans", 1, 1, f_synIDtrans},
802 {"synconcealed", 2, 2, f_synconcealed},
803 {"synstack", 2, 2, f_synstack},
804 {"system", 1, 2, f_system},
805 {"systemlist", 1, 2, f_systemlist},
806 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
807 {"tabpagenr", 0, 1, f_tabpagenr},
808 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
809 {"tagfiles", 0, 0, f_tagfiles},
810 {"taglist", 1, 1, f_taglist},
811#ifdef FEAT_FLOAT
812 {"tan", 1, 1, f_tan},
813 {"tanh", 1, 1, f_tanh},
814#endif
815 {"tempname", 0, 0, f_tempname},
816 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
817 {"test_autochdir", 0, 0, f_test_autochdir},
818 {"test_disable_char_avail", 1, 1, f_test_disable_char_avail},
819 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
820#ifdef FEAT_JOB_CHANNEL
821 {"test_null_channel", 0, 0, f_test_null_channel},
822#endif
823 {"test_null_dict", 0, 0, f_test_null_dict},
824#ifdef FEAT_JOB_CHANNEL
825 {"test_null_job", 0, 0, f_test_null_job},
826#endif
827 {"test_null_list", 0, 0, f_test_null_list},
828 {"test_null_partial", 0, 0, f_test_null_partial},
829 {"test_null_string", 0, 0, f_test_null_string},
830 {"test_settime", 1, 1, f_test_settime},
831#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200832 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200833 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200834 {"timer_start", 2, 3, f_timer_start},
835 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200836 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200837#endif
838 {"tolower", 1, 1, f_tolower},
839 {"toupper", 1, 1, f_toupper},
840 {"tr", 3, 3, f_tr},
841#ifdef FEAT_FLOAT
842 {"trunc", 1, 1, f_trunc},
843#endif
844 {"type", 1, 1, f_type},
845 {"undofile", 1, 1, f_undofile},
846 {"undotree", 0, 0, f_undotree},
847 {"uniq", 1, 3, f_uniq},
848 {"values", 1, 1, f_values},
849 {"virtcol", 1, 1, f_virtcol},
850 {"visualmode", 0, 1, f_visualmode},
851 {"wildmenumode", 0, 0, f_wildmenumode},
852 {"win_findbuf", 1, 1, f_win_findbuf},
853 {"win_getid", 0, 2, f_win_getid},
854 {"win_gotoid", 1, 1, f_win_gotoid},
855 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
856 {"win_id2win", 1, 1, f_win_id2win},
857 {"winbufnr", 1, 1, f_winbufnr},
858 {"wincol", 0, 0, f_wincol},
859 {"winheight", 1, 1, f_winheight},
860 {"winline", 0, 0, f_winline},
861 {"winnr", 0, 1, f_winnr},
862 {"winrestcmd", 0, 0, f_winrestcmd},
863 {"winrestview", 1, 1, f_winrestview},
864 {"winsaveview", 0, 0, f_winsaveview},
865 {"winwidth", 1, 1, f_winwidth},
866 {"wordcount", 0, 0, f_wordcount},
867 {"writefile", 2, 3, f_writefile},
868 {"xor", 2, 2, f_xor},
869};
870
871#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
872
873/*
874 * Function given to ExpandGeneric() to obtain the list of internal
875 * or user defined function names.
876 */
877 char_u *
878get_function_name(expand_T *xp, int idx)
879{
880 static int intidx = -1;
881 char_u *name;
882
883 if (idx == 0)
884 intidx = -1;
885 if (intidx < 0)
886 {
887 name = get_user_func_name(xp, idx);
888 if (name != NULL)
889 return name;
890 }
891 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
892 {
893 STRCPY(IObuff, functions[intidx].f_name);
894 STRCAT(IObuff, "(");
895 if (functions[intidx].f_max_argc == 0)
896 STRCAT(IObuff, ")");
897 return IObuff;
898 }
899
900 return NULL;
901}
902
903/*
904 * Function given to ExpandGeneric() to obtain the list of internal or
905 * user defined variable or function names.
906 */
907 char_u *
908get_expr_name(expand_T *xp, int idx)
909{
910 static int intidx = -1;
911 char_u *name;
912
913 if (idx == 0)
914 intidx = -1;
915 if (intidx < 0)
916 {
917 name = get_function_name(xp, idx);
918 if (name != NULL)
919 return name;
920 }
921 return get_user_var_name(xp, ++intidx);
922}
923
924#endif /* FEAT_CMDL_COMPL */
925
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 *)"");
Bram Moolenaarf845b872017-01-06 14:04:54 +01003976 dict_add_nr_str(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
3977 : buflist_findlnum(buf), NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003978 dict_add_nr_str(dict, "loaded", buf->b_ml.ml_mfp != NULL, NULL);
3979 dict_add_nr_str(dict, "listed", buf->b_p_bl, NULL);
3980 dict_add_nr_str(dict, "changed", bufIsChanged(buf), NULL);
3981 dict_add_nr_str(dict, "changedtick", buf->b_changedtick, NULL);
3982 dict_add_nr_str(dict, "hidden",
3983 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0,
3984 NULL);
3985
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02003986 /* Get a reference to buffer variables */
3987 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003988
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02003989 /* List of windows displaying this buffer */
3990 windows = list_alloc();
3991 if (windows != NULL)
3992 {
3993 FOR_ALL_TAB_WINDOWS(tp, wp)
3994 if (wp->w_buffer == buf)
3995 list_append_number(windows, (varnumber_T)wp->w_id);
3996 dict_add_list(dict, "windows", windows);
3997 }
3998
3999#ifdef FEAT_SIGNS
4000 if (buf->b_signlist != NULL)
4001 {
4002 /* List of signs placed in this buffer */
4003 list_T *signs = list_alloc();
4004 if (signs != NULL)
4005 {
4006 get_buffer_signs(buf, signs);
4007 dict_add_list(dict, "signs", signs);
4008 }
4009 }
4010#endif
4011
4012 return dict;
4013}
4014
4015/*
4016 * "getbufinfo()" function
4017 */
4018 static void
4019f_getbufinfo(typval_T *argvars, typval_T *rettv)
4020{
4021 buf_T *buf = NULL;
4022 buf_T *argbuf = NULL;
4023 dict_T *d;
4024 int filtered = FALSE;
4025 int sel_buflisted = FALSE;
4026 int sel_bufloaded = FALSE;
4027
4028 if (rettv_list_alloc(rettv) != OK)
4029 return;
4030
4031 /* List of all the buffers or selected buffers */
4032 if (argvars[0].v_type == VAR_DICT)
4033 {
4034 dict_T *sel_d = argvars[0].vval.v_dict;
4035
4036 if (sel_d != NULL)
4037 {
4038 dictitem_T *di;
4039
4040 filtered = TRUE;
4041
4042 di = dict_find(sel_d, (char_u *)"buflisted", -1);
4043 if (di != NULL && get_tv_number(&di->di_tv))
4044 sel_buflisted = TRUE;
4045
4046 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
4047 if (di != NULL && get_tv_number(&di->di_tv))
4048 sel_bufloaded = TRUE;
4049 }
4050 }
4051 else if (argvars[0].v_type != VAR_UNKNOWN)
4052 {
4053 /* Information about one buffer. Argument specifies the buffer */
4054 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4055 ++emsg_off;
4056 argbuf = get_buf_tv(&argvars[0], FALSE);
4057 --emsg_off;
4058 if (argbuf == NULL)
4059 return;
4060 }
4061
4062 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004063 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004064 {
4065 if (argbuf != NULL && argbuf != buf)
4066 continue;
4067 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
4068 || (sel_buflisted && !buf->b_p_bl)))
4069 continue;
4070
4071 d = get_buffer_info(buf);
4072 if (d != NULL)
4073 list_append_dict(rettv->vval.v_list, d);
4074 if (argbuf != NULL)
4075 return;
4076 }
4077}
4078
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004079static void get_buffer_lines(buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv);
4080
4081/*
4082 * Get line or list of lines from buffer "buf" into "rettv".
4083 * Return a range (from start to end) of lines in rettv from the specified
4084 * buffer.
4085 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4086 */
4087 static void
4088get_buffer_lines(
4089 buf_T *buf,
4090 linenr_T start,
4091 linenr_T end,
4092 int retlist,
4093 typval_T *rettv)
4094{
4095 char_u *p;
4096
4097 rettv->v_type = VAR_STRING;
4098 rettv->vval.v_string = NULL;
4099 if (retlist && rettv_list_alloc(rettv) == FAIL)
4100 return;
4101
4102 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4103 return;
4104
4105 if (!retlist)
4106 {
4107 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4108 p = ml_get_buf(buf, start, FALSE);
4109 else
4110 p = (char_u *)"";
4111 rettv->vval.v_string = vim_strsave(p);
4112 }
4113 else
4114 {
4115 if (end < start)
4116 return;
4117
4118 if (start < 1)
4119 start = 1;
4120 if (end > buf->b_ml.ml_line_count)
4121 end = buf->b_ml.ml_line_count;
4122 while (start <= end)
4123 if (list_append_string(rettv->vval.v_list,
4124 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4125 break;
4126 }
4127}
4128
4129/*
4130 * Get the lnum from the first argument.
4131 * Also accepts "$", then "buf" is used.
4132 * Returns 0 on error.
4133 */
4134 static linenr_T
4135get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
4136{
4137 if (argvars[0].v_type == VAR_STRING
4138 && argvars[0].vval.v_string != NULL
4139 && argvars[0].vval.v_string[0] == '$'
4140 && buf != NULL)
4141 return buf->b_ml.ml_line_count;
4142 return (linenr_T)get_tv_number_chk(&argvars[0], NULL);
4143}
4144
4145/*
4146 * "getbufline()" function
4147 */
4148 static void
4149f_getbufline(typval_T *argvars, typval_T *rettv)
4150{
4151 linenr_T lnum;
4152 linenr_T end;
4153 buf_T *buf;
4154
4155 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4156 ++emsg_off;
4157 buf = get_buf_tv(&argvars[0], FALSE);
4158 --emsg_off;
4159
4160 lnum = get_tv_lnum_buf(&argvars[1], buf);
4161 if (argvars[2].v_type == VAR_UNKNOWN)
4162 end = lnum;
4163 else
4164 end = get_tv_lnum_buf(&argvars[2], buf);
4165
4166 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4167}
4168
4169/*
4170 * "getbufvar()" function
4171 */
4172 static void
4173f_getbufvar(typval_T *argvars, typval_T *rettv)
4174{
4175 buf_T *buf;
4176 buf_T *save_curbuf;
4177 char_u *varname;
4178 dictitem_T *v;
4179 int done = FALSE;
4180
4181 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4182 varname = get_tv_string_chk(&argvars[1]);
4183 ++emsg_off;
4184 buf = get_buf_tv(&argvars[0], FALSE);
4185
4186 rettv->v_type = VAR_STRING;
4187 rettv->vval.v_string = NULL;
4188
4189 if (buf != NULL && varname != NULL)
4190 {
4191 /* set curbuf to be our buf, temporarily */
4192 save_curbuf = curbuf;
4193 curbuf = buf;
4194
Bram Moolenaar30567352016-08-27 21:25:44 +02004195 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004196 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004197 if (varname[1] == NUL)
4198 {
4199 /* get all buffer-local options in a dict */
4200 dict_T *opts = get_winbuf_options(TRUE);
4201
4202 if (opts != NULL)
4203 {
4204 rettv->v_type = VAR_DICT;
4205 rettv->vval.v_dict = opts;
4206 ++opts->dv_refcount;
4207 done = TRUE;
4208 }
4209 }
4210 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4211 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004212 done = TRUE;
4213 }
4214 else if (STRCMP(varname, "changedtick") == 0)
4215 {
4216 rettv->v_type = VAR_NUMBER;
4217 rettv->vval.v_number = curbuf->b_changedtick;
4218 done = TRUE;
4219 }
4220 else
4221 {
4222 /* Look up the variable. */
4223 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4224 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4225 'b', varname, FALSE);
4226 if (v != NULL)
4227 {
4228 copy_tv(&v->di_tv, rettv);
4229 done = TRUE;
4230 }
4231 }
4232
4233 /* restore previous notion of curbuf */
4234 curbuf = save_curbuf;
4235 }
4236
4237 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4238 /* use the default value */
4239 copy_tv(&argvars[2], rettv);
4240
4241 --emsg_off;
4242}
4243
4244/*
4245 * "getchar()" function
4246 */
4247 static void
4248f_getchar(typval_T *argvars, typval_T *rettv)
4249{
4250 varnumber_T n;
4251 int error = FALSE;
4252
4253 /* Position the cursor. Needed after a message that ends in a space. */
4254 windgoto(msg_row, msg_col);
4255
4256 ++no_mapping;
4257 ++allow_keys;
4258 for (;;)
4259 {
4260 if (argvars[0].v_type == VAR_UNKNOWN)
4261 /* getchar(): blocking wait. */
4262 n = safe_vgetc();
4263 else if (get_tv_number_chk(&argvars[0], &error) == 1)
4264 /* getchar(1): only check if char avail */
4265 n = vpeekc_any();
4266 else if (error || vpeekc_any() == NUL)
4267 /* illegal argument or getchar(0) and no char avail: return zero */
4268 n = 0;
4269 else
4270 /* getchar(0) and char avail: return char */
4271 n = safe_vgetc();
4272
4273 if (n == K_IGNORE)
4274 continue;
4275 break;
4276 }
4277 --no_mapping;
4278 --allow_keys;
4279
4280 set_vim_var_nr(VV_MOUSE_WIN, 0);
4281 set_vim_var_nr(VV_MOUSE_WINID, 0);
4282 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4283 set_vim_var_nr(VV_MOUSE_COL, 0);
4284
4285 rettv->vval.v_number = n;
4286 if (IS_SPECIAL(n) || mod_mask != 0)
4287 {
4288 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4289 int i = 0;
4290
4291 /* Turn a special key into three bytes, plus modifier. */
4292 if (mod_mask != 0)
4293 {
4294 temp[i++] = K_SPECIAL;
4295 temp[i++] = KS_MODIFIER;
4296 temp[i++] = mod_mask;
4297 }
4298 if (IS_SPECIAL(n))
4299 {
4300 temp[i++] = K_SPECIAL;
4301 temp[i++] = K_SECOND(n);
4302 temp[i++] = K_THIRD(n);
4303 }
4304#ifdef FEAT_MBYTE
4305 else if (has_mbyte)
4306 i += (*mb_char2bytes)(n, temp + i);
4307#endif
4308 else
4309 temp[i++] = n;
4310 temp[i++] = NUL;
4311 rettv->v_type = VAR_STRING;
4312 rettv->vval.v_string = vim_strsave(temp);
4313
4314#ifdef FEAT_MOUSE
4315 if (is_mouse_key(n))
4316 {
4317 int row = mouse_row;
4318 int col = mouse_col;
4319 win_T *win;
4320 linenr_T lnum;
4321# ifdef FEAT_WINDOWS
4322 win_T *wp;
4323# endif
4324 int winnr = 1;
4325
4326 if (row >= 0 && col >= 0)
4327 {
4328 /* Find the window at the mouse coordinates and compute the
4329 * text position. */
4330 win = mouse_find_win(&row, &col);
4331 (void)mouse_comp_pos(win, &row, &col, &lnum);
4332# ifdef FEAT_WINDOWS
4333 for (wp = firstwin; wp != win; wp = wp->w_next)
4334 ++winnr;
4335# endif
4336 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4337 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4338 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4339 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4340 }
4341 }
4342#endif
4343 }
4344}
4345
4346/*
4347 * "getcharmod()" function
4348 */
4349 static void
4350f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4351{
4352 rettv->vval.v_number = mod_mask;
4353}
4354
4355/*
4356 * "getcharsearch()" function
4357 */
4358 static void
4359f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4360{
4361 if (rettv_dict_alloc(rettv) != FAIL)
4362 {
4363 dict_T *dict = rettv->vval.v_dict;
4364
4365 dict_add_nr_str(dict, "char", 0L, last_csearch());
4366 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
4367 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
4368 }
4369}
4370
4371/*
4372 * "getcmdline()" function
4373 */
4374 static void
4375f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4376{
4377 rettv->v_type = VAR_STRING;
4378 rettv->vval.v_string = get_cmdline_str();
4379}
4380
4381/*
4382 * "getcmdpos()" function
4383 */
4384 static void
4385f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4386{
4387 rettv->vval.v_number = get_cmdline_pos() + 1;
4388}
4389
4390/*
4391 * "getcmdtype()" function
4392 */
4393 static void
4394f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4395{
4396 rettv->v_type = VAR_STRING;
4397 rettv->vval.v_string = alloc(2);
4398 if (rettv->vval.v_string != NULL)
4399 {
4400 rettv->vval.v_string[0] = get_cmdline_type();
4401 rettv->vval.v_string[1] = NUL;
4402 }
4403}
4404
4405/*
4406 * "getcmdwintype()" function
4407 */
4408 static void
4409f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4410{
4411 rettv->v_type = VAR_STRING;
4412 rettv->vval.v_string = NULL;
4413#ifdef FEAT_CMDWIN
4414 rettv->vval.v_string = alloc(2);
4415 if (rettv->vval.v_string != NULL)
4416 {
4417 rettv->vval.v_string[0] = cmdwin_type;
4418 rettv->vval.v_string[1] = NUL;
4419 }
4420#endif
4421}
4422
4423#if defined(FEAT_CMDL_COMPL)
4424/*
4425 * "getcompletion()" function
4426 */
4427 static void
4428f_getcompletion(typval_T *argvars, typval_T *rettv)
4429{
4430 char_u *pat;
4431 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004432 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004433 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
4434 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004435
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004436 if (argvars[2].v_type != VAR_UNKNOWN)
4437 filtered = get_tv_number_chk(&argvars[2], NULL);
4438
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004439 if (p_wic)
4440 options |= WILD_ICASE;
4441
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004442 /* For filtered results, 'wildignore' is used */
4443 if (!filtered)
4444 options |= WILD_KEEP_ALL;
4445
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004446 ExpandInit(&xpc);
4447 xpc.xp_pattern = get_tv_string(&argvars[0]);
4448 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4449 xpc.xp_context = cmdcomplete_str_to_type(get_tv_string(&argvars[1]));
4450 if (xpc.xp_context == EXPAND_NOTHING)
4451 {
4452 if (argvars[1].v_type == VAR_STRING)
4453 EMSG2(_(e_invarg2), argvars[1].vval.v_string);
4454 else
4455 EMSG(_(e_invarg));
4456 return;
4457 }
4458
4459# if defined(FEAT_MENU)
4460 if (xpc.xp_context == EXPAND_MENUS)
4461 {
4462 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4463 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4464 }
4465# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02004466#ifdef FEAT_CSCOPE
4467 if (xpc.xp_context == EXPAND_CSCOPE)
4468 {
4469 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4470 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4471 }
4472#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02004473#ifdef FEAT_SIGNS
4474 if (xpc.xp_context == EXPAND_SIGN)
4475 {
4476 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4477 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4478 }
4479#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004480
4481 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4482 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
4483 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004484 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004485
4486 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4487
4488 for (i = 0; i < xpc.xp_numfiles; i++)
4489 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4490 }
4491 vim_free(pat);
4492 ExpandCleanup(&xpc);
4493}
4494#endif
4495
4496/*
4497 * "getcwd()" function
4498 */
4499 static void
4500f_getcwd(typval_T *argvars, typval_T *rettv)
4501{
4502 win_T *wp = NULL;
4503 char_u *cwd;
4504
4505 rettv->v_type = VAR_STRING;
4506 rettv->vval.v_string = NULL;
4507
4508 wp = find_tabwin(&argvars[0], &argvars[1]);
4509 if (wp != NULL)
4510 {
4511 if (wp->w_localdir != NULL)
4512 rettv->vval.v_string = vim_strsave(wp->w_localdir);
4513 else if (globaldir != NULL)
4514 rettv->vval.v_string = vim_strsave(globaldir);
4515 else
4516 {
4517 cwd = alloc(MAXPATHL);
4518 if (cwd != NULL)
4519 {
4520 if (mch_dirname(cwd, MAXPATHL) != FAIL)
4521 rettv->vval.v_string = vim_strsave(cwd);
4522 vim_free(cwd);
4523 }
4524 }
4525#ifdef BACKSLASH_IN_FILENAME
4526 if (rettv->vval.v_string != NULL)
4527 slash_adjust(rettv->vval.v_string);
4528#endif
4529 }
4530}
4531
4532/*
4533 * "getfontname()" function
4534 */
4535 static void
4536f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4537{
4538 rettv->v_type = VAR_STRING;
4539 rettv->vval.v_string = NULL;
4540#ifdef FEAT_GUI
4541 if (gui.in_use)
4542 {
4543 GuiFont font;
4544 char_u *name = NULL;
4545
4546 if (argvars[0].v_type == VAR_UNKNOWN)
4547 {
4548 /* Get the "Normal" font. Either the name saved by
4549 * hl_set_font_name() or from the font ID. */
4550 font = gui.norm_font;
4551 name = hl_get_font_name();
4552 }
4553 else
4554 {
4555 name = get_tv_string(&argvars[0]);
4556 if (STRCMP(name, "*") == 0) /* don't use font dialog */
4557 return;
4558 font = gui_mch_get_font(name, FALSE);
4559 if (font == NOFONT)
4560 return; /* Invalid font name, return empty string. */
4561 }
4562 rettv->vval.v_string = gui_mch_get_fontname(font, name);
4563 if (argvars[0].v_type != VAR_UNKNOWN)
4564 gui_mch_free_font(font);
4565 }
4566#endif
4567}
4568
4569/*
4570 * "getfperm({fname})" function
4571 */
4572 static void
4573f_getfperm(typval_T *argvars, typval_T *rettv)
4574{
4575 char_u *fname;
4576 stat_T st;
4577 char_u *perm = NULL;
4578 char_u flags[] = "rwx";
4579 int i;
4580
4581 fname = get_tv_string(&argvars[0]);
4582
4583 rettv->v_type = VAR_STRING;
4584 if (mch_stat((char *)fname, &st) >= 0)
4585 {
4586 perm = vim_strsave((char_u *)"---------");
4587 if (perm != NULL)
4588 {
4589 for (i = 0; i < 9; i++)
4590 {
4591 if (st.st_mode & (1 << (8 - i)))
4592 perm[i] = flags[i % 3];
4593 }
4594 }
4595 }
4596 rettv->vval.v_string = perm;
4597}
4598
4599/*
4600 * "getfsize({fname})" function
4601 */
4602 static void
4603f_getfsize(typval_T *argvars, typval_T *rettv)
4604{
4605 char_u *fname;
4606 stat_T st;
4607
4608 fname = get_tv_string(&argvars[0]);
4609
4610 rettv->v_type = VAR_NUMBER;
4611
4612 if (mch_stat((char *)fname, &st) >= 0)
4613 {
4614 if (mch_isdir(fname))
4615 rettv->vval.v_number = 0;
4616 else
4617 {
4618 rettv->vval.v_number = (varnumber_T)st.st_size;
4619
4620 /* non-perfect check for overflow */
4621 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
4622 rettv->vval.v_number = -2;
4623 }
4624 }
4625 else
4626 rettv->vval.v_number = -1;
4627}
4628
4629/*
4630 * "getftime({fname})" function
4631 */
4632 static void
4633f_getftime(typval_T *argvars, typval_T *rettv)
4634{
4635 char_u *fname;
4636 stat_T st;
4637
4638 fname = get_tv_string(&argvars[0]);
4639
4640 if (mch_stat((char *)fname, &st) >= 0)
4641 rettv->vval.v_number = (varnumber_T)st.st_mtime;
4642 else
4643 rettv->vval.v_number = -1;
4644}
4645
4646/*
4647 * "getftype({fname})" function
4648 */
4649 static void
4650f_getftype(typval_T *argvars, typval_T *rettv)
4651{
4652 char_u *fname;
4653 stat_T st;
4654 char_u *type = NULL;
4655 char *t;
4656
4657 fname = get_tv_string(&argvars[0]);
4658
4659 rettv->v_type = VAR_STRING;
4660 if (mch_lstat((char *)fname, &st) >= 0)
4661 {
4662#ifdef S_ISREG
4663 if (S_ISREG(st.st_mode))
4664 t = "file";
4665 else if (S_ISDIR(st.st_mode))
4666 t = "dir";
4667# ifdef S_ISLNK
4668 else if (S_ISLNK(st.st_mode))
4669 t = "link";
4670# endif
4671# ifdef S_ISBLK
4672 else if (S_ISBLK(st.st_mode))
4673 t = "bdev";
4674# endif
4675# ifdef S_ISCHR
4676 else if (S_ISCHR(st.st_mode))
4677 t = "cdev";
4678# endif
4679# ifdef S_ISFIFO
4680 else if (S_ISFIFO(st.st_mode))
4681 t = "fifo";
4682# endif
4683# ifdef S_ISSOCK
4684 else if (S_ISSOCK(st.st_mode))
4685 t = "fifo";
4686# endif
4687 else
4688 t = "other";
4689#else
4690# ifdef S_IFMT
4691 switch (st.st_mode & S_IFMT)
4692 {
4693 case S_IFREG: t = "file"; break;
4694 case S_IFDIR: t = "dir"; break;
4695# ifdef S_IFLNK
4696 case S_IFLNK: t = "link"; break;
4697# endif
4698# ifdef S_IFBLK
4699 case S_IFBLK: t = "bdev"; break;
4700# endif
4701# ifdef S_IFCHR
4702 case S_IFCHR: t = "cdev"; break;
4703# endif
4704# ifdef S_IFIFO
4705 case S_IFIFO: t = "fifo"; break;
4706# endif
4707# ifdef S_IFSOCK
4708 case S_IFSOCK: t = "socket"; break;
4709# endif
4710 default: t = "other";
4711 }
4712# else
4713 if (mch_isdir(fname))
4714 t = "dir";
4715 else
4716 t = "file";
4717# endif
4718#endif
4719 type = vim_strsave((char_u *)t);
4720 }
4721 rettv->vval.v_string = type;
4722}
4723
4724/*
4725 * "getline(lnum, [end])" function
4726 */
4727 static void
4728f_getline(typval_T *argvars, typval_T *rettv)
4729{
4730 linenr_T lnum;
4731 linenr_T end;
4732 int retlist;
4733
4734 lnum = get_tv_lnum(argvars);
4735 if (argvars[1].v_type == VAR_UNKNOWN)
4736 {
4737 end = 0;
4738 retlist = FALSE;
4739 }
4740 else
4741 {
4742 end = get_tv_lnum(&argvars[1]);
4743 retlist = TRUE;
4744 }
4745
4746 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
4747}
4748
Bram Moolenaar4ae20952016-08-13 15:29:14 +02004749#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02004750 static void
4751get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
4752{
Bram Moolenaard823fa92016-08-12 16:29:27 +02004753 if (what_arg->v_type == VAR_UNKNOWN)
4754 {
4755 if (rettv_list_alloc(rettv) == OK)
4756 if (is_qf || wp != NULL)
4757 (void)get_errorlist(wp, -1, rettv->vval.v_list);
4758 }
4759 else
4760 {
4761 if (rettv_dict_alloc(rettv) == OK)
4762 if (is_qf || (wp != NULL))
4763 {
4764 if (what_arg->v_type == VAR_DICT)
4765 {
4766 dict_T *d = what_arg->vval.v_dict;
4767
4768 if (d != NULL)
4769 get_errorlist_properties(wp, d, rettv->vval.v_dict);
4770 }
4771 else
4772 EMSG(_(e_dictreq));
4773 }
4774 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02004775}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02004776#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02004777
4778/*
4779 * "getloclist()" function
4780 */
4781 static void
4782f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4783{
4784#ifdef FEAT_QUICKFIX
4785 win_T *wp;
4786
4787 wp = find_win_by_nr(&argvars[0], NULL);
4788 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
4789#endif
4790}
4791
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004792/*
4793 * "getmatches()" function
4794 */
4795 static void
4796f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4797{
4798#ifdef FEAT_SEARCH_EXTRA
4799 dict_T *dict;
4800 matchitem_T *cur = curwin->w_match_head;
4801 int i;
4802
4803 if (rettv_list_alloc(rettv) == OK)
4804 {
4805 while (cur != NULL)
4806 {
4807 dict = dict_alloc();
4808 if (dict == NULL)
4809 return;
4810 if (cur->match.regprog == NULL)
4811 {
4812 /* match added with matchaddpos() */
4813 for (i = 0; i < MAXPOSMATCH; ++i)
4814 {
4815 llpos_T *llpos;
4816 char buf[6];
4817 list_T *l;
4818
4819 llpos = &cur->pos.pos[i];
4820 if (llpos->lnum == 0)
4821 break;
4822 l = list_alloc();
4823 if (l == NULL)
4824 break;
4825 list_append_number(l, (varnumber_T)llpos->lnum);
4826 if (llpos->col > 0)
4827 {
4828 list_append_number(l, (varnumber_T)llpos->col);
4829 list_append_number(l, (varnumber_T)llpos->len);
4830 }
4831 sprintf(buf, "pos%d", i + 1);
4832 dict_add_list(dict, buf, l);
4833 }
4834 }
4835 else
4836 {
4837 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
4838 }
4839 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
4840 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
4841 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
4842# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
4843 if (cur->conceal_char)
4844 {
4845 char_u buf[MB_MAXBYTES + 1];
4846
4847 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
4848 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
4849 }
4850# endif
4851 list_append_dict(rettv->vval.v_list, dict);
4852 cur = cur->next;
4853 }
4854 }
4855#endif
4856}
4857
4858/*
4859 * "getpid()" function
4860 */
4861 static void
4862f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
4863{
4864 rettv->vval.v_number = mch_get_pid();
4865}
4866
4867 static void
4868getpos_both(
4869 typval_T *argvars,
4870 typval_T *rettv,
4871 int getcurpos)
4872{
4873 pos_T *fp;
4874 list_T *l;
4875 int fnum = -1;
4876
4877 if (rettv_list_alloc(rettv) == OK)
4878 {
4879 l = rettv->vval.v_list;
4880 if (getcurpos)
4881 fp = &curwin->w_cursor;
4882 else
4883 fp = var2fpos(&argvars[0], TRUE, &fnum);
4884 if (fnum != -1)
4885 list_append_number(l, (varnumber_T)fnum);
4886 else
4887 list_append_number(l, (varnumber_T)0);
4888 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
4889 : (varnumber_T)0);
4890 list_append_number(l, (fp != NULL)
4891 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
4892 : (varnumber_T)0);
4893 list_append_number(l,
4894#ifdef FEAT_VIRTUALEDIT
4895 (fp != NULL) ? (varnumber_T)fp->coladd :
4896#endif
4897 (varnumber_T)0);
4898 if (getcurpos)
4899 {
4900 update_curswant();
4901 list_append_number(l, curwin->w_curswant == MAXCOL ?
4902 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
4903 }
4904 }
4905 else
4906 rettv->vval.v_number = FALSE;
4907}
4908
4909
4910/*
4911 * "getcurpos()" function
4912 */
4913 static void
4914f_getcurpos(typval_T *argvars, typval_T *rettv)
4915{
4916 getpos_both(argvars, rettv, TRUE);
4917}
4918
4919/*
4920 * "getpos(string)" function
4921 */
4922 static void
4923f_getpos(typval_T *argvars, typval_T *rettv)
4924{
4925 getpos_both(argvars, rettv, FALSE);
4926}
4927
4928/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02004929 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004930 */
4931 static void
4932f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4933{
4934#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02004935 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004936#endif
4937}
4938
4939/*
4940 * "getreg()" function
4941 */
4942 static void
4943f_getreg(typval_T *argvars, typval_T *rettv)
4944{
4945 char_u *strregname;
4946 int regname;
4947 int arg2 = FALSE;
4948 int return_list = FALSE;
4949 int error = FALSE;
4950
4951 if (argvars[0].v_type != VAR_UNKNOWN)
4952 {
4953 strregname = get_tv_string_chk(&argvars[0]);
4954 error = strregname == NULL;
4955 if (argvars[1].v_type != VAR_UNKNOWN)
4956 {
4957 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
4958 if (!error && argvars[2].v_type != VAR_UNKNOWN)
4959 return_list = (int)get_tv_number_chk(&argvars[2], &error);
4960 }
4961 }
4962 else
4963 strregname = get_vim_var_str(VV_REG);
4964
4965 if (error)
4966 return;
4967
4968 regname = (strregname == NULL ? '"' : *strregname);
4969 if (regname == 0)
4970 regname = '"';
4971
4972 if (return_list)
4973 {
4974 rettv->v_type = VAR_LIST;
4975 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
4976 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
4977 if (rettv->vval.v_list == NULL)
4978 (void)rettv_list_alloc(rettv);
4979 else
4980 ++rettv->vval.v_list->lv_refcount;
4981 }
4982 else
4983 {
4984 rettv->v_type = VAR_STRING;
4985 rettv->vval.v_string = get_reg_contents(regname,
4986 arg2 ? GREG_EXPR_SRC : 0);
4987 }
4988}
4989
4990/*
4991 * "getregtype()" function
4992 */
4993 static void
4994f_getregtype(typval_T *argvars, typval_T *rettv)
4995{
4996 char_u *strregname;
4997 int regname;
4998 char_u buf[NUMBUFLEN + 2];
4999 long reglen = 0;
5000
5001 if (argvars[0].v_type != VAR_UNKNOWN)
5002 {
5003 strregname = get_tv_string_chk(&argvars[0]);
5004 if (strregname == NULL) /* type error; errmsg already given */
5005 {
5006 rettv->v_type = VAR_STRING;
5007 rettv->vval.v_string = NULL;
5008 return;
5009 }
5010 }
5011 else
5012 /* Default to v:register */
5013 strregname = get_vim_var_str(VV_REG);
5014
5015 regname = (strregname == NULL ? '"' : *strregname);
5016 if (regname == 0)
5017 regname = '"';
5018
5019 buf[0] = NUL;
5020 buf[1] = NUL;
5021 switch (get_reg_type(regname, &reglen))
5022 {
5023 case MLINE: buf[0] = 'V'; break;
5024 case MCHAR: buf[0] = 'v'; break;
5025 case MBLOCK:
5026 buf[0] = Ctrl_V;
5027 sprintf((char *)buf + 1, "%ld", reglen + 1);
5028 break;
5029 }
5030 rettv->v_type = VAR_STRING;
5031 rettv->vval.v_string = vim_strsave(buf);
5032}
5033
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005034#ifdef FEAT_WINDOWS
5035/*
5036 * Returns information (variables, options, etc.) about a tab page
5037 * as a dictionary.
5038 */
5039 static dict_T *
5040get_tabpage_info(tabpage_T *tp, int tp_idx)
5041{
5042 win_T *wp;
5043 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005044 list_T *l;
5045
5046 dict = dict_alloc();
5047 if (dict == NULL)
5048 return NULL;
5049
Bram Moolenaar33928832016-08-18 21:22:04 +02005050 dict_add_nr_str(dict, "tabnr", tp_idx, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005051
5052 l = list_alloc();
5053 if (l != NULL)
5054 {
5055 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5056 wp; wp = wp->w_next)
5057 list_append_number(l, (varnumber_T)wp->w_id);
5058 dict_add_list(dict, "windows", l);
5059 }
5060
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005061 /* Make a reference to tabpage variables */
5062 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005063
5064 return dict;
5065}
5066#endif
5067
5068/*
5069 * "gettabinfo()" function
5070 */
5071 static void
5072f_gettabinfo(typval_T *argvars, typval_T *rettv)
5073{
5074#ifdef FEAT_WINDOWS
5075 tabpage_T *tp, *tparg = NULL;
5076 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005077 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005078
5079 if (rettv_list_alloc(rettv) != OK)
5080 return;
5081
5082 if (argvars[0].v_type != VAR_UNKNOWN)
5083 {
5084 /* Information about one tab page */
5085 tparg = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5086 if (tparg == NULL)
5087 return;
5088 }
5089
5090 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005091 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005092 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005093 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005094 if (tparg != NULL && tp != tparg)
5095 continue;
5096 d = get_tabpage_info(tp, tpnr);
5097 if (d != NULL)
5098 list_append_dict(rettv->vval.v_list, d);
5099 if (tparg != NULL)
5100 return;
5101 }
5102#endif
5103}
5104
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005105/*
5106 * "gettabvar()" function
5107 */
5108 static void
5109f_gettabvar(typval_T *argvars, typval_T *rettv)
5110{
5111 win_T *oldcurwin;
5112 tabpage_T *tp, *oldtabpage;
5113 dictitem_T *v;
5114 char_u *varname;
5115 int done = FALSE;
5116
5117 rettv->v_type = VAR_STRING;
5118 rettv->vval.v_string = NULL;
5119
5120 varname = get_tv_string_chk(&argvars[1]);
5121 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5122 if (tp != NULL && varname != NULL)
5123 {
5124 /* Set tp to be our tabpage, temporarily. Also set the window to the
5125 * first window in the tabpage, otherwise the window is not valid. */
5126 if (switch_win(&oldcurwin, &oldtabpage,
5127 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
5128 == OK)
5129 {
5130 /* look up the variable */
5131 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5132 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5133 if (v != NULL)
5134 {
5135 copy_tv(&v->di_tv, rettv);
5136 done = TRUE;
5137 }
5138 }
5139
5140 /* restore previous notion of curwin */
5141 restore_win(oldcurwin, oldtabpage, TRUE);
5142 }
5143
5144 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5145 copy_tv(&argvars[2], rettv);
5146}
5147
5148/*
5149 * "gettabwinvar()" function
5150 */
5151 static void
5152f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5153{
5154 getwinvar(argvars, rettv, 1);
5155}
5156
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005157#ifdef FEAT_WINDOWS
5158/*
5159 * Returns information about a window as a dictionary.
5160 */
5161 static dict_T *
5162get_win_info(win_T *wp, short tpnr, short winnr)
5163{
5164 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005165
5166 dict = dict_alloc();
5167 if (dict == NULL)
5168 return NULL;
5169
Bram Moolenaar33928832016-08-18 21:22:04 +02005170 dict_add_nr_str(dict, "tabnr", tpnr, NULL);
5171 dict_add_nr_str(dict, "winnr", winnr, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005172 dict_add_nr_str(dict, "winid", wp->w_id, NULL);
5173 dict_add_nr_str(dict, "height", wp->w_height, NULL);
5174 dict_add_nr_str(dict, "width", wp->w_width, NULL);
Bram Moolenaar33928832016-08-18 21:22:04 +02005175 dict_add_nr_str(dict, "bufnr", wp->w_buffer->b_fnum, NULL);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005176
Bram Moolenaar386600f2016-08-15 22:16:25 +02005177#ifdef FEAT_QUICKFIX
5178 dict_add_nr_str(dict, "quickfix", bt_quickfix(wp->w_buffer), NULL);
5179 dict_add_nr_str(dict, "loclist",
5180 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL), NULL);
5181#endif
5182
Bram Moolenaar30567352016-08-27 21:25:44 +02005183 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005184 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005185
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005186 return dict;
5187}
5188#endif
5189
5190/*
5191 * "getwininfo()" function
5192 */
5193 static void
5194f_getwininfo(typval_T *argvars, typval_T *rettv)
5195{
5196#ifdef FEAT_WINDOWS
5197 tabpage_T *tp;
5198 win_T *wp = NULL, *wparg = NULL;
5199 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005200 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005201#endif
5202
5203 if (rettv_list_alloc(rettv) != OK)
5204 return;
5205
5206#ifdef FEAT_WINDOWS
5207 if (argvars[0].v_type != VAR_UNKNOWN)
5208 {
5209 wparg = win_id2wp(argvars);
5210 if (wparg == NULL)
5211 return;
5212 }
5213
5214 /* Collect information about either all the windows across all the tab
5215 * pages or one particular window.
5216 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005217 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005218 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005219 tabnr++;
5220 winnr = 0;
5221 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005222 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005223 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005224 if (wparg != NULL && wp != wparg)
5225 continue;
5226 d = get_win_info(wp, tabnr, winnr);
5227 if (d != NULL)
5228 list_append_dict(rettv->vval.v_list, d);
5229 if (wparg != NULL)
5230 /* found information about a specific window */
5231 return;
5232 }
5233 }
5234#endif
5235}
5236
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005237/*
5238 * "getwinposx()" function
5239 */
5240 static void
5241f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5242{
5243 rettv->vval.v_number = -1;
5244#ifdef FEAT_GUI
5245 if (gui.in_use)
5246 {
5247 int x, y;
5248
5249 if (gui_mch_get_winpos(&x, &y) == OK)
5250 rettv->vval.v_number = x;
5251 }
5252#endif
5253}
5254
5255/*
5256 * "win_findbuf()" function
5257 */
5258 static void
5259f_win_findbuf(typval_T *argvars, typval_T *rettv)
5260{
5261 if (rettv_list_alloc(rettv) != FAIL)
5262 win_findbuf(argvars, rettv->vval.v_list);
5263}
5264
5265/*
5266 * "win_getid()" function
5267 */
5268 static void
5269f_win_getid(typval_T *argvars, typval_T *rettv)
5270{
5271 rettv->vval.v_number = win_getid(argvars);
5272}
5273
5274/*
5275 * "win_gotoid()" function
5276 */
5277 static void
5278f_win_gotoid(typval_T *argvars, typval_T *rettv)
5279{
5280 rettv->vval.v_number = win_gotoid(argvars);
5281}
5282
5283/*
5284 * "win_id2tabwin()" function
5285 */
5286 static void
5287f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5288{
5289 if (rettv_list_alloc(rettv) != FAIL)
5290 win_id2tabwin(argvars, rettv->vval.v_list);
5291}
5292
5293/*
5294 * "win_id2win()" function
5295 */
5296 static void
5297f_win_id2win(typval_T *argvars, typval_T *rettv)
5298{
5299 rettv->vval.v_number = win_id2win(argvars);
5300}
5301
5302/*
5303 * "getwinposy()" function
5304 */
5305 static void
5306f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5307{
5308 rettv->vval.v_number = -1;
5309#ifdef FEAT_GUI
5310 if (gui.in_use)
5311 {
5312 int x, y;
5313
5314 if (gui_mch_get_winpos(&x, &y) == OK)
5315 rettv->vval.v_number = y;
5316 }
5317#endif
5318}
5319
5320/*
5321 * "getwinvar()" function
5322 */
5323 static void
5324f_getwinvar(typval_T *argvars, typval_T *rettv)
5325{
5326 getwinvar(argvars, rettv, 0);
5327}
5328
5329/*
5330 * "glob()" function
5331 */
5332 static void
5333f_glob(typval_T *argvars, typval_T *rettv)
5334{
5335 int options = WILD_SILENT|WILD_USE_NL;
5336 expand_T xpc;
5337 int error = FALSE;
5338
5339 /* When the optional second argument is non-zero, don't remove matches
5340 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5341 rettv->v_type = VAR_STRING;
5342 if (argvars[1].v_type != VAR_UNKNOWN)
5343 {
5344 if (get_tv_number_chk(&argvars[1], &error))
5345 options |= WILD_KEEP_ALL;
5346 if (argvars[2].v_type != VAR_UNKNOWN)
5347 {
5348 if (get_tv_number_chk(&argvars[2], &error))
5349 {
5350 rettv->v_type = VAR_LIST;
5351 rettv->vval.v_list = NULL;
5352 }
5353 if (argvars[3].v_type != VAR_UNKNOWN
5354 && get_tv_number_chk(&argvars[3], &error))
5355 options |= WILD_ALLLINKS;
5356 }
5357 }
5358 if (!error)
5359 {
5360 ExpandInit(&xpc);
5361 xpc.xp_context = EXPAND_FILES;
5362 if (p_wic)
5363 options += WILD_ICASE;
5364 if (rettv->v_type == VAR_STRING)
5365 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
5366 NULL, options, WILD_ALL);
5367 else if (rettv_list_alloc(rettv) != FAIL)
5368 {
5369 int i;
5370
5371 ExpandOne(&xpc, get_tv_string(&argvars[0]),
5372 NULL, options, WILD_ALL_KEEP);
5373 for (i = 0; i < xpc.xp_numfiles; i++)
5374 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5375
5376 ExpandCleanup(&xpc);
5377 }
5378 }
5379 else
5380 rettv->vval.v_string = NULL;
5381}
5382
5383/*
5384 * "globpath()" function
5385 */
5386 static void
5387f_globpath(typval_T *argvars, typval_T *rettv)
5388{
5389 int flags = 0;
5390 char_u buf1[NUMBUFLEN];
5391 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
5392 int error = FALSE;
5393 garray_T ga;
5394 int i;
5395
5396 /* When the optional second argument is non-zero, don't remove matches
5397 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5398 rettv->v_type = VAR_STRING;
5399 if (argvars[2].v_type != VAR_UNKNOWN)
5400 {
5401 if (get_tv_number_chk(&argvars[2], &error))
5402 flags |= WILD_KEEP_ALL;
5403 if (argvars[3].v_type != VAR_UNKNOWN)
5404 {
5405 if (get_tv_number_chk(&argvars[3], &error))
5406 {
5407 rettv->v_type = VAR_LIST;
5408 rettv->vval.v_list = NULL;
5409 }
5410 if (argvars[4].v_type != VAR_UNKNOWN
5411 && get_tv_number_chk(&argvars[4], &error))
5412 flags |= WILD_ALLLINKS;
5413 }
5414 }
5415 if (file != NULL && !error)
5416 {
5417 ga_init2(&ga, (int)sizeof(char_u *), 10);
5418 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
5419 if (rettv->v_type == VAR_STRING)
5420 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
5421 else if (rettv_list_alloc(rettv) != FAIL)
5422 for (i = 0; i < ga.ga_len; ++i)
5423 list_append_string(rettv->vval.v_list,
5424 ((char_u **)(ga.ga_data))[i], -1);
5425 ga_clear_strings(&ga);
5426 }
5427 else
5428 rettv->vval.v_string = NULL;
5429}
5430
5431/*
5432 * "glob2regpat()" function
5433 */
5434 static void
5435f_glob2regpat(typval_T *argvars, typval_T *rettv)
5436{
5437 char_u *pat = get_tv_string_chk(&argvars[0]);
5438
5439 rettv->v_type = VAR_STRING;
5440 rettv->vval.v_string = (pat == NULL)
5441 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
5442}
5443
5444/* for VIM_VERSION_ defines */
5445#include "version.h"
5446
5447/*
5448 * "has()" function
5449 */
5450 static void
5451f_has(typval_T *argvars, typval_T *rettv)
5452{
5453 int i;
5454 char_u *name;
5455 int n = FALSE;
5456 static char *(has_list[]) =
5457 {
5458#ifdef AMIGA
5459 "amiga",
5460# ifdef FEAT_ARP
5461 "arp",
5462# endif
5463#endif
5464#ifdef __BEOS__
5465 "beos",
5466#endif
5467#ifdef MACOS
5468 "mac",
5469#endif
5470#if defined(MACOS_X_UNIX)
5471 "macunix", /* built with 'darwin' enabled */
5472#endif
5473#if defined(__APPLE__) && __APPLE__ == 1
5474 "osx", /* built with or without 'darwin' enabled */
5475#endif
5476#ifdef __QNX__
5477 "qnx",
5478#endif
5479#ifdef UNIX
5480 "unix",
5481#endif
5482#ifdef VMS
5483 "vms",
5484#endif
5485#ifdef WIN32
5486 "win32",
5487#endif
5488#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
5489 "win32unix",
5490#endif
5491#if defined(WIN64) || defined(_WIN64)
5492 "win64",
5493#endif
5494#ifdef EBCDIC
5495 "ebcdic",
5496#endif
5497#ifndef CASE_INSENSITIVE_FILENAME
5498 "fname_case",
5499#endif
5500#ifdef HAVE_ACL
5501 "acl",
5502#endif
5503#ifdef FEAT_ARABIC
5504 "arabic",
5505#endif
5506#ifdef FEAT_AUTOCMD
5507 "autocmd",
5508#endif
5509#ifdef FEAT_BEVAL
5510 "balloon_eval",
5511# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
5512 "balloon_multiline",
5513# endif
5514#endif
5515#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
5516 "builtin_terms",
5517# ifdef ALL_BUILTIN_TCAPS
5518 "all_builtin_terms",
5519# endif
5520#endif
5521#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
5522 || defined(FEAT_GUI_W32) \
5523 || defined(FEAT_GUI_MOTIF))
5524 "browsefilter",
5525#endif
5526#ifdef FEAT_BYTEOFF
5527 "byte_offset",
5528#endif
5529#ifdef FEAT_JOB_CHANNEL
5530 "channel",
5531#endif
5532#ifdef FEAT_CINDENT
5533 "cindent",
5534#endif
5535#ifdef FEAT_CLIENTSERVER
5536 "clientserver",
5537#endif
5538#ifdef FEAT_CLIPBOARD
5539 "clipboard",
5540#endif
5541#ifdef FEAT_CMDL_COMPL
5542 "cmdline_compl",
5543#endif
5544#ifdef FEAT_CMDHIST
5545 "cmdline_hist",
5546#endif
5547#ifdef FEAT_COMMENTS
5548 "comments",
5549#endif
5550#ifdef FEAT_CONCEAL
5551 "conceal",
5552#endif
5553#ifdef FEAT_CRYPT
5554 "cryptv",
5555 "crypt-blowfish",
5556 "crypt-blowfish2",
5557#endif
5558#ifdef FEAT_CSCOPE
5559 "cscope",
5560#endif
5561#ifdef FEAT_CURSORBIND
5562 "cursorbind",
5563#endif
5564#ifdef CURSOR_SHAPE
5565 "cursorshape",
5566#endif
5567#ifdef DEBUG
5568 "debug",
5569#endif
5570#ifdef FEAT_CON_DIALOG
5571 "dialog_con",
5572#endif
5573#ifdef FEAT_GUI_DIALOG
5574 "dialog_gui",
5575#endif
5576#ifdef FEAT_DIFF
5577 "diff",
5578#endif
5579#ifdef FEAT_DIGRAPHS
5580 "digraphs",
5581#endif
5582#ifdef FEAT_DIRECTX
5583 "directx",
5584#endif
5585#ifdef FEAT_DND
5586 "dnd",
5587#endif
5588#ifdef FEAT_EMACS_TAGS
5589 "emacs_tags",
5590#endif
5591 "eval", /* always present, of course! */
5592 "ex_extra", /* graduated feature */
5593#ifdef FEAT_SEARCH_EXTRA
5594 "extra_search",
5595#endif
5596#ifdef FEAT_FKMAP
5597 "farsi",
5598#endif
5599#ifdef FEAT_SEARCHPATH
5600 "file_in_path",
5601#endif
5602#ifdef FEAT_FILTERPIPE
5603 "filterpipe",
5604#endif
5605#ifdef FEAT_FIND_ID
5606 "find_in_path",
5607#endif
5608#ifdef FEAT_FLOAT
5609 "float",
5610#endif
5611#ifdef FEAT_FOLDING
5612 "folding",
5613#endif
5614#ifdef FEAT_FOOTER
5615 "footer",
5616#endif
5617#if !defined(USE_SYSTEM) && defined(UNIX)
5618 "fork",
5619#endif
5620#ifdef FEAT_GETTEXT
5621 "gettext",
5622#endif
5623#ifdef FEAT_GUI
5624 "gui",
5625#endif
5626#ifdef FEAT_GUI_ATHENA
5627# ifdef FEAT_GUI_NEXTAW
5628 "gui_neXtaw",
5629# else
5630 "gui_athena",
5631# endif
5632#endif
5633#ifdef FEAT_GUI_GTK
5634 "gui_gtk",
5635# ifdef USE_GTK3
5636 "gui_gtk3",
5637# else
5638 "gui_gtk2",
5639# endif
5640#endif
5641#ifdef FEAT_GUI_GNOME
5642 "gui_gnome",
5643#endif
5644#ifdef FEAT_GUI_MAC
5645 "gui_mac",
5646#endif
5647#ifdef FEAT_GUI_MOTIF
5648 "gui_motif",
5649#endif
5650#ifdef FEAT_GUI_PHOTON
5651 "gui_photon",
5652#endif
5653#ifdef FEAT_GUI_W32
5654 "gui_win32",
5655#endif
5656#ifdef FEAT_HANGULIN
5657 "hangul_input",
5658#endif
5659#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
5660 "iconv",
5661#endif
5662#ifdef FEAT_INS_EXPAND
5663 "insert_expand",
5664#endif
5665#ifdef FEAT_JOB_CHANNEL
5666 "job",
5667#endif
5668#ifdef FEAT_JUMPLIST
5669 "jumplist",
5670#endif
5671#ifdef FEAT_KEYMAP
5672 "keymap",
5673#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02005674 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005675#ifdef FEAT_LANGMAP
5676 "langmap",
5677#endif
5678#ifdef FEAT_LIBCALL
5679 "libcall",
5680#endif
5681#ifdef FEAT_LINEBREAK
5682 "linebreak",
5683#endif
5684#ifdef FEAT_LISP
5685 "lispindent",
5686#endif
5687#ifdef FEAT_LISTCMDS
5688 "listcmds",
5689#endif
5690#ifdef FEAT_LOCALMAP
5691 "localmap",
5692#endif
5693#ifdef FEAT_LUA
5694# ifndef DYNAMIC_LUA
5695 "lua",
5696# endif
5697#endif
5698#ifdef FEAT_MENU
5699 "menu",
5700#endif
5701#ifdef FEAT_SESSION
5702 "mksession",
5703#endif
5704#ifdef FEAT_MODIFY_FNAME
5705 "modify_fname",
5706#endif
5707#ifdef FEAT_MOUSE
5708 "mouse",
5709#endif
5710#ifdef FEAT_MOUSESHAPE
5711 "mouseshape",
5712#endif
5713#if defined(UNIX) || defined(VMS)
5714# ifdef FEAT_MOUSE_DEC
5715 "mouse_dec",
5716# endif
5717# ifdef FEAT_MOUSE_GPM
5718 "mouse_gpm",
5719# endif
5720# ifdef FEAT_MOUSE_JSB
5721 "mouse_jsbterm",
5722# endif
5723# ifdef FEAT_MOUSE_NET
5724 "mouse_netterm",
5725# endif
5726# ifdef FEAT_MOUSE_PTERM
5727 "mouse_pterm",
5728# endif
5729# ifdef FEAT_MOUSE_SGR
5730 "mouse_sgr",
5731# endif
5732# ifdef FEAT_SYSMOUSE
5733 "mouse_sysmouse",
5734# endif
5735# ifdef FEAT_MOUSE_URXVT
5736 "mouse_urxvt",
5737# endif
5738# ifdef FEAT_MOUSE_XTERM
5739 "mouse_xterm",
5740# endif
5741#endif
5742#ifdef FEAT_MBYTE
5743 "multi_byte",
5744#endif
5745#ifdef FEAT_MBYTE_IME
5746 "multi_byte_ime",
5747#endif
5748#ifdef FEAT_MULTI_LANG
5749 "multi_lang",
5750#endif
5751#ifdef FEAT_MZSCHEME
5752#ifndef DYNAMIC_MZSCHEME
5753 "mzscheme",
5754#endif
5755#endif
5756#ifdef FEAT_NUM64
5757 "num64",
5758#endif
5759#ifdef FEAT_OLE
5760 "ole",
5761#endif
5762 "packages",
5763#ifdef FEAT_PATH_EXTRA
5764 "path_extra",
5765#endif
5766#ifdef FEAT_PERL
5767#ifndef DYNAMIC_PERL
5768 "perl",
5769#endif
5770#endif
5771#ifdef FEAT_PERSISTENT_UNDO
5772 "persistent_undo",
5773#endif
5774#ifdef FEAT_PYTHON
5775#ifndef DYNAMIC_PYTHON
5776 "python",
5777#endif
5778#endif
5779#ifdef FEAT_PYTHON3
5780#ifndef DYNAMIC_PYTHON3
5781 "python3",
5782#endif
5783#endif
5784#ifdef FEAT_POSTSCRIPT
5785 "postscript",
5786#endif
5787#ifdef FEAT_PRINTER
5788 "printer",
5789#endif
5790#ifdef FEAT_PROFILE
5791 "profile",
5792#endif
5793#ifdef FEAT_RELTIME
5794 "reltime",
5795#endif
5796#ifdef FEAT_QUICKFIX
5797 "quickfix",
5798#endif
5799#ifdef FEAT_RIGHTLEFT
5800 "rightleft",
5801#endif
5802#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
5803 "ruby",
5804#endif
5805#ifdef FEAT_SCROLLBIND
5806 "scrollbind",
5807#endif
5808#ifdef FEAT_CMDL_INFO
5809 "showcmd",
5810 "cmdline_info",
5811#endif
5812#ifdef FEAT_SIGNS
5813 "signs",
5814#endif
5815#ifdef FEAT_SMARTINDENT
5816 "smartindent",
5817#endif
5818#ifdef STARTUPTIME
5819 "startuptime",
5820#endif
5821#ifdef FEAT_STL_OPT
5822 "statusline",
5823#endif
5824#ifdef FEAT_SUN_WORKSHOP
5825 "sun_workshop",
5826#endif
5827#ifdef FEAT_NETBEANS_INTG
5828 "netbeans_intg",
5829#endif
5830#ifdef FEAT_SPELL
5831 "spell",
5832#endif
5833#ifdef FEAT_SYN_HL
5834 "syntax",
5835#endif
5836#if defined(USE_SYSTEM) || !defined(UNIX)
5837 "system",
5838#endif
5839#ifdef FEAT_TAG_BINS
5840 "tag_binary",
5841#endif
5842#ifdef FEAT_TAG_OLDSTATIC
5843 "tag_old_static",
5844#endif
5845#ifdef FEAT_TAG_ANYWHITE
5846 "tag_any_white",
5847#endif
5848#ifdef FEAT_TCL
5849# ifndef DYNAMIC_TCL
5850 "tcl",
5851# endif
5852#endif
5853#ifdef FEAT_TERMGUICOLORS
5854 "termguicolors",
5855#endif
5856#ifdef TERMINFO
5857 "terminfo",
5858#endif
5859#ifdef FEAT_TERMRESPONSE
5860 "termresponse",
5861#endif
5862#ifdef FEAT_TEXTOBJ
5863 "textobjects",
5864#endif
5865#ifdef HAVE_TGETENT
5866 "tgetent",
5867#endif
5868#ifdef FEAT_TIMERS
5869 "timers",
5870#endif
5871#ifdef FEAT_TITLE
5872 "title",
5873#endif
5874#ifdef FEAT_TOOLBAR
5875 "toolbar",
5876#endif
5877#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
5878 "unnamedplus",
5879#endif
5880#ifdef FEAT_USR_CMDS
5881 "user-commands", /* was accidentally included in 5.4 */
5882 "user_commands",
5883#endif
5884#ifdef FEAT_VIMINFO
5885 "viminfo",
5886#endif
5887#ifdef FEAT_WINDOWS
5888 "vertsplit",
5889#endif
5890#ifdef FEAT_VIRTUALEDIT
5891 "virtualedit",
5892#endif
5893 "visual",
5894#ifdef FEAT_VISUALEXTRA
5895 "visualextra",
5896#endif
5897#ifdef FEAT_VREPLACE
5898 "vreplace",
5899#endif
5900#ifdef FEAT_WILDIGN
5901 "wildignore",
5902#endif
5903#ifdef FEAT_WILDMENU
5904 "wildmenu",
5905#endif
5906#ifdef FEAT_WINDOWS
5907 "windows",
5908#endif
5909#ifdef FEAT_WAK
5910 "winaltkeys",
5911#endif
5912#ifdef FEAT_WRITEBACKUP
5913 "writebackup",
5914#endif
5915#ifdef FEAT_XIM
5916 "xim",
5917#endif
5918#ifdef FEAT_XFONTSET
5919 "xfontset",
5920#endif
5921#ifdef FEAT_XPM_W32
5922 "xpm",
5923 "xpm_w32", /* for backward compatibility */
5924#else
5925# if defined(HAVE_XPM)
5926 "xpm",
5927# endif
5928#endif
5929#ifdef USE_XSMP
5930 "xsmp",
5931#endif
5932#ifdef USE_XSMP_INTERACT
5933 "xsmp_interact",
5934#endif
5935#ifdef FEAT_XCLIPBOARD
5936 "xterm_clipboard",
5937#endif
5938#ifdef FEAT_XTERM_SAVE
5939 "xterm_save",
5940#endif
5941#if defined(UNIX) && defined(FEAT_X11)
5942 "X11",
5943#endif
5944 NULL
5945 };
5946
5947 name = get_tv_string(&argvars[0]);
5948 for (i = 0; has_list[i] != NULL; ++i)
5949 if (STRICMP(name, has_list[i]) == 0)
5950 {
5951 n = TRUE;
5952 break;
5953 }
5954
5955 if (n == FALSE)
5956 {
5957 if (STRNICMP(name, "patch", 5) == 0)
5958 {
5959 if (name[5] == '-'
5960 && STRLEN(name) >= 11
5961 && vim_isdigit(name[6])
5962 && vim_isdigit(name[8])
5963 && vim_isdigit(name[10]))
5964 {
5965 int major = atoi((char *)name + 6);
5966 int minor = atoi((char *)name + 8);
5967
5968 /* Expect "patch-9.9.01234". */
5969 n = (major < VIM_VERSION_MAJOR
5970 || (major == VIM_VERSION_MAJOR
5971 && (minor < VIM_VERSION_MINOR
5972 || (minor == VIM_VERSION_MINOR
5973 && has_patch(atoi((char *)name + 10))))));
5974 }
5975 else
5976 n = has_patch(atoi((char *)name + 5));
5977 }
5978 else if (STRICMP(name, "vim_starting") == 0)
5979 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01005980 else if (STRICMP(name, "ttyin") == 0)
5981 n = mch_input_isatty();
5982 else if (STRICMP(name, "ttyout") == 0)
5983 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005984#ifdef FEAT_MBYTE
5985 else if (STRICMP(name, "multi_byte_encoding") == 0)
5986 n = has_mbyte;
5987#endif
5988#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
5989 else if (STRICMP(name, "balloon_multiline") == 0)
5990 n = multiline_balloon_available();
5991#endif
5992#ifdef DYNAMIC_TCL
5993 else if (STRICMP(name, "tcl") == 0)
5994 n = tcl_enabled(FALSE);
5995#endif
5996#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
5997 else if (STRICMP(name, "iconv") == 0)
5998 n = iconv_enabled(FALSE);
5999#endif
6000#ifdef DYNAMIC_LUA
6001 else if (STRICMP(name, "lua") == 0)
6002 n = lua_enabled(FALSE);
6003#endif
6004#ifdef DYNAMIC_MZSCHEME
6005 else if (STRICMP(name, "mzscheme") == 0)
6006 n = mzscheme_enabled(FALSE);
6007#endif
6008#ifdef DYNAMIC_RUBY
6009 else if (STRICMP(name, "ruby") == 0)
6010 n = ruby_enabled(FALSE);
6011#endif
6012#ifdef FEAT_PYTHON
6013#ifdef DYNAMIC_PYTHON
6014 else if (STRICMP(name, "python") == 0)
6015 n = python_enabled(FALSE);
6016#endif
6017#endif
6018#ifdef FEAT_PYTHON3
6019#ifdef DYNAMIC_PYTHON3
6020 else if (STRICMP(name, "python3") == 0)
6021 n = python3_enabled(FALSE);
6022#endif
6023#endif
6024#ifdef DYNAMIC_PERL
6025 else if (STRICMP(name, "perl") == 0)
6026 n = perl_enabled(FALSE);
6027#endif
6028#ifdef FEAT_GUI
6029 else if (STRICMP(name, "gui_running") == 0)
6030 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006031# ifdef FEAT_BROWSE
6032 else if (STRICMP(name, "browse") == 0)
6033 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6034# endif
6035#endif
6036#ifdef FEAT_SYN_HL
6037 else if (STRICMP(name, "syntax_items") == 0)
6038 n = syntax_present(curwin);
6039#endif
6040#if defined(WIN3264)
6041 else if (STRICMP(name, "win95") == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006042 n = FALSE; /* Win9x is no more supported. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006043#endif
6044#ifdef FEAT_NETBEANS_INTG
6045 else if (STRICMP(name, "netbeans_enabled") == 0)
6046 n = netbeans_active();
6047#endif
6048 }
6049
6050 rettv->vval.v_number = n;
6051}
6052
6053/*
6054 * "has_key()" function
6055 */
6056 static void
6057f_has_key(typval_T *argvars, typval_T *rettv)
6058{
6059 if (argvars[0].v_type != VAR_DICT)
6060 {
6061 EMSG(_(e_dictreq));
6062 return;
6063 }
6064 if (argvars[0].vval.v_dict == NULL)
6065 return;
6066
6067 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
6068 get_tv_string(&argvars[1]), -1) != NULL;
6069}
6070
6071/*
6072 * "haslocaldir()" function
6073 */
6074 static void
6075f_haslocaldir(typval_T *argvars, typval_T *rettv)
6076{
6077 win_T *wp = NULL;
6078
6079 wp = find_tabwin(&argvars[0], &argvars[1]);
6080 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6081}
6082
6083/*
6084 * "hasmapto()" function
6085 */
6086 static void
6087f_hasmapto(typval_T *argvars, typval_T *rettv)
6088{
6089 char_u *name;
6090 char_u *mode;
6091 char_u buf[NUMBUFLEN];
6092 int abbr = FALSE;
6093
6094 name = get_tv_string(&argvars[0]);
6095 if (argvars[1].v_type == VAR_UNKNOWN)
6096 mode = (char_u *)"nvo";
6097 else
6098 {
6099 mode = get_tv_string_buf(&argvars[1], buf);
6100 if (argvars[2].v_type != VAR_UNKNOWN)
6101 abbr = (int)get_tv_number(&argvars[2]);
6102 }
6103
6104 if (map_to_exists(name, mode, abbr))
6105 rettv->vval.v_number = TRUE;
6106 else
6107 rettv->vval.v_number = FALSE;
6108}
6109
6110/*
6111 * "histadd()" function
6112 */
6113 static void
6114f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6115{
6116#ifdef FEAT_CMDHIST
6117 int histype;
6118 char_u *str;
6119 char_u buf[NUMBUFLEN];
6120#endif
6121
6122 rettv->vval.v_number = FALSE;
6123 if (check_restricted() || check_secure())
6124 return;
6125#ifdef FEAT_CMDHIST
6126 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6127 histype = str != NULL ? get_histtype(str) : -1;
6128 if (histype >= 0)
6129 {
6130 str = get_tv_string_buf(&argvars[1], buf);
6131 if (*str != NUL)
6132 {
6133 init_history();
6134 add_to_history(histype, str, FALSE, NUL);
6135 rettv->vval.v_number = TRUE;
6136 return;
6137 }
6138 }
6139#endif
6140}
6141
6142/*
6143 * "histdel()" function
6144 */
6145 static void
6146f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6147{
6148#ifdef FEAT_CMDHIST
6149 int n;
6150 char_u buf[NUMBUFLEN];
6151 char_u *str;
6152
6153 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6154 if (str == NULL)
6155 n = 0;
6156 else if (argvars[1].v_type == VAR_UNKNOWN)
6157 /* only one argument: clear entire history */
6158 n = clr_history(get_histtype(str));
6159 else if (argvars[1].v_type == VAR_NUMBER)
6160 /* index given: remove that entry */
6161 n = del_history_idx(get_histtype(str),
6162 (int)get_tv_number(&argvars[1]));
6163 else
6164 /* string given: remove all matching entries */
6165 n = del_history_entry(get_histtype(str),
6166 get_tv_string_buf(&argvars[1], buf));
6167 rettv->vval.v_number = n;
6168#endif
6169}
6170
6171/*
6172 * "histget()" function
6173 */
6174 static void
6175f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6176{
6177#ifdef FEAT_CMDHIST
6178 int type;
6179 int idx;
6180 char_u *str;
6181
6182 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6183 if (str == NULL)
6184 rettv->vval.v_string = NULL;
6185 else
6186 {
6187 type = get_histtype(str);
6188 if (argvars[1].v_type == VAR_UNKNOWN)
6189 idx = get_history_idx(type);
6190 else
6191 idx = (int)get_tv_number_chk(&argvars[1], NULL);
6192 /* -1 on type error */
6193 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6194 }
6195#else
6196 rettv->vval.v_string = NULL;
6197#endif
6198 rettv->v_type = VAR_STRING;
6199}
6200
6201/*
6202 * "histnr()" function
6203 */
6204 static void
6205f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6206{
6207 int i;
6208
6209#ifdef FEAT_CMDHIST
6210 char_u *history = get_tv_string_chk(&argvars[0]);
6211
6212 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6213 if (i >= HIST_CMD && i < HIST_COUNT)
6214 i = get_history_idx(i);
6215 else
6216#endif
6217 i = -1;
6218 rettv->vval.v_number = i;
6219}
6220
6221/*
6222 * "highlightID(name)" function
6223 */
6224 static void
6225f_hlID(typval_T *argvars, typval_T *rettv)
6226{
6227 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
6228}
6229
6230/*
6231 * "highlight_exists()" function
6232 */
6233 static void
6234f_hlexists(typval_T *argvars, typval_T *rettv)
6235{
6236 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
6237}
6238
6239/*
6240 * "hostname()" function
6241 */
6242 static void
6243f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6244{
6245 char_u hostname[256];
6246
6247 mch_get_host_name(hostname, 256);
6248 rettv->v_type = VAR_STRING;
6249 rettv->vval.v_string = vim_strsave(hostname);
6250}
6251
6252/*
6253 * iconv() function
6254 */
6255 static void
6256f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6257{
6258#ifdef FEAT_MBYTE
6259 char_u buf1[NUMBUFLEN];
6260 char_u buf2[NUMBUFLEN];
6261 char_u *from, *to, *str;
6262 vimconv_T vimconv;
6263#endif
6264
6265 rettv->v_type = VAR_STRING;
6266 rettv->vval.v_string = NULL;
6267
6268#ifdef FEAT_MBYTE
6269 str = get_tv_string(&argvars[0]);
6270 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
6271 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
6272 vimconv.vc_type = CONV_NONE;
6273 convert_setup(&vimconv, from, to);
6274
6275 /* If the encodings are equal, no conversion needed. */
6276 if (vimconv.vc_type == CONV_NONE)
6277 rettv->vval.v_string = vim_strsave(str);
6278 else
6279 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6280
6281 convert_setup(&vimconv, NULL, NULL);
6282 vim_free(from);
6283 vim_free(to);
6284#endif
6285}
6286
6287/*
6288 * "indent()" function
6289 */
6290 static void
6291f_indent(typval_T *argvars, typval_T *rettv)
6292{
6293 linenr_T lnum;
6294
6295 lnum = get_tv_lnum(argvars);
6296 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6297 rettv->vval.v_number = get_indent_lnum(lnum);
6298 else
6299 rettv->vval.v_number = -1;
6300}
6301
6302/*
6303 * "index()" function
6304 */
6305 static void
6306f_index(typval_T *argvars, typval_T *rettv)
6307{
6308 list_T *l;
6309 listitem_T *item;
6310 long idx = 0;
6311 int ic = FALSE;
6312
6313 rettv->vval.v_number = -1;
6314 if (argvars[0].v_type != VAR_LIST)
6315 {
6316 EMSG(_(e_listreq));
6317 return;
6318 }
6319 l = argvars[0].vval.v_list;
6320 if (l != NULL)
6321 {
6322 item = l->lv_first;
6323 if (argvars[2].v_type != VAR_UNKNOWN)
6324 {
6325 int error = FALSE;
6326
6327 /* Start at specified item. Use the cached index that list_find()
6328 * sets, so that a negative number also works. */
6329 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
6330 idx = l->lv_idx;
6331 if (argvars[3].v_type != VAR_UNKNOWN)
6332 ic = (int)get_tv_number_chk(&argvars[3], &error);
6333 if (error)
6334 item = NULL;
6335 }
6336
6337 for ( ; item != NULL; item = item->li_next, ++idx)
6338 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6339 {
6340 rettv->vval.v_number = idx;
6341 break;
6342 }
6343 }
6344}
6345
6346static int inputsecret_flag = 0;
6347
6348/*
6349 * "input()" function
6350 * Also handles inputsecret() when inputsecret is set.
6351 */
6352 static void
6353f_input(typval_T *argvars, typval_T *rettv)
6354{
6355 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6356}
6357
6358/*
6359 * "inputdialog()" function
6360 */
6361 static void
6362f_inputdialog(typval_T *argvars, typval_T *rettv)
6363{
6364#if defined(FEAT_GUI_TEXTDIALOG)
6365 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
6366 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6367 {
6368 char_u *message;
6369 char_u buf[NUMBUFLEN];
6370 char_u *defstr = (char_u *)"";
6371
6372 message = get_tv_string_chk(&argvars[0]);
6373 if (argvars[1].v_type != VAR_UNKNOWN
6374 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
6375 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6376 else
6377 IObuff[0] = NUL;
6378 if (message != NULL && defstr != NULL
6379 && do_dialog(VIM_QUESTION, NULL, message,
6380 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6381 rettv->vval.v_string = vim_strsave(IObuff);
6382 else
6383 {
6384 if (message != NULL && defstr != NULL
6385 && argvars[1].v_type != VAR_UNKNOWN
6386 && argvars[2].v_type != VAR_UNKNOWN)
6387 rettv->vval.v_string = vim_strsave(
6388 get_tv_string_buf(&argvars[2], buf));
6389 else
6390 rettv->vval.v_string = NULL;
6391 }
6392 rettv->v_type = VAR_STRING;
6393 }
6394 else
6395#endif
6396 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6397}
6398
6399/*
6400 * "inputlist()" function
6401 */
6402 static void
6403f_inputlist(typval_T *argvars, typval_T *rettv)
6404{
6405 listitem_T *li;
6406 int selected;
6407 int mouse_used;
6408
6409#ifdef NO_CONSOLE_INPUT
6410 /* While starting up, there is no place to enter text. */
6411 if (no_console_input())
6412 return;
6413#endif
6414 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6415 {
6416 EMSG2(_(e_listarg), "inputlist()");
6417 return;
6418 }
6419
6420 msg_start();
6421 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
6422 lines_left = Rows; /* avoid more prompt */
6423 msg_scroll = TRUE;
6424 msg_clr_eos();
6425
6426 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
6427 {
6428 msg_puts(get_tv_string(&li->li_tv));
6429 msg_putchar('\n');
6430 }
6431
6432 /* Ask for choice. */
6433 selected = prompt_for_number(&mouse_used);
6434 if (mouse_used)
6435 selected -= lines_left;
6436
6437 rettv->vval.v_number = selected;
6438}
6439
6440
6441static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6442
6443/*
6444 * "inputrestore()" function
6445 */
6446 static void
6447f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6448{
6449 if (ga_userinput.ga_len > 0)
6450 {
6451 --ga_userinput.ga_len;
6452 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
6453 + ga_userinput.ga_len);
6454 /* default return is zero == OK */
6455 }
6456 else if (p_verbose > 1)
6457 {
6458 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
6459 rettv->vval.v_number = 1; /* Failed */
6460 }
6461}
6462
6463/*
6464 * "inputsave()" function
6465 */
6466 static void
6467f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
6468{
6469 /* Add an entry to the stack of typeahead storage. */
6470 if (ga_grow(&ga_userinput, 1) == OK)
6471 {
6472 save_typeahead((tasave_T *)(ga_userinput.ga_data)
6473 + ga_userinput.ga_len);
6474 ++ga_userinput.ga_len;
6475 /* default return is zero == OK */
6476 }
6477 else
6478 rettv->vval.v_number = 1; /* Failed */
6479}
6480
6481/*
6482 * "inputsecret()" function
6483 */
6484 static void
6485f_inputsecret(typval_T *argvars, typval_T *rettv)
6486{
6487 ++cmdline_star;
6488 ++inputsecret_flag;
6489 f_input(argvars, rettv);
6490 --cmdline_star;
6491 --inputsecret_flag;
6492}
6493
6494/*
6495 * "insert()" function
6496 */
6497 static void
6498f_insert(typval_T *argvars, typval_T *rettv)
6499{
6500 long before = 0;
6501 listitem_T *item;
6502 list_T *l;
6503 int error = FALSE;
6504
6505 if (argvars[0].v_type != VAR_LIST)
6506 EMSG2(_(e_listarg), "insert()");
6507 else if ((l = argvars[0].vval.v_list) != NULL
6508 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
6509 {
6510 if (argvars[2].v_type != VAR_UNKNOWN)
6511 before = (long)get_tv_number_chk(&argvars[2], &error);
6512 if (error)
6513 return; /* type error; errmsg already given */
6514
6515 if (before == l->lv_len)
6516 item = NULL;
6517 else
6518 {
6519 item = list_find(l, before);
6520 if (item == NULL)
6521 {
6522 EMSGN(_(e_listidx), before);
6523 l = NULL;
6524 }
6525 }
6526 if (l != NULL)
6527 {
6528 list_insert_tv(l, &argvars[1], item);
6529 copy_tv(&argvars[0], rettv);
6530 }
6531 }
6532}
6533
6534/*
6535 * "invert(expr)" function
6536 */
6537 static void
6538f_invert(typval_T *argvars, typval_T *rettv)
6539{
6540 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
6541}
6542
6543/*
6544 * "isdirectory()" function
6545 */
6546 static void
6547f_isdirectory(typval_T *argvars, typval_T *rettv)
6548{
6549 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
6550}
6551
6552/*
6553 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
6554 * or it refers to a List or Dictionary that is locked.
6555 */
6556 static int
6557tv_islocked(typval_T *tv)
6558{
6559 return (tv->v_lock & VAR_LOCKED)
6560 || (tv->v_type == VAR_LIST
6561 && tv->vval.v_list != NULL
6562 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
6563 || (tv->v_type == VAR_DICT
6564 && tv->vval.v_dict != NULL
6565 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
6566}
6567
6568/*
6569 * "islocked()" function
6570 */
6571 static void
6572f_islocked(typval_T *argvars, typval_T *rettv)
6573{
6574 lval_T lv;
6575 char_u *end;
6576 dictitem_T *di;
6577
6578 rettv->vval.v_number = -1;
6579 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
6580 GLV_NO_AUTOLOAD, FNE_CHECK_START);
6581 if (end != NULL && lv.ll_name != NULL)
6582 {
6583 if (*end != NUL)
6584 EMSG(_(e_trailing));
6585 else
6586 {
6587 if (lv.ll_tv == NULL)
6588 {
6589 if (check_changedtick(lv.ll_name))
6590 rettv->vval.v_number = 1; /* always locked */
6591 else
6592 {
6593 di = find_var(lv.ll_name, NULL, TRUE);
6594 if (di != NULL)
6595 {
6596 /* Consider a variable locked when:
6597 * 1. the variable itself is locked
6598 * 2. the value of the variable is locked.
6599 * 3. the List or Dict value is locked.
6600 */
6601 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
6602 || tv_islocked(&di->di_tv));
6603 }
6604 }
6605 }
6606 else if (lv.ll_range)
6607 EMSG(_("E786: Range not allowed"));
6608 else if (lv.ll_newkey != NULL)
6609 EMSG2(_(e_dictkey), lv.ll_newkey);
6610 else if (lv.ll_list != NULL)
6611 /* List item. */
6612 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
6613 else
6614 /* Dictionary item. */
6615 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
6616 }
6617 }
6618
6619 clear_lval(&lv);
6620}
6621
6622#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
6623/*
6624 * "isnan()" function
6625 */
6626 static void
6627f_isnan(typval_T *argvars, typval_T *rettv)
6628{
6629 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
6630 && isnan(argvars[0].vval.v_float);
6631}
6632#endif
6633
6634/*
6635 * "items(dict)" function
6636 */
6637 static void
6638f_items(typval_T *argvars, typval_T *rettv)
6639{
6640 dict_list(argvars, rettv, 2);
6641}
6642
6643#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
6644/*
6645 * Get the job from the argument.
6646 * Returns NULL if the job is invalid.
6647 */
6648 static job_T *
6649get_job_arg(typval_T *tv)
6650{
6651 job_T *job;
6652
6653 if (tv->v_type != VAR_JOB)
6654 {
6655 EMSG2(_(e_invarg2), get_tv_string(tv));
6656 return NULL;
6657 }
6658 job = tv->vval.v_job;
6659
6660 if (job == NULL)
6661 EMSG(_("E916: not a valid job"));
6662 return job;
6663}
6664
6665/*
6666 * "job_getchannel()" function
6667 */
6668 static void
6669f_job_getchannel(typval_T *argvars, typval_T *rettv)
6670{
6671 job_T *job = get_job_arg(&argvars[0]);
6672
6673 if (job != NULL)
6674 {
6675 rettv->v_type = VAR_CHANNEL;
6676 rettv->vval.v_channel = job->jv_channel;
6677 if (job->jv_channel != NULL)
6678 ++job->jv_channel->ch_refcount;
6679 }
6680}
6681
6682/*
6683 * "job_info()" function
6684 */
6685 static void
6686f_job_info(typval_T *argvars, typval_T *rettv)
6687{
6688 job_T *job = get_job_arg(&argvars[0]);
6689
6690 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
6691 job_info(job, rettv->vval.v_dict);
6692}
6693
6694/*
6695 * "job_setoptions()" function
6696 */
6697 static void
6698f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
6699{
6700 job_T *job = get_job_arg(&argvars[0]);
6701 jobopt_T opt;
6702
6703 if (job == NULL)
6704 return;
6705 clear_job_options(&opt);
6706 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == OK)
6707 job_set_options(job, &opt);
6708 free_job_options(&opt);
6709}
6710
6711/*
6712 * "job_start()" function
6713 */
6714 static void
6715f_job_start(typval_T *argvars, typval_T *rettv)
6716{
6717 rettv->v_type = VAR_JOB;
6718 if (check_restricted() || check_secure())
6719 return;
6720 rettv->vval.v_job = job_start(argvars);
6721}
6722
6723/*
6724 * "job_status()" function
6725 */
6726 static void
6727f_job_status(typval_T *argvars, typval_T *rettv)
6728{
6729 job_T *job = get_job_arg(&argvars[0]);
6730
6731 if (job != NULL)
6732 {
6733 rettv->v_type = VAR_STRING;
6734 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
6735 }
6736}
6737
6738/*
6739 * "job_stop()" function
6740 */
6741 static void
6742f_job_stop(typval_T *argvars, typval_T *rettv)
6743{
6744 job_T *job = get_job_arg(&argvars[0]);
6745
6746 if (job != NULL)
6747 rettv->vval.v_number = job_stop(job, argvars);
6748}
6749#endif
6750
6751/*
6752 * "join()" function
6753 */
6754 static void
6755f_join(typval_T *argvars, typval_T *rettv)
6756{
6757 garray_T ga;
6758 char_u *sep;
6759
6760 if (argvars[0].v_type != VAR_LIST)
6761 {
6762 EMSG(_(e_listreq));
6763 return;
6764 }
6765 if (argvars[0].vval.v_list == NULL)
6766 return;
6767 if (argvars[1].v_type == VAR_UNKNOWN)
6768 sep = (char_u *)" ";
6769 else
6770 sep = get_tv_string_chk(&argvars[1]);
6771
6772 rettv->v_type = VAR_STRING;
6773
6774 if (sep != NULL)
6775 {
6776 ga_init2(&ga, (int)sizeof(char), 80);
6777 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
6778 ga_append(&ga, NUL);
6779 rettv->vval.v_string = (char_u *)ga.ga_data;
6780 }
6781 else
6782 rettv->vval.v_string = NULL;
6783}
6784
6785/*
6786 * "js_decode()" function
6787 */
6788 static void
6789f_js_decode(typval_T *argvars, typval_T *rettv)
6790{
6791 js_read_T reader;
6792
6793 reader.js_buf = get_tv_string(&argvars[0]);
6794 reader.js_fill = NULL;
6795 reader.js_used = 0;
6796 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
6797 EMSG(_(e_invarg));
6798}
6799
6800/*
6801 * "js_encode()" function
6802 */
6803 static void
6804f_js_encode(typval_T *argvars, typval_T *rettv)
6805{
6806 rettv->v_type = VAR_STRING;
6807 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
6808}
6809
6810/*
6811 * "json_decode()" function
6812 */
6813 static void
6814f_json_decode(typval_T *argvars, typval_T *rettv)
6815{
6816 js_read_T reader;
6817
6818 reader.js_buf = get_tv_string(&argvars[0]);
6819 reader.js_fill = NULL;
6820 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01006821 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006822}
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). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009511 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
9512 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009513 if (pat2 == NULL || pat3 == NULL)
9514 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009515 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009516 if (*mpat == NUL)
9517 STRCPY(pat3, pat2);
9518 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +01009519 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009520 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;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011819 int err = FALSE;
11820 FILE *fd;
11821 list_T *list = NULL;
11822 int flags = SHELL_SILENT;
11823
11824 rettv->v_type = VAR_STRING;
11825 rettv->vval.v_string = NULL;
11826 if (check_restricted() || check_secure())
11827 goto errret;
11828
11829 if (argvars[1].v_type != VAR_UNKNOWN)
11830 {
11831 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010011832 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011833 * command.
11834 */
11835 if ((infile = vim_tempname('i', TRUE)) == NULL)
11836 {
11837 EMSG(_(e_notmp));
11838 goto errret;
11839 }
11840
11841 fd = mch_fopen((char *)infile, WRITEBIN);
11842 if (fd == NULL)
11843 {
11844 EMSG2(_(e_notopen), infile);
11845 goto errret;
11846 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010011847 if (argvars[1].v_type == VAR_NUMBER)
11848 {
11849 linenr_T lnum;
11850 buf_T *buf;
11851
11852 buf = buflist_findnr(argvars[1].vval.v_number);
11853 if (buf == NULL)
11854 {
11855 EMSGN(_(e_nobufnr), argvars[1].vval.v_number);
11856 goto errret;
11857 }
11858
11859 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
11860 {
11861 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
11862 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
11863 {
11864 err = TRUE;
11865 break;
11866 }
11867 if (putc(NL, fd) == EOF)
11868 {
11869 err = TRUE;
11870 break;
11871 }
11872 }
11873 }
11874 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011875 {
11876 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
11877 err = TRUE;
11878 }
11879 else
11880 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010011881 size_t len;
11882 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011883
11884 p = get_tv_string_buf_chk(&argvars[1], buf);
11885 if (p == NULL)
11886 {
11887 fclose(fd);
11888 goto errret; /* type error; errmsg already given */
11889 }
11890 len = STRLEN(p);
11891 if (len > 0 && fwrite(p, len, 1, fd) != 1)
11892 err = TRUE;
11893 }
11894 if (fclose(fd) != 0)
11895 err = TRUE;
11896 if (err)
11897 {
11898 EMSG(_("E677: Error writing temp file"));
11899 goto errret;
11900 }
11901 }
11902
11903 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
11904 * echoes typeahead, that messes up the display. */
11905 if (!msg_silent)
11906 flags += SHELL_COOKED;
11907
11908 if (retlist)
11909 {
11910 int len;
11911 listitem_T *li;
11912 char_u *s = NULL;
11913 char_u *start;
11914 char_u *end;
11915 int i;
11916
11917 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
11918 if (res == NULL)
11919 goto errret;
11920
11921 list = list_alloc();
11922 if (list == NULL)
11923 goto errret;
11924
11925 for (i = 0; i < len; ++i)
11926 {
11927 start = res + i;
11928 while (i < len && res[i] != NL)
11929 ++i;
11930 end = res + i;
11931
11932 s = alloc((unsigned)(end - start + 1));
11933 if (s == NULL)
11934 goto errret;
11935
11936 for (p = s; start < end; ++p, ++start)
11937 *p = *start == NUL ? NL : *start;
11938 *p = NUL;
11939
11940 li = listitem_alloc();
11941 if (li == NULL)
11942 {
11943 vim_free(s);
11944 goto errret;
11945 }
11946 li->li_tv.v_type = VAR_STRING;
11947 li->li_tv.v_lock = 0;
11948 li->li_tv.vval.v_string = s;
11949 list_append(list, li);
11950 }
11951
11952 ++list->lv_refcount;
11953 rettv->v_type = VAR_LIST;
11954 rettv->vval.v_list = list;
11955 list = NULL;
11956 }
11957 else
11958 {
11959 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
11960#ifdef USE_CR
11961 /* translate <CR> into <NL> */
11962 if (res != NULL)
11963 {
11964 char_u *s;
11965
11966 for (s = res; *s; ++s)
11967 {
11968 if (*s == CAR)
11969 *s = NL;
11970 }
11971 }
11972#else
11973# ifdef USE_CRNL
11974 /* translate <CR><NL> into <NL> */
11975 if (res != NULL)
11976 {
11977 char_u *s, *d;
11978
11979 d = res;
11980 for (s = res; *s; ++s)
11981 {
11982 if (s[0] == CAR && s[1] == NL)
11983 ++s;
11984 *d++ = *s;
11985 }
11986 *d = NUL;
11987 }
11988# endif
11989#endif
11990 rettv->vval.v_string = res;
11991 res = NULL;
11992 }
11993
11994errret:
11995 if (infile != NULL)
11996 {
11997 mch_remove(infile);
11998 vim_free(infile);
11999 }
12000 if (res != NULL)
12001 vim_free(res);
12002 if (list != NULL)
12003 list_free(list);
12004}
12005
12006/*
12007 * "system()" function
12008 */
12009 static void
12010f_system(typval_T *argvars, typval_T *rettv)
12011{
12012 get_cmd_output_as_rettv(argvars, rettv, FALSE);
12013}
12014
12015/*
12016 * "systemlist()" function
12017 */
12018 static void
12019f_systemlist(typval_T *argvars, typval_T *rettv)
12020{
12021 get_cmd_output_as_rettv(argvars, rettv, TRUE);
12022}
12023
12024/*
12025 * "tabpagebuflist()" function
12026 */
12027 static void
12028f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12029{
12030#ifdef FEAT_WINDOWS
12031 tabpage_T *tp;
12032 win_T *wp = NULL;
12033
12034 if (argvars[0].v_type == VAR_UNKNOWN)
12035 wp = firstwin;
12036 else
12037 {
12038 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12039 if (tp != NULL)
12040 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12041 }
12042 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
12043 {
12044 for (; wp != NULL; wp = wp->w_next)
12045 if (list_append_number(rettv->vval.v_list,
12046 wp->w_buffer->b_fnum) == FAIL)
12047 break;
12048 }
12049#endif
12050}
12051
12052
12053/*
12054 * "tabpagenr()" function
12055 */
12056 static void
12057f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
12058{
12059 int nr = 1;
12060#ifdef FEAT_WINDOWS
12061 char_u *arg;
12062
12063 if (argvars[0].v_type != VAR_UNKNOWN)
12064 {
12065 arg = get_tv_string_chk(&argvars[0]);
12066 nr = 0;
12067 if (arg != NULL)
12068 {
12069 if (STRCMP(arg, "$") == 0)
12070 nr = tabpage_index(NULL) - 1;
12071 else
12072 EMSG2(_(e_invexpr2), arg);
12073 }
12074 }
12075 else
12076 nr = tabpage_index(curtab);
12077#endif
12078 rettv->vval.v_number = nr;
12079}
12080
12081
12082#ifdef FEAT_WINDOWS
12083static int get_winnr(tabpage_T *tp, typval_T *argvar);
12084
12085/*
12086 * Common code for tabpagewinnr() and winnr().
12087 */
12088 static int
12089get_winnr(tabpage_T *tp, typval_T *argvar)
12090{
12091 win_T *twin;
12092 int nr = 1;
12093 win_T *wp;
12094 char_u *arg;
12095
12096 twin = (tp == curtab) ? curwin : tp->tp_curwin;
12097 if (argvar->v_type != VAR_UNKNOWN)
12098 {
12099 arg = get_tv_string_chk(argvar);
12100 if (arg == NULL)
12101 nr = 0; /* type error; errmsg already given */
12102 else if (STRCMP(arg, "$") == 0)
12103 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
12104 else if (STRCMP(arg, "#") == 0)
12105 {
12106 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
12107 if (twin == NULL)
12108 nr = 0;
12109 }
12110 else
12111 {
12112 EMSG2(_(e_invexpr2), arg);
12113 nr = 0;
12114 }
12115 }
12116
12117 if (nr > 0)
12118 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12119 wp != twin; wp = wp->w_next)
12120 {
12121 if (wp == NULL)
12122 {
12123 /* didn't find it in this tabpage */
12124 nr = 0;
12125 break;
12126 }
12127 ++nr;
12128 }
12129 return nr;
12130}
12131#endif
12132
12133/*
12134 * "tabpagewinnr()" function
12135 */
12136 static void
12137f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
12138{
12139 int nr = 1;
12140#ifdef FEAT_WINDOWS
12141 tabpage_T *tp;
12142
12143 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12144 if (tp == NULL)
12145 nr = 0;
12146 else
12147 nr = get_winnr(tp, &argvars[1]);
12148#endif
12149 rettv->vval.v_number = nr;
12150}
12151
12152
12153/*
12154 * "tagfiles()" function
12155 */
12156 static void
12157f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
12158{
12159 char_u *fname;
12160 tagname_T tn;
12161 int first;
12162
12163 if (rettv_list_alloc(rettv) == FAIL)
12164 return;
12165 fname = alloc(MAXPATHL);
12166 if (fname == NULL)
12167 return;
12168
12169 for (first = TRUE; ; first = FALSE)
12170 if (get_tagfname(&tn, first, fname) == FAIL
12171 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
12172 break;
12173 tagname_free(&tn);
12174 vim_free(fname);
12175}
12176
12177/*
12178 * "taglist()" function
12179 */
12180 static void
12181f_taglist(typval_T *argvars, typval_T *rettv)
12182{
12183 char_u *tag_pattern;
12184
12185 tag_pattern = get_tv_string(&argvars[0]);
12186
12187 rettv->vval.v_number = FALSE;
12188 if (*tag_pattern == NUL)
12189 return;
12190
12191 if (rettv_list_alloc(rettv) == OK)
12192 (void)get_tags(rettv->vval.v_list, tag_pattern);
12193}
12194
12195/*
12196 * "tempname()" function
12197 */
12198 static void
12199f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
12200{
12201 static int x = 'A';
12202
12203 rettv->v_type = VAR_STRING;
12204 rettv->vval.v_string = vim_tempname(x, FALSE);
12205
12206 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
12207 * names. Skip 'I' and 'O', they are used for shell redirection. */
12208 do
12209 {
12210 if (x == 'Z')
12211 x = '0';
12212 else if (x == '9')
12213 x = 'A';
12214 else
12215 {
12216#ifdef EBCDIC
12217 if (x == 'I')
12218 x = 'J';
12219 else if (x == 'R')
12220 x = 'S';
12221 else
12222#endif
12223 ++x;
12224 }
12225 } while (x == 'I' || x == 'O');
12226}
12227
12228#ifdef FEAT_FLOAT
12229/*
12230 * "tan()" function
12231 */
12232 static void
12233f_tan(typval_T *argvars, typval_T *rettv)
12234{
12235 float_T f = 0.0;
12236
12237 rettv->v_type = VAR_FLOAT;
12238 if (get_float_arg(argvars, &f) == OK)
12239 rettv->vval.v_float = tan(f);
12240 else
12241 rettv->vval.v_float = 0.0;
12242}
12243
12244/*
12245 * "tanh()" function
12246 */
12247 static void
12248f_tanh(typval_T *argvars, typval_T *rettv)
12249{
12250 float_T f = 0.0;
12251
12252 rettv->v_type = VAR_FLOAT;
12253 if (get_float_arg(argvars, &f) == OK)
12254 rettv->vval.v_float = tanh(f);
12255 else
12256 rettv->vval.v_float = 0.0;
12257}
12258#endif
12259
12260/*
12261 * "test_alloc_fail(id, countdown, repeat)" function
12262 */
12263 static void
12264f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
12265{
12266 if (argvars[0].v_type != VAR_NUMBER
12267 || argvars[0].vval.v_number <= 0
12268 || argvars[1].v_type != VAR_NUMBER
12269 || argvars[1].vval.v_number < 0
12270 || argvars[2].v_type != VAR_NUMBER)
12271 EMSG(_(e_invarg));
12272 else
12273 {
12274 alloc_fail_id = argvars[0].vval.v_number;
12275 if (alloc_fail_id >= aid_last)
12276 EMSG(_(e_invarg));
12277 alloc_fail_countdown = argvars[1].vval.v_number;
12278 alloc_fail_repeat = argvars[2].vval.v_number;
12279 did_outofmem_msg = FALSE;
12280 }
12281}
12282
12283/*
12284 * "test_autochdir()"
12285 */
12286 static void
12287f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12288{
12289#if defined(FEAT_AUTOCHDIR)
12290 test_autochdir = TRUE;
12291#endif
12292}
12293
12294/*
12295 * "test_disable_char_avail({expr})" function
12296 */
12297 static void
12298f_test_disable_char_avail(typval_T *argvars, typval_T *rettv UNUSED)
12299{
12300 disable_char_avail_for_testing = (int)get_tv_number(&argvars[0]);
12301}
12302
12303/*
12304 * "test_garbagecollect_now()" function
12305 */
12306 static void
12307f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12308{
12309 /* This is dangerous, any Lists and Dicts used internally may be freed
12310 * while still in use. */
12311 garbage_collect(TRUE);
12312}
12313
12314#ifdef FEAT_JOB_CHANNEL
12315 static void
12316f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
12317{
12318 rettv->v_type = VAR_CHANNEL;
12319 rettv->vval.v_channel = NULL;
12320}
12321#endif
12322
12323 static void
12324f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
12325{
12326 rettv->v_type = VAR_DICT;
12327 rettv->vval.v_dict = NULL;
12328}
12329
12330#ifdef FEAT_JOB_CHANNEL
12331 static void
12332f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
12333{
12334 rettv->v_type = VAR_JOB;
12335 rettv->vval.v_job = NULL;
12336}
12337#endif
12338
12339 static void
12340f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
12341{
12342 rettv->v_type = VAR_LIST;
12343 rettv->vval.v_list = NULL;
12344}
12345
12346 static void
12347f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
12348{
12349 rettv->v_type = VAR_PARTIAL;
12350 rettv->vval.v_partial = NULL;
12351}
12352
12353 static void
12354f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
12355{
12356 rettv->v_type = VAR_STRING;
12357 rettv->vval.v_string = NULL;
12358}
12359
12360 static void
12361f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
12362{
12363 time_for_testing = (time_t)get_tv_number(&argvars[0]);
12364}
12365
12366#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
12367/*
12368 * Get a callback from "arg". It can be a Funcref or a function name.
12369 * When "arg" is zero return an empty string.
12370 * Return NULL for an invalid argument.
12371 */
12372 char_u *
12373get_callback(typval_T *arg, partial_T **pp)
12374{
12375 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
12376 {
12377 *pp = arg->vval.v_partial;
12378 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012379 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012380 }
12381 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012382 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012383 {
12384 func_ref(arg->vval.v_string);
12385 return arg->vval.v_string;
12386 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012387 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
12388 return (char_u *)"";
12389 EMSG(_("E921: Invalid callback argument"));
12390 return NULL;
12391}
12392
12393/*
12394 * Unref/free "callback" and "partial" retured by get_callback().
12395 */
12396 void
12397free_callback(char_u *callback, partial_T *partial)
12398{
12399 if (partial != NULL)
12400 partial_unref(partial);
12401 else if (callback != NULL)
12402 {
12403 func_unref(callback);
12404 vim_free(callback);
12405 }
12406}
12407#endif
12408
12409#ifdef FEAT_TIMERS
12410/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020012411 * "timer_info([timer])" function
12412 */
12413 static void
12414f_timer_info(typval_T *argvars, typval_T *rettv)
12415{
12416 timer_T *timer = NULL;
12417
12418 if (rettv_list_alloc(rettv) != OK)
12419 return;
12420 if (argvars[0].v_type != VAR_UNKNOWN)
12421 {
12422 if (argvars[0].v_type != VAR_NUMBER)
12423 EMSG(_(e_number_exp));
12424 else
12425 {
12426 timer = find_timer((int)get_tv_number(&argvars[0]));
12427 if (timer != NULL)
12428 add_timer_info(rettv, timer);
12429 }
12430 }
12431 else
12432 add_timer_info_all(rettv);
12433}
12434
12435/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012436 * "timer_pause(timer, paused)" function
12437 */
12438 static void
12439f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
12440{
12441 timer_T *timer = NULL;
12442 int paused = (int)get_tv_number(&argvars[1]);
12443
12444 if (argvars[0].v_type != VAR_NUMBER)
12445 EMSG(_(e_number_exp));
12446 else
12447 {
12448 timer = find_timer((int)get_tv_number(&argvars[0]));
12449 if (timer != NULL)
12450 timer->tr_paused = paused;
12451 }
12452}
12453
12454/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012455 * "timer_start(time, callback [, options])" function
12456 */
12457 static void
12458f_timer_start(typval_T *argvars, typval_T *rettv)
12459{
Bram Moolenaar75537a92016-09-05 22:45:28 +020012460 long msec = (long)get_tv_number(&argvars[0]);
12461 timer_T *timer;
12462 int repeat = 0;
12463 char_u *callback;
12464 dict_T *dict;
12465 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012466
Bram Moolenaar75537a92016-09-05 22:45:28 +020012467 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012468 if (check_secure())
12469 return;
12470 if (argvars[2].v_type != VAR_UNKNOWN)
12471 {
12472 if (argvars[2].v_type != VAR_DICT
12473 || (dict = argvars[2].vval.v_dict) == NULL)
12474 {
12475 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
12476 return;
12477 }
12478 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
12479 repeat = get_dict_number(dict, (char_u *)"repeat");
12480 }
12481
Bram Moolenaar75537a92016-09-05 22:45:28 +020012482 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012483 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020012484 return;
12485
12486 timer = create_timer(msec, repeat);
12487 if (timer == NULL)
12488 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012489 else
12490 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020012491 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020012492 timer->tr_callback = vim_strsave(callback);
12493 else
12494 /* pointer into the partial */
12495 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020012496 timer->tr_partial = partial;
12497 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012498 }
12499}
12500
12501/*
12502 * "timer_stop(timer)" function
12503 */
12504 static void
12505f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
12506{
12507 timer_T *timer;
12508
12509 if (argvars[0].v_type != VAR_NUMBER)
12510 {
12511 EMSG(_(e_number_exp));
12512 return;
12513 }
12514 timer = find_timer((int)get_tv_number(&argvars[0]));
12515 if (timer != NULL)
12516 stop_timer(timer);
12517}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020012518
12519/*
12520 * "timer_stopall()" function
12521 */
12522 static void
12523f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12524{
12525 stop_all_timers();
12526}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012527#endif
12528
12529/*
12530 * "tolower(string)" function
12531 */
12532 static void
12533f_tolower(typval_T *argvars, typval_T *rettv)
12534{
12535 char_u *p;
12536
12537 p = vim_strsave(get_tv_string(&argvars[0]));
12538 rettv->v_type = VAR_STRING;
12539 rettv->vval.v_string = p;
12540
12541 if (p != NULL)
12542 while (*p != NUL)
12543 {
12544#ifdef FEAT_MBYTE
12545 int l;
12546
12547 if (enc_utf8)
12548 {
12549 int c, lc;
12550
12551 c = utf_ptr2char(p);
12552 lc = utf_tolower(c);
12553 l = utf_ptr2len(p);
12554 /* TODO: reallocate string when byte count changes. */
12555 if (utf_char2len(lc) == l)
12556 utf_char2bytes(lc, p);
12557 p += l;
12558 }
12559 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
12560 p += l; /* skip multi-byte character */
12561 else
12562#endif
12563 {
12564 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
12565 ++p;
12566 }
12567 }
12568}
12569
12570/*
12571 * "toupper(string)" function
12572 */
12573 static void
12574f_toupper(typval_T *argvars, typval_T *rettv)
12575{
12576 rettv->v_type = VAR_STRING;
12577 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
12578}
12579
12580/*
12581 * "tr(string, fromstr, tostr)" function
12582 */
12583 static void
12584f_tr(typval_T *argvars, typval_T *rettv)
12585{
12586 char_u *in_str;
12587 char_u *fromstr;
12588 char_u *tostr;
12589 char_u *p;
12590#ifdef FEAT_MBYTE
12591 int inlen;
12592 int fromlen;
12593 int tolen;
12594 int idx;
12595 char_u *cpstr;
12596 int cplen;
12597 int first = TRUE;
12598#endif
12599 char_u buf[NUMBUFLEN];
12600 char_u buf2[NUMBUFLEN];
12601 garray_T ga;
12602
12603 in_str = get_tv_string(&argvars[0]);
12604 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
12605 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
12606
12607 /* Default return value: empty string. */
12608 rettv->v_type = VAR_STRING;
12609 rettv->vval.v_string = NULL;
12610 if (fromstr == NULL || tostr == NULL)
12611 return; /* type error; errmsg already given */
12612 ga_init2(&ga, (int)sizeof(char), 80);
12613
12614#ifdef FEAT_MBYTE
12615 if (!has_mbyte)
12616#endif
12617 /* not multi-byte: fromstr and tostr must be the same length */
12618 if (STRLEN(fromstr) != STRLEN(tostr))
12619 {
12620#ifdef FEAT_MBYTE
12621error:
12622#endif
12623 EMSG2(_(e_invarg2), fromstr);
12624 ga_clear(&ga);
12625 return;
12626 }
12627
12628 /* fromstr and tostr have to contain the same number of chars */
12629 while (*in_str != NUL)
12630 {
12631#ifdef FEAT_MBYTE
12632 if (has_mbyte)
12633 {
12634 inlen = (*mb_ptr2len)(in_str);
12635 cpstr = in_str;
12636 cplen = inlen;
12637 idx = 0;
12638 for (p = fromstr; *p != NUL; p += fromlen)
12639 {
12640 fromlen = (*mb_ptr2len)(p);
12641 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
12642 {
12643 for (p = tostr; *p != NUL; p += tolen)
12644 {
12645 tolen = (*mb_ptr2len)(p);
12646 if (idx-- == 0)
12647 {
12648 cplen = tolen;
12649 cpstr = p;
12650 break;
12651 }
12652 }
12653 if (*p == NUL) /* tostr is shorter than fromstr */
12654 goto error;
12655 break;
12656 }
12657 ++idx;
12658 }
12659
12660 if (first && cpstr == in_str)
12661 {
12662 /* Check that fromstr and tostr have the same number of
12663 * (multi-byte) characters. Done only once when a character
12664 * of in_str doesn't appear in fromstr. */
12665 first = FALSE;
12666 for (p = tostr; *p != NUL; p += tolen)
12667 {
12668 tolen = (*mb_ptr2len)(p);
12669 --idx;
12670 }
12671 if (idx != 0)
12672 goto error;
12673 }
12674
12675 (void)ga_grow(&ga, cplen);
12676 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
12677 ga.ga_len += cplen;
12678
12679 in_str += inlen;
12680 }
12681 else
12682#endif
12683 {
12684 /* When not using multi-byte chars we can do it faster. */
12685 p = vim_strchr(fromstr, *in_str);
12686 if (p != NULL)
12687 ga_append(&ga, tostr[p - fromstr]);
12688 else
12689 ga_append(&ga, *in_str);
12690 ++in_str;
12691 }
12692 }
12693
12694 /* add a terminating NUL */
12695 (void)ga_grow(&ga, 1);
12696 ga_append(&ga, NUL);
12697
12698 rettv->vval.v_string = ga.ga_data;
12699}
12700
12701#ifdef FEAT_FLOAT
12702/*
12703 * "trunc({float})" function
12704 */
12705 static void
12706f_trunc(typval_T *argvars, typval_T *rettv)
12707{
12708 float_T f = 0.0;
12709
12710 rettv->v_type = VAR_FLOAT;
12711 if (get_float_arg(argvars, &f) == OK)
12712 /* trunc() is not in C90, use floor() or ceil() instead. */
12713 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
12714 else
12715 rettv->vval.v_float = 0.0;
12716}
12717#endif
12718
12719/*
12720 * "type(expr)" function
12721 */
12722 static void
12723f_type(typval_T *argvars, typval_T *rettv)
12724{
12725 int n = -1;
12726
12727 switch (argvars[0].v_type)
12728 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020012729 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
12730 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012731 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020012732 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
12733 case VAR_LIST: n = VAR_TYPE_LIST; break;
12734 case VAR_DICT: n = VAR_TYPE_DICT; break;
12735 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012736 case VAR_SPECIAL:
12737 if (argvars[0].vval.v_number == VVAL_FALSE
12738 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020012739 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012740 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020012741 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012742 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020012743 case VAR_JOB: n = VAR_TYPE_JOB; break;
12744 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012745 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010012746 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012747 n = -1;
12748 break;
12749 }
12750 rettv->vval.v_number = n;
12751}
12752
12753/*
12754 * "undofile(name)" function
12755 */
12756 static void
12757f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
12758{
12759 rettv->v_type = VAR_STRING;
12760#ifdef FEAT_PERSISTENT_UNDO
12761 {
12762 char_u *fname = get_tv_string(&argvars[0]);
12763
12764 if (*fname == NUL)
12765 {
12766 /* If there is no file name there will be no undo file. */
12767 rettv->vval.v_string = NULL;
12768 }
12769 else
12770 {
12771 char_u *ffname = FullName_save(fname, FALSE);
12772
12773 if (ffname != NULL)
12774 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
12775 vim_free(ffname);
12776 }
12777 }
12778#else
12779 rettv->vval.v_string = NULL;
12780#endif
12781}
12782
12783/*
12784 * "undotree()" function
12785 */
12786 static void
12787f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
12788{
12789 if (rettv_dict_alloc(rettv) == OK)
12790 {
12791 dict_T *dict = rettv->vval.v_dict;
12792 list_T *list;
12793
12794 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
12795 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
12796 dict_add_nr_str(dict, "save_last",
12797 (long)curbuf->b_u_save_nr_last, NULL);
12798 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
12799 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
12800 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
12801
12802 list = list_alloc();
12803 if (list != NULL)
12804 {
12805 u_eval_tree(curbuf->b_u_oldhead, list);
12806 dict_add_list(dict, "entries", list);
12807 }
12808 }
12809}
12810
12811/*
12812 * "values(dict)" function
12813 */
12814 static void
12815f_values(typval_T *argvars, typval_T *rettv)
12816{
12817 dict_list(argvars, rettv, 1);
12818}
12819
12820/*
12821 * "virtcol(string)" function
12822 */
12823 static void
12824f_virtcol(typval_T *argvars, typval_T *rettv)
12825{
12826 colnr_T vcol = 0;
12827 pos_T *fp;
12828 int fnum = curbuf->b_fnum;
12829
12830 fp = var2fpos(&argvars[0], FALSE, &fnum);
12831 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
12832 && fnum == curbuf->b_fnum)
12833 {
12834 getvvcol(curwin, fp, NULL, NULL, &vcol);
12835 ++vcol;
12836 }
12837
12838 rettv->vval.v_number = vcol;
12839}
12840
12841/*
12842 * "visualmode()" function
12843 */
12844 static void
12845f_visualmode(typval_T *argvars, typval_T *rettv)
12846{
12847 char_u str[2];
12848
12849 rettv->v_type = VAR_STRING;
12850 str[0] = curbuf->b_visual_mode_eval;
12851 str[1] = NUL;
12852 rettv->vval.v_string = vim_strsave(str);
12853
12854 /* A non-zero number or non-empty string argument: reset mode. */
12855 if (non_zero_arg(&argvars[0]))
12856 curbuf->b_visual_mode_eval = NUL;
12857}
12858
12859/*
12860 * "wildmenumode()" function
12861 */
12862 static void
12863f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12864{
12865#ifdef FEAT_WILDMENU
12866 if (wild_menu_showing)
12867 rettv->vval.v_number = 1;
12868#endif
12869}
12870
12871/*
12872 * "winbufnr(nr)" function
12873 */
12874 static void
12875f_winbufnr(typval_T *argvars, typval_T *rettv)
12876{
12877 win_T *wp;
12878
12879 wp = find_win_by_nr(&argvars[0], NULL);
12880 if (wp == NULL)
12881 rettv->vval.v_number = -1;
12882 else
12883 rettv->vval.v_number = wp->w_buffer->b_fnum;
12884}
12885
12886/*
12887 * "wincol()" function
12888 */
12889 static void
12890f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
12891{
12892 validate_cursor();
12893 rettv->vval.v_number = curwin->w_wcol + 1;
12894}
12895
12896/*
12897 * "winheight(nr)" function
12898 */
12899 static void
12900f_winheight(typval_T *argvars, typval_T *rettv)
12901{
12902 win_T *wp;
12903
12904 wp = find_win_by_nr(&argvars[0], NULL);
12905 if (wp == NULL)
12906 rettv->vval.v_number = -1;
12907 else
12908 rettv->vval.v_number = wp->w_height;
12909}
12910
12911/*
12912 * "winline()" function
12913 */
12914 static void
12915f_winline(typval_T *argvars UNUSED, typval_T *rettv)
12916{
12917 validate_cursor();
12918 rettv->vval.v_number = curwin->w_wrow + 1;
12919}
12920
12921/*
12922 * "winnr()" function
12923 */
12924 static void
12925f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
12926{
12927 int nr = 1;
12928
12929#ifdef FEAT_WINDOWS
12930 nr = get_winnr(curtab, &argvars[0]);
12931#endif
12932 rettv->vval.v_number = nr;
12933}
12934
12935/*
12936 * "winrestcmd()" function
12937 */
12938 static void
12939f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
12940{
12941#ifdef FEAT_WINDOWS
12942 win_T *wp;
12943 int winnr = 1;
12944 garray_T ga;
12945 char_u buf[50];
12946
12947 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020012948 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012949 {
12950 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
12951 ga_concat(&ga, buf);
12952 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
12953 ga_concat(&ga, buf);
12954 ++winnr;
12955 }
12956 ga_append(&ga, NUL);
12957
12958 rettv->vval.v_string = ga.ga_data;
12959#else
12960 rettv->vval.v_string = NULL;
12961#endif
12962 rettv->v_type = VAR_STRING;
12963}
12964
12965/*
12966 * "winrestview()" function
12967 */
12968 static void
12969f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
12970{
12971 dict_T *dict;
12972
12973 if (argvars[0].v_type != VAR_DICT
12974 || (dict = argvars[0].vval.v_dict) == NULL)
12975 EMSG(_(e_invarg));
12976 else
12977 {
12978 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
12979 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
12980 if (dict_find(dict, (char_u *)"col", -1) != NULL)
12981 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
12982#ifdef FEAT_VIRTUALEDIT
12983 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
12984 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
12985#endif
12986 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
12987 {
12988 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
12989 curwin->w_set_curswant = FALSE;
12990 }
12991
12992 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
12993 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
12994#ifdef FEAT_DIFF
12995 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
12996 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
12997#endif
12998 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
12999 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
13000 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
13001 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
13002
13003 check_cursor();
13004 win_new_height(curwin, curwin->w_height);
13005# ifdef FEAT_WINDOWS
13006 win_new_width(curwin, W_WIDTH(curwin));
13007# endif
13008 changed_window_setting();
13009
13010 if (curwin->w_topline <= 0)
13011 curwin->w_topline = 1;
13012 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
13013 curwin->w_topline = curbuf->b_ml.ml_line_count;
13014#ifdef FEAT_DIFF
13015 check_topfill(curwin, TRUE);
13016#endif
13017 }
13018}
13019
13020/*
13021 * "winsaveview()" function
13022 */
13023 static void
13024f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
13025{
13026 dict_T *dict;
13027
13028 if (rettv_dict_alloc(rettv) == FAIL)
13029 return;
13030 dict = rettv->vval.v_dict;
13031
13032 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
13033 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
13034#ifdef FEAT_VIRTUALEDIT
13035 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
13036#endif
13037 update_curswant();
13038 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
13039
13040 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
13041#ifdef FEAT_DIFF
13042 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
13043#endif
13044 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
13045 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
13046}
13047
13048/*
13049 * "winwidth(nr)" function
13050 */
13051 static void
13052f_winwidth(typval_T *argvars, typval_T *rettv)
13053{
13054 win_T *wp;
13055
13056 wp = find_win_by_nr(&argvars[0], NULL);
13057 if (wp == NULL)
13058 rettv->vval.v_number = -1;
13059 else
13060#ifdef FEAT_WINDOWS
13061 rettv->vval.v_number = wp->w_width;
13062#else
13063 rettv->vval.v_number = Columns;
13064#endif
13065}
13066
13067/*
13068 * "wordcount()" function
13069 */
13070 static void
13071f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
13072{
13073 if (rettv_dict_alloc(rettv) == FAIL)
13074 return;
13075 cursor_pos_info(rettv->vval.v_dict);
13076}
13077
13078/*
13079 * "writefile()" function
13080 */
13081 static void
13082f_writefile(typval_T *argvars, typval_T *rettv)
13083{
13084 int binary = FALSE;
13085 int append = FALSE;
13086 char_u *fname;
13087 FILE *fd;
13088 int ret = 0;
13089
13090 if (check_restricted() || check_secure())
13091 return;
13092
13093 if (argvars[0].v_type != VAR_LIST)
13094 {
13095 EMSG2(_(e_listarg), "writefile()");
13096 return;
13097 }
13098 if (argvars[0].vval.v_list == NULL)
13099 return;
13100
13101 if (argvars[2].v_type != VAR_UNKNOWN)
13102 {
13103 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
13104 binary = TRUE;
13105 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
13106 append = TRUE;
13107 }
13108
13109 /* Always open the file in binary mode, library functions have a mind of
13110 * their own about CR-LF conversion. */
13111 fname = get_tv_string(&argvars[1]);
13112 if (*fname == NUL || (fd = mch_fopen((char *)fname,
13113 append ? APPENDBIN : WRITEBIN)) == NULL)
13114 {
13115 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
13116 ret = -1;
13117 }
13118 else
13119 {
13120 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
13121 ret = -1;
13122 fclose(fd);
13123 }
13124
13125 rettv->vval.v_number = ret;
13126}
13127
13128/*
13129 * "xor(expr, expr)" function
13130 */
13131 static void
13132f_xor(typval_T *argvars, typval_T *rettv)
13133{
13134 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
13135 ^ get_tv_number_chk(&argvars[1], NULL);
13136}
13137
13138
13139#endif /* FEAT_EVAL */