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