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