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